misc/pvpanic: share the shutdown handler across devices
Subsystems (2)
PATCH 1/1 misc/pvpanic: share the shutdown handler across devices
Each pvpanic device registers its own power-off handler at SYS_OFF_PRIO_LOW. register_sys_off_handler() accepts only one handler per non-default priority, so when a second pvpanic device is probed the registration fails with -EBUSY and the driver logs "Could not register sys_off_handler". The handler that did get registered notifies every device on the list, but it is owned by the first device. Once that device is unbound, or its shutdown event is masked through sysfs, the handler is unregistered and the remaining device stops reporting shutdowns even though its events mask still has PVPANIC_SHUTDOWN set.
Register a single handler for the driver and keep it as long as at least one device has the shutdown event enabled. Serialize updates of the events mask and of the handler registration with a mutex. The panic and power-off paths still take only the list spinlock, so access the events mask with READ_ONCE()/WRITE_ONCE().
The shutdown event only reaches pvpanic when no higher priority power-off handler, such as the ACPI one, turns the machine off first. Tested on a v6.18 based guest booted with acpi=off under QEMU 8.2.2 (TCG) with two pvpanic-pci devices created with events=7: after unbinding the first device and powering off, QEMU did not receive the shutdown byte (0x4) without this change and does receive it with it. Masking the shutdown event on a single device still suppresses the write.
OpenAI Codex was used to write the code change and the QEMU test scripts, and to run the tests. GitHub Copilot was used to review the change and to rewrite this changelog.
--- drivers/misc/pvpanic/pvpanic.c | 56 ++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 26 deletions(-)
drivers/misc/pvpanic/pvpanic.c +30 -26
@@ -16,6 +16,7 @@#include <linux/limits.h> #include <linux/list.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/panic_notifier.h> #include <linux/platform_device.h> #include <linux/reboot.h>
@@ -35,12 +36,14 @@ struct pvpanic_instance {void __iomem *base;unsigned int capability;unsigned int events; -struct sys_off_handler *sys_off;struct list_head list; };static struct list_head pvpanic_list;static spinlock_t pvpanic_lock; +static DEFINE_MUTEX (pvpanic_sys_off_lock); +static struct sys_off_handler *pvpanic_sys_off_handler; +static unsigned int pvpanic_sys_off_users;static void pvpanic_send_event (unsigned int event)
@@ -51,7 +54,7 @@ pvpanic_send_event(unsigned int event)return ;list_for_each_entry (pi_cur, &pvpanic_list, list) { -if (event & pi_cur->capability & pi_cur->events) +if (event & pi_cur->capability &READ_ONCE (pi_cur->events))iowrite8 (event, pi_cur->base); }spin_unlock (&pvpanic_lock);
@@ -86,30 +89,34 @@ static int pvpanic_sys_off(struct sys_off_data *data)return NOTIFY_DONE ; } -static void pvpanic_synchronize_sys_off_handler (struct device *dev,struct pvpanic_instance *pi) +static void pvpanic_set_events (struct device *dev,struct pvpanic_instance *pi, +unsigned int events) { -/* The kernel core has logic to fall back to system halt if no -* sys_off_handler is registered. -* When the pvpanic sys_off_handler is disabled via sysfs the kernel -* should use that fallback logic, so the handler needs to be unregistered. -*/ -struct sys_off_handler *sys_off; -if (!(pi->events &PVPANIC_SHUTDOWN ) == !pi->sys_off) -return ; - -if (!pi->sys_off) { - sys_off =register_sys_off_handler (SYS_OFF_MODE_POWER_OFF ,SYS_OFF_PRIO_LOW , +mutex_lock (&pvpanic_sys_off_lock); +if ((pi->events ^ events) &PVPANIC_SHUTDOWN ) { +if (events &PVPANIC_SHUTDOWN ) + pvpanic_sys_off_users++; +else + pvpanic_sys_off_users--; + } +WRITE_ONCE (pi->events, events); + +if (!pvpanic_sys_off_users) { +/* Allow the kernel to fall back to halt if no handler remains. */ +unregister_sys_off_handler (pvpanic_sys_off_handler); + pvpanic_sys_off_handler =NULL ; + }else if (dev && !pvpanic_sys_off_handler) { + sys_off =register_sys_off_handler (SYS_OFF_MODE_POWER_OFF , +SYS_OFF_PRIO_LOW , pvpanic_sys_off,NULL );if (IS_ERR (sys_off))dev_warn (dev,"Could not register sys_off_handler: %pe\n" , sys_off);else - pi->sys_off = sys_off; - }else { -unregister_sys_off_handler (pi->sys_off); - pi->sys_off =NULL ; + pvpanic_sys_off_handler = sys_off; } +mutex_unlock (&pvpanic_sys_off_lock); }static void pvpanic_remove (void *param)
@@ -126,7 +133,7 @@ static void pvpanic_remove(void *param) }spin_unlock (&pvpanic_lock); -unregister_sys_off_handler (pi->sys_off); +pvpanic_set_events (NULL , pi,0 ); }static ssize_t capability_show (struct device *dev,struct device_attribute *attr,char *buf)
@@ -141,7 +148,7 @@ static ssize_t events_show(struct device *dev, struct device_attribute *attr, ch {struct pvpanic_instance *pi =dev_get_drvdata (dev); -return sysfs_emit (buf,"%x\n" , pi->events); +return sysfs_emit (buf,"%x\n" ,READ_ONCE (pi->events)); } static ssize_t events_store(struct device *dev, struct device_attribute *attr,
@@ -158,8 +165,7 @@ static ssize_t events_store(struct device *dev, struct device_attribute *attr,if ((tmp & pi->capability) != tmp)return -EINVAL ; - pi->events = tmp; -pvpanic_synchronize_sys_off_handler (dev, pi); +pvpanic_set_events (dev, pi, tmp);return count; }
@@ -197,10 +203,8 @@ int devm_pvpanic_probe(struct device *dev, void __iomem *base)/* initlize capability by RDPT */ pi->capability &=ioread8 (base); - pi->events = pi->capability; - - pi->sys_off =NULL ; -pvpanic_synchronize_sys_off_handler (dev, pi); + pi->events =0 ; +pvpanic_set_events (dev, pi, pi->capability);spin_lock (&pvpanic_lock);list_add (&pi->list, &pvpanic_list);
base-commit: 917fcbf033154a6c8d83cfa6c88f012b71eb37b2
Signature · 1 line
2.43.0
Greg Kroah-Hartman
maintainerQuoted 10 lines · Zhu GuangLei
Each pvpanic device registers its own power-off handler at
SYS_OFF_PRIO_LOW.register_sys_off_handler()accepts only one handler per non-default priority, so when a second pvpanic device is probed the registration fails with-EBUSYand the driver logs "Could not registersys_off_handler". The handler that did get registered notifies every device on the list, but it is owned by the first device. Once that device is unbound, or its shutdown event is masked through sysfs, the handler is unregistered and the remaining device stops reporting shutdowns even though its events mask still hasPVPANIC_SHUTDOWNset.
Quoted 5 lines
Register a single handler for the driver and keep it as long as at least one device has the shutdown event enabled. Serialize updates of the events mask and of the handler registration with a mutex. The panic and power-off paths still take only the list spinlock, so access the events mask with READ_ONCE()/WRITE_ONCE().
If a spinlock is used, why is the read/write once stuff needed?
Quoted 69 lines
The shutdown event only reaches pvpanic when no higher priority power-off handler, such as the ACPI one, turns the machine off first. Tested on a v6.18 based guest booted with acpi=off under QEMU 8.2.2 (TCG) with two pvpanic-pci devices created with events=7: after unbinding the first device and powering off, QEMU did not receive the shutdown byte (
0x4) without this change and does receive it with it. Masking the shutdown event on a single device still suppresses the write.OpenAI Codex was used to write the code change and the QEMU test scripts, and to run the tests. GitHub Copilot was used to review the change and to rewrite this changelog.
Fixes 7b51b1373321 ("misc/pvpanic: add support for normal shutdowns")Signed-off-by Zhu GuangLei <[email protected]>--- drivers/misc/pvpanic/pvpanic.c | 56 ++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 26 deletions(-)drivers/misc/pvpanic/pvpanic.c +7
-7@@ -16,6 +16,7 @@#include <linux/limits.h> #include <linux/list.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/panic_notifier.h> #include <linux/platform_device.h> #include <linux/reboot.h> @@ -35,12 +36,14 @@ struct pvpanic_instance {void __iomem *base;unsigned int capability;unsigned int events; -struct sys_off_handler *sys_off;struct list_head list; };static struct list_head pvpanic_list;static spinlock_t pvpanic_lock; +static DEFINE_MUTEX (pvpanic_sys_off_lock); +static struct sys_off_handler *pvpanic_sys_off_handler; +static unsigned int pvpanic_sys_off_users;static void pvpanic_send_event (unsigned int event)@@ -51,7 +54,7 @@ pvpanic_send_event(unsigned int event)return ;list_for_each_entry (pi_cur, &pvpanic_list, list) { -if (event & pi_cur->capability & pi_cur->events) +if (event & pi_cur->capability &READ_ONCE (pi_cur->events))iowrite8 (event, pi_cur->base); }spin_unlock (&pvpanic_lock);@@ -86,30 +89,34 @@ static int pvpanic_sys_off(struct sys_off_data *data)return NOTIFY_DONE ; } -static void pvpanic_synchronize_sys_off_handler (struct device *dev,struct pvpanic_instance *pi) +static void pvpanic_set_events (struct device *dev,struct pvpanic_instance *pi, +unsigned int events) { -/* The kernel core has logic to fall back to system halt if no -* sys_off_handler is registered. -* When the pvpanic sys_off_handler is disabled via sysfs the kernel -* should use that fallback logic, so the handler needs to be unregistered.
drivers/misc/pvpanic/pvpanic.c static int pvpanic_sys_off(struct sys_off_data *data) · line 97
Quoted 10 lines
drivers/misc/pvpanic/pvpanic.c +1
-7-*/ -struct sys_off_handler *sys_off; -if (!(pi->events &PVPANIC_SHUTDOWN ) == !pi->sys_off) -return ; - -if (!pi->sys_off) { - sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_LOW, +mutex_lock (&pvpanic_sys_off_lock);
guard()?
drivers/misc/pvpanic/pvpanic.c static int pvpanic_sys_off(struct sys_off_data *data) · line 104
Quoted 7 lines
drivers/misc/pvpanic/pvpanic.c +7
-0+if ((pi->events ^ events) &PVPANIC_SHUTDOWN ) { +if (events &PVPANIC_SHUTDOWN ) + pvpanic_sys_off_users++; +else + pvpanic_sys_off_users--; + } +WRITE_ONCE (pi->events, events);
As you have a global lock here, again, why is WRITE_ONCE() needed?
thanks,
greg k-h
Quoted 7 lines · Greg Kroah-Hartman
Quoted 5 lines
Register a single handler for the driver and keep it as long as at least one device has the shutdown event enabled. Serialize updates of the events mask and of the handler registration with a mutex. The panic and power-off paths still take only the list spinlock, so access the events mask with READ_ONCE()/WRITE_ONCE().
If a spinlock is used, why is the read/write once stuff needed?
Because the writer did not take that spinlock: v1 updated the mask under the new mutex only, while pvpanic_send_event() reads it under pvpanic_lock. That is not a good reason, though. v2 updates the mask under pvpanic_lock as well and drops READ_ONCE()/WRITE_ONCE().
Quoted 3 lines
Quoted 1 line
+
mutex_lock(&pvpanic_sys_off_lock);
guard()?
Yes, will do in v2.
Quoted 3 lines
Quoted 1 line
+
WRITE_ONCE(pi->events, events);As you have a global lock here, again, why is
WRITE_ONCE()needed?
Same reason as above, it is gone in v2.
thanks,
Zhu GuangLei
PATCH v2 1/1 misc/pvpanic: share the shutdown handler across devices
In reply to Zhu GuangLeiEach pvpanic device registers its own power-off handler at SYS_OFF_PRIO_LOW. register_sys_off_handler() accepts only one handler per non-default priority, so when a second pvpanic device is probed the registration fails with -EBUSY and the driver logs "Could not register sys_off_handler". The handler that did get registered notifies every device on the list, but it is owned by the first device. Once that device is unbound, or its shutdown event is masked through sysfs, the handler is unregistered and the remaining device stops reporting shutdowns even though its events mask still has PVPANIC_SHUTDOWN set.
Register a single handler for the driver and keep it as long as at least one device has the shutdown event enabled. Serialize updates of the events mask and of the handler registration with a mutex, and also update the mask under the list spinlock, which is the only lock taken by the panic and power-off paths.
The shutdown event only reaches pvpanic when no higher priority power-off handler, such as the ACPI one, turns the machine off first. Tested on a v6.18 based guest booted with acpi=off under QEMU 8.2.2 (TCG) with two pvpanic-pci devices created with events=7: after unbinding the first device, or masking its shutdown event through sysfs, and powering off, QEMU did not receive the shutdown byte (0x4) without this change and does receive it with it.
Masking the shutdown event on a single device still suppresses the write.
OpenAI Codex was used to write the code change and the QEMU test scripts, and to run the tests. GitHub Copilot was used to review the change, to rewrite this changelog and to prepare v2.
---
Notes:
Changes in v2:
- Update the events mask under pvpanic_lock instead of using READ_ONCE()/WRITE_ONCE(), and read it under the mutex in events_show()
- Use guard()/scoped_guard() for the locks (Greg)
- Also test masking the shutdown event of the first device via sysfs
v1: https://lore.kernel.org/all/[email protected]/
drivers/misc/pvpanic/pvpanic.c | 55 ++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 23 deletions(-)
drivers/misc/pvpanic/pvpanic.c +32 -23
@@ -7,6 +7,7 @@* Copyright (C) 2021 Oracle. */ +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/errno.h> #include <linux/gfp_types.h>
@@ -16,6 +17,7 @@#include <linux/limits.h> #include <linux/list.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/panic_notifier.h> #include <linux/platform_device.h> #include <linux/reboot.h>
@@ -35,12 +37,14 @@ struct pvpanic_instance {void __iomem *base;unsigned int capability;unsigned int events; -struct sys_off_handler *sys_off;struct list_head list; };static struct list_head pvpanic_list;static spinlock_t pvpanic_lock; +static DEFINE_MUTEX (pvpanic_sys_off_lock); +static struct sys_off_handler *pvpanic_sys_off_handler; +static unsigned int pvpanic_sys_off_users;static void pvpanic_send_event (unsigned int event)
@@ -86,29 +90,35 @@ static int pvpanic_sys_off(struct sys_off_data *data)return NOTIFY_DONE ; } -static void pvpanic_synchronize_sys_off_handler (struct device *dev,struct pvpanic_instance *pi) +static void pvpanic_set_events (struct device *dev,struct pvpanic_instance *pi, +unsigned int events) { -/* The kernel core has logic to fall back to system halt if no -* sys_off_handler is registered. -* When the pvpanic sys_off_handler is disabled via sysfs the kernel -* should use that fallback logic, so the handler needs to be unregistered. -*/ -struct sys_off_handler *sys_off; -if (!(pi->events &PVPANIC_SHUTDOWN ) == !pi->sys_off) -return ; +guard (mutex)(&pvpanic_sys_off_lock); -if (!pi->sys_off) { - sys_off =register_sys_off_handler (SYS_OFF_MODE_POWER_OFF ,SYS_OFF_PRIO_LOW , +if ((pi->events ^ events) &PVPANIC_SHUTDOWN ) { +if (events &PVPANIC_SHUTDOWN ) + pvpanic_sys_off_users++; +else + pvpanic_sys_off_users--; + } + +scoped_guard (spinlock, &pvpanic_lock) + pi->events = events; + +if (!pvpanic_sys_off_users) { +/* Allow the kernel to fall back to halt if no handler remains. */ +unregister_sys_off_handler (pvpanic_sys_off_handler); + pvpanic_sys_off_handler =NULL ; + }else if (dev && !pvpanic_sys_off_handler) { + sys_off =register_sys_off_handler (SYS_OFF_MODE_POWER_OFF , +SYS_OFF_PRIO_LOW , pvpanic_sys_off,NULL );if (IS_ERR (sys_off))dev_warn (dev,"Could not register sys_off_handler: %pe\n" , sys_off);else - pi->sys_off = sys_off; - }else { -unregister_sys_off_handler (pi->sys_off); - pi->sys_off =NULL ; + pvpanic_sys_off_handler = sys_off; } }
@@ -126,7 +136,7 @@ static void pvpanic_remove(void *param) }spin_unlock (&pvpanic_lock); -unregister_sys_off_handler (pi->sys_off); +pvpanic_set_events (NULL , pi,0 ); }static ssize_t capability_show (struct device *dev,struct device_attribute *attr,char *buf)
@@ -141,6 +151,8 @@ static ssize_t events_show(struct device *dev, struct device_attribute *attr, ch {struct pvpanic_instance *pi =dev_get_drvdata (dev); +guard (mutex)(&pvpanic_sys_off_lock); +return sysfs_emit (buf,"%x\n" , pi->events); }
@@ -158,8 +170,7 @@ static ssize_t events_store(struct device *dev, struct device_attribute *attr,if ((tmp & pi->capability) != tmp)return -EINVAL ; - pi->events = tmp; -pvpanic_synchronize_sys_off_handler (dev, pi); +pvpanic_set_events (dev, pi, tmp);return count; }
@@ -197,10 +208,8 @@ int devm_pvpanic_probe(struct device *dev, void __iomem *base)/* initlize capability by RDPT */ pi->capability &=ioread8 (base); - pi->events = pi->capability; - - pi->sys_off =NULL ; -pvpanic_synchronize_sys_off_handler (dev, pi); + pi->events =0 ; +pvpanic_set_events (dev, pi, pi->capability);spin_lock (&pvpanic_lock);list_add (&pi->list, &pvpanic_list);
base-commit: 917fcbf033154a6c8d83cfa6c88f012b71eb37b2
Signature · 1 line
2.43.0
Greg Kroah-Hartman
maintainerdrivers/misc/pvpanic/pvpanic.c static ssize_t events_show(struct device *dev, struct device_attribute *attr, ch · line 157
Quoted 8 lines · Zhu GuangLei
drivers/misc/pvpanic/pvpanic.c +2
-0@@ -141,6 +151,8 @@ static ssize_t events_show(struct device *dev, struct device_attribute *attr, ch {struct pvpanic_instance *pi =dev_get_drvdata (dev); +guard (mutex)(&pvpanic_sys_off_lock); +return sysfs_emit (buf,"%x\n" , pi->events); }
Why do you need a lock to read a single value that could change right after it is read?
thanks,
greg k-h
Quoted 7 lines · Greg Kroah-Hartman
drivers/misc/pvpanic/pvpanic.c static ssize_t events_show(struct device *dev, struct device_attribute *attr, ch · line 157
Quoted 4 lines
drivers/misc/pvpanic/pvpanic.c +2
-0+guard (mutex)(&pvpanic_sys_off_lock); +return sysfs_emit (buf,"%x\n" , pi->events); }Why do you need a lock to read a single value that could change right after it is read?
You don't, the lock adds nothing there. I'll drop it in v3 and leave events_show() as it is today.
thanks,
Zhu GuangLei
PATCH v3 1/1 misc/pvpanic: share the shutdown handler across devices
In reply to Zhu GuangLeiEach pvpanic device registers its own power-off handler at SYS_OFF_PRIO_LOW. register_sys_off_handler() accepts only one handler per non-default priority, so when a second pvpanic device is probed the registration fails with -EBUSY and the driver logs "Could not register sys_off_handler". The handler that did get registered notifies every device on the list, but it is owned by the first device. Once that device is unbound, or its shutdown event is masked through sysfs, the handler is unregistered and the remaining device stops reporting shutdowns even though its events mask still has PVPANIC_SHUTDOWN set.
Register a single handler for the driver and keep it as long as at least one device has the shutdown event enabled. Serialize updates of the events mask and of the handler registration with a mutex, and also update the mask under the list spinlock, which is the only lock taken by the panic and power-off paths.
The shutdown event only reaches pvpanic when no higher priority power-off handler, such as the ACPI one, turns the machine off first. Tested on a v6.18 based guest booted with acpi=off under QEMU 8.2.2 (TCG) with two pvpanic-pci devices created with events=7: after unbinding the first device, or masking its shutdown event through sysfs, and powering off, QEMU did not receive the shutdown byte (0x4) without this change and does receive it with it.
Masking the shutdown event on a single device still suppresses the write.
OpenAI Codex was used to write the code change and the QEMU test scripts, and to run the tests. GitHub Copilot was used to review the change, to rewrite this changelog and to prepare v2.
---
Notes:
Changes in v3:
- Drop the mutex from events_show(), a plain read is enough (Greg)
Changes in v2:
- Update the events mask under pvpanic_lock instead of using READ_ONCE()/WRITE_ONCE() (Greg)
- Use guard()/scoped_guard() for the locks (Greg)
- Also test masking the shutdown event of the first device via sysfs
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
drivers/misc/pvpanic/pvpanic.c | 53 +++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 23 deletions(-)
drivers/misc/pvpanic/pvpanic.c +30 -23
@@ -7,6 +7,7 @@* Copyright (C) 2021 Oracle. */ +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/errno.h> #include <linux/gfp_types.h>
@@ -16,6 +17,7 @@#include <linux/limits.h> #include <linux/list.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/panic_notifier.h> #include <linux/platform_device.h> #include <linux/reboot.h>
@@ -35,12 +37,14 @@ struct pvpanic_instance {void __iomem *base;unsigned int capability;unsigned int events; -struct sys_off_handler *sys_off;struct list_head list; };static struct list_head pvpanic_list;static spinlock_t pvpanic_lock; +static DEFINE_MUTEX (pvpanic_sys_off_lock); +static struct sys_off_handler *pvpanic_sys_off_handler; +static unsigned int pvpanic_sys_off_users;static void pvpanic_send_event (unsigned int event)
@@ -86,29 +90,35 @@ static int pvpanic_sys_off(struct sys_off_data *data)return NOTIFY_DONE ; } -static void pvpanic_synchronize_sys_off_handler (struct device *dev,struct pvpanic_instance *pi) +static void pvpanic_set_events (struct device *dev,struct pvpanic_instance *pi, +unsigned int events) { -/* The kernel core has logic to fall back to system halt if no -* sys_off_handler is registered. -* When the pvpanic sys_off_handler is disabled via sysfs the kernel -* should use that fallback logic, so the handler needs to be unregistered. -*/ -struct sys_off_handler *sys_off; -if (!(pi->events &PVPANIC_SHUTDOWN ) == !pi->sys_off) -return ; +guard (mutex)(&pvpanic_sys_off_lock); + +if ((pi->events ^ events) &PVPANIC_SHUTDOWN ) { +if (events &PVPANIC_SHUTDOWN ) + pvpanic_sys_off_users++; +else + pvpanic_sys_off_users--; + } -if (!pi->sys_off) { - sys_off =register_sys_off_handler (SYS_OFF_MODE_POWER_OFF ,SYS_OFF_PRIO_LOW , +scoped_guard (spinlock, &pvpanic_lock) + pi->events = events; + +if (!pvpanic_sys_off_users) { +/* Allow the kernel to fall back to halt if no handler remains. */ +unregister_sys_off_handler (pvpanic_sys_off_handler); + pvpanic_sys_off_handler =NULL ; + }else if (dev && !pvpanic_sys_off_handler) { + sys_off =register_sys_off_handler (SYS_OFF_MODE_POWER_OFF , +SYS_OFF_PRIO_LOW , pvpanic_sys_off,NULL );if (IS_ERR (sys_off))dev_warn (dev,"Could not register sys_off_handler: %pe\n" , sys_off);else - pi->sys_off = sys_off; - }else { -unregister_sys_off_handler (pi->sys_off); - pi->sys_off =NULL ; + pvpanic_sys_off_handler = sys_off; } }
@@ -126,7 +136,7 @@ static void pvpanic_remove(void *param) }spin_unlock (&pvpanic_lock); -unregister_sys_off_handler (pi->sys_off); +pvpanic_set_events (NULL , pi,0 ); }static ssize_t capability_show (struct device *dev,struct device_attribute *attr,char *buf)
@@ -158,8 +168,7 @@ static ssize_t events_store(struct device *dev, struct device_attribute *attr,if ((tmp & pi->capability) != tmp)return -EINVAL ; - pi->events = tmp; -pvpanic_synchronize_sys_off_handler (dev, pi); +pvpanic_set_events (dev, pi, tmp);return count; }
@@ -197,10 +206,8 @@ int devm_pvpanic_probe(struct device *dev, void __iomem *base)/* initlize capability by RDPT */ pi->capability &=ioread8 (base); - pi->events = pi->capability; - - pi->sys_off =NULL ; -pvpanic_synchronize_sys_off_handler (dev, pi); + pi->events =0 ; +pvpanic_set_events (dev, pi, pi->capability);spin_lock (&pvpanic_lock);list_add (&pi->list, &pvpanic_list);
base-commit: 917fcbf033154a6c8d83cfa6c88f012b71eb37b2
Signature · 1 line
2.43.0
Greg Kroah-Hartman
maintainerQuoted 26 lines · Zhu GuangLei
Each pvpanic device registers its own power-off handler at
SYS_OFF_PRIO_LOW.register_sys_off_handler()accepts only one handler per non-default priority, so when a second pvpanic device is probed the registration fails with-EBUSYand the driver logs "Could not registersys_off_handler". The handler that did get registered notifies every device on the list, but it is owned by the first device. Once that device is unbound, or its shutdown event is masked through sysfs, the handler is unregistered and the remaining device stops reporting shutdowns even though its events mask still hasPVPANIC_SHUTDOWNset.Register a single handler for the driver and keep it as long as at least one device has the shutdown event enabled. Serialize updates of the events mask and of the handler registration with a mutex, and also update the mask under the list spinlock, which is the only lock taken by the panic and power-off paths.
The shutdown event only reaches pvpanic when no higher priority power-off handler, such as the ACPI one, turns the machine off first. Tested on a v6.18 based guest booted with acpi=off under QEMU 8.2.2 (TCG) with two pvpanic-pci devices created with events=7: after unbinding the first device, or masking its shutdown event through sysfs, and powering off, QEMU did not receive the shutdown byte (
0x4) without this change and does receive it with it.
Masking the shutdown event on a single device still suppresses the write.
Quoted 7 lines
OpenAI Codex was used to write the code change and the QEMU test scripts, and to run the tests. GitHub Copilot was used to review the change, to rewrite this changelog and to prepare v2.
Fixes 7b51b1373321 ("misc/pvpanic: add support for normal shutdowns")Signed-off-by Zhu GuangLei <[email protected]>
Please slow down, there is no rush, at MOST do a new version after waiting 24 hours for others to review and catch up. Usually a few days is fine.
thanks,
greg k-h