* [PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 02/22] nvmet: fix nvmet_ns::bdev access using nvmet_subsys::lock Nilay Shroff
` (20 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::bdev field is protected by nvmet_subsys::lock. Annotate
it with __guarded_by(&subsys->lock) so that the clang context analyzer
can validate accesses to this field.
Some accesses to nvmet_ns::bdev in the I/O hotpath do not require
holding nvmet_subsys::lock, as the namespace reference guarantees that
the namespace remains valid and the field is not modified while I/O is
in flight. However, the clang context analyzer cannot express this
access pattern directly. So introduce nvmet_ns_bdev() as an inline
helper annotated with __context_unsafe() to allow these accesses
without requiring the subsystem lock. Convert the relevant accesses in
the I/O hotpath and other request-processing paths to use this helper.
Additionally, annotate namespace enable, disable, revalidation and
write protect functions with __must_hold() to make their locking
requirements explicit. This includes the block device and P2P memory
namespace operations.
Also pass subsys as an explicit argument to nvmet_p2pmem_ns_add_p2p()
and nvmet_setup_p2p_ns_map(). This allows their __must_hold()
annotations and lockdep assertions to reference subsys->lock directly,
rather than accessing the same lock through ctrl->subsys->lock.
When the same subsystem lock is accessed through different expressions,
such as subsys->lock, ctrl->subsys->lock and ns->subsys->lock, clang's
context analyzer cannot infer that they refer to the same lock.
Explicitly assert that ns->subsys->lock is held using
lockdep_assert_held() in nvmet_p2pmem_ns_add_p2p() to satisfy the
corresponding context annotation.
Since nvmet_ns_revalidate() requires callers to hold ns->subsys->lock,
acquire req->ns->subsys->lock before invoking it from
nvmet_execute_identify_ns() and nvmet_execute_identify_ns_zns().
Finally, update nvmet_set_feat_write_protect() to acquire
req->ns->subsys->lock instead of subsys->lock. Both expressions refer
to the same lock, but using the former allows the context analyzer to
correctly validate the locking requirement of
nvmet_write_protect_flush_sync().
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 52 ++++++------
drivers/nvme/target/core.c | 28 ++++--
drivers/nvme/target/io-cmd-bdev.c | 22 ++---
drivers/nvme/target/nvmet.h | 136 ++++++++++++++++++------------
drivers/nvme/target/rdma.c | 2 +-
drivers/nvme/target/zns.c | 44 +++++-----
6 files changed, 163 insertions(+), 121 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index e0be6eb0fa04..d45c05f5d746 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -259,15 +259,17 @@ static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,
return status;
/* we don't have the right data for file backed ns */
- if (!req->ns->bdev)
+ if (!nvmet_ns_bdev(req->ns))
return NVME_SC_SUCCESS;
- host_reads = part_stat_read(req->ns->bdev, ios[READ]);
+ host_reads = part_stat_read(nvmet_ns_bdev(req->ns), ios[READ]);
data_units_read =
- DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[READ]), 1000);
- host_writes = part_stat_read(req->ns->bdev, ios[WRITE]);
+ DIV_ROUND_UP(part_stat_read(nvmet_ns_bdev(req->ns),
+ sectors[READ]), 1000);
+ host_writes = part_stat_read(nvmet_ns_bdev(req->ns), ios[WRITE]);
data_units_written =
- DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[WRITE]), 1000);
+ DIV_ROUND_UP(part_stat_read(nvmet_ns_bdev(req->ns),
+ sectors[WRITE]), 1000);
put_unaligned_le64(host_reads, &slog->host_reads[0]);
put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
@@ -319,7 +321,7 @@ static void nvmet_execute_get_log_page_rmi(struct nvmet_req *req)
if (status)
goto out;
- if (!req->ns->bdev || !bdev_rot(req->ns->bdev)) {
+ if (!nvmet_ns_bdev(req->ns) || !bdev_rot(nvmet_ns_bdev(req->ns))) {
status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
goto out;
}
@@ -336,7 +338,7 @@ static void nvmet_execute_get_log_page_rmi(struct nvmet_req *req)
}
log->endgid = req->cmd->get_log_page.lsi;
- disk = req->ns->bdev->bd_disk;
+ disk = nvmet_ns_bdev(req->ns)->bd_disk;
if (disk && disk->ia_ranges)
log->numa = cpu_to_le16(disk->ia_ranges->nr_ia_ranges);
else
@@ -537,15 +539,17 @@ static void nvmet_execute_get_log_page_endgrp(struct nvmet_req *req)
goto out;
}
- if (!req->ns->bdev)
+ if (!nvmet_ns_bdev(req->ns))
goto copy;
- host_reads = part_stat_read(req->ns->bdev, ios[READ]);
+ host_reads = part_stat_read(nvmet_ns_bdev(req->ns), ios[READ]);
data_units_read =
- DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[READ]), 1000);
- host_writes = part_stat_read(req->ns->bdev, ios[WRITE]);
+ DIV_ROUND_UP(part_stat_read(nvmet_ns_bdev(req->ns),
+ sectors[READ]), 1000);
+ host_writes = part_stat_read(nvmet_ns_bdev(req->ns), ios[WRITE]);
data_units_written =
- DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[WRITE]), 1000);
+ DIV_ROUND_UP(part_stat_read(nvmet_ns_bdev(req->ns),
+ sectors[WRITE]), 1000);
put_unaligned_le64(host_reads, &log->hrc[0]);
put_unaligned_le64(data_units_read, &log->dur[0]);
@@ -859,11 +863,10 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)
goto done;
}
- if (nvmet_ns_revalidate(req->ns)) {
- mutex_lock(&req->ns->subsys->lock);
+ mutex_lock(&req->ns->subsys->lock);
+ if (nvmet_ns_revalidate(req->ns))
nvmet_ns_changed(req->ns->subsys, req->ns->nsid);
- mutex_unlock(&req->ns->subsys->lock);
- }
+ mutex_unlock(&req->ns->subsys->lock);
/*
* nuse = ncap = nsze isn't always true, but we have no way to find
@@ -880,8 +883,8 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)
break;
}
- if (req->ns->bdev)
- nvmet_bdev_set_limits(req->ns->bdev, id);
+ if (nvmet_ns_bdev(req->ns))
+ nvmet_bdev_set_limits(nvmet_ns_bdev(req->ns), id);
/*
* We just provide a single LBA format that matches what the
@@ -1095,8 +1098,8 @@ static void nvme_execute_identify_ns_nvm(struct nvmet_req *req)
status = NVME_SC_INTERNAL;
goto out;
}
- if (req->ns->bdev)
- nvmet_bdev_set_nvm_limits(req->ns->bdev, id);
+ if (nvmet_ns_bdev(req->ns))
+ nvmet_bdev_set_nvm_limits(nvmet_ns_bdev(req->ns), id);
status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
kfree(id);
out:
@@ -1123,14 +1126,14 @@ static void nvmet_execute_id_cs_indep(struct nvmet_req *req)
id->nmic = NVME_NS_NMIC_SHARED;
if (req->ns->readonly)
id->nsattr |= NVME_NS_ATTR_RO;
- if (req->ns->bdev && bdev_rot(req->ns->bdev))
+ if (nvmet_ns_bdev(req->ns) && bdev_rot(nvmet_ns_bdev(req->ns)))
id->nsfeat |= NVME_NS_ROTATIONAL;
/*
* We need flush command to flush the file's metadata,
* so report supporting vwc if backend is file, even
* though buffered_io is disable.
*/
- if (req->ns->bdev && !bdev_write_cache(req->ns->bdev))
+ if (nvmet_ns_bdev(req->ns) && !bdev_write_cache(nvmet_ns_bdev(req->ns)))
id->nsfeat |= NVME_NS_VWC_NOT_PRESENT;
status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
@@ -1216,6 +1219,7 @@ static void nvmet_execute_abort(struct nvmet_req *req)
}
static u16 nvmet_write_protect_flush_sync(struct nvmet_req *req)
+ __must_hold(&req->ns->subsys->lock)
{
u16 status;
@@ -1239,7 +1243,7 @@ static u16 nvmet_set_feat_write_protect(struct nvmet_req *req)
if (status)
return status;
- mutex_lock(&subsys->lock);
+ mutex_lock(&req->ns->subsys->lock);
switch (write_protect) {
case NVME_NS_WRITE_PROTECT:
req->ns->readonly = true;
@@ -1258,7 +1262,7 @@ static u16 nvmet_set_feat_write_protect(struct nvmet_req *req)
if (!status)
nvmet_ns_changed(subsys, req->ns->nsid);
- mutex_unlock(&subsys->lock);
+ mutex_unlock(&req->ns->subsys->lock);
return status;
}
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index cc1955bc8c84..bc7f7eef2b1c 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -484,12 +484,14 @@ void nvmet_put_namespace(struct nvmet_ns *ns)
}
static void nvmet_ns_dev_disable(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock)
{
nvmet_bdev_ns_disable(ns);
nvmet_file_ns_disable(ns);
}
static int nvmet_p2pmem_ns_enable(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock)
{
int ret;
struct pci_dev *p2p_dev;
@@ -533,14 +535,21 @@ static int nvmet_p2pmem_ns_enable(struct nvmet_ns *ns)
return 0;
}
-static void nvmet_p2pmem_ns_add_p2p(struct nvmet_ctrl *ctrl,
- struct nvmet_ns *ns)
+static void nvmet_p2pmem_ns_add_p2p(struct nvmet_subsys *subsys,
+ struct nvmet_ctrl *ctrl, struct nvmet_ns *ns)
+ __must_hold(&subsys->lock)
{
struct device *clients[2];
struct pci_dev *p2p_dev;
int ret;
- lockdep_assert_held(&ctrl->subsys->lock);
+ /*
+ * Although subsys->lock and ns->subsys->lock refer to the same lock,
+ * clang's thread-safety analysis cannot infer this relationship. Assert
+ * that ns->subsys->lock is held to satisfy the corresponding lock
+ * annotation.
+ */
+ lockdep_assert_held(&ns->subsys->lock);
if (!ctrl->p2p_client || !ns->use_p2pmem)
return;
@@ -616,7 +625,7 @@ int nvmet_ns_enable(struct nvmet_ns *ns)
goto out_dev_disable;
list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
- nvmet_p2pmem_ns_add_p2p(ctrl, ns);
+ nvmet_p2pmem_ns_add_p2p(subsys, ctrl, ns);
if (ns->pr.enable) {
ret = nvmet_pr_init_ns(ns);
@@ -1574,13 +1583,14 @@ bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn)
return false;
}
-static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
- struct device *p2p_client)
+static void nvmet_setup_p2p_ns_map(struct nvmet_subsys *subsys,
+ struct nvmet_ctrl *ctrl, struct device *p2p_client)
+ __must_hold(&subsys->lock)
{
struct nvmet_ns *ns;
unsigned long idx;
- lockdep_assert_held(&ctrl->subsys->lock);
+ lockdep_assert_held(&subsys->lock);
if (!p2p_client)
return;
@@ -1588,7 +1598,7 @@ static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
ctrl->p2p_client = get_device(p2p_client);
nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns)
- nvmet_p2pmem_ns_add_p2p(ctrl, ns);
+ nvmet_p2pmem_ns_add_p2p(subsys, ctrl, ns);
}
static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl)
@@ -1733,7 +1743,7 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args)
if (ret)
goto init_pr_fail;
list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
- nvmet_setup_p2p_ns_map(ctrl, args->p2p_client);
+ nvmet_setup_p2p_ns_map(subsys, ctrl, args->p2p_client);
nvmet_debugfs_ctrl_setup(ctrl);
mutex_unlock(&subsys->lock);
up_read(&nvmet_config_sem);
diff --git a/drivers/nvme/target/io-cmd-bdev.c b/drivers/nvme/target/io-cmd-bdev.c
index 72618dced191..7c263cb169a8 100644
--- a/drivers/nvme/target/io-cmd-bdev.c
+++ b/drivers/nvme/target/io-cmd-bdev.c
@@ -73,6 +73,7 @@ void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
}
static void nvmet_bdev_ns_enable_integrity(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock)
{
struct blk_integrity *bi = bdev_get_integrity(ns->bdev);
@@ -206,7 +207,7 @@ static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
int rc;
size_t resid, len;
- bi = bdev_get_integrity(req->ns->bdev);
+ bi = bdev_get_integrity(nvmet_ns_bdev(req->ns));
if (unlikely(!bi)) {
pr_err("Unable to locate bio_integrity\n");
return -ENODEV;
@@ -291,11 +292,11 @@ static void nvmet_bdev_execute_rw(struct nvmet_req *req)
if (nvmet_use_inline_bvec(req)) {
bio = &req->b.inline_bio;
- bio_init(bio, req->ns->bdev, req->inline_bvec,
+ bio_init(bio, nvmet_ns_bdev(req->ns), req->inline_bvec,
ARRAY_SIZE(req->inline_bvec), opf);
} else {
- bio = bio_alloc(req->ns->bdev, bio_max_segs(sg_cnt), opf,
- GFP_KERNEL);
+ bio = bio_alloc(nvmet_ns_bdev(req->ns), bio_max_segs(sg_cnt),
+ opf, GFP_KERNEL);
}
bio->bi_iter.bi_sector = sector;
bio->bi_private = req;
@@ -320,8 +321,9 @@ static void nvmet_bdev_execute_rw(struct nvmet_req *req)
}
}
- bio = bio_alloc(req->ns->bdev, bio_max_segs(sg_cnt),
- opf, GFP_KERNEL);
+ bio = bio_alloc(nvmet_ns_bdev(req->ns),
+ bio_max_segs(sg_cnt), opf,
+ GFP_KERNEL);
bio->bi_iter.bi_sector = sector;
bio_chain(bio, prev);
@@ -349,7 +351,7 @@ static void nvmet_bdev_execute_flush(struct nvmet_req *req)
{
struct bio *bio = &req->b.inline_bio;
- if (!bdev_write_cache(req->ns->bdev)) {
+ if (!bdev_write_cache(nvmet_ns_bdev(req->ns))) {
nvmet_req_complete(req, NVME_SC_SUCCESS);
return;
}
@@ -357,7 +359,7 @@ static void nvmet_bdev_execute_flush(struct nvmet_req *req)
if (!nvmet_check_transfer_len(req, 0))
return;
- bio_init(bio, req->ns->bdev, req->inline_bvec,
+ bio_init(bio, nvmet_ns_bdev(req->ns), req->inline_bvec,
ARRAY_SIZE(req->inline_bvec), REQ_OP_WRITE | REQ_PREFLUSH);
bio->bi_private = req;
bio->bi_end_io = nvmet_bio_done;
@@ -392,7 +394,7 @@ static void nvmet_bdev_execute_discard(struct nvmet_req *req)
nr_sects = (sector_t)le32_to_cpu(range.nlb) <<
(ns->blksize_shift - 9);
- __blkdev_issue_discard(ns->bdev,
+ __blkdev_issue_discard(nvmet_ns_bdev(ns),
nvmet_lba_to_sect(ns, range.slba), nr_sects,
GFP_KERNEL, &bio);
}
@@ -442,7 +444,7 @@ static void nvmet_bdev_execute_write_zeroes(struct nvmet_req *req)
nr_sector = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
(req->ns->blksize_shift - 9));
- ret = __blkdev_issue_zeroout(req->ns->bdev, sector, nr_sector,
+ ret = __blkdev_issue_zeroout(nvmet_ns_bdev(req->ns), sector, nr_sector,
GFP_KERNEL, &bio, 0);
if (bio) {
bio->bi_private = req;
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index e33884a81efc..fc2ccf992d30 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -98,10 +98,60 @@ struct nvmet_pr_per_ctrl_ref {
uuid_t hostid;
};
+struct nvmet_subsys {
+ enum nvme_subsys_type type;
+
+ struct mutex lock;
+ struct kref ref;
+
+ struct xarray namespaces;
+ unsigned int nr_namespaces;
+ u32 max_nsid;
+ u16 cntlid_min;
+ u16 cntlid_max;
+
+ struct list_head ctrls;
+
+ struct list_head hosts;
+ bool allow_any_host;
+#ifdef CONFIG_NVME_TARGET_DEBUGFS
+ struct dentry *debugfs_dir;
+#endif
+ u16 max_qid;
+
+ u64 ver;
+ char serial[NVMET_SN_MAX_SIZE];
+ bool subsys_discovered;
+ char *subsysnqn;
+ bool pi_support;
+ bool tbkas;
+
+ struct config_group group;
+
+ struct config_group namespaces_group;
+ struct config_group allowed_hosts_group;
+
+ u16 vendor_id;
+ u16 subsys_vendor_id;
+ char *model_number;
+ u32 ieee_oui;
+ char *firmware_rev;
+
+#ifdef CONFIG_NVME_TARGET_PASSTHRU
+ struct nvmet_passthru *passthru;
+ struct config_group passthru_group;
+#endif /* CONFIG_NVME_TARGET_PASSTHRU */
+
+#ifdef CONFIG_BLK_DEV_ZONED
+ u8 zasl;
+#endif /* CONFIG_BLK_DEV_ZONED */
+};
+
struct nvmet_ns {
struct percpu_ref ref;
struct file *bdev_file;
- struct block_device *bdev;
+ struct block_device *bdev
+ __guarded_by(&subsys->lock);
struct file *file;
bool readonly;
u32 nsid;
@@ -136,12 +186,29 @@ struct nvmet_ns {
#endif
};
+/*
+ * The accessors generated by this macro are annotated with __context_unsafe()
+ * because they are intended for use in I/O hotpaths and request-processing
+ * paths where a namespace reference is held. These paths access namespace
+ * fields that are guaranteed not to change while the reference is held and the
+ * I/O or request processing is in progress.
+ */
+#define NVMET_NS_ACCESSOR(_type, _name) \
+static inline _type nvmet_ns_##_name(struct nvmet_ns *ns) \
+ __context_unsafe(/* access _name after getting ns reference */) \
+{ \
+ return ns->_name; \
+}
+
+NVMET_NS_ACCESSOR(struct block_device *, bdev)
+
static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
{
return container_of(to_config_group(item), struct nvmet_ns, group);
}
static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock)
{
return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL;
}
@@ -336,55 +403,6 @@ struct nvmet_passthru {
unsigned int clear_ids;
};
-struct nvmet_subsys {
- enum nvme_subsys_type type;
-
- struct mutex lock;
- struct kref ref;
-
- struct xarray namespaces;
- unsigned int nr_namespaces;
- u32 max_nsid;
- u16 cntlid_min;
- u16 cntlid_max;
-
- struct list_head ctrls;
-
- struct list_head hosts;
- bool allow_any_host;
-#ifdef CONFIG_NVME_TARGET_DEBUGFS
- struct dentry *debugfs_dir;
-#endif
- u16 max_qid;
-
- u64 ver;
- char serial[NVMET_SN_MAX_SIZE];
- bool subsys_discovered;
- char *subsysnqn;
- bool pi_support;
- bool tbkas;
-
- struct config_group group;
-
- struct config_group namespaces_group;
- struct config_group allowed_hosts_group;
-
- u16 vendor_id;
- u16 subsys_vendor_id;
- char *model_number;
- u32 ieee_oui;
- char *firmware_rev;
-
-#ifdef CONFIG_NVME_TARGET_PASSTHRU
- struct nvmet_passthru *passthru;
- struct config_group passthru_group;
-#endif /* CONFIG_NVME_TARGET_PASSTHRU */
-
-#ifdef CONFIG_BLK_DEV_ZONED
- u8 zasl;
-#endif /* CONFIG_BLK_DEV_ZONED */
-};
-
static inline struct nvmet_subsys *to_subsys(struct config_item *item)
{
return container_of(to_config_group(item), struct nvmet_subsys, group);
@@ -739,19 +757,25 @@ extern struct rw_semaphore nvmet_ana_sem;
bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn);
-int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
+int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock);
int nvmet_file_ns_enable(struct nvmet_ns *ns);
-void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
+void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock);
void nvmet_file_ns_disable(struct nvmet_ns *ns);
-u16 nvmet_bdev_flush(struct nvmet_req *req);
+u16 nvmet_bdev_flush(struct nvmet_req *req)
+ __must_hold(&req->ns->subsys->lock);
u16 nvmet_file_flush(struct nvmet_req *req);
void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
-void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns);
+void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock);
void nvmet_file_ns_revalidate(struct nvmet_ns *ns);
-bool nvmet_ns_revalidate(struct nvmet_ns *ns);
+bool nvmet_ns_revalidate(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock);
u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts);
-bool nvmet_bdev_zns_enable(struct nvmet_ns *ns);
+bool nvmet_bdev_zns_enable(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock);
void nvmet_execute_identify_ctrl_zns(struct nvmet_req *req);
void nvmet_execute_identify_ns_zns(struct nvmet_req *req);
void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req);
diff --git a/drivers/nvme/target/rdma.c b/drivers/nvme/target/rdma.c
index f386c388da18..0b351d39340c 100644
--- a/drivers/nvme/target/rdma.c
+++ b/drivers/nvme/target/rdma.c
@@ -594,7 +594,7 @@ static void nvmet_rdma_set_sig_attrs(struct nvmet_req *req,
u8 pi_type = req->ns->pi_type;
struct blk_integrity *bi;
- bi = bdev_get_integrity(req->ns->bdev);
+ bi = bdev_get_integrity(nvmet_ns_bdev(req->ns));
memset(sig_attrs, 0, sizeof(*sig_attrs));
diff --git a/drivers/nvme/target/zns.c b/drivers/nvme/target/zns.c
index cea05b4bc740..ce5309453668 100644
--- a/drivers/nvme/target/zns.c
+++ b/drivers/nvme/target/zns.c
@@ -110,30 +110,29 @@ void nvmet_execute_identify_ns_zns(struct nvmet_req *req)
if (status)
goto out;
- if (nvmet_ns_revalidate(req->ns)) {
- mutex_lock(&req->ns->subsys->lock);
+ mutex_lock(&req->ns->subsys->lock);
+ if (nvmet_ns_revalidate(req->ns))
nvmet_ns_changed(req->ns->subsys, req->ns->nsid);
- mutex_unlock(&req->ns->subsys->lock);
- }
+ mutex_unlock(&req->ns->subsys->lock);
- if (!req->ns->bdev || !bdev_is_zoned(req->ns->bdev)) {
+ if (!nvmet_ns_bdev(req->ns) || !bdev_is_zoned(nvmet_ns_bdev(req->ns))) {
status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
req->error_loc = offsetof(struct nvme_identify, nsid);
goto out;
}
- zsze = (bdev_zone_sectors(req->ns->bdev) << 9) >>
+ zsze = (bdev_zone_sectors(nvmet_ns_bdev(req->ns)) << 9) >>
req->ns->blksize_shift;
id_zns->lbafe[0].zsze = cpu_to_le64(zsze);
- mor = bdev_max_open_zones(req->ns->bdev);
+ mor = bdev_max_open_zones(nvmet_ns_bdev(req->ns));
if (!mor)
mor = U32_MAX;
else
mor--;
id_zns->mor = cpu_to_le32(mor);
- mar = bdev_max_active_zones(req->ns->bdev);
+ mar = bdev_max_active_zones(nvmet_ns_bdev(req->ns));
if (!mar)
mar = U32_MAX;
else
@@ -151,7 +150,7 @@ static u16 nvmet_bdev_validate_zone_mgmt_recv(struct nvmet_req *req)
sector_t sect = nvmet_lba_to_sect(req->ns, req->cmd->zmr.slba);
u32 out_bufsize = (le32_to_cpu(req->cmd->zmr.numd) + 1) << 2;
- if (sect >= get_capacity(req->ns->bdev->bd_disk)) {
+ if (sect >= get_capacity(nvmet_ns_bdev(req->ns)->bd_disk)) {
req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, slba);
return NVME_SC_LBA_RANGE | NVME_STATUS_DNR;
}
@@ -247,7 +246,8 @@ static unsigned long nvmet_req_nr_zones_from_slba(struct nvmet_req *req)
{
unsigned int sect = nvmet_lba_to_sect(req->ns, req->cmd->zmr.slba);
- return bdev_nr_zones(req->ns->bdev) - bdev_zone_no(req->ns->bdev, sect);
+ return bdev_nr_zones(nvmet_ns_bdev(req->ns)) -
+ bdev_zone_no(nvmet_ns_bdev(req->ns), sect);
}
static unsigned long get_nr_zones_from_buf(struct nvmet_req *req, u32 bufsize)
@@ -291,8 +291,8 @@ static void nvmet_bdev_zone_zmgmt_recv_work(struct work_struct *w)
goto out;
}
- ret = blkdev_report_zones(req->ns->bdev, start_sect, req_slba_nr_zones,
- nvmet_bdev_report_zone_cb, &rz_data);
+ ret = blkdev_report_zones(nvmet_ns_bdev(req->ns), start_sect,
+ req_slba_nr_zones, nvmet_bdev_report_zone_cb, &rz_data);
if (ret < 0) {
status = NVME_SC_INTERNAL;
goto out;
@@ -415,7 +415,7 @@ static int zmgmt_send_scan_cb(struct blk_zone *z, unsigned i, void *d)
static u16 nvmet_bdev_zone_mgmt_emulate_all(struct nvmet_req *req)
{
- struct block_device *bdev = req->ns->bdev;
+ struct block_device *bdev = nvmet_ns_bdev(req->ns);
unsigned int nr_zones = bdev_nr_zones(bdev);
struct bio *bio = NULL;
sector_t sector = 0;
@@ -471,8 +471,9 @@ static u16 nvmet_bdev_execute_zmgmt_send_all(struct nvmet_req *req)
switch (zsa_req_op(req->cmd->zms.zsa)) {
case REQ_OP_ZONE_RESET:
- ret = blkdev_zone_mgmt(req->ns->bdev, REQ_OP_ZONE_RESET, 0,
- get_capacity(req->ns->bdev->bd_disk));
+ ret = blkdev_zone_mgmt(nvmet_ns_bdev(req->ns),
+ REQ_OP_ZONE_RESET, 0,
+ get_capacity(nvmet_ns_bdev(req->ns)->bd_disk));
if (ret < 0)
return blkdev_zone_mgmt_errno_to_nvme_status(ret);
break;
@@ -494,7 +495,7 @@ static void nvmet_bdev_zmgmt_send_work(struct work_struct *w)
struct nvmet_req *req = container_of(w, struct nvmet_req, z.zmgmt_work);
sector_t sect = nvmet_lba_to_sect(req->ns, req->cmd->zms.slba);
enum req_op op = zsa_req_op(req->cmd->zms.zsa);
- struct block_device *bdev = req->ns->bdev;
+ struct block_device *bdev = nvmet_ns_bdev(req->ns);
sector_t zone_sectors = bdev_zone_sectors(bdev);
u16 status = NVME_SC_SUCCESS;
int ret;
@@ -560,13 +561,14 @@ void nvmet_bdev_execute_zone_append(struct nvmet_req *req)
u32 data_len = nvmet_rw_data_len(req);
struct bio *bio;
int sg_cnt;
+ struct block_device *bdev = nvmet_ns_bdev(req->ns);
/* Request is completed on len mismatch in nvmet_check_transfer_len() */
if (!nvmet_check_transfer_len(req, nvmet_rw_data_len(req)))
return;
if (data_len >
- bdev_max_zone_append_sectors(req->ns->bdev) << SECTOR_SHIFT) {
+ bdev_max_zone_append_sectors(bdev) << SECTOR_SHIFT) {
req->error_loc = offsetof(struct nvme_rw_command, length);
status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
goto out;
@@ -577,13 +579,13 @@ void nvmet_bdev_execute_zone_append(struct nvmet_req *req)
return;
}
- if (sect >= get_capacity(req->ns->bdev->bd_disk)) {
+ if (sect >= get_capacity(bdev->bd_disk)) {
req->error_loc = offsetof(struct nvme_rw_command, slba);
status = NVME_SC_LBA_RANGE | NVME_STATUS_DNR;
goto out;
}
- if (sect & (bdev_zone_sectors(req->ns->bdev) - 1)) {
+ if (sect & (bdev_zone_sectors(bdev) - 1)) {
req->error_loc = offsetof(struct nvme_rw_command, slba);
status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
goto out;
@@ -591,10 +593,10 @@ void nvmet_bdev_execute_zone_append(struct nvmet_req *req)
if (nvmet_use_inline_bvec(req)) {
bio = &req->z.inline_bio;
- bio_init(bio, req->ns->bdev, req->inline_bvec,
+ bio_init(bio, bdev, req->inline_bvec,
ARRAY_SIZE(req->inline_bvec), opf);
} else {
- bio = bio_alloc(req->ns->bdev, req->sg_cnt, opf, GFP_KERNEL);
+ bio = bio_alloc(bdev, req->sg_cnt, opf, GFP_KERNEL);
}
bio->bi_end_io = nvmet_bdev_zone_append_bio_done;
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 02/22] nvmet: fix nvmet_ns::bdev access using nvmet_subsys::lock
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 03/22] nvmet: add context annotation for nvmet_ns::file Nilay Shroff
` (19 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
nvmet_get_smart_log_all() iterates over enabled namespaces and accesses
ns->bdev to collect block device statistics without holding
nvmet_subsys::lock.
Since nvmet_ns::bdev is annotated with __guarded_by(&subsys->lock), the
clang context analyser reports this access as a locking violation:
drivers/nvme/target/admin-cmd.c:274:12: error: reading variable 'bdev' requires holding mutex 'ns->subsys->lock' [-Werror,-Wthread-safety-precise]
274 | if (!ns->bdev)
| ^
Fix this by acquiring ctrl->subsys->lock while iterating over the
enabled namespaces and accessing their block device statistics.
Although ctrl->subsys->lock and ns->subsys->lock refer to the same
mutex, the Clang context analyzer cannot infer this relationship
because the lock is accessed through different expressions. Explicitly
assert the ns->subsys->lock capability using __assume_ctx_lock()
before accessing ns->bdev, and release the assumed capability using
__release_ctx_lock() afterward.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index d45c05f5d746..70b52a43156b 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -289,17 +289,31 @@ static u16 nvmet_get_smart_log_all(struct nvmet_req *req,
unsigned long idx;
ctrl = req->sq->ctrl;
+ mutex_lock(&ctrl->subsys->lock);
nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
+ /*
+ * Both ctrl->subsys->lock and ns->subsys->lock refer to the
+ * same lock. However there's no way for us to annotate such
+ * locking semantics using clang context annotation. So
+ * explicitly assert the capability.
+ */
+ __assume_ctx_lock(&ns->subsys->lock);
+
/* we don't have the right data for file backed ns */
- if (!ns->bdev)
+ if (!ns->bdev) {
+ __release_ctx_lock(&ns->subsys->lock);
continue;
+ }
host_reads += part_stat_read(ns->bdev, ios[READ]);
data_units_read += DIV_ROUND_UP(
part_stat_read(ns->bdev, sectors[READ]), 1000);
host_writes += part_stat_read(ns->bdev, ios[WRITE]);
data_units_written += DIV_ROUND_UP(
part_stat_read(ns->bdev, sectors[WRITE]), 1000);
+
+ __release_ctx_lock(&ns->subsys->lock);
}
+ mutex_unlock(&ctrl->subsys->lock);
put_unaligned_le64(host_reads, &slog->host_reads[0]);
put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 03/22] nvmet: add context annotation for nvmet_ns::file
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 02/22] nvmet: fix nvmet_ns::bdev access using nvmet_subsys::lock Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 04/22] nvmet: add context annotation for nvmet_ns::bdev_file Nilay Shroff
` (18 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::file field is protected by nvmet_subsys::lock.
Annotate it with __guarded_by(&subsys->lock) so that the clang
context analyzer can validate accesses to this field.
Some accesses to nvmet_ns::file in the I/O hotpath do not require
holding nvmet_subsys::lock, as the namespace reference guarantees
that the namespace remains valid and the field is not modified while
I/O is in flight. However, the clang context analyzer cannot express
this access pattern directly.
Introduce nvmet_ns_file() as an inline helper annotated with
__context_unsafe() to allow these accesses without requiring the
subsystem lock. Convert the relevant accesses in the I/O hotpath and
request-processing paths to use this helper.
Additionally, annotate nvmet_file_ns_enable(), nvmet_file_ns_disable()
and nvmet_file_ns_revalidate() with __must_hold(&ns->subsys->lock) to
document and validate their locking requirements.
Since nvmet_file_flush() accesses the namespace file, change its
interface to accept a struct file pointer as an argument. This allows
callers to obtain the file pointer using the appropriate access
mechanism before invoking the helper:
- nvmet_file_flush_work() runs in the I/O hotpath after acquiring a
namespace reference, so it uses nvmet_ns_file() to obtain the file
pointer without holding the subsystem lock.
- nvmet_write_protect_flush_sync() already holds the subsystem lock,
so it can directly access ns->file and pass the pointer to
nvmet_file_flush().
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 5 +++--
drivers/nvme/target/core.c | 2 +-
drivers/nvme/target/io-cmd-file.c | 19 ++++++++++---------
drivers/nvme/target/nvmet.h | 15 ++++++++++-----
4 files changed, 24 insertions(+), 17 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index 70b52a43156b..756bf627c16e 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -1236,9 +1236,10 @@ static u16 nvmet_write_protect_flush_sync(struct nvmet_req *req)
__must_hold(&req->ns->subsys->lock)
{
u16 status;
+ struct file *file = req->ns->file;
- if (req->ns->file)
- status = nvmet_file_flush(req);
+ if (file)
+ status = nvmet_file_flush(req, file);
else
status = nvmet_bdev_flush(req);
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index bc7f7eef2b1c..0cf0f311ed10 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -1163,7 +1163,7 @@ static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
switch (req->ns->csi) {
case NVME_CSI_NVM:
- if (req->ns->file)
+ if (nvmet_ns_file(req->ns))
ret = nvmet_file_parse_io_cmd(req);
else
ret = nvmet_bdev_parse_io_cmd(req);
diff --git a/drivers/nvme/target/io-cmd-file.c b/drivers/nvme/target/io-cmd-file.c
index 3098b902f519..b7c5c7ecf470 100644
--- a/drivers/nvme/target/io-cmd-file.c
+++ b/drivers/nvme/target/io-cmd-file.c
@@ -84,17 +84,17 @@ static ssize_t nvmet_file_submit_bvec(struct nvmet_req *req, loff_t pos,
if (req->cmd->rw.opcode == nvme_cmd_write) {
if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
ki_flags |= IOCB_DSYNC;
- call_iter = req->ns->file->f_op->write_iter;
+ call_iter = nvmet_ns_file(req->ns)->f_op->write_iter;
rw = ITER_SOURCE;
} else {
- call_iter = req->ns->file->f_op->read_iter;
+ call_iter = nvmet_ns_file(req->ns)->f_op->read_iter;
rw = ITER_DEST;
}
iov_iter_bvec(&iter, rw, req->f.bvec, nr_segs, count);
iocb->ki_pos = pos;
- iocb->ki_filp = req->ns->file;
+ iocb->ki_filp = nvmet_ns_file(req->ns);
iocb->ki_flags = ki_flags | iocb->ki_filp->f_iocb_flags;
return call_iter(iocb, &iter);
@@ -258,7 +258,7 @@ static void nvmet_file_execute_rw(struct nvmet_req *req)
if (req->ns->buffered_io) {
if (likely(!req->f.mpool_alloc) &&
- (req->ns->file->f_mode & FMODE_NOWAIT) &&
+ (nvmet_ns_file(req->ns)->f_mode & FMODE_NOWAIT) &&
nvmet_file_execute_io(req, IOCB_NOWAIT))
return;
nvmet_file_submit_buffered_io(req);
@@ -266,16 +266,17 @@ static void nvmet_file_execute_rw(struct nvmet_req *req)
nvmet_file_execute_io(req, 0);
}
-u16 nvmet_file_flush(struct nvmet_req *req)
+u16 nvmet_file_flush(struct nvmet_req *req, struct file *file)
{
- return errno_to_nvme_status(req, vfs_fsync(req->ns->file, 1));
+ return errno_to_nvme_status(req, vfs_fsync(file, 1));
}
static void nvmet_file_flush_work(struct work_struct *w)
{
struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
+ struct file *file = nvmet_ns_file(req->ns);
- nvmet_req_complete(req, nvmet_file_flush(req));
+ nvmet_req_complete(req, nvmet_file_flush(req, file));
}
static void nvmet_file_execute_flush(struct nvmet_req *req)
@@ -313,7 +314,7 @@ static void nvmet_file_execute_discard(struct nvmet_req *req)
}
len = len64;
- ret = vfs_fallocate(req->ns->file, mode, offset, len);
+ ret = vfs_fallocate(nvmet_ns_file(req->ns), mode, offset, len);
if (ret && ret != -EOPNOTSUPP) {
req->error_slba = le64_to_cpu(range.slba);
status = errno_to_nvme_status(req, ret);
@@ -368,7 +369,7 @@ static void nvmet_file_write_zeroes_work(struct work_struct *w)
}
len = len64;
- ret = vfs_fallocate(req->ns->file, mode, offset, len);
+ ret = vfs_fallocate(nvmet_ns_file(req->ns), mode, offset, len);
nvmet_req_complete(req, ret < 0 ? errno_to_nvme_status(req, ret) : 0);
}
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index fc2ccf992d30..20b87de18054 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -152,7 +152,8 @@ struct nvmet_ns {
struct file *bdev_file;
struct block_device *bdev
__guarded_by(&subsys->lock);
- struct file *file;
+ struct file *file
+ __guarded_by(&subsys->lock);
bool readonly;
u32 nsid;
u32 blksize_shift;
@@ -201,6 +202,7 @@ static inline _type nvmet_ns_##_name(struct nvmet_ns *ns) \
}
NVMET_NS_ACCESSOR(struct block_device *, bdev)
+NVMET_NS_ACCESSOR(struct file *, file)
static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
{
@@ -759,17 +761,20 @@ bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn);
int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
__must_hold(&ns->subsys->lock);
-int nvmet_file_ns_enable(struct nvmet_ns *ns);
+int nvmet_file_ns_enable(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock);
void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
__must_hold(&ns->subsys->lock);
-void nvmet_file_ns_disable(struct nvmet_ns *ns);
+void nvmet_file_ns_disable(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock);
u16 nvmet_bdev_flush(struct nvmet_req *req)
__must_hold(&req->ns->subsys->lock);
-u16 nvmet_file_flush(struct nvmet_req *req);
+u16 nvmet_file_flush(struct nvmet_req *req, struct file *file);
void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
__must_hold(&ns->subsys->lock);
-void nvmet_file_ns_revalidate(struct nvmet_ns *ns);
+void nvmet_file_ns_revalidate(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock);
bool nvmet_ns_revalidate(struct nvmet_ns *ns)
__must_hold(&ns->subsys->lock);
u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts);
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 04/22] nvmet: add context annotation for nvmet_ns::bdev_file
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (2 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 03/22] nvmet: add context annotation for nvmet_ns::file Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 05/22] nvmet: add context annotation for nvmet_ns::readonly Nilay Shroff
` (17 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::bdev_file field is protected by nvmet_subsys::lock.
Annotate it with __guarded_by(&subsys->lock) so that the clang
context analyzer can validate accesses to this field.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/nvmet.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 20b87de18054..256ffa3c08cc 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -149,7 +149,8 @@ struct nvmet_subsys {
struct nvmet_ns {
struct percpu_ref ref;
- struct file *bdev_file;
+ struct file *bdev_file
+ __guarded_by(&subsys->lock);
struct block_device *bdev
__guarded_by(&subsys->lock);
struct file *file
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 05/22] nvmet: add context annotation for nvmet_ns::readonly
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (3 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 04/22] nvmet: add context annotation for nvmet_ns::bdev_file Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 06/22] nvmet: add context annotation for nvmet_ns::blksize_shift Nilay Shroff
` (16 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::readonly field is protected by nvmet_subsys::lock.
Annotate it with __guarded_by(&subsys->lock) to allow the Clang
context analyzer to validate accesses to this field.
The admin command handlers nvmet_get_feat_write_protect() and
nvmet_set_feat_write_protect() access nvmet_ns::readonly while
holding ns->subsys->lock. However, the context analyzer cannot
recognize that subsys->lock and ns->subsys->lock refer to the
same mutex. Use ns->subsys->lock directly in these functions
to avoid false positive warnings.
The admin identify functions, nvmet_execute_identify_ns() and
nvmet_execute_id_cs_indep() as well as I/O hotpath function
nvmet_io_cmd_check_access() read nvmet_ns::readonly without holding
the lock. These lockless reads may race with updates to the field,
potentially observing a transient value. Such races are acceptable
for these accesses.
Introduce nvmet_ns_readonly() to perform these lockless reads using
data_race(READ_ONCE()). This suppresses KCSAN reports for the
intentional races and informs the context analyzer that the access
is intentionally performed without holding the lock.
Use WRITE_ONCE() for updates to nvmet_ns::readonly in
nvmet_set_feat_write_protect() to pair with the lockless
READ_ONCE() accesses.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 15 +++++++--------
drivers/nvme/target/core.c | 2 +-
drivers/nvme/target/nvmet.h | 18 +++++++++++++++++-
3 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index 756bf627c16e..1f553ff3800c 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -943,7 +943,7 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)
id->lbaf[0].ms = cpu_to_le16(req->ns->metadata_size);
}
- if (req->ns->readonly)
+ if (nvmet_ns_readonly(req->ns))
id->nsattr |= NVME_NS_ATTR_RO;
done:
if (!status)
@@ -1138,7 +1138,7 @@ static void nvmet_execute_id_cs_indep(struct nvmet_req *req)
id->nstat = NVME_NSTAT_NRDY;
id->anagrpid = cpu_to_le32(req->ns->anagrpid);
id->nmic = NVME_NS_NMIC_SHARED;
- if (req->ns->readonly)
+ if (nvmet_ns_readonly(req->ns))
id->nsattr |= NVME_NS_ATTR_RO;
if (nvmet_ns_bdev(req->ns) && bdev_rot(nvmet_ns_bdev(req->ns)))
id->nsfeat |= NVME_NS_ROTATIONAL;
@@ -1261,13 +1261,13 @@ static u16 nvmet_set_feat_write_protect(struct nvmet_req *req)
mutex_lock(&req->ns->subsys->lock);
switch (write_protect) {
case NVME_NS_WRITE_PROTECT:
- req->ns->readonly = true;
+ WRITE_ONCE(req->ns->readonly, true);
status = nvmet_write_protect_flush_sync(req);
if (status)
- req->ns->readonly = false;
+ WRITE_ONCE(req->ns->readonly, false);
break;
case NVME_NS_NO_WRITE_PROTECT:
- req->ns->readonly = false;
+ WRITE_ONCE(req->ns->readonly, false);
status = 0;
break;
default:
@@ -1458,20 +1458,19 @@ void nvmet_execute_set_features(struct nvmet_req *req)
static u16 nvmet_get_feat_write_protect(struct nvmet_req *req)
{
- struct nvmet_subsys *subsys = nvmet_req_subsys(req);
u32 result;
result = nvmet_req_find_ns(req);
if (result)
return result;
- mutex_lock(&subsys->lock);
+ mutex_lock(&req->ns->subsys->lock);
if (req->ns->readonly == true)
result = NVME_NS_WRITE_PROTECT;
else
result = NVME_NS_NO_WRITE_PROTECT;
nvmet_set_result(req, result);
- mutex_unlock(&subsys->lock);
+ mutex_unlock(&req->ns->subsys->lock);
return 0;
}
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index 0cf0f311ed10..2c60210c14d6 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -1082,7 +1082,7 @@ static inline u16 nvmet_check_ana_state(struct nvmet_port *port,
static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req)
{
- if (unlikely(req->ns->readonly)) {
+ if (unlikely(nvmet_ns_readonly(req->ns))) {
switch (req->cmd->common.opcode) {
case nvme_cmd_read:
case nvme_cmd_flush:
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 256ffa3c08cc..5598edf53b76 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -155,7 +155,8 @@ struct nvmet_ns {
__guarded_by(&subsys->lock);
struct file *file
__guarded_by(&subsys->lock);
- bool readonly;
+ bool readonly
+ __guarded_by(&subsys->lock);
u32 nsid;
u32 blksize_shift;
loff_t size;
@@ -205,6 +206,21 @@ static inline _type nvmet_ns_##_name(struct nvmet_ns *ns) \
NVMET_NS_ACCESSOR(struct block_device *, bdev)
NVMET_NS_ACCESSOR(struct file *, file)
+/*
+ * These accessors use data_race() and READ_ONCE() marker to allow lockless
+ * access to the corresponding fields. The data_race() also helps silence the
+ * context analyser warning about lockless access to the marked field. Use them
+ * only in code paths where a concurrent update to the field is harmless and
+ * the resulting data race is acceptable.
+ */
+#define NVMET_NS_ACCESSOR_MARKED(_type, _name) \
+static inline _type nvmet_ns_##_name(struct nvmet_ns *ns) \
+{ \
+ return data_race(READ_ONCE(ns->_name)); \
+}
+
+NVMET_NS_ACCESSOR_MARKED(bool, readonly)
+
static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
{
return container_of(to_config_group(item), struct nvmet_ns, group);
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 06/22] nvmet: add context annotation for nvmet_ns::blksize_shift
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (4 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 05/22] nvmet: add context annotation for nvmet_ns::readonly Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 07/22] nvmet: add context annotation for nvmet_ns::size Nilay Shroff
` (15 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::blksize_shift field is protected by nvmet_subsys::lock.
Annotate it with __guarded_by(&subsys->lock) to allow the Clang context
analyzer to validate accesses to this field.
The I/O hotpath also accesses nvmet_ns::blksize_shift without holding
subsys->lock. This is safe because the namespace reference held by the
I/O request prevents the namespace from being modified while the I/O is
in flight.
Introduce nvmet_ns_blksize_shift(), annotated with __context_unsafe(),
to allow these lockless accesses without triggering context analyzer
warnings. Use this accessor at all relevant call sites that access
blksize_shift without holding subsys->lock, including the namespace
identify, block I/O, file I/O, ZNS, and common LBA/sector conversion
paths.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 4 ++--
drivers/nvme/target/io-cmd-bdev.c | 4 ++--
drivers/nvme/target/io-cmd-file.c | 7 ++++---
drivers/nvme/target/nvmet.h | 10 ++++++----
drivers/nvme/target/zns.c | 2 +-
5 files changed, 15 insertions(+), 12 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index 1f553ff3800c..940ba0e14449 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -887,7 +887,7 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)
* that out from the underlying device.
*/
id->ncap = id->nsze =
- cpu_to_le64(req->ns->size >> req->ns->blksize_shift);
+ cpu_to_le64(req->ns->size >> nvmet_ns_blksize_shift(req->ns));
switch (req->port->ana_state[req->ns->anagrpid]) {
case NVME_ANA_INACCESSIBLE:
case NVME_ANA_PERSISTENT_LOSS:
@@ -931,7 +931,7 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)
memcpy(&id->nguid, &req->ns->nguid, sizeof(id->nguid));
- id->lbaf[0].ds = req->ns->blksize_shift;
+ id->lbaf[0].ds = nvmet_ns_blksize_shift(req->ns);
if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns)) {
id->dpc = NVME_NS_DPC_PI_FIRST | NVME_NS_DPC_PI_LAST |
diff --git a/drivers/nvme/target/io-cmd-bdev.c b/drivers/nvme/target/io-cmd-bdev.c
index 7c263cb169a8..e950b442ffc6 100644
--- a/drivers/nvme/target/io-cmd-bdev.c
+++ b/drivers/nvme/target/io-cmd-bdev.c
@@ -393,7 +393,7 @@ static void nvmet_bdev_execute_discard(struct nvmet_req *req)
break;
nr_sects = (sector_t)le32_to_cpu(range.nlb) <<
- (ns->blksize_shift - 9);
+ (nvmet_ns_blksize_shift(ns) - 9);
__blkdev_issue_discard(nvmet_ns_bdev(ns),
nvmet_lba_to_sect(ns, range.slba), nr_sects,
GFP_KERNEL, &bio);
@@ -442,7 +442,7 @@ static void nvmet_bdev_execute_write_zeroes(struct nvmet_req *req)
sector = nvmet_lba_to_sect(req->ns, write_zeroes->slba);
nr_sector = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
- (req->ns->blksize_shift - 9));
+ (nvmet_ns_blksize_shift(req->ns) - 9));
ret = __blkdev_issue_zeroout(nvmet_ns_bdev(req->ns), sector, nr_sector,
GFP_KERNEL, &bio, 0);
diff --git a/drivers/nvme/target/io-cmd-file.c b/drivers/nvme/target/io-cmd-file.c
index b7c5c7ecf470..e1e02f5f5efb 100644
--- a/drivers/nvme/target/io-cmd-file.c
+++ b/drivers/nvme/target/io-cmd-file.c
@@ -122,7 +122,7 @@ static bool nvmet_file_check_lba_range(struct nvmet_req *req, u64 slba,
{
u64 pos64;
- if (check_shl_overflow(slba, req->ns->blksize_shift, &pos64) ||
+ if (check_shl_overflow(slba, nvmet_ns_blksize_shift(req->ns), &pos64) ||
pos64 > req->ns->size || len > req->ns->size - pos64)
return false;
@@ -305,7 +305,8 @@ static void nvmet_file_execute_discard(struct nvmet_req *req)
break;
if (check_shl_overflow((u64)le32_to_cpu(range.nlb),
- req->ns->blksize_shift, &len64) ||
+ nvmet_ns_blksize_shift(req->ns),
+ &len64) ||
!nvmet_file_check_lba_range(req, le64_to_cpu(range.slba),
len64, &offset)) {
req->error_slba = le64_to_cpu(range.slba);
@@ -361,7 +362,7 @@ static void nvmet_file_write_zeroes_work(struct work_struct *w)
int ret;
if (check_shl_overflow((u64)le16_to_cpu(write_zeroes->length) + 1,
- req->ns->blksize_shift, &len64) ||
+ nvmet_ns_blksize_shift(req->ns), &len64) ||
!nvmet_file_check_lba_range(req, le64_to_cpu(write_zeroes->slba),
len64, &offset)) {
nvmet_req_complete(req, errno_to_nvme_status(req, -ENOSPC));
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 5598edf53b76..6204dcfb82dc 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -158,7 +158,8 @@ struct nvmet_ns {
bool readonly
__guarded_by(&subsys->lock);
u32 nsid;
- u32 blksize_shift;
+ u32 blksize_shift
+ __guarded_by(&subsys->lock);
loff_t size;
u8 nguid[16];
uuid_t uuid;
@@ -205,6 +206,7 @@ static inline _type nvmet_ns_##_name(struct nvmet_ns *ns) \
NVMET_NS_ACCESSOR(struct block_device *, bdev)
NVMET_NS_ACCESSOR(struct file *, file)
+NVMET_NS_ACCESSOR(u32, blksize_shift)
/*
* These accessors use data_race() and READ_ONCE() marker to allow lockless
@@ -807,7 +809,7 @@ void nvmet_bdev_execute_zone_append(struct nvmet_req *req);
static inline u32 nvmet_rw_data_len(struct nvmet_req *req)
{
return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
- req->ns->blksize_shift;
+ nvmet_ns_blksize_shift(req->ns);
}
static inline u32 nvmet_rw_metadata_len(struct nvmet_req *req)
@@ -1000,12 +1002,12 @@ static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns)
static inline __le64 nvmet_sect_to_lba(struct nvmet_ns *ns, sector_t sect)
{
- return cpu_to_le64(sect >> (ns->blksize_shift - SECTOR_SHIFT));
+ return cpu_to_le64(sect >> (nvmet_ns_blksize_shift(ns) - SECTOR_SHIFT));
}
static inline sector_t nvmet_lba_to_sect(struct nvmet_ns *ns, __le64 lba)
{
- return le64_to_cpu(lba) << (ns->blksize_shift - SECTOR_SHIFT);
+ return le64_to_cpu(lba) << (nvmet_ns_blksize_shift(ns) - SECTOR_SHIFT);
}
static inline bool nvmet_use_inline_bvec(struct nvmet_req *req)
diff --git a/drivers/nvme/target/zns.c b/drivers/nvme/target/zns.c
index ce5309453668..9afea37862ef 100644
--- a/drivers/nvme/target/zns.c
+++ b/drivers/nvme/target/zns.c
@@ -122,7 +122,7 @@ void nvmet_execute_identify_ns_zns(struct nvmet_req *req)
}
zsze = (bdev_zone_sectors(nvmet_ns_bdev(req->ns)) << 9) >>
- req->ns->blksize_shift;
+ nvmet_ns_blksize_shift(req->ns);
id_zns->lbafe[0].zsze = cpu_to_le64(zsze);
mor = bdev_max_open_zones(nvmet_ns_bdev(req->ns));
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 07/22] nvmet: add context annotation for nvmet_ns::size
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (5 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 06/22] nvmet: add context annotation for nvmet_ns::blksize_shift Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:50 ` sashiko-bot
2026-11-18 7:34 ` [PATCH v2 08/22] nvmet: add context annotation for nvmet_ns::nguid Nilay Shroff
` (14 subsequent siblings)
21 siblings, 1 reply; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::size field is protected by nvmet_subsys::lock. Annotate
it with __guarded_by(&subsys->lock) to allow the Clang context analyzer
to validate accesses to this field.
The I/O hotpath also accesses ns->size without holding subsys->lock.
Unlike ns->bdev, which cannot be updated while I/O is in flight due to
the namespace reference, ns->size can be modified concurrently by
nvmet_ns_revalidate_size_store() or nvmet_file_ns_revalidate().
Consequently, lockless reads may observe a transient value of ns->size,
which is acceptable for these access paths.
Introduce nvmet_ns_size() to perform these lockless reads using
data_race(READ_ONCE()) which would silence KCSAN splat as well as help
avoid clang context analyser warning. Use this accessor at the relevant
call sites where accessing ns->size without holding subsys->lock is
harmless despite concurrent updates.
Pair these lockless reads with WRITE_ONCE() for all updates to ns->size.
Also use READ_ONCE() in nvmet_ns_revalidate() when reading the old and
new size values to detect changes.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 3 ++-
drivers/nvme/target/core.c | 4 ++--
drivers/nvme/target/io-cmd-bdev.c | 4 ++--
drivers/nvme/target/io-cmd-file.c | 7 ++++---
drivers/nvme/target/nvmet.h | 4 +++-
5 files changed, 13 insertions(+), 9 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index 940ba0e14449..de47c64a2028 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -887,7 +887,8 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)
* that out from the underlying device.
*/
id->ncap = id->nsze =
- cpu_to_le64(req->ns->size >> nvmet_ns_blksize_shift(req->ns));
+ cpu_to_le64(nvmet_ns_size(req->ns) >>
+ nvmet_ns_blksize_shift(req->ns));
switch (req->port->ana_state[req->ns->anagrpid]) {
case NVME_ANA_INACCESSIBLE:
case NVME_ANA_PERSISTENT_LOSS:
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index 2c60210c14d6..b031595d8edb 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -582,14 +582,14 @@ static void nvmet_p2pmem_ns_add_p2p(struct nvmet_subsys *subsys,
bool nvmet_ns_revalidate(struct nvmet_ns *ns)
{
- loff_t oldsize = ns->size;
+ loff_t oldsize = READ_ONCE(ns->size);
if (ns->bdev)
nvmet_bdev_ns_revalidate(ns);
else
nvmet_file_ns_revalidate(ns);
- return oldsize != ns->size;
+ return oldsize != READ_ONCE(ns->size);
}
int nvmet_ns_enable(struct nvmet_ns *ns)
diff --git a/drivers/nvme/target/io-cmd-bdev.c b/drivers/nvme/target/io-cmd-bdev.c
index e950b442ffc6..0ed691387f1c 100644
--- a/drivers/nvme/target/io-cmd-bdev.c
+++ b/drivers/nvme/target/io-cmd-bdev.c
@@ -115,7 +115,7 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
return ret;
}
ns->bdev = file_bdev(ns->bdev_file);
- ns->size = bdev_nr_bytes(ns->bdev);
+ WRITE_ONCE(ns->size, bdev_nr_bytes(ns->bdev));
ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
ns->pi_type = 0;
@@ -136,7 +136,7 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
{
- ns->size = bdev_nr_bytes(ns->bdev);
+ WRITE_ONCE(ns->size, bdev_nr_bytes(ns->bdev));
}
u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)
diff --git a/drivers/nvme/target/io-cmd-file.c b/drivers/nvme/target/io-cmd-file.c
index e1e02f5f5efb..c87de131037d 100644
--- a/drivers/nvme/target/io-cmd-file.c
+++ b/drivers/nvme/target/io-cmd-file.c
@@ -15,7 +15,7 @@
void nvmet_file_ns_revalidate(struct nvmet_ns *ns)
{
- ns->size = i_size_read(ns->file->f_mapping->host);
+ WRITE_ONCE(ns->size, i_size_read(ns->file->f_mapping->host));
}
void nvmet_file_ns_disable(struct nvmet_ns *ns)
@@ -68,7 +68,7 @@ int nvmet_file_ns_enable(struct nvmet_ns *ns)
err:
fput(ns->file);
ns->file = NULL;
- ns->size = 0;
+ WRITE_ONCE(ns->size, 0);
ns->blksize_shift = 0;
return ret;
}
@@ -123,7 +123,8 @@ static bool nvmet_file_check_lba_range(struct nvmet_req *req, u64 slba,
u64 pos64;
if (check_shl_overflow(slba, nvmet_ns_blksize_shift(req->ns), &pos64) ||
- pos64 > req->ns->size || len > req->ns->size - pos64)
+ pos64 > nvmet_ns_size(req->ns) ||
+ len > nvmet_ns_size(req->ns) - pos64)
return false;
*pos = pos64;
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 6204dcfb82dc..c90599afcdbd 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -160,7 +160,8 @@ struct nvmet_ns {
u32 nsid;
u32 blksize_shift
__guarded_by(&subsys->lock);
- loff_t size;
+ loff_t size
+ __guarded_by(&subsys->lock);
u8 nguid[16];
uuid_t uuid;
u32 anagrpid;
@@ -222,6 +223,7 @@ static inline _type nvmet_ns_##_name(struct nvmet_ns *ns) \
}
NVMET_NS_ACCESSOR_MARKED(bool, readonly)
+NVMET_NS_ACCESSOR_MARKED(loff_t, size)
static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
{
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* Re: [PATCH v2 07/22] nvmet: add context annotation for nvmet_ns::size
2026-11-18 7:34 ` [PATCH v2 07/22] nvmet: add context annotation for nvmet_ns::size Nilay Shroff
@ 2026-11-18 7:50 ` sashiko-bot
0 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-11-18 7:50 UTC (permalink / raw)
To: Nilay Shroff; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] nvmet: double READ_ONCE() in nvmet_file_check_lba_range() can
underflow
--
commit d3bcca86a53dabf9c0c2d63509a67010aa5e3ea3
Author: Nilay Shroff <nilay@linux.ibm.com>
nvmet: add context annotation for nvmet_ns::size
This commit annotates nvmet_ns::size with __guarded_by(&subsys->lock),
introduces nvmet_ns_size() using data_race(READ_ONCE()) for lockless reads,
and pairs those reads with WRITE_ONCE() on updates.
> diff --git a/drivers/nvme/target/io-cmd-file.c b/drivers/nvme/target/io-cmd-file.c
> index e1e02f5f5efb1..c87de131037d4 100644
> --- a/drivers/nvme/target/io-cmd-file.c
> +++ b/drivers/nvme/target/io-cmd-file.c
[ ... ]
> @@ -123,7 +123,8 @@ static bool nvmet_file_check_lba_range(struct nvmet_req *req, u64 slba,
> u64 pos64;
>
> if (check_shl_overflow(slba, nvmet_ns_blksize_shift(req->ns), &pos64) ||
> - pos64 > req->ns->size || len > req->ns->size - pos64)
> + pos64 > nvmet_ns_size(req->ns) ||
> + len > nvmet_ns_size(req->ns) - pos64)
> return false;
>
> *pos = pos64;
[Severity: Medium]
Can calling nvmet_ns_size(req->ns) twice here underflow nvmet_ns_size(req->ns)
- pos64 and bypass the bounds check?
Because nvmet_ns_size() uses READ_ONCE(), the two calls perform two separate
loads of req->ns->size. If a concurrent nvmet_file_ns_revalidate() shrinks
req->ns->size below pos64 between the two loads, the first check passes against
the old size while the unsigned 64-bit subtraction in the second check wraps
around near U64_MAX:
CPU0 CPU1
nvmet_file_check_lba_range()
pos64 > nvmet_ns_size(req->ns)
/* size1 >= pos64: false */
nvmet_file_ns_revalidate()
WRITE_ONCE(ns->size, size2)
/* size2 < pos64 */
len > nvmet_ns_size(req->ns) - pos64
/* (u64)size2 - pos64 underflows */
An out-of-range write request then passes nvmet_file_check_lba_range() and can
extend the backing file past the namespace capacity instead of failing with
NVME_SC_CAP_EXCEEDED. Reading nvmet_ns_size(req->ns) once into a local
variable and using it for both comparisons avoids this.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118073505.288910-1-nilay@linux.ibm.com?part=7
^ permalink raw reply [flat|nested] 25+ messages in thread
* [PATCH v2 08/22] nvmet: add context annotation for nvmet_ns::nguid
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (6 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 07/22] nvmet: add context annotation for nvmet_ns::size Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 09/22] nvmet: add context annotation for nvmet_ns::uuid Nilay Shroff
` (13 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::nguid field is protected by nvmet_subsys::lock. Annotate
it with __guarded_by(&subsys->lock) so that the clang context analyzer
can validate accesses to this field.
Some accesses to nvmet_ns::nguid in the I/O hotpath do not require
holding nvmet_subsys::lock, as the namespace reference guarantees that
the namespace remains valid and the field is not modified while I/O is
in flight. However, the clang context analyzer cannot express this
access pattern directly.
Introduce nvmet_ns_nguid() as an inline helper annotated with
__context_unsafe() to allow these accesses without requiring the
subsystem lock. Convert the relevant accesses in the I/O hotpath and
request-processing paths to use this helper.
Additionally, acquire ns->subsys->lock in nvmet_ns_device_nguid_show()
before accessing ns->nguid, since this configfs control path does not
hold the lock.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 7 ++++---
drivers/nvme/target/configfs.c | 9 ++++++++-
drivers/nvme/target/nvmet.h | 4 +++-
3 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index de47c64a2028..45b68f2419c8 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -930,7 +930,7 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)
*/
id->endgid = cpu_to_le16(req->ns->nsid);
- memcpy(&id->nguid, &req->ns->nguid, sizeof(id->nguid));
+ memcpy(&id->nguid, nvmet_ns_nguid(req->ns), sizeof(id->nguid));
id->lbaf[0].ds = nvmet_ns_blksize_shift(req->ns);
@@ -1070,10 +1070,11 @@ static void nvmet_execute_identify_desclist(struct nvmet_req *req)
if (status)
goto out;
}
- if (memchr_inv(req->ns->nguid, 0, sizeof(req->ns->nguid))) {
+ if (memchr_inv(nvmet_ns_nguid(req->ns), 0, sizeof(req->ns->nguid))) {
status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID,
NVME_NIDT_NGUID_LEN,
- &req->ns->nguid, &off);
+ nvmet_ns_nguid(req->ns),
+ &off);
if (status)
goto out;
}
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index 27c50fa533e7..cd2c301bc69a 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -643,7 +643,14 @@ CONFIGFS_ATTR(nvmet_ns_, device_uuid);
static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
{
- return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
+ struct nvmet_ns *ns = to_nvmet_ns(item);
+ ssize_t ret;
+
+ mutex_lock(&ns->subsys->lock);
+ ret = sprintf(page, "%pUb\n", &ns->nguid);
+ mutex_unlock(&ns->subsys->lock);
+
+ return ret;
}
static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index c90599afcdbd..d284ec162916 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -162,7 +162,8 @@ struct nvmet_ns {
__guarded_by(&subsys->lock);
loff_t size
__guarded_by(&subsys->lock);
- u8 nguid[16];
+ u8 nguid[16]
+ __guarded_by(&subsys->lock);
uuid_t uuid;
u32 anagrpid;
@@ -208,6 +209,7 @@ static inline _type nvmet_ns_##_name(struct nvmet_ns *ns) \
NVMET_NS_ACCESSOR(struct block_device *, bdev)
NVMET_NS_ACCESSOR(struct file *, file)
NVMET_NS_ACCESSOR(u32, blksize_shift)
+NVMET_NS_ACCESSOR(u8 *, nguid)
/*
* These accessors use data_race() and READ_ONCE() marker to allow lockless
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 09/22] nvmet: add context annotation for nvmet_ns::uuid
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (7 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 08/22] nvmet: add context annotation for nvmet_ns::nguid Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 10/22] nvmet: fix nvmet_ns::uuid access using nvmet_subsys::lock Nilay Shroff
` (12 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::uuid field is protected by nvmet_subsys::lock. Annotate
it with __guarded_by(&subsys->lock) so that the clang context analyzer
can validate accesses to this field.
Some accesses to nvmet_ns::uuid in the I/O hotpath do not require
holding nvmet_subsys::lock, as the namespace reference guarantees that
the namespace remains valid and the field is not modified while I/O is
in flight. However, the clang context analyzer cannot express this
access pattern directly.
Introduce nvmet_ns_uuid() as an inline helper annotated with
__context_unsafe() to allow these accesses without requiring the
subsystem lock. Convert the relevant accesses in the I/O hotpath and
request-processing paths to use this helper.
Additionally, acquire ns->subsys->lock in nvmet_ns_device_uuid_show()
before accessing ns->uuid, since this configfs control path does not
hold the lock.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 6 ++++--
drivers/nvme/target/configfs.c | 9 ++++++++-
drivers/nvme/target/nvmet.h | 4 +++-
3 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index 45b68f2419c8..75a833253545 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -1058,15 +1058,17 @@ static void nvmet_execute_identify_desclist(struct nvmet_req *req)
{
off_t off = 0;
u16 status;
+ uuid_t uuid;
status = nvmet_req_find_ns(req);
if (status)
goto out;
- if (memchr_inv(&req->ns->uuid, 0, sizeof(req->ns->uuid))) {
+ uuid = nvmet_ns_uuid(req->ns);
+ if (memchr_inv(&uuid, 0, sizeof(uuid))) {
status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
NVME_NIDT_UUID_LEN,
- &req->ns->uuid, &off);
+ &uuid, &off);
if (status)
goto out;
}
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index cd2c301bc69a..f8a4d0d88946 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -615,7 +615,14 @@ CONFIGFS_ATTR(nvmet_ns_, p2pmem);
static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
{
- return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
+ struct nvmet_ns *ns = to_nvmet_ns(item);
+ ssize_t size;
+
+ mutex_lock(&ns->subsys->lock);
+ size = sprintf(page, "%pUb\n", &ns->uuid);
+ mutex_unlock(&ns->subsys->lock);
+
+ return size;
}
static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index d284ec162916..847115759985 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -164,7 +164,8 @@ struct nvmet_ns {
__guarded_by(&subsys->lock);
u8 nguid[16]
__guarded_by(&subsys->lock);
- uuid_t uuid;
+ uuid_t uuid
+ __guarded_by(&subsys->lock);
u32 anagrpid;
bool buffered_io;
@@ -210,6 +211,7 @@ NVMET_NS_ACCESSOR(struct block_device *, bdev)
NVMET_NS_ACCESSOR(struct file *, file)
NVMET_NS_ACCESSOR(u32, blksize_shift)
NVMET_NS_ACCESSOR(u8 *, nguid)
+NVMET_NS_ACCESSOR(uuid_t, uuid)
/*
* These accessors use data_race() and READ_ONCE() marker to allow lockless
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 10/22] nvmet: fix nvmet_ns::uuid access using nvmet_subsys::lock
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (8 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 09/22] nvmet: add context annotation for nvmet_ns::uuid Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 11/22] nvmet: add context annotation for nvmet_ns::buffered_io Nilay Shroff
` (11 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::uuid field is protected by nvmet_subsys::lock. However,
nvmet_ns_alloc() calls uuid_gen() without holding this lock, triggering
a Clang context analyzer error:
drivers/nvme/target/core.c:781:16: error: passing pointer to variable 'uuid' requires holding mutex '_res->subsys->lock'
[-Werror,-Wthread-safety-pointer]
781 | uuid_gen(&ns->uuid);
| ^
Fix this by separating namespace allocation from initialization.
Rename nvmet_ns_alloc() to nvmet_ns_alloc_and_init() and introduce
nvmet_ns_init(). Move part of the namespace initialization into a new
nvmet_ns_init(), Annotate nvmet_ns_init() with
__must_hold(&subsys->lock) to document and validate this requirement.
Although subsys->lock and ns->subsys->lock refer to the same mutex, the
Clang context analyzer cannot infer this relationship. Add
lockdep_assert_held(&ns->subsys->lock) in nvmet_ns_init() so that the
context analyzer can satisfy the corresponding lock annotation.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/configfs.c | 2 +-
drivers/nvme/target/core.c | 48 ++++++++++++++++++++++------------
drivers/nvme/target/nvmet.h | 2 +-
3 files changed, 33 insertions(+), 19 deletions(-)
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index f8a4d0d88946..10249affcccc 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -899,7 +899,7 @@ static struct config_group *nvmet_ns_make(struct config_group *group,
}
ret = -ENOMEM;
- ns = nvmet_ns_alloc(subsys, nsid);
+ ns = nvmet_ns_alloc_and_init(subsys, nsid);
if (!ns)
goto out;
config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index b031595d8edb..6b0ec84a4c09 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -723,7 +723,36 @@ void nvmet_ns_free(struct nvmet_ns *ns)
kfree(ns);
}
-struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
+static int nvmet_ns_init(struct nvmet_subsys *subsys, struct nvmet_ns *ns,
+ u32 nsid)
+ __must_hold(&subsys->lock)
+{
+ int ret;
+
+ ns->subsys = subsys;
+
+ lockdep_assert_held(&ns->subsys->lock);
+
+ init_completion(&ns->disable_done);
+ ns->nsid = nsid;
+
+ ret = xa_insert(&subsys->namespaces, ns->nsid, ns, GFP_KERNEL);
+ if (ret)
+ return ret;
+
+ if (ns->nsid > subsys->max_nsid)
+ subsys->max_nsid = nsid;
+
+ subsys->nr_namespaces++;
+
+ uuid_gen(&ns->uuid);
+ ns->buffered_io = false;
+ ns->csi = NVME_CSI_NVM;
+
+ return ret;
+}
+
+struct nvmet_ns *nvmet_ns_alloc_and_init(struct nvmet_subsys *subsys, u32 nsid)
{
struct nvmet_ns *ns;
@@ -736,19 +765,9 @@ struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
if (!ns)
goto out_unlock;
- init_completion(&ns->disable_done);
-
- ns->nsid = nsid;
- ns->subsys = subsys;
-
- if (ns->nsid > subsys->max_nsid)
- subsys->max_nsid = nsid;
-
- if (xa_insert(&subsys->namespaces, ns->nsid, ns, GFP_KERNEL))
+ if (nvmet_ns_init(subsys, ns, nsid))
goto out_exit;
- subsys->nr_namespaces++;
-
mutex_unlock(&subsys->lock);
down_write(&nvmet_ana_sem);
@@ -756,13 +775,8 @@ struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
nvmet_ana_group_enabled[ns->anagrpid]++;
up_write(&nvmet_ana_sem);
- uuid_gen(&ns->uuid);
- ns->buffered_io = false;
- ns->csi = NVME_CSI_NVM;
-
return ns;
out_exit:
- subsys->max_nsid = nvmet_max_nsid(subsys);
kfree(ns);
out_unlock:
mutex_unlock(&subsys->lock);
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 847115759985..01132beb5904 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -702,7 +702,7 @@ u16 nvmet_req_find_ns(struct nvmet_req *req);
void nvmet_put_namespace(struct nvmet_ns *ns);
int nvmet_ns_enable(struct nvmet_ns *ns);
void nvmet_ns_disable(struct nvmet_ns *ns);
-struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
+struct nvmet_ns *nvmet_ns_alloc_and_init(struct nvmet_subsys *subsys, u32 nsid);
void nvmet_ns_free(struct nvmet_ns *ns);
void nvmet_send_ana_event(struct nvmet_subsys *subsys,
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 11/22] nvmet: add context annotation for nvmet_ns::buffered_io
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (9 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 10/22] nvmet: fix nvmet_ns::uuid access using nvmet_subsys::lock Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 12/22] nvmet: fix nvmet_ns::buffered_io access using nvmet_subsys::lock Nilay Shroff
` (10 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::buffered_io field is protected by nvmet_subsys::lock.
Annotate it with __guarded_by(&subsys->lock) so that the clang context
analyzer can validate accesses to this field.
Some accesses to nvmet_ns::buffered_io in the I/O hotpath do not require
holding nvmet_subsys::lock, as the namespace reference guarantees that
the namespace remains valid and the field is not modified while I/O is
in flight. However, the clang context analyzer cannot express this
access pattern directly.
Introduce nvmet_ns_buffered_io() as an inline helper annotated with
__context_unsafe() to allow these accesses without requiring the
subsystem lock. Convert the relevant accesses in the I/O hotpath and
request-processing paths to use this helper.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/io-cmd-file.c | 2 +-
drivers/nvme/target/nvmet.h | 4 +++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/target/io-cmd-file.c b/drivers/nvme/target/io-cmd-file.c
index c87de131037d..31538c8cc759 100644
--- a/drivers/nvme/target/io-cmd-file.c
+++ b/drivers/nvme/target/io-cmd-file.c
@@ -257,7 +257,7 @@ static void nvmet_file_execute_rw(struct nvmet_req *req)
} else
req->f.mpool_alloc = false;
- if (req->ns->buffered_io) {
+ if (nvmet_ns_buffered_io(req->ns)) {
if (likely(!req->f.mpool_alloc) &&
(nvmet_ns_file(req->ns)->f_mode & FMODE_NOWAIT) &&
nvmet_file_execute_io(req, IOCB_NOWAIT))
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 01132beb5904..5e9bfec2d384 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -168,7 +168,8 @@ struct nvmet_ns {
__guarded_by(&subsys->lock);
u32 anagrpid;
- bool buffered_io;
+ bool buffered_io
+ __guarded_by(&subsys->lock);
bool enabled;
#define NVMET_NS_IO_LIVE 0
unsigned long flags;
@@ -212,6 +213,7 @@ NVMET_NS_ACCESSOR(struct file *, file)
NVMET_NS_ACCESSOR(u32, blksize_shift)
NVMET_NS_ACCESSOR(u8 *, nguid)
NVMET_NS_ACCESSOR(uuid_t, uuid)
+NVMET_NS_ACCESSOR(bool, buffered_io)
/*
* These accessors use data_race() and READ_ONCE() marker to allow lockless
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 12/22] nvmet: fix nvmet_ns::buffered_io access using nvmet_subsys::lock
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (10 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 11/22] nvmet: add context annotation for nvmet_ns::buffered_io Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 13/22] nvmet: add context annotation for nvmet_ns::enabled Nilay Shroff
` (9 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
nvmet_ns::buffered_io field is protected by nvmet_subsys::lock, however
nvmet_ns_buffered_io_show() accesses nvmet_ns::buffered_io without
holding this lock, triggering a Clang context analyzer error:
drivers/nvme/target/configfs.c:773:50: error: reading variable 'buffered_io' requires holding mutex 'to_nvmet_ns(item).subsys->lock'
[-Werror,-Wthread-safety-analysis]
773 | return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
|
Fix the above error by first acquiring nvmet_subsys::lock in
nvmet_ns_buffered_io_show() before accessing nvmet_ns::buffered_io.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/configfs.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index 10249affcccc..154d917ed21d 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -770,7 +770,14 @@ CONFIGFS_ATTR(nvmet_ns_, enable);
static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
{
- return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
+ struct nvmet_ns *ns = to_nvmet_ns(item);
+ ssize_t ret;
+
+ mutex_lock(&ns->subsys->lock);
+ ret = sprintf(page, "%d\n", ns->buffered_io);
+ mutex_unlock(&ns->subsys->lock);
+
+ return ret;
}
static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 13/22] nvmet: add context annotation for nvmet_ns::enabled
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (11 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 12/22] nvmet: fix nvmet_ns::buffered_io access using nvmet_subsys::lock Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 14/22] nvmet: fix nvmet_ns::enable access using nvmet_subsys::lock Nilay Shroff
` (8 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::enabled field is protected by nvmet_subsys::lock.
Annotate it with __guarded_by(&subsys->lock) so that the clang
context analyzer can validate accesses to this field.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/nvmet.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 5e9bfec2d384..6a1c27cc08e5 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -170,7 +170,8 @@ struct nvmet_ns {
bool buffered_io
__guarded_by(&subsys->lock);
- bool enabled;
+ bool enabled
+ __guarded_by(&subsys->lock);
#define NVMET_NS_IO_LIVE 0
unsigned long flags;
struct nvmet_subsys *subsys;
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 14/22] nvmet: fix nvmet_ns::enable access using nvmet_subsys::lock
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (12 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 13/22] nvmet: add context annotation for nvmet_ns::enabled Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 15/22] nvmet: add context annotation for nvmet_ns::device_path Nilay Shroff
` (7 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
nvmet_ns::enable is protected by nvmet_subsys::lock. The clang
context analyser complains about accessing ns->enabled without
subsys->lock in nvmet_ns_enable_show():
drivers/nvme/target/configfs.c:741:50: error: reading variable 'enabled' requires holding mutex 'to_nvmet_ns(item).subsys->lock'
[-Werror,-Wthread-safety-analysis]
741 | return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
| ^
Fix the above error by acquiring ns->subsys->lock before
accessing ns->enabled in nvmet_ns_enable_show().
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/configfs.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index 154d917ed21d..997d0a1fa07a 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -737,7 +737,14 @@ CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
{
- return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
+ struct nvmet_ns *ns = to_nvmet_ns(item);
+ ssize_t ret;
+
+ mutex_lock(&ns->subsys->lock);
+ ret = sprintf(page, "%d\n", ns->enabled);
+ mutex_unlock(&ns->subsys->lock);
+
+ return ret;
}
static ssize_t nvmet_ns_enable_store(struct config_item *item,
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 15/22] nvmet: add context annotation for nvmet_ns::device_path
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (13 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 14/22] nvmet: fix nvmet_ns::enable access using nvmet_subsys::lock Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 16/22] nvmet: fix nvmet_ns::device_path access using nvmet_subsys::lock Nilay Shroff
` (6 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::device_path field is protected by
nvmet_subsys::lock. Annotate it with
__guarded_by(&subsys->lock) to allow the Clang context
analyzer to validate accesses to this field.
Some request-processing paths access ns->device_path without
holding subsys->lock. This is safe because the namespace
reference held by the request prevents the ns->device_path from
being modified while I/O is in flight. However, the context
analyzer cannot express this access pattern directly.
Introduce nvmet_ns_device_path() as an inline helper
annotated with __context_unsafe() to allow these lockless
accesses without triggering context analyzer warnings.
Additionally, update __assign_req_name() to use this accessor
when copying device_path into the trace event's request name.
This is safe because the tracing function is invoked after
the namespace reference has been acquired.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/nvmet.h | 4 +++-
drivers/nvme/target/trace.h | 2 +-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 6a1c27cc08e5..db5d5a3fec3d 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -175,7 +175,8 @@ struct nvmet_ns {
#define NVMET_NS_IO_LIVE 0
unsigned long flags;
struct nvmet_subsys *subsys;
- const char *device_path;
+ const char *device_path
+ __guarded_by(&subsys->lock);
struct config_group device_group;
struct config_group group;
@@ -215,6 +216,7 @@ NVMET_NS_ACCESSOR(u32, blksize_shift)
NVMET_NS_ACCESSOR(u8 *, nguid)
NVMET_NS_ACCESSOR(uuid_t, uuid)
NVMET_NS_ACCESSOR(bool, buffered_io)
+NVMET_NS_ACCESSOR(const char *, device_path)
/*
* These accessors use data_race() and READ_ONCE() marker to allow lockless
diff --git a/drivers/nvme/target/trace.h b/drivers/nvme/target/trace.h
index 7f7ebf9558e5..b15294e24793 100644
--- a/drivers/nvme/target/trace.h
+++ b/drivers/nvme/target/trace.h
@@ -59,7 +59,7 @@ static inline void __assign_req_name(char *name, struct nvmet_req *req)
return;
}
- strscpy_pad(name, req->ns->device_path, DISK_NAME_LEN);
+ strscpy_pad(name, nvmet_ns_device_path(req->ns), DISK_NAME_LEN);
}
#endif
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 16/22] nvmet: fix nvmet_ns::device_path access using nvmet_subsys::lock
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (14 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 15/22] nvmet: add context annotation for nvmet_ns::device_path Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 17/22] nvmet: add context annotation for PCI P2P parameters in struct nvmet_ns Nilay Shroff
` (5 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::device_path field is protected by nvmet_subsys::lock.
However, nvmet_ns_device_path_show() accesses ns->device_path without
holding the corresponding lock, triggering a Clang context analyzer
error:
drivers/nvme/target/configfs.c:535:50: error: reading variable 'device_path' requires holding mutex 'to_nvmet_ns(item).subsys->lock'
[-Werror,-Wthread-safety-analysis]
535 | return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
| ^
Fix this by acquiring ns->subsys->lock before accessing ns->device_path
in nvmet_ns_device_path_show().
Additionally, nvmet_ns_free() frees ns->device_path without holding
subsys->lock, triggering another context analyzer error:
drivers/nvme/target/core.c:737:12: error: reading variable 'device_path' requires holding mutex 'ns->subsys->lock'
[-Werror,-Wthread-safety-analysis]
737 | kfree(ns->device_path);
| ^
Fix the above error, by moving the kfree() call into the existing
subsys->lock critical section to ensure that the field is accessed
while holding the required lock.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/configfs.c | 9 ++++++++-
drivers/nvme/target/core.c | 2 +-
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index 997d0a1fa07a..a69405279fe3 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -531,7 +531,14 @@ CONFIGFS_ATTR(nvmet_, addr_tsas);
*/
static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
{
- return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
+ struct nvmet_ns *ns = to_nvmet_ns(item);
+ ssize_t ret;
+
+ mutex_lock(&ns->subsys->lock);
+ ret = sprintf(page, "%s\n", ns->device_path);
+ mutex_unlock(&ns->subsys->lock);
+
+ return ret;
}
static ssize_t nvmet_ns_device_path_store(struct config_item *item,
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index 6b0ec84a4c09..6c22d5d97d1c 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -713,13 +713,13 @@ void nvmet_ns_free(struct nvmet_ns *ns)
subsys->max_nsid = nvmet_max_nsid(subsys);
subsys->nr_namespaces--;
+ kfree(ns->device_path);
mutex_unlock(&subsys->lock);
down_write(&nvmet_ana_sem);
nvmet_ana_group_enabled[ns->anagrpid]--;
up_write(&nvmet_ana_sem);
- kfree(ns->device_path);
kfree(ns);
}
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 17/22] nvmet: add context annotation for PCI P2P parameters in struct nvmet_ns
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (15 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 16/22] nvmet: fix nvmet_ns::device_path access using nvmet_subsys::lock Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:34 ` [PATCH v2 18/22] nvmet: fix PCI P2P parameters access using nvmet_subsys::lock Nilay Shroff
` (4 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
nvmet_ns::p2p_dev and nvmet_ns::use_p2pmem are protected by
nvmet_subsys::lock. Annotate it with __guarded_by(&subsys->lock) so
that the clang context analyzer can validate accesses to this field.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/nvmet.h | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index db5d5a3fec3d..08cf6364c4a6 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -184,8 +184,10 @@ struct nvmet_ns {
struct completion disable_done;
mempool_t *bvec_pool;
- struct pci_dev *p2p_dev;
- int use_p2pmem;
+ struct pci_dev *p2p_dev
+ __guarded_by(&subsys->lock);
+ int use_p2pmem
+ __guarded_by(&subsys->lock);
int pi_type;
int metadata_size;
u8 csi;
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 18/22] nvmet: fix PCI P2P parameters access using nvmet_subsys::lock
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (16 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 17/22] nvmet: add context annotation for PCI P2P parameters in struct nvmet_ns Nilay Shroff
@ 2026-11-18 7:34 ` Nilay Shroff
2026-11-18 7:55 ` sashiko-bot
2026-11-18 7:35 ` [PATCH v2 19/22] nvmet: add context annotation for integrity params in struct nvmet_ns Nilay Shroff
` (3 subsequent siblings)
21 siblings, 1 reply; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:34 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::p2p_dev and nvmet_ns::use_p2pmem fields are protected by
nvmet_subsys::lock. However, nvmet_ns_p2pmem_show() accesses these
fields without holding the corresponding lock, triggering Clang context
analyzer errors:
drivers/nvme/target/configfs.c:587:42: error: reading variable 'p2p_dev' requires holding mutex 'to_nvmet_ns(item).subsys->lock'
[-Werror,-Wthread-safety-analysis]
587 | return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
| ^
drivers/nvme/target/configfs.c:587:55: error: reading variable 'use_p2pmem' requires holding mutex 'to_nvmet_ns(item).subsys->lock'
[-Werror,-Wthread-safety-analysis]
587 | return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
| ^
Fix the above errors by acquiring ns->subsys->lock before accessing
PCI P2P parameters in nvmet_ns_p2pmem_show().
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/configfs.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index a69405279fe3..d7a153dbd494 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -582,8 +582,13 @@ CONFIGFS_ATTR(nvmet_ns_, device_path);
static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
{
struct nvmet_ns *ns = to_nvmet_ns(item);
+ ssize_t ret;
- return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
+ mutex_lock(&ns->subsys->lock);
+ ret = pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
+ mutex_unlock(&ns->subsys->lock);
+
+ return ret;
}
static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* Re: [PATCH v2 18/22] nvmet: fix PCI P2P parameters access using nvmet_subsys::lock
2026-11-18 7:34 ` [PATCH v2 18/22] nvmet: fix PCI P2P parameters access using nvmet_subsys::lock Nilay Shroff
@ 2026-11-18 7:55 ` sashiko-bot
0 siblings, 0 replies; 25+ messages in thread
From: sashiko-bot @ 2026-11-18 7:55 UTC (permalink / raw)
To: Nilay Shroff; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] nvmet: missing Fixes trailer for nvmet_ns_p2pmem_show() lock fix
--
commit 0c0ad9daba7f9b5b285f1b0f44cf7bc001282d91
Author: Nilay Shroff <nilay@linux.ibm.com>
> nvmet: fix PCI P2P parameters access using nvmet_subsys::lock
>
> The nvmet_ns::p2p_dev and nvmet_ns::use_p2pmem fields are protected by
> nvmet_subsys::lock. However, nvmet_ns_p2pmem_show() accesses these
> fields without holding the corresponding lock, triggering Clang context
> analyzer errors:
>
> drivers/nvme/target/configfs.c:587:42: error: reading variable 'p2p_dev' requires holding mutex 'to_nvmet_ns(item).subsys->lock'
> [-Werror,-Wthread-safety-analysis]
> 587 | return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
> | ^
> drivers/nvme/target/configfs.c:587:55: error: reading variable 'use_p2pmem' requires holding mutex 'to_nvmet_ns(item).subsys->lock'
> [-Werror,-Wthread-safety-analysis]
> 587 | return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
> | ^
>
> Fix the above errors by acquiring ns->subsys->lock before accessing
> PCI P2P parameters in nvmet_ns_p2pmem_show().
>
> Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
[Severity: Low]
Since nvmet_ns_p2pmem_store() updates ns->p2p_dev and drops the old PCI
device reference via pci_dev_put() under ns->subsys->lock, reading
ns->p2p_dev in nvmet_ns_p2pmem_show() without holding ns->subsys->lock is
a concurrency bug introduced with P2P memory support. Should this include:
Fixes: c6925093d0b2 ("nvmet: Optionally use PCI P2P memory")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118073505.288910-1-nilay@linux.ibm.com?part=18
^ permalink raw reply [flat|nested] 25+ messages in thread
* [PATCH v2 19/22] nvmet: add context annotation for integrity params in struct nvmet_ns
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (17 preceding siblings ...)
2026-11-18 7:34 ` [PATCH v2 18/22] nvmet: fix PCI P2P parameters access using nvmet_subsys::lock Nilay Shroff
@ 2026-11-18 7:35 ` Nilay Shroff
2026-11-18 7:35 ` [PATCH v2 20/22] nvmet: add context annotation for nvmet_ns::csi Nilay Shroff
` (2 subsequent siblings)
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:35 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::pi_type and nvmet_ns::metadata_size fields are protected
by nvmet_subsys::lock. Annotate them with __guarded_by(&subsys->lock)
to allow the Clang context analyzer to validate accesses to these fields.
Some request-processing and I/O paths access these fields without
holding subsys->lock. This is safe because the namespace reference held
by the request prevents the integrity parameters from being modified
while I/O is in flight. However, the context analyzer cannot express
this access pattern directly.
Introduce nvmet_ns_pi_type() and nvmet_ns_metadata_size() as inline
helpers annotated with __context_unsafe() to allow these lockless
accesses without triggering context analyzer warnings.
Use these helpers in the namespace identify path, metadata length
calculation, PI capability check, and RDMA signature attribute setup.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 4 ++--
drivers/nvme/target/nvmet.h | 13 +++++++++----
drivers/nvme/target/rdma.c | 2 +-
3 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index 75a833253545..ea43d83d269b 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -939,9 +939,9 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)
NVME_NS_DPC_PI_TYPE1 | NVME_NS_DPC_PI_TYPE2 |
NVME_NS_DPC_PI_TYPE3;
id->mc = NVME_MC_EXTENDED_LBA;
- id->dps = req->ns->pi_type;
+ id->dps = nvmet_ns_pi_type(req->ns);
id->flbas = NVME_NS_FLBAS_META_EXT;
- id->lbaf[0].ms = cpu_to_le16(req->ns->metadata_size);
+ id->lbaf[0].ms = cpu_to_le16(nvmet_ns_metadata_size(req->ns));
}
if (nvmet_ns_readonly(req->ns))
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 08cf6364c4a6..1da9cfacfc4c 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -188,8 +188,10 @@ struct nvmet_ns {
__guarded_by(&subsys->lock);
int use_p2pmem
__guarded_by(&subsys->lock);
- int pi_type;
- int metadata_size;
+ int pi_type
+ __guarded_by(&subsys->lock);
+ int metadata_size
+ __guarded_by(&subsys->lock);
u8 csi;
struct nvmet_pr pr;
struct xarray pr_per_ctrl_refs;
@@ -219,6 +221,8 @@ NVMET_NS_ACCESSOR(u8 *, nguid)
NVMET_NS_ACCESSOR(uuid_t, uuid)
NVMET_NS_ACCESSOR(bool, buffered_io)
NVMET_NS_ACCESSOR(const char *, device_path)
+NVMET_NS_ACCESSOR(int, pi_type)
+NVMET_NS_ACCESSOR(int, metadata_size)
/*
* These accessors use data_race() and READ_ONCE() marker to allow lockless
@@ -830,7 +834,7 @@ static inline u32 nvmet_rw_metadata_len(struct nvmet_req *req)
if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
return 0;
return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) *
- req->ns->metadata_size;
+ nvmet_ns_metadata_size(req->ns);
}
static inline u32 nvmet_dsm_len(struct nvmet_req *req)
@@ -1010,7 +1014,8 @@ static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns)
{
if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
return false;
- return ns->pi_type && ns->metadata_size == sizeof(struct t10_pi_tuple);
+ return nvmet_ns_pi_type(ns) &&
+ nvmet_ns_metadata_size(ns) == sizeof(struct t10_pi_tuple);
}
static inline __le64 nvmet_sect_to_lba(struct nvmet_ns *ns, sector_t sect)
diff --git a/drivers/nvme/target/rdma.c b/drivers/nvme/target/rdma.c
index 0b351d39340c..2cec457f12fd 100644
--- a/drivers/nvme/target/rdma.c
+++ b/drivers/nvme/target/rdma.c
@@ -591,7 +591,7 @@ static void nvmet_rdma_set_sig_attrs(struct nvmet_req *req,
{
struct nvme_command *cmd = req->cmd;
u16 control = le16_to_cpu(cmd->rw.control);
- u8 pi_type = req->ns->pi_type;
+ u8 pi_type = nvmet_ns_pi_type(req->ns);
struct blk_integrity *bi;
bi = bdev_get_integrity(nvmet_ns_bdev(req->ns));
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 20/22] nvmet: add context annotation for nvmet_ns::csi
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (18 preceding siblings ...)
2026-11-18 7:35 ` [PATCH v2 19/22] nvmet: add context annotation for integrity params in struct nvmet_ns Nilay Shroff
@ 2026-11-18 7:35 ` Nilay Shroff
2026-11-18 7:35 ` [PATCH v2 21/22] nvmet: fix nvmet_ns::csi access using nvmet_subsys::lock Nilay Shroff
2026-11-18 7:35 ` [PATCH v2 22/22] nvmet: add context annotation for nvmet_ns::debugfs_dir Nilay Shroff
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:35 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::csi field is protected by nvmet_subsys::lock. Annotate
it with __guarded_by(&subsys->lock) so that the clang context analyzer
can validate accesses to this field.
Some accesses to nvmet_ns::csi in the I/O hotpath do not require
holding nvmet_subsys::lock, as the namespace reference guarantees that
the namespace remains valid and the field is not modified while I/O is
in flight. However, the clang context analyzer cannot express this
access pattern directly.
Introduce nvmet_ns_csi() as an inline helper annotated with
__context_unsafe() to allow these accesses without requiring the
subsystem lock. Convert the relevant accesses in the I/O hotpath and
request-processing paths to use this helper.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 4 +++-
drivers/nvme/target/core.c | 2 +-
drivers/nvme/target/nvmet.h | 4 +++-
3 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index ea43d83d269b..b0ec1ed7ddfa 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -1059,6 +1059,7 @@ static void nvmet_execute_identify_desclist(struct nvmet_req *req)
off_t off = 0;
u16 status;
uuid_t uuid;
+ u8 csi;
status = nvmet_req_find_ns(req);
if (status)
@@ -1081,9 +1082,10 @@ static void nvmet_execute_identify_desclist(struct nvmet_req *req)
goto out;
}
+ csi = nvmet_ns_csi(req->ns);
status = nvmet_copy_ns_identifier(req, NVME_NIDT_CSI,
NVME_NIDT_CSI_LEN,
- &req->ns->csi, &off);
+ &csi, &off);
if (status)
goto out;
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index 6c22d5d97d1c..e3aab719629c 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -1175,7 +1175,7 @@ static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
return ret;
}
- switch (req->ns->csi) {
+ switch (nvmet_ns_csi(req->ns)) {
case NVME_CSI_NVM:
if (nvmet_ns_file(req->ns))
ret = nvmet_file_parse_io_cmd(req);
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 1da9cfacfc4c..1123c9c708df 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -192,7 +192,8 @@ struct nvmet_ns {
__guarded_by(&subsys->lock);
int metadata_size
__guarded_by(&subsys->lock);
- u8 csi;
+ u8 csi
+ __guarded_by(&subsys->lock);
struct nvmet_pr pr;
struct xarray pr_per_ctrl_refs;
#ifdef CONFIG_NVME_TARGET_DEBUGFS
@@ -223,6 +224,7 @@ NVMET_NS_ACCESSOR(bool, buffered_io)
NVMET_NS_ACCESSOR(const char *, device_path)
NVMET_NS_ACCESSOR(int, pi_type)
NVMET_NS_ACCESSOR(int, metadata_size)
+NVMET_NS_ACCESSOR(u8, csi)
/*
* These accessors use data_race() and READ_ONCE() marker to allow lockless
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 21/22] nvmet: fix nvmet_ns::csi access using nvmet_subsys::lock
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (19 preceding siblings ...)
2026-11-18 7:35 ` [PATCH v2 20/22] nvmet: add context annotation for nvmet_ns::csi Nilay Shroff
@ 2026-11-18 7:35 ` Nilay Shroff
2026-11-18 7:35 ` [PATCH v2 22/22] nvmet: add context annotation for nvmet_ns::debugfs_dir Nilay Shroff
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:35 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::csi field is protected by nvmet_subsys::lock.
However, nvmet_execute_identify_nslist() accesses ns->csi without
holding the corresponding lock, triggering a Clang context analyzer
error:
drivers/nvme/target/admin-cmd.c:1016:24: error: reading variable 'csi' requires holding mutex 'ns->subsys->lock'
[-Werror,-Wthread-safety-analysis]
1016 | if (match_css && ns->csi != req->cmd->identify.csi)
| ^
Fix this by acquiring ctrl->subsys->lock before iterating over the
enabled namespaces.
Although ctrl->subsys->lock and ns->subsys->lock refer to the same
mutex, the Clang context analyzer cannot recognize that they represent
the same lock because they are accessed through different expressions.
Explicitly assert the ns->subsys->lock capability using
__assume_ctx_lock() before accessing ns->csi, and release the assumed
capability using __release_ctx_lock() afterward.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index b0ec1ed7ddfa..de13614b3cb0 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -1015,15 +1015,26 @@ static void nvmet_execute_identify_nslist(struct nvmet_req *req, bool match_css)
goto out;
}
+ mutex_lock(&ctrl->subsys->lock);
nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
- if (ns->nsid <= min_nsid)
- continue;
- if (match_css && ns->csi != req->cmd->identify.csi)
- continue;
- list[i++] = cpu_to_le32(ns->nsid);
+ /*
+ * Both ctrl->subsys->lock and ns->subsys->lock refer to the
+ * same lock. However, there's no way to annotate such locking
+ * semantics using clang context annotation. So explicitly
+ * assert the capability.
+ */
+ __assume_ctx_lock(&ns->subsys->lock);
+
+ if (ns->nsid > min_nsid &&
+ (!match_css || ns->csi == req->cmd->identify.csi))
+ list[i++] = cpu_to_le32(ns->nsid);
+
+ __release_ctx_lock(&ns->subsys->lock);
+
if (i == buf_size / sizeof(__le32))
break;
}
+ mutex_unlock(&ctrl->subsys->lock);
status = nvmet_copy_to_sgl(req, 0, list, buf_size);
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH v2 22/22] nvmet: add context annotation for nvmet_ns::debugfs_dir
2026-11-18 7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
` (20 preceding siblings ...)
2026-11-18 7:35 ` [PATCH v2 21/22] nvmet: fix nvmet_ns::csi access using nvmet_subsys::lock Nilay Shroff
@ 2026-11-18 7:35 ` Nilay Shroff
21 siblings, 0 replies; 25+ messages in thread
From: Nilay Shroff @ 2026-11-18 7:35 UTC (permalink / raw)
To: kbusch, hch, bvanassche, sagi, axboe, chaitanyak
Cc: gjoyce, elver, linux-nvme, Nilay Shroff
The nvmet_ns::debugfs_dir field is protected by nvmet_subsys::lock.
Annotate it with __guarded_by(&subsys->lock) so that the clang context
analyzer can validate accesses to this field.
Also, mark nvmet_debugfs_ns_setup() and nvmet_debugfs_ns_free() with
__must_hold(&ns->subsys->lock) to make its locking requirement explicit.
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
drivers/nvme/target/debugfs.h | 6 ++++--
drivers/nvme/target/nvmet.h | 3 ++-
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/drivers/nvme/target/debugfs.h b/drivers/nvme/target/debugfs.h
index b559d254fc2a..ebab05ddf3e6 100644
--- a/drivers/nvme/target/debugfs.h
+++ b/drivers/nvme/target/debugfs.h
@@ -14,8 +14,10 @@ int nvmet_debugfs_subsys_setup(struct nvmet_subsys *subsys);
void nvmet_debugfs_subsys_free(struct nvmet_subsys *subsys);
int nvmet_debugfs_ctrl_setup(struct nvmet_ctrl *ctrl);
void nvmet_debugfs_ctrl_free(struct nvmet_ctrl *ctrl);
-void nvmet_debugfs_ns_setup(struct nvmet_ns *ns);
-void nvmet_debugfs_ns_free(struct nvmet_ns *ns);
+void nvmet_debugfs_ns_setup(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock);
+void nvmet_debugfs_ns_free(struct nvmet_ns *ns)
+ __must_hold(&ns->subsys->lock);
int __init nvmet_init_debugfs(void);
void nvmet_exit_debugfs(void);
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 1123c9c708df..e6309960f026 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -197,7 +197,8 @@ struct nvmet_ns {
struct nvmet_pr pr;
struct xarray pr_per_ctrl_refs;
#ifdef CONFIG_NVME_TARGET_DEBUGFS
- struct dentry *debugfs_dir;
+ struct dentry *debugfs_dir
+ __guarded_by(&subsys->lock);
#endif
};
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread