Race between freeing and waking page

From: Matthew Wilcox
Date: Thu Aug 27 2020 - 08:46:15 EST


On Thu, Aug 27, 2020 at 12:01:00AM -0700, Hugh Dickins wrote:
> It was a crash from checking PageWaiters on a Tail in wake_up_page(),
> called from end_page_writeback(), from ext4_finish_bio(): yet the
> page a tail of a shmem huge page. Linus's wake_up_page_bit() changes?
> No, I don't think so. It seems to me that once end_page_writeback()
> has done its test_clear_page_writeback(), it has no further hold on
> the struct page, which could be reused as part of a compound page
> by the time of wake_up_page()'s PageWaiters check. But I probably
> need to muse on that for longer.

I think you're right. Example:

truncate_inode_pages_range()
pagevec_lookup_entries()
lock_page()

--- ctx switch ---

ext4_finish_bio()
end_page_writeback()
test_clear_page_writeback()

--- ctx switch ---

wait_on_page_writeback() <- noop
truncate_inode_page()
unlock_page()
pagevec_release()

... page can now be allocated

--- ctx switch ---

wake_up_page()
PageWaiters then has that check for PageTail.

This isn't unique to ext4; the iomap completion path behaves the exact
same way. The thing is, this is a harmless race. It seems unnecessary
for anybody here to incur the overhead of adding a page ref to be sure
the page isn't reallocated. We don't want to wake up the waiters before
clearing the bit in question.

I'm tempted to suggest this:

static void wake_up_page(struct page *page, int bit)
{
- if (!PageWaiters(page))
+ if (PageTail(page) || !PageWaiters(page))
return;
wake_up_page_bit(page, bit);

which only adds an extra read to the struct page that we were going to
access anyway. Even that seems unnecessary though; PageWaiters is
going to be clear. Maybe we can just change the PF policy from
PF_ONLY_HEAD to PF_ANY. I don't think it's critical that we have this
check.

Nick?