Re: [PATCH v4 01/13] notifier: add device-managed registration APIs
From: Uwe Kleine-König
Date: Mon Aug 10 2026 - 00:37:40 EST
Hello,
On Sun, Jul 26, 2026 at 10:17:27AM +0000, Eliav Farber wrote:
> diff --git a/kernel/notifier.c b/kernel/notifier.c
> index 2f9fe7c30287..c1a66fa0c331 100644
> --- a/kernel/notifier.c
> +++ b/kernel/notifier.c
> @@ -1,4 +1,5 @@
> // SPDX-License-Identifier: GPL-2.0-only
> +#include <linux/device/devres.h>
> #include <linux/kdebug.h>
> #include <linux/kprobes.h>
> #include <linux/export.h>
> @@ -197,6 +198,56 @@ int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
> }
> EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
>
> +struct atomic_notifier_chain_devres {
> + struct atomic_notifier_head *nh;
> + struct notifier_block *nb;
> +};
> +
> +static void devm_atomic_notifier_chain_unregister(struct device *dev, void *res)
> +{
> + struct atomic_notifier_chain_devres *dr = res;
> +
> + atomic_notifier_chain_unregister(dr->nh, dr->nb);
> +}
> +
> +/**
> + * devm_atomic_notifier_chain_register - Device-managed atomic notifier registration
> + * @dev: Device to tie the notifier lifetime to
> + * @nh: Pointer to head of the atomic notifier chain
> + * @n: New entry in notifier chain
> + *
> + * Adds a notifier to an atomic notifier chain and registers a cleanup
> + * action to automatically unregister it when @dev is unbound.
> + *
> + * Return:
> + * 0 on success, negative errno on error.
> + */
> +int devm_atomic_notifier_chain_register(struct device *dev,
> + struct atomic_notifier_head *nh,
> + struct notifier_block *n)
> +{
> + struct atomic_notifier_chain_devres *dr;
> + int ret;
> +
> + dr = devres_alloc(devm_atomic_notifier_chain_unregister,
> + sizeof(*dr), GFP_KERNEL);
> + if (!dr)
> + return -ENOMEM;
> +
> + ret = atomic_notifier_chain_register(nh, n);
> + if (ret) {
> + devres_free(dr);
> + return ret;
> + }
> +
> + dr->nh = nh;
> + dr->nb = n;
> + devres_add(dev, dr);
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(devm_atomic_notifier_chain_register);
IMHO devm_atomic_notifier_chain_register() should look as follows:
ret = atomic_notifier_chain_register(nh, n);
if (ret)
return ret;
return devm_add_action_or_reset(dev, devm_atomic_notifier_chain_unregister, dr)
which is much easier and includes less details from the inner workings
of devm. Same for the blocking variant.
Best regards
Uwe
Attachment:
signature.asc
Description: PGP signature