block: Add lock context annotations
Subsystems (6)
cover block: Add lock context annotations
Hi Jens,
Deriving from the block layer source code which synchronization object protects which member variable and which functions is nontrivial. This patch series documents this in such a way that the documentation can be verified by the Clang compiler. This patch series adds __must_hold(), __guarded_by() and related annotations to the block layer core, the blk-cgroup policies, the I/O schedulers, sed-opal, the partition code and the loop driver. With these annotations and with CONFIG_WARN_CONTEXT_ANALYSIS=y, Clang verifies at compile time that the annotated functions are only called and that the annotated variables and structure members are only accessed with the appropriate lock held. See also Documentation/dev-tools/context-analysis.rst.
This series is organized as follows:
- Patch 1 fixes a bug that was found while adding annotations:
deadline_##name##_next_rq_show() walks an RB-tree and prints a request without holding dd->lock. This patch does not depend on any other patch in this series.
- Patch 8 replaces an unreachable branch in bfq_exit_icq() with WARN_ON_ONCE().
- Patches 2-4, 9, 10, 12, 19, 22, 26, 31, 45 and 47 prepare for adding annotations. Most of these patches pass a pointer explicitly to a function instead of deriving it from another argument because Clang cannot determine that e.g. q->debugfs_mutex and hctx->queue->debugfs_mutex are the same mutex. No functional change is intended by any of these patches.
- Patch 50 enables context analysis for block/partitions/.
- The other patches add lock context annotations. Reads that happen on purpose without holding the lock that protects the data, e.g. in debugfs code, have been marked with data_race() or context_unsafe().
This series has been prepared against your for-next branch (commit 51d9c0564bb3) and has been compile-tested as follows on x86_64:
- Each patch with Clang 24, CONFIG_WARN_CONTEXT_ANALYSIS=y, CONFIG_WERROR=y and W=1 (block/ and drivers/block/loop.o).
- The entire series with gcc 15 and W=1 to verify that the kernel still builds without warnings if context analysis is not available.
- It has been verified that the blktests tests still pass.
Please consider this patch series for the next merge window.
Thanks,
Bart.
Bart Van Assche (52):
mq-deadline: Hold dd->lock in deadline_##name##_next_rq_show()
mq-deadline: Access per_prio state in dd_exit_sched() with dd->lock
held
mq-deadline: Compute &dd->per_prio[prio] with dd->lock held
mq-deadline: Mark the racy debugfs batching and starved reads
mq-deadline: Add lock context annotations
mq-deadline: Add __guarded_by() annotations
badblocks: Add lock context annotations for bb->lock block, bfq: Replace an unreachable branch in bfq_exit_icq() block, bfq: Fold _bfq_exit_icq() into bfq_exit_icq() block, bfq: Pass bfqd to functions that are called with bfqd->lock
held
block, bfq: Add lock context annotations
blk-cgroup: Pass q to blkg_create() and blkg_lookup_create()
blk-cgroup: Add lock context annotations
blk-cgroup: Annotate all_blkcgs with LIST_HEAD_GUARDED()
blk-crypto: Add lock context annotations
block: Add __must_hold() annotations for fq->mq_flush_lock
block: Annotate struct blk_flush_queue members with __guarded_by()
blk-iocost: Add lock context annotations
blk-iocost: Pass the ioc pointer to hweight_after_donation()
blk-iocost: Annotate struct ioc fields protected by ioc->lock
blk-iolatency: Add a __must_hold() annotation for lat_info->lock
blk-iolatency: Use WRITE_ONCE() in iolatency_clear_scaling()
blk-iolatency: Annotate child_latency_info with __guarded_by()
blk-mq: Add lock context annotations
blk-mq: Annotate blk_mq_ctx::rq_lists with __guarded_by()
blk-mq-debugfs: Pass q to blk_mq_debugfs_unregister_sched_hctx()
blk-mq-debugfs: Add __must_hold() annotations for q->debugfs_mutex
blk-mq-sched: Add lock context annotations
blk-zoned: Add lock context annotations
blk-zoned: Annotate struct blk_zone_wplug members with __guarded_by()
block: Pass the gendisk pointer explicitly to several bdev.c functions
block: Add __must_hold() annotations in block/bdev.c
block: Annotate the block_device_operations open and release callbacks
block: Add __must_hold() annotations for bdev_lock
block: Add lock context annotations in block/elevator.c
block: Annotate elv_list with LIST_HEAD_GUARDED()
block: Annotate the queue_limits_*() definitions in blk-settings.c
block: Add __must_hold() annotations for the IA ranges sysfs functions
block: Add a __must_hold() annotation for blk_holder_mutex
block: Add a __must_hold() annotation for bio_slab_lock
block: Annotate bio_slabs and slab_ref with __guarded_by()
sed-opal: Add __must_hold() annotations for dev->dev_lock
block: Annotate the disk_events list with LIST_HEAD_GUARDED()
block: Annotate struct disk_events members with __guarded_by()
block: Simplify the major_names[] traversal in blk_probe_dev()
block: Annotate major_names with __guarded_by()
loop: Pass disk instead of lo to __loop_clr_fd()
loop: Annotate lo_release() and __loop_clr_fd() with __must_hold()
block: partitions: Add __must_hold() annotations
block: partitions: Enable lock context analysis
kyber: Add __must_hold() annotations for khd->lock
kyber: Add __guarded_by() annotations to struct kyber_hctx_data
block/badblocks.c | 16 +++++++ block/bdev.c | 87 +++++++++++++++++++++++-------------- block/bfq-cgroup.c | 23 +++++++--- block/bfq-iosched.c | 73 +++++++++++++------------------ block/bfq-iosched.h | 17 +++++--- block/bio.c | 9 ++-- block/blk-cgroup.c | 32 ++++++++------ block/blk-crypto-fallback.c | 1 + block/blk-crypto-profile.c | 2 + block/blk-flush.c | 13 +++--- block/blk-ia-ranges.c | 2 + block/blk-iocost.c | 68 +++++++++++++++++++---------- block/blk-iolatency.c | 20 ++++++--- block/blk-mq-debugfs.c | 25 ++++++++--- block/blk-mq-debugfs.h | 40 ++++++++++++----- block/blk-mq-sched.c | 6 ++- block/blk-mq-sched.h | 12 +++-- block/blk-mq.c | 22 ++++++++-- block/blk-mq.h | 5 ++- block/blk-settings.c | 3 ++ block/blk-zoned.c | 62 ++++++++++++++++++-------- block/blk.h | 24 ++++++---- block/disk-events.c | 36 ++++++++++----- block/elevator.c | 5 ++- block/genhd.c | 15 ++++--- block/holder.c | 1 + block/kyber-iosched.c | 30 ++++++++----- block/mq-deadline.c | 87 ++++++++++++++++++++++++------------- block/partitions/Makefile | 2 + block/partitions/core.c | 8 ++-- block/sed-opal.c | 17 +++++++- drivers/block/loop.c | 17 +++++--- include/linux/blkdev.h | 9 ++-- 33 files changed, 523 insertions(+), 266 deletions(-)
PATCH 1/52 mq-deadline: Hold dd->lock in deadline_##name##_next_rq_show()
deadline_##name##_next_rq_show() walks per_prio->sort_list[data_dir] via deadline_from_pos() and prints the request that it finds without holding dd->lock. Concurrent request insertion and dispatching modify that RB-tree, so the traversal may follow stale pointers. Additionally, the request may be dispatched, completed and reused for another I/O while __blk_mq_debugfs_rq_show() prints it. Fix this by holding dd->lock while looking up and printing the request, as the other mq-deadline debugfs functions that iterate over requests already do.
--- block/mq-deadline.c | 2 ++ 1 file changed, 2 insertions(+)
block/mq-deadline.c +2 -0
@@ -843,10 +843,12 @@ static int deadline_##name##_next_rq_show(void *data, \struct dd_per_prio *per_prio = &dd->per_prio[prio]; \struct request *rq; \ \ +spin_lock (&dd->lock); \ rq =deadline_from_pos (per_prio, data_dir, \ per_prio->latest_pos[data_dir]); \if (rq) \__blk_mq_debugfs_rq_show (m, rq); \ +spin_unlock (&dd->lock); \return 0 ; \ }
PATCH 2/52 mq-deadline: Access per_prio state in dd_exit_sched() with dd->lock held
In reply to Bart Van Asschedd_exit_sched() inspects per_prio->fifo_list[] and reads per_prio->stats without holding dd->lock and only holds dd->lock around dd_queued(). Read all per_prio state with dd->lock held and report it after dd->lock has been released. This change prepares for annotating deadline_data::per_prio[] with __guarded_by(&lock).
No functional change is intended since dd_exit_sched() is called when no other users of the scheduler data remain.
--- block/mq-deadline.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
block/mq-deadline.c +14 -8
@@ -500,21 +500,27 @@ static void dd_exit_sched(struct elevator_queue *e)enum dd_prio prio;for (prio =0 ; prio <=DD_PRIO_MAX ; prio++) { -struct dd_per_prio *per_prio = &dd->per_prio[prio]; -const struct io_stats_per_prio *stats = &per_prio->stats; -uint32_t queued; - -WARN_ON_ONCE (!list_empty (&per_prio->fifo_list[DD_READ ])); -WARN_ON_ONCE (!list_empty (&per_prio->fifo_list[DD_WRITE ])); +struct dd_per_prio *per_prio; +bool read_empty, write_empty; +u32 inserted, merged, dispatched, queued; +int completed;spin_lock (&dd->lock); + per_prio = &dd->per_prio[prio]; + read_empty =list_empty (&per_prio->fifo_list[DD_READ ]); + write_empty =list_empty (&per_prio->fifo_list[DD_WRITE ]); queued =dd_queued (dd, prio); + inserted = per_prio->stats.inserted; + merged = per_prio->stats.merged; + dispatched = per_prio->stats.dispatched; + completed =atomic_read (&per_prio->stats.completed);spin_unlock (&dd->lock); +WARN_ON_ONCE (!read_empty); +WARN_ON_ONCE (!write_empty);WARN_ONCE (queued !=0 ,"statistics for priority %d: i %u m %u d %u c %u\n" , - prio, stats->inserted, stats->merged, - stats->dispatched,atomic_read (&stats->completed)); + prio, inserted, merged, dispatched, completed); }kfree (dd);
PATCH 3/52 mq-deadline: Compute &dd->per_prio[prio] with dd->lock held
In reply to Bart Van AsscheCompute &dd->per_prio[prio] after dd->lock has been acquired in deadline_##name##_fifo_start() and deadline_##name##_next_rq_show(). This change prepares for annotating deadline_data::per_prio[] with __guarded_by(&lock) such that Clang's context analysis sees all per_prio accesses happen with dd->lock held.
No functional change is intended since this change only affects when an address is computed.
--- block/mq-deadline.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
block/mq-deadline.c +4 -2
@@ -809,9 +809,10 @@ static void *deadline_##name##_fifo_start(struct seq_file *m, \ { \struct request_queue *q = m->private; \struct deadline_data *dd = q->elevator->elevator_data; \ -struct dd_per_prio *per_prio = &dd->per_prio[prio]; \ +struct dd_per_prio *per_prio; \ \spin_lock (&dd->lock); \ + per_prio = &dd->per_prio[prio]; \return seq_list_start (&per_prio->fifo_list[data_dir], *pos); \ } \ \
@@ -846,10 +847,11 @@ static int deadline_##name##_next_rq_show(void *data, \ { \struct request_queue *q = data; \struct deadline_data *dd = q->elevator->elevator_data; \ -struct dd_per_prio *per_prio = &dd->per_prio[prio]; \ +struct dd_per_prio *per_prio; \struct request *rq; \ \spin_lock (&dd->lock); \ + per_prio = &dd->per_prio[prio]; \ rq =deadline_from_pos (per_prio, data_dir, \ per_prio->latest_pos[data_dir]); \if (rq) \
PATCH 4/52 mq-deadline: Mark the racy debugfs batching and starved reads
In reply to Bart Van Asschedeadline_batching_show() and deadline_starved_show() read dd->batching and dd->starved without holding dd->lock. Wrap these reads with data_race(READ_ONCE(...)) such that both KCSAN and Clang's context analysis recognize these as intentional lockless reads. This change prepares for annotating these members with __guarded_by(&lock).
--- block/mq-deadline.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
block/mq-deadline.c +4 -2
@@ -873,7 +873,8 @@ static int deadline_batching_show(void *data, struct seq_file *m)struct request_queue *q = data;struct deadline_data *dd = q->elevator->elevator_data; -seq_printf (m,"%u\n" , dd->batching); +/* Racy debugfs read. */ +seq_printf (m,"%u\n" ,data_race (READ_ONCE (dd->batching)));return 0 ; }
@@ -882,7 +883,8 @@ static int deadline_starved_show(void *data, struct seq_file *m)struct request_queue *q = data;struct deadline_data *dd = q->elevator->elevator_data; -seq_printf (m,"%u\n" , dd->starved); +/* Racy debugfs read. */ +seq_printf (m,"%u\n" ,data_race (READ_ONCE (dd->starved)));return 0 ; }
PATCH 5/52 mq-deadline: Add lock context annotations
In reply to Bart Van AsscheAnnotate the mq-deadline functions that are called with dd->lock held with __must_hold():
- dd_queued(), dd_start_request(), __dd_dispatch_request(), dd_dispatch_prio_aged_requests() and dd_owned_by_driver() require dd->lock.
- dd_request_merge(), dd_request_merged() and dd_merged_requests() are elevator callbacks that are called with the deadline_data lock held. Express that lock in terms of the request queue argument.
- dd_insert_request() requires the deadline_data lock of the request queue associated with its hctx argument.
Move the DD_DATA_FROM_RQ() macro definition up such that it can be used in the above annotations. The macro definition itself is unchanged.
--- block/mq-deadline.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
block/mq-deadline.c +12 -2
@@ -103,6 +103,9 @@ struct deadline_data {spinlock_t lock; }; +#define DD_DATA_FROM_RQ (rq) \ + ((struct deadline_data *)(rq)->elevator->elevator_data) +/* Maps an I/O priority class to a deadline scheduler priority. */ static const enum dd_prio ioprio_class_to_prio[] = { [IOPRIO_CLASS_NONE ] =DD_BE_PRIO ,
@@ -183,6 +186,7 @@ static void deadline_remove_request(struct request_queue *q,static void dd_request_merged (struct request_queue *q,struct request *req,enum elv_merge type) + __must_hold(&DD_DATA_FROM_RQ(q)->lock) {struct deadline_data *dd = q->elevator->elevator_data;const u8 ioprio_class =dd_rq_ioclass (req);
@@ -203,6 +207,7 @@ static void dd_request_merged(struct request_queue *q, struct request *req,*/ static void dd_merged_requests (struct request_queue *q,struct request *req,struct request *next) + __must_hold(&DD_DATA_FROM_RQ(q)->lock) {struct deadline_data *dd = q->elevator->elevator_data;const u8 ioprio_class =dd_rq_ioclass (next);
@@ -244,6 +249,7 @@ static void deadline_move_request(struct dd_per_prio *per_prio,/* Number of requests queued for a given priority level. */ static u32 dd_queued (struct deadline_data *dd,enum dd_prio prio) + __must_hold(&dd->lock) {const struct io_stats_per_prio *stats = &dd->per_prio[prio].stats;
@@ -305,6 +311,7 @@ static bool started_after(struct deadline_data *dd, struct request *rq,static struct request *dd_start_request (struct deadline_data *dd,enum dd_data_dir data_dir,struct request *rq) + __must_hold(&dd->lock) {u8 ioprio_class =dd_rq_ioclass (rq);enum dd_prio prio = ioprio_class_to_prio[ioprio_class];
@@ -322,6 +329,7 @@ static struct request *dd_start_request(struct deadline_data *dd,static struct request *__dd_dispatch_request (struct deadline_data *dd,struct dd_per_prio *per_prio,unsigned long latest_start) + __must_hold(&dd->lock) {struct request *rq, *next_rq;enum dd_data_dir data_dir;
@@ -416,6 +424,7 @@ static struct request *__dd_dispatch_request(struct deadline_data *dd,*/ static struct request *dd_dispatch_prio_aged_requests (struct deadline_data *dd,unsigned long now) + __must_hold(&dd->lock) {struct request *rq;enum dd_prio prio;
@@ -573,6 +582,7 @@ static int dd_init_sched(struct request_queue *q, struct elevator_queue *eq)*/ static int dd_request_merge (struct request_queue *q,struct request **rq,struct bio *bio) + __must_hold(&DD_DATA_FROM_RQ(q)->lock) {struct deadline_data *dd = q->elevator->elevator_data;const u8 ioprio_class =IOPRIO_PRIO_CLASS (bio->bi_ioprio);
@@ -625,6 +635,7 @@ static bool dd_bio_merge(struct request_queue *q, struct bio *bio,*/ static void dd_insert_request (struct blk_mq_hw_ctx *hctx,struct request *rq,blk_insert_t flags,struct list_head *free) + __must_hold(&DD_DATA_FROM_RQ(hctx->queue)->lock) {struct request_queue *q = hctx->queue;struct deadline_data *dd = q->elevator->elevator_data;
@@ -798,8 +809,6 @@ static const struct elv_fs_entry deadline_attrs[] = { };#define RQ_FROM_SEQ_FILE (m) ((struct request_queue *)(m)->private) -#define DD_DATA_FROM_RQ (rq) \ - ((struct deadline_data *)(rq)->elevator->elevator_data)#ifdef CONFIG_BLK_DEBUG_FS #define DEADLINE_DEBUGFS_DDIR_ATTRS (prio, data_dir, name) \
@@ -907,6 +916,7 @@ static int dd_queued_show(void *data, struct seq_file *m)/* Number of requests owned by the block driver for a given priority. */ static u32 dd_owned_by_driver (struct deadline_data *dd,enum dd_prio prio) + __must_hold(&dd->lock) {const struct io_stats_per_prio *stats = &dd->per_prio[prio].stats;
PATCH 6/52 mq-deadline: Add __guarded_by() annotations
In reply to Bart Van AsscheAnnotate 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.
--- block/mq-deadline.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-)
block/mq-deadline.c +22 -15
@@ -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;
PATCH 7/52 badblocks: Add lock context annotations for bb->lock
In reply to Bart Van AsscheThe badblocks helper functions that modify the bad blocks state are only called from inside write_seqlock_irq*(&bb->lock) / write_sequnlock_irq*(&bb->lock) sections. Annotate these functions with __must_hold(&bb->lock):
- front_merge()
- front_combine()
- front_overwrite()
- insert_at()
- badblocks_update_acked()
- try_adjacent_combine()
- front_clear()
- front_splitting_clear()
The helper functions that only read the bad blocks table are called either from inside a write_seqlock_irq*() section or by _badblocks_check() from inside a read_seqbegin() / read_seqretry() section. Clang's context analysis treats the latter as holding bb->lock shared. Hence, annotate these functions with __must_hold_shared(&bb->lock):
- prev_by_hint()
- prev_badblocks()
- can_merge_front()
- can_combine_front()
- overlap_front()
- overlap_behind()
- can_front_overwrite()
- _badblocks_check()
--- block/badblocks.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
block/badblocks.c +16 -0
@@ -455,6 +455,7 @@* table. */ static int prev_by_hint (struct badblocks *bb,sector_t s,int hint) + __must_hold_shared(&bb->lock) {int hint_end = hint +2 ;u64 *p = bb->page;
@@ -480,6 +481,7 @@ static int prev_by_hint(struct badblocks *bb, sector_t s, int hint)*/ static int prev_badblocks (struct badblocks *bb,struct badblocks_context *bad,int hint) + __must_hold_shared(&bb->lock) {sector_t s = bad->start;int ret =-1 ;
@@ -533,6 +535,7 @@ static int prev_badblocks(struct badblocks *bb, struct badblocks_context *bad,*/ static bool can_merge_front (struct badblocks *bb,int prev,struct badblocks_context *bad) + __must_hold_shared(&bb->lock) {sector_t s = bad->start;u64 *p = bb->page;
@@ -550,6 +553,7 @@ static bool can_merge_front(struct badblocks *bb, int prev,* merged from bad->len. */ static int front_merge (struct badblocks *bb,int prev,struct badblocks_context *bad) + __must_hold(&bb->lock) {sector_t sectors = bad->len;sector_t s = bad->start;
@@ -587,6 +591,7 @@ static int front_merge(struct badblocks *bb, int prev, struct badblocks_context*/ static bool can_combine_front (struct badblocks *bb,int prev,struct badblocks_context *bad) + __must_hold_shared(&bb->lock) {u64 *p = bb->page;
@@ -607,6 +612,7 @@ static bool can_combine_front(struct badblocks *bb, int prev,* it is unnecessary to clear p[perv] after front merge. */ static void front_combine (struct badblocks *bb,int prev) + __must_hold(&bb->lock) {u64 *p = bb->page;
@@ -625,6 +631,7 @@ static void front_combine(struct badblocks *bb, int prev)*/ static bool overlap_front (struct badblocks *bb,int front,struct badblocks_context *bad) + __must_hold_shared(&bb->lock) {u64 *p = bb->page;
@@ -640,6 +647,7 @@ static bool overlap_front(struct badblocks *bb, int front,*/ static bool overlap_behind (struct badblocks *bb,struct badblocks_context *bad,int behind) + __must_hold_shared(&bb->lock) {u64 *p = bb->page;
@@ -672,6 +680,7 @@ static bool overlap_behind(struct badblocks *bb, struct badblocks_context *bad,*/ static bool can_front_overwrite (struct badblocks *bb,int prev,struct badblocks_context *bad,int *extra) + __must_hold_shared(&bb->lock) {u64 *p = bb->page;int len;
@@ -715,6 +724,7 @@ static bool can_front_overwrite(struct badblocks *bb, int prev,*/ static int front_overwrite (struct badblocks *bb,int prev,struct badblocks_context *bad,int extra) + __must_hold(&bb->lock) {u64 *p = bb->page;sector_t orig_end =BB_END (p[prev]);
@@ -776,6 +786,7 @@ static int front_overwrite(struct badblocks *bb, int prev,* the location is indexed by 'at'. */ static int insert_at (struct badblocks *bb,int at,struct badblocks_context *bad) + __must_hold(&bb->lock) {u64 *p = bb->page;int len;
@@ -791,6 +802,7 @@ static int insert_at(struct badblocks *bb, int at, struct badblocks_context *bad }static void badblocks_update_acked (struct badblocks *bb) + __must_hold(&bb->lock) {bool unacked = false;u64 *p = bb->page;
@@ -815,6 +827,7 @@ static void badblocks_update_acked(struct badblocks *bb)* overlapped with the bad range (from bad table) indexed by 'behind'. */ static bool try_adjacent_combine (struct badblocks *bb,int prev) + __must_hold(&bb->lock) {u64 *p = bb->page;
@@ -987,6 +1000,7 @@ static bool _badblocks_set(struct badblocks *bb, sector_t s, sector_t sectors,*/ static int front_clear (struct badblocks *bb,int prev,struct badblocks_context *bad,int *deleted) + __must_hold(&bb->lock) {sector_t sectors = bad->len;sector_t s = bad->start;
@@ -1030,6 +1044,7 @@ static int front_clear(struct badblocks *bb, int prev,*/ static int front_splitting_clear (struct badblocks *bb,int prev,struct badblocks_context *bad) + __must_hold(&bb->lock) {u64 *p = bb->page;u64 end =BB_END (p[prev]);
@@ -1185,6 +1200,7 @@ static bool _badblocks_clear(struct badblocks *bb, sector_t s, sector_t sectors)/* Do the exact work to check bad blocks range from the bad block table */ static int _badblocks_check (struct badblocks *bb,sector_t s,sector_t sectors,sector_t *first_bad,sector_t *bad_sectors) + __must_hold_shared(&bb->lock) {int prev =-1 , hint =-1 , set =0 ;struct badblocks_context bad;
PATCH 8/52 block, bfq: Replace an unreachable branch in bfq_exit_icq()
In reply to Bart Van AsscheIf bic_to_bfqd() returns NULL, bfq_exit_icq() calls _bfq_exit_icq() without holding bfqd->lock. _bfq_exit_icq() calls bfq_put_stable_ref() and bfq_exit_icq_bfqq(), both of which require bfqd->lock to be held and access bfqq->bfqd.
bic_to_bfqd() cannot return NULL in practice: ioc_exit_icq() dereferences q->elevator before calling .exit_icq(), elevator_exit() calls ioc_clear_queue() before blk_mq_exit_sched(), and bfq_exit_queue() never clears elevator_data. Replace the unreachable else branch and its stale comment with WARN_ON_ONCE(!bfqd).
This change prepares for folding _bfq_exit_icq() into bfq_exit_icq() and annotating bfq_exit_icq_bfqq() with __must_hold(&bfqd->lock).
--- block/bfq-iosched.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-)
block/bfq-iosched.c +6 -16
@@ -5481,22 +5481,12 @@ static void bfq_exit_icq(struct io_cq *icq)struct bfq_data *bfqd =bic_to_bfqd (bic);unsigned long flags; -/* -* If bfqd and thus bfqd->num_actuators is not available any -* longer, then cycle over all possible per-actuator bfqqs in -* next loop. We rely on bic being zeroed on creation, and -* therefore on its unused per-actuator fields being NULL. -* -* bfqd is NULL if scheduler already exited, and in that case -* this is the last time these queues are accessed. -*/ -if (bfqd) { -spin_lock_irqsave (&bfqd->lock, flags); -_bfq_exit_icq (bic, bfqd->num_actuators); -spin_unlock_irqrestore (&bfqd->lock, flags); - }else { -_bfq_exit_icq (bic,BFQ_MAX_ACTUATORS ); - } +if (WARN_ON_ONCE (!bfqd)) +return ; + +spin_lock_irqsave (&bfqd->lock, flags); +_bfq_exit_icq (bic, bfqd->num_actuators); +spin_unlock_irqrestore (&bfqd->lock, flags); }/*
PATCH 9/52 block, bfq: Fold _bfq_exit_icq() into bfq_exit_icq()
In reply to Bart Van Assche_bfq_exit_icq() was originally split out from bfq_exit_icq() to silence a sparse warning caused by conditionally acquiring bfqd->lock. Now that bfq_exit_icq() unconditionally acquires bfqd->lock and is the sole caller of _bfq_exit_icq(), fold _bfq_exit_icq() back into bfq_exit_icq() and drop the redundant num_actuators helper parameter.
No functional change.
--- block/bfq-iosched.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-)
block/bfq-iosched.c +9 -15
@@ -5461,31 +5461,25 @@ static void bfq_exit_icq_bfqq(struct bfq_io_cq *bic, bool is_sync, } } -static void _bfq_exit_icq (struct bfq_io_cq *bic,unsigned int num_actuators) -{ -struct bfq_iocq_bfqq_data *bfqq_data = bic->bfqq_data; -unsigned int act_idx; - -for (act_idx =0 ; act_idx < num_actuators; act_idx++) { -if (bfqq_data[act_idx].stable_merge_bfqq) -bfq_put_stable_ref (bfqq_data[act_idx].stable_merge_bfqq); - -bfq_exit_icq_bfqq (bic, true, act_idx); -bfq_exit_icq_bfqq (bic, false, act_idx); - } -} -static void bfq_exit_icq (struct io_cq *icq) {struct bfq_io_cq *bic =icq_to_bic (icq);struct bfq_data *bfqd =bic_to_bfqd (bic); +struct bfq_iocq_bfqq_data *bfqq_data = bic->bfqq_data;unsigned long flags; +unsigned int act_idx;if (WARN_ON_ONCE (!bfqd))return ;spin_lock_irqsave (&bfqd->lock, flags); -_bfq_exit_icq (bic, bfqd->num_actuators); +for (act_idx =0 ; act_idx < bfqd->num_actuators; act_idx++) { +if (bfqq_data[act_idx].stable_merge_bfqq) +bfq_put_stable_ref (bfqq_data[act_idx].stable_merge_bfqq); + +bfq_exit_icq_bfqq (bic, true, act_idx); +bfq_exit_icq_bfqq (bic, false, act_idx); + }spin_unlock_irqrestore (&bfqd->lock, flags); }
PATCH 10/52 block, bfq: Pass bfqd to functions that are called with bfqd->lock held
In reply to Bart Van AsschePass the bfqd pointer explicitly to bfq_bic_update_cgroup(), bfq_init_rq() and bfq_exit_icq_bfqq() instead of deriving it from the bic, the request or the bfqq. All callers of these functions hold bfqd->lock. This change prepares for annotating these functions with __must_hold(&bfqd->lock). Clang's context analysis cannot determine that e.g. bic_to_bfqd(bic)->lock and q->elevator->elevator_data->lock refer to the same spinlock.
Remove the bfqd == NULL check from bfq_bic_update_cgroup() since it is dead code: both callers dereference bfqd by locking bfqd->lock before calling this function. Remove the bfqd == NULL check from bfq_exit_icq_bfqq() since bfqq->bfqd is never NULL. Make bic_to_bfqd() static since it no longer has any callers outside block/bfq-iosched.c.
No functional change is intended since bic_to_bfqd(bic), rq->q's elevator data and bfqq->bfqd all equal the bfqd pointer passed by the callers.
--- block/bfq-cgroup.c | 11 +++++++---- block/bfq-iosched.c | 28 +++++++++++----------------- block/bfq-iosched.h | 4 ++-- 3 files changed, 20 insertions(+), 23 deletions(-)
block/bfq-cgroup.c +7 -4
@@ -779,9 +779,9 @@ static void __bfq_bic_change_cgroup(struct bfq_data *bfqd, } } -void bfq_bic_update_cgroup (struct bfq_io_cq *bic,struct bio *bio) +void bfq_bic_update_cgroup (struct bfq_data *bfqd,struct bfq_io_cq *bic, +struct bio *bio) { -struct bfq_data *bfqd =bic_to_bfqd (bic);struct bfq_group *bfqg =bfq_bio_bfqg (bfqd, bio);uint64_t serial_nr;
@@ -791,7 +791,7 @@ void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio)* Check whether blkcg has changed. The condition may trigger * spuriously on a newly created cic but there's no harm. */ -if (unlikely (!bfqd) ||likely (bic->blkcg_serial_nr == serial_nr)) +if (likely (bic->blkcg_serial_nr == serial_nr))return ;/*
@@ -1430,7 +1430,10 @@ void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg) entity->sched_data = &bfqg->sched_data; } -void bfq_bic_update_cgroup (struct bfq_io_cq *bic,struct bio *bio) {} +void bfq_bic_update_cgroup (struct bfq_data *bfqd,struct bfq_io_cq *bic, +struct bio *bio) +{ +}void bfq_end_wr_async (struct bfq_data *bfqd) {
block/bfq-iosched.c +11 -17
@@ -433,7 +433,7 @@ void bic_set_bfqq(struct bfq_io_cq *bic, } } -struct bfq_data *bic_to_bfqd (struct bfq_io_cq *bic) +static struct bfq_data *bic_to_bfqd (struct bfq_io_cq *bic) {return bic->icq.q->elevator->elevator_data; }
@@ -2459,7 +2459,7 @@ static bool bfq_bio_merge(struct request_queue *q, struct bio *bio,* Make sure cgroup info is uptodate for current process before * considering the merge. */ -bfq_bic_update_cgroup (bic, bio); +bfq_bic_update_cgroup (bfqd, bic, bio); bfqd->bio_bfqq =bic_to_bfqq (bic,op_is_sync (bio->bi_opf),bfq_actuator_index (bfqd, bio));
@@ -5446,16 +5446,12 @@ static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)bfq_release_process_ref (bfqd, bfqq); } -static void bfq_exit_icq_bfqq (struct bfq_io_cq *bic,bool is_sync, -unsigned int actuator_idx) +static void bfq_exit_icq_bfqq (struct bfq_data *bfqd,struct bfq_io_cq *bic, +bool is_sync,unsigned int actuator_idx) {struct bfq_queue *bfqq =bic_to_bfqq (bic, is_sync, actuator_idx); -struct bfq_data *bfqd; - -if (bfqq) - bfqd = bfqq->bfqd;/* NULL if scheduler already exited */ -if (bfqq && bfqd) { +if (bfqq) {bic_set_bfqq (bic,NULL , is_sync, actuator_idx);bfq_exit_bfqq (bfqd, bfqq); }
@@ -5477,8 +5473,8 @@ static void bfq_exit_icq(struct io_cq *icq)if (bfqq_data[act_idx].stable_merge_bfqq)bfq_put_stable_ref (bfqq_data[act_idx].stable_merge_bfqq); -bfq_exit_icq_bfqq (bic, true, act_idx); -bfq_exit_icq_bfqq (bic, false, act_idx); +bfq_exit_icq_bfqq (bfqd, bic, true, act_idx); +bfq_exit_icq_bfqq (bfqd, bic, false, act_idx); }spin_unlock_irqrestore (&bfqd->lock, flags); }
@@ -6216,7 +6212,7 @@ static inline void bfq_update_insert_stats(struct request_queue *q, blk_opf_t cmd_flags) {}#endif /* CONFIG_BFQ_CGROUP_DEBUG */ -static struct bfq_queue *bfq_init_rq (struct request *rq); +static struct bfq_queue *bfq_init_rq (struct bfq_data *bfqd,struct request *rq);static void bfq_insert_request (struct blk_mq_hw_ctx *hctx,struct request *rq,blk_insert_t flags)
@@ -6233,7 +6229,7 @@ static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,bfqg_stats_update_legacy_io (q, rq);#endif spin_lock_irq (&bfqd->lock); - bfqq =bfq_init_rq (rq); + bfqq =bfq_init_rq (bfqd, rq);if (blk_mq_sched_try_insert_merge (q, rq, &free)) {spin_unlock_irq (&bfqd->lock);blk_mq_free_requests (&free);
@@ -6909,11 +6905,9 @@ static struct bfq_queue *bfq_get_bfqq_handle_split(struct bfq_data *bfqd,* rq after rq has been inserted or merged. So, it is safe to execute * these preparation operations when rq is finally inserted or merged. */ -static struct bfq_queue *bfq_init_rq (struct request *rq) +static struct bfq_queue *bfq_init_rq (struct bfq_data *bfqd,struct request *rq) { -struct request_queue *q = rq->q;struct bio *bio = rq->bio; -struct bfq_data *bfqd = q->elevator->elevator_data;struct bfq_io_cq *bic;const int is_sync =rq_is_sync (rq);struct bfq_queue *bfqq;
@@ -6934,7 +6928,7 @@ static struct bfq_queue *bfq_init_rq(struct request *rq) bic =icq_to_bic (rq->elv.icq);bfq_check_ioprio_change (bic, bio); -bfq_bic_update_cgroup (bic, bio); +bfq_bic_update_cgroup (bfqd, bic, bio); bfqq =bfq_get_bfqq_handle_split (bfqd, bic, bio, a_idx, is_sync);bfqq_request_allocated (bfqq);
block/bfq-iosched.h +2 -2
@@ -1046,7 +1046,6 @@ struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync, unsigned int actuator_idx);void bic_set_bfqq (struct bfq_io_cq *bic,struct bfq_queue *bfqq,bool is_sync,unsigned int actuator_idx); -struct bfq_data *bic_to_bfqd (struct bfq_io_cq *bic);void bfq_pos_tree_add_move (struct bfq_data *bfqd,struct bfq_queue *bfqq);void bfq_weights_tree_add (struct bfq_queue *bfqq);void bfq_weights_tree_remove (struct bfq_queue *bfqq);
@@ -1082,7 +1081,8 @@ void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg);#endif void bfq_init_entity (struct bfq_entity *entity,struct bfq_group *bfqg); -void bfq_bic_update_cgroup (struct bfq_io_cq *bic,struct bio *bio); +void bfq_bic_update_cgroup (struct bfq_data *bfqd,struct bfq_io_cq *bic, +struct bio *bio);void bfq_end_wr_async (struct bfq_data *bfqd);struct bfq_group *bfq_bio_bfqg (struct bfq_data *bfqd,struct bio *bio);struct blkcg_gq *bfqg_to_blkg (struct bfq_group *bfqg);
PATCH 11/52 block, bfq: Add lock context annotations
In reply to Bart Van AsscheAnnotate the BFQ functions that are called with bfqd->lock held with __must_hold(&bfqd->lock):
- bfq_schedule_dispatch(), bfq_put_async_queues(), bfq_end_wr_async_queues(), bfq_bfqq_move() and bfq_bic_update_cgroup(). Annotate the declarations in bfq-iosched.h, the definitions and the !CONFIG_BFQ_GROUP_IOSCHED stubs where present.
- bfq_exit_bfqq(), bfq_exit_icq_bfqq(), bfq_init_rq(), bfq_completed_request() and __bfq_put_async_bfqq().
- bfq_sync_bfqq_move(), __bfq_bic_change_cgroup(), bfq_reparent_leaf_entity() and bfq_reparent_active_queues().
Many other BFQ functions are not annotated because the BFQ code obtains the bfqd->lock pointer via several different paths and because Clang cannot determine that these paths refer to the same spinlock.
--- block/bfq-cgroup.c | 12 +++++++++++- block/bfq-iosched.c | 11 ++++++++++- block/bfq-iosched.h | 15 ++++++++++----- 3 files changed, 31 insertions(+), 7 deletions(-)
block/bfq-cgroup.c +11 -1
@@ -642,6 +642,7 @@ struct bfq_group *bfq_bio_bfqg(struct bfq_data *bfqd, struct bio *bio)*/ void bfq_bfqq_move (struct bfq_data *bfqd,struct bfq_queue *bfqq,struct bfq_group *bfqg) + __must_hold(&bfqd->lock) {struct bfq_entity *entity = &bfqq->entity;struct bfq_group *old_parent =bfqq_group (bfqq);
@@ -713,6 +714,7 @@ static void bfq_sync_bfqq_move(struct bfq_data *bfqd,struct bfq_io_cq *bic, struct bfq_group *bfqg, unsigned int act_idx) + __must_hold(&bfqd->lock) { struct bfq_queue *bfqq;
@@ -761,6 +763,7 @@ static void bfq_sync_bfqq_move(struct bfq_data *bfqd,static void __bfq_bic_change_cgroup (struct bfq_data *bfqd,struct bfq_io_cq *bic,struct bfq_group *bfqg) + __must_hold(&bfqd->lock) {unsigned int act_idx;
@@ -781,6 +784,7 @@ static void __bfq_bic_change_cgroup(struct bfq_data *bfqd,void bfq_bic_update_cgroup (struct bfq_data *bfqd,struct bfq_io_cq *bic,struct bio *bio) + __must_hold(&bfqd->lock) {struct bfq_group *bfqg =bfq_bio_bfqg (bfqd, bio);uint64_t serial_nr;
@@ -825,6 +829,7 @@ static void bfq_flush_idle_tree(struct bfq_service_tree *st)static void bfq_reparent_leaf_entity (struct bfq_data *bfqd,struct bfq_entity *entity,int ioprio_class) + __must_hold(&bfqd->lock) {struct bfq_queue *bfqq;struct bfq_entity *child_entity = entity;
@@ -856,6 +861,7 @@ static void bfq_reparent_active_queues(struct bfq_data *bfqd,struct bfq_group *bfqg, struct bfq_service_tree *st, int ioprio_class) + __must_hold(&bfqd->lock) { struct rb_root *active = &st->active;struct bfq_entity *entity;
@@ -1415,7 +1421,10 @@ struct cftype bfq_blkg_files[] = {#else /* CONFIG_BFQ_GROUP_IOSCHED */ void bfq_bfqq_move (struct bfq_data *bfqd,struct bfq_queue *bfqq, -struct bfq_group *bfqg) {} +struct bfq_group *bfqg) + __must_hold(&bfqd->lock) +{ +}void bfq_init_entity (struct bfq_entity *entity,struct bfq_group *bfqg) {
@@ -1432,6 +1441,7 @@ void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)void bfq_bic_update_cgroup (struct bfq_data *bfqd,struct bfq_io_cq *bic,struct bio *bio) + __must_hold(&bfqd->lock) { }
block/bfq-iosched.c +10 -1
@@ -465,6 +465,7 @@ static struct bfq_io_cq *bfq_bic_lookup(struct request_queue *q)* driver that will restart queueing. */ void bfq_schedule_dispatch (struct bfq_data *bfqd) + __must_hold(&bfqd->lock) {lockdep_assert_held (&bfqd->lock);
@@ -2627,6 +2628,7 @@ static void bfq_bfqq_end_wr(struct bfq_queue *bfqq)void bfq_end_wr_async_queues (struct bfq_data *bfqd,struct bfq_group *bfqg) + __must_hold(&bfqd->lock) {int i, j, k;
@@ -5433,6 +5435,7 @@ void bfq_put_cooperator(struct bfq_queue *bfqq) }static void bfq_exit_bfqq (struct bfq_data *bfqd,struct bfq_queue *bfqq) + __must_hold(&bfqd->lock) {if (bfqq == bfqd->in_service_queue) {__bfq_bfqq_expire (bfqd, bfqq,BFQQE_BUDGET_TIMEOUT );
@@ -5448,6 +5451,7 @@ static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)static void bfq_exit_icq_bfqq (struct bfq_data *bfqd,struct bfq_io_cq *bic,bool is_sync,unsigned int actuator_idx) + __must_hold(&bfqd->lock) {struct bfq_queue *bfqq =bic_to_bfqq (bic, is_sync, actuator_idx);
@@ -6212,7 +6216,8 @@ static inline void bfq_update_insert_stats(struct request_queue *q, blk_opf_t cmd_flags) {}#endif /* CONFIG_BFQ_CGROUP_DEBUG */ -static struct bfq_queue *bfq_init_rq (struct bfq_data *bfqd,struct request *rq); +static struct bfq_queue *bfq_init_rq (struct bfq_data *bfqd,struct request *rq) + __must_hold(&bfqd->lock);static void bfq_insert_request (struct blk_mq_hw_ctx *hctx,struct request *rq,blk_insert_t flags)
@@ -6325,6 +6330,7 @@ static void bfq_update_hw_tag(struct bfq_data *bfqd) }static void bfq_completed_request (struct bfq_queue *bfqq,struct bfq_data *bfqd) + __must_hold(&bfqd->lock) {u64 now_ns;u32 delta_us;
@@ -6906,6 +6912,7 @@ static struct bfq_queue *bfq_get_bfqq_handle_split(struct bfq_data *bfqd,* these preparation operations when rq is finally inserted or merged. */ static struct bfq_queue *bfq_init_rq (struct bfq_data *bfqd,struct request *rq) + __must_hold(&bfqd->lock) {struct bio *bio = rq->bio;struct bfq_io_cq *bic;
@@ -7051,6 +7058,7 @@ static enum hrtimer_restart bfq_idle_slice_timer(struct hrtimer *timer)static void __bfq_put_async_bfqq (struct bfq_data *bfqd,struct bfq_queue **bfqq_ptr) + __must_hold(&bfqd->lock) {struct bfq_queue *bfqq = *bfqq_ptr;
@@ -7072,6 +7080,7 @@ static void __bfq_put_async_bfqq(struct bfq_data *bfqd,* exist for sure until all the requests on a device are gone). */ void bfq_put_async_queues (struct bfq_data *bfqd,struct bfq_group *bfqg) + __must_hold(&bfqd->lock) {int i, j, k;
block/bfq-iosched.h +10 -5
@@ -1053,10 +1053,13 @@ void bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq, bool compensate, enum bfqq_expiration reason);void bfq_put_queue (struct bfq_queue *bfqq);void bfq_put_cooperator (struct bfq_queue *bfqq); -void bfq_end_wr_async_queues (struct bfq_data *bfqd,struct bfq_group *bfqg); +void bfq_end_wr_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg) + __must_hold(&bfqd->lock);void bfq_release_process_ref (struct bfq_data *bfqd,struct bfq_queue *bfqq); -void bfq_schedule_dispatch (struct bfq_data *bfqd); -void bfq_put_async_queues (struct bfq_data *bfqd,struct bfq_group *bfqg); +void bfq_schedule_dispatch(struct bfq_data *bfqd) + __must_hold(&bfqd->lock); +void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg) + __must_hold(&bfqd->lock);/* ------------ end of main algorithm interface -------------- */
@@ -1070,7 +1073,8 @@ void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,void bfqg_stats_update_dequeue (struct bfq_group *bfqg);void bfqg_stats_set_start_idle_time (struct bfq_group *bfqg); void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq, -struct bfq_group *bfqg); + struct bfq_group *bfqg) + __must_hold(&bfqd->lock);#ifdef CONFIG_BFQ_CGROUP_DEBUG void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
@@ -1082,7 +1086,8 @@ void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg);void bfq_init_entity (struct bfq_entity *entity,struct bfq_group *bfqg); void bfq_bic_update_cgroup(struct bfq_data *bfqd, struct bfq_io_cq *bic, -struct bio *bio); + struct bio *bio) + __must_hold(&bfqd->lock);void bfq_end_wr_async (struct bfq_data *bfqd);struct bfq_group *bfq_bio_bfqg (struct bfq_data *bfqd,struct bio *bio);struct blkcg_gq *bfqg_to_blkg (struct bfq_group *bfqg);
PATCH 12/52 blk-cgroup: Pass q to blkg_create() and blkg_lookup_create()
In reply to Bart Van AsscheMake the callers of blkg_create() and blkg_lookup_create() pass the request queue pointer as an additional argument. All callers already have that pointer since they hold q->queue_lock while calling these functions. Use q instead of disk->queue in blkg_create(), remove the local variable q from blkg_lookup_create() since it is now passed as an argument, and update the stale kernel-doc comment on blkg_lookup_create().
This change prepares for annotating blkg_create() and blkg_lookup_create() with __must_hold(&q->queue_lock). Clang's context analysis cannot determine that e.g. bio->bi_bdev->bd_queue->queue_lock and bio->bi_bdev->bd_disk->queue->queue_lock in blkg_tryget_closest() refer to the same spinlock. No functional change is intended since the request queue pointer passed by all callers equals disk->queue.
--- block/blk-cgroup.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-)
block/blk-cgroup.c +13 -12
@@ -391,15 +391,16 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk,* necessary using %GFP_NOWAIT. @new_blkg is always consumed on return. */ static struct blkcg_gq *blkg_create (struct blkcg *blkcg,struct gendisk *disk, +struct request_queue *q,struct blkcg_gq *new_blkg) {struct blkcg_gq *blkg;int i, ret; -lockdep_assert_held (&disk->queue->queue_lock); +lockdep_assert_held (&q->queue_lock);/* request_queue is dying, do not create/recreate a blkg */ -if (blk_queue_dying (disk->queue)) { +if (blk_queue_dying (q)) { ret = -ENODEV ; goto err_free_blkg; }
@@ -416,7 +417,7 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,/* link parent */ if (blkcg_parent (blkcg)) { - blkg->parent =blkg_lookup (blkcg_parent (blkcg), disk->queue); + blkg->parent =blkg_lookup (blkcg_parent (blkcg), q);if (WARN_ON_ONCE (!blkg->parent)) { ret = -ENODEV ; goto err_free_blkg;
@@ -434,10 +435,10 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,/* insert */ spin_lock (&blkcg->lock); - ret =radix_tree_insert (&blkcg->blkg_tree, disk->queue->id, blkg); + ret =radix_tree_insert (&blkcg->blkg_tree, q->id, blkg);if (likely (!ret)) {hlist_add_head_rcu (&blkg->blkcg_node, &blkcg->blkg_list); -list_add (&blkg->q_node, &disk->queue->blkg_list); +list_add (&blkg->q_node, &q->blkg_list);for (i =0 ; i <BLKCG_MAX_POLS ; i++) {struct blkcg_policy *pol = blkcg_policy[i];
@@ -469,19 +470,19 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,* blkg_lookup_create - lookup blkg, try to create one if not there * @blkcg: blkcg of interest * @disk: gendisk of interest +* @q: request queue associated with @disk * * Lookup blkg for the @blkcg - @disk pair. If it doesn't exist, try to * create one. blkg creation is performed recursively from blkcg_root such * that all non-root blkg's have access to the parent blkg. This function -* should be called under RCU read lock and takes @disk->queue->queue_lock. +* must be called with @q->queue_lock held. * * Returns the blkg or the closest blkg if blkg_create() fails as it walks * down from root. */ static struct blkcg_gq *blkg_lookup_create (struct blkcg *blkcg, -struct gendisk *disk) +struct gendisk *disk,struct request_queue *q) { -struct request_queue *q = disk->queue;struct blkcg_gq *blkg;rcu_read_lock ();
@@ -516,7 +517,7 @@ static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, parent =blkcg_parent (parent); } - blkg =blkg_create (pos, disk,NULL ); + blkg =blkg_create (pos, disk, q,NULL );if (IS_ERR (blkg)) { blkg = ret_blkg;break ;
@@ -915,7 +916,7 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,if (blkg) {blkg_free (new_blkg); }else { - blkg =blkg_create (pos, disk, new_blkg); + blkg =blkg_create (pos, disk, q, new_blkg);if (IS_ERR (blkg)) { ret =PTR_ERR (blkg); goto fail_preloaded;
@@ -1512,7 +1513,7 @@ int blkcg_init_disk(struct gendisk *disk)/* Make sure the root blkg exists. */ /* spin_lock_irq can serve as RCU read-side critical section. */ spin_lock_irq (&q->queue_lock); - blkg =blkg_create (&blkcg_root, disk, new_blkg); + blkg =blkg_create (&blkcg_root, disk, q, new_blkg);if (IS_ERR (blkg)) goto err_unlock; q->root_blkg = blkg;
@@ -2130,7 +2131,7 @@ static inline struct blkcg_gq *blkg_tryget_closest(struct bio *bio,* time, hold lock to create new blkg. */ spin_lock_irqsave (&q->queue_lock, flags); - blkg =blkg_lookup_create (blkcg, bio->bi_bdev->bd_disk); + blkg =blkg_lookup_create (blkcg, bio->bi_bdev->bd_disk, q);if (blkg) blkg =blkg_lookup_tryget (blkg);spin_unlock_irqrestore (&q->queue_lock, flags);
PATCH 13/52 blk-cgroup: Add lock context annotations
In reply to Bart Van AsscheAnnotate the following functions with __must_hold() such that Clang's context analysis verifies that all callers hold the required lock:
- blkg_create() and blkg_lookup_create() require q->queue_lock.
- blkcg_iostat_update() requires blkg_stat_lock.
- blkcg_policy_teardown_pds() requires q->queue_lock.
- blkcg_free_all_cpd() requires blkcg_pol_mutex.
--- block/blk-cgroup.c | 5 +++++ 1 file changed, 5 insertions(+)
block/blk-cgroup.c +5 -0
@@ -393,6 +393,7 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk,static struct blkcg_gq *blkg_create (struct blkcg *blkcg,struct gendisk *disk,struct request_queue *q,struct blkcg_gq *new_blkg) + __must_hold(&q->queue_lock) {struct blkcg_gq *blkg;int i, ret;
@@ -482,6 +483,7 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,*/ static struct blkcg_gq *blkg_lookup_create (struct blkcg *blkcg,struct gendisk *disk,struct request_queue *q) + __must_hold(&q->queue_lock) {struct blkcg_gq *blkg;
@@ -1000,6 +1002,7 @@ static void blkg_iostat_sub(struct blkg_iostat *dst, struct blkg_iostat *src)static void blkcg_iostat_update (struct blkcg_gq *blkg,struct blkg_iostat *cur,struct blkg_iostat *last) + __must_hold(&blkg_stat_lock) {struct blkg_iostat delta;unsigned long flags;
@@ -1570,6 +1573,7 @@ EXPORT_SYMBOL_GPL(io_cgrp_subsys);*/ static void blkcg_policy_teardown_pds (struct request_queue *q,const struct blkcg_policy *pol) + __must_hold(&q->queue_lock) {struct blkcg_gq *blkg;
@@ -1747,6 +1751,7 @@ void blkcg_deactivate_policy(struct gendisk *disk,EXPORT_SYMBOL_GPL (blkcg_deactivate_policy);static void blkcg_free_all_cpd (struct blkcg_policy *pol) + __must_hold(&blkcg_pol_mutex) {struct blkcg *blkcg;
PATCH 14/52 blk-cgroup: Annotate all_blkcgs with LIST_HEAD_GUARDED()
In reply to Bart Van AsscheThe all_blkcgs list is documented as being protected by blkcg_pol_mutex. Replace that comment with LIST_HEAD_GUARDED() so that Clang's context analysis can check that every access to the list happens while blkcg_pol_mutex is held.
Do not annotate blkcg_policy[] since it is read without holding blkcg_pol_mutex from several hot paths, e.g. blkg allocation and freeing.
--- block/blk-cgroup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-cgroup.c +1 -1
@@ -58,7 +58,7 @@ atomic_t blkcg_nr_congested __read_mostly = ATOMIC_INIT(0);static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS ]; -static LIST_HEAD (all_blkcgs);/* protected by blkcg_pol_mutex */ +static LIST_HEAD_GUARDED(all_blkcgs, blkcg_pol_mutex);bool blkcg_debug_stats = false;
PATCH 15/52 blk-crypto: Add lock context annotations
In reply to Bart Van Asscheblk_crypto_find_keyslot() and blk_crypto_find_and_grab_keyslot() are called either with profile->lock held for reading or with that lock held for writing via blk_crypto_hw_enter(). Annotate both functions with __must_hold_shared(&profile->lock).
blk_crypto_fallback_init() is only called with tfms_init_lock held. Annotate it with __must_hold(&tfms_init_lock).
--- block/blk-crypto-fallback.c | 1 + block/blk-crypto-profile.c | 2 ++ 2 files changed, 3 insertions(+)
block/blk-crypto-fallback.c +1 -0
@@ -535,6 +535,7 @@ int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key)static bool blk_crypto_fallback_inited;static int blk_crypto_fallback_init (void ) + __must_hold(&tfms_init_lock) {int i;int err;
block/blk-crypto-profile.c +2 -0
@@ -189,6 +189,7 @@ blk_crypto_remove_slot_from_lru_list(struct blk_crypto_keyslot *slot)static struct blk_crypto_keyslot *blk_crypto_find_keyslot (struct blk_crypto_profile *profile,const struct blk_crypto_key *key) + __must_hold_shared(&profile->lock) {const struct hlist_head *head =blk_crypto_hash_bucket_for_key (profile, key);
@@ -204,6 +205,7 @@ blk_crypto_find_keyslot(struct blk_crypto_profile *profile,static struct blk_crypto_keyslot *blk_crypto_find_and_grab_keyslot (struct blk_crypto_profile *profile,const struct blk_crypto_key *key) + __must_hold_shared(&profile->lock) {struct blk_crypto_keyslot *slot;
PATCH 16/52 block: Add __must_hold() annotations for fq->mq_flush_lock
In reply to Bart Van Asscheblk_flush_complete_seq() and blk_kick_flush() modify the flush queue state and hence must be called with fq->mq_flush_lock held. Annotate both functions with __must_hold(&fq->mq_flush_lock). Annotate both the forward declaration and the definition of blk_kick_flush().
--- block/blk-flush.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
block/blk-flush.c +4 -1
@@ -92,7 +92,8 @@ enum { }; static void blk_kick_flush(struct request_queue *q, -struct blk_flush_queue *fq,blk_opf_t flags); + struct blk_flush_queue *fq, blk_opf_t flags) + __must_hold(&fq->mq_flush_lock);static inline struct blk_flush_queue *blk_get_flush_queue (struct blk_mq_ctx *ctx)
@@ -148,6 +149,7 @@ static void blk_account_io_flush(struct request *rq)static void blk_flush_complete_seq (struct request *rq,struct blk_flush_queue *fq,unsigned int seq,blk_status_t error) + __must_hold(&fq->mq_flush_lock) {struct request_queue *q = rq->q;struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
@@ -281,6 +283,7 @@ bool is_flush_rq(struct request *rq)*/ static void blk_kick_flush (struct request_queue *q,struct blk_flush_queue *fq,blk_opf_t flags) + __must_hold(&fq->mq_flush_lock) {struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx]; struct request *first_rq =
PATCH 17/52 block: Annotate struct blk_flush_queue members with __guarded_by()
In reply to Bart Van AsscheAll accesses of the flush state machine members of struct blk_flush_queue happen with mq_flush_lock held. Document this by annotating these members with __guarded_by() such that Clang's context analysis verifies that the lock is held when these members are accessed. Leave flush_rq unannotated because it is set once during allocation and read without holding mq_flush_lock.
Initialize the flush_queue[] lists inside a spinlock_init scoped guard such that the context analysis accepts these initialization accesses.
--- block/blk-flush.c | 8 ++++---- block/blk.h | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-)
block/blk-flush.c +4 -4
@@ -495,16 +495,16 @@ struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size,if (!fq) goto fail; -spin_lock_init (&fq->mq_flush_lock); + scoped_guard(spinlock_init, &fq->mq_flush_lock) { +INIT_LIST_HEAD (&fq->flush_queue[0 ]); +INIT_LIST_HEAD (&fq->flush_queue[1 ]); + } rq_sz =round_up (rq_sz + cmd_size,cache_line_size ()); fq->flush_rq =kzalloc_node (rq_sz, flags, node);if (!fq->flush_rq) goto fail_rq; -INIT_LIST_HEAD (&fq->flush_queue[0 ]); -INIT_LIST_HEAD (&fq->flush_queue[1 ]); -return fq; fail_rq:
block/blk.h +12 -6
@@ -34,12 +34,18 @@ extern struct dentry *blk_debugfs_root;struct blk_flush_queue {spinlock_t mq_flush_lock; -unsigned int flush_pending_idx:1 ; -unsigned int flush_running_idx:1 ; -blk_status_t rq_status; -unsigned long flush_pending_since; -struct list_head flush_queue[2 ]; -unsigned long flush_data_in_flight; + unsigned int flush_pending_idx:1 +__guarded_by (&mq_flush_lock); + unsigned int flush_running_idx:1 +__guarded_by (&mq_flush_lock); + blk_status_t rq_status + __guarded_by(&mq_flush_lock); +unsigned long flush_pending_since + __guarded_by(&mq_flush_lock); + struct list_head flush_queue[2] + __guarded_by(&mq_flush_lock); +unsigned long flush_data_in_flight + __guarded_by(&mq_flush_lock);struct request *flush_rq;struct rcu_head rcu_head; };
PATCH 18/52 blk-iocost: Add lock context annotations
In reply to Bart Van AsscheMany blk-iocost functions verify with lockdep_assert_held() that their callers hold ioc->lock and/or iocg->waitq.lock. Annotate these functions and also a few functions without such an assertion but that are always called with these locks held with __must_hold() such that Clang verifies the locking requirements at compile time:
- ioc_refresh_margins(), ioc_refresh_period_us(), ioc_refresh_lcoefs(), ioc_refresh_params_disk(), ioc_refresh_params(), ioc_refresh_vrate(), ioc_adjust_base_vrate(), commit_weights(), ioc_forgive_debts() and ioc_check_iocgs() require ioc->lock.
- propagate_weights() requires iocg->ioc->lock.
- iocg_kick_delay(), iocg_incur_debt(), iocg_kick_waitq() and iocg_handle_over_budget() require iocg->waitq.lock.
- iocg_pay_debt() requires both iocg->ioc->lock and iocg->waitq.lock.
Do not annotate iocg_incur_debt() for iocg->ioc->lock because callers hold that lock conditionally or through a different pointer expression; the existing lockdep_assert_held(&iocg->ioc->lock) keeps verifying it at runtime.
--- block/blk-iocost.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
block/blk-iocost.c +17 -0
@@ -784,6 +784,7 @@ static inline u32 iocg_hweight_cache_inuse(struct ioc_gq *iocg)#include <trace/events/iocost.h> static void ioc_refresh_margins (struct ioc *ioc) + __must_hold(&ioc->lock) {struct ioc_margins *margins = &ioc->margins;u32 period_us = ioc->period_us;
@@ -796,6 +797,7 @@ static void ioc_refresh_margins(struct ioc *ioc)/* latency Qos params changed, update period_us and all the dependent params */ static void ioc_refresh_period_us (struct ioc *ioc) + __must_hold(&ioc->lock) {u32 ppm, lat, multi, period_us;
@@ -928,6 +930,7 @@ static void calc_lcoefs(u64 bps, u64 seqiops, u64 randiops, }static void ioc_refresh_lcoefs (struct ioc *ioc) + __must_hold(&ioc->lock) {u64 *u = ioc->params.i_lcoefs;u64 *c = ioc->params.lcoefs;
@@ -947,6 +950,7 @@ static void ioc_refresh_lcoefs(struct ioc *ioc)*/ static bool ioc_refresh_params_disk (struct ioc *ioc,bool force,struct gendisk *disk) + __must_hold(&ioc->lock) {const struct ioc_params *p;int idx;
@@ -985,6 +989,7 @@ static bool ioc_refresh_params_disk(struct ioc *ioc, bool force, }static bool ioc_refresh_params (struct ioc *ioc,bool force) + __must_hold(&ioc->lock) {return ioc_refresh_params_disk (ioc, force, ioc->rqos.disk); }
@@ -997,6 +1002,7 @@ static bool ioc_refresh_params(struct ioc *ioc, bool force)* matches what got lost. */ static void ioc_refresh_vrate (struct ioc *ioc,struct ioc_now *now) + __must_hold(&ioc->lock) {s64 pleft = ioc->period_at + ioc->period_us - now->now;s64 vperiod = ioc->period_us * ioc->vtime_base_rate;
@@ -1029,6 +1035,7 @@ static void ioc_refresh_vrate(struct ioc *ioc, struct ioc_now *now)static void ioc_adjust_base_vrate (struct ioc *ioc,u32 rq_wait_pct,int nr_lagging,int nr_shortages,int prev_busy_level,u32 *missed_ppm) + __must_hold(&ioc->lock) {u64 vrate = ioc->vtime_base_rate;u64 vrate_min = ioc->vrate_min, vrate_max = ioc->vrate_max;
@@ -1188,6 +1195,7 @@ static void __propagate_weights(struct ioc_gq *iocg, u32 active, u32 inuse, }static void commit_weights (struct ioc *ioc) + __must_hold(&ioc->lock) {lockdep_assert_held (&ioc->lock);
@@ -1201,6 +1209,7 @@ static void commit_weights(struct ioc *ioc)static void propagate_weights (struct ioc_gq *iocg,u32 active,u32 inuse,bool save,struct ioc_now *now) + __must_hold(&iocg->ioc->lock) {__propagate_weights (iocg, active, inuse, save, now);commit_weights (iocg->ioc);
@@ -1402,6 +1411,7 @@ static bool iocg_activate(struct ioc_gq *iocg, struct ioc_now *now) }static bool iocg_kick_delay (struct ioc_gq *iocg,struct ioc_now *now) + __must_hold(&iocg->waitq.lock) {struct ioc *ioc = iocg->ioc;struct blkcg_gq *blkg =iocg_to_blkg (iocg);
@@ -1468,6 +1478,7 @@ static bool iocg_kick_delay(struct ioc_gq *iocg, struct ioc_now *now)static void iocg_incur_debt (struct ioc_gq *iocg,u64 abs_cost,struct ioc_now *now) + __must_hold(&iocg->waitq.lock) {struct iocg_pcpu_stat *gcs;
@@ -1493,6 +1504,8 @@ static void iocg_incur_debt(struct ioc_gq *iocg, u64 abs_cost,static void iocg_pay_debt (struct ioc_gq *iocg,u64 abs_vpay,struct ioc_now *now) + __must_hold(&iocg->ioc->lock) + __must_hold(&iocg->waitq.lock) {lockdep_assert_held (&iocg->ioc->lock);lockdep_assert_held (&iocg->waitq.lock);
@@ -1550,6 +1563,7 @@ static int iocg_wake_fn(struct wait_queue_entry *wq_entry, unsigned mode,*/ static void iocg_kick_waitq (struct ioc_gq *iocg,bool pay_debt,struct ioc_now *now) + __must_hold(&iocg->waitq.lock) {struct ioc *ioc = iocg->ioc;struct iocg_wake_ctx ctx = { .iocg = iocg };
@@ -2146,6 +2160,7 @@ static void transfer_surpluses(struct list_head *surpluses, struct ioc_now *now)*/ static void ioc_forgive_debts (struct ioc *ioc,u64 usage_us_sum,int nr_debtors,struct ioc_now *now) + __must_hold(&ioc->lock) {struct ioc_gq *iocg;u64 dur, usage_pct, nr_cycles, nr_cycles_shift;
@@ -2238,6 +2253,7 @@ static void ioc_forgive_debts(struct ioc *ioc, u64 usage_us_sum, int nr_debtors,* iocgs. */ static int ioc_check_iocgs (struct ioc *ioc,struct ioc_now *now) + __must_hold(&ioc->lock) {int nr_debtors =0 ;struct ioc_gq *iocg, *tiocg;
@@ -2699,6 +2715,7 @@ iocg_handle_over_budget(struct rq_qos *rqos, struct ioc_gq *iocg, struct bio *bio, struct ioc_now *now, struct iocg_wait *wait, bool use_debt, bool ioc_locked, u64 abs_cost, u64 cost) + __must_hold(&iocg->waitq.lock) { lockdep_assert_held(&iocg->waitq.lock);
Tejun Heo
maintainerRe: [PATCH 18/52] blk-iocost: Add lock context annotations
Hello, Bart.
Quoted 5 lines · Bart Van Assche
Many blk-iocost functions verify with
lockdep_assert_held()that their callers holdioc->lockand/oriocg->waitq.lock. Annotate these functions and also a few functions without such an assertion but that are always called with these locks held with__must_hold()such that Clang verifies the locking requirements at compile time:
I raised this before and I'm not going to nack this as it's a subsystem-wide decision, but I still think this is a silly feel-good thing which doesn't add anything substantial, just noise and churn:
- This doesn't catch anything subtle. The conditionally locked and lockless accesses stay unannotated as the tool can't express them. Only the trivial cases are left, and those are now fairly reliably covered by AI review.
- More than half of the functions annotated here already have lockdep_assert_held() for the same lock.
- 19/52 adds an argument to hweight_after_donation() only because clang can't tell that iocg->ioc->lock and ioc->lock are the same lock.
Thanks.
--
tejun
PATCH 19/52 blk-iocost: Pass the ioc pointer to hweight_after_donation()
In reply to Bart Van AsschePass the ioc pointer explicitly to hweight_after_donation() instead of deriving it from iocg->ioc. The only caller, ioc_timer_fn(), holds ioc->lock. This change prepares for annotating hweight_after_donation() with __must_hold(&ioc->lock). Clang's context analysis cannot determine that iocg->ioc->lock and the ioc->lock held by ioc_timer_fn() refer to the same spinlock.
No functional change is intended since ioc_timer_fn() only passes iocgs from its ioc->active_iocgs list and since iocg->ioc equals ioc for these iocgs.
--- block/blk-iocost.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
block/blk-iocost.c +5 -5
@@ -1823,10 +1823,10 @@ static void iocg_flush_stat(struct list_head *target_iocgs, struct ioc_now *now)* capacity. @hwm is the upper bound and used to signal no donation. This * function also throws away @iocg's excess budget. */ -static u32 hweight_after_donation (struct ioc_gq *iocg,u32 old_hwi,u32 hwm, -u32 usage,struct ioc_now *now) +static u32 hweight_after_donation (struct ioc *ioc,struct ioc_gq *iocg, +u32 old_hwi,u32 hwm,u32 usage, +struct ioc_now *now) { -struct ioc *ioc = iocg->ioc;u64 vtime =atomic64_read (&iocg->vtime);s64 excess, delta, target, new_hwi;
@@ -2424,8 +2424,8 @@ static void ioc_timer_fn(struct timer_list *timer)*/ current_hweight (iocg, &hwa, &old_hwi); hwm =current_hweight_max (iocg); - new_hwi =hweight_after_donation (iocg, old_hwi, hwm, - usage, &now); + new_hwi =hweight_after_donation (ioc, iocg, old_hwi, + hwm, usage, &now);/* * Donation calculation assumes hweight_after_donation * to be positive, a condition that a donor w/ hwa < 2
PATCH 20/52 blk-iocost: Annotate struct ioc fields protected by ioc->lock
In reply to Bart Van AsscheAdd __guarded_by(&lock) annotations to the members of struct ioc that are only accessed with ioc->lock held: vrate_min, vrate_max, the active iocg list, the running state, the vtime error accumulator, the weights_updated flag, the debt forgiveness state, the autop state and the user_qos_params / user_cost_model flags. This allows Clang's context analysis to verify that these members are only accessed with ioc->lock held.
Annotate ioc_autop_idx(), ioc_start_period() and hweight_after_donation() with __must_hold(&ioc->lock) since all their callers hold ioc->lock. Use scoped_guard(spinlock_init) for the initialization of the guarded members in blk_iocost_init().
The remaining unannotated members are read without holding ioc->lock, e.g. from the issue path, from tracepoints or under the period seqcount.
--- block/blk-iocost.c | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-)
block/blk-iocost.c +22 -19
@@ -415,18 +415,18 @@ struct ioc {struct ioc_margins margins;u32 period_us;u32 timer_slack_ns; -u64 vrate_min; -u64 vrate_max; + u64 vrate_min __guarded_by(&lock); + u64 vrate_max __guarded_by(&lock);spinlock_t lock;struct timer_list timer; -struct list_head active_iocgs;/* active cgroups */ + struct list_head active_iocgs __guarded_by(&lock); /* active cgroups */struct ioc_pcpu_stat __percpu *pcpu_stat; -enum ioc_running running; + enum ioc_running running __guarded_by(&lock);atomic64_t vtime_rate;u64 vtime_base_rate; -s64 vtime_err; + s64 vtime_err __guarded_by(&lock);seqcount_spinlock_t period_seqcount;u64 period_at;/* wallclock starttime */
@@ -435,19 +435,19 @@ struct ioc {atomic64_t cur_period;/* inc'd each period */ int busy_level;/* saturation history */ -bool weights_updated; + bool weights_updated __guarded_by(&lock);atomic_t hweight_gen;/* for lazy hweights */ /* debt forgivness */ -u64 dfgv_period_at; -u64 dfgv_period_rem; -u64 dfgv_usage_us_sum; - -u64 autop_too_fast_at; -u64 autop_too_slow_at; -int autop_idx; - bool user_qos_params:1; - bool user_cost_model:1; + u64 dfgv_period_at __guarded_by(&lock); + u64 dfgv_period_rem __guarded_by(&lock); + u64 dfgv_usage_us_sum __guarded_by(&lock); + + u64 autop_too_fast_at __guarded_by(&lock); + u64 autop_too_slow_at __guarded_by(&lock); + int autop_idx __guarded_by(&lock); + bool user_qos_params:1 __guarded_by(&lock); + bool user_cost_model:1 __guarded_by(&lock); };struct iocg_pcpu_stat {
@@ -840,6 +840,7 @@ static void ioc_refresh_period_us(struct ioc *ioc)* the init path. */ static int ioc_autop_idx (struct ioc *ioc,struct gendisk *disk) + __must_hold(&ioc->lock) {int idx = ioc->autop_idx;const struct ioc_params *p = &autop[idx];
@@ -1107,6 +1108,7 @@ static void ioc_now(struct ioc *ioc, struct ioc_now *now) }static void ioc_start_period (struct ioc *ioc,struct ioc_now *now) + __must_hold(&ioc->lock) {WARN_ON_ONCE (ioc->running !=IOC_RUNNING );
@@ -1826,6 +1828,7 @@ static void iocg_flush_stat(struct list_head *target_iocgs, struct ioc_now *now)static u32 hweight_after_donation (struct ioc *ioc,struct ioc_gq *iocg,u32 old_hwi,u32 hwm,u32 usage,struct ioc_now *now) + __must_hold(&ioc->lock) {u64 vtime =atomic64_read (&iocg->vtime);s64 excess, delta, target, new_hwi;
@@ -3048,11 +3051,11 @@ static int blk_iocost_init(struct gendisk *disk)local64_set (&ccs->rq_wait_ns,0 ); } -spin_lock_init (&ioc->lock);timer_setup (&ioc->timer, ioc_timer_fn,0 ); -INIT_LIST_HEAD (&ioc->active_iocgs); - - ioc->running =IOC_IDLE ; + scoped_guard(spinlock_init, &ioc->lock) { +INIT_LIST_HEAD (&ioc->active_iocgs); + ioc->running =IOC_IDLE ; + } ioc->vtime_base_rate =VTIME_PER_USEC ;atomic64_set (&ioc->vtime_rate,VTIME_PER_USEC );seqcount_spinlock_init (&ioc->period_seqcount, &ioc->lock);
PATCH 21/52 blk-iolatency: Add a __must_hold() annotation for lat_info->lock
In reply to Bart Van Asschescale_cookie_change() modifies the scale cookie and hence must be called with lat_info->lock held. All of its callers hold that lock. Annotate scale_cookie_change() with __must_hold(&lat_info->lock).
--- block/blk-iolatency.c | 1 + 1 file changed, 1 insertion(+)
block/blk-iolatency.c +1 -0
@@ -329,6 +329,7 @@ static inline unsigned long scale_amount(unsigned long qd, bool up)static void scale_cookie_change (struct blk_iolatency *blkiolat,struct child_latency_info *lat_info,bool up) + __must_hold(&lat_info->lock) {unsigned long qd = blkiolat->rqos.disk->queue->nr_requests;unsigned long scale =scale_amount (qd, up);
PATCH 22/52 blk-iolatency: Use WRITE_ONCE() in iolatency_clear_scaling()
In reply to Bart Van Asschecheck_scale_change() reads child_latency_info.scale_lat with READ_ONCE() and without holding the child_latency_info lock. iolatency_check_latencies() updates scale_lat with WRITE_ONCE() but iolatency_clear_scaling() uses a plain store. Use WRITE_ONCE() in iolatency_clear_scaling() too such that all scale_lat writes that may race with the lockless read are marked.
--- block/blk-iolatency.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-iolatency.c +1 -1
@@ -822,7 +822,7 @@ static void iolatency_clear_scaling(struct blkcg_gq *blkg)atomic_set (&lat_info->scale_cookie,DEFAULT_SCALE_COOKIE ); lat_info->last_scale_event =0 ; lat_info->scale_grp =NULL ; - lat_info->scale_lat =0 ; +WRITE_ONCE (lat_info->scale_lat,0 );spin_unlock (&lat_info->lock); } }
PATCH 23/52 blk-iolatency: Annotate child_latency_info with __guarded_by()
In reply to Bart Van AsscheAnnotate the last_scale_event, scale_lat, nr_samples and scale_grp members of struct child_latency_info with __guarded_by(&lock) such that the Clang context analysis can verify that these members are only accessed with the child_latency_info lock held. Leave scale_cookie unannotated since it is an atomic_t that is accessed without holding the lock.
Mark the two intentional lockless reads in check_scale_change() such that the context analysis accepts these. Use context_unsafe(READ_ONCE()) for scale_lat since all writers of that member use WRITE_ONCE() and hence there is no need to disable KCSAN for this read. Use data_race(READ_ONCE()) for nr_samples since that member is updated with plain assignments. Both reads are hints only and their result is allowed to be stale.
--- block/blk-iolatency.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
block/blk-iolatency.c +11 -6
@@ -109,16 +109,16 @@ struct child_latency_info {spinlock_t lock;/* Last time we adjusted the scale of everybody. */ -u64 last_scale_event; + u64 last_scale_event __guarded_by(&lock);/* The latency that we missed. */ -u64 scale_lat; + u64 scale_lat __guarded_by(&lock);/* Total io's from all of our children for the last summation. */ -u64 nr_samples; + u64 nr_samples __guarded_by(&lock);/* The guy who actually changed the latency numbers. */ -struct iolatency_grp *scale_grp; + struct iolatency_grp *scale_grp __guarded_by(&lock);/* Cookie to tell if we need to scale up or down. */ atomic_t scale_cookie;
@@ -412,7 +412,11 @@ static void check_scale_change(struct iolatency_grp *iolat) lat_info = &parent->child_lat; cur_cookie =atomic_read (&lat_info->scale_cookie); - scale_lat =READ_ONCE (lat_info->scale_lat); +/* +* Lockless read. All writers use WRITE_ONCE(), see also +* iolatency_check_latencies() and iolatency_clear_scaling(). +*/ + scale_lat =context_unsafe (READ_ONCE (lat_info->scale_lat));if (cur_cookie < our_cookie) direction =-1 ;
@@ -438,7 +442,8 @@ static void check_scale_change(struct iolatency_grp *iolat)* or less of the IO's for the last summation just skip this * scale down event. */ - samples_thresh = lat_info->nr_samples *5 ; +/* Lockless hint read; stale values are fine. */ + samples_thresh =data_race (READ_ONCE (lat_info->nr_samples)) *5 ; samples_thresh =max (1ULL ,div64_u64 (samples_thresh,100 ));if (iolat->nr_samples <= samples_thresh)return ;
PATCH 24/52 blk-mq: Add lock context annotations
In reply to Bart Van AsscheAnnotate the blk-mq functions that are always called with a lock held with __must_hold() such that Clang's context analysis verifies that all callers hold the required lock:
- blk_mq_hctx_mark_pending() and blk_mq_hctx_clear_pending() require ctx->lock.
- blk_freeze_set_owner() and blk_unfreeze_check_owner() require q->mq_freeze_lock. Annotate both the CONFIG_LOCKDEP and the !CONFIG_LOCKDEP implementations.
- __blk_mq_remove_cpuhp(), __blk_mq_add_cpuhp() and __blk_mq_remove_cpuhp_list() require blk_mq_cpuhp_lock.
- blk_mq_exit_hw_queues(), blk_mq_update_tag_set_shared(), blk_mq_add_queue_tag_set() and blk_mq_del_queue_tag_set() require the tag set tag_list_lock.
- __blk_mq_update_nr_hw_queues() requires both set->update_nr_hwq_lock and set->tag_list_lock.
- blk_mq_update_nr_requests() requires q->elevator_lock. Annotate both the declaration in blk-mq.h and the definition.
Keep the existing lockdep_assert_held() calls since these also verify the locking requirements at runtime.
--- block/blk-mq.c | 16 ++++++++++++++++ block/blk-mq.h | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-)
block/blk-mq.c +16 -0
@@ -70,6 +70,7 @@ static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)*/ static void blk_mq_hctx_mark_pending (struct blk_mq_hw_ctx *hctx,struct blk_mq_ctx *ctx) + __must_hold(&ctx->lock) {const int bit = ctx->index_hw[hctx->type];
@@ -79,6 +80,7 @@ static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,static void blk_mq_hctx_clear_pending (struct blk_mq_hw_ctx *hctx,struct blk_mq_ctx *ctx) + __must_hold(&ctx->lock) {const int bit = ctx->index_hw[hctx->type];
@@ -115,6 +117,7 @@ void blk_mq_in_driver_rw(struct block_device *part, unsigned int inflight[2])#ifdef CONFIG_LOCKDEP static bool blk_freeze_set_owner (struct request_queue *q,struct task_struct *owner) + __must_hold(&q->mq_freeze_lock) {if (!owner)return false;
@@ -136,6 +139,7 @@ static bool blk_freeze_set_owner(struct request_queue *q,/* verify the last unfreeze in owner context */ static bool blk_unfreeze_check_owner (struct request_queue *q) + __must_hold(&q->mq_freeze_lock) {if (q->mq_freeze_owner != current)return false;
@@ -150,11 +154,13 @@ static bool blk_unfreeze_check_owner(struct request_queue *q)static bool blk_freeze_set_owner (struct request_queue *q,struct task_struct *owner) + __must_hold(&q->mq_freeze_lock) {return false; }static bool blk_unfreeze_check_owner (struct request_queue *q) + __must_hold(&q->mq_freeze_lock) {return false; }
@@ -3894,6 +3900,7 @@ static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node) }static void __blk_mq_remove_cpuhp (struct blk_mq_hw_ctx *hctx) + __must_hold(&blk_mq_cpuhp_lock) {lockdep_assert_held (&blk_mq_cpuhp_lock);
@@ -3919,6 +3926,7 @@ static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx) }static void __blk_mq_add_cpuhp (struct blk_mq_hw_ctx *hctx) + __must_hold(&blk_mq_cpuhp_lock) {lockdep_assert_held (&blk_mq_cpuhp_lock);
@@ -3933,6 +3941,7 @@ static void __blk_mq_add_cpuhp(struct blk_mq_hw_ctx *hctx) }static void __blk_mq_remove_cpuhp_list (struct list_head *head) + __must_hold(&blk_mq_cpuhp_lock) {struct blk_mq_hw_ctx *hctx;
@@ -4037,6 +4046,7 @@ static void blk_mq_exit_hctx(struct request_queue *q,static void blk_mq_exit_hw_queues (struct request_queue *q,struct blk_mq_tag_set *set,int nr_queue) + __must_hold(&set->tag_list_lock) {struct blk_mq_hw_ctx *hctx;unsigned long i;
@@ -4368,6 +4378,7 @@ static void queue_set_hctx_shared(struct request_queue *q, bool shared)static void blk_mq_update_tag_set_shared (struct blk_mq_tag_set *set,bool shared) + __must_hold(&set->tag_list_lock) {struct request_queue *q;unsigned int memflags;
@@ -4382,6 +4393,7 @@ static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set, }static void blk_mq_del_queue_tag_set (struct request_queue *q) + __must_hold(&q->tag_set->tag_list_lock) {struct blk_mq_tag_set *set = q->tag_set;
@@ -4401,6 +4413,7 @@ static void blk_mq_del_queue_tag_set(struct request_queue *q)static void blk_mq_add_queue_tag_set (struct blk_mq_tag_set *set,struct request_queue *q) + __must_hold(&set->tag_list_lock) {unsigned int i;
@@ -5087,6 +5100,7 @@ EXPORT_SYMBOL(blk_mq_free_tag_set);struct elevator_tags *blk_mq_update_nr_requests (struct request_queue *q,struct elevator_tags *et,unsigned int nr) + __must_hold(&q->elevator_lock) {struct blk_mq_tag_set *set = q->tag_set;struct elevator_tags *old_et =NULL ;
@@ -5208,6 +5222,8 @@ static int blk_mq_elv_switch_none(struct request_queue *q,static void __blk_mq_update_nr_hw_queues (struct blk_mq_tag_set *set,int nr_hw_queues) + __must_hold(&set->update_nr_hwq_lock) + __must_hold(&set->tag_list_lock) {struct request_queue *q;int prev_nr_hw_queues = set->nr_hw_queues;
block/blk-mq.h +2 -1
@@ -48,7 +48,8 @@ int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, struct io_comp_batch *void blk_mq_exit_queue (struct request_queue *q);struct elevator_tags *blk_mq_update_nr_requests (struct request_queue *q,struct elevator_tags *tags, -unsigned int nr); +unsigned int nr) + __must_hold(&q->elevator_lock);void blk_mq_wake_waiters (struct request_queue *q);bool blk_mq_dispatch_rq_list (struct blk_mq_hw_ctx *hctx,struct list_head *,bool );
PATCH 25/52 blk-mq: Annotate blk_mq_ctx::rq_lists with __guarded_by()
In reply to Bart Van AsscheThe software queue request lists in struct blk_mq_ctx are only modified and traversed with blk_mq_ctx::lock held. Mark them with __guarded_by(&lock) so that Clang's context analysis can check this.
Use scoped_guard(spinlock_init) when initializing the lists, and add a __must_hold() annotation to the debugfs seq_file ->next() callback. That callback runs with the lock held, because ->start() acquires it and ->stop() releases it.
The lockless check in blk_mq_sched_bio_merge() uses list_empty_careful(), which is already annotated as an intentional lockless access, so it does not need any changes.
--- block/blk-mq-debugfs.c | 1 + block/blk-mq.c | 6 +++--- block/blk-mq.h | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-)
block/blk-mq-debugfs.c +1 -0
@@ -506,6 +506,7 @@ static void *ctx_##name##_rq_list_start(struct seq_file *m, loff_t *pos) \ \ static void *ctx_##name##_rq_list_next(struct seq_file *m, void *v, \ loff_t *pos) \ + __must_hold(&TO_CTX(m)->lock) \ { \ struct blk_mq_ctx *ctx = m->private; \ \
block/blk-mq.c +3 -3
@@ -4167,9 +4167,9 @@ static void blk_mq_init_cpu_queues(struct request_queue *q,int k; __ctx->cpu = i; -spin_lock_init (&__ctx->lock); -for (k =HCTX_TYPE_DEFAULT ; k <HCTX_MAX_TYPES ; k++) -INIT_LIST_HEAD (&__ctx->rq_lists[k]); + scoped_guard(spinlock_init, &__ctx->lock) +for (k =HCTX_TYPE_DEFAULT ; k <HCTX_MAX_TYPES ; k++) +INIT_LIST_HEAD (&__ctx->rq_lists[k]); __ctx->queue = q;
block/blk-mq.h +1 -1
@@ -19,7 +19,7 @@ struct blk_mq_ctxs {struct blk_mq_ctx {struct {spinlock_t lock; -struct list_head rq_lists[HCTX_MAX_TYPES ]; + struct list_head rq_lists[HCTX_MAX_TYPES] __guarded_by(&lock); }____cacheline_aligned_in_smp ;unsigned int cpu;
PATCH 26/52 blk-mq-debugfs: Pass q to blk_mq_debugfs_unregister_sched_hctx()
In reply to Bart Van AsschePass the request queue pointer explicitly to blk_mq_debugfs_unregister_sched_hctx(), as is already done for blk_mq_debugfs_register_sched_hctx(). This change prepares for annotating this function with __must_hold(&q->debugfs_mutex). Clang's context analysis cannot determine that hctx->queue->debugfs_mutex and the q->debugfs_mutex held by the caller refer to the same mutex.
No functional change is intended since the only caller passes a q that equals hctx->queue.
--- block/blk-mq-debugfs.c | 7 ++++--- block/blk-mq-debugfs.h | 6 ++++-- block/blk-mq-sched.c | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-)
block/blk-mq-debugfs.c +4 -3
@@ -819,11 +819,12 @@ void blk_mq_debugfs_register_sched_hctx(struct request_queue *q, e->hctx_debugfs_attrs); } -void blk_mq_debugfs_unregister_sched_hctx (struct blk_mq_hw_ctx *hctx) +void blk_mq_debugfs_unregister_sched_hctx (struct request_queue *q, +struct blk_mq_hw_ctx *hctx) { -lockdep_assert_held (&hctx->queue->debugfs_mutex); +lockdep_assert_held (&q->debugfs_mutex); -if (!hctx->queue->debugfs_dir) +if (!q->debugfs_dir)return ;debugfs_remove_recursive (hctx->sched_debugfs_dir); hctx->sched_debugfs_dir =NULL ;
block/blk-mq-debugfs.h +4 -2
@@ -31,7 +31,8 @@ void blk_mq_debugfs_register_sched(struct request_queue *q);void blk_mq_debugfs_unregister_sched (struct request_queue *q);void blk_mq_debugfs_register_sched_hctx (struct request_queue *q,struct blk_mq_hw_ctx *hctx); -void blk_mq_debugfs_unregister_sched_hctx (struct blk_mq_hw_ctx *hctx); +void blk_mq_debugfs_unregister_sched_hctx (struct request_queue *q, +struct blk_mq_hw_ctx *hctx);void blk_mq_debugfs_register_rq_qos (struct request_queue *q);#else
@@ -69,7 +70,8 @@ static inline void blk_mq_debugfs_register_sched_hctx(struct request_queue *q, { } -static inline void blk_mq_debugfs_unregister_sched_hctx (struct blk_mq_hw_ctx *hctx) +static inline void blk_mq_debugfs_unregister_sched_hctx (struct request_queue *q, +struct blk_mq_hw_ctx *hctx) { }
block/blk-mq-sched.c +1 -1
@@ -411,7 +411,7 @@ void blk_mq_sched_unreg_debugfs(struct request_queue *q)blk_debugfs_lock_nomemsave (q);queue_for_each_hw_ctx (q, hctx, i) -blk_mq_debugfs_unregister_sched_hctx (hctx); +blk_mq_debugfs_unregister_sched_hctx (q, hctx);blk_mq_debugfs_unregister_sched (q);blk_debugfs_unlock_nomemrestore (q); }
PATCH 27/52 blk-mq-debugfs: Add __must_hold() annotations for q->debugfs_mutex
In reply to Bart Van AsscheSeveral blk-mq debugfs registration and unregistration functions must be called with q->debugfs_mutex held. Some of these functions already verify this with lockdep_assert_held(). Additionally annotate these functions with __must_hold(&q->debugfs_mutex) such that Clang verifies the locking requirements at compile time:
- debugfs_create_files()
- blk_mq_debugfs_register()
- blk_mq_debugfs_register_ctx()
- blk_mq_debugfs_register_hctx()
- blk_mq_debugfs_register_sched()
- blk_mq_debugfs_unregister_sched()
- blk_mq_debugfs_register_sched_hctx()
- blk_mq_debugfs_unregister_sched_hctx()
- blk_mq_debugfs_register_rq_qos()
Pass q to blk_mq_debugfs_register_ctx() instead of deriving it from hctx->queue since Clang's context analysis cannot determine that q->debugfs_mutex and hctx->queue->debugfs_mutex are the same mutex. No functional change is intended since blk_mq_debugfs_register_hctx(), the only caller of that function, is only called with q == hctx->queue.
Annotate the declarations in blk-mq-debugfs.h, the definitions and also the inline stubs that are used if CONFIG_BLK_DEBUG_FS=n such that the locking requirements do not depend on the kernel configuration. Include <linux/blkdev.h> in blk-mq-debugfs.h so that struct request_queue is defined for &q->debugfs_mutex in both configurations.
--- block/blk-mq-debugfs.c | 17 +++++++++++++---- block/blk-mq-debugfs.h | 34 +++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 13 deletions(-)
block/blk-mq-debugfs.c +13 -4
@@ -625,6 +625,7 @@ static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {static void debugfs_create_files (struct request_queue *q,struct dentry *parent,void *data,const struct blk_mq_debugfs_attr *attr) + __must_hold(&q->debugfs_mutex) {lockdep_assert_held (&q->debugfs_mutex);/*
@@ -643,6 +644,7 @@ static void debugfs_create_files(struct request_queue *q, struct dentry *parent, }void blk_mq_debugfs_register (struct request_queue *q) + __must_hold(&q->debugfs_mutex) {struct blk_mq_hw_ctx *hctx;unsigned long i;
@@ -657,8 +659,10 @@ void blk_mq_debugfs_register(struct request_queue *q)blk_mq_debugfs_register_rq_qos (q); } -static void blk_mq_debugfs_register_ctx (struct blk_mq_hw_ctx *hctx, +static void blk_mq_debugfs_register_ctx (struct request_queue *q, +struct blk_mq_hw_ctx *hctx,struct blk_mq_ctx *ctx) + __must_hold(&q->debugfs_mutex) {struct dentry *ctx_dir;char name[20 ];
@@ -666,12 +670,12 @@ static void blk_mq_debugfs_register_ctx(struct blk_mq_hw_ctx *hctx,snprintf (name,sizeof (name),"cpu%u" , ctx->cpu); ctx_dir =debugfs_create_dir (name, hctx->debugfs_dir); -debugfs_create_files (hctx->queue, ctx_dir, ctx, - blk_mq_debugfs_ctx_attrs); +debugfs_create_files (q, ctx_dir, ctx, blk_mq_debugfs_ctx_attrs); }void blk_mq_debugfs_register_hctx (struct request_queue *q,struct blk_mq_hw_ctx *hctx) + __must_hold(&q->debugfs_mutex) {struct blk_mq_ctx *ctx;char name[20 ];
@@ -687,7 +691,7 @@ void blk_mq_debugfs_register_hctx(struct request_queue *q, blk_mq_debugfs_hctx_attrs);hctx_for_each_ctx (hctx, ctx, i) -blk_mq_debugfs_register_ctx (hctx, ctx); +blk_mq_debugfs_register_ctx (q, hctx, ctx); }void blk_mq_debugfs_unregister_hctx (struct blk_mq_hw_ctx *hctx)
@@ -721,6 +725,7 @@ void blk_mq_debugfs_unregister_hctxs(struct request_queue *q) }void blk_mq_debugfs_register_sched (struct request_queue *q) + __must_hold(&q->debugfs_mutex) {struct elevator_type *e = q->elevator->type;
@@ -742,6 +747,7 @@ void blk_mq_debugfs_register_sched(struct request_queue *q) }void blk_mq_debugfs_unregister_sched (struct request_queue *q) + __must_hold(&q->debugfs_mutex) {lockdep_assert_held (&q->debugfs_mutex);
@@ -782,6 +788,7 @@ static void blk_mq_debugfs_register_rqos(struct rq_qos *rqos) }void blk_mq_debugfs_register_rq_qos (struct request_queue *q) + __must_hold(&q->debugfs_mutex) {lockdep_assert_held (&q->debugfs_mutex);
@@ -797,6 +804,7 @@ void blk_mq_debugfs_register_rq_qos(struct request_queue *q)void blk_mq_debugfs_register_sched_hctx (struct request_queue *q,struct blk_mq_hw_ctx *hctx) + __must_hold(&q->debugfs_mutex) {struct elevator_type *e = q->elevator->type;
@@ -821,6 +829,7 @@ void blk_mq_debugfs_register_sched_hctx(struct request_queue *q,void blk_mq_debugfs_unregister_sched_hctx (struct request_queue *q,struct blk_mq_hw_ctx *hctx) + __must_hold(&q->debugfs_mutex) {lockdep_assert_held (&q->debugfs_mutex);
block/blk-mq-debugfs.h +25 -9
@@ -2,12 +2,14 @@ #ifndef INT_BLK_MQ_DEBUGFS_H #define INT_BLK_MQ_DEBUGFS_H +#include <linux/blkdev.h> + +struct blk_mq_hw_ctx; + #ifdef CONFIG_BLK_DEBUG_FS #include <linux/seq_file.h> -struct blk_mq_hw_ctx; - struct blk_mq_debugfs_attr { const char *name; umode_t mode;
@@ -20,28 +22,37 @@ struct blk_mq_debugfs_attr {int __blk_mq_debugfs_rq_show (struct seq_file *m,struct request *rq);int blk_mq_debugfs_rq_show (struct seq_file *m,void *v); -void blk_mq_debugfs_register (struct request_queue *q); +void blk_mq_debugfs_register(struct request_queue *q) + __must_hold(&q->debugfs_mutex); void blk_mq_debugfs_register_hctx(struct request_queue *q, -struct blk_mq_hw_ctx *hctx); + struct blk_mq_hw_ctx *hctx) + __must_hold(&q->debugfs_mutex);void blk_mq_debugfs_unregister_hctx (struct blk_mq_hw_ctx *hctx);void blk_mq_debugfs_register_hctxs (struct request_queue *q);void blk_mq_debugfs_unregister_hctxs (struct request_queue *q); -void blk_mq_debugfs_register_sched (struct request_queue *q); -void blk_mq_debugfs_unregister_sched (struct request_queue *q); +void blk_mq_debugfs_register_sched(struct request_queue *q) + __must_hold(&q->debugfs_mutex); +void blk_mq_debugfs_unregister_sched(struct request_queue *q) + __must_hold(&q->debugfs_mutex); void blk_mq_debugfs_register_sched_hctx(struct request_queue *q, -struct blk_mq_hw_ctx *hctx); + struct blk_mq_hw_ctx *hctx) + __must_hold(&q->debugfs_mutex); void blk_mq_debugfs_unregister_sched_hctx(struct request_queue *q, -struct blk_mq_hw_ctx *hctx); + struct blk_mq_hw_ctx *hctx) + __must_hold(&q->debugfs_mutex); -void blk_mq_debugfs_register_rq_qos (struct request_queue *q); +void blk_mq_debugfs_register_rq_qos(struct request_queue *q) + __must_hold(&q->debugfs_mutex);#else static inline void blk_mq_debugfs_register (struct request_queue *q) + __must_hold(&q->debugfs_mutex) { }static inline void blk_mq_debugfs_register_hctx (struct request_queue *q,struct blk_mq_hw_ctx *hctx) + __must_hold(&q->debugfs_mutex) { }
@@ -58,24 +69,29 @@ static inline void blk_mq_debugfs_unregister_hctxs(struct request_queue *q) }static inline void blk_mq_debugfs_register_sched (struct request_queue *q) + __must_hold(&q->debugfs_mutex) { }static inline void blk_mq_debugfs_unregister_sched (struct request_queue *q) + __must_hold(&q->debugfs_mutex) { }static inline void blk_mq_debugfs_register_sched_hctx (struct request_queue *q,struct blk_mq_hw_ctx *hctx) + __must_hold(&q->debugfs_mutex) { }static inline void blk_mq_debugfs_unregister_sched_hctx (struct request_queue *q,struct blk_mq_hw_ctx *hctx) + __must_hold(&q->debugfs_mutex) { }static inline void blk_mq_debugfs_register_rq_qos (struct request_queue *q) + __must_hold(&q->debugfs_mutex) { }
PATCH 28/52 blk-mq-sched: Add lock context annotations
In reply to Bart Van AsscheAnnotate the scheduler resource allocation functions that are called with set->update_nr_hwq_lock held and the scheduler initialization and teardown functions that are called with q->elevator_lock held with __must_hold():
- blk_mq_alloc_sched_ctx_batch()
- blk_mq_alloc_sched_res_batch()
- blk_mq_init_sched()
- blk_mq_exit_sched()
Annotate both the declarations in blk-mq-sched.h and the definitions such that Clang's context analysis verifies all callers, including the callers in block/elevator.c and block/blk-mq.c. Do not annotate blk_mq_free_sched_res_batch() since it has no callers.
--- block/blk-mq-sched.c | 4 ++++ block/blk-mq-sched.h | 12 ++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-)
block/blk-mq-sched.c +4 -0
@@ -486,6 +486,7 @@ void blk_mq_free_sched_ctx_batch(struct xarray *elv_tbl)int blk_mq_alloc_sched_ctx_batch (struct xarray *elv_tbl,struct blk_mq_tag_set *set) + __must_hold(&set->update_nr_hwq_lock) {struct request_queue *q;struct elv_change_ctx *ctx;
@@ -572,6 +573,7 @@ int blk_mq_alloc_sched_res(struct request_queue *q,int blk_mq_alloc_sched_res_batch (struct xarray *elv_tbl,struct blk_mq_tag_set *set,unsigned int nr_hw_queues) + __must_hold(&set->update_nr_hwq_lock) {struct elv_change_ctx *ctx;struct request_queue *q;
@@ -617,6 +619,7 @@ int blk_mq_alloc_sched_res_batch(struct xarray *elv_tbl,/* caller must have a reference to @e, will grab another one if successful */ int blk_mq_init_sched (struct request_queue *q,struct elevator_type *e,struct elevator_resources *res) + __must_hold(&q->elevator_lock) {unsigned int flags = q->tag_set->flags;struct elevator_tags *et = res->et;
@@ -689,6 +692,7 @@ void blk_mq_sched_free_rqs(struct request_queue *q) }void blk_mq_exit_sched (struct request_queue *q,struct elevator_queue *e) + __must_hold(&q->elevator_lock) {struct blk_mq_hw_ctx *hctx;unsigned long i;
block/blk-mq-sched.h +8 -4
@@ -19,8 +19,10 @@ void __blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx);void blk_mq_sched_dispatch_requests (struct blk_mq_hw_ctx *hctx); int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e, -struct elevator_resources *res); -void blk_mq_exit_sched (struct request_queue *q,struct elevator_queue *e); + struct elevator_resources *res) + __must_hold(&q->elevator_lock); +void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e) + __must_hold(&q->elevator_lock);void blk_mq_sched_free_rqs (struct request_queue *q); struct elevator_tags *blk_mq_alloc_sched_tags(struct blk_mq_tag_set *set,
@@ -30,9 +32,11 @@ int blk_mq_alloc_sched_res(struct request_queue *q,struct elevator_resources *res, unsigned int nr_hw_queues); int blk_mq_alloc_sched_res_batch(struct xarray *elv_tbl, -struct blk_mq_tag_set *set,unsigned int nr_hw_queues); + struct blk_mq_tag_set *set, unsigned int nr_hw_queues) + __must_hold(&set->update_nr_hwq_lock); int blk_mq_alloc_sched_ctx_batch(struct xarray *elv_tbl, -struct blk_mq_tag_set *set); + struct blk_mq_tag_set *set) + __must_hold(&set->update_nr_hwq_lock);void blk_mq_free_sched_ctx_batch (struct xarray *elv_tbl);void blk_mq_free_sched_tags (struct elevator_tags *et,struct blk_mq_tag_set *set);
PATCH 29/52 blk-zoned: Add lock context annotations
In reply to Bart Van AsscheAnnotate the zone write plug functions that are called with zwplug->lock held with __must_hold(&zwplug->lock):
- disk_zone_wplug_abort()
- disk_mark_zone_wplug_dead()
- disk_check_zone_wplug_dead()
- disk_zone_wplug_update_cond()
- disk_zone_wplug_set_wp_offset()
- disk_zone_wplug_schedule_work()
- disk_zone_wplug_add_bio()
- blk_zone_wplug_prepare_bio()
Annotate the zone revalidation functions that are called with disk->zone_revalidate_mutex held with __must_hold(&disk->zone_revalidate_mutex):
- disk_revalidate_capacity()
- disk_alloc_zone_resources()
- disk_init_revalidate_args()
- disk_revalidate_zone_resources()
- disk_revalidate_zones()
--- block/blk-zoned.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
block/blk-zoned.c +13 -0
@@ -789,6 +789,7 @@ static inline void blk_zone_wplug_bio_io_error(struct blk_zone_wplug *zwplug,* Abort (fail) all plugged BIOs of a zone write plug. */ static void disk_zone_wplug_abort (struct blk_zone_wplug *zwplug) + __must_hold(&zwplug->lock) {struct gendisk *disk = zwplug->disk;struct bio *bio;
@@ -826,6 +827,7 @@ static void disk_zone_wplug_abort(struct blk_zone_wplug *zwplug)* unhashed when its last reference is dropped. */ static void disk_mark_zone_wplug_dead (struct blk_zone_wplug *zwplug) + __must_hold(&zwplug->lock) {lockdep_assert_held (&zwplug->lock);
@@ -836,6 +838,7 @@ static void disk_mark_zone_wplug_dead(struct blk_zone_wplug *zwplug) }static inline bool disk_check_zone_wplug_dead (struct blk_zone_wplug *zwplug) + __must_hold(&zwplug->lock) {if (disk_zone_wplug_is_offline_or_readonly (zwplug)) {disk_zone_wplug_abort (zwplug);
@@ -931,6 +934,7 @@ static struct blk_zone_wplug *disk_get_or_alloc_zone_wplug(struct gendisk *disk,*/ static void disk_zone_wplug_update_cond (struct gendisk *disk,struct blk_zone_wplug *zwplug) + __must_hold(&zwplug->lock) {lockdep_assert_held (&zwplug->lock);
@@ -951,6 +955,7 @@ static void disk_zone_wplug_update_cond(struct gendisk *disk,static void disk_zone_wplug_set_wp_offset (struct gendisk *disk,struct blk_zone_wplug *zwplug,unsigned int wp_offset) + __must_hold(&zwplug->lock) {lockdep_assert_held (&zwplug->lock);
@@ -1373,6 +1378,7 @@ void blk_zone_mgmt_bio_endio(struct bio *bio)static void disk_zone_wplug_schedule_work (struct gendisk *disk,struct blk_zone_wplug *zwplug) + __must_hold(&zwplug->lock) {lockdep_assert_held (&zwplug->lock);
@@ -1394,6 +1400,7 @@ static void disk_zone_wplug_schedule_work(struct gendisk *disk,static inline void disk_zone_wplug_add_bio (struct gendisk *disk,struct blk_zone_wplug *zwplug,struct bio *bio,unsigned int nr_segs) + __must_hold(&zwplug->lock) {/* * Grab an extra reference on the BIO request queue usage counter.
@@ -1549,6 +1556,7 @@ void blk_zone_write_plug_init_request(struct request *req)*/ static bool blk_zone_wplug_prepare_bio (struct blk_zone_wplug *zwplug,struct bio *bio) + __must_hold(&zwplug->lock) {struct gendisk *disk = bio->bi_bdev->bd_disk;
@@ -2081,6 +2089,7 @@ static unsigned int disk_get_nr_zones(struct gendisk *disk, sector_t capacity)#define BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE 128static int disk_alloc_zone_resources (struct gendisk *disk,sector_t capacity) + __must_hold(&disk->zone_revalidate_mutex) {struct queue_limits *lim = &disk->queue->limits;unsigned int nr_zones, pool_size, i;
@@ -2227,6 +2236,7 @@ struct blk_revalidate_zone_args {static int disk_init_revalidate_args (struct gendisk *disk,struct blk_revalidate_zone_args *args) + __must_hold(&disk->zone_revalidate_mutex) { args->disk = disk; args->nr_zones =disk_get_nr_zones (disk, args->capacity);
@@ -2247,6 +2257,7 @@ static int disk_init_revalidate_args(struct gendisk *disk,*/ static int disk_revalidate_zone_resources (struct gendisk *disk,struct blk_revalidate_zone_args *args) + __must_hold(&disk->zone_revalidate_mutex) {struct request_queue *q = disk->queue;unsigned int nr_seq_zones;
@@ -2354,6 +2365,7 @@ static void disk_drop_zone_wplug(struct blk_zone_wplug *zwplug, void *data)static int disk_revalidate_capacity (struct gendisk *disk,struct blk_revalidate_zone_args *args) + __must_hold(&disk->zone_revalidate_mutex) {struct queue_limits *lim = &disk->queue->limits;sector_t zone_sectors = lim->chunk_sectors;
@@ -2561,6 +2573,7 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx, }static int disk_revalidate_zones (struct gendisk *disk,bool drop_all_zwplugs) + __must_hold(&disk->zone_revalidate_mutex) {struct blk_revalidate_zone_args args = { };struct blk_report_zones_args rep_args = {
Damien Le Moal
maintainerRe: [PATCH 29/52] blk-zoned: Add lock context annotations
Quoted 21 lines · Bart Van Assche
Annotate the zone write plug functions that are called with
zwplug->lockheld with__must_hold(&zwplug->lock):
-disk_zone_wplug_abort()
-disk_mark_zone_wplug_dead()
-disk_check_zone_wplug_dead()
-disk_zone_wplug_update_cond()
-disk_zone_wplug_set_wp_offset()
-disk_zone_wplug_schedule_work()
-disk_zone_wplug_add_bio()
-blk_zone_wplug_prepare_bio()Annotate the zone revalidation functions that are called with
disk->zone_revalidate_mutexheld with__must_hold(&disk->zone_revalidate_mutex):
-disk_revalidate_capacity()
-disk_alloc_zone_resources()
-disk_init_revalidate_args()
-disk_revalidate_zone_resources()
-disk_revalidate_zones()Signed-off-by Bart Van Assche <[email protected]>
[...]
block/blk-zoned.c static void disk_zone_wplug_abort(struct blk_zone_wplug *zwplug) · line 832
Quoted 7 lines
block/blk-zoned.c +1
-0@@ -826,6 +827,7 @@ static void disk_zone_wplug_abort(struct blk_zone_wplug *zwplug)* unhashed when its last reference is dropped. */ static void disk_mark_zone_wplug_dead (struct blk_zone_wplug *zwplug) + __must_hold(&zwplug->lock) {lockdep_assert_held (&zwplug->lock);
Since we already have this assert, what does the __must_hold() do exactly?
Signature · 2 lines
Damien Le Moal Western Digital Research
PATCH 30/52 blk-zoned: Annotate struct blk_zone_wplug members with __guarded_by()
In reply to Bart Van AsscheThe bio_list, flags, wp_offset and cond members of struct blk_zone_wplug are protected by the zone write plug spinlock. Annotate these members with __guarded_by() such that Clang's context analysis can verify that the lock is held when these members are accessed.
Add __must_hold() annotations to disk_zone_wplug_is_full() and disk_zone_wplug_is_offline_or_readonly() since all callers hold the zone write plug lock. Use scoped_guard(spinlock_init, ...) to initialize the guarded members of a newly allocated zone write plug. Use context_unsafe() for setting the zone condition before the zone write plug is published in the hash table and for the accesses in disk_free_zone_wplug(), which runs when the last reference has been dropped and may be called either with or without zwplug->lock held.
--- block/blk-zoned.c | 49 +++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 17 deletions(-)
block/blk-zoned.c +32 -17
@@ -165,16 +165,16 @@ static inline bool blk_zstate_is_conv(enum blk_zstate zs)struct blk_zone_wplug {struct hlist_node node;struct list_head entry; -struct bio_list bio_list; + struct bio_list bio_list __guarded_by(&lock);struct work_struct bio_work;struct rcu_head rcu_head;struct gendisk *disk;spinlock_t lock;refcount_t ref; -unsigned int flags; + unsigned int flags __guarded_by(&lock);unsigned int zone_no; -unsigned int wp_offset; -enum blk_zone_cond cond; + unsigned int wp_offset __guarded_by(&lock); + enum blk_zone_cond cond __guarded_by(&lock); };static inline bool disk_need_zone_resources (struct gendisk *disk)
@@ -633,6 +633,7 @@ static bool disk_zone_is_last(struct gendisk *disk, struct blk_zone *zone)static bool disk_zone_wplug_is_full (struct gendisk *disk,struct blk_zone_wplug *zwplug) + __must_hold(&zwplug->lock) {if (zwplug->zone_no < disk->nr_zones -1 )return zwplug->wp_offset >= disk->zone_capacity;
@@ -640,6 +641,7 @@ static bool disk_zone_wplug_is_full(struct gendisk *disk, }static bool disk_zone_wplug_is_offline_or_readonly (struct blk_zone_wplug *zwplug) + __must_hold(&zwplug->lock) {return disk_zone_cond_is_offline_or_readonly (zwplug->cond); }
@@ -675,11 +677,10 @@ static bool disk_insert_zone_wplug(struct gendisk *disk,*/ zones_state =rcu_dereference_check (disk->zones_state,lockdep_is_held (&disk->zone_wplugs_hash_lock)); -if (zones_state) - zwplug->cond = -blk_zstate_to_zone_cond (zones_state[zwplug->zone_no]); -else - zwplug->cond =BLK_ZONE_COND_ACTIVE ; +/* Not yet published in zone_wplugs_hash, so zwplug->lock is unneeded. */ +context_unsafe (zwplug->cond = zones_state ? +blk_zstate_to_zone_cond (zones_state[zwplug->zone_no]) : +BLK_ZONE_COND_ACTIVE );hlist_add_head_rcu (&zwplug->node, &disk->zone_wplugs_hash[idx]);atomic_inc (&disk->nr_zone_wplugs);
@@ -751,15 +752,27 @@ static void disk_free_zone_wplug(struct blk_zone_wplug *zwplug) {struct gendisk *disk = zwplug->disk;unsigned long flags; +unsigned int zwp_flags; +bool empty; -WARN_ON_ONCE (!(zwplug->flags &BLK_ZONE_WPLUG_DEAD )); -WARN_ON_ONCE (zwplug->flags &BLK_ZONE_WPLUG_PLUGGED ); -WARN_ON_ONCE (!bio_list_empty (&zwplug->bio_list)); +/* +* The last reference has been dropped and disk_free_zone_wplug() may +* be called either with or without zwplug->lock held, so zwplug->lock +* cannot be acquired here. +*/ + zwp_flags =context_unsafe (zwplug->flags); +/* Same as above: zwplug->lock may or may not be held. */ + empty =context_unsafe (bio_list_empty (&zwplug->bio_list)); +WARN_ON_ONCE (!(zwp_flags &BLK_ZONE_WPLUG_DEAD )); +WARN_ON_ONCE (zwp_flags &BLK_ZONE_WPLUG_PLUGGED ); +WARN_ON_ONCE (!empty);spin_lock_irqsave (&disk->zone_wplugs_hash_lock, flags);blk_zstate_set (rcu_dereference_check (disk->zones_state,lockdep_is_held (&disk->zone_wplugs_hash_lock)), - disk->nr_zones, zwplug->zone_no, zwplug->cond,0 ); + disk->nr_zones, zwplug->zone_no, +/* May be called with or without zwplug->lock held. */ +context_unsafe (zwplug->cond),0 );hlist_del_init_rcu (&zwplug->node);atomic_dec (&disk->nr_zone_wplugs);spin_unlock_irqrestore (&disk->zone_wplugs_hash_lock, flags);
@@ -907,11 +920,13 @@ static struct blk_zone_wplug *disk_get_or_alloc_zone_wplug(struct gendisk *disk,INIT_HLIST_NODE (&zwplug->node);refcount_set (&zwplug->ref,2 ); -spin_lock_init (&zwplug->lock); - zwplug->flags =0 ; + scoped_guard(spinlock_init, &zwplug->lock) { + zwplug->flags =0 ; + zwplug->wp_offset = +bdev_offset_from_zone_start (disk->part0, sector); +bio_list_init (&zwplug->bio_list); + } zwplug->zone_no = zno; - zwplug->wp_offset =bdev_offset_from_zone_start (disk->part0, sector); -bio_list_init (&zwplug->bio_list);INIT_WORK (&zwplug->bio_work, blk_zone_wplug_bio_work);INIT_LIST_HEAD (&zwplug->entry); zwplug->disk = disk;
Damien Le Moal
maintainerRe: [PATCH 30/52] blk-zoned: Annotate struct blk_zone_wplug members with __guarded_by()
Quoted 81 lines · Bart Van Assche
The
bio_list, flags,wp_offsetand cond members of structblk_zone_wplugare protected by the zone write plug spinlock. Annotate these members with__guarded_by()such that Clang's context analysis can verify that the lock is held when these members are accessed.Add
__must_hold()annotations todisk_zone_wplug_is_full()anddisk_zone_wplug_is_offline_or_readonly()since all callers hold the zone write plug lock. Usescoped_guard(spinlock_init, ...)to initialize the guarded members of a newly allocated zone write plug. Usecontext_unsafe()for setting the zone condition before the zone write plug is published in the hash table and for the accesses indisk_free_zone_wplug(), which runs when the last reference has been dropped and may be called either with or withoutzwplug->lockheld.Signed-off-by Bart Van Assche <[email protected]>--- block/blk-zoned.c | 49 +++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 17 deletions(-)block/blk-zoned.c +11
-9@@ -165,16 +165,16 @@ static inline bool blk_zstate_is_conv(enum blk_zstate zs)struct blk_zone_wplug {struct hlist_node node;struct list_head entry; -struct bio_list bio_list; + struct bio_list bio_list __guarded_by(&lock);struct work_struct bio_work;struct rcu_head rcu_head;struct gendisk *disk;spinlock_t lock;refcount_t ref; -unsigned int flags; + unsigned int flags __guarded_by(&lock);unsigned int zone_no; -unsigned int wp_offset; -enum blk_zone_cond cond; + unsigned int wp_offset __guarded_by(&lock); + enum blk_zone_cond cond __guarded_by(&lock); };static inline bool disk_need_zone_resources (struct gendisk *disk)@@ -633,6 +633,7 @@ static bool disk_zone_is_last(struct gendisk *disk, struct blk_zone *zone)static bool disk_zone_wplug_is_full (struct gendisk *disk,struct blk_zone_wplug *zwplug) + __must_hold(&zwplug->lock) {if (zwplug->zone_no < disk->nr_zones -1 )return zwplug->wp_offset >= disk->zone_capacity;@@ -640,6 +641,7 @@ static bool disk_zone_wplug_is_full(struct gendisk *disk, }static bool disk_zone_wplug_is_offline_or_readonly (struct blk_zone_wplug *zwplug) + __must_hold(&zwplug->lock) {return disk_zone_cond_is_offline_or_readonly (zwplug->cond); }@@ -675,11 +677,10 @@ static bool disk_insert_zone_wplug(struct gendisk *disk,*/ zones_state =rcu_dereference_check (disk->zones_state,lockdep_is_held (&disk->zone_wplugs_hash_lock)); -if (zones_state) - zwplug->cond = -blk_zstate_to_zone_cond (zones_state[zwplug->zone_no]); -else - zwplug->cond =BLK_ZONE_COND_ACTIVE ; +/* Not yet published in zone_wplugs_hash, so zwplug->lock is unneeded. */ +context_unsafe (zwplug->cond = zones_state ? +blk_zstate_to_zone_cond (zones_state[zwplug->zone_no]) : +BLK_ZONE_COND_ACTIVE );hlist_add_head_rcu (&zwplug->node, &disk->zone_wplugs_hash[idx]);atomic_inc (&disk->nr_zone_wplugs);@@ -751,15 +752,27 @@ static void disk_free_zone_wplug(struct blk_zone_wplug *zwplug) {struct gendisk *disk = zwplug->disk;unsigned long flags; +unsigned int zwp_flags;
block/blk-zoned.c static void disk_free_zone_wplug(struct blk_zone_wplug *zwplug) · line 762
Quoted 10 lines
block/blk-zoned.c +6
-3+bool empty; -WARN_ON_ONCE (!(zwplug->flags &BLK_ZONE_WPLUG_DEAD )); -WARN_ON_ONCE (zwplug->flags &BLK_ZONE_WPLUG_PLUGGED ); -WARN_ON_ONCE (!bio_list_empty (&zwplug->bio_list)); +/* +* The last reference has been dropped and disk_free_zone_wplug() may +* be called either with or without zwplug->lock held, so zwplug->lock +* cannot be acquired here. +*/
Calling disk_free_zone_wplug() with the zwplug spinlock still held would be a really big problem since that potentially could result in a use after free. So what path are you talking about here?
This entire annotation thing makes the code really ugly...
Quoted 35 lines
block/blk-zoned.c +15
-5+ zwp_flags =context_unsafe (zwplug->flags); +/* Same as above: zwplug->lock may or may not be held. */ + empty =context_unsafe (bio_list_empty (&zwplug->bio_list)); +WARN_ON_ONCE (!(zwp_flags &BLK_ZONE_WPLUG_DEAD )); +WARN_ON_ONCE (zwp_flags &BLK_ZONE_WPLUG_PLUGGED ); +WARN_ON_ONCE (!empty);spin_lock_irqsave (&disk->zone_wplugs_hash_lock, flags);blk_zstate_set (rcu_dereference_check (disk->zones_state,lockdep_is_held (&disk->zone_wplugs_hash_lock)), - disk->nr_zones, zwplug->zone_no, zwplug->cond,0 ); + disk->nr_zones, zwplug->zone_no, +/* May be called with or without zwplug->lock held. */ +context_unsafe (zwplug->cond),0 );hlist_del_init_rcu (&zwplug->node);atomic_dec (&disk->nr_zone_wplugs);spin_unlock_irqrestore (&disk->zone_wplugs_hash_lock, flags);@@ -907,11 +920,13 @@ static struct blk_zone_wplug *disk_get_or_alloc_zone_wplug(struct gendisk *disk,INIT_HLIST_NODE (&zwplug->node);refcount_set (&zwplug->ref,2 ); -spin_lock_init (&zwplug->lock); - zwplug->flags =0 ; + scoped_guard(spinlock_init, &zwplug->lock) { + zwplug->flags =0 ; + zwplug->wp_offset = +bdev_offset_from_zone_start (disk->part0, sector); +bio_list_init (&zwplug->bio_list); + } zwplug->zone_no = zno; - zwplug->wp_offset =bdev_offset_from_zone_start (disk->part0, sector); -bio_list_init (&zwplug->bio_list);INIT_WORK (&zwplug->bio_work, blk_zone_wplug_bio_work);INIT_LIST_HEAD (&zwplug->entry); zwplug->disk = disk;
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH 30/52] blk-zoned: Annotate struct blk_zone_wplug members with __guarded_by()
Quoted 5 lines · Damien Le Moal
Calling
disk_free_zone_wplug()with the zwplug spinlock still held would be a really big problem since that potentially could result in a use after free. So what path are you talking about here?This entire annotation thing makes the code really ugly...
Well, part of it is having to explain the locking rules, and if they are complicated that usually shows real problems.
The other is the whole scope_guarnd and unsafe_context junk, which really needs to be replaced with something sane before we can use these annotations without turning the code into cat barf.
Mostly due to the latter I've largely given up on trying to use this otherwise extremely useful feature.
Damien Le Moal
maintainerRe: [PATCH 30/52] blk-zoned: Annotate struct blk_zone_wplug members with __guarded_by()
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Damien Le Moal
Calling
disk_free_zone_wplug()with the zwplug spinlock still held would be a really big problem since that potentially could result in a use after free. So what path are you talking about here?This entire annotation thing makes the code really ugly...
Well, part of it is having to explain the locking rules, and if they are complicated that usually shows real problems.
Quoted 6 lines
The other is the whole
scope_guarndandunsafe_contextjunk, which really needs to be replaced with something sane before we can use these annotations without turning the code into cat barf.Mostly due to the latter I've largely given up on trying to use this otherwise extremely useful feature.
OK. But my point about the comment saying that disk_free_zone_wplug() may be called with or without the spinlock held stands though: that is not correct and the annotation should reflect that the lock must *not* be held instead of saying "maybe". So we need a "__must_not_hold()" or similar.
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH 30/52] blk-zoned: Annotate struct blk_zone_wplug members with __guarded_by()
Quoted 2 lines · Damien Le Moal
the annotation should reflect that the lock must *not* be held instead of saying "maybe". So we need a "
__must_not_hold()" or similar.
__must_not_hold exists.
PATCH 31/52 block: Pass the gendisk pointer explicitly to several bdev.c functions
In reply to Bart Van AsschePass a pointer to struct gendisk explicitly to set_init_blocksize(), blkdev_flush_mapping(), blkdev_put_whole(), blkdev_get_whole(), blkdev_get_part(), blkdev_put_part(), bdev_unblock_writes(), bdev_unclaimed(), bdev_yield_write_access() and bd_yield_claim(). All callers of these functions hold disk->open_mutex.
This change prepares for annotating these functions with __must_hold(&disk->open_mutex). Clang's context analysis cannot determine that expressions like bdev_whole(part)->bd_disk->open_mutex or file_bdev(bdev_file)->bd_disk->open_mutex refer to the same mutex as disk->open_mutex. The new argument is not yet used by set_init_blocksize(), bdev_unblock_writes() and bdev_unclaimed(). It will be used by the lock context annotations.
No functional change is intended since the gendisk pointer passed by all callers equals bdev->bd_disk.
--- block/bdev.c | 69 +++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 33 deletions(-)
block/bdev.c +36 -33
@@ -136,7 +136,7 @@ int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode, lend >> PAGE_SHIFT); } -static void set_init_blocksize (struct block_device *bdev) +static void set_init_blocksize (struct gendisk *disk,struct block_device *bdev) {unsigned int bsize =bdev_logical_block_size (bdev);loff_t size =i_size_read (BD_INODE (bdev));
@@ -750,7 +750,8 @@ static void bd_end_claim(struct block_device *bdev, void *holder) } } -static void blkdev_flush_mapping (struct block_device *bdev) +static void blkdev_flush_mapping (struct gendisk *disk, +struct block_device *bdev) {WARN_ON_ONCE (bdev->bd_holders);sync_blockdev (bdev);
@@ -758,17 +759,17 @@ static void blkdev_flush_mapping(struct block_device *bdev)bdev_write_inode (bdev); } -static void blkdev_put_whole (struct block_device *bdev) +static void blkdev_put_whole (struct gendisk *disk,struct block_device *bdev) {if (atomic_dec_and_test (&bdev->bd_openers)) -blkdev_flush_mapping (bdev); -if (bdev->bd_disk->fops->release) - bdev->bd_disk->fops->release (bdev->bd_disk); +blkdev_flush_mapping (disk, bdev); +if (disk->fops->release) + disk->fops->release (disk); } -static int blkdev_get_whole (struct block_device *bdev,blk_mode_t mode) +static int blkdev_get_whole (struct gendisk *disk,struct block_device *bdev, +blk_mode_t mode) { -struct gendisk *disk = bdev->bd_disk;int ret;if (disk->fops->open) {
@@ -783,7 +784,7 @@ static int blkdev_get_whole(struct block_device *bdev, blk_mode_t mode) }if (!atomic_read (&bdev->bd_openers)) -set_init_blocksize (bdev); +set_init_blocksize (disk, bdev);atomic_inc (&bdev->bd_openers);if (test_bit (GD_NEED_PART_SCAN , &disk->state)) {/*
@@ -792,19 +793,19 @@ static int blkdev_get_whole(struct block_device *bdev, blk_mode_t mode)*/ ret =bdev_disk_changed (disk, false);if (ret && (mode &BLK_OPEN_STRICT_SCAN )) { -blkdev_put_whole (bdev); +blkdev_put_whole (disk, bdev);return ret; } }return 0 ; } -static int blkdev_get_part (struct block_device *part,blk_mode_t mode) +static int blkdev_get_part (struct gendisk *disk,struct block_device *part, +blk_mode_t mode) { -struct gendisk *disk = part->bd_disk;int ret; - ret =blkdev_get_whole (bdev_whole (part), mode); + ret =blkdev_get_whole (disk,bdev_whole (part), mode);if (ret)return ret;
@@ -814,13 +815,13 @@ static int blkdev_get_part(struct block_device *part, blk_mode_t mode)if (!atomic_read (&part->bd_openers)) { disk->open_partitions++; -set_init_blocksize (part); +set_init_blocksize (disk, part); }atomic_inc (&part->bd_openers);return 0 ; out_blkdev_put: -blkdev_put_whole (bdev_whole (part)); +blkdev_put_whole (disk,bdev_whole (part));return ret; }
@@ -849,15 +850,15 @@ int bdev_permission(dev_t dev, blk_mode_t mode, void *holder)return 0 ; } -static void blkdev_put_part (struct block_device *part) +static void blkdev_put_part (struct gendisk *disk,struct block_device *part) {struct block_device *whole =bdev_whole (part);if (atomic_dec_and_test (&part->bd_openers)) { -blkdev_flush_mapping (part); +blkdev_flush_mapping (disk, part); whole->bd_disk->open_partitions--; } -blkdev_put_whole (whole); +blkdev_put_whole (disk, whole); }struct block_device *blkdev_get_no_open (dev_t dev,bool autoload)
@@ -899,7 +900,7 @@ static void bdev_block_writes(struct block_device *bdev) bdev->bd_writers--; } -static void bdev_unblock_writes (struct block_device *bdev) +static void bdev_unblock_writes (struct gendisk *disk,struct block_device *bdev) { bdev->bd_writers++; }
@@ -928,25 +929,27 @@ static void bdev_claim_write_access(struct block_device *bdev, blk_mode_t mode) bdev->bd_writers++; } -static inline bool bdev_unclaimed (const struct file *bdev_file) +static inline bool bdev_unclaimed (struct gendisk *disk, +const struct file *bdev_file) {return bdev_file->private_data ==BDEV_I (bdev_file->f_mapping->host); } -static void bdev_yield_write_access (struct file *bdev_file) +static void bdev_yield_write_access (struct gendisk *disk, +struct file *bdev_file) {struct block_device *bdev;if (bdev_allow_write_mounted)return ; -if (bdev_unclaimed (bdev_file)) +if (bdev_unclaimed (disk, bdev_file))return ; bdev =file_bdev (bdev_file);if (bdev_file->f_mode &FMODE_WRITE_RESTRICTED ) -bdev_unblock_writes (bdev); +bdev_unblock_writes (disk, bdev);else if (bdev_file->f_mode &FMODE_WRITE ) bdev->bd_writers--; }
@@ -997,9 +1000,9 @@ int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder,if (!bdev_may_open (bdev, mode)) goto put_module;if (bdev_is_partition (bdev)) - ret =blkdev_get_part (bdev, mode); + ret =blkdev_get_part (disk, bdev, mode);else - ret =blkdev_get_whole (bdev, mode); + ret =blkdev_get_whole (disk, bdev, mode);if (ret) goto put_module;bdev_claim_write_access (bdev, mode);
@@ -1136,7 +1139,7 @@ struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode, }EXPORT_SYMBOL (bdev_file_open_by_path); -static inline void bd_yield_claim (struct file *bdev_file) +static inline void bd_yield_claim (struct gendisk *disk,struct file *bdev_file) {struct block_device *bdev =file_bdev (bdev_file);void *holder = bdev_file->private_data;
@@ -1146,7 +1149,7 @@ static inline void bd_yield_claim(struct file *bdev_file)if (WARN_ON_ONCE (IS_ERR_OR_NULL (holder)))return ; -if (!bdev_unclaimed (bdev_file)) +if (!bdev_unclaimed (disk, bdev_file))bd_end_claim (bdev, holder); }
@@ -1171,10 +1174,10 @@ void bdev_release(struct file *bdev_file)sync_blockdev (bdev);mutex_lock (&disk->open_mutex); -bdev_yield_write_access (bdev_file); +bdev_yield_write_access (disk, bdev_file);if (holder) -bd_yield_claim (bdev_file); +bd_yield_claim (disk, bdev_file);/* * Trigger event checking and tell drivers to flush MEDIA_CHANGE
@@ -1184,9 +1187,9 @@ void bdev_release(struct file *bdev_file)disk_flush_events (disk,DISK_EVENT_MEDIA_CHANGE );if (bdev_is_partition (bdev)) -blkdev_put_part (bdev); +blkdev_put_part (disk, bdev);else -blkdev_put_whole (bdev); +blkdev_put_whole (disk, bdev);mutex_unlock (&disk->open_mutex);module_put (disk->fops->owner);
@@ -1215,8 +1218,8 @@ void bdev_yield_claim(struct file *bdev_file) disk = bdev->bd_disk;mutex_lock (&disk->open_mutex); -bdev_yield_write_access (bdev_file); -bd_yield_claim (bdev_file); +bdev_yield_write_access (disk, bdev_file); +bd_yield_claim (disk, bdev_file);/* * Tell release we already gave up our hold on the * device and if write restrictions are available that
PATCH 32/52 block: Add __must_hold() annotations in block/bdev.c
In reply to Bart Van AsscheAnnotate the static functions in block/bdev.c that require disk->open_mutex to be held by their callers with __must_hold(&disk->open_mutex). Use __must_hold(&bdev->bd_disk->open_mutex) for the functions that do not have a gendisk argument:bd_finish_claiming(), bd_end_claim(), bdev_writes_blocked(), bdev_block_writes(), bdev_may_open() and bdev_claim_write_access(). This enables Clang's context analysis to verify that disk->open_mutex is held by all callers.
--- block/bdev.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
block/bdev.c +16 -0
@@ -137,6 +137,7 @@ int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode, }static void set_init_blocksize (struct gendisk *disk,struct block_device *bdev) + __must_hold(&disk->open_mutex) {unsigned int bsize =bdev_logical_block_size (bdev);loff_t size =i_size_read (BD_INODE (bdev));
@@ -678,6 +679,7 @@ static void bd_clear_claiming(struct block_device *whole, void *holder)*/ static void bd_finish_claiming (struct block_device *bdev,void *holder,const struct blk_holder_ops *hops) + __must_hold(&bdev->bd_disk->open_mutex) {struct block_device *whole =bdev_whole (bdev);
@@ -716,6 +718,7 @@ void bd_abort_claiming(struct block_device *bdev, void *holder)EXPORT_SYMBOL (bd_abort_claiming);static void bd_end_claim (struct block_device *bdev,void *holder) + __must_hold(&bdev->bd_disk->open_mutex) {struct block_device *whole =bdev_whole (bdev);bool unblock = false;
@@ -752,6 +755,7 @@ static void bd_end_claim(struct block_device *bdev, void *holder)static void blkdev_flush_mapping (struct gendisk *disk,struct block_device *bdev) + __must_hold(&disk->open_mutex) {WARN_ON_ONCE (bdev->bd_holders);sync_blockdev (bdev);
@@ -760,6 +764,7 @@ static void blkdev_flush_mapping(struct gendisk *disk, }static void blkdev_put_whole (struct gendisk *disk,struct block_device *bdev) + __must_hold(&disk->open_mutex) {if (atomic_dec_and_test (&bdev->bd_openers))blkdev_flush_mapping (disk, bdev);
@@ -769,6 +774,7 @@ static void blkdev_put_whole(struct gendisk *disk, struct block_device *bdev)static int blkdev_get_whole (struct gendisk *disk,struct block_device *bdev,blk_mode_t mode) + __must_hold(&disk->open_mutex) {int ret;
@@ -802,6 +808,7 @@ static int blkdev_get_whole(struct gendisk *disk, struct block_device *bdev,static int blkdev_get_part (struct gendisk *disk,struct block_device *part,blk_mode_t mode) + __must_hold(&disk->open_mutex) {int ret;
@@ -851,6 +858,7 @@ int bdev_permission(dev_t dev, blk_mode_t mode, void *holder) }static void blkdev_put_part (struct gendisk *disk,struct block_device *part) + __must_hold(&disk->open_mutex) {struct block_device *whole =bdev_whole (part);
@@ -891,21 +899,25 @@ void blkdev_put_no_open(struct block_device *bdev) }static bool bdev_writes_blocked (struct block_device *bdev) + __must_hold(&bdev->bd_disk->open_mutex) {return bdev->bd_writers <0 ; }static void bdev_block_writes (struct block_device *bdev) + __must_hold(&bdev->bd_disk->open_mutex) { bdev->bd_writers--; }static void bdev_unblock_writes (struct gendisk *disk,struct block_device *bdev) + __must_hold(&disk->open_mutex) { bdev->bd_writers++; }static bool bdev_may_open (struct block_device *bdev,blk_mode_t mode) + __must_hold(&bdev->bd_disk->open_mutex) {if (bdev_allow_write_mounted)return true;
@@ -918,6 +930,7 @@ static bool bdev_may_open(struct block_device *bdev, blk_mode_t mode) }static void bdev_claim_write_access (struct block_device *bdev,blk_mode_t mode) + __must_hold(&bdev->bd_disk->open_mutex) {if (bdev_allow_write_mounted)return ;
@@ -931,12 +944,14 @@ static void bdev_claim_write_access(struct block_device *bdev, blk_mode_t mode)static inline bool bdev_unclaimed (struct gendisk *disk,const struct file *bdev_file) + __must_hold(&disk->open_mutex) {return bdev_file->private_data ==BDEV_I (bdev_file->f_mapping->host); }static void bdev_yield_write_access (struct gendisk *disk,struct file *bdev_file) + __must_hold(&disk->open_mutex) {struct block_device *bdev;
@@ -1140,6 +1155,7 @@ struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode,EXPORT_SYMBOL (bdev_file_open_by_path);static inline void bd_yield_claim (struct gendisk *disk,struct file *bdev_file) + __must_hold(&disk->open_mutex) {struct block_device *bdev =file_bdev (bdev_file);void *holder = bdev_file->private_data;
PATCH 33/52 block: Annotate the block_device_operations open and release callbacks
In reply to Bart Van AsscheThe block layer calls the ->open() and ->release() callbacks of struct block_device_operations with disk->open_mutex held, from blkdev_get_whole() and blkdev_put_whole() respectively. Document this requirement by annotating both callback declarations with __must_hold(&disk->open_mutex). This requirement is also documented in Documentation/filesystems/locking.rst.
Clang's context analysis verifies this annotation at the two call sites in block/bdev.c. The ->open() and ->release() implementations are not checked against it since Clang does not propagate function pointer annotations to the functions that are assigned to these pointers.
--- include/linux/blkdev.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/blkdev.h +4 -2
@@ -1639,8 +1639,10 @@ struct block_device_operations {void (*submit_bio)(struct bio *bio);int (*poll_bio)(struct bio *bio,struct io_comp_batch *iob,unsigned int flags); -int (*open)(struct gendisk *disk,blk_mode_t mode); -void (*release)(struct gendisk *disk); + int (*open)(struct gendisk *disk, blk_mode_t mode) + __must_hold(&disk->open_mutex); + void (*release)(struct gendisk *disk) + __must_hold(&disk->open_mutex);int (*ioctl)(struct block_device *bdev,blk_mode_t mode,unsigned cmd,unsigned long arg); int (*compat_ioctl)(struct block_device *bdev, blk_mode_t mode,
PATCH 34/52 block: Add __must_hold() annotations for bdev_lock
In reply to Bart Van Asschebd_may_claim() and bd_clear_claiming() must be called with bdev_lock held. This is already verified at runtime with lockdep_assert_held(). Additionally annotate both functions with __must_hold(&bdev_lock) such that Clang verifies at compile time that all callers hold bdev_lock.
--- block/bdev.c | 2 ++ 1 file changed, 2 insertions(+)
block/bdev.c +2 -0
@@ -585,6 +585,7 @@ long nr_blockdev_pages(void)*/ static bool bd_may_claim (struct block_device *bdev,void *holder,const struct blk_holder_ops *hops) + __must_hold(&bdev_lock) {struct block_device *whole =bdev_whole (bdev);
@@ -660,6 +661,7 @@ int bd_prepare_to_claim(struct block_device *bdev, void *holder,EXPORT_SYMBOL_GPL (bd_prepare_to_claim);/* only for the loop driver */ static void bd_clear_claiming (struct block_device *whole,void *holder) + __must_hold(&bdev_lock) {lockdep_assert_held (&bdev_lock);/* tell others that we're done */
PATCH 35/52 block: Add lock context annotations in block/elevator.c
In reply to Bart Van AsscheAnnotate __elevator_find() with __must_hold(&elv_list_lock) since it iterates over the elv_list and since all callers hold elv_list_lock.
Annotate elevator_exit() and elevator_switch() with __must_hold(&q->elevator_lock). Both functions already verify this with lockdep_assert_held().
--- block/elevator.c | 3 +++ 1 file changed, 3 insertions(+)
block/elevator.c +3 -0
@@ -97,6 +97,7 @@ static bool elevator_match(const struct elevator_type *e, const char *name) }static struct elevator_type *__elevator_find (const char *name) + __must_hold(&elv_list_lock) {struct elevator_type *e;
@@ -150,6 +151,7 @@ static void elevator_release(struct kobject *kobj) }static void elevator_exit (struct request_queue *q) + __must_hold(&q->elevator_lock) {struct elevator_queue *e = q->elevator;
@@ -560,6 +562,7 @@ EXPORT_SYMBOL_GPL(elv_unregister);* to restore the old io scheduler, so leaving the io scheduler being none. */ static int elevator_switch (struct request_queue *q,struct elv_change_ctx *ctx) + __must_hold(&q->elevator_lock) {struct elevator_type *new_e =NULL ;int ret =0 ;
PATCH 36/52 block: Annotate elv_list with LIST_HEAD_GUARDED()
In reply to Bart Van AsscheAll accesses of the global elv_list happen with elv_list_lock held. Define elv_list using LIST_HEAD_GUARDED() so that Clang's context analysis can verify that elv_list_lock is held on every access.
--- block/elevator.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/elevator.c +1 -1
@@ -46,7 +46,7 @@#include "blk-cgroup.h" static DEFINE_SPINLOCK (elv_list_lock); -static LIST_HEAD (elv_list); +static LIST_HEAD_GUARDED(elv_list, elv_list_lock);/* * Merge hash stuff.
PATCH 37/52 block: Annotate the queue_limits_*() definitions in blk-settings.c
In reply to Bart Van AsscheThe declarations of queue_limits_commit_update() and queue_limits_commit_update_frozen() in <linux/blkdev.h> have a __releases(&q->limits_lock) annotation since these functions release the lock acquired by queue_limits_start_update(), and the declaration of queue_limits_set() has a __must_not_hold(&q->limits_lock) annotation. Add the same annotations to the definitions of these functions such that the declarations and the definitions are consistent.
--- block/blk-settings.c | 3 +++ 1 file changed, 3 insertions(+)
block/blk-settings.c +3 -0
@@ -552,6 +552,7 @@ int blk_set_default_limits(struct queue_limits *lim)*/ int queue_limits_commit_update (struct request_queue *q,struct queue_limits *lim) + __releases(&q->limits_lock) {int error;
@@ -591,6 +592,7 @@ EXPORT_SYMBOL_GPL(queue_limits_commit_update);*/ int queue_limits_commit_update_frozen (struct request_queue *q,struct queue_limits *lim) + __releases(&q->limits_lock) {unsigned int memflags;int ret;
@@ -615,6 +617,7 @@ EXPORT_SYMBOL_GPL(queue_limits_commit_update_frozen);* Returns 0 if successful, else a negative error code. */ int queue_limits_set (struct request_queue *q,struct queue_limits *lim) + __must_not_hold(&q->limits_lock) {mutex_lock (&q->limits_lock);return queue_limits_commit_update (q, lim);
PATCH 38/52 block: Add __must_hold() annotations for the IA ranges sysfs functions
In reply to Bart Van Asschedisk_register_independent_access_ranges() and disk_unregister_independent_access_ranges() must be called with q->sysfs_lock held. This is already verified at runtime with lockdep_assert_held(). Additionally annotate both the declarations in block/blk.h and the definitions with __must_hold(&disk->queue->sysfs_lock) such that Clang verifies at compile time that all callers hold that lock.
--- block/blk-ia-ranges.c | 2 ++ block/blk.h | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-)
block/blk-ia-ranges.c +2 -0
@@ -106,6 +106,7 @@ static const struct kobj_type blk_ia_ranges_ktype = {* Register with sysfs a set of independent access ranges for @disk. */ int disk_register_independent_access_ranges (struct gendisk *disk) + __must_hold(&disk->queue->sysfs_lock) {struct blk_independent_access_ranges *iars = disk->ia_ranges;struct request_queue *q = disk->queue;
@@ -149,6 +150,7 @@ int disk_register_independent_access_ranges(struct gendisk *disk) }void disk_unregister_independent_access_ranges (struct gendisk *disk) + __must_hold(&disk->queue->sysfs_lock) {struct request_queue *q = disk->queue;struct blk_independent_access_ranges *iars = disk->ia_ranges;
block/blk.h +4 -2
@@ -711,8 +711,10 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg);extern const struct address_space_operations def_blk_aops; -int disk_register_independent_access_ranges (struct gendisk *disk); -void disk_unregister_independent_access_ranges (struct gendisk *disk); +int disk_register_independent_access_ranges(struct gendisk *disk) + __must_hold(&disk->queue->sysfs_lock); +void disk_unregister_independent_access_ranges(struct gendisk *disk) + __must_hold(&disk->queue->sysfs_lock);int should_fail_bio (struct bio *bio);#ifdef CONFIG_FAIL_MAKE_REQUEST
PATCH 39/52 block: Add a __must_hold() annotation for blk_holder_mutex
In reply to Bart Van Asschebd_find_holder_disk() iterates over the disk->slave_bdevs list. That list is protected by blk_holder_mutex and both callers of this function hold blk_holder_mutex. Annotate bd_find_holder_disk() with __must_hold(&blk_holder_mutex) such that Clang verifies this.
--- block/holder.c | 1 + 1 file changed, 1 insertion(+)
block/holder.c +1 -0
@@ -12,6 +12,7 @@ static DEFINE_MUTEX(blk_holder_mutex);static struct bd_holder_disk *bd_find_holder_disk (struct block_device *bdev,struct gendisk *disk) + __must_hold(&blk_holder_mutex) {struct bd_holder_disk *holder;
PATCH 40/52 block: Add a __must_hold() annotation for bio_slab_lock
In reply to Bart Van Asschecreate_bio_slab() modifies the bio_slabs xarray. That xarray is protected by bio_slab_lock and the only caller of create_bio_slab(), bio_find_or_create_slab(), holds that mutex. Annotate create_bio_slab() with __must_hold(&bio_slab_lock).
--- block/bio.c | 1 + 1 file changed, 1 insertion(+)
block/bio.c +1 -0
@@ -86,6 +86,7 @@ static DEFINE_MUTEX(bio_slab_lock);static DEFINE_XARRAY (bio_slabs);static struct bio_slab *create_bio_slab (unsigned int size) + __must_hold(&bio_slab_lock) {struct bio_slab *bslab =kzalloc_obj (*bslab);
PATCH 41/52 block: Annotate bio_slabs and slab_ref with __guarded_by()
In reply to Bart Van AsscheThe bio_slabs xarray and the slab_ref member of struct bio_slab are only accessed with bio_slab_lock held. The internal xarray lock is not sufficient since a lookup and the subsequent reference count update must happen atomically. Annotate both with __guarded_by(&bio_slab_lock) such that Clang's context analysis can verify that bio_slab_lock is held for all accesses. Move the bio_slab_lock definition above the struct bio_slab definition such that it can be referenced from the member annotation.
--- block/bio.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
block/bio.c +5 -3
@@ -76,14 +76,16 @@ EXPORT_SYMBOL(fs_bio_set);/* * Our slab pool management */ +static DEFINE_MUTEX (bio_slab_lock); +struct bio_slab {struct kmem_cache *slab; -unsigned int slab_ref; + unsigned int slab_ref __guarded_by(&bio_slab_lock);unsigned int slab_size;char name[12 ]; }; -static DEFINE_MUTEX (bio_slab_lock); -static DEFINE_XARRAY (bio_slabs); + +static __guarded_by(&bio_slab_lock) DEFINE_XARRAY(bio_slabs);static struct bio_slab *create_bio_slab (unsigned int size) __must_hold(&bio_slab_lock)
PATCH 42/52 sed-opal: Add __must_hold() annotations for dev->dev_lock
In reply to Bart Van AsscheThe Opal session and suspend-key handling functions must be called with dev->dev_lock held. Annotate these functions with __must_hold(&dev->dev_lock) such that Clang verifies that all callers hold that mutex:
- execute_step() and execute_steps()
- opal_discovery0_step()
- end_opal_session_error()
- add_suspend_info()
- setup_opal_dev()
- __opal_lock_unlock()
- __opal_set_mbr_done()
- opal_lock_check_for_saved_key()
- opal_read_table() and opal_write_table()
Annotate both the forward declarations and the definitions of opal_discovery0_step() and end_opal_session_error().
--- block/sed-opal.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
block/sed-opal.c +15 -2
@@ -224,8 +224,10 @@ static const u8 opalmethod[][OPAL_METHOD_LENGTH] = { { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x08, 0x01 }, }; -static int end_opal_session_error (struct opal_dev *dev); -static int opal_discovery0_step (struct opal_dev *dev); +static int end_opal_session_error(struct opal_dev *dev) + __must_hold(&dev->dev_lock); +static int opal_discovery0_step (struct opal_dev *dev) + __must_hold(&dev->dev_lock); struct opal_suspend_data {struct opal_lock_unlock unlk;
@@ -522,6 +524,7 @@ static void check_geometry(struct opal_dev *dev, const void *data)static int execute_step (struct opal_dev *dev,const struct opal_step *step,size_t stepIndex) + __must_hold(&dev->dev_lock) {int error = step->fn (dev, step->data);
@@ -536,6 +539,7 @@ static int execute_step(struct opal_dev *dev,static int execute_steps (struct opal_dev *dev,const struct opal_step *steps,size_t n_steps) + __must_hold(&dev->dev_lock) {size_t state =0 ;int error;
@@ -686,6 +690,7 @@ static int opal_discovery0(struct opal_dev *dev, void *data) }static int opal_discovery0_step (struct opal_dev *dev) + __must_hold(&dev->dev_lock) {const struct opal_step discovery0_step = { opal_discovery0,NULL
@@ -1238,6 +1243,7 @@ static int start_opal_session_cont(struct opal_dev *dev)static void add_suspend_info (struct opal_dev *dev,struct opal_suspend_data *sus) + __must_hold(&dev->dev_lock) {struct opal_suspend_data *iter;
@@ -2541,6 +2547,7 @@ static int end_opal_session(struct opal_dev *dev, void *data) }static int end_opal_session_error (struct opal_dev *dev) + __must_hold(&dev->dev_lock) {const struct opal_step error_end_session = { end_opal_session,
@@ -2550,6 +2557,7 @@ static int end_opal_session_error(struct opal_dev *dev) }static inline void setup_opal_dev (struct opal_dev *dev) + __must_hold(&dev->dev_lock) { dev->tsn =0 ; dev->hsn =0 ;
@@ -2902,6 +2910,7 @@ static int opal_reverttper(struct opal_dev *dev, struct opal_key *opal, bool psistatic int __opal_lock_unlock (struct opal_dev *dev,struct opal_lock_unlock *lk_unlk) + __must_hold(&dev->dev_lock) {const struct opal_step unlock_steps[] = { { start_auth_opal_session, &lk_unlk->session },
@@ -2923,6 +2932,7 @@ static int __opal_lock_unlock(struct opal_dev *dev, }static int __opal_set_mbr_done (struct opal_dev *dev,struct opal_key *key) + __must_hold(&dev->dev_lock) {u8 mbr_done_tf =OPAL_TRUE ;const struct opal_step mbrdone_step[] = {
@@ -2936,6 +2946,7 @@ static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)static void opal_lock_check_for_saved_key (struct opal_dev *dev,struct opal_lock_unlock *lk_unlk) + __must_hold(&dev->dev_lock) {struct opal_suspend_data *iter;
@@ -3297,6 +3308,7 @@ EXPORT_SYMBOL(opal_unlock_from_suspend);static int opal_read_table (struct opal_dev *dev,struct opal_read_write_table *rw_tbl) + __must_hold(&dev->dev_lock) {const struct opal_step read_table_steps[] = { { start_admin1LSP_opal_session, &rw_tbl->key },
@@ -3313,6 +3325,7 @@ static int opal_read_table(struct opal_dev *dev,static int opal_write_table (struct opal_dev *dev,struct opal_read_write_table *rw_tbl) + __must_hold(&dev->dev_lock) {const struct opal_step write_table_steps[] = { { start_admin1LSP_opal_session, &rw_tbl->key },
PATCH 43/52 block: Annotate the disk_events list with LIST_HEAD_GUARDED()
In reply to Bart Van AsscheAll accesses of the global disk_events list happen with disk_events_mutex held. Define this list with LIST_HEAD_GUARDED() such that Clang's context analysis verifies that disk_events_mutex is held when the list is accessed.
--- block/disk-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/disk-events.c +1 -1
@@ -33,7 +33,7 @@ static char *disk_uevents[] = {/* list of all disk_events */ static DEFINE_MUTEX (disk_events_mutex); -static LIST_HEAD (disk_events); +static LIST_HEAD_GUARDED(disk_events, disk_events_mutex);/* disable in-kernel polling by default */ static unsigned long disk_events_dfl_poll_msecs;
PATCH 44/52 block: Annotate struct disk_events members with __guarded_by()
In reply to Bart Van AsscheAnnotate the block, pending and clearing members of struct disk_events with __guarded_by(&lock). The block counter is protected by the spinlock rather than by block_mutex since __disk_unblock_events() modifies it without holding block_mutex. Move the member comments above the members to keep the lines short.
Use scoped_guard(spinlock_init) to initialize ev->block, wrap the existing lockless read of ev->clearing in disk_clear_events() with data_race(READ_ONCE(...)), and wrap passing &ev->clearing to disk_check_events() and reading ev->block during teardown with context_unsafe(). This lets Clang's context analysis verify that the annotated members are only accessed with ev->lock held. No functional change.
--- block/disk-events.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-)
block/disk-events.c +25 -9
@@ -13,9 +13,12 @@ struct disk_events {spinlock_t lock;struct mutex block_mutex;/* protects blocking */ -int block;/* event blocking depth */ -unsigned int pending;/* events already sent out */ -unsigned int clearing;/* events being cleared */ +/* event blocking depth */ + int block __guarded_by(&lock); +/* events already sent out */ + unsigned int pending __guarded_by(&lock); +/* events being cleared */ + unsigned int clearing __guarded_by(&lock);long poll_msecs;/* interval, -1 for default */ struct delayed_work dwork;
@@ -250,7 +253,8 @@ static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)* if ev->clearing is not 0, the disk_flush_events got called in the * middle of this function, so we want to run the workfn without delay. */ -__disk_unblock_events (disk, ev->clearing ? true : false); +/* Lockless read. */ +__disk_unblock_events (disk,data_race (READ_ONCE (ev->clearing)));/* then, fetch and clear pending events */ spin_lock_irq (&ev->lock);
@@ -310,7 +314,11 @@ static void disk_events_workfn(struct work_struct *work)struct delayed_work *dwork =to_delayed_work (work); struct disk_events *ev = container_of(dwork, struct disk_events, dwork); -disk_check_events (ev, &ev->clearing); +/* +* disk_check_events() reads *clearing_ptr locklessly before calling +* ->check_events() and clears *clearing_ptr under ev->lock. +*/ +disk_check_events (ev,context_unsafe (&ev->clearing)); }/*
@@ -445,9 +453,9 @@ int disk_alloc_events(struct gendisk *disk)INIT_LIST_HEAD (&ev->node); ev->disk = disk; -spin_lock_init (&ev->lock);mutex_init (&ev->block_mutex); - ev->block =1 ; +scoped_guard (spinlock_init, &ev->lock) + ev->block = 1; ev->poll_msecs =-1 ;INIT_DELAYED_WORK (&ev->dwork, disk_events_workfn);
@@ -484,7 +492,15 @@ void disk_del_events(struct gendisk *disk)void disk_release_events (struct gendisk *disk) { -/* the block count should be 1 from disk_del_events() */ -WARN_ON_ONCE (disk->ev && disk->ev->block !=1 ); +if (disk->ev) { +/* +* The block count should be 1 from disk_del_events() and no +* other context can access disk->ev once it is about to be +* freed. +*/ +int block =context_unsafe (disk->ev->block); + +WARN_ON_ONCE (block !=1 ); + }kfree (disk->ev); }
PATCH 45/52 block: Simplify the major_names[] traversal in blk_probe_dev()
In reply to Bart Van Asscheblk_probe_dev() only reads the major_names[] hash chain. Traverse it with a struct blk_major_name pointer instead of a pointer to the previous next pointer, as blkdev_show() already does. This change prepares for annotating major_names[] with __guarded_by(): Clang's context analysis verifies reads of major_names[] but does not yet check accesses through a pointer obtained with &major_names[index].
No functional change is intended.
--- block/genhd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
block/genhd.c +4 -4
@@ -884,12 +884,12 @@ static ssize_t disk_badblocks_store(struct device *dev,static bool blk_probe_dev (dev_t devt) {unsigned int major =MAJOR (devt); -struct blk_major_name **n; +struct blk_major_name *p;mutex_lock (&major_names_lock); -for (n = &major_names[major_to_index (major)]; *n; n = &(*n)->next) { -if ((*n)->major == major && (*n)->probe) { - (*n)->probe (devt); +for (p = major_names[major_to_index (major)]; p; p = p->next) { +if (p->major == major && p->probe) { + p->probe (devt);mutex_unlock (&major_names_lock);return true; }
PATCH 46/52 block: Annotate major_names with __guarded_by()
In reply to Bart Van AsscheThe major_names[] hash table is modified with both major_names_lock and major_names_spinlock held, while readers hold only one of the two:blkdev_show() holds the spinlock and blk_probe_dev() and the free slot search in __register_blkdev() hold the mutex. This matches the semantics of __guarded_by() with multiple locks: reads require holding at least one of the locks and writes require holding all of them.
Annotate major_names[] accordingly and move the lock definitions above the table so that the annotation can reference them.
--- block/genhd.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
block/genhd.c +4 -3
@@ -175,6 +175,8 @@ EXPORT_SYMBOL_GPL(bdev_count_inflight);* */ #define BLKDEV_MAJOR_HASH_SIZE 255 +static DEFINE_MUTEX (major_names_lock); +static DEFINE_SPINLOCK (major_names_spinlock); static struct blk_major_name { struct blk_major_name *next; int major;
@@ -182,9 +184,8 @@ static struct blk_major_name {#ifdef CONFIG_BLOCK_LEGACY_AUTOLOAD void (*probe)(dev_t devt);#endif -} *major_names[BLKDEV_MAJOR_HASH_SIZE ]; -static DEFINE_MUTEX (major_names_lock); -static DEFINE_SPINLOCK (major_names_spinlock); +} *major_names[BLKDEV_MAJOR_HASH_SIZE] +__guarded_by (&major_names_lock, &major_names_spinlock);/* index in the above - for now: assume no multimajor ranges */ static inline int major_to_index (unsigned major)
PATCH 47/52 loop: Pass disk instead of lo to __loop_clr_fd()
In reply to Bart Van AsscheChange __loop_clr_fd() to take struct gendisk *disk instead of struct loop_device *lo and use disk directly instead of lo->lo_disk. Its sole caller is lo_release(), which already has disk and holds disk->open_mutex.
This change prepares for annotating lo_release() and __loop_clr_fd() with __must_hold(&disk->open_mutex). Clang's context analysis cannot prove that lo->lo_disk equals disk across disk->private_data.
No functional change is intended since disk->private_data == lo and lo->lo_disk == disk.
--- drivers/block/loop.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/block/loop.c +8 -7
@@ -1030,8 +1030,9 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,return error; } -static void __loop_clr_fd (struct loop_device *lo) +static void __loop_clr_fd (struct gendisk *disk) { +struct loop_device *lo = disk->private_data;struct queue_limits lim;struct file *filp;gfp_t gfp = lo->old_gfp_mask;
@@ -1059,15 +1060,15 @@ static void __loop_clr_fd(struct loop_device *lo) lim.io_min =SECTOR_SIZE ;queue_limits_commit_update (lo->lo_queue, &lim); -invalidate_disk (lo->lo_disk); +invalidate_disk (disk);loop_sysfs_exit (lo);/* let user-space know about this change */ -kobject_uevent (&disk_to_dev (lo->lo_disk)->kobj,KOBJ_CHANGE ); +kobject_uevent (&disk_to_dev (disk)->kobj,KOBJ_CHANGE );mapping_set_gfp_mask (filp->f_mapping, gfp);/* This is safe: open() is still holding a reference. */ module_put (THIS_MODULE ); -disk_force_media_change (lo->lo_disk); +disk_force_media_change (disk);/* * Remove all partitions, including partitions added manually with
@@ -1076,7 +1077,7 @@ static void __loop_clr_fd(struct loop_device *lo)* open_mutex has been held already in release path, so don't acquire * it here. */ - err =bdev_disk_changed (lo->lo_disk, false); + err =bdev_disk_changed (disk, false);if (err)pr_warn ("%s: partition scan of loop%d failed (rc=%d)\n" , __func__, lo->lo_number, err);
@@ -1090,7 +1091,7 @@ static void __loop_clr_fd(struct loop_device *lo)*/ lo->lo_flags =0 ;if (!part_shift) -set_bit (GD_SUPPRESS_PART_SCAN , &lo->lo_disk->state); +set_bit (GD_SUPPRESS_PART_SCAN , &disk->state);mutex_lock (&lo->lo_mutex);WRITE_ONCE (lo->lo_state, Lo_unbound);mutex_unlock (&lo->lo_mutex);
@@ -1695,7 +1696,7 @@ static void lo_release(struct gendisk *disk)mutex_unlock (&lo->lo_mutex);if (need_clear) -__loop_clr_fd (lo); +__loop_clr_fd (disk); }static void lo_free_disk (struct gendisk *disk)
PATCH 48/52 loop: Annotate lo_release() and __loop_clr_fd() with __must_hold()
In reply to Bart Van AsscheThe block layer invokes ->release() (lo_release()) with disk->open_mutex held, and __loop_clr_fd() relies on that lock when calling bdev_disk_changed(). Because Clang's context analysis does not know that lo_release() is called with disk->open_mutex held (and does not infer __must_hold() on a callback definition from a function pointer annotation), annotate both lo_release() and __loop_clr_fd() with __must_hold(&disk->open_mutex).
Prepare for annotating bdev_disk_changed() with __must_hold(&disk->open_mutex) without introducing a context-analysis warning in drivers/block/loop.c under CONFIG_WARN_CONTEXT_ANALYSIS_ALL=y.
--- drivers/block/loop.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/block/loop.c +2 -0
@@ -1031,6 +1031,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode, }static void __loop_clr_fd (struct gendisk *disk) + __must_hold(&disk->open_mutex) {struct loop_device *lo = disk->private_data;struct queue_limits lim;
@@ -1676,6 +1677,7 @@ static int lo_open(struct gendisk *disk, blk_mode_t mode) }static void lo_release (struct gendisk *disk) + __must_hold(&disk->open_mutex) {struct loop_device *lo = disk->private_data;bool need_clear = false;
PATCH 49/52 block: partitions: Add __must_hold() annotations
In reply to Bart Van AsscheAnnotate the functions in block/partitions/core.c that are called with disk->open_mutex held with __must_hold(&disk->open_mutex):
- add_partition()
- blk_add_partition()
- blk_add_partitions()
- bdev_disk_changed() (both declaration in <linux/blkdev.h> and definition)
Remove the stale comment above add_partition() claiming it may be called without open_mutex held, which contradicts both lockdep_assert_held(&disk->open_mutex) and the new annotation.
Do not annotate drop_partition() since its callers obtain the mutex through a different pointer, e.g. disk->open_mutex instead of part->bd_disk->open_mutex, and since Clang cannot determine that these expressions refer to the same mutex. The lockdep_assert_held() call in drop_partition() keeps verifying the locking requirement at runtime.
--- block/partitions/core.c | 8 ++++---- include/linux/blkdev.h | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-)
block/partitions/core.c +4 -4
@@ -288,13 +288,10 @@ static ssize_t whole_disk_show(struct device *dev, } static const DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL); -/* -* Must be called either with open_mutex held, before a disk can be opened or -* after all disk users are gone. -*/ static struct block_device *add_partition (struct gendisk *disk,int partno,sector_t start,sector_t len,int flags,struct partition_meta_info *info) + __must_hold(&disk->open_mutex) {dev_t devt =MKDEV (0 ,0 );struct device *ddev =disk_to_dev (disk);
@@ -530,6 +527,7 @@ static bool disk_unlock_native_capacity(struct gendisk *disk)static bool blk_add_partition (struct gendisk *disk,struct parsed_partitions *state,int p) + __must_hold(&disk->open_mutex) {sector_t size = state->parts[p].size;sector_t from = state->parts[p].from;
@@ -581,6 +579,7 @@ static bool blk_add_partition(struct gendisk *disk, }static int blk_add_partitions (struct gendisk *disk) + __must_hold(&disk->open_mutex) {struct parsed_partitions *state;int ret = -EAGAIN , p;
@@ -642,6 +641,7 @@ static int blk_add_partitions(struct gendisk *disk) }int bdev_disk_changed (struct gendisk *disk,bool invalidate) + __must_hold(&disk->open_mutex) {struct block_device *part;unsigned long idx;
include/linux/blkdev.h +2 -1
@@ -1015,7 +1015,8 @@ static inline unsigned int bdev_nr_zones(struct block_device *bdev)return disk_nr_zones (bdev->bd_disk); } -int bdev_disk_changed (struct gendisk *disk,bool invalidate); +int bdev_disk_changed(struct gendisk *disk, bool invalidate) + __must_hold(&disk->open_mutex);void put_disk (struct gendisk *disk); struct gendisk *__blk_alloc_disk(struct queue_limits *lim, int node,
PATCH 50/52 block: partitions: Enable lock context analysis
In reply to Bart Van AsscheEnable Clang's lock context analysis for the block/partitions directory by setting CONTEXT_ANALYSIS := y in block/partitions/Makefile. The CONTEXT_ANALYSIS setting in block/Makefile does not apply to the block/partitions subdirectory.
--- block/partitions/Makefile | 2 ++ 1 file changed, 2 insertions(+)
block/partitions/Makefile +2 -0
@@ -3,6 +3,8 @@ # Makefile for the linux kernel. # +CONTEXT_ANALYSIS := y + obj-$(CONFIG_BLOCK) += core.o obj-$(CONFIG_ACORN_PARTITION) += acorn.o obj-$(CONFIG_AMIGA_PARTITION) += amiga.o
PATCH 51/52 kyber: Add __must_hold() annotations for khd->lock
In reply to Bart Van Asschekyber_flush_busy_kcqs(), kyber_get_domain_token() and kyber_dispatch_cur_domain() are only called (directly or via kyber_dispatch_cur_domain()) by kyber_dispatch_request() and the latter function holds khd->lock while calling them. Annotate these three functions with __must_hold(&khd->lock).
--- block/kyber-iosched.c | 3 +++ 1 file changed, 3 insertions(+)
block/kyber-iosched.c +3 -0
@@ -667,6 +667,7 @@ static bool flush_busy_kcq(struct sbitmap *sb, unsigned int bitnr, void *data)static void kyber_flush_busy_kcqs (struct kyber_hctx_data *khd,unsigned int sched_domain,struct list_head *list) + __must_hold(&khd->lock) {struct flush_kcq_data data = { .khd = khd,
@@ -692,6 +693,7 @@ static int kyber_domain_wake(wait_queue_entry_t *wqe, unsigned mode, int flags,static int kyber_get_domain_token (struct kyber_queue_data *kqd,struct kyber_hctx_data *khd,struct blk_mq_hw_ctx *hctx) + __must_hold(&khd->lock) {unsigned int sched_domain = khd->cur_domain;struct sbitmap_queue *domain_tokens = &kqd->domain_tokens[sched_domain];
@@ -740,6 +742,7 @@ static struct request * kyber_dispatch_cur_domain(struct kyber_queue_data *kqd, struct kyber_hctx_data *khd, struct blk_mq_hw_ctx *hctx) + __must_hold(&khd->lock) { struct list_head *rqs;struct request *rq;
PATCH 52/52 kyber: Add __guarded_by() annotations to struct kyber_hctx_data
In reply to Bart Van AsscheAnnotate 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.
--- block/kyber-iosched.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)
block/kyber-iosched.c +16 -11
@@ -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 ; }
Damien Le Moal
maintainerQuoted 13 lines · Bart Van Assche
Hi Jens,
Deriving from the block layer source code which synchronization object protects which member variable and which functions is nontrivial. This patch series documents this in such a way that the documentation can be verified by the Clang compiler. This patch series adds
__must_hold(),__guarded_by()and related annotations to the block layer core, the blk-cgroup policies, the I/O schedulers, sed-opal, the partition code and the loop driver. With these annotations and withCONFIG_WARN_CONTEXT_ANALYSIS=y, Clang verifies at compile time that the annotated functions are only called and that the annotated variables and structure members are only accessed with the appropriate lock held. See alsoDocumentation/dev-tools/context-analysis.rst.
Splitting this giant series into digestible pieces would be nice...
Anyway, will try to have a look.
Signature · 2 lines
Damien Le Moal Western Digital Research