Skip to contentSkip to conversation

drivers: perf: Fix smp_processor_id() use in preemptible code

PATCH Superseded Alexandre Ghiti 7 participants 9 messages Aug 26, 2024 – Aug 28, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
index 31a17a56eb3b..25b1b699b3e2 100644
@@ -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
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Alexandre Ghiti

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.

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
index 31a17a56eb3b..25b1b699b3e2 100644
@@ -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

Tested-by on v1 1/1
#2 lore raw
In reply to Alexandre Ghiti
Quoted 7 lines · Alexandre Ghiti

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.

I think this also needs:

Fixes a8625217a054 ("drivers/perf: riscv: Implement SBI PMU snapshot function")

Best regards,
Nam

#3 lore raw
Quoted 5 lines · Nam Cao
Quoted 4 lines · Alexandre Ghiti

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.

Quoted 7 lines
Quoted 3 lines

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

Tested-by on v1 1/1
#4 lore raw
In reply to Alexandre Ghiti
Quoted 3 lines · Alexandre Ghiti

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.

simply

Quoted 1 line
Quoted 2 lines

Andrea

#5 lore raw

Hi Andrea,

Quoted 6 lines · Andrea Parri
Quoted 3 lines · Alexandre Ghiti

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.

Quoted 6 lines

simply

Quoted 1 line

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

Andrea

maintainer
#6 lore raw
In reply to Alexandre Ghiti
Quoted 33 lines · Alexandre Ghiti

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 +5 -1
index 31a17a56eb3b..25b1b699b3e2 100644
@@ -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

#7 lore raw

Hi Will,

Quoted 27 lines · Will Deacon
Quoted 25 lines · Alexandre Ghiti

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 +2 -0
index 31a17a56eb3b..25b1b699b3e2 100644
@@ -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

#8 lore raw
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() 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 +5 -1
index 31a17a56eb3b..25b1b699b3e2 100644
@@ -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