Commit 854eb502 authored by Harish's avatar Harish Committed by Michael Ellerman

selftests/powerpc: Fix CPU affinity for child process

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: 00b7ec5c ("selftests/powerpc: Import Anton's context_switch2 benchmark")
Reported-by: default avatarShirisha Ganta <shiganta@in.ibm.com>
Signed-off-by: default avatarSandipan Das <sandipan@linux.ibm.com>
Signed-off-by: default avatarHarish <harish@linux.ibm.com>
Reviewed-by: default avatarKamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Reviewed-by: default avatarSatheesh Rajendran <sathnaga@linux.vnet.ibm.com>
Signed-off-by: default avatarMichael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20200609081423.529664-1-harish@linux.ibm.com
parent cf1ae052
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <limits.h> #include <limits.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/sysinfo.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/shm.h> #include <sys/shm.h>
#include <linux/futex.h> #include <linux/futex.h>
...@@ -104,8 +105,9 @@ static void start_thread_on(void *(*fn)(void *), void *arg, unsigned long cpu) ...@@ -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) static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
{ {
int pid; int pid, ncpus;
cpu_set_t cpuset; cpu_set_t *cpuset;
size_t size;
pid = fork(); pid = fork();
if (pid == -1) { if (pid == -1) {
...@@ -116,14 +118,23 @@ static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu) ...@@ -116,14 +118,23 @@ static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
if (pid) if (pid)
return; return;
CPU_ZERO(&cpuset); ncpus = get_nprocs();
CPU_SET(cpu, &cpuset); 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"); perror("sched_setaffinity");
CPU_FREE(cpuset);
exit(1); exit(1);
} }
CPU_FREE(cpuset);
fn(arg); fn(arg);
exit(0); exit(0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment