perf_event: Fix incorrect range check on cpu number
PATCH 1/2 perf_event: Fix incorrect range check on cpu number
It is quite legitimate for CPUs to be numbered sparsely, meaning that it possible for an online CPU to have a number which is greater than the total count of possible CPUs.
Currently find_get_context() has a sanity check on the cpu number where it checks it against num_possible_cpus(). This test can fail for a legitimate cpu number if the cpu_possible_mask is sparsely populated.
This fixes the problem by checking the CPU number against nr_cpumask_bits instead, since that is the appropriate check to ensure that the cpu number is same to pass to cpu_isset() subsequently.
--- kernel/perf_event.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/perf_event.c +1 -1
@@ -1604,7 +1604,7 @@ static struct perf_event_context *find_get_context(pid_t pid, int cpu)if (perf_paranoid_cpu () && !capable (CAP_SYS_ADMIN ))return ERR_PTR (-EACCES ); -if (cpu <0 || cpu >num_possible_cpus ()) +if (cpu <0 || cpu >= nr_cpumask_bits)return ERR_PTR (-EINVAL );/*
Quoted 4 lines · Paul Mackerras
It is quite legitimate for CPUs to be numbered sparsely, meaning that it possible for an online CPU to have a number which is greater than the total count of possible CPUs.
Quoted 8 lines
Currently
find_get_context()has a sanity check on the cpu number where it checks it againstnum_possible_cpus(). This test can fail for a legitimate cpu number if thecpu_possible_maskis sparsely populated.This fixes the problem by checking the CPU number against
nr_cpumask_bitsinstead, since that is the appropriate check to ensure that the cpu number is same to pass tocpu_isset()subsequently.
Cute, do you actually have hardware that does this?
Quoted 21 lines
Reported-by Michael Neuling <[email protected]>Tested-by Michael Neuling <[email protected]>Signed-off-by Paul Mackerras <[email protected]>--- kernel/perf_event.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/perf_event.c +1
-1@@ -1604,7 +1604,7 @@ static struct perf_event_context *find_get_context(pid_t pid, int cpu)if (perf_paranoid_cpu () && !capable (CAP_SYS_ADMIN ))return ERR_PTR (-EACCES ); -if (cpu <0 || cpu >num_possible_cpus ()) +if (cpu <0 || cpu >= nr_cpumask_bits)return ERR_PTR (-EINVAL );/*
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Paul Mackerras
It is quite legitimate for CPUs to be numbered sparsely, meaning that it possible for an online CPU to have a number which is greater than the total count of possible CPUs.
Currently
find_get_context()has a sanity check on the cpu number where it checks it againstnum_possible_cpus(). This test can fail for a legitimate cpu number if thecpu_possible_maskis sparsely populated.
Quoted 5 lines
Quoted 3 lines
This fixes the problem by checking the CPU number against
nr_cpumask_bitsinstead, since that is the appropriate check to ensure that the cpu number is same to pass tocpu_isset()subsequently.Cute, do you actually have hardware that does this?
Yeah, Mikey ran across this on a POWER7 box here.
Paul.
Quoted 12 lines · Paul Mackerras
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Paul Mackerras
It is quite legitimate for CPUs to be numbered sparsely, meaning that it possible for an online CPU to have a number which is greater than the total count of possible CPUs.
Currently
find_get_context()has a sanity check on the cpu number where it checks it againstnum_possible_cpus(). This test can fail for a legitimate cpu number if thecpu_possible_maskis sparsely populated.
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
This fixes the problem by checking the CPU number against
nr_cpumask_bitsinstead, since that is the appropriate check to ensure that the cpu number is same to pass tocpu_isset()subsequently.Cute, do you actually have hardware that does this?
Yeah, Mikey ran across this on a POWER7 box here.
Does the perf tool need to be fixed too? The "perf stat" tool, at least, has a "-a" switch that tells the tool to count the event on all cpus, and it does this by iterating over the number of cpus, 0..n, assuming they are all contiguous.
- Corey
Quoted 17 lines · Corey Ashford
Quoted 15 lines · Paul Mackerras
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Paul Mackerras
It is quite legitimate for CPUs to be numbered sparsely, meaning that it possible for an online CPU to have a number which is greater than the total count of possible CPUs.
Currently
find_get_context()has a sanity check on the cpu number where it checks it againstnum_possible_cpus(). This test can fail for a legitimate cpu number if thecpu_possible_maskis sparsely populated.This fixes the problem by checking the CPU number against
nr_cpumask_bitsinstead, since that is the appropriate check to ensure that the cpu number is same to pass tocpu_isset()subsequently.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Cute, do you actually have hardware that does this?
Yeah, Mikey ran across this on a POWER7 box here.
Does the perf tool need to be fixed too? The "perf stat" tool, at least, has a "-a" switch that tells the tool to count the event on all cpus, and it does this by iterating over the number of cpus, 0..n, assuming they are all contiguous.
Yes, see patch 2/2 of this series.
Ingo
Quoted 20 lines · Ingo Molnar
Quoted 18 lines · Corey Ashford
Quoted 17 lines · Paul Mackerras
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Paul Mackerras
It is quite legitimate for CPUs to be numbered sparsely, meaning that it possible for an online CPU to have a number which is greater than the total count of possible CPUs.
Currently
find_get_context()has a sanity check on the cpu number where it checks it againstnum_possible_cpus(). This test can fail for a legitimate cpu number if thecpu_possible_maskis sparsely populated.Quoted 4 lines
Quoted 3 lines
This fixes the problem by checking the CPU number against
nr_cpumask_bitsinstead, since that is the appropriate check to ensure that the cpu number is same to pass tocpu_isset()subsequently.Cute, do you actually have hardware that does this?
Yeah, Mikey ran across this on a POWER7 box here.
Quoted 8 lines
Quoted 4 lines
Does the perf tool need to be fixed too? The "perf stat" tool, at least, has a "-a" switch that tells the tool to count the event on all cpus, and it does this by iterating over the number of cpus, 0..n, assuming they are all contiguous.
Yes, see patch 2/2 of this series.
Ingo
Oops! missed that. Thanks!
- Corey
Commit-ID: 0f624e7e5625f4c30c836b7a5decfe2553582391 Gitweb: http://git.kernel.org/tip/0f624e7e5625f4c30c836b7a5decfe2553582391 Author: Paul Mackerras <[email protected]> AuthorDate: Tue, 15 Dec 2009 19:40:32 +1100 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 15 Dec 2009 13:09:55 +0100
perf_event: Fix incorrect range check on cpu number
It is quite legitimate for CPUs to be numbered sparsely, meaning that it possible for an online CPU to have a number which is greater than the total count of possible CPUs.
Currently find_get_context() has a sanity check on the cpu number where it checks it against num_possible_cpus(). This test can fail for a legitimate cpu number if the cpu_possible_mask is sparsely populated.
This fixes the problem by checking the CPU number against nr_cpumask_bits instead, since that is the appropriate check to ensure that the cpu number is same to pass to cpu_isset() subsequently.
LKML-Reference: <[email protected]>
--- kernel/perf_event.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/perf_event.c +1 -1
@@ -1603,7 +1603,7 @@ static struct perf_event_context *find_get_context(pid_t pid, int cpu)if (perf_paranoid_cpu () && !capable (CAP_SYS_ADMIN ))return ERR_PTR (-EACCES ); -if (cpu <0 || cpu >num_possible_cpus ()) +if (cpu <0 || cpu >= nr_cpumask_bits)return ERR_PTR (-EINVAL );/*