Re: [PATCH 2/3] mm/migrate: Convert isolate_movable_page() to use folios

From: Matthew Wilcox
Date: Mon Jan 23 2023 - 10:48:36 EST


On Sun, Jan 22, 2023 at 12:46:34PM +0000, Matthew Wilcox wrote:
> On Fri, Jan 20, 2023 at 04:56:21PM -0800, Vishal Moola (Oracle) wrote:
> > int isolate_movable_page(struct page *page, isolate_mode_t mode)
> > {
> > + struct folio *folio = page_folio(page);
> > const struct movable_operations *mops;
> >
> > /*
> > @@ -71,11 +72,11 @@ int isolate_movable_page(struct page *page, isolate_mode_t mode)
> > * the put_page() at the end of this block will take care of
> > * release this page, thus avoiding a nasty leakage.
> > */
> > - if (unlikely(!get_page_unless_zero(page)))
> > + if (unlikely(!folio_try_get(folio)))
>
> This changes behaviour. Previously when called on a tail page, the
> call failed. Now it succeeds, getting a ref on something that at
> least was the folio head at some point.
>
> If you're going to do this, you need to recheck that the page is still
> part of the folio after getting the ref (see gup.c for an example).
> But I think we should probably maintain the behaviour of failing on
> tail pages.
>
> Maybe something like ...
>
> if (unlikely(!get_page_unless_zero(page)))
> goto out;
> /* Refcount is zero on tail pages, so we must have a head */
> folio = (struct folio *)page;

I've been thinking about this some more as I don't like doing these
kinds of casts (except in the helper functions). What do you think
to adding:

struct folio *folio_get_nontail_page(struct page *)
{
if unlikely(!get_page_unless_zero(page))
return NULL;
return (struct folio *)page;
}

and then isolate_movable_page() looks like:

struct folio *folio;
[...]

folio = folio_get_nontail_page(page);
if (!folio)
goto out;

I keep thinking about how this is all going to work when we get to
one-pointer-per-page. Telling tail pages from head pages becomes hard.
This probably becomes an out-of-line function that looks something like ..

struct memdesc *memdesc = READ_ONCE(page->memdesc);
struct folio *folio;

if (!memdesc_is_folio(memdesc))
return NULL;
folio = memdesc_folio(memdesc);
if (!folio_try_get(folio))
return NULL;
if (READ_ONCE(page->memdesc) != memdesc ||
folio->pfn != page_pfn(page)) {
folio_put(folio);
return NULL;
}

return folio;

(note: We need to check that page->memdesc still points to this folio after
getting the refcount on it. we could loop around if it fails, but
failing the entire get is OK; if memdesc changed then either before this
function was called, after this function was called or while this
function was called, the refcount on the memdesc it was pointing to
was zero)