Re: [PATCH v14 071/138] mm/writeback: Add filemap_dirty_folio()

Matthew Wilcox
2021-08-15T03:32:10Z

On Thu, Aug 12, 2021 at 06:07:05PM +0200, Vlastimil Babka wrote:
> On 7/15/21 5:35 AM, Matthew Wilcox (Oracle) wrote:
> > Reimplement __set_page_dirty_nobuffers() as a wrapper around
> > filemap_dirty_folio().
>
> I assume it becomes obvious later why the new "mapping" parameter instead of
> taking it from the folio, but maybe the changelog should say it here?

---

mm/writeback: Add filemap_dirty_folio()

Reimplement __set_page_dirty_nobuffers() as a wrapper around
filemap_dirty_folio(). Eventually folio_mark_dirty() will pass
the folio's mapping to the address space's ->dirty_folio()
operation, so add the parameter to filemap_dirty_folio() now.

---

Nobody seems quite sure whether it's possible to truncate (or otherwise
remove) a page from a file while it's being marked as dirty. viz:

int set_page_dirty(struct page *page)
{
struct address_space *mapping = page_mapping(page);
if (likely(mapping)) {
...
return mapping->a_ops->set_page_dirty(page);
}

so ->set_page_dirty can only be called if page has a mapping (obviously,
otherwise we wouldn't know whose ->set_page_dirty to call). But then
in __set_page_dirty_nobuffers(), we check to see if mapping has
become unset:

if (!TestSetPageDirty(page)) {
struct address_space *mapping = page_mapping(page);

if (!mapping) {
unlock_page_memcg(page);
return 1;
}

Confusingly, the comment to __set_page_dirty_nobuffers says:

* The caller must ensure this doesn't race with truncation. Most will simply
* hold the page lock, but e.g. zap_pte_range() calls with the page mapped and
* the pte lock held, which also locks out truncation.

I believe this is left-over from commit 2d6d7f982846 in 2015.

Anyway, passing mapping as a parameter is something we already do for
just about every other address_space operation, and we already called
page_mapping() to get it, so why make the callee call it again? Not to
mention people get confused about whether to call page_mapping() or just
look at page->mapping. Changing the ->set_page_dirty() operation to
->dirty_folio() is something I've postponed until the 5.17/5.18 timeframe,
but we might as well pass the parameter to filemap_dirty_folio() now.