Skip to contentSkip to conversation

avoid possible double completions for the same request

PATCH In review Ye Bin 3 participants 6 messages Jun 8 – Jun 9

Subsystems (3)
lore
Share & tools
lore

Ye Bin

author
#0 lore raw

cover avoid possible double completions for the same request

From: Ye Bin <[email protected]>

This patchset fix possible double completions for the same request. Refer to the comit f1342709d18a ("scsi: no longer depend on blk-mq for double completion") to implement a mechanism that does not rely on the block layer.

Ye Bin (2):
nvme: operate nvme_request->flags with atomic api
nvme: avoid possible double completions for the same request

 drivers/nvme/host/apple.c     |  4 ++--
 drivers/nvme/host/core.c      | 13 ++++++++++---
 drivers/nvme/host/fc.c        |  2 +-
 drivers/nvme/host/ioctl.c     |  4 ++--
 drivers/nvme/host/multipath.c | 12 ++++++------
 drivers/nvme/host/nvme.h      | 13 ++++++++-----
 drivers/nvme/host/pci.c       | 11 ++++++-----
 7 files changed, 35 insertions(+), 24 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 nvme: operate nvme_request->flags with atomic api

From: Ye Bin <[email protected]>

This patch does not change any function. The nvme_request->flags is converted to the unsigned long type and the atomic operation interface is used. This prevents the status exception caused by concurrent operations on flags. In addition, this is also a preparation for fixing the problem that I/Os are processed twice in the done phase.

---
 drivers/nvme/host/apple.c     |  4 ++--
 drivers/nvme/host/core.c      |  7 ++++---
 drivers/nvme/host/fc.c        |  2 +-
 drivers/nvme/host/ioctl.c     |  4 ++--
 drivers/nvme/host/multipath.c | 12 ++++++------
 drivers/nvme/host/nvme.h      | 10 +++++-----
 drivers/nvme/host/pci.c       | 11 ++++++-----
 7 files changed, 26 insertions(+), 24 deletions(-)
drivers/nvme/host/apple.c +2 -2
index 423c9c628e7b..f669fecd5604 100644
@@ -941,7 +941,7 @@ static enum blk_eh_timer_return apple_nvme_timeout(struct request *req)
 		if (blk_mq_request_started(req) &&
 		    !blk_mq_request_completed(req)) {
 			nvme_req(req)->status = NVME_SC_HOST_ABORTED_CMD;
-			nvme_req(req)->flags |= NVME_REQ_CANCELLED;
+			set_bit(NVME_REQ_CANCELLED, &nvme_req(req)->flags);
 			blk_mq_complete_request(req);
 		}
 		return BLK_EH_DONE;
@@ -966,7 +966,7 @@ static enum blk_eh_timer_return apple_nvme_timeout(struct request *req)
 	 */
 	dev_warn(anv->dev, "I/O %d(aq:%d) timeout: resetting controller\n",
 		 req->tag, q->is_adminq);
-	nvme_req(req)->flags |= NVME_REQ_CANCELLED;
+	set_bit(NVME_REQ_CANCELLED, &nvme_req(req)->flags);
 	apple_nvme_disable(anv, false);
 	nvme_reset_ctrl(&anv->ctrl);
 	return BLK_EH_DONE;
drivers/nvme/host/core.c +4 -3
index dc388e24caad..c06f18dc5a65 100644
@@ -533,7 +533,7 @@ bool nvme_cancel_request(struct request *req, void *data)
 		return true;
 
 	nvme_req(req)->status = NVME_SC_HOST_ABORTED_CMD;
-	nvme_req(req)->flags |= NVME_REQ_CANCELLED;
+	set_bit(NVME_REQ_CANCELLED, &nvme_req(req)->flags);
 	blk_mq_complete_request(req);
 	return true;
 }
@@ -790,7 +790,8 @@ bool __nvme_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
 	 * sequence. until the controller will be LIVE, fail with
 	 * BLK_STS_RESOURCE so that they will be rescheduled.
 	 */
-	if (rq->q == ctrl->admin_q && (req->flags & NVME_REQ_USERCMD))
+	if (rq->q == ctrl->admin_q &&
+	    test_bit(NVME_REQ_USERCMD, &req->flags))
 		return false;
 
 	if (ctrl->ops->flags & NVME_F_FABRICS) {
@@ -1149,7 +1150,7 @@ int nvme_execute_rq(struct request *rq, bool at_head)
 	blk_status_t status;
 
 	status = blk_execute_rq(rq, at_head);
-	if (nvme_req(rq)->flags & NVME_REQ_CANCELLED)
+	if (test_bit(NVME_REQ_CANCELLED, &nvme_req(rq)->flags))
 		return -EINTR;
 	if (nvme_req(rq)->status)
 		return nvme_req(rq)->status;
drivers/nvme/host/fc.c +1 -1
index e4f4528fe2a2..f6e25ccc730b 100644
@@ -2420,7 +2420,7 @@ static bool nvme_fc_terminate_exchange(struct request *req, void *data)
 	struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
 
-	op->nreq.flags |= NVME_REQ_CANCELLED;
+	set_bit(NVME_REQ_CANCELLED, &op->nreq.flags);
 	__nvme_fc_abort_op(ctrl, op);
 	return true;
 }
drivers/nvme/host/ioctl.c +2 -2
index 9597a87cf05d..62316da41521 100644
@@ -108,7 +108,7 @@ static struct request *nvme_alloc_user_request(struct request_queue *q,
 	if (IS_ERR(req))
 		return req;
 	nvme_init_request(req, cmd);
-	nvme_req(req)->flags |= NVME_REQ_USERCMD;
+	set_bit(NVME_REQ_USERCMD, &nvme_req(req)->flags);
 	return req;
 }
 
@@ -416,7 +416,7 @@ static enum rq_end_io_ret nvme_uring_cmd_end_io(struct request *req,
 	struct io_uring_cmd *ioucmd = req->end_io_data;
 	struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
 
-	if (nvme_req(req)->flags & NVME_REQ_CANCELLED) {
+	if (test_bit(NVME_REQ_CANCELLED, &nvme_req(req)->flags)) {
 		pdu->status = -EINTR;
 	} else {
 		pdu->status = nvme_req(req)->status;
drivers/nvme/host/multipath.c +6 -6
index 263161cb8ac0..7ac1b13e24cb 100644
@@ -170,16 +170,16 @@ void nvme_mpath_start_request(struct request *rq)
 	struct gendisk *disk = ns->head->disk;
 
 	if ((READ_ONCE(ns->head->subsys->iopolicy) == NVME_IOPOLICY_QD) &&
-	    !(nvme_req(rq)->flags & NVME_MPATH_CNT_ACTIVE)) {
+	    !test_bit(NVME_MPATH_CNT_ACTIVE, &nvme_req(rq)->flags)) {
 		atomic_inc(&ns->ctrl->nr_active);
-		nvme_req(rq)->flags |= NVME_MPATH_CNT_ACTIVE;
+		set_bit(NVME_MPATH_CNT_ACTIVE, &nvme_req(rq)->flags);
 	}
 
 	if (!blk_queue_io_stat(disk->queue) || blk_rq_is_passthrough(rq) ||
-	    (nvme_req(rq)->flags & NVME_MPATH_IO_STATS))
+	    test_bit(NVME_MPATH_IO_STATS, &nvme_req(rq)->flags))
 		return;
 
-	nvme_req(rq)->flags |= NVME_MPATH_IO_STATS;
+	set_bit(NVME_MPATH_IO_STATS, &nvme_req(rq)->flags);
 	nvme_req(rq)->start_time = bdev_start_io_acct(disk->part0, req_op(rq),
 						      jiffies);
 }
@@ -189,10 +189,10 @@ void nvme_mpath_end_request(struct request *rq)
 {
 	struct nvme_ns *ns = rq->q->queuedata;
 
-	if (nvme_req(rq)->flags & NVME_MPATH_CNT_ACTIVE)
+	if (test_bit(NVME_MPATH_CNT_ACTIVE, &nvme_req(rq)->flags))
 		atomic_dec_if_positive(&ns->ctrl->nr_active);
 
-	if (!(nvme_req(rq)->flags & NVME_MPATH_IO_STATS))
+	if (!test_bit(NVME_MPATH_IO_STATS, &nvme_req(rq)->flags))
 		return;
 	bdev_end_io_acct(ns->head->disk->part0, req_op(rq),
 			 blk_rq_bytes(rq) >> SECTOR_SHIFT,
drivers/nvme/host/nvme.h +5 -5
index ccd5e05dac98..1059976b413b 100644
@@ -243,8 +243,8 @@ struct nvme_request {
 	union nvme_result	result;
 	u8			genctr;
 	u8			retries;
-	u8			flags;
 	u16			status;
+	unsigned long		flags;
 #ifdef CONFIG_NVME_MULTIPATH
 	unsigned long		start_time;
 #endif
@@ -257,10 +257,10 @@ struct nvme_request {
 #define REQ_NVME_MPATH		REQ_DRV
 
 enum {
-	NVME_REQ_CANCELLED		= (1 << 0),
-	NVME_REQ_USERCMD		= (1 << 1),
-	NVME_MPATH_IO_STATS		= (1 << 2),
-	NVME_MPATH_CNT_ACTIVE		= (1 << 3),
+	NVME_REQ_CANCELLED		= 0,
+	NVME_REQ_USERCMD		= 1,
+	NVME_MPATH_IO_STATS		= 2,
+	NVME_MPATH_CNT_ACTIVE		= 3,
 };
 
 static inline struct nvme_request *nvme_req(struct request *req)
drivers/nvme/host/pci.c +6 -5
index 9fd04cd7c5cb..ab77973338b5 100644
@@ -760,7 +760,7 @@ static inline bool nvme_pci_metadata_use_sgls(struct request *req)
 	if (!nvme_ctrl_meta_sgl_supported(&dev->ctrl))
 		return false;
 	return req->nr_integrity_segments > 1 ||
-		nvme_req(req)->flags & NVME_REQ_USERCMD;
+		test_bit(NVME_REQ_USERCMD, &nvme_req(req)->flags);
 }
 
 static inline enum nvme_use_sgl nvme_pci_use_sgls(struct nvme_dev *dev,
@@ -783,7 +783,7 @@ static inline enum nvme_use_sgl nvme_pci_use_sgls(struct nvme_dev *dev,
 		 *    only way to describe such a command in NVMe.
 		 */
 		if (req_phys_gap_mask(req) & (NVME_CTRL_PAGE_SIZE - 1) ||
-		    nvme_req(req)->flags & NVME_REQ_USERCMD ||
+		    test_bit(NVME_REQ_USERCMD, &nvme_req(req)->flags) ||
 		    req->nr_integrity_segments > 1)
 			return SGL_FORCED;
 		return SGL_SUPPORTED;
@@ -1303,7 +1303,8 @@ static blk_status_t nvme_pci_setup_meta_iter(struct request *req)
 	 * leverages this routine when that happens.
 	 */
 	if (!nvme_ctrl_meta_sgl_supported(&dev->ctrl) ||
-	    (entries == 1 && !(nvme_req(req)->flags & NVME_REQ_USERCMD))) {
+	    (entries == 1 &&
+	     !test_bit(NVME_REQ_USERCMD, &nvme_req(req)->flags))) {
 		iod->cmd.common.metadata = cpu_to_le64(iter.addr);
 		iod->meta_total_len = iter.len;
 		iod->meta_dma = iter.addr;
@@ -1909,7 +1910,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req)
 		dev_warn_ratelimited(dev->ctrl.device,
 			 "I/O tag %d (%04x) QID %d timeout, disable controller\n",
 			 req->tag, nvme_cid(req), nvmeq->qid);
-		nvme_req(req)->flags |= NVME_REQ_CANCELLED;
+		set_bit(NVME_REQ_CANCELLED, &nvme_req(req)->flags);
 		nvme_dev_disable(dev, true);
 		return BLK_EH_DONE;
 	case NVME_CTRL_RESETTING:
@@ -1929,7 +1930,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req)
 			 "I/O tag %d (%04x) opcode %#x (%s) QID %d timeout, reset controller\n",
 			 req->tag, nvme_cid(req), opcode,
 			 nvme_opcode_str(nvmeq->qid, opcode), nvmeq->qid);
-		nvme_req(req)->flags |= NVME_REQ_CANCELLED;
+		set_bit(NVME_REQ_CANCELLED, &nvme_req(req)->flags);
 		goto disable;
 	}
 
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/2 nvme: avoid possible double completions for the same request

In reply to Ye Bin

From: Ye Bin <[email protected]>

Multiple instances of null pointer references have occurred in the production environment due to the same request reporting "done" twice when hardware failures happen. The exception stack trace information is as follows:

Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000

Mem abort info:
ESR = 0x96000004
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
Data abort info:
ISV = 0, ISS = 0x00000004
CM = 0, WnR = 0
user pgtable: 4k pages, 48-bit VAs, pgdp=00000020ce32d000 [0000000000000000] pgd=0000000000000000, p4d=0000000000000000

Internal error: Oops: 0000000096000004 [#1] SMP
CPU: 18 PID: 577 Comm: kworker/18:1H Kdump: loaded Tainted: G           O  K   5.10.0-182.aarch64 #1
Workqueue: kblockd blk_mq_requeue_work
pstate: 40400009 (nZcv daif +PAN -UAO -TCO BTYPE=--)
pc : cc+0x60/0x1a0
lr : blk_mq_request_bypass_insert+0x40/0x1a0

sp : ffff8001046fbcf0
x29: ffff8001046fbcf0 x28: 0000000000000000
x27: ffff800103aa3c48 x26: ffff800101b4ac10
x25: ffff0027fff49005 x24: 0000000000000088
x23: 0000000000000000 x22: 0000000000000000
x21: ffff00208c86bc40 x20: ffff00208c86bc40
x19: 0000000000000000 x18: 0000000000000000
x17: 0000000000000000 x16: ffff800100db2c70
x15: 0000aaaaca58e4d0 x14: 0000000000000000
x13: 0000000000000000 x12: 0000000000000000
x11: 0000000000000000 x10: 0000000000000eb0
x9 : ffff800100640420 x8 : fefefefefefefeff
x7 : 0000000000000018 x6 : ffff00208a700d74
x5 : 00646b636f6c626b x4 : 0000000000000000
x3 : 0000000000000000 x2 : 0000000000000001
x1 : 0000000000000000 x0 : 0000000000000000

Call trace:
 blk_mq_request_bypass_insert+0x60/0x1a0
 blk_mq_requeue_work+0x154/0x2b0
 process_one_work+0x1d8/0x4cc
 worker_thread+0x158/0x410
 kthread+0x108/0x134
 ret_from_fork+0x10/0x18

To avoid the preceding problem, the NVME_REQ_COMPLETE flag is added by referring to the implementation of scsi commit f1342709d18a ("scsi: Do not rely on blk-mq for double completions"). The flag is set in the interrupt entry processing to prevent the same I/O from being executed twice.

---
 drivers/nvme/host/core.c | 6 ++++++
 drivers/nvme/host/nvme.h | 3 +++
 2 files changed, 9 insertions(+)
drivers/nvme/host/core.c +6 -0
index c06f18dc5a65..d162a713a00c 100644
@@ -532,6 +532,10 @@ bool nvme_cancel_request(struct request *req, void *data)
 	if (blk_mq_rq_state(req) != MQ_RQ_IN_FLIGHT)
 		return true;
 
+	if (unlikely(test_and_set_bit(NVME_REQ_COMPLETE,
+				      &nvme_req(req)->flags)))
+		return true;
+
 	nvme_req(req)->status = NVME_SC_HOST_ABORTED_CMD;
 	set_bit(NVME_REQ_CANCELLED, &nvme_req(req)->flags);
 	blk_mq_complete_request(req);
@@ -1091,6 +1095,8 @@ blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req)
 
 	if (!(req->rq_flags & RQF_DONTPREP))
 		nvme_clear_nvme_request(req);
+	else
+		clear_bit(NVME_REQ_COMPLETE, &nvme_req(req)->flags);
 
 	switch (req_op(req)) {
 	case REQ_OP_DRV_IN:
drivers/nvme/host/nvme.h +3 -0
index 1059976b413b..a60ffb6f463f 100644
@@ -261,6 +261,7 @@ enum {
 	NVME_REQ_USERCMD		= 1,
 	NVME_MPATH_IO_STATS		= 2,
 	NVME_MPATH_CNT_ACTIVE		= 3,
+	NVME_REQ_COMPLETE		= 4,
 };
 
 static inline struct nvme_request *nvme_req(struct request *req)
@@ -807,6 +808,8 @@ static inline bool nvme_try_complete_req(struct request *req, __le16 status,
 	nvme_should_fail(req);
 	if (unlikely(blk_should_fake_timeout(req->q)))
 		return true;
+	if (unlikely(test_and_set_bit(NVME_REQ_COMPLETE, &rq->flags)))
+		return true;
 	return blk_mq_complete_request_remote(req);
 }
 
Signature · 1 line
2.34.1
maintainer
#3 lore raw

Re: [PATCH 2/2] nvme: avoid possible double completions for the same request

Quoted 3 lines · Ye Bin

To avoid the preceding problem, the NVME_REQ_COMPLETE flag is added by referring to the implementation of scsi commit f1342709d18a ("scsi: Do not rely on blk-mq for double completions").

That scsi commit was solving a different problem for a racing interaction between the low level driver and the timeout handler and error injection. It wasn't about protecting against misbehaving hardware.

drivers/nvme/host/nvme.h static inline bool nvme_try_complete_req(struct request *req, __le16 status, · line 812

Quoted 7 lines

static inline struct nvme_request *nvme_req(struct request *req)

drivers/nvme/host/nvme.h +2 -0
@@ -807,6 +808,8 @@ static inline bool nvme_try_complete_req(struct request *req, __le16 status,
 	nvme_should_fail(req);
 	if (unlikely(blk_should_fake_timeout(req->q)))
 		return true;
+	if (unlikely(test_and_set_bit(NVME_REQ_COMPLETE, &rq->flags)))
+		return true;

I think you need to invert this flag from "COMPLETE" to "INFLIGHT", because the default allocated state is that this flag is cleared, so this check as you have it wouldn't catch a phantom completion to a request the host never sent.

You also have this check after the driver updated its internal generation counter for a bogus completion, so now our actual state could be off from what's actually being prepared.

This previous proposal for a similar problem was probably more on the right track:

https://lore.kernel.org/linux-nvme/[email protected]/

However, I'm really skeptical controllers actually behave this way. You wouldn't have a viable storage device if it was doing this.

maintainer
#4 lore raw

Re: [PATCH 2/2] nvme: avoid possible double completions for the same request

Quoted 2 lines · Keith Busch

However, I'm really skeptical controllers actually behave this way. You wouldn't have a viable storage device if it was doing this.

Same, and I would be strongly opposed to including theoretical work-arounds like this that just have fast path overhead for zero gain on a valid device. If your device is broken, ask the manufacturer to fix it. Let's please not burden the kernel with catering to broken implementations.

Signature · 1 line
Jens Axboe
author
#5 lore raw

Re: [PATCH 2/2] nvme: avoid possible double completions for the same request

In reply to Keith Busch
Quoted 23 lines · Keith Busch
Quoted 3 lines · Ye Bin

To avoid the preceding problem, the NVME_REQ_COMPLETE flag is added by referring to the implementation of scsi commit f1342709d18a ("scsi: Do not rely on blk-mq for double completions").

That scsi commit was solving a different problem for a racing interaction between the low level driver and the timeout handler and error injection. It wasn't about protecting against misbehaving hardware.

drivers/nvme/host/nvme.h static inline bool nvme_try_complete_req(struct request *req, __le16 status, · line 812

Quoted 7 lines

static inline struct nvme_request *nvme_req(struct request *req)

drivers/nvme/host/nvme.h +2 -0
@@ -807,6 +808,8 @@ static inline bool nvme_try_complete_req(struct request *req, __le16 status,
  	nvme_should_fail(req);
  	if (unlikely(blk_should_fake_timeout(req->q)))
  		return true;
+	if (unlikely(test_and_set_bit(NVME_REQ_COMPLETE, &rq->flags)))
+		return true;

I think you need to invert this flag from "COMPLETE" to "INFLIGHT", because the default allocated state is that this flag is cleared, so this check as you have it wouldn't catch a phantom completion to a request the host never sent.

Quoted 9 lines

You also have this check after the driver updated its internal generation counter for a bogus completion, so now our actual state could be off from what's actually being prepared.

This previous proposal for a similar problem was probably more on the right track:

https://lore.kernel.org/linux-nvme/[email protected]/

Thanks for your reply.
This solution is indeed more accurate.

Quoted 3 lines

However, I'm really skeptical controllers actually behave this way. You wouldn't have a viable storage device if it was doing this.