Skip to content

Commit 2fff8a9

Browse files
Bart Van Asscheaxboe
authored andcommitted
block: Check locking assumptions at runtime
Instead of documenting the locking assumptions of most block layer functions as a comment, use lockdep_assert_held() to verify locking assumptions at runtime. Signed-off-by: Bart Van Assche <bart.vanassche@sandisk.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Cc: Hannes Reinecke <hare@suse.com> Cc: Omar Sandoval <osandov@fb.com> Cc: Ming Lei <ming.lei@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 9e0c829 commit 2fff8a9

5 files changed

Lines changed: 64 additions & 37 deletions

File tree

‎block/blk-core.c‎

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,12 @@ static void blk_delay_work(struct work_struct *work)
236236
* Description:
237237
* Sometimes queueing needs to be postponed for a little while, to allow
238238
* resources to come back. This function will make sure that queueing is
239-
* restarted around the specified time. Queue lock must be held.
239+
* restarted around the specified time.
240240
*/
241241
void blk_delay_queue(struct request_queue *q, unsigned long msecs)
242242
{
243+
lockdep_assert_held(q->queue_lock);
244+
243245
if (likely(!blk_queue_dead(q)))
244246
queue_delayed_work(kblockd_workqueue, &q->delay_work,
245247
msecs_to_jiffies(msecs));
@@ -257,6 +259,8 @@ EXPORT_SYMBOL(blk_delay_queue);
257259
**/
258260
void blk_start_queue_async(struct request_queue *q)
259261
{
262+
lockdep_assert_held(q->queue_lock);
263+
260264
queue_flag_clear(QUEUE_FLAG_STOPPED, q);
261265
blk_run_queue_async(q);
262266
}
@@ -269,10 +273,11 @@ EXPORT_SYMBOL(blk_start_queue_async);
269273
* Description:
270274
* blk_start_queue() will clear the stop flag on the queue, and call
271275
* the request_fn for the queue if it was in a stopped state when
272-
* entered. Also see blk_stop_queue(). Queue lock must be held.
276+
* entered. Also see blk_stop_queue().
273277
**/
274278
void blk_start_queue(struct request_queue *q)
275279
{
280+
lockdep_assert_held(q->queue_lock);
276281
WARN_ON(!irqs_disabled());
277282

278283
queue_flag_clear(QUEUE_FLAG_STOPPED, q);
@@ -292,10 +297,12 @@ EXPORT_SYMBOL(blk_start_queue);
292297
* or if it simply chooses not to queue more I/O at one point, it can
293298
* call this function to prevent the request_fn from being called until
294299
* the driver has signalled it's ready to go again. This happens by calling
295-
* blk_start_queue() to restart queue operations. Queue lock must be held.
300+
* blk_start_queue() to restart queue operations.
296301
**/
297302
void blk_stop_queue(struct request_queue *q)
298303
{
304+
lockdep_assert_held(q->queue_lock);
305+
299306
cancel_delayed_work(&q->delay_work);
300307
queue_flag_set(QUEUE_FLAG_STOPPED, q);
301308
}
@@ -348,6 +355,8 @@ EXPORT_SYMBOL(blk_sync_queue);
348355
*/
349356
inline void __blk_run_queue_uncond(struct request_queue *q)
350357
{
358+
lockdep_assert_held(q->queue_lock);
359+
351360
if (unlikely(blk_queue_dead(q)))
352361
return;
353362

@@ -369,11 +378,12 @@ EXPORT_SYMBOL_GPL(__blk_run_queue_uncond);
369378
* @q: The queue to run
370379
*
371380
* Description:
372-
* See @blk_run_queue. This variant must be called with the queue lock
373-
* held and interrupts disabled.
381+
* See @blk_run_queue.
374382
*/
375383
void __blk_run_queue(struct request_queue *q)
376384
{
385+
lockdep_assert_held(q->queue_lock);
386+
377387
if (unlikely(blk_queue_stopped(q)))
378388
return;
379389

@@ -387,10 +397,17 @@ EXPORT_SYMBOL(__blk_run_queue);
387397
*
388398
* Description:
389399
* Tells kblockd to perform the equivalent of @blk_run_queue on behalf
390-
* of us. The caller must hold the queue lock.
400+
* of us.
401+
*
402+
* Note:
403+
* Since it is not allowed to run q->delay_work after blk_cleanup_queue()
404+
* has canceled q->delay_work, callers must hold the queue lock to avoid
405+
* race conditions between blk_cleanup_queue() and blk_run_queue_async().
391406
*/
392407
void blk_run_queue_async(struct request_queue *q)
393408
{
409+
lockdep_assert_held(q->queue_lock);
410+
394411
if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
395412
mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
396413
}
@@ -1136,6 +1153,8 @@ static struct request *__get_request(struct request_list *rl, unsigned int op,
11361153
int may_queue;
11371154
req_flags_t rq_flags = RQF_ALLOCED;
11381155

1156+
lockdep_assert_held(q->queue_lock);
1157+
11391158
if (unlikely(blk_queue_dying(q)))
11401159
return ERR_PTR(-ENODEV);
11411160

@@ -1309,6 +1328,8 @@ static struct request *get_request(struct request_queue *q, unsigned int op,
13091328
struct request_list *rl;
13101329
struct request *rq;
13111330

1331+
lockdep_assert_held(q->queue_lock);
1332+
13121333
rl = blk_get_rl(q, bio); /* transferred to @rq on success */
13131334
retry:
13141335
rq = __get_request(rl, op, bio, gfp_mask);
@@ -1402,6 +1423,8 @@ EXPORT_SYMBOL(blk_get_request);
14021423
*/
14031424
void blk_requeue_request(struct request_queue *q, struct request *rq)
14041425
{
1426+
lockdep_assert_held(q->queue_lock);
1427+
14051428
blk_delete_timer(rq);
14061429
blk_clear_rq_complete(rq);
14071430
trace_block_rq_requeue(q, rq);
@@ -1476,9 +1499,6 @@ static void blk_pm_put_request(struct request *rq)
14761499
static inline void blk_pm_put_request(struct request *rq) {}
14771500
#endif
14781501

1479-
/*
1480-
* queue lock must be held
1481-
*/
14821502
void __blk_put_request(struct request_queue *q, struct request *req)
14831503
{
14841504
req_flags_t rq_flags = req->rq_flags;
@@ -1491,6 +1511,8 @@ void __blk_put_request(struct request_queue *q, struct request *req)
14911511
return;
14921512
}
14931513

1514+
lockdep_assert_held(q->queue_lock);
1515+
14941516
blk_pm_put_request(req);
14951517

14961518
elv_completed_request(q, req);
@@ -2327,9 +2349,6 @@ EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
23272349
*
23282350
* Return:
23292351
* The number of bytes to fail.
2330-
*
2331-
* Context:
2332-
* queue_lock must be held.
23332352
*/
23342353
unsigned int blk_rq_err_bytes(const struct request *rq)
23352354
{
@@ -2469,15 +2488,14 @@ void blk_account_io_start(struct request *rq, bool new_io)
24692488
* Return:
24702489
* Pointer to the request at the top of @q if available. Null
24712490
* otherwise.
2472-
*
2473-
* Context:
2474-
* queue_lock must be held.
24752491
*/
24762492
struct request *blk_peek_request(struct request_queue *q)
24772493
{
24782494
struct request *rq;
24792495
int ret;
24802496

2497+
lockdep_assert_held(q->queue_lock);
2498+
24812499
while ((rq = __elv_next_request(q)) != NULL) {
24822500

24832501
rq = blk_pm_peek_request(q, rq);
@@ -2593,12 +2611,11 @@ void blk_dequeue_request(struct request *rq)
25932611
*
25942612
* Block internal functions which don't want to start timer should
25952613
* call blk_dequeue_request().
2596-
*
2597-
* Context:
2598-
* queue_lock must be held.
25992614
*/
26002615
void blk_start_request(struct request *req)
26012616
{
2617+
lockdep_assert_held(req->q->queue_lock);
2618+
26022619
blk_dequeue_request(req);
26032620

26042621
if (test_bit(QUEUE_FLAG_STATS, &req->q->queue_flags)) {
@@ -2623,14 +2640,13 @@ EXPORT_SYMBOL(blk_start_request);
26232640
* Return:
26242641
* Pointer to the request at the top of @q if available. Null
26252642
* otherwise.
2626-
*
2627-
* Context:
2628-
* queue_lock must be held.
26292643
*/
26302644
struct request *blk_fetch_request(struct request_queue *q)
26312645
{
26322646
struct request *rq;
26332647

2648+
lockdep_assert_held(q->queue_lock);
2649+
26342650
rq = blk_peek_request(q);
26352651
if (rq)
26362652
blk_start_request(rq);
@@ -2776,13 +2792,12 @@ void blk_unprep_request(struct request *req)
27762792
}
27772793
EXPORT_SYMBOL_GPL(blk_unprep_request);
27782794

2779-
/*
2780-
* queue lock must be held
2781-
*/
27822795
void blk_finish_request(struct request *req, blk_status_t error)
27832796
{
27842797
struct request_queue *q = req->q;
27852798

2799+
lockdep_assert_held(req->q->queue_lock);
2800+
27862801
if (req->rq_flags & RQF_STATS)
27872802
blk_stat_add(req);
27882803

@@ -2864,6 +2879,8 @@ static bool blk_end_bidi_request(struct request *rq, blk_status_t error,
28642879
static bool __blk_end_bidi_request(struct request *rq, blk_status_t error,
28652880
unsigned int nr_bytes, unsigned int bidi_bytes)
28662881
{
2882+
lockdep_assert_held(rq->q->queue_lock);
2883+
28672884
if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
28682885
return true;
28692886

@@ -2930,6 +2947,8 @@ EXPORT_SYMBOL(blk_end_request_all);
29302947
bool __blk_end_request(struct request *rq, blk_status_t error,
29312948
unsigned int nr_bytes)
29322949
{
2950+
lockdep_assert_held(rq->q->queue_lock);
2951+
29332952
return __blk_end_bidi_request(rq, error, nr_bytes, 0);
29342953
}
29352954
EXPORT_SYMBOL(__blk_end_request);
@@ -2947,6 +2966,8 @@ void __blk_end_request_all(struct request *rq, blk_status_t error)
29472966
bool pending;
29482967
unsigned int bidi_bytes = 0;
29492968

2969+
lockdep_assert_held(rq->q->queue_lock);
2970+
29502971
if (unlikely(blk_bidi_rq(rq)))
29512972
bidi_bytes = blk_rq_bytes(rq->next_rq);
29522973

@@ -3211,6 +3232,8 @@ static void queue_unplugged(struct request_queue *q, unsigned int depth,
32113232
bool from_schedule)
32123233
__releases(q->queue_lock)
32133234
{
3235+
lockdep_assert_held(q->queue_lock);
3236+
32143237
trace_block_unplug(q, depth, !from_schedule);
32153238

32163239
if (from_schedule)

‎block/blk-flush.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ static void flush_data_end_io(struct request *rq, blk_status_t error)
346346
struct request_queue *q = rq->q;
347347
struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
348348

349+
lockdep_assert_held(q->queue_lock);
350+
349351
/*
350352
* Updating q->in_flight[] here for making this tag usable
351353
* early. Because in blk_queue_start_tag(),
@@ -411,9 +413,6 @@ static void mq_flush_data_end_io(struct request *rq, blk_status_t error)
411413
* or __blk_mq_run_hw_queue() to dispatch request.
412414
* @rq is being submitted. Analyze what needs to be done and put it on the
413415
* right queue.
414-
*
415-
* CONTEXT:
416-
* spin_lock_irq(q->queue_lock) in !mq case
417416
*/
418417
void blk_insert_flush(struct request *rq)
419418
{
@@ -422,6 +421,9 @@ void blk_insert_flush(struct request *rq)
422421
unsigned int policy = blk_flush_policy(fflags, rq);
423422
struct blk_flush_queue *fq = blk_get_flush_queue(q, rq->mq_ctx);
424423

424+
if (!q->mq_ops)
425+
lockdep_assert_held(q->queue_lock);
426+
425427
/*
426428
* @policy now records what operations need to be done. Adjust
427429
* REQ_PREFLUSH and FUA for the driver.

‎block/blk-merge.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,9 @@ static void blk_account_io_merge(struct request *req)
648648
static struct request *attempt_merge(struct request_queue *q,
649649
struct request *req, struct request *next)
650650
{
651+
if (!q->mq_ops)
652+
lockdep_assert_held(q->queue_lock);
653+
651654
if (!rq_mergeable(req) || !rq_mergeable(next))
652655
return NULL;
653656

‎block/blk-tag.c‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,14 @@ EXPORT_SYMBOL(blk_queue_resize_tags);
258258
* all transfers have been done for a request. It's important to call
259259
* this function before end_that_request_last(), as that will put the
260260
* request back on the free list thus corrupting the internal tag list.
261-
*
262-
* Notes:
263-
* queue lock must be held.
264261
**/
265262
void blk_queue_end_tag(struct request_queue *q, struct request *rq)
266263
{
267264
struct blk_queue_tag *bqt = q->queue_tags;
268265
unsigned tag = rq->tag; /* negative tags invalid */
269266

267+
lockdep_assert_held(q->queue_lock);
268+
270269
BUG_ON(tag >= bqt->real_max_depth);
271270

272271
list_del_init(&rq->queuelist);
@@ -307,16 +306,15 @@ EXPORT_SYMBOL(blk_queue_end_tag);
307306
* calling this function. The request will also be removed from
308307
* the request queue, so it's the drivers responsibility to readd
309308
* it if it should need to be restarted for some reason.
310-
*
311-
* Notes:
312-
* queue lock must be held.
313309
**/
314310
int blk_queue_start_tag(struct request_queue *q, struct request *rq)
315311
{
316312
struct blk_queue_tag *bqt = q->queue_tags;
317313
unsigned max_depth;
318314
int tag;
319315

316+
lockdep_assert_held(q->queue_lock);
317+
320318
if (unlikely((rq->rq_flags & RQF_QUEUED))) {
321319
printk(KERN_ERR
322320
"%s: request %p for device [%s] already tagged %d",
@@ -389,14 +387,13 @@ EXPORT_SYMBOL(blk_queue_start_tag);
389387
* Hardware conditions may dictate a need to stop all pending requests.
390388
* In this case, we will safely clear the block side of the tag queue and
391389
* readd all requests to the request queue in the right order.
392-
*
393-
* Notes:
394-
* queue lock must be held.
395390
**/
396391
void blk_queue_invalidate_tags(struct request_queue *q)
397392
{
398393
struct list_head *tmp, *n;
399394

395+
lockdep_assert_held(q->queue_lock);
396+
400397
list_for_each_safe(tmp, n, &q->tag_busy_list)
401398
blk_requeue_request(q, list_entry_rq(tmp));
402399
}

‎block/blk-timeout.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,15 @@ unsigned long blk_rq_timeout(unsigned long timeout)
189189
* Notes:
190190
* Each request has its own timer, and as it is added to the queue, we
191191
* set up the timer. When the request completes, we cancel the timer.
192-
* Queue lock must be held for the non-mq case, mq case doesn't care.
193192
*/
194193
void blk_add_timer(struct request *req)
195194
{
196195
struct request_queue *q = req->q;
197196
unsigned long expiry;
198197

198+
if (!q->mq_ops)
199+
lockdep_assert_held(q->queue_lock);
200+
199201
/* blk-mq has its own handler, so we don't need ->rq_timed_out_fn */
200202
if (!q->mq_ops && !q->rq_timed_out_fn)
201203
return;

0 commit comments

Comments
 (0)