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

power: vexpress: fix corruption in notifier registration

Vexpress platforms provide two different restart handlers: SYS_REBOOT
that restart the entire system, while DB_RESET only restarts the
daughter board containing the CPU. DB_RESET is overridden by SYS_REBOOT
if it exists.

notifier_chain_register used in register_restart_handler by design
relies on notifiers to be registered once only, however vexpress restart
notifier can get registered twice. When this happen it corrupts list
of notifiers, as result some notifiers can be not called on proper
event, traverse on list can be cycled forever, and second unregister
can access already freed memory.

So far, since this was the only restart handler in the system, no issue
was observed even if the same notifier was registered twice. However
commit 6c5c0d48b686 ("watchdog: sp805: add restart handler") added
support for SP805 restart handlers and since the system under test
contains two vexpress restart and two SP805 watchdog instances, it was
observed that during the boot traversing the restart handler list looped
forever as there's a cycle in that list resulting in boot hang.

This patch fixes the issues by ensuring that the notifier is installed
only once.

Cc: Sebastian Reichel <sre@kernel.org>
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
Fixes: 46c99ac66222 ("power/reset: vexpress: Register with kernel restart handler")
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.co.uk>

authored by

Sudeep Holla and committed by
Sebastian Reichel
09bebb1a 3ffa6583

+8 -4
+8 -4
drivers/power/reset/vexpress-poweroff.c
··· 35 35 } 36 36 37 37 static struct device *vexpress_power_off_device; 38 + static atomic_t vexpress_restart_nb_refcnt = ATOMIC_INIT(0); 38 39 39 40 static void vexpress_power_off(void) 40 41 { ··· 100 99 int err; 101 100 102 101 vexpress_restart_device = dev; 103 - err = register_restart_handler(&vexpress_restart_nb); 104 - if (err) { 105 - dev_err(dev, "cannot register restart handler (err=%d)\n", err); 106 - return err; 102 + if (atomic_inc_return(&vexpress_restart_nb_refcnt) == 1) { 103 + err = register_restart_handler(&vexpress_restart_nb); 104 + if (err) { 105 + dev_err(dev, "cannot register restart handler (err=%d)\n", err); 106 + atomic_dec(&vexpress_restart_nb_refcnt); 107 + return err; 108 + } 107 109 } 108 110 device_create_file(dev, &dev_attr_active); 109 111