Skip to contentSkip to conversation

virtio-pmem: Asynchronous flush

RFC In review Pankaj Gupta 2 participants 12 messages Jan 11, 2022 – Feb 16, 2022

Subsystems (4)
lore
Share & tools
lore

Pankaj Gupta

author maintainer
#0 lore raw

v3 cover virtio-pmem: Asynchronous flush

Jeff reported preflush order issue with the existing implementation of virtio pmem preflush. Dan suggested[1] to implement asynchronous flush for virtio pmem using work queue as done in md/RAID. This patch series intends to solve the preflush ordering issue and makes the flush asynchronous for the submitting thread. Also, adds the flush coalscing logic.

Submitting this RFC v3 series for review. Thank You!

RFC v2 -> RFC v3
- Improve commit log message - patch1.
- Improve return error handling for Async flush.
- declare'INIT_WORK' only once.
- More testing and bug fix.

[1] https://marc.info/?l=linux-kernel&m=157446316409937&w=2

Pankaj Gupta (2):
virtio-pmem: Async virtio-pmem flush
pmem: enable pmem_submit_bio for asynchronous flush

 drivers/nvdimm/nd_virtio.c   | 74 +++++++++++++++++++++++++++---------
 drivers/nvdimm/pmem.c        | 15 ++++++--
 drivers/nvdimm/region_devs.c |  4 +-
 drivers/nvdimm/virtio_pmem.c | 10 +++++
 drivers/nvdimm/virtio_pmem.h | 16 ++++++++
 5 files changed, 98 insertions(+), 21 deletions(-)
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

RFC v3 1/2 virtio-pmem: Async virtio-pmem flush

Enable asynchronous flush for virtio pmem using work queue. Also, coalesce the flush requests when a flush is already in process. This functionality is copied from md/RAID code.

When a flush is already in process, new flush requests wait till previous flush completes in another context (work queue). For all the requests come between ongoing flush and new flush start time, only single flush executes, thus adhers to flush coalscing logic. This is important for maintaining the flush request order with request coalscing.

---
 drivers/nvdimm/nd_virtio.c   | 74 +++++++++++++++++++++++++++---------
 drivers/nvdimm/virtio_pmem.c | 10 +++++
 drivers/nvdimm/virtio_pmem.h | 16 ++++++++
 3 files changed, 83 insertions(+), 17 deletions(-)
drivers/nvdimm/nd_virtio.c +57 -17
index 10351d5b49fa..179ea7a73338 100644
@@ -100,26 +100,66 @@ static int virtio_pmem_flush(struct nd_region *nd_region)
 /* The asynchronous flush callback function */
 int async_pmem_flush(struct nd_region *nd_region, struct bio *bio)
 {
-	/*
-	 * Create child bio for asynchronous flush and chain with
-	 * parent bio. Otherwise directly call nd_region flush.
+	/* queue asynchronous flush and coalesce the flush requests */
+	struct virtio_device *vdev = nd_region->provider_data;
+	struct virtio_pmem *vpmem  = vdev->priv;
+	ktime_t req_start = ktime_get_boottime();
+	int ret = -EINPROGRESS;
+
+	spin_lock_irq(&vpmem->lock);
+	/* flush requests wait until ongoing flush completes,
+	 * hence coalescing all the pending requests.
 	 */
-	if (bio && bio->bi_iter.bi_sector != -1) {
-		struct bio *child = bio_alloc(GFP_ATOMIC, 0);
-
-		if (!child)
-			return -ENOMEM;
-		bio_copy_dev(child, bio);
-		child->bi_opf = REQ_PREFLUSH;
-		child->bi_iter.bi_sector = -1;
-		bio_chain(child, bio);
-		submit_bio(child);
-		return 0;
+	wait_event_lock_irq(vpmem->sb_wait,
+			    !vpmem->flush_bio ||
+			    ktime_before(req_start, vpmem->prev_flush_start),
+			    vpmem->lock);
+	/* new request after previous flush is completed */
+	if (ktime_after(req_start, vpmem->prev_flush_start)) {
+		WARN_ON(vpmem->flush_bio);
+		vpmem->flush_bio = bio;
+		bio = NULL;
+	}
+	spin_unlock_irq(&vpmem->lock);
+
+	if (!bio)
+		queue_work(vpmem->pmem_wq, &vpmem->flush_work);
+	else {
+	/* flush completed in other context while we waited */
+		if (bio && (bio->bi_opf & REQ_PREFLUSH))
+			bio->bi_opf &= ~REQ_PREFLUSH;
+		else if (bio && (bio->bi_opf & REQ_FUA))
+			bio->bi_opf &= ~REQ_FUA;
+
+		ret = vpmem->prev_flush_err;
 	}
-	if (virtio_pmem_flush(nd_region))
-		return -EIO;
 
-	return 0;
+	return ret;
 };
 EXPORT_SYMBOL_GPL(async_pmem_flush);
+
+void submit_async_flush(struct work_struct *ws)
+{
+	struct virtio_pmem *vpmem = container_of(ws, struct virtio_pmem, flush_work);
+	struct bio *bio = vpmem->flush_bio;
+
+	vpmem->start_flush = ktime_get_boottime();
+	vpmem->prev_flush_err = virtio_pmem_flush(vpmem->nd_region);
+	vpmem->prev_flush_start = vpmem->start_flush;
+	vpmem->flush_bio = NULL;
+	wake_up(&vpmem->sb_wait);
+
+	if (vpmem->prev_flush_err)
+		bio->bi_status = errno_to_blk_status(-EIO);
+
+	/* Submit parent bio only for PREFLUSH */
+	if (bio && (bio->bi_opf & REQ_PREFLUSH)) {
+		bio->bi_opf &= ~REQ_PREFLUSH;
+		submit_bio(bio);
+	} else if (bio && (bio->bi_opf & REQ_FUA)) {
+		bio->bi_opf &= ~REQ_FUA;
+		bio_endio(bio);
+	}
+}
+EXPORT_SYMBOL_GPL(submit_async_flush);
 MODULE_LICENSE("GPL");
drivers/nvdimm/virtio_pmem.c +10 -0
index 726c7354d465..75ed9b7ddea1 100644
@@ -24,6 +24,7 @@ static int init_vq(struct virtio_pmem *vpmem)
 		return PTR_ERR(vpmem->req_vq);
 
 	spin_lock_init(&vpmem->pmem_lock);
+	spin_lock_init(&vpmem->lock);
 	INIT_LIST_HEAD(&vpmem->req_list);
 
 	return 0;
@@ -57,7 +58,14 @@ static int virtio_pmem_probe(struct virtio_device *vdev)
 		dev_err(&vdev->dev, "failed to initialize virtio pmem vq's\n");
 		goto out_err;
 	}
+	vpmem->pmem_wq = alloc_workqueue("vpmem_wq", WQ_MEM_RECLAIM, 0);
+	if (!vpmem->pmem_wq) {
+		err = -ENOMEM;
+		goto out_err;
+	}
 
+	INIT_WORK(&vpmem->flush_work, submit_async_flush);
+	init_waitqueue_head(&vpmem->sb_wait);
 	virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
 			start, &vpmem->start);
 	virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
@@ -90,10 +98,12 @@ static int virtio_pmem_probe(struct virtio_device *vdev)
 		goto out_nd;
 	}
 	nd_region->provider_data = dev_to_virtio(nd_region->dev.parent->parent);
+	vpmem->nd_region = nd_region;
 	return 0;
 out_nd:
 	nvdimm_bus_unregister(vpmem->nvdimm_bus);
 out_vq:
+	destroy_workqueue(vpmem->pmem_wq);
 	vdev->config->del_vqs(vdev);
 out_err:
 	return err;
drivers/nvdimm/virtio_pmem.h +16 -0
index 0dddefe594c4..495dc20e1cdb 100644
@@ -35,9 +35,24 @@ struct virtio_pmem {
 	/* Virtio pmem request queue */
 	struct virtqueue *req_vq;
 
+	struct bio *flush_bio;
+	/* last_flush is when the last completed flush was started */
+	ktime_t prev_flush_start, start_flush;
+	int prev_flush_err;
+
+	/* work queue for deferred flush */
+	struct work_struct flush_work;
+	struct workqueue_struct *pmem_wq;
+
+	/* Synchronize flush wait queue data */
+	spinlock_t lock;
+	/* for waiting for previous flush to complete */
+	wait_queue_head_t sb_wait;
+
 	/* nvdimm bus registers virtio pmem device */
 	struct nvdimm_bus *nvdimm_bus;
 	struct nvdimm_bus_descriptor nd_desc;
+	struct nd_region *nd_region;
 
 	/* List to store deferred work if virtqueue is full */
 	struct list_head req_list;
@@ -52,4 +67,5 @@ struct virtio_pmem {
 
 void virtio_pmem_host_ack(struct virtqueue *vq);
 int async_pmem_flush(struct nd_region *nd_region, struct bio *bio);
+void submit_async_flush(struct work_struct *ws);
 #endif
Signature · 1 line
2.25.1
#2 lore raw

Re: [RFC v3 1/2] virtio-pmem: Async virtio-pmem flush

Quoted 9 lines · Pankaj Gupta

Enable asynchronous flush for virtio pmem using work queue. Also, coalesce the flush requests when a flush is already in process. This functionality is copied from md/RAID code.

When a flush is already in process, new flush requests wait till previous flush completes in another context (work queue). For all the requests come between ongoing flush and new flush start time, only single flush executes, thus adhers to flush coalscing logic. This is

s/adhers/adheres/

s/coalscing/coalescing/

Quoted 1 line

important for maintaining the flush request order with request coalscing.

s/coalscing/coalescing/

Quoted 16 lines
---
 drivers/nvdimm/nd_virtio.c   | 74 +++++++++++++++++++++++++++---------
 drivers/nvdimm/virtio_pmem.c | 10 +++++
 drivers/nvdimm/virtio_pmem.h | 16 ++++++++
 3 files changed, 83 insertions(+), 17 deletions(-)
drivers/nvdimm/nd_virtio.c +0 -0
index 10351d5b49fa..179ea7a73338 100644
@@ -100,26 +100,66 @@ static int virtio_pmem_flush(struct nd_region *nd_region)
 /* The asynchronous flush callback function */
 int async_pmem_flush(struct nd_region *nd_region, struct bio *bio)
 {

drivers/nvdimm/nd_virtio.c static int virtio_pmem_flush(struct nd_region *nd_region) · line 109

Quoted 10 lines
drivers/nvdimm/nd_virtio.c +7 -3
-       /*
-        * Create child bio for asynchronous flush and chain with
-        * parent bio. Otherwise directly call nd_region flush.
+       /* queue asynchronous flush and coalesce the flush requests */
+       struct virtio_device *vdev = nd_region->provider_data;
+       struct virtio_pmem *vpmem  = vdev->priv;
+       ktime_t req_start = ktime_get_boottime();
+       int ret = -EINPROGRESS;
+
+       spin_lock_irq(&vpmem->lock);

Why a new lock and not continue to use ->pmem_lock?

Have you tested this with CONFIG_PROVE_LOCKING?

Along those lines do you have a selftest that can be added to the kernel as well so that 0day or other bots could offer early warnings on regressions?

Quoted 36 lines
drivers/nvdimm/nd_virtio.c +24 -11
+       /* flush requests wait until ongoing flush completes,
+        * hence coalescing all the pending requests.
         */
-       if (bio && bio->bi_iter.bi_sector != -1) {
-               struct bio *child = bio_alloc(GFP_ATOMIC, 0);
-
-               if (!child)
-                       return -ENOMEM;
-               bio_copy_dev(child, bio);
-               child->bi_opf = REQ_PREFLUSH;
-               child->bi_iter.bi_sector = -1;
-               bio_chain(child, bio);
-               submit_bio(child);
-               return 0;
+       wait_event_lock_irq(vpmem->sb_wait,
+                           !vpmem->flush_bio ||
+                           ktime_before(req_start, vpmem->prev_flush_start),
+                           vpmem->lock);
+       /* new request after previous flush is completed */
+       if (ktime_after(req_start, vpmem->prev_flush_start)) {
+               WARN_ON(vpmem->flush_bio);
+               vpmem->flush_bio = bio;
+               bio = NULL;
+       }
+       spin_unlock_irq(&vpmem->lock);
+
+       if (!bio)
+               queue_work(vpmem->pmem_wq, &vpmem->flush_work);
+       else {
+       /* flush completed in other context while we waited */
+               if (bio && (bio->bi_opf & REQ_PREFLUSH))
+                       bio->bi_opf &= ~REQ_PREFLUSH;
+               else if (bio && (bio->bi_opf & REQ_FUA))
+                       bio->bi_opf &= ~REQ_FUA;
+
+               ret = vpmem->prev_flush_err;

drivers/nvdimm/nd_virtio.c static int virtio_pmem_flush(struct nd_region *nd_region) · line 141

Quoted 10 lines
drivers/nvdimm/nd_virtio.c +3 -3
        }
-       if (virtio_pmem_flush(nd_region))
-               return -EIO;
 
-       return 0;
+       return ret;
 };
 EXPORT_SYMBOL_GPL(async_pmem_flush);
+
+void submit_async_flush(struct work_struct *ws)

This name is too generic to be exported from drivers/nvdimm/nd_virtio.c

...it strikes me that there is little reason for nd_virtio and virtio_pmem to be separate modules. They are both enabled by the same Kconfig, so why not combine them into one module and drop the exports?

Quoted 103 lines
drivers/nvdimm/nd_virtio.c +23 -0
+{
+       struct virtio_pmem *vpmem = container_of(ws, struct virtio_pmem, flush_work);
+       struct bio *bio = vpmem->flush_bio;
+
+       vpmem->start_flush = ktime_get_boottime();
+       vpmem->prev_flush_err = virtio_pmem_flush(vpmem->nd_region);
+       vpmem->prev_flush_start = vpmem->start_flush;
+       vpmem->flush_bio = NULL;
+       wake_up(&vpmem->sb_wait);
+
+       if (vpmem->prev_flush_err)
+               bio->bi_status = errno_to_blk_status(-EIO);
+
+       /* Submit parent bio only for PREFLUSH */
+       if (bio && (bio->bi_opf & REQ_PREFLUSH)) {
+               bio->bi_opf &= ~REQ_PREFLUSH;
+               submit_bio(bio);
+       } else if (bio && (bio->bi_opf & REQ_FUA)) {
+               bio->bi_opf &= ~REQ_FUA;
+               bio_endio(bio);
+       }
+}
+EXPORT_SYMBOL_GPL(submit_async_flush);
 MODULE_LICENSE("GPL");
drivers/nvdimm/virtio_pmem.c +10 -0
index 726c7354d465..75ed9b7ddea1 100644
@@ -24,6 +24,7 @@ static int init_vq(struct virtio_pmem *vpmem)
                return PTR_ERR(vpmem->req_vq);
 
        spin_lock_init(&vpmem->pmem_lock);
+       spin_lock_init(&vpmem->lock);
        INIT_LIST_HEAD(&vpmem->req_list);
 
        return 0;
@@ -57,7 +58,14 @@ static int virtio_pmem_probe(struct virtio_device *vdev)
                dev_err(&vdev->dev, "failed to initialize virtio pmem vq's\n");
                goto out_err;
        }
+       vpmem->pmem_wq = alloc_workqueue("vpmem_wq", WQ_MEM_RECLAIM, 0);
+       if (!vpmem->pmem_wq) {
+               err = -ENOMEM;
+               goto out_err;
+       }
 
+       INIT_WORK(&vpmem->flush_work, submit_async_flush);
+       init_waitqueue_head(&vpmem->sb_wait);
        virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
                        start, &vpmem->start);
        virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
@@ -90,10 +98,12 @@ static int virtio_pmem_probe(struct virtio_device *vdev)
                goto out_nd;
        }
        nd_region->provider_data = dev_to_virtio(nd_region->dev.parent->parent);
+       vpmem->nd_region = nd_region;
        return 0;
 out_nd:
        nvdimm_bus_unregister(vpmem->nvdimm_bus);
 out_vq:
+       destroy_workqueue(vpmem->pmem_wq);
        vdev->config->del_vqs(vdev);
 out_err:
        return err;
drivers/nvdimm/virtio_pmem.h +16 -1
index 0dddefe594c4..495dc20e1cdb 100644
@@ -35,9 +35,24 @@ struct virtio_pmem {
        /* Virtio pmem request queue */
        struct virtqueue *req_vq;
 
+       struct bio *flush_bio;
+       /* last_flush is when the last completed flush was started */
+       ktime_t prev_flush_start, start_flush;
+       int prev_flush_err;
+
+       /* work queue for deferred flush */
+       struct work_struct flush_work;
+       struct workqueue_struct *pmem_wq;
+
+       /* Synchronize flush wait queue data */
+       spinlock_t lock;
+       /* for waiting for previous flush to complete */
+       wait_queue_head_t sb_wait;
+
        /* nvdimm bus registers virtio pmem device */
        struct nvdimm_bus *nvdimm_bus;
        struct nvdimm_bus_descriptor nd_desc;
+       struct nd_region *nd_region;
 
        /* List to store deferred work if virtqueue is full */
        struct list_head req_list;
@@ -52,4 +67,5 @@ struct virtio_pmem {
 
 void virtio_pmem_host_ack(struct virtqueue *vq);
 int async_pmem_flush(struct nd_region *nd_region, struct bio *bio);
+void submit_async_flush(struct work_struct *ws);
 #endif
--

2.25.1

author maintainer
#3 lore raw

Re: [RFC v3 1/2] virtio-pmem: Async virtio-pmem flush

Quoted 10 lines
Quoted 9 lines

Enable asynchronous flush for virtio pmem using work queue. Also, coalesce the flush requests when a flush is already in process. This functionality is copied from md/RAID code.

When a flush is already in process, new flush requests wait till previous flush completes in another context (work queue). For all the requests come between ongoing flush and new flush start time, only single flush executes, thus adhers to flush coalscing logic. This is

Quoted 7 lines

s/adhers/adheres/

s/coalscing/coalescing/

Quoted 1 line

important for maintaining the flush request order with request coalscing.

s/coalscing/coalescing/

o.k. Sorry for the spelling mistakes.

Quoted 21 lines
Quoted 21 lines
---
 drivers/nvdimm/nd_virtio.c   | 74 +++++++++++++++++++++++++++---------
 drivers/nvdimm/virtio_pmem.c | 10 +++++
 drivers/nvdimm/virtio_pmem.h | 16 ++++++++
 3 files changed, 83 insertions(+), 17 deletions(-)
drivers/nvdimm/nd_virtio.c +1 -3
index 10351d5b49fa..179ea7a73338 100644
@@ -100,26 +100,66 @@ static int virtio_pmem_flush(struct nd_region *nd_region)
 /* The asynchronous flush callback function */
 int async_pmem_flush(struct nd_region *nd_region, struct bio *bio)
 {
-       /*
-        * Create child bio for asynchronous flush and chain with
-        * parent bio. Otherwise directly call nd_region flush.
+       /* queue asynchronous flush and coalesce the flush requests */
Quoted 8 lines

drivers/nvdimm/nd_virtio.c static int virtio_pmem_flush(struct nd_region *nd_region) · line 109

Quoted 6 lines
drivers/nvdimm/nd_virtio.c +6 -0
+       struct virtio_device *vdev = nd_region->provider_data;
+       struct virtio_pmem *vpmem  = vdev->priv;
+       ktime_t req_start = ktime_get_boottime();
+       int ret = -EINPROGRESS;
+
+       spin_lock_irq(&vpmem->lock);

Why a new lock and not continue to use ->pmem_lock?

This spinlock is to protect entry in 'wait_event_lock_irq' and the Other spinlock is to protect virtio queue data.

Quoted 2 lines

Have you tested this with CONFIG_PROVE_LOCKING?

No, I only ran xfs tests and some of my unit test program. Will enable and test with CONFIG_PROVE_LOCKING.

Quoted 4 lines

Along those lines do you have a selftest that can be added to the kernel as well so that 0day or other bots could offer early warnings on regressions?

Will try to add one.

Thank you Dan for the feedback!

Best regards,
Pankaj

Quoted 50 lines
Quoted 37 lines
drivers/nvdimm/nd_virtio.c +24 -11
+       /* flush requests wait until ongoing flush completes,
+        * hence coalescing all the pending requests.
         */
-       if (bio && bio->bi_iter.bi_sector != -1) {
-               struct bio *child = bio_alloc(GFP_ATOMIC, 0);
-
-               if (!child)
-                       return -ENOMEM;
-               bio_copy_dev(child, bio);
-               child->bi_opf = REQ_PREFLUSH;
-               child->bi_iter.bi_sector = -1;
-               bio_chain(child, bio);
-               submit_bio(child);
-               return 0;
+       wait_event_lock_irq(vpmem->sb_wait,
+                           !vpmem->flush_bio ||
+                           ktime_before(req_start, vpmem->prev_flush_start),
+                           vpmem->lock);
+       /* new request after previous flush is completed */
+       if (ktime_after(req_start, vpmem->prev_flush_start)) {
+               WARN_ON(vpmem->flush_bio);
+               vpmem->flush_bio = bio;
+               bio = NULL;
+       }
+       spin_unlock_irq(&vpmem->lock);
+
+       if (!bio)
+               queue_work(vpmem->pmem_wq, &vpmem->flush_work);
+       else {
+       /* flush completed in other context while we waited */
+               if (bio && (bio->bi_opf & REQ_PREFLUSH))
+                       bio->bi_opf &= ~REQ_PREFLUSH;
+               else if (bio && (bio->bi_opf & REQ_FUA))
+                       bio->bi_opf &= ~REQ_FUA;
+
+               ret = vpmem->prev_flush_err;

drivers/nvdimm/nd_virtio.c static int virtio_pmem_flush(struct nd_region *nd_region) · line 141

Quoted 10 lines
drivers/nvdimm/nd_virtio.c +3 -3
        }
-       if (virtio_pmem_flush(nd_region))
-               return -EIO;
 
-       return 0;
+       return ret;
 };
 EXPORT_SYMBOL_GPL(async_pmem_flush);
+
+void submit_async_flush(struct work_struct *ws)

This name is too generic to be exported from drivers/nvdimm/nd_virtio.c

Quoted 3 lines

...it strikes me that there is little reason for nd_virtio and virtio_pmem to be separate modules. They are both enabled by the same Kconfig, so why not combine them into one module and drop the exports?

makes sense.

Quoted 104 lines
Quoted 104 lines
drivers/nvdimm/nd_virtio.c +23 -0
+{
+       struct virtio_pmem *vpmem = container_of(ws, struct virtio_pmem, flush_work);
+       struct bio *bio = vpmem->flush_bio;
+
+       vpmem->start_flush = ktime_get_boottime();
+       vpmem->prev_flush_err = virtio_pmem_flush(vpmem->nd_region);
+       vpmem->prev_flush_start = vpmem->start_flush;
+       vpmem->flush_bio = NULL;
+       wake_up(&vpmem->sb_wait);
+
+       if (vpmem->prev_flush_err)
+               bio->bi_status = errno_to_blk_status(-EIO);
+
+       /* Submit parent bio only for PREFLUSH */
+       if (bio && (bio->bi_opf & REQ_PREFLUSH)) {
+               bio->bi_opf &= ~REQ_PREFLUSH;
+               submit_bio(bio);
+       } else if (bio && (bio->bi_opf & REQ_FUA)) {
+               bio->bi_opf &= ~REQ_FUA;
+               bio_endio(bio);
+       }
+}
+EXPORT_SYMBOL_GPL(submit_async_flush);
 MODULE_LICENSE("GPL");
drivers/nvdimm/virtio_pmem.c +10 -0
index 726c7354d465..75ed9b7ddea1 100644
@@ -24,6 +24,7 @@ static int init_vq(struct virtio_pmem *vpmem)
                return PTR_ERR(vpmem->req_vq);
 
        spin_lock_init(&vpmem->pmem_lock);
+       spin_lock_init(&vpmem->lock);
        INIT_LIST_HEAD(&vpmem->req_list);
 
        return 0;
@@ -57,7 +58,14 @@ static int virtio_pmem_probe(struct virtio_device *vdev)
                dev_err(&vdev->dev, "failed to initialize virtio pmem vq's\n");
                goto out_err;
        }
+       vpmem->pmem_wq = alloc_workqueue("vpmem_wq", WQ_MEM_RECLAIM, 0);
+       if (!vpmem->pmem_wq) {
+               err = -ENOMEM;
+               goto out_err;
+       }
 
+       INIT_WORK(&vpmem->flush_work, submit_async_flush);
+       init_waitqueue_head(&vpmem->sb_wait);
        virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
                        start, &vpmem->start);
        virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
@@ -90,10 +98,12 @@ static int virtio_pmem_probe(struct virtio_device *vdev)
                goto out_nd;
        }
        nd_region->provider_data = dev_to_virtio(nd_region->dev.parent->parent);
+       vpmem->nd_region = nd_region;
        return 0;
 out_nd:
        nvdimm_bus_unregister(vpmem->nvdimm_bus);
 out_vq:
+       destroy_workqueue(vpmem->pmem_wq);
        vdev->config->del_vqs(vdev);
 out_err:
        return err;
drivers/nvdimm/virtio_pmem.h +16 -1
index 0dddefe594c4..495dc20e1cdb 100644
@@ -35,9 +35,24 @@ struct virtio_pmem {
        /* Virtio pmem request queue */
        struct virtqueue *req_vq;
 
+       struct bio *flush_bio;
+       /* last_flush is when the last completed flush was started */
+       ktime_t prev_flush_start, start_flush;
+       int prev_flush_err;
+
+       /* work queue for deferred flush */
+       struct work_struct flush_work;
+       struct workqueue_struct *pmem_wq;
+
+       /* Synchronize flush wait queue data */
+       spinlock_t lock;
+       /* for waiting for previous flush to complete */
+       wait_queue_head_t sb_wait;
+
        /* nvdimm bus registers virtio pmem device */
        struct nvdimm_bus *nvdimm_bus;
        struct nvdimm_bus_descriptor nd_desc;
+       struct nd_region *nd_region;
 
        /* List to store deferred work if virtqueue is full */
        struct list_head req_list;
@@ -52,4 +67,5 @@ struct virtio_pmem {
 
 void virtio_pmem_host_ack(struct virtqueue *vq);
 int async_pmem_flush(struct nd_region *nd_region, struct bio *bio);
+void submit_async_flush(struct work_struct *ws);
 #endif
--

2.25.1

#4 lore raw

Re: [RFC v3 1/2] virtio-pmem: Async virtio-pmem flush

Quoted 51 lines · Pankaj Gupta
Quoted 11 lines
Quoted 10 lines

Enable asynchronous flush for virtio pmem using work queue. Also, coalesce the flush requests when a flush is already in process. This functionality is copied from md/RAID code.

When a flush is already in process, new flush requests wait till previous flush completes in another context (work queue). For all the requests come between ongoing flush and new flush start time, only single flush executes, thus adhers to flush coalscing logic. This is

Quoted 7 lines

s/adhers/adheres/

s/coalscing/coalescing/

Quoted 1 line

important for maintaining the flush request order with request coalscing.

s/coalscing/coalescing/

o.k. Sorry for the spelling mistakes.

Quoted 21 lines
Quoted 21 lines
---
 drivers/nvdimm/nd_virtio.c   | 74 +++++++++++++++++++++++++++---------
 drivers/nvdimm/virtio_pmem.c | 10 +++++
 drivers/nvdimm/virtio_pmem.h | 16 ++++++++
 3 files changed, 83 insertions(+), 17 deletions(-)
drivers/nvdimm/nd_virtio.c +1 -3
index 10351d5b49fa..179ea7a73338 100644
@@ -100,26 +100,66 @@ static int virtio_pmem_flush(struct nd_region *nd_region)
 /* The asynchronous flush callback function */
 int async_pmem_flush(struct nd_region *nd_region, struct bio *bio)
 {
-       /*
-        * Create child bio for asynchronous flush and chain with
-        * parent bio. Otherwise directly call nd_region flush.
+       /* queue asynchronous flush and coalesce the flush requests */
Quoted 8 lines

drivers/nvdimm/nd_virtio.c static int virtio_pmem_flush(struct nd_region *nd_region) · line 109

Quoted 6 lines
drivers/nvdimm/nd_virtio.c +6 -0
+       struct virtio_device *vdev = nd_region->provider_data;
+       struct virtio_pmem *vpmem  = vdev->priv;
+       ktime_t req_start = ktime_get_boottime();
+       int ret = -EINPROGRESS;
+
+       spin_lock_irq(&vpmem->lock);

Why a new lock and not continue to use ->pmem_lock?

Quoted 2 lines

This spinlock is to protect entry in 'wait_event_lock_irq' and the Other spinlock is to protect virtio queue data.

Understood, but md shares the mddev->lock for both purposes, so I would ask that you either document what motivates the locking split, or just reuse the lock until a strong reason to split them arises.

author maintainer
#5 lore raw

Re: [RFC v3 1/2] virtio-pmem: Async virtio-pmem flush

Quoted 51 lines
Quoted 51 lines
Quoted 11 lines
Quoted 10 lines

Enable asynchronous flush for virtio pmem using work queue. Also, coalesce the flush requests when a flush is already in process. This functionality is copied from md/RAID code.

When a flush is already in process, new flush requests wait till previous flush completes in another context (work queue). For all the requests come between ongoing flush and new flush start time, only single flush executes, thus adhers to flush coalscing logic. This is

Quoted 7 lines

s/adhers/adheres/

s/coalscing/coalescing/

Quoted 1 line

important for maintaining the flush request order with request coalscing.

s/coalscing/coalescing/

o.k. Sorry for the spelling mistakes.

Quoted 29 lines
Quoted 17 lines
---
 drivers/nvdimm/nd_virtio.c   | 74 +++++++++++++++++++++++++++---------
 drivers/nvdimm/virtio_pmem.c | 10 +++++
 drivers/nvdimm/virtio_pmem.h | 16 ++++++++
 3 files changed, 83 insertions(+), 17 deletions(-)
drivers/nvdimm/nd_virtio.c +0 -0
index 10351d5b49fa..179ea7a73338 100644
@@ -100,26 +100,66 @@ static int virtio_pmem_flush(struct nd_region *nd_region)
 /* The asynchronous flush callback function */
 int async_pmem_flush(struct nd_region *nd_region, struct bio *bio)
 {

drivers/nvdimm/nd_virtio.c static int virtio_pmem_flush(struct nd_region *nd_region) · line 109

Quoted 10 lines
drivers/nvdimm/nd_virtio.c +7 -3
-       /*
-        * Create child bio for asynchronous flush and chain with
-        * parent bio. Otherwise directly call nd_region flush.
+       /* queue asynchronous flush and coalesce the flush requests */
+       struct virtio_device *vdev = nd_region->provider_data;
+       struct virtio_pmem *vpmem  = vdev->priv;
+       ktime_t req_start = ktime_get_boottime();
+       int ret = -EINPROGRESS;
+
+       spin_lock_irq(&vpmem->lock);

Why a new lock and not continue to use ->pmem_lock?

Quoted 6 lines
Quoted 2 lines

This spinlock is to protect entry in 'wait_event_lock_irq' and the Other spinlock is to protect virtio queue data.

Understood, but md shares the mddev->lock for both purposes, so I would ask that you either document what motivates the locking split, or just reuse the lock until a strong reason to split them arises.

O.k. Will check again if we could use same lock Or document it.

Thanks,
Pankaj

author maintainer
#6 lore raw

RFC v3 2/2 pmem: enable pmem_submit_bio for asynchronous flush

In reply to Pankaj Gupta

Return from "pmem_submit_bio" when asynchronous flush is still in progress in other context.

---
 drivers/nvdimm/pmem.c        | 15 ++++++++++++---
 drivers/nvdimm/region_devs.c |  4 +++-
 2 files changed, 15 insertions(+), 4 deletions(-)
drivers/nvdimm/pmem.c +12 -3
index fe7ece1534e1..f20e30277a68 100644
@@ -201,8 +201,12 @@ static void pmem_submit_bio(struct bio *bio)
 	struct pmem_device *pmem = bio->bi_bdev->bd_disk->private_data;
 	struct nd_region *nd_region = to_region(pmem);
 
-	if (bio->bi_opf & REQ_PREFLUSH)
+	if (bio->bi_opf & REQ_PREFLUSH) {
 		ret = nvdimm_flush(nd_region, bio);
+		/* asynchronous flush completes in other context */
+		if (ret == -EINPROGRESS)
+			return;
+	}
 
 	do_acct = blk_queue_io_stat(bio->bi_bdev->bd_disk->queue);
 	if (do_acct)
@@ -222,13 +226,18 @@ static void pmem_submit_bio(struct bio *bio)
 	if (do_acct)
 		bio_end_io_acct(bio, start);
 
-	if (bio->bi_opf & REQ_FUA)
+	if (bio->bi_opf & REQ_FUA) {
 		ret = nvdimm_flush(nd_region, bio);
+		/* asynchronous flush completes in other context */
+		if (ret == -EINPROGRESS)
+			return;
+	}
 
 	if (ret)
 		bio->bi_status = errno_to_blk_status(ret);
 
-	bio_endio(bio);
+	if (bio)
+		bio_endio(bio);
 }
 
 static int pmem_rw_page(struct block_device *bdev, sector_t sector,
drivers/nvdimm/region_devs.c +3 -1
index 9ccf3d608799..8512d2eaed4e 100644
@@ -1190,7 +1190,9 @@ int nvdimm_flush(struct nd_region *nd_region, struct bio *bio)
 	if (!nd_region->flush)
 		rc = generic_nvdimm_flush(nd_region);
 	else {
-		if (nd_region->flush(nd_region, bio))
+		rc = nd_region->flush(nd_region, bio);
+		/* ongoing flush in other context */
+		if (rc && rc != -EINPROGRESS)
 			rc = -EIO;
 	}
 
Signature · 1 line
2.25.1
#7 lore raw

Re: [RFC v3 2/2] pmem: enable pmem_submit_bio for asynchronous flush

Quoted 15 lines · Pankaj Gupta

Return from "pmem_submit_bio" when asynchronous flush is still in progress in other context.

---
 drivers/nvdimm/pmem.c        | 15 ++++++++++++---
 drivers/nvdimm/region_devs.c |  4 +++-
 2 files changed, 15 insertions(+), 4 deletions(-)
drivers/nvdimm/pmem.c +0 -0
index fe7ece1534e1..f20e30277a68 100644

drivers/nvdimm/pmem.c static void pmem_submit_bio(struct bio *bio) · line 206

Quoted 7 lines
drivers/nvdimm/pmem.c +2 -1
@@ -201,8 +201,12 @@ static void pmem_submit_bio(struct bio *bio)
        struct pmem_device *pmem = bio->bi_bdev->bd_disk->private_data;
        struct nd_region *nd_region = to_region(pmem);
 
-       if (bio->bi_opf & REQ_PREFLUSH)
+       if (bio->bi_opf & REQ_PREFLUSH) {
                ret = nvdimm_flush(nd_region, bio);
+               /* asynchronous flush completes in other context */

I think a negative error code is a confusing way to capture the case of "bio successfully coalesced to previously pending flush request. Perhaps reserve negative codes for failure, 0 for synchronously completed, and > 0 for coalesced flush request.

Quoted 32 lines
drivers/nvdimm/pmem.c +10 -2
+               if (ret == -EINPROGRESS)
+                       return;
+       }
 
        do_acct = blk_queue_io_stat(bio->bi_bdev->bd_disk->queue);
        if (do_acct)
@@ -222,13 +226,18 @@ static void pmem_submit_bio(struct bio *bio)
        if (do_acct)
                bio_end_io_acct(bio, start);
 
-       if (bio->bi_opf & REQ_FUA)
+       if (bio->bi_opf & REQ_FUA) {
                ret = nvdimm_flush(nd_region, bio);
+               /* asynchronous flush completes in other context */
+               if (ret == -EINPROGRESS)
+                       return;
+       }
 
        if (ret)
                bio->bi_status = errno_to_blk_status(ret);
 
-       bio_endio(bio);
+       if (bio)
+               bio_endio(bio);
 }
 
 static int pmem_rw_page(struct block_device *bdev, sector_t sector,
drivers/nvdimm/region_devs.c +0 -0
index 9ccf3d608799..8512d2eaed4e 100644

drivers/nvdimm/region_devs.c int nvdimm_flush(struct nd_region *nd_region, struct bio *bio) · line 1196

Quoted 8 lines
drivers/nvdimm/region_devs.c +3 -1
@@ -1190,7 +1190,9 @@ int nvdimm_flush(struct nd_region *nd_region, struct bio *bio)
        if (!nd_region->flush)
                rc = generic_nvdimm_flush(nd_region);
        else {
-               if (nd_region->flush(nd_region, bio))
+               rc = nd_region->flush(nd_region, bio);
+               /* ongoing flush in other context */
+               if (rc && rc != -EINPROGRESS)
                        rc = -EIO;

Why change this to -EIO vs just let the error code through untranslated?

Quoted 6 lines
drivers/nvdimm/region_devs.c +0 -1
        }
 
--

2.25.1

author maintainer
#8 lore raw

Re: [RFC v3 2/2] pmem: enable pmem_submit_bio for asynchronous flush

Quoted 18 lines
Quoted 18 lines

Return from "pmem_submit_bio" when asynchronous flush is still in progress in other context.

---
 drivers/nvdimm/pmem.c        | 15 ++++++++++++---
 drivers/nvdimm/region_devs.c |  4 +++-
 2 files changed, 15 insertions(+), 4 deletions(-)
drivers/nvdimm/pmem.c +0 -0
index fe7ece1534e1..f20e30277a68 100644
@@ -201,8 +201,12 @@ static void pmem_submit_bio(struct bio *bio)
        struct pmem_device *pmem = bio->bi_bdev->bd_disk->private_data;
        struct nd_region *nd_region = to_region(pmem);
 
Quoted 9 lines

drivers/nvdimm/pmem.c static void pmem_submit_bio(struct bio *bio) · line 206

Quoted 4 lines
drivers/nvdimm/pmem.c +2 -1
-       if (bio->bi_opf & REQ_PREFLUSH)
+       if (bio->bi_opf & REQ_PREFLUSH) {
                ret = nvdimm_flush(nd_region, bio);
+               /* asynchronous flush completes in other context */

I think a negative error code is a confusing way to capture the case of "bio successfully coalesced to previously pending flush request. Perhaps reserve negative codes for failure, 0 for synchronously completed, and > 0 for coalesced flush request.

Yes. I implemented this way previously, will revert it to. Thanks!

Quoted 27 lines
Quoted 27 lines
drivers/nvdimm/pmem.c +10 -2
+               if (ret == -EINPROGRESS)
+                       return;
+       }
 
        do_acct = blk_queue_io_stat(bio->bi_bdev->bd_disk->queue);
        if (do_acct)
@@ -222,13 +226,18 @@ static void pmem_submit_bio(struct bio *bio)
        if (do_acct)
                bio_end_io_acct(bio, start);
 
-       if (bio->bi_opf & REQ_FUA)
+       if (bio->bi_opf & REQ_FUA) {
                ret = nvdimm_flush(nd_region, bio);
+               /* asynchronous flush completes in other context */
+               if (ret == -EINPROGRESS)
+                       return;
+       }
 
        if (ret)
                bio->bi_status = errno_to_blk_status(ret);
 
-       bio_endio(bio);
+       if (bio)
+               bio_endio(bio);
 }
 
Quoted 16 lines
Quoted 6 lines
drivers/nvdimm/pmem.c +0 -0
 static int pmem_rw_page(struct block_device *bdev, sector_t sector,
drivers/nvdimm/region_devs.c +0 -0
index 9ccf3d608799..8512d2eaed4e 100644

drivers/nvdimm/region_devs.c int nvdimm_flush(struct nd_region *nd_region, struct bio *bio) · line 1196

Quoted 8 lines
drivers/nvdimm/region_devs.c +3 -1
@@ -1190,7 +1190,9 @@ int nvdimm_flush(struct nd_region *nd_region, struct bio *bio)
        if (!nd_region->flush)
                rc = generic_nvdimm_flush(nd_region);
        else {
-               if (nd_region->flush(nd_region, bio))
+               rc = nd_region->flush(nd_region, bio);
+               /* ongoing flush in other context */
+               if (rc && rc != -EINPROGRESS)
                        rc = -EIO;

Why change this to -EIO vs just let the error code through untranslated?

The reason was to be generic error code instead of returning host side return codes to guest?

Thanks!
Pankaj

Quoted 7 lines
Quoted 7 lines
drivers/nvdimm/region_devs.c +0 -1
        }
 
--

2.25.1

#9 lore raw

Re: [RFC v3 2/2] pmem: enable pmem_submit_bio for asynchronous flush

Quoted 75 lines · Pankaj Gupta
Quoted 19 lines
Quoted 19 lines

Return from "pmem_submit_bio" when asynchronous flush is still in progress in other context.

---
 drivers/nvdimm/pmem.c        | 15 ++++++++++++---
 drivers/nvdimm/region_devs.c |  4 +++-
 2 files changed, 15 insertions(+), 4 deletions(-)
drivers/nvdimm/pmem.c +0 -0
index fe7ece1534e1..f20e30277a68 100644
@@ -201,8 +201,12 @@ static void pmem_submit_bio(struct bio *bio)
        struct pmem_device *pmem = bio->bi_bdev->bd_disk->private_data;
        struct nd_region *nd_region = to_region(pmem);
 
Quoted 9 lines

drivers/nvdimm/pmem.c static void pmem_submit_bio(struct bio *bio) · line 206

Quoted 4 lines
drivers/nvdimm/pmem.c +2 -1
-       if (bio->bi_opf & REQ_PREFLUSH)
+       if (bio->bi_opf & REQ_PREFLUSH) {
                ret = nvdimm_flush(nd_region, bio);
+               /* asynchronous flush completes in other context */

I think a negative error code is a confusing way to capture the case of "bio successfully coalesced to previously pending flush request. Perhaps reserve negative codes for failure, 0 for synchronously completed, and > 0 for coalesced flush request.

Yes. I implemented this way previously, will revert it to. Thanks!

Quoted 27 lines
Quoted 27 lines
drivers/nvdimm/pmem.c +10 -2
+               if (ret == -EINPROGRESS)
+                       return;
+       }
 
        do_acct = blk_queue_io_stat(bio->bi_bdev->bd_disk->queue);
        if (do_acct)
@@ -222,13 +226,18 @@ static void pmem_submit_bio(struct bio *bio)
        if (do_acct)
                bio_end_io_acct(bio, start);
 
-       if (bio->bi_opf & REQ_FUA)
+       if (bio->bi_opf & REQ_FUA) {
                ret = nvdimm_flush(nd_region, bio);
+               /* asynchronous flush completes in other context */
+               if (ret == -EINPROGRESS)
+                       return;
+       }
 
        if (ret)
                bio->bi_status = errno_to_blk_status(ret);
 
-       bio_endio(bio);
+       if (bio)
+               bio_endio(bio);
 }
 
Quoted 16 lines
Quoted 6 lines
drivers/nvdimm/pmem.c +0 -0
 static int pmem_rw_page(struct block_device *bdev, sector_t sector,
drivers/nvdimm/region_devs.c +0 -0
index 9ccf3d608799..8512d2eaed4e 100644

drivers/nvdimm/region_devs.c int nvdimm_flush(struct nd_region *nd_region, struct bio *bio) · line 1196

Quoted 8 lines
drivers/nvdimm/region_devs.c +3 -1
@@ -1190,7 +1190,9 @@ int nvdimm_flush(struct nd_region *nd_region, struct bio *bio)
        if (!nd_region->flush)
                rc = generic_nvdimm_flush(nd_region);
        else {
-               if (nd_region->flush(nd_region, bio))
+               rc = nd_region->flush(nd_region, bio);
+               /* ongoing flush in other context */
+               if (rc && rc != -EINPROGRESS)
                        rc = -EIO;

Why change this to -EIO vs just let the error code through untranslated?

Quoted 2 lines

The reason was to be generic error code instead of returning host side return codes to guest?

Ok, maybe a comment to indicate the need to avoid exposing these error codes toa guest so someone does not ask the same question in the future?

author maintainer
#10 lore raw

Re: [RFC v3 2/2] pmem: enable pmem_submit_bio for asynchronous flush

Quoted 73 lines
Quoted 73 lines
Quoted 17 lines
Quoted 17 lines

Return from "pmem_submit_bio" when asynchronous flush is still in progress in other context.

---
 drivers/nvdimm/pmem.c        | 15 ++++++++++++---
 drivers/nvdimm/region_devs.c |  4 +++-
 2 files changed, 15 insertions(+), 4 deletions(-)
drivers/nvdimm/pmem.c +0 -0
index fe7ece1534e1..f20e30277a68 100644
@@ -201,8 +201,12 @@ static void pmem_submit_bio(struct bio *bio)
        struct pmem_device *pmem = bio->bi_bdev->bd_disk->private_data;
        struct nd_region *nd_region = to_region(pmem);
 
Quoted 9 lines

drivers/nvdimm/pmem.c static void pmem_submit_bio(struct bio *bio) · line 206

Quoted 4 lines
drivers/nvdimm/pmem.c +2 -1
-       if (bio->bi_opf & REQ_PREFLUSH)
+       if (bio->bi_opf & REQ_PREFLUSH) {
                ret = nvdimm_flush(nd_region, bio);
+               /* asynchronous flush completes in other context */

I think a negative error code is a confusing way to capture the case of "bio successfully coalesced to previously pending flush request. Perhaps reserve negative codes for failure, 0 for synchronously completed, and > 0 for coalesced flush request.

Yes. I implemented this way previously, will revert it to. Thanks!

Quoted 43 lines
Quoted 33 lines
drivers/nvdimm/pmem.c +10 -2
+               if (ret == -EINPROGRESS)
+                       return;
+       }
 
        do_acct = blk_queue_io_stat(bio->bi_bdev->bd_disk->queue);
        if (do_acct)
@@ -222,13 +226,18 @@ static void pmem_submit_bio(struct bio *bio)
        if (do_acct)
                bio_end_io_acct(bio, start);
 
-       if (bio->bi_opf & REQ_FUA)
+       if (bio->bi_opf & REQ_FUA) {
                ret = nvdimm_flush(nd_region, bio);
+               /* asynchronous flush completes in other context */
+               if (ret == -EINPROGRESS)
+                       return;
+       }
 
        if (ret)
                bio->bi_status = errno_to_blk_status(ret);
 
-       bio_endio(bio);
+       if (bio)
+               bio_endio(bio);
 }
 
 static int pmem_rw_page(struct block_device *bdev, sector_t sector,
drivers/nvdimm/region_devs.c +0 -0
index 9ccf3d608799..8512d2eaed4e 100644

drivers/nvdimm/region_devs.c int nvdimm_flush(struct nd_region *nd_region, struct bio *bio) · line 1196

Quoted 8 lines
drivers/nvdimm/region_devs.c +3 -1
@@ -1190,7 +1190,9 @@ int nvdimm_flush(struct nd_region *nd_region, struct bio *bio)
        if (!nd_region->flush)
                rc = generic_nvdimm_flush(nd_region);
        else {
-               if (nd_region->flush(nd_region, bio))
+               rc = nd_region->flush(nd_region, bio);
+               /* ongoing flush in other context */
+               if (rc && rc != -EINPROGRESS)
                        rc = -EIO;

Why change this to -EIO vs just let the error code through untranslated?

Quoted 6 lines
Quoted 2 lines

The reason was to be generic error code instead of returning host side return codes to guest?

Ok, maybe a comment to indicate the need to avoid exposing these error codes toa guest so someone does not ask the same question in the future?

Sure.

Thanks,
Pankaj

author maintainer
#11 lore raw
In reply to Pankaj Gupta

ping

Quoted 30 lines

Jeff reported preflush order issue with the existing implementation of virtio pmem preflush. Dan suggested[1] to implement asynchronous flush for virtio pmem using work queue as done in md/RAID. This patch series intends to solve the preflush ordering issue and makes the flush asynchronous for the submitting thread. Also, adds the flush coalscing logic.

Submitting this RFC v3 series for review. Thank You!

RFC v2 -> RFC v3
- Improve commit log message - patch1.
- Improve return error handling for Async flush.
- declare'INIT_WORK' only once.
- More testing and bug fix.

[1] https://marc.info/?l=linux-kernel&m=157446316409937&w=2

Pankaj Gupta (2):
virtio-pmem: Async virtio-pmem flush
pmem: enable pmem_submit_bio for asynchronous flush

 drivers/nvdimm/nd_virtio.c   | 74 +++++++++++++++++++++++++++---------
 drivers/nvdimm/pmem.c        | 15 ++++++--
 drivers/nvdimm/region_devs.c |  4 +-
 drivers/nvdimm/virtio_pmem.c | 10 +++++
 drivers/nvdimm/virtio_pmem.h | 16 ++++++++
 5 files changed, 98 insertions(+), 21 deletions(-)

--
2.25.1