Skip to content

Commit dfa03ff

Browse files
sandip4nmpe
authored andcommitted
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: 3752e45 ("selftests/powerpc: Add tests of PMU EBBs") Reported-by: Shirisha Ganta <[email protected]> Signed-off-by: Sandipan Das <[email protected]> Reviewed-by: Kamalesh Babulal <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/a408c4b8e9a23bb39b539417a21eb0ff47bb5127.1596084858.git.sandipan@linux.ibm.com
1 parent d3a133a commit dfa03ff

File tree

1 file changed

+25
-12
lines changed
  • tools/testing/selftests/powerpc

1 file changed

+25
-12
lines changed

tools/testing/selftests/powerpc/utils.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <string.h>
1717
#include <sys/ioctl.h>
1818
#include <sys/stat.h>
19+
#include <sys/sysinfo.h>
1920
#include <sys/types.h>
2021
#include <sys/utsname.h>
2122
#include <unistd.h>
@@ -88,28 +89,40 @@ void *get_auxv_entry(int type)
8889

8990
int pick_online_cpu(void)
9091
{
91-
cpu_set_t mask;
92-
int cpu;
92+
int ncpus, cpu = -1;
93+
cpu_set_t *mask;
94+
size_t size;
95+
96+
ncpus = get_nprocs_conf();
97+
size = CPU_ALLOC_SIZE(ncpus);
98+
mask = CPU_ALLOC(ncpus);
99+
if (!mask) {
100+
perror("malloc");
101+
return -1;
102+
}
93103

94-
CPU_ZERO(&mask);
104+
CPU_ZERO_S(size, mask);
95105

96-
if (sched_getaffinity(0, sizeof(mask), &mask)) {
106+
if (sched_getaffinity(0, size, mask)) {
97107
perror("sched_getaffinity");
98-
return -1;
108+
goto done;
99109
}
100110

101111
/* We prefer a primary thread, but skip 0 */
102-
for (cpu = 8; cpu < CPU_SETSIZE; cpu += 8)
103-
if (CPU_ISSET(cpu, &mask))
104-
return cpu;
112+
for (cpu = 8; cpu < ncpus; cpu += 8)
113+
if (CPU_ISSET_S(cpu, size, mask))
114+
goto done;
105115

106116
/* Search for anything, but in reverse */
107-
for (cpu = CPU_SETSIZE - 1; cpu >= 0; cpu--)
108-
if (CPU_ISSET(cpu, &mask))
109-
return cpu;
117+
for (cpu = ncpus - 1; cpu >= 0; cpu--)
118+
if (CPU_ISSET_S(cpu, size, mask))
119+
goto done;
110120

111121
printf("No cpus in affinity mask?!\n");
112-
return -1;
122+
123+
done:
124+
CPU_FREE(mask);
125+
return cpu;
113126
}
114127

115128
bool is_ppc64le(void)

0 commit comments

Comments
 (0)