Skip to contentSkip to conversation

virtio: Implement virtio pstore device (v5)

PATCH In review Namhyung Kim 3 participants 11 messages Sep 4, 2016 – Sep 23, 2016

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover virtio: Implement virtio pstore device (v5)

Hello,

This is another iteration of the virtio-pstore work. I've addressed comments from Michael S. Tsirkin on the kernel code.

* changes in v5)
- convert __virtioXX to __leXX (Michael)

* changes in v4)
- use qio_channel_file_new_path() (Daniel)
- rename to delete_old_pstore_file (Daniel)
- convert G_REMOVE_SOURCE to FALSE (Daniel)

* changes in v3)
- use QIOChannel API (Stefan, Daniel)
- add bound check for malcious guests (Daniel)
- drop support PSTORE_TYPE_CONSOLE for now
- update license to allow GPL v2 or later (Michael)
- limit number of pstore files on qemu

* changes in v2)
- update VIRTIO_ID_PSTORE to 22 (Cornelia, Stefan)
- make buffer size configurable (Cornelia)
- support PSTORE_TYPE_CONSOLE (Kees)
- use separate virtqueues for read and write
- support concurrent async write
- manage pstore (file) id in device side
- fix various mistakes in qemu device (Stefan)

It started from the fact that dumping ftrace buffer at kernel oops/panic takes too much time. Although there's a way to reduce the size of the original data, sometimes I want to have the information as many as possible. Maybe kexec/kdump can solve this problem but it consumes some portion of guest memory so I'd like to avoid it. And I know the qemu + crashtool can dump and analyze the whole guest memory including the ftrace buffer without wasting guest memory, but it adds one more layer and has some limitation as an out-of-tree tool like not being in sync with the kernel changes.

So I think it'd be great using the pstore interface to dump guest kernel data on the host. One can read the data on the host directly or on the guest (at the next boot) using pstore filesystem as usual. While this patchset only implements dumping kernel log buffer, it can be extended to have ftrace buffer and probably some more..

The patch 0001 implements virtio pstore driver. It has two virt queue for (sync) read and (async) write, pstore buffer and io request and response structure. The virtio_pstore_req struct is to give information about the current pstore operation. The result will be written to the virtio_pstore_res struct. For read operation it also uses virtio_pstore_fileinfo struct.

The patch 0002 and 0003 implement virtio-pstore legacy PCI device on qemu-kvm and kvmtool respectively. I referenced virtio-baloon and virtio-rng implementations and I don't know whether kvmtool supports modern virtio 1.0+ spec. Other transports might be supported later.

For example, using virtio-pstore on qemu looks like below:

  $ qemu-system-x86_64 -enable-kvm -device virtio-pstore,directory=xxx

When guest kernel gets panic the log messages will be saved under the xxx directory.

  $ ls xxx
  dmesg-1.enc.z  dmesg-2.enc.z

As you can see the pstore subsystem compresses the log data using zlib (now supports lzo and lz4 too). The data can be extracted with the following command:

  $ cat xxx/dmesg-1.enc.z | \
Quoted 1 line

python -c 'import sys, zlib; print(zlib.decompress(sys.stdin.read()))'

  Oops#1 Part1
  <5>[    0.000000] Linux version 4.6.0kvm+ (namhyung@danjae) (gcc version 5.3.0 (GCC) ) #145 SMP Mon Jul 18 10:22:45 KST 2016
  <6>[    0.000000] Command line: root=/dev/vda console=ttyS0
  <6>[    0.000000] x86/fpu: Legacy x87 FPU detected.
  <6>[    0.000000] x86/fpu: Using 'eager' FPU context switches.
  <6>[    0.000000] e820: BIOS-provided physical RAM map:
  <6>[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
  <6>[    0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
  <6>[    0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
  <6>[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x0000000007fddfff] usable
  <6>[    0.000000] BIOS-e820: [mem 0x0000000007fde000-0x0000000007ffffff] reserved
  <6>[    0.000000] BIOS-e820: [mem 0x00000000feffc000-0x00000000feffffff] reserved
  <6>[    0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved
  <6>[    0.000000] NX (Execute Disable) protection: active
  <6>[    0.000000] SMBIOS 2.8 present.
  <7>[    0.000000] DMI: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.9.3-0-ge2fc41e-prebuilt.qemu-project.org 04/01/2014
  ...

Namhyung Kim (3):
virtio: Basic implementation of virtio pstore driver
qemu: Implement virtio-pstore device
kvmtool: Implement virtio-pstore device

 drivers/virtio/Kconfig             |  10 +
 drivers/virtio/Makefile            |   1 +
 drivers/virtio/virtio_pstore.c     | 417 +++++++++++++++++++++++++++++++++++++
 include/uapi/linux/Kbuild          |   1 +
 include/uapi/linux/virtio_ids.h    |   1 +
 include/uapi/linux/virtio_pstore.h |  74 +++++++
 6 files changed, 504 insertions(+)
 create mode 100644 drivers/virtio/virtio_pstore.c
 create mode 100644 include/uapi/linux/virtio_pstore.h
Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Anthony Liguori <[email protected]>
Cc Anton Vorontsov <[email protected]>
Cc Colin Cross <[email protected]>
Cc Kees Cook <[email protected]>
Cc Tony Luck <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Will Deacon <[email protected]>

Thanks,
Namhyung

Signature · 5 lines
2.9.3
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#1 lore raw

PATCH 1/3 virtio: Basic implementation of virtio pstore driver

The virtio pstore driver provides interface to the pstore subsystem so that the guest kernel's log/dump message can be saved on the host machine. Users can access the log file directly on the host, or on the guest at the next boot using pstore filesystem. It currently deals with kernel log (printk) buffer only, but we can extend it to have other information (like ftrace dump) later.

It supports legacy PCI device using a 16K buffer by default and it's configurable. It uses two virtqueues - one for (sync) read and another for (async) write. Since it cannot wait for write finished, it supports up to 128 concurrent IO.

Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Anthony Liguori <[email protected]>
Cc Anton Vorontsov <[email protected]>
Cc Colin Cross <[email protected]>
Cc Kees Cook <[email protected]>
Cc Tony Luck <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Will Deacon <[email protected]>
---
 drivers/virtio/Kconfig             |  10 +
 drivers/virtio/Makefile            |   1 +
 drivers/virtio/virtio_pstore.c     | 417 +++++++++++++++++++++++++++++++++++++
 include/uapi/linux/Kbuild          |   1 +
 include/uapi/linux/virtio_ids.h    |   1 +
 include/uapi/linux/virtio_pstore.h |  74 +++++++
 6 files changed, 504 insertions(+)
 create mode 100644 drivers/virtio/virtio_pstore.c
 create mode 100644 include/uapi/linux/virtio_pstore.h
drivers/virtio/Kconfig +10 -0
index 77590320d44c..8f0e6c796c12 100644
@@ -58,6 +58,16 @@ config VIRTIO_INPUT
 
 	 If unsure, say M.
 
+config VIRTIO_PSTORE
+	tristate "Virtio pstore driver"
+	depends on VIRTIO
+	depends on PSTORE
+	---help---
+	 This driver supports virtio pstore devices to save/restore
+	 panic and oops messages on the host.
+
+	 If unsure, say M.
+
  config VIRTIO_MMIO
 	tristate "Platform bus driver for memory mapped virtio devices"
 	depends on HAS_IOMEM && HAS_DMA
drivers/virtio/Makefile +1 -0
index 41e30e3dc842..bee68cb26d48 100644
@@ -5,3 +5,4 @@ virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o
 virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o
 obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o
 obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o
+obj-$(CONFIG_VIRTIO_PSTORE) += virtio_pstore.o
drivers/virtio/virtio_pstore.c new file +417 -0

417 lines not on this page · raw message

include/uapi/linux/Kbuild +1 -0
index 6d4e92ccdc91..9bbb1554d8b2 100644
@@ -449,6 +449,7 @@ header-y += virtio_ids.h
 header-y += virtio_input.h
 header-y += virtio_net.h
 header-y += virtio_pci.h
+header-y += virtio_pstore.h
 header-y += virtio_ring.h
 header-y += virtio_rng.h
 header-y += virtio_scsi.h
include/uapi/linux/virtio_ids.h +1 -0
index 77925f587b15..c72a9ab588c0 100644
@@ -41,5 +41,6 @@
 #define VIRTIO_ID_CAIF	       12 /* Virtio caif */
 #define VIRTIO_ID_GPU          16 /* virtio GPU */
 #define VIRTIO_ID_INPUT        18 /* virtio input */
+#define VIRTIO_ID_PSTORE       22 /* virtio pstore */
 
 #endif /* _LINUX_VIRTIO_IDS_H */
include/uapi/linux/virtio_pstore.h new file +74 -0

74 lines not on this page · raw message

Signature · 6 lines
2.9.3

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#7 lore raw

PATCH 2/3 qemu: Implement virtio-pstore device

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

Add virtio pstore device to allow kernel log files saved on the host. It will save the log files on the directory given by pstore device option.

  $ qemu-system-x86_64 -device virtio-pstore,directory=dir-xx ...

(guest) # echo c > /proc/sysrq-trigger

  $ ls dir-xx
  dmesg-1.enc.z  dmesg-2.enc.z

The log files are usually compressed using zlib. Users can see the log messages directly on the host or on the guest (using pstore filesystem).

The 'directory' property is required for virtio-pstore device to work. It also adds 'bufsize' property to set size of pstore bufer.

Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Anthony Liguori <[email protected]>
Cc Anton Vorontsov <[email protected]>
Cc Colin Cross <[email protected]>
Cc Kees Cook <[email protected]>
Cc Tony Luck <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Daniel P. Berrange <[email protected]>
---
 hw/virtio/Makefile.objs                        |   2 +-
 hw/virtio/virtio-pci.c                         |  52 ++
 hw/virtio/virtio-pci.h                         |  14 +
 hw/virtio/virtio-pstore.c                      | 689 +++++++++++++++++++++++++
 include/hw/pci/pci.h                           |   1 +
 include/hw/virtio/virtio-pstore.h              |  36 ++
 include/standard-headers/linux/virtio_ids.h    |   1 +
 include/standard-headers/linux/virtio_pstore.h |  76 +++
 qdev-monitor.c                                 |   1 +
 9 files changed, 871 insertions(+), 1 deletion(-)
 create mode 100644 hw/virtio/virtio-pstore.c
 create mode 100644 include/hw/virtio/virtio-pstore.h
 create mode 100644 include/standard-headers/linux/virtio_pstore.h
hw/virtio/Makefile.objs +1 -1
index 3e2b175..aae7082 100644
@@ -4,4 +4,4 @@ common-obj-y += virtio-bus.o
 common-obj-y += virtio-mmio.o
 
 obj-y += virtio.o virtio-balloon.o 
-obj-$(CONFIG_LINUX) += vhost.o vhost-backend.o vhost-user.o
+obj-$(CONFIG_LINUX) += vhost.o vhost-backend.o vhost-user.o virtio-pstore.o
hw/virtio/virtio-pci.c +52 -0

64 lines not on this page · raw message

hw/virtio/virtio-pci.h +14 -0
index 25fbf8a..354b2b7 100644
@@ -31,6 +31,7 @@
 #ifdef CONFIG_VHOST_SCSI
 #include "hw/virtio/vhost-scsi.h"
 #endif
+#include "hw/virtio/virtio-pstore.h"
 
 typedef struct VirtIOPCIProxy VirtIOPCIProxy;
 typedef struct VirtIOBlkPCI VirtIOBlkPCI;
@@ -44,6 +45,7 @@ typedef struct VirtIOInputPCI VirtIOInputPCI;
 typedef struct VirtIOInputHIDPCI VirtIOInputHIDPCI;
 typedef struct VirtIOInputHostPCI VirtIOInputHostPCI;
 typedef struct VirtIOGPUPCI VirtIOGPUPCI;
+typedef struct VirtIOPstorePCI VirtIOPstorePCI;
 
 /* virtio-pci-bus */
 
@@ -324,6 +326,18 @@ struct VirtIOGPUPCI {
     VirtIOGPU vdev;
 };
 
+/*
+ * virtio-pstore-pci: This extends VirtioPCIProxy.
+ */
+#define TYPE_VIRTIO_PSTORE_PCI "virtio-pstore-pci"
+#define VIRTIO_PSTORE_PCI(obj) \
+        OBJECT_CHECK(VirtIOPstorePCI, (obj), TYPE_VIRTIO_PSTORE_PCI)
+
+struct VirtIOPstorePCI {
+    VirtIOPCIProxy parent_obj;
+    VirtIOPstore vdev;
+};
+
 /* Virtio ABI version, if we increment this, we break the guest driver. */
 #define VIRTIO_PCI_ABI_VERSION          0
 
hw/virtio/virtio-pstore.c new file +689 -0

689 lines not on this page · raw message

include/hw/pci/pci.h +1 -0
index 929ec2f..b31774a 100644
@@ -79,6 +79,7 @@
 #define PCI_DEVICE_ID_VIRTIO_SCSI        0x1004
 #define PCI_DEVICE_ID_VIRTIO_RNG         0x1005
 #define PCI_DEVICE_ID_VIRTIO_9P          0x1009
+#define PCI_DEVICE_ID_VIRTIO_PSTORE      0x100a
 
 #define PCI_VENDOR_ID_REDHAT             0x1b36
 #define PCI_DEVICE_ID_REDHAT_BRIDGE      0x0001
include/hw/virtio/virtio-pstore.h new file +36 -0
new file mode 100644
index 0000000..85b1828
@@ -0,0 +1,36 @@
+/*
+ * Virtio Pstore Support
+ *
+ * Authors:
+ *  Namhyung Kim      <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef _QEMU_VIRTIO_PSTORE_H
+#define _QEMU_VIRTIO_PSTORE_H
+
+#include "standard-headers/linux/virtio_pstore.h"
+#include "hw/virtio/virtio.h"
+#include "hw/pci/pci.h"
+
+#define TYPE_VIRTIO_PSTORE "virtio-pstore-device"
+#define VIRTIO_PSTORE(obj) \
+        OBJECT_CHECK(VirtIOPstore, (obj), TYPE_VIRTIO_PSTORE)
+
+typedef struct VirtIOPstore {
+    VirtIODevice    parent_obj;
+    VirtQueue      *rvq;
+    VirtQueue      *wvq;
+    char           *directory;
+    int             file_idx;
+    int             num_file;
+    struct dirent **files;
+    uint64_t        id;
+    uint64_t        bufsize;
+    uint64_t        file_max;
+} VirtIOPstore;
+
+#endif
include/standard-headers/linux/virtio_ids.h +1 -0
index 77925f5..c72a9ab 100644
@@ -41,5 +41,6 @@
 #define VIRTIO_ID_CAIF	       12 /* Virtio caif */
 #define VIRTIO_ID_GPU          16 /* virtio GPU */
 #define VIRTIO_ID_INPUT        18 /* virtio input */
+#define VIRTIO_ID_PSTORE       22 /* virtio pstore */
 
 #endif /* _LINUX_VIRTIO_IDS_H */
include/standard-headers/linux/virtio_pstore.h new file +76 -0

76 lines not on this page · raw message

qdev-monitor.c +1 -0
index e19617f..e1df5a9 100644
@@ -73,6 +73,7 @@ static const QDevAlias qdev_alias_table[] = {
     { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
     { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
     { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
+    { "virtio-pstore-pci", "virtio-pstore" },
     { }
 };
 
Signature · 6 lines
2.9.3

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#10 lore raw

PATCH 3/3 kvmtool: Implement virtio-pstore device

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

Add virtio pstore device to allow kernel log messages saved on the host. With this patch, it will save the log files under directory given by --pstore option.

  $ lkvm run --pstore=dir-xx

(guest) # echo c > /proc/sysrq-trigger

  $ ls dir-xx
  dmesg-1.enc.z  dmesg-2.enc.z

The log files are usually compressed using zlib. User can easily see the messages on the host or on the guest (using pstore filesystem).

---
 Makefile                     |   1 +
 builtin-run.c                |   2 +
 include/kvm/kvm-config.h     |   1 +
 include/kvm/virtio-pci-dev.h |   2 +
 include/kvm/virtio-pstore.h  |  53 +++++
 include/linux/virtio_ids.h   |   1 +
 virtio/pstore.c              | 447 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 507 insertions(+)
 create mode 100644 include/kvm/virtio-pstore.h
 create mode 100644 virtio/pstore.c
Makefile +1 -0
index 1f0196f..d7462b9 100644
@@ -67,6 +67,7 @@ OBJS	+= virtio/net.o
 OBJS	+= virtio/rng.o
 OBJS    += virtio/balloon.o
 OBJS	+= virtio/pci.o
+OBJS	+= virtio/pstore.o
 OBJS	+= disk/blk.o
 OBJS	+= disk/qcow.o
 OBJS	+= disk/raw.o
builtin-run.c +2 -0
index 72b878d..08c12dd 100644
@@ -128,6 +128,8 @@ void kvm_run_set_wrapper_sandbox(void)
 			" rootfs"),					\
 	OPT_STRING('\0', "hugetlbfs", &(cfg)->hugetlbfs_path, "path",	\
 			"Hugetlbfs path"),				\
+	OPT_STRING('\0', "pstore", &(cfg)->pstore_path, "path",		\
+			"pstore data path"),				\
 									\
 	OPT_GROUP("Kernel options:"),					\
 	OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel",	\
include/kvm/kvm-config.h +1 -0
index 386fa8c..42b7651 100644
@@ -45,6 +45,7 @@ struct kvm_config {
 	const char *hugetlbfs_path;
 	const char *custom_rootfs_name;
 	const char *real_cmdline;
+	const char *pstore_path;
 	struct virtio_net_params *net_params;
 	bool single_step;
 	bool vnc;
include/kvm/virtio-pci-dev.h +2 -0
index 48ae018..4339d94 100644
@@ -15,6 +15,7 @@
 #define PCI_DEVICE_ID_VIRTIO_BLN		0x1005
 #define PCI_DEVICE_ID_VIRTIO_SCSI		0x1008
 #define PCI_DEVICE_ID_VIRTIO_9P			0x1009
+#define PCI_DEVICE_ID_VIRTIO_PSTORE		0x100a
 #define PCI_DEVICE_ID_VESA			0x2000
 #define PCI_DEVICE_ID_PCI_SHMEM			0x0001
 
@@ -34,5 +35,6 @@
 #define PCI_CLASS_RNG				0xff0000
 #define PCI_CLASS_BLN				0xff0000
 #define PCI_CLASS_9P				0xff0000
+#define PCI_CLASS_PSTORE			0xff0000
 
 #endif /* VIRTIO_PCI_DEV_H_ */
include/kvm/virtio-pstore.h new file +53 -0

53 lines not on this page · raw message

include/linux/virtio_ids.h +1 -0
index 5f60aa4..40eabf7 100644
@@ -40,5 +40,6 @@
 #define VIRTIO_ID_RPROC_SERIAL 11 /* virtio remoteproc serial link */
 #define VIRTIO_ID_CAIF	       12 /* Virtio caif */
 #define VIRTIO_ID_INPUT        18 /* virtio input */
+#define VIRTIO_ID_PSTORE       22 /* virtio pstore */
 
 #endif /* _LINUX_VIRTIO_IDS_H */
virtio/pstore.c new file +447 -0

447 lines not on this page · raw message

Signature · 1 line
2.9.3
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] virtio: Basic implementation of virtio pstore driver

In reply to Namhyung Kim
Quoted 21 lines · Namhyung Kim

The virtio pstore driver provides interface to the pstore subsystem so that the guest kernel's log/dump message can be saved on the host machine. Users can access the log file directly on the host, or on the guest at the next boot using pstore filesystem. It currently deals with kernel log (printk) buffer only, but we can extend it to have other information (like ftrace dump) later.

It supports legacy PCI device using a 16K buffer by default and it's configurable. It uses two virtqueues - one for (sync) read and another for (async) write. Since it cannot wait for write finished, it supports up to 128 concurrent IO.

Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Anthony Liguori <[email protected]>
Cc Anton Vorontsov <[email protected]>
Cc Colin Cross <[email protected]>
Cc Kees Cook <[email protected]>
Cc Tony Luck <[email protected]>
Cc Steven Rostedt <[email protected]>
Quoted 8 lines

While I can't speak to the virtio parts, the interface into pstore looks fine to me. :)

-Kees

Signature · 6 lines
Kees Cook
Nexus Security
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#3 lore raw

Re: [PATCH 1/3] virtio: Basic implementation of virtio pstore driver

In reply to Namhyung Kim
Quoted 11 lines · Namhyung Kim

The virtio pstore driver provides interface to the pstore subsystem so that the guest kernel's log/dump message can be saved on the host machine. Users can access the log file directly on the host, or on the guest at the next boot using pstore filesystem. It currently deals with kernel log (printk) buffer only, but we can extend it to have other information (like ftrace dump) later.

It supports legacy PCI device using a 16K buffer by default and it's configurable. It uses two virtqueues - one for (sync) read and another for (async) write. Since it cannot wait for write finished, it supports up to 128 concurrent IO.

Please document the locks that this code relies on. It is generally not safe to call virtqueue_*() from multiple threads. I also wonder about locking for virtio_pstore->req_id and other fields. Are locks missing or is something in pstore ensuring safety?

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+static int virt_pstore_open(struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);

drivers/virtio/virtio_pstore.c line 118

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_OPEN);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);

This assumes the device puts compatible Linux errno values in res->ret. The function doesn't need to return -errno if I'm reading fs/pstore/ code correctly. You could return -1 on error to avoid making this assumption. The same applies to other res->ret usage below.

drivers/virtio/virtio_pstore.c line 125

Quoted 7 lines
drivers/virtio/virtio_pstore.c new file +7 -0
+}
+
+static int virt_pstore_close(struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req = &vps->req[vps->req_id];
+	struct virtio_pstore_res *res = &vps->res[vps->req_id];

Assigning &vps->req[vps->req_id]/&vps->res[vps->req_id] is unnecessary, virt_pstore_get_reqs() handles that below.

Quoted 45 lines
drivers/virtio/virtio_pstore.c new file +45 -0
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_CLOSE);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);
+}
+
+static ssize_t virt_pstore_read(u64 *id, enum pstore_type_id *type,
+				int *count, struct timespec *time,
+				char **buf, bool *compressed,
+				ssize_t *ecc_notice_size,
+				struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct virtio_pstore_fileinfo info;
+	struct scatterlist sgo[1], sgi[3];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+	unsigned int flags;
+	int ret;
+	void *bf;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_READ);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_table(sgi, 3);
+	sg_set_buf(&sgi[0], res, sizeof(*res));
+	sg_set_buf(&sgi[1], &info, sizeof(info));
+	sg_set_buf(&sgi[2], psi->buf, psi->bufsize);
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);

drivers/virtio/virtio_pstore.c line 180

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	if (len < sizeof(*res) + sizeof(info))
+		return -1;
+
+	ret = le32_to_cpu(res->ret);
+	if (ret < 0)
+		return ret;
+
+	len = le32_to_cpu(info.len);

We trust the device but a length check would be nice here to be clear that the memcpy() below is always safe:

if (len > psi->bufsize)

    return -1;

drivers/virtio/virtio_pstore.c line 184

Quoted 4 lines
drivers/virtio/virtio_pstore.c new file +4 -0
+
+	bf = kmalloc(len, GFP_KERNEL);
+	if (bf == NULL)
+		return -ENOMEM;

There's no point in returning -ENOMEM if we return -1 and res->ret above. The caller has no way of knowing the true meaning of the return value. I would return -1 to be clear that there are no error constants.

Quoted 25 lines
drivers/virtio/virtio_pstore.c new file +25 -0
+
+	*id    = le64_to_cpu(info.id);
+	*type  = from_virtio_type(info.type);
+	*count = le32_to_cpu(info.count);
+
+	flags = le32_to_cpu(info.flags);
+	*compressed = flags & VIRTIO_PSTORE_FL_COMPRESSED;
+
+	time->tv_sec  = le64_to_cpu(info.time_sec);
+	time->tv_nsec = le32_to_cpu(info.time_nsec);
+
+	memcpy(bf, psi->buf, len);
+	*buf = bf;
+
+	return len;
+}
+
+static int notrace virt_pstore_write(enum pstore_type_id type,
+				     enum kmsg_dump_reason reason,
+				     u64 *id, unsigned int part, int count,
+				     bool compressed, size_t size,
+				     struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;

drivers/virtio/virtio_pstore.c line 219

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[2], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int flags = compressed ? VIRTIO_PSTORE_FL_COMPRESSED : 0;
+
+	if (vps->failed)
+		return -1;
+
+	*id = vps->req_id;
+	virt_pstore_get_reqs(vps, &req, &res);

This function does not wait for a response from the device so you cannot call virt_pstore_get_reqs() without risk of reusing an in-flight buffer.

drivers/virtio/virtio_pstore.c line 229

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	req->cmd   = cpu_to_le16(VIRTIO_PSTORE_CMD_WRITE);
+	req->type  = to_virtio_type(type);
+	req->flags = cpu_to_le32(flags);
+
+	sg_init_table(sgo, 2);
+	sg_set_buf(&sgo[0], req, sizeof(*req));
+	sg_set_buf(&sgo[1], psi->buf, size);
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[1], sgs, 1, 1, vps, GFP_ATOMIC);

If we don't wait for request completion then virtqueue_add_sgs() could fail here if the virtqueue is already full.

Quoted 16 lines
drivers/virtio/virtio_pstore.c new file +16 -0
+	virtqueue_kick(vps->vq[1]);
+
+	return 0;
+}
+
+static int virt_pstore_erase(enum pstore_type_id type, u64 id, int count,
+			     struct timespec time, struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);

drivers/virtio/virtio_pstore.c line 255

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	req->cmd   = cpu_to_le16(VIRTIO_PSTORE_CMD_ERASE);
+	req->type  = to_virtio_type(type);
+	req->id	   = cpu_to_le64(id);
+	req->count = cpu_to_le32(count);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);

Erase commands are sent on the "read" virtqueue, not the "write" virtqueue?

Quoted 24 lines
drivers/virtio/virtio_pstore.c new file +24 -0
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);
+}
+
+static int virt_pstore_init(struct virtio_pstore *vps)
+{
+	struct pstore_info *psinfo = &vps->pstore;
+	int err;
+
+	if (!psinfo->bufsize)
+		psinfo->bufsize = VIRT_PSTORE_BUFSIZE;
+
+	psinfo->buf = alloc_pages_exact(psinfo->bufsize, GFP_KERNEL);
+	if (!psinfo->buf) {
+		pr_err("cannot allocate pstore buffer\n");
+		return -ENOMEM;
+	}
+
+	psinfo->owner = THIS_MODULE;
+	psinfo->name  = "virtio";
+	psinfo->open  = virt_pstore_open;
+	psinfo->close = virt_pstore_close;
+	psinfo->read  = virt_pstore_read;

drivers/virtio/virtio_pstore.c line 289

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+	psinfo->erase = virt_pstore_erase;
+	psinfo->write = virt_pstore_write;
+	psinfo->flags = PSTORE_FLAGS_FRAGILE;
+
+	psinfo->data  = vps;
+	spin_lock_init(&psinfo->buf_lock);
+
+	err = pstore_register(psinfo);
+	if (err)
+		kfree(psinfo->buf);

Should this be free_pages_exact() instead of kfree()?

Is it safe to call pstore_register() before the remaining initialization steps?

My understanding is that if pstore is already mounted then requests will immediately be sent. In order to support this we'd need to initialize everything else first before calling pstore_register().

Quoted 65 lines
drivers/virtio/virtio_pstore.c new file +65 -0
+
+	return err;
+}
+
+static int virt_pstore_exit(struct virtio_pstore *vps)
+{
+	struct pstore_info *psinfo = &vps->pstore;
+
+	pstore_unregister(psinfo);
+
+	free_pages_exact(psinfo->buf, psinfo->bufsize);
+	psinfo->buf = NULL;
+	psinfo->bufsize = 0;
+
+	return 0;
+}
+
+static int virtpstore_init_vqs(struct virtio_pstore *vps)
+{
+	vq_callback_t *callbacks[] = { virtpstore_ack, virtpstore_check };
+	const char *names[] = { "pstore_read", "pstore_write" };
+
+	return vps->vdev->config->find_vqs(vps->vdev, 2, vps->vq,
+					   callbacks, names);
+}
+
+static void virtpstore_init_config(struct virtio_pstore *vps)
+{
+	u32 bufsize;
+
+	virtio_cread(vps->vdev, struct virtio_pstore_config, bufsize, &bufsize);
+
+	vps->pstore.bufsize = PAGE_ALIGN(bufsize);
+}
+
+static void virtpstore_confirm_config(struct virtio_pstore *vps)
+{
+	u32 bufsize = vps->pstore.bufsize;
+
+	virtio_cwrite(vps->vdev, struct virtio_pstore_config, bufsize,
+		     &bufsize);
+}
+
+static int virtpstore_probe(struct virtio_device *vdev)
+{
+	struct virtio_pstore *vps;
+	int err;
+
+	if (!vdev->config->get) {
+		dev_err(&vdev->dev, "driver init: config access disabled\n");
+		return -EINVAL;
+	}
+
+	vdev->priv = vps = kzalloc(sizeof(*vps), GFP_KERNEL);
+	if (!vps) {
+		err = -ENOMEM;
+		goto out;
+	}
+	vps->vdev = vdev;
+
+	err = virtpstore_init_vqs(vps);
+	if (err < 0)
+		goto out_free;
+
+	virtpstore_init_config(vps);

drivers/virtio/virtio_pstore.c line 364

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	err = virt_pstore_init(vps);
+	if (err)
+		goto out_del_vq;
+
+	virtpstore_confirm_config(vps);
+
+	init_waitqueue_head(&vps->acked);
+
+	virtio_device_ready(vdev);

This call is needed if the .probe() function uses virtqueues before returning. Right now it either doesn't use the virtqueues or it has already used them in virt_pstore_init(). Please move this before pstore_register().

#6 lore raw

Re: [PATCH 1/3] virtio: Basic implementation of virtio pstore driver

In reply to Namhyung Kim
Quoted 11 lines · Namhyung Kim

The virtio pstore driver provides interface to the pstore subsystem so that the guest kernel's log/dump message can be saved on the host machine. Users can access the log file directly on the host, or on the guest at the next boot using pstore filesystem. It currently deals with kernel log (printk) buffer only, but we can extend it to have other information (like ftrace dump) later.

It supports legacy PCI device using a 16K buffer by default and it's configurable. It uses two virtqueues - one for (sync) read and another for (async) write. Since it cannot wait for write finished, it supports up to 128 concurrent IO.

Please document the locks that this code relies on. It is generally not safe to call virtqueue_*() from multiple threads. I also wonder about locking for virtio_pstore->req_id and other fields. Are locks missing or is something in pstore ensuring safety?

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+static int virt_pstore_open(struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);

drivers/virtio/virtio_pstore.c line 118

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_OPEN);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);

This assumes the device puts compatible Linux errno values in res->ret. The function doesn't need to return -errno if I'm reading fs/pstore/ code correctly. You could return -1 on error to avoid making this assumption. The same applies to other res->ret usage below.

drivers/virtio/virtio_pstore.c line 125

Quoted 7 lines
drivers/virtio/virtio_pstore.c new file +7 -0
+}
+
+static int virt_pstore_close(struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req = &vps->req[vps->req_id];
+	struct virtio_pstore_res *res = &vps->res[vps->req_id];

Assigning &vps->req[vps->req_id]/&vps->res[vps->req_id] is unnecessary, virt_pstore_get_reqs() handles that below.

Quoted 45 lines
drivers/virtio/virtio_pstore.c new file +45 -0
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_CLOSE);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);
+}
+
+static ssize_t virt_pstore_read(u64 *id, enum pstore_type_id *type,
+				int *count, struct timespec *time,
+				char **buf, bool *compressed,
+				ssize_t *ecc_notice_size,
+				struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct virtio_pstore_fileinfo info;
+	struct scatterlist sgo[1], sgi[3];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+	unsigned int flags;
+	int ret;
+	void *bf;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_READ);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_table(sgi, 3);
+	sg_set_buf(&sgi[0], res, sizeof(*res));
+	sg_set_buf(&sgi[1], &info, sizeof(info));
+	sg_set_buf(&sgi[2], psi->buf, psi->bufsize);
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);

drivers/virtio/virtio_pstore.c line 180

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	if (len < sizeof(*res) + sizeof(info))
+		return -1;
+
+	ret = le32_to_cpu(res->ret);
+	if (ret < 0)
+		return ret;
+
+	len = le32_to_cpu(info.len);

We trust the device but a length check would be nice here to be clear that the memcpy() below is always safe:

if (len > psi->bufsize)

    return -1;

drivers/virtio/virtio_pstore.c line 184

Quoted 4 lines
drivers/virtio/virtio_pstore.c new file +4 -0
+
+	bf = kmalloc(len, GFP_KERNEL);
+	if (bf == NULL)
+		return -ENOMEM;

There's no point in returning -ENOMEM if we return -1 and res->ret above. The caller has no way of knowing the true meaning of the return value. I would return -1 to be clear that there are no error constants.

Quoted 25 lines
drivers/virtio/virtio_pstore.c new file +25 -0
+
+	*id    = le64_to_cpu(info.id);
+	*type  = from_virtio_type(info.type);
+	*count = le32_to_cpu(info.count);
+
+	flags = le32_to_cpu(info.flags);
+	*compressed = flags & VIRTIO_PSTORE_FL_COMPRESSED;
+
+	time->tv_sec  = le64_to_cpu(info.time_sec);
+	time->tv_nsec = le32_to_cpu(info.time_nsec);
+
+	memcpy(bf, psi->buf, len);
+	*buf = bf;
+
+	return len;
+}
+
+static int notrace virt_pstore_write(enum pstore_type_id type,
+				     enum kmsg_dump_reason reason,
+				     u64 *id, unsigned int part, int count,
+				     bool compressed, size_t size,
+				     struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;

drivers/virtio/virtio_pstore.c line 219

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[2], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int flags = compressed ? VIRTIO_PSTORE_FL_COMPRESSED : 0;
+
+	if (vps->failed)
+		return -1;
+
+	*id = vps->req_id;
+	virt_pstore_get_reqs(vps, &req, &res);

This function does not wait for a response from the device so you cannot call virt_pstore_get_reqs() without risk of reusing an in-flight buffer.

drivers/virtio/virtio_pstore.c line 229

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	req->cmd   = cpu_to_le16(VIRTIO_PSTORE_CMD_WRITE);
+	req->type  = to_virtio_type(type);
+	req->flags = cpu_to_le32(flags);
+
+	sg_init_table(sgo, 2);
+	sg_set_buf(&sgo[0], req, sizeof(*req));
+	sg_set_buf(&sgo[1], psi->buf, size);
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[1], sgs, 1, 1, vps, GFP_ATOMIC);

If we don't wait for request completion then virtqueue_add_sgs() could fail here if the virtqueue is already full.

Quoted 16 lines
drivers/virtio/virtio_pstore.c new file +16 -0
+	virtqueue_kick(vps->vq[1]);
+
+	return 0;
+}
+
+static int virt_pstore_erase(enum pstore_type_id type, u64 id, int count,
+			     struct timespec time, struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);

drivers/virtio/virtio_pstore.c line 255

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	req->cmd   = cpu_to_le16(VIRTIO_PSTORE_CMD_ERASE);
+	req->type  = to_virtio_type(type);
+	req->id	   = cpu_to_le64(id);
+	req->count = cpu_to_le32(count);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);

Erase commands are sent on the "read" virtqueue, not the "write" virtqueue?

Quoted 24 lines
drivers/virtio/virtio_pstore.c new file +24 -0
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);
+}
+
+static int virt_pstore_init(struct virtio_pstore *vps)
+{
+	struct pstore_info *psinfo = &vps->pstore;
+	int err;
+
+	if (!psinfo->bufsize)
+		psinfo->bufsize = VIRT_PSTORE_BUFSIZE;
+
+	psinfo->buf = alloc_pages_exact(psinfo->bufsize, GFP_KERNEL);
+	if (!psinfo->buf) {
+		pr_err("cannot allocate pstore buffer\n");
+		return -ENOMEM;
+	}
+
+	psinfo->owner = THIS_MODULE;
+	psinfo->name  = "virtio";
+	psinfo->open  = virt_pstore_open;
+	psinfo->close = virt_pstore_close;
+	psinfo->read  = virt_pstore_read;

drivers/virtio/virtio_pstore.c line 289

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+	psinfo->erase = virt_pstore_erase;
+	psinfo->write = virt_pstore_write;
+	psinfo->flags = PSTORE_FLAGS_FRAGILE;
+
+	psinfo->data  = vps;
+	spin_lock_init(&psinfo->buf_lock);
+
+	err = pstore_register(psinfo);
+	if (err)
+		kfree(psinfo->buf);

Should this be free_pages_exact() instead of kfree()?

Is it safe to call pstore_register() before the remaining initialization steps?

My understanding is that if pstore is already mounted then requests will immediately be sent. In order to support this we'd need to initialize everything else first before calling pstore_register().

Quoted 65 lines
drivers/virtio/virtio_pstore.c new file +65 -0
+
+	return err;
+}
+
+static int virt_pstore_exit(struct virtio_pstore *vps)
+{
+	struct pstore_info *psinfo = &vps->pstore;
+
+	pstore_unregister(psinfo);
+
+	free_pages_exact(psinfo->buf, psinfo->bufsize);
+	psinfo->buf = NULL;
+	psinfo->bufsize = 0;
+
+	return 0;
+}
+
+static int virtpstore_init_vqs(struct virtio_pstore *vps)
+{
+	vq_callback_t *callbacks[] = { virtpstore_ack, virtpstore_check };
+	const char *names[] = { "pstore_read", "pstore_write" };
+
+	return vps->vdev->config->find_vqs(vps->vdev, 2, vps->vq,
+					   callbacks, names);
+}
+
+static void virtpstore_init_config(struct virtio_pstore *vps)
+{
+	u32 bufsize;
+
+	virtio_cread(vps->vdev, struct virtio_pstore_config, bufsize, &bufsize);
+
+	vps->pstore.bufsize = PAGE_ALIGN(bufsize);
+}
+
+static void virtpstore_confirm_config(struct virtio_pstore *vps)
+{
+	u32 bufsize = vps->pstore.bufsize;
+
+	virtio_cwrite(vps->vdev, struct virtio_pstore_config, bufsize,
+		     &bufsize);
+}
+
+static int virtpstore_probe(struct virtio_device *vdev)
+{
+	struct virtio_pstore *vps;
+	int err;
+
+	if (!vdev->config->get) {
+		dev_err(&vdev->dev, "driver init: config access disabled\n");
+		return -EINVAL;
+	}
+
+	vdev->priv = vps = kzalloc(sizeof(*vps), GFP_KERNEL);
+	if (!vps) {
+		err = -ENOMEM;
+		goto out;
+	}
+	vps->vdev = vdev;
+
+	err = virtpstore_init_vqs(vps);
+	if (err < 0)
+		goto out_free;
+
+	virtpstore_init_config(vps);

drivers/virtio/virtio_pstore.c line 364

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	err = virt_pstore_init(vps);
+	if (err)
+		goto out_del_vq;
+
+	virtpstore_confirm_config(vps);
+
+	init_waitqueue_head(&vps->acked);
+
+	virtio_device_ready(vdev);

This call is needed if the .probe() function uses virtqueues before returning. Right now it either doesn't use the virtqueues or it has already used them in virt_pstore_init(). Please move this before pstore_register().

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#8 lore raw

Re: [PATCH 2/3] qemu: Implement virtio-pstore device

In reply to Namhyung Kim
Quoted 8 lines · Namhyung Kim
hw/virtio/virtio-pstore.c new file +8 -0
+static void virtio_pstore_handle_io(VirtIODevice *vdev, VirtQueue *vq)
+{
+    VirtIOPstore *s = VIRTIO_PSTORE(vdev);
+    VirtQueueElement *elem;
+    struct virtio_pstore_req req;
+    struct virtio_pstore_res res;
+    ssize_t len = 0;
+    int ret;

hw/virtio/virtio-pstore.c line 438

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +10 -0
+
+    for (;;) {
+        elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
+        if (!elem) {
+            return;
+        }
+
+        if (elem->out_num < 1 || elem->in_num < 1) {
+            error_report("request or response buffer is missing");
+            exit(1);

The new virtio_error() function might be available, depending on when this patch series is merged. virtio_error() should be used instead of exit(1). See "[PATCH v5 0/9] virtio: avoid exit() when device enters invalid states" on qemu-devel.

hw/virtio/virtio-pstore.c line 444

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+        }
+
+        if (elem->out_num > 2 || elem->in_num > 3) {
+            error_report("invalid number of input/output buffer");
+            exit(1);
+        }

The VIRTIO specification requires that flexible framing is supported. The device cannot make assumptions about the scatter-gather list. It must support any layout (e.g. even multiple 1-byte iovecs making up the buffer).

Quoted 29 lines
hw/virtio/virtio-pstore.c new file +29 -0
+
+        len = iov_to_buf(elem->out_sg, elem->out_num, 0, &req, sizeof(req));
+        if (len != (ssize_t)sizeof(req)) {
+            error_report("invalid request size: %ld", (long)len);
+            exit(1);
+        }
+        res.cmd  = req.cmd;
+        res.type = req.type;
+
+        switch (le16_to_cpu(req.cmd)) {
+        case VIRTIO_PSTORE_CMD_OPEN:
+            ret = virtio_pstore_do_open(s);
+            break;
+        case VIRTIO_PSTORE_CMD_CLOSE:
+            ret = virtio_pstore_do_close(s);
+            break;
+        case VIRTIO_PSTORE_CMD_ERASE:
+            ret = virtio_pstore_do_erase(s, &req);
+            break;
+        case VIRTIO_PSTORE_CMD_READ:
+            ret = virtio_pstore_do_read(s, elem);
+            if (ret == 1) {
+                /* async channel io */
+                continue;
+            }
+            break;
+        case VIRTIO_PSTORE_CMD_WRITE:
+            ret = virtio_pstore_do_write(s, elem, &req);
+            if (ret == 1) {

hw/virtio/virtio-pstore.c line 483

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +10 -0
+                /* async channel io */
+                continue;
+            }
+            break;
+        default:
+            ret = -1;
+            break;
+        }
+
+        res.ret = ret;

Missing cpu_to_le()?

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+static void pstore_set_bufsize(Object *obj, Visitor *v,
+                               const char *name, void *opaque,
+                               Error **errp)
+{
+    VirtIOPstore *s = opaque;
+    Error *error = NULL;

hw/virtio/virtio-pstore.c line 610

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +10 -0
+    uint64_t value;
+
+    visit_type_size(v, name, &value, &error);
+    if (error) {
+        error_propagate(errp, error);
+        return;
+    }
+
+    if (value < 4096) {
+        error_setg(&error, "Warning: too small buffer size: %"PRIu64, value);

This is an error, not a warning. Please remove "Warning:" so it's clear to the user that this message caused QEMU to fail.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#4 lore raw

Re: [PATCH 1/3] virtio: Basic implementation of virtio pstore driver

In reply to Stefan Hajnoczi

Hi Stefan,

Quoted 13 lines · Stefan Hajnoczi
Quoted 11 lines · Namhyung Kim

The virtio pstore driver provides interface to the pstore subsystem so that the guest kernel's log/dump message can be saved on the host machine. Users can access the log file directly on the host, or on the guest at the next boot using pstore filesystem. It currently deals with kernel log (printk) buffer only, but we can extend it to have other information (like ftrace dump) later.

It supports legacy PCI device using a 16K buffer by default and it's configurable. It uses two virtqueues - one for (sync) read and another for (async) write. Since it cannot wait for write finished, it supports up to 128 concurrent IO.

Quoted 4 lines

Please document the locks that this code relies on. It is generally not safe to call virtqueue_*() from multiple threads. I also wonder about locking for virtio_pstore->req_id and other fields. Are locks missing or is something in pstore ensuring safety?

Ok, I should use atomic inc for pstore->req_id. The open-read-close are serialized by the read_mutex of pstore_info. Write can happend anytime so I gave it a dedicate queue.

Erase is a problem though, normally it's only doable after mount operation is done so no contention to the open-read-close. But if the pstore_update_ms is set, timer routine can schedule a work to do the open-read-close loop which might contend to erase.

I'm not sure how useful pstore_update_ms is and the descriptoin saids

"milliseconds before pstore updates its content " "(default is -1, which means runtime updates are disabled; " "enabling this option is not safe, it may lead to further " "corruption on Oopses)")

Quoted 18 lines
Quoted 18 lines
drivers/virtio/virtio_pstore.c new file +17 -0
+static int virt_pstore_open(struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_OPEN);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);
Quoted 8 lines

drivers/virtio/virtio_pstore.c line 118

Quoted 3 lines
drivers/virtio/virtio_pstore.c new file +3 -0
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);

This assumes the device puts compatible Linux errno values in res->ret. The function doesn't need to return -errno if I'm reading fs/pstore/ code correctly. You could return -1 on error to avoid making this assumption. The same applies to other res->ret usage below.

Ok.

Quoted 11 lines

drivers/virtio/virtio_pstore.c line 125

Quoted 8 lines
drivers/virtio/virtio_pstore.c new file +7 -0
+}
+
+static int virt_pstore_close(struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req = &vps->req[vps->req_id];
+	struct virtio_pstore_res *res = &vps->res[vps->req_id];

Assigning &vps->req[vps->req_id]/&vps->res[vps->req_id] is unnecessary, virt_pstore_get_reqs() handles that below.

Ah, right.

Quoted 60 lines
Quoted 46 lines
drivers/virtio/virtio_pstore.c new file +45 -0
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_CLOSE);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);
+}
+
+static ssize_t virt_pstore_read(u64 *id, enum pstore_type_id *type,
+				int *count, struct timespec *time,
+				char **buf, bool *compressed,
+				ssize_t *ecc_notice_size,
+				struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct virtio_pstore_fileinfo info;
+	struct scatterlist sgo[1], sgi[3];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+	unsigned int flags;
+	int ret;
+	void *bf;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_READ);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_table(sgi, 3);
+	sg_set_buf(&sgi[0], res, sizeof(*res));
+	sg_set_buf(&sgi[1], &info, sizeof(info));
+	sg_set_buf(&sgi[2], psi->buf, psi->bufsize);
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);

drivers/virtio/virtio_pstore.c line 180

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	if (len < sizeof(*res) + sizeof(info))
+		return -1;
+
+	ret = le32_to_cpu(res->ret);
+	if (ret < 0)
+		return ret;
+
+	len = le32_to_cpu(info.len);

We trust the device but a length check would be nice here to be clear that the memcpy() below is always safe:

Quoted 2 lines

if (len > psi->bufsize)

    return -1;

Good point.

Quoted 9 lines

drivers/virtio/virtio_pstore.c line 184

Quoted 5 lines
drivers/virtio/virtio_pstore.c new file +4 -0
+
+	bf = kmalloc(len, GFP_KERNEL);
+	if (bf == NULL)
+		return -ENOMEM;

There's no point in returning -ENOMEM if we return -1 and res->ret above. The caller has no way of knowing the true meaning of the return value. I would return -1 to be clear that there are no error constants.

Ok.

Quoted 31 lines
Quoted 31 lines
drivers/virtio/virtio_pstore.c new file +30 -0
+
+	*id    = le64_to_cpu(info.id);
+	*type  = from_virtio_type(info.type);
+	*count = le32_to_cpu(info.count);
+
+	flags = le32_to_cpu(info.flags);
+	*compressed = flags & VIRTIO_PSTORE_FL_COMPRESSED;
+
+	time->tv_sec  = le64_to_cpu(info.time_sec);
+	time->tv_nsec = le32_to_cpu(info.time_nsec);
+
+	memcpy(bf, psi->buf, len);
+	*buf = bf;
+
+	return len;
+}
+
+static int notrace virt_pstore_write(enum pstore_type_id type,
+				     enum kmsg_dump_reason reason,
+				     u64 *id, unsigned int part, int count,
+				     bool compressed, size_t size,
+				     struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[2], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int flags = compressed ? VIRTIO_PSTORE_FL_COMPRESSED : 0;
+
Quoted 8 lines

drivers/virtio/virtio_pstore.c line 219

Quoted 5 lines
drivers/virtio/virtio_pstore.c new file +5 -0
+	if (vps->failed)
+		return -1;
+
+	*id = vps->req_id;
+	virt_pstore_get_reqs(vps, &req, &res);

This function does not wait for a response from the device so you cannot call virt_pstore_get_reqs() without risk of reusing an in-flight buffer.

Right. This part is debatable IMHO.

Generally pstore is used to dump oops buffer when kernel is dying. This is an occasional event so I think it's ok with the current code.

The problematic case is when other writers (console, pmsg or ftrace) are enabled. They might contend with the oops, but pstore serializes them with a spinlock. But as you said it still has a risk of reusing the in-flight buffer. I think it's okay that oops overwriting other, but the reverse is not good. I tried to mitigate the problem by managing the buffer position to spread out the write but it's not a complete solution.

I thought simply stopping the guest when writing oops, or we might create a way to tell pstore to stop other writers (until writing oops finished) somehow.

Kees, what do you think?

Quoted 14 lines

drivers/virtio/virtio_pstore.c line 229

Quoted 11 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	req->cmd   = cpu_to_le16(VIRTIO_PSTORE_CMD_WRITE);
+	req->type  = to_virtio_type(type);
+	req->flags = cpu_to_le32(flags);
+
+	sg_init_table(sgo, 2);
+	sg_set_buf(&sgo[0], req, sizeof(*req));
+	sg_set_buf(&sgo[1], psi->buf, size);
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[1], sgs, 1, 1, vps, GFP_ATOMIC);

If we don't wait for request completion then virtqueue_add_sgs() could fail here if the virtqueue is already full.

Ah, didn't known that. So we need to wait for completion somehow.

Quoted 22 lines
Quoted 22 lines
drivers/virtio/virtio_pstore.c new file +21 -0
+	virtqueue_kick(vps->vq[1]);
+
+	return 0;
+}
+
+static int virt_pstore_erase(enum pstore_type_id type, u64 id, int count,
+			     struct timespec time, struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd   = cpu_to_le16(VIRTIO_PSTORE_CMD_ERASE);
+	req->type  = to_virtio_type(type);
+	req->id	   = cpu_to_le64(id);
+	req->count = cpu_to_le32(count);
Quoted 8 lines

drivers/virtio/virtio_pstore.c line 115

Quoted 5 lines
drivers/virtio/virtio_pstore.c new file +5 -0
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);

Erase commands are sent on the "read" virtqueue, not the "write" virtqueue?

Yes, you can think they're sync and async queues. The write is the only async operation.

Quoted 29 lines
Quoted 29 lines
drivers/virtio/virtio_pstore.c new file +28 -0
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);
+}
+
+static int virt_pstore_init(struct virtio_pstore *vps)
+{
+	struct pstore_info *psinfo = &vps->pstore;
+	int err;
+
+	if (!psinfo->bufsize)
+		psinfo->bufsize = VIRT_PSTORE_BUFSIZE;
+
+	psinfo->buf = alloc_pages_exact(psinfo->bufsize, GFP_KERNEL);
+	if (!psinfo->buf) {
+		pr_err("cannot allocate pstore buffer\n");
+		return -ENOMEM;
+	}
+
+	psinfo->owner = THIS_MODULE;
+	psinfo->name  = "virtio";
+	psinfo->open  = virt_pstore_open;
+	psinfo->close = virt_pstore_close;
+	psinfo->read  = virt_pstore_read;
+	psinfo->erase = virt_pstore_erase;
+	psinfo->write = virt_pstore_write;
+	psinfo->flags = PSTORE_FLAGS_FRAGILE;
+
Quoted 8 lines

drivers/virtio/virtio_pstore.c line 289

Quoted 6 lines
drivers/virtio/virtio_pstore.c new file +6 -0
+	psinfo->data  = vps;
+	spin_lock_init(&psinfo->buf_lock);
+
+	err = pstore_register(psinfo);
+	if (err)
+		kfree(psinfo->buf);

Should this be free_pages_exact() instead of kfree()?

Oops, right.

Quoted 7 lines

Is it safe to call pstore_register() before the remaining initialization steps?

My understanding is that if pstore is already mounted then requests will immediately be sent. In order to support this we'd need to initialize everything else first before calling pstore_register().

Fair enough. Will change.

Quoted 73 lines
Quoted 73 lines
drivers/virtio/virtio_pstore.c new file +72 -0
+
+	return err;
+}
+
+static int virt_pstore_exit(struct virtio_pstore *vps)
+{
+	struct pstore_info *psinfo = &vps->pstore;
+
+	pstore_unregister(psinfo);
+
+	free_pages_exact(psinfo->buf, psinfo->bufsize);
+	psinfo->buf = NULL;
+	psinfo->bufsize = 0;
+
+	return 0;
+}
+
+static int virtpstore_init_vqs(struct virtio_pstore *vps)
+{
+	vq_callback_t *callbacks[] = { virtpstore_ack, virtpstore_check };
+	const char *names[] = { "pstore_read", "pstore_write" };
+
+	return vps->vdev->config->find_vqs(vps->vdev, 2, vps->vq,
+					   callbacks, names);
+}
+
+static void virtpstore_init_config(struct virtio_pstore *vps)
+{
+	u32 bufsize;
+
+	virtio_cread(vps->vdev, struct virtio_pstore_config, bufsize, &bufsize);
+
+	vps->pstore.bufsize = PAGE_ALIGN(bufsize);
+}
+
+static void virtpstore_confirm_config(struct virtio_pstore *vps)
+{
+	u32 bufsize = vps->pstore.bufsize;
+
+	virtio_cwrite(vps->vdev, struct virtio_pstore_config, bufsize,
+		     &bufsize);
+}
+
+static int virtpstore_probe(struct virtio_device *vdev)
+{
+	struct virtio_pstore *vps;
+	int err;
+
+	if (!vdev->config->get) {
+		dev_err(&vdev->dev, "driver init: config access disabled\n");
+		return -EINVAL;
+	}
+
+	vdev->priv = vps = kzalloc(sizeof(*vps), GFP_KERNEL);
+	if (!vps) {
+		err = -ENOMEM;
+		goto out;
+	}
+	vps->vdev = vdev;
+
+	err = virtpstore_init_vqs(vps);
+	if (err < 0)
+		goto out_free;
+
+	virtpstore_init_config(vps);
+
+	err = virt_pstore_init(vps);
+	if (err)
+		goto out_del_vq;
+
+	virtpstore_confirm_config(vps);
+
Quoted 8 lines

drivers/virtio/virtio_pstore.c line 364

Quoted 3 lines
drivers/virtio/virtio_pstore.c new file +3 -0
+	init_waitqueue_head(&vps->acked);
+
+	virtio_device_ready(vdev);

This call is needed if the .probe() function uses virtqueues before returning. Right now it either doesn't use the virtqueues or it has already used them in virt_pstore_init(). Please move this before pstore_register().

Will do.

Thank you so much for your detailed review!
Namhyung

#5 lore raw

Re: [PATCH 1/3] virtio: Basic implementation of virtio pstore driver

In reply to Stefan Hajnoczi

Hi Stefan,

Quoted 13 lines · Stefan Hajnoczi
Quoted 11 lines · Namhyung Kim

The virtio pstore driver provides interface to the pstore subsystem so that the guest kernel's log/dump message can be saved on the host machine. Users can access the log file directly on the host, or on the guest at the next boot using pstore filesystem. It currently deals with kernel log (printk) buffer only, but we can extend it to have other information (like ftrace dump) later.

It supports legacy PCI device using a 16K buffer by default and it's configurable. It uses two virtqueues - one for (sync) read and another for (async) write. Since it cannot wait for write finished, it supports up to 128 concurrent IO.

Quoted 4 lines

Please document the locks that this code relies on. It is generally not safe to call virtqueue_*() from multiple threads. I also wonder about locking for virtio_pstore->req_id and other fields. Are locks missing or is something in pstore ensuring safety?

Ok, I should use atomic inc for pstore->req_id. The open-read-close are serialized by the read_mutex of pstore_info. Write can happend anytime so I gave it a dedicate queue.

Erase is a problem though, normally it's only doable after mount operation is done so no contention to the open-read-close. But if the pstore_update_ms is set, timer routine can schedule a work to do the open-read-close loop which might contend to erase.

I'm not sure how useful pstore_update_ms is and the descriptoin saids

"milliseconds before pstore updates its content " "(default is -1, which means runtime updates are disabled; " "enabling this option is not safe, it may lead to further " "corruption on Oopses)")

Quoted 18 lines
Quoted 18 lines
drivers/virtio/virtio_pstore.c new file +17 -0
+static int virt_pstore_open(struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_OPEN);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);
Quoted 8 lines

drivers/virtio/virtio_pstore.c line 118

Quoted 3 lines
drivers/virtio/virtio_pstore.c new file +3 -0
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);

This assumes the device puts compatible Linux errno values in res->ret. The function doesn't need to return -errno if I'm reading fs/pstore/ code correctly. You could return -1 on error to avoid making this assumption. The same applies to other res->ret usage below.

Ok.

Quoted 11 lines

drivers/virtio/virtio_pstore.c line 125

Quoted 8 lines
drivers/virtio/virtio_pstore.c new file +7 -0
+}
+
+static int virt_pstore_close(struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req = &vps->req[vps->req_id];
+	struct virtio_pstore_res *res = &vps->res[vps->req_id];

Assigning &vps->req[vps->req_id]/&vps->res[vps->req_id] is unnecessary, virt_pstore_get_reqs() handles that below.

Ah, right.

Quoted 60 lines
Quoted 46 lines
drivers/virtio/virtio_pstore.c new file +45 -0
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_CLOSE);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);
+}
+
+static ssize_t virt_pstore_read(u64 *id, enum pstore_type_id *type,
+				int *count, struct timespec *time,
+				char **buf, bool *compressed,
+				ssize_t *ecc_notice_size,
+				struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct virtio_pstore_fileinfo info;
+	struct scatterlist sgo[1], sgi[3];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+	unsigned int flags;
+	int ret;
+	void *bf;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd = cpu_to_le16(VIRTIO_PSTORE_CMD_READ);
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_table(sgi, 3);
+	sg_set_buf(&sgi[0], res, sizeof(*res));
+	sg_set_buf(&sgi[1], &info, sizeof(info));
+	sg_set_buf(&sgi[2], psi->buf, psi->bufsize);
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);

drivers/virtio/virtio_pstore.c line 180

Quoted 10 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	if (len < sizeof(*res) + sizeof(info))
+		return -1;
+
+	ret = le32_to_cpu(res->ret);
+	if (ret < 0)
+		return ret;
+
+	len = le32_to_cpu(info.len);

We trust the device but a length check would be nice here to be clear that the memcpy() below is always safe:

Quoted 2 lines

if (len > psi->bufsize)

    return -1;

Good point.

Quoted 9 lines

drivers/virtio/virtio_pstore.c line 184

Quoted 5 lines
drivers/virtio/virtio_pstore.c new file +4 -0
+
+	bf = kmalloc(len, GFP_KERNEL);
+	if (bf == NULL)
+		return -ENOMEM;

There's no point in returning -ENOMEM if we return -1 and res->ret above. The caller has no way of knowing the true meaning of the return value. I would return -1 to be clear that there are no error constants.

Ok.

Quoted 31 lines
Quoted 31 lines
drivers/virtio/virtio_pstore.c new file +30 -0
+
+	*id    = le64_to_cpu(info.id);
+	*type  = from_virtio_type(info.type);
+	*count = le32_to_cpu(info.count);
+
+	flags = le32_to_cpu(info.flags);
+	*compressed = flags & VIRTIO_PSTORE_FL_COMPRESSED;
+
+	time->tv_sec  = le64_to_cpu(info.time_sec);
+	time->tv_nsec = le32_to_cpu(info.time_nsec);
+
+	memcpy(bf, psi->buf, len);
+	*buf = bf;
+
+	return len;
+}
+
+static int notrace virt_pstore_write(enum pstore_type_id type,
+				     enum kmsg_dump_reason reason,
+				     u64 *id, unsigned int part, int count,
+				     bool compressed, size_t size,
+				     struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[2], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int flags = compressed ? VIRTIO_PSTORE_FL_COMPRESSED : 0;
+
Quoted 8 lines

drivers/virtio/virtio_pstore.c line 219

Quoted 5 lines
drivers/virtio/virtio_pstore.c new file +5 -0
+	if (vps->failed)
+		return -1;
+
+	*id = vps->req_id;
+	virt_pstore_get_reqs(vps, &req, &res);

This function does not wait for a response from the device so you cannot call virt_pstore_get_reqs() without risk of reusing an in-flight buffer.

Right. This part is debatable IMHO.

Generally pstore is used to dump oops buffer when kernel is dying. This is an occasional event so I think it's ok with the current code.

The problematic case is when other writers (console, pmsg or ftrace) are enabled. They might contend with the oops, but pstore serializes them with a spinlock. But as you said it still has a risk of reusing the in-flight buffer. I think it's okay that oops overwriting other, but the reverse is not good. I tried to mitigate the problem by managing the buffer position to spread out the write but it's not a complete solution.

I thought simply stopping the guest when writing oops, or we might create a way to tell pstore to stop other writers (until writing oops finished) somehow.

Kees, what do you think?

Quoted 14 lines

drivers/virtio/virtio_pstore.c line 229

Quoted 11 lines
drivers/virtio/virtio_pstore.c new file +10 -0
+
+	req->cmd   = cpu_to_le16(VIRTIO_PSTORE_CMD_WRITE);
+	req->type  = to_virtio_type(type);
+	req->flags = cpu_to_le32(flags);
+
+	sg_init_table(sgo, 2);
+	sg_set_buf(&sgo[0], req, sizeof(*req));
+	sg_set_buf(&sgo[1], psi->buf, size);
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[1], sgs, 1, 1, vps, GFP_ATOMIC);

If we don't wait for request completion then virtqueue_add_sgs() could fail here if the virtqueue is already full.

Ah, didn't known that. So we need to wait for completion somehow.

Quoted 22 lines
Quoted 22 lines
drivers/virtio/virtio_pstore.c new file +21 -0
+	virtqueue_kick(vps->vq[1]);
+
+	return 0;
+}
+
+static int virt_pstore_erase(enum pstore_type_id type, u64 id, int count,
+			     struct timespec time, struct pstore_info *psi)
+{
+	struct virtio_pstore *vps = psi->data;
+	struct virtio_pstore_req *req;
+	struct virtio_pstore_res *res;
+	struct scatterlist sgo[1], sgi[1];
+	struct scatterlist *sgs[2] = { sgo, sgi };
+	unsigned int len;
+
+	virt_pstore_get_reqs(vps, &req, &res);
+
+	req->cmd   = cpu_to_le16(VIRTIO_PSTORE_CMD_ERASE);
+	req->type  = to_virtio_type(type);
+	req->id	   = cpu_to_le64(id);
+	req->count = cpu_to_le32(count);
Quoted 8 lines

drivers/virtio/virtio_pstore.c line 115

Quoted 5 lines
drivers/virtio/virtio_pstore.c new file +5 -0
+
+	sg_init_one(sgo, req, sizeof(*req));
+	sg_init_one(sgi, res, sizeof(*res));
+	virtqueue_add_sgs(vps->vq[0], sgs, 1, 1, vps, GFP_KERNEL);
+	virtqueue_kick(vps->vq[0]);

Erase commands are sent on the "read" virtqueue, not the "write" virtqueue?

Yes, you can think they're sync and async queues. The write is the only async operation.

Quoted 29 lines
Quoted 29 lines
drivers/virtio/virtio_pstore.c new file +28 -0
+
+	wait_event(vps->acked, virtqueue_get_buf(vps->vq[0], &len));
+	return le32_to_cpu(res->ret);
+}
+
+static int virt_pstore_init(struct virtio_pstore *vps)
+{
+	struct pstore_info *psinfo = &vps->pstore;
+	int err;
+
+	if (!psinfo->bufsize)
+		psinfo->bufsize = VIRT_PSTORE_BUFSIZE;
+
+	psinfo->buf = alloc_pages_exact(psinfo->bufsize, GFP_KERNEL);
+	if (!psinfo->buf) {
+		pr_err("cannot allocate pstore buffer\n");
+		return -ENOMEM;
+	}
+
+	psinfo->owner = THIS_MODULE;
+	psinfo->name  = "virtio";
+	psinfo->open  = virt_pstore_open;
+	psinfo->close = virt_pstore_close;
+	psinfo->read  = virt_pstore_read;
+	psinfo->erase = virt_pstore_erase;
+	psinfo->write = virt_pstore_write;
+	psinfo->flags = PSTORE_FLAGS_FRAGILE;
+
Quoted 8 lines

drivers/virtio/virtio_pstore.c line 289

Quoted 6 lines
drivers/virtio/virtio_pstore.c new file +6 -0
+	psinfo->data  = vps;
+	spin_lock_init(&psinfo->buf_lock);
+
+	err = pstore_register(psinfo);
+	if (err)
+		kfree(psinfo->buf);

Should this be free_pages_exact() instead of kfree()?

Oops, right.

Quoted 7 lines

Is it safe to call pstore_register() before the remaining initialization steps?

My understanding is that if pstore is already mounted then requests will immediately be sent. In order to support this we'd need to initialize everything else first before calling pstore_register().

Fair enough. Will change.

Quoted 73 lines
Quoted 73 lines
drivers/virtio/virtio_pstore.c new file +72 -0
+
+	return err;
+}
+
+static int virt_pstore_exit(struct virtio_pstore *vps)
+{
+	struct pstore_info *psinfo = &vps->pstore;
+
+	pstore_unregister(psinfo);
+
+	free_pages_exact(psinfo->buf, psinfo->bufsize);
+	psinfo->buf = NULL;
+	psinfo->bufsize = 0;
+
+	return 0;
+}
+
+static int virtpstore_init_vqs(struct virtio_pstore *vps)
+{
+	vq_callback_t *callbacks[] = { virtpstore_ack, virtpstore_check };
+	const char *names[] = { "pstore_read", "pstore_write" };
+
+	return vps->vdev->config->find_vqs(vps->vdev, 2, vps->vq,
+					   callbacks, names);
+}
+
+static void virtpstore_init_config(struct virtio_pstore *vps)
+{
+	u32 bufsize;
+
+	virtio_cread(vps->vdev, struct virtio_pstore_config, bufsize, &bufsize);
+
+	vps->pstore.bufsize = PAGE_ALIGN(bufsize);
+}
+
+static void virtpstore_confirm_config(struct virtio_pstore *vps)
+{
+	u32 bufsize = vps->pstore.bufsize;
+
+	virtio_cwrite(vps->vdev, struct virtio_pstore_config, bufsize,
+		     &bufsize);
+}
+
+static int virtpstore_probe(struct virtio_device *vdev)
+{
+	struct virtio_pstore *vps;
+	int err;
+
+	if (!vdev->config->get) {
+		dev_err(&vdev->dev, "driver init: config access disabled\n");
+		return -EINVAL;
+	}
+
+	vdev->priv = vps = kzalloc(sizeof(*vps), GFP_KERNEL);
+	if (!vps) {
+		err = -ENOMEM;
+		goto out;
+	}
+	vps->vdev = vdev;
+
+	err = virtpstore_init_vqs(vps);
+	if (err < 0)
+		goto out_free;
+
+	virtpstore_init_config(vps);
+
+	err = virt_pstore_init(vps);
+	if (err)
+		goto out_del_vq;
+
+	virtpstore_confirm_config(vps);
+
Quoted 8 lines

drivers/virtio/virtio_pstore.c line 364

Quoted 3 lines
drivers/virtio/virtio_pstore.c new file +3 -0
+	init_waitqueue_head(&vps->acked);
+
+	virtio_device_ready(vdev);

This call is needed if the .probe() function uses virtqueues before returning. Right now it either doesn't use the virtqueues or it has already used them in virt_pstore_init(). Please move this before pstore_register().

Will do.

Thank you so much for your detailed review!
Namhyung

#9 lore raw

Re: [PATCH 2/3] qemu: Implement virtio-pstore device

In reply to Stefan Hajnoczi
Quoted 16 lines · Stefan Hajnoczi
Quoted 15 lines · Namhyung Kim
hw/virtio/virtio-pstore.c new file +15 -0
+static void virtio_pstore_handle_io(VirtIODevice *vdev, VirtQueue *vq)
+{
+    VirtIOPstore *s = VIRTIO_PSTORE(vdev);
+    VirtQueueElement *elem;
+    struct virtio_pstore_req req;
+    struct virtio_pstore_res res;
+    ssize_t len = 0;
+    int ret;
+
+    for (;;) {
+        elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
+        if (!elem) {
+            return;
+        }
+
Quoted 8 lines

hw/virtio/virtio-pstore.c line 438

Quoted 3 lines
hw/virtio/virtio-pstore.c new file +3 -0
+        if (elem->out_num < 1 || elem->in_num < 1) {
+            error_report("request or response buffer is missing");
+            exit(1);

The new virtio_error() function might be available, depending on when this patch series is merged. virtio_error() should be used instead of exit(1). See "[PATCH v5 0/9] virtio: avoid exit() when device enters invalid states" on qemu-devel.

Thanks for the info, will take a look.

Quoted 8 lines

hw/virtio/virtio-pstore.c line 444

Quoted 7 lines
hw/virtio/virtio-pstore.c new file +6 -0
+        }
+
+        if (elem->out_num > 2 || elem->in_num > 3) {
+            error_report("invalid number of input/output buffer");
+            exit(1);
+        }
Quoted 4 lines

The VIRTIO specification requires that flexible framing is supported. The device cannot make assumptions about the scatter-gather list. It must support any layout (e.g. even multiple 1-byte iovecs making up the buffer).

Ok.

Quoted 34 lines
Quoted 34 lines
hw/virtio/virtio-pstore.c new file +33 -0
+
+        len = iov_to_buf(elem->out_sg, elem->out_num, 0, &req, sizeof(req));
+        if (len != (ssize_t)sizeof(req)) {
+            error_report("invalid request size: %ld", (long)len);
+            exit(1);
+        }
+        res.cmd  = req.cmd;
+        res.type = req.type;
+
+        switch (le16_to_cpu(req.cmd)) {
+        case VIRTIO_PSTORE_CMD_OPEN:
+            ret = virtio_pstore_do_open(s);
+            break;
+        case VIRTIO_PSTORE_CMD_CLOSE:
+            ret = virtio_pstore_do_close(s);
+            break;
+        case VIRTIO_PSTORE_CMD_ERASE:
+            ret = virtio_pstore_do_erase(s, &req);
+            break;
+        case VIRTIO_PSTORE_CMD_READ:
+            ret = virtio_pstore_do_read(s, elem);
+            if (ret == 1) {
+                /* async channel io */
+                continue;
+            }
+            break;
+        case VIRTIO_PSTORE_CMD_WRITE:
+            ret = virtio_pstore_do_write(s, elem, &req);
+            if (ret == 1) {
+                /* async channel io */
+                continue;
+            }
+            break;
Quoted 8 lines

hw/virtio/virtio-pstore.c line 483

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+        default:
+            ret = -1;
+            break;
+        }
+
+        res.ret = ret;

Missing cpu_to_le()?

Right!

Quoted 12 lines
Quoted 12 lines
hw/virtio/virtio-pstore.c new file +11 -0
+static void pstore_set_bufsize(Object *obj, Visitor *v,
+                               const char *name, void *opaque,
+                               Error **errp)
+{
+    VirtIOPstore *s = opaque;
+    Error *error = NULL;
+    uint64_t value;
+
+    visit_type_size(v, name, &value, &error);
+    if (error) {
+        error_propagate(errp, error);
Quoted 8 lines

hw/virtio/virtio-pstore.c line 610

Quoted 5 lines
hw/virtio/virtio-pstore.c new file +5 -0
+        return;
+    }
+
+    if (value < 4096) {
+        error_setg(&error, "Warning: too small buffer size: %"PRIu64, value);

This is an error, not a warning. Please remove "Warning:" so it's clear to the user that this message caused QEMU to fail.

Will do.

Thanks,
Namhyung