[PATCH 2/5] irqdesc: Use dynamic lockdep keys for interrupt descriptors

From: Bart Van Assche
Date: Mon Feb 03 2025 - 13:03:49 EST


Having to call irq_set_lockdep_class() if nested locking is expected is
cumbersome. Hence, prepare for removing irq_set_lockdep_class() by
associating a dynamic key with the synchronization objects in interrupt
descriptors.

Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>
---
include/linux/irqdesc.h | 4 ++++
kernel/irq/irqdesc.c | 22 +++++++++-------------
2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/include/linux/irqdesc.h b/include/linux/irqdesc.h
index fd091c35d572..5f4bd476fcc8 100644
--- a/include/linux/irqdesc.h
+++ b/include/linux/irqdesc.h
@@ -46,6 +46,7 @@ struct irqstat {
* @threads_handled: stats field for deferred spurious detection of threaded handlers
* @threads_handled_last: comparator field for deferred spurious detection of threaded handlers
* @lock: locking for SMP
+ * @lock_key: Lockdep class for @lock
* @affinity_hint: hint to user space for preferred irq affinity
* @affinity_notify: context for notification of affinity changes
* @pending_mask: pending rebalanced interrupts
@@ -60,6 +61,7 @@ struct irqstat {
* @rcu: rcu head for delayed free
* @kobj: kobject used to represent this struct in sysfs
* @request_mutex: mutex to protect request/free before locking desc->lock
+ * @request_key: Lockdep class for @request_mutex
* @dir: /proc/irq/ procfs entry
* @debugfs_file: dentry for the debugfs file
* @name: flow handler name for /proc/interrupts output
@@ -81,6 +83,7 @@ struct irq_desc {
atomic_t threads_handled;
int threads_handled_last;
raw_spinlock_t lock;
+ struct lock_class_key lock_key;
struct cpumask *percpu_enabled;
const struct cpumask *percpu_affinity;
#ifdef CONFIG_SMP
@@ -111,6 +114,7 @@ struct irq_desc {
struct kobject kobj;
#endif
struct mutex request_mutex;
+ struct lock_class_key request_key;
int parent_irq;
struct module *owner;
const char *name;
diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
index 287830739783..261305a213fd 100644
--- a/kernel/irq/irqdesc.c
+++ b/kernel/irq/irqdesc.c
@@ -19,11 +19,6 @@

#include "internals.h"

-/*
- * lockdep: we want to handle all irq_desc locks as a single lock-class:
- */
-static struct lock_class_key irq_desc_lock_class;
-
#if defined(CONFIG_SMP)
static int __init irq_affinity_setup(char *str)
{
@@ -222,8 +217,11 @@ static int init_desc(struct irq_desc *desc, int irq, int node,
}

raw_spin_lock_init(&desc->lock);
- lockdep_set_class(&desc->lock, &irq_desc_lock_class);
+ lockdep_register_key(&desc->lock_key);
+ lockdep_set_class(&desc->lock, &desc->lock_key);
mutex_init(&desc->request_mutex);
+ lockdep_register_key(&desc->request_key);
+ lockdep_set_class(&desc->lock, &desc->request_key);
init_waitqueue_head(&desc->wait_for_threads);
desc_set_defaults(irq, desc, node, affinity, owner);
irqd_set(&desc->irq_data, flags);
@@ -484,13 +482,17 @@ static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
return desc;
}

+/* Called from RCU context. Hence, must not sleep. */
static void irq_kobj_release(struct kobject *kobj)
{
struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);

free_masks(desc);
free_percpu(desc->kstat_irqs);
- kfree(desc);
+ lockdep_unregister_key_nosync(&desc->request_key);
+ lockdep_unregister_key_nosync(&desc->lock_key);
+ init_rcu_head(&desc->rcu);
+ kfree_rcu(desc, rcu);
}

static void delayed_free_desc(struct rcu_head *rhp)
@@ -1064,12 +1066,6 @@ unsigned int kstat_irqs_usr(unsigned int irq)
void __irq_set_lockdep_class(unsigned int irq, struct lock_class_key *lock_class,
struct lock_class_key *request_class)
{
- struct irq_desc *desc = irq_to_desc(irq);
-
- if (desc) {
- lockdep_set_class(&desc->lock, lock_class);
- lockdep_set_class(&desc->request_mutex, request_class);
- }
}
EXPORT_SYMBOL_GPL(__irq_set_lockdep_class);
#endif