Skip to content

Commit c3dbe54

Browse files
jankaraaxboe
authored andcommitted
blktrace: Avoid sparse warnings when assigning q->blk_trace
Mostly for historical reasons, q->blk_trace is assigned through xchg() and cmpxchg() atomic operations. Although this is correct, sparse complains about this because it violates rcu annotations since commit c780e86 ("blktrace: Protect q->blk_trace with RCU") which started to use rcu for accessing q->blk_trace. Furthermore there's no real need for atomic operations anymore since all changes to q->blk_trace happen under q->blk_trace_mutex and since it also makes more sense to check if q->blk_trace is set with the mutex held earlier. So let's just replace xchg() with rcu_replace_pointer() and cmpxchg() with explicit check and rcu_assign_pointer(). This makes the code more efficient and sparse happy. Reported-by: kbuild test robot <[email protected]> Signed-off-by: Jan Kara <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 1b0b283 commit c3dbe54

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

kernel/trace/blktrace.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ static int __blk_trace_remove(struct request_queue *q)
347347
{
348348
struct blk_trace *bt;
349349

350-
bt = xchg(&q->blk_trace, NULL);
350+
bt = rcu_replace_pointer(q->blk_trace, NULL,
351+
lockdep_is_held(&q->blk_trace_mutex));
351352
if (!bt)
352353
return -EINVAL;
353354

@@ -501,7 +502,8 @@ static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
501502
* bdev can be NULL, as with scsi-generic, this is a helpful as
502503
* we can be.
503504
*/
504-
if (q->blk_trace) {
505+
if (rcu_dereference_protected(q->blk_trace,
506+
lockdep_is_held(&q->blk_trace_mutex))) {
505507
pr_warn("Concurrent blktraces are not allowed on %s\n",
506508
buts->name);
507509
return -EBUSY;
@@ -556,10 +558,7 @@ static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
556558
bt->pid = buts->pid;
557559
bt->trace_state = Blktrace_setup;
558560

559-
ret = -EBUSY;
560-
if (cmpxchg(&q->blk_trace, NULL, bt))
561-
goto err;
562-
561+
rcu_assign_pointer(q->blk_trace, bt);
563562
get_probe_ref();
564563

565564
ret = 0;
@@ -1642,7 +1641,8 @@ static int blk_trace_remove_queue(struct request_queue *q)
16421641
{
16431642
struct blk_trace *bt;
16441643

1645-
bt = xchg(&q->blk_trace, NULL);
1644+
bt = rcu_replace_pointer(q->blk_trace, NULL,
1645+
lockdep_is_held(&q->blk_trace_mutex));
16461646
if (bt == NULL)
16471647
return -EINVAL;
16481648

@@ -1674,10 +1674,7 @@ static int blk_trace_setup_queue(struct request_queue *q,
16741674

16751675
blk_trace_setup_lba(bt, bdev);
16761676

1677-
ret = -EBUSY;
1678-
if (cmpxchg(&q->blk_trace, NULL, bt))
1679-
goto free_bt;
1680-
1677+
rcu_assign_pointer(q->blk_trace, bt);
16811678
get_probe_ref();
16821679
return 0;
16831680

0 commit comments

Comments
 (0)