Re: [perf] lockdep warning between cpu_add_remove_lock and &dev->mutex.

From: Peter Zijlstra
Date: Mon Jan 23 2023 - 06:41:53 EST


On Mon, Jan 23, 2023 at 07:39:24PM +0900, Tetsuo Handa wrote:
> Hello.
>
> I tried to apply below patch, and hit lockdep warning during boot.
> Can you break this dependency?

cpu_add_remove_lock
cpu_hotplug_lock
pmus_lock
dev->mutex (pmu_dev_alloc)

vs

dev->mutex
cpu_add_remove_lock (pci_device_probe)


Possibly something like this might do -- I'm not entirely sure it's
fully correct, needs a bit of auditing.

---
diff --git a/kernel/events/core.c b/kernel/events/core.c
index eacc3702654d..d6b2265a9982 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -13570,9 +13570,9 @@ static void perf_event_exit_cpu_context(int cpu)
{
struct perf_cpu_context *cpuctx;
struct perf_event_context *ctx;
+ int idx = srcu_read_lock(&pmus_srcu);

// XXX simplify cpuctx->online
- mutex_lock(&pmus_lock);
cpuctx = per_cpu_ptr(&perf_cpu_context, cpu);
ctx = &cpuctx->ctx;

@@ -13581,7 +13581,7 @@ static void perf_event_exit_cpu_context(int cpu)
cpuctx->online = 0;
mutex_unlock(&ctx->mutex);
cpumask_clear_cpu(cpu, perf_online_mask);
- mutex_unlock(&pmus_lock);
+ srcu_read_unlock(&pmus_srcu, idx);
}
#else

@@ -13593,10 +13594,11 @@ int perf_event_init_cpu(unsigned int cpu)
{
struct perf_cpu_context *cpuctx;
struct perf_event_context *ctx;
+ int idx;

perf_swevent_init_cpu(cpu);

- mutex_lock(&pmus_lock);
+ idx = srcu_read_lock(&pmus_srcu);
cpumask_set_cpu(cpu, perf_online_mask);
cpuctx = per_cpu_ptr(&perf_cpu_context, cpu);
ctx = &cpuctx->ctx;
@@ -13604,7 +13606,7 @@ int perf_event_init_cpu(unsigned int cpu)
mutex_lock(&ctx->mutex);
cpuctx->online = 1;
mutex_unlock(&ctx->mutex);
- mutex_unlock(&pmus_lock);
+ srcu_read_unlock(&pmus_srcu, idx);

return 0;
}