drivers: perf: Fix smp_processor_id() use in preemptible code
Subsystems (3)
PATCH 1/1 drivers: perf: Fix smp_processor_id() use in preemptible code
As reported in [1], the use of smp_processor_id() in pmu_sbi_device_probe() must be protected by disabling the preemption, so simple use get_cpu()/put_cpu() instead.
--- drivers/perf/riscv_pmu_sbi.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/perf/riscv_pmu_sbi.c +6 -1
@@ -1373,11 +1373,15 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)/* SBI PMU Snapsphot is only available in SBI v2.0 */ if (sbi_v2_available) { +int cpu; + ret =pmu_sbi_snapshot_alloc (pmu);if (ret) goto out_unregister; - ret =pmu_sbi_snapshot_setup (pmu,smp_processor_id ()); + cpu =get_cpu (); + + ret =pmu_sbi_snapshot_setup (pmu, cpu);if (ret) {/* Snapshot is an optional feature. Continue if not available */ pmu_sbi_snapshot_free (pmu);
@@ -1391,6 +1395,7 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)*/ static_branch_enable (&sbi_pmu_snapshot_available); } +put_cpu (); }register_sysctl ("kernel" , sbi_pmu_sysctl_table);
Signature · 1 line
2.39.2
Quoted 8 lines · Alexandre Ghiti
As reported in [1], the use of
smp_processor_id()inpmu_sbi_device_probe()must be protected by disabling the preemption, so simple use get_cpu()/put_cpu() instead.Reported-by Nam Cao <[email protected]>Signed-off-by Alexandre Ghiti <[email protected]>
LGTM.
Regards,
Anup
Quoted 36 lines
--- drivers/perf/riscv_pmu_sbi.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)drivers/perf/riscv_pmu_sbi.c +6
-2@@ -1373,11 +1373,15 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)/* SBI PMU Snapsphot is only available in SBI v2.0 */ if (sbi_v2_available) { +int cpu; + ret =pmu_sbi_snapshot_alloc (pmu);if (ret) goto out_unregister; - ret =pmu_sbi_snapshot_setup (pmu,smp_processor_id ()); + cpu =get_cpu (); + + ret =pmu_sbi_snapshot_setup (pmu, cpu);if (ret) {/* Snapshot is an optional feature. Continue if not available */ pmu_sbi_snapshot_free (pmu);@@ -1391,6 +1395,7 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)*/ static_branch_enable (&sbi_pmu_snapshot_available); } +put_cpu (); }register_sysctl ("kernel" , sbi_pmu_sysctl_table); --2.39.2
Quoted 7 lines · Alexandre Ghiti
As reported in [1], the use of
smp_processor_id()inpmu_sbi_device_probe()must be protected by disabling the preemption, so simple use get_cpu()/put_cpu() instead.Reported-by Nam Cao <[email protected]>Signed-off-by Alexandre Ghiti <[email protected]>
I think this also needs:
Best regards,
Nam
Alexandre Ghiti
reviewerQuoted 5 lines · Nam Cao
Quoted 4 lines · Alexandre Ghiti
As reported in [1], the use of
smp_processor_id()inpmu_sbi_device_probe()must be protected by disabling the preemption, so simple use get_cpu()/put_cpu() instead.
Quoted 7 lines
Quoted 3 lines
Reported-by Nam Cao <[email protected]>Signed-off-by Alexandre Ghiti <[email protected]>Tested-by Nam Cao <[email protected]>I think this also needs:
Fixes a8625217a054 ("drivers/perf: riscv: Implement SBI PMU snapshot function")
Indeed, thanks for the tag!
Quoted 8 lines
Best regards,
Nam_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 3 lines · Alexandre Ghiti
As reported in [1], the use of
smp_processor_id()inpmu_sbi_device_probe()must be protected by disabling the preemption, so simple use get_cpu()/put_cpu() instead.
simply
Quoted 1 line
Reported-by Nam Cao <[email protected]>
Quoted 2 lines
Signed-off-by Alexandre Ghiti <[email protected]>
Andrea
Hi Andrea,
Quoted 6 lines · Andrea Parri
Quoted 3 lines · Alexandre Ghiti
As reported in [1], the use of
smp_processor_id()inpmu_sbi_device_probe()must be protected by disabling the preemption, so simple use get_cpu()/put_cpu() instead.
Quoted 6 lines
simply
Quoted 1 line
Reported-by Nam Cao <[email protected]>Reported-by Andrea Parri <[email protected]>
I added it at first but since there is no link to a bug report, checkpatch complains so I removed it. But I have just checked the documentation which states:
"The tag should be followed by a Closes: tag pointing to the report, unless the report is not available on the web"
So I'm sorry about that, it won't happen again.
Thanks,
Alex
Quoted 8 lines
Quoted 4 lines
Signed-off-by Alexandre Ghiti <[email protected]>Tested-by Andrea Parri <[email protected]>Andrea
Will Deacon
maintainerQuoted 33 lines · Alexandre Ghiti
As reported in [1], the use of
smp_processor_id()inpmu_sbi_device_probe()must be protected by disabling the preemption, so simple use get_cpu()/put_cpu() instead.Reported-by Nam Cao <[email protected]>Signed-off-by Alexandre Ghiti <[email protected]>--- drivers/perf/riscv_pmu_sbi.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)drivers/perf/riscv_pmu_sbi.c +5
-1@@ -1373,11 +1373,15 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)/* SBI PMU Snapsphot is only available in SBI v2.0 */ if (sbi_v2_available) { +int cpu; + ret =pmu_sbi_snapshot_alloc (pmu);if (ret) goto out_unregister; - ret =pmu_sbi_snapshot_setup (pmu,smp_processor_id ()); + cpu =get_cpu (); + + ret =pmu_sbi_snapshot_setup (pmu, cpu);if (ret) {/* Snapshot is an optional feature. Continue if not available */ pmu_sbi_snapshot_free (pmu);
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_device_probe(struct platform_device *pdev) · line 1398
Quoted 4 lines
drivers/perf/riscv_pmu_sbi.c +1
-0@@ -1391,6 +1395,7 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)*/ static_branch_enable (&sbi_pmu_snapshot_available); } +put_cpu ();
Are you sure it's safe to enable the static key with preemption disabled? I thought that could block on a mutex.
Will
Hi Will,
Quoted 27 lines · Will Deacon
Quoted 25 lines · Alexandre Ghiti
As reported in [1], the use of
smp_processor_id()inpmu_sbi_device_probe()must be protected by disabling the preemption, so simple use get_cpu()/put_cpu() instead.Reported-by Nam Cao <[email protected]>Signed-off-by Alexandre Ghiti <[email protected]>--- drivers/perf/riscv_pmu_sbi.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)drivers/perf/riscv_pmu_sbi.c +2
-0@@ -1373,11 +1373,15 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)/* SBI PMU Snapsphot is only available in SBI v2.0 */ if (sbi_v2_available) { +int cpu; + ret =pmu_sbi_snapshot_alloc (pmu);if (ret) goto out_unregister;
Quoted 15 lines
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_device_probe(struct platform_device *pdev) · line 1398
Quoted 12 lines
drivers/perf/riscv_pmu_sbi.c +4
-1- ret =pmu_sbi_snapshot_setup (pmu,smp_processor_id ()); + cpu =get_cpu (); + + ret =pmu_sbi_snapshot_setup (pmu, cpu);if (ret) {/* Snapshot is an optional feature. Continue if not available */ pmu_sbi_snapshot_free (pmu);@@ -1391,6 +1395,7 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)*/ static_branch_enable (&sbi_pmu_snapshot_available); } +put_cpu ();Are you sure it's safe to enable the static key with preemption disabled? I thought that could block on a mutex.
Yep, it seems you're right, thanks for jumping in.
I'm discussing with Atish how to fix that differently, I'll be back with another version very soon.
Thanks again,
Alex
Quoted 2 lines
Will
Quoted 46 lines · Alexandre Ghiti
Hi Will,
Quoted 42 lines · Will Deacon
Quoted 33 lines · Alexandre Ghiti
As reported in [1], the use of
smp_processor_id()inpmu_sbi_device_probe()must be protected by disabling the preemption, so simple use get_cpu()/put_cpu() instead.Reported-by Nam Cao <[email protected]>Signed-off-by Alexandre Ghiti <[email protected]>--- drivers/perf/riscv_pmu_sbi.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)drivers/perf/riscv_pmu_sbi.c +5
-1@@ -1373,11 +1373,15 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)/* SBI PMU Snapsphot is only available in SBI v2.0 */ if (sbi_v2_available) { +int cpu; + ret =pmu_sbi_snapshot_alloc (pmu);if (ret) goto out_unregister; - ret =pmu_sbi_snapshot_setup (pmu,smp_processor_id ()); + cpu =get_cpu (); + + ret =pmu_sbi_snapshot_setup (pmu, cpu);if (ret) {/* Snapshot is an optional feature. Continue if not available */ pmu_sbi_snapshot_free (pmu);drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_device_probe(struct platform_device *pdev) · line 1398
Quoted 4 lines
drivers/perf/riscv_pmu_sbi.c +1
-0@@ -1391,6 +1395,7 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)*/ static_branch_enable (&sbi_pmu_snapshot_available); } +put_cpu ();Are you sure it's safe to enable the static key with preemption disabled? I thought that could block on a mutex.
Thanks Will for pointing that out.
Quoted 5 lines
Yep, it seems you're right, thanks for jumping in.
I'm discussing with Atish how to fix that differently, I'll be back with another version very soon.
Looking at the driver core framework code, I am wondering if a probe function can be preempted to run on a different cpu. If it can only be preempted by higher priority kernel threads or interrupts but is guaranteed to run on the same cpu again, we can just use the raw_smp_processor_id.
However, if there is no guarantee then we can just invoke get_cpu/put_cpu around pmu_sbi_snapshot_setup.
Quoted 11 lines
Thanks again,
Alex
Quoted 2 lines
Will
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv