Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

block: Fix oops in locked_inode_to_wb_and_lock_list()

When block device is closed, we call inode_detach_wb() in __blkdev_put()
which sets inode->i_wb to NULL. That is contrary to expectations that
inode->i_wb stays valid once set during the whole inode's lifetime and
leads to oops in wb_get() in locked_inode_to_wb_and_lock_list() because
inode_to_wb() returned NULL.

The reason why we called inode_detach_wb() is not valid anymore though.
BDI is guaranteed to stay along until we call bdi_put() from
bdev_evict_inode() so we can postpone calling inode_detach_wb() to that
moment.

Also add a warning to catch if someone uses inode_detach_wb() in a
dangerous way.

Reported-by: Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <axboe@fb.com>

authored by

Jan Kara and committed by
Jens Axboe
f759741d b1c51afc

+3 -6
+2 -6
fs/block_dev.c
··· 885 885 spin_lock(&bdev_lock); 886 886 list_del_init(&bdev->bd_list); 887 887 spin_unlock(&bdev_lock); 888 + /* Detach inode from wb early as bdi_put() may free bdi->wb */ 889 + inode_detach_wb(inode); 888 890 if (bdev->bd_bdi != &noop_backing_dev_info) { 889 891 bdi_put(bdev->bd_bdi); 890 892 bdev->bd_bdi = &noop_backing_dev_info; ··· 1877 1875 kill_bdev(bdev); 1878 1876 1879 1877 bdev_write_inode(bdev); 1880 - /* 1881 - * Detaching bdev inode from its wb in __destroy_inode() 1882 - * is too late: the queue which embeds its bdi (along with 1883 - * root wb) can be gone as soon as we put_disk() below. 1884 - */ 1885 - inode_detach_wb(bdev->bd_inode); 1886 1878 } 1887 1879 if (bdev->bd_contains == bdev) { 1888 1880 if (disk->fops->release)
+1
include/linux/writeback.h
··· 237 237 static inline void inode_detach_wb(struct inode *inode) 238 238 { 239 239 if (inode->i_wb) { 240 + WARN_ON_ONCE(!(inode->i_state & I_CLEAR)); 240 241 wb_put(inode->i_wb); 241 242 inode->i_wb = NULL; 242 243 }