Skip to content

Commit 0d519bb

Browse files
YuKuai-huaweiaxboe
authored andcommitted
brd: fix sleeping function called from invalid context in brd_insert_page()
__xa_cmpxchg() is called with rcu_read_lock(), and it will allocate memory if necessary. Fix the problem by moving rcu_read_lock() after __xa_cmpxchg(), meanwhile, it still should be held before xa_unlock(), prevent returned page to be freed by concurrent discard. Fixes: bbcacab ("brd: avoid extra xarray lookups on first write") Reported-by: [email protected] Closes: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Yu Kuai <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 01ed88a commit 0d519bb

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

drivers/block/brd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ static struct page *brd_insert_page(struct brd_device *brd, sector_t sector,
6464

6565
rcu_read_unlock();
6666
page = alloc_page(gfp | __GFP_ZERO | __GFP_HIGHMEM);
67-
rcu_read_lock();
68-
if (!page)
67+
if (!page) {
68+
rcu_read_lock();
6969
return ERR_PTR(-ENOMEM);
70+
}
7071

7172
xa_lock(&brd->brd_pages);
7273
ret = __xa_cmpxchg(&brd->brd_pages, sector >> PAGE_SECTORS_SHIFT, NULL,
7374
page, gfp);
75+
rcu_read_lock();
7476
if (ret) {
7577
xa_unlock(&brd->brd_pages);
7678
__free_page(page);

0 commit comments

Comments
 (0)