On Mon, Sep 30, 2024 at 01:12:37PM -0700, Linus Torvalds wrote: > It's basically been that way forever. The code has changed many times, > but we've basically always had that "wait on bit will wait not until > the next wakeup, but until it actually sees the bit being clear". > > And by "always" I mean "going back at least to before the git tree". I > didn't search further. It's not new. > > The only reason I pointed at that (relatively recent) commit from 2021 > is that when we rewrote the page bit waiting logic (for some unrelated > horrendous scalability issues with tens of thousands of pages on wait > queues), the rewritten code _tried_ to not do it, and instead go "we > were woken up by a bit clear op, so now we've waited enough". > > And that then caused problems as explained in that commit c2407cf7d22d > ("mm: make wait_on_page_writeback() wait for multiple pending > writebacks") because the wakeups aren't atomic wrt the actual bit > setting/clearing/testing. Could we break out if folio->mapping has changed? Clearly if it has, we're no longer waiting for the folio we thought we were waiting for, but for a folio which now belongs to a different file. maybe this: +void __folio_wait_writeback(struct address_space *mapping, struct folio *folio) +{ + while (folio_test_writeback(folio) && folio->mapping == mapping) { + trace_folio_wait_writeback(folio, mapping); + folio_wait_bit(folio, PG_writeback); + } +} [...] void folio_wait_writeback(struct folio *folio) { - while (folio_test_writeback(folio)) { - trace_folio_wait_writeback(folio, folio_mapping(folio)); - folio_wait_bit(folio, PG_writeback); - } + __folio_wait_writeback(folio->mapping, folio); }