[PATCH v3] coresight: Fix scheduling while atomic in coresight_cpu_pm_notify()
From: Mohamed Ayman
Date: Thu Jul 16 2026 - 17:42:52 EST
Dropping the last reference to a coresight_device can trigger a kernel
panic on PREEMPT_RT builds due to a "scheduling while atomic" violation.
When the CPU enters an idle state, coresight_cpu_pm_notify() is invoked
with local interrupts disabled. It calls coresight_cpu_get_active_path(),
which currently uses coresight_get_percpu_source_ref() to get a kobject
reference, and then immediately drops it with coresight_put_percpu_source_ref().
If this put_device() call drops the very last reference (e.g., due to a
concurrent device unregistration), it synchronously triggers the release
cascade. On PREEMPT_RT, free_percpu() takes a sleeping rt-mutex.
Furthermore, any parent device in the release chain might also acquire
sleeping locks, causing a system crash in the atomic PM context.
Fix this by eliminating the get/put dance entirely in the PM notifier path.
Since coresight_cpu_pm_notify() runs with IRQs disabled, it is safe to
read the per-cpu source pointer directly under the coresight_dev_lock,
check the mode, and return the path without unnecessarily manipulating the
kobject refcount.
Suggested-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
Signed-off-by: Mohamed Ayman <mohamedaymanworkspace@xxxxxxxxx>
---
drivers/hwtracing/coresight/coresight-core.c | 22 ++++++--------------
1 file changed, 6 insertions(+), 16 deletions(-)
diff --git a/drivers/hwtracing/coresight/coresight-core.c b/drivers/hwtracing/coresight/coresight-core.c
index 6d65c43d5..713bcff53 100644
--- a/drivers/hwtracing/coresight/coresight-core.c
+++ b/drivers/hwtracing/coresight/coresight-core.c
@@ -1850,25 +1850,15 @@ static void coresight_release_device_list(void)
static struct coresight_path *coresight_cpu_get_active_path(enum cs_mode mode)
{
struct coresight_device *source;
- bool is_active = false;
+ struct coresight_path *path = NULL;
- source = coresight_get_percpu_source_ref(smp_processor_id());
- if (!source)
- return NULL;
-
- if (coresight_get_mode(source) & mode)
- is_active = true;
+ guard(raw_spinlock_irqsave)(&coresight_dev_lock);
- coresight_put_percpu_source_ref(source);
+ source = per_cpu(csdev_source, smp_processor_id());
+ if (source && (coresight_get_mode(source) & mode))
+ path = source->path;
- /*
- * It is expected to run in atomic context or with the CPU lock held for
- * sysfs mode, so it cannot be preempted to disable the path. Here
- * returns the active path pointer without concern that its state may
- * change. Since the build path has taken a reference on the component,
- * the path can be safely used by the caller.
- */
- return is_active ? source->path : NULL;
+ return path;
}
/* Return: 1 if PM is required, 0 if skip, or a negative error */
--
2.34.1