Re: [BUG?] invalidate_lock not held in read_cache_folio()

From: Matthew Wilcox

Date: Mon Jun 08 2026 - 10:12:19 EST


On Mon, Jun 08, 2026 at 06:59:46PM +0530, Dev Jain wrote:
>
>
> On 08/06/26 5:49 pm, 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
>
> Yep, I realized that later, thanks : )
>
> Just that the kerneldoc says "Expects mapping->invalidate_lock to be held"
> so was wondering whether this comment is appropriate. Difficult for me to
> reason about that since this goes into fs/block code.

Yes, you've found a documentation bug.

+ BUG_ON(!inode_is_locked(mapping->host) &&
+ !rwsem_is_locked(&mapping->invalidate_lock) &&
+ !S_ISBLK(mapping->host->i_mode));

might do what you're looking for?