powerpc/perf: Fix IMC allocation routine
PATCH 1/1 powerpc/perf: Fix IMC allocation routine
When setting nr_cpus=1, we observed a crash in IMC code during boot due to a missing allocation: basically, IMC code is taking the number of threads into account in imc_mem_init() and if we manually set nr_cpus for a value that is not multiple of the number of threads per core, an integer division in that function will discard the decimal portion, leading IMC to not allocate one mem_info struct. This causes a NULL pointer dereference later, on is_core_imc_mem_inited().
This patch just rounds that division up, fixing the bug.
---
Anju, looks good to you? Tested in P9 with latest FW available.
arch/powerpc/perf/imc-pmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/perf/imc-pmu.c +2 -2
@@ -1104,7 +1104,7 @@ static int init_nest_pmu_ref(void)static void cleanup_all_core_imc_memory (void ) { -int i, nr_cores =num_present_cpus () / threads_per_core; +int i, nr_cores =DIV_ROUND_UP (num_present_cpus (), threads_per_core);struct imc_mem_info *ptr = core_imc_pmu->mem_info;int size = core_imc_pmu->counter_mem_size;
@@ -1212,7 +1212,7 @@ static int imc_mem_init(struct imc_pmu *pmu_ptr, struct device_node *parent,if (!pmu_ptr->pmu.name)return -ENOMEM ; - nr_cores =num_present_cpus () / threads_per_core; + nr_cores =DIV_ROUND_UP (num_present_cpus (), threads_per_core); pmu_ptr->mem_info =kcalloc (nr_cores,sizeof (struct imc_mem_info ),GFP_KERNEL );
Signature · 1 line
2.14.2
Hi Guilherme,
Nice catch.
Quoted 11 lines · Guilherme G. Piccoli
When setting nr_cpus=1, we observed a crash in IMC code during boot due to a missing allocation: basically, IMC code is taking the number of threads into account in
imc_mem_init()and if we manually setnr_cpusfor a value that is not multiple of the number of threads per core, an integer division in that function will discard the decimal portion, leading IMC to not allocate onemem_infostruct. This causes a NULL pointer dereference later, onis_core_imc_mem_inited().This patch just rounds that division up, fixing the bug.
Signed-off-by Guilherme G. Piccoli <[email protected]>
Quoted 20 lines
---
Anju, looks good to you? Tested in P9 with latest FW available.arch/powerpc/perf/imc-pmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/powerpc/perf/imc-pmu.c +1
-1@@ -1104,7 +1104,7 @@ static int init_nest_pmu_ref(void)static void cleanup_all_core_imc_memory (void ) { -int i, nr_cores =num_present_cpus () / threads_per_core; +int i, nr_cores =DIV_ROUND_UP (num_present_cpus (), threads_per_core);struct imc_mem_info *ptr = core_imc_pmu->mem_info;int size = core_imc_pmu->counter_mem_size;
arch/powerpc/perf/imc-pmu.c static int imc_mem_init(struct imc_pmu *pmu_ptr, struct device_node *parent, · line 1218
Quoted 8 lines
arch/powerpc/perf/imc-pmu.c +1
-1@@ -1212,7 +1212,7 @@ static int imc_mem_init(struct imc_pmu *pmu_ptr, struct device_node *parent,if (!pmu_ptr->pmu.name)return -ENOMEM ; - nr_cores =num_present_cpus () / threads_per_core; + nr_cores =DIV_ROUND_UP (num_present_cpus (), threads_per_core); pmu_ptr->mem_info =kcalloc (nr_cores,sizeof (struct imc_mem_info ),GFP_KERNEL );
Thanks,
Anju
Quoted 13 lines · Anju T Sudhakar
Hi Guilherme,
Nice catch.
Quoted 8 lines · Guilherme G. Piccoli
When setting nr_cpus=1, we observed a crash in IMC code during boot due to a missing allocation: basically, IMC code is taking the number of threads into account in
imc_mem_init()and if we manually setnr_cpusfor a value that is not multiple of the number of threads per core, an integer division in that function will discard the decimal portion, leading IMC to not allocate onemem_infostruct. This causes a NULL pointer dereference later, onis_core_imc_mem_inited().
Quoted 4 lines
Quoted 3 lines
This patch just rounds that division up, fixing the bug.
Signed-off-by Guilherme G. Piccoli <[email protected]>Acked-by Anju T Sudhakar <[email protected]>
Thanks Anju :)
Quoted 31 lines
Quoted 20 lines
---
Anju, looks good to you? Tested in P9 with latest FW available.arch/powerpc/perf/imc-pmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/powerpc/perf/imc-pmu.c +1
-1@@ -1104,7 +1104,7 @@ static int init_nest_pmu_ref(void)static void cleanup_all_core_imc_memory (void ) { -int i, nr_cores =num_present_cpus () / threads_per_core; +int i, nr_cores =DIV_ROUND_UP (num_present_cpus (), threads_per_core);struct imc_mem_info *ptr = core_imc_pmu->mem_info;int size = core_imc_pmu->counter_mem_size;arch/powerpc/perf/imc-pmu.c static int imc_mem_init(struct imc_pmu *pmu_ptr, struct device_node *parent, · line 1218
Quoted 8 lines
arch/powerpc/perf/imc-pmu.c +1
-1@@ -1212,7 +1212,7 @@ static int imc_mem_init(struct imc_pmu *pmu_ptr, struct device_node *parent,if (!pmu_ptr->pmu.name)return -ENOMEM ; - nr_cores =num_present_cpus () / threads_per_core; + nr_cores =DIV_ROUND_UP (num_present_cpus (), threads_per_core); pmu_ptr->mem_info =kcalloc (nr_cores,sizeof (struct imc_mem_info ),GFP_KERNEL );Thanks,
Anju
Michael Ellerman
AppliedQuoted 11 lines · Guilherme G. Piccoli
When setting nr_cpus=1, we observed a crash in IMC code during boot due to a missing allocation: basically, IMC code is taking the number of threads into account in
imc_mem_init()and if we manually setnr_cpusfor a value that is not multiple of the number of threads per core, an integer division in that function will discard the decimal portion, leading IMC to not allocate onemem_infostruct. This causes a NULL pointer dereference later, onis_core_imc_mem_inited().This patch just rounds that division up, fixing the bug.
Signed-off-by Guilherme G. Piccoli <[email protected]>
Applied to powerpc fixes, thanks.
https://git.kernel.org/powerpc/c/0b167f11085a34281349f395d20005
cheers