Re: [PATCH v2] mm/z3fold.c: Lock z3fold page before __SetPageMovable()

From: Henry Burns
Date: Mon Jul 01 2019 - 21:17:09 EST


On Mon, Jul 1, 2019 at 6:00 PM Shakeel Butt <shakeelb@xxxxxxxxxx> wrote:
>
> On Mon, Jul 1, 2019 at 5:51 PM Henry Burns <henryburns@xxxxxxxxxx> wrote:
> >
> > __SetPageMovable() expects it's page to be locked, but z3fold.c doesn't
> > lock the page. Following zsmalloc.c's example we call trylock_page() and
> > unlock_page(). Also makes z3fold_page_migrate() assert that newpage is
> > passed in locked, as documentation.
> >
> > Signed-off-by: Henry Burns <henryburns@xxxxxxxxxx>
> > Suggested-by: Vitaly Wool <vitalywool@xxxxxxxxx>
> > ---
> > Changelog since v1:
> > - Added an if statement around WARN_ON(trylock_page(page)) to avoid
> > unlocking a page locked by a someone else.
> >
> > mm/z3fold.c | 6 +++++-
> > 1 file changed, 5 insertions(+), 1 deletion(-)
> >
> > diff --git a/mm/z3fold.c b/mm/z3fold.c
> > index e174d1549734..6341435b9610 100644
> > --- a/mm/z3fold.c
> > +++ b/mm/z3fold.c
> > @@ -918,7 +918,10 @@ static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp,
> > set_bit(PAGE_HEADLESS, &page->private);
> > goto headless;
> > }
> > - __SetPageMovable(page, pool->inode->i_mapping);
> > + if (!WARN_ON(!trylock_page(page))) {
> > + __SetPageMovable(page, pool->inode->i_mapping);
> > + unlock_page(page);
> > + }
>
> Can you please comment why lock_page() is not used here?
Since z3fold_alloc can be called in atomic or non atomic context,
calling lock_page() could trigger a number of
warnings about might_sleep() being called in atomic context. WARN_ON
should avoid the problem described
above as well, and in any weird condition where someone else has the
page lock, we can avoid calling
__SetPageMovable().
>
> > z3fold_page_lock(zhdr);
> >
> > found:
> > @@ -1325,6 +1328,7 @@ static int z3fold_page_migrate(struct address_space *mapping, struct page *newpa
> >
> > VM_BUG_ON_PAGE(!PageMovable(page), page);
> > VM_BUG_ON_PAGE(!PageIsolated(page), page);
> > + VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
> >
> > zhdr = page_address(page);
> > pool = zhdr_to_pool(zhdr);
> > --
> > 2.22.0.410.gd8fdbe21b5-goog
> >