Re: [RFC PATCH v2 4/5] mm: Split release_pages work into 3 passes

From: Alexander Duyck
Date: Wed Aug 19 2020 - 10:57:40 EST


On Wed, Aug 19, 2020 at 12:54 AM Alex Shi <alex.shi@xxxxxxxxxxxxxxxxx> wrote:
>
>
>
> 在 2020/8/19 下午12:27, Alexander Duyck 写道:
> > From: Alexander Duyck <alexander.h.duyck@xxxxxxxxxxxxxxx>
> >
> > The release_pages function has a number of paths that end up with the
> > LRU lock having to be released and reacquired. Such an example would be the
> > freeing of THP pages as it requires releasing the LRU lock so that it can
> > be potentially reacquired by __put_compound_page.
> >
> > In order to avoid that we can split the work into 3 passes, the first
> > without the LRU lock to go through and sort out those pages that are not in
> > the LRU so they can be freed immediately from those that can't. The second
> > pass will then go through removing those pages from the LRU in batches as
> > large as a pagevec can hold before freeing the LRU lock. Once the pages have
> > been removed from the LRU we can then proceed to free the remaining pages
> > without needing to worry about if they are in the LRU any further.
> >
> > The general idea is to avoid bouncing the LRU lock between pages and to
> > hopefully aggregate the lock for up to the full page vector worth of pages.
> >
> > Signed-off-by: Alexander Duyck <alexander.h.duyck@xxxxxxxxxxxxxxx>
> > ---
> > mm/swap.c | 109 +++++++++++++++++++++++++++++++++++++------------------------
> > 1 file changed, 67 insertions(+), 42 deletions(-)
> >
> > diff --git a/mm/swap.c b/mm/swap.c
> > index fe53449fa1b8..b405f81b2c60 100644
> > --- a/mm/swap.c
> > +++ b/mm/swap.c
> > @@ -795,6 +795,54 @@ void lru_add_drain_all(void)
> > }
> > #endif
> >
> > +static void __release_page(struct page *page, struct list_head *pages_to_free)
> > +{
> > + if (PageCompound(page)) {
> > + __put_compound_page(page);
> > + } else {
> > + /* Clear Active bit in case of parallel mark_page_accessed */
> > + __ClearPageActive(page);
> > + __ClearPageWaiters(page);
> > +
> > + list_add(&page->lru, pages_to_free);
> > + }
> > +}
> > +
> > +static void __release_lru_pages(struct pagevec *pvec,
> > + struct list_head *pages_to_free)
> > +{
> > + struct lruvec *lruvec = NULL;
> > + unsigned long flags = 0;
> > + int i;
> > +
> > + /*
> > + * The pagevec at this point should contain a set of pages with
> > + * their reference count at 0 and the LRU flag set. We will now
> > + * need to pull the pages from their LRU lists.
> > + *
> > + * We walk the list backwards here since that way we are starting at
> > + * the pages that should be warmest in the cache.
> > + */
> > + for (i = pagevec_count(pvec); i--;) {
> > + struct page *page = pvec->pages[i];
> > +
> > + lruvec = relock_page_lruvec_irqsave(page, lruvec, &flags);
>
> the lock bounce is better with the patch, would you like to do further
> like using add_lruvecs to reduce bounce more?
>
> Thanks
> Alex

I'm not sure how much doing something like that would add. In my case
I had a very specific issue that this is addressing which is the fact
that every compound page was taking the LRU lock and zone lock
separately. With this patch that is reduced to one LRU lock per 15
pages and then the zone lock per page. By adding or sorting pages by
lruvec I am not sure there will be much benefit as I am not certain
how often we will end up with pages being interleaved between multiple
lruvecs. In addition as I am limiting the quantity to a pagevec which
limits the pages to 15 I am not sure there will be much benefit to be
seen for sorting the pages beforehand.

Thanks.

- Alex