2021-03-24 15:41:32

by Mihai Carabas

[permalink] [raw]
Subject: [PATCH v8] add support for pci in the pvpanic driver

This patchset adds support for PCI in the pvpanic driver. The device already
got in qemu [1].

v2:
- mmio -> MMIO, pci -> PCI suggested by Randy Dunlap.
- group pvpanic-common.c and mmio.c in the same module. The intention was to
have only one module and the common code splitted up to be re-used by the
pvpanic-pci module in the next patch.
- add a new patch where add the licenses for the new files (when I moved the
code to mmio.c I preserved the original licenses there)
- properly clean-up PCI device when driver removed.

v3:
- drop patch 3 with licenses and add them when creating files
- create a new patch (2/3) which allowes multiple pvpanic instances

v4:
- fix Makefile in patch 1/3 and 3/3 for pvpanic.o as suggested by Arnd

v5:
- rebase on 5.12
- fix a warning caused by one of the patches

v6:
- remove pr_fmt as were not used
- "remove" functions of type void
- moved events/capability per-device structure

v7:
- rebased on char-misc-next

v8:
- fixed build error while compiling only the first patch (accept
my apologizes Greg)
- build and tested pvpanic functionality after each applied patch

How to test:

Run a VM with latest qemu which has the pvpanic-pci device:
qemu-system-aarch64 -device pvpanic-pci

Generate a panic in the guest:
echo 1 > /proc/sys/kernel/sysrq
echo c > /proc/sysrq-trigger

Observe in the QMP console the panic events received by the device:

{"timestamp": {"seconds": 1613122186, "microseconds": 141729}, "event":
"GUEST_PANICKED", "data": {"action": "pause"}}

{"timestamp": {"seconds": 1613122186, "microseconds": 141833}, "event":
"GUEST_PANICKED", "data": {"action": "poweroff"}}

{"timestamp": {"seconds": 1613122186, "microseconds": 141896}, "event":
"SHUTDOWN", "data": {"guest": true, "reason": "guest-panic"}}


[1] https://github.com/qemu/qemu/commit/9df52f58e76e904fb141b10318362d718f470db2


Mihai Carabas (3):
misc/pvpanic: split-up generic and platform dependent code
misc/pvpanic: probe multiple instances
misc/pvpanic: add PCI driver

.../ABI/testing/sysfs-bus-pci-devices-pvpanic | 4 +-
drivers/misc/Kconfig | 9 +-
drivers/misc/Makefile | 2 +-
drivers/misc/pvpanic.c | 161 ---------------------
drivers/misc/pvpanic/Kconfig | 27 ++++
drivers/misc/pvpanic/Makefile | 8 +
drivers/misc/pvpanic/pvpanic-mmio.c | 144 ++++++++++++++++++
drivers/misc/pvpanic/pvpanic-pci.c | 125 ++++++++++++++++
drivers/misc/pvpanic/pvpanic.c | 113 +++++++++++++++
drivers/misc/pvpanic/pvpanic.h | 21 +++
10 files changed, 443 insertions(+), 171 deletions(-)
delete mode 100644 drivers/misc/pvpanic.c
create mode 100644 drivers/misc/pvpanic/Kconfig
create mode 100644 drivers/misc/pvpanic/Makefile
create mode 100644 drivers/misc/pvpanic/pvpanic-mmio.c
create mode 100644 drivers/misc/pvpanic/pvpanic-pci.c
create mode 100644 drivers/misc/pvpanic/pvpanic.c
create mode 100644 drivers/misc/pvpanic/pvpanic.h

--
1.8.3.1


2021-03-24 15:42:12

by Mihai Carabas

[permalink] [raw]
Subject: [PATCH v8 3/3] misc/pvpanic: add PCI driver

Add support for pvpanic PCI device added in qemu [1]. At probe time, obtain the
address where to read/write pvpanic events and pass it to the generic handling
code. Will follow the same logic as pvpanic MMIO device driver. At remove time,
unmap base address and disable PCI device.

[1] https://github.com/qemu/qemu/commit/9df52f58e76e904fb141b10318362d718f470db2

Signed-off-by: Mihai Carabas <[email protected]>
---
.../ABI/testing/sysfs-bus-pci-devices-pvpanic | 4 +-
drivers/misc/pvpanic/Kconfig | 8 ++
drivers/misc/pvpanic/Makefile | 1 +
drivers/misc/pvpanic/pvpanic-pci.c | 125 +++++++++++++++++++++
4 files changed, 137 insertions(+), 1 deletion(-)
create mode 100644 drivers/misc/pvpanic/pvpanic-pci.c

diff --git a/Documentation/ABI/testing/sysfs-bus-pci-devices-pvpanic b/Documentation/ABI/testing/sysfs-bus-pci-devices-pvpanic
index 1936f73..4ec03cd 100644
--- a/Documentation/ABI/testing/sysfs-bus-pci-devices-pvpanic
+++ b/Documentation/ABI/testing/sysfs-bus-pci-devices-pvpanic
@@ -1,4 +1,5 @@
-What: /sys/devices/pci0000:00/*/QEMU0001:00/capability
+What: /sys/devices/pci0000:00/*/QEMU0001:00/capability for MMIO
+ /sys/bus/pci/drivers/pvpanic-pci/0000:00:0*.0/capability for PCI
Date: Jan 2021
Contact: zhenwei pi <[email protected]>
Description:
@@ -12,6 +13,7 @@ Description:
https://git.qemu.org/?p=qemu.git;a=blob_plain;f=docs/specs/pvpanic.txt

What: /sys/devices/pci0000:00/*/QEMU0001:00/events
+ /sys/bus/pci/drivers/pvpanic-pci/0000:00:0*.0/events for PCI
Date: Jan 2021
Contact: zhenwei pi <[email protected]>
Description:
diff --git a/drivers/misc/pvpanic/Kconfig b/drivers/misc/pvpanic/Kconfig
index 454f1ee..12d40a2 100644
--- a/drivers/misc/pvpanic/Kconfig
+++ b/drivers/misc/pvpanic/Kconfig
@@ -17,3 +17,11 @@ config PVPANIC_MMIO
depends on HAS_IOMEM && (ACPI || OF) && PVPANIC
help
This driver provides support for the MMIO pvpanic device.
+
+config PVPANIC_PCI
+ tristate "pvpanic PCI device support"
+ depends on PCI && PVPANIC
+ help
+ This driver provides support for the PCI pvpanic device.
+ pvpanic is a paravirtualized device provided by QEMU which
+ forwards the panic events from the guest to the host.
diff --git a/drivers/misc/pvpanic/Makefile b/drivers/misc/pvpanic/Makefile
index e12a2dc..9471df7 100644
--- a/drivers/misc/pvpanic/Makefile
+++ b/drivers/misc/pvpanic/Makefile
@@ -5,3 +5,4 @@
# Copyright (C) 2021 Oracle.
#
obj-$(CONFIG_PVPANIC_MMIO) += pvpanic.o pvpanic-mmio.o
+obj-$(CONFIG_PVPANIC_PCI) += pvpanic.o pvpanic-pci.o
diff --git a/drivers/misc/pvpanic/pvpanic-pci.c b/drivers/misc/pvpanic/pvpanic-pci.c
new file mode 100644
index 00000000..f38a80a
--- /dev/null
+++ b/drivers/misc/pvpanic/pvpanic-pci.c
@@ -0,0 +1,125 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Pvpanic PCI Device Support
+ *
+ * Copyright (C) 2021 Oracle.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+
+#include <uapi/misc/pvpanic.h>
+
+#include "pvpanic.h"
+
+#define PCI_VENDOR_ID_REDHAT 0x1b36
+#define PCI_DEVICE_ID_REDHAT_PVPANIC 0x0011
+
+MODULE_AUTHOR("Mihai Carabas <[email protected]>");
+MODULE_DESCRIPTION("pvpanic device driver ");
+MODULE_LICENSE("GPL");
+
+static const struct pci_device_id pvpanic_pci_id_tbl[] = {
+ { PCI_DEVICE(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_PVPANIC)},
+ {}
+};
+
+static ssize_t capability_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct pvpanic_instance *pi = dev_get_drvdata(dev);
+
+ return sysfs_emit(buf, "%x\n", pi->capability);
+}
+static DEVICE_ATTR_RO(capability);
+
+static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct pvpanic_instance *pi = dev_get_drvdata(dev);
+
+ return sysfs_emit(buf, "%x\n", pi->events);
+}
+
+static ssize_t events_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct pvpanic_instance *pi = dev_get_drvdata(dev);
+ unsigned int tmp;
+ int err;
+
+ err = kstrtouint(buf, 16, &tmp);
+ if (err)
+ return err;
+
+ if ((tmp & pi->capability) != tmp)
+ return -EINVAL;
+
+ pi->events = tmp;
+
+ return count;
+}
+static DEVICE_ATTR_RW(events);
+
+static struct attribute *pvpanic_pci_dev_attrs[] = {
+ &dev_attr_capability.attr,
+ &dev_attr_events.attr,
+ NULL
+};
+ATTRIBUTE_GROUPS(pvpanic_pci_dev);
+
+static int pvpanic_pci_probe(struct pci_dev *pdev,
+ const struct pci_device_id *ent)
+{
+ struct device *dev = &pdev->dev;
+ struct pvpanic_instance *pi;
+ void __iomem *base;
+ int ret;
+
+ ret = pci_enable_device(pdev);
+ if (ret < 0)
+ return ret;
+
+ base = pci_iomap(pdev, 0, 0);
+ if (IS_ERR(base))
+ return PTR_ERR(base);
+
+ pi = kmalloc(sizeof(*pi), GFP_ATOMIC);
+ if (!pi)
+ return -ENOMEM;
+
+ pi->base = base;
+ pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
+
+ /* initlize capability by RDPT */
+ pi->capability &= ioread8(base);
+ pi->events = pi->capability;
+
+ dev_set_drvdata(dev, pi);
+
+ return pvpanic_probe(pi);
+}
+
+static void pvpanic_pci_remove(struct pci_dev *pdev)
+{
+ struct pvpanic_instance *pi = dev_get_drvdata(&pdev->dev);
+
+ pvpanic_remove(pi);
+ iounmap(pi->base);
+ kfree(pi);
+ pci_disable_device(pdev);
+}
+
+static struct pci_driver pvpanic_pci_driver = {
+ .name = "pvpanic-pci",
+ .id_table = pvpanic_pci_id_tbl,
+ .probe = pvpanic_pci_probe,
+ .remove = pvpanic_pci_remove,
+ .driver = {
+ .dev_groups = pvpanic_pci_dev_groups,
+ },
+};
+
+module_pci_driver(pvpanic_pci_driver);
--
1.8.3.1

2021-03-28 13:02:07

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v8] add support for pci in the pvpanic driver

On Wed, Mar 24, 2021 at 04:49:13PM +0200, Mihai Carabas wrote:
> This patchset adds support for PCI in the pvpanic driver. The device already
> got in qemu [1].
>
> v2:
> - mmio -> MMIO, pci -> PCI suggested by Randy Dunlap.
> - group pvpanic-common.c and mmio.c in the same module. The intention was to
> have only one module and the common code splitted up to be re-used by the
> pvpanic-pci module in the next patch.
> - add a new patch where add the licenses for the new files (when I moved the
> code to mmio.c I preserved the original licenses there)
> - properly clean-up PCI device when driver removed.
>
> v3:
> - drop patch 3 with licenses and add them when creating files
> - create a new patch (2/3) which allowes multiple pvpanic instances
>
> v4:
> - fix Makefile in patch 1/3 and 3/3 for pvpanic.o as suggested by Arnd
>
> v5:
> - rebase on 5.12
> - fix a warning caused by one of the patches
>
> v6:
> - remove pr_fmt as were not used
> - "remove" functions of type void
> - moved events/capability per-device structure
>
> v7:
> - rebased on char-misc-next
>
> v8:
> - fixed build error while compiling only the first patch (accept
> my apologizes Greg)
> - build and tested pvpanic functionality after each applied patch

Much better, now queued up, thanks for sticking with it.

greg k-h