[PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev

Nilay Shroff nilay at linux.ibm.com
Fri Nov 20 09:26:08 PST 2026


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.

Thanks,
--Nilay




More information about the Linux-nvme mailing list