public inbox for [email protected]
 help / color / mirror / Atom feed
From: Kanchan Joshi <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]
Cc: [email protected], [email protected],
	[email protected], [email protected],
	[email protected], [email protected], [email protected],
	[email protected], [email protected], [email protected],
	[email protected]
Subject: [PATCH 15/17] nvme: wire-up polling for uring-passthru
Date: Tue,  8 Mar 2022 20:51:03 +0530	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

From: Pankaj Raghav <[email protected]>

add .iopoll handler for char device (/dev/ngX), which in turn redirects
to bio_poll to implement polling.

Signed-off-by: Pankaj Raghav <[email protected]>
---
 block/blk-mq.c                |  3 +-
 drivers/nvme/host/core.c      |  1 +
 drivers/nvme/host/ioctl.c     | 79 ++++++++++++++++++++++++++++++++++-
 drivers/nvme/host/multipath.c |  1 +
 drivers/nvme/host/nvme.h      |  4 ++
 include/linux/blk-mq.h        |  1 +
 6 files changed, 86 insertions(+), 3 deletions(-)

diff --git a/block/blk-mq.c b/block/blk-mq.c
index 29f65eaf3e6b..6b37774b0d59 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -1193,7 +1193,7 @@ void blk_execute_rq_nowait(struct request *rq, bool at_head, rq_end_io_fn *done)
 }
 EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
 
-static bool blk_rq_is_poll(struct request *rq)
+bool blk_rq_is_poll(struct request *rq)
 {
 	if (!rq->mq_hctx)
 		return false;
@@ -1203,6 +1203,7 @@ static bool blk_rq_is_poll(struct request *rq)
 		return false;
 	return true;
 }
+EXPORT_SYMBOL_GPL(blk_rq_is_poll);
 
 static void blk_rq_poll_completion(struct request *rq, struct completion *wait)
 {
diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 4a385001f124..64254771a28e 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -3671,6 +3671,7 @@ static const struct file_operations nvme_ns_chr_fops = {
 	.unlocked_ioctl	= nvme_ns_chr_ioctl,
 	.compat_ioctl	= compat_ptr_ioctl,
 	.async_cmd	= nvme_ns_chr_async_cmd,
+	.iopoll		= nvme_iopoll,
 };
 
 static int nvme_add_ns_cdev(struct nvme_ns *ns)
diff --git a/drivers/nvme/host/ioctl.c b/drivers/nvme/host/ioctl.c
index a6712fb3eb98..701feaecabbe 100644
--- a/drivers/nvme/host/ioctl.c
+++ b/drivers/nvme/host/ioctl.c
@@ -37,6 +37,12 @@ static struct nvme_uring_cmd_pdu *nvme_uring_cmd_pdu(struct io_uring_cmd *ioucmd
 	return (struct nvme_uring_cmd_pdu *)&ioucmd->pdu;
 }
 
+static inline bool is_polling_enabled(struct io_uring_cmd *ioucmd,
+				      struct request *req)
+{
+	return (ioucmd->flags & IO_URING_F_UCMD_POLLED) && blk_rq_is_poll(req);
+}
+
 static void nvme_pt_task_cb(struct io_uring_cmd *ioucmd)
 {
 	struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
@@ -71,8 +77,17 @@ static void nvme_end_async_pt(struct request *req, blk_status_t err)
 
 	pdu->req = req;
 	req->bio = bio;
-	/* this takes care of setting up task-work */
-	io_uring_cmd_complete_in_task(ioucmd, nvme_pt_task_cb);
+
+	/*
+	 * IO can be completed directly (i.e. without task work) if we are
+	 * polling and in the task context already
+	 */
+	if (is_polling_enabled(ioucmd, req)) {
+		nvme_pt_task_cb(ioucmd);
+	} else {
+		/* this takes care of setting up task-work */
+		io_uring_cmd_complete_in_task(ioucmd, nvme_pt_task_cb);
+	}
 }
 
 static void nvme_setup_uring_cmd_data(struct request *rq,
@@ -180,6 +195,10 @@ static int nvme_submit_user_cmd(struct request_queue *q,
 	if (ioucmd) { /* async dispatch */
 		if (cmd->common.opcode == nvme_cmd_write ||
 				cmd->common.opcode == nvme_cmd_read) {
+			if (bio && is_polling_enabled(ioucmd, req)) {
+				ioucmd->bio = bio;
+				bio->bi_opf |= REQ_POLLED;
+			}
 			nvme_setup_uring_cmd_data(req, ioucmd, meta, meta_buffer,
 					meta_len);
 			blk_execute_rq_nowait(req, 0, nvme_end_async_pt);
@@ -505,6 +524,32 @@ int nvme_ns_chr_async_cmd(struct io_uring_cmd *ioucmd)
 	return nvme_ns_async_ioctl(ns, ioucmd);
 }
 
+int nvme_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
+		unsigned int flags)
+{
+	struct bio *bio = NULL;
+	struct nvme_ns *ns = NULL;
+	struct request_queue *q = NULL;
+	int ret = 0;
+
+	rcu_read_lock();
+	bio = READ_ONCE(kiocb->private);
+	ns = container_of(file_inode(kiocb->ki_filp)->i_cdev, struct nvme_ns,
+			  cdev);
+	q = ns->queue;
+	/*
+	 * bio and driver_cb are a part of the same union inside io_uring_cmd
+	 * struct. If driver is loaded without poll queues, completion will be
+	 * IRQ based and driver_cb is populated. We do not want to treat that
+	 * as bio and get into troubles. Avoid this by checking if queue is
+	 * polled and bail out if not.
+	 */
+	if ((test_bit(QUEUE_FLAG_POLL, &q->queue_flags)) && bio && bio->bi_bdev)
+		ret = bio_poll(bio, iob, flags);
+	rcu_read_unlock();
+	return ret;
+}
+
 #ifdef CONFIG_NVME_MULTIPATH
 static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd,
 		void __user *argp, struct nvme_ns_head *head, int srcu_idx)
@@ -585,6 +630,36 @@ long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
 	srcu_read_unlock(&head->srcu, srcu_idx);
 	return ret;
 }
+
+int nvme_ns_head_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
+			unsigned int flags)
+{
+	struct bio *bio = NULL;
+	struct request_queue *q = NULL;
+	struct cdev *cdev = file_inode(kiocb->ki_filp)->i_cdev;
+	struct nvme_ns_head *head = container_of(cdev, struct nvme_ns_head, cdev);
+	int srcu_idx = srcu_read_lock(&head->srcu);
+	struct nvme_ns *ns = nvme_find_path(head);
+	int ret = -EWOULDBLOCK;
+
+	if (ns) {
+		bio = READ_ONCE(kiocb->private);
+		q = ns->queue;
+		/*
+		 * bio and driver_cb are part of same union inside io_uring_cmd
+		 * struct. If driver is loaded without poll queues, completion
+		 * will be IRQ based and driver_cb is populated. We do not want
+		 * to treat that as bio and get into troubles. Avoid this by
+		 * checking if queue is polled, and bail out if not.
+		 */
+		if ((test_bit(QUEUE_FLAG_POLL, &q->queue_flags)) && bio &&
+		    bio->bi_bdev)
+			ret = bio_poll(bio, iob, flags);
+	}
+
+	srcu_read_unlock(&head->srcu, srcu_idx);
+	return ret;
+}
 #endif /* CONFIG_NVME_MULTIPATH */
 
 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index 1d798d09456f..27995358c847 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -460,6 +460,7 @@ static const struct file_operations nvme_ns_head_chr_fops = {
 	.unlocked_ioctl	= nvme_ns_head_chr_ioctl,
 	.compat_ioctl	= compat_ptr_ioctl,
 	.async_cmd	= nvme_ns_head_chr_async_cmd,
+	.iopoll		= nvme_ns_head_iopoll,
 };
 
 static int nvme_add_ns_head_cdev(struct nvme_ns_head *head)
diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
index 9a3e5093dedc..0be437c25077 100644
--- a/drivers/nvme/host/nvme.h
+++ b/drivers/nvme/host/nvme.h
@@ -755,7 +755,11 @@ long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
 long nvme_dev_ioctl(struct file *file, unsigned int cmd,
 		unsigned long arg);
 int nvme_ns_chr_async_cmd(struct io_uring_cmd *ioucmd);
+int nvme_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
+		unsigned int flags);
 int nvme_ns_head_chr_async_cmd(struct io_uring_cmd *ioucmd);
+int nvme_ns_head_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
+			unsigned int flags);
 int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo);
 
 extern const struct attribute_group *nvme_ns_id_attr_groups[];
diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
index 5f21f71b2529..9f00b2a5a991 100644
--- a/include/linux/blk-mq.h
+++ b/include/linux/blk-mq.h
@@ -977,6 +977,7 @@ int blk_rq_map_kern(struct request_queue *, struct request *, void *,
 int blk_rq_append_bio(struct request *rq, struct bio *bio);
 void blk_execute_rq_nowait(struct request *rq, bool at_head,
 		rq_end_io_fn *end_io);
+bool blk_rq_is_poll(struct request *rq);
 blk_status_t blk_execute_rq(struct request *rq, bool at_head);
 
 struct req_iterator {
-- 
2.25.1


  parent reply	other threads:[~2022-03-08 15:43 UTC|newest]

Thread overview: 122+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20220308152651epcas5p1ebd2dc7fa01db43dd587c228a3695696@epcas5p1.samsung.com>
2022-03-08 15:20 ` [PATCH 00/17] io_uring passthru over nvme Kanchan Joshi
     [not found]   ` <CGME20220308152653epcas5p10c31f58cf6bff125cc0baa176b4d4fac@epcas5p1.samsung.com>
2022-03-08 15:20     ` [PATCH 01/17] io_uring: add support for 128-byte SQEs Kanchan Joshi
     [not found]   ` <CGME20220308152655epcas5p4ae47d715e1c15069e97152dcd283fd40@epcas5p4.samsung.com>
2022-03-08 15:20     ` [PATCH 02/17] fs: add file_operations->async_cmd() Kanchan Joshi
     [not found]   ` <CGME20220308152658epcas5p3929bd1fcf75edc505fec71901158d1b5@epcas5p3.samsung.com>
2022-03-08 15:20     ` [PATCH 03/17] io_uring: add infra and support for IORING_OP_URING_CMD Kanchan Joshi
2022-03-11  1:51       ` Luis Chamberlain
2022-03-11  2:43         ` Jens Axboe
2022-03-11 17:11           ` Luis Chamberlain
2022-03-11 18:47             ` Paul Moore
2022-03-11 20:57               ` Luis Chamberlain
2022-03-11 21:03                 ` Paul Moore
2022-03-14 16:25             ` Casey Schaufler
2022-03-14 16:32               ` Luis Chamberlain
2022-03-14 18:05                 ` Casey Schaufler
2022-03-14 19:40                   ` Luis Chamberlain
     [not found]   ` <CGME20220308152700epcas5p4130d20119a3a250a2515217d6552f668@epcas5p4.samsung.com>
2022-03-08 15:20     ` [PATCH 04/17] nvme: modify nvme_alloc_request to take an additional parameter Kanchan Joshi
2022-03-11  6:38       ` Christoph Hellwig
     [not found]   ` <CGME20220308152702epcas5p1eb1880e024ac8b9531c85a82f31a4e78@epcas5p1.samsung.com>
2022-03-08 15:20     ` [PATCH 05/17] nvme: wire-up support for async-passthru on char-device Kanchan Joshi
2022-03-10  0:02       ` Clay Mayers
2022-03-10  8:32         ` Kanchan Joshi
2022-03-11  7:01       ` Christoph Hellwig
2022-03-14 16:23         ` Kanchan Joshi
2022-03-15  8:54           ` Christoph Hellwig
2022-03-16  7:27             ` Kanchan Joshi
2022-03-24  6:22               ` Christoph Hellwig
2022-03-24 17:45                 ` Kanchan Joshi
2022-03-11 17:56       ` Luis Chamberlain
2022-03-11 18:53         ` Paul Moore
2022-03-11 21:02           ` Luis Chamberlain
2022-03-13 21:53       ` Sagi Grimberg
2022-03-14 17:54         ` Kanchan Joshi
2022-03-15  9:02           ` Sagi Grimberg
2022-03-16  9:21             ` Kanchan Joshi
2022-03-16 10:56               ` Sagi Grimberg
2022-03-16 11:51                 ` Kanchan Joshi
2022-03-16 13:52                   ` Sagi Grimberg
2022-03-16 14:35                     ` Jens Axboe
2022-03-16 14:50                       ` Sagi Grimberg
2022-03-24  6:20                         ` Christoph Hellwig
2022-03-24 10:42                           ` Sagi Grimberg
2022-03-22 15:18       ` Clay Mayers
2022-03-22 16:57         ` Kanchan Joshi
     [not found]   ` <CGME20220308152704epcas5p16610e1f50672b25fa1df5f7c5c261bb5@epcas5p1.samsung.com>
2022-03-08 15:20     ` [PATCH 06/17] io_uring: prep for fixed-buffer enabled uring-cmd Kanchan Joshi
     [not found]   ` <CGME20220308152707epcas5p430127761a7fd4bf90c2501eabe9ee96e@epcas5p4.samsung.com>
2022-03-08 15:20     ` [PATCH 07/17] io_uring: add support for uring_cmd with fixed-buffer Kanchan Joshi
     [not found]   ` <CGME20220308152709epcas5p1f9d274a0214dc462c22c278a72d8697c@epcas5p1.samsung.com>
2022-03-08 15:20     ` [PATCH 08/17] nvme: enable passthrough " Kanchan Joshi
2022-03-10  8:32       ` Christoph Hellwig
2022-03-11  6:43       ` Christoph Hellwig
2022-03-14 13:06         ` Kanchan Joshi
2022-03-15  8:55           ` Christoph Hellwig
2022-03-14 12:18       ` Ming Lei
2022-03-14 13:09         ` Kanchan Joshi
     [not found]   ` <CGME20220308152711epcas5p31de5d63f5de91fae94e61e5c857c0f13@epcas5p3.samsung.com>
2022-03-08 15:20     ` [PATCH 09/17] io_uring: plug for async bypass Kanchan Joshi
2022-03-10  8:33       ` Christoph Hellwig
2022-03-14 14:33         ` Ming Lei
2022-03-15  8:56           ` Christoph Hellwig
2022-03-11 17:15       ` Luis Chamberlain
     [not found]   ` <CGME20220308152714epcas5p4c5a0d16512fd7054c9a713ee28ede492@epcas5p4.samsung.com>
2022-03-08 15:20     ` [PATCH 10/17] block: wire-up support for plugging Kanchan Joshi
2022-03-10  8:34       ` Christoph Hellwig
2022-03-10 12:40         ` Kanchan Joshi
2022-03-14 14:40           ` Ming Lei
2022-03-21  7:02             ` Kanchan Joshi
2022-03-23  1:27               ` Ming Lei
2022-03-23  1:41                 ` Jens Axboe
2022-03-23  1:58                   ` Jens Axboe
2022-03-23  2:10                     ` Ming Lei
2022-03-23  2:17                       ` Jens Axboe
     [not found]   ` <CGME20220308152716epcas5p3d38d2372c184259f1a10c969f7e4396f@epcas5p3.samsung.com>
2022-03-08 15:20     ` [PATCH 11/17] block: factor out helper for bio allocation from cache Kanchan Joshi
2022-03-10  8:35       ` Christoph Hellwig
2022-03-10 12:25         ` Kanchan Joshi
2022-03-24  6:30           ` Christoph Hellwig
2022-03-24 17:45             ` Kanchan Joshi
2022-03-25  5:38               ` Christoph Hellwig
     [not found]   ` <CGME20220308152718epcas5p3afd2c8a628f4e9733572cbb39270989d@epcas5p3.samsung.com>
2022-03-08 15:21     ` [PATCH 12/17] nvme: enable bio-cache for fixed-buffer passthru Kanchan Joshi
2022-03-11  6:48       ` Christoph Hellwig
2022-03-14 18:18         ` Kanchan Joshi
2022-03-15  8:57           ` Christoph Hellwig
     [not found]   ` <CGME20220308152720epcas5p19653942458e160714444942ddb8b8579@epcas5p1.samsung.com>
2022-03-08 15:21     ` [PATCH 13/17] nvme: allow user passthrough commands to poll Kanchan Joshi
2022-03-08 17:08       ` Keith Busch
2022-03-09  7:03         ` Kanchan Joshi
2022-03-11  6:49           ` Christoph Hellwig
     [not found]   ` <CGME20220308152723epcas5p34460b4af720e515317f88dbb78295f06@epcas5p3.samsung.com>
2022-03-08 15:21     ` [PATCH 14/17] io_uring: add polling support for uring-cmd Kanchan Joshi
2022-03-11  6:50       ` Christoph Hellwig
2022-03-14 10:16         ` Kanchan Joshi
2022-03-15  8:57           ` Christoph Hellwig
2022-03-16  5:09             ` Kanchan Joshi
2022-03-24  6:30               ` Christoph Hellwig
     [not found]   ` <CGME20220308152725epcas5p36d1ce3269a47c1c22cc0d66bdc2b9eb3@epcas5p3.samsung.com>
2022-03-08 15:21     ` Kanchan Joshi [this message]
     [not found]   ` <CGME20220308152727epcas5p20e605718dd99e97c94f9232d40d04d95@epcas5p2.samsung.com>
2022-03-08 15:21     ` [PATCH 16/17] io_uring: add support for non-inline uring-cmd Kanchan Joshi
     [not found]   ` <CGME20220308152729epcas5p17e82d59c68076eb46b5ef658619d65e3@epcas5p1.samsung.com>
2022-03-08 15:21     ` [PATCH 17/17] nvme: enable non-inline passthru commands Kanchan Joshi
2022-03-10  8:36       ` Christoph Hellwig
2022-03-10 11:50         ` Kanchan Joshi
2022-03-10 14:19           ` Christoph Hellwig
2022-03-10 18:43             ` Kanchan Joshi
2022-03-11  6:27               ` Christoph Hellwig
2022-03-22 17:10                 ` Kanchan Joshi
2022-03-24  6:32                   ` Christoph Hellwig
2022-03-25 13:39                     ` Kanchan Joshi
2022-03-28  4:44                       ` Kanchan Joshi
2022-03-30 12:59                         ` Christoph Hellwig
2022-03-30 13:02                       ` Christoph Hellwig
2022-03-30 13:14                         ` Kanchan Joshi
2022-04-01  1:25                           ` Jens Axboe
2022-04-01  2:33                             ` Kanchan Joshi
2022-04-01  2:44                               ` Jens Axboe
2022-04-01  3:05                                 ` Jens Axboe
2022-04-01  6:32                                 ` Kanchan Joshi
2022-04-19 17:31                                 ` Kanchan Joshi
2022-04-19 18:19                                   ` Jens Axboe
     [not found]                                     ` <CGME20220420152003epcas5p3991e6941773690bcb425fd9d817105c3@epcas5p3.samsung.com>
2022-04-20 15:14                                       ` Kanchan Joshi
2022-04-20 15:28                                         ` Kanchan Joshi
2022-04-01  1:23                         ` Jens Axboe
2022-04-01  1:22                   ` Jens Axboe
2022-04-01  6:29                     ` Kanchan Joshi
2022-03-24 21:09       ` Clay Mayers
2022-03-24 23:36         ` Jens Axboe
2022-03-10  8:29   ` [PATCH 00/17] io_uring passthru over nvme Christoph Hellwig
2022-03-10 10:05     ` Kanchan Joshi
2022-03-11 16:43       ` Luis Chamberlain
2022-03-11 23:35         ` Adam Manzanares
2022-03-12  2:27           ` Adam Manzanares
2022-03-13  5:07             ` Kanchan Joshi
2022-03-14 20:30               ` Adam Manzanares
2022-03-13  5:10         ` Kanchan Joshi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox