From: Sandipan Das <hidden> Date: 2020-06-09 07:47:55
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().
Fixes: 3752e453f6ba ("selftests/powerpc: Add tests of PMU EBBs")
Reported-by: Shirisha Ganta <redacted>
Signed-off-by: Sandipan Das <redacted>
Reviewed-by: Kamalesh Babulal <redacted>
---
Previous versions can be found at:
v1: https://lore.kernel.org/linuxppc-dev/20200608144212.985144-1-sandipan@linux.ibm.com/
Changes in v2:
- Added NULL check for the affinity mask as suggested by Kamalesh.
- Changed "cpu set" to "CPU affinity mask" in the commit message.
---
tools/testing/selftests/powerpc/utils.c | 37 +++++++++++++++++--------
1 file changed, 25 insertions(+), 12 deletions(-)
@@ -88,28 +89,40 @@ void *get_auxv_entry(int type)intpick_online_cpu(void){-cpu_set_tmask;-intcpu;+intncpus,cpu=-1;+cpu_set_t*mask;+size_tsize;++ncpus=get_nprocs();+size=CPU_ALLOC_SIZE(ncpus);+mask=CPU_ALLOC(ncpus);+if(!mask){+perror("malloc");+return-1;+}-CPU_ZERO(&mask);+CPU_ZERO_S(size,mask);-if(sched_getaffinity(0,sizeof(mask),&mask)){+if(sched_getaffinity(0,size,mask)){perror("sched_getaffinity");-return-1;+gotodone;}/* We prefer a primary thread, but skip 0 */-for(cpu=8;cpu<CPU_SETSIZE;cpu+=8)-if(CPU_ISSET(cpu,&mask))-returncpu;+for(cpu=8;cpu<ncpus;cpu+=8)+if(CPU_ISSET_S(cpu,size,mask))+gotodone;/* Search for anything, but in reverse */-for(cpu=CPU_SETSIZE-1;cpu>=0;cpu--)-if(CPU_ISSET(cpu,&mask))-returncpu;+for(cpu=ncpus-1;cpu>=0;cpu--)+if(CPU_ISSET_S(cpu,size,mask))+gotodone;printf("No cpus in affinity mask?!\n");-return-1;++done:+CPU_FREE(mask);+returncpu;}boolis_ppc64le(void)
From: Michael Ellerman <mpe@ellerman.id.au> Date: 2020-07-29 13:50:19
Sandipan Das [off-list ref] writes:
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().
Fixes: 3752e453f6ba ("selftests/powerpc: Add tests of PMU EBBs")
Reported-by: Shirisha Ganta <redacted>
Signed-off-by: Sandipan Das <redacted>
Reviewed-by: Kamalesh Babulal <redacted>
---
Previous versions can be found at:
v1: https://lore.kernel.org/linuxppc-dev/20200608144212.985144-1-sandipan@linux.ibm.com/
Changes in v2:
- Added NULL check for the affinity mask as suggested by Kamalesh.
- Changed "cpu set" to "CPU affinity mask" in the commit message.
* Sandipan Das [off-list ref] [2020-06-09 13:07:33]:
quoted hunk
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().
Fixes: 3752e453f6ba ("selftests/powerpc: Add tests of PMU EBBs")
Reported-by: Shirisha Ganta <redacted>
Signed-off-by: Sandipan Das <redacted>
Reviewed-by: Kamalesh Babulal <redacted>
---
Previous versions can be found at:
v1: https://lore.kernel.org/linuxppc-dev/20200608144212.985144-1-sandipan@linux.ibm.com/
@@ -88,28 +89,40 @@ void *get_auxv_entry(int type) int pick_online_cpu(void) {- cpu_set_t mask;- int cpu;+ int ncpus, cpu = -1;+ cpu_set_t *mask;+ size_t size;++ ncpus = get_nprocs();
Please use get_nprocs_conf or sysconf(_SC_NPROCESSORS_CONF). The manpage
seems to suggest the latter. Not sure how accurate the manpage is.
get_nprocs is returning online cpus and when smt is off, the cpu numbers
would be sparse and hence the result from get_nprocs wouldn't be ideal for
allocating cpumask. However get_nprocs_conf would return the max configured
cpus and would be able to handle it.
I think this was the same situation hit by Michael Ellerman.
From: Sandipan Das <hidden> Date: 2020-07-30 05:04:53
Hi Srikar, Michael,
On 29/07/20 9:33 pm, Srikar Dronamraju wrote:
* Sandipan Das [off-list ref] [2020-06-09 13:07:33]:
quoted
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().
Fixes: 3752e453f6ba ("selftests/powerpc: Add tests of PMU EBBs")
Reported-by: Shirisha Ganta <redacted>
Signed-off-by: Sandipan Das <redacted>
Reviewed-by: Kamalesh Babulal <redacted>
---
Previous versions can be found at:
v1: https://lore.kernel.org/linuxppc-dev/20200608144212.985144-1-sandipan@linux.ibm.com/
@@ -88,28 +89,40 @@ void *get_auxv_entry(int type) int pick_online_cpu(void) {- cpu_set_t mask;- int cpu;+ int ncpus, cpu = -1;+ cpu_set_t *mask;+ size_t size;++ ncpus = get_nprocs();
Please use get_nprocs_conf or sysconf(_SC_NPROCESSORS_CONF). The manpage
seems to suggest the latter. Not sure how accurate the manpage is.
get_nprocs is returning online cpus and when smt is off, the cpu numbers
would be sparse and hence the result from get_nprocs wouldn't be ideal for
allocating cpumask. However get_nprocs_conf would return the max configured
cpus and would be able to handle it.
I think this was the same situation hit by Michael Ellerman.
Yes, that seems to be the case. Thanks for testing this.
Will fix this in v3.
- Sandipan