Skip to contentSkip to conversation

block-layer interposer

RFC PATCH New Hannes Reinecke 1 participant 3 messages Nov 19, 2020

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

v2 cover block-layer interposer

Hi all,

here's a combined version of the earlier patchset from Sergei, fixing some issues I've found during testing and including (some) feedback from Mike. So with this patchset I could do a

echo "0 33554432 linear /dev/sda 0" | dmsetup create sda-cloned

on a system with root-fs on /dev/sda2, and things would continue to run just like normal.

There are some things which might need to be improved:
- I've tried to remove the stub bio clone for the dm interposer as suggested by Mike, but that resulted in an endless recursion in submit_bio_noacct(). That needs more debugging, but I'm also not sure if we _can_ do it; the end_io callback might refer to bi_disk->driver_private, and then will get confused as we've reassigned the 'bi_disk' setting. Not sure if there are fixed rules for it, so for now I'll probably leave it.
- The interposer is 'magically' hooked to the first block device in the device-mapper table. What I really want is to have an explicit listing of the interposer in the output of things like 'dmsetup table' like

0 33554432 interposer /dev/dm-0
0 33554432 linear /dev/sda 0

and the first entry having the device number of /dev/sda. Plan was to have that automatically registered once you set up the original table, but then I'm not sure if that's the right way to go. Mike?
- The removal of blkdev_get() as an exported API made things awkward, as we now have to do a blkdev_get_by_dev(); if not we end up with an uninitalized blkcg and a resulting crash. We might be able to fix this up by moving bdget() into dm_blk_open(), and have md->bdev uninitalized otherwise. But again I'm not sure if that's the right way to go.

Anyway, comments and reviews are welcome.

Sergei Shtepa (2):
blk_interposer - Block Layer Interposer dm_interposer - blk_interposer for device-mapper

 block/blk-core.c          |  34 +++++++++++
 block/genhd.c             |  55 ++++++++++++++++++
 drivers/md/dm-table.c     |  59 +++++++++++++++++++
 drivers/md/dm.c           | 140 ++++++++++++++++++++++++++++++++++++++++++----
 drivers/md/dm.h           |   4 +-
 include/linux/blk_types.h |   6 +-
 include/linux/genhd.h     |  19 +++++++
 7 files changed, 303 insertions(+), 14 deletions(-)
Signature · 1 line
2.16.4
#1 lore raw

PATCH v2 1/2 blk_interposer - Block Layer Interposer

From: Sergei Shtepa <[email protected]>

The block layer interposer allows to intercept bio requests. This allows you to connect device mapper and other kernel modules to the block device stack on the fly.

---
 block/blk-core.c          | 34 +++++++++++++++++++++++++++++
 block/genhd.c             | 55 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/blk_types.h |  6 ++++--
 include/linux/genhd.h     | 19 ++++++++++++++++
 4 files changed, 112 insertions(+), 2 deletions(-)
block/blk-core.c +34 -0
index 2db8bda43b6e..130f0124d939 100644
@@ -1030,6 +1030,37 @@ static blk_qc_t __submit_bio_noacct_mq(struct bio *bio)
 	return ret;
 }
 
+static blk_qc_t __submit_bio_interposed(struct bio *bio)
+{
+	struct bio_list bio_list[2] = { };
+	blk_qc_t ret = BLK_QC_T_NONE;
+
+	current->bio_list = bio_list;
+	if (likely(bio_queue_enter(bio) == 0)) {
+		struct gendisk *disk = bio->bi_disk;
+
+		bio_set_flag(bio, BIO_INTERPOSED);
+		if (likely(blk_has_interposer(disk))) {
+			struct blk_interposer *ip = disk->interposer;
+			ret = ip->ip_submit_bio(ip, bio);
+
+			if (ret == BLK_QC_T_NONE)
+				blk_queue_exit(disk->queue);
+		} else {
+			/* interposer was removed */
+			bio_list_add(&current->bio_list[0], bio);
+			blk_queue_exit(bio->bi_disk->queue);
+		}
+	}
+	current->bio_list = NULL;
+
+	/* Resubmit remaining bios */
+	while ((bio = bio_list_pop(&bio_list[0])))
+		ret = submit_bio_noacct(bio);
+
+	return ret;
+}
+
 /**
  * submit_bio_noacct - re-submit a bio to the block device layer for I/O
  * @bio:  The bio describing the location in memory and on the device.
@@ -1055,6 +1086,9 @@ blk_qc_t submit_bio_noacct(struct bio *bio)
 		return BLK_QC_T_NONE;
 	}
 
+	if (blk_has_interposer(bio->bi_disk) &&
+	    !bio_flagged(bio, BIO_INTERPOSED))
+		return __submit_bio_interposed(bio);
 	if (!bio->bi_disk->fops->submit_bio)
 		return __submit_bio_noacct_mq(bio);
 	return __submit_bio_noacct(bio);
block/genhd.c +55 -0

58 lines not on this page · raw message

include/linux/blk_types.h +4 -2
index d9b69bbde5cc..996b803e5aa1 100644
@@ -207,7 +207,7 @@ struct bio {
 						 * top bits REQ_OP. Use
 						 * accessors.
 						 */
-	unsigned short		bi_flags;	/* status, etc and bvec pool number */
+	unsigned int		bi_flags;	/* status, etc and bvec pool number */
 	unsigned short		bi_ioprio;
 	unsigned short		bi_write_hint;
 	blk_status_t		bi_status;
@@ -284,6 +284,8 @@ enum {
 				 * of this bio. */
 	BIO_CGROUP_ACCT,	/* has been accounted to a cgroup */
 	BIO_TRACKED,		/* set if bio goes through the rq_qos path */
+	BIO_INTERPOSED,		/* bio has been interposed and can be moved to
+				 * a different disk */
 	BIO_FLAG_LAST
 };
 
@@ -302,7 +304,7 @@ enum {
  * freed.
  */
 #define BVEC_POOL_BITS		(3)
-#define BVEC_POOL_OFFSET	(16 - BVEC_POOL_BITS)
+#define BVEC_POOL_OFFSET	(32 - BVEC_POOL_BITS)
 #define BVEC_POOL_IDX(bio)	((bio)->bi_flags >> BVEC_POOL_OFFSET)
 #if (1<< BVEC_POOL_BITS) < (BVEC_POOL_NR+1)
 # error "BVEC_POOL_BITS is too small"
include/linux/genhd.h +19 -0
index 03da3f603d30..f58ab391deda 100644
@@ -164,6 +164,15 @@ struct blk_integrity {
 	unsigned char				tag_size;
 };
 
+struct blk_interposer;
+typedef blk_qc_t (*ip_submit_bio_t) (struct blk_interposer *ip, struct bio *bio);
+
+struct blk_interposer {
+	struct gendisk *ip_disk;
+	ip_submit_bio_t ip_submit_bio;
+	void *ip_private;
+};
+
 struct gendisk {
 	/* major, first_minor and minors are input parameters only,
 	 * don't use directly.  Use disk_devt() and disk_max_parts().
@@ -188,6 +197,7 @@ struct gendisk {
 
 	const struct block_device_operations *fops;
 	struct request_queue *queue;
+	struct blk_interposer *interposer;
 	void *private_data;
 
 	int flags;
@@ -409,4 +419,13 @@ static inline dev_t blk_lookup_devt(const char *name, int partno)
 }
 #endif /* CONFIG_BLOCK */
 
+/*
+ * Block device interposing
+ */
+#define blk_has_interposer(d) ((d)->interposer != NULL)
+#define blk_interposer_active(ip) ((ip)->ip_disk != NULL)
+
+int blk_interposer_attach(struct gendisk *, struct blk_interposer *);
+struct blk_interposer * blk_interposer_detach(struct gendisk *);
+
 #endif /* _LINUX_GENHD_H */
Signature · 1 line
2.16.4
#2 lore raw

PATCH v2 2/2 dm_interposer - blk_interposer for device-mapper

In reply to Hannes Reinecke

From: Sergei Shtepa <[email protected]>

Implement a block interposer for device-mapper to
attach to an existing block layer stack.

---
 drivers/md/dm-table.c |  59 +++++++++++++++++++++
 drivers/md/dm.c       | 140 ++++++++++++++++++++++++++++++++++++++++++++++----
 drivers/md/dm.h       |   4 +-
 3 files changed, 191 insertions(+), 12 deletions(-)
drivers/md/dm-table.c +59 -0
index ce543b761be7..56923d0795b2 100644
@@ -1809,6 +1809,65 @@ static bool dm_table_requires_stable_pages(struct dm_table *t)
 	return false;
 }
 
+static char *_dm_interposer_claim_ptr = "device-mapper interposer";
+
+static int device_activate_interposer(struct dm_target *ti,
+				      struct dm_dev *dev, sector_t start,
+				      sector_t len, void *data)
+{
+	struct blk_interposer *blk_ip = dev->bdev->bd_disk->interposer;
+	struct mapped_device *md = data;
+
+	if (!blk_ip)
+		return false;
+	if (md) {
+		struct block_device *bdev;
+
+		bdev = blkdev_get_by_dev(md->bdev->bd_dev,
+					     dev->mode | FMODE_EXCL,
+					     _dm_interposer_claim_ptr);
+		if (!bdev)
+			return false;
+		blk_ip->ip_private = md;
+	} else if (blk_ip->ip_private) {
+		md = blk_ip->ip_private;
+		blkdev_put(md->bdev, dev->mode | FMODE_EXCL);
+		blk_ip->ip_private = NULL;
+	}
+	return true;
+}
+
+bool dm_table_activate_interposer(struct dm_table *t, struct mapped_device *md)
+{
+	struct dm_target *ti;
+
+	if (t->num_targets) {
+		ti = t->targets;
+
+		if (!ti->type->iterate_devices ||
+		    !ti->type->iterate_devices(ti,
+				device_activate_interposer, md))
+			return false;
+		DMINFO("%s: activated interposer", dm_device_name(md));
+	}
+	return true;
+}
+
+bool dm_table_deactivate_interposer(struct dm_table *t)
+{
+	struct dm_target *ti;
+
+	if (t->num_targets) {
+		ti = t->targets;
+
+		if (!ti->type->iterate_devices ||
+		    !ti->type->iterate_devices(ti,
+				device_activate_interposer, NULL))
+			return false;
+	}
+	return true;
+}
+
 void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
 			       struct queue_limits *limits)
 {
drivers/md/dm.c +129 -11

185 lines not on this page · raw message

drivers/md/dm.h +3 -1
index fffe1e289c53..51b98e10e9bb 100644
@@ -75,7 +75,9 @@ bool dm_table_supports_dax(struct dm_table *t, iterate_devices_callout_fn fn,
 			   int *blocksize);
 int device_supports_dax(struct dm_target *ti, struct dm_dev *dev,
 			   sector_t start, sector_t len, void *data);
-
+bool dm_table_activate_interposer(struct dm_table *t,
+				  struct mapped_device *md);
+bool dm_table_deactivate_interposer(struct dm_table *t);
 void dm_lock_md_type(struct mapped_device *md);
 void dm_unlock_md_type(struct mapped_device *md);
 void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type);
Signature · 1 line
2.16.4