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

Nilay Shroff nilay at linux.ibm.com
Thu Nov 19 06:20:50 PST 2026


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 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.

>> 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.
> 
> 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




More information about the Linux-nvme mailing list