selftests/powerpc: Fix online CPU selection

The size of the CPU affinity mask must be large enough for
systems with a very large number of CPUs. Otherwise, tests
which try to determine the first online CPU by calling
sched_getaffinity() will fail. This makes sure that the size
of the allocated affinity mask is dependent on the number of
CPUs as reported by get_nprocs_conf().

Fixes: 3752e453f6 ("selftests/powerpc: Add tests of PMU EBBs")
Reported-by: Shirisha Ganta <shiganta@in.ibm.com>
Signed-off-by: Sandipan Das <sandipan@linux.ibm.com>
Reviewed-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/a408c4b8e9a23bb39b539417a21eb0ff47bb5127.1596084858.git.sandipan@linux.ibm.com
This commit is contained in:
Sandipan Das 2020-07-30 10:38:46 +05:30 committed by Michael Ellerman
parent d3a133aa0e
commit dfa03fff86
1 changed files with 26 additions and 13 deletions

View File

@ -16,6 +16,7 @@
#include <string.h> #include <string.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/sysinfo.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <unistd.h> #include <unistd.h>
@ -88,28 +89,40 @@ void *get_auxv_entry(int type)
int pick_online_cpu(void) int pick_online_cpu(void)
{ {
cpu_set_t mask; int ncpus, cpu = -1;
int cpu; cpu_set_t *mask;
size_t size;
CPU_ZERO(&mask); ncpus = get_nprocs_conf();
size = CPU_ALLOC_SIZE(ncpus);
if (sched_getaffinity(0, sizeof(mask), &mask)) { mask = CPU_ALLOC(ncpus);
perror("sched_getaffinity"); if (!mask) {
perror("malloc");
return -1; return -1;
} }
CPU_ZERO_S(size, mask);
if (sched_getaffinity(0, size, mask)) {
perror("sched_getaffinity");
goto done;
}
/* We prefer a primary thread, but skip 0 */ /* We prefer a primary thread, but skip 0 */
for (cpu = 8; cpu < CPU_SETSIZE; cpu += 8) for (cpu = 8; cpu < ncpus; cpu += 8)
if (CPU_ISSET(cpu, &mask)) if (CPU_ISSET_S(cpu, size, mask))
return cpu; goto done;
/* Search for anything, but in reverse */ /* Search for anything, but in reverse */
for (cpu = CPU_SETSIZE - 1; cpu >= 0; cpu--) for (cpu = ncpus - 1; cpu >= 0; cpu--)
if (CPU_ISSET(cpu, &mask)) if (CPU_ISSET_S(cpu, size, mask))
return cpu; goto done;
printf("No cpus in affinity mask?!\n"); printf("No cpus in affinity mask?!\n");
return -1;
done:
CPU_FREE(mask);
return cpu;
} }
bool is_ppc64le(void) bool is_ppc64le(void)