Add resource-managed variant of register_restart_handler(). Signed-off-by: Dmitry Osipenko <digetx@xxxxxxxxx> --- include/linux/reboot.h | 2 ++ kernel/reboot.c | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/linux/reboot.h b/include/linux/reboot.h index f72fb8729608..fb17fad63e53 100644 --- a/include/linux/reboot.h +++ b/include/linux/reboot.h @@ -49,6 +49,8 @@ extern int register_restart_handler(struct notifier_block *); extern int unregister_restart_handler(struct notifier_block *); extern void do_kernel_restart(char *cmd); +extern int devm_register_restart_handler(struct device *, struct notifier_block *); + extern int register_poweroff_handler(struct notifier_block *); extern int unregister_poweroff_handler(struct notifier_block *); diff --git a/kernel/reboot.c b/kernel/reboot.c index 6e90afec2bb8..b611e49a5ac6 100644 --- a/kernel/reboot.c +++ b/kernel/reboot.c @@ -200,6 +200,33 @@ int unregister_restart_handler(struct notifier_block *nb) } EXPORT_SYMBOL(unregister_restart_handler); +static void devm_unregister_restart_handler(struct device *dev, void *res) +{ + WARN_ON(unregister_restart_handler(*(struct notifier_block **)res)); +} + +int devm_register_restart_handler(struct device *dev, struct notifier_block *nb) +{ + struct notifier_block **rcnb; + int ret; + + rcnb = devres_alloc(devm_unregister_restart_handler, + sizeof(*rcnb), GFP_KERNEL); + if (!rcnb) + return -ENOMEM; + + ret = register_restart_handler(nb); + if (!ret) { + *rcnb = nb; + devres_add(dev, rcnb); + } else { + devres_free(rcnb); + } + + return ret; +} +EXPORT_SYMBOL(devm_register_restart_handler); + /** * do_kernel_restart - Execute kernel restart handler call chain * -- 2.32.0