Annotate the rqs[], cur_domain, batching and domain_ws[] members of struct kyber_hctx_data with __guarded_by(&lock) such that Clang's context analysis verifies that these members are only accessed with kyber_hctx_data.lock held. domain_ws[] is only accessed from kyber_get_domain_token() and that function is called with kyber_hctx_data.lock held. Initialize rqs[], cur_domain and batching inside a spinlock_init scoped guard, add a __must_hold() annotation to the debugfs rqs_next() callback since the lock is acquired in rqs_start() and released in rqs_stop(), and wrap the intentionally lockless reads in the debugfs show functions with data_race(READ_ONCE(...)). The lockless check in kyber_has_work() uses list_empty_careful(), which is already annotated as an intentional lockless access. Do not annotate kcqs, kcq_map[], domain_wait[] and wait_index[] since these are not protected by kyber_hctx_data.lock. Signed-off-by: Bart Van Assche --- block/kyber-iosched.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/block/kyber-iosched.c b/block/kyber-iosched.c index ec147e2cf923..5b664d4b9afe 100644 --- a/block/kyber-iosched.c +++ b/block/kyber-iosched.c @@ -173,13 +173,13 @@ struct kyber_queue_data { struct kyber_hctx_data { spinlock_t lock; - struct list_head rqs[KYBER_NUM_DOMAINS]; - unsigned int cur_domain; - unsigned int batching; + struct list_head rqs[KYBER_NUM_DOMAINS] __guarded_by(&lock); + unsigned int cur_domain __guarded_by(&lock); + unsigned int batching __guarded_by(&lock); struct kyber_ctx_queue *kcqs; struct sbitmap kcq_map[KYBER_NUM_DOMAINS]; struct sbq_wait domain_wait[KYBER_NUM_DOMAINS]; - struct sbq_wait_state *domain_ws[KYBER_NUM_DOMAINS]; + struct sbq_wait_state *domain_ws[KYBER_NUM_DOMAINS] __guarded_by(&lock); atomic_t wait_index[KYBER_NUM_DOMAINS]; }; @@ -483,10 +483,14 @@ static int kyber_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) } } - spin_lock_init(&khd->lock); + scoped_guard(spinlock_init, &khd->lock) { + for (i = 0; i < KYBER_NUM_DOMAINS; i++) + INIT_LIST_HEAD(&khd->rqs[i]); + khd->cur_domain = 0; + khd->batching = 0; + } for (i = 0; i < KYBER_NUM_DOMAINS; i++) { - INIT_LIST_HEAD(&khd->rqs[i]); khd->domain_wait[i].sbq = NULL; init_waitqueue_func_entry(&khd->domain_wait[i].wait, kyber_domain_wake); @@ -495,9 +499,6 @@ static int kyber_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) atomic_set(&khd->wait_index[i], 0); } - khd->cur_domain = 0; - khd->batching = 0; - hctx->sched_data = khd; return 0; @@ -911,6 +912,7 @@ static void *kyber_##name##_rqs_start(struct seq_file *m, loff_t *pos) \ \ static void *kyber_##name##_rqs_next(struct seq_file *m, void *v, \ loff_t *pos) \ + __must_hold(&KYBER_HCTX_DATA(HCTX_FROM_SEQ_FILE(m))->lock) \ { \ struct blk_mq_hw_ctx *hctx = m->private; \ struct kyber_hctx_data *khd = hctx->sched_data; \ @@ -953,8 +955,10 @@ static int kyber_cur_domain_show(void *data, struct seq_file *m) { struct blk_mq_hw_ctx *hctx = data; struct kyber_hctx_data *khd = hctx->sched_data; + /* Racy debugfs read. */ + unsigned int cur_domain = data_race(READ_ONCE(khd->cur_domain)); - seq_printf(m, "%s\n", kyber_domain_names[khd->cur_domain]); + seq_printf(m, "%s\n", kyber_domain_names[cur_domain]); return 0; } @@ -963,7 +967,8 @@ static int kyber_batching_show(void *data, struct seq_file *m) struct blk_mq_hw_ctx *hctx = data; struct kyber_hctx_data *khd = hctx->sched_data; - seq_printf(m, "%u\n", khd->batching); + /* Racy debugfs read. */ + seq_printf(m, "%u\n", data_race(READ_ONCE(khd->batching))); return 0; }