Skip to contentSkip to conversation

nvme: add csi, ms and nuse to sysfs

PATCH Superseded Daniel Wagner 4 participants 12 messages Dec 6, 2023 – Dec 7, 2023

Subsystems (4)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover nvme: add csi, ms and nuse to sysfs

I'v dropped the RFC status and tested it lot more which found an obvious bug. When initializating the disk in nvme_alloc_ns, the ns->head pointer was still NULL. This is why I added the 'nvme: initialize head before namespace' patch.

I can't really explain why my testing didn't catch this earlier. Anyway, I rebuild my test setup completely to make sure I got the most obvious things tested. The blktests with loop and nvme-tcp pass, nvme-rdma breaks but so does the base line test. So not a regression by this series.

Thanks,
Daniel

libnvme changes:
https://github.com/igaw/libnvme/tree/tree-no-cmd

changes:
v3:
- cut overlong lines shorter
- fixed disk (queuedata) initialization order
- more testing with blktest
- added nuse ratelimit
- added reviewed tags

v2:
- moved ns id data to nvme_ns_head
- dropped ds, nsze
- https://lore.kernel.org/linux-nvme/[email protected]/

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

Daniel Wagner (4):
nvme: lookup ctrl from request instead from namespace
nvme: initialize head before namespace
nvme: move ns id info to struct nvme_ns_head
nvme: add csi, ms and nuse to sysfs

 drivers/nvme/host/apple.c      |   4 +-
 drivers/nvme/host/core.c       | 279 +++++++++++++++++++--------------
 drivers/nvme/host/fc.c         |   4 +-
 drivers/nvme/host/ioctl.c      |  20 +--
 drivers/nvme/host/multipath.c  |  31 ++--
 drivers/nvme/host/nvme.h       |  62 ++++----
 drivers/nvme/host/rdma.c       |  10 +-
 drivers/nvme/host/sysfs.c      |  31 ++++
 drivers/nvme/host/tcp.c        |   8 +-
 drivers/nvme/host/zns.c        |  34 ++--
 drivers/nvme/target/loop.c     |   4 +-
 drivers/nvme/target/passthru.c |   8 +-
 12 files changed, 291 insertions(+), 204 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/4 nvme: lookup ctrl from request instead from namespace

In preparation to use struct nvme_ns_head pointers instead of a struct nvme_ns pointers, get the ctrl pointer from a request. The reason is that there is no easy and fast way to lookup the ctrl from struct nvme_ns_head.

---
 drivers/nvme/host/core.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/nvme/host/core.c +8 -5
index d699f0c8b13e..900c045fcae0 100644
@@ -769,6 +769,7 @@ static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
 		struct nvme_command *cmnd)
 {
 	unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
+	struct nvme_ctrl *ctrl = nvme_req(req)->ctrl;
 	struct nvme_dsm_range *range;
 	struct bio *bio;
 
@@ -786,10 +787,10 @@ static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
 		 * discard page. If that's also busy, it's safe to return
 		 * busy, as we know we can make progress once that's freed.
 		 */
-		if (test_and_set_bit_lock(0, &ns->ctrl->discard_page_busy))
+		if (test_and_set_bit_lock(0, &ctrl->discard_page_busy))
 			return BLK_STS_RESOURCE;
 
-		range = page_address(ns->ctrl->discard_page);
+		range = page_address(ctrl->discard_page);
 	}
 
 	if (queue_max_discard_segments(req->q) == 1) {
@@ -815,8 +816,8 @@ static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
 	}
 
 	if (WARN_ON_ONCE(n != segments)) {
-		if (virt_to_page(range) == ns->ctrl->discard_page)
-			clear_bit_unlock(0, &ns->ctrl->discard_page_busy);
+		if (virt_to_page(range) == ctrl->discard_page)
+			clear_bit_unlock(0, &ctrl->discard_page_busy);
 		else
 			kfree(range);
 		return BLK_STS_IOERR;
@@ -861,9 +862,11 @@ static void nvme_set_ref_tag(struct nvme_ns *ns, struct nvme_command *cmnd,
 static inline blk_status_t nvme_setup_write_zeroes(struct nvme_ns *ns,
 		struct request *req, struct nvme_command *cmnd)
 {
+	struct nvme_ctrl *ctrl = nvme_req(req)->ctrl;
+
 	memset(cmnd, 0, sizeof(*cmnd));
 
-	if (ns->ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
+	if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
 		return nvme_setup_discard(ns, req, cmnd);
 
 	cmnd->write_zeroes.opcode = nvme_cmd_write_zeroes;
Signature · 1 line
2.43.0
#2 lore raw

PATCH v3 2/4 nvme: initialize head before namespace

In reply to Daniel Wagner

In preparation to use struct nvme_ns_head pointers instead of a struct nvme_ns pointers, initialize the head pointer before we create the disk. This allows us to attach the head as private data to the disk object.

---
 drivers/nvme/host/core.c | 46 ++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 21 deletions(-)
drivers/nvme/host/core.c +25 -21
index 900c045fcae0..1fabe1b81de0 100644
@@ -3474,10 +3474,11 @@ static int nvme_global_check_duplicate_ids(struct nvme_subsystem *this,
 	return ret;
 }
 
-static int nvme_init_ns_head(struct nvme_ns *ns, struct nvme_ns_info *info)
+static int nvme_init_ns_head(struct nvme_ctrl *ctrl,
+			     struct nvme_ns_info *info,
+			     struct nvme_ns_head **head)
 {
-	struct nvme_ctrl *ctrl = ns->ctrl;
-	struct nvme_ns_head *head = NULL;
+	struct nvme_ns_head *h = NULL;
 	int ret;
 
 	ret = nvme_global_check_duplicate_ids(ctrl->subsys, &info->ids);
@@ -3499,8 +3500,8 @@ static int nvme_init_ns_head(struct nvme_ns *ns, struct nvme_ns_info *info)
 		 * up at any time.
 		 */
 		nvme_print_device_info(ctrl);
-		if ((ns->ctrl->ops->flags & NVME_F_FABRICS) || /* !PCIe */
-		    ((ns->ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) &&
+		if ((ctrl->ops->flags & NVME_F_FABRICS) || /* !PCIe */
+		    ((ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) &&
 		     info->is_shared)) {
 			dev_err(ctrl->device,
 				"ignoring nsid %d because of duplicate IDs\n",
@@ -3519,8 +3520,8 @@ static int nvme_init_ns_head(struct nvme_ns *ns, struct nvme_ns_info *info)
 	}
 
 	mutex_lock(&ctrl->subsys->lock);
-	head = nvme_find_ns_head(ctrl, info->nsid);
-	if (!head) {
+	h = nvme_find_ns_head(ctrl, info->nsid);
+	if (!h) {
 		ret = nvme_subsys_check_duplicate_ids(ctrl->subsys, &info->ids);
 		if (ret) {
 			dev_err(ctrl->device,
@@ -3528,20 +3529,20 @@ static int nvme_init_ns_head(struct nvme_ns *ns, struct nvme_ns_info *info)
 				info->nsid);
 			goto out_unlock;
 		}
-		head = nvme_alloc_ns_head(ctrl, info);
-		if (IS_ERR(head)) {
-			ret = PTR_ERR(head);
+		h = nvme_alloc_ns_head(ctrl, info);
+		if (IS_ERR(h)) {
+			ret = PTR_ERR(h);
 			goto out_unlock;
 		}
 	} else {
 		ret = -EINVAL;
-		if (!info->is_shared || !head->shared) {
+		if (!info->is_shared || !h->shared) {
 			dev_err(ctrl->device,
 				"Duplicate unshared namespace %d\n",
 				info->nsid);
 			goto out_put_ns_head;
 		}
-		if (!nvme_ns_ids_equal(&head->ids, &info->ids)) {
+		if (!nvme_ns_ids_equal(&h->ids, &info->ids)) {
 			dev_err(ctrl->device,
 				"IDs don't match for shared namespace %d\n",
 					info->nsid);
@@ -3557,13 +3558,12 @@ static int nvme_init_ns_head(struct nvme_ns *ns, struct nvme_ns_info *info)
 		}
 	}
 
-	list_add_tail_rcu(&ns->siblings, &head->list);
-	ns->head = head;
+	*head = h;
 	mutex_unlock(&ctrl->subsys->lock);
 	return 0;
 
 out_put_ns_head:
-	nvme_put_ns_head(head);
+	nvme_put_ns_head(h);
 out_unlock:
 	mutex_unlock(&ctrl->subsys->lock);
 	return ret;
@@ -3615,15 +3615,22 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info)
 	if (!ns)
 		return;
 
+	if (nvme_init_ns_head(ctrl, info, &ns->head))
+		goto out_free_ns;
+
 	disk = blk_mq_alloc_disk(ctrl->tagset, ns);
 	if (IS_ERR(disk))
-		goto out_free_ns;
+		goto out_free_head;
 	disk->fops = &nvme_bdev_ops;
 	disk->private_data = ns;
 
 	ns->disk = disk;
 	ns->queue = disk->queue;
 
+	mutex_lock(&ctrl->subsys->lock);
+	list_add_tail_rcu(&ns->siblings, &ns->head->list);
+	mutex_unlock(&ctrl->subsys->lock);
+
 	if (ctrl->opts && ctrl->opts->data_digest)
 		blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, ns->queue);
 
@@ -3635,9 +3642,6 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info)
 	ns->ctrl = ctrl;
 	kref_init(&ns->kref);
 
-	if (nvme_init_ns_head(ns, info))
-		goto out_cleanup_disk;
-
 	/*
 	 * If multipathing is enabled, the device name for all disks and not
 	 * just those that represent shared namespaces needs to be based on the
@@ -3691,9 +3695,9 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info)
 	if (list_empty(&ns->head->list))
 		list_del_init(&ns->head->entry);
 	mutex_unlock(&ctrl->subsys->lock);
-	nvme_put_ns_head(ns->head);
- out_cleanup_disk:
 	put_disk(disk);
+ out_free_head:
+	nvme_put_ns_head(ns->head);
  out_free_ns:
 	kfree(ns);
 }
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v3 2/4] nvme: initialize head before namespace

Quoted 14 lines · Daniel Wagner

In preparation to use struct nvme_ns_head pointers instead of a struct nvme_ns pointers, initialize the head pointer before we create the disk. This allows us to attach the head as private data to the disk object.

---
 drivers/nvme/host/core.c | 46 ++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 21 deletions(-)
drivers/nvme/host/core.c +0 -0
index 900c045fcae0..1fabe1b81de0 100644

drivers/nvme/host/core.c static int nvme_global_check_duplicate_ids(struct nvme_subsystem *this, · line 3479

Quoted 7 lines
drivers/nvme/host/core.c +3 -1
@@ -3474,10 +3474,11 @@ static int nvme_global_check_duplicate_ids(struct nvme_subsystem *this,
 	return ret;
 }
 
-static int nvme_init_ns_head(struct nvme_ns *ns, struct nvme_ns_info *info)
+static int nvme_init_ns_head(struct nvme_ctrl *ctrl,
+			     struct nvme_ns_info *info,
+			     struct nvme_ns_head **head)

Can we just return the head or an ERR_PTR here instead of an additional argument? That would also remove the need for the variable renaming below.

I'd also rename the function to nvme_find_or_alloc_ns_head if you're at it.

drivers/nvme/host/core.c static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info) · line 3632

Quoted 3 lines
drivers/nvme/host/core.c +3 -0
+	mutex_lock(&ctrl->subsys->lock);
+	list_add_tail_rcu(&ns->siblings, &ns->head->list);
+	mutex_unlock(&ctrl->subsys->lock);

This can't race with someone else adding the ns as all scanning is from the scan work item. Maybe ad da comment on why this pattern is safe? Because I think it wasn't when the code was originally added..

Otherwise this looks good to me.

#4 lore raw

PATCH v3 3/4 nvme: move ns id info to struct nvme_ns_head

In reply to Daniel Wagner

Move the namesapce info to struct nvme_ns_head, because it's the same for all associated namespaces.

The head pointer is accessible from the ns pointer so we could just update all places with ns->x to ns->head->x. While this is okay for the slow path, it's not for the fast path. Thus we store the head pointer as private data into request_queue.

---
 drivers/nvme/host/apple.c      |   4 +-
 drivers/nvme/host/core.c       | 192 +++++++++++++++++----------------
 drivers/nvme/host/fc.c         |   4 +-
 drivers/nvme/host/ioctl.c      |  20 ++--
 drivers/nvme/host/multipath.c  |  31 +++---
 drivers/nvme/host/nvme.h       |  60 ++++++-----
 drivers/nvme/host/rdma.c       |  10 +-
 drivers/nvme/host/tcp.c        |   8 +-
 drivers/nvme/host/zns.c        |  34 +++---
 drivers/nvme/target/loop.c     |   4 +-
 drivers/nvme/target/passthru.c |   8 +-
 11 files changed, 197 insertions(+), 178 deletions(-)
drivers/nvme/host/apple.c +2 -2
index 596bb11eeba5..04c1c5fbd4d1 100644
@@ -732,7 +732,7 @@ static int apple_nvme_remove_sq(struct apple_nvme *anv)
 static blk_status_t apple_nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
 					const struct blk_mq_queue_data *bd)
 {
-	struct nvme_ns *ns = hctx->queue->queuedata;
+	struct nvme_ns_head *head = hctx->queue->queuedata;
 	struct apple_nvme_queue *q = hctx->driver_data;
 	struct apple_nvme *anv = queue_to_apple_nvme(q);
 	struct request *req = bd->rq;
@@ -753,7 +753,7 @@ static blk_status_t apple_nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
 	if (!nvme_check_ready(&anv->ctrl, req, true))
 		return nvme_fail_nonready_command(&anv->ctrl, req);
 
-	ret = nvme_setup_cmd(ns, req);
+	ret = nvme_setup_cmd(head, req);
 	if (ret)
 		return ret;
 
drivers/nvme/host/core.c +102 -90

490 lines not on this page · raw message

drivers/nvme/host/fc.c +2 -2
index fb22976a36a8..41f9bacae6f4 100644
@@ -2799,7 +2799,7 @@ static blk_status_t
 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
 			const struct blk_mq_queue_data *bd)
 {
-	struct nvme_ns *ns = hctx->queue->queuedata;
+	struct nvme_ns_head *head = hctx->queue->queuedata;
 	struct nvme_fc_queue *queue = hctx->driver_data;
 	struct nvme_fc_ctrl *ctrl = queue->ctrl;
 	struct request *rq = bd->rq;
@@ -2813,7 +2813,7 @@ nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
 	    !nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
 		return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
 
-	ret = nvme_setup_cmd(ns, rq);
+	ret = nvme_setup_cmd(head, rq);
 	if (ret)
 		return ret;
 
drivers/nvme/host/ioctl.c +10 -10
index 529b9954d2b8..29263009bb6e 100644
@@ -61,7 +61,7 @@ static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c,
 	 * and marks this command as supported.  If not reject unprivileged
 	 * passthrough.
 	 */
-	effects = nvme_command_effects(ns->ctrl, ns, c->common.opcode);
+	effects = nvme_command_effects(ns->ctrl, ns->head, c->common.opcode);
 	if (!(effects & NVME_CMD_EFFECTS_CSUPP))
 		return false;
 
@@ -168,8 +168,8 @@ static int nvme_map_user_request(struct request *req, u64 ubuffer,
 		unsigned int flags)
 {
 	struct request_queue *q = req->q;
-	struct nvme_ns *ns = q->queuedata;
-	struct block_device *bdev = ns ? ns->disk->part0 : NULL;
+	struct nvme_ns_head *head = q->queuedata;
+	struct block_device *bdev = head ? head->disk->part0 : NULL;
 	struct bio *bio = NULL;
 	void *meta = NULL;
 	int ret;
@@ -222,7 +222,7 @@ static int nvme_submit_user_cmd(struct request_queue *q,
 		void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
 		u64 *result, unsigned timeout, unsigned int flags)
 {
-	struct nvme_ns *ns = q->queuedata;
+	struct nvme_ns_head *head = q->queuedata;
 	struct nvme_ctrl *ctrl;
 	struct request *req;
 	void *meta = NULL;
@@ -245,7 +245,7 @@ static int nvme_submit_user_cmd(struct request_queue *q,
 	bio = req->bio;
 	ctrl = nvme_req(req)->ctrl;
 
-	effects = nvme_passthru_start(ctrl, ns, cmd->common.opcode);
+	effects = nvme_passthru_start(ctrl, head, cmd->common.opcode);
 	ret = nvme_execute_rq(req, false);
 	if (result)
 		*result = le64_to_cpu(nvme_req(req)->result.u64);
@@ -257,7 +257,7 @@ static int nvme_submit_user_cmd(struct request_queue *q,
 	blk_mq_free_request(req);
 
 	if (effects)
-		nvme_passthru_end(ctrl, ns, effects, cmd, ret);
+		nvme_passthru_end(ctrl, head, effects, cmd, ret);
 
 	return ret;
 }
@@ -283,10 +283,10 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
 		return -EINVAL;
 	}
 
-	length = (io.nblocks + 1) << ns->lba_shift;
+	length = (io.nblocks + 1) << ns->head->lba_shift;
 
 	if ((io.control & NVME_RW_PRINFO_PRACT) &&
-	    ns->ms == sizeof(struct t10_pi_tuple)) {
+	    ns->head->ms == sizeof(struct t10_pi_tuple)) {
 		/*
 		 * Protection information is stripped/inserted by the
 		 * controller.
@@ -296,11 +296,11 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
 		meta_len = 0;
 		metadata = NULL;
 	} else {
-		meta_len = (io.nblocks + 1) * ns->ms;
+		meta_len = (io.nblocks + 1) * ns->head->ms;
 		metadata = nvme_to_user_ptr(io.metadata);
 	}
 
-	if (ns->features & NVME_NS_EXT_LBAS) {
+	if (ns->head->features & NVME_NS_EXT_LBAS) {
 		length += meta_len;
 		meta_len = 0;
 	} else if (meta_len) {
drivers/nvme/host/multipath.c +17 -14
index 0a88d7bdc5e3..24ccd5b9c350 100644
@@ -82,11 +82,14 @@ void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
 
 void nvme_failover_req(struct request *req)
 {
-	struct nvme_ns *ns = req->q->queuedata;
+	struct nvme_ns_head *head = req->q->queuedata;
+	struct nvme_ctrl *ctrl = nvme_req(req)->ctrl;
+	struct nvme_ns *ns;
 	u16 status = nvme_req(req)->status & 0x7ff;
 	unsigned long flags;
 	struct bio *bio;
 
+	ns = nvme_find_get_ns(ctrl, head->ns_id);
 	nvme_mpath_clear_current_path(ns);
 
 	/*
@@ -94,14 +97,14 @@ void nvme_failover_req(struct request *req)
 	 * ready to serve this namespace.  Kick of a re-read of the ANA
 	 * information page, and just try any other available path for now.
 	 */
-	if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) {
+	if (nvme_is_ana_error(status) && ctrl->ana_log_buf) {
 		set_bit(NVME_NS_ANA_PENDING, &ns->flags);
-		queue_work(nvme_wq, &ns->ctrl->ana_work);
+		queue_work(nvme_wq, &ctrl->ana_work);
 	}
 
-	spin_lock_irqsave(&ns->head->requeue_lock, flags);
+	spin_lock_irqsave(&head->requeue_lock, flags);
 	for (bio = req->bio; bio; bio = bio->bi_next) {
-		bio_set_dev(bio, ns->head->disk->part0);
+		bio_set_dev(bio, head->disk->part0);
 		if (bio->bi_opf & REQ_POLLED) {
 			bio->bi_opf &= ~REQ_POLLED;
 			bio->bi_cookie = BLK_QC_T_NONE;
@@ -115,17 +118,17 @@ void nvme_failover_req(struct request *req)
 		 */
 		bio->bi_opf &= ~REQ_NOWAIT;
 	}
-	blk_steal_bios(&ns->head->requeue_list, req);
-	spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
+	blk_steal_bios(&head->requeue_list, req);
+	spin_unlock_irqrestore(&head->requeue_lock, flags);
 
 	blk_mq_end_request(req, 0);
-	kblockd_schedule_work(&ns->head->requeue_work);
+	kblockd_schedule_work(&head->requeue_work);
 }
 
 void nvme_mpath_start_request(struct request *rq)
 {
-	struct nvme_ns *ns = rq->q->queuedata;
-	struct gendisk *disk = ns->head->disk;
+	struct nvme_ns_head *head = rq->q->queuedata;
+	struct gendisk *disk = head->disk;
 
 	if (!blk_queue_io_stat(disk->queue) || blk_rq_is_passthrough(rq))
 		return;
@@ -138,11 +141,11 @@ EXPORT_SYMBOL_GPL(nvme_mpath_start_request);
 
 void nvme_mpath_end_request(struct request *rq)
 {
-	struct nvme_ns *ns = rq->q->queuedata;
+	struct nvme_ns_head *head = rq->q->queuedata;
 
 	if (!(nvme_req(rq)->flags & NVME_MPATH_IO_STATS))
 		return;
-	bdev_end_io_acct(ns->head->disk->part0, req_op(rq),
+	bdev_end_io_acct(head->disk->part0, req_op(rq),
 			 blk_rq_bytes(rq) >> SECTOR_SHIFT,
 			 nvme_req(rq)->start_time);
 }
@@ -202,10 +205,10 @@ void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
 	up_read(&ctrl->namespaces_rwsem);
 }
 
-void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
+void nvme_mpath_revalidate_paths(struct nvme_ns_head *head)
 {
-	struct nvme_ns_head *head = ns->head;
 	sector_t capacity = get_capacity(head->disk);
+	struct nvme_ns *ns;
 	int node;
 	int srcu_idx;
 
drivers/nvme/host/nvme.h +31 -29

129 lines not on this page · raw message

drivers/nvme/host/rdma.c +5 -5
index 81e2621169e5..0e967413ee43 100644
@@ -1407,7 +1407,7 @@ static int nvme_rdma_map_sg_pi(struct nvme_rdma_queue *queue,
 	struct nvme_rdma_sgl *sgl = &req->data_sgl;
 	struct ib_reg_wr *wr = &req->reg_wr;
 	struct request *rq = blk_mq_rq_from_pdu(req);
-	struct nvme_ns *ns = rq->q->queuedata;
+	struct nvme_ns_head *head = rq->q->queuedata;
 	struct bio *bio = rq->bio;
 	struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
 	int nr;
@@ -1423,7 +1423,7 @@ static int nvme_rdma_map_sg_pi(struct nvme_rdma_queue *queue,
 		goto mr_put;
 
 	nvme_rdma_set_sig_attrs(blk_get_integrity(bio->bi_bdev->bd_disk), c,
-				req->mr->sig_attrs, ns->pi_type);
+				req->mr->sig_attrs, head->pi_type);
 	nvme_rdma_set_prot_checks(c, &req->mr->sig_attrs->check_mask);
 
 	ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
@@ -1979,7 +1979,7 @@ static enum blk_eh_timer_return nvme_rdma_timeout(struct request *rq)
 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
 		const struct blk_mq_queue_data *bd)
 {
-	struct nvme_ns *ns = hctx->queue->queuedata;
+	struct nvme_ns_head *head = hctx->queue->queuedata;
 	struct nvme_rdma_queue *queue = hctx->driver_data;
 	struct request *rq = bd->rq;
 	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
@@ -2007,7 +2007,7 @@ static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
 	ib_dma_sync_single_for_cpu(dev, sqe->dma,
 			sizeof(struct nvme_command), DMA_TO_DEVICE);
 
-	ret = nvme_setup_cmd(ns, rq);
+	ret = nvme_setup_cmd(head, rq);
 	if (ret)
 		goto unmap_qe;
 
@@ -2017,7 +2017,7 @@ static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
 	    queue->pi_support &&
 	    (c->common.opcode == nvme_cmd_write ||
 	     c->common.opcode == nvme_cmd_read) &&
-	    nvme_ns_has_pi(ns))
+	    nvme_ns_has_pi(head))
 		req->use_sig_mr = true;
 	else
 		req->use_sig_mr = false;
drivers/nvme/host/tcp.c +4 -4
index 08805f027810..169462faad47 100644
@@ -2483,7 +2483,7 @@ static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
 	return 0;
 }
 
-static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
+static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns_head *head,
 		struct request *rq)
 {
 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
@@ -2492,7 +2492,7 @@ static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
 	u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
 	blk_status_t ret;
 
-	ret = nvme_setup_cmd(ns, rq);
+	ret = nvme_setup_cmd(head, rq);
 	if (ret)
 		return ret;
 
@@ -2548,7 +2548,7 @@ static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx)
 static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
 		const struct blk_mq_queue_data *bd)
 {
-	struct nvme_ns *ns = hctx->queue->queuedata;
+	struct nvme_ns_head *head = hctx->queue->queuedata;
 	struct nvme_tcp_queue *queue = hctx->driver_data;
 	struct request *rq = bd->rq;
 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
@@ -2558,7 +2558,7 @@ static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
 	if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
 		return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
 
-	ret = nvme_tcp_setup_cmd_pdu(ns, rq);
+	ret = nvme_tcp_setup_cmd_pdu(head, rq);
 	if (unlikely(ret))
 		return ret;
 
drivers/nvme/host/zns.c +18 -16
index ec8557810c21..51ac3a61ce3f 100644
@@ -11,7 +11,7 @@ int nvme_revalidate_zones(struct nvme_ns *ns)
 {
 	struct request_queue *q = ns->queue;
 
-	blk_queue_chunk_sectors(q, ns->zsze);
+	blk_queue_chunk_sectors(q, ns->head->zsze);
 	blk_queue_max_zone_append_sectors(q, ns->ctrl->max_zone_append);
 
 	return blk_revalidate_disk_zones(ns->disk, NULL);
@@ -99,11 +99,12 @@ int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf)
 		goto free_data;
 	}
 
-	ns->zsze = nvme_lba_to_sect(ns, le64_to_cpu(id->lbafe[lbaf].zsze));
-	if (!is_power_of_2(ns->zsze)) {
+	ns->head->zsze =
+		nvme_lba_to_sect(ns->head, le64_to_cpu(id->lbafe[lbaf].zsze));
+	if (!is_power_of_2(ns->head->zsze)) {
 		dev_warn(ns->ctrl->device,
 			"invalid zone size:%llu for namespace:%u\n",
-			ns->zsze, ns->head->ns_id);
+			ns->head->zsze, ns->head->ns_id);
 		status = -ENODEV;
 		goto free_data;
 	}
@@ -128,7 +129,7 @@ static void *nvme_zns_alloc_report_buffer(struct nvme_ns *ns,
 				   sizeof(struct nvme_zone_descriptor);
 
 	nr_zones = min_t(unsigned int, nr_zones,
-			 get_capacity(ns->disk) >> ilog2(ns->zsze));
+			 get_capacity(ns->head->disk) >> ilog2(ns->head->zsze));
 
 	bufsize = sizeof(struct nvme_zone_report) +
 		nr_zones * sizeof(struct nvme_zone_descriptor);
@@ -162,13 +163,13 @@ static int nvme_zone_parse_entry(struct nvme_ns *ns,
 
 	zone.type = BLK_ZONE_TYPE_SEQWRITE_REQ;
 	zone.cond = entry->zs >> 4;
-	zone.len = ns->zsze;
-	zone.capacity = nvme_lba_to_sect(ns, le64_to_cpu(entry->zcap));
-	zone.start = nvme_lba_to_sect(ns, le64_to_cpu(entry->zslba));
+	zone.len = ns->head->zsze;
+	zone.capacity = nvme_lba_to_sect(ns->head, le64_to_cpu(entry->zcap));
+	zone.start = nvme_lba_to_sect(ns->head, le64_to_cpu(entry->zslba));
 	if (zone.cond == BLK_ZONE_COND_FULL)
 		zone.wp = zone.start + zone.len;
 	else
-		zone.wp = nvme_lba_to_sect(ns, le64_to_cpu(entry->wp));
+		zone.wp = nvme_lba_to_sect(ns->head, le64_to_cpu(entry->wp));
 
 	return cb(&zone, idx, data);
 }
@@ -196,11 +197,11 @@ int nvme_ns_report_zones(struct nvme_ns *ns, sector_t sector,
 	c.zmr.zrasf = NVME_ZRASF_ZONE_REPORT_ALL;
 	c.zmr.pr = NVME_REPORT_ZONE_PARTIAL;
 
-	sector &= ~(ns->zsze - 1);
+	sector &= ~(ns->head->zsze - 1);
 	while (zone_idx < nr_zones && sector < get_capacity(ns->disk)) {
 		memset(report, 0, buflen);
 
-		c.zmr.slba = cpu_to_le64(nvme_sect_to_lba(ns, sector));
+		c.zmr.slba = cpu_to_le64(nvme_sect_to_lba(ns->head, sector));
 		ret = nvme_submit_sync_cmd(ns->queue, &c, report, buflen);
 		if (ret) {
 			if (ret > 0)
@@ -220,7 +221,7 @@ int nvme_ns_report_zones(struct nvme_ns *ns, sector_t sector,
 			zone_idx++;
 		}
 
-		sector += ns->zsze * nz;
+		sector += ns->head->zsze * nz;
 	}
 
 	if (zone_idx > 0)
@@ -232,14 +233,15 @@ int nvme_ns_report_zones(struct nvme_ns *ns, sector_t sector,
 	return ret;
 }
 
-blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns *ns, struct request *req,
-		struct nvme_command *c, enum nvme_zone_mgmt_action action)
+blk_status_t nvme_setup_zone_mgmt_send(struct nvme_ns_head *head,
+		struct request *req, struct nvme_command *c,
+		enum nvme_zone_mgmt_action action)
 {
 	memset(c, 0, sizeof(*c));
 
 	c->zms.opcode = nvme_cmd_zone_mgmt_send;
-	c->zms.nsid = cpu_to_le32(ns->head->ns_id);
-	c->zms.slba = cpu_to_le64(nvme_sect_to_lba(ns, blk_rq_pos(req)));
+	c->zms.nsid = cpu_to_le32(head->ns_id);
+	c->zms.slba = cpu_to_le64(nvme_sect_to_lba(head, blk_rq_pos(req)));
 	c->zms.zsa = action;
 
 	if (req_op(req) == REQ_OP_ZONE_RESET_ALL)
drivers/nvme/target/loop.c +2 -2
index 9cb434c58075..1c3abadecaa7 100644
@@ -131,7 +131,7 @@ static void nvme_loop_execute_work(struct work_struct *work)
 static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx,
 		const struct blk_mq_queue_data *bd)
 {
-	struct nvme_ns *ns = hctx->queue->queuedata;
+	struct nvme_ns_head *head = hctx->queue->queuedata;
 	struct nvme_loop_queue *queue = hctx->driver_data;
 	struct request *req = bd->rq;
 	struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
@@ -141,7 +141,7 @@ static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx,
 	if (!nvme_check_ready(&queue->ctrl->ctrl, req, queue_ready))
 		return nvme_fail_nonready_command(&queue->ctrl->ctrl, req);
 
-	ret = nvme_setup_cmd(ns, req);
+	ret = nvme_setup_cmd(head, req);
 	if (ret)
 		return ret;
 
drivers/nvme/target/passthru.c +4 -4
index 9fe07d7efa96..7c13084a8f87 100644
@@ -216,11 +216,11 @@ static void nvmet_passthru_execute_cmd_work(struct work_struct *w)
 	struct nvmet_req *req = container_of(w, struct nvmet_req, p.work);
 	struct request *rq = req->p.rq;
 	struct nvme_ctrl *ctrl = nvme_req(rq)->ctrl;
-	struct nvme_ns *ns = rq->q->queuedata;
+	struct nvme_ns_head *head = rq->q->queuedata;
 	u32 effects;
 	int status;
 
-	effects = nvme_passthru_start(ctrl, ns, req->cmd->common.opcode);
+	effects = nvme_passthru_start(ctrl, head, req->cmd->common.opcode);
 	status = nvme_execute_rq(rq, false);
 	if (status == NVME_SC_SUCCESS &&
 	    req->cmd->common.opcode == nvme_admin_identify) {
@@ -243,7 +243,7 @@ static void nvmet_passthru_execute_cmd_work(struct work_struct *w)
 	blk_mq_free_request(rq);
 
 	if (effects)
-		nvme_passthru_end(ctrl, ns, effects, req->cmd, status);
+		nvme_passthru_end(ctrl, head, effects, req->cmd, status);
 }
 
 static enum rq_end_io_ret nvmet_passthru_req_done(struct request *rq,
@@ -339,7 +339,7 @@ static void nvmet_passthru_execute_cmd(struct nvmet_req *req)
 	 * non-trivial effects, make sure to execute the command synchronously
 	 * in a workqueue so that nvme_passthru_end gets called.
 	 */
-	effects = nvme_command_effects(ctrl, ns, req->cmd->common.opcode);
+	effects = nvme_command_effects(ctrl, ns->head, req->cmd->common.opcode);
 	if (req->p.use_workqueue ||
 	    (effects & ~(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC))) {
 		INIT_WORK(&req->p.work, nvmet_passthru_execute_cmd_work);
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v3 3/4] nvme: move ns id info to struct nvme_ns_head

Quoted 6 lines · Daniel Wagner

Move the namesapce info to struct nvme_ns_head, because it's the same for all associated namespaces.

The head pointer is accessible from the ns pointer so we could just update all places with ns->x to ns->head->x. While this is okay for the slow path,

Do you have any data to show that it matters? All the I/O command setup functions already access the ns_head for ->ns_id, so looking at more fields can't really make any difference.

If we have a good argument about reducing the pointer chasing I'm all for it, but please as a separate, well-documented commit that also explains the tradeoffs for the newly added lookups this adds in a few places.

#6 lore raw

Re: [PATCH v3 3/4] nvme: move ns id info to struct nvme_ns_head

Quoted 11 lines · Christoph Hellwig
Quoted 6 lines · Daniel Wagner

Move the namesapce info to struct nvme_ns_head, because it's the same for all associated namespaces.

The head pointer is accessible from the ns pointer so we could just update all places with ns->x to ns->head->x. While this is okay for the slow path,

Do you have any data to show that it matters? All the I/O command setup functions already access the ns_head for ->ns_id, so looking at more fields can't really make any difference.

I've splitted the patch so that the first patch just moves the variables around and changes ns->x to ns->head->x ('patched'). Then I changed the layout of nvme_ns_head, so that all variables used in nvme_setup_rw() are in one cacheline ('cache line optimized') and the last change is passing the nvme_ns_head pointer around ('use nvme_ns_head directly')

I assume that nvme_setup_rw is the function which is used most and thus I've tried to benchmark nvme_setup_rw with issuing 4k reads. I am sure my benchmark setup is not perfect but well that's what I have.

Anyway, the results are pointing towards that moving the variables to nvme_ns_head has a slight performance impact but that can be more than mitigated by optimizing the cacheline access. The change to use nvme_ns_head directly seems to eat up all the cacheline optimization gains again.

'patched' layout:

struct nvme_ns_head {

	struct list_head           list;                 /*     0    16 */
	struct srcu_struct         srcu;                 /*    16    72 */

/* --- cacheline 1 boundary (64 bytes) was 24 bytes ago --- */
struct nvme_subsystem * subsys; /* 88 8 */
unsigned int ns_id; /* 96 4 */
struct nvme_ns_ids ids; /* 100 41 */

/* XXX 3 bytes hole, try to pack */

/* --- cacheline 2 boundary (128 bytes) was 16 bytes ago --- */
struct list_head entry; /* 144 16 */
struct kref ref; /* 160 4 */
bool shared; /* 164 1 */

/* XXX 3 bytes hole, try to pack */

int instance; /* 168 4 */

/* XXX 4 bytes hole, try to pack */

struct nvme_effects_log * effects; /* 176 8 */
int lba_shift; /* 184 4 */
u16 ms; /* 188 2 */
u16 pi_size; /* 190 2 */
/* --- cacheline 3 boundary (192 bytes) --- */
u16 sgs; /* 192 2 */

/* XXX 2 bytes hole, try to pack */

u32 sws; /* 196 4 */
u64 nuse; /* 200 8 */
u8 pi_type; /* 208 1 */
u8 guard_type; /* 209 1 */

/* XXX 6 bytes hole, try to pack */

u64 zsze; /* 216 8 */
unsigned long features; /* 224 8 */
struct ratelimit_state rs_nuse; /* 232 104 */
/* --- cacheline 5 boundary (320 bytes) was 16 bytes ago --- */

[...]
}

'cacheline optimized' layout:

struct nvme_ns_head {

	struct list_head           list;                 /*     0    16 */
	struct srcu_struct         srcu;                 /*    16    72 */

/* --- cacheline 1 boundary (64 bytes) was 24 bytes ago --- */
struct nvme_subsystem * subsys; /* 88 8 */
struct nvme_ns_ids ids; /* 96 41 */

/* XXX 7 bytes hole, try to pack */

/* --- cacheline 2 boundary (128 bytes) was 16 bytes ago --- */
struct list_head entry; /* 144 16 */
struct kref ref; /* 160 4 */
bool shared; /* 164 1 */

/* XXX 3 bytes hole, try to pack */

int instance; /* 168 4 */

/* XXX 4 bytes hole, try to pack */

struct nvme_effects_log * effects; /* 176 8 */
u64 nuse; /* 184 8 */
/* --- cacheline 3 boundary (192 bytes) --- */
unsigned int ns_id; /* 192 4 */
int lba_shift; /* 196 4 */
u16 ms; /* 200 2 */
u16 pi_size; /* 202 2 */
u8 pi_type; /* 204 1 */
u8 guard_type; /* 205 1 */
u16 sgs; /* 206 2 */
u32 sws; /* 208 4 */
[...]
}

fio test job:
[global]
name=nvme-read
time_based
ramp_time=30
runtime=120
readwrite=read
bs=4k
ioengine=io_uring
direct=1
numjobs=4
iodepth=64
group_reporting=1[nvme0]
new_group
filename=/dev/nvme0n1
cpus_allowed=1-4
cpus_allowed_policy=split

[nvme0]
new_group
filename=/dev/nvme0n1

bandwidth

      'baseline'  'patched'  'cache line optimized' 'use nvme_ns_head directly'
      1608        1632       1613                   1618
      1608        1610       1634                   1618
      1623        1639       1642                   1646
      1638        1610       1640                   1619
      1637        1611       1642                   1620
avg   1622.8      1620.4     1634.2                 1624.2
stdev 14.75       14.01      12.29                  12.21

ios

      'baseline'  'patched'  'cache line optimized' 'use nvme_ns_head directly'
      65626946    66735998   66268893               66458877
      65641469    66041634   66888910               66384526
      66012335    66904002   67132768               67329550
      66589757    66013222   67132053               66491121
      66569213    66033040   67132075               66474708
avg   66087944    66345579.2 66910939.8             66627756.4
stdev 474608.24   437260.67  374068.50              394426.34
#7 lore raw

Re: [PATCH v3 3/4] nvme: move ns id info to struct nvme_ns_head

In reply to Daniel Wagner
kernel test robot noticed the following build errors:

Hi Daniel,

kernel test robot noticed the following build errors:

[auto build test ERROR on v6.7-rc4]
[also build test ERROR on linus/master next-20231206]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Wagner/nvme-lookup-ctrl-from-request-instead-from-namespace/20231206-161455
base:   v6.7-rc4
patch link:    https://lore.kernel.org/r/20231206081244.32733-4-dwagner%40suse.de
patch subject: [PATCH v3 3/4] nvme: move ns id info to struct nvme_ns_head
config: i386-buildonly-randconfig-003-20231206 (https://download.01.org/0day-ci/archive/20231207/[email protected]/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231207/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/nvme/host/core.c:2034:31: error: incompatible pointer types passing 'struct nvme_ns_head *' to parameter of type 'struct nvme_ns *' [-Werror,-Wincompatible-pointer-types]
                   nvme_mpath_revalidate_paths(ns->head);
                                               ^~~~~~~~
   drivers/nvme/host/nvme.h:945:64: note: passing argument to parameter 'ns' here
   static inline void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
                                                                  ^
   drivers/nvme/host/core.c:2109:31: error: incompatible pointer types passing 'struct nvme_ns_head *' to parameter of type 'struct nvme_ns *' [-Werror,-Wincompatible-pointer-types]
                   nvme_mpath_revalidate_paths(ns->head);
                                               ^~~~~~~~
   drivers/nvme/host/nvme.h:945:64: note: passing argument to parameter 'ns' here
   static inline void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
                                                                  ^

2 errors generated.

vim +2034 drivers/nvme/host/core.c

2022
2023 static int nvme_update_ns_info_generic(struct nvme_ns *ns,
2024 struct nvme_ns_info *info)
2025 {
2026 blk_mq_freeze_queue(ns->disk->queue);
2027 nvme_set_queue_limits(ns->ctrl, ns->queue);
2028 set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
2029 blk_mq_unfreeze_queue(ns->disk->queue);
2030
2031 if (nvme_ns_head_multipath(ns->head)) {
2032 blk_mq_freeze_queue(ns->head->disk->queue);
2033 set_disk_ro(ns->head->disk, nvme_ns_is_readonly(ns, info));

> 2034			nvme_mpath_revalidate_paths(ns->head);

2035 blk_stack_limits(&ns->head->disk->queue->limits,
2036 &ns->queue->limits, 0);
2037 ns->head->disk->flags |= GENHD_FL_HIDDEN;
2038 blk_mq_unfreeze_queue(ns->head->disk->queue);
2039 }
2040
2041 /* Hide the block-interface for these devices */
2042 ns->disk->flags |= GENHD_FL_HIDDEN;
2043 set_bit(NVME_NS_READY, &ns->flags);
2044
2045 return 0;
2046 }
2047

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#8 lore raw

Re: [PATCH v3 3/4] nvme: move ns id info to struct nvme_ns_head

In reply to Daniel Wagner
kernel test robot noticed the following build errors:

Hi Daniel,

kernel test robot noticed the following build errors:

[auto build test ERROR on v6.7-rc4]
[also build test ERROR on linus/master next-20231206]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Wagner/nvme-lookup-ctrl-from-request-instead-from-namespace/20231206-161455
base:   v6.7-rc4
patch link:    https://lore.kernel.org/r/20231206081244.32733-4-dwagner%40suse.de
patch subject: [PATCH v3 3/4] nvme: move ns id info to struct nvme_ns_head
config: arm64-defconfig (https://download.01.org/0day-ci/archive/20231207/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231207/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/nvme/host/core.c: In function 'nvme_update_ns_info_generic':

>> drivers/nvme/host/core.c:2034:47: error: passing argument 1 of 'nvme_mpath_revalidate_paths' from incompatible pointer type [-Werror=incompatible-pointer-types]
    2034 |                 nvme_mpath_revalidate_paths(ns->head);
         |                                             ~~^~~~~~
         |                                               |
         |                                               struct nvme_ns_head *
   In file included from drivers/nvme/host/core.c:25:
   drivers/nvme/host/nvme.h:945:64: note: expected 'struct nvme_ns *' but argument is of type 'struct nvme_ns_head *'
     945 | static inline void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
         |                                                ~~~~~~~~~~~~~~~~^~
   drivers/nvme/host/core.c: In function 'nvme_update_ns_info_block':
   drivers/nvme/host/core.c:2109:47: error: passing argument 1 of 'nvme_mpath_revalidate_paths' from incompatible pointer type [-Werror=incompatible-pointer-types]
    2109 |                 nvme_mpath_revalidate_paths(ns->head);
         |                                             ~~^~~~~~
         |                                               |
         |                                               struct nvme_ns_head *

drivers/nvme/host/nvme.h:945:64: note: expected 'struct nvme_ns *' but argument is of type 'struct nvme_ns_head *'
945 | static inline void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
| ~~~~~~~~~~~~~~~~^~
cc1: some warnings being treated as errors

vim +/nvme_mpath_revalidate_paths +2034 drivers/nvme/host/core.c

2022
2023 static int nvme_update_ns_info_generic(struct nvme_ns *ns,
2024 struct nvme_ns_info *info)
2025 {
2026 blk_mq_freeze_queue(ns->disk->queue);
2027 nvme_set_queue_limits(ns->ctrl, ns->queue);
2028 set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
2029 blk_mq_unfreeze_queue(ns->disk->queue);
2030
2031 if (nvme_ns_head_multipath(ns->head)) {
2032 blk_mq_freeze_queue(ns->head->disk->queue);
2033 set_disk_ro(ns->head->disk, nvme_ns_is_readonly(ns, info));

> 2034			nvme_mpath_revalidate_paths(ns->head);

2035 blk_stack_limits(&ns->head->disk->queue->limits,
2036 &ns->queue->limits, 0);
2037 ns->head->disk->flags |= GENHD_FL_HIDDEN;
2038 blk_mq_unfreeze_queue(ns->head->disk->queue);
2039 }
2040
2041 /* Hide the block-interface for these devices */
2042 ns->disk->flags |= GENHD_FL_HIDDEN;
2043 set_bit(NVME_NS_READY, &ns->flags);
2044
2045 return 0;
2046 }
2047

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#9 lore raw

Re: [PATCH v3 3/4] nvme: move ns id info to struct nvme_ns_head

In reply to Daniel Wagner

Hi Daniel,

kernel test robot noticed the following build warnings:

https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Wagner/nvme-lookup-ctrl-from-request-instead-from-namespace/20231206-161455
base:   v6.7-rc4
patch link:    https://lore.kernel.org/r/20231206081244.32733-4-dwagner%40suse.de
patch subject: [PATCH v3 3/4] nvme: move ns id info to struct nvme_ns_head
config: i386-randconfig-141-20231207 (https://download.01.org/0day-ci/archive/20231207/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-12) 11.3.0
reproduce: (https://download.01.org/0day-ci/archive/20231207/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
drivers/nvme/target/passthru.c:354 nvmet_passthru_execute_cmd() warn: variable dereferenced before check 'ns' (see line 342)

vim +/ns +354 drivers/nvme/target/passthru.c

c1fef73f793b7f Logan Gunthorpe    2020-07-24  292  static void nvmet_passthru_execute_cmd(struct nvmet_req *req)
c1fef73f793b7f Logan Gunthorpe    2020-07-24  293  {
ab7a2737ac5acd Christoph Hellwig  2021-08-27  294  	struct nvme_ctrl *ctrl = nvmet_req_subsys(req)->passthru_ctrl;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  295  	struct request_queue *q = ctrl->admin_q;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  296  	struct nvme_ns *ns = NULL;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  297  	struct request *rq = NULL;
47e9730c26a4a5 Chaitanya Kulkarni 2020-11-09  298  	unsigned int timeout;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  299  	u32 effects;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  300  	u16 status;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  301  	int ret;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  302  
c1fef73f793b7f Logan Gunthorpe    2020-07-24  303  	if (likely(req->sq->qid != 0)) {
c1fef73f793b7f Logan Gunthorpe    2020-07-24  304  		u32 nsid = le32_to_cpu(req->cmd->common.nsid);
c1fef73f793b7f Logan Gunthorpe    2020-07-24  305  
c1fef73f793b7f Logan Gunthorpe    2020-07-24  306  		ns = nvme_find_get_ns(ctrl, nsid);
c1fef73f793b7f Logan Gunthorpe    2020-07-24  307  		if (unlikely(!ns)) {
c1fef73f793b7f Logan Gunthorpe    2020-07-24  308  			pr_err("failed to get passthru ns nsid:%u\n", nsid);
c1fef73f793b7f Logan Gunthorpe    2020-07-24  309  			status = NVME_SC_INVALID_NS | NVME_SC_DNR;
4db69a3d7cfe31 Chaitanya Kulkarni 2020-08-06  310  			goto out;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  311  		}
c1fef73f793b7f Logan Gunthorpe    2020-07-24  312  
c1fef73f793b7f Logan Gunthorpe    2020-07-24  313  		q = ns->queue;
20c2c3bb83f26c Chaitanya Kulkarni 2021-02-09  314  		timeout = nvmet_req_subsys(req)->io_timeout;
a2f6a2b8ce43db Chaitanya Kulkarni 2020-11-09  315  	} else {
20c2c3bb83f26c Chaitanya Kulkarni 2021-02-09  316  		timeout = nvmet_req_subsys(req)->admin_timeout;

ns is NULL here

c1fef73f793b7f Logan Gunthorpe    2020-07-24  317  	}
c1fef73f793b7f Logan Gunthorpe    2020-07-24  318  
e559398f47e090 Christoph Hellwig  2022-03-15  319  	rq = blk_mq_alloc_request(q, nvme_req_op(req->cmd), 0);
c1fef73f793b7f Logan Gunthorpe    2020-07-24  320  	if (IS_ERR(rq)) {
c1fef73f793b7f Logan Gunthorpe    2020-07-24  321  		status = NVME_SC_INTERNAL;
4db69a3d7cfe31 Chaitanya Kulkarni 2020-08-06  322  		goto out_put_ns;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  323  	}
e559398f47e090 Christoph Hellwig  2022-03-15  324  	nvme_init_request(rq, req->cmd);
c1fef73f793b7f Logan Gunthorpe    2020-07-24  325  
a2f6a2b8ce43db Chaitanya Kulkarni 2020-11-09  326  	if (timeout)
a2f6a2b8ce43db Chaitanya Kulkarni 2020-11-09  327  		rq->timeout = timeout;
a2f6a2b8ce43db Chaitanya Kulkarni 2020-11-09  328  
c1fef73f793b7f Logan Gunthorpe    2020-07-24  329  	if (req->sg_cnt) {
c1fef73f793b7f Logan Gunthorpe    2020-07-24  330  		ret = nvmet_passthru_map_sg(req, rq);
c1fef73f793b7f Logan Gunthorpe    2020-07-24  331  		if (unlikely(ret)) {
c1fef73f793b7f Logan Gunthorpe    2020-07-24  332  			status = NVME_SC_INTERNAL;
a2138fd49467d0 Chaitanya Kulkarni 2020-08-06  333  			goto out_put_req;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  334  		}
c1fef73f793b7f Logan Gunthorpe    2020-07-24  335  	}
c1fef73f793b7f Logan Gunthorpe    2020-07-24  336  
c1fef73f793b7f Logan Gunthorpe    2020-07-24  337  	/*
2a459f6933e1c4 Christoph Hellwig  2022-12-21  338  	 * If a command needs post-execution fixups, or there are any
2a459f6933e1c4 Christoph Hellwig  2022-12-21  339  	 * non-trivial effects, make sure to execute the command synchronously
2a459f6933e1c4 Christoph Hellwig  2022-12-21  340  	 * in a workqueue so that nvme_passthru_end gets called.
c1fef73f793b7f Logan Gunthorpe    2020-07-24  341  	 */
a754bb00c0d393 Daniel Wagner      2023-12-06 @342  	effects = nvme_command_effects(ctrl, ns->head, req->cmd->common.opcode);
                                                                                             ^^^^^^^^

Unchecked dereference

2a459f6933e1c4 Christoph Hellwig  2022-12-21  343  	if (req->p.use_workqueue ||
2a459f6933e1c4 Christoph Hellwig  2022-12-21  344  	    (effects & ~(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC))) {
c1fef73f793b7f Logan Gunthorpe    2020-07-24  345  		INIT_WORK(&req->p.work, nvmet_passthru_execute_cmd_work);
c1fef73f793b7f Logan Gunthorpe    2020-07-24  346  		req->p.rq = rq;
8832cf922151e9 Sagi Grimberg      2022-03-21  347  		queue_work(nvmet_wq, &req->p.work);
c1fef73f793b7f Logan Gunthorpe    2020-07-24  348  	} else {
e2e530867245d0 Christoph Hellwig  2022-05-24  349  		rq->end_io = nvmet_passthru_req_done;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  350  		rq->end_io_data = req;
e2e530867245d0 Christoph Hellwig  2022-05-24  351  		blk_execute_rq_nowait(rq, false);
c1fef73f793b7f Logan Gunthorpe    2020-07-24  352  	}
c1fef73f793b7f Logan Gunthorpe    2020-07-24  353  
c1fef73f793b7f Logan Gunthorpe    2020-07-24 @354  	if (ns)

The rest of the code checks

c1fef73f793b7f Logan Gunthorpe    2020-07-24  355  		nvme_put_ns(ns);
c1fef73f793b7f Logan Gunthorpe    2020-07-24  356  
c1fef73f793b7f Logan Gunthorpe    2020-07-24  357  	return;
c1fef73f793b7f Logan Gunthorpe    2020-07-24  358  
a2138fd49467d0 Chaitanya Kulkarni 2020-08-06  359  out_put_req:
7ee51cf60a90c2 Chaitanya Kulkarni 2020-08-06  360  	blk_mq_free_request(rq);
4db69a3d7cfe31 Chaitanya Kulkarni 2020-08-06  361  out_put_ns:
c1fef73f793b7f Logan Gunthorpe    2020-07-24  362  	if (ns)
c1fef73f793b7f Logan Gunthorpe    2020-07-24  363  		nvme_put_ns(ns);
4db69a3d7cfe31 Chaitanya Kulkarni 2020-08-06  364  out:
c1fef73f793b7f Logan Gunthorpe    2020-07-24  365  	nvmet_req_complete(req, status);
c1fef73f793b7f Logan Gunthorpe    2020-07-24  366  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#10 lore raw

PATCH v3 4/4 nvme: add csi, ms and nuse to sysfs

In reply to Daniel Wagner

libnvme is using the sysfs for enumarating the nvme resources. Though there are few missing attritbutes in the sysfs. For these libnvme issues commands during discovering.

As the kernel already knows all these attributes and we would like to avoid libnvme to issue commands all the time, expose these missing attributes.

The nuse value is updated on request because the nuse is a volatile value. Since any user can read the sysfs attribute, a very simple rate limit is added (update max every 5 seconds). A more sophisticated update strategy can be added later if there is actually a need for it.

---
 drivers/nvme/host/core.c  | 28 ++++++++++++++++++++++++++++
 drivers/nvme/host/nvme.h  |  2 ++
 drivers/nvme/host/sysfs.c | 31 +++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)
drivers/nvme/host/core.c +28 -0
index caa52c2f57c8..e7dd64ee1653 100644
@@ -1663,6 +1663,33 @@ static void nvme_ns_release(struct nvme_ns *ns)
 	nvme_put_ns(ns);
 }
 
+int nvme_ns_update_nuse(struct nvme_ns_head *head)
+{
+	static DEFINE_RATELIMIT_STATE(_rs, 5 * HZ, 1);
+	struct nvme_id_ns *id;
+	struct nvme_ns *ns;
+	int srcu_idx, ret = -EWOULDBLOCK;
+
+	if (__ratelimit(&_rs))
+		return 0;
+
+	srcu_idx = srcu_read_lock(&head->srcu);
+	ns = nvme_find_path(head);
+	if (!ns)
+		goto out_unlock;
+
+	ret = nvme_identify_ns(ns->ctrl, head->ns_id, &id);
+	if (ret)
+		goto out_unlock;
+
+	head->nuse = le64_to_cpu(id->nuse);
+	kfree(id);
+
+out_unlock:
+	srcu_read_unlock(&head->srcu, srcu_idx);
+	return ret;
+}
+
 static int nvme_open(struct gendisk *disk, blk_mode_t mode)
 {
 	return nvme_ns_open(disk->private_data);
@@ -2068,6 +2095,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
 	blk_mq_freeze_queue(ns->disk->queue);
 	lbaf = nvme_lbaf_index(id->flbas);
 	ns->head->lba_shift = id->lbaf[lbaf].ds;
+	ns->head->nuse = le64_to_cpu(id->nuse);
 	nvme_set_queue_limits(ns->ctrl, ns->queue);
 
 	ret = nvme_configure_metadata(ns, id);
drivers/nvme/host/nvme.h +2 -0
index e6c7890b14c9..6a928646dc09 100644
@@ -456,6 +456,7 @@ struct nvme_ns_head {
 	u16			pi_size;
 	u16			sgs;
 	u32			sws;
+	u64			nuse;
 	u8			pi_type;
 	u8			guard_type;
 #ifdef CONFIG_BLK_DEV_ZONED
@@ -867,6 +868,7 @@ int nvme_ns_chr_uring_cmd(struct io_uring_cmd *ioucmd,
 		unsigned int issue_flags);
 int nvme_ns_head_chr_uring_cmd(struct io_uring_cmd *ioucmd,
 		unsigned int issue_flags);
+int nvme_ns_update_nuse(struct nvme_ns_head *head);
 int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo);
 int nvme_dev_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags);
 
drivers/nvme/host/sysfs.c +31 -0
index c6b7fbd4d34d..c24854eca496 100644
@@ -114,12 +114,43 @@ static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_RO(nsid);
 
+static ssize_t csi_show(struct device *dev, struct device_attribute *attr,
+		char *buf)
+{
+	return sysfs_emit(buf, "%d\n", dev_to_ns_head(dev)->ids.csi);
+}
+static DEVICE_ATTR_RO(csi);
+
+static ssize_t metadata_bytes_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "%d\n", dev_to_ns_head(dev)->ms);
+}
+static DEVICE_ATTR_RO(metadata_bytes);
+
+static ssize_t nuse_show(struct device *dev, struct device_attribute *attr,
+		char *buf)
+{
+	struct nvme_ns_head *head = dev_to_ns_head(dev);
+	int ret;
+
+	ret = nvme_ns_update_nuse(head);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "%llu\n", head->nuse);
+}
+static DEVICE_ATTR_RO(nuse);
+
 static struct attribute *nvme_ns_id_attrs[] = {
 	&dev_attr_wwid.attr,
 	&dev_attr_uuid.attr,
 	&dev_attr_nguid.attr,
 	&dev_attr_eui.attr,
+	&dev_attr_csi.attr,
 	&dev_attr_nsid.attr,
+	&dev_attr_metadata_bytes.attr,
+	&dev_attr_nuse.attr,
 #ifdef CONFIG_NVME_MULTIPATH
 	&dev_attr_ana_grpid.attr,
 	&dev_attr_ana_state.attr,
Signature · 1 line
2.43.0
#11 lore raw
Quoted 27 lines · Daniel Wagner

libnvme is using the sysfs for enumarating the nvme resources. Though there are few missing attritbutes in the sysfs. For these libnvme issues commands during discovering.

As the kernel already knows all these attributes and we would like to avoid libnvme to issue commands all the time, expose these missing attributes.

The nuse value is updated on request because the nuse is a volatile value. Since any user can read the sysfs attribute, a very simple rate limit is added (update max every 5 seconds). A more sophisticated update strategy can be added later if there is actually a need for it.

---
 drivers/nvme/host/core.c  | 28 ++++++++++++++++++++++++++++
 drivers/nvme/host/nvme.h  |  2 ++
 drivers/nvme/host/sysfs.c | 31 +++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)
drivers/nvme/host/core.c +0 -0
index caa52c2f57c8..e7dd64ee1653 100644
@@ -1663,6 +1663,33 @@ static void nvme_ns_release(struct nvme_ns *ns)
 	nvme_put_ns(ns);
 }

drivers/nvme/host/core.c static void nvme_ns_release(struct nvme_ns *ns) · line 1674

Quoted 10 lines
drivers/nvme/host/core.c +9 -0
 
+int nvme_ns_update_nuse(struct nvme_ns_head *head)
+{
+	static DEFINE_RATELIMIT_STATE(_rs, 5 * HZ, 1);
+	struct nvme_id_ns *id;
+	struct nvme_ns *ns;
+	int srcu_idx, ret = -EWOULDBLOCK;
+
+	if (__ratelimit(&_rs))
+		return 0;

Can you add a comment on the ratelimiting here?

drivers/nvme/host/core.c static void nvme_ns_release(struct nvme_ns *ns) · line 1685

Quoted 11 lines
drivers/nvme/host/core.c +11 -0
+
+	srcu_idx = srcu_read_lock(&head->srcu);
+	ns = nvme_find_path(head);
+	if (!ns)
+		goto out_unlock;
+
+	ret = nvme_identify_ns(ns->ctrl, head->ns_id, &id);
+	if (ret)
+		goto out_unlock;
+
+	head->nuse = le64_to_cpu(id->nuse);

This looks like the wrong thing to do for the non-multipath nodes, which should be able to go straight to the ns.

I'd move this to sysfs.c, and then do a similar trick to say nvme_send_pr_command to directly use the ns for the non-multipath nodes, and do what you're doing here for the multipath nodes.

drivers/nvme/host/core.c static void nvme_ns_release(struct nvme_ns *ns) · line 1686

Quoted 1 line
drivers/nvme/host/core.c +0 -0
 static struct attribute *nvme_ns_id_attrs[] = {

And I duess the _id is not correct now, I'd just drop it.