Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Andrey Smirnov and committed by
Linus Torvalds
2d8364ba c512ac01

+31
+4
include/linux/reboot.h
··· 6 6 #include <linux/notifier.h> 7 7 #include <uapi/linux/reboot.h> 8 8 9 + struct device; 10 + 9 11 #define SYS_DOWN 0x0001 /* Notify of system down */ 10 12 #define SYS_RESTART SYS_DOWN 11 13 #define SYS_HALT 0x0002 /* Notify of system halt */ ··· 40 38 41 39 extern int register_reboot_notifier(struct notifier_block *); 42 40 extern int unregister_reboot_notifier(struct notifier_block *); 41 + 42 + extern int devm_register_reboot_notifier(struct device *, struct notifier_block *); 43 43 44 44 extern int register_restart_handler(struct notifier_block *); 45 45 extern int unregister_restart_handler(struct notifier_block *);
+27
kernel/reboot.c
··· 104 104 } 105 105 EXPORT_SYMBOL(unregister_reboot_notifier); 106 106 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 + 107 134 /* 108 135 * Notifier list for kernel code which wants to be called 109 136 * to restart the system.