From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F402FC433DF for ; Wed, 19 Aug 2020 07:54:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8815920772 for ; Wed, 19 Aug 2020 07:54:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8815920772 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D779A8D0008; Wed, 19 Aug 2020 03:54:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D29058D0001; Wed, 19 Aug 2020 03:54:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C168D8D0008; Wed, 19 Aug 2020 03:54:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0233.hostedemail.com [216.40.44.233]) by kanga.kvack.org (Postfix) with ESMTP id ACC658D0001 for ; Wed, 19 Aug 2020 03:54:36 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 6F18F1EE6 for ; Wed, 19 Aug 2020 07:54:36 +0000 (UTC) X-FDA: 77166556152.28.twist83_5e161bd27026 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin28.hostedemail.com (Postfix) with ESMTP id 42E576C3C for ; Wed, 19 Aug 2020 07:54:36 +0000 (UTC) X-HE-Tag: twist83_5e161bd27026 X-Filterd-Recvd-Size: 7830 Received: from out30-54.freemail.mail.aliyun.com (out30-54.freemail.mail.aliyun.com [115.124.30.54]) by imf09.hostedemail.com (Postfix) with ESMTP for ; Wed, 19 Aug 2020 07:54:33 +0000 (UTC) X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e01422;MF=alex.shi@linux.alibaba.com;NM=1;PH=DS;RN=19;SR=0;TI=SMTPD_---0U6CdRlv_1597823666; Received: from IT-FVFX43SYHV2H.local(mailfrom:alex.shi@linux.alibaba.com fp:SMTPD_---0U6CdRlv_1597823666) by smtp.aliyun-inc.com(127.0.0.1); Wed, 19 Aug 2020 15:54:28 +0800 Subject: Re: [RFC PATCH v2 4/5] mm: Split release_pages work into 3 passes To: Alexander Duyck Cc: yang.shi@linux.alibaba.com, lkp@intel.com, rong.a.chen@intel.com, khlebnikov@yandex-team.ru, kirill@shutemov.name, hughd@google.com, linux-kernel@vger.kernel.org, daniel.m.jordan@oracle.com, linux-mm@kvack.org, shakeelb@google.com, willy@infradead.org, hannes@cmpxchg.org, tj@kernel.org, cgroups@vger.kernel.org, akpm@linux-foundation.org, richard.weiyang@gmail.com, mgorman@techsingularity.net, iamjoonsoo.kim@lge.com References: <20200819041852.23414.95939.stgit@localhost.localdomain> <20200819042730.23414.41309.stgit@localhost.localdomain> From: Alex Shi Message-ID: <15edf807-ce03-83f7-407d-5929341b2b4e@linux.alibaba.com> Date: Wed, 19 Aug 2020 15:53:15 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <20200819042730.23414.41309.stgit@localhost.localdomain> Content-Type: text/plain; charset=utf-8 X-Rspamd-Queue-Id: 42E576C3C X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: =E5=9C=A8 2020/8/19 =E4=B8=8B=E5=8D=8812:27, Alexander Duyck =E5=86=99=E9= =81=93: > From: Alexander Duyck >=20 > 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 c= an > be potentially reacquired by __put_compound_page. >=20 > 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 no= t in > the LRU so they can be freed immediately from those that can't. The sec= ond > 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 pag= es > without needing to worry about if they are in the LRU any further. >=20 > 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 pa= ges. >=20 > Signed-off-by: Alexander Duyck > --- > mm/swap.c | 109 +++++++++++++++++++++++++++++++++++++----------------= -------- > 1 file changed, 67 insertions(+), 42 deletions(-) >=20 > 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 > =20 > +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 =3D NULL; > + unsigned long flags =3D 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 =3D pagevec_count(pvec); i--;) { > + struct page *page =3D pvec->pages[i]; > + > + lruvec =3D 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 > + VM_BUG_ON_PAGE(!PageLRU(page), page); > + __ClearPageLRU(page); > + del_page_from_lru_list(page, lruvec, page_off_lru(page)); > + } > + > + unlock_page_lruvec_irqrestore(lruvec, flags); > + > + /* > + * A batch of pages are no longer on the LRU list. Go through and > + * start the final process of returning the deferred pages to their > + * appropriate freelists. > + */ > + for (i =3D pagevec_count(pvec); i--;) > + __release_page(pvec->pages[i], pages_to_free); > +} > + > /** > * release_pages - batched put_page() > * @pages: array of pages to release > @@ -806,32 +854,24 @@ void lru_add_drain_all(void) > void release_pages(struct page **pages, int nr) > { > int i; > + struct pagevec pvec; > LIST_HEAD(pages_to_free); > - struct lruvec *lruvec =3D NULL; > - unsigned long flags; > - unsigned int lock_batch; > =20 > + pagevec_init(&pvec); > + > + /* > + * We need to first walk through the list cleaning up the low hanging > + * fruit and clearing those pages that either cannot be freed or that > + * are non-LRU. We will store the LRU pages in a pagevec so that we > + * can get to them in the next pass. > + */ > for (i =3D 0; i < nr; i++) { > struct page *page =3D pages[i]; > =20 > - /* > - * Make sure the IRQ-safe lock-holding time does not get > - * excessive with a continuous string of pages from the > - * same lruvec. The lock is held only if lruvec !=3D NULL. > - */ > - if (lruvec && ++lock_batch =3D=3D SWAP_CLUSTER_MAX) { > - unlock_page_lruvec_irqrestore(lruvec, flags); > - lruvec =3D NULL; > - } > - > if (is_huge_zero_page(page)) > continue; > =20 > if (is_zone_device_page(page)) { > - if (lruvec) { > - unlock_page_lruvec_irqrestore(lruvec, flags); > - lruvec =3D NULL; > - } > /* > * ZONE_DEVICE pages that return 'false' from > * put_devmap_managed_page() do not require special > @@ -848,36 +888,21 @@ void release_pages(struct page **pages, int nr) > if (!put_page_testzero(page)) > continue; > =20 > - if (PageCompound(page)) { > - if (lruvec) { > - unlock_page_lruvec_irqrestore(lruvec, flags); > - lruvec =3D NULL; > - } > - __put_compound_page(page); > + if (!PageLRU(page)) { > + __release_page(page, &pages_to_free); > continue; > } > =20 > - if (PageLRU(page)) { > - struct lruvec *prev_lruvec =3D lruvec; > - > - lruvec =3D relock_page_lruvec_irqsave(page, lruvec, > - &flags); > - if (prev_lruvec !=3D lruvec) > - lock_batch =3D 0; > - > - VM_BUG_ON_PAGE(!PageLRU(page), page); > - __ClearPageLRU(page); > - del_page_from_lru_list(page, lruvec, page_off_lru(page)); > + /* record page so we can get it in the next pass */ > + if (!pagevec_add(&pvec, page)) { > + __release_lru_pages(&pvec, &pages_to_free); > + pagevec_reinit(&pvec); > } > - > - /* Clear Active bit in case of parallel mark_page_accessed */ > - __ClearPageActive(page); > - __ClearPageWaiters(page); > - > - list_add(&page->lru, &pages_to_free); > } > - if (lruvec) > - unlock_page_lruvec_irqrestore(lruvec, flags); > + > + /* flush any remaining LRU pages that need to be processed */ > + if (pagevec_count(&pvec)) > + __release_lru_pages(&pvec, &pages_to_free); > =20 > mem_cgroup_uncharge_list(&pages_to_free); > free_unref_page_list(&pages_to_free); >=20