Skip to contentSkip to conversation

kernel/reboot: add devm_register_reboot_notifier

PATCH Applied Andrey Smirnov 2 participants 4 messages Mar 20, 2017 – Mar 30, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel/reboot: add devm_register_reboot_notifier

Add devm_* wrapper around register_reboot_notifier to simplify device specific reboot notifier registration/unregistration.

---
 include/linux/reboot.h |  3 +++
 kernel/reboot.c        | 27 +++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
include/linux/reboot.h +3 -0
index a7ff409..0ca2541 100644
@@ -38,6 +38,9 @@ extern int reboot_force;
 extern int register_reboot_notifier(struct notifier_block *);
 extern int unregister_reboot_notifier(struct notifier_block *);
 
+struct device;
+extern int devm_register_reboot_notifier(struct device *, struct notifier_block *);
+
 extern int register_restart_handler(struct notifier_block *);
 extern int unregister_restart_handler(struct notifier_block *);
 extern void do_kernel_restart(char *cmd);
kernel/reboot.c +27 -0
index bd30a97..e4ced88 100644
@@ -104,6 +104,33 @@ int unregister_reboot_notifier(struct notifier_block *nb)
 }
 EXPORT_SYMBOL(unregister_reboot_notifier);
 
+static void devm_unregister_reboot_notifier(struct device *dev, void *res)
+{
+	WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
+}
+
+int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
+{
+	struct notifier_block **rcnb;
+	int ret;
+
+	rcnb = devres_alloc(devm_unregister_reboot_notifier,
+			    sizeof(*rcnb), GFP_KERNEL);
+	if (!rcnb)
+		return -ENOMEM;
+
+	ret = register_reboot_notifier(nb);
+	if (!ret) {
+		*rcnb = nb;
+		devres_add(dev, rcnb);
+	} else {
+		devres_free(rcnb);
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL(devm_register_reboot_notifier);
+
 /*
  *	Notifier list for kernel code which wants to be called
  *	to restart the system.
Signature · 1 line
2.9.3
#1 lore raw
Quoted 25 lines · On Mon, 20 Mar 2017 10:17:53 -0700 Andrey Smirnov <[email protected]>…

Add devm_* wrapper around register_reboot_notifier to simplify device specific reboot notifier registration/unregistration.

kernel/reboot.c +16 -0
@@ -104,6 +104,33 @@ int unregister_reboot_notifier(struct notifier_block *nb)
 }
 EXPORT_SYMBOL(unregister_reboot_notifier);
 
+static void devm_unregister_reboot_notifier(struct device *dev, void *res)
+{
+	WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
+}
+
+int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
+{
+	struct notifier_block **rcnb;
+	int ret;
+
+	rcnb = devres_alloc(devm_unregister_reboot_notifier,
+			    sizeof(*rcnb), GFP_KERNEL);
+	if (!rcnb)
+		return -ENOMEM;
+
+	ret = register_reboot_notifier(nb);

kernel/reboot.c int unregister_reboot_notifier(struct notifier_block *nb) · line 132

Quoted 10 lines
kernel/reboot.c +10 -0
+	if (!ret) {
+		*rcnb = nb;
+		devres_add(dev, rcnb);
+	} else {
+		devres_free(rcnb);
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL(devm_register_reboot_notifier);

Seems reasonable. Can we please have some patches which actually use this?

#2 lore raw
Quoted 32 lines · Andrew Morton
Quoted 30 lines · On Mon, 20 Mar 2017 10:17:53 -0700 Andrey Smirnov <[email protected]>…

Add devm_* wrapper around register_reboot_notifier to simplify device specific reboot notifier registration/unregistration.

kernel/reboot.c +21 -0
@@ -104,6 +104,33 @@ int unregister_reboot_notifier(struct notifier_block *nb)
 }
 EXPORT_SYMBOL(unregister_reboot_notifier);
 
+static void devm_unregister_reboot_notifier(struct device *dev, void *res)
+{
+     WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
+}
+
+int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
+{
+     struct notifier_block **rcnb;
+     int ret;
+
+     rcnb = devres_alloc(devm_unregister_reboot_notifier,
+                         sizeof(*rcnb), GFP_KERNEL);
+     if (!rcnb)
+             return -ENOMEM;
+
+     ret = register_reboot_notifier(nb);
+     if (!ret) {
+             *rcnb = nb;
+             devres_add(dev, rcnb);
+     } else {
+             devres_free(rcnb);
Quoted 8 lines

kernel/reboot.c int unregister_reboot_notifier(struct notifier_block *nb) · line 132

Quoted 5 lines
kernel/reboot.c +5 -0
+     }
+
+     return ret;
+}
+EXPORT_SYMBOL(devm_register_reboot_notifier);

Seems reasonable. Can we please have some patches which actually use this?

I will be submitting a serdev/watchdog driver in next couple of weeks, I can CC you on that thread.

Thanks,
Andrey Smirnov

#3 lore raw
Quoted 43 lines · On Thu, 30 Mar 2017 15:55:43 -0700 Andrey Smirnov <[email protected]>…
Quoted 32 lines · Andrew Morton
Quoted 30 lines · On Mon, 20 Mar 2017 10:17:53 -0700 Andrey Smirnov <[email protected]>…

Add devm_* wrapper around register_reboot_notifier to simplify device specific reboot notifier registration/unregistration.

kernel/reboot.c +21 -0
@@ -104,6 +104,33 @@ int unregister_reboot_notifier(struct notifier_block *nb)
 }
 EXPORT_SYMBOL(unregister_reboot_notifier);
 
+static void devm_unregister_reboot_notifier(struct device *dev, void *res)
+{
+     WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
+}
+
+int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
+{
+     struct notifier_block **rcnb;
+     int ret;
+
+     rcnb = devres_alloc(devm_unregister_reboot_notifier,
+                         sizeof(*rcnb), GFP_KERNEL);
+     if (!rcnb)
+             return -ENOMEM;
+
+     ret = register_reboot_notifier(nb);
+     if (!ret) {
+             *rcnb = nb;
+             devres_add(dev, rcnb);
+     } else {
+             devres_free(rcnb);
Quoted 8 lines

kernel/reboot.c int unregister_reboot_notifier(struct notifier_block *nb) · line 132

Quoted 5 lines
kernel/reboot.c +5 -0
+     }
+
+     return ret;
+}
+EXPORT_SYMBOL(devm_register_reboot_notifier);

Seems reasonable. Can we please have some patches which actually use this?

Quoted 2 lines

I will be submitting a serdev/watchdog driver in next couple of weeks, I can CC you on that thread.

It would be nice to convert some other call sites as well, please - at least to get additional test coverage and to demonstrate the usefulness of the new interface.