| From 1fe3e0d89f9a261fdcdbf15642daf82059708920 Mon Sep 17 00:00:00 2001 |
| From: Harish <harish@linux.ibm.com> |
| Date: Tue, 9 Jun 2020 13:44:23 +0530 |
| Subject: [PATCH] selftests/powerpc: Fix CPU affinity for child process |
| |
| commit 854eb5022be04f81e318765f089f41a57c8e5d83 upstream. |
| |
| On systems with large number of cpus, test fails trying to set |
| affinity by calling sched_setaffinity() with smaller size for affinity |
| mask. This patch fixes it by making sure that the size of allocated |
| affinity mask is dependent on the number of CPUs as reported by |
| get_nprocs(). |
| |
| Fixes: 00b7ec5c9cf3 ("selftests/powerpc: Import Anton's context_switch2 benchmark") |
| Reported-by: Shirisha Ganta <shiganta@in.ibm.com> |
| Signed-off-by: Sandipan Das <sandipan@linux.ibm.com> |
| Signed-off-by: Harish <harish@linux.ibm.com> |
| Reviewed-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com> |
| Reviewed-by: Satheesh Rajendran <sathnaga@linux.vnet.ibm.com> |
| Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> |
| Link: https://lore.kernel.org/r/20200609081423.529664-1-harish@linux.ibm.com |
| Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com> |
| |
| diff --git a/tools/testing/selftests/powerpc/benchmarks/context_switch.c b/tools/testing/selftests/powerpc/benchmarks/context_switch.c |
| index a2e8c9da7fa5..d50cc05df495 100644 |
| --- a/tools/testing/selftests/powerpc/benchmarks/context_switch.c |
| +++ b/tools/testing/selftests/powerpc/benchmarks/context_switch.c |
| @@ -19,6 +19,7 @@ |
| #include <limits.h> |
| #include <sys/time.h> |
| #include <sys/syscall.h> |
| +#include <sys/sysinfo.h> |
| #include <sys/types.h> |
| #include <sys/shm.h> |
| #include <linux/futex.h> |
| @@ -104,8 +105,9 @@ static void start_thread_on(void *(*fn)(void *), void *arg, unsigned long cpu) |
| |
| static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu) |
| { |
| - int pid; |
| - cpu_set_t cpuset; |
| + int pid, ncpus; |
| + cpu_set_t *cpuset; |
| + size_t size; |
| |
| pid = fork(); |
| if (pid == -1) { |
| @@ -116,14 +118,23 @@ static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu) |
| if (pid) |
| return; |
| |
| - CPU_ZERO(&cpuset); |
| - CPU_SET(cpu, &cpuset); |
| + ncpus = get_nprocs(); |
| + size = CPU_ALLOC_SIZE(ncpus); |
| + cpuset = CPU_ALLOC(ncpus); |
| + if (!cpuset) { |
| + perror("malloc"); |
| + exit(1); |
| + } |
| + CPU_ZERO_S(size, cpuset); |
| + CPU_SET_S(cpu, size, cpuset); |
| |
| - if (sched_setaffinity(0, sizeof(cpuset), &cpuset)) { |
| + if (sched_setaffinity(0, size, cpuset)) { |
| perror("sched_setaffinity"); |
| + CPU_FREE(cpuset); |
| exit(1); |
| } |
| |
| + CPU_FREE(cpuset); |
| fn(arg); |
| |
| exit(0); |
| -- |
| 2.27.0 |
| |