Skip to contentSkip to conversation

nvmet: passthru cleanup and fixup I/O hotpath

PATCH Superseded Nilay Shroff 2 participants 5 messages Sep 25 – Sep 28

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover nvmet: passthru cleanup and fixup I/O hotpath

Hi,

This series addresses a race in the passthru I/O hotpath where concurrently disabling the passthru controller while I/Os are in flight can result in a use-after-free bug.

We were able to reproduce this bug with NVMe/TCP configured and by injecting an additional delay into the target-side I/O processing code. If the passthru controller is disabled while an I/O is still in flight, it results in the following kernel crash:

BUG: Kernel NULL pointer dereference on read at 0x00000030
[...]
CPU: 9 UID: 0 PID: 4143 Comm: kworker/9:5H Kdump: loaded Not tainted 7.3.0-rc3+ #14 PREEMPT 
Hardware name: IBM,9080-HEX Power11 (architected) 0x820200 0xf000007 of:IBM,FW1110.00 (NH1110_031) hv:phyp pSeries
Workqueue: nvmet_tcp_wq nvmet_tcp_io_work [nvmet_tcp]
[...]
NIP [c0080000156d8fe0] nvmet_passthru_execute_cmd+0x58/0x45c [nvmet]
LR [c0080000156d8fc4] nvmet_passthru_execute_cmd+0x3c/0x45c [nvmet]
Call Trace:
 nvmet_passthru_execute_cmd+0x3c/0x45c [nvmet] (unreliable)
 nvmet_tcp_done_recv_pdu+0x2d0/0x718 [nvmet_tcp]
 nvmet_tcp_try_recv_pdu+0x29c/0x348 [nvmet_tcp]
 nvmet_tcp_io_work+0xe8/0x838 [nvmet_tcp]
 process_one_work+0x224/0x5ec
 worker_thread+0x1f8/0x3e8
 kthread+0x178/0x1ac
 start_kernel_thread+0x14/0x18

There are two patches in this series. The first patch groups all passthru-related fields into a separate struct nvmet_passthru, which makes the code easier to maintain and reason about. The second patch fixes the kernel bug described above.

As usual, code review comments and feedback are most welcome!

Thanks!

Nilay Shroff (2):
nvmet: introduce struct nvmet_passthru
nvmet: fix use-after-free error in passthru I/O hotpath

 drivers/nvme/target/configfs.c | 45 +++++++++--------
 drivers/nvme/target/core.c     |  6 ++-
 drivers/nvme/target/nvmet.h    | 41 +++++++++++++---
 drivers/nvme/target/passthru.c | 88 +++++++++++++++++++++++++---------
 4 files changed, 130 insertions(+), 50 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 nvmet: introduce struct nvmet_passthru

Currently, passthru parameters are grouped under struct nvmet_subsys. Since passthru can be configured through configfs and all passthru parameters are exposed under a separate configfs subdirectory, group these parameters under a new struct nvmet_passthru.

Grouping passthru-specific parameters in a separate structure makes the code easier to maintain and reason about.

There are no functional changes intended.

---
 drivers/nvme/target/configfs.c | 45 ++++++++++++++++++++--------------
 drivers/nvme/target/core.c     |  2 +-
 drivers/nvme/target/nvmet.h    | 18 ++++++++------
 drivers/nvme/target/passthru.c | 44 ++++++++++++++++++---------------
 4 files changed, 62 insertions(+), 47 deletions(-)
drivers/nvme/target/configfs.c +26 -19
index b03b5d1c2dc3..6f060ee7e15b 100644
@@ -904,22 +904,23 @@ static const struct config_item_type nvmet_namespaces_type = {
 static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
 		char *page)
 {
-	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
+	struct nvmet_passthru *passthru = &to_subsys(item->ci_parent)->passthru;
 
-	return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
+	return snprintf(page, PAGE_SIZE, "%s\n", passthru->ctrl_path);
 }
 
 static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
 		const char *page, size_t count)
 {
 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
+	struct nvmet_passthru *passthru = &subsys->passthru;
 	size_t len;
 	int ret;
 
 	mutex_lock(&subsys->lock);
 
 	ret = -EBUSY;
-	if (subsys->passthru_ctrl)
+	if (passthru->ctrl)
 		goto out_unlock;
 
 	ret = -EINVAL;
@@ -927,10 +928,10 @@ static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
 	if (!len)
 		goto out_unlock;
 
-	kfree(subsys->passthru_ctrl_path);
+	kfree(passthru->ctrl_path);
 	ret = -ENOMEM;
-	subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
-	if (!subsys->passthru_ctrl_path)
+	passthru->ctrl_path = kstrndup(page, len, GFP_KERNEL);
+	if (!passthru->ctrl_path)
 		goto out_unlock;
 
 	mutex_unlock(&subsys->lock);
@@ -945,9 +946,9 @@ CONFIGFS_ATTR(nvmet_passthru_, device_path);
 static ssize_t nvmet_passthru_enable_show(struct config_item *item,
 		char *page)
 {
-	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
+	struct nvmet_passthru *passthru = &to_subsys(item->ci_parent)->passthru;
 
-	return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
+	return sprintf(page, "%d\n", passthru->ctrl ? 1 : 0);
 }
 
 static ssize_t nvmet_passthru_enable_store(struct config_item *item,
@@ -972,18 +973,20 @@ CONFIGFS_ATTR(nvmet_passthru_, enable);
 static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
 		char *page)
 {
-	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
+	struct nvmet_passthru *passthru = &to_subsys(item->ci_parent)->passthru;
+
+	return sprintf(page, "%u\n", passthru->admin_timeout);
 }
 
 static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
 		const char *page, size_t count)
 {
-	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
+	struct nvmet_passthru *passthru = &to_subsys(item->ci_parent)->passthru;
 	unsigned int timeout;
 
 	if (kstrtouint(page, 0, &timeout))
 		return -EINVAL;
-	subsys->admin_timeout = timeout;
+	passthru->admin_timeout = timeout;
 	return count;
 }
 CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
@@ -991,18 +994,20 @@ CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
 static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
 		char *page)
 {
-	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
+	struct nvmet_passthru *passthru = &to_subsys(item->ci_parent)->passthru;
+
+	return sprintf(page, "%u\n", passthru->io_timeout);
 }
 
 static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
 		const char *page, size_t count)
 {
-	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
+	struct nvmet_passthru *passthru = &to_subsys(item->ci_parent)->passthru;
 	unsigned int timeout;
 
 	if (kstrtouint(page, 0, &timeout))
 		return -EINVAL;
-	subsys->io_timeout = timeout;
+	passthru->io_timeout = timeout;
 	return count;
 }
 CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
@@ -1010,18 +1015,20 @@ CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
 static ssize_t nvmet_passthru_clear_ids_show(struct config_item *item,
 		char *page)
 {
-	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->clear_ids);
+	struct nvmet_passthru *passthru = &to_subsys(item->ci_parent)->passthru;
+
+	return sprintf(page, "%u\n", passthru->clear_ids);
 }
 
 static ssize_t nvmet_passthru_clear_ids_store(struct config_item *item,
 		const char *page, size_t count)
 {
-	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
+	struct nvmet_passthru *passthru = &to_subsys(item->ci_parent)->passthru;
 	unsigned int clear_ids;
 
 	if (kstrtouint(page, 0, &clear_ids))
 		return -EINVAL;
-	subsys->clear_ids = clear_ids;
+	passthru->clear_ids = clear_ids;
 	return count;
 }
 CONFIGFS_ATTR(nvmet_passthru_, clear_ids);
@@ -1042,9 +1049,9 @@ static const struct config_item_type nvmet_passthru_type = {
 
 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
 {
-	config_group_init_type_name(&subsys->passthru_group,
+	config_group_init_type_name(&subsys->passthru.group,
 				    "passthru", &nvmet_passthru_type);
-	configfs_add_default_group(&subsys->passthru_group,
+	configfs_add_default_group(&subsys->passthru.group,
 				   &subsys->group);
 }
 
drivers/nvme/target/core.c +1 -1
index 8eea0a504308..9ab07dbe8cbe 100644
@@ -1646,7 +1646,7 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args)
 #ifdef CONFIG_NVME_TARGET_PASSTHRU
 	/* By default, set loop targets to clear IDS by default */
 	if (ctrl->port->disc_addr.trtype == NVMF_TRTYPE_LOOP)
-		subsys->clear_ids = 1;
+		subsys->passthru.clear_ids = 1;
 #endif
 
 	INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
drivers/nvme/target/nvmet.h +11 -7
index 162e2fdd848e..8f5dccee7d26 100644
@@ -319,6 +319,15 @@ struct nvmet_ctrl {
 	struct nvmet_pr_log_mgr pr_log_mgr;
 };
 
+struct nvmet_passthru {
+	struct nvme_ctrl	*ctrl;
+	char			*ctrl_path;
+	struct config_group	group;
+	unsigned int		admin_timeout;
+	unsigned int		io_timeout;
+	unsigned int		clear_ids;
+};
+
 struct nvmet_subsys {
 	enum nvme_subsys_type	type;
 
@@ -358,12 +367,7 @@ struct nvmet_subsys {
 	char			*firmware_rev;
 
 #ifdef CONFIG_NVME_TARGET_PASSTHRU
-	struct nvme_ctrl	*passthru_ctrl;
-	char			*passthru_ctrl_path;
-	struct config_group	passthru_group;
-	unsigned int		admin_timeout;
-	unsigned int		io_timeout;
-	unsigned int		clear_ids;
+	struct nvmet_passthru	passthru;
 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
 
 #ifdef CONFIG_BLK_DEV_ZONED
@@ -793,7 +797,7 @@ u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req);
 u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req);
 static inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys)
 {
-	return subsys->passthru_ctrl;
+	return subsys->passthru.ctrl;
 }
 #else /* CONFIG_NVME_TARGET_PASSTHRU */
 static inline void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys)
drivers/nvme/target/passthru.c +24 -20

135 lines not on this page · raw message

Signature · 1 line
2.53.0
#4 lore raw

PATCH 2/2 nvmet: fix use-after-free error in passthru I/O hotpath

In reply to Nilay Shroff

Concurrently disabling a passthru controller while passthru I/Os are in flight can potentially result in a use-after-free. Introduce a percpu refcount, an atomic flag, and an nvmet request flag to track reference held by passthru I/Os and protect the passthru controller lifetime.

When enabling the passthru controller, initialize the percpu refcount and set the enabled flag. Each passthru I/O acquires a reference before entering the passthru hot path and sets the nvmet request flag to record that the reference is held. The reference is released when the I/O completes and the request flag is set.

When disabling the passthru controller, clear the enabled flag, kill the percpu refcount, and wait for all in-flight I/Os to release their references before releasing the passthru controller. Concurrent disable attempts are serialized by the atomic enabled flag: only the first disable attempt observes the flag set and proceeds, while subsequent attempts bail out.

Once disabling starts, new I/Os fail to acquire a live reference and therefore cannot enter the passthru hot path.

---
 drivers/nvme/target/core.c     |  4 +++
 drivers/nvme/target/nvmet.h    | 23 ++++++++++++++
 drivers/nvme/target/passthru.c | 58 ++++++++++++++++++++++++++++------
 3 files changed, 75 insertions(+), 10 deletions(-)
drivers/nvme/target/core.c +4 -0
index 9ab07dbe8cbe..0870977eccf6 100644
@@ -819,6 +819,9 @@ static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
 		nvmet_pr_put_ns_pc_ref(pc_ref);
 	if (ns)
 		nvmet_put_namespace(ns);
+
+	if (req->p.ref_held)
+		nvmet_put_passthru_ref(req);
 }
 
 void nvmet_req_complete(struct nvmet_req *req, u16 status)
@@ -1195,6 +1198,7 @@ bool nvmet_req_init(struct nvmet_req *req, struct nvmet_sq *sq,
 	req->error_loc = NVMET_NO_ERROR_LOC;
 	req->error_slba = 0;
 	req->pc_ref = NULL;
+	req->p.ref_held = false;
 
 	/* no support for fused commands yet */
 	if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
drivers/nvme/target/nvmet.h +23 -0
index 8f5dccee7d26..2953102a90bd 100644
@@ -320,6 +320,11 @@ struct nvmet_ctrl {
 };
 
 struct nvmet_passthru {
+	struct percpu_ref	ref;
+	struct completion	disable_done;
+#define NVMET_PASSTHRU_ENABLED	0
+	unsigned long		flags;
+
 	struct nvme_ctrl	*ctrl;
 	char			*ctrl_path;
 	struct config_group	group;
@@ -478,6 +483,7 @@ struct nvmet_req {
 			struct request		*rq;
 			struct work_struct      work;
 			bool			use_workqueue;
+			bool			ref_held;
 		} p;
 #ifdef CONFIG_BLK_DEV_ZONED
 		struct {
@@ -799,6 +805,16 @@ static inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys)
 {
 	return subsys->passthru.ctrl;
 }
+
+static inline bool nvmet_get_passthru_ref(struct nvmet_req *req)
+{
+	return percpu_ref_tryget_live(&nvmet_req_subsys(req)->passthru.ref);
+}
+
+static inline void nvmet_put_passthru_ref(struct nvmet_req *req)
+{
+	percpu_ref_put(&nvmet_req_subsys(req)->passthru.ref);
+}
 #else /* CONFIG_NVME_TARGET_PASSTHRU */
 static inline void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys)
 {
@@ -818,6 +834,13 @@ static inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys)
 {
 	return NULL;
 }
+static inline bool nvmet_get_passthru_ref(struct nvmet_req *req)
+{
+	return NULL;
+}
+static inline void nvmet_put_passthru_ref(struct nvmet_req *req)
+{
+}
 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
 
 static inline bool nvmet_is_passthru_req(struct nvmet_req *req)
drivers/nvme/target/passthru.c +48 -10

106 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/2] nvmet: introduce struct nvmet_passthru

In reply to Nilay Shroff
Quoted 9 lines · Nilay Shroff

Currently, passthru parameters are grouped under struct nvmet_subsys. Since passthru can be configured through configfs and all passthru parameters are exposed under a separate configfs subdirectory, group these parameters under a new struct nvmet_passthru.

Grouping passthru-specific parameters in a separate structure makes the code easier to maintain and reason about.

There are no functional changes intended.

If the fields are move out anyway, should they be a separate, dynamically allocated object so that non-passthrough users don't pay for the memory allocation?

#3 lore raw

Re: [PATCH 1/2] nvmet: introduce struct nvmet_passthru

Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Nilay Shroff

Currently, passthru parameters are grouped under struct nvmet_subsys. Since passthru can be configured through configfs and all passthru parameters are exposed under a separate configfs subdirectory, group these parameters under a new struct nvmet_passthru.

Quoted 9 lines
Quoted 4 lines

Grouping passthru-specific parameters in a separate structure makes the code easier to maintain and reason about.

There are no functional changes intended.

If the fields are move out anyway, should they be a separate, dynamically allocated object so that non-passthrough users don't pay for the memory allocation?

Yes makes sense. I'll change it to the dynamically allocated object in next revision.

Thanks,
--Nilay