nvmet: add context annotation for params in struct nvmet_ns
Subsystems (2)
v2 cover nvmet: add context annotation for params in struct nvmet_ns
Hi,
This series adds Clang context annotations to fields in struct nvmet_ns that are protected by nvmet_subsys::lock.
The goal is to incrementally introduce context annotations throughout the NVMe target code. Covering the entire nvmet subsystem in a single series would result in a very large patchset with dozens of patches, making focused review difficult. To keep the changes manageable, I have split the work into multiple series, each focusing on a specific subset of context annotations.
This series focuses on fields in struct nvmet_ns protected by nvmet_subsys::lock. Subsequent series will expand the scope to other structures and locks.
The patches are organized so that each field is handled incrementally:
first, a field is annotated with __guarded_by(&subsys->lock), and then any access violations reported by the Clang context analyzer are addressed in follow-up patches. Not every field annotation necessarily triggers analyzer warnings. In such cases, the series simply moves on to the next field.
This approach keeps individual patches focused and makes it easier to review the annotations and the corresponding locking changes independently.
As always, review feedback, comments, and suggestions are very welcome!
Changes from v1:
- Dropped patches #6/23 and #7/23, which annotated ns->nsid with __guarded_by(&ns->subsys->lock). Since ns->nsid is immutable during the lifetime of a namespace, it does not require additional protection (Bart Van Assche)
- Since the Clang context analyzer cannot infer that the same lock is
being accessed through different expressions (e.g., subsys->lock and
ns->subsys->lock), pass subsys as an additional argument through the
relevant function call chains and use lockdep_assert_held(&ns->subsys->lock)
instead of __assume_ctx_lock() wherever possible (Bart Van Assche).Link to v1: https://lore.kernel.org/all/[email protected]/
Nilay Shroff (22):
nvmet: add context annotation for nvmet_ns::bdev
nvmet: fix nvmet_ns::bdev access using nvmet_subsys::lock
nvmet: add context annotation for nvmet_ns::file
nvmet: add context annotation for nvmet_ns::bdev_file
nvmet: add context annotation for nvmet_ns::readonly
nvmet: add context annotation for nvmet_ns::blksize_shift
nvmet: add context annotation for nvmet_ns::size
nvmet: add context annotation for nvmet_ns::nguid
nvmet: add context annotation for nvmet_ns::uuid
nvmet: fix nvmet_ns::uuid access using nvmet_subsys::lock
nvmet: add context annotation for nvmet_ns::buffered_io
nvmet: fix nvmet_ns::buffered_io access using nvmet_subsys::lock
nvmet: add context annotation for nvmet_ns::enabled
nvmet: fix nvmet_ns::enable access using nvmet_subsys::lock
nvmet: add context annotation for nvmet_ns::device_path
nvmet: fix nvmet_ns::device_path access using nvmet_subsys::lock
nvmet: add context annotation for PCI P2P parameters in struct
nvmet_ns
nvmet: fix PCI P2P parameters access using nvmet_subsys::lock
nvmet: add context annotation for integrity params in struct nvmet_ns
nvmet: add context annotation for nvmet_ns::csi
nvmet: fix nvmet_ns::csi access using nvmet_subsys::lock
nvmet: add context annotation for nvmet_ns::debugfs_dir
drivers/nvme/target/admin-cmd.c | 135 ++++++++++------- drivers/nvme/target/configfs.c | 54 ++++++- drivers/nvme/target/core.c | 88 +++++++---- drivers/nvme/target/debugfs.h | 6 +- drivers/nvme/target/io-cmd-bdev.c | 30 ++-- drivers/nvme/target/io-cmd-file.c | 35 +++-- drivers/nvme/target/nvmet.h | 233 +++++++++++++++++++----------- drivers/nvme/target/rdma.c | 4 +- drivers/nvme/target/trace.h | 2 +- drivers/nvme/target/zns.c | 46 +++--- 10 files changed, 405 insertions(+), 228 deletions(-)
base-commit: 68099cf27253a8c89d703a282d3d811446d9453b
Signature · 1 line
2.53.0
PATCH v2 1/22 nvmet: add context annotation for nvmet_ns::bdev
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().
--- 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(-)
drivers/nvme/target/admin-cmd.c +28 -24
@@ -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; }
drivers/nvme/target/core.c +19 -9
@@ -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);
drivers/nvme/target/io-cmd-bdev.c +12 -10
@@ -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;
drivers/nvme/target/nvmet.h +80 -56
176 lines not on this page · raw message
drivers/nvme/target/rdma.c +1 -1
@@ -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));
drivers/nvme/target/zns.c +23 -21
133 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev
Quoted 8 lines · Nilay Shroff
Some accesses to
nvmet_ns::bdevin the I/O hotpath do not require holdingnvmet_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 introducenvmet_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.
This is not what __context_unsafe() is intended for. Has it been considered to introduce a token context lock and to annotate the functions called with either the namespace reference held or with the nvmet_subsys::lock held?
Quoted 4 lines
Also pass subsys as an explicit argument to
nvmet_p2pmem_ns_add_p2p()andnvmet_setup_p2p_ns_map(). This allows their__must_hold()annotations and lockdep assertions to referencesubsys->lockdirectly, rather than accessing the same lock throughctrl->subsys->lock.
How many changes does this patch include? Should this patch perhaps be split?
Thanks,
Bart.
Re: [PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev
Quoted 10 lines · Bart Van Assche
Quoted 8 lines · Nilay Shroff
Some accesses to
nvmet_ns::bdevin the I/O hotpath do not require holdingnvmet_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 introducenvmet_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.
Quoted 5 lines
This is not what
__context_unsafe()is intended for. Has it been considered to introduce a token context lock and to annotate the functions called with either the namespace reference held or with thenvmet_subsys::lockheld?
I haven't considered using a token context lock so far, but token context lock looks interesting and plausible. I'll explore this approach further. At a high level, I think we would need something along these lines:
/* Declare the context lock */token_context_lock(nvmet_ns_ref);
/* Annotate fields with this lock */
struct nvmet_ns {
...
struct block_device *bdev
__guarded_by(&subsys->lock, nvmet_ns_ref);
...
};The namespace lookup and release functions could then be annotated as follows:
u16 nvmet_req_find_ns(struct nvmet_req *req)
__cond_acquires_shared(0, nvmet_ns_ref);
void nvmet_ns_put(struct nvmet_ns *ns)
__releases_shared(nvmet_ns_ref);
These annotations would allow the I/O hotpath to access namespace fields while holding a namespace reference, without requiring nvmet_subsys::lock.
For the control path, which updates namespace fields while holding nvmet_subsys::lock, we could introduce wrappers along these lines:
static inline void nvmet_subsys_lock(struct nvmet_subsys *subsys)
__acquires(nvmet_ns_ref)
{
mutex_lock(&subsys->lock);
__acquire(nvmet_ns_ref);
}static inline void nvmet_subsys_unlock(struct nvmet_subsys *subsys)
__releases(nvmet_ns_ref)
{
__release(nvmet_ns_ref);
mutex_unlock(&subsys->lock);
}If we can successfully model the namespace reference-counting semantics using token context locks, that would be a nice improvement. It would also allow us to get rid of the NVMET_NS_ACCESSOR() macro, which currently expands into helpers marked with __context_unsafe().
I'll first experiment with modeling the namespace reference-counting logic using token context locks. If that works out, I'll respin the series accordingly.
Quoted 8 lines
Quoted 4 lines
Also pass subsys as an explicit argument to
nvmet_p2pmem_ns_add_p2p()andnvmet_setup_p2p_ns_map(). This allows their__must_hold()annotations and lockdep assertions to referencesubsys->lockdirectly, rather than accessing the same lock throughctrl->subsys->lock.How many changes does this patch include? Should this patch perhaps be split?
Yes in the next revision, I'll split the changes.
Thanks,
--Nilay
Re: [PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev
In reply to Bart Van AsscheHi Bart,
Quoted 10 lines · Bart Van Assche
Quoted 8 lines · Nilay Shroff
Some accesses to
nvmet_ns::bdevin the I/O hotpath do not require holdingnvmet_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 introducenvmet_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.
Quoted 5 lines
This is not what
__context_unsafe()is intended for. Has it been considered to introduce a token context lock and to annotate the functions called with either the namespace reference held or with thenvmet_subsys::lockheld?
I spent some time evaluating whether we could model namespace references using token context locks, but it looks like this approach does not fit the namespace-reference lifetime particularly well.
A namespace reference is optional. It is acquired after a successful namespace lookup and stored in req->ns. Later, nvmet_req_complete() drops the reference only if req->ns is non-NULL. Clang's conditional context annotations cannot express this ownership relationship directly. They can conditionally acquire or release a context based on a function's return value. Consequently, modeling this with token context locks would likely require splitting several existing completion and error paths into separate variants depending on whether the request holds a namespace reference.
Callback dispatch makes this even more complicated. req->execute is a common callback used by I/O commands, Fabrics commands, and admin commands. I/O handlers generally execute with a namespace reference held, whereas Fabrics and many admin handlers do not require one. Annotating the req->execute ccallback type as requiring the namespace-reference token would therefore incorrectly impose that requirement on all command handlers. Leaving the callback type unannotated, on the other hand, means Clang cannot propagate the token requirement from the dispatcher to individual I/O handlers.
Splitting the callback mechanism solely to accommodate context annotations would add considerable complexity to the existing design.
Given these constraints, I think it would be more practical to retain the NVMET_NS_ACCESSOR() helpers marked with __context_unsafe() for accesses protected by a live namespace reference. We can keep these helpers narrowly scoped to the relevant namespace-field accesses and document the namespace- reference requirement explicitly.
For control paths, we can continue using lock-based context annotations, since nvmet_subsys::lock directly describes the synchronization protecting those fields.
This approach does not provide the same level of static checking for I/O-path accesses, but it avoids introducing substantial complexity into namespace- reference handling and request dispatch.
Thanks,
--Nilay
Re: [PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev
In reply to Nilay Shroffdrivers/nvme/target/nvmet.h struct nvmet_ns { · line 201
Quoted 13 lines · Nilay Shroff
drivers/nvme/target/nvmet.h +13
-0+/* +* 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; \ +}
Lock context analysis enables thread-safety checking at compile time. The accessors defined by NVMET_NS_ACCESSOR() disable compile time thread-safety checks. So these accessors defeat the purpose of this patch series, isn't it?
Thanks,
Bart.
Re: [PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev
Quoted 11 lines · Bart Van Assche
Quoted 10 lines · Nilay Shroff
drivers/nvme/target/nvmet.h +10
-0+/* +* 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 */) \
Quoted 9 lines
drivers/nvme/target/nvmet.h struct nvmet_ns { · line 201
Quoted 3 lines
drivers/nvme/target/nvmet.h +3
-0+{ \ + return ns->_name; \ +}Lock context analysis enables thread-safety checking at compile time. The accessors defined by
NVMET_NS_ACCESSOR()disable compile time thread-safety checks. So these accessors defeat the purpose of this patch series, isn't it?
By design, the NVMe target I/O hotpaths access namespace parameters without holding subsys->lock. Instead, they acquire a namespace reference before accessing these parameters.
The namespace reference provides the required synchronization guarantee: namespace parameters cannot be modified while outstanding namespace references exist. When a namespace is disabled, the target first prevents new references from being acquired and then waits for all outstanding references to drain before allowing namespace parameters to be modified. This allows I/O hotpaths to safely access namespace parameters without acquiring subsys->lock, avoiding the overhead of taking a mutex in the I/O and request-processing paths.
On the other hand, control paths that access or modify namespace parameters must continue to hold subsys->lock. The problem is that Clang's context annotations can express lock-based access requirements, but cannot currently express the above reference- counting-based synchronization guarantee. That's why I introduced the NVMET_NS_ACCESSOR() macro. The accessors generated by this macro are marked with __context_unsafe() to explicitly opt out of context checking for these particular accesses. They are intended to be used only in I/O hotpaths and request-processing paths where a namespace reference has already been acquired.
This does not eliminate context checking for namespace parameter accesses in general. Control-path accesses are still subject to the __guarded_by(&subsys->lock) annotations and must satisfy the corresponding locking requirements.
Thanks,
--Nilay
PATCH v2 2/22 nvmet: fix nvmet_ns::bdev access using nvmet_subsys::lock
In reply to Nilay Shroffnvmet_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.
--- drivers/nvme/target/admin-cmd.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/nvme/target/admin-cmd.c +15 -1
@@ -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 ]);
Signature · 1 line
2.53.0
Re: [PATCH v2 02/22] nvmet: fix nvmet_ns::bdev access using nvmet_subsys::lock
drivers/nvme/target/admin-cmd.c static u16 nvmet_get_smart_log_all(struct nvmet_req *req, · line 300
Quoted 7 lines · Nilay Shroff
drivers/nvme/target/admin-cmd.c +7
-0+/* +* 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);
__assume_ctx_lock() should be avoided whenever possible. Please either modify this function such that "subsys" is passed as an argument or use lockdep_assert_held().
Thanks,
Bart.
Re: [PATCH v2 02/22] nvmet: fix nvmet_ns::bdev access using nvmet_subsys::lock
Quoted 9 lines · Bart Van Assche
Quoted 7 lines · Nilay Shroff
+ /*
+ * Bothctrl->subsys->lockandns->subsys->lockrefer 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);
Quoted 4 lines
__assume_ctx_lock()should be avoided whenever possible. Please either modify this function such that "subsys" is passed as an argument or uselockdep_assert_held().
Yes, okay. I need to extract the namespace iteration into a helper function and pass subsys as an argument. This will allow to annotate the helper with
__must_hold(&subsys->lock) and then use lockdep_assert_held() to establish
the context for accessing namespace fields, avoiding the explicit use of
__assume_ctx_lock().
Thanks,
--Nilay
PATCH v2 3/22 nvmet: add context annotation for nvmet_ns::file
In reply to Nilay ShroffThe 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().
--- 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(-)
drivers/nvme/target/admin-cmd.c +3 -2
@@ -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);
drivers/nvme/target/core.c +1 -1
@@ -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);
drivers/nvme/target/io-cmd-file.c +10 -9
@@ -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 ); }
drivers/nvme/target/nvmet.h +10 -5
@@ -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);
Signature · 1 line
2.53.0
PATCH v2 4/22 nvmet: add context annotation for nvmet_ns::bdev_file
In reply to Nilay ShroffThe 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.
--- drivers/nvme/target/nvmet.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/target/nvmet.h +2 -1
@@ -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
Signature · 1 line
2.53.0
PATCH v2 5/22 nvmet: add context annotation for nvmet_ns::readonly
In reply to Nilay ShroffThe 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.
--- 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(-)
drivers/nvme/target/admin-cmd.c +7 -8
@@ -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 ; }
drivers/nvme/target/core.c +1 -1
@@ -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:
drivers/nvme/target/nvmet.h +17 -1
@@ -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);
Signature · 1 line
2.53.0
PATCH v2 6/22 nvmet: add context annotation for nvmet_ns::blksize_shift
In reply to Nilay ShroffThe 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.
--- 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(-)
drivers/nvme/target/admin-cmd.c +2 -2
@@ -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 |
drivers/nvme/target/io-cmd-bdev.c +2 -2
@@ -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 );
drivers/nvme/target/io-cmd-file.c +4 -3
@@ -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 ));
drivers/nvme/target/nvmet.h +6 -4
@@ -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)
drivers/nvme/target/zns.c +1 -1
@@ -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));
Signature · 1 line
2.53.0
PATCH v2 7/22 nvmet: add context annotation for nvmet_ns::size
In reply to Nilay ShroffThe 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.
--- 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(-)
drivers/nvme/target/admin-cmd.c +2 -1
@@ -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 :
drivers/nvme/target/core.c +2 -2
@@ -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)
drivers/nvme/target/io-cmd-bdev.c +2 -2
@@ -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)
drivers/nvme/target/io-cmd-file.c +4 -3
@@ -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;
drivers/nvme/target/nvmet.h +3 -1
@@ -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) {
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v2 07/22] nvmet: add context annotation for nvmet_ns::size
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 --
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 <[email protected]>
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.
Quoted 4 lines
drivers/nvme/target/io-cmd-file.c +0
-0
[ ... ]
drivers/nvme/target/io-cmd-file.c static bool nvmet_file_check_lba_range(struct nvmet_req *req, u64 slba, · line 130
Quoted 10 lines
drivers/nvme/target/io-cmd-file.c +2
-1@@ -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 CPU1nvmet_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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v2 8/22 nvmet: add context annotation for nvmet_ns::nguid
In reply to Nilay ShroffThe 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.
--- 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(-)
drivers/nvme/target/admin-cmd.c +4 -3
@@ -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; }
drivers/nvme/target/configfs.c +8 -1
@@ -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,
drivers/nvme/target/nvmet.h +3 -1
@@ -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
Signature · 1 line
2.53.0
PATCH v2 9/22 nvmet: add context annotation for nvmet_ns::uuid
In reply to Nilay ShroffThe 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.
--- 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(-)
drivers/nvme/target/admin-cmd.c +4 -2
@@ -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; }
drivers/nvme/target/configfs.c +8 -1
@@ -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,
drivers/nvme/target/nvmet.h +3 -1
@@ -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
Signature · 1 line
2.53.0
PATCH v2 10/22 nvmet: fix nvmet_ns::uuid access using nvmet_subsys::lock
In reply to Nilay ShroffThe 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.
--- 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(-)
drivers/nvme/target/configfs.c +1 -1
@@ -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);
drivers/nvme/target/core.c +31 -17
70 lines not on this page · raw message
drivers/nvme/target/nvmet.h +1 -1
@@ -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,
Signature · 1 line
2.53.0
PATCH v2 11/22 nvmet: add context annotation for nvmet_ns::buffered_io
In reply to Nilay ShroffThe 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.
--- drivers/nvme/target/io-cmd-file.c | 2 +- drivers/nvme/target/nvmet.h | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/nvme/target/io-cmd-file.c +1 -1
@@ -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 ))
drivers/nvme/target/nvmet.h +3 -1
@@ -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 0unsigned 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
Signature · 1 line
2.53.0
PATCH v2 12/22 nvmet: fix nvmet_ns::buffered_io access using nvmet_subsys::lock
In reply to Nilay Shroffnvmet_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.
--- drivers/nvme/target/configfs.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/nvme/target/configfs.c +8 -1
@@ -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,
Signature · 1 line
2.53.0
PATCH v2 13/22 nvmet: add context annotation for nvmet_ns::enabled
In reply to Nilay ShroffThe 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.
--- drivers/nvme/target/nvmet.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/target/nvmet.h +2 -1
@@ -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 0unsigned long flags;struct nvmet_subsys *subsys;
Signature · 1 line
2.53.0
PATCH v2 14/22 nvmet: fix nvmet_ns::enable access using nvmet_subsys::lock
In reply to Nilay Shroffnvmet_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().
--- drivers/nvme/target/configfs.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/nvme/target/configfs.c +8 -1
@@ -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,
Signature · 1 line
2.53.0
PATCH v2 15/22 nvmet: add context annotation for nvmet_ns::device_path
In reply to Nilay ShroffThe 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.
--- drivers/nvme/target/nvmet.h | 4 +++- drivers/nvme/target/trace.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/nvme/target/nvmet.h +3 -1
@@ -175,7 +175,8 @@ struct nvmet_ns {#define NVMET_NS_IO_LIVE 0unsigned 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
drivers/nvme/target/trace.h +1 -1
@@ -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
Signature · 1 line
2.53.0
PATCH v2 16/22 nvmet: fix nvmet_ns::device_path access using nvmet_subsys::lock
In reply to Nilay ShroffThe 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.
--- drivers/nvme/target/configfs.c | 9 ++++++++- drivers/nvme/target/core.c | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-)
drivers/nvme/target/configfs.c +8 -1
@@ -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,
drivers/nvme/target/core.c +1 -1
@@ -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); }
Signature · 1 line
2.53.0
PATCH v2 17/22 nvmet: add context annotation for PCI P2P parameters in struct nvmet_ns
In reply to Nilay Shroffnvmet_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.
--- drivers/nvme/target/nvmet.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/nvme/target/nvmet.h +4 -2
@@ -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;
Signature · 1 line
2.53.0
PATCH v2 18/22 nvmet: fix PCI P2P parameters access using nvmet_subsys::lock
In reply to Nilay ShroffThe 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().
--- drivers/nvme/target/configfs.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/nvme/target/configfs.c +6 -1
@@ -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,
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v2 18/22] nvmet: fix PCI P2P parameters access using nvmet_subsys::lock
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 --
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 <[email protected]>
Quoted 16 lines
nvmet: fix PCI P2P parameters access using
nvmet_subsys::lockThe
nvmet_ns::p2p_devandnvmet_ns::use_p2pmemfields are protected bynvmet_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 | returnpci_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 | returnpci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
| ^
Quoted 4 lines
Fix the above errors by acquiring
ns->subsys->lockbefore accessing PCI P2P parameters innvmet_ns_p2pmem_show().Signed-off-by Nilay Shroff <[email protected]>
[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:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
PATCH v2 19/22 nvmet: add context annotation for integrity params in struct nvmet_ns
In reply to Nilay ShroffThe 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.
--- 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(-)
drivers/nvme/target/admin-cmd.c +2 -2
@@ -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))
drivers/nvme/target/nvmet.h +9 -4
@@ -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)
drivers/nvme/target/rdma.c +1 -1
@@ -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));
Signature · 1 line
2.53.0
PATCH v2 20/22 nvmet: add context annotation for nvmet_ns::csi
In reply to Nilay ShroffThe 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.
--- 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(-)
drivers/nvme/target/admin-cmd.c +3 -1
@@ -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;
drivers/nvme/target/core.c +1 -1
@@ -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);
drivers/nvme/target/nvmet.h +3 -1
@@ -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
Signature · 1 line
2.53.0
PATCH v2 21/22 nvmet: fix nvmet_ns::csi access using nvmet_subsys::lock
In reply to Nilay ShroffThe 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.
--- drivers/nvme/target/admin-cmd.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/nvme/target/admin-cmd.c +16 -5
@@ -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);
Signature · 1 line
2.53.0
PATCH v2 22/22 nvmet: add context annotation for nvmet_ns::debugfs_dir
In reply to Nilay ShroffThe 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.
--- drivers/nvme/target/debugfs.h | 6 ++++-- drivers/nvme/target/nvmet.h | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-)
drivers/nvme/target/debugfs.h +4 -2
@@ -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 );
drivers/nvme/target/nvmet.h +2 -1
@@ -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 };
Signature · 1 line
2.53.0