Skip to contentSkip to conversation

dma mapping optimisations

PATCH Superseded Keith Busch 4 participants 19 messages Jul 26, 2022 – Jul 28, 2022

Subsystems (6)
VFS Block layer Library code NVMe +2 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover dma mapping optimisations

From: Keith Busch <[email protected]>

The typical journey a user address takes for a read or write to a block device undergoes various represenations for every IO. Each consumes memory and CPU cycles. When the backing storage is NVMe, the sequence looks something like the following:

__user void *
struct iov_iter
struct pages[]
struct bio_vec[]
struct scatterlist[]
__le64[]

Applications will often use the same buffer for many IO, though, so these per-IO transformations to reach the exact same hardware descriptor is unnecessary.

The io_uring interface already provides a way for users to register buffers to get to the 'struct bio_vec[]'. That still leaves the scatterlist needed for the repeated dma_map_sg(), then transform to nvme's PRP list format.

This series takes the registered buffers a step further. A block driver can implement a new .dma_map() callback to complete the to the hardware's DMA mapped address representation, and return a cookie so a user can reference it later for any given IO. When used, the block stack can skip significant amounts of code, improving CPU utilization, and, if not bandwidth limited, IOPs. The larger the IO, the more signficant the improvement.

The implementation is currently limited to mapping a registered buffer to a single block device.

Here's some perf profiling 128k random read tests demonstrating the CPU savings:

With premapped bvec:

--46.84%--blk_mq_submit_bio

            |
            |--31.67%--blk_mq_try_issue_directly
                       |
                        --31.57%--__blk_mq_try_issue_directly
                                  |
                                   --31.39%--nvme_queue_rq
                                             |
                                             |--25.35%--nvme_prep_rq.part.68

With premapped DMA:

--25.86%--blk_mq_submit_bio

            |
            |--12.95%--blk_mq_try_issue_directly
                       |
                        --12.84%--__blk_mq_try_issue_directly
                                  |
                                   --12.53%--nvme_queue_rq
                                             |
                                             |--5.01%--nvme_prep_rq.part.68

Keith Busch (5):
blk-mq: add ops to dma map bvec
iov_iter: introduce type for preregistered dma tags
block: add dma tag bio type
io_uring: add support for dma pre-mapping
nvme-pci: implement dma_map support

 block/bdev.c                  |  20 +++
 block/bio.c                   |  25 ++-
 block/blk-merge.c             |  18 +++
 drivers/nvme/host/pci.c       | 291 +++++++++++++++++++++++++++++++++-
 include/linux/bio.h           |  21 +--
 include/linux/blk-mq.h        |  25 +++
 include/linux/blk_types.h     |   6 +-
 include/linux/blkdev.h        |  16 ++
 include/linux/uio.h           |   9 ++
 include/uapi/linux/io_uring.h |  12 ++
 io_uring/io_uring.c           | 129 +++++++++++++++
 io_uring/net.c                |   2 +-
 io_uring/rsrc.c               |  13 +-
 io_uring/rsrc.h               |  16 +-
 io_uring/rw.c                 |   2 +-
 lib/iov_iter.c                |  25 ++-
 16 files changed, 600 insertions(+), 30 deletions(-)
Signature · 1 line
2.30.2
#6 lore raw

PATCH 4/5 io_uring: add support for dma pre-mapping

From: Keith Busch <[email protected]>

Provide a new register operation that can request to pre-map a known bvec to the driver of the requested file descriptor's specific implementation. If successful, io_uring will use the returned dma tag for future fixed buffer requests to the same file.

---
 include/uapi/linux/io_uring.h |  12 ++++
 io_uring/io_uring.c           | 129 ++++++++++++++++++++++++++++++++++
 io_uring/net.c                |   2 +-
 io_uring/rsrc.c               |  13 +++-
 io_uring/rsrc.h               |  16 ++++-
 io_uring/rw.c                 |   2 +-
 6 files changed, 166 insertions(+), 8 deletions(-)
include/uapi/linux/io_uring.h +12 -0
index 1463cfecb56b..daacbe899d1d 100644
@@ -485,6 +485,10 @@ enum {
 	IORING_REGISTER_NOTIFIERS		= 26,
 	IORING_UNREGISTER_NOTIFIERS		= 27,
 
+	/* dma map registered buffers */
+	IORING_REGISTER_MAP_BUFFERS		= 28,
+	IORING_REGISTER_UNMAP_BUFFERS		= 29,
+
 	/* this goes last */
 	IORING_REGISTER_LAST
 };
@@ -661,4 +665,12 @@ struct io_uring_recvmsg_out {
 	__u32 flags;
 };
 
+struct io_uring_map_buffers {
+	__s32	fd;
+	__s32	buf_start;
+	__s32	buf_end;
+	__u32	flags;
+	__u64	rsvd[2];
+};
+
 #endif
io_uring/io_uring.c +129 -0

141 lines not on this page · raw message

io_uring/net.c +1 -1
index 8276b9537194..68a996318959 100644
@@ -977,7 +977,7 @@ int io_sendzc(struct io_kiocb *req, unsigned int issue_flags)
 
 	if (zc->flags & IORING_RECVSEND_FIXED_BUF) {
 		ret = io_import_fixed(WRITE, &msg.msg_iter, req->imu,
-					(u64)(uintptr_t)zc->buf, zc->len);
+					(u64)(uintptr_t)zc->buf, zc->len, NULL);
 		if (unlikely(ret))
 				return ret;
 	} else {
io_uring/rsrc.c +10 -3
index 59704b9ac537..1a7a8dedbbd5 100644
@@ -148,6 +148,7 @@ static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slo
 			unpin_user_page(imu->bvec[i].bv_page);
 		if (imu->acct_pages)
 			io_unaccount_mem(ctx, imu->acct_pages);
+		io_dma_unmap(imu);
 		kvfree(imu);
 	}
 	*slot = NULL;
@@ -1285,6 +1286,7 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
 	imu->ubuf = (unsigned long) iov->iov_base;
 	imu->ubuf_end = imu->ubuf + iov->iov_len;
 	imu->nr_bvecs = nr_pages;
+	imu->dma_tag = NULL;
 	*pimu = imu;
 	ret = 0;
 done:
@@ -1359,9 +1361,8 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
 	return ret;
 }
 
-int io_import_fixed(int ddir, struct iov_iter *iter,
-			   struct io_mapped_ubuf *imu,
-			   u64 buf_addr, size_t len)
+int io_import_fixed(int ddir, struct iov_iter *iter, struct io_mapped_ubuf *imu,
+		    u64 buf_addr, size_t len, struct file *file)
 {
 	u64 buf_end;
 	size_t offset;
@@ -1379,6 +1380,12 @@ int io_import_fixed(int ddir, struct iov_iter *iter,
 	 * and advance us to the beginning.
 	 */
 	offset = buf_addr - imu->ubuf;
+	if (imu->dma_tag && file == imu->dma_file) {
+		unsigned long nr_segs = (buf_addr & (PAGE_SIZE - 1)) +
+					(len >> PAGE_SHIFT);
+		iov_iter_dma_tag(iter, ddir, imu->dma_tag, offset, nr_segs, len);
+		return 0;
+	}
 	iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, offset + len);
 
 	if (offset) {
io_uring/rsrc.h +13 -3
index f3a9a177941f..6e63b7a57b34 100644
@@ -50,6 +50,11 @@ struct io_mapped_ubuf {
 	u64		ubuf_end;
 	unsigned int	nr_bvecs;
 	unsigned long	acct_pages;
+	void		*dma_tag;
+	struct file	*dma_file;
+#ifdef CONFIG_BLOCK
+	struct block_device *bdev;
+#endif
 	struct bio_vec	bvec[];
 };
 
@@ -64,9 +69,14 @@ int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
 			 struct io_rsrc_data *data_to_kill);
 
-int io_import_fixed(int ddir, struct iov_iter *iter,
-			   struct io_mapped_ubuf *imu,
-			   u64 buf_addr, size_t len);
+int io_import_fixed(int ddir, struct iov_iter *iter, struct io_mapped_ubuf *imu,
+		    u64 buf_addr, size_t len, struct file *file);
+
+#ifdef CONFIG_BLOCK
+void io_dma_unmap(struct io_mapped_ubuf *imu);
+#else
+static inline void io_dma_unmap(struct io_mapped_ubuf *imu) {}
+#endif
 
 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
io_uring/rw.c +1 -1
index 2b784795103c..9e2164d09adb 100644
@@ -359,7 +359,7 @@ static struct iovec *__io_import_iovec(int ddir, struct io_kiocb *req,
 	ssize_t ret;
 
 	if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
-		ret = io_import_fixed(ddir, iter, req->imu, rw->addr, rw->len);
+		ret = io_import_fixed(ddir, iter, req->imu, rw->addr, rw->len, req->file);
 		if (ret)
 			return ERR_PTR(ret);
 		return NULL;
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/5 blk-mq: add ops to dma map bvec

In reply to Keith Busch

From: Keith Busch <[email protected]>

The same buffer may be used for many subsequent IO's. Instead of setting up the mapping per-IO, provide an interface that can allow a buffer to be premapped just once and referenced again later.

---
 block/bdev.c           | 20 ++++++++++++++++++++
 include/linux/blk-mq.h | 13 +++++++++++++
 include/linux/blkdev.h | 16 ++++++++++++++++
 3 files changed, 49 insertions(+)
block/bdev.c +20 -0
index ce05175e71ce..345176e12bb1 100644
@@ -1069,3 +1069,23 @@ void sync_bdevs(bool wait)
 	spin_unlock(&blockdev_superblock->s_inode_list_lock);
 	iput(old_inode);
 }
+
+#ifdef CONFIG_HAS_DMA
+void *block_dma_map(struct block_device *bdev, struct bio_vec *bvec,
+		       int nr_vecs)
+{
+	struct request_queue *q = bdev_get_queue(bdev);
+
+	if (q->mq_ops && q->mq_ops->dma_map)
+		return q->mq_ops->dma_map(q, bvec, nr_vecs);
+	return ERR_PTR(-EINVAL);
+}
+
+void block_dma_unmap(struct block_device *bdev, void *dma_tag)
+{
+	struct request_queue *q = bdev_get_queue(bdev);
+
+	if (q->mq_ops && q->mq_ops->dma_unmap)
+		return q->mq_ops->dma_unmap(q, dma_tag);
+}
+#endif
include/linux/blk-mq.h +13 -0
index effee1dc715a..e10aabb36c2c 100644
@@ -639,6 +639,19 @@ struct blk_mq_ops {
 	 */
 	void (*show_rq)(struct seq_file *m, struct request *rq);
 #endif
+
+#ifdef CONFIG_HAS_DMA
+	/**
+	 * @dma_map: Create a dma mapping. On success, returns an opaque cookie
+	 * that the can be referenced by the driver in future requests.
+	 */
+	void *(*dma_map)(struct request_queue *q, struct bio_vec *bvec, int nr_vecs);
+
+	/**
+	 * @dma_unmap: Tear down a previously created dma mapping.
+	 */
+	void (*dma_unmap)(struct request_queue *q, void *dma_tag);
+#endif
 };
 
 enum {
include/linux/blkdev.h +16 -0
index d04bdf549efa..47f7f6f430d9 100644
@@ -1526,4 +1526,20 @@ struct io_comp_batch {
 
 #define DEFINE_IO_COMP_BATCH(name)	struct io_comp_batch name = { }
 
+#ifdef CONFIG_HAS_DMA
+void *block_dma_map(struct block_device *bdev, struct bio_vec *bvec,
+		       int nr_vecs);
+void block_dma_unmap(struct block_device *bdev, void *dma_tag);
+#else
+static inline void *block_dma_map(struct block_device *bdev,
+				struct bio_vec *bvec, int nr_vecs)
+{
+	return ERR_PTR(-ENOTSUPP);
+}
+
+static inline void block_dma_unmap(struct block_device *bdev, void *dma_tag)
+{
+}
+#endif
+
 #endif /* _LINUX_BLKDEV_H */
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/5 iov_iter: introduce type for preregistered dma tags

In reply to Keith Busch

From: Keith Busch <[email protected]>

Introduce a new iov_iter type representing a pre-registered DMA address tag. The tag is an opaque cookie specific to the lower level driver that created it, and can be referenced at any arbitrary offset.

---
 include/linux/uio.h |  9 +++++++++
 lib/iov_iter.c      | 25 ++++++++++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)
include/linux/uio.h +9 -0
index 34ba4a731179..de8af68eacb3 100644
@@ -26,6 +26,7 @@ enum iter_type {
 	ITER_PIPE,
 	ITER_XARRAY,
 	ITER_DISCARD,
+	ITER_DMA_TAG,
 };
 
 struct iov_iter_state {
@@ -46,6 +47,7 @@ struct iov_iter {
 		const struct bio_vec *bvec;
 		struct xarray *xarray;
 		struct pipe_inode_info *pipe;
+		void *dma_tag;
 	};
 	union {
 		unsigned long nr_segs;
@@ -85,6 +87,11 @@ static inline bool iov_iter_is_bvec(const struct iov_iter *i)
 	return iov_iter_type(i) == ITER_BVEC;
 }
 
+static inline bool iov_iter_is_dma_tag(const struct iov_iter *i)
+{
+	return iov_iter_type(i) == ITER_DMA_TAG;
+}
+
 static inline bool iov_iter_is_pipe(const struct iov_iter *i)
 {
 	return iov_iter_type(i) == ITER_PIPE;
@@ -229,6 +236,8 @@ void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec
 			unsigned long nr_segs, size_t count);
 void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec,
 			unsigned long nr_segs, size_t count);
+void iov_iter_dma_tag(struct iov_iter *i, unsigned int direction, void *dma_tag,
+			unsigned int dma_offset, unsigned long nr_segs, size_t count);
 void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe,
 			size_t count);
 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count);
lib/iov_iter.c +22 -3
index 507e732ef7cf..e26cb0889820 100644
@@ -1077,6 +1077,9 @@ void iov_iter_advance(struct iov_iter *i, size_t size)
 		i->count -= size;
 	} else if (iov_iter_is_discard(i)) {
 		i->count -= size;
+	} else if (iov_iter_is_dma_tag(i)) {
+		i->iov_offset += size;
+		i->count -= size;
 	}
 }
 EXPORT_SYMBOL(iov_iter_advance);
@@ -1201,6 +1204,22 @@ void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
 }
 EXPORT_SYMBOL(iov_iter_bvec);
 
+void iov_iter_dma_tag(struct iov_iter *i, unsigned int direction,
+			void *dma_tag, unsigned int dma_offset,
+			unsigned long nr_segs, size_t count)
+{
+	WARN_ON(direction & ~(READ | WRITE));
+	*i = (struct iov_iter){
+		.iter_type = ITER_DMA_TAG,
+		.data_source = direction,
+		.nr_segs = nr_segs,
+		.dma_tag = dma_tag,
+		.iov_offset = dma_offset,
+		.count = count
+	};
+}
+EXPORT_SYMBOL(iov_iter_dma_tag);
+
 void iov_iter_pipe(struct iov_iter *i, unsigned int direction,
 			struct pipe_inode_info *pipe,
 			size_t count)
@@ -2124,8 +2143,8 @@ EXPORT_SYMBOL(import_single_range);
  */
 void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
 {
-	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) &&
-			 !iov_iter_is_kvec(i))
+	if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) &&
+			 !iov_iter_is_dma_tag(i)) && !iov_iter_is_kvec(i))
 		return;
 	i->iov_offset = state->iov_offset;
 	i->count = state->count;
@@ -2141,7 +2160,7 @@ void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
 	BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec));
 	if (iov_iter_is_bvec(i))
 		i->bvec -= state->nr_segs - i->nr_segs;
-	else
+	else if (!iov_iter_is_dma_tag(i))
 		i->iov -= state->nr_segs - i->nr_segs;
 	i->nr_segs = state->nr_segs;
 }
Signature · 1 line
2.30.2
#5 lore raw

PATCH 3/5 block: add dma tag bio type

In reply to Keith Busch

From: Keith Busch <[email protected]>

Premapped buffers don't require a generic bio_vec since these have already been dma mapped to the driver specific data structure. Repurpose the bi_io_vec with the driver specific tag as they are mutually exclusive, and provide all the setup and split helpers to support dma tags.

In order to use this, a driver must implement the .dma_map() callback. If the driver provides this callback, then it must be aware that any given bio may be using a dma_tag instead of a bio_vec.

Note, this isn't working with blk_integrity.

---
 block/bio.c               | 25 ++++++++++++++++++++++++-
 block/blk-merge.c         | 18 ++++++++++++++++++
 include/linux/bio.h       | 21 ++++++++++++---------
 include/linux/blk-mq.h    | 12 ++++++++++++
 include/linux/blk_types.h |  6 +++++-
 5 files changed, 71 insertions(+), 11 deletions(-)
block/bio.c +24 -1
index ce3bc3578ac4..472bdc4fd419 100644
@@ -229,7 +229,8 @@ static void bio_free(struct bio *bio)
 	WARN_ON_ONCE(!bs);
 
 	bio_uninit(bio);
-	bvec_free(&bs->bvec_pool, bio->bi_io_vec, bio->bi_max_vecs);
+	if (!bio_flagged(bio, BIO_DMA_TAGGED))
+		bvec_free(&bs->bvec_pool, bio->bi_io_vec, bio->bi_max_vecs);
 	mempool_free(p - bs->front_pad, &bs->bio_pool);
 }
 
@@ -762,6 +763,8 @@ static int __bio_clone(struct bio *bio, struct bio *bio_src, gfp_t gfp)
 	bio_set_flag(bio, BIO_CLONED);
 	if (bio_flagged(bio_src, BIO_THROTTLED))
 		bio_set_flag(bio, BIO_THROTTLED);
+	if (bio_flagged(bio_src, BIO_DMA_TAGGED))
+		bio_set_flag(bio, BIO_DMA_TAGGED);
 	bio->bi_ioprio = bio_src->bi_ioprio;
 	bio->bi_iter = bio_src->bi_iter;
 
@@ -1151,6 +1154,21 @@ void bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
 	bio_set_flag(bio, BIO_CLONED);
 }
 
+static void bio_iov_dma_tag_set(struct bio *bio, struct iov_iter *iter)
+{
+	size_t size = iov_iter_count(iter);
+
+	bio->bi_vcnt = iter->nr_segs;
+	bio->bi_dma_tag = iter->dma_tag;
+	bio->bi_iter.bi_bvec_done = iter->iov_offset;
+	bio->bi_iter.bi_size = size;
+	bio->bi_opf |= REQ_NOMERGE;
+	bio_set_flag(bio, BIO_NO_PAGE_REF);
+	bio_set_flag(bio, BIO_DMA_TAGGED);
+
+	iov_iter_advance(iter, bio->bi_iter.bi_size);
+}
+
 static int bio_iov_add_page(struct bio *bio, struct page *page,
 		unsigned int len, unsigned int offset)
 {
@@ -1287,6 +1305,11 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
 		return 0;
 	}
 
+	if (iov_iter_is_dma_tag(iter)) {
+		bio_iov_dma_tag_set(bio, iter);
+		return 0;
+	}
+
 	do {
 		ret = __bio_iov_iter_get_pages(bio, iter);
 	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
block/blk-merge.c +18 -0
index 4c8a699754c9..240eae7666d4 100644
@@ -276,6 +276,24 @@ static struct bio *blk_bio_segment_split(struct request_queue *q,
 	const unsigned max_bytes = get_max_io_size(q, bio) << 9;
 	const unsigned max_segs = queue_max_segments(q);
 
+	if (bio_flagged(bio, BIO_DMA_TAGGED)) {
+		int offset = offset_in_page(bio->bi_iter.bi_bvec_done);
+
+		nsegs = ALIGN(bio->bi_iter.bi_size + offset, PAGE_SIZE) >> PAGE_SHIFT;
+		if (bio->bi_iter.bi_size > max_bytes) {
+			bytes = max_bytes;
+			nsegs = (bytes + offset) >> PAGE_SHIFT;
+		} else if (nsegs > max_segs) {
+			nsegs = max_segs;
+			bytes = PAGE_SIZE * nsegs - offset;
+		} else {
+			*segs = nsegs;
+			return NULL;
+		}
+
+		goto split;
+	}
+
 	bio_for_each_bvec(bv, bio, iter) {
 		/*
 		 * If the queue doesn't support SG gaps and adding this
include/linux/bio.h +12 -9
index ca22b06700a9..649348bc03c2 100644
@@ -61,11 +61,17 @@ static inline bool bio_has_data(struct bio *bio)
 	return false;
 }
 
+static inline bool bio_flagged(const struct bio *bio, unsigned int bit)
+{
+	return (bio->bi_flags & (1U << bit)) != 0;
+}
+
 static inline bool bio_no_advance_iter(const struct bio *bio)
 {
 	return bio_op(bio) == REQ_OP_DISCARD ||
 	       bio_op(bio) == REQ_OP_SECURE_ERASE ||
-	       bio_op(bio) == REQ_OP_WRITE_ZEROES;
+	       bio_op(bio) == REQ_OP_WRITE_ZEROES ||
+	       bio_flagged(bio, BIO_DMA_TAGGED);
 }
 
 static inline void *bio_data(struct bio *bio)
@@ -98,9 +104,11 @@ static inline void bio_advance_iter(const struct bio *bio,
 {
 	iter->bi_sector += bytes >> 9;
 
-	if (bio_no_advance_iter(bio))
+	if (bio_no_advance_iter(bio)) {
 		iter->bi_size -= bytes;
-	else
+		if (bio_flagged(bio, BIO_DMA_TAGGED))
+			iter->bi_bvec_done += bytes;
+	} else
 		bvec_iter_advance(bio->bi_io_vec, iter, bytes);
 		/* TODO: It is reasonable to complete bio with error here. */
 }
@@ -225,11 +233,6 @@ static inline void bio_cnt_set(struct bio *bio, unsigned int count)
 	atomic_set(&bio->__bi_cnt, count);
 }
 
-static inline bool bio_flagged(struct bio *bio, unsigned int bit)
-{
-	return (bio->bi_flags & (1U << bit)) != 0;
-}
-
 static inline void bio_set_flag(struct bio *bio, unsigned int bit)
 {
 	bio->bi_flags |= (1U << bit);
@@ -447,7 +450,7 @@ static inline void bio_wouldblock_error(struct bio *bio)
  */
 static inline int bio_iov_vecs_to_alloc(struct iov_iter *iter, int max_segs)
 {
-	if (iov_iter_is_bvec(iter))
+	if (iov_iter_is_bvec(iter) || iov_iter_is_dma_tag(iter))
 		return 0;
 	return iov_iter_npages(iter, max_segs);
 }
include/linux/blk-mq.h +12 -0
index e10aabb36c2c..3eb045a9ba41 100644
@@ -1141,6 +1141,18 @@ static inline int blk_rq_map_sg(struct request_queue *q, struct request *rq,
 }
 void blk_dump_rq_flags(struct request *, char *);
 
+static inline void *blk_rq_dma_tag(struct request *rq)
+{
+	return rq->bio && bio_flagged(rq->bio, BIO_DMA_TAGGED) ?
+		rq->bio->bi_dma_tag : 0;
+}
+
+static inline size_t blk_rq_dma_offset(struct request *rq)
+{
+	return rq->bio && bio_flagged(rq->bio, BIO_DMA_TAGGED) ?
+		rq->bio->bi_iter.bi_bvec_done : 0;
+}
+
 #ifdef CONFIG_BLK_DEV_ZONED
 static inline unsigned int blk_rq_zone_no(struct request *rq)
 {
include/linux/blk_types.h +5 -1
index 1ef99790f6ed..ea6db439acbe 100644
@@ -299,7 +299,10 @@ struct bio {
 
 	atomic_t		__bi_cnt;	/* pin count */
 
-	struct bio_vec		*bi_io_vec;	/* the actual vec list */
+	union {
+		struct bio_vec		*bi_io_vec;	/* the actual vec list */
+		void			*bi_dma_tag;    /* driver specific tag */
+	};
 
 	struct bio_set		*bi_pool;
 
@@ -334,6 +337,7 @@ enum {
 	BIO_QOS_MERGED,		/* but went through rq_qos merge path */
 	BIO_REMAPPED,
 	BIO_ZONE_WRITE_LOCKED,	/* Owns a zoned device zone write lock */
+	BIO_DMA_TAGGED,		/* Using premmaped dma buffers */
 	BIO_FLAG_LAST
 };
 
Signature · 1 line
2.30.2
#18 lore raw

PATCH 5/5 nvme-pci: implement dma_map support

In reply to Keith Busch

From: Keith Busch <[email protected]>

Implement callbacks to convert a registered bio_vec to a prp list, and use this for each IO that uses the returned tag. This saves repeated IO conversions and dma mapping/unmapping. In many cases, the driver can skip per-IO pool allocations entirely, saving potentially signficant CPU cycles.

---
 drivers/nvme/host/pci.c | 291 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 283 insertions(+), 8 deletions(-)
drivers/nvme/host/pci.c +283 -8
index 644664098ae7..571d955eaef0 100644
@@ -110,6 +110,14 @@ struct nvme_queue;
 static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
 static bool __nvme_disable_io_queues(struct nvme_dev *dev, u8 opcode);
 
+struct nvme_dma_mapping {
+	int nr_pages;
+	u16 offset;
+	u8  rsvd[2];
+	dma_addr_t prp_dma_addr;
+	__le64 *prps;
+};
+
 /*
  * Represents an NVM Express device.  Each nvme_dev is a PCI function.
  */
@@ -544,6 +552,35 @@ static inline bool nvme_pci_use_sgls(struct nvme_dev *dev, struct request *req)
 	return true;
 }
 
+static void nvme_sync_dma(struct nvme_dev *dev, struct request *req)
+{
+	int index, offset, i, length, nprps;
+	struct nvme_dma_mapping *mapping;
+	bool needs_sync;
+
+	mapping = blk_rq_dma_tag(req);
+	offset = blk_rq_dma_offset(req) + mapping->offset;
+	index = offset >> NVME_CTRL_PAGE_SHIFT;
+	needs_sync = rq_data_dir(req) == READ &&
+		 dma_need_sync(dev->dev, le64_to_cpu(mapping->prps[index]));
+
+	if (!needs_sync)
+		return;
+
+	offset = offset & (NVME_CTRL_PAGE_SIZE - 1);
+	length = blk_rq_payload_bytes(req) - (NVME_CTRL_PAGE_SIZE - offset);
+	nprps = DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE);
+
+	dma_sync_single_for_cpu(dev->dev,
+		le64_to_cpu(mapping->prps[index++]),
+		NVME_CTRL_PAGE_SIZE - offset, DMA_FROM_DEVICE);
+	for (i = 1; i < nprps; i++) {
+		dma_sync_single_for_cpu(dev->dev,
+			le64_to_cpu(mapping->prps[index++]),
+			NVME_CTRL_PAGE_SIZE, DMA_FROM_DEVICE);
+	}
+}
+
 static void nvme_free_prps(struct nvme_dev *dev, struct request *req)
 {
 	const int last_prp = NVME_CTRL_PAGE_SIZE / sizeof(__le64) - 1;
@@ -576,6 +613,21 @@ static void nvme_free_sgls(struct nvme_dev *dev, struct request *req)
 	}
 }
 
+static void nvme_free_prp_chain(struct nvme_dev *dev, struct request *req,
+				struct nvme_iod *iod)
+{
+	if (iod->npages < 0)
+		return;
+
+	if (iod->npages == 0)
+		dma_pool_free(dev->prp_small_pool, nvme_pci_iod_list(req)[0],
+			      iod->first_dma);
+	else if (iod->use_sgl)
+		nvme_free_sgls(dev, req);
+	else
+		nvme_free_prps(dev, req);
+}
+
 static void nvme_unmap_sg(struct nvme_dev *dev, struct request *req)
 {
 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
@@ -595,18 +647,15 @@ static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
 		dma_unmap_page(dev->dev, iod->first_dma, iod->dma_len,
 			       rq_dma_dir(req));
 		return;
+	} else if (blk_rq_dma_tag(req)) {
+		nvme_sync_dma(dev, req);
+		nvme_free_prp_chain(dev, req, iod);
+		return;
 	}
 
 	WARN_ON_ONCE(!iod->nents);
-
 	nvme_unmap_sg(dev, req);
-	if (iod->npages == 0)
-		dma_pool_free(dev->prp_small_pool, nvme_pci_iod_list(req)[0],
-			      iod->first_dma);
-	else if (iod->use_sgl)
-		nvme_free_sgls(dev, req);
-	else
-		nvme_free_prps(dev, req);
+	nvme_free_prp_chain(dev, req, iod);
 	mempool_free(iod->sg, dev->iod_mempool);
 }
 
@@ -835,6 +884,122 @@ static blk_status_t nvme_setup_sgl_simple(struct nvme_dev *dev,
 	return BLK_STS_OK;
 }
 
+static blk_status_t nvme_premapped(struct nvme_dev *dev, struct request *req,
+				   struct nvme_rw_command *cmnd,
+				   struct nvme_iod *iod)
+{
+	static const int last_prp = NVME_CTRL_PAGE_SIZE / sizeof(__le64) - 1;
+	dma_addr_t prp_list_start, prp_list_end, prp_dma;
+	int index, offset, i, length, nprps, nprps_left;
+	void **list = nvme_pci_iod_list(req);
+	struct nvme_dma_mapping *mapping;
+	struct dma_pool *pool;
+	__le64 *prp_list;
+	bool needs_sync;
+
+	mapping = blk_rq_dma_tag(req);
+	offset = blk_rq_dma_offset(req) + mapping->offset;
+	index = offset >> NVME_CTRL_PAGE_SHIFT;
+	offset = offset & (NVME_CTRL_PAGE_SIZE - 1);
+	needs_sync = rq_data_dir(req) == WRITE &&
+		 dma_need_sync(dev->dev, le64_to_cpu(mapping->prps[index]));
+
+	/*
+	 * XXX: For PAGE_SIZE > NVME_CTRL_PAGE_SIZE, is it faster to save the
+	 * PRP list implementation and sync multiple partial pages, more
+	 * efficient to sync PAGE_SIZE and build the PRP list per-IO from a
+	 * host PAGE_SIZE representation, or cleverly sync physically
+	 * contiguous regions?
+	 */
+	if (needs_sync) {
+		dma_sync_single_for_device(dev->dev,
+			le64_to_cpu(mapping->prps[index]),
+			NVME_CTRL_PAGE_SIZE - offset, DMA_TO_DEVICE);
+	}
+
+	length = blk_rq_payload_bytes(req) - (NVME_CTRL_PAGE_SIZE - offset);
+	cmnd->dptr.prp1 = cpu_to_le64(le64_to_cpu(mapping->prps[index++]) + offset);
+
+	if (length <= 0)
+		return BLK_STS_OK;
+
+	if (length <= NVME_CTRL_PAGE_SIZE) {
+		if (needs_sync)
+			dma_sync_single_for_device(dev->dev,
+				le64_to_cpu(mapping->prps[index]),
+				NVME_CTRL_PAGE_SIZE, DMA_TO_DEVICE);
+		cmnd->dptr.prp2 = mapping->prps[index];
+		return BLK_STS_OK;
+	}
+
+	nprps = DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE);
+	prp_list_start = mapping->prp_dma_addr + 8 * index;
+	prp_list_end = prp_list_start + 8 * nprps;
+
+	/* Optimization when remaining list fits in one nvme page */
+	if ((prp_list_start >> NVME_CTRL_PAGE_SHIFT) ==
+	    (prp_list_end >> NVME_CTRL_PAGE_SHIFT)) {
+		cmnd->dptr.prp2 = cpu_to_le64(prp_list_start);
+		goto sync;
+	}
+
+	if (nprps <= (256 / 8)) {
+		pool = dev->prp_small_pool;
+		iod->npages = 0;
+	} else {
+		pool = dev->prp_page_pool;
+		iod->npages = 1;
+	}
+
+	prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
+	if (!prp_list) {
+		iod->npages = -1;
+		return BLK_STS_RESOURCE;
+	}
+
+	list[0] = prp_list;
+	iod->first_dma = prp_dma;
+	i = 0;
+	for (;;) {
+		dma_addr_t next_prp_dma;
+		__le64 *next_prp_list;
+
+		if (nprps_left <= last_prp + 1) {
+			memcpy(prp_list, &mapping->prps[index], nprps_left * 8);
+			break;
+		}
+
+		memcpy(prp_list, &mapping->prps[index],
+		       NVME_CTRL_PAGE_SIZE - 8);
+		nprps_left -= last_prp;
+		index += last_prp;
+
+		next_prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &next_prp_dma);
+		if (!next_prp_list)
+			goto free_prps;
+
+		prp_list[last_prp] = cpu_to_le64(next_prp_dma);
+		prp_list = next_prp_list;
+		prp_dma = next_prp_dma;
+		list[iod->npages++] = prp_list;
+	}
+	cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma);
+
+sync:
+	if (!needs_sync)
+		return BLK_STS_OK;
+
+	for (i = 0; i < nprps; i++)
+		dma_sync_single_for_device(dev->dev,
+			le64_to_cpu(mapping->prps[index++]),
+			NVME_CTRL_PAGE_SIZE, DMA_TO_DEVICE);
+	return BLK_STS_OK;
+
+free_prps:
+	nvme_free_prps(dev, req);
+	return BLK_STS_RESOURCE;
+}
+
 static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
 		struct nvme_command *cmnd)
 {
@@ -842,6 +1007,12 @@ static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
 	blk_status_t ret = BLK_STS_RESOURCE;
 	int nr_mapped;
 
+	if (blk_rq_dma_tag(req)) {
+		iod->dma_len = 0;
+		iod->use_sgl = false;
+		return nvme_premapped(dev, req, &cmnd->rw, iod);
+	}
+
 	if (blk_rq_nr_phys_segments(req) == 1) {
 		struct bio_vec bv = req_bvec(req);
 
@@ -1732,6 +1903,106 @@ static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled)
 	return result;
 }
 
+#ifdef CONFIG_HAS_DMA
+/*
+ * Important: bvec must be describing a virtually contiguous buffer.
+ */
+static void *nvme_pci_dma_map(struct request_queue *q,
+			       struct bio_vec *bvec, int nr_vecs)
+{
+	const int nvme_pages = 1 << (PAGE_SIZE - NVME_CTRL_PAGE_SIZE);
+	struct nvme_ns *ns = q->queuedata;
+	struct nvme_dev *dev = to_nvme_dev(ns->ctrl);
+	struct nvme_dma_mapping *mapping;
+	int i, j, k, size, ret = -ENOMEM;
+
+	if (!nr_vecs)
+		return ERR_PTR(-EINVAL);
+
+	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
+	if (!mapping)
+		return ERR_PTR(-ENOMEM);
+
+	mapping->nr_pages = nr_vecs * nvme_pages;
+	size = sizeof(*mapping->prps) * mapping->nr_pages;
+	mapping->prps = dma_alloc_coherent(dev->dev, size,
+				&mapping->prp_dma_addr, GFP_KERNEL);
+	if (!mapping->prps)
+		goto free_mapping;
+
+	for (i = 0, k = 0; i < nr_vecs; i++) {
+		struct bio_vec *bv = bvec + i;
+		int pages_per = nvme_pages;
+		dma_addr_t dma_addr;
+
+		if (i == 0) {
+			mapping->offset = bv->bv_offset;
+			pages_per -= mapping->offset >> NVME_CTRL_PAGE_SHIFT;
+		} else if (bv->bv_offset) {
+			ret = -EINVAL;
+			goto err;
+		}
+
+		if (bv->bv_offset + bv->bv_len != PAGE_SIZE &&
+		    i < nr_vecs - 1) {
+			ret = -EINVAL;
+			goto err;
+		}
+
+		dma_addr = dma_map_bvec(dev->dev, bv, 0, 0);
+		if (dma_mapping_error(dev->dev, dma_addr)) {
+			ret = -EIO;
+			goto err;
+		}
+
+		if (i == 0)
+			dma_addr -= mapping->offset;
+
+		for (j = 0; j < nvme_pages; j++)
+			mapping->prps[k++] = cpu_to_le64(dma_addr +
+						j * NVME_CTRL_PAGE_SIZE);
+	}
+
+	get_device(dev->dev);
+	return mapping;
+
+err:
+	for (i = 0; i < k; i += nvme_pages) {
+		__u64 dma_addr = le64_to_cpu(mapping->prps[i]);
+
+		dma_unmap_page(dev->dev, dma_addr,
+			       PAGE_SIZE - offset_in_page(dma_addr), 0);
+	}
+
+	dma_free_coherent(dev->dev, size, (void *)mapping->prps,
+			  mapping->prp_dma_addr);
+free_mapping:
+	kfree(mapping);
+	return ERR_PTR(ret);
+}
+
+static void nvme_pci_dma_unmap(struct request_queue *q, void *dma_tag)
+{
+	const int nvme_pages = 1 << (PAGE_SIZE - NVME_CTRL_PAGE_SIZE);
+	struct nvme_ns *ns = q->queuedata;
+	struct nvme_dev *dev = to_nvme_dev(ns->ctrl);
+	struct nvme_dma_mapping *mapping = dma_tag;
+	int i;
+
+	for (i = 0; i < mapping->nr_pages; i += nvme_pages) {
+		__u64 dma_addr = le64_to_cpu(mapping->prps[i]);
+
+		dma_unmap_page(dev->dev, dma_addr,
+			       PAGE_SIZE - offset_in_page(dma_addr), 0);
+	}
+
+	dma_free_coherent(dev->dev, mapping->nr_pages * sizeof(*mapping->prps),
+			  (void *)mapping->prps, mapping->prp_dma_addr);
+	kfree(mapping);
+	put_device(dev->dev);
+}
+#endif
+
 static const struct blk_mq_ops nvme_mq_admin_ops = {
 	.queue_rq	= nvme_queue_rq,
 	.complete	= nvme_pci_complete_rq,
@@ -1750,6 +2021,10 @@ static const struct blk_mq_ops nvme_mq_ops = {
 	.map_queues	= nvme_pci_map_queues,
 	.timeout	= nvme_timeout,
 	.poll		= nvme_poll,
+#ifdef CONFIG_HAS_DMA
+	.dma_map	= nvme_pci_dma_map,
+	.dma_unmap	= nvme_pci_dma_unmap,
+#endif
 };
 
 static void nvme_dev_remove_admin(struct nvme_dev *dev)
Signature · 1 line
2.30.2
maintainer
#3 lore raw

Re: [PATCH 2/5] iov_iter: introduce type for preregistered dma tags

In reply to Keith Busch

lib/iov_iter.c void iov_iter_bvec(struct iov_iter *i, unsigned int direction, · line 1215

Quoted 9 lines · Keith Busch
lib/iov_iter.c +9 -0
+void iov_iter_dma_tag(struct iov_iter *i, unsigned int direction,
+			void *dma_tag, unsigned int dma_offset,
+			unsigned long nr_segs, size_t count)
+{
+	WARN_ON(direction & ~(READ | WRITE));
+	*i = (struct iov_iter){
+		.iter_type = ITER_DMA_TAG,
+		.data_source = direction,
+		.nr_segs = nr_segs,

Could you can that cargo-culting? Just what the hell is nr_segs here?

maintainer
#7 lore raw

Re: [PATCH 4/5] io_uring: add support for dma pre-mapping

In reply to Keith Busch

io_uring/io_uring.c static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, · line 3778

Quoted 4 lines · Keith Busch
io_uring/io_uring.c +4 -0
+	if (S_ISBLK(file_inode(file)->i_mode))
+		bdev = I_BDEV(file->f_mapping->host);
+	else if (S_ISREG(file_inode(file)->i_mode))
+		bdev = file->f_inode->i_sb->s_bdev;

*blink*

Just what's the intended use of the second case here?

maintainer
#4 lore raw

Re: [PATCH 2/5] iov_iter: introduce type for preregistered dma tags

In reply to Al Viro
Quoted 14 lines · Al Viro

lib/iov_iter.c void iov_iter_bvec(struct iov_iter *i, unsigned int direction, · line 1215

Quoted 9 lines · Keith Busch
lib/iov_iter.c +9 -0
+void iov_iter_dma_tag(struct iov_iter *i, unsigned int direction,
+			void *dma_tag, unsigned int dma_offset,
+			unsigned long nr_segs, size_t count)
+{
+	WARN_ON(direction & ~(READ | WRITE));
+	*i = (struct iov_iter){
+		.iter_type = ITER_DMA_TAG,
+		.data_source = direction,
+		.nr_segs = nr_segs,

Could you can that cargo-culting? Just what the hell is nr_segs here?

Thanks for the catch. Setting nr_segs here is useless carry-over from an earlier version when I thought it would be used to build the request. Turns out it's not used at all.

maintainer
#8 lore raw

Re: [PATCH 4/5] io_uring: add support for dma pre-mapping

In reply to Al Viro
Quoted 10 lines · Al Viro

io_uring/io_uring.c static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, · line 3778

Quoted 4 lines · Keith Busch
io_uring/io_uring.c +4 -0
+	if (S_ISBLK(file_inode(file)->i_mode))
+		bdev = I_BDEV(file->f_mapping->host);
+	else if (S_ISREG(file_inode(file)->i_mode))
+		bdev = file->f_inode->i_sb->s_bdev;

*blink*

Just what's the intended use of the second case here?

??

The use case is same as the first's: dma map the user addresses to the backing storage. There's two cases here because getting the block_device for a regular filesystem file is different than a raw block device.

maintainer
#9 lore raw

Re: [PATCH 4/5] io_uring: add support for dma pre-mapping

Quoted 10 lines · Keith Busch
Quoted 9 lines · Al Viro

io_uring/io_uring.c static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, · line 3778

Quoted 4 lines · Keith Busch
io_uring/io_uring.c +4 -0
+	if (S_ISBLK(file_inode(file)->i_mode))
+		bdev = I_BDEV(file->f_mapping->host);
+	else if (S_ISREG(file_inode(file)->i_mode))
+		bdev = file->f_inode->i_sb->s_bdev;

*blink*

Quoted 7 lines
Quoted 1 line

Just what's the intended use of the second case here?

??

The use case is same as the first's: dma map the user addresses to the backing storage. There's two cases here because getting the block_device for a regular filesystem file is different than a raw block device.

Excuse me, but "file on some filesystem + block number on underlying device" makes no sense as an API...

maintainer
#15 lore raw

Re: [PATCH 4/5] io_uring: add support for dma pre-mapping

In reply to Keith Busch
Quoted 22 lines · Keith Busch
io_uring/io_uring.c +22 -0
+	file = fget(map.fd);
+	if (!file)
+		return -EBADF;
+
+	if (S_ISBLK(file_inode(file)->i_mode))
+		bdev = I_BDEV(file->f_mapping->host);
+	else if (S_ISREG(file_inode(file)->i_mode))
+		bdev = file->f_inode->i_sb->s_bdev;
+	else
+		return -EOPNOTSUPP;
+
+	for (i = map.buf_start; i < map.buf_end; i++) {
+		struct io_mapped_ubuf *imu = ctx->user_bufs[i];
+		void *tag;
+
+		if (imu->dma_tag) {
+			ret = -EBUSY;
+			goto err;
+		}
+
+		tag = block_dma_map(bdev, imu->bvec, imu->nr_bvecs);
+		if (IS_ERR(tag)) {

io_uring/io_uring.c static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, · line 3802

Quoted 10 lines
io_uring/io_uring.c +10 -0
+			ret = PTR_ERR(tag);
+			goto err;
+		}
+
+		imu->dma_tag = tag;
+		imu->dma_file = file;
+		imu->bdev = bdev;
+	}
+
+	fput(file);

This, BTW, is completely insane - what happens if you follow that with close(map.fd)? A bunch of dangling struct file references?

I really don't understand what you are trying to do here.

maintainer
#16 lore raw

Re: [PATCH 4/5] io_uring: add support for dma pre-mapping

Quoted 29 lines · Al Viro
Quoted 27 lines · Keith Busch
io_uring/io_uring.c +27 -0
+	file = fget(map.fd);
+	if (!file)
+		return -EBADF;
+
+	if (S_ISBLK(file_inode(file)->i_mode))
+		bdev = I_BDEV(file->f_mapping->host);
+	else if (S_ISREG(file_inode(file)->i_mode))
+		bdev = file->f_inode->i_sb->s_bdev;
+	else
+		return -EOPNOTSUPP;
+
+	for (i = map.buf_start; i < map.buf_end; i++) {
+		struct io_mapped_ubuf *imu = ctx->user_bufs[i];
+		void *tag;
+
+		if (imu->dma_tag) {
+			ret = -EBUSY;
+			goto err;
+		}
+
+		tag = block_dma_map(bdev, imu->bvec, imu->nr_bvecs);
+		if (IS_ERR(tag)) {
+			ret = PTR_ERR(tag);
+			goto err;
+		}
+
+		imu->dma_tag = tag;
Quoted 8 lines

io_uring/io_uring.c static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, · line 3802

Quoted 5 lines
io_uring/io_uring.c +5 -0
+		imu->dma_file = file;
+		imu->bdev = bdev;
+	}
+
+	fput(file);

This, BTW, is completely insane - what happens if you follow that with close(map.fd)? A bunch of dangling struct file references?

This should have been tied to files registered with the io_uring instance holding a reference, and cleaned up when the files are unregistered. I may be missing some cases here, so I'll fix that up.

Quoted 1 line

I really don't understand what you are trying to do here

We want to register userspace addresses with the block_device just once. We can skip costly per-IO setup this way.

maintainer
#10 lore raw

Re: [PATCH 4/5] io_uring: add support for dma pre-mapping

In reply to Al Viro
Quoted 13 lines · Al Viro
Quoted 12 lines · Keith Busch
Quoted 10 lines · Al Viro

io_uring/io_uring.c static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, · line 3778

Quoted 4 lines · Keith Busch
io_uring/io_uring.c +4 -0
+	if (S_ISBLK(file_inode(file)->i_mode))
+		bdev = I_BDEV(file->f_mapping->host);
+	else if (S_ISREG(file_inode(file)->i_mode))
+		bdev = file->f_inode->i_sb->s_bdev;

*blink*

Just what's the intended use of the second case here?

Quoted 8 lines
Quoted 5 lines

??

The use case is same as the first's: dma map the user addresses to the backing storage. There's two cases here because getting the block_device for a regular filesystem file is different than a raw block device.

Excuse me, but "file on some filesystem + block number on underlying device" makes no sense as an API...

Sorry if I'm misunderstanding your concern here.

The API is a file descriptor + index range of registered buffers (which is a pre-existing io_uring API). The file descriptor can come from opening either a raw block device (ex: /dev/nvme0n1), or any regular file on a mounted filesystem using nvme as a backing store.

You don't need to know about specific block numbers. You can use the result with any offset in the underlying block device.

This also isn't necessarily tied to nvme-pci; that's just the only low-level driver I've enabled in this series, but others may come later.

maintainer
#17 lore raw

Re: [PATCH 4/5] io_uring: add support for dma pre-mapping

In reply to Keith Busch
Quoted 6 lines · Keith Busch
Quoted 2 lines

This, BTW, is completely insane - what happens if you follow that with close(map.fd)? A bunch of dangling struct file references?

This should have been tied to files registered with the io_uring instance holding a reference, and cleaned up when the files are unregistered. I may be missing some cases here, so I'll fix that up.

???

Your code does the following sequence:
file = fget(some number)
store the obtained pointer in a lot of places fput(file)

What is "may be missing" and what kind of "registration" could possibly help here? As soon as fget() had returned the reference, another thread might have removed it from the descriptor table, leaving you the sole holder of reference to object. In that case it will be destroyed by fput(), making its memory free for reuse.

Looks like you have some very odd idea of what the struct file lifetime rules are...

Quoted 4 lines
Quoted 1 line

I really don't understand what you are trying to do here

We want to register userspace addresses with the block_device just once. We can skip costly per-IO setup this way.

Explain, please. How will those be used afterwards and how will IO be matched with the file you've passed here?

#11 lore raw

Re: [PATCH 4/5] io_uring: add support for dma pre-mapping

In reply to Keith Busch
Quoted 23 lines · Keith Busch
Quoted 13 lines · Al Viro
Quoted 12 lines · Keith Busch
Quoted 10 lines · Al Viro

io_uring/io_uring.c static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, · line 3778

Quoted 4 lines · Keith Busch
io_uring/io_uring.c +4 -0
+	if (S_ISBLK(file_inode(file)->i_mode))
+		bdev = I_BDEV(file->f_mapping->host);
+	else if (S_ISREG(file_inode(file)->i_mode))
+		bdev = file->f_inode->i_sb->s_bdev;

*blink*

Just what's the intended use of the second case here?

Quoted 8 lines
Quoted 5 lines

??

The use case is same as the first's: dma map the user addresses to the backing storage. There's two cases here because getting the block_device for a regular filesystem file is different than a raw block device.

Excuse me, but "file on some filesystem + block number on underlying device" makes no sense as an API...

Quoted 6 lines

Sorry if I'm misunderstanding your concern here.

The API is a file descriptor + index range of registered buffers (which is a pre-existing io_uring API). The file descriptor can come from opening either a raw block device (ex: /dev/nvme0n1), or any regular file on a mounted filesystem using nvme as a backing store.

That's fundamentally flawed. Filesystems can have multiple block devices backing them that the VFS doesn't actually know about (e.g. btrfs, XFS, etc). Further, some of these filesystems can spread indiivdual file data across mutliple block devices i.e. the backing bdev changes as file offset changes....

Filesystems might not even have a block device (NFS, CIFS, etc) - what happens if you call this function on a file belonging to such a filesystem?

Quoted 2 lines

You don't need to know about specific block numbers. You can use the result with any offset in the underlying block device.

Sure, but you how exactly do you know what block device the file offset maps to?

We have entire layers like fs/iomap or bufferheads for this - their entire purpose in life is to efficiently manage the translation between {file, file_offset} and {dev, dev_offset} for the purposes of IO and data access...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
maintainer
#12 lore raw

Re: [PATCH 4/5] io_uring: add support for dma pre-mapping

Quoted 37 lines · Dave Chinner
Quoted 23 lines · Keith Busch
Quoted 13 lines · Al Viro
Quoted 12 lines · Keith Busch
Quoted 10 lines · Al Viro

io_uring/io_uring.c static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, · line 3778

Quoted 4 lines · Keith Busch
io_uring/io_uring.c +4 -0
+	if (S_ISBLK(file_inode(file)->i_mode))
+		bdev = I_BDEV(file->f_mapping->host);
+	else if (S_ISREG(file_inode(file)->i_mode))
+		bdev = file->f_inode->i_sb->s_bdev;

*blink*

Just what's the intended use of the second case here?

Quoted 8 lines
Quoted 5 lines

??

The use case is same as the first's: dma map the user addresses to the backing storage. There's two cases here because getting the block_device for a regular filesystem file is different than a raw block device.

Excuse me, but "file on some filesystem + block number on underlying device" makes no sense as an API...

Quoted 6 lines

Sorry if I'm misunderstanding your concern here.

The API is a file descriptor + index range of registered buffers (which is a pre-existing io_uring API). The file descriptor can come from opening either a raw block device (ex: /dev/nvme0n1), or any regular file on a mounted filesystem using nvme as a backing store.

That's fundamentally flawed. Filesystems can have multiple block devices backing them that the VFS doesn't actually know about (e.g. btrfs, XFS, etc). Further, some of these filesystems can spread indiivdual file data across mutliple block devices i.e. the backing bdev changes as file offset changes....

Quoted 3 lines

Filesystems might not even have a block device (NFS, CIFS, etc) - what happens if you call this function on a file belonging to such a filesystem?

The block_device driver has to opt-in to this feature. If a multi-device block driver wants to opt-in to this, then it would be responsible to handle translating that driver's specific cookie to whatever representation the drivers it stacks atop require. Otherwise, the cookie threaded through the bio is an opque value: nothing between io_uring and the block_device driver need to decode it.

If the block_device doesn't support providing this cookie, then io_uring just falls back to the existing less optimal methond, and all will continue to work as it does today.

#13 lore raw

Re: [PATCH 4/5] io_uring: add support for dma pre-mapping

Quoted 42 lines · Keith Busch
Quoted 37 lines · Dave Chinner
Quoted 23 lines · Keith Busch
Quoted 13 lines · Al Viro
Quoted 12 lines · Keith Busch
Quoted 10 lines · Al Viro

Review

Quoted 4 lines · Keith Busch
(quoted diff) +4 -0
+	if (S_ISBLK(file_inode(file)->i_mode))
+		bdev = I_BDEV(file->f_mapping->host);
+	else if (S_ISREG(file_inode(file)->i_mode))
+		bdev = file->f_inode->i_sb->s_bdev;

*blink*

Just what's the intended use of the second case here?

Quoted 8 lines
Quoted 5 lines

??

The use case is same as the first's: dma map the user addresses to the backing storage. There's two cases here because getting the block_device for a regular filesystem file is different than a raw block device.

Excuse me, but "file on some filesystem + block number on underlying device" makes no sense as an API...

Quoted 6 lines

Sorry if I'm misunderstanding your concern here.

The API is a file descriptor + index range of registered buffers (which is a pre-existing io_uring API). The file descriptor can come from opening either a raw block device (ex: /dev/nvme0n1), or any regular file on a mounted filesystem using nvme as a backing store.

That's fundamentally flawed. Filesystems can have multiple block devices backing them that the VFS doesn't actually know about (e.g. btrfs, XFS, etc). Further, some of these filesystems can spread indiivdual file data across mutliple block devices i.e. the backing bdev changes as file offset changes....

Quoted 3 lines

Filesystems might not even have a block device (NFS, CIFS, etc) - what happens if you call this function on a file belonging to such a filesystem?

Quoted 6 lines

The block_device driver has to opt-in to this feature. If a multi-device block driver wants to opt-in to this, then it would be responsible to handle translating that driver's specific cookie to whatever representation the drivers it stacks atop require. Otherwise, the cookie threaded through the bio is an opque value: nothing between io_uring and the block_device driver need to decode it.

I'm not talking about "multi-device" block devices like we build with DM or MD to present a single stacked block device to the filesystem. I'm talking about the fact that both btrfs and XFS support multiple *independent* block devices in the one filesystem.

i.e.:

# mkfs.xfs -r rtdev=/dev/nvme0n1 -l logdev=/dev/nvme1n1,size=2000m /dev/nvme2n1
meta-data=/dev/nvme2n1           isize=512    agcount=4, agsize=22893287 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=0    bigtime=1 inobtcount=1 nrext64=0
data     =                       bsize=4096   blocks=91573146, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =/dev/nvme1n1           bsize=4096   blocks=512000, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =/dev/nvme0n1           extsz=4096   blocks=91573146, rtextents=91573146
#

This builds an XFS filesystem which can write file data to either /dev/nvme0n1 or /dev/nvme2n1, and journal IO will get sent to a third block dev (/dev/nvme1n1).

So, which block device do we map for the DMA buffers that contain the file data for any given file in that filesystem? There is no guarantee that is is sb->s_bdev, because it only points at one of the two block devices that can contain file data.

Btrfs is similar, but it might stripe data across /dev/nvme0n1, /dev/nvme1n1 and /dev/nvme2n1 for a single file writes (and hence reads) and so needs separate DMA mappings for each block device just to do IO direct to/from one file....

Indeed, for XFS there's no requirement that the block devices have the same capabilities or even storage types - the rtdev could be spinning disks, the logdev an nvme SSD, and the datadev is pmem. If XFs has to do something special, it queries the bdev it needs to operate on (e.g. DAX mappings are only allowed on pmem based devices).

Hence it is invalid to assume that sb->s_bdev points at the actual block device the data for any given regular file is stored on. It is also invalid to assume the characteristics of the device in sb->s_bdev are common for all files in the filesystem.

IOWs, the only way you can make something like this work via filesystem mapping infrastructure to translate file offset to to a {dev, dev_offset} tuple to tell you what persistently mapped device buffers you need to use for IO to the given file {offset,len} range that IO needs to be done on....

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
maintainer
#14 lore raw

Re: [PATCH 4/5] io_uring: add support for dma pre-mapping

Quoted 54 lines · Dave Chinner
Quoted 6 lines · Keith Busch

The block_device driver has to opt-in to this feature. If a multi-device block driver wants to opt-in to this, then it would be responsible to handle translating that driver's specific cookie to whatever representation the drivers it stacks atop require. Otherwise, the cookie threaded through the bio is an opque value: nothing between io_uring and the block_device driver need to decode it.

I'm not talking about "multi-device" block devices like we build with DM or MD to present a single stacked block device to the filesystem. I'm talking about the fact that both btrfs and XFS support multiple *independent* block devices in the one filesystem.

i.e.:

# mkfs.xfs -r rtdev=/dev/nvme0n1 -l logdev=/dev/nvme1n1,size=2000m /dev/nvme2n1
meta-data=/dev/nvme2n1           isize=512    agcount=4, agsize=22893287 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=0    bigtime=1 inobtcount=1 nrext64=0
data     =                       bsize=4096   blocks=91573146, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =/dev/nvme1n1           bsize=4096   blocks=512000, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =/dev/nvme0n1           extsz=4096   blocks=91573146, rtextents=91573146
#

This builds an XFS filesystem which can write file data to either /dev/nvme0n1 or /dev/nvme2n1, and journal IO will get sent to a third block dev (/dev/nvme1n1).

So, which block device do we map for the DMA buffers that contain the file data for any given file in that filesystem? There is no guarantee that is is sb->s_bdev, because it only points at one of the two block devices that can contain file data.

Btrfs is similar, but it might stripe data across /dev/nvme0n1, /dev/nvme1n1 and /dev/nvme2n1 for a single file writes (and hence reads) and so needs separate DMA mappings for each block device just to do IO direct to/from one file....

Indeed, for XFS there's no requirement that the block devices have the same capabilities or even storage types - the rtdev could be spinning disks, the logdev an nvme SSD, and the datadev is pmem. If XFs has to do something special, it queries the bdev it needs to operate on (e.g. DAX mappings are only allowed on pmem based devices).

Hence it is invalid to assume that sb->s_bdev points at the actual block device the data for any given regular file is stored on. It is also invalid to assume the characteristics of the device in sb->s_bdev are common for all files in the filesystem.

Quoted 5 lines

IOWs, the only way you can make something like this work via filesystem mapping infrastructure to translate file offset to to a {dev, dev_offset} tuple to tell you what persistently mapped device buffers you need to use for IO to the given file {offset,len} range that IO needs to be done on....

Thank you for the explanation. I understand now, sorry for my previous misunderstanding.

I may consider just initially supporting direct raw block devices if I can't find a viable solution quick enough.