Re: [BUG?] invalidate_lock not held in read_cache_folio()
From: Jan Kara
Date: Tue Jun 09 2026 - 08:03:47 EST
On Mon 08-06-26 13:19:30, Matthew Wilcox wrote:
> On Mon, Jun 08, 2026 at 04:54:51PM +0530, Dev Jain wrote:
> > Hi,
> >
> > My understanding is that there are two ways we are serializing against concurrent
> > addition of a folio into the pagecache (see 730633f0b7f9).
> >
> > The kerneldoc of read_cache_folio() says:
> >
> > "Context: May sleep. Expects mapping->invalidate_lock to be held."
> >
> > I put the following in __filemap_add_folio():
> >
> > + BUG_ON(!inode_is_locked(mapping->host) &&
> > + !rwsem_is_locked(&mapping->invalidate_lock));
> >
> >
> > And got the following trace:
> >
> >
> > [ 0.115587] Call trace:
> > [ 0.115686] __filemap_add_folio+0x5dc/0x680 (P)
> > [ 0.115871] filemap_add_folio+0xec/0x2d0
> > [ 0.116031] do_read_cache_folio+0x144/0x2d8
> > [ 0.116194] read_cache_folio+0x1c/0x30
> > [ 0.116344] read_part_sector+0x4c/0xf0
> > [ 0.116499] read_lba+0xb0/0x1a0
> > [ 0.116627] efi_partition+0xa8/0x740
> > [ 0.116769] bdev_disk_changed+0x238/0x620
> > [ 0.116939] blkdev_get_whole+0xac/0x100
> > [ 0.117090] bdev_open+0x280/0x3c0
>
> You can't truncate or holepunch a bdev
You'd hope so but see blkdev_fallocate(). You can punch out pages in the
middle of the bdev mapping. And the blockdev code actually takes care to
grab invalidate_lock around the punching ops. I'd say that
read_part_sector() should grab invalidate_lock to protect against these
races.
Honza
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR