[PATCH v3] nvme-multipath: serialize ana_log_buf lifecycle with ana_lock
Nilay Shroff
nilay at linux.ibm.com
Tue Dec 1 04:54:55 PST 2026
On 12/1/26 1:19 PM, Guixin Liu wrote:
> nvme_parse_ana_log() reads ctrl->ana_log_buf and ctrl->ana_log_size
> under ctrl->ana_lock, but the buffer lifecycle is not fully serialized
> against it:
>
> - nvme_mpath_uninit() frees the buffer without ana_lock, so a
> concurrent parser in nvme_mpath_add_disk() can use freed memory.
> - nvme_mpath_init_identify() reallocates the buffer and updates
> ana_log_size outside ana_lock. nvme_mpath_add_disk() tests
> nvme_ctrl_use_ana() before taking the lock, so it can pass the check
> and then dereference a NULL buffer after nvme_mpath_uninit() cleared
> it; and the freshly allocated buffer can be parsed before
> nvme_read_ana_log() populates it, reading uninitialized memory.
> - The shrink path leaves the old log in place while shrinking
> ana_log_size, and anagrpmax is updated outside ana_lock, so a
> concurrent parser validates stale descriptors against the new
> limits and trips WARN_ON_ONCE().
> - After nvme_mpath_uninit() clears the buffer, nvme_mpath_add_disk()
> can still queue ana_work; nvme_read_ana_log() then calls
> nvme_get_log() with a NULL buffer and size 0, and
> nvme_bytes_to_numd(0) underflows to 0xffffffff.
>
> Take ana_lock around the buffer release in nvme_mpath_uninit() and
> around the reallocation in nvme_mpath_init_identify(), updating
> ana_log_buf, ana_log_size and anagrpmax together so a parser never
> observes a freed or NULL buffer or stale limits. Allocate the new
> buffer outside the lock and zero it, and clear ngrps on the shrink
> path, so a parser running before nvme_read_ana_log() populates the log
> sees ngrps == 0. nvme_parse_ana_log() and nvme_read_ana_log() bail out
> when the buffer is NULL. Keep the ana_work drain outside the lock:
> ana_work itself takes ana_lock, so cancelling it under the lock would
> deadlock.
>
Along with this change, can we also annotate relevant ANA fields
in struct nvme_ctrl using ctrl->ana_lock? That would help document
all fields protected with ana_lock as well as all references
to those fields would be also analyzed by thread-safety context
analyzer when compiled with clang.
Thanks,
--Nilay
More information about the Linux-nvme
mailing list