Skip to content

Commit 2d8364b

Browse files
ndreystorvalds
authored andcommitted
kernel/reboot.c: add devm_register_reboot_notifier()
Add devm_* wrapper around register_reboot_notifier to simplify device specific reboot notifier registration/unregistration. [akpm@linux-foundation.org: move `struct device' forward decl to top-of-file] Link: http://lkml.kernel.org/r/20170320171753.1705-1-andrew.smirnov@gmail.com Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c512ac0 commit 2d8364b

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎include/linux/reboot.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <linux/notifier.h>
77
#include <uapi/linux/reboot.h>
88

9+
struct device;
10+
911
#define SYS_DOWN 0x0001 /* Notify of system down */
1012
#define SYS_RESTART SYS_DOWN
1113
#define SYS_HALT 0x0002 /* Notify of system halt */
@@ -39,6 +41,8 @@ extern int reboot_force;
3941
extern int register_reboot_notifier(struct notifier_block *);
4042
extern int unregister_reboot_notifier(struct notifier_block *);
4143

44+
extern int devm_register_reboot_notifier(struct device *, struct notifier_block *);
45+
4246
extern int register_restart_handler(struct notifier_block *);
4347
extern int unregister_restart_handler(struct notifier_block *);
4448
extern void do_kernel_restart(char *cmd);

‎kernel/reboot.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,33 @@ int unregister_reboot_notifier(struct notifier_block *nb)
104104
}
105105
EXPORT_SYMBOL(unregister_reboot_notifier);
106106

107+
static void devm_unregister_reboot_notifier(struct device *dev, void *res)
108+
{
109+
WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
110+
}
111+
112+
int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
113+
{
114+
struct notifier_block **rcnb;
115+
int ret;
116+
117+
rcnb = devres_alloc(devm_unregister_reboot_notifier,
118+
sizeof(*rcnb), GFP_KERNEL);
119+
if (!rcnb)
120+
return -ENOMEM;
121+
122+
ret = register_reboot_notifier(nb);
123+
if (!ret) {
124+
*rcnb = nb;
125+
devres_add(dev, rcnb);
126+
} else {
127+
devres_free(rcnb);
128+
}
129+
130+
return ret;
131+
}
132+
EXPORT_SYMBOL(devm_register_reboot_notifier);
133+
107134
/*
108135
* Notifier list for kernel code which wants to be called
109136
* to restart the system.

0 commit comments

Comments
 (0)