Skip to contentSkip to conversation

KVM "fake DAX" device flushing

RFC In review Pankaj Gupta 5 participants 34 messages Oct 12, 2017 – Oct 20, 2017

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

cover KVM "fake DAX" device flushing

We are sharing the prototype version of 'fake DAX' flushing interface for the initial feedback. This is still work in progress and not yet ready for merging.

Prototype right now just implements basic functionality without advanced features with two major parts:

- Qemu virtio-pmem device
It exposes a persistent memory range to KVM guest which at host side is file backed memory and works as persistent memory device. In addition to this it provides a virtio flushing interface for KVM guest to do a Qemu side sync for guest DAX persistent memory range.

- Guest virtio-pmem driver
Reads persistent memory range from paravirt device and reserves system memory map. It also allocates a block device corresponding to the pmem range which is accessed by DAX capable file systems. (file system support is still pending).

We shared the project idea for 'fake DAX' flushing interface here [1]. Based on suggestions here [2], we implemented guest 'virtio-pmem' driver and Qemu paravirt device.

[1] https://www.spinics.net/lists/kvm/msg149761.html
[2] https://www.spinics.net/lists/kvm/msg153095.html

Work yet to be done:

- Separate out the common code used by ACPI pmem interface and reuse it.

- In pmem device memmap allocation and working. There is some parallel work going on upstream related to 'memory_hotplug restructuring' [3] and also hitting a memory section alignment issue [4].

[3] https://lwn.net/Articles/712099/
[4] https://www.mail-archive.com/[email protected]/msg02978.html

- Provide DAX capable file-system(ext4 & XFS) support.
- Qemu device flush functionality trigger with guest fsync on file.
- Qemu live migration work when host page cache is used.
- Multiple virtio-pmem disks support.
- Prepare virtio spec after we get feedback on current approach.

 drivers/nvdimm/pfn.h             |   14 -
 drivers/nvdimm/pfn_devs.c        |   20 --
 drivers/nvdimm/pmem.c            |   40 ----
 drivers/nvdimm/pmem.h            |    5 
 drivers/virtio/Kconfig           |   10 +
 drivers/virtio/Makefile          |    1 
 drivers/virtio/virtio_pmem.c     |  322 +++++++++++++++++++++++++++++++++++++++
 include/linux/memremap.h         |   23 ++
 include/linux/pfn.h              |   15 +
 include/linux/pmem_common.h      |   52 ++++++
 include/uapi/linux/virtio_pmem.h |   55 ++++++
 11 files changed, 479 insertions(+), 78 deletions(-)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

RFC 1/2 pmem: Move reusable code to base header files

This patch moves common code to base header files so that it can be used for both ACPI pmem and VIRTIO pmem drivers. More common code needs to be moved out in future based on functionality required for virtio_pmem driver and coupling of code with existing ACPI pmem driver.

---
 drivers/nvdimm/pfn.h        | 14 ------------
 drivers/nvdimm/pfn_devs.c   | 20 -----------------
 drivers/nvdimm/pmem.c       | 40 ----------------------------------
 drivers/nvdimm/pmem.h       |  5 +----
 include/linux/memremap.h    | 23 ++++++++++++++++++++
 include/linux/pfn.h         | 15 +++++++++++++
 include/linux/pmem_common.h | 52 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 91 insertions(+), 78 deletions(-)
 create mode 100644 include/linux/pmem_common.h
drivers/nvdimm/pfn.h +0 -14
index dde9853453d3..1a853f651faf 100644
@@ -40,18 +40,4 @@ struct nd_pfn_sb {
 	__le64 checksum;
 };
 
-#ifdef CONFIG_SPARSEMEM
-#define PFN_SECTION_ALIGN_DOWN(x) SECTION_ALIGN_DOWN(x)
-#define PFN_SECTION_ALIGN_UP(x) SECTION_ALIGN_UP(x)
-#else
-/*
- * In this case ZONE_DEVICE=n and we will disable 'pfn' device support,
- * but we still want pmem to compile.
- */
-#define PFN_SECTION_ALIGN_DOWN(x) (x)
-#define PFN_SECTION_ALIGN_UP(x) (x)
-#endif
-
-#define PHYS_SECTION_ALIGN_DOWN(x) PFN_PHYS(PFN_SECTION_ALIGN_DOWN(PHYS_PFN(x)))
-#define PHYS_SECTION_ALIGN_UP(x) PFN_PHYS(PFN_SECTION_ALIGN_UP(PHYS_PFN(x)))
 #endif /* __NVDIMM_PFN_H */
drivers/nvdimm/pfn_devs.c +0 -20
index 9576c444f0ab..52d6923e92fc 100644
@@ -513,26 +513,6 @@ int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
 }
 EXPORT_SYMBOL(nd_pfn_probe);
 
-/*
- * We hotplug memory at section granularity, pad the reserved area from
- * the previous section base to the namespace base address.
- */
-static unsigned long init_altmap_base(resource_size_t base)
-{
-	unsigned long base_pfn = PHYS_PFN(base);
-
-	return PFN_SECTION_ALIGN_DOWN(base_pfn);
-}
-
-static unsigned long init_altmap_reserve(resource_size_t base)
-{
-	unsigned long reserve = PHYS_PFN(SZ_8K);
-	unsigned long base_pfn = PHYS_PFN(base);
-
-	reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn);
-	return reserve;
-}
-
 static struct vmem_altmap *__nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
 		struct resource *res, struct vmem_altmap *altmap)
 {
drivers/nvdimm/pmem.c +0 -40
index 39dfd7affa31..5075131b715b 100644
@@ -77,46 +77,6 @@ static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
 	return rc;
 }
 
-static void write_pmem(void *pmem_addr, struct page *page,
-		unsigned int off, unsigned int len)
-{
-	unsigned int chunk;
-	void *mem;
-
-	while (len) {
-		mem = kmap_atomic(page);
-		chunk = min_t(unsigned int, len, PAGE_SIZE);
-		memcpy_flushcache(pmem_addr, mem + off, chunk);
-		kunmap_atomic(mem);
-		len -= chunk;
-		off = 0;
-		page++;
-		pmem_addr += PAGE_SIZE;
-	}
-}
-
-static blk_status_t read_pmem(struct page *page, unsigned int off,
-		void *pmem_addr, unsigned int len)
-{
-	unsigned int chunk;
-	int rc;
-	void *mem;
-
-	while (len) {
-		mem = kmap_atomic(page);
-		chunk = min_t(unsigned int, len, PAGE_SIZE);
-		rc = memcpy_mcsafe(mem + off, pmem_addr, chunk);
-		kunmap_atomic(mem);
-		if (rc)
-			return BLK_STS_IOERR;
-		len -= chunk;
-		off = 0;
-		page++;
-		pmem_addr += PAGE_SIZE;
-	}
-	return BLK_STS_OK;
-}
-
 static blk_status_t pmem_do_bvec(struct pmem_device *pmem, struct page *page,
 			unsigned int len, unsigned int off, bool is_write,
 			sector_t sector)
drivers/nvdimm/pmem.h +1 -4
index c5917f040fa7..8c5620614ec0 100644
@@ -1,9 +1,6 @@
 #ifndef __NVDIMM_PMEM_H__
 #define __NVDIMM_PMEM_H__
-#include <linux/badblocks.h>
-#include <linux/types.h>
-#include <linux/pfn_t.h>
-#include <linux/fs.h>
+#include <linux/pmem_common.h>
 
 /* this definition is in it's own header for tools/testing/nvdimm to consume */
 struct pmem_device {
include/linux/memremap.h +23 -0
index 79f8ba7c3894..e4eb81020306 100644
@@ -3,12 +3,35 @@
 #include <linux/mm.h>
 #include <linux/ioport.h>
 #include <linux/percpu-refcount.h>
+#include <linux/sizes.h>
+#include <linux/pfn.h>
 
 #include <asm/pgtable.h>
 
 struct resource;
 struct device;
 
+/*
+ * We hotplug memory at section granularity, pad the reserved area from
+ * the previous section base to the namespace base address.
+ */
+static inline unsigned long init_altmap_base(resource_size_t base)
+{
+	unsigned long base_pfn = PHYS_PFN(base);
+
+	return PFN_SECTION_ALIGN_DOWN(base_pfn);
+}
+
+static inline unsigned long init_altmap_reserve(resource_size_t base)
+{
+	unsigned long reserve = PHYS_PFN(SZ_8K);
+	unsigned long base_pfn = PHYS_PFN(base);
+
+	reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn);
+	return reserve;
+}
+
+
 /**
  * struct vmem_altmap - pre-allocated storage for vmemmap_populate
  * @base_pfn: base of the entire dev_pagemap mapping
include/linux/pfn.h +15 -0
index 1132953235c0..2d8f69cc1470 100644
@@ -20,4 +20,19 @@ typedef struct {
 #define PFN_PHYS(x)	((phys_addr_t)(x) << PAGE_SHIFT)
 #define PHYS_PFN(x)	((unsigned long)((x) >> PAGE_SHIFT))
 
+#ifdef CONFIG_SPARSEMEM
+#define PFN_SECTION_ALIGN_DOWN(x) SECTION_ALIGN_DOWN(x)
+#define PFN_SECTION_ALIGN_UP(x) SECTION_ALIGN_UP(x)
+#else
+/*
+ * In this case ZONE_DEVICE=n and we will disable 'pfn' device support,
+ * but we still want pmem to compile.
+ */
+#define PFN_SECTION_ALIGN_DOWN(x) (x)
+#define PFN_SECTION_ALIGN_UP(x) (x)
+#endif
+
+#define PHYS_SECTION_ALIGN_DOWN(x) PFN_PHYS(PFN_SECTION_ALIGN_DOWN(PHYS_PFN(x)))
+#define PHYS_SECTION_ALIGN_UP(x) PFN_PHYS(PFN_SECTION_ALIGN_UP(PHYS_PFN(x)))
+
 #endif
include/linux/pmem_common.h new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
2.13.0
#4 lore raw

RFC 2/2 KVM: add virtio-pmem driver

In reply to Pankaj Gupta

This patch adds virtio-pmem driver for KVM guest. Guest reads the persistent memory range information over virtio bus from Qemu and reserves the range as persistent memory. Guest also allocates a block device corresponding to the pmem range which later can be accessed with DAX compatible file systems. Idea is to use the virtio channel between guest and host to perform the block device flush for guest pmem DAX device.

There is work to do including DAX file system support and other advanced features.

---
 drivers/virtio/Kconfig           |  10 ++
 drivers/virtio/Makefile          |   1 +
 drivers/virtio/virtio_pmem.c     | 322 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_pmem.h |  55 +++++++
 4 files changed, 388 insertions(+)
 create mode 100644 drivers/virtio/virtio_pmem.c
 create mode 100644 include/uapi/linux/virtio_pmem.h
drivers/virtio/Kconfig +10 -0
index cff773f15b7e..0192c4bda54b 100644
@@ -38,6 +38,16 @@ config VIRTIO_PCI_LEGACY
 
 	  If unsure, say Y.
 
+config VIRTIO_PMEM
+	tristate "Virtio pmem driver"
+	depends on VIRTIO
+	---help---
+	 This driver adds persistent memory range within a KVM guest.
+         It also associates a block device corresponding to the pmem
+	 range.
+
+	 If unsure, say M.
+
 config VIRTIO_BALLOON
 	tristate "Virtio balloon driver"
 	depends on VIRTIO
drivers/virtio/Makefile +1 -0
index 41e30e3dc842..032ade725cc2 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_PMEM) += virtio_pmem.o
drivers/virtio/virtio_pmem.c new file +322 -0

322 lines not on this page · raw message

include/uapi/linux/virtio_pmem.h new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
2.13.0
#33 lore raw

RFC 1/1 QEMU: Add virtio pmem device

In reply to Pankaj Gupta

This patch adds virtio-pmem Qemu device.

This device configures memory address range information with file backend type. It acts like persistent memory device for KVM guest. It presents the memory address range to virtio-pmem driver over virtio channel and does the block flush whenever there is request from guest to flush/sync. (later part is yet to be implemented).

Current code is a prototype to support guest with persistent memory range & DAX.

---
 hw/virtio/Makefile.objs                     |   2 +-
 hw/virtio/virtio-pci.c                      |  44 ++++++++++
 hw/virtio/virtio-pci.h                      |  14 +++
 hw/virtio/virtio-pmem.c                     | 130 ++++++++++++++++++++++++++++
 include/hw/pci/pci.h                        |   1 +
 include/hw/virtio/virtio-pmem.h             |  42 +++++++++
 include/standard-headers/linux/virtio_ids.h |   1 +
 7 files changed, 233 insertions(+), 1 deletion(-)
 create mode 100644 hw/virtio/virtio-pmem.c
 create mode 100644 include/hw/virtio/virtio-pmem.h
hw/virtio/Makefile.objs +1 -1
index 765d363c1f..bb5573d2ef 100644
@@ -5,7 +5,7 @@ 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-pmem.o
 obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock.o
 obj-y += virtio-crypto.o
 obj-$(CONFIG_VIRTIO_PCI) += virtio-crypto-pci.o
hw/virtio/virtio-pci.c +44 -0

56 lines not on this page · raw message

hw/virtio/virtio-pci.h +14 -0
index 69f5959623..37f0eeabd2 100644
@@ -19,6 +19,7 @@
 #include "hw/virtio/virtio-blk.h"
 #include "hw/virtio/virtio-net.h"
 #include "hw/virtio/virtio-rng.h"
+#include "hw/virtio/virtio-pmem.h"
 #include "hw/virtio/virtio-serial.h"
 #include "hw/virtio/virtio-scsi.h"
 #include "hw/virtio/virtio-balloon.h"
@@ -53,6 +54,7 @@ typedef struct VirtIOInputHostPCI VirtIOInputHostPCI;
 typedef struct VirtIOGPUPCI VirtIOGPUPCI;
 typedef struct VHostVSockPCI VHostVSockPCI;
 typedef struct VirtIOCryptoPCI VirtIOCryptoPCI;
+typedef struct VirtIOPMEMPCI VirtIOPMEMPCI;
 
 /* virtio-pci-bus */
 
@@ -254,6 +256,18 @@ struct VirtIOBlkPCI {
 };
 
 /*
+ * virtio-pmem-pci: This extends VirtioPCIProxy.
+ */
+#define TYPE_VIRTIO_PMEM_PCI "virtio-pmem-pci"
+#define VIRTIO_PMEM_PCI(obj) \
+        OBJECT_CHECK(VirtIOPMEMPCI, (obj), TYPE_VIRTIO_PMEM_PCI)
+
+struct VirtIOPMEMPCI {
+    VirtIOPCIProxy parent_obj;
+    VirtIOPMEM vdev;
+};
+
+/*
  * virtio-balloon-pci: This extends VirtioPCIProxy.
  */
 #define TYPE_VIRTIO_BALLOON_PCI "virtio-balloon-pci"
hw/virtio/virtio-pmem.c new file +130 -0

130 lines not on this page · raw message

include/hw/pci/pci.h +1 -0
index 8bb6449dd7..b9bfb79a11 100644
@@ -85,6 +85,7 @@ extern bool pci_available;
 #define PCI_DEVICE_ID_VIRTIO_RNG         0x1005
 #define PCI_DEVICE_ID_VIRTIO_9P          0x1009
 #define PCI_DEVICE_ID_VIRTIO_VSOCK       0x1012
+#define PCI_DEVICE_ID_VIRTIO_PMEM        0x1013
 
 #define PCI_VENDOR_ID_REDHAT             0x1b36
 #define PCI_DEVICE_ID_REDHAT_BRIDGE      0x0001
include/hw/virtio/virtio-pmem.h new file +42 -0

42 lines not on this page · raw message

include/standard-headers/linux/virtio_ids.h +1 -0
index 6d5c3b2d4f..5ebd04980d 100644
@@ -43,5 +43,6 @@
 #define VIRTIO_ID_INPUT        18 /* virtio input */
 #define VIRTIO_ID_VSOCK        19 /* virtio vsock transport */
 #define VIRTIO_ID_CRYPTO       20 /* virtio crypto */
+#define VIRTIO_ID_PMEM         21 /* virtio pmem */
 
 #endif /* _LINUX_VIRTIO_IDS_H */
Signature · 1 line
2.13.0
#2 lore raw

Re: [RFC 1/2] pmem: Move reusable code to base header files

In reply to Pankaj Gupta
Quoted 7 lines · Pankaj Gupta

This patch moves common code to base header files so that it can be used for both ACPI pmem and VIRTIO pmem drivers. More common code needs to be moved out in future based on functionality required for virtio_pmem driver and coupling of code with existing ACPI pmem driver.

[..]

include/linux/pmem_common.h

Quoted 5 lines
include/linux/pmem_common.h new file +0 -0
new file mode 100644
index 000000000000..e2e718c74b3f

This should be a common C file, not a header.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

In reply to Pankaj Gupta
Quoted 28 lines · Pankaj Gupta

This patch adds virtio-pmem driver for KVM guest. Guest reads the persistent memory range information over virtio bus from Qemu and reserves the range as persistent memory. Guest also allocates a block device corresponding to the pmem range which later can be accessed with DAX compatible file systems. Idea is to use the virtio channel between guest and host to perform the block device flush for guest pmem DAX device.

There is work to do including DAX file system support and other advanced features.

---
 drivers/virtio/Kconfig           |  10 ++
 drivers/virtio/Makefile          |   1 +
 drivers/virtio/virtio_pmem.c     | 322 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_pmem.h |  55 +++++++
 4 files changed, 388 insertions(+)
 create mode 100644 drivers/virtio/virtio_pmem.c
 create mode 100644 include/uapi/linux/virtio_pmem.h
drivers/virtio/Kconfig +0 -0
index cff773f15b7e..0192c4bda54b 100644

drivers/virtio/Kconfig config VIRTIO_PCI_LEGACY · line 45

Quoted 8 lines
drivers/virtio/Kconfig +5 -0
@@ -38,6 +38,16 @@ config VIRTIO_PCI_LEGACY
 
          If unsure, say Y.
 
+config VIRTIO_PMEM
+       tristate "Virtio pmem driver"
+       depends on VIRTIO
+       ---help---
+        This driver adds persistent memory range within a KVM guest.

I think we need to call this something other than persistent memory to make it clear that this not memory where the persistence can be managed from userspace. The persistence point always requires a driver call, so this is something distinctly different than "persistent memory". For example, it's a bug if this memory range ends up backing a device-dax range in the guest where there is no such thing as a driver callback to perform the flushing. How does this solution protect against that scenario?

Quoted 58 lines
drivers/virtio/Kconfig +5 -0
+         It also associates a block device corresponding to the pmem
+        range.
+
+        If unsure, say M.
+
 config VIRTIO_BALLOON
        tristate "Virtio balloon driver"
        depends on VIRTIO
drivers/virtio/Makefile +1 -0
index 41e30e3dc842..032ade725cc2 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_PMEM) += virtio_pmem.o
drivers/virtio/virtio_pmem.c new file +35 -0
new file mode 100644
index 000000000000..74e47cae0e24
@@ -0,0 +1,322 @@
+/*
+ * virtio-pmem driver
+ */
+
+#include <linux/virtio.h>
+#include <linux/swap.h>
+#include <linux/workqueue.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/oom.h>
+#include <linux/wait.h>
+#include <linux/mm.h>
+#include <linux/mount.h>
+#include <linux/magic.h>
+#include <linux/virtio_pmem.h>
+
+void devm_vpmem_disable(struct device *dev, struct resource *res, void *addr)
+{
+       devm_memunmap(dev, addr);
+       devm_release_mem_region(dev, res->start, resource_size(res));
+}
+
+static void pmem_flush_done(struct virtqueue *vq)
+{
+       return;
+};
+
+static void virtio_pmem_release_queue(void *q)
+{
+       blk_cleanup_queue(q);
+}
+
+static void virtio_pmem_freeze_queue(void *q)
+{

drivers/virtio/virtio_pmem.c line 45

Quoted 10 lines
drivers/virtio/virtio_pmem.c new file +10 -0
+       blk_freeze_queue_start(q);
+}
+
+static void virtio_pmem_release_disk(void *__pmem)
+{
+       struct virtio_pmem *pmem = __pmem;
+
+       del_gendisk(pmem->disk);
+       put_disk(pmem->disk);
+}

This code seems identical to the base pmem case, it should move to the shared code object.

Quoted 120 lines
drivers/virtio/virtio_pmem.c new file +120 -0
+
+static int init_vq(struct virtio_pmem *vpmem)
+{
+       struct virtqueue *vq;
+
+       /* single vq */
+       vq = virtio_find_single_vq(vpmem->vdev, pmem_flush_done, "flush_queue");
+
+       if (IS_ERR(vq))
+               return PTR_ERR(vq);
+
+       return 0;
+}
+
+static struct vmem_altmap *setup_pmem_pfn(struct virtio_pmem *vpmem,
+                       struct resource *res, struct vmem_altmap *altmap)
+{
+       u32 start_pad = 0, end_trunc = 0;
+       resource_size_t start, size;
+       unsigned long npfns;
+       phys_addr_t offset;
+
+       size = resource_size(res);
+       start = PHYS_SECTION_ALIGN_DOWN(res->start);
+
+       if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
+               IORES_DESC_NONE) == REGION_MIXED) {
+
+               start = res->start;
+               start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
+       }
+       start = res->start;
+       size = PHYS_SECTION_ALIGN_UP(start + size) - start;
+
+       if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
+               IORES_DESC_NONE) == REGION_MIXED) {
+
+               size = resource_size(res);
+               end_trunc = start + size -
+                               PHYS_SECTION_ALIGN_DOWN(start + size);
+       }
+
+       start += start_pad;
+       size = resource_size(res);
+       npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K)
+                                               / PAGE_SIZE);
+
+      /*
+       * vmemmap_populate_hugepages() allocates the memmap array in
+       * HPAGE_SIZE chunks.
+       */
+       offset = ALIGN(start + SZ_8K + 64 * npfns, HPAGE_SIZE) - start;
+       vpmem->data_offset = offset;
+
+       struct vmem_altmap __altmap = {
+               .base_pfn = init_altmap_base(start+start_pad),
+               .reserve = init_altmap_reserve(start+start_pad),
+       };
+
+       res->start += start_pad;
+       res->end -= end_trunc;
+       memcpy(altmap, &__altmap, sizeof(*altmap));
+       altmap->free = PHYS_PFN(offset - SZ_8K);
+       altmap->alloc = 0;
+
+       return altmap;
+}
+
+static blk_status_t pmem_do_bvec(struct virtio_pmem *pmem, struct page *page,
+                       unsigned int len, unsigned int off, bool is_write,
+                       sector_t sector)
+{
+       blk_status_t rc = BLK_STS_OK;
+       phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
+       void *pmem_addr = pmem->virt_addr + pmem_off;
+
+       if (!is_write) {
+               rc = read_pmem(page, off, pmem_addr, len);
+                       flush_dcache_page(page);
+       } else {
+               flush_dcache_page(page);
+               write_pmem(pmem_addr, page, off, len);
+       }
+
+       return rc;
+}
+
+static int vpmem_rw_page(struct block_device *bdev, sector_t sector,
+                      struct page *page, bool is_write)
+{
+       struct virtio_pmem  *pmem = bdev->bd_queue->queuedata;
+       blk_status_t rc;
+
+       rc = pmem_do_bvec(pmem, page, hpage_nr_pages(page) * PAGE_SIZE,
+                         0, is_write, sector);
+
+       if (rc == 0)
+               page_endio(page, is_write, 0);
+
+       return blk_status_to_errno(rc);
+}
+
+#ifndef REQ_FLUSH
+#define REQ_FLUSH REQ_PREFLUSH
+#endif
+
+static blk_qc_t virtio_pmem_make_request(struct request_queue *q,
+                       struct bio *bio)
+{
+       blk_status_t rc = 0;
+       struct bio_vec bvec;
+       struct bvec_iter iter;
+       struct virtio_pmem *pmem = q->queuedata;
+
+       if (bio->bi_opf & REQ_FLUSH)
+               //todo host flush command
+
+       bio_for_each_segment(bvec, bio, iter) {
+               rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
+                               bvec.bv_offset, op_is_write(bio_op(bio)),

drivers/virtio/virtio_pmem.c line 175

Quoted 10 lines
drivers/virtio/virtio_pmem.c new file +10 -0
+                               iter.bi_sector);
+               if (rc) {
+                       bio->bi_status = rc;
+                       break;
+               }
+       }
+
+       bio_endio(bio);
+       return BLK_QC_T_NONE;
+}

Again, the above could be shared by both drivers.

Quoted 122 lines
drivers/virtio/virtio_pmem.c new file +122 -0
+
+static const struct block_device_operations pmem_fops = {
+       .owner =                THIS_MODULE,
+       .rw_page =              vpmem_rw_page,
+       //.revalidate_disk =    nvdimm_revalidate_disk,
+};
+
+static int virtio_pmem_probe(struct virtio_device *vdev)
+{
+       struct virtio_pmem *vpmem;
+       int err = 0;
+       void *addr;
+       struct resource *res, res_pfn;
+       struct request_queue *q;
+       struct vmem_altmap __altmap, *altmap = NULL;
+       struct gendisk *disk;
+       struct device *gendev;
+       int nid = dev_to_node(&vdev->dev);
+
+       if (!vdev->config->get) {
+               dev_err(&vdev->dev, "%s failure: config disabled\n",
+                       __func__);
+               return -EINVAL;
+       }
+
+       vdev->priv = vpmem = devm_kzalloc(&vdev->dev, sizeof(*vpmem),
+                       GFP_KERNEL);
+
+       if (!vpmem) {
+               err = -ENOMEM;
+               goto out;
+       }
+
+       dev_set_drvdata(&vdev->dev, vpmem);
+
+       vpmem->vdev = vdev;
+       err = init_vq(vpmem);
+       if (err)
+               goto out;
+
+       if (!virtio_has_feature(vdev, VIRTIO_PMEM_PLUG)) {
+               dev_err(&vdev->dev, "%s : pmem not supported\n",
+                       __func__);
+               goto out;
+       }
+
+       virtio_cread(vpmem->vdev, struct virtio_pmem_config,
+                       start, &vpmem->start);
+       virtio_cread(vpmem->vdev, struct virtio_pmem_config,
+                       size, &vpmem->size);
+
+       res_pfn.start = vpmem->start;
+       res_pfn.end   = vpmem->start + vpmem->size-1;
+
+       /* used for allocating memmap in the pmem device */
+       altmap        = setup_pmem_pfn(vpmem, &res_pfn, &__altmap);
+
+       res = devm_request_mem_region(&vdev->dev,
+                       res_pfn.start, resource_size(&res_pfn), "virtio-pmem");
+
+       if (!res) {
+               dev_warn(&vdev->dev, "could not reserve region ");
+               return -EBUSY;
+       }
+
+       q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(&vdev->dev));
+
+       if (!q)
+               return -ENOMEM;
+
+       if (devm_add_action_or_reset(&vdev->dev,
+                               virtio_pmem_release_queue, q))
+               return -ENOMEM;
+
+       vpmem->pfn_flags = PFN_DEV;
+
+       /* allocate memap in pmem device itself */
+       if (IS_ENABLED(CONFIG_ZONE_DEVICE)) {
+
+               addr = devm_memremap_pages(&vdev->dev, res,
+                               &q->q_usage_counter, altmap);
+               vpmem->pfn_flags |= PFN_MAP;
+       } else
+               addr = devm_memremap(&vdev->dev, vpmem->start,
+                               vpmem->size, ARCH_MEMREMAP_PMEM);
+
+        /*
+         * At release time the queue must be frozen before
+         * devm_memremap_pages is unwound
+         */
+       if (devm_add_action_or_reset(&vdev->dev,
+                               virtio_pmem_freeze_queue, q))
+               return -ENOMEM;
+
+       if (IS_ERR(addr))
+               return PTR_ERR(addr);
+
+       vpmem->virt_addr = addr;
+       blk_queue_write_cache(q, 0, 0);
+       blk_queue_make_request(q, virtio_pmem_make_request);
+       blk_queue_physical_block_size(q, PAGE_SIZE);
+       blk_queue_logical_block_size(q, 512);
+       blk_queue_max_hw_sectors(q, UINT_MAX);
+       queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
+       queue_flag_set_unlocked(QUEUE_FLAG_DAX, q);
+       q->queuedata = vpmem;
+
+       disk = alloc_disk_node(0, nid);
+       if (!disk)
+               return -ENOMEM;
+       vpmem->disk = disk;
+
+       disk->fops                = &pmem_fops;
+       disk->queue               = q;
+       disk->flags               = GENHD_FL_EXT_DEVT;
+       strcpy(disk->disk_name, "vpmem");
+       set_capacity(disk, vpmem->size/512);
+       gendev = disk_to_dev(disk);
+
+       virtio_device_ready(vdev);
+       device_add_disk(&vdev->dev, disk);
+

drivers/virtio/virtio_pmem.c line 307

Quoted 10 lines
drivers/virtio/virtio_pmem.c new file +10 -0
+       if (devm_add_action_or_reset(&vdev->dev,
+                       virtio_pmem_release_disk, vpmem))
+               return -ENOMEM;
+
+       revalidate_disk(disk);
+       return 0;
+out:
+       vdev->config->del_vqs(vdev);
+       return err;
+}

Here we have a mix of code that is common and some that is virtio specific, the shared code should be factored out into a common helper that both drivers call.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

Quoted 33 lines
Quoted 33 lines

This patch adds virtio-pmem driver for KVM guest. Guest reads the persistent memory range information over virtio bus from Qemu and reserves the range as persistent memory. Guest also allocates a block device corresponding to the pmem range which later can be accessed with DAX compatible file systems. Idea is to use the virtio channel between guest and host to perform the block device flush for guest pmem DAX device.

There is work to do including DAX file system support and other advanced features.

---
 drivers/virtio/Kconfig           |  10 ++
 drivers/virtio/Makefile          |   1 +
 drivers/virtio/virtio_pmem.c     | 322
 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_pmem.h |  55 +++++++
 4 files changed, 388 insertions(+)
 create mode 100644 drivers/virtio/virtio_pmem.c
 create mode 100644 include/uapi/linux/virtio_pmem.h
drivers/virtio/Kconfig +0 -0
index cff773f15b7e..0192c4bda54b 100644
@@ -38,6 +38,16 @@ config VIRTIO_PCI_LEGACY
 
          If unsure, say Y.
 
Quoted 14 lines

drivers/virtio/Kconfig config VIRTIO_PCI_LEGACY · line 45

Quoted 5 lines
drivers/virtio/Kconfig +5 -0
+config VIRTIO_PMEM
+       tristate "Virtio pmem driver"
+       depends on VIRTIO
+       ---help---
+        This driver adds persistent memory range within a KVM guest.

I think we need to call this something other than persistent memory to make it clear that this not memory where the persistence can be managed from userspace. The persistence point always requires a driver call, so this is something distinctly different than "persistent memory". For example, it's a bug if this memory range ends up backing a device-dax range in the guest where there is no such thing as a driver callback to perform the flushing. How does this solution protect against that scenario?

yes, you are right we are not providing device_dax in this case so it should be clear from name. Any suggestion for name?

Quoted 65 lines
Quoted 65 lines
drivers/virtio/Kconfig +5 -0
+         It also associates a block device corresponding to the pmem
+        range.
+
+        If unsure, say M.
+
 config VIRTIO_BALLOON
        tristate "Virtio balloon driver"
        depends on VIRTIO
drivers/virtio/Makefile +1 -0
index 41e30e3dc842..032ade725cc2 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_PMEM) += virtio_pmem.o
drivers/virtio/virtio_pmem.c new file +18 -0
new file mode 100644
index 000000000000..74e47cae0e24
@@ -0,0 +1,322 @@
+/*
+ * virtio-pmem driver
+ */
+
+#include <linux/virtio.h>
+#include <linux/swap.h>
+#include <linux/workqueue.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/oom.h>
+#include <linux/wait.h>
+#include <linux/mm.h>
+#include <linux/mount.h>
+#include <linux/magic.h>
+#include <linux/virtio_pmem.h>
+
+void devm_vpmem_disable(struct device *dev, struct resource *res, void

*addr)

drivers/virtio/virtio_pmem.c new file +22 -0
+{
+       devm_memunmap(dev, addr);
+       devm_release_mem_region(dev, res->start, resource_size(res));
+}
+
+static void pmem_flush_done(struct virtqueue *vq)
+{
+       return;
+};
+
+static void virtio_pmem_release_queue(void *q)
+{
+       blk_cleanup_queue(q);
+}
+
+static void virtio_pmem_freeze_queue(void *q)
+{
+       blk_freeze_queue_start(q);
+}
+
+static void virtio_pmem_release_disk(void *__pmem)
+{
Quoted 8 lines

drivers/virtio/virtio_pmem.c line 45

Quoted 5 lines
drivers/virtio/virtio_pmem.c new file +5 -0
+       struct virtio_pmem *pmem = __pmem;
+
+       del_gendisk(pmem->disk);
+       put_disk(pmem->disk);
+}

This code seems identical to the base pmem case, it should move to the shared code object.

Sure!

Quoted 127 lines
Quoted 127 lines
drivers/virtio/virtio_pmem.c new file +7 -0
+
+static int init_vq(struct virtio_pmem *vpmem)
+{
+       struct virtqueue *vq;
+
+       /* single vq */
+       vq = virtio_find_single_vq(vpmem->vdev, pmem_flush_done,

"flush_queue");

drivers/virtio/virtio_pmem.c new file +62 -0
+
+       if (IS_ERR(vq))
+               return PTR_ERR(vq);
+
+       return 0;
+}
+
+static struct vmem_altmap *setup_pmem_pfn(struct virtio_pmem *vpmem,
+                       struct resource *res, struct vmem_altmap *altmap)
+{
+       u32 start_pad = 0, end_trunc = 0;
+       resource_size_t start, size;
+       unsigned long npfns;
+       phys_addr_t offset;
+
+       size = resource_size(res);
+       start = PHYS_SECTION_ALIGN_DOWN(res->start);
+
+       if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
+               IORES_DESC_NONE) == REGION_MIXED) {
+
+               start = res->start;
+               start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
+       }
+       start = res->start;
+       size = PHYS_SECTION_ALIGN_UP(start + size) - start;
+
+       if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
+               IORES_DESC_NONE) == REGION_MIXED) {
+
+               size = resource_size(res);
+               end_trunc = start + size -
+                               PHYS_SECTION_ALIGN_DOWN(start + size);
+       }
+
+       start += start_pad;
+       size = resource_size(res);
+       npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K)
+                                               / PAGE_SIZE);
+
+      /*
+       * vmemmap_populate_hugepages() allocates the memmap array in
+       * HPAGE_SIZE chunks.
+       */
+       offset = ALIGN(start + SZ_8K + 64 * npfns, HPAGE_SIZE) - start;
+       vpmem->data_offset = offset;
+
+       struct vmem_altmap __altmap = {
+               .base_pfn = init_altmap_base(start+start_pad),
+               .reserve = init_altmap_reserve(start+start_pad),
+       };
+
+       res->start += start_pad;
+       res->end -= end_trunc;
+       memcpy(altmap, &__altmap, sizeof(*altmap));
+       altmap->free = PHYS_PFN(offset - SZ_8K);
+       altmap->alloc = 0;
+
+       return altmap;
+}
+
+static blk_status_t pmem_do_bvec(struct virtio_pmem *pmem, struct page

*page,

drivers/virtio/virtio_pmem.c new file +55 -0
+                       unsigned int len, unsigned int off, bool is_write,
+                       sector_t sector)
+{
+       blk_status_t rc = BLK_STS_OK;
+       phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
+       void *pmem_addr = pmem->virt_addr + pmem_off;
+
+       if (!is_write) {
+               rc = read_pmem(page, off, pmem_addr, len);
+                       flush_dcache_page(page);
+       } else {
+               flush_dcache_page(page);
+               write_pmem(pmem_addr, page, off, len);
+       }
+
+       return rc;
+}
+
+static int vpmem_rw_page(struct block_device *bdev, sector_t sector,
+                      struct page *page, bool is_write)
+{
+       struct virtio_pmem  *pmem = bdev->bd_queue->queuedata;
+       blk_status_t rc;
+
+       rc = pmem_do_bvec(pmem, page, hpage_nr_pages(page) * PAGE_SIZE,
+                         0, is_write, sector);
+
+       if (rc == 0)
+               page_endio(page, is_write, 0);
+
+       return blk_status_to_errno(rc);
+}
+
+#ifndef REQ_FLUSH
+#define REQ_FLUSH REQ_PREFLUSH
+#endif
+
+static blk_qc_t virtio_pmem_make_request(struct request_queue *q,
+                       struct bio *bio)
+{
+       blk_status_t rc = 0;
+       struct bio_vec bvec;
+       struct bvec_iter iter;
+       struct virtio_pmem *pmem = q->queuedata;
+
+       if (bio->bi_opf & REQ_FLUSH)
+               //todo host flush command
+
+       bio_for_each_segment(bvec, bio, iter) {
+               rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
+                               bvec.bv_offset, op_is_write(bio_op(bio)),
+                               iter.bi_sector);
+               if (rc) {
+                       bio->bi_status = rc;
+                       break;
Quoted 8 lines

drivers/virtio/virtio_pmem.c line 175

Quoted 6 lines
drivers/virtio/virtio_pmem.c new file +6 -0
+               }
+       }
+
+       bio_endio(bio);
+       return BLK_QC_T_NONE;
+}

Again, the above could be shared by both drivers.

yes, I will do that.

Quoted 130 lines
Quoted 130 lines
drivers/virtio/virtio_pmem.c new file +59 -0
+
+static const struct block_device_operations pmem_fops = {
+       .owner =                THIS_MODULE,
+       .rw_page =              vpmem_rw_page,
+       //.revalidate_disk =    nvdimm_revalidate_disk,
+};
+
+static int virtio_pmem_probe(struct virtio_device *vdev)
+{
+       struct virtio_pmem *vpmem;
+       int err = 0;
+       void *addr;
+       struct resource *res, res_pfn;
+       struct request_queue *q;
+       struct vmem_altmap __altmap, *altmap = NULL;
+       struct gendisk *disk;
+       struct device *gendev;
+       int nid = dev_to_node(&vdev->dev);
+
+       if (!vdev->config->get) {
+               dev_err(&vdev->dev, "%s failure: config disabled\n",
+                       __func__);
+               return -EINVAL;
+       }
+
+       vdev->priv = vpmem = devm_kzalloc(&vdev->dev, sizeof(*vpmem),
+                       GFP_KERNEL);
+
+       if (!vpmem) {
+               err = -ENOMEM;
+               goto out;
+       }
+
+       dev_set_drvdata(&vdev->dev, vpmem);
+
+       vpmem->vdev = vdev;
+       err = init_vq(vpmem);
+       if (err)
+               goto out;
+
+       if (!virtio_has_feature(vdev, VIRTIO_PMEM_PLUG)) {
+               dev_err(&vdev->dev, "%s : pmem not supported\n",
+                       __func__);
+               goto out;
+       }
+
+       virtio_cread(vpmem->vdev, struct virtio_pmem_config,
+                       start, &vpmem->start);
+       virtio_cread(vpmem->vdev, struct virtio_pmem_config,
+                       size, &vpmem->size);
+
+       res_pfn.start = vpmem->start;
+       res_pfn.end   = vpmem->start + vpmem->size-1;
+
+       /* used for allocating memmap in the pmem device */
+       altmap        = setup_pmem_pfn(vpmem, &res_pfn, &__altmap);
+
+       res = devm_request_mem_region(&vdev->dev,
+                       res_pfn.start, resource_size(&res_pfn),

"virtio-pmem");

drivers/virtio/virtio_pmem.c new file +69 -0
+
+       if (!res) {
+               dev_warn(&vdev->dev, "could not reserve region ");
+               return -EBUSY;
+       }
+
+       q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(&vdev->dev));
+
+       if (!q)
+               return -ENOMEM;
+
+       if (devm_add_action_or_reset(&vdev->dev,
+                               virtio_pmem_release_queue, q))
+               return -ENOMEM;
+
+       vpmem->pfn_flags = PFN_DEV;
+
+       /* allocate memap in pmem device itself */
+       if (IS_ENABLED(CONFIG_ZONE_DEVICE)) {
+
+               addr = devm_memremap_pages(&vdev->dev, res,
+                               &q->q_usage_counter, altmap);
+               vpmem->pfn_flags |= PFN_MAP;
+       } else
+               addr = devm_memremap(&vdev->dev, vpmem->start,
+                               vpmem->size, ARCH_MEMREMAP_PMEM);
+
+        /*
+         * At release time the queue must be frozen before
+         * devm_memremap_pages is unwound
+         */
+       if (devm_add_action_or_reset(&vdev->dev,
+                               virtio_pmem_freeze_queue, q))
+               return -ENOMEM;
+
+       if (IS_ERR(addr))
+               return PTR_ERR(addr);
+
+       vpmem->virt_addr = addr;
+       blk_queue_write_cache(q, 0, 0);
+       blk_queue_make_request(q, virtio_pmem_make_request);
+       blk_queue_physical_block_size(q, PAGE_SIZE);
+       blk_queue_logical_block_size(q, 512);
+       blk_queue_max_hw_sectors(q, UINT_MAX);
+       queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
+       queue_flag_set_unlocked(QUEUE_FLAG_DAX, q);
+       q->queuedata = vpmem;
+
+       disk = alloc_disk_node(0, nid);
+       if (!disk)
+               return -ENOMEM;
+       vpmem->disk = disk;
+
+       disk->fops                = &pmem_fops;
+       disk->queue               = q;
+       disk->flags               = GENHD_FL_EXT_DEVT;
+       strcpy(disk->disk_name, "vpmem");
+       set_capacity(disk, vpmem->size/512);
+       gendev = disk_to_dev(disk);
+
+       virtio_device_ready(vdev);
+       device_add_disk(&vdev->dev, disk);
+
+       if (devm_add_action_or_reset(&vdev->dev,
+                       virtio_pmem_release_disk, vpmem))
+               return -ENOMEM;
+
+       revalidate_disk(disk);
+       return 0;
Quoted 8 lines

drivers/virtio/virtio_pmem.c line 307

Quoted 4 lines
drivers/virtio/virtio_pmem.c new file +4 -0
+out:
+       vdev->config->del_vqs(vdev);
+       return err;
+}

Here we have a mix of code that is common and some that is virtio specific, the shared code should be factored out into a common helper that both drivers call.

yes, i will factor this as well.

Quoted 7 lines

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

Re: [Qemu-devel] [RFC 1/2] pmem: Move reusable code to base header files

In reply to Dan Williams
Quoted 10 lines · Pankaj Gupta
Quoted 7 lines

This patch moves common code to base header files so that it can be used for both ACPI pmem and VIRTIO pmem drivers. More common code needs to be moved out in future based on functionality required for virtio_pmem driver and coupling of code with existing ACPI pmem driver.

Quoted 8 lines

[..]

include/linux/pmem_common.h

Quoted 5 lines
include/linux/pmem_common.h new file +0 -0
new file mode 100644
index 000000000000..e2e718c74b3f

This should be a common C file, not a header.

Sure! will create a common C file to put all the common code there.

Quoted 2 lines

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

In reply to Pankaj Gupta
Quoted 48 lines · Pankaj Gupta
Quoted 33 lines
Quoted 33 lines

This patch adds virtio-pmem driver for KVM guest. Guest reads the persistent memory range information over virtio bus from Qemu and reserves the range as persistent memory. Guest also allocates a block device corresponding to the pmem range which later can be accessed with DAX compatible file systems. Idea is to use the virtio channel between guest and host to perform the block device flush for guest pmem DAX device.

There is work to do including DAX file system support and other advanced features.

---
 drivers/virtio/Kconfig           |  10 ++
 drivers/virtio/Makefile          |   1 +
 drivers/virtio/virtio_pmem.c     | 322
 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_pmem.h |  55 +++++++
 4 files changed, 388 insertions(+)
 create mode 100644 drivers/virtio/virtio_pmem.c
 create mode 100644 include/uapi/linux/virtio_pmem.h
drivers/virtio/Kconfig +0 -0
index cff773f15b7e..0192c4bda54b 100644
@@ -38,6 +38,16 @@ config VIRTIO_PCI_LEGACY
 
          If unsure, say Y.
 
Quoted 14 lines

drivers/virtio/Kconfig config VIRTIO_PCI_LEGACY · line 45

Quoted 5 lines
drivers/virtio/Kconfig +5 -0
+config VIRTIO_PMEM
+       tristate "Virtio pmem driver"
+       depends on VIRTIO
+       ---help---
+        This driver adds persistent memory range within a KVM guest.

I think we need to call this something other than persistent memory to make it clear that this not memory where the persistence can be managed from userspace. The persistence point always requires a driver call, so this is something distinctly different than "persistent memory". For example, it's a bug if this memory range ends up backing a device-dax range in the guest where there is no such thing as a driver callback to perform the flushing. How does this solution protect against that scenario?

Quoted 2 lines

yes, you are right we are not providing device_dax in this case so it should be clear from name. Any suggestion for name?

So currently /proc/iomem in a guest with a pmem device attached to a namespace looks like this:

    c00000000-13bfffffff : Persistent Memory
       c00000000-13bfffffff : namespace2.0

Can we call it "Virtio Shared Memory" to make it clear it is a different beast than typical "Persistent Memory"? You can likely inject your own name into the resource tree the same way we do in the NFIT driver. See acpi_nfit_insert_resource().

#8 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

Quoted 55 lines · Pankaj Gupta
Quoted 48 lines
Quoted 33 lines
Quoted 33 lines

This patch adds virtio-pmem driver for KVM guest. Guest reads the persistent memory range information over virtio bus from Qemu and reserves the range as persistent memory. Guest also allocates a block device corresponding to the pmem range which later can be accessed with DAX compatible file systems. Idea is to use the virtio channel between guest and host to perform the block device flush for guest pmem DAX device.

There is work to do including DAX file system support and other advanced features.

---
 drivers/virtio/Kconfig           |  10 ++
 drivers/virtio/Makefile          |   1 +
 drivers/virtio/virtio_pmem.c     | 322
 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_pmem.h |  55 +++++++
 4 files changed, 388 insertions(+)
 create mode 100644 drivers/virtio/virtio_pmem.c
 create mode 100644 include/uapi/linux/virtio_pmem.h
drivers/virtio/Kconfig +0 -0
index cff773f15b7e..0192c4bda54b 100644
@@ -38,6 +38,16 @@ config VIRTIO_PCI_LEGACY
 
          If unsure, say Y.
 
Quoted 14 lines

drivers/virtio/Kconfig config VIRTIO_PCI_LEGACY · line 45

Quoted 5 lines
drivers/virtio/Kconfig +5 -0
+config VIRTIO_PMEM
+       tristate "Virtio pmem driver"
+       depends on VIRTIO
+       ---help---
+        This driver adds persistent memory range within a KVM guest.

I think we need to call this something other than persistent memory to make it clear that this not memory where the persistence can be managed from userspace. The persistence point always requires a driver call, so this is something distinctly different than "persistent memory". For example, it's a bug if this memory range ends up backing a device-dax range in the guest where there is no such thing as a driver callback to perform the flushing. How does this solution protect against that scenario?

Quoted 3 lines

yes, you are right we are not providing device_dax in this case so it should
be clear from name. Any suggestion for name?

Quoted 8 lines

So currently /proc/iomem in a guest with a pmem device attached to a namespace looks like this:

    c00000000-13bfffffff : Persistent Memory
       c00000000-13bfffffff : namespace2.0

Can we call it "Virtio Shared Memory" to make it clear it is a different beast than typical "Persistent Memory"? You can likely

I think somewhere we need persistent keyword 'Virtio Persistent Memory' or so.

Quoted 2 lines

inject your own name into the resource tree the same way we do in the NFIT driver. See acpi_nfit_insert_resource().

Sure! thank you.

#9 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

Quoted 34 lines · Pankaj Gupta
Quoted 34 lines
Quoted 31 lines · Pankaj Gupta
Quoted 31 lines
Quoted 31 lines

  This patch adds virtio-pmem driver for KVM guest. Guest reads the persistent memory range information over virtio bus from Qemu and reserves the range as persistent memory. Guest also allocates a block device corresponding to the pmem range which later can be accessed with DAX compatible file systems. Idea is to use the virtio channel between guest and host to perform the block device flush for guest pmem DAX device.

  There is work to do including DAX file system support and other advanced features.

---
 drivers/virtio/Kconfig           |  10 ++
 drivers/virtio/Makefile          |   1 +
 drivers/virtio/virtio_pmem.c     | 322
 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_pmem.h |  55 +++++++

 4 files changed, 388 insertions(+)

 create mode 100644 drivers/virtio/virtio_pmem.c create mode 100644 include/uapi/linux/virtio_pmem.h

drivers/virtio/Kconfig +0 -0
index cff773f15b7e..0192c4bda54b 100644
@@ -38,6 +38,16 @@ config VIRTIO_PCI_LEGACY
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

          If unsure, say Y.

+config VIRTIO_PMEM
+       tristate "Virtio pmem driver"
+       depends on VIRTIO
+       ---help---
+        This driver adds persistent memory range within a KVM guest.

With "Virtio Block Backed Pmem" we could name the config option VIRTIO_BLOCK_PMEM

The documentation text could make it clear to people that the image shows up as a disk image on the host, but as a pmem memory range in the guest.

Quoted 12 lines
Quoted 12 lines
Quoted 5 lines
Quoted 5 lines

I think we need to call this something other than persistent memory to
make it clear that this not memory where the persistence can be managed from userspace. The persistence point always requires

So currently /proc/iomem in a guest with a pmem device attached to a
namespace looks like this:

    c00000000-13bfffffff : Persistent Memory
       c00000000-13bfffffff : namespace2.0

Quoted 6 lines
Quoted 2 lines

Can we call it "Virtio Shared Memory" to make it clear it is a different beast than typical "Persistent Memory"?  You can likely

I think somewhere we need persistent keyword 'Virtio Persistent Memory' or
so.

Still hoping for better ideas than "Virtio Block Backed Pmem" :)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#10 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

Quoted 43 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines

  This patch adds virtio-pmem driver for KVM guest. Guest reads the persistent memory range information over virtio bus from Qemu and reserves the range as persistent memory. Guest also allocates a block device corresponding to the pmem range which later can be accessed with DAX compatible file systems. Idea is to use the virtio channel between guest and host to perform the block device flush for guest pmem DAX device.

  There is work to do including DAX file system support and other advanced features.

---
 drivers/virtio/Kconfig           |  10 ++
 drivers/virtio/Makefile          |   1 +
 drivers/virtio/virtio_pmem.c     | 322
 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_pmem.h |  55 +++++++

 4 files changed, 388 insertions(+)

 create mode 100644 drivers/virtio/virtio_pmem.c create mode 100644 include/uapi/linux/virtio_pmem.h

drivers/virtio/Kconfig +0 -0
index cff773f15b7e..0192c4bda54b 100644
@@ -38,6 +38,16 @@ config VIRTIO_PCI_LEGACY
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

          If unsure, say Y.

+config VIRTIO_PMEM
+       tristate "Virtio pmem driver"
+       depends on VIRTIO
+       ---help---
+        This driver adds persistent memory range within a KVM guest.

With "Virtio Block Backed Pmem" we could name the config option VIRTIO_BLOCK_PMEM

Quoted 3 lines

The documentation text could make it clear to people that the image shows up as a disk image on the host, but as a pmem memory range in the guest.

yes, this looks better.
thank you.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 6 lines
Quoted 6 lines

I think we need to call this something other than persistent memory to
make it clear that this not memory where the persistence can be managed from userspace. The persistence point always requires

So currently /proc/iomem in a guest with a pmem device attached to a
namespace looks like this:

    c00000000-13bfffffff : Persistent Memory
       c00000000-13bfffffff : namespace2.0

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Can we call it "Virtio Shared Memory" to make it clear it is a different beast than typical "Persistent Memory"?  You can likely

I think somewhere we need persistent keyword 'Virtio Persistent Memory' or
so.

Still hoping for better ideas than "Virtio Block Backed Pmem" :)

:-)

Quoted 1 line
#11 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

In reply to Rik van Riel
Quoted 61 lines
Quoted 33 lines
Quoted 33 lines
Quoted 31 lines · wrote:
Quoted 31 lines
Quoted 31 lines

  This patch adds virtio-pmem driver for KVM guest. Guest reads the persistent memory range information over virtio bus from Qemu and reserves the range as persistent memory. Guest also allocates a block device corresponding to the pmem range which later can be accessed with DAX compatible file systems. Idea is to use the virtio channel between guest and host to perform the block device flush for guest pmem DAX device.

  There is work to do including DAX file system support and other advanced features.

---
 drivers/virtio/Kconfig           |  10 ++
 drivers/virtio/Makefile          |   1 +
 drivers/virtio/virtio_pmem.c     | 322
 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_pmem.h |  55 +++++++

 4 files changed, 388 insertions(+)

 create mode 100644 drivers/virtio/virtio_pmem.c create mode 100644 include/uapi/linux/virtio_pmem.h

drivers/virtio/Kconfig +0 -0
index cff773f15b7e..0192c4bda54b 100644
@@ -38,6 +38,16 @@ config VIRTIO_PCI_LEGACY
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

          If unsure, say Y.

+config VIRTIO_PMEM
+       tristate "Virtio pmem driver"
+       depends on VIRTIO
+       ---help---
+        This driver adds persistent memory range within a KVM guest.

With "Virtio Block Backed Pmem" we could name the config option VIRTIO_BLOCK_PMEM

The documentation text could make it clear to people that the image shows up as a disk image on the host, but as a pmem memory range in the guest.

Quoted 12 lines
Quoted 12 lines
Quoted 5 lines
Quoted 5 lines

I think we need to call this something other than persistent memory to
make it clear that this not memory where the persistence can be managed from userspace. The persistence point always requires

So currently /proc/iomem in a guest with a pmem device attached to a
namespace looks like this:

    c00000000-13bfffffff : Persistent Memory
       c00000000-13bfffffff : namespace2.0

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

Can we call it "Virtio Shared Memory" to make it clear it is a different beast than typical "Persistent Memory"?  You can likely

I think somewhere we need persistent keyword 'Virtio Persistent Memory' or
so.

Still hoping for better ideas than "Virtio Block Backed Pmem" :)

Dan,

I have a query regarding below patch [*]. My assumption is its halted because of memory hotplug restructuring work? Anything I am missing here?

[*] https://www.mail-archive.com/[email protected]/msg02978.html

#12 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

Quoted 7 lines · Pankaj Gupta

Dan,

I have a query regarding below patch [*]. My assumption is its halted because of memory hotplug restructuring work? Anything I am missing here?

[*] https://www.mail-archive.com/[email protected]/msg02978.html

It's fallen to the back of my queue since the original driving need of platform firmware changing offsets from boot-to-boot is no longer an issue. However, it does mean that you need to arrange for 128MB aligned devm_memremap_pages() ranges for the foreseeable future.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#13 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

Quoted 7 lines
Quoted 7 lines

Dan,

I have a query regarding below patch [*]. My assumption is its halted because of memory hotplug restructuring work? Anything I am missing here?

Quoted 6 lines
Quoted 1 line

[*] https://www.mail-archive.com/[email protected]/msg02978.html

It's fallen to the back of my queue since the original driving need of platform firmware changing offsets from boot-to-boot is no longer an issue. However, it does mean that you need to arrange for 128MB aligned devm_memremap_pages() ranges for the foreseeable future.

o.k I will provide 128MB aligned pages to devm_memremap_pages() function.

Thanks,
Pankaj

#14 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

In reply to Pankaj Gupta
Quoted 158 lines · Pankaj Gupta

This patch adds virtio-pmem driver for KVM guest. Guest reads the persistent memory range information over virtio bus from Qemu and reserves the range as persistent memory. Guest also allocates a block device corresponding to the pmem range which later can be accessed with DAX compatible file systems. Idea is to use the virtio channel between guest and host to perform the block device flush for guest pmem DAX device.

There is work to do including DAX file system support and other advanced features.

---
 drivers/virtio/Kconfig           |  10 ++
 drivers/virtio/Makefile          |   1 +
 drivers/virtio/virtio_pmem.c     | 322 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_pmem.h |  55 +++++++
 4 files changed, 388 insertions(+)
 create mode 100644 drivers/virtio/virtio_pmem.c
 create mode 100644 include/uapi/linux/virtio_pmem.h
drivers/virtio/Kconfig +10 -0
index cff773f15b7e..0192c4bda54b 100644
@@ -38,6 +38,16 @@ config VIRTIO_PCI_LEGACY
 
 	  If unsure, say Y.
 
+config VIRTIO_PMEM
+	tristate "Virtio pmem driver"
+	depends on VIRTIO
+	---help---
+	 This driver adds persistent memory range within a KVM guest.
+         It also associates a block device corresponding to the pmem
+	 range.
+
+	 If unsure, say M.
+
 config VIRTIO_BALLOON
 	tristate "Virtio balloon driver"
 	depends on VIRTIO
drivers/virtio/Makefile +1 -0
index 41e30e3dc842..032ade725cc2 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_PMEM) += virtio_pmem.o
drivers/virtio/virtio_pmem.c new file +99 -0
new file mode 100644
index 000000000000..74e47cae0e24
@@ -0,0 +1,322 @@
+/*
+ * virtio-pmem driver
+ */
+
+#include <linux/virtio.h>
+#include <linux/swap.h>
+#include <linux/workqueue.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/oom.h>
+#include <linux/wait.h>
+#include <linux/mm.h>
+#include <linux/mount.h>
+#include <linux/magic.h>
+#include <linux/virtio_pmem.h>
+
+void devm_vpmem_disable(struct device *dev, struct resource *res, void *addr)
+{
+	devm_memunmap(dev, addr);
+	devm_release_mem_region(dev, res->start, resource_size(res));
+}
+
+static void pmem_flush_done(struct virtqueue *vq)
+{
+	return;
+};
+
+static void virtio_pmem_release_queue(void *q)
+{
+	blk_cleanup_queue(q);
+}
+
+static void virtio_pmem_freeze_queue(void *q)
+{
+	blk_freeze_queue_start(q);
+}
+
+static void virtio_pmem_release_disk(void *__pmem)
+{
+	struct virtio_pmem *pmem = __pmem;
+
+	del_gendisk(pmem->disk);
+	put_disk(pmem->disk);
+}
+
+static int init_vq(struct virtio_pmem *vpmem)
+{
+	struct virtqueue *vq;
+
+	/* single vq */
+	vq = virtio_find_single_vq(vpmem->vdev, pmem_flush_done, "flush_queue");
+
+	if (IS_ERR(vq))
+		return PTR_ERR(vq);
+
+	return 0;
+}
+
+static struct vmem_altmap *setup_pmem_pfn(struct virtio_pmem *vpmem,
+			struct resource *res, struct vmem_altmap *altmap)
+{
+	u32 start_pad = 0, end_trunc = 0;
+	resource_size_t start, size;
+	unsigned long npfns;
+	phys_addr_t offset;
+
+	size = resource_size(res);
+	start = PHYS_SECTION_ALIGN_DOWN(res->start);
+
+	if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
+		IORES_DESC_NONE) == REGION_MIXED) {
+
+		start = res->start;
+		start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
+	}
+	start = res->start;
+	size = PHYS_SECTION_ALIGN_UP(start + size) - start;
+
+	if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
+		IORES_DESC_NONE) == REGION_MIXED) {
+
+		size = resource_size(res);
+		end_trunc = start + size -
+				PHYS_SECTION_ALIGN_DOWN(start + size);
+	}
+
+	start += start_pad;
+	size = resource_size(res);
+	npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K)
+						/ PAGE_SIZE);
+
+      /*
+       * vmemmap_populate_hugepages() allocates the memmap array in
+       * HPAGE_SIZE chunks.
+       */
+	offset = ALIGN(start + SZ_8K + 64 * npfns, HPAGE_SIZE) - start;
+	vpmem->data_offset = offset;
+

drivers/virtio/virtio_pmem.c line 109

Quoted 10 lines
drivers/virtio/virtio_pmem.c new file +10 -0
+	struct vmem_altmap __altmap = {
+		.base_pfn = init_altmap_base(start+start_pad),
+		.reserve = init_altmap_reserve(start+start_pad),
+	};
+
+	res->start += start_pad;
+	res->end -= end_trunc;
+	memcpy(altmap, &__altmap, sizeof(*altmap));
+	altmap->free = PHYS_PFN(offset - SZ_8K);
+	altmap->alloc = 0;

The __altmap part can be rewritten using compound literal syntax:

  *altmap = (struct vmem_altmap) {
      .base_pfn = init_altmap_base(start+start_pad),
      .reserve = init_altmap_reserve(start+start_pad),
      .free = PHYS_PFN(offset - SZ_8K),
  };

This eliminates the temporary variable, memcpy, and explicit alloc = 0 initialization.

Quoted 7 lines
drivers/virtio/virtio_pmem.c new file +7 -0
+
+	return altmap;
+}
+
+static blk_status_t pmem_do_bvec(struct virtio_pmem *pmem, struct page *page,
+			unsigned int len, unsigned int off, bool is_write,
+			sector_t sector)

drivers/virtio/virtio_pmem.c line 126

Quoted 10 lines
drivers/virtio/virtio_pmem.c new file +10 -0
+{
+	blk_status_t rc = BLK_STS_OK;
+	phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
+	void *pmem_addr = pmem->virt_addr + pmem_off;
+
+	if (!is_write) {
+		rc = read_pmem(page, off, pmem_addr, len);
+			flush_dcache_page(page);
+	} else {
+		flush_dcache_page(page);

What are the semantics of this device? Why flush the dcache here if an explicit flush command needs to be sent over the virtqueue?

Quoted 14 lines
drivers/virtio/virtio_pmem.c new file +14 -0
+		write_pmem(pmem_addr, page, off, len);
+	}
+
+	return rc;
+}
+
+static int vpmem_rw_page(struct block_device *bdev, sector_t sector,
+		       struct page *page, bool is_write)
+{
+	struct virtio_pmem  *pmem = bdev->bd_queue->queuedata;
+	blk_status_t rc;
+
+	rc = pmem_do_bvec(pmem, page, hpage_nr_pages(page) * PAGE_SIZE,
+			  0, is_write, sector);

drivers/virtio/virtio_pmem.c line 150

Quoted 10 lines
drivers/virtio/virtio_pmem.c new file +10 -0
+
+	if (rc == 0)
+		page_endio(page, is_write, 0);
+
+	return blk_status_to_errno(rc);
+}
+
+#ifndef REQ_FLUSH
+#define REQ_FLUSH REQ_PREFLUSH
+#endif

Is this out-of-tree kernel module compatibility stuff that can be removed?

drivers/virtio/virtio_pmem.c line 161

Quoted 11 lines
drivers/virtio/virtio_pmem.c new file +11 -0
+
+static blk_qc_t virtio_pmem_make_request(struct request_queue *q,
+			struct bio *bio)
+{
+	blk_status_t rc = 0;
+	struct bio_vec bvec;
+	struct bvec_iter iter;
+	struct virtio_pmem *pmem = q->queuedata;
+
+	if (bio->bi_opf & REQ_FLUSH)
+		//todo host flush command

This detail is critical to the device design. What is the plan?

Quoted 49 lines
drivers/virtio/virtio_pmem.c new file +49 -0
+
+	bio_for_each_segment(bvec, bio, iter) {
+		rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
+				bvec.bv_offset, op_is_write(bio_op(bio)),
+				iter.bi_sector);
+		if (rc) {
+			bio->bi_status = rc;
+			break;
+		}
+	}
+
+	bio_endio(bio);
+	return BLK_QC_T_NONE;
+}
+
+static const struct block_device_operations pmem_fops = {
+	.owner =		THIS_MODULE,
+	.rw_page =		vpmem_rw_page,
+	//.revalidate_disk =	nvdimm_revalidate_disk,
+};
+
+static int virtio_pmem_probe(struct virtio_device *vdev)
+{
+	struct virtio_pmem *vpmem;
+	int err = 0;
+	void *addr;
+	struct resource *res, res_pfn;
+	struct request_queue *q;
+	struct vmem_altmap __altmap, *altmap = NULL;
+	struct gendisk *disk;
+	struct device *gendev;
+	int nid = dev_to_node(&vdev->dev);
+
+	if (!vdev->config->get) {
+		dev_err(&vdev->dev, "%s failure: config disabled\n",
+			__func__);
+		return -EINVAL;
+	}
+
+	vdev->priv = vpmem = devm_kzalloc(&vdev->dev, sizeof(*vpmem),
+			GFP_KERNEL);
+
+	if (!vpmem) {
+		err = -ENOMEM;
+		goto out;
+	}
+
+	dev_set_drvdata(&vdev->dev, vpmem);
+

drivers/virtio/virtio_pmem.c line 220

Quoted 10 lines
drivers/virtio/virtio_pmem.c new file +10 -0
+	vpmem->vdev = vdev;
+	err = init_vq(vpmem);
+	if (err)
+		goto out;
+
+	if (!virtio_has_feature(vdev, VIRTIO_PMEM_PLUG)) {
+		dev_err(&vdev->dev, "%s : pmem not supported\n",
+			__func__);
+		goto out;
+	}

Why is VIRTIO_PMEM_PLUG optional for this new device type if it's already required by the first version of the driver?

err is not set when the error path is taken.

drivers/virtio/virtio_pmem.c line 225

Quoted 5 lines
drivers/virtio/virtio_pmem.c new file +5 -0
+
+	virtio_cread(vpmem->vdev, struct virtio_pmem_config,
+			start, &vpmem->start);
+	virtio_cread(vpmem->vdev, struct virtio_pmem_config,
+			size, &vpmem->size);

The struct resource start and size fields can vary between architectures. Virtio device configuration space layout must not vary between architectures. Please use u64.

Quoted 50 lines
drivers/virtio/virtio_pmem.c new file +50 -0
+
+	res_pfn.start = vpmem->start;
+	res_pfn.end   = vpmem->start + vpmem->size-1;
+
+	/* used for allocating memmap in the pmem device */
+	altmap	      = setup_pmem_pfn(vpmem, &res_pfn, &__altmap);
+
+	res = devm_request_mem_region(&vdev->dev,
+			res_pfn.start, resource_size(&res_pfn), "virtio-pmem");
+
+	if (!res) {
+		dev_warn(&vdev->dev, "could not reserve region ");
+		return -EBUSY;
+	}
+
+	q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(&vdev->dev));
+
+	if (!q)
+		return -ENOMEM;
+
+	if (devm_add_action_or_reset(&vdev->dev,
+				virtio_pmem_release_queue, q))
+		return -ENOMEM;
+
+	vpmem->pfn_flags = PFN_DEV;
+
+	/* allocate memap in pmem device itself */
+	if (IS_ENABLED(CONFIG_ZONE_DEVICE)) {
+
+		addr = devm_memremap_pages(&vdev->dev, res,
+				&q->q_usage_counter, altmap);
+		vpmem->pfn_flags |= PFN_MAP;
+	} else
+		addr = devm_memremap(&vdev->dev, vpmem->start,
+				vpmem->size, ARCH_MEMREMAP_PMEM);
+
+        /*
+         * At release time the queue must be frozen before
+         * devm_memremap_pages is unwound
+         */
+	if (devm_add_action_or_reset(&vdev->dev,
+				virtio_pmem_freeze_queue, q))
+		return -ENOMEM;
+
+	if (IS_ERR(addr))
+		return PTR_ERR(addr);
+
+	vpmem->virt_addr = addr;
+	blk_queue_write_cache(q, 0, 0);
+	blk_queue_make_request(q, virtio_pmem_make_request);

drivers/virtio/virtio_pmem.c line 285

Quoted 10 lines
drivers/virtio/virtio_pmem.c new file +10 -0
+	blk_queue_physical_block_size(q, PAGE_SIZE);
+	blk_queue_logical_block_size(q, 512);
+	blk_queue_max_hw_sectors(q, UINT_MAX);
+	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
+	queue_flag_set_unlocked(QUEUE_FLAG_DAX, q);
+	q->queuedata = vpmem;
+
+	disk = alloc_disk_node(0, nid);
+	if (!disk)
+		return -ENOMEM;

The return statements in this function look suspicious. There probably needs to be a cleanup to avoid memory leaks.

Quoted 50 lines
drivers/virtio/virtio_pmem.c new file +37 -0
+	vpmem->disk = disk;
+
+	disk->fops                = &pmem_fops;
+	disk->queue               = q;
+	disk->flags               = GENHD_FL_EXT_DEVT;
+	strcpy(disk->disk_name, "vpmem");
+	set_capacity(disk, vpmem->size/512);
+	gendev = disk_to_dev(disk);
+
+	virtio_device_ready(vdev);
+	device_add_disk(&vdev->dev, disk);
+
+	if (devm_add_action_or_reset(&vdev->dev,
+			virtio_pmem_release_disk, vpmem))
+		return -ENOMEM;
+
+	revalidate_disk(disk);
+	return 0;
+out:
+	vdev->config->del_vqs(vdev);
+	return err;
+}
+
+static struct virtio_driver virtio_pmem_driver = {
+	.feature_table		= features,
+	.feature_table_size	= ARRAY_SIZE(features),
+	.driver.name		= KBUILD_MODNAME,
+	.driver.owner		= THIS_MODULE,
+	.id_table		= id_table,
+	.probe			= virtio_pmem_probe,
+	//.remove		= virtio_pmem_remove,
+};
+
+module_virtio_driver(virtio_pmem_driver);
+MODULE_DEVICE_TABLE(virtio, id_table);
+MODULE_DESCRIPTION("Virtio pmem driver");
+MODULE_LICENSE("GPL");
include/uapi/linux/virtio_pmem.h new file +7 -0
new file mode 100644
index 000000000000..ec0c728c79ba
@@ -0,0 +1,55 @@
+/*
+ * Virtio pmem Device
+ *
+ *
+ */
+
+#ifndef _LINUX_VIRTIO_PMEM_H

include/uapi/linux/virtio_pmem.h line 17

Quoted 10 lines
include/uapi/linux/virtio_pmem.h new file +10 -0
+#define _LINUX_VIRTIO_PMEM_H
+
+#include <linux/types.h>
+#include <linux/virtio_types.h>
+#include <linux/virtio_ids.h>
+#include <linux/virtio_config.h>
+#include <linux/pfn_t.h>
+#include <linux/fs.h>
+#include <linux/blk-mq.h>
+#include <linux/pmem_common.h>

include/uapi/ headers must compile when included from userspace applications. The header should define userspace APIs only.

If you want to define kernel-internal APIs, please add them to include/linux/ or similar.

This also means that include/uapi/ headers cannot include include/linux/pmem_common.h or other kernel headers outside #ifdef __KERNEL__.

This header file should only contain struct virtio_pmem_config, VIRTIO_PMEM_PLUG, and other virtio device definitions.

Quoted 41 lines
include/uapi/linux/virtio_pmem.h new file +38 -0
+
+bool pmem_should_map_pages(struct device *dev);
+
+#define VIRTIO_PMEM_PLUG 0
+
+struct virtio_pmem_config {
+
+uint64_t start;
+uint64_t size;
+uint64_t align;
+uint64_t data_offset;
+};
+
+struct virtio_pmem {
+
+	struct virtio_device *vdev;
+	struct virtqueue *req_vq;
+
+	uint64_t start;
+	uint64_t size;
+	uint64_t align;
+	uint64_t data_offset;
+	u64 pfn_flags;
+	void *virt_addr;
+	struct gendisk *disk;
+} __packed;
+
+static struct virtio_device_id id_table[] = {
+	{ VIRTIO_ID_PMEM, VIRTIO_DEV_ANY_ID },
+	{ 0 },
+};
+
+static unsigned int features[] = {
+	VIRTIO_PMEM_PLUG,
+};
+
+#endif
+
Signature · 1 line
2.13.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#15 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

Quoted 176 lines · Pankaj Gupta
Quoted 160 lines

This patch adds virtio-pmem driver for KVM guest. Guest reads the persistent memory range information over virtio bus from Qemu and reserves the range as persistent memory. Guest also allocates a block device corresponding to the pmem range which later can be accessed with DAX compatible file systems. Idea is to use the virtio channel between guest and host to perform the block device flush for guest pmem DAX device.

There is work to do including DAX file system support and other advanced features.

---
 drivers/virtio/Kconfig           |  10 ++
 drivers/virtio/Makefile          |   1 +
 drivers/virtio/virtio_pmem.c     | 322
 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_pmem.h |  55 +++++++
 4 files changed, 388 insertions(+)
 create mode 100644 drivers/virtio/virtio_pmem.c
 create mode 100644 include/uapi/linux/virtio_pmem.h
drivers/virtio/Kconfig +10 -0
index cff773f15b7e..0192c4bda54b 100644
@@ -38,6 +38,16 @@ config VIRTIO_PCI_LEGACY
 
 	  If unsure, say Y.
 
+config VIRTIO_PMEM
+	tristate "Virtio pmem driver"
+	depends on VIRTIO
+	---help---
+	 This driver adds persistent memory range within a KVM guest.
+         It also associates a block device corresponding to the pmem
+	 range.
+
+	 If unsure, say M.
+
 config VIRTIO_BALLOON
 	tristate "Virtio balloon driver"
 	depends on VIRTIO
drivers/virtio/Makefile +1 -0
index 41e30e3dc842..032ade725cc2 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_PMEM) += virtio_pmem.o
drivers/virtio/virtio_pmem.c new file +18 -0
new file mode 100644
index 000000000000..74e47cae0e24
@@ -0,0 +1,322 @@
+/*
+ * virtio-pmem driver
+ */
+
+#include <linux/virtio.h>
+#include <linux/swap.h>
+#include <linux/workqueue.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/oom.h>
+#include <linux/wait.h>
+#include <linux/mm.h>
+#include <linux/mount.h>
+#include <linux/magic.h>
+#include <linux/virtio_pmem.h>
+
+void devm_vpmem_disable(struct device *dev, struct resource *res, void

*addr)

drivers/virtio/virtio_pmem.c new file +81 -0
+{
+	devm_memunmap(dev, addr);
+	devm_release_mem_region(dev, res->start, resource_size(res));
+}
+
+static void pmem_flush_done(struct virtqueue *vq)
+{
+	return;
+};
+
+static void virtio_pmem_release_queue(void *q)
+{
+	blk_cleanup_queue(q);
+}
+
+static void virtio_pmem_freeze_queue(void *q)
+{
+	blk_freeze_queue_start(q);
+}
+
+static void virtio_pmem_release_disk(void *__pmem)
+{
+	struct virtio_pmem *pmem = __pmem;
+
+	del_gendisk(pmem->disk);
+	put_disk(pmem->disk);
+}
+
+static int init_vq(struct virtio_pmem *vpmem)
+{
+	struct virtqueue *vq;
+
+	/* single vq */
+	vq = virtio_find_single_vq(vpmem->vdev, pmem_flush_done, "flush_queue");
+
+	if (IS_ERR(vq))
+		return PTR_ERR(vq);
+
+	return 0;
+}
+
+static struct vmem_altmap *setup_pmem_pfn(struct virtio_pmem *vpmem,
+			struct resource *res, struct vmem_altmap *altmap)
+{
+	u32 start_pad = 0, end_trunc = 0;
+	resource_size_t start, size;
+	unsigned long npfns;
+	phys_addr_t offset;
+
+	size = resource_size(res);
+	start = PHYS_SECTION_ALIGN_DOWN(res->start);
+
+	if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
+		IORES_DESC_NONE) == REGION_MIXED) {
+
+		start = res->start;
+		start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
+	}
+	start = res->start;
+	size = PHYS_SECTION_ALIGN_UP(start + size) - start;
+
+	if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
+		IORES_DESC_NONE) == REGION_MIXED) {
+
+		size = resource_size(res);
+		end_trunc = start + size -
+				PHYS_SECTION_ALIGN_DOWN(start + size);
+	}
+
+	start += start_pad;
+	size = resource_size(res);
+	npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K)
+						/ PAGE_SIZE);
+
+      /*
+       * vmemmap_populate_hugepages() allocates the memmap array in
+       * HPAGE_SIZE chunks.
+       */
+	offset = ALIGN(start + SZ_8K + 64 * npfns, HPAGE_SIZE) - start;
+	vpmem->data_offset = offset;
+

drivers/virtio/virtio_pmem.c line 109

Quoted 10 lines
drivers/virtio/virtio_pmem.c new file +10 -0
+	struct vmem_altmap __altmap = {
+		.base_pfn = init_altmap_base(start+start_pad),
+		.reserve = init_altmap_reserve(start+start_pad),
+	};
+
+	res->start += start_pad;
+	res->end -= end_trunc;
+	memcpy(altmap, &__altmap, sizeof(*altmap));
+	altmap->free = PHYS_PFN(offset - SZ_8K);
+	altmap->alloc = 0;

The __altmap part can be rewritten using compound literal syntax:

Quoted 8 lines
  *altmap = (struct vmem_altmap) {
      .base_pfn = init_altmap_base(start+start_pad),
      .reserve = init_altmap_reserve(start+start_pad),
      .free = PHYS_PFN(offset - SZ_8K),
  };

This eliminates the temporary variable, memcpy, and explicit alloc = 0 initialization.

o.k will use it.

Quoted 14 lines
Quoted 14 lines
drivers/virtio/virtio_pmem.c new file +5 -0
+
+	return altmap;
+}
+
+static blk_status_t pmem_do_bvec(struct virtio_pmem *pmem, struct page

*page,

drivers/virtio/virtio_pmem.c new file +7 -0
+			unsigned int len, unsigned int off, bool is_write,
+			sector_t sector)
+{
+	blk_status_t rc = BLK_STS_OK;
+	phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
+	void *pmem_addr = pmem->virt_addr + pmem_off;
+
Quoted 8 lines

drivers/virtio/virtio_pmem.c line 126

Quoted 5 lines
drivers/virtio/virtio_pmem.c new file +5 -0
+	if (!is_write) {
+		rc = read_pmem(page, off, pmem_addr, len);
+			flush_dcache_page(page);
+	} else {
+		flush_dcache_page(page);

What are the semantics of this device? Why flush the dcache here if an explicit flush command needs to be sent over the virtqueue?

yes, we don't need it in this case. I yet have to think more about flushing with block and file-system level support :)

Quoted 20 lines
Quoted 20 lines
drivers/virtio/virtio_pmem.c new file +19 -0
+		write_pmem(pmem_addr, page, off, len);
+	}
+
+	return rc;
+}
+
+static int vpmem_rw_page(struct block_device *bdev, sector_t sector,
+		       struct page *page, bool is_write)
+{
+	struct virtio_pmem  *pmem = bdev->bd_queue->queuedata;
+	blk_status_t rc;
+
+	rc = pmem_do_bvec(pmem, page, hpage_nr_pages(page) * PAGE_SIZE,
+			  0, is_write, sector);
+
+	if (rc == 0)
+		page_endio(page, is_write, 0);
+
+	return blk_status_to_errno(rc);
Quoted 8 lines

drivers/virtio/virtio_pmem.c line 150

Quoted 5 lines
drivers/virtio/virtio_pmem.c new file +5 -0
+}
+
+#ifndef REQ_FLUSH
+#define REQ_FLUSH REQ_PREFLUSH
+#endif

Is this out-of-tree kernel module compatibility stuff that can be removed?

yes, will check what filesystem expects for flush if not used then remove.

Quoted 14 lines

drivers/virtio/virtio_pmem.c line 161

Quoted 12 lines
drivers/virtio/virtio_pmem.c new file +11 -0
+
+static blk_qc_t virtio_pmem_make_request(struct request_queue *q,
+			struct bio *bio)
+{
+	blk_status_t rc = 0;
+	struct bio_vec bvec;
+	struct bvec_iter iter;
+	struct virtio_pmem *pmem = q->queuedata;
+
+	if (bio->bi_opf & REQ_FLUSH)
+		//todo host flush command

This detail is critical to the device design. What is the plan?

yes, this is good point.

was thinking of guest sending a flush command to Qemu which will do a fsync on file fd.

If we do a async flush and move the task to wait queue till we receive flush complete reply from host we can allow other tasks to execute in current cpu.

Any suggestions you have or anything I am not foreseeing here?

Quoted 55 lines
Quoted 55 lines
drivers/virtio/virtio_pmem.c new file +54 -0
+
+	bio_for_each_segment(bvec, bio, iter) {
+		rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
+				bvec.bv_offset, op_is_write(bio_op(bio)),
+				iter.bi_sector);
+		if (rc) {
+			bio->bi_status = rc;
+			break;
+		}
+	}
+
+	bio_endio(bio);
+	return BLK_QC_T_NONE;
+}
+
+static const struct block_device_operations pmem_fops = {
+	.owner =		THIS_MODULE,
+	.rw_page =		vpmem_rw_page,
+	//.revalidate_disk =	nvdimm_revalidate_disk,
+};
+
+static int virtio_pmem_probe(struct virtio_device *vdev)
+{
+	struct virtio_pmem *vpmem;
+	int err = 0;
+	void *addr;
+	struct resource *res, res_pfn;
+	struct request_queue *q;
+	struct vmem_altmap __altmap, *altmap = NULL;
+	struct gendisk *disk;
+	struct device *gendev;
+	int nid = dev_to_node(&vdev->dev);
+
+	if (!vdev->config->get) {
+		dev_err(&vdev->dev, "%s failure: config disabled\n",
+			__func__);
+		return -EINVAL;
+	}
+
+	vdev->priv = vpmem = devm_kzalloc(&vdev->dev, sizeof(*vpmem),
+			GFP_KERNEL);
+
+	if (!vpmem) {
+		err = -ENOMEM;
+		goto out;
+	}
+
+	dev_set_drvdata(&vdev->dev, vpmem);
+
+	vpmem->vdev = vdev;
+	err = init_vq(vpmem);
+	if (err)
+		goto out;
+
Quoted 8 lines

drivers/virtio/virtio_pmem.c line 220

Quoted 5 lines
drivers/virtio/virtio_pmem.c new file +5 -0
+	if (!virtio_has_feature(vdev, VIRTIO_PMEM_PLUG)) {
+		dev_err(&vdev->dev, "%s : pmem not supported\n",
+			__func__);
+		goto out;
+	}

Why is VIRTIO_PMEM_PLUG optional for this new device type if it's already required by the first version of the driver?

I just added it, initially kept it a place holder for any feature bit use. I will remove it if we are not using it.

Quoted 12 lines

err is not set when the error path is taken.

drivers/virtio/virtio_pmem.c line 225

Quoted 5 lines
drivers/virtio/virtio_pmem.c new file +5 -0
+
+	virtio_cread(vpmem->vdev, struct virtio_pmem_config,
+			start, &vpmem->start);
+	virtio_cread(vpmem->vdev, struct virtio_pmem_config,
+			size, &vpmem->size);

The struct resource start and size fields can vary between architectures. Virtio device configuration space layout must not vary between architectures. Please use u64.

sure.

Quoted 56 lines
Quoted 56 lines
drivers/virtio/virtio_pmem.c new file +55 -0
+
+	res_pfn.start = vpmem->start;
+	res_pfn.end   = vpmem->start + vpmem->size-1;
+
+	/* used for allocating memmap in the pmem device */
+	altmap	      = setup_pmem_pfn(vpmem, &res_pfn, &__altmap);
+
+	res = devm_request_mem_region(&vdev->dev,
+			res_pfn.start, resource_size(&res_pfn), "virtio-pmem");
+
+	if (!res) {
+		dev_warn(&vdev->dev, "could not reserve region ");
+		return -EBUSY;
+	}
+
+	q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(&vdev->dev));
+
+	if (!q)
+		return -ENOMEM;
+
+	if (devm_add_action_or_reset(&vdev->dev,
+				virtio_pmem_release_queue, q))
+		return -ENOMEM;
+
+	vpmem->pfn_flags = PFN_DEV;
+
+	/* allocate memap in pmem device itself */
+	if (IS_ENABLED(CONFIG_ZONE_DEVICE)) {
+
+		addr = devm_memremap_pages(&vdev->dev, res,
+				&q->q_usage_counter, altmap);
+		vpmem->pfn_flags |= PFN_MAP;
+	} else
+		addr = devm_memremap(&vdev->dev, vpmem->start,
+				vpmem->size, ARCH_MEMREMAP_PMEM);
+
+        /*
+         * At release time the queue must be frozen before
+         * devm_memremap_pages is unwound
+         */
+	if (devm_add_action_or_reset(&vdev->dev,
+				virtio_pmem_freeze_queue, q))
+		return -ENOMEM;
+
+	if (IS_ERR(addr))
+		return PTR_ERR(addr);
+
+	vpmem->virt_addr = addr;
+	blk_queue_write_cache(q, 0, 0);
+	blk_queue_make_request(q, virtio_pmem_make_request);
+	blk_queue_physical_block_size(q, PAGE_SIZE);
+	blk_queue_logical_block_size(q, 512);
+	blk_queue_max_hw_sectors(q, UINT_MAX);
+	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
+	queue_flag_set_unlocked(QUEUE_FLAG_DAX, q);
Quoted 8 lines

drivers/virtio/virtio_pmem.c line 285

Quoted 5 lines
drivers/virtio/virtio_pmem.c new file +5 -0
+	q->queuedata = vpmem;
+
+	disk = alloc_disk_node(0, nid);
+	if (!disk)
+		return -ENOMEM;

The return statements in this function look suspicious. There probably needs to be a cleanup to avoid memory leaks.

Sure! valid point.

Quoted 66 lines
Quoted 52 lines
drivers/virtio/virtio_pmem.c new file +37 -0
+	vpmem->disk = disk;
+
+	disk->fops                = &pmem_fops;
+	disk->queue               = q;
+	disk->flags               = GENHD_FL_EXT_DEVT;
+	strcpy(disk->disk_name, "vpmem");
+	set_capacity(disk, vpmem->size/512);
+	gendev = disk_to_dev(disk);
+
+	virtio_device_ready(vdev);
+	device_add_disk(&vdev->dev, disk);
+
+	if (devm_add_action_or_reset(&vdev->dev,
+			virtio_pmem_release_disk, vpmem))
+		return -ENOMEM;
+
+	revalidate_disk(disk);
+	return 0;
+out:
+	vdev->config->del_vqs(vdev);
+	return err;
+}
+
+static struct virtio_driver virtio_pmem_driver = {
+	.feature_table		= features,
+	.feature_table_size	= ARRAY_SIZE(features),
+	.driver.name		= KBUILD_MODNAME,
+	.driver.owner		= THIS_MODULE,
+	.id_table		= id_table,
+	.probe			= virtio_pmem_probe,
+	//.remove		= virtio_pmem_remove,
+};
+
+module_virtio_driver(virtio_pmem_driver);
+MODULE_DEVICE_TABLE(virtio, id_table);
+MODULE_DESCRIPTION("Virtio pmem driver");
+MODULE_LICENSE("GPL");
include/uapi/linux/virtio_pmem.h new file +7 -0
new file mode 100644
index 000000000000..ec0c728c79ba
@@ -0,0 +1,55 @@
+/*
+ * Virtio pmem Device
+ *
+ *
+ */
+
+#ifndef _LINUX_VIRTIO_PMEM_H

include/uapi/linux/virtio_pmem.h line 17

Quoted 10 lines
include/uapi/linux/virtio_pmem.h new file +10 -0
+#define _LINUX_VIRTIO_PMEM_H
+
+#include <linux/types.h>
+#include <linux/virtio_types.h>
+#include <linux/virtio_ids.h>
+#include <linux/virtio_config.h>
+#include <linux/pfn_t.h>
+#include <linux/fs.h>
+#include <linux/blk-mq.h>
+#include <linux/pmem_common.h>

include/uapi/ headers must compile when included from userspace applications. The header should define userspace APIs only.

Quoted 6 lines

If you want to define kernel-internal APIs, please add them to include/linux/ or similar.

This also means that include/uapi/ headers cannot include include/linux/pmem_common.h or other kernel headers outside #ifdef __KERNEL__.

o.k

Quoted 3 lines

This header file should only contain struct virtio_pmem_config, VIRTIO_PMEM_PLUG, and other virtio device definitions.

Sure!

Quoted 34 lines
Quoted 34 lines
include/uapi/linux/virtio_pmem.h new file +33 -0
+
+bool pmem_should_map_pages(struct device *dev);
+
+#define VIRTIO_PMEM_PLUG 0
+
+struct virtio_pmem_config {
+
+uint64_t start;
+uint64_t size;
+uint64_t align;
+uint64_t data_offset;
+};
+
+struct virtio_pmem {
+
+	struct virtio_device *vdev;
+	struct virtqueue *req_vq;
+
+	uint64_t start;
+	uint64_t size;
+	uint64_t align;
+	uint64_t data_offset;
+	u64 pfn_flags;
+	void *virt_addr;
+	struct gendisk *disk;
+} __packed;
+
+static struct virtio_device_id id_table[] = {
+	{ VIRTIO_ID_PMEM, VIRTIO_DEV_ANY_ID },
+	{ 0 },
+};
+
+static unsigned int features[] = {
Quoted 9 lines
Quoted 8 lines
include/uapi/linux/virtio_pmem.h new file +5 -1
+	VIRTIO_PMEM_PLUG,
+};
+
+#endif
+
--

2.13.0

Thanks,
Pankaj

#19 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

In reply to Stefan Hajnoczi
Quoted 1 line · Stefan Hajnoczi

On Thu, Oct 12, 2017 at 09:20:26PM +0530, Pankaj Gupta wrote:

[..]

Quoted 6 lines

drivers/virtio/virtio_pmem.c line 150

Quoted 3 lines
drivers/virtio/virtio_pmem.c new file +3 -0
+#ifndef REQ_FLUSH
+#define REQ_FLUSH REQ_PREFLUSH
+#endif

Is this out-of-tree kernel module compatibility stuff that can be removed?

Yes, this was copied from the pmem driver where it can also be removed, it was used to workaround a merge order problem in linux-next when these definitions were changed several kernel releases back.

#16 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

In reply to Pankaj Gupta
Quoted 14 lines · Pankaj Gupta
Quoted 5 lines · Pankaj Gupta
Quoted 4 lines
drivers/virtio/virtio_pmem.c new file +4 -0
+static blk_qc_t virtio_pmem_make_request(struct request_queue *q,
+			struct bio *bio)
+{
+	blk_status_t rc = 0;
Quoted 8 lines

drivers/virtio/virtio_pmem.c line 161

Quoted 6 lines
drivers/virtio/virtio_pmem.c new file +6 -0
+	struct bio_vec bvec;
+	struct bvec_iter iter;
+	struct virtio_pmem *pmem = q->queuedata;
+
+	if (bio->bi_opf & REQ_FLUSH)
+		//todo host flush command

This detail is critical to the device design. What is the plan?

Quoted 4 lines

yes, this is good point.

was thinking of guest sending a flush command to Qemu which will do a fsync on file fd.

Previously there was discussion about fsyncing a specific file range instead of the whole file. This could perform better in cases where only a subset of dirty pages need to be flushed.

One possibility is to design the virtio interface to communicate ranges but the emulation code simply fsyncs the fd for the time being. Later on, if the necessary kernel and userspace interfaces are added, we can make use of the interface.

Quoted 5 lines

If we do a async flush and move the task to wait queue till we receive flush complete reply from host we can allow other tasks to execute in current cpu.

Any suggestions you have or anything I am not foreseeing here?

My main thought about this patch series is whether pmem should be a virtio-blk feature bit instead of a whole new device. There is quite a bit of overlap between the two.

Stefan

#17 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

Quoted 20 lines · Stefan Hajnoczi
Quoted 14 lines · Pankaj Gupta
Quoted 5 lines · Pankaj Gupta
Quoted 4 lines
drivers/virtio/virtio_pmem.c new file +4 -0
+static blk_qc_t virtio_pmem_make_request(struct request_queue *q,
+                 struct bio *bio)
+{
+ blk_status_t rc = 0;
Quoted 8 lines

drivers/virtio/virtio_pmem.c line 161

Quoted 6 lines

+ struct bio_vec bvec;
+ struct bvec_iter iter;
+ struct virtio_pmem *pmem = q->queuedata;

drivers/virtio/virtio_pmem.c new file +3 -0
+
+ if (bio->bi_opf & REQ_FLUSH)
+         //todo host flush command

This detail is critical to the device design. What is the plan?

Quoted 4 lines

yes, this is good point.

was thinking of guest sending a flush command to Qemu which will do a fsync on file fd.

Quoted 8 lines

Previously there was discussion about fsyncing a specific file range instead of the whole file. This could perform better in cases where only a subset of dirty pages need to be flushed.

One possibility is to design the virtio interface to communicate ranges but the emulation code simply fsyncs the fd for the time being. Later on, if the necessary kernel and userspace interfaces are added, we can make use of the interface.

Range based is not a natural storage cache management mechanism. All that is it available typically is a full write-cache-flush mechanism and upper layers would need to customized for range-based flushing.

Quoted 9 lines
Quoted 5 lines

If we do a async flush and move the task to wait queue till we receive flush complete reply from host we can allow other tasks to execute in current cpu.

Any suggestions you have or anything I am not foreseeing here?

My main thought about this patch series is whether pmem should be a virtio-blk feature bit instead of a whole new device. There is quite a bit of overlap between the two.

I'd be open to that... there's already provisions in the pmem driver for platforms where cpu caches are flushed on power-loss, a virtio mode for this shared-memory case seems reasonable.

#18 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

In reply to Stefan Hajnoczi
Quoted 17 lines · Pankaj Gupta
Quoted 14 lines
Quoted 13 lines · Pankaj Gupta

drivers/virtio/virtio_pmem.c line 161

Quoted 10 lines
drivers/virtio/virtio_pmem.c new file +10 -0
+static blk_qc_t virtio_pmem_make_request(struct request_queue *q,
+			struct bio *bio)
+{
+	blk_status_t rc = 0;
+	struct bio_vec bvec;
+	struct bvec_iter iter;
+	struct virtio_pmem *pmem = q->queuedata;
+
+	if (bio->bi_opf & REQ_FLUSH)
+		//todo host flush command

This detail is critical to the device design. What is the plan?

Quoted 8 lines
Quoted 4 lines

yes, this is good point.

was thinking of guest sending a flush command to Qemu which will do a fsync on file fd.

Previously there was discussion about fsyncing a specific file range instead of the whole file. This could perform better in cases where only a subset of dirty pages need to be flushed.

yes, We had discussion about this and decided to do entire block flush then to range level flush.

Quoted 10 lines

One possibility is to design the virtio interface to communicate ranges but the emulation code simply fsyncs the fd for the time being. Later on, if the necessary kernel and userspace interfaces are added, we can make use of the interface.

Quoted 4 lines

If we do a async flush and move the task to wait queue till we receive flush complete reply from host we can allow other tasks to execute in current cpu.

Quoted 5 lines
Quoted 1 line

Any suggestions you have or anything I am not foreseeing here?

My main thought about this patch series is whether pmem should be a virtio-blk feature bit instead of a whole new device. There is quite a bit of overlap between the two.

Exposing options with existing virtio-blk device to be used as persistent memory range at high level would require additional below features:

- Use a persistent memory range with an option to allocate memmap array in the device itself for .

- Block operations for DAX and persistent memory range.

- Bifurcation at filesystem level based on type of virtio-blk device selected.

- Bifurcation of flushing interface and communication channel between guest & host.

But yes these features can be dynamically configured based on type of device added? What if we have virtio-blk:virtio-pmem (m:n) devices ratio?And scale involved?

If i understand correctly virtio-blk is high performance interface with multiqueue support and additional features at host side like data-plane mode etc. If we bloat it with additional stuff(even when we need them) and provide locking with additional features both at guest as well as host side we will get a hit in performance? Also as requirement of both the interfaces would grow it will be more difficult to maintain? I would prefer more simpler interfaces with defined functionality but yes common code can be shared and used using well defined wrappers.

Quoted 3 lines

Stefan

#20 lore raw

Re: [RFC 2/2] KVM: add virtio-pmem driver

In reply to Pankaj Gupta

I think this driver is at entirely the wrong level.

If you want to expose pmem to a guest with flushing assist do it as pmem, and not a block driver.

#21 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

Quoted 6 lines

I think this driver is at entirely the wrong level.

If you want to expose pmem to a guest with flushing assist do it as pmem, and not a block driver.

Are you saying do it as existing i.e ACPI pmem like interface? The reason we have created this new driver is exiting pmem driver does not define proper semantics for guest flushing requests.

Regarding block support of driver, we want to achieve DAX support to bypass guest page cache. Also, we want to utilize existing DAX capable file-system interfaces(e.g fsync) from userspace file API's to trigger the host side flush request.

Below link has details of previous discussion:
https://marc.info/?l=kvm&m=150091133700361&w=2

Thanks,
Pankaj

#22 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

Quoted 3 lines · Pankaj Gupta

Are you saying do it as existing i.e ACPI pmem like interface? The reason we have created this new driver is exiting pmem driver does not define proper semantics for guest flushing requests.

At this point I'm caring about the Linux-internal interface, and for that it should be integrated into the nvdimm subsystem and not a block driver. How the host <-> guest interface looks is a different idea.

Quoted 5 lines

Regarding block support of driver, we want to achieve DAX support to bypass guest page cache. Also, we want to utilize existing DAX capable file-system interfaces(e.g fsync) from userspace file API's to trigger the host side flush request.

Well, if you want to support XFS+DAX better don't make it a block devices, because I'll post patches soon to stop using the block device entirely for the DAX case.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#23 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

Quoted 11 lines
Quoted 4 lines

Are you saying do it as existing i.e ACPI pmem like interface? The reason we have created this new driver is exiting pmem driver does not define proper semantics for guest flushing requests.

At this point I'm caring about the Linux-internal interface, and for that it should be integrated into the nvdimm subsystem and not a block driver. How the host <-> guest interface looks is a different idea.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

Regarding block support of driver, we want to achieve DAX support to bypass guest page cache. Also, we want to utilize existing DAX capable file-system interfaces(e.g fsync) from userspace file API's to trigger the host side flush request.

Well, if you want to support XFS+DAX better don't make it a block devices, because I'll post patches soon to stop using the block device entirely for the DAX case.

o.k I will look at your patches once they are in mailing list. Thanks for the heads up.

If I am guessing it right, we don't need block device additional features for pmem? We can bypass block device features like blk device cache flush etc. Also, still we would be supporting ext4 & XFS filesystem with pmem?

If there is time to your patches can you please elaborate on this a bit.

Thanks,
Pankaj

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#24 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

Quoted 20 lines · Pankaj Gupta
Quoted 11 lines
Quoted 4 lines

Are you saying do it as existing i.e ACPI pmem like interface? The reason we have created this new driver is exiting pmem driver does not define proper semantics for guest flushing requests.

At this point I'm caring about the Linux-internal interface, and for that it should be integrated into the nvdimm subsystem and not a block driver. How the host <-> guest interface looks is a different idea.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

Regarding block support of driver, we want to achieve DAX support to bypass guest page cache. Also, we want to utilize existing DAX capable file-system interfaces(e.g fsync) from userspace file API's to trigger the host side flush request.

Well, if you want to support XFS+DAX better don't make it a block devices, because I'll post patches soon to stop using the block device entirely for the DAX case.

Quoted 8 lines

o.k I will look at your patches once they are in mailing list. Thanks for the heads up.

If I am guessing it right, we don't need block device additional features for pmem? We can bypass block device features like blk device cache flush etc. Also, still we would be supporting ext4 & XFS filesystem with pmem?

If there is time to your patches can you please elaborate on this a bit.

I think the idea is that the nvdimm subsystem already adds block device semantics on top of the struct nvdimms that it manages. See drivers/nvdimm/blk.c.

So it would be cleaner to make virtio-pmem an nvdimm bus. This will eliminate the duplication between your driver and drivers/nvdimm/ code. Try "git grep nvdimm_bus_register" to find drivers that use the nvdimm subsystem.

The virtio-pmem driver could register itself similar to the existing ACPI NFIT and BIOS e820 pmem drivers.

Stefan

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#25 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

Quoted 30 lines · Stefan Hajnoczi
Quoted 20 lines · Pankaj Gupta
Quoted 11 lines
Quoted 4 lines

Are you saying do it as existing i.e ACPI pmem like interface? The reason we have created this new driver is exiting pmem driver does not define proper semantics for guest flushing requests.

At this point I'm caring about the Linux-internal interface, and for that it should be integrated into the nvdimm subsystem and not a block driver. How the host <-> guest interface looks is a different idea.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

Regarding block support of driver, we want to achieve DAX support to bypass guest page cache. Also, we want to utilize existing DAX capable file-system interfaces(e.g fsync) from userspace file API's to trigger the host side flush request.

Well, if you want to support XFS+DAX better don't make it a block devices, because I'll post patches soon to stop using the block device entirely for the DAX case.

Quoted 8 lines

o.k I will look at your patches once they are in mailing list. Thanks for the heads up.

If I am guessing it right, we don't need block device additional features for pmem? We can bypass block device features like blk device cache flush etc. Also, still we would be supporting ext4 & XFS filesystem with pmem?

If there is time to your patches can you please elaborate on this a bit.

Quoted 8 lines

I think the idea is that the nvdimm subsystem already adds block device semantics on top of the struct nvdimms that it manages. See drivers/nvdimm/blk.c.

So it would be cleaner to make virtio-pmem an nvdimm bus. This will eliminate the duplication between your driver and drivers/nvdimm/ code. Try "git grep nvdimm_bus_register" to find drivers that use the nvdimm subsystem.

This use case is not "Persistent Memory". Persistent Memory is something you can map and make persistent with CPU instructions. Anything that requires a driver call is device driver managed "Shared Memory".

#26 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

Quoted 40 lines · Dan Williams
Quoted 30 lines · Stefan Hajnoczi
Quoted 20 lines · Pankaj Gupta
Quoted 11 lines
Quoted 4 lines

Are you saying do it as existing i.e ACPI pmem like interface? The reason we have created this new driver is exiting pmem driver does not define proper semantics for guest flushing requests.

At this point I'm caring about the Linux-internal interface, and for that it should be integrated into the nvdimm subsystem and not a block driver. How the host <-> guest interface looks is a different idea.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

Regarding block support of driver, we want to achieve DAX support to bypass guest page cache. Also, we want to utilize existing DAX capable file-system interfaces(e.g fsync) from userspace file API's to trigger the host side flush request.

Well, if you want to support XFS+DAX better don't make it a block devices, because I'll post patches soon to stop using the block device entirely for the DAX case.

Quoted 8 lines

o.k I will look at your patches once they are in mailing list. Thanks for the heads up.

If I am guessing it right, we don't need block device additional features for pmem? We can bypass block device features like blk device cache flush etc. Also, still we would be supporting ext4 & XFS filesystem with pmem?

If there is time to your patches can you please elaborate on this a bit.

Quoted 8 lines

I think the idea is that the nvdimm subsystem already adds block device semantics on top of the struct nvdimms that it manages. See drivers/nvdimm/blk.c.

So it would be cleaner to make virtio-pmem an nvdimm bus. This will eliminate the duplication between your driver and drivers/nvdimm/ code. Try "git grep nvdimm_bus_register" to find drivers that use the nvdimm subsystem.

Quoted 4 lines

This use case is not "Persistent Memory". Persistent Memory is something you can map and make persistent with CPU instructions. Anything that requires a driver call is device driver managed "Shared Memory".

Dan, in that case do you have ideas regarding Christoph Hellwig's comment that this driver should be integrated into the nvdimm subsystem instead of a new block driver?

Stefan

#27 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

In reply to Dan Williams
Quoted 4 lines · Dan Williams

This use case is not "Persistent Memory". Persistent Memory is something you can map and make persistent with CPU instructions. Anything that requires a driver call is device driver managed "Shared Memory".

How is this any different than the existing nvdimm_flush()? If you really care about the not driver thing it could easily be a write to a doorbell page or a hypercall, but in the end that's just semantics.

#28 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Dan Williams

This use case is not "Persistent Memory". Persistent Memory is something you can map and make persistent with CPU instructions. Anything that requires a driver call is device driver managed "Shared Memory".

How is this any different than the existing nvdimm_flush()? If you really care about the not driver thing it could easily be a write to a doorbell page or a hypercall, but in the end that's just semantics.

The difference is that nvdimm_flush() is not mandatory, and that the platform will automatically perform the same flush at power-fail. Applications should be able to assume that if they are using MAP_SYNC that no other coordination with the kernel or the hypervisor is necessary.

Advertising this as a generic Persistent Memory range to the guest means that the guest could theoretically use it with device-dax where there is no driver or filesystem sync interface. The hypervisor will be waiting for flush notifications and the guest will just issue cache flushes and sfence instructions. So, as far as I can see we need to differentiate this virtio-model from standard "Persistent Memory" to the guest and remove the possibility of guests/applications making the wrong assumption.

Non-ODP RDMA in a guest comes to mind...

#29 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

Quoted 6 lines · Dan Williams

The difference is that nvdimm_flush() is not mandatory, and that the platform will automatically perform the same flush at power-fail. Applications should be able to assume that if they are using MAP_SYNC that no other coordination with the kernel or the hypervisor is necessary.

Quoted 8 lines

Advertising this as a generic Persistent Memory range to the guest means that the guest could theoretically use it with device-dax where there is no driver or filesystem sync interface. The hypervisor will be waiting for flush notifications and the guest will just issue cache flushes and sfence instructions. So, as far as I can see we need to differentiate this virtio-model from standard "Persistent Memory" to the guest and remove the possibility of guests/applications making the wrong assumption.

So add a flag that it is not. We already have the nd_volatile type, that is special. For now only in Linux, but I think adding this type to the spec eventually would be very useful for efficiently exposing directly mappable device to VM guests.

#30 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

Quoted 16 lines · Christoph Hellwig
Quoted 6 lines · Dan Williams

The difference is that nvdimm_flush() is not mandatory, and that the platform will automatically perform the same flush at power-fail. Applications should be able to assume that if they are using MAP_SYNC that no other coordination with the kernel or the hypervisor is necessary.

Quoted 8 lines

Advertising this as a generic Persistent Memory range to the guest means that the guest could theoretically use it with device-dax where there is no driver or filesystem sync interface. The hypervisor will be waiting for flush notifications and the guest will just issue cache flushes and sfence instructions. So, as far as I can see we need to differentiate this virtio-model from standard "Persistent Memory" to the guest and remove the possibility of guests/applications making the wrong assumption.

Quoted 4 lines

So add a flag that it is not. We already have the nd_volatile type, that is special. For now only in Linux, but I think adding this type to the spec eventually would be very useful for efficiently exposing directly mappable device to VM guests.

Right, that's the same recommendation I gave.

    https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg08404.html

...so maybe I'm misunderstanding your concern? It sounds like we're on the same page.

#31 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

Quoted 6 lines · Dan Williams

Right, that's the same recommendation I gave.

    https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg08404.html

...so maybe I'm misunderstanding your concern? It sounds like we're on the same page.

Yes, the above is exactly what I think we should do it. And in many ways virtio seems overkill if we could just have a hypercall or doorbell page as the queueing infrastructure in virtio shouldn't really be needed.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#32 lore raw

Re: [Qemu-devel] [RFC 2/2] KVM: add virtio-pmem driver

Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Dan Williams

Right, that's the same recommendation I gave.

    https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg08404.html
Quoted 7 lines
Quoted 2 lines

...so maybe I'm misunderstanding your concern? It sounds like we're on the same page.

Yes, the above is exactly what I think we should do it. And in many ways virtio seems overkill if we could just have a hypercall or doorbell page as the queueing infrastructure in virtio shouldn't really be needed.

Ah ok, yes, get rid of the virtio-pmem driver and just make nvdimm_flush() do a hypercall based on region-type flag.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>