Skip to content

Commit

Permalink
blk-mq: Fix a race between bt_clear_tag() and bt_get()
Browse files Browse the repository at this point in the history
What we need is the following two guarantees:
* Any thread that observes the effect of the test_and_set_bit() by
  __bt_get_word() also observes the preceding addition of 'current'
  to the appropriate wait list. This is guaranteed by the semantics
  of the spin_unlock() operation performed by prepare_and_wait().
  Hence the conversion of test_and_set_bit_lock() into
  test_and_set_bit().
* The wait lists are examined by bt_clear() after the tag bit has
  been cleared. clear_bit_unlock() guarantees that any thread that
  observes that the bit has been cleared also observes the store
  operations preceding clear_bit_unlock(). However,
  clear_bit_unlock() does not prevent that the wait lists are examined
  before that the tag bit is cleared. Hence the addition of a memory
  barrier between clear_bit() and the wait list examination.

Signed-off-by: Bart Van Assche <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Robert Elliott <[email protected]>
Cc: Ming Lei <[email protected]>
Cc: Alexander Gordeev <[email protected]>
Cc: <[email protected]> # v3.13+
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
bvanassche authored and axboe committed Dec 9, 2014
1 parent 9e98e9d commit c38d185
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions block/blk-mq-tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static int __bt_get_word(struct blk_align_bitmap *bm, unsigned int last_tag)
return -1;
}
last_tag = tag + 1;
} while (test_and_set_bit_lock(tag, &bm->word));
} while (test_and_set_bit(tag, &bm->word));

return tag;
}
Expand Down Expand Up @@ -357,11 +357,10 @@ static void bt_clear_tag(struct blk_mq_bitmap_tags *bt, unsigned int tag)
struct bt_wait_state *bs;
int wait_cnt;

/*
* The unlock memory barrier need to order access to req in free
* path and clearing tag bit
*/
clear_bit_unlock(TAG_TO_BIT(bt, tag), &bt->map[index].word);
clear_bit(TAG_TO_BIT(bt, tag), &bt->map[index].word);

/* Ensure that the wait list checks occur after clear_bit(). */
smp_mb();

bs = bt_wake_ptr(bt);
if (!bs)
Expand Down

0 comments on commit c38d185

Please sign in to comment.