Re: [PATCH 1/2] mm: Allow architectures to request 'old' entries when prefaulting

From: Kirill A. Shutemov
Date: Tue Dec 22 2020 - 05:01:42 EST


On Sat, Dec 19, 2020 at 12:34:17PM -0800, Linus Torvalds wrote:
> On Sat, Dec 19, 2020 at 4:41 AM Kirill A. Shutemov <kirill@xxxxxxxxxxxxx> wrote:
> >
> > @@ -2884,19 +2966,18 @@ void filemap_map_pages(struct vm_fault *vmf,
> > if (vmf->pte)
> > vmf->pte += xas.xa_index - last_pgoff;
> > last_pgoff = xas.xa_index;
> > - if (alloc_set_pte(vmf, page))
> > - goto unlock;
> > + if (pte_none(*vmf->pte))
> > + do_set_pte(vmf, page);
> > + /* no need to invalidate: a not-present page won't be cached */
> > + update_mmu_cache(vma, vmf->address, vmf->pte);
> > unlock_page(head);
> > - goto next;
> > + continue;
>
> This can't be right.
>
> Look at what happens if "pte_none()" is not true.. It won't install
> the new pte, but it also won't drop the ref to the page.

Ouch.

> So I think it needs to be
>
> - if (alloc_set_pte(vmf, page))
> + if (!pte_none(*vmf->pte))
> goto unlock;
> + do_set_pte(vmf, page);
>
> instead, so that the "if somebody else already filled the page table"
> case gets handled right.

Updated patch is below.