Skip to contentSkip to conversation

dm: improve bio splitting at expense of requeue complexity

PATCH In review Mike Snitzer 2 participants 4 messages Jul 6, 2022 – Jul 7, 2022

Subsystems (3)
lore
Share & tools
lore

Mike Snitzer

author maintainer
#0 lore raw

v3 cover dm: improve bio splitting at expense of requeue complexity

This v3 moves all bio_rewind() code to drivers/md/dm-io-rewind.c for exclussive use by DM core's dm_io requeue support via dm_io_rewind().

It was determined that block core doesn't need to expose bio_rewind() for more general use. If/when that changes we can revisit, armed with the understanding of how DM's use of bio_rewind() has faired.

These changes are now staged for 5.20 inclussion via linux-dm.git

Ming Lei (2):
dm: add bio_rewind() API to DM core
dm: add two stage requeue mechanism

 drivers/md/Makefile       |   2 +-
 drivers/md/dm-core.h      |  13 +++-
 drivers/md/dm-io-rewind.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/md/dm.c           | 121 +++++++++++++++++++++++++--------
 4 files changed, 273 insertions(+), 29 deletions(-)
 create mode 100644 drivers/md/dm-io-rewind.c
Signature · 6 lines
2.15.0

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#1 lore raw

PATCH v3 1/2 dm: add bio_rewind() API to DM core

From: Ming Lei <[email protected]>

Commit 7759eb23fd98 ("block: remove bio_rewind_iter()") removed a similar API for the following reasons:

    ```
    It is pointed that bio_rewind_iter() is one very bad API[1]:

    1) bio size may not be restored after rewinding

    2) it causes some bogus change, such as 5151842b9d8732 (block: reset
    bi_iter.bi_done after splitting bio)

    3) rewinding really makes things complicated wrt. bio splitting

    4) unnecessary updating of .bi_done in fast path

    [1] https://marc.info/?t=153549924200005&r=1&w=2

    So this patch takes Kent's suggestion to restore one bio into its original
    state via saving bio iterator(struct bvec_iter) in bio_integrity_prep(),
    given now bio_rewind_iter() is only used by bio integrity code.
    ```

However, saving off a copy of the 32 bytes bio->bi_iter in case rewind needed isn't efficient because it bloats per-bio-data for what is an unlikely case. That suggestion also ignores the need to restore crypto and integrity info.

Add bio_rewind() API for a specific use-case that is much more narrow than the previous more generic rewind code that was reverted:

1) most bios have a fixed end sector since bio split is done from front of the bio, if driver just records how many sectors between current bio's start sector and the original bio's end sector, the original position can be restored. Keeping the original bio's end sector fixed is a _hard_ requirement for this bio_rewind() interface!

2) if a bio's end sector won't change (usually bio_trim() isn't called, or in the case of DM it preserves original bio), user can restore the original position by storing sector offset from the current ->bi_iter.bi_sector to bio's end sector; together with saving bio size, only 8 bytes is needed to restore to original bio.

3) DM's requeue use case: when BLK_STS_DM_REQUEUE happens, DM core needs to restore to an "original bio" which represents the current dm_io to be requeued (which may be a subset of the original bio). By storing the sector offset from the original bio's end sector and dm_io's size, bio_rewind() can restore such original bio. See commit 7dd76d1feec7 ("dm: improve bio splitting and associated IO accounting") for more details on how DM does this. Leveraging this, allows DM core to shift the need for bio cloning from bio-split time (during IO submission) to the less likely BLK_STS_DM_REQUEUE handling (after IO completes with that error).

4) Unlike the original rewind API, bio_rewind() doesn't add .bi_done to bvec_iter and there is no effect on the fast path.

Implement bio_rewind() by factoring out clear helpers that it calls:
bio_integrity_rewind, bio_crypt_rewind and bio_rewind_iter.

DM is able to ensure that bio_rewind() is used safely but, given the constraint that the bio's end must never change, other hypothetical future callers may not take the same care. So make bio_rewind() and all supporting code local to DM to avoid risk of hypothetical abuse.

---
 drivers/md/Makefile       |   2 +-
 drivers/md/dm-core.h      |   2 +
 drivers/md/dm-io-rewind.c | 143 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 146 insertions(+), 1 deletion(-)
 create mode 100644 drivers/md/dm-io-rewind.c
drivers/md/Makefile +1 -1
index 0454b0885b01..270f694850ec 100644
@@ -5,7 +5,7 @@
 
 dm-mod-y	+= dm.o dm-table.o dm-target.o dm-linear.o dm-stripe.o \
 		   dm-ioctl.o dm-io.o dm-kcopyd.o dm-sysfs.o dm-stats.o \
-		   dm-rq.o
+		   dm-rq.o dm-io-rewind.o
 dm-multipath-y	+= dm-path-selector.o dm-mpath.o
 dm-historical-service-time-y += dm-ps-historical-service-time.o
 dm-io-affinity-y += dm-ps-io-affinity.o
drivers/md/dm-core.h +2 -0
index 5d9afca0d105..5793a27b2118 100644
@@ -319,4 +319,6 @@ extern atomic_t dm_global_event_nr;
 extern wait_queue_head_t dm_global_eventq;
 void dm_issue_global_event(void);
 
+void bio_rewind(struct bio *bio, unsigned bytes);
+
 #endif
drivers/md/dm-io-rewind.c new file +143 -0

143 lines not on this page · raw message

Signature · 6 lines
2.15.0

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#2 lore raw

Re: [5.20 PATCH v3 1/2] dm: add bio_rewind() API to DM core

If this now lives in dm (which I'm not really sold on, but I guess it is the way forward..), can you please give the functions dm_ prefixes?

author maintainer
#3 lore raw

PATCH v3 2/2 dm: add two stage requeue mechanism

In reply to Mike Snitzer

From: Ming Lei <[email protected]>

Commit 61b6e2e5321d ("dm: fix BLK_STS_DM_REQUEUE handling when dm_io represents split bio") reverted DM core's bio splitting back to using bio_split()+bio_chain() because it was found that otherwise DM's BLK_STS_DM_REQUEUE would trigger a live-lock waiting for bio completion that would never occur.

Restore using bio_trim()+bio_inc_remaining(), like was done in commit 7dd76d1feec7 ("dm: improve bio splitting and associated IO accounting"), but this time with proper handling for the above scenario that is covered in more detail in the commit header for 61b6e2e5321d.

Solve this issue by adding a two staged dm_io requeue mechanism that uses the new bio_rewind() via dm_io_rewind():

1) requeue the dm_io into the requeue_list added to struct mapped_device, and schedule it via new added requeue work. This workqueue just clones the dm_io->orig_bio (which DM saves and ensures its end sector isn't modified). dm_io_rewind() uses the sectors and sectors_offset members of the dm_io that are recorded relative to the end of orig_bio: bio_rewind()+bio_trim() are then used to make that cloned bio reflect the subset of the original bio that is represented by the dm_io that is being requeued.

2) the 2nd stage requeue is same with original requeue, but io->orig_bio points to new cloned bio (which matches the requeued dm_io as described above).

This allows DM core to shift the need for bio cloning from bio-split time (during IO submission) to the less likely BLK_STS_DM_REQUEUE handling (after IO completes with that error).

---
 drivers/md/dm-core.h      |  13 ++++-
 drivers/md/dm-io-rewind.c |  25 +++++++++-
 drivers/md/dm.c           | 121 +++++++++++++++++++++++++++++++++++-----------
 3 files changed, 129 insertions(+), 30 deletions(-)
drivers/md/dm-core.h +11 -2
index 5793a27b2118..5a3fe5897b1e 100644
@@ -22,6 +22,8 @@
 
 #define DM_RESERVED_MAX_IOS		1024
 
+struct dm_io;
+
 struct dm_kobject_holder {
 	struct kobject kobj;
 	struct completion completion;
@@ -91,6 +93,14 @@ struct mapped_device {
 	spinlock_t deferred_lock;
 	struct bio_list deferred;
 
+	/*
+	 * requeue work context is needed for cloning one new bio
+	 * to represent the dm_io to be requeued, since each
+	 * dm_io may point to the original bio from FS.
+	 */
+	struct work_struct requeue_work;
+	struct dm_io *requeue_list;
+
 	void *interface_ptr;
 
 	/*
@@ -275,7 +285,6 @@ struct dm_io {
 	atomic_t io_count;
 	struct mapped_device *md;
 
-	struct bio *split_bio;
 	/* The three fields represent mapped part of original bio */
 	struct bio *orig_bio;
 	unsigned int sector_offset; /* offset to end of orig_bio */
@@ -319,6 +328,6 @@ extern atomic_t dm_global_event_nr;
 extern wait_queue_head_t dm_global_eventq;
 void dm_issue_global_event(void);
 
-void bio_rewind(struct bio *bio, unsigned bytes);
+void dm_io_rewind(struct dm_io *io, struct bio_set *bs);
 
 #endif
drivers/md/dm-io-rewind.c +24 -1
index fbeaa8a342ed..3ba7162f85fa 100644
@@ -131,7 +131,7 @@ static inline void bio_rewind_iter(const struct bio *bio,
  * rewinding from end of bio and restoring its original position.
  * Caller is also responsibile for restoring bio's size.
  */
-void bio_rewind(struct bio *bio, unsigned bytes)
+static void bio_rewind(struct bio *bio, unsigned bytes)
 {
 	if (bio_integrity(bio))
 		bio_integrity_rewind(bio, bytes);
@@ -141,3 +141,26 @@ void bio_rewind(struct bio *bio, unsigned bytes)
 
 	bio_rewind_iter(bio, &bio->bi_iter, bytes);
 }
+
+void dm_io_rewind(struct dm_io *io, struct bio_set *bs)
+{
+	struct bio *orig = io->orig_bio;
+	struct bio *new_orig = bio_alloc_clone(orig->bi_bdev, orig,
+					       GFP_NOIO, bs);
+	/*
+	 * bio_rewind can restore to previous position since the end
+	 * sector is fixed for original bio, but we still need to
+	 * restore bio's size manually (using io->sectors).
+	 */
+	bio_rewind(new_orig, ((io->sector_offset << 9) -
+			      orig->bi_iter.bi_size));
+	bio_trim(new_orig, 0, io->sectors);
+
+	bio_chain(new_orig, orig);
+	/*
+	 * __bi_remaining was increased (by dm_split_and_process_bio),
+	 * so must drop the one added in bio_chain.
+	 */
+	atomic_dec(&orig->__bi_remaining);
+	io->orig_bio = new_orig;
+}
drivers/md/dm.c +94 -27

198 lines not on this page · raw message

Signature · 6 lines
2.15.0

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel