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=-11.2 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 DC179C4363D for ; Tue, 22 Sep 2020 05:02:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4278923A5C for ; Tue, 22 Sep 2020 05:02:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4278923A5C 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 948246B00A3; Tue, 22 Sep 2020 01:02:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FA826B00A4; Tue, 22 Sep 2020 01:02:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7E87F6B00A5; Tue, 22 Sep 2020 01:02:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0109.hostedemail.com [216.40.44.109]) by kanga.kvack.org (Postfix) with ESMTP id 6436A6B00A3 for ; Tue, 22 Sep 2020 01:02:39 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 1DBA0180AD804 for ; Tue, 22 Sep 2020 05:02:39 +0000 (UTC) X-FDA: 77289502038.22.bike47_53131942714b Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id F3B1218038E67 for ; Tue, 22 Sep 2020 05:02:38 +0000 (UTC) X-HE-Tag: bike47_53131942714b X-Filterd-Recvd-Size: 8140 Received: from out30-57.freemail.mail.aliyun.com (out30-57.freemail.mail.aliyun.com [115.124.30.57]) by imf28.hostedemail.com (Postfix) with ESMTP for ; Tue, 22 Sep 2020 05:02:37 +0000 (UTC) X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R581e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04423;MF=alex.shi@linux.alibaba.com;NM=1;PH=DS;RN=21;SR=0;TI=SMTPD_---0U9jveFZ_1600750949; Received: from IT-FVFX43SYHV2H.local(mailfrom:alex.shi@linux.alibaba.com fp:SMTPD_---0U9jveFZ_1600750949) by smtp.aliyun-inc.com(127.0.0.1); Tue, 22 Sep 2020 13:02:30 +0800 Subject: Re: [PATCH v18 19/32] mm/swap.c: serialize memcg changes in pagevec_lru_move_fn To: Hugh Dickins Cc: akpm@linux-foundation.org, mgorman@techsingularity.net, tj@kernel.org, khlebnikov@yandex-team.ru, daniel.m.jordan@oracle.com, willy@infradead.org, hannes@cmpxchg.org, lkp@intel.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, shakeelb@google.com, iamjoonsoo.kim@lge.com, richard.weiyang@gmail.com, kirill@shutemov.name, alexander.duyck@gmail.com, rong.a.chen@intel.com, mhocko@suse.com, vdavydov.dev@gmail.com, shy828301@gmail.com References: <1598273705-69124-1-git-send-email-alex.shi@linux.alibaba.com> <1598273705-69124-20-git-send-email-alex.shi@linux.alibaba.com> From: Alex Shi Message-ID: <721fd79e-453c-4832-22e3-4e95d3c9b6b0@linux.alibaba.com> Date: Tue, 22 Sep 2020 13:00:22 +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: Content-Type: text/plain; charset=gbk 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: =D4=DA 2020/9/22 =C9=CF=CE=E78:42, Hugh Dickins =D0=B4=B5=C0: > On Mon, 24 Aug 2020, Alex Shi wrote: >=20 >> Hugh Dickins' found a memcg change bug on original version: >> If we want to change the pgdat->lru_lock to memcg's lruvec lock, we ha= ve >> to serialize mem_cgroup_move_account during pagevec_lru_move_fn. The >> possible bad scenario would like: >> >> cpu 0 cpu 1 >> lruvec =3D mem_cgroup_page_lruvec() >> if (!isolate_lru_page()) >> mem_cgroup_move_account >> >> spin_lock_irqsave(&lruvec->lru_lock <=3D=3D wrong lock. >> >> So we need the ClearPageLRU to block isolate_lru_page(), that serializ= es >=20 > s/the ClearPageLRU/TestClearPageLRU/ Thanks, will change it. >=20 >> the memcg change. and then removing the PageLRU check in move_fn calle= e >> as the consequence. >=20 > Deserves another paragraph about __pagevec_lru_add(): > "__pagevec_lru_add_fn() is different from the others, because the pages > it deals with are, by definition, not yet on the lru. TestClearPageLRU > is not needed and would not work, so __pagevec_lru_add() goes its own w= ay." Thanks for comments! will add it into new commit log. >=20 >> >> Reported-by: Hugh Dickins >=20 > True. >=20 >> Signed-off-by: Hugh Dickins >=20 > I did provide some lines, but I think it's just > Acked-by: Hugh Dickins > to go below your Signed-off-by. Thanks! >=20 >> Signed-off-by: Alex Shi >> Cc: Andrew Morton >> Cc: linux-mm@kvack.org >> Cc: linux-kernel@vger.kernel.org >> --- >> mm/swap.c | 44 +++++++++++++++++++++++++++++++++++--------- >> 1 file changed, 35 insertions(+), 9 deletions(-) >=20 > In your lruv19 branch, this patch got renamed (s/moveing/moving/): > but I think it's better with the old name used here in v18, and without > those mm/vmscan.c mods to check_move_unevictable_pages() tacked on: > please move those back to 16/32, which already makes changes to vmscan.= c. >=20 Yes, will move that part there. Thanks! Alex >> >> diff --git a/mm/swap.c b/mm/swap.c >> index 446ffe280809..2d9a86bf93a4 100644 >> --- a/mm/swap.c >> +++ b/mm/swap.c >> @@ -221,8 +221,14 @@ static void pagevec_lru_move_fn(struct pagevec *p= vec, >> spin_lock_irqsave(&pgdat->lru_lock, flags); >> } >> =20 >> + /* block memcg migration during page moving between lru */ >> + if (!TestClearPageLRU(page)) >> + continue; >> + >> lruvec =3D mem_cgroup_page_lruvec(page, pgdat); >> (*move_fn)(page, lruvec); >> + >> + SetPageLRU(page); >> } >> if (pgdat) >> spin_unlock_irqrestore(&pgdat->lru_lock, flags); >> @@ -232,7 +238,7 @@ static void pagevec_lru_move_fn(struct pagevec *pv= ec, >> =20 >> static void pagevec_move_tail_fn(struct page *page, struct lruvec *lr= uvec) >> { >> - if (PageLRU(page) && !PageUnevictable(page)) { >> + if (!PageUnevictable(page)) { >> del_page_from_lru_list(page, lruvec, page_lru(page)); >> ClearPageActive(page); >> add_page_to_lru_list_tail(page, lruvec, page_lru(page)); >> @@ -306,7 +312,7 @@ void lru_note_cost_page(struct page *page) >> =20 >> static void __activate_page(struct page *page, struct lruvec *lruvec) >> { >> - if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) { >> + if (!PageActive(page) && !PageUnevictable(page)) { >> int lru =3D page_lru_base_type(page); >> int nr_pages =3D thp_nr_pages(page); >> =20 >> @@ -362,7 +368,8 @@ void activate_page(struct page *page) >> =20 >> page =3D compound_head(page); >> spin_lock_irq(&pgdat->lru_lock); >> - __activate_page(page, mem_cgroup_page_lruvec(page, pgdat)); >> + if (PageLRU(page)) >> + __activate_page(page, mem_cgroup_page_lruvec(page, pgdat)); >> spin_unlock_irq(&pgdat->lru_lock); >> } >> #endif >=20 > Every time I look at this, I wonder if that's right, or an unnecessary > optimization strayed in, or whatever. For the benefit of others lookin= g > at this patch, yes it is right: this is the !CONFIG_SMP alternative > version of activate_page(), and needs that PageLRU check to compensate > for the check that has now been removed from __activate_page() itself. >=20 >> @@ -521,9 +528,6 @@ static void lru_deactivate_file_fn(struct page *pa= ge, struct lruvec *lruvec) >> bool active; >> int nr_pages =3D thp_nr_pages(page); >> =20 >> - if (!PageLRU(page)) >> - return; >> - >> if (PageUnevictable(page)) >> return; >> =20 >> @@ -564,7 +568,7 @@ static void lru_deactivate_file_fn(struct page *pa= ge, struct lruvec *lruvec) >> =20 >> static void lru_deactivate_fn(struct page *page, struct lruvec *lruve= c) >> { >> - if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) { >> + if (PageActive(page) && !PageUnevictable(page)) { >> int lru =3D page_lru_base_type(page); >> int nr_pages =3D thp_nr_pages(page); >> =20 >> @@ -581,7 +585,7 @@ static void lru_deactivate_fn(struct page *page, s= truct lruvec *lruvec) >> =20 >> static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec) >> { >> - if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) && >> + if (PageAnon(page) && PageSwapBacked(page) && >> !PageSwapCache(page) && !PageUnevictable(page)) { >> bool active =3D PageActive(page); >> int nr_pages =3D thp_nr_pages(page); >> @@ -979,7 +983,29 @@ static void __pagevec_lru_add_fn(struct page *pag= e, struct lruvec *lruvec) >> */ >> void __pagevec_lru_add(struct pagevec *pvec) >> { >> - pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn); >> + int i; >> + struct pglist_data *pgdat =3D NULL; >> + struct lruvec *lruvec; >> + unsigned long flags =3D 0; >> + >> + for (i =3D 0; i < pagevec_count(pvec); i++) { >> + struct page *page =3D pvec->pages[i]; >> + struct pglist_data *pagepgdat =3D page_pgdat(page); >> + >> + if (pagepgdat !=3D pgdat) { >> + if (pgdat) >> + spin_unlock_irqrestore(&pgdat->lru_lock, flags); >> + pgdat =3D pagepgdat; >> + spin_lock_irqsave(&pgdat->lru_lock, flags); >> + } >> + >> + lruvec =3D mem_cgroup_page_lruvec(page, pgdat); >> + __pagevec_lru_add_fn(page, lruvec); >> + } >> + if (pgdat) >> + spin_unlock_irqrestore(&pgdat->lru_lock, flags); >> + release_pages(pvec->pages, pvec->nr); >> + pagevec_reinit(pvec); >> } >> =20 >> /** >> --=20 >> 1.8.3.1