[PATCH 12/15] nvme: add Clang context annotations for nvme_queue::cq_poll_lock

From: Nilay Shroff

Date: Wed Jun 10 2026 - 10:39:16 EST


nvme_queue::cqes, nvme_queue::cq_head, and nvme_queue::cq_phase are
protected by nvme_queue::cq_poll_lock. Annotate these fields with
__guarded_by(&cq_poll_lock) and annotate helpers accessing them with
__must_hold(&cq_poll_lock) so that Clang's context analysis can
validate the locking requirements.

IRQ-based queues do not use cq_poll_lock and instead rely on interrupt
serialization. Annotate nvme_irq() and nvme_irq_check() with
__context_unsafe() to suppress the corresponding context analysis
warnings.

After annotating the completion queue state and helper functions,
Clang's context analysis reports that nvme_poll() invokes
nvme_cqe_pending() without holding nvme_queue::cq_poll_lock:

drivers/nvme/host/pci.c:1683:7: warning: calling function 'nvme_cqe_pending' requires holding spinlock 'hctx->driver_data->cq_poll_lock'
exclusively [-Wthread-safety-analysis]
1683 | !nvme_cqe_pending(nvmeq))

Fix the warning by acquiring nvme_queue::cq_poll_lock before invoking
nvme_cqe_pending() in nvme_poll().

Signed-off-by: Nilay Shroff <nilay@xxxxxxxxxxxxx>
---
drivers/nvme/host/pci.c | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
index c464570cffd0..343860584b31 100644
--- a/drivers/nvme/host/pci.c
+++ b/drivers/nvme/host/pci.c
@@ -369,17 +369,17 @@ struct nvme_queue {
void *sq_cmds;
/* only used for poll queues: */
spinlock_t cq_poll_lock ____cacheline_aligned_in_smp;
- struct nvme_completion *cqes;
+ struct nvme_completion *cqes __guarded_by(&cq_poll_lock);
dma_addr_t sq_dma_addr;
dma_addr_t cq_dma_addr;
u32 __iomem *q_db;
u32 q_depth;
u16 cq_vector;
- u16 cq_head;
+ u16 cq_head __guarded_by(&cq_poll_lock);
u16 sq_tail __guarded_by(&sq_lock);
u16 last_sq_tail __guarded_by(&sq_lock);
u16 qid;
- u8 cq_phase;
+ u8 cq_phase __guarded_by(&cq_poll_lock);
u8 sqes;
unsigned long flags;
#define NVMEQ_ENABLED 0
@@ -1534,6 +1534,7 @@ static void nvme_pci_complete_batch(struct io_comp_batch *iob)

/* We read the CQE phase first to check if the rest of the entry is valid */
static inline bool nvme_cqe_pending(struct nvme_queue *nvmeq)
+ __must_hold(nvmeq->cq_poll_lock)
{
struct nvme_completion *hcqe = &nvmeq->cqes[nvmeq->cq_head];

@@ -1541,6 +1542,7 @@ static inline bool nvme_cqe_pending(struct nvme_queue *nvmeq)
}

static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
+ __must_hold(nvmeq->cq_poll_lock)
{
u16 head = nvmeq->cq_head;

@@ -1558,6 +1560,7 @@ static inline struct blk_mq_tags *nvme_queue_tagset(struct nvme_queue *nvmeq)

static inline void nvme_handle_cqe(struct nvme_queue *nvmeq,
struct io_comp_batch *iob, u16 idx)
+ __must_hold(nvmeq->cq_poll_lock)
{
struct nvme_completion *cqe = &nvmeq->cqes[idx];
__u16 command_id = READ_ONCE(cqe->command_id);
@@ -1595,6 +1598,7 @@ static inline void nvme_handle_cqe(struct nvme_queue *nvmeq,
}

static inline void nvme_update_cq_head(struct nvme_queue *nvmeq)
+ __must_hold(nvmeq->cq_poll_lock)
{
u32 tmp = nvmeq->cq_head + 1;

@@ -1608,6 +1612,7 @@ static inline void nvme_update_cq_head(struct nvme_queue *nvmeq)

static inline bool nvme_poll_cq(struct nvme_queue *nvmeq,
struct io_comp_batch *iob)
+ __must_hold(nvmeq->cq_poll_lock)
{
bool found = false;

@@ -1628,6 +1633,7 @@ static inline bool nvme_poll_cq(struct nvme_queue *nvmeq,
}

static irqreturn_t nvme_irq(int irq, void *data)
+ __context_unsafe(/* IRQ queues do not use cq_poll_lock */)
{
struct nvme_queue *nvmeq = data;
DEFINE_IO_COMP_BATCH(iob);
@@ -1641,6 +1647,7 @@ static irqreturn_t nvme_irq(int irq, void *data)
}

static irqreturn_t nvme_irq_check(int irq, void *data)
+ __context_unsafe(/* IRQ queues do not use cq_poll_lock */)
{
struct nvme_queue *nvmeq = data;

@@ -1673,11 +1680,14 @@ static int nvme_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
struct nvme_queue *nvmeq = hctx->driver_data;
bool found;

- if (!test_bit(NVMEQ_POLLED, &nvmeq->flags) ||
- !nvme_cqe_pending(nvmeq))
+ if (!test_bit(NVMEQ_POLLED, &nvmeq->flags))
return 0;

spin_lock(&nvmeq->cq_poll_lock);
+ if (!nvme_cqe_pending(nvmeq)) {
+ spin_unlock(&nvmeq->cq_poll_lock);
+ return 0;
+ }
found = nvme_poll_cq(nvmeq, iob);
spin_unlock(&nvmeq->cq_poll_lock);

@@ -2133,6 +2143,7 @@ static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
}

static int nvme_alloc_queue(struct nvme_dev *dev, int qid, int depth)
+ __context_unsafe(/* safe to allocate queue without any protection */)
{
struct nvme_queue *nvmeq = &dev->queues[qid];

--
2.53.0