Re: [PATCH V8 20/44] mm/pkeys: Add PKS test for context switching

From: Edgecombe, Rick P
Date: Tue Feb 01 2022 - 12:43:28 EST


On Thu, 2022-01-27 at 09:54 -0800, ira.weiny@xxxxxxxxx wrote:
> +int check_context_switch(int cpu)
> +{
> + int switch_done[2];
> + int setup_done[2];
> + cpu_set_t cpuset;
> + char result[32];
> + int rc = 0;
> + pid_t pid;
> + int fd;
> +
> + CPU_ZERO(&cpuset);
> + CPU_SET(cpu, &cpuset);
> + /*
> + * Ensure the two processes run on the same CPU so that they
> go through
> + * a context switch.
> + */
> + sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset);
> +
> + if (pipe(setup_done)) {
> + printf("ERROR: Failed to create pipe\n");
> + return -1;
> + }
> + if (pipe(switch_done)) {
> + printf("ERROR: Failed to create pipe\n");
> + return -1;
> + }
> +
> + pid = fork();
> + if (pid == 0) {
> + char done = 'y';
> +
> + fd = open(PKS_TEST_FILE, O_RDWR);
> + if (fd < 0) {
> + printf("ERROR: cannot open %s\n",
> PKS_TEST_FILE);
> + return -1;

When this happens, the error is printed, but the parent process just
hangs forever. Might make it hard to script running all the selftests.

Also, the other x86 selftests mostly use [RUN], [INFO], [OK], [FAIL],
[SKIP] and [OK] in their print statements. Probably should stick to the
pattern across all the print statements. This is probably a "[SKIP]".
Just realized I've omitted the "[]" in the CET series too.

> + }
> +
> + cpu = sched_getcpu();
> + printf("Child running on cpu %d...\n", cpu);
> +
> + /* Allocate and run test. */
> + write(fd, RUN_SINGLE, 1);
> +
> + /* Arm for context switch test */
> + write(fd, ARM_CTX_SWITCH, 1);
> +
> + printf(" tell parent to go\n");
> + write(setup_done[1], &done, sizeof(done));
> +
> + /* Context switch out... */
> + printf(" Waiting for parent...\n");
> + read(switch_done[0], &done, sizeof(done));
> +
> + /* Check msr restored */
> + printf("Checking result\n");
> + write(fd, CHECK_CTX_SWITCH, 1);
> +
> + read(fd, result, 10);
> + printf(" #PF, context switch, pkey allocation and
> free tests: %s\n", result);
> + if (!strncmp(result, "PASS", 10)) {
> + rc = -1;
> + done = 'F';
> + }
> +
> + /* Signal result */
> + write(setup_done[1], &done, sizeof(done));
> + } else {
> + char done = 'y';
> +
> + read(setup_done[0], &done, sizeof(done));
> + cpu = sched_getcpu();
> + printf("Parent running on cpu %d\n", cpu);
> +
> + fd = open(PKS_TEST_FILE, O_RDWR);
> + if (fd < 0) {
> + printf("ERROR: cannot open %s\n",
> PKS_TEST_FILE);
> + return -1;
> + }
> +
> + /* run test with the same pkey */
> + write(fd, RUN_SINGLE, 1);
> +
> + printf(" Signaling child.\n");
> + write(switch_done[1], &done, sizeof(done));
> +
> + /* Wait for result */
> + read(setup_done[0], &done, sizeof(done));
> + if (done == 'F')
> + rc = -1;
> + }