2024-04-11 22:04:21

by Thomas Weißschuh

[permalink] [raw]
Subject: [PATCH RFC v2] misc/pvpanic: add support for normal shutdowns

Shutdown requests are normally hardware dependent.
By extending pvpanic to also handle shutdown requests, guests can
submit such requests with an easily implementable and cross-platform
mechanism.

Signed-off-by: Thomas Weißschuh <[email protected]>
---
To: Arnd Bergmann <[email protected]>
To: Greg Kroah-Hartman <[email protected]>
Cc: [email protected]
Signed-off-by: Thomas Weißschuh <[email protected]>

Changes in v2:
- Drop RFC status
- Drop define from header file as it was submitted on its own
- Handle disabling of handler through sysfs
- Link to v1: https://lore.kernel.org/r/[email protected]
---
This patch is based on char-misc/char-misc-testing as it depends on
commit ad76f3e8f57c ("misc/pvpanic: add shutdown event definition")

The corresponding patch to qemu has also been submitted[0].
General discussions about the feature should happen on the other thread.

[0] https://lore.kernel.org/qemu-devel/[email protected]/
---
drivers/misc/pvpanic/pvpanic.c | 43 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/pvpanic/pvpanic.c b/drivers/misc/pvpanic/pvpanic.c
index df3457ce1cb1..17c0eb549463 100644
--- a/drivers/misc/pvpanic/pvpanic.c
+++ b/drivers/misc/pvpanic/pvpanic.c
@@ -19,6 +19,7 @@
#include <linux/module.h>
#include <linux/panic_notifier.h>
#include <linux/platform_device.h>
+#include <linux/reboot.h>
#include <linux/spinlock.h>
#include <linux/sysfs.h>
#include <linux/types.h>
@@ -35,6 +36,7 @@ struct pvpanic_instance {
void __iomem *base;
unsigned int capability;
unsigned int events;
+ struct sys_off_handler *sys_off;
struct list_head list;
};

@@ -78,6 +80,39 @@ static struct notifier_block pvpanic_panic_nb = {
.priority = INT_MAX,
};

+static int pvpanic_sys_off(struct sys_off_data *data)
+{
+ pvpanic_send_event(PVPANIC_SHUTDOWN);
+
+ return NOTIFY_DONE;
+}
+
+static void pvpanic_synchronize_sys_off_handler(struct device *dev, struct pvpanic_instance *pi)
+{
+ /* 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,
+ 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;
+ }
+}
+
static void pvpanic_remove(void *param)
{
struct pvpanic_instance *pi_cur, *pi_next;
@@ -91,6 +126,8 @@ static void pvpanic_remove(void *param)
}
}
spin_unlock(&pvpanic_lock);
+
+ unregister_sys_off_handler(pi->sys_off);
}

static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
@@ -123,6 +160,7 @@ static ssize_t events_store(struct device *dev, struct device_attribute *attr,
return -EINVAL;

pi->events = tmp;
+ pvpanic_synchronize_sys_off_handler(dev, pi);

return count;
}
@@ -156,12 +194,15 @@ int devm_pvpanic_probe(struct device *dev, void __iomem *base)
return -ENOMEM;

pi->base = base;
- pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
+ pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED | PVPANIC_SHUTDOWN;

/* initlize capability by RDPT */
pi->capability &= ioread8(base);
pi->events = pi->capability;

+ pi->sys_off = NULL;
+ pvpanic_synchronize_sys_off_handler(dev, pi);
+
spin_lock(&pvpanic_lock);
list_add(&pi->list, &pvpanic_list);
spin_unlock(&pvpanic_lock);

---
base-commit: dc806bd48abc1b8a4ae72709a37e65db42a32048
change-id: 20231104-pvpanic-shutdown-5fa38a879ad1

Best regards,
--
Thomas Weißschuh <[email protected]>



2024-04-12 06:06:39

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH RFC v2] misc/pvpanic: add support for normal shutdowns

On Fri, Apr 12, 2024 at 12:03:57AM +0200, Thomas Wei?schuh wrote:
> Shutdown requests are normally hardware dependent.
> By extending pvpanic to also handle shutdown requests, guests can
> submit such requests with an easily implementable and cross-platform
> mechanism.
>
> Signed-off-by: Thomas Wei?schuh <[email protected]>
> ---
> To: Arnd Bergmann <[email protected]>
> To: Greg Kroah-Hartman <[email protected]>
> Cc: [email protected]
> Signed-off-by: Thomas Wei?schuh <[email protected]>
>
> Changes in v2:
> - Drop RFC status

Nope, still there :(