Annotate the run-time data members of struct deadline_data that are protected by deadline_data.lock with __guarded_by() such that Clang's context analysis can verify that these members are only accessed with the lock held. Skip the tunables since these are read and modified without holding the lock. Note that per_prio[].stats.completed is an atomic_t intentionally updated without dd->lock in dd_finish_request() via rq->elv.priv[0]. Initialize the protected members inside a spinlock_init guard. Wrap the intentional lockless access in dd_has_work() in context_unsafe(). The list_empty_careful() check on dd->dispatch does not need context_unsafe() because list_empty_careful() is already annotated, and deadline_batching_show() / deadline_starved_show() already use data_race(). Annotate deadline_##name##_fifo_next() and deadline_dispatch_next() with __must_hold() since the seq_file start callbacks acquire the lock and the stop callbacks release it. Signed-off-by: Bart Van Assche --- block/mq-deadline.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/block/mq-deadline.c b/block/mq-deadline.c index d8deba7ba18d..13e421d7dccf 100644 --- a/block/mq-deadline.c +++ b/block/mq-deadline.c @@ -63,6 +63,7 @@ struct io_stats_per_prio { uint32_t inserted; uint32_t merged; uint32_t dispatched; + /* Updated atomically without dd->lock in dd_finish_request(). */ atomic_t completed; }; @@ -83,13 +84,15 @@ struct deadline_data { * run time data */ - struct list_head dispatch; - struct dd_per_prio per_prio[DD_PRIO_COUNT]; + struct list_head dispatch __guarded_by(&lock); + struct dd_per_prio per_prio[DD_PRIO_COUNT] __guarded_by(&lock); /* Data direction of latest dispatched request. */ - enum dd_data_dir last_dir; - unsigned int batching; /* number of sequential requests made */ - unsigned int starved; /* times reads have starved writes */ + enum dd_data_dir last_dir __guarded_by(&lock); + /* number of sequential requests made */ + unsigned int batching __guarded_by(&lock); + /* times reads have starved writes */ + unsigned int starved __guarded_by(&lock); /* * settings that change how the i/o scheduler behaves @@ -549,23 +552,24 @@ static int dd_init_sched(struct request_queue *q, struct elevator_queue *eq) eq->elevator_data = dd; - INIT_LIST_HEAD(&dd->dispatch); - for (prio = 0; prio <= DD_PRIO_MAX; prio++) { - struct dd_per_prio *per_prio = &dd->per_prio[prio]; + scoped_guard(spinlock_init, &dd->lock) { + INIT_LIST_HEAD(&dd->dispatch); + for (prio = 0; prio <= DD_PRIO_MAX; prio++) { + struct dd_per_prio *per_prio = &dd->per_prio[prio]; - INIT_LIST_HEAD(&per_prio->fifo_list[DD_READ]); - INIT_LIST_HEAD(&per_prio->fifo_list[DD_WRITE]); - per_prio->sort_list[DD_READ] = RB_ROOT; - per_prio->sort_list[DD_WRITE] = RB_ROOT; + INIT_LIST_HEAD(&per_prio->fifo_list[DD_READ]); + INIT_LIST_HEAD(&per_prio->fifo_list[DD_WRITE]); + per_prio->sort_list[DD_READ] = RB_ROOT; + per_prio->sort_list[DD_WRITE] = RB_ROOT; + } + dd->last_dir = DD_WRITE; } dd->fifo_expire[DD_READ] = read_expire; dd->fifo_expire[DD_WRITE] = write_expire; dd->writes_starved = writes_starved; dd->front_merges = 1; - dd->last_dir = DD_WRITE; dd->fifo_batch = fifo_batch; dd->prio_aging_expire = prio_aging_expire; - spin_lock_init(&dd->lock); /* We dispatch from request queue wide instead of hw queue */ blk_queue_flag_set(QUEUE_FLAG_SQ_SCHED, q); @@ -739,7 +743,8 @@ static bool dd_has_work(struct blk_mq_hw_ctx *hctx) return true; for (prio = 0; prio <= DD_PRIO_MAX; prio++) - if (dd_has_work_for_prio(&dd->per_prio[prio])) + /* dd_has_work_for_prio() performs a lockless check. */ + if (dd_has_work_for_prio(context_unsafe(&dd->per_prio[prio]))) return true; return false; @@ -827,6 +832,7 @@ static void *deadline_##name##_fifo_start(struct seq_file *m, \ \ static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \ loff_t *pos) \ + __must_hold(&DD_DATA_FROM_RQ(RQ_FROM_SEQ_FILE(m))->lock) \ { \ struct request_queue *q = m->private; \ struct deadline_data *dd = q->elevator->elevator_data; \ @@ -954,6 +960,7 @@ static void *deadline_dispatch_start(struct seq_file *m, loff_t *pos) } static void *deadline_dispatch_next(struct seq_file *m, void *v, loff_t *pos) + __must_hold(&DD_DATA_FROM_RQ(RQ_FROM_SEQ_FILE(m))->lock) { struct request_queue *q = m->private; struct deadline_data *dd = q->elevator->elevator_data;