From e38b9eda475fd5ef8d256ae3ced566452c1c5d58 Mon Sep 17 00:00:00 2001 From: Eliav Farber Date: Sun, 16 Aug 2026 06:06:00 +0000 Subject: notifier: add device-managed registration APIs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add devm_atomic_notifier_chain_register() and devm_blocking_notifier_chain_register() that automatically unregister the notifier when the device is unbound. Many drivers repeat the same boilerplate pattern: 1. Register the notifier with *_notifier_chain_register() 2. Check for error 3. Register a devm action to unregister on teardown 4. Implement a per-driver static unregister callback With the new devm_*_notifier_chain_register() APIs, this reduces to a single call with one error path, eliminating per-driver unregister callbacks entirely. The implementation follows the established devres pattern used by other device-managed kernel APIs. A devm variant for raw notifier chains is intentionally not included because raw notifiers require the caller to provide all locking. The devres teardown callback has no way to acquire the caller's lock, and devres_alloc() with GFP_KERNEL cannot be used if the caller's lock is a spinlock. Signed-off-by: Eliav Farber Acked-by: Linus Walleij Tested-by: Bradley Morgan Acked-by: Bradley Morgan # kernel/ Link: https://patch.msgid.link/20260816060648.4030-2-farbere@amazon.com [ukleinek: Add note to kdoc about devm_atomic_notifier_chain_register() not being callable in atomic context.] Signed-off-by: Uwe Kleine-König --- include/linux/notifier.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'include/linux') diff --git a/include/linux/notifier.h b/include/linux/notifier.h index 01b6c9d9956f..4eeae9741a6e 100644 --- a/include/linux/notifier.h +++ b/include/linux/notifier.h @@ -46,6 +46,7 @@ * often but notifier_blocks will seldom be removed. */ +struct device; struct notifier_block; typedef int (*notifier_fn_t)(struct notifier_block *nb, @@ -145,8 +146,14 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh); extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh, struct notifier_block *nb); +int devm_atomic_notifier_chain_register(struct device *dev, + struct atomic_notifier_head *nh, + struct notifier_block *nb); extern int blocking_notifier_chain_register(struct blocking_notifier_head *nh, struct notifier_block *nb); +int devm_blocking_notifier_chain_register(struct device *dev, + struct blocking_notifier_head *nh, + struct notifier_block *nb); extern int raw_notifier_chain_register(struct raw_notifier_head *nh, struct notifier_block *nb); extern int srcu_notifier_chain_register(struct srcu_notifier_head *nh, -- cgit v1.2.3