From: Yu Kuai <[email protected]>

cached_dev_cache_miss() allocates cache_bio with GFP_NOWAIT.  Passing a bdev
to bio_alloc_bioset() can attach blkcg state and sleep to create a missing
blkg after blkg lookup is protected by q->blkcg_mutex.

Use the nowait bio allocation/association path.  If the cache bio needs a
missing blkg to be created, fail the association and fall back to the existing
miss submission path.

journal_write_unlocked() also resets journal bios while holding the journal
spinlock.  Reset those bios without a bdev, set bi_bdev while still under the
lock, and associate blkcg after dropping the lock.

Signed-off-by: Yu Kuai <[email protected]>
---
 drivers/md/bcache/journal.c | 9 ++++++---
 drivers/md/bcache/request.c | 2 ++
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/journal.c b/drivers/md/bcache/journal.c
index 144693b7c46a..49d2fb9a5f20 100644
--- a/drivers/md/bcache/journal.c
+++ b/drivers/md/bcache/journal.c
@@ -714,8 +714,9 @@ static CLOSURE_CALLBACK(journal_write_unlocked)
 
                atomic_long_add(sectors, &ca->meta_sectors_written);
 
-               bio_reset(bio, ca->bdev, REQ_OP_WRITE | 
-                         REQ_SYNC | REQ_META | REQ_PREFLUSH | REQ_FUA);
+               bio_reset(bio, NULL, REQ_OP_WRITE | REQ_SYNC | REQ_META |
+                         REQ_PREFLUSH | REQ_FUA);
+               bio->bi_bdev = ca->bdev;
                bio->bi_iter.bi_sector  = PTR_OFFSET(k, i);
                bio->bi_iter.bi_size = sectors << 9;
 
@@ -740,8 +741,10 @@ static CLOSURE_CALLBACK(journal_write_unlocked)
 
        spin_unlock(&c->journal.lock);
 
-       while ((bio = bio_list_pop(&list)))
+       while ((bio = bio_list_pop(&list))) {
+               bio_associate_blkg(bio, false);
                closure_bio_submit(c, bio, cl);
+       }
 
        continue_at(cl, journal_write_done, NULL);
 }
diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index c2b7a694ea99..647ca5018d07 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -932,6 +932,8 @@ static int cached_dev_cache_miss(struct btree *b, struct 
search *s,
        if (!cache_bio)
                goto out_submit;
 
+       if (!bio_clone_blkg_association(cache_bio, miss, true))
+               goto out_put;
        cache_bio->bi_iter.bi_sector    = miss->bi_iter.bi_sector;
        cache_bio->bi_iter.bi_size      = s->insert_bio_sectors << 9;
 
-- 
2.51.0


Reply via email to