[PATCH 01/23] nvmet: add context annotation for nvmet_ns::bdev
Nilay Shroff
nilay at linux.ibm.com
Tue Nov 10 23:25:23 PST 2026
On 11/10/26 10:59 PM, Bart Van Assche wrote:
> On 11/9/26 10:38 PM, Nilay Shroff wrote:
>> I agree that passing the lock owner as a function argument is preferable whenever
>> possible. For this particular case, I tried passing subsys down the call chain to
>> nvmet_ns_dev() and annotating the function with __must_hold(&subsys- >lock). However,
>> Clang still reports a locking violation when accessing ns->bdev:
>
> Please take a look at the attached patches and consider to add these to
> the start of this series. These patches are needed to implement what I
> suggested in my previous email in this email thread.
>
I reviewed the patches you attached and tried implementing your suggestion by passing
the struct nvmet_subsys pointer down the call chain to nvmet_ns_dev(), which accesses
ns->bdev protected by subsys->lock.
The relevant code looks like this:
struct nvmet_subsys {
...
struct mutex lock;
...
}
struct nvmet_ns {
...
struct block_device *bdev __guarded_by(&subsys->lock);
...
struct nvmet_subsys *subsys;
...
}
static inline struct device *nvmet_ns_dev(struct nvmet_subsys *subsys,
struct nvmet_ns *ns)
__must_hold(&subsys->lock)
{
return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL;
}
Attached the complete patch for your reference. Even after making
this change, clang still complains about locking violation:
drivers/nvme/target/nvmet.h:255:13: error: reading variable 'bdev' requires holding mutex 'ns->subsys->lock' [-Werror,-Wthread-safety-precise]
255 | return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL;
| ^
drivers/nvme/target/nvmet.h:255:13: note: found near match 'subsys->lock'
From this error, it appears that the Clang context analyzer expects accesses
to ns->bdev to be protected specifically by ns->subsys->lock. It recognizes
subsys->lock as a near match but cannot establish that both expressions refer
to the same mutex.
Therefore, passing the subsys pointer down to nvmet_ns_dev() and annotating the
function with __must_hold(&subsys->lock) does not resolve the warning in this case.
I also looked at the approach in your proposed patches for the NVMe host driver.
There, passing the struct nvme_subsystem pointer down the call chain works because
the list being protected, nsheads, is itself a member of struct nvme_subsystem:
struct nvme_subsystem {
....
struct mutex lock;
struct list_head nsheads __guarded_by(&lock);
....
}
In this case, the analyzer can associate accesses to subsys->nsheads directly with
subsys->lock, allowing __must_hold(&subsys->lock) to express the locking requirement
correctly.
The situation is different for nvmet_ns::bdev, where the lock protecting the field
belongs to the namespace's subsystem which is a separate struct nvmet_subsys. It seems
the analyzer cannot establish the equivalence between subsys->lock and ns->subsys->lock,
even when both pointers refer to the same subsystem.
Regarding your patches, I'm happy to include them in my series. However, my current
series focuses on adding context annotations to the NVMe target driver, whereas your
proposed patches address the NVMe host driver. So I'd suggest posting your changes as
a separate series so they can be reviewed independently.
Thanks,
--Nilay
-------------- next part --------------
A non-text attachment was scrubbed...
Name: nvmet-ns-bdev.diff
Type: text/x-patch
Size: 5288 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-nvme/attachments/20261111/e16200cd/attachment-0001.bin>
More information about the Linux-nvme
mailing list