[PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev
Nilay Shroff
nilay at linux.ibm.com
Mon Nov 23 00:55:16 PST 2026
On 11/20/26 10:56 PM, Nilay Shroff wrote:
> Hi Bart,
>
> On 11/19/26 5:36 AM, Bart Van Assche wrote:
>> On 11/17/26 11:34 PM, Nilay Shroff wrote:
>>> 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.
>>
>> 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?
>>
>
> 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.
>
After thinking about it for a while, I found a middle ground which shall
help getting rid of context_unsafe annotation around nvmet namespace
accessor macro while still using token context lock:
// Define token context lock modeling ns ref count
token_context_lock(nvmet_ns_ref);
Then, we guard ns field using both subsys->lock and nvmet_ns_ref.
struct nvmet_ns {
...
struct block_device *bdev
__guarded_by(&subsys->lock, nvmet_ns_ref);
...
}
Then I'd define helpers which are just wrapper around subsys->lock.
static inline void nvmet_subsys_lock(struct nvmet_subsys *subsys)
__acquires(&subsys->lock)
__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)
__releases(&subsys->lock)
{
__release(nvmet_ns_ref);
mutex_unlock(&subsys->lock);
}
These wrappers model holding subsys->lock as also holding the exclusive
nvmet_ns_ref context. This allows the control path to satisfy both
context requirements when updating ns->bdev.
For I/O-path accesses, I would retain the accessor macro, but instead of
__context_unsafe() assume an explicit shared-context assumption and also
add a lokcdep assertion for runtime validation:
#define NVMET_NS_ACCESSOR(_type, _name) \
static inline _type nvmet_ns_##_name(struct nvmet_req *req) \
{ \
lockdep_assert(req->ns != NULL); \
__assume_shared_ctx_lock(nvmet_ns_ref); \
return req->ns->_name; \
}
With this arrangement, Clang's context analyzer would enforce the following:
1. Reading ns->bdev requires holding at least one of subsys->lock or nvmet_ns_ref.
I/O-path accesses would go through nvmet_ns_bdev(), which assumes the shared
namespace-reference context.
2. Updating ns->bdev requires holding both subsys->lock and nvmet_ns_ref. The
subsystem lock wrappers provide both capabilities to the control path.
3. Direct/plain accesses to ns->bdev outside the accessor would still be checked
by Clang. An access without either capability would result in a compilation
warning. This would alert the user about requirement of accessing ns->bdev
only after acquiring subsys->lock or nvmet_ns_ref token context lock or access
ns->bdev after acquiring namespace reference (if called from the I/O hotpath).
And at runtime (when lockdep is enabled), the accessor checks that req->ns is
non-NULL. This relies on the fact that req->ns is populated only after successfully
acquiring a namespace reference and remains valid until request completion.
The main advantage is that we retain static checking for direct accesses to ns->bdev,
while confining the context assumption to a small accessor used by the I/O path.
This also avoids having to propagate the namespace-reference token through the
common req->execute callback and deal with the different reference-ownership
requirements of I/O, Fabrics and admin commands.
I think this could provide a reasonable compromise between static checking and the
existing nvmet request-lifetime design. Any thoughts?
Thanks,
--Nilay
More information about the Linux-nvme
mailing list