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

Nilay Shroff nilay at linux.ibm.com
Wed Nov 18 23:03:28 PST 2026


On 11/19/26 5:55 AM, Bart Van Assche wrote:
> On 11/17/26 11:34 PM, Nilay Shroff wrote:
>> +/*
>> + * 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?
> 
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




More information about the Linux-nvme mailing list