Skip to contentSkip to conversation

Introduce power-off+restart call chain API

PATCH Superseded Dmitry Osipenko 4 participants 63 messages Nov 26, 2021 – Dec 13, 2021

Subsystems (21)
Arm (32-bit) x86 PowerPC ACPI +17 more subsystems

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover Introduce power-off+restart call chain API

Problem
-------

SoC devices require power-off call chaining functionality from kernel. We have a widely used restart chaining provided by restart notifier API, but nothing for power-off.

Solution
--------

Introduce new API that provides both restart and power-off call chains.

Why combine restart with power-off? Because drivers often do both. More practical to have API that provides both under the same roof.

The new API is designed with simplicity and extensibility in mind. It's built upon the existing restart and reboot APIs. The simplicity is in new helper functions that are convenient for drivers. The extensibility is in the design that doesn't hardcode callback arguments, making easy to add new parameters and remove old.

This is a third attempt to introduce the new API. First was made by Guenter Roeck back in 2014, second was made by Thierry Reding in 2017. In fact the work didn't stop and recently arm_pm_restart() was removed from v5.14 kernel, which was a part of preparatory work started by Guenter Roeck. I took into account experience and ideas from the previous attempts, extended and polished them.

Adoption plan
-------------

This patchset introduces the new API. It also converts multiple drivers and arch code to the new API to demonstrate how it all looks in practice.

The plan is:

1. Merge new API (patches 1-8). This API will co-exist with the old APIs.

2. Convert arch code to do_kernel_power_off() (patches 9-21).

3. Convert drivers and platform code to the new API.

4. Remove obsolete pm_power_off and pm_power_off_prepare variables.

5. Make restart-notifier API private to kernel/reboot.c once no users left.

It's fully implemented here:

[1] https://github.com/grate-driver/linux/commits/sys-off-handler

For now I'm sending only the first 25 base patches out of ~180. It's preferable to squash 1-2, partially 3 and 4 points of the plan into a single patchset to ease and speed up applying of the rest of the patches. Majority of drivers and platform patches depend on the base, hence they will come later (and per subsystem), once base will land.

All [1] patches are compile-tested. Tegra and x86 ACPI patches are tested on hardware. The remaining should be covered by unit tests (unpublished).

Results
-------

1. Devices can be powered off properly.

2. Global variables are removed from drivers.

3. Global pm_power_off and pm_power_off_prepare callback variables are removed once all users are converted to the new API. The latter callback is removed by patch #25 of this series.

4. Ambiguous call chain ordering is prohibited. See patch #5 which adds verification of restart handlers priorities, ensuring that they are unique.

Changelog:

v4: - Made a very minor improvement to doc comments, clarifying couple
default values.

- Corrected list of emails recipient by adding Linus, Sebastian, Philipp and more NDS people. Removed bouncing emails.

- Added acks that were given to v3.

v3: - Renamed power_handler to sys_off_handler as was suggested by
Rafael Wysocki.

- Improved doc-comments as was suggested by Rafael Wysocki. Added more doc-comments.

- Implemented full set of 180 patches which convert whole kernel in accordance to the plan, see link [1] above. Slightly adjusted API to better suit for the remaining converted drivers.

* Added unregister_sys_off_handler() that is handy for a couple old platform drivers.

* Dropped devm_register_trivial_restart_handler(), 'simple' variant is enough to have.

- Improved "Add atomic/blocking_notifier_has_unique_priority()" patch, as was suggested by Andy Shevchenko. Also replaced down_write() with down_read() and factored out common notifier_has_unique_priority().

- Added stop_chain field to struct restart_data and reboot_prep_data after discovering couple drivers wanting that feature.

- Added acks that were given to v2.

v2: - Replaced standalone power-off call chain demo-API with the combined
power-off+restart API because this is what drivers want. It's a more comprehensive solution.

- Converted multiple drivers and arch code to the new API. Suggested by Andy Shevchenko. I skimmed through the rest of drivers, verifying that new API suits them. The rest of the drivers will be converted once we will settle on the new API, otherwise will be too many patches here.

- v2 API doesn't expose notifier to users and require handlers to have unique priority. Suggested by Guenter Roeck.

- v2 API has power-off chaining disabled by default and require drivers to explicitly opt-in to the chaining. This preserves old behaviour for existing drivers once they are converted to the new API.

Dmitry Osipenko (25):
notifier: Remove extern annotation from function prototypes
notifier: Add blocking_notifier_call_chain_is_empty()
notifier: Add atomic/blocking_notifier_has_unique_priority()
reboot: Correct typo in a comment
reboot: Warn if restart handler has duplicated priority
reboot: Warn if unregister_restart_handler() fails
reboot: Remove extern annotation from function prototypes
kernel: Add combined power-off+restart handler call chain API
ARM: Use do_kernel_power_off()
csky: Use do_kernel_power_off()
riscv: Use do_kernel_power_off()
arm64: Use do_kernel_power_off()
parisc: Use do_kernel_power_off()
xen/x86: Use do_kernel_power_off()
powerpc: Use do_kernel_power_off()
m68k: Switch to new sys-off handler API
sh: Use do_kernel_power_off()
x86: Use do_kernel_power_off()
ia64: Use do_kernel_power_off()
mips: Use do_kernel_power_off()
nds32: Use do_kernel_power_off()
memory: emif: Use kernel_can_power_off()
ACPI: power: Switch to sys-off handler API
regulator: pfuze100: Use devm_register_sys_off_handler()
reboot: Remove pm_power_off_prepare()

 arch/arm/kernel/reboot.c               |   4 +-
 arch/arm64/kernel/process.c            |   3 +-
 arch/csky/kernel/power.c               |   6 +-
 arch/ia64/kernel/process.c             |   4 +-
 arch/m68k/emu/natfeat.c                |   3 +-
 arch/m68k/include/asm/machdep.h        |   1 -
 arch/m68k/kernel/process.c             |   5 +-
 arch/m68k/kernel/setup_mm.c            |   1 -
 arch/m68k/kernel/setup_no.c            |   1 -
 arch/m68k/mac/config.c                 |   4 +-
 arch/mips/kernel/reset.c               |   3 +-
 arch/nds32/kernel/process.c            |   3 +-
 arch/parisc/kernel/process.c           |   4 +-
 arch/powerpc/kernel/setup-common.c     |   4 +-
 arch/powerpc/xmon/xmon.c               |   3 +-
 arch/riscv/kernel/reset.c              |  12 +-
 arch/sh/kernel/reboot.c                |   3 +-
 arch/x86/kernel/reboot.c               |   4 +-
 arch/x86/xen/enlighten_pv.c            |   4 +-
 drivers/acpi/sleep.c                   |  25 +-
 drivers/memory/emif.c                  |   2 +-
 drivers/regulator/pfuze100-regulator.c |  38 +-
 include/linux/notifier.h               |  37 +-
 include/linux/pm.h                     |   1 -
 include/linux/reboot.h                 | 305 ++++++++++++--
 kernel/notifier.c                      |  83 ++++
 kernel/power/hibernate.c               |   2 +-
 kernel/reboot.c                        | 556 ++++++++++++++++++++++++-
 28 files changed, 985 insertions(+), 136 deletions(-)
Signature · 1 line
2.33.1
#1 lore raw

PATCH v4 1/25 notifier: Remove extern annotation from function prototypes

There is no need to annotate function prototypes with 'extern', it makes code less readable. Remove unnecessary annotations from <notifier.h>.

---
 include/linux/notifier.h | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
include/linux/notifier.h +15 -15
index 87069b8459af..4b80a815b666 100644
@@ -90,7 +90,7 @@ struct srcu_notifier_head {
 	} while (0)
 
 /* srcu_notifier_heads must be cleaned up dynamically */
-extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
+void srcu_init_notifier_head(struct srcu_notifier_head *nh);
 #define srcu_cleanup_notifier_head(name)	\
 		cleanup_srcu_struct(&(name)->srcu);
 
@@ -141,36 +141,36 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
 
 #ifdef __KERNEL__
 
-extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
+int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
 		struct notifier_block *nb);
-extern int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
+int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
 		struct notifier_block *nb);
-extern int raw_notifier_chain_register(struct raw_notifier_head *nh,
+int raw_notifier_chain_register(struct raw_notifier_head *nh,
 		struct notifier_block *nb);
-extern int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
+int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
 		struct notifier_block *nb);
 
-extern int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
+int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
 		struct notifier_block *nb);
-extern int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
+int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
 		struct notifier_block *nb);
-extern int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
+int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
 		struct notifier_block *nb);
-extern int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
+int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
 		struct notifier_block *nb);
 
-extern int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
+int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
 		unsigned long val, void *v);
-extern int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
+int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
 		unsigned long val, void *v);
-extern int raw_notifier_call_chain(struct raw_notifier_head *nh,
+int raw_notifier_call_chain(struct raw_notifier_head *nh,
 		unsigned long val, void *v);
-extern int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
+int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
 		unsigned long val, void *v);
 
-extern int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh,
+int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh,
 		unsigned long val_up, unsigned long val_down, void *v);
-extern int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
+int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
 		unsigned long val_up, unsigned long val_down, void *v);
 
 #define NOTIFY_DONE		0x0000		/* Don't care */
Signature · 1 line
2.33.1
#2 lore raw

PATCH v4 2/25 notifier: Add blocking_notifier_call_chain_is_empty()

In reply to Dmitry Osipenko

Add blocking_notifier_call_chain_is_empty() that returns true if call chain is empty.

---
 include/linux/notifier.h |  2 ++
 kernel/notifier.c        | 14 ++++++++++++++
 2 files changed, 16 insertions(+)
include/linux/notifier.h +2 -0
index 4b80a815b666..924c9d7c8e73 100644
@@ -173,6 +173,8 @@ int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh,
 int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
 		unsigned long val_up, unsigned long val_down, void *v);
 
+bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh);
+
 #define NOTIFY_DONE		0x0000		/* Don't care */
 #define NOTIFY_OK		0x0001		/* Suits me */
 #define NOTIFY_STOP_MASK	0x8000		/* Don't call further */
kernel/notifier.c +14 -0
index b8251dc0bc0f..b20cb7b9b1f0 100644
@@ -322,6 +322,20 @@ int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
 }
 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
 
+/**
+ *	blocking_notifier_call_chain_is_empty - Check whether notifier chain is empty
+ *	@nh: Pointer to head of the blocking notifier chain
+ *
+ *	Checks whether notifier chain is empty.
+ *
+ *	Returns true is notifier chain is empty, false otherwise.
+ */
+bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh)
+{
+	return !rcu_access_pointer(nh->head);
+}
+EXPORT_SYMBOL_GPL(blocking_notifier_call_chain_is_empty);
+
 /*
  *	Raw notifier chain routines.  There is no protection;
  *	the caller must provide it.  Use at your own risk!
Signature · 1 line
2.33.1
#5 lore raw

PATCH v4 3/25 notifier: Add atomic/blocking_notifier_has_unique_priority()

In reply to Dmitry Osipenko

Add atomic/blocking_notifier_has_unique_priority() helpers which return true if given handler has unique priority.

---
 include/linux/notifier.h |  5 +++
 kernel/notifier.c        | 69 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+)
include/linux/notifier.h +5 -0
index 924c9d7c8e73..2c4036f225e1 100644
@@ -175,6 +175,11 @@ int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
 
 bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh);
 
+bool atomic_notifier_has_unique_priority(struct atomic_notifier_head *nh,
+		struct notifier_block *nb);
+bool blocking_notifier_has_unique_priority(struct blocking_notifier_head *nh,
+		struct notifier_block *nb);
+
 #define NOTIFY_DONE		0x0000		/* Don't care */
 #define NOTIFY_OK		0x0001		/* Suits me */
 #define NOTIFY_STOP_MASK	0x8000		/* Don't call further */
kernel/notifier.c +69 -0

87 lines not on this page · raw message

Signature · 1 line
2.33.1
#11 lore raw

PATCH v4 4/25 reboot: Correct typo in a comment

In reply to Dmitry Osipenko

Correct s/implemenations/implementations/ in <reboot.h>.

---
 include/linux/reboot.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/reboot.h +1 -1
index af907a3d68d1..7c288013a3ca 100644
@@ -63,7 +63,7 @@ struct pt_regs;
 extern void machine_crash_shutdown(struct pt_regs *);
 
 /*
- * Architecture independent implemenations of sys_reboot commands.
+ * Architecture independent implementations of sys_reboot commands.
  */
 
 extern void kernel_restart_prepare(char *cmd);
Signature · 1 line
2.33.1
#13 lore raw

PATCH v4 5/25 reboot: Warn if restart handler has duplicated priority

In reply to Dmitry Osipenko

Add sanity check which ensures that there are no two restart handlers registered with the same priority. Normally it's a direct sign of a problem if two handlers use the same priority.

---
 kernel/reboot.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
kernel/reboot.c +14 -1
index 6bcc5d6a6572..e6659ae329f1 100644
@@ -182,7 +182,20 @@ static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
  */
 int register_restart_handler(struct notifier_block *nb)
 {
-	return atomic_notifier_chain_register(&restart_handler_list, nb);
+	int ret;
+
+	ret = atomic_notifier_chain_register(&restart_handler_list, nb);
+	if (ret)
+		return ret;
+
+	/*
+	 * Handler must have unique priority. Otherwise call order is
+	 * determined by registration order, which is unreliable.
+	 */
+	WARN(!atomic_notifier_has_unique_priority(&restart_handler_list, nb),
+	     "restart handler must have unique priority\n");
+
+	return 0;
 }
 EXPORT_SYMBOL(register_restart_handler);
 
Signature · 1 line
2.33.1
#25 lore raw

PATCH v4 6/25 reboot: Warn if unregister_restart_handler() fails

In reply to Dmitry Osipenko

Emit warning if unregister_restart_handler() fails since it never should fail. This will ease further API development by catching mistakes early.

---
 kernel/reboot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/reboot.c +1 -1
index e6659ae329f1..f0e7b9c13f6b 100644
@@ -210,7 +210,7 @@ EXPORT_SYMBOL(register_restart_handler);
  */
 int unregister_restart_handler(struct notifier_block *nb)
 {
-	return atomic_notifier_chain_unregister(&restart_handler_list, nb);
+	return WARN_ON(atomic_notifier_chain_unregister(&restart_handler_list, nb));
 }
 EXPORT_SYMBOL(unregister_restart_handler);
 
Signature · 1 line
2.33.1
#30 lore raw

PATCH v4 7/25 reboot: Remove extern annotation from function prototypes

In reply to Dmitry Osipenko

There is no need to annotate function prototypes with 'extern', it makes code less readable. Remove unnecessary annotations from <reboot.h>.

---
 include/linux/reboot.h | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
include/linux/reboot.h +19 -19
index 7c288013a3ca..b7fa25726323 100644
@@ -40,36 +40,36 @@ extern int reboot_cpu;
 extern int reboot_force;
 
 
-extern int register_reboot_notifier(struct notifier_block *);
-extern int unregister_reboot_notifier(struct notifier_block *);
+int register_reboot_notifier(struct notifier_block *);
+int unregister_reboot_notifier(struct notifier_block *);
 
-extern int devm_register_reboot_notifier(struct device *, struct notifier_block *);
+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);
+int register_restart_handler(struct notifier_block *);
+int unregister_restart_handler(struct notifier_block *);
+void do_kernel_restart(char *cmd);
 
 /*
  * Architecture-specific implementations of sys_reboot commands.
  */
 
-extern void migrate_to_reboot_cpu(void);
-extern void machine_restart(char *cmd);
-extern void machine_halt(void);
-extern void machine_power_off(void);
+void migrate_to_reboot_cpu(void);
+void machine_restart(char *cmd);
+void machine_halt(void);
+void machine_power_off(void);
 
-extern void machine_shutdown(void);
+void machine_shutdown(void);
 struct pt_regs;
-extern void machine_crash_shutdown(struct pt_regs *);
+void machine_crash_shutdown(struct pt_regs *);
 
 /*
  * Architecture independent implementations of sys_reboot commands.
  */
 
-extern void kernel_restart_prepare(char *cmd);
-extern void kernel_restart(char *cmd);
-extern void kernel_halt(void);
-extern void kernel_power_off(void);
+void kernel_restart_prepare(char *cmd);
+void kernel_restart(char *cmd);
+void kernel_halt(void);
+void kernel_power_off(void);
 
 extern int C_A_D; /* for sysctl */
 void ctrl_alt_del(void);
@@ -77,15 +77,15 @@ void ctrl_alt_del(void);
 #define POWEROFF_CMD_PATH_LEN	256
 extern char poweroff_cmd[POWEROFF_CMD_PATH_LEN];
 
-extern void orderly_poweroff(bool force);
-extern void orderly_reboot(void);
+void orderly_poweroff(bool force);
+void orderly_reboot(void);
 void hw_protection_shutdown(const char *reason, int ms_until_forced);
 
 /*
  * Emergency restart, callable from an interrupt handler.
  */
 
-extern void emergency_restart(void);
+void emergency_restart(void);
 #include <asm/emergency-restart.h>
 
 #endif /* _LINUX_REBOOT_H */
Signature · 1 line
2.33.1
#42 lore raw

PATCH v4 9/25 ARM: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/arm/kernel/reboot.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/arm/kernel/reboot.c +1 -3
index 3044fcb8d073..2cb943422554 100644
@@ -116,9 +116,7 @@ void machine_power_off(void)
 {
 	local_irq_disable();
 	smp_send_stop();
-
-	if (pm_power_off)
-		pm_power_off();
+	do_kernel_power_off();
 }
 
 /*
Signature · 1 line
2.33.1
#43 lore raw

PATCH v4 10/25 csky: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/csky/kernel/power.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/csky/kernel/power.c +2 -4
index 923ee4e381b8..86ee202906f8 100644
@@ -9,16 +9,14 @@ EXPORT_SYMBOL(pm_power_off);
 void machine_power_off(void)
 {
 	local_irq_disable();
-	if (pm_power_off)
-		pm_power_off();
+	do_kernel_power_off();
 	asm volatile ("bkpt");
 }
 
 void machine_halt(void)
 {
 	local_irq_disable();
-	if (pm_power_off)
-		pm_power_off();
+	do_kernel_power_off();
 	asm volatile ("bkpt");
 }
 
Signature · 1 line
2.33.1
#44 lore raw

PATCH v4 11/25 riscv: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/riscv/kernel/reset.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
arch/riscv/kernel/reset.c +4 -8
index 9c842c41684a..912288572226 100644
@@ -23,16 +23,12 @@ void machine_restart(char *cmd)
 
 void machine_halt(void)
 {
-	if (pm_power_off != NULL)
-		pm_power_off();
-	else
-		default_power_off();
+	do_kernel_power_off();
+	default_power_off();
 }
 
 void machine_power_off(void)
 {
-	if (pm_power_off != NULL)
-		pm_power_off();
-	else
-		default_power_off();
+	do_kernel_power_off();
+	default_power_off();
 }
Signature · 1 line
2.33.1
#45 lore raw

PATCH v4 12/25 arm64: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/arm64/kernel/process.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm64/kernel/process.c +1 -2
index aacf2f5559a8..f8db031afa7d 100644
@@ -110,8 +110,7 @@ void machine_power_off(void)
 {
 	local_irq_disable();
 	smp_send_stop();
-	if (pm_power_off)
-		pm_power_off();
+	do_kernel_power_off();
 }
 
 /*
Signature · 1 line
2.33.1
#46 lore raw

PATCH v4 13/25 parisc: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/parisc/kernel/process.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/parisc/kernel/process.c +2 -2
index ea3d83b6fb62..928201b1f58f 100644
@@ -26,6 +26,7 @@
 #include <linux/module.h>
 #include <linux/personality.h>
 #include <linux/ptrace.h>
+#include <linux/reboot.h>
 #include <linux/sched.h>
 #include <linux/sched/debug.h>
 #include <linux/sched/task.h>
@@ -114,8 +115,7 @@ void machine_power_off(void)
 	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
 
 	/* ipmi_poweroff may have been installed. */
-	if (pm_power_off)
-		pm_power_off();
+	do_kernel_power_off();
 		
 	/* It seems we have no way to power the system off via
 	 * software. The user has to press the button himself. */
Signature · 1 line
2.33.1
#47 lore raw

PATCH v4 14/25 xen/x86: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/x86/xen/enlighten_pv.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/xen/enlighten_pv.c +2 -2
index 5004feb16783..527fa545eb1f 100644
@@ -31,6 +31,7 @@
 #include <linux/gfp.h>
 #include <linux/edd.h>
 #include <linux/objtool.h>
+#include <linux/reboot.h>
 
 #include <xen/xen.h>
 #include <xen/events.h>
@@ -1068,8 +1069,7 @@ static void xen_machine_halt(void)
 
 static void xen_machine_power_off(void)
 {
-	if (pm_power_off)
-		pm_power_off();
+	do_kernel_power_off();
 	xen_reboot(SHUTDOWN_poweroff);
 }
 
Signature · 1 line
2.33.1
#48 lore raw

PATCH v4 15/25 powerpc: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/powerpc/kernel/setup-common.c | 4 +---
 arch/powerpc/xmon/xmon.c           | 3 +--
 2 files changed, 2 insertions(+), 5 deletions(-)
arch/powerpc/kernel/setup-common.c +1 -3
index 4f1322b65760..71c4ccd9bbb1 100644
@@ -161,9 +161,7 @@ void machine_restart(char *cmd)
 void machine_power_off(void)
 {
 	machine_shutdown();
-	if (pm_power_off)
-		pm_power_off();
-
+	do_kernel_power_off();
 	smp_send_stop();
 	machine_hang();
 }
arch/powerpc/xmon/xmon.c +1 -2
index 83100c6524cc..759e167704e6 100644
@@ -1243,8 +1243,7 @@ static void bootcmds(void)
 	} else if (cmd = 'h') {
 		ppc_md.halt();
 	} else if (cmd = 'p') {
-		if (pm_power_off)
-			pm_power_off();
+		do_kernel_power_off();
 	}
 }
 
Signature · 1 line
2.33.1
#49 lore raw

PATCH v4 16/25 m68k: Switch to new sys-off handler API

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use register_power_off_handler() that registers power-off handlers and do_kernel_power_off() that invokes chained power-off handlers. Legacy pm_power_off() will be removed once all drivers will be converted to the new power-off API.

Normally arch code should adopt only the do_kernel_power_off() at first, but m68k is a special case because it uses pm_power_off() "inside out", i.e. pm_power_off() invokes machine_power_off() [in fact it does nothing], while it's machine_power_off() that should invoke the pm_power_off(), and thus, we can't convert platforms to the new API separately. There are only two platforms changed here, so it's not a big deal.

---
 arch/m68k/emu/natfeat.c         | 3 ++-
 arch/m68k/include/asm/machdep.h | 1 -
 arch/m68k/kernel/process.c      | 5 ++---
 arch/m68k/kernel/setup_mm.c     | 1 -
 arch/m68k/kernel/setup_no.c     | 1 -
 arch/m68k/mac/config.c          | 4 +++-
 6 files changed, 7 insertions(+), 8 deletions(-)
arch/m68k/emu/natfeat.c +2 -1
index 71b78ecee75c..b19dc00026d9 100644
@@ -15,6 +15,7 @@
 #include <linux/string.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/reboot.h>
 #include <linux/io.h>
 #include <asm/machdep.h>
 #include <asm/natfeat.h>
@@ -90,5 +91,5 @@ void __init nf_init(void)
 	pr_info("NatFeats found (%s, %lu.%lu)\n", buf, version >> 16,
 		version & 0xffff);
 
-	mach_power_off = nf_poweroff;
+	register_platform_power_off(nf_poweroff);
 }
arch/m68k/include/asm/machdep.h +0 -1
index 8fd80ef1b77e..8d8c3ee2069f 100644
@@ -24,7 +24,6 @@ extern int (*mach_get_rtc_pll)(struct rtc_pll_info *);
 extern int (*mach_set_rtc_pll)(struct rtc_pll_info *);
 extern void (*mach_reset)( void );
 extern void (*mach_halt)( void );
-extern void (*mach_power_off)( void );
 extern unsigned long (*mach_hd_init) (unsigned long, unsigned long);
 extern void (*mach_hd_setup)(char *, int *);
 extern void (*mach_heartbeat) (int);
arch/m68k/kernel/process.c +2 -3
index a6030dbaa089..e160a7c57bd3 100644
@@ -67,12 +67,11 @@ void machine_halt(void)
 
 void machine_power_off(void)
 {
-	if (mach_power_off)
-		mach_power_off();
+	do_kernel_power_off();
 	for (;;);
 }
 
-void (*pm_power_off)(void) = machine_power_off;
+void (*pm_power_off)(void);
 EXPORT_SYMBOL(pm_power_off);
 
 void show_regs(struct pt_regs * regs)
arch/m68k/kernel/setup_mm.c +0 -1
index 4b51bfd38e5f..50f4f120a4ff 100644
@@ -98,7 +98,6 @@ EXPORT_SYMBOL(mach_get_rtc_pll);
 EXPORT_SYMBOL(mach_set_rtc_pll);
 void (*mach_reset)( void );
 void (*mach_halt)( void );
-void (*mach_power_off)( void );
 #ifdef CONFIG_HEARTBEAT
 void (*mach_heartbeat) (int);
 EXPORT_SYMBOL(mach_heartbeat);
arch/m68k/kernel/setup_no.c +0 -1
index 5e4104f07a44..00bf82258233 100644
@@ -55,7 +55,6 @@ int (*mach_hwclk) (int, struct rtc_time*);
 /* machine dependent reboot functions */
 void (*mach_reset)(void);
 void (*mach_halt)(void);
-void (*mach_power_off)(void);
 
 #ifdef CONFIG_M68000
 #if defined(CONFIG_M68328)
arch/m68k/mac/config.c +3 -1
index 5d16f9b47aa9..727320dedf08 100644
@@ -12,6 +12,7 @@
 
 #include <linux/errno.h>
 #include <linux/module.h>
+#include <linux/reboot.h>
 #include <linux/types.h>
 #include <linux/mm.h>
 #include <linux/tty.h>
@@ -139,7 +140,6 @@ void __init config_mac(void)
 	mach_hwclk = mac_hwclk;
 	mach_reset = mac_reset;
 	mach_halt = mac_poweroff;
-	mach_power_off = mac_poweroff;
 #if IS_ENABLED(CONFIG_INPUT_M68K_BEEP)
 	mach_beep = mac_mksound;
 #endif
@@ -159,6 +159,8 @@ void __init config_mac(void)
 
 	if (macintosh_config->ident = MAC_MODEL_IICI)
 		mach_l2_flush = via_l2_flush;
+
+	register_platform_power_off(mac_poweroff);
 }
 
 
Signature · 1 line
2.33.1
#50 lore raw

PATCH v4 17/25 sh: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/sh/kernel/reboot.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/sh/kernel/reboot.c +1 -2
index 5c33f036418b..e8eeedc9b182 100644
@@ -46,8 +46,7 @@ static void native_machine_shutdown(void)
 
 static void native_machine_power_off(void)
 {
-	if (pm_power_off)
-		pm_power_off();
+	do_kernel_power_off();
 }
 
 static void native_machine_halt(void)
Signature · 1 line
2.33.1
#51 lore raw

PATCH v4 18/25 x86: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/x86/kernel/reboot.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/reboot.c +2 -2
index 0a40df66a40d..cd7d9416d81a 100644
@@ -747,10 +747,10 @@ static void native_machine_halt(void)
 
 static void native_machine_power_off(void)
 {
-	if (pm_power_off) {
+	if (kernel_can_power_off()) {
 		if (!reboot_force)
 			machine_shutdown();
-		pm_power_off();
+		do_kernel_power_off();
 	}
 	/* A fallback in case there is no PM info available */
 	tboot_shutdown(TB_SHUTDOWN_HALT);
Signature · 1 line
2.33.1
#54 lore raw

PATCH v4 19/25 ia64: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/ia64/kernel/process.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/ia64/kernel/process.c +2 -2
index 834df24a88f1..cee4d7db2143 100644
@@ -19,6 +19,7 @@
 #include <linux/module.h>
 #include <linux/notifier.h>
 #include <linux/personality.h>
+#include <linux/reboot.h>
 #include <linux/sched.h>
 #include <linux/sched/debug.h>
 #include <linux/sched/hotplug.h>
@@ -599,8 +600,7 @@ machine_halt (void)
 void
 machine_power_off (void)
 {
-	if (pm_power_off)
-		pm_power_off();
+	do_kernel_power_off();
 	machine_halt();
 }
 
Signature · 1 line
2.33.1
#55 lore raw

PATCH v4 20/25 mips: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/mips/kernel/reset.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/mips/kernel/reset.c +1 -2
index 6288780b779e..e7ce07b3e79b 100644
@@ -114,8 +114,7 @@ void machine_halt(void)
 
 void machine_power_off(void)
 {
-	if (pm_power_off)
-		pm_power_off();
+	do_kernel_power_off();
 
 #ifdef CONFIG_SMP
 	preempt_disable();
Signature · 1 line
2.33.1
#56 lore raw

PATCH v4 21/25 nds32: Use do_kernel_power_off()

In reply to Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/nds32/kernel/process.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/nds32/kernel/process.c +1 -2
index 49fab9e39cbf..0936dcd7db1b 100644
@@ -54,8 +54,7 @@ EXPORT_SYMBOL(machine_halt);
 
 void machine_power_off(void)
 {
-	if (pm_power_off)
-		pm_power_off();
+	do_kernel_power_off();
 }
 
 EXPORT_SYMBOL(machine_power_off);
Signature · 1 line
2.33.1
#60 lore raw

PATCH v4 23/25 ACPI: power: Switch to sys-off handler API

In reply to Dmitry Osipenko

Switch to sys-off API that replaces legacy pm_power_off callbacks.

---
 drivers/acpi/sleep.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)
drivers/acpi/sleep.c +11 -14
index eaa47753b758..2e613fddd614 100644
@@ -47,19 +47,11 @@ static void acpi_sleep_tts_switch(u32 acpi_state)
 	}
 }
 
-static int tts_notify_reboot(struct notifier_block *this,
-			unsigned long code, void *x)
+static void tts_reboot_prepare(struct reboot_prep_data *data)
 {
 	acpi_sleep_tts_switch(ACPI_STATE_S5);
-	return NOTIFY_DONE;
 }
 
-static struct notifier_block tts_notifier = {
-	.notifier_call	= tts_notify_reboot,
-	.next		= NULL,
-	.priority	= 0,
-};
-
 static int acpi_sleep_prepare(u32 acpi_state)
 {
 #ifdef CONFIG_ACPI_SLEEP
@@ -1020,7 +1012,7 @@ static void acpi_sleep_hibernate_setup(void)
 static inline void acpi_sleep_hibernate_setup(void) {}
 #endif /* !CONFIG_HIBERNATION */
 
-static void acpi_power_off_prepare(void)
+static void acpi_power_off_prepare(struct power_off_prep_data *data)
 {
 	/* Prepare to power off the system */
 	acpi_sleep_prepare(ACPI_STATE_S5);
@@ -1028,7 +1020,7 @@ static void acpi_power_off_prepare(void)
 	acpi_os_wait_events_complete();
 }
 
-static void acpi_power_off(void)
+static void acpi_power_off(struct power_off_data *data)
 {
 	/* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
 	pr_debug("%s called\n", __func__);
@@ -1036,6 +1028,11 @@ static void acpi_power_off(void)
 	acpi_enter_sleep_state(ACPI_STATE_S5);
 }
 
+static struct sys_off_handler acpi_sys_off_handler = {
+	.power_off_priority = POWEROFF_PRIO_FIRMWARE,
+	.reboot_prepare_cb = tts_reboot_prepare,
+};
+
 int __init acpi_sleep_init(void)
 {
 	char supported[ACPI_S_STATE_COUNT * 3 + 1];
@@ -1052,8 +1049,8 @@ int __init acpi_sleep_init(void)
 
 	if (acpi_sleep_state_supported(ACPI_STATE_S5)) {
 		sleep_states[ACPI_STATE_S5] = 1;
-		pm_power_off_prepare = acpi_power_off_prepare;
-		pm_power_off = acpi_power_off;
+		acpi_sys_off_handler.power_off_cb = acpi_power_off;
+		acpi_sys_off_handler.power_off_prepare_cb = acpi_power_off_prepare;
 	} else {
 		acpi_no_s5 = true;
 	}
@@ -1069,6 +1066,6 @@ int __init acpi_sleep_init(void)
 	 * Register the tts_notifier to reboot notifier list so that the _TTS
 	 * object can also be evaluated when the system enters S5.
 	 */
-	register_reboot_notifier(&tts_notifier);
+	register_sys_off_handler(&acpi_sys_off_handler);
 	return 0;
 }
Signature · 1 line
2.33.1
#61 lore raw

PATCH v4 24/25 regulator: pfuze100: Use devm_register_sys_off_handler()

In reply to Dmitry Osipenko

Use devm_register_sys_off_handler() that replaces global pm_power_off_prepare variable and allows to register multiple power-off handlers.

---
 drivers/regulator/pfuze100-regulator.c | 38 ++++++++++----------------
 1 file changed, 14 insertions(+), 24 deletions(-)
drivers/regulator/pfuze100-regulator.c +14 -24
index d60d7d1b7fa2..2eca8d43a097 100644
@@ -10,6 +10,7 @@
 #include <linux/of_device.h>
 #include <linux/regulator/of_regulator.h>
 #include <linux/platform_device.h>
+#include <linux/reboot.h>
 #include <linux/regulator/driver.h>
 #include <linux/regulator/machine.h>
 #include <linux/regulator/pfuze100.h>
@@ -76,6 +77,7 @@ struct pfuze_chip {
 	struct pfuze_regulator regulator_descs[PFUZE100_MAX_REGULATOR];
 	struct regulator_dev *regulators[PFUZE100_MAX_REGULATOR];
 	struct pfuze_regulator *pfuze_regulators;
+	struct sys_off_handler sys_off;
 };
 
 static const int pfuze100_swbst[] = {
@@ -569,10 +571,10 @@ static inline struct device_node *match_of_node(int index)
 	return pfuze_matches[index].of_node;
 }
 
-static struct pfuze_chip *syspm_pfuze_chip;
-
-static void pfuze_power_off_prepare(void)
+static void pfuze_power_off_prepare(struct power_off_prep_data *data)
 {
+	struct pfuze_chip *syspm_pfuze_chip = data->cb_data;
+
 	dev_info(syspm_pfuze_chip->dev, "Configure standby mode for power off");
 
 	/* Switch from default mode: APS/APS to APS/Off */
@@ -611,24 +613,23 @@ static void pfuze_power_off_prepare(void)
 
 static int pfuze_power_off_prepare_init(struct pfuze_chip *pfuze_chip)
 {
+	int err;
+
 	if (pfuze_chip->chip_id != PFUZE100) {
 		dev_warn(pfuze_chip->dev, "Requested pm_power_off_prepare handler for not supported chip\n");
 		return -ENODEV;
 	}
 
-	if (pm_power_off_prepare) {
-		dev_warn(pfuze_chip->dev, "pm_power_off_prepare is already registered.\n");
-		return -EBUSY;
-	}
+	pfuze_chip->sys_off.power_off_prepare_cb = pfuze_power_off_prepare;
+	pfuze_chip->sys_off.cb_data = pfuze_chip;
 
-	if (syspm_pfuze_chip) {
-		dev_warn(pfuze_chip->dev, "syspm_pfuze_chip is already set.\n");
-		return -EBUSY;
+	err = devm_register_sys_off_handler(pfuze_chip->dev, &pfuze_chip->sys_off);
+	if (err) {
+		dev_err(pfuze_chip->dev,
+			"failed to register sys-off handler: %d\n", err);
+		return err;
 	}
 
-	syspm_pfuze_chip = pfuze_chip;
-	pm_power_off_prepare = pfuze_power_off_prepare;
-
 	return 0;
 }
 
@@ -837,23 +838,12 @@ static int pfuze100_regulator_probe(struct i2c_client *client,
 	return 0;
 }
 
-static int pfuze100_regulator_remove(struct i2c_client *client)
-{
-	if (syspm_pfuze_chip) {
-		syspm_pfuze_chip = NULL;
-		pm_power_off_prepare = NULL;
-	}
-
-	return 0;
-}
-
 static struct i2c_driver pfuze_driver = {
 	.driver = {
 		.name = "pfuze100-regulator",
 		.of_match_table = pfuze_dt_ids,
 	},
 	.probe = pfuze100_regulator_probe,
-	.remove = pfuze100_regulator_remove,
 };
 module_i2c_driver(pfuze_driver);
 
Signature · 1 line
2.33.1
#62 lore raw

PATCH v4 25/25 reboot: Remove pm_power_off_prepare()

In reply to Dmitry Osipenko

All pm_power_off_prepare() users were converted to sys-off handler API. Remove the obsolete callback.

---
 include/linux/pm.h |  1 -
 kernel/reboot.c    | 11 -----------
 2 files changed, 12 deletions(-)
include/linux/pm.h +0 -1
index 1d8209c09686..d9bf1426f81e 100644
@@ -20,7 +20,6 @@
  * Callbacks for platform drivers to implement.
  */
 extern void (*pm_power_off)(void);
-extern void (*pm_power_off_prepare)(void);
 
 struct device; /* we have a circular dep with device.h */
 #ifdef CONFIG_VT_CONSOLE_SLEEP
kernel/reboot.c +0 -11
index 4884204f9a31..a832bb660040 100644
@@ -48,13 +48,6 @@ int reboot_cpu;
 enum reboot_type reboot_type = BOOT_ACPI;
 int reboot_force;
 
-/*
- * If set, this is used for preparing the system to power off.
- */
-
-void (*pm_power_off_prepare)(void);
-EXPORT_SYMBOL_GPL(pm_power_off_prepare);
-
 /**
  *	emergency_restart - reboot the system
  *
@@ -807,10 +800,6 @@ void do_kernel_power_off(void)
 
 static void do_kernel_power_off_prepare(void)
 {
-	/* legacy pm_power_off_prepare() is unchained and has highest priority */
-	if (pm_power_off_prepare)
-		return pm_power_off_prepare();
-
 	blocking_notifier_call_chain(&power_off_handler_list, POWEROFF_PREPARE,
 				     NULL);
 }
Signature · 1 line
2.33.1
#35 lore raw

PATCH v4 8/25 kernel: Add combined power-off+restart handler call chain API

In reply to Dmitry Osipenko

SoC platforms often have multiple ways of how to perform system's power-off and restart operations. Meanwhile today's kernel is limited to a single option. Add combined power-off+restart handler call chain API, which is inspired by the restart API. The new API provides both power-off and restart functionality.

The old pm_power_off method will be kept around till all users are converted to the new API.

Current restart API will be replaced by the new unified API since new API is its superset. The restart functionality of the sys-off handler API is built upon the existing restart-notifier APIs.

In order to ease conversion to the new API, convenient helpers are added for the common use-cases. They will reduce amount of boilerplate code and remove global variables. These helpers preserve old behaviour for cases where only one power-off handler is expected, this is what all existing drivers want, and thus, they could be easily converted to the new API. Users of the new API should explicitly enable power-off chaining by setting corresponding flag of the power_handler structure.

---
 include/linux/reboot.h   | 265 ++++++++++++++++++-
 kernel/power/hibernate.c |   2 +-
 kernel/reboot.c          | 536 ++++++++++++++++++++++++++++++++++++++-
 3 files changed, 795 insertions(+), 8 deletions(-)
include/linux/reboot.h +261 -4
index b7fa25726323..76799bb3a560 100644
@@ -8,10 +8,35 @@
 
 struct device;
 
-#define SYS_DOWN	0x0001	/* Notify of system down */
-#define SYS_RESTART	SYS_DOWN
-#define SYS_HALT	0x0002	/* Notify of system halt */
-#define SYS_POWER_OFF	0x0003	/* Notify of system power off */
+enum reboot_prepare_mode {
+	SYS_DOWN = 1,		/* Notify of system down */
+	SYS_RESTART = SYS_DOWN,
+	SYS_HALT,		/* Notify of system halt */
+	SYS_POWER_OFF,		/* Notify of system power off */
+};
+
+/*
+ * Standard restart priority levels. Intended to be set in the
+ * sys_off_handler.restart_priority field.
+ *
+ * Use `RESTART_PRIO_ABC +- prio` style for additional levels.
+ *
+ * RESTART_PRIO_RESERVED:	Falls back to RESTART_PRIO_DEFAULT.
+ *				Drivers may leave priority initialized
+ *				to zero, to auto-set it to the default level.
+ *
+ * RESTART_PRIO_LOW:		Use this for handler of last resort.
+ *
+ * RESTART_PRIO_DEFAULT:	Use this for default/generic handler.
+ *
+ * RESTART_PRIO_HIGH:		Use this if you have multiple handlers and
+ *				this handler has higher priority than the
+ *				default handler.
+ */
+#define RESTART_PRIO_RESERVED		0
+#define RESTART_PRIO_LOW		8
+#define RESTART_PRIO_DEFAULT		128
+#define RESTART_PRIO_HIGH		192
 
 enum reboot_mode {
 	REBOOT_UNDEFINED = -1,
@@ -49,6 +74,237 @@ int register_restart_handler(struct notifier_block *);
 int unregister_restart_handler(struct notifier_block *);
 void do_kernel_restart(char *cmd);
 
+/*
+ * System power-off and restart API.
+ */
+
+/*
+ * Standard power-off priority levels. Intended to be set in the
+ * sys_off_handler.power_off_priority field.
+ *
+ * Use `POWEROFF_PRIO_ABC +- prio` style for additional levels.
+ *
+ * POWEROFF_PRIO_RESERVED:	Falls back to POWEROFF_PRIO_DEFAULT.
+ *				Drivers may leave priority initialized
+ *				to zero, to auto-set it to the default level.
+ *
+ * POWEROFF_PRIO_PLATFORM:	Intended to be used by platform-level handler.
+ *				Has lowest priority since device drivers are
+ *				expected to take over platform handler which
+ *				doesn't allow further callback chaining.
+ *
+ * POWEROFF_PRIO_DEFAULT:	Use this for default/generic handler.
+ *
+ * POWEROFF_PRIO_FIRMWARE:	Use this if handler uses firmware call.
+ *				Has highest priority since firmware is expected
+ *				to know best how to power-off hardware properly.
+ */
+#define POWEROFF_PRIO_RESERVED		0
+#define POWEROFF_PRIO_PLATFORM		1
+#define POWEROFF_PRIO_DEFAULT		128
+#define POWEROFF_PRIO_HIGH		192
+#define POWEROFF_PRIO_FIRMWARE		224
+
+enum poweroff_mode {
+	POWEROFF_NORMAL = 0,
+	POWEROFF_PREPARE,
+};
+
+/**
+ * struct power_off_data - Power-off callback argument
+ *
+ * @cb_data: Callback data.
+ */
+struct power_off_data {
+	void *cb_data;
+};
+
+/**
+ * struct power_off_prep_data - Power-off preparation callback argument
+ *
+ * @cb_data: Callback data.
+ */
+struct power_off_prep_data {
+	void *cb_data;
+};
+
+/**
+ * struct restart_data - Restart callback argument
+ *
+ * @cb_data: Callback data.
+ * @cmd: Restart command string.
+ * @stop_chain: Further lower priority callbacks won't be executed if set to
+ *		true. Can be changed within callback. Default is false.
+ * @mode: Reboot mode ID.
+ */
+struct restart_data {
+	void *cb_data;
+	const char *cmd;
+	bool stop_chain;
+	enum reboot_mode mode;
+};
+
+/**
+ * struct reboot_prep_data - Reboot and shutdown preparation callback argument
+ *
+ * @cb_data: Callback data.
+ * @cmd: Restart command string.
+ * @stop_chain: Further lower priority callbacks won't be executed if set to
+ *		true. Can be changed within callback. Default is false.
+ * @mode: Preparation mode ID.
+ */
+struct reboot_prep_data {
+	void *cb_data;
+	const char *cmd;
+	bool stop_chain;
+	enum reboot_prepare_mode mode;
+};
+
+struct sys_off_handler_private_data {
+	struct notifier_block power_off_nb;
+	struct notifier_block restart_nb;
+	struct notifier_block reboot_nb;
+	void (*platform_power_off_cb)(void);
+	void (*simple_power_off_cb)(void *data);
+	void *simple_power_off_cb_data;
+	bool registered;
+};
+
+/**
+ * struct sys_off_handler - System power-off and restart handler
+ *
+ * @cb_data: Pointer to user's data.
+ *
+ * @power_off_cb: Callback that powers off this machine. Inactive if NULL.
+ *
+ * @power_off_prepare_cb: Power-off preparation callback. All power-off
+ * preparation callbacks are invoked after @reboot_prepare_cb and before
+ * @power_off_cb. Inactive if NULL.
+ *
+ * @power_off_priority: Power-off callback priority, must be unique.
+ * Zero value is reserved and auto-reassigned to POWEROFF_PRIO_DEFAULT.
+ * Inactive if @power_off_cb is NULL.
+ *
+ * @power_off_chaining_allowed: Set to false if callback's execution should
+ * stop when @power_off_cb fails to power off this machine. True if further
+ * lower priority power-off callback should be executed. False is default
+ * value.
+ *
+ * @restart_cb: Callback that reboots this machine. Inactive if NULL.
+ *
+ * @restart_priority: Restart callback priority, must be unique. Zero value
+ * is reserved and auto-reassigned to RESTART_PRIO_DEFAULT. Inactive if
+ * @restart_cb is NULL.
+ *
+ * @restart_chaining_disallowed: Set to true if callback's execution should
+ * stop when @restart_cb fails to restart this machine. False if further
+ * lower priority restart callback should be executed. False is default
+ * value.
+ *
+ * @reboot_prepare_cb: Reboot/shutdown preparation callback. All reboot
+ * preparation callbacks are invoked before @restart_cb or @power_off_cb,
+ * depending on the mode. It's registered with register_reboot_notifier().
+ * The point is to remove boilerplate code from drivers which use this
+ * callback in conjunction with the restart/power-off callbacks.
+ *
+ * @reboot_priority: Reboot/shutdown preparation callback priority, doesn't
+ * need to be unique. Zero is default value. Inactive if @reboot_prepare_cb
+ * is NULL.
+ *
+ * @priv: Internal data. Shouldn't be touched.
+ *
+ * Describes power-off and restart handlers which are invoked by kernel
+ * to power off or restart this machine. Supports prioritized chaining for
+ * both restart and power-off handlers.
+ *
+ * Struct sys_off_handler can be static. Members of this structure must not be
+ * altered while handler is registered.
+ *
+ * Fill the structure members and pass it to @register_sys_off_handler().
+ */
+struct sys_off_handler {
+	void *cb_data;
+
+	void (*power_off_cb)(struct power_off_data *data);
+	void (*power_off_prepare_cb)(struct power_off_prep_data *data);
+	int power_off_priority;
+	bool power_off_chaining_allowed;
+
+	void (*restart_cb)(struct restart_data *data);
+	int restart_priority;
+	bool restart_chaining_disallowed;
+
+	void (*reboot_prepare_cb)(struct reboot_prep_data *data);
+	int reboot_priority;
+
+	const struct sys_off_handler_private_data priv;
+};
+
+int register_sys_off_handler(struct sys_off_handler *handler);
+int unregister_sys_off_handler(struct sys_off_handler *handler);
+
+int devm_register_sys_off_handler(struct device *dev,
+				  struct sys_off_handler *handler);
+
+int devm_register_prioritized_power_off_handler(struct device *dev,
+						int priority,
+						void (*callback)(void *data),
+						void *cb_data);
+
+/**
+ *	devm_register_simple_power_off_handler - Register simple power-off callback
+ *	@dev: Device that registers callback
+ *	@callback: Callback function
+ *	@cb_data: Callback's argument
+ *
+ *	Registers resource-managed power-off callback with default priority.
+ *	It will be invoked as last step of the power-off sequence. Further
+ *	lower priority callbacks won't be executed if this @callback fails.
+ *
+ *	Returns zero on success, or error code on failure.
+ */
+static inline int
+devm_register_simple_power_off_handler(struct device *dev,
+				       void (*callback)(void *data),
+				       void *cb_data)
+{
+	return devm_register_prioritized_power_off_handler(dev,
+							   POWEROFF_PRIO_DEFAULT,
+							   callback, cb_data);
+}
+
+int register_platform_power_off(void (*power_off)(void));
+int unregister_platform_power_off(void (*power_off)(void));
+
+int devm_register_prioritized_restart_handler(struct device *dev,
+					      int priority,
+					      void (*callback)(struct restart_data *data),
+					      void *cb_data);
+
+/**
+ *	devm_register_simple_restart_handler - Register simple restart callback
+ *	@dev: Device that registers callback
+ *	@callback: Callback function
+ *	@cb_data: Callback's argument
+ *
+ *	Registers resource-managed restart callback with default priority.
+ *	It will be invoked as a part of the restart sequence. Further
+ *	lower priority callback will be executed if this @callback fails.
+ *
+ *	Returns zero on success, or error code on failure.
+ */
+static inline int
+devm_register_simple_restart_handler(struct device *dev,
+				     void (*callback)(struct restart_data *data),
+				     void *cb_data)
+{
+	return devm_register_prioritized_restart_handler(dev,
+							 RESTART_PRIO_DEFAULT,
+							 callback, cb_data);
+}
+
+void do_kernel_power_off(void);
+
 /*
  * Architecture-specific implementations of sys_reboot commands.
  */
@@ -70,6 +326,7 @@ void kernel_restart_prepare(char *cmd);
 void kernel_restart(char *cmd);
 void kernel_halt(void);
 void kernel_power_off(void);
+bool kernel_can_power_off(void);
 
 extern int C_A_D; /* for sysctl */
 void ctrl_alt_del(void);
kernel/power/hibernate.c +1 -1
index e6af502c2fd7..aebbdd757fb9 100644
@@ -666,7 +666,7 @@ static void power_down(void)
 		hibernation_platform_enter();
 		fallthrough;
 	case HIBERNATION_SHUTDOWN:
-		if (pm_power_off)
+		if (kernel_can_power_off())
 			kernel_power_off();
 		break;
 	}
kernel/reboot.c +533 -3

560 lines not on this page · raw message

Signature · 1 line
2.33.1
#57 lore raw

PATCH v4 22/25 memory: emif: Use kernel_can_power_off()

In reply to Dmitry Osipenko

Replace legacy pm_power_off with kernel_can_power_off() helper that is aware about chained power-off handlers.

---
 drivers/memory/emif.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/memory/emif.c +1 -1
index 762d0c0f0716..cab10d5274a0 100644
@@ -630,7 +630,7 @@ static irqreturn_t emif_threaded_isr(int irq, void *dev_id)
 		dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
 
 		/* If we have Power OFF ability, use it, else try restarting */
-		if (pm_power_off) {
+		if (kernel_can_power_off()) {
 			kernel_power_off();
 		} else {
 			WARN(1, "FIXME: NO pm_power_off!!! trying restart\n");
Signature · 7 lines
2.33.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#14 lore raw

Re: [PATCH v4 05/25] reboot: Warn if restart handler has duplicated priority

In reply to Dmitry Osipenko
Quoted 3 lines · Dmitry Osipenko

Add sanity check which ensures that there are no two restart handlers registered with the same priority. Normally it's a direct sign of a problem if two handlers use the same priority.

The patch doesn't ensure the property that there are no duplicated-priority entries on the chain.

I'd rather see a atomic_notifier_chain_register_unique() that returns -EBUSY or something istead of adding an entry with duplicate priority. That way it would need only one list traversal unless you want to register the duplicate anyway (then you would call the older atomic_notifier_chain_register() after reporting the error).

(Or you could return > 0 when a duplicate is registered in atomic_notifier_chain_register() if the callers are prepared for that. I don't really like this way, though.)

Best Regards
Michał Mirosław

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#36 lore raw

Re: [PATCH v4 08/25] kernel: Add combined power-off+restart handler call chain API

In reply to Dmitry Osipenko
Quoted 13 lines · Dmitry Osipenko

SoC platforms often have multiple ways of how to perform system's power-off and restart operations. Meanwhile today's kernel is limited to a single option. Add combined power-off+restart handler call chain API, which is inspired by the restart API. The new API provides both power-off and restart functionality.

The old pm_power_off method will be kept around till all users are converted to the new API.

Current restart API will be replaced by the new unified API since new API is its superset. The restart functionality of the sys-off handler API is built upon the existing restart-notifier APIs.

Quoted 7 lines

In order to ease conversion to the new API, convenient helpers are added for the common use-cases. They will reduce amount of boilerplate code and remove global variables. These helpers preserve old behaviour for cases where only one power-off handler is expected, this is what all existing drivers want, and thus, they could be easily converted to the new API. Users of the new API should explicitly enable power-off chaining by setting corresponding flag of the power_handler structure.

[...]

Hi,

A general question: do we really need three distinct chains for this? Can't there be only one that chain of callbacks that get a stage (RESTART_PREPARE, RESTART, POWER_OFF_PREPARE, POWER_OFF) and can ignore them at will? Calling through POWER_OFF_PREPARE would also return whether that POWER_OFF is possible (for kernel_can_power_off()).

I would also split this patch into preparation cleanups (like wrapping pm_power_off call with a function) and adding the notifier-based implementation.

Best Regards
Michał Mirosław

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#52 lore raw

Re: [PATCH v4 18/25] x86: Use do_kernel_power_off()

In reply to Dmitry Osipenko
Quoted 15 lines · Dmitry Osipenko

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/x86/kernel/reboot.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/reboot.c +0 -0
index 0a40df66a40d..cd7d9416d81a 100644

arch/x86/kernel/reboot.c static void native_machine_halt(void) · line 754

Quoted 10 lines
arch/x86/kernel/reboot.c +2 -2
@@ -747,10 +747,10 @@ static void native_machine_halt(void)
 
 static void native_machine_power_off(void)
 {
-	if (pm_power_off) {
+	if (kernel_can_power_off()) {
 		if (!reboot_force)
 			machine_shutdown();
-		pm_power_off();
+		do_kernel_power_off();
 	}

Judging from an old commit from 2006 [1], this can be rewritten as:

if (!reboot_force && kernel_can_power_off())

	machine_shutdown();

do_kernel_power_off();

And maybe later reworked so it doesn't need kernel_can_power_off().

[1] http://lkml.iu.edu/hypermail//linux/kernel/0511.3/0681.html

Best Regards
Michał Mirosław

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#58 lore raw

Re: [PATCH v4 22/25] memory: emif: Use kernel_can_power_off()

In reply to Dmitry Osipenko
Quoted 13 lines · Dmitry Osipenko

Replace legacy pm_power_off with kernel_can_power_off() helper that is aware about chained power-off handlers.

---
 drivers/memory/emif.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/memory/emif.c +0 -0
index 762d0c0f0716..cab10d5274a0 100644

drivers/memory/emif.c static irqreturn_t emif_threaded_isr(int irq, void *dev_id) · line 636

Quoted 8 lines
drivers/memory/emif.c +1 -1
@@ -630,7 +630,7 @@ static irqreturn_t emif_threaded_isr(int irq, void *dev_id)
 		dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
 
 		/* If we have Power OFF ability, use it, else try restarting */
-		if (pm_power_off) {
+		if (kernel_can_power_off()) {
 			kernel_power_off();
 		} else {
 			WARN(1, "FIXME: NO pm_power_off!!! trying restart\n");

BTW, this part of the code seems to be better moved to generic code that could replace POWER_OFF request with REBOOT like it is done for reboot() syscall.

Best Regards
Michał Mirosław

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#37 lore raw

Re: [PATCH v4 08/25] kernel: Add combined power-off+restart handler call chain API

In reply to Michał Mirosław

28.11.2021 03:43, Michał Mirosław пишет:

Quoted 21 lines · Dmitry Osipenko
Quoted 13 lines

SoC platforms often have multiple ways of how to perform system's power-off and restart operations. Meanwhile today's kernel is limited to a single option. Add combined power-off+restart handler call chain API, which is inspired by the restart API. The new API provides both power-off and restart functionality.

The old pm_power_off method will be kept around till all users are converted to the new API.

Current restart API will be replaced by the new unified API since new API is its superset. The restart functionality of the sys-off handler API is built upon the existing restart-notifier APIs.

Quoted 7 lines

In order to ease conversion to the new API, convenient helpers are added for the common use-cases. They will reduce amount of boilerplate code and remove global variables. These helpers preserve old behaviour for cases where only one power-off handler is expected, this is what all existing drivers want, and thus, they could be easily converted to the new API. Users of the new API should explicitly enable power-off chaining by setting corresponding flag of the power_handler structure.

Quoted 5 lines

[...]

Hi,

A general question: do we really need three distinct chains for this?

Hello Michał,

At minimum this makes code easier to follow.

Quoted 4 lines

Can't there be only one that chain of callbacks that get a stage (RESTART_PREPARE, RESTART, POWER_OFF_PREPARE, POWER_OFF) and can ignore them at will? Calling through POWER_OFF_PREPARE would also return whether that POWER_OFF is possible (for kernel_can_power_off()).

I'm having trouble with parsing this comment. Could you please try to rephrase it? I don't see how you could check whether power-off handler is available if you'll mix all handlers together.

Quoted 3 lines

I would also split this patch into preparation cleanups (like wrapping pm_power_off call with a function) and adding the notifier-based implementation.

What's the benefit of this split up will be? Are you suggesting that it will ease reviewing of this patch or something else?

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#59 lore raw

Re: [PATCH v4 22/25] memory: emif: Use kernel_can_power_off()

In reply to Michał Mirosław

28.11.2021 04:23, Michał Mirosław пишет:

Quoted 16 lines · Dmitry Osipenko
Quoted 15 lines

Replace legacy pm_power_off with kernel_can_power_off() helper that is aware about chained power-off handlers.

---
 drivers/memory/emif.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/memory/emif.c +0 -0
index 762d0c0f0716..cab10d5274a0 100644
@@ -630,7 +630,7 @@ static irqreturn_t emif_threaded_isr(int irq, void *dev_id)
 		dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
 
Quoted 10 lines

drivers/memory/emif.c static irqreturn_t emif_threaded_isr(int irq, void *dev_id) · line 636

Quoted 6 lines
drivers/memory/emif.c +1 -1
 		/* If we have Power OFF ability, use it, else try restarting */
-		if (pm_power_off) {
+		if (kernel_can_power_off()) {
 			kernel_power_off();
 		} else {
 			WARN(1, "FIXME: NO pm_power_off!!! trying restart\n");

BTW, this part of the code seems to be better moved to generic code that could replace POWER_OFF request with REBOOT like it is done for reboot() syscall.

Not sure that it can be done. Somebody will have to verify that it won't break all those platform power-off handlers. Better to keep this code as-is in the context of this patchset.

#15 lore raw

Re: [PATCH v4 05/25] reboot: Warn if restart handler has duplicated priority

In reply to Michał Mirosław

28.11.2021 03:28, Michał Mirosław пишет:

Quoted 7 lines · Dmitry Osipenko
Quoted 3 lines

Add sanity check which ensures that there are no two restart handlers registered with the same priority. Normally it's a direct sign of a problem if two handlers use the same priority.

The patch doesn't ensure the property that there are no duplicated-priority entries on the chain.

It's not the exact point of this patch.

Quoted 5 lines

I'd rather see a atomic_notifier_chain_register_unique() that returns -EBUSY or something istead of adding an entry with duplicate priority. That way it would need only one list traversal unless you want to register the duplicate anyway (then you would call the older atomic_notifier_chain_register() after reporting the error).

The point of this patch is to warn developers about the problem that needs to be fixed. We already have such troubling drivers in mainline.

It's not critical to register different handlers with a duplicated priorities, but such cases really need to be corrected. We shouldn't break users' machines during transition to the new API, meanwhile developers should take action of fixing theirs drivers.

Quoted 3 lines

(Or you could return > 0 when a duplicate is registered in atomic_notifier_chain_register() if the callers are prepared for that. I don't really like this way, though.)

I had a similar thought at some point before and decided that I'm not in favor of this approach. It's nicer to have a dedicated function that verifies the uniqueness, IMO.

#53 lore raw

Re: [PATCH v4 18/25] x86: Use do_kernel_power_off()

In reply to Michał Mirosław

28.11.2021 04:15, Michał Mirosław пишет:

Quoted 29 lines · Dmitry Osipenko
Quoted 15 lines

Kernel now supports chained power-off handlers. Use do_kernel_power_off() that invokes chained power-off handlers. It also invokes legacy pm_power_off() for now, which will be removed once all drivers will be converted to the new power-off API.

---
 arch/x86/kernel/reboot.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/reboot.c +0 -0
index 0a40df66a40d..cd7d9416d81a 100644

arch/x86/kernel/reboot.c static void native_machine_halt(void) · line 754

Quoted 10 lines
arch/x86/kernel/reboot.c +2 -2
@@ -747,10 +747,10 @@ static void native_machine_halt(void)
 
 static void native_machine_power_off(void)
 {
-	if (pm_power_off) {
+	if (kernel_can_power_off()) {
 		if (!reboot_force)
 			machine_shutdown();
-		pm_power_off();
+		do_kernel_power_off();
 	}

Judging from an old commit from 2006 [1], this can be rewritten as:

Quoted 7 lines

if (!reboot_force && kernel_can_power_off())

	machine_shutdown();

do_kernel_power_off();

And maybe later reworked so it doesn't need kernel_can_power_off().

[1] http://lkml.iu.edu/hypermail//linux/kernel/0511.3/0681.html

It could be rewritten like you're suggesting, but I'd prefer to keep the old variant, for clarity.

#38 lore raw

Re: [PATCH v4 08/25] kernel: Add combined power-off+restart handler call chain API

In reply to Dmitry Osipenko
Quoted 32 lines · Dmitry Osipenko

28.11.2021 03:43, Michał Mirosław пишет:

Quoted 21 lines · Dmitry Osipenko
Quoted 13 lines

SoC platforms often have multiple ways of how to perform system's power-off and restart operations. Meanwhile today's kernel is limited to a single option. Add combined power-off+restart handler call chain API, which is inspired by the restart API. The new API provides both power-off and restart functionality.

The old pm_power_off method will be kept around till all users are converted to the new API.

Current restart API will be replaced by the new unified API since new API is its superset. The restart functionality of the sys-off handler API is built upon the existing restart-notifier APIs.

Quoted 7 lines

In order to ease conversion to the new API, convenient helpers are added for the common use-cases. They will reduce amount of boilerplate code and remove global variables. These helpers preserve old behaviour for cases where only one power-off handler is expected, this is what all existing drivers want, and thus, they could be easily converted to the new API. Users of the new API should explicitly enable power-off chaining by setting corresponding flag of the power_handler structure.

Quoted 5 lines

[...]

Hi,

A general question: do we really need three distinct chains for this?

Hello Michał,

At minimum this makes code easier to follow.

Quoted 8 lines
Quoted 4 lines

Can't there be only one that chain of callbacks that get a stage (RESTART_PREPARE, RESTART, POWER_OFF_PREPARE, POWER_OFF) and can ignore them at will? Calling through POWER_OFF_PREPARE would also return whether that POWER_OFF is possible (for kernel_can_power_off()).

I'm having trouble with parsing this comment. Could you please try to rephrase it? I don't see how you could check whether power-off handler is available if you'll mix all handlers together.

If notify_call_chain() would be fixed to return NOTIFY_OK if any call returned NOTIFY_OK, then this would be a clear way to gather the answer if any of the handlers will attempt the final action (reboot or power off).

Quoted 7 lines
Quoted 4 lines

I would also split this patch into preparation cleanups (like wrapping pm_power_off call with a function) and adding the notifier-based implementation.

What's the benefit of this split up will be? Are you suggesting that it will ease reviewing of this patch or something else?

Mainly to ease review, as the wrapping will be a no-op, but the addition of notifier chain changes semantics a bit.

Best Regards
Michał Mirosław

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#39 lore raw

Re: [PATCH v4 08/25] kernel: Add combined power-off+restart handler call chain API

29.11.2021 00:17, Michał Mirosław пишет:

Quoted 8 lines
Quoted 3 lines

I'm having trouble with parsing this comment. Could you please try to rephrase it? I don't see how you could check whether power-off handler is available if you'll mix all handlers together.

If notify_call_chain() would be fixed to return NOTIFY_OK if any call returned NOTIFY_OK, then this would be a clear way to gather the answer if any of the handlers will attempt the final action (reboot or power off).

Could you please show a code snippet that implements your suggestion?

#16 lore raw

Re: [PATCH v4 05/25] reboot: Warn if restart handler has duplicated priority

In reply to Dmitry Osipenko
Quoted 25 lines · Dmitry Osipenko

28.11.2021 03:28, Michał Mirosław пишет:

Quoted 7 lines · Dmitry Osipenko
Quoted 3 lines

Add sanity check which ensures that there are no two restart handlers registered with the same priority. Normally it's a direct sign of a problem if two handlers use the same priority.

The patch doesn't ensure the property that there are no duplicated-priority entries on the chain.

It's not the exact point of this patch.

Quoted 5 lines

I'd rather see a atomic_notifier_chain_register_unique() that returns -EBUSY or something istead of adding an entry with duplicate priority. That way it would need only one list traversal unless you want to register the duplicate anyway (then you would call the older atomic_notifier_chain_register() after reporting the error).

The point of this patch is to warn developers about the problem that needs to be fixed. We already have such troubling drivers in mainline.

It's not critical to register different handlers with a duplicated priorities, but such cases really need to be corrected. We shouldn't break users' machines during transition to the new API, meanwhile developers should take action of fixing theirs drivers.

Quoted 7 lines
Quoted 3 lines

(Or you could return > 0 when a duplicate is registered in atomic_notifier_chain_register() if the callers are prepared for that. I don't really like this way, though.)

I had a similar thought at some point before and decided that I'm not in favor of this approach. It's nicer to have a dedicated function that verifies the uniqueness, IMO.

I don't like the part that it traverses the list second time to check the uniqueness. But actually you could avoid that if notifier_chain_register() would always add equal-priority entries in reverse order:

static int notifier_chain_register(struct notifier_block **nl,
struct notifier_block *n)
{
while ((*nl) != NULL) {
if (unlikely((*nl) == n)) {
WARN(1, "double register detected"); return 0;
}
- if (n->priority > (*nl)->priority)
+ if (n->priority >= (*nl)->priority)
break;
nl = &((*nl)->next);
}
n->next = *nl;
rcu_assign_pointer(*nl, n);
return 0;
}

Then the check for uniqueness after adding would be:

WARN(nb->next && nb->priority == nb->next->priority);

Best Regards
Michał Mirosław

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#40 lore raw

Re: [PATCH v4 08/25] kernel: Add combined power-off+restart handler call chain API

In reply to Dmitry Osipenko
Quoted 9 lines · Dmitry Osipenko

29.11.2021 00:17, Michał Mirosław пишет:

Quoted 7 lines
Quoted 3 lines

I'm having trouble with parsing this comment. Could you please try to rephrase it? I don't see how you could check whether power-off handler is available if you'll mix all handlers together.

If notify_call_chain() would be fixed to return NOTIFY_OK if any call returned NOTIFY_OK, then this would be a clear way to gather the answer if any of the handlers will attempt the final action (reboot or power off).

Could you please show a code snippet that implements your suggestion?

A rough idea is this:

static int notifier_call_chain(struct notifier_block **nl,
unsigned long val, void *v, int nr_to_call, int *nr_calls)
{
- int ret = NOTIFY_DONE;
+ int ret, result = NOTIFY_DONE;
struct notifier_block *nb, *next_nb;

nb = rcu_dereference_raw(*nl);

while (nb && nr_to_call) {
...
ret = nb->notifier_call(nb, val, v);
+
+ /* Assuming NOTIFY_STOP-carrying return is always greater than non-stopping one. */
+ if (result < ret)
+ result = ret;
...
}
- return ret;
+ return result;
}

Then:

bool prepare_reboot()

{
	int ret = xx_notifier_call_chain(&shutdown_notifier, PREPARE_REBOOT, ...);
	return ret == NOTIFY_OK;
}

And the return value would signify whether the reboot will be attempted when calling the chain for the REBOOT action. (Analogously for powering off.)

Best Regards
Michał Mirosław

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#17 lore raw

Re: [PATCH v4 05/25] reboot: Warn if restart handler has duplicated priority

In reply to Michał Mirosław

29.11.2021 03:26, Michał Mirosław пишет:

Quoted 57 lines · Dmitry Osipenko
Quoted 25 lines

28.11.2021 03:28, Michał Mirosław пишет:

Quoted 7 lines · Dmitry Osipenko
Quoted 3 lines

Add sanity check which ensures that there are no two restart handlers registered with the same priority. Normally it's a direct sign of a problem if two handlers use the same priority.

The patch doesn't ensure the property that there are no duplicated-priority entries on the chain.

It's not the exact point of this patch.

Quoted 5 lines

I'd rather see a atomic_notifier_chain_register_unique() that returns -EBUSY or something istead of adding an entry with duplicate priority. That way it would need only one list traversal unless you want to register the duplicate anyway (then you would call the older atomic_notifier_chain_register() after reporting the error).

The point of this patch is to warn developers about the problem that needs to be fixed. We already have such troubling drivers in mainline.

It's not critical to register different handlers with a duplicated priorities, but such cases really need to be corrected. We shouldn't break users' machines during transition to the new API, meanwhile developers should take action of fixing theirs drivers.

Quoted 7 lines
Quoted 3 lines

(Or you could return > 0 when a duplicate is registered in atomic_notifier_chain_register() if the callers are prepared for that. I don't really like this way, though.)

I had a similar thought at some point before and decided that I'm not in favor of this approach. It's nicer to have a dedicated function that verifies the uniqueness, IMO.

I don't like the part that it traverses the list second time to check the uniqueness. But actually you could avoid that if notifier_chain_register() would always add equal-priority entries in reverse order:

static int notifier_chain_register(struct notifier_block **nl,

(quoted diff) +1 -1
 		struct notifier_block *n)
 {
 	while ((*nl) != NULL) {
 		if (unlikely((*nl) = n)) {
 			WARN(1, "double register detected");
 			return 0;
 		}
-		if (n->priority > (*nl)->priority)
+		if (n->priority >= (*nl)->priority)
 			break;
 		nl = &((*nl)->next);
 	}
 	n->next = *nl;
 	rcu_assign_pointer(*nl, n);
 	return 0;
 }
Quoted 3 lines

Then the check for uniqueness after adding would be:

WARN(nb->next && nb->priority = nb->next->priority);

We can't just change the registration order because invocation order of the call chain depends on the registration order and some of current users may rely on that order. I'm pretty sure that changing the order will have unfortunate consequences.

#41 lore raw

Re: [PATCH v4 08/25] kernel: Add combined power-off+restart handler call chain API

In reply to Michał Mirosław

29.11.2021 03:36, Michał Mirosław пишет:

Quoted 38 lines · Dmitry Osipenko
Quoted 9 lines

29.11.2021 00:17, Michał Mirosław пишет:

Quoted 7 lines
Quoted 3 lines

I'm having trouble with parsing this comment. Could you please try to rephrase it? I don't see how you could check whether power-off handler is available if you'll mix all handlers together.

If notify_call_chain() would be fixed to return NOTIFY_OK if any call returned NOTIFY_OK, then this would be a clear way to gather the answer if any of the handlers will attempt the final action (reboot or power off).

Could you please show a code snippet that implements your suggestion?

A rough idea is this:

static int notifier_call_chain(struct notifier_block **nl,

(quoted diff) +6 -2
 			       unsigned long val, void *v,
 			       int nr_to_call, int *nr_calls)
 {
-	int ret = NOTIFY_DONE;
+	int ret, result = NOTIFY_DONE;
 	struct notifier_block *nb, *next_nb;
 
 	nb = rcu_dereference_raw(*nl);
 
 	while (nb && nr_to_call) {
...
 		ret = nb->notifier_call(nb, val, v);
+
+		/* Assuming NOTIFY_STOP-carrying return is always greater than non-stopping one. */
+		if (result < ret)
+			result = ret;
...
 	}
-	return ret;
+	return result;
 }

Then:

Quoted 8 lines

bool prepare_reboot()

{
	int ret = xx_notifier_call_chain(&shutdown_notifier, PREPARE_REBOOT, ...);
	return ret = NOTIFY_OK;
}

And the return value would signify whether the reboot will be attempted when calling the chain for the REBOOT action. (Analogously for powering off.)

If you started to execute call chain, then you began the power-off / restart sequence, this is a point of no return. Sorry, I still don't understand what you're trying to achieve.

The approach of having separate call chains is simple and intuitive, I don't see reasons to change it.

#31 lore raw

Re: [PATCH v4 07/25] reboot: Remove extern annotation from function prototypes

In reply to Dmitry Osipenko
Quoted 5 lines · Dmitry Osipenko

There is no need to annotate function prototypes with 'extern', it makes code less readable. Remove unnecessary annotations from <reboot.h>.

I'm not sure that this is really useful.

Personally, I tend to respect the existing conventions like this.

Surely, this change is not required for the rest of the series to work.

Quoted 83 lines
---
 include/linux/reboot.h | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
include/linux/reboot.h +19 -20
index 7c288013a3ca..b7fa25726323 100644
@@ -40,36 +40,36 @@ extern int reboot_cpu;
 extern int reboot_force;
 
 
-extern int register_reboot_notifier(struct notifier_block *);
-extern int unregister_reboot_notifier(struct notifier_block *);
+int register_reboot_notifier(struct notifier_block *);
+int unregister_reboot_notifier(struct notifier_block *);
 
-extern int devm_register_reboot_notifier(struct device *, struct notifier_block *);
+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);
+int register_restart_handler(struct notifier_block *);
+int unregister_restart_handler(struct notifier_block *);
+void do_kernel_restart(char *cmd);
 
 /*
  * Architecture-specific implementations of sys_reboot commands.
  */
 
-extern void migrate_to_reboot_cpu(void);
-extern void machine_restart(char *cmd);
-extern void machine_halt(void);
-extern void machine_power_off(void);
+void migrate_to_reboot_cpu(void);
+void machine_restart(char *cmd);
+void machine_halt(void);
+void machine_power_off(void);
 
-extern void machine_shutdown(void);
+void machine_shutdown(void);
 struct pt_regs;
-extern void machine_crash_shutdown(struct pt_regs *);
+void machine_crash_shutdown(struct pt_regs *);
 
 /*
  * Architecture independent implementations of sys_reboot commands.
  */
 
-extern void kernel_restart_prepare(char *cmd);
-extern void kernel_restart(char *cmd);
-extern void kernel_halt(void);
-extern void kernel_power_off(void);
+void kernel_restart_prepare(char *cmd);
+void kernel_restart(char *cmd);
+void kernel_halt(void);
+void kernel_power_off(void);
 
 extern int C_A_D; /* for sysctl */
 void ctrl_alt_del(void);
@@ -77,15 +77,15 @@ void ctrl_alt_del(void);
 #define POWEROFF_CMD_PATH_LEN  256
 extern char poweroff_cmd[POWEROFF_CMD_PATH_LEN];
 
-extern void orderly_poweroff(bool force);
-extern void orderly_reboot(void);
+void orderly_poweroff(bool force);
+void orderly_reboot(void);
 void hw_protection_shutdown(const char *reason, int ms_until_forced);
 
 /*
  * Emergency restart, callable from an interrupt handler.
  */
 
-extern void emergency_restart(void);
+void emergency_restart(void);
 #include <asm/emergency-restart.h>
 
 #endif /* _LINUX_REBOOT_H */
--

2.33.1

#3 lore raw

Re: [PATCH v4 02/25] notifier: Add blocking_notifier_call_chain_is_empty()

In reply to Dmitry Osipenko
Quoted 34 lines · Dmitry Osipenko

Add blocking_notifier_call_chain_is_empty() that returns true if call chain is empty.

---
 include/linux/notifier.h |  2 ++
 kernel/notifier.c        | 14 ++++++++++++++
 2 files changed, 16 insertions(+)
include/linux/notifier.h +2 -0
index 4b80a815b666..924c9d7c8e73 100644
@@ -173,6 +173,8 @@ int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh,
 int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
                unsigned long val_up, unsigned long val_down, void *v);
 
+bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh);
+
 #define NOTIFY_DONE            0x0000          /* Don't care */
 #define NOTIFY_OK              0x0001          /* Suits me */
 #define NOTIFY_STOP_MASK       0x8000          /* Don't call further */
kernel/notifier.c +3 -0
index b8251dc0bc0f..b20cb7b9b1f0 100644
@@ -322,6 +322,20 @@ int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
 }
 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
 
+/**
+ *     blocking_notifier_call_chain_is_empty - Check whether notifier chain is empty
+ *     @nh: Pointer to head of the blocking notifier chain

kernel/notifier.c int blocking_notifier_call_chain(struct blocking_notifier_head *nh, · line 337

Quoted 10 lines
kernel/notifier.c +10 -0
+ *
+ *     Checks whether notifier chain is empty.
+ *
+ *     Returns true is notifier chain is empty, false otherwise.
+ */
+bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh)
+{
+       return !rcu_access_pointer(nh->head);
+}
+EXPORT_SYMBOL_GPL(blocking_notifier_call_chain_is_empty);

The check is not reliable (racy) without locking, so I wouldn't export anything like this to modules.

At least IMO it should be added along with a user.

#32 lore raw

Re: [PATCH v4 07/25] reboot: Remove extern annotation from function prototypes

In reply to Rafael J. Wysocki

10.12.2021 21:09, Rafael J. Wysocki пишет:

Quoted 5 lines · Dmitry Osipenko
Quoted 4 lines

There is no need to annotate function prototypes with 'extern', it makes code less readable. Remove unnecessary annotations from <reboot.h>.

Quoted 7 lines
Quoted 1 line

I'm not sure that this is really useful.

Personally, I tend to respect the existing conventions like this.

Surely, this change is not required for the rest of the series to work.

Problem that such things start to spread all over the kernel with a copy-paste approach if there is nobody to clean up the code.

This is not a common convention and sometimes it's getting corrected [1].

[1] https://git.kernel.org/linus/6d7434931

#4 lore raw

Re: [PATCH v4 02/25] notifier: Add blocking_notifier_call_chain_is_empty()

In reply to Rafael J. Wysocki

10.12.2021 21:14, Rafael J. Wysocki пишет:

Quoted 49 lines · Dmitry Osipenko
Quoted 34 lines

Add blocking_notifier_call_chain_is_empty() that returns true if call chain is empty.

---
 include/linux/notifier.h |  2 ++
 kernel/notifier.c        | 14 ++++++++++++++
 2 files changed, 16 insertions(+)
include/linux/notifier.h +2 -0
index 4b80a815b666..924c9d7c8e73 100644
@@ -173,6 +173,8 @@ int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh,
 int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
                unsigned long val_up, unsigned long val_down, void *v);
 
+bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh);
+
 #define NOTIFY_DONE            0x0000          /* Don't care */
 #define NOTIFY_OK              0x0001          /* Suits me */
 #define NOTIFY_STOP_MASK       0x8000          /* Don't call further */
kernel/notifier.c +3 -0
index b8251dc0bc0f..b20cb7b9b1f0 100644
@@ -322,6 +322,20 @@ int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
 }
 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
 
+/**
+ *     blocking_notifier_call_chain_is_empty - Check whether notifier chain is empty
+ *     @nh: Pointer to head of the blocking notifier chain

kernel/notifier.c int blocking_notifier_call_chain(struct blocking_notifier_head *nh, · line 337

Quoted 10 lines
kernel/notifier.c +10 -0
+ *
+ *     Checks whether notifier chain is empty.
+ *
+ *     Returns true is notifier chain is empty, false otherwise.
+ */
+bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh)
+{
+       return !rcu_access_pointer(nh->head);
+}
+EXPORT_SYMBOL_GPL(blocking_notifier_call_chain_is_empty);

The check is not reliable (racy) without locking, so I wouldn't export anything like this to modules.

Quoted 2 lines

At least IMO it should be added along with a user.

I'll remove the export since it's indeed not obvious how other users may want to use this function.

#6 lore raw

Re: [PATCH v4 03/25] notifier: Add atomic/blocking_notifier_has_unique_priority()

In reply to Dmitry Osipenko
Quoted 63 lines · Dmitry Osipenko

Add atomic/blocking_notifier_has_unique_priority() helpers which return true if given handler has unique priority.

---
 include/linux/notifier.h |  5 +++
 kernel/notifier.c        | 69 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+)
include/linux/notifier.h +5 -0
index 924c9d7c8e73..2c4036f225e1 100644
@@ -175,6 +175,11 @@ int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
 
 bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh);
 
+bool atomic_notifier_has_unique_priority(struct atomic_notifier_head *nh,
+               struct notifier_block *nb);
+bool blocking_notifier_has_unique_priority(struct blocking_notifier_head *nh,
+               struct notifier_block *nb);
+
 #define NOTIFY_DONE            0x0000          /* Don't care */
 #define NOTIFY_OK              0x0001          /* Suits me */
 #define NOTIFY_STOP_MASK       0x8000          /* Don't call further */
kernel/notifier.c +22 -0
index b20cb7b9b1f0..7a325b742104 100644
@@ -122,6 +122,19 @@ static int notifier_call_chain_robust(struct notifier_block **nl,
        return ret;
 }
 
+static int notifier_has_unique_priority(struct notifier_block **nl,
+                                       struct notifier_block *n)
+{
+       while (*nl && (*nl)->priority >= n->priority) {
+               if ((*nl)->priority = n->priority && *nl != n)
+                       return false;
+
+               nl = &((*nl)->next);
+       }
+
+       return true;
+}
+
 /*
  *     Atomic notifier chain routines.  Registration and unregistration
  *     use a spinlock, and call_chain is synchronized by RCU (no locks).
@@ -203,6 +216,30 @@ int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
 NOKPROBE_SYMBOL(atomic_notifier_call_chain);
 
+/**
+ *     atomic_notifier_has_unique_priority - Checks whether notifier's priority is unique
+ *     @nh: Pointer to head of the atomic notifier chain
+ *     @n: Entry in notifier chain to check
+ *
+ *     Checks whether there is another notifier in the chain with the same priority.
+ *     Must be called in process context.
+ *
+ *     Returns true if priority is unique, false otherwise.

kernel/notifier.c int atomic_notifier_call_chain(struct atomic_notifier_head *nh, · line 237

Quoted 10 lines
kernel/notifier.c +10 -0
+ */
+bool atomic_notifier_has_unique_priority(struct atomic_notifier_head *nh,
+               struct notifier_block *n)
+{
+       unsigned long flags;
+       bool ret;
+
+       spin_lock_irqsave(&nh->lock, flags);
+       ret = notifier_has_unique_priority(&nh->head, n);
+       spin_unlock_irqrestore(&nh->lock, flags);

This only works if the caller can prevent new entries from being added to the list at this point or if the caller knows that they cannot be added for some reason, but the kerneldoc doesn't mention this limitation.

Quoted 23 lines
kernel/notifier.c +16 -0
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(atomic_notifier_has_unique_priority);
+
 /*
  *     Blocking notifier chain routines.  All access to the chain is
  *     synchronized by an rwsem.
@@ -336,6 +373,38 @@ bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh)
 }
 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain_is_empty);
 
+/**
+ *     blocking_notifier_has_unique_priority - Checks whether notifier's priority is unique
+ *     @nh: Pointer to head of the blocking notifier chain
+ *     @n: Entry in notifier chain to check
+ *
+ *     Checks whether there is another notifier in the chain with the same priority.
+ *     Must be called in process context.
+ *
+ *     Returns true if priority is unique, false otherwise.
+ */
+bool blocking_notifier_has_unique_priority(struct blocking_notifier_head *nh,

kernel/notifier.c bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh) · line 396

Quoted 10 lines
kernel/notifier.c +10 -0
+               struct notifier_block *n)
+{
+       bool ret;
+
+       /*
+        * This code gets used during boot-up, when task switching is
+        * not yet working and interrupts must remain disabled. At such
+        * times we must not call down_read().
+        */
+       if (system_state != SYSTEM_BOOTING)

No, please don't do this, it makes the whole thing error-prone.

kernel/notifier.c bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh) · line 402

Quoted 6 lines
kernel/notifier.c +6 -0
+               down_read(&nh->rwsem);
+
+       ret = notifier_has_unique_priority(&nh->head, n);
+
+       if (system_state != SYSTEM_BOOTING)
+               up_read(&nh->rwsem);

And still what if a new entry with a non-unique priority is added to the chain at this point?

Quoted 11 lines
kernel/notifier.c +5 -1
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(blocking_notifier_has_unique_priority);
+
 /*
  *     Raw notifier chain routines.  There is no protection;
  *     the caller must provide it.  Use at your own risk!
--

2.33.1

#12 lore raw

Re: [PATCH v4 04/25] reboot: Correct typo in a comment

In reply to Dmitry Osipenko
Quoted 4 lines · Dmitry Osipenko

Correct s/implemenations/implementations/ in <reboot.h>.

This patch clearly need not be part of this series.

Quoted 20 lines
---
 include/linux/reboot.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/reboot.h +1 -2
index af907a3d68d1..7c288013a3ca 100644
@@ -63,7 +63,7 @@ struct pt_regs;
 extern void machine_crash_shutdown(struct pt_regs *);
 
 /*
- * Architecture independent implemenations of sys_reboot commands.
+ * Architecture independent implementations of sys_reboot commands.
  */
 
 extern void kernel_restart_prepare(char *cmd);
--

2.33.1

#18 lore raw

Re: [PATCH v4 05/25] reboot: Warn if restart handler has duplicated priority

In reply to Dmitry Osipenko
Quoted 59 lines · Dmitry Osipenko

29.11.2021 03:26, Michał Mirosław пишет:

Quoted 57 lines · Dmitry Osipenko
Quoted 25 lines

28.11.2021 03:28, Michał Mirosław пишет:

Quoted 7 lines · Dmitry Osipenko
Quoted 3 lines

Add sanity check which ensures that there are no two restart handlers registered with the same priority. Normally it's a direct sign of a problem if two handlers use the same priority.

The patch doesn't ensure the property that there are no duplicated-priority entries on the chain.

It's not the exact point of this patch.

Quoted 5 lines

I'd rather see a atomic_notifier_chain_register_unique() that returns -EBUSY or something istead of adding an entry with duplicate priority. That way it would need only one list traversal unless you want to register the duplicate anyway (then you would call the older atomic_notifier_chain_register() after reporting the error).

The point of this patch is to warn developers about the problem that needs to be fixed. We already have such troubling drivers in mainline.

It's not critical to register different handlers with a duplicated priorities, but such cases really need to be corrected. We shouldn't break users' machines during transition to the new API, meanwhile developers should take action of fixing theirs drivers.

Quoted 7 lines
Quoted 3 lines

(Or you could return > 0 when a duplicate is registered in atomic_notifier_chain_register() if the callers are prepared for that. I don't really like this way, though.)

I had a similar thought at some point before and decided that I'm not in favor of this approach. It's nicer to have a dedicated function that verifies the uniqueness, IMO.

I don't like the part that it traverses the list second time to check the uniqueness. But actually you could avoid that if notifier_chain_register() would always add equal-priority entries in reverse order:

static int notifier_chain_register(struct notifier_block **nl,
struct notifier_block *n)

 {
      while ((*nl) != NULL) {
              if (unlikely((*nl) = n)) {
                      WARN(1, "double register detected");
                      return 0;
              }
(quoted diff) +1 -1
-             if (n->priority > (*nl)->priority)
+             if (n->priority >= (*nl)->priority)
                      break;
              nl = &((*nl)->next);
      }
      n->next = *nl;
      rcu_assign_pointer(*nl, n);
      return 0;
 }
Quoted 6 lines
Quoted 3 lines

Then the check for uniqueness after adding would be:

WARN(nb->next && nb->priority = nb->next->priority);

We can't just change the registration order because invocation order of the call chain depends on the registration order

It doesn't if unique priorities are required and isn't that what you want?

Quoted 3 lines

and some of current
users may rely on that order. I'm pretty sure that changing the order will have unfortunate consequences.

Well, the WARN() doesn't help much then.

Either you can make all of the users register with unique priorities, and then you can make the registration reject non-unique ones, or you cannot assume them to be unique.

#26 lore raw

Re: [PATCH v4 06/25] reboot: Warn if unregister_restart_handler() fails

In reply to Dmitry Osipenko
Quoted 14 lines · Dmitry Osipenko

Emit warning if unregister_restart_handler() fails since it never should fail. This will ease further API development by catching mistakes early.

---
 kernel/reboot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/reboot.c +0 -0
index e6659ae329f1..f0e7b9c13f6b 100644

kernel/reboot.c EXPORT_SYMBOL(register_restart_handler); · line 213

Quoted 5 lines
kernel/reboot.c +1 -1
@@ -210,7 +210,7 @@ EXPORT_SYMBOL(register_restart_handler);
  */
 int unregister_restart_handler(struct notifier_block *nb)
 {
-       return atomic_notifier_chain_unregister(&restart_handler_list, nb);
+       return WARN_ON(atomic_notifier_chain_unregister(&restart_handler_list, nb));

The only reason why it can fail is if the object pointed to by nb is not in the chain. Why WARN() about this? And what about systems with panic_on_warn set?

Quoted 6 lines
kernel/reboot.c +0 -1
 }
 EXPORT_SYMBOL(unregister_restart_handler);
 
--

2.33.1

#33 lore raw

Re: [PATCH v4 07/25] reboot: Remove extern annotation from function prototypes

In reply to Dmitry Osipenko
Quoted 13 lines · Dmitry Osipenko

10.12.2021 21:09, Rafael J. Wysocki пишет:

Quoted 11 lines · Dmitry Osipenko
Quoted 5 lines

There is no need to annotate function prototypes with 'extern', it makes code less readable. Remove unnecessary annotations from <reboot.h>.

I'm not sure that this is really useful.

Personally, I tend to respect the existing conventions like this.

Quoted 8 lines
Quoted 1 line

Surely, this change is not required for the rest of the series to work.

Problem that such things start to spread all over the kernel with a copy-paste approach if there is nobody to clean up the code.

This is not a common convention and sometimes it's getting corrected [1].

[1] https://git.kernel.org/linus/6d7434931

In separate patches outside of series adding new features, if one is so inclined.

#7 lore raw

Re: [PATCH v4 03/25] notifier: Add atomic/blocking_notifier_has_unique_priority()

In reply to Rafael J. Wysocki

10.12.2021 21:19, Rafael J. Wysocki пишет:
...

Quoted 6 lines
Quoted 6 lines
kernel/notifier.c +6 -0
+bool atomic_notifier_has_unique_priority(struct atomic_notifier_head *nh,
+               struct notifier_block *n)
+{
+       unsigned long flags;
+       bool ret;
+
Quoted 8 lines

kernel/notifier.c int atomic_notifier_call_chain(struct atomic_notifier_head *nh, · line 237

Quoted 3 lines
kernel/notifier.c +3 -0
+       spin_lock_irqsave(&nh->lock, flags);
+       ret = notifier_has_unique_priority(&nh->head, n);
+       spin_unlock_irqrestore(&nh->lock, flags);

This only works if the caller can prevent new entries from being added to the list at this point or if the caller knows that they cannot be added for some reason, but the kerneldoc doesn't mention this limitation.

I'll update the comment.

..

Quoted 5 lines
Quoted 5 lines
kernel/notifier.c +5 -0
+bool blocking_notifier_has_unique_priority(struct blocking_notifier_head *nh,
+               struct notifier_block *n)
+{
+       bool ret;
+
Quoted 8 lines

kernel/notifier.c bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh) · line 396

Quoted 6 lines
kernel/notifier.c +6 -0
+       /*
+        * This code gets used during boot-up, when task switching is
+        * not yet working and interrupts must remain disabled. At such
+        * times we must not call down_read().
+        */
+       if (system_state != SYSTEM_BOOTING)

No, please don't do this, it makes the whole thing error-prone.

What should I do then?

Quoted 9 lines

kernel/notifier.c bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh) · line 402

Quoted 6 lines
kernel/notifier.c +6 -0
+               down_read(&nh->rwsem);
+
+       ret = notifier_has_unique_priority(&nh->head, n);
+
+       if (system_state != SYSTEM_BOOTING)
+               up_read(&nh->rwsem);

And still what if a new entry with a non-unique priority is added to the chain at this point?

If entry with a non-unique priority is added after the check, then obviously it won't be detected. I don't understand the question. These down/up_read() are the locks that prevent the race, if that's the question.

#27 lore raw

Re: [PATCH v4 06/25] reboot: Warn if unregister_restart_handler() fails

In reply to Rafael J. Wysocki

10.12.2021 21:32, Rafael J. Wysocki пишет:

Quoted 10 lines · Dmitry Osipenko
Quoted 9 lines

Emit warning if unregister_restart_handler() fails since it never should fail. This will ease further API development by catching mistakes early.

---
 kernel/reboot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

kernel/reboot.c EXPORT_SYMBOL(register_restart_handler); · line 213

Quoted 10 lines
kernel/reboot.c +1 -1
index e6659ae329f1..f0e7b9c13f6b 100644
@@ -210,7 +210,7 @@ EXPORT_SYMBOL(register_restart_handler);
  */
 int unregister_restart_handler(struct notifier_block *nb)
 {
-       return atomic_notifier_chain_unregister(&restart_handler_list, nb);
+       return WARN_ON(atomic_notifier_chain_unregister(&restart_handler_list, nb));

The only reason why it can fail is if the object pointed to by nb is not in the chain.

I had exactly this case where object wasn't in the chain due to a bug and this warning was very helpful.

Quoted 2 lines

Why WARN() about this? And what about systems with
panic_on_warn set?

That warning condition will never happen normally, only when something is seriously wrong.

Those systems with panic_on_warn will get what was they asked for.

#34 lore raw

Re: [PATCH v4 07/25] reboot: Remove extern annotation from function prototypes

In reply to Rafael J. Wysocki

10.12.2021 21:35, Rafael J. Wysocki пишет:

Quoted 19 lines · Dmitry Osipenko
Quoted 18 lines

10.12.2021 21:09, Rafael J. Wysocki пишет:

Quoted 5 lines · Dmitry Osipenko
Quoted 4 lines

There is no need to annotate function prototypes with 'extern', it makes code less readable. Remove unnecessary annotations from <reboot.h>.

Quoted 7 lines
Quoted 1 line

I'm not sure that this is really useful.

Personally, I tend to respect the existing conventions like this.

Surely, this change is not required for the rest of the series to work.

Problem that such things start to spread all over the kernel with a copy-paste approach if there is nobody to clean up the code.

Quoted 7 lines
Quoted 3 lines

This is not a common convention and sometimes it's getting corrected [1].

[1] https://git.kernel.org/linus/6d7434931

In separate patches outside of series adding new features, if one is so inclined.

Alright, I'll drop this patch then because it can't be done in parallel without creating the merge conflict. I'll try not to forget to come back to this later on.

#19 lore raw

Re: [PATCH v4 05/25] reboot: Warn if restart handler has duplicated priority

In reply to Rafael J. Wysocki

10.12.2021 21:27, Rafael J. Wysocki пишет:

Quoted 73 lines · Dmitry Osipenko
Quoted 59 lines

29.11.2021 03:26, Michał Mirosław пишет:

Quoted 57 lines · Dmitry Osipenko
Quoted 25 lines

28.11.2021 03:28, Michał Mirosław пишет:

Quoted 7 lines · Dmitry Osipenko
Quoted 3 lines

Add sanity check which ensures that there are no two restart handlers registered with the same priority. Normally it's a direct sign of a problem if two handlers use the same priority.

The patch doesn't ensure the property that there are no duplicated-priority entries on the chain.

It's not the exact point of this patch.

Quoted 5 lines

I'd rather see a atomic_notifier_chain_register_unique() that returns -EBUSY or something istead of adding an entry with duplicate priority. That way it would need only one list traversal unless you want to register the duplicate anyway (then you would call the older atomic_notifier_chain_register() after reporting the error).

The point of this patch is to warn developers about the problem that needs to be fixed. We already have such troubling drivers in mainline.

It's not critical to register different handlers with a duplicated priorities, but such cases really need to be corrected. We shouldn't break users' machines during transition to the new API, meanwhile developers should take action of fixing theirs drivers.

Quoted 7 lines
Quoted 3 lines

(Or you could return > 0 when a duplicate is registered in atomic_notifier_chain_register() if the callers are prepared for that. I don't really like this way, though.)

I had a similar thought at some point before and decided that I'm not in favor of this approach. It's nicer to have a dedicated function that verifies the uniqueness, IMO.

I don't like the part that it traverses the list second time to check the uniqueness. But actually you could avoid that if notifier_chain_register() would always add equal-priority entries in reverse order:

static int notifier_chain_register(struct notifier_block **nl,
struct notifier_block *n)

 {
      while ((*nl) != NULL) {
              if (unlikely((*nl) = n)) {
                      WARN(1, "double register detected");
                      return 0;
              }
(quoted diff) +1 -1
-             if (n->priority > (*nl)->priority)
+             if (n->priority >= (*nl)->priority)
                      break;
              nl = &((*nl)->next);
      }
      n->next = *nl;
      rcu_assign_pointer(*nl, n);
      return 0;
 }
Quoted 6 lines
Quoted 3 lines

Then the check for uniqueness after adding would be:

WARN(nb->next && nb->priority = nb->next->priority);

We can't just change the registration order because invocation order of the call chain depends on the registration order

It doesn't if unique priorities are required and isn't that what you want?

Quoted 3 lines

and some of current
users may rely on that order. I'm pretty sure that changing the order will have unfortunate consequences.

Quoted 5 lines

Well, the WARN() doesn't help much then.

Either you can make all of the users register with unique priorities, and then you can make the registration reject non-unique ones, or you cannot assume them to be unique.

There is no strong requirement for priorities to be unique, the reboot.c code will work properly.

The potential problem is on the user's side and the warning is intended to aid the user.

We can make it a strong requirement, but only after converting and testing all kernel drivers. I'll consider to add patches for that.

#8 lore raw

Re: [PATCH v4 03/25] notifier: Add atomic/blocking_notifier_has_unique_priority()

In reply to Dmitry Osipenko
Quoted 35 lines · Dmitry Osipenko

10.12.2021 21:19, Rafael J. Wysocki пишет:
...

Quoted 6 lines
Quoted 6 lines
kernel/notifier.c +6 -0
+bool atomic_notifier_has_unique_priority(struct atomic_notifier_head *nh,
+               struct notifier_block *n)
+{
+       unsigned long flags;
+       bool ret;
+
Quoted 8 lines

kernel/notifier.c int atomic_notifier_call_chain(struct atomic_notifier_head *nh, · line 237

Quoted 3 lines
kernel/notifier.c +3 -0
+       spin_lock_irqsave(&nh->lock, flags);
+       ret = notifier_has_unique_priority(&nh->head, n);
+       spin_unlock_irqrestore(&nh->lock, flags);

This only works if the caller can prevent new entries from being added to the list at this point or if the caller knows that they cannot be added for some reason, but the kerneldoc doesn't mention this limitation.

I'll update the comment.

..

Quoted 5 lines
Quoted 5 lines
kernel/notifier.c +5 -0
+bool blocking_notifier_has_unique_priority(struct blocking_notifier_head *nh,
+               struct notifier_block *n)
+{
+       bool ret;
+
Quoted 8 lines

kernel/notifier.c bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh) · line 396

Quoted 6 lines
kernel/notifier.c +6 -0
+       /*
+        * This code gets used during boot-up, when task switching is
+        * not yet working and interrupts must remain disabled. At such
+        * times we must not call down_read().
+        */
+       if (system_state != SYSTEM_BOOTING)

No, please don't do this, it makes the whole thing error-prone.

Quoted 1 line

What should I do then?

First of all, do you know of any users who may want to call this during early initialization? If so, then why may they want to do that?

Depending on the above, I would consider adding a special mechanism for them.

Quoted 10 lines
Quoted 9 lines

kernel/notifier.c bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh) · line 402

Quoted 6 lines
kernel/notifier.c +6 -0
+               down_read(&nh->rwsem);
+
+       ret = notifier_has_unique_priority(&nh->head, n);
+
+       if (system_state != SYSTEM_BOOTING)
+               up_read(&nh->rwsem);

And still what if a new entry with a non-unique priority is added to the chain at this point?

Quoted 2 lines

If entry with a non-unique priority is added after the check, then obviously it won't be detected.

Why isn't this a problem?

Quoted 2 lines

I don't understand the question. These
down/up_read() are the locks that prevent the race, if that's the question.

Not really, they only prevent the race from occurring while notifier_has_unique_priority() is running.

If anyone depends on this check for correctness, they need to lock the rwsem, do the check, do the thing depending on the check while holding the rwsem and then release the rwsem. Otherwise it is racy.

#28 lore raw

Re: [PATCH v4 06/25] reboot: Warn if unregister_restart_handler() fails

In reply to Dmitry Osipenko
Quoted 26 lines · Dmitry Osipenko

10.12.2021 21:32, Rafael J. Wysocki пишет:

Quoted 10 lines · Dmitry Osipenko
Quoted 9 lines

Emit warning if unregister_restart_handler() fails since it never should fail. This will ease further API development by catching mistakes early.

---
 kernel/reboot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

kernel/reboot.c EXPORT_SYMBOL(register_restart_handler); · line 213

Quoted 10 lines
kernel/reboot.c +1 -1
index e6659ae329f1..f0e7b9c13f6b 100644
@@ -210,7 +210,7 @@ EXPORT_SYMBOL(register_restart_handler);
  */
 int unregister_restart_handler(struct notifier_block *nb)
 {
-       return atomic_notifier_chain_unregister(&restart_handler_list, nb);
+       return WARN_ON(atomic_notifier_chain_unregister(&restart_handler_list, nb));

The only reason why it can fail is if the object pointed to by nb is not in the chain.

Quoted 2 lines

I had exactly this case where object wasn't in the chain due to a bug and this warning was very helpful.

During the development. In production it would be rather annoying.

Quoted 7 lines
Quoted 2 lines

Why WARN() about this? And what about systems with
panic_on_warn set?

That warning condition will never happen normally, only when something is seriously wrong.

Those systems with panic_on_warn will get what was they asked for.

They may not be asking for panicking on bugs in the reboot notifier code, though. That's what your change is making them panic on.

#20 lore raw

Re: [PATCH v4 05/25] reboot: Warn if restart handler has duplicated priority

In reply to Dmitry Osipenko
Quoted 75 lines · Dmitry Osipenko

10.12.2021 21:27, Rafael J. Wysocki пишет:

Quoted 73 lines · Dmitry Osipenko
Quoted 59 lines

29.11.2021 03:26, Michał Mirosław пишет:

Quoted 57 lines · Dmitry Osipenko
Quoted 25 lines

28.11.2021 03:28, Michał Mirosław пишет:

Quoted 7 lines · Dmitry Osipenko
Quoted 3 lines

Add sanity check which ensures that there are no two restart handlers registered with the same priority. Normally it's a direct sign of a problem if two handlers use the same priority.

The patch doesn't ensure the property that there are no duplicated-priority entries on the chain.

It's not the exact point of this patch.

Quoted 5 lines

I'd rather see a atomic_notifier_chain_register_unique() that returns -EBUSY or something istead of adding an entry with duplicate priority. That way it would need only one list traversal unless you want to register the duplicate anyway (then you would call the older atomic_notifier_chain_register() after reporting the error).

The point of this patch is to warn developers about the problem that needs to be fixed. We already have such troubling drivers in mainline.

It's not critical to register different handlers with a duplicated priorities, but such cases really need to be corrected. We shouldn't break users' machines during transition to the new API, meanwhile developers should take action of fixing theirs drivers.

Quoted 7 lines
Quoted 3 lines

(Or you could return > 0 when a duplicate is registered in atomic_notifier_chain_register() if the callers are prepared for that. I don't really like this way, though.)

I had a similar thought at some point before and decided that I'm not in favor of this approach. It's nicer to have a dedicated function that verifies the uniqueness, IMO.

I don't like the part that it traverses the list second time to check the uniqueness. But actually you could avoid that if notifier_chain_register() would always add equal-priority entries in reverse order:

static int notifier_chain_register(struct notifier_block **nl,
struct notifier_block *n)

 {
      while ((*nl) != NULL) {
              if (unlikely((*nl) = n)) {
                      WARN(1, "double register detected");
                      return 0;
              }
(quoted diff) +1 -1
-             if (n->priority > (*nl)->priority)
+             if (n->priority >= (*nl)->priority)
                      break;
              nl = &((*nl)->next);
      }
      n->next = *nl;
      rcu_assign_pointer(*nl, n);
      return 0;
 }
Quoted 6 lines
Quoted 3 lines

Then the check for uniqueness after adding would be:

WARN(nb->next && nb->priority = nb->next->priority);

We can't just change the registration order because invocation order of the call chain depends on the registration order

It doesn't if unique priorities are required and isn't that what you want?

Quoted 3 lines

and some of current
users may rely on that order. I'm pretty sure that changing the order will have unfortunate consequences.

Quoted 8 lines
Quoted 5 lines

Well, the WARN() doesn't help much then.

Either you can make all of the users register with unique priorities, and then you can make the registration reject non-unique ones, or you cannot assume them to be unique.

There is no strong requirement for priorities to be unique, the reboot.c code will work properly.

In which case adding the WARN() is not appropriate IMV.

Also I've looked at the existing code and at least in some cases the order in which the notifiers run doesn't matter. I'm not sure what the purpose of this patch is TBH.

Quoted 2 lines

The potential problem is on the user's side and the warning is intended to aid the user.

Unless somebody has the panic_on_warn mentioned previously set and really the user need not understand what the WARN() is about. IOW, WARN() helps developers, not users.

Quoted 2 lines

We can make it a strong requirement, but only after converting and testing all kernel drivers.

Right.

Quoted 1 line

I'll consider to add patches for that.

But can you avoid adding more patches to this series?

#9 lore raw

Re: [PATCH v4 03/25] notifier: Add atomic/blocking_notifier_has_unique_priority()

In reply to Rafael J. Wysocki

10.12.2021 22:05, Rafael J. Wysocki пишет:

Quoted 36 lines · Dmitry Osipenko
Quoted 35 lines

10.12.2021 21:19, Rafael J. Wysocki пишет:
...

Quoted 6 lines
Quoted 6 lines
kernel/notifier.c +6 -0
+bool atomic_notifier_has_unique_priority(struct atomic_notifier_head *nh,
+               struct notifier_block *n)
+{
+       unsigned long flags;
+       bool ret;
+
Quoted 8 lines

kernel/notifier.c int atomic_notifier_call_chain(struct atomic_notifier_head *nh, · line 237

Quoted 3 lines
kernel/notifier.c +3 -0
+       spin_lock_irqsave(&nh->lock, flags);
+       ret = notifier_has_unique_priority(&nh->head, n);
+       spin_unlock_irqrestore(&nh->lock, flags);

This only works if the caller can prevent new entries from being added to the list at this point or if the caller knows that they cannot be added for some reason, but the kerneldoc doesn't mention this limitation.

I'll update the comment.

..

Quoted 13 lines

kernel/notifier.c bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh) · line 396

Quoted 11 lines
kernel/notifier.c +11 -0
+bool blocking_notifier_has_unique_priority(struct blocking_notifier_head *nh,
+               struct notifier_block *n)
+{
+       bool ret;
+
+       /*
+        * This code gets used during boot-up, when task switching is
+        * not yet working and interrupts must remain disabled. At such
+        * times we must not call down_read().
+        */
+       if (system_state != SYSTEM_BOOTING)

No, please don't do this, it makes the whole thing error-prone.

Quoted 5 lines
Quoted 1 line

What should I do then?

First of all, do you know of any users who may want to call this during early initialization? If so, then why may they want to do that?

I'll need to carefully review all those dozens of platform restart handlers to answer this question.

Quoted 1 line

Depending on the above, I would consider adding a special mechanism for them.

Please notice that every blocking_notifier_*() function has this SYSTEM_BOOTING check, it's not my invention. Notifier API needs to be generic.

Quoted 14 lines
Quoted 10 lines
Quoted 9 lines

kernel/notifier.c bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh) · line 402

Quoted 6 lines
kernel/notifier.c +6 -0
+               down_read(&nh->rwsem);
+
+       ret = notifier_has_unique_priority(&nh->head, n);
+
+       if (system_state != SYSTEM_BOOTING)
+               up_read(&nh->rwsem);

And still what if a new entry with a non-unique priority is added to the chain at this point?

Quoted 2 lines

If entry with a non-unique priority is added after the check, then obviously it won't be detected.

Why isn't this a problem?>> I don't understand the question. These

Quoted 9 lines
Quoted 1 line

down/up_read() are the locks that prevent the race, if that's the question.

Not really, they only prevent the race from occurring while notifier_has_unique_priority() is running.

If anyone depends on this check for correctness, they need to lock the rwsem, do the check, do the thing depending on the check while holding the rwsem and then release the rwsem. Otherwise it is racy.

It's fine that it's a bit "racy" since in the context of this series. We always do the check after adding new entry, so it's not a problem.

There are two options:

1. Use blocking_notifier_has_unique_priority() like it's done in this patchset. Remove it after all drivers are converted to the new API and add blocking_notifier_chain_register_unique().

2. Add blocking_notifier_chain_register_unique(), but don't let it fail the registration of non-unique entries until all drivers are converted to the new API.

#29 lore raw

Re: [PATCH v4 06/25] reboot: Warn if unregister_restart_handler() fails

In reply to Rafael J. Wysocki

10.12.2021 22:08, Rafael J. Wysocki пишет:

Quoted 35 lines · Dmitry Osipenko
Quoted 26 lines

10.12.2021 21:32, Rafael J. Wysocki пишет:

Quoted 10 lines · Dmitry Osipenko
Quoted 9 lines

Emit warning if unregister_restart_handler() fails since it never should fail. This will ease further API development by catching mistakes early.

---
 kernel/reboot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

kernel/reboot.c EXPORT_SYMBOL(register_restart_handler); · line 213

Quoted 10 lines
kernel/reboot.c +1 -1
index e6659ae329f1..f0e7b9c13f6b 100644
@@ -210,7 +210,7 @@ EXPORT_SYMBOL(register_restart_handler);
  */
 int unregister_restart_handler(struct notifier_block *nb)
 {
-       return atomic_notifier_chain_unregister(&restart_handler_list, nb);
+       return WARN_ON(atomic_notifier_chain_unregister(&restart_handler_list, nb));

The only reason why it can fail is if the object pointed to by nb is not in the chain.

Quoted 2 lines

I had exactly this case where object wasn't in the chain due to a bug and this warning was very helpful.

During the development. In production it would be rather annoying.

Quoted 3 lines
Quoted 2 lines

Why WARN() about this? And what about systems with
panic_on_warn set?

Quoted 8 lines
Quoted 4 lines

That warning condition will never happen normally, only when something is seriously wrong.

Those systems with panic_on_warn will get what was they asked for.

They may not be asking for panicking on bugs in the reboot notifier code, though. That's what your change is making them panic on.

Alright, I'll drop the warnings and turn the warning about uniqueness into error or warning message.

#21 lore raw

Re: [PATCH v4 05/25] reboot: Warn if restart handler has duplicated priority

In reply to Rafael J. Wysocki

10.12.2021 22:14, Rafael J. Wysocki пишет:

Quoted 82 lines · Dmitry Osipenko
Quoted 81 lines

10.12.2021 21:27, Rafael J. Wysocki пишет:

Quoted 78 lines · Dmitry Osipenko
Quoted 59 lines

29.11.2021 03:26, Michał Mirosław пишет:

Quoted 57 lines · Dmitry Osipenko
Quoted 25 lines

28.11.2021 03:28, Michał Mirosław пишет:

Quoted 7 lines · Dmitry Osipenko
Quoted 3 lines

Add sanity check which ensures that there are no two restart handlers registered with the same priority. Normally it's a direct sign of a problem if two handlers use the same priority.

The patch doesn't ensure the property that there are no duplicated-priority entries on the chain.

It's not the exact point of this patch.

Quoted 5 lines

I'd rather see a atomic_notifier_chain_register_unique() that returns -EBUSY or something istead of adding an entry with duplicate priority. That way it would need only one list traversal unless you want to register the duplicate anyway (then you would call the older atomic_notifier_chain_register() after reporting the error).

The point of this patch is to warn developers about the problem that needs to be fixed. We already have such troubling drivers in mainline.

It's not critical to register different handlers with a duplicated priorities, but such cases really need to be corrected. We shouldn't break users' machines during transition to the new API, meanwhile developers should take action of fixing theirs drivers.

Quoted 7 lines
Quoted 3 lines

(Or you could return > 0 when a duplicate is registered in atomic_notifier_chain_register() if the callers are prepared for that. I don't really like this way, though.)

I had a similar thought at some point before and decided that I'm not in favor of this approach. It's nicer to have a dedicated function that verifies the uniqueness, IMO.

I don't like the part that it traverses the list second time to check the uniqueness. But actually you could avoid that if notifier_chain_register() would always add equal-priority entries in reverse order:

static int notifier_chain_register(struct notifier_block **nl,
struct notifier_block *n)

 {
      while ((*nl) != NULL) {
              if (unlikely((*nl) = n)) {
                      WARN(1, "double register detected");
                      return 0;
              }
(quoted diff) +1 -1
-             if (n->priority > (*nl)->priority)
+             if (n->priority >= (*nl)->priority)
                      break;
              nl = &((*nl)->next);
      }
      n->next = *nl;
      rcu_assign_pointer(*nl, n);
      return 0;
 }
Quoted 6 lines
Quoted 3 lines

Then the check for uniqueness after adding would be:

WARN(nb->next && nb->priority = nb->next->priority);

We can't just change the registration order because invocation order of the call chain depends on the registration order

It doesn't if unique priorities are required and isn't that what you want?

Quoted 3 lines

and some of current
users may rely on that order. I'm pretty sure that changing the order will have unfortunate consequences.

Well, the WARN() doesn't help much then.

Either you can make all of the users register with unique priorities, and then you can make the registration reject non-unique ones, or you cannot assume them to be unique.

Quoted 8 lines
Quoted 2 lines

There is no strong requirement for priorities to be unique, the reboot.c code will work properly.

In which case adding the WARN() is not appropriate IMV.

Also I've looked at the existing code and at least in some cases the order in which the notifiers run doesn't matter. I'm not sure what the purpose of this patch is TBH.

The purpose is to let developer know that driver needs to be corrected.

Quoted 7 lines
Quoted 2 lines

The potential problem is on the user's side and the warning is intended to aid the user.

Unless somebody has the panic_on_warn mentioned previously set and really the user need not understand what the WARN() is about. IOW, WARN() helps developers, not users.

Quoted 8 lines
Quoted 2 lines

We can make it a strong requirement, but only after converting and testing all kernel drivers.

Right.

Quoted 1 line

I'll consider to add patches for that.

But can you avoid adding more patches to this series?

I won't add more patches since such patches can be added only after completion of transition to the new API of the whole kernel.

#22 lore raw

Re: [PATCH v4 05/25] reboot: Warn if restart handler has duplicated priority

10.12.2021 22:42, Dmitry Osipenko пишет:
...

Quoted 21 lines
Quoted 8 lines
Quoted 2 lines

There is no strong requirement for priorities to be unique, the reboot.c code will work properly.

In which case adding the WARN() is not appropriate IMV.

Also I've looked at the existing code and at least in some cases the order in which the notifiers run doesn't matter. I'm not sure what the purpose of this patch is TBH.

The purpose is to let developer know that driver needs to be corrected.

Quoted 10 lines
Quoted 2 lines

The potential problem is on the user's side and the warning is intended to aid the user.

Unless somebody has the panic_on_warn mentioned previously set and really the user need not understand what the WARN() is about. IOW, WARN() helps developers, not users.

Quoted 2 lines

We can make it a strong requirement, but only after converting and testing all kernel drivers.

Quoted 9 lines
Quoted 5 lines

Right.

Quoted 1 line

I'll consider to add patches for that.

But can you avoid adding more patches to this series?

I won't add more patches since such patches can be added only after completion of transition to the new API of the whole kernel.

Thank you for the review.

#23 lore raw

Re: [PATCH v4 05/25] reboot: Warn if restart handler has duplicated priority

10.12.2021 22:44, Dmitry Osipenko пишет:

Quoted 27 lines

10.12.2021 22:42, Dmitry Osipenko пишет:
...

Quoted 25 lines
Quoted 8 lines
Quoted 2 lines

There is no strong requirement for priorities to be unique, the reboot.c code will work properly.

In which case adding the WARN() is not appropriate IMV.

Also I've looked at the existing code and at least in some cases the order in which the notifiers run doesn't matter. I'm not sure what the purpose of this patch is TBH.

The purpose is to let developer know that driver needs to be corrected.

Quoted 14 lines
Quoted 2 lines

The potential problem is on the user's side and the warning is intended to aid the user.

Unless somebody has the panic_on_warn mentioned previously set and really the user need not understand what the WARN() is about. IOW, WARN() helps developers, not users.

Quoted 2 lines

We can make it a strong requirement, but only after converting and testing all kernel drivers.

Right.

Quoted 1 line

I'll consider to add patches for that.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

But can you avoid adding more patches to this series?

I won't add more patches since such patches can be added only after completion of transition to the new API of the whole kernel.

Thank you for the review.

I meant you, Rafael, and Michał, just in case :)

#10 lore raw

Re: [PATCH v4 03/25] notifier: Add atomic/blocking_notifier_has_unique_priority()

In reply to Dmitry Osipenko

10.12.2021 22:33, Dmitry Osipenko пишет:

Quoted 12 lines
Quoted 7 lines

Not really, they only prevent the race from occurring while notifier_has_unique_priority() is running.

If anyone depends on this check for correctness, they need to lock the rwsem, do the check, do the thing depending on the check while holding the rwsem and then release the rwsem. Otherwise it is racy.

It's fine that it's a bit "racy" since in the context of this series. We always do the check after adding new entry, so it's not a problem.

There are two options:

Quoted 7 lines

1. Use blocking_notifier_has_unique_priority() like it's done in this patchset. Remove it after all drivers are converted to the new API and add blocking_notifier_chain_register_unique().

2. Add blocking_notifier_chain_register_unique(), but don't let it fail the registration of non-unique entries until all drivers are converted to the new API.

There is third, perhaps the best option:

3. Add blocking_notifier_chain_register_unique() and fall back to blocking_notifier_chain_register() if unique fails, do it until all drivers are converted to the new API.

#24 lore raw

Re: [PATCH v4 05/25] reboot: Warn if restart handler has duplicated priority

In reply to Rafael J. Wysocki
Quoted 89 lines · Rafael J. Wysocki
Quoted 73 lines · Dmitry Osipenko

10.12.2021 21:27, Rafael J. Wysocki пишет:

Quoted 72 lines · Dmitry Osipenko
Quoted 58 lines

29.11.2021 03:26, Michał Mirosław пишет:

Quoted 57 lines · Dmitry Osipenko
Quoted 25 lines

28.11.2021 03:28, Michał Mirosław пишет:

Quoted 7 lines · Dmitry Osipenko
Quoted 3 lines

Add sanity check which ensures that there are no two restart handlers registered with the same priority. Normally it's a direct sign of a problem if two handlers use the same priority.

The patch doesn't ensure the property that there are no duplicated-priority entries on the chain.

It's not the exact point of this patch.

Quoted 5 lines

I'd rather see a atomic_notifier_chain_register_unique() that returns -EBUSY or something istead of adding an entry with duplicate priority. That way it would need only one list traversal unless you want to register the duplicate anyway (then you would call the older atomic_notifier_chain_register() after reporting the error).

The point of this patch is to warn developers about the problem that needs to be fixed. We already have such troubling drivers in mainline.

It's not critical to register different handlers with a duplicated priorities, but such cases really need to be corrected. We shouldn't break users' machines during transition to the new API, meanwhile developers should take action of fixing theirs drivers.

Quoted 7 lines
Quoted 3 lines

(Or you could return > 0 when a duplicate is registered in atomic_notifier_chain_register() if the callers are prepared for that. I don't really like this way, though.)

I had a similar thought at some point before and decided that I'm not in favor of this approach. It's nicer to have a dedicated function that verifies the uniqueness, IMO.

I don't like the part that it traverses the list second time to check the uniqueness. But actually you could avoid that if notifier_chain_register() would always add equal-priority entries in reverse order:

static int notifier_chain_register(struct notifier_block **nl,
struct notifier_block *n)

 {
      while ((*nl) != NULL) {
              if (unlikely((*nl) = n)) {
                      WARN(1, "double register detected");
                      return 0;
              }
(quoted diff) +1 -1
-             if (n->priority > (*nl)->priority)
+             if (n->priority >= (*nl)->priority)
                      break;
              nl = &((*nl)->next);
      }
      n->next = *nl;
      rcu_assign_pointer(*nl, n);
      return 0;
 }
Quoted 6 lines
Quoted 3 lines

Then the check for uniqueness after adding would be:

WARN(nb->next && nb->priority = nb->next->priority);

We can't just change the registration order because invocation order of the call chain depends on the registration order

It doesn't if unique priorities are required and isn't that what you want?

Quoted 3 lines

and some of current
users may rely on that order. I'm pretty sure that changing the order will have unfortunate consequences.

Quoted 8 lines
Quoted 5 lines

Well, the WARN() doesn't help much then.

Either you can make all of the users register with unique priorities, and then you can make the registration reject non-unique ones, or you cannot assume them to be unique.

There is no strong requirement for priorities to be unique, the reboot.c code will work properly.

In which case adding the WARN() is not appropriate IMV.

Also I've looked at the existing code and at least in some cases the order in which the notifiers run doesn't matter. I'm not sure what the purpose of this patch is TBH.

Quoted 6 lines
Quoted 2 lines

The potential problem is on the user's side and the warning is intended to aid the user.

Unless somebody has the panic_on_warn mentioned previously set and really the user need not understand what the WARN() is about. IOW, WARN() helps developers, not users.

Do panic_on_warn and reboot_on_panic play well with having a WARN() in the reboot notifier handling?

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds