Re: [PATCH v4 01/13] notifier: add device-managed registration APIs

Uwe Kleine-König <[email protected]>
Newsgroups org.kernel.vger.linux-pwm,org.kernel.vger.linux-acpi,org.kernel.vger.linux-gpio,org.kernel.vger.linux-iio,org.kernel.vger.linux-input,org.kernel.vger.linux-kernel,org.kernel.vger.platform-driver-x86
Message-ID <anlUQXtxrghA8kNT@monoceros>
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
signature.asc (application/pgp-signature, 488 B)
-----BEGIN PGP SIGNATURE-----

iQEzBAABCgAdFiEEP4GsaTp6HlmJrf7Tj4D7WH0S/k4FAmp5VYEACgkQj4D7WH0S
/k4A9Af+Ojn2E+l4xDQpsSDbVEqgiXv61C4OfU2RE50xgQP4qAZidi+pc4zGxtUg
0z13yR8gFAmrMM9N6O1VhriVFGIUA9vMYDtAyeU2TLjBIjMLKUFrr2vChSm/skP1
t/4Z+hc+YzosxSB0JIgsL5Bb3AwUCzwX/8tP18KNafUyRr8aQ9oOXfu/M+YNzh7a
3mc1JDU1vaCOJFX/SPtRdy9YLa7m9KEvawXtOChlmpMAxSCbHrjSAPJMkiBFc4k2
zqsTCnh/psoYVGSnNg/gfrXxINJkt3GkiKT84w3/SX02KB37Sx+CwIYavuUv/zEj
P5J9NCBIC3tSQpekexPhQywVL+yAsQ==
=Jvr3
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.