Skip to contentSkip to conversation

virtio: Implement virtio pstore device (v2)

PATCH In review Namhyung Kim 5 participants 30 messages Jul 27, 2016 – Aug 1, 2016

Subsystems (7)
KVM Kbuild ACPI virtio core +3 more subsystems
lore
Share & tools
lore
#0 lore raw

cover virtio: Implement virtio pstore device (v2)

Hello,

This is v2 of the virtio-pstore work. In this patchset I addressed most of feedbacks from previous version. Limiting disk size is not implemented yet.

* 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-0003 are preparation for pstore to support virtio device which requires async write. The patch 0004 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 0005 adds support for PSTORE_TYPE_CONSOLE which was requested by Kees. The console data is appended to a single file for now.

The patch 0006 and 0007 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
  ...

To enable PSTORE_TYPE_CONSOLE, add 'console=true' to virtio-pstore device option. Also 'bufsize' option can set different size for pstore buffer (default is 16K). Maybe we can add a config option to control the compression later.

Currently the kvmtool doesn't support any options except the directory the pstore saves the logs.

Namhyung Kim (7):
pstore: Split pstore fragile flags pstore/ram: Set pstore flags dynamically
pstore: Manage buffer position for async write
virtio: Basic implementation of virtio pstore driver
virtio-pstore: Support PSTORE_TYPE_CONSOLE
qemu: Implement virtio-pstore device
kvmtool: Implement virtio-pstore device

 drivers/acpi/apei/erst.c             |   2 +-
 drivers/firmware/efi/efi-pstore.c    |   4 +-
 drivers/virtio/Kconfig               |  10 +
 drivers/virtio/Makefile              |   1 +
 drivers/virtio/virtio_pstore.c       | 421 +++++++++++++++++++++
 fs/pstore/platform.c                 |  65 +++-
 fs/pstore/ram.c                      |   8 +
 include/linux/pstore.h               |   9 +-
 include/uapi/linux/Kbuild            |   1 +
 include/uapi/linux/virtio_ids.h      |   1 +
 include/uapi/linux/virtio_pstore.h   |  78 +++-
 11 files changed, 580 insertions(+), 20 deletions(-)
 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 · 1 line
2.8.0
#1 lore raw

PATCH 1/7 pstore: Split pstore fragile flags

This patch adds new PSTORE_FLAGS for each pstore type so that they can be enabled separately. This is a preparation for ongoing virtio-pstore work to support those types flexibly.

The PSTORE_FLAGS_FRAGILE is changed to PSTORE_FLAGS_DMESG to preserve the original behavior.

Cc Anton Vorontsov <[email protected]>
Cc Colin Cross <[email protected]>
Cc Kees Cook <[email protected]>
Cc Tony Luck <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Len Brown <[email protected]>
Cc Matt Fleming <[email protected]>
---
 drivers/acpi/apei/erst.c          |  2 +-
 drivers/firmware/efi/efi-pstore.c |  2 +-
 fs/pstore/platform.c              | 17 ++++++++++-------
 fs/pstore/ram.c                   |  2 ++
 include/linux/pstore.h            |  7 ++++++-
 5 files changed, 20 insertions(+), 10 deletions(-)
drivers/acpi/apei/erst.c +1 -1
index f096ab3cb54d..ec4f507b524f 100644
@@ -938,7 +938,7 @@ static int erst_clearer(enum pstore_type_id type, u64 id, int count,
 static struct pstore_info erst_info = {
 	.owner		= THIS_MODULE,
 	.name		= "erst",
-	.flags		= PSTORE_FLAGS_FRAGILE,
+	.flags		= PSTORE_FLAGS_DMESG,
 	.open		= erst_open_pstore,
 	.close		= erst_close_pstore,
 	.read		= erst_reader,
drivers/firmware/efi/efi-pstore.c +1 -1
index 30a24d09ea6c..4daa5acd9117 100644
@@ -362,7 +362,7 @@ static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
 static struct pstore_info efi_pstore_info = {
 	.owner		= THIS_MODULE,
 	.name		= "efi",
-	.flags		= PSTORE_FLAGS_FRAGILE,
+	.flags		= PSTORE_FLAGS_DMESG,
 	.open		= efi_pstore_open,
 	.close		= efi_pstore_close,
 	.read		= efi_pstore_read,
fs/pstore/platform.c +10 -7
index 16ecca5b72d8..76dd604a0f2c 100644
@@ -659,13 +659,14 @@ int pstore_register(struct pstore_info *psi)
 	if (pstore_is_mounted())
 		pstore_get_records(0);
 
-	pstore_register_kmsg();
-
-	if ((psi->flags & PSTORE_FLAGS_FRAGILE) == 0) {
+	if (psi->flags & PSTORE_FLAGS_DMESG)
+		pstore_register_kmsg();
+	if (psi->flags & PSTORE_FLAGS_CONSOLE)
 		pstore_register_console();
+	if (psi->flags & PSTORE_FLAGS_FTRACE)
 		pstore_register_ftrace();
+	if (psi->flags & PSTORE_FLAGS_PMSG)
 		pstore_register_pmsg();
-	}
 
 	if (pstore_update_ms >= 0) {
 		pstore_timer.expires = jiffies +
@@ -689,12 +690,14 @@ EXPORT_SYMBOL_GPL(pstore_register);
 
 void pstore_unregister(struct pstore_info *psi)
 {
-	if ((psi->flags & PSTORE_FLAGS_FRAGILE) == 0) {
+	if (psi->flags & PSTORE_FLAGS_PMSG)
 		pstore_unregister_pmsg();
+	if (psi->flags & PSTORE_FLAGS_FTRACE)
 		pstore_unregister_ftrace();
+	if (psi->flags & PSTORE_FLAGS_CONSOLE)
 		pstore_unregister_console();
-	}
-	pstore_unregister_kmsg();
+	if (psi->flags & PSTORE_FLAGS_DMESG)
+		pstore_unregister_kmsg();
 
 	free_buf_for_compression();
 
fs/pstore/ram.c +2 -0
index 47516a794011..ba19a74e95bc 100644
@@ -624,6 +624,8 @@ static int ramoops_probe(struct platform_device *pdev)
 		goto fail_clear;
 	}
 
+	cxt->pstore.flags = PSTORE_FLAGS_ALL;
+
 	err = pstore_register(&cxt->pstore);
 	if (err) {
 		pr_err("registering with pstore failed\n");
include/linux/pstore.h +6 -1
index 899e95e84400..069b96faf478 100644
@@ -74,7 +74,12 @@ struct pstore_info {
 	void		*data;
 };
 
-#define	PSTORE_FLAGS_FRAGILE	1
+#define PSTORE_FLAGS_DMESG	(1 << 0)
+#define PSTORE_FLAGS_CONSOLE	(1 << 1)
+#define PSTORE_FLAGS_FTRACE	(1 << 2)
+#define PSTORE_FLAGS_PMSG	(1 << 3)
+
+#define PSTORE_FLAGS_ALL	((1 << 4) - 1)
 
 extern int pstore_register(struct pstore_info *);
 extern void pstore_unregister(struct pstore_info *);
Signature · 1 line
2.8.0
#2 lore raw

PATCH 2/7 pstore/ram: Set pstore flags dynamically

In reply to Namhyung Kim

The ramoops can be configured to enable each pstore type by setting their size. In that case, it'd be better not to register disabled types in the first place.

Cc Anton Vorontsov <[email protected]>
Cc Colin Cross <[email protected]>
Cc Kees Cook <[email protected]>
Cc Tony Luck <[email protected]>
---
 fs/pstore/ram.c        | 8 +++++++-
 include/linux/pstore.h | 2 --
 2 files changed, 7 insertions(+), 3 deletions(-)
fs/pstore/ram.c +7 -1
index ba19a74e95bc..6c93268f7ced 100644
@@ -624,7 +624,13 @@ static int ramoops_probe(struct platform_device *pdev)
 		goto fail_clear;
 	}
 
-	cxt->pstore.flags = PSTORE_FLAGS_ALL;
+	cxt->pstore.flags = PSTORE_FLAGS_DMESG;
+	if (cxt->console_size)
+		cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
+	if (cxt->ftrace_size)
+		cxt->pstore.flags |= PSTORE_FLAGS_FTRACE;
+	if (cxt->pmsg_size)
+		cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
 
 	err = pstore_register(&cxt->pstore);
 	if (err) {
include/linux/pstore.h +0 -2
index 069b96faf478..9790904de6d2 100644
@@ -79,8 +79,6 @@ struct pstore_info {
 #define PSTORE_FLAGS_FTRACE	(1 << 2)
 #define PSTORE_FLAGS_PMSG	(1 << 3)
 
-#define PSTORE_FLAGS_ALL	((1 << 4) - 1)
-
 extern int pstore_register(struct pstore_info *);
 extern void pstore_unregister(struct pstore_info *);
 extern bool pstore_cannot_block_path(enum kmsg_dump_reason reason);
Signature · 1 line
2.8.0
#3 lore raw

PATCH 3/7 pstore: Manage buffer position for async write

In reply to Namhyung Kim

Some pstore backend only supports async mode, so it's possible to do some IO at the same time. In this case we need to manage the single psinfo->buf not to broken by concurrent IO.

For example PSTORE_TYPE_CONSOLE IO is serialized by psinfo->buf_lock but later IO can be issued before finishing previous request. In this case it overwrites psinfo->buf so the previous result might be broken.

This patch adds psinfo->bufpos field to keep track of current position in order to use psinfo->buf as a ring buffer. However it's just a simple, best-effort way of doing that, and provides no 100% guarantee. It's only effective for small concurrent IO like PSTORE_TYPE_CONSOLE IMHO.

The new PSTORE_FLAGS_ASYNC flag enables management of buffer position. The pstore_prepare_buf() is called before accessing the psinfo->buf and the pstore_update_buf() is called after accessing the buf.

The pstore_get_buf() is provided for psinfo->write callback to determine the current position of available buffer.

Cc Anton Vorontsov <[email protected]>
Cc Colin Cross <[email protected]>
Cc Kees Cook <[email protected]>
Cc Tony Luck <[email protected]>
Cc Matt Fleming <[email protected]>
---
 drivers/firmware/efi/efi-pstore.c |  2 +-
 fs/pstore/platform.c              | 48 +++++++++++++++++++++++++++++++--------
 include/linux/pstore.h            |  4 ++++
 3 files changed, 44 insertions(+), 10 deletions(-)
drivers/firmware/efi/efi-pstore.c +1 -1
index 4daa5acd9117..ae0ffe259e07 100644
@@ -259,7 +259,7 @@ static int efi_pstore_write(enum pstore_type_id type,
 
 	efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
 			      !pstore_cannot_block_path(reason),
-			      size, psi->buf);
+			      size, pstore_get_buf(psi));
 
 	if (reason == KMSG_DUMP_OOPS)
 		efivar_run_worker();
fs/pstore/platform.c +39 -9

112 lines not on this page · raw message

include/linux/pstore.h +4 -0
index 9790904de6d2..14f524177b1f 100644
@@ -52,6 +52,7 @@ struct pstore_info {
 	spinlock_t	buf_lock;	/* serialize access to 'buf' */
 	char		*buf;
 	size_t		bufsize;
+	size_t		bufpos;
 	struct mutex	read_mutex;	/* serialize open/read/close */
 	int		flags;
 	int		(*open)(struct pstore_info *psi);
@@ -79,8 +80,11 @@ struct pstore_info {
 #define PSTORE_FLAGS_FTRACE	(1 << 2)
 #define PSTORE_FLAGS_PMSG	(1 << 3)
 
+#define PSTORE_FLAGS_ASYNC      (1 << 30)
+
 extern int pstore_register(struct pstore_info *);
 extern void pstore_unregister(struct pstore_info *);
 extern bool pstore_cannot_block_path(enum kmsg_dump_reason reason);
+extern void *pstore_get_buf(struct pstore_info *);
 
 #endif /*_LINUX_PSTORE_H*/
Signature · 1 line
2.8.0
#4 lore raw

PATCH 4/7 virtio: Basic implementation of virtio pstore driver

In reply to 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 single order-2 page buffer. 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. The buffer size is configurable now.

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]>
---
 drivers/virtio/Kconfig             |  10 +
 drivers/virtio/Makefile            |   1 +
 drivers/virtio/virtio_pstore.c     | 414 +++++++++++++++++++++++++++++++++++++
 include/uapi/linux/Kbuild          |   1 +
 include/uapi/linux/virtio_ids.h    |   1 +
 include/uapi/linux/virtio_pstore.h |  74 +++++++
 6 files changed, 501 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 +414 -0

414 lines not on this page · raw message

include/uapi/linux/Kbuild +1 -0
index ec10cfef166a..c3a7d56599c5 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 · 1 line
2.8.0
#5 lore raw

PATCH 4/7 virtio: Basic implementation of virtio pstore driver

In reply to 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 single order-2 page buffer. 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. The buffer size is configurable now.

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]>
---
 drivers/virtio/Kconfig             |  10 +
 drivers/virtio/Makefile            |   1 +
 drivers/virtio/virtio_pstore.c     | 414 +++++++++++++++++++++++++++++++++++++
 include/uapi/linux/Kbuild          |   1 +
 include/uapi/linux/virtio_ids.h    |   1 +
 include/uapi/linux/virtio_pstore.h |  74 +++++++
 6 files changed, 501 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 +414 -0

414 lines not on this page · raw message

include/uapi/linux/Kbuild +1 -0
index ec10cfef166a..c3a7d56599c5 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.8.0

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

PATCH 5/7 virtio-pstore: Support PSTORE_TYPE_CONSOLE

In reply to Namhyung Kim

With help of buffer management functions, it now supports CONSOLE type pstore write. The config space has flags field which defines the VIRTIO_PSTORE_CONFIG_FL_CONSOLE. When it's set, the virtio-pstore driver also sets PSTORE_FLAGS_ASYNC so that the buffer management is enabled.

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]>
---
 drivers/virtio/virtio_pstore.c     | 9 ++++++++-
 include/uapi/linux/virtio_pstore.h | 4 ++++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/virtio/virtio_pstore.c +8 -1
index 4ee1c186f582..458c4d3ccbb1 100644
@@ -33,6 +33,7 @@ struct type_table {
 	u16 virtio;
 } type_table[] = {
 	TYPE_TABLE_ENTRY(DMESG),
+	TYPE_TABLE_ENTRY(CONSOLE),
 };
 
 #undef TYPE_TABLE_ENTRY
@@ -276,7 +277,8 @@ static int virt_pstore_init(struct virtio_pstore *vps)
 	psinfo->read  = virt_pstore_read;
 	psinfo->erase = virt_pstore_erase;
 	psinfo->write = virt_pstore_write;
-	psinfo->flags = PSTORE_FLAGS_DMESG;
+	/* preserve flags from config */
+	psinfo->flags |= PSTORE_FLAGS_DMESG;
 
 	psinfo->data  = vps;
 	spin_lock_init(&psinfo->buf_lock);
@@ -313,10 +315,15 @@ static int virtpstore_init_vqs(struct virtio_pstore *vps)
 static void virtpstore_init_config(struct virtio_pstore *vps)
 {
 	u32 bufsize;
+	u32 flags;
 
 	virtio_cread(vps->vdev, struct virtio_pstore_config, bufsize, &bufsize);
+	virtio_cread(vps->vdev, struct virtio_pstore_config, flags, &flags);
 
 	vps->pstore.bufsize = PAGE_ALIGN(bufsize);
+
+	if (flags & VIRTIO_PSTORE_CONFIG_FL_CONSOLE)
+		vps->pstore.flags |= PSTORE_FLAGS_CONSOLE | PSTORE_FLAGS_ASYNC;
 }
 
 static void virtpstore_confirm_config(struct virtio_pstore *vps)
include/uapi/linux/virtio_pstore.h +4 -0
index f4b0d204d8ae..56d0b1554231 100644
@@ -37,9 +37,12 @@
 
 #define VIRTIO_PSTORE_TYPE_UNKNOWN  0
 #define VIRTIO_PSTORE_TYPE_DMESG    1
+#define VIRTIO_PSTORE_TYPE_CONSOLE  2
 
 #define VIRTIO_PSTORE_FL_COMPRESSED  1
 
+#define VIRTIO_PSTORE_CONFIG_FL_CONSOLE  (1 << 0)
+
 struct virtio_pstore_req {
 	__virtio16		cmd;
 	__virtio16		type;
@@ -69,6 +72,7 @@ struct virtio_pstore_fileinfo {
 
 struct virtio_pstore_config {
 	__virtio32		bufsize;
+	__virtio32		flags;
 };
 
 #endif /* _LINUX_VIRTIO_PSTORE_H */
Signature · 1 line
2.8.0
#7 lore raw

PATCH 5/7 virtio-pstore: Support PSTORE_TYPE_CONSOLE

In reply to Namhyung Kim

With help of buffer management functions, it now supports CONSOLE type pstore write. The config space has flags field which defines the VIRTIO_PSTORE_CONFIG_FL_CONSOLE. When it's set, the virtio-pstore driver also sets PSTORE_FLAGS_ASYNC so that the buffer management is enabled.

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]>
---
 drivers/virtio/virtio_pstore.c     | 9 ++++++++-
 include/uapi/linux/virtio_pstore.h | 4 ++++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/virtio/virtio_pstore.c +8 -1
index 4ee1c186f582..458c4d3ccbb1 100644
@@ -33,6 +33,7 @@ struct type_table {
 	u16 virtio;
 } type_table[] = {
 	TYPE_TABLE_ENTRY(DMESG),
+	TYPE_TABLE_ENTRY(CONSOLE),
 };
 
 #undef TYPE_TABLE_ENTRY
@@ -276,7 +277,8 @@ static int virt_pstore_init(struct virtio_pstore *vps)
 	psinfo->read  = virt_pstore_read;
 	psinfo->erase = virt_pstore_erase;
 	psinfo->write = virt_pstore_write;
-	psinfo->flags = PSTORE_FLAGS_DMESG;
+	/* preserve flags from config */
+	psinfo->flags |= PSTORE_FLAGS_DMESG;
 
 	psinfo->data  = vps;
 	spin_lock_init(&psinfo->buf_lock);
@@ -313,10 +315,15 @@ static int virtpstore_init_vqs(struct virtio_pstore *vps)
 static void virtpstore_init_config(struct virtio_pstore *vps)
 {
 	u32 bufsize;
+	u32 flags;
 
 	virtio_cread(vps->vdev, struct virtio_pstore_config, bufsize, &bufsize);
+	virtio_cread(vps->vdev, struct virtio_pstore_config, flags, &flags);
 
 	vps->pstore.bufsize = PAGE_ALIGN(bufsize);
+
+	if (flags & VIRTIO_PSTORE_CONFIG_FL_CONSOLE)
+		vps->pstore.flags |= PSTORE_FLAGS_CONSOLE | PSTORE_FLAGS_ASYNC;
 }
 
 static void virtpstore_confirm_config(struct virtio_pstore *vps)
include/uapi/linux/virtio_pstore.h +4 -0
index f4b0d204d8ae..56d0b1554231 100644
@@ -37,9 +37,12 @@
 
 #define VIRTIO_PSTORE_TYPE_UNKNOWN  0
 #define VIRTIO_PSTORE_TYPE_DMESG    1
+#define VIRTIO_PSTORE_TYPE_CONSOLE  2
 
 #define VIRTIO_PSTORE_FL_COMPRESSED  1
 
+#define VIRTIO_PSTORE_CONFIG_FL_CONSOLE  (1 << 0)
+
 struct virtio_pstore_req {
 	__virtio16		cmd;
 	__virtio16		type;
@@ -69,6 +72,7 @@ struct virtio_pstore_fileinfo {
 
 struct virtio_pstore_config {
 	__virtio32		bufsize;
+	__virtio32		flags;
 };
 
 #endif /* _LINUX_VIRTIO_PSTORE_H */
Signature · 6 lines
2.8.0

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

PATCH 6/7 qemu: Implement virtio-pstore device

In reply to Namhyung Kim

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' and 'console' (boolean) properties.

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]>
---
 hw/virtio/Makefile.objs                        |   2 +-
 hw/virtio/virtio-pci.c                         |  54 +++
 hw/virtio/virtio-pci.h                         |  14 +
 hw/virtio/virtio-pstore.c                      | 477 +++++++++++++++++++++++++
 include/hw/pci/pci.h                           |   1 +
 include/hw/virtio/virtio-pstore.h              |  34 ++
 include/standard-headers/linux/virtio_ids.h    |   1 +
 include/standard-headers/linux/virtio_pstore.h |  80 +++++
 qdev-monitor.c                                 |   1 +
 9 files changed, 663 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 +54 -0

66 lines not on this page · raw message

hw/virtio/virtio-pci.h +14 -0
index e4548c2..b4c039f 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 */
 
@@ -311,6 +313,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 +477 -0

477 lines not on this page · raw message

include/hw/pci/pci.h +1 -0
index 74d797d..000e1e9 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 +34 -0
new file mode 100644
index 0000000..d188a48
@@ -0,0 +1,34 @@
+/*
+ * 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      *vq[2];
+    char           *directory;
+    uint64_t        id;
+    uint64_t        console_id;
+    DIR            *dirp;
+    uint64_t        bufsize;
+    bool            console;
+} 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 +80 -0

80 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 · 1 line
2.8.0
#24 lore raw

PATCH 6/7 qemu: Implement virtio-pstore device

In reply to Namhyung Kim

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' and 'console' (boolean) properties.

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]>
---
 hw/virtio/Makefile.objs                        |   2 +-
 hw/virtio/virtio-pci.c                         |  54 +++
 hw/virtio/virtio-pci.h                         |  14 +
 hw/virtio/virtio-pstore.c                      | 477 +++++++++++++++++++++++++
 include/hw/pci/pci.h                           |   1 +
 include/hw/virtio/virtio-pstore.h              |  34 ++
 include/standard-headers/linux/virtio_ids.h    |   1 +
 include/standard-headers/linux/virtio_pstore.h |  80 +++++
 qdev-monitor.c                                 |   1 +
 9 files changed, 663 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 +54 -0

66 lines not on this page · raw message

hw/virtio/virtio-pci.h +14 -0
index e4548c2..b4c039f 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 */
 
@@ -311,6 +313,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 +477 -0

477 lines not on this page · raw message

include/hw/pci/pci.h +1 -0
index 74d797d..000e1e9 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 +34 -0
new file mode 100644
index 0000000..d188a48
@@ -0,0 +1,34 @@
+/*
+ * 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      *vq[2];
+    char           *directory;
+    uint64_t        id;
+    uint64_t        console_id;
+    DIR            *dirp;
+    uint64_t        bufsize;
+    bool            console;
+} 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 +80 -0

80 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.8.0

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

PATCH 7/7 kvmtool: Implement virtio-pstore device

In reply to Namhyung Kim

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).

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 Will Deacon <[email protected]>
---
 Makefile                     |   1 +
 builtin-run.c                |   2 +
 include/kvm/kvm-config.h     |   1 +
 include/kvm/virtio-pci-dev.h |   2 +
 include/kvm/virtio-pstore.h  |  57 ++++++
 include/linux/virtio_ids.h   |   1 +
 virtio/pstore.c              | 459 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 523 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 +57 -0

57 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 +459 -0

459 lines not on this page · raw message

Signature · 1 line
2.8.0
#26 lore raw

PATCH 7/7 kvmtool: Implement virtio-pstore device

In reply to Namhyung Kim

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).

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 Will Deacon <[email protected]>
---
 Makefile                     |   1 +
 builtin-run.c                |   2 +
 include/kvm/kvm-config.h     |   1 +
 include/kvm/virtio-pci-dev.h |   2 +
 include/kvm/virtio-pstore.h  |  57 ++++++
 include/linux/virtio_ids.h   |   1 +
 virtio/pstore.c              | 459 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 523 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 +57 -0

57 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 +459 -0

459 lines not on this page · raw message

Signature · 1 line
2.8.0
#27 lore raw
In reply to Namhyung Kim
Quoted 5 lines · Namhyung Kim

Hello,

This is v2 of the virtio-pstore work. In this patchset I addressed most of feedbacks from previous version. Limiting disk size is not implemented yet.

For some reason, only parts of the patchset were received. Pls post all patches to all lists.

If you are changing the virtio interface with host, like a new device, they you must copy the virtio TC so make sure there are no objections from there.

https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=virtio#feedback

Quoted 126 lines

* 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-0003 are preparation for pstore to support virtio device which requires async write. The patch 0004 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 0005 adds support for PSTORE_TYPE_CONSOLE which was requested by Kees. The console data is appended to a single file for now.

The patch 0006 and 0007 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
  ...

To enable PSTORE_TYPE_CONSOLE, add 'console=true' to virtio-pstore device option. Also 'bufsize' option can set different size for pstore buffer (default is 16K). Maybe we can add a config option to control the compression later.

Currently the kvmtool doesn't support any options except the directory the pstore saves the logs.

Namhyung Kim (7):
pstore: Split pstore fragile flags pstore/ram: Set pstore flags dynamically
pstore: Manage buffer position for async write
virtio: Basic implementation of virtio pstore driver
virtio-pstore: Support PSTORE_TYPE_CONSOLE
qemu: Implement virtio-pstore device
kvmtool: Implement virtio-pstore device

 drivers/acpi/apei/erst.c             |   2 +-
 drivers/firmware/efi/efi-pstore.c    |   4 +-
 drivers/virtio/Kconfig               |  10 +
 drivers/virtio/Makefile              |   1 +
 drivers/virtio/virtio_pstore.c       | 421 +++++++++++++++++++++
 fs/pstore/platform.c                 |  65 +++-
 fs/pstore/ram.c                      |   8 +
 include/linux/pstore.h               |   9 +-
 include/uapi/linux/Kbuild            |   1 +
 include/uapi/linux/virtio_ids.h      |   1 +
 include/uapi/linux/virtio_pstore.h   |  78 +++-
 11 files changed, 580 insertions(+), 20 deletions(-)
 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]>
Quoted 11 lines

Thanks,
Namhyung

Signature · 6 lines
2.8.0

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

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

#9 lore raw

Re: [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Namhyung Kim
Quoted 7 lines · Namhyung Kim

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

So if the point is handling system crashes, I suspect virtio is the wrong protocol to use. ATM it's rather elaborate for performance, it's likely not to work when linux is crashing. I think you want something very very simple that will still work when you crash. Like maybe a serial device?

Quoted 5 lines
  $ 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).

So this lacks all management tools that a regular storage device has, and these are necessary if people are to use this in production.

For example, some kind of provision for limiting the amount of host disk this can consume seems called for. Rate-limiting disk writes on host also seems necessary. Handling host disk errors always propagates error to guest but an option to e.g. stop vm might be useful to avoid corruption.

Quoted 3 lines

The 'directory' property is required for virtio-pstore device to work. It also adds 'bufsize' and 'console' (boolean) properties.

No idea what these do. Seem to be RW by guest but have no effect otherwise.

Quoted 157 lines
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]>
---
 hw/virtio/Makefile.objs                        |   2 +-
 hw/virtio/virtio-pci.c                         |  54 +++
 hw/virtio/virtio-pci.h                         |  14 +
 hw/virtio/virtio-pstore.c                      | 477 +++++++++++++++++++++++++
 include/hw/pci/pci.h                           |   1 +
 include/hw/virtio/virtio-pstore.h              |  34 ++
 include/standard-headers/linux/virtio_ids.h    |   1 +
 include/standard-headers/linux/virtio_pstore.h |  80 +++++
 qdev-monitor.c                                 |   1 +
 9 files changed, 663 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 +54 -0
index f0677b7..d99a405 100644
@@ -2414,6 +2414,59 @@ static const TypeInfo virtio_host_pci_info = {
 };
 #endif
 
+/* virtio-pstore-pci */
+
+static void virtio_pstore_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
+{
+    VirtIOPstorePCI *vps = VIRTIO_PSTORE_PCI(vpci_dev);
+    DeviceState *vdev = DEVICE(&vps->vdev);
+    Error *err = NULL;
+
+    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
+    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
+    if (err) {
+        error_propagate(errp, err);
+        return;
+    }
+}
+
+static void virtio_pstore_pci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
+    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
+
+    k->realize = virtio_pstore_pci_realize;
+    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
+
+    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
+    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_PSTORE;
+    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
+    pcidev_k->class_id = PCI_CLASS_OTHERS;
+}
+
+static void virtio_pstore_pci_instance_init(Object *obj)
+{
+    VirtIOPstorePCI *dev = VIRTIO_PSTORE_PCI(obj);
+
+    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
+                                TYPE_VIRTIO_PSTORE);
+    object_property_add_alias(obj, "directory", OBJECT(&dev->vdev),
+                              "directory", &error_abort);
+    object_property_add_alias(obj, "bufsize", OBJECT(&dev->vdev),
+                              "bufsize", &error_abort);
+    object_property_add_alias(obj, "console", OBJECT(&dev->vdev),
+                              "console", &error_abort);
+}
+
+static const TypeInfo virtio_pstore_pci_info = {
+    .name          = TYPE_VIRTIO_PSTORE_PCI,
+    .parent        = TYPE_VIRTIO_PCI,
+    .instance_size = sizeof(VirtIOPstorePCI),
+    .instance_init = virtio_pstore_pci_instance_init,
+    .class_init    = virtio_pstore_pci_class_init,
+};
+
 /* virtio-pci-bus */
 
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
@@ -2483,6 +2536,7 @@ static void virtio_pci_register_types(void)
 #ifdef CONFIG_VHOST_SCSI
     type_register_static(&vhost_scsi_pci_info);
 #endif
+    type_register_static(&virtio_pstore_pci_info);
 }
 
 type_init(virtio_pci_register_types)
hw/virtio/virtio-pci.h +14 -0
index e4548c2..b4c039f 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 */
 
@@ -311,6 +313,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 +0 -0
new file mode 100644
index 0000000..2ca7786

hw/virtio/virtio-pstore.c line 9

Quoted 9 lines
hw/virtio/virtio-pstore.c new file +9 -0
@@ -0,0 +1,477 @@
+/*
+ * Virtio Pstore Device
+ *
+ * Copyright (C) 2016  LG Electronics
+ *
+ * Authors:
+ *  Namhyung Kim  <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.

We generally ask new code to be v2 or later.

Quoted 47 lines
hw/virtio/virtio-pstore.c new file +46 -0
 See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include <stdio.h>
+
+#include "qemu/osdep.h"
+#include "qemu/iov.h"
+#include "qemu-common.h"
+#include "qemu/cutils.h"
+#include "qemu/error-report.h"
+#include "sysemu/kvm.h"
+#include "qapi/visitor.h"
+#include "qapi-event.h"
+#include "trace.h"
+
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-bus.h"
+#include "hw/virtio/virtio-access.h"
+#include "hw/virtio/virtio-pstore.h"
+
+
+static void virtio_pstore_to_filename(VirtIOPstore *s, char *buf, size_t sz,
+                                      struct virtio_pstore_req *req)
+{
+    const char *basename;
+    unsigned long long id = 0;
+    unsigned int flags = le32_to_cpu(req->flags);
+
+    switch (le16_to_cpu(req->type)) {
+    case VIRTIO_PSTORE_TYPE_DMESG:
+        basename = "dmesg";
+        id = s->id++;
+        break;
+    case VIRTIO_PSTORE_TYPE_CONSOLE:
+        basename = "console";
+        if (s->console_id) {
+            id = s->console_id;
+        } else {
+            id = s->console_id = s->id++;
+        }
+        break;
+    default:
+        basename = "unknown";
+        break;
+    }

hw/virtio/virtio-pstore.c line 65

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +10 -0
+
+    snprintf(buf, sz, "%s/%s-%llu%s", s->directory, basename, id,
+             flags & VIRTIO_PSTORE_FL_COMPRESSED ? ".enc.z" : "");
+}
+
+static void virtio_pstore_from_filename(VirtIOPstore *s, char *name,
+                                        char *buf, size_t sz,
+                                        struct virtio_pstore_fileinfo *info)
+{
+    snprintf(buf, sz, "%s/%s", s->directory, name);

if this does not fit, buf will not be 0 terminated and can generally be corrupted.

Quoted 121 lines
hw/virtio/virtio-pstore.c new file +121 -0
+
+    if (g_str_has_prefix(name, "dmesg-")) {
+        info->type = VIRTIO_PSTORE_TYPE_DMESG;
+        name += strlen("dmesg-");
+    } else if (g_str_has_prefix(name, "console-")) {
+        info->type = VIRTIO_PSTORE_TYPE_CONSOLE;
+        name += strlen("console-");
+    } else if (g_str_has_prefix(name, "unknown-")) {
+        info->type = VIRTIO_PSTORE_TYPE_UNKNOWN;
+        name += strlen("unknown-");
+    }
+
+    qemu_strtoull(name, NULL, 0, &info->id);
+
+    info->flags = 0;
+    if (g_str_has_suffix(name, ".enc.z")) {
+        info->flags |= VIRTIO_PSTORE_FL_COMPRESSED;
+    }
+}
+
+static ssize_t virtio_pstore_do_open(VirtIOPstore *s)
+{
+    s->dirp = opendir(s->directory);
+    if (s->dirp == NULL) {
+        return -1;
+    }
+
+    return 0;
+}
+
+static ssize_t virtio_pstore_do_read(VirtIOPstore *s, struct iovec *in_sg,
+                                     unsigned int in_num,
+                                     struct virtio_pstore_res *res)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    struct stat stbuf;
+    struct dirent *dent;
+    int sg_num = in_num;
+    struct iovec sg[sg_num];
+    struct virtio_pstore_fileinfo info;
+    size_t offset = sizeof(*res) + sizeof(info);
+
+    if (s->dirp == NULL) {
+        return -1;
+    }
+
+    dent = readdir(s->dirp);
+    while (dent) {
+        if (dent->d_name[0] != '.') {
+            break;
+        }
+        dent = readdir(s->dirp);
+    }
+
+    if (dent == NULL) {
+        return 0;
+    }
+
+    /* skip res and fileinfo */
+    sg_num = iov_copy(sg, sg_num, in_sg, in_num, offset,
+                      iov_size(in_sg, in_num) - offset);
+
+    virtio_pstore_from_filename(s, dent->d_name, path, sizeof(path), &info);
+    fd = open(path, O_RDONLY);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
+        return -1;
+    }
+
+    if (fstat(fd, &stbuf) < 0) {
+        len = -1;
+        goto out;
+    }
+
+    len = readv(fd, sg, sg_num);
+    if (len < 0) {
+        if (errno == EAGAIN) {
+            len = 0;
+        }
+        goto out;
+    }
+
+    info.id        = cpu_to_le64(info.id);
+    info.type      = cpu_to_le16(info.type);
+    info.flags     = cpu_to_le32(info.flags);
+    info.len       = cpu_to_le32(len);
+    info.time_sec  = cpu_to_le64(stbuf.st_ctim.tv_sec);
+    info.time_nsec = cpu_to_le32(stbuf.st_ctim.tv_nsec);
+
+    iov_from_buf(in_sg, in_num, sizeof(*res), &info, sizeof(info));
+    len += sizeof(info);
+
+ out:
+    close(fd);
+    return len;
+}
+
+static ssize_t virtio_pstore_do_write(VirtIOPstore *s, struct iovec *out_sg,
+                                      unsigned int out_num,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    unsigned short type;
+    int flags = O_WRONLY | O_CREAT;
+
+    /* we already consume the req */
+    iov_discard_front(&out_sg, &out_num, sizeof(*req));
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    type = le16_to_cpu(req->type);
+
+    if (type == VIRTIO_PSTORE_TYPE_DMESG) {
+        flags |= O_TRUNC;
+    } else if (type == VIRTIO_PSTORE_TYPE_CONSOLE) {
+        flags |= O_APPEND;
+    }

hw/virtio/virtio-pstore.c line 196

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +10 -0
+
+    fd = open(path, flags, 0644);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
+        return -1;
+    }
+    len = writev(fd, out_sg, out_num);
+    close(fd);
+
+    return len;

All this is blocking VM until host io completes.

Quoted 71 lines
hw/virtio/virtio-pstore.c new file +71 -0
+}
+
+static ssize_t virtio_pstore_do_close(VirtIOPstore *s)
+{
+    if (s->dirp == NULL) {
+        return 0;
+    }
+
+    closedir(s->dirp);
+    s->dirp = NULL;
+
+    return 0;
+}
+
+static ssize_t virtio_pstore_do_erase(VirtIOPstore *s,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    return unlink(path);
+}
+
+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;
+        }
+
+        if (elem->out_num < 1 || elem->in_num < 1) {
+            error_report("request or response buffer is missing");
+            exit(1);
+        }
+
+        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_READ:
+            ret = virtio_pstore_do_read(s, elem->in_sg, elem->in_num, &res);
+            if (ret > 0) {
+                len = ret;
+                ret = 0;
+            }
+            break;
+        case VIRTIO_PSTORE_CMD_WRITE:
+            ret = virtio_pstore_do_write(s, elem->out_sg, elem->out_num, &req);
+            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);

hw/virtio/virtio-pstore.c line 277

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +10 -0
+            break;
+        default:
+            ret = -1;
+            break;
+        }
+
+        res.ret  = ret;
+
+        iov_from_buf(elem->in_sg, elem->in_num, 0, &res, sizeof(res));
+        virtqueue_push(vq, elem, sizeof(res) + len);

this is wrong - len should be # of bytes written into guest memory.

Quoted 363 lines
hw/virtio/virtio-pstore.c new file +200 -0
+
+        virtio_notify(vdev, vq);
+        g_free(elem);
+
+        if (ret < 0) {
+            return;
+        }
+    }
+}
+
+static void virtio_pstore_device_realize(DeviceState *dev, Error **errp)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+    VirtIOPstore *s = VIRTIO_PSTORE(dev);
+
+    virtio_init(vdev, "virtio-pstore", VIRTIO_ID_PSTORE,
+                sizeof(struct virtio_pstore_config));
+
+    s->id = 1;
+    s->console_id = 0;
+
+    s->vq[0] = virtio_add_queue(vdev, 128, virtio_pstore_handle_io);
+    s->vq[1] = virtio_add_queue(vdev, 128, virtio_pstore_handle_io);
+}
+
+static void virtio_pstore_device_unrealize(DeviceState *dev, Error **errp)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+
+    virtio_cleanup(vdev);
+}
+
+static void virtio_pstore_get_config(VirtIODevice *vdev, uint8_t *config_data)
+{
+    VirtIOPstore *dev = VIRTIO_PSTORE(vdev);
+    struct virtio_pstore_config config;
+
+    config.bufsize = cpu_to_le32(dev->bufsize);
+    if (dev->console) {
+        config.flags |= cpu_to_le32(VIRTIO_PSTORE_CONFIG_FL_CONSOLE);
+    }
+
+    memcpy(config_data, &config, sizeof(struct virtio_pstore_config));
+}
+
+static void virtio_pstore_set_config(VirtIODevice *vdev,
+                                     const uint8_t *config_data)
+{
+    VirtIOPstore *dev = VIRTIO_PSTORE(vdev);
+    struct virtio_pstore_config config;
+
+    memcpy(&config, config_data, sizeof(struct virtio_pstore_config));
+
+    dev->bufsize = le32_to_cpu(config.bufsize);
+}
+
+static uint64_t get_features(VirtIODevice *vdev, uint64_t f, Error **errp)
+{
+    return f;
+}
+
+static void pstore_get_directory(Object *obj, Visitor *v,
+                                 const char *name, void *opaque,
+                                 Error **errp)
+{
+    VirtIOPstore *s = opaque;
+
+    visit_type_str(v, name, &s->directory, errp);
+}
+
+static void pstore_set_directory(Object *obj, Visitor *v,
+                                 const char *name, void *opaque,
+                                 Error **errp)
+{
+    VirtIOPstore *s = opaque;
+    Error *local_err = NULL;
+    char *value;
+
+    visit_type_str(v, name, &value, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+
+    g_free(s->directory);
+    s->directory = value;
+}
+
+static void pstore_release_directory(Object *obj, const char *name,
+                                     void *opaque)
+{
+    VirtIOPstore *s = opaque;
+
+    g_free(s->directory);
+    s->directory = NULL;
+}
+
+static void pstore_get_bufsize(Object *obj, Visitor *v,
+                               const char *name, void *opaque,
+                               Error **errp)
+{
+    VirtIOPstore *s = opaque;
+    uint64_t value = s->bufsize;
+
+    visit_type_size(v, name, &value, errp);
+}
+
+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);
+        return;
+    }
+
+    if (value < 4096) {
+        error_report("Warning: too small buffer size: %"PRIu64, value);
+    }
+
+    s->bufsize = value;
+}
+
+static void pstore_get_console(Object *obj, Visitor *v,
+                               const char *name, void *opaque,
+                               Error **errp)
+{
+    VirtIOPstore *s = opaque;
+    bool value = s->console;
+
+    visit_type_bool(v, name, &value, errp);
+}
+
+static void pstore_set_console(Object *obj, Visitor *v,
+                               const char *name, void *opaque,
+                               Error **errp)
+{
+    VirtIOPstore *s = opaque;
+    Error *error = NULL;
+    bool value;
+
+    visit_type_bool(v, name, &value, &error);
+    if (error) {
+        error_propagate(errp, error);
+        return;
+    }
+
+    s->console = value;
+}
+
+static Property virtio_pstore_properties[] = {
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void virtio_pstore_instance_init(Object *obj)
+{
+    VirtIOPstore *s = VIRTIO_PSTORE(obj);
+
+    object_property_add(obj, "directory", "str",
+                        pstore_get_directory, pstore_set_directory,
+                        pstore_release_directory, s, NULL);
+    object_property_add(obj, "bufsize", "size",
+                        pstore_get_bufsize, pstore_set_bufsize, NULL, s, NULL);
+    object_property_add(obj, "console", "bool",
+                        pstore_get_console, pstore_set_console, NULL, s, NULL);
+}
+
+static void virtio_pstore_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
+
+    dc->props = virtio_pstore_properties;
+    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
+    vdc->realize = virtio_pstore_device_realize;
+    vdc->unrealize = virtio_pstore_device_unrealize;
+    vdc->get_config = virtio_pstore_get_config;
+    vdc->set_config = virtio_pstore_set_config;
+    vdc->get_features = get_features;
+}
+
+static const TypeInfo virtio_pstore_info = {
+    .name = TYPE_VIRTIO_PSTORE,
+    .parent = TYPE_VIRTIO_DEVICE,
+    .instance_size = sizeof(VirtIOPstore),
+    .instance_init = virtio_pstore_instance_init,
+    .class_init = virtio_pstore_class_init,
+};
+
+static void virtio_register_types(void)
+{
+    type_register_static(&virtio_pstore_info);
+}
+
+type_init(virtio_register_types)
include/hw/pci/pci.h +1 -0
index 74d797d..000e1e9 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 +34 -0
new file mode 100644
index 0000000..d188a48
@@ -0,0 +1,34 @@
+/*
+ * 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      *vq[2];
+    char           *directory;
+    uint64_t        id;
+    uint64_t        console_id;
+    DIR            *dirp;
+    uint64_t        bufsize;
+    bool            console;
+} 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 +80 -0
new file mode 100644
index 0000000..b893d15
@@ -0,0 +1,80 @@
+#ifndef _LINUX_VIRTIO_PSTORE_H
+#define _LINUX_VIRTIO_PSTORE_H
+/* This header is BSD licensed so anyone can use the definitions to implement
+ * compatible drivers/servers.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of IBM nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE. */
+#include "standard-headers/linux/types.h"
+#include "standard-headers/linux/virtio_types.h"
+#include "standard-headers/linux/virtio_ids.h"
+#include "standard-headers/linux/virtio_config.h"
+
+#define VIRTIO_PSTORE_CMD_NULL   0
+#define VIRTIO_PSTORE_CMD_OPEN   1
+#define VIRTIO_PSTORE_CMD_READ   2
+#define VIRTIO_PSTORE_CMD_WRITE  3
+#define VIRTIO_PSTORE_CMD_ERASE  4
+#define VIRTIO_PSTORE_CMD_CLOSE  5
+
+#define VIRTIO_PSTORE_TYPE_UNKNOWN  0
+#define VIRTIO_PSTORE_TYPE_DMESG    1
+#define VIRTIO_PSTORE_TYPE_CONSOLE  2
+
+#define VIRTIO_PSTORE_FL_COMPRESSED  1
+
+#define VIRTIO_PSTORE_CONFIG_FL_CONSOLE  (1 << 0)
+
+struct virtio_pstore_req {
+    __virtio16 cmd;
+    __virtio16 type;
+    __virtio32 flags;
+    __virtio64 id;
+    __virtio32 count;
+    __virtio32 reserved;
+};
+
+struct virtio_pstore_res {
+    __virtio16 cmd;
+    __virtio16 type;
+    __virtio32 ret;
+};
+
+struct virtio_pstore_fileinfo {
+    __virtio64 id;
+    __virtio32 count;
+    __virtio16 type;
+    __virtio16 unused;
+    __virtio32 flags;
+    __virtio32 len;
+    __virtio64 time_sec;
+    __virtio32 time_nsec;
+    __virtio32 reserved;
+};
+
+struct virtio_pstore_config {
+    __virtio32 bufsize;
+    __virtio32 flags;
+};
+
+#endif /* _LINUX_VIRTIO_PSTORE_H */
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 · 1 line
2.8.0

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

#28 lore raw
In reply to Michael S. Tsirkin

Hello,

Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Namhyung Kim

Hello,

This is v2 of the virtio-pstore work. In this patchset I addressed most of feedbacks from previous version. Limiting disk size is not implemented yet.

Quoted 8 lines

For some reason, only parts of the patchset were received. Pls post all patches to all lists.

If you are changing the virtio interface with host, like a new device, they you must copy the virtio TC so make sure there are no objections from there.

https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=virtio#feedback

Hmm.. I think I've CC-ed all the kvm, qemu and virtualization lists but missed the virtio list, sorry. Will add next time.

Thanks,
Namhyung

#29 lore raw
In reply to Michael S. Tsirkin

Hello,

Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Namhyung Kim

Hello,

This is v2 of the virtio-pstore work. In this patchset I addressed most of feedbacks from previous version. Limiting disk size is not implemented yet.

Quoted 8 lines

For some reason, only parts of the patchset were received. Pls post all patches to all lists.

If you are changing the virtio interface with host, like a new device, they you must copy the virtio TC so make sure there are no objections from there.

https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=virtio#feedback

Hmm.. I think I've CC-ed all the kvm, qemu and virtualization lists but missed the virtio list, sorry. Will add next time.

Thanks,
Namhyung

#10 lore raw

Re: [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Michael S. Tsirkin
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Namhyung Kim

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 ...
Quoted 8 lines
Quoted 1 line

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

So if the point is handling system crashes, I suspect virtio is the wrong protocol to use. ATM it's rather elaborate for performance, it's likely not to work when linux is crashing. I think you want something very very simple that will still work when you crash. Like maybe a serial device?

Well, I dont' know. As you know, the kernel oops dump is already sent to serial device but it's rather slow. As I wrote in the cover letter, enabling ftrace_dump_on_oops makes it even worse.. Also pstore saves the (compressed) binary data so I thought it'd be better to have a dedicated IO channel.

I know that we can't rely on anything in kernel when the kernel is crashing. In the virtualization environment, I think it'd be great if it can dump the crash data in the host directly so I chose the virtio. I thought the virtio is a relatively thin layer and independent from other kernel features. Even if it doesn't guarantee to work 100%, it's worth trying IMHO.

Quoted 11 lines
Quoted 6 lines
  $ 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).

So this lacks all management tools that a regular storage device has, and these are necessary if people are to use this in production.

Quoted 5 lines

For example, some kind of provision for limiting the amount of host disk this can consume seems called for. Rate-limiting disk writes on host also seems necessary. Handling host disk errors always propagates error to guest but an option to e.g. stop vm might be useful to avoid corruption.

Yes, it needs that kind of management. I'd like to look at the existing implementation. But basically I thought it as a debugging feature and it won't produce much data for the default setting. Maybe I can limit the file size not to exceed the buffer size and keep up to pre-configured number of files for each type. Now host disk error will be propagated to guest.

Quoted 7 lines
Quoted 4 lines

The 'directory' property is required for virtio-pstore device to work. It also adds 'bufsize' and 'console' (boolean) properties.

No idea what these do. Seem to be RW by guest but have no effect otherwise.

The 'directory' is a pathname which it saves the data files. The 'bufsize' sets the size of buffer used by pstore. The 'console' enables saving pstore console type data.

Quoted 18 lines
Quoted 18 lines
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]>

---

[SNIP]

Quoted 9 lines
Quoted 9 lines
hw/virtio/virtio-pstore.c new file +3 -0
new file mode 100644
index 0000000..2ca7786
@@ -0,0 +1,477 @@
+/*
+ * Virtio Pstore Device
+ *
Quoted 8 lines

hw/virtio/virtio-pstore.c

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+ * Copyright (C) 2016  LG Electronics
+ *
+ * Authors:
+ *  Namhyung Kim  <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.

We generally ask new code to be v2 or later.

Ok, will update.

Quoted 53 lines
Quoted 53 lines
hw/virtio/virtio-pstore.c new file +51 -0
 See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include <stdio.h>
+
+#include "qemu/osdep.h"
+#include "qemu/iov.h"
+#include "qemu-common.h"
+#include "qemu/cutils.h"
+#include "qemu/error-report.h"
+#include "sysemu/kvm.h"
+#include "qapi/visitor.h"
+#include "qapi-event.h"
+#include "trace.h"
+
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-bus.h"
+#include "hw/virtio/virtio-access.h"
+#include "hw/virtio/virtio-pstore.h"
+
+
+static void virtio_pstore_to_filename(VirtIOPstore *s, char *buf, size_t sz,
+                                      struct virtio_pstore_req *req)
+{
+    const char *basename;
+    unsigned long long id = 0;
+    unsigned int flags = le32_to_cpu(req->flags);
+
+    switch (le16_to_cpu(req->type)) {
+    case VIRTIO_PSTORE_TYPE_DMESG:
+        basename = "dmesg";
+        id = s->id++;
+        break;
+    case VIRTIO_PSTORE_TYPE_CONSOLE:
+        basename = "console";
+        if (s->console_id) {
+            id = s->console_id;
+        } else {
+            id = s->console_id = s->id++;
+        }
+        break;
+    default:
+        basename = "unknown";
+        break;
+    }
+
+    snprintf(buf, sz, "%s/%s-%llu%s", s->directory, basename, id,
+             flags & VIRTIO_PSTORE_FL_COMPRESSED ? ".enc.z" : "");
+}
+
Quoted 8 lines

hw/virtio/virtio-pstore.c line 65

Quoted 5 lines
hw/virtio/virtio-pstore.c new file +5 -0
+static void virtio_pstore_from_filename(VirtIOPstore *s, char *name,
+                                        char *buf, size_t sz,
+                                        struct virtio_pstore_fileinfo *info)
+{
+    snprintf(buf, sz, "%s/%s", s->directory, name);

if this does not fit, buf will not be 0 terminated and can generally be corrupted.

Will change it to use malloc instead.

Quoted 21 lines
Quoted 21 lines
hw/virtio/virtio-pstore.c new file +19 -0
+
+    if (g_str_has_prefix(name, "dmesg-")) {
+        info->type = VIRTIO_PSTORE_TYPE_DMESG;
+        name += strlen("dmesg-");
+    } else if (g_str_has_prefix(name, "console-")) {
+        info->type = VIRTIO_PSTORE_TYPE_CONSOLE;
+        name += strlen("console-");
+    } else if (g_str_has_prefix(name, "unknown-")) {
+        info->type = VIRTIO_PSTORE_TYPE_UNKNOWN;
+        name += strlen("unknown-");
+    }
+
+    qemu_strtoull(name, NULL, 0, &info->id);
+
+    info->flags = 0;
+    if (g_str_has_suffix(name, ".enc.z")) {
+        info->flags |= VIRTIO_PSTORE_FL_COMPRESSED;
+    }
+}

[SNIP]

Quoted 26 lines
Quoted 26 lines
hw/virtio/virtio-pstore.c new file +26 -0
+static ssize_t virtio_pstore_do_write(VirtIOPstore *s, struct iovec *out_sg,
+                                      unsigned int out_num,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    unsigned short type;
+    int flags = O_WRONLY | O_CREAT;
+
+    /* we already consume the req */
+    iov_discard_front(&out_sg, &out_num, sizeof(*req));
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    type = le16_to_cpu(req->type);
+
+    if (type == VIRTIO_PSTORE_TYPE_DMESG) {
+        flags |= O_TRUNC;
+    } else if (type == VIRTIO_PSTORE_TYPE_CONSOLE) {
+        flags |= O_APPEND;
+    }
+
+    fd = open(path, flags, 0644);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
Quoted 8 lines

hw/virtio/virtio-pstore.c line 196

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+        return -1;
+    }
+    len = writev(fd, out_sg, out_num);
+    close(fd);
+
+    return len;

All this is blocking VM until host io completes.

Hmm.. I don't know about the internals of qemu. So does it make guest stop? If so, that's what I want to do for _DMESG. :) As it's called only on kernel oops I think it's admittable. But for _CONSOLE, it needs to do asynchronously. Maybe I can add a thread to do the work.

Quoted 77 lines
Quoted 77 lines
hw/virtio/virtio-pstore.c new file +75 -0
+}
+
+static ssize_t virtio_pstore_do_close(VirtIOPstore *s)
+{
+    if (s->dirp == NULL) {
+        return 0;
+    }
+
+    closedir(s->dirp);
+    s->dirp = NULL;
+
+    return 0;
+}
+
+static ssize_t virtio_pstore_do_erase(VirtIOPstore *s,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    return unlink(path);
+}
+
+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;
+        }
+
+        if (elem->out_num < 1 || elem->in_num < 1) {
+            error_report("request or response buffer is missing");
+            exit(1);
+        }
+
+        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_READ:
+            ret = virtio_pstore_do_read(s, elem->in_sg, elem->in_num, &res);
+            if (ret > 0) {
+                len = ret;
+                ret = 0;
+            }
+            break;
+        case VIRTIO_PSTORE_CMD_WRITE:
+            ret = virtio_pstore_do_write(s, elem->out_sg, elem->out_num, &req);
+            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;
+        default:
+            ret = -1;
+            break;
Quoted 8 lines

hw/virtio/virtio-pstore.c line 277

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+        }
+
+        res.ret  = ret;
+
+        iov_from_buf(elem->in_sg, elem->in_num, 0, &res, sizeof(res));
+        virtqueue_push(vq, elem, sizeof(res) + len);

this is wrong - len should be # of bytes written into guest memory.

???

All command except READ only write the virtio_pstore_ret so len is 0. For READ, virtio_pstore_do_read() returns the actual bytes it wrote (minus sizeof(res) of course), so I think it's fine, no?

Anyway, thanks for your review!

Thanks,
Namhyung

Quoted 10 lines

hw/virtio/virtio-pstore.c line 286

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +9 -0
+
+        virtio_notify(vdev, vq);
+        g_free(elem);
+
+        if (ret < 0) {
+            return;
+        }
+    }
+}

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

#11 lore raw

Re: [PATCH 6/7] qemu: Implement virtio-pstore device

Quoted 37 lines · Namhyung Kim
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Namhyung Kim
hw/virtio/virtio-pstore.c new file +26 -0
+static ssize_t virtio_pstore_do_write(VirtIOPstore *s, struct iovec *out_sg,
+                                      unsigned int out_num,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    unsigned short type;
+    int flags = O_WRONLY | O_CREAT;
+
+    /* we already consume the req */
+    iov_discard_front(&out_sg, &out_num, sizeof(*req));
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    type = le16_to_cpu(req->type);
+
+    if (type == VIRTIO_PSTORE_TYPE_DMESG) {
+        flags |= O_TRUNC;
+    } else if (type == VIRTIO_PSTORE_TYPE_CONSOLE) {
+        flags |= O_APPEND;
+    }
+
+    fd = open(path, flags, 0644);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
Quoted 8 lines

hw/virtio/virtio-pstore.c line 196

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+        return -1;
+    }
+    len = writev(fd, out_sg, out_num);
+    close(fd);
+
+    return len;

All this is blocking VM until host io completes.

Quoted 4 lines

Hmm.. I don't know about the internals of qemu. So does it make guest stop? If so, that's what I want to do for _DMESG. :) As it's called only on kernel oops I think it's admittable. But for _CONSOLE, it needs to do asynchronously. Maybe I can add a thread to do the work.

Please look at include/io/channel.h. QEMU is event-driven and tends to use asynchronous I/O instead of spawning threads. The include/io/ APIs allow you to do asynchronous I/O in the event loop.

Stefan

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

#12 lore raw

Re: [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Namhyung Kim
Quoted 37 lines · Namhyung Kim
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Namhyung Kim
hw/virtio/virtio-pstore.c new file +26 -0
+static ssize_t virtio_pstore_do_write(VirtIOPstore *s, struct iovec *out_sg,
+                                      unsigned int out_num,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    unsigned short type;
+    int flags = O_WRONLY | O_CREAT;
+
+    /* we already consume the req */
+    iov_discard_front(&out_sg, &out_num, sizeof(*req));
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    type = le16_to_cpu(req->type);
+
+    if (type == VIRTIO_PSTORE_TYPE_DMESG) {
+        flags |= O_TRUNC;
+    } else if (type == VIRTIO_PSTORE_TYPE_CONSOLE) {
+        flags |= O_APPEND;
+    }
+
+    fd = open(path, flags, 0644);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
Quoted 8 lines

hw/virtio/virtio-pstore.c line 196

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+        return -1;
+    }
+    len = writev(fd, out_sg, out_num);
+    close(fd);
+
+    return len;

All this is blocking VM until host io completes.

Quoted 4 lines

Hmm.. I don't know about the internals of qemu. So does it make guest stop? If so, that's what I want to do for _DMESG. :) As it's called only on kernel oops I think it's admittable. But for _CONSOLE, it needs to do asynchronously. Maybe I can add a thread to do the work.

Please look at include/io/channel.h. QEMU is event-driven and tends to use asynchronous I/O instead of spawning threads. The include/io/ APIs allow you to do asynchronous I/O in the event loop.

Stefan

#13 lore raw

Re: [Qemu-devel] [PATCH 6/7] qemu: Implement virtio-pstore device

Quoted 38 lines · Stefan Hajnoczi
Quoted 37 lines · Namhyung Kim
Quoted 35 lines · Michael S. Tsirkin
Quoted 22 lines · Namhyung Kim
hw/virtio/virtio-pstore.c new file +22 -0
+static ssize_t virtio_pstore_do_write(VirtIOPstore *s, struct iovec *out_sg,
+                                      unsigned int out_num,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    unsigned short type;
+    int flags = O_WRONLY | O_CREAT;
+
+    /* we already consume the req */
+    iov_discard_front(&out_sg, &out_num, sizeof(*req));
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    type = le16_to_cpu(req->type);
+
+    if (type == VIRTIO_PSTORE_TYPE_DMESG) {
+        flags |= O_TRUNC;
+    } else if (type == VIRTIO_PSTORE_TYPE_CONSOLE) {
+        flags |= O_APPEND;
+    }

hw/virtio/virtio-pstore.c line 196

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +10 -0
+
+    fd = open(path, flags, 0644);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
+        return -1;
+    }
+    len = writev(fd, out_sg, out_num);
+    close(fd);
+
+    return len;

All this is blocking VM until host io completes.

Quoted 8 lines
Quoted 4 lines

Hmm.. I don't know about the internals of qemu. So does it make guest stop? If so, that's what I want to do for _DMESG. :) As it's called only on kernel oops I think it's admittable. But for _CONSOLE, it needs to do asynchronously. Maybe I can add a thread to do the work.

Please look at include/io/channel.h. QEMU is event-driven and tends to use asynchronous I/O instead of spawning threads. The include/io/ APIs allow you to do asynchronous I/O in the event loop.

That is true, except for I/O to/from plain files - the QIOChannelFile impl doesn't do anything special (yet) to make that work correctly in non-blocking mode. Of course that could be fixed...

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#20 lore raw

Re: [Qemu-devel] [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Namhyung Kim
Quoted 222 lines · Namhyung Kim

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' and 'console' (boolean) properties.

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]>
---
 hw/virtio/Makefile.objs                        |   2 +-
 hw/virtio/virtio-pci.c                         |  54 +++
 hw/virtio/virtio-pci.h                         |  14 +
 hw/virtio/virtio-pstore.c                      | 477 +++++++++++++++++++++++++
 include/hw/pci/pci.h                           |   1 +
 include/hw/virtio/virtio-pstore.h              |  34 ++
 include/standard-headers/linux/virtio_ids.h    |   1 +
 include/standard-headers/linux/virtio_pstore.h |  80 +++++
 qdev-monitor.c                                 |   1 +
 9 files changed, 663 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 +54 -0
index f0677b7..d99a405 100644
@@ -2414,6 +2414,59 @@ static const TypeInfo virtio_host_pci_info = {
 };
 #endif
 
+/* virtio-pstore-pci */
+
+static void virtio_pstore_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
+{
+    VirtIOPstorePCI *vps = VIRTIO_PSTORE_PCI(vpci_dev);
+    DeviceState *vdev = DEVICE(&vps->vdev);
+    Error *err = NULL;
+
+    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
+    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
+    if (err) {
+        error_propagate(errp, err);
+        return;
+    }
+}
+
+static void virtio_pstore_pci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
+    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
+
+    k->realize = virtio_pstore_pci_realize;
+    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
+
+    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
+    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_PSTORE;
+    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
+    pcidev_k->class_id = PCI_CLASS_OTHERS;
+}
+
+static void virtio_pstore_pci_instance_init(Object *obj)
+{
+    VirtIOPstorePCI *dev = VIRTIO_PSTORE_PCI(obj);
+
+    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
+                                TYPE_VIRTIO_PSTORE);
+    object_property_add_alias(obj, "directory", OBJECT(&dev->vdev),
+                              "directory", &error_abort);
+    object_property_add_alias(obj, "bufsize", OBJECT(&dev->vdev),
+                              "bufsize", &error_abort);
+    object_property_add_alias(obj, "console", OBJECT(&dev->vdev),
+                              "console", &error_abort);
+}
+
+static const TypeInfo virtio_pstore_pci_info = {
+    .name          = TYPE_VIRTIO_PSTORE_PCI,
+    .parent        = TYPE_VIRTIO_PCI,
+    .instance_size = sizeof(VirtIOPstorePCI),
+    .instance_init = virtio_pstore_pci_instance_init,
+    .class_init    = virtio_pstore_pci_class_init,
+};
+
 /* virtio-pci-bus */
 
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
@@ -2483,6 +2536,7 @@ static void virtio_pci_register_types(void)
 #ifdef CONFIG_VHOST_SCSI
     type_register_static(&vhost_scsi_pci_info);
 #endif
+    type_register_static(&virtio_pstore_pci_info);
 }
 
 type_init(virtio_pci_register_types)
hw/virtio/virtio-pci.h +14 -0
index e4548c2..b4c039f 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 */
 
@@ -311,6 +313,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 +48 -0
new file mode 100644
index 0000000..2ca7786
@@ -0,0 +1,477 @@
+/*
+ * Virtio Pstore Device
+ *
+ * Copyright (C) 2016  LG Electronics
+ *
+ * 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.
+ *
+ */
+
+#include <stdio.h>
+
+#include "qemu/osdep.h"
+#include "qemu/iov.h"
+#include "qemu-common.h"
+#include "qemu/cutils.h"
+#include "qemu/error-report.h"
+#include "sysemu/kvm.h"
+#include "qapi/visitor.h"
+#include "qapi-event.h"
+#include "trace.h"
+
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-bus.h"
+#include "hw/virtio/virtio-access.h"
+#include "hw/virtio/virtio-pstore.h"
+
+
+static void virtio_pstore_to_filename(VirtIOPstore *s, char *buf, size_t sz,
+                                      struct virtio_pstore_req *req)
+{
+    const char *basename;
+    unsigned long long id = 0;
+    unsigned int flags = le32_to_cpu(req->flags);
+
+    switch (le16_to_cpu(req->type)) {
+    case VIRTIO_PSTORE_TYPE_DMESG:
+        basename = "dmesg";
+        id = s->id++;
+        break;
+    case VIRTIO_PSTORE_TYPE_CONSOLE:
+        basename = "console";
+        if (s->console_id) {
+            id = s->console_id;
+        } else {

hw/virtio/virtio-pstore.c line 58

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +10 -0
+            id = s->console_id = s->id++;
+        }
+        break;
+    default:
+        basename = "unknown";
+        break;
+    }
+
+    snprintf(buf, sz, "%s/%s-%llu%s", s->directory, basename, id,
+             flags & VIRTIO_PSTORE_FL_COMPRESSED ? ".enc.z" : "");

Please use g_strdup_printf() instead of splattering into a pre-allocated buffer than may or may not be large enough.

Quoted 32 lines
hw/virtio/virtio-pstore.c new file +32 -0
+}
+
+static void virtio_pstore_from_filename(VirtIOPstore *s, char *name,
+                                        char *buf, size_t sz,
+                                        struct virtio_pstore_fileinfo *info)
+{
+    snprintf(buf, sz, "%s/%s", s->directory, name);
+
+    if (g_str_has_prefix(name, "dmesg-")) {
+        info->type = VIRTIO_PSTORE_TYPE_DMESG;
+        name += strlen("dmesg-");
+    } else if (g_str_has_prefix(name, "console-")) {
+        info->type = VIRTIO_PSTORE_TYPE_CONSOLE;
+        name += strlen("console-");
+    } else if (g_str_has_prefix(name, "unknown-")) {
+        info->type = VIRTIO_PSTORE_TYPE_UNKNOWN;
+        name += strlen("unknown-");
+    }
+
+    qemu_strtoull(name, NULL, 0, &info->id);
+
+    info->flags = 0;
+    if (g_str_has_suffix(name, ".enc.z")) {
+        info->flags |= VIRTIO_PSTORE_FL_COMPRESSED;
+    }
+}
+
+static ssize_t virtio_pstore_do_open(VirtIOPstore *s)
+{
+    s->dirp = opendir(s->directory);
+    if (s->dirp == NULL) {
+        return -1;

hw/virtio/virtio-pstore.c line 100

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +10 -0
+    }
+
+    return 0;
+}
+
+static ssize_t virtio_pstore_do_read(VirtIOPstore *s, struct iovec *in_sg,
+                                     unsigned int in_num,
+                                     struct virtio_pstore_res *res)
+{
+    char path[PATH_MAX];

Don't declare PATH_MAX sized variables

hw/virtio/virtio-pstore.c line 106

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+    int fd;
+    ssize_t len;
+    struct stat stbuf;
+    struct dirent *dent;
+    int sg_num = in_num;
+    struct iovec sg[sg_num];

'sg_num' is initialized from 'in_num' which comes from the guest, and I'm not seeing anything which is bounds-checking the 'in_num' value. So you've possibly got a security flaw here I think, if the guest can cause QEMU to allocate arbitrary stack memory & thus overflow by setting arbitrarily large in_num.

Quoted 8 lines
hw/virtio/virtio-pstore.c new file +8 -0
+    struct virtio_pstore_fileinfo info;
+    size_t offset = sizeof(*res) + sizeof(info);
+
+    if (s->dirp == NULL) {
+        return -1;
+    }
+
+    dent = readdir(s->dirp);

hw/virtio/virtio-pstore.c line 124

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +10 -0
+    while (dent) {
+        if (dent->d_name[0] != '.') {
+            break;
+        }
+        dent = readdir(s->dirp);
+    }
+
+    if (dent == NULL) {
+        return 0;
+    }

So this seems to just be picking the first filename reported by readdir that isn't starting with '.'. Surely this can't the right logic when your corresponding do_write method can pick several different filenames, its potluck which do_read will give back.

Quoted 51 lines
hw/virtio/virtio-pstore.c new file +51 -0
+
+    /* skip res and fileinfo */
+    sg_num = iov_copy(sg, sg_num, in_sg, in_num, offset,
+                      iov_size(in_sg, in_num) - offset);
+
+    virtio_pstore_from_filename(s, dent->d_name, path, sizeof(path), &info);
+    fd = open(path, O_RDONLY);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
+        return -1;
+    }
+
+    if (fstat(fd, &stbuf) < 0) {
+        len = -1;
+        goto out;
+    }
+
+    len = readv(fd, sg, sg_num);
+    if (len < 0) {
+        if (errno == EAGAIN) {
+            len = 0;
+        }
+        goto out;
+    }
+
+    info.id        = cpu_to_le64(info.id);
+    info.type      = cpu_to_le16(info.type);
+    info.flags     = cpu_to_le32(info.flags);
+    info.len       = cpu_to_le32(len);
+    info.time_sec  = cpu_to_le64(stbuf.st_ctim.tv_sec);
+    info.time_nsec = cpu_to_le32(stbuf.st_ctim.tv_nsec);
+
+    iov_from_buf(in_sg, in_num, sizeof(*res), &info, sizeof(info));
+    len += sizeof(info);
+
+ out:
+    close(fd);
+    return len;
+}
+
+static ssize_t virtio_pstore_do_write(VirtIOPstore *s, struct iovec *out_sg,
+                                      unsigned int out_num,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    unsigned short type;
+    int flags = O_WRONLY | O_CREAT;
+
+    /* we already consume the req */

hw/virtio/virtio-pstore.c line 185

Quoted 10 lines
hw/virtio/virtio-pstore.c new file +10 -0
+    iov_discard_front(&out_sg, &out_num, sizeof(*req));
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    type = le16_to_cpu(req->type);
+
+    if (type == VIRTIO_PSTORE_TYPE_DMESG) {
+        flags |= O_TRUNC;
+    } else if (type == VIRTIO_PSTORE_TYPE_CONSOLE) {
+        flags |= O_APPEND;

Using O_APPEND will cause the file to grow without bound on the host, which is highly undesirable, aka a security flaw.

hw/virtio/virtio-pstore.c line 197

Quoted 12 lines
hw/virtio/virtio-pstore.c new file +12 -0
+    }
+
+    fd = open(path, flags, 0644);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
+        return -1;
+    }
+    len = writev(fd, out_sg, out_num);
+    close(fd);
+
+    return len;
+}

Regards,
Daniel

Signature · 8 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#18 lore raw

Re: [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Namhyung Kim
Quoted 5 lines · On Thu, 28 Jul 2016 14:39:53 +0900 Namhyung Kim <[email protected]> wrote:

Well, I dont' know. As you know, the kernel oops dump is already sent to serial device but it's rather slow. As I wrote in the cover letter, enabling ftrace_dump_on_oops makes it even worse.. Also pstore saves the (compressed) binary data so I thought it'd be better to have a dedicated IO channel.

BTW, I agree with this. It is better to have a quick way to grab the ftrace buffers when a crash happens, as serial is excruciatingly slow. Although, currently I still use kexec/kdump, but as Namhyung said, it depends on crash being up to date. I tend to be sending in updates every time I have to use it.

-- Steve

#19 lore raw

Re: [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Namhyung Kim
Quoted 5 lines · On Thu, 28 Jul 2016 14:39:53 +0900 Namhyung Kim <[email protected]> wrote:

Well, I dont' know. As you know, the kernel oops dump is already sent to serial device but it's rather slow. As I wrote in the cover letter, enabling ftrace_dump_on_oops makes it even worse.. Also pstore saves the (compressed) binary data so I thought it'd be better to have a dedicated IO channel.

BTW, I agree with this. It is better to have a quick way to grab the ftrace buffers when a crash happens, as serial is excruciatingly slow. Although, currently I still use kexec/kdump, but as Namhyung said, it depends on crash being up to date. I tend to be sending in updates every time I have to use it.

-- Steve

#14 lore raw

Re: [Qemu-devel] [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Daniel P. Berrange

Hello,

Quoted 44 lines · Daniel P. Berrange
Quoted 43 lines · Stefan Hajnoczi
Quoted 41 lines · Namhyung Kim
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Namhyung Kim
hw/virtio/virtio-pstore.c new file +26 -0
+static ssize_t virtio_pstore_do_write(VirtIOPstore *s, struct iovec *out_sg,
+                                      unsigned int out_num,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    unsigned short type;
+    int flags = O_WRONLY | O_CREAT;
+
+    /* we already consume the req */
+    iov_discard_front(&out_sg, &out_num, sizeof(*req));
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    type = le16_to_cpu(req->type);
+
+    if (type == VIRTIO_PSTORE_TYPE_DMESG) {
+        flags |= O_TRUNC;
+    } else if (type == VIRTIO_PSTORE_TYPE_CONSOLE) {
+        flags |= O_APPEND;
+    }
+
+    fd = open(path, flags, 0644);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
Quoted 8 lines

hw/virtio/virtio-pstore.c line 196

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+        return -1;
+    }
+    len = writev(fd, out_sg, out_num);
+    close(fd);
+
+    return len;

All this is blocking VM until host io completes.

Hmm.. I don't know about the internals of qemu. So does it make guest stop? If so, that's what I want to do for _DMESG. :) As it's called only on kernel oops I think it's admittable. But for _CONSOLE, it needs to do asynchronously. Maybe I can add a thread to do the work.

Quoted 7 lines
Quoted 3 lines

Please look at include/io/channel.h. QEMU is event-driven and tends to use asynchronous I/O instead of spawning threads. The include/io/ APIs allow you to do asynchronous I/O in the event loop.

That is true, except for I/O to/from plain files - the QIOChannelFile impl doesn't do anything special (yet) to make that work correctly in non-blocking mode. Of course that could be fixed...

Yep, I don't know how I can use the QIOChannelFile for async IO. AFAICS it's just a wrapper for normal readv/writev. Who is responsible to do these IO when I use the IO channel API? Also does it guarantee that all IOs are processed in order?

Thanks,
Namhyung

#21 lore raw

Re: [Qemu-devel] [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Daniel P. Berrange
Quoted 34 lines · Daniel P. Berrange
Quoted 33 lines · Namhyung Kim

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' and 'console' (boolean) properties.

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]>

---

[SNIP]

Quoted 22 lines
Quoted 22 lines
hw/virtio/virtio-pstore.c new file +22 -0
+static void virtio_pstore_to_filename(VirtIOPstore *s, char *buf, size_t sz,
+                                      struct virtio_pstore_req *req)
+{
+    const char *basename;
+    unsigned long long id = 0;
+    unsigned int flags = le32_to_cpu(req->flags);
+
+    switch (le16_to_cpu(req->type)) {
+    case VIRTIO_PSTORE_TYPE_DMESG:
+        basename = "dmesg";
+        id = s->id++;
+        break;
+    case VIRTIO_PSTORE_TYPE_CONSOLE:
+        basename = "console";
+        if (s->console_id) {
+            id = s->console_id;
+        } else {
+            id = s->console_id = s->id++;
+        }
+        break;
+    default:
+        basename = "unknown";
Quoted 8 lines

hw/virtio/virtio-pstore.c line 58

Quoted 5 lines
hw/virtio/virtio-pstore.c new file +5 -0
+        break;
+    }
+
+    snprintf(buf, sz, "%s/%s-%llu%s", s->directory, basename, id,
+             flags & VIRTIO_PSTORE_FL_COMPRESSED ? ".enc.z" : "");

Please use g_strdup_printf() instead of splattering into a pre-allocated buffer than may or may not be large enough.

OK.

Quoted 37 lines
Quoted 37 lines
hw/virtio/virtio-pstore.c new file +36 -0
+}
+
+static void virtio_pstore_from_filename(VirtIOPstore *s, char *name,
+                                        char *buf, size_t sz,
+                                        struct virtio_pstore_fileinfo *info)
+{
+    snprintf(buf, sz, "%s/%s", s->directory, name);
+
+    if (g_str_has_prefix(name, "dmesg-")) {
+        info->type = VIRTIO_PSTORE_TYPE_DMESG;
+        name += strlen("dmesg-");
+    } else if (g_str_has_prefix(name, "console-")) {
+        info->type = VIRTIO_PSTORE_TYPE_CONSOLE;
+        name += strlen("console-");
+    } else if (g_str_has_prefix(name, "unknown-")) {
+        info->type = VIRTIO_PSTORE_TYPE_UNKNOWN;
+        name += strlen("unknown-");
+    }
+
+    qemu_strtoull(name, NULL, 0, &info->id);
+
+    info->flags = 0;
+    if (g_str_has_suffix(name, ".enc.z")) {
+        info->flags |= VIRTIO_PSTORE_FL_COMPRESSED;
+    }
+}
+
+static ssize_t virtio_pstore_do_open(VirtIOPstore *s)
+{
+    s->dirp = opendir(s->directory);
+    if (s->dirp == NULL) {
+        return -1;
+    }
+
+    return 0;
+}
Quoted 8 lines

hw/virtio/virtio-pstore.c line 100

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+
+static ssize_t virtio_pstore_do_read(VirtIOPstore *s, struct iovec *in_sg,
+                                     unsigned int in_num,
+                                     struct virtio_pstore_res *res)
+{
+    char path[PATH_MAX];

Don't declare PATH_MAX sized variables

Will change to use g_strdup_printf() as you said.

Quoted 8 lines

hw/virtio/virtio-pstore.c line 106

Quoted 7 lines
hw/virtio/virtio-pstore.c new file +6 -0
+    int fd;
+    ssize_t len;
+    struct stat stbuf;
+    struct dirent *dent;
+    int sg_num = in_num;
+    struct iovec sg[sg_num];
Quoted 5 lines

'sg_num' is initialized from 'in_num' which comes from the guest, and I'm not seeing anything which is bounds-checking the 'in_num' value. So you've possibly got a security flaw here I think, if the guest can cause QEMU to allocate arbitrary stack memory & thus overflow by setting arbitrarily large in_num.

Will add a bound check then.

Quoted 16 lines
Quoted 16 lines
hw/virtio/virtio-pstore.c new file +15 -0
+    struct virtio_pstore_fileinfo info;
+    size_t offset = sizeof(*res) + sizeof(info);
+
+    if (s->dirp == NULL) {
+        return -1;
+    }
+
+    dent = readdir(s->dirp);
+    while (dent) {
+        if (dent->d_name[0] != '.') {
+            break;
+        }
+        dent = readdir(s->dirp);
+    }
+
Quoted 8 lines

hw/virtio/virtio-pstore.c line 124

Quoted 3 lines
hw/virtio/virtio-pstore.c new file +3 -0
+    if (dent == NULL) {
+        return 0;
+    }

So this seems to just be picking the first filename reported by readdir that isn't starting with '.'. Surely this can't the right logic when your corresponding do_write method can pick several different filenames, its potluck which do_read will give back.

Do you mean that it'd be better to check a list of known filenames and fail if not?

Quoted 57 lines
Quoted 57 lines
hw/virtio/virtio-pstore.c new file +56 -0
+
+    /* skip res and fileinfo */
+    sg_num = iov_copy(sg, sg_num, in_sg, in_num, offset,
+                      iov_size(in_sg, in_num) - offset);
+
+    virtio_pstore_from_filename(s, dent->d_name, path, sizeof(path), &info);
+    fd = open(path, O_RDONLY);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
+        return -1;
+    }
+
+    if (fstat(fd, &stbuf) < 0) {
+        len = -1;
+        goto out;
+    }
+
+    len = readv(fd, sg, sg_num);
+    if (len < 0) {
+        if (errno == EAGAIN) {
+            len = 0;
+        }
+        goto out;
+    }
+
+    info.id        = cpu_to_le64(info.id);
+    info.type      = cpu_to_le16(info.type);
+    info.flags     = cpu_to_le32(info.flags);
+    info.len       = cpu_to_le32(len);
+    info.time_sec  = cpu_to_le64(stbuf.st_ctim.tv_sec);
+    info.time_nsec = cpu_to_le32(stbuf.st_ctim.tv_nsec);
+
+    iov_from_buf(in_sg, in_num, sizeof(*res), &info, sizeof(info));
+    len += sizeof(info);
+
+ out:
+    close(fd);
+    return len;
+}
+
+static ssize_t virtio_pstore_do_write(VirtIOPstore *s, struct iovec *out_sg,
+                                      unsigned int out_num,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    unsigned short type;
+    int flags = O_WRONLY | O_CREAT;
+
+    /* we already consume the req */
+    iov_discard_front(&out_sg, &out_num, sizeof(*req));
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    type = le16_to_cpu(req->type);
Quoted 8 lines

hw/virtio/virtio-pstore.c line 185

Quoted 5 lines
hw/virtio/virtio-pstore.c new file +5 -0
+
+    if (type == VIRTIO_PSTORE_TYPE_DMESG) {
+        flags |= O_TRUNC;
+    } else if (type == VIRTIO_PSTORE_TYPE_CONSOLE) {
+        flags |= O_APPEND;

Using O_APPEND will cause the file to grow without bound on the host, which is highly undesirable, aka a security flaw.

Right. The plan is to add size checking and to split if it exceeds some limit. And we can keep some number of recent files only.

Thanks,
Namhyung

Quoted 22 lines

hw/virtio/virtio-pstore.c line 197

Quoted 13 lines
hw/virtio/virtio-pstore.c new file +12 -0
+    }
+
+    fd = open(path, flags, 0644);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
+        return -1;
+    }
+    len = writev(fd, out_sg, out_num);
+    close(fd);
+
+    return len;
+}

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#15 lore raw

Re: [Qemu-devel] [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Namhyung Kim
Quoted 51 lines · Namhyung Kim

Hello,

Quoted 48 lines · Daniel P. Berrange
Quoted 46 lines · Stefan Hajnoczi
Quoted 37 lines · Namhyung Kim
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Namhyung Kim
hw/virtio/virtio-pstore.c new file +26 -0
+static ssize_t virtio_pstore_do_write(VirtIOPstore *s, struct iovec *out_sg,
+                                      unsigned int out_num,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    unsigned short type;
+    int flags = O_WRONLY | O_CREAT;
+
+    /* we already consume the req */
+    iov_discard_front(&out_sg, &out_num, sizeof(*req));
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    type = le16_to_cpu(req->type);
+
+    if (type == VIRTIO_PSTORE_TYPE_DMESG) {
+        flags |= O_TRUNC;
+    } else if (type == VIRTIO_PSTORE_TYPE_CONSOLE) {
+        flags |= O_APPEND;
+    }
+
+    fd = open(path, flags, 0644);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
Quoted 8 lines

hw/virtio/virtio-pstore.c line 196

Quoted 6 lines
hw/virtio/virtio-pstore.c new file +6 -0
+        return -1;
+    }
+    len = writev(fd, out_sg, out_num);
+    close(fd);
+
+    return len;

All this is blocking VM until host io completes.

Quoted 4 lines

Hmm.. I don't know about the internals of qemu. So does it make guest stop? If so, that's what I want to do for _DMESG. :) As it's called only on kernel oops I think it's admittable. But for _CONSOLE, it needs to do asynchronously. Maybe I can add a thread to do the work.

Please look at include/io/channel.h. QEMU is event-driven and tends to use asynchronous I/O instead of spawning threads. The include/io/ APIs allow you to do asynchronous I/O in the event loop.

Quoted 8 lines
Quoted 3 lines

That is true, except for I/O to/from plain files - the QIOChannelFile impl doesn't do anything special (yet) to make that work correctly in non-blocking mode. Of course that could be fixed...

Yep, I don't know how I can use the QIOChannelFile for async IO. AFAICS it's just a wrapper for normal readv/writev. Who is responsible to do these IO when I use the IO channel API? Also does it guarantee that all IOs are processed in order?

I'd suggest just using QIOChannelFile regardless - we need to fix the blocking behaviour already for sake of the qemu chardev code, and your code just adds more pressure to fix it. I/O will be done in the order in which you make the calls, as with regular POSIX I/O funcs you're currently using.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#22 lore raw

Re: [Qemu-devel] [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Namhyung Kim
Quoted 17 lines · Namhyung Kim
Quoted 16 lines · Daniel P. Berrange
Quoted 16 lines
hw/virtio/virtio-pstore.c new file +16 -0
+static void virtio_pstore_from_filename(VirtIOPstore *s, char *name,
+                                        char *buf, size_t sz,
+                                        struct virtio_pstore_fileinfo *info)
+{
+    snprintf(buf, sz, "%s/%s", s->directory, name);
+
+    if (g_str_has_prefix(name, "dmesg-")) {
+        info->type = VIRTIO_PSTORE_TYPE_DMESG;
+        name += strlen("dmesg-");
+    } else if (g_str_has_prefix(name, "console-")) {
+        info->type = VIRTIO_PSTORE_TYPE_CONSOLE;
+        name += strlen("console-");
+    } else if (g_str_has_prefix(name, "unknown-")) {
+        info->type = VIRTIO_PSTORE_TYPE_UNKNOWN;
+        name += strlen("unknown-");
+    }

[snip]

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
hw/virtio/virtio-pstore.c new file +15 -0
+    struct virtio_pstore_fileinfo info;
+    size_t offset = sizeof(*res) + sizeof(info);
+
+    if (s->dirp == NULL) {
+        return -1;
+    }
+
+    dent = readdir(s->dirp);
+    while (dent) {
+        if (dent->d_name[0] != '.') {
+            break;
+        }
+        dent = readdir(s->dirp);
+    }
+
Quoted 8 lines

hw/virtio/virtio-pstore.c line 124

Quoted 3 lines
hw/virtio/virtio-pstore.c new file +3 -0
+    if (dent == NULL) {
+        return 0;
+    }

So this seems to just be picking the first filename reported by readdir that isn't starting with '.'. Surely this can't the right logic when your corresponding do_write method can pick several different filenames, its potluck which do_read will give back.

Quoted 2 lines

Do you mean that it'd be better to check a list of known filenames and fail if not?

No, I mean that you have several different VIRTIO_PSTORE_TYPE_nnn and use a different file for each constant. When reading this directory though you're not looking for the file corresponding to any given VIRTIO_PSTORE_TYPE_nnn - you're simply reading whichever file is found first. So you might have just read a TYPE_CONSOLE or have read a TYPE_DMESG - it surely doesn't make sense to randonly read either TYPE_CONSOLE or TYPE_DMESG based on whatever order readdir() lists the files.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#16 lore raw

Re: [Qemu-devel] [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Daniel P. Berrange

Hi Daniel,

Quoted 61 lines · Daniel P. Berrange
Quoted 51 lines · Namhyung Kim

Hello,

Quoted 48 lines · Daniel P. Berrange
Quoted 46 lines · Stefan Hajnoczi
Quoted 37 lines · Namhyung Kim
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Namhyung Kim
(quoted diff) +26 -0
+static ssize_t virtio_pstore_do_write(VirtIOPstore *s, struct iovec *out_sg,
+                                      unsigned int out_num,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    unsigned short type;
+    int flags = O_WRONLY | O_CREAT;
+
+    /* we already consume the req */
+    iov_discard_front(&out_sg, &out_num, sizeof(*req));
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    type = le16_to_cpu(req->type);
+
+    if (type == VIRTIO_PSTORE_TYPE_DMESG) {
+        flags |= O_TRUNC;
+    } else if (type == VIRTIO_PSTORE_TYPE_CONSOLE) {
+        flags |= O_APPEND;
+    }
+
+    fd = open(path, flags, 0644);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+        return -1;
+    }
+    len = writev(fd, out_sg, out_num);
+    close(fd);
+
+    return len;

All this is blocking VM until host io completes.

Quoted 4 lines

Hmm.. I don't know about the internals of qemu. So does it make guest stop? If so, that's what I want to do for _DMESG. :) As it's called only on kernel oops I think it's admittable. But for _CONSOLE, it needs to do asynchronously. Maybe I can add a thread to do the work.

Please look at include/io/channel.h. QEMU is event-driven and tends to use asynchronous I/O instead of spawning threads. The include/io/ APIs allow you to do asynchronous I/O in the event loop.

Quoted 8 lines
Quoted 3 lines

That is true, except for I/O to/from plain files - the QIOChannelFile impl doesn't do anything special (yet) to make that work correctly in non-blocking mode. Of course that could be fixed...

Yep, I don't know how I can use the QIOChannelFile for async IO. AFAICS it's just a wrapper for normal readv/writev. Who is responsible to do these IO when I use the IO channel API? Also does it guarantee that all IOs are processed in order?

Quoted 5 lines

I'd suggest just using QIOChannelFile regardless - we need to fix the blocking behaviour already for sake of the qemu chardev code, and your code just adds more pressure to fix it. I/O will be done in the order in which you make the calls, as with regular POSIX I/O funcs you're currently using.

Thanks for the info.

So can I simply replace regular IO funcs to QIOChannel API like below?

  ioc = qio_channel_file_new_path(path, flags, 0644, &err);
  qio_channel_set_blocking(ioc, false, &err);
  len = qio_channel_writev(ioc, out_sg, out_num, &err);
  qio_channel_file_close(ioc, &err);
  return len;

Or do I need to call qio_channel_add_watch() or something?

Thanks,
Namhyung

#17 lore raw

Re: [Qemu-devel] [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Daniel P. Berrange

Hi Daniel,

Quoted 61 lines · Daniel P. Berrange
Quoted 51 lines · Namhyung Kim

Hello,

Quoted 48 lines · Daniel P. Berrange
Quoted 46 lines · Stefan Hajnoczi
Quoted 37 lines · Namhyung Kim
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Namhyung Kim
(quoted diff) +26 -0
+static ssize_t virtio_pstore_do_write(VirtIOPstore *s, struct iovec *out_sg,
+                                      unsigned int out_num,
+                                      struct virtio_pstore_req *req)
+{
+    char path[PATH_MAX];
+    int fd;
+    ssize_t len;
+    unsigned short type;
+    int flags = O_WRONLY | O_CREAT;
+
+    /* we already consume the req */
+    iov_discard_front(&out_sg, &out_num, sizeof(*req));
+
+    virtio_pstore_to_filename(s, path, sizeof(path), req);
+
+    type = le16_to_cpu(req->type);
+
+    if (type == VIRTIO_PSTORE_TYPE_DMESG) {
+        flags |= O_TRUNC;
+    } else if (type == VIRTIO_PSTORE_TYPE_CONSOLE) {
+        flags |= O_APPEND;
+    }
+
+    fd = open(path, flags, 0644);
+    if (fd < 0) {
+        error_report("cannot open %s", path);
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+        return -1;
+    }
+    len = writev(fd, out_sg, out_num);
+    close(fd);
+
+    return len;

All this is blocking VM until host io completes.

Quoted 4 lines

Hmm.. I don't know about the internals of qemu. So does it make guest stop? If so, that's what I want to do for _DMESG. :) As it's called only on kernel oops I think it's admittable. But for _CONSOLE, it needs to do asynchronously. Maybe I can add a thread to do the work.

Please look at include/io/channel.h. QEMU is event-driven and tends to use asynchronous I/O instead of spawning threads. The include/io/ APIs allow you to do asynchronous I/O in the event loop.

Quoted 8 lines
Quoted 3 lines

That is true, except for I/O to/from plain files - the QIOChannelFile impl doesn't do anything special (yet) to make that work correctly in non-blocking mode. Of course that could be fixed...

Yep, I don't know how I can use the QIOChannelFile for async IO. AFAICS it's just a wrapper for normal readv/writev. Who is responsible to do these IO when I use the IO channel API? Also does it guarantee that all IOs are processed in order?

Quoted 5 lines

I'd suggest just using QIOChannelFile regardless - we need to fix the blocking behaviour already for sake of the qemu chardev code, and your code just adds more pressure to fix it. I/O will be done in the order in which you make the calls, as with regular POSIX I/O funcs you're currently using.

Thanks for the info.

So can I simply replace regular IO funcs to QIOChannel API like below?

  ioc = qio_channel_file_new_path(path, flags, 0644, &err);
  qio_channel_set_blocking(ioc, false, &err);
  len = qio_channel_writev(ioc, out_sg, out_num, &err);
  qio_channel_file_close(ioc, &err);
  return len;

Or do I need to call qio_channel_add_watch() or something?

Thanks,
Namhyung

#23 lore raw

Re: [Qemu-devel] [PATCH 6/7] qemu: Implement virtio-pstore device

In reply to Daniel P. Berrange
Quoted 48 lines · Daniel P. Berrange
Quoted 17 lines · Namhyung Kim
Quoted 16 lines · Daniel P. Berrange
Quoted 16 lines
hw/virtio/virtio-pstore.c new file +16 -0
+static void virtio_pstore_from_filename(VirtIOPstore *s, char *name,
+                                        char *buf, size_t sz,
+                                        struct virtio_pstore_fileinfo *info)
+{
+    snprintf(buf, sz, "%s/%s", s->directory, name);
+
+    if (g_str_has_prefix(name, "dmesg-")) {
+        info->type = VIRTIO_PSTORE_TYPE_DMESG;
+        name += strlen("dmesg-");
+    } else if (g_str_has_prefix(name, "console-")) {
+        info->type = VIRTIO_PSTORE_TYPE_CONSOLE;
+        name += strlen("console-");
+    } else if (g_str_has_prefix(name, "unknown-")) {
+        info->type = VIRTIO_PSTORE_TYPE_UNKNOWN;
+        name += strlen("unknown-");
+    }

[snip]

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
hw/virtio/virtio-pstore.c new file +15 -0
+    struct virtio_pstore_fileinfo info;
+    size_t offset = sizeof(*res) + sizeof(info);
+
+    if (s->dirp == NULL) {
+        return -1;
+    }
+
+    dent = readdir(s->dirp);
+    while (dent) {
+        if (dent->d_name[0] != '.') {
+            break;
+        }
+        dent = readdir(s->dirp);
+    }
+
Quoted 8 lines

hw/virtio/virtio-pstore.c line 124

Quoted 3 lines
hw/virtio/virtio-pstore.c new file +3 -0
+    if (dent == NULL) {
+        return 0;
+    }

So this seems to just be picking the first filename reported by readdir that isn't starting with '.'. Surely this can't the right logic when your corresponding do_write method can pick several different filenames, its potluck which do_read will give back.

Quoted 2 lines

Do you mean that it'd be better to check a list of known filenames and fail if not?

Quoted 8 lines

No, I mean that you have several different VIRTIO_PSTORE_TYPE_nnn and use a different file for each constant. When reading this directory though you're not looking for the file corresponding to any given VIRTIO_PSTORE_TYPE_nnn - you're simply reading whichever file is found first. So you might have just read a TYPE_CONSOLE or have read a TYPE_DMESG - it surely doesn't make sense to randonly read either TYPE_CONSOLE or TYPE_DMESG based on whatever order readdir() lists the files.

In fact, each VIRTIO_PSTORE_TYPE_xxx can have more than one files and I don't care about the ordering between them. They will be fed to the pstore filesystem on the guest.

But I also think it'd be better to scan files of known types only rather than read out whatever readdir() gives.

Thanks,
Namhyung