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=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 00631C4727E for ; Tue, 22 Sep 2020 00:42:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AC6F923A84 for ; Tue, 22 Sep 2020 00:42:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nHYRBUyb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729410AbgIVAms (ORCPT ); Mon, 21 Sep 2020 20:42:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728701AbgIVAms (ORCPT ); Mon, 21 Sep 2020 20:42:48 -0400 Received: from mail-oo1-xc43.google.com (mail-oo1-xc43.google.com [IPv6:2607:f8b0:4864:20::c43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2DABC061755 for ; Mon, 21 Sep 2020 17:42:46 -0700 (PDT) Received: by mail-oo1-xc43.google.com with SMTP id o20so3740636ook.1 for ; Mon, 21 Sep 2020 17:42:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=R2R7WH/B9uorM3A755wPvM+NF9B+5Y9TzKQCMLEQCzw=; b=nHYRBUybpof6BfO9qgW0DwAlj2Q8hrrwjsx+WrbRtnVElQvMV8mPnKlrB5f1vHCE6K f1YW6WLqfPCzHihvOiBU5+kQFm/N7Ql9wLsJdpI2XMtrKheCjme5CR3XsZhHIdYiiusr x3Hwt+vxQk9fH5cMXiaaGXe/qO7DPFWCCii4vIfjQr4CuVwlmLMYlPj45b+eug9QUGlw M2kzP+M5/BOTbtNMToaUteYtoRH1jXm+Z267vcqLLCVrC0SpkvzNrURe8sgWTJhg/g/9 TpD4sFqfoJ0mdzJBoTT7KEW8YrbNqybsJlF+9F7vzTyMYrWUiuEX6SDNK8RYZqEXIBCw aqNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=R2R7WH/B9uorM3A755wPvM+NF9B+5Y9TzKQCMLEQCzw=; b=cvYCEpgAmuppJVi39RxinRfQCYCtnIyczKvi1S7IoQj9qgskFRl4uah0qO9P/PPf8C l0Oe+PsqY/eW7DQurRaaQRZTRu6cSpJ9UMCG7IZVbNDGuo9qARpvvJlkW0r7p3HIThPc Jwej4GirDtHG9tZs8BaQUfqPy/tOJXpeyGqtC7wUzbjBPHARLfgYq/awp/pFjuHns4PC 8et6S5OAhV/4f6GeaOUgUS6q3qH2AT2s0r3ITu+3Rj58U+odIy/5e0Dxfenxe+ACurWd qf752TK8BUvXcaKjgSABa6fAmlGjCb0fzE7GD3CsJvZ4sSmKYBECbGGSyuTzIadGz5yE +NLw== X-Gm-Message-State: AOAM532C6QT9kbMr/Onpxjbwq7tMryCwZZNs29L8u1NdJWe+AJCVnX+n L3CpFysg6fgWa0suKJ2nMStJyA== X-Google-Smtp-Source: ABdhPJw1u7dTAMqNjdfZpV1uZ7U6wjy6vAvFezS5ILPqoII1fWNCGnPgAYMBjfTuSe9KtTndRZ0zJQ== X-Received: by 2002:a4a:bb05:: with SMTP id f5mr1334298oop.5.1600735366120; Mon, 21 Sep 2020 17:42:46 -0700 (PDT) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id b16sm6379801otq.31.2020.09.21.17.42.42 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Mon, 21 Sep 2020 17:42:45 -0700 (PDT) Date: Mon, 21 Sep 2020 17:42:41 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Alex Shi cc: akpm@linux-foundation.org, mgorman@techsingularity.net, tj@kernel.org, hughd@google.com, 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 Subject: Re: [PATCH v18 19/32] mm/swap.c: serialize memcg changes in pagevec_lru_move_fn In-Reply-To: <1598273705-69124-20-git-send-email-alex.shi@linux.alibaba.com> Message-ID: References: <1598273705-69124-1-git-send-email-alex.shi@linux.alibaba.com> <1598273705-69124-20-git-send-email-alex.shi@linux.alibaba.com> User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 24 Aug 2020, Alex Shi wrote: > 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 have > to serialize mem_cgroup_move_account during pagevec_lru_move_fn. The > possible bad scenario would like: > > cpu 0 cpu 1 > lruvec = mem_cgroup_page_lruvec() > if (!isolate_lru_page()) > mem_cgroup_move_account > > spin_lock_irqsave(&lruvec->lru_lock <== wrong lock. > > So we need the ClearPageLRU to block isolate_lru_page(), that serializes s/the ClearPageLRU/TestClearPageLRU/ > the memcg change. and then removing the PageLRU check in move_fn callee > as the consequence. 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 way." > > Reported-by: Hugh Dickins True. > Signed-off-by: Hugh Dickins I did provide some lines, but I think it's just Acked-by: Hugh Dickins to go below your Signed-off-by. > 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(-) 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. > > 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 *pvec, > spin_lock_irqsave(&pgdat->lru_lock, flags); > } > > + /* block memcg migration during page moving between lru */ > + if (!TestClearPageLRU(page)) > + continue; > + > lruvec = 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 *pvec, > > static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec) > { > - 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) > > static void __activate_page(struct page *page, struct lruvec *lruvec) > { > - if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) { > + if (!PageActive(page) && !PageUnevictable(page)) { > int lru = page_lru_base_type(page); > int nr_pages = thp_nr_pages(page); > > @@ -362,7 +368,8 @@ void activate_page(struct page *page) > > page = 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 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 looking 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. > @@ -521,9 +528,6 @@ static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec) > bool active; > int nr_pages = thp_nr_pages(page); > > - if (!PageLRU(page)) > - return; > - > if (PageUnevictable(page)) > return; > > @@ -564,7 +568,7 @@ static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec) > > static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec) > { > - if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) { > + if (PageActive(page) && !PageUnevictable(page)) { > int lru = page_lru_base_type(page); > int nr_pages = thp_nr_pages(page); > > @@ -581,7 +585,7 @@ static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec) > > 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 = PageActive(page); > int nr_pages = thp_nr_pages(page); > @@ -979,7 +983,29 @@ static void __pagevec_lru_add_fn(struct page *page, 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 = NULL; > + struct lruvec *lruvec; > + unsigned long flags = 0; > + > + for (i = 0; i < pagevec_count(pvec); i++) { > + struct page *page = pvec->pages[i]; > + struct pglist_data *pagepgdat = page_pgdat(page); > + > + if (pagepgdat != pgdat) { > + if (pgdat) > + spin_unlock_irqrestore(&pgdat->lru_lock, flags); > + pgdat = pagepgdat; > + spin_lock_irqsave(&pgdat->lru_lock, flags); > + } > + > + lruvec = 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); > } > > /** > -- > 1.8.3.1 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=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1,USER_IN_DEF_DKIM_WL 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 73C42C4727C for ; Tue, 22 Sep 2020 00:42:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DB37B23A8B for ; Tue, 22 Sep 2020 00:42:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nHYRBUyb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DB37B23A8B Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 03177900015; Mon, 21 Sep 2020 20:42:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EFD4C900012; Mon, 21 Sep 2020 20:42:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEB82900015; Mon, 21 Sep 2020 20:42:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0022.hostedemail.com [216.40.44.22]) by kanga.kvack.org (Postfix) with ESMTP id C2797900012 for ; Mon, 21 Sep 2020 20:42:47 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 8A2021EE6 for ; Tue, 22 Sep 2020 00:42:47 +0000 (UTC) X-FDA: 77288847174.27.lead95_3406acf27149 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin27.hostedemail.com (Postfix) with ESMTP id 6638A3D668 for ; Tue, 22 Sep 2020 00:42:47 +0000 (UTC) X-HE-Tag: lead95_3406acf27149 X-Filterd-Recvd-Size: 9440 Received: from mail-oo1-f66.google.com (mail-oo1-f66.google.com [209.85.161.66]) by imf43.hostedemail.com (Postfix) with ESMTP for ; Tue, 22 Sep 2020 00:42:46 +0000 (UTC) Received: by mail-oo1-f66.google.com with SMTP id c4so1264230oou.6 for ; Mon, 21 Sep 2020 17:42:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=R2R7WH/B9uorM3A755wPvM+NF9B+5Y9TzKQCMLEQCzw=; b=nHYRBUybpof6BfO9qgW0DwAlj2Q8hrrwjsx+WrbRtnVElQvMV8mPnKlrB5f1vHCE6K f1YW6WLqfPCzHihvOiBU5+kQFm/N7Ql9wLsJdpI2XMtrKheCjme5CR3XsZhHIdYiiusr x3Hwt+vxQk9fH5cMXiaaGXe/qO7DPFWCCii4vIfjQr4CuVwlmLMYlPj45b+eug9QUGlw M2kzP+M5/BOTbtNMToaUteYtoRH1jXm+Z267vcqLLCVrC0SpkvzNrURe8sgWTJhg/g/9 TpD4sFqfoJ0mdzJBoTT7KEW8YrbNqybsJlF+9F7vzTyMYrWUiuEX6SDNK8RYZqEXIBCw aqNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=R2R7WH/B9uorM3A755wPvM+NF9B+5Y9TzKQCMLEQCzw=; b=Zy++gBSXoPLJN6ekyTCgZwOjx3Gu6fz1hWiQtA6HyuMD27lI1cy5a5K+Q94FdT8Ovp Ehy39rXv6pd+pyiTKMqFW1d4oTyWmlhdaFVYqpjWVN3NMOV00BZvcvAVr082kpYQPADv vEdyVFnWJlCvwG8AzeP72x0Iweee5dxPpMgWhW2GV0vV1aUGFa372SfPh6jy2Eq7dC3Y eQ+DtNweDTZatWvJKZKVQbowuUZfra0aFviPhHN5R1za+yGN8z0MmWajdW5tOihdVbY0 56KoNVdFRpyuWxIRQ6+NTeXbdm18KxTwvy+5xfnkKYKxwqt67hgTUJp8EClc9hYswceL bsFg== X-Gm-Message-State: AOAM531ofiq1aXzMcv54kD4hO3WVP4BiXUugMixPBYp2lagsi6URxckF zFIDCZLNqUAOxDmyj0+6D+fMpw== X-Google-Smtp-Source: ABdhPJw1u7dTAMqNjdfZpV1uZ7U6wjy6vAvFezS5ILPqoII1fWNCGnPgAYMBjfTuSe9KtTndRZ0zJQ== X-Received: by 2002:a4a:bb05:: with SMTP id f5mr1334298oop.5.1600735366120; Mon, 21 Sep 2020 17:42:46 -0700 (PDT) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id b16sm6379801otq.31.2020.09.21.17.42.42 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Mon, 21 Sep 2020 17:42:45 -0700 (PDT) Date: Mon, 21 Sep 2020 17:42:41 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Alex Shi cc: akpm@linux-foundation.org, mgorman@techsingularity.net, tj@kernel.org, hughd@google.com, 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 Subject: Re: [PATCH v18 19/32] mm/swap.c: serialize memcg changes in pagevec_lru_move_fn In-Reply-To: <1598273705-69124-20-git-send-email-alex.shi@linux.alibaba.com> Message-ID: References: <1598273705-69124-1-git-send-email-alex.shi@linux.alibaba.com> <1598273705-69124-20-git-send-email-alex.shi@linux.alibaba.com> User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII 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: On Mon, 24 Aug 2020, Alex Shi wrote: > 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 have > to serialize mem_cgroup_move_account during pagevec_lru_move_fn. The > possible bad scenario would like: > > cpu 0 cpu 1 > lruvec = mem_cgroup_page_lruvec() > if (!isolate_lru_page()) > mem_cgroup_move_account > > spin_lock_irqsave(&lruvec->lru_lock <== wrong lock. > > So we need the ClearPageLRU to block isolate_lru_page(), that serializes s/the ClearPageLRU/TestClearPageLRU/ > the memcg change. and then removing the PageLRU check in move_fn callee > as the consequence. 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 way." > > Reported-by: Hugh Dickins True. > Signed-off-by: Hugh Dickins I did provide some lines, but I think it's just Acked-by: Hugh Dickins to go below your Signed-off-by. > 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(-) 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. > > 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 *pvec, > spin_lock_irqsave(&pgdat->lru_lock, flags); > } > > + /* block memcg migration during page moving between lru */ > + if (!TestClearPageLRU(page)) > + continue; > + > lruvec = 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 *pvec, > > static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec) > { > - 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) > > static void __activate_page(struct page *page, struct lruvec *lruvec) > { > - if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) { > + if (!PageActive(page) && !PageUnevictable(page)) { > int lru = page_lru_base_type(page); > int nr_pages = thp_nr_pages(page); > > @@ -362,7 +368,8 @@ void activate_page(struct page *page) > > page = 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 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 looking 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. > @@ -521,9 +528,6 @@ static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec) > bool active; > int nr_pages = thp_nr_pages(page); > > - if (!PageLRU(page)) > - return; > - > if (PageUnevictable(page)) > return; > > @@ -564,7 +568,7 @@ static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec) > > static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec) > { > - if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) { > + if (PageActive(page) && !PageUnevictable(page)) { > int lru = page_lru_base_type(page); > int nr_pages = thp_nr_pages(page); > > @@ -581,7 +585,7 @@ static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec) > > 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 = PageActive(page); > int nr_pages = thp_nr_pages(page); > @@ -979,7 +983,29 @@ static void __pagevec_lru_add_fn(struct page *page, 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 = NULL; > + struct lruvec *lruvec; > + unsigned long flags = 0; > + > + for (i = 0; i < pagevec_count(pvec); i++) { > + struct page *page = pvec->pages[i]; > + struct pglist_data *pagepgdat = page_pgdat(page); > + > + if (pagepgdat != pgdat) { > + if (pgdat) > + spin_unlock_irqrestore(&pgdat->lru_lock, flags); > + pgdat = pagepgdat; > + spin_lock_irqsave(&pgdat->lru_lock, flags); > + } > + > + lruvec = 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); > } > > /** > -- > 1.8.3.1 From mboxrd@z Thu Jan 1 00:00:00 1970 From: Hugh Dickins Subject: Re: [PATCH v18 19/32] mm/swap.c: serialize memcg changes in pagevec_lru_move_fn Date: Mon, 21 Sep 2020 17:42:41 -0700 (PDT) Message-ID: References: <1598273705-69124-1-git-send-email-alex.shi@linux.alibaba.com> <1598273705-69124-20-git-send-email-alex.shi@linux.alibaba.com> Mime-Version: 1.0 Return-path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=R2R7WH/B9uorM3A755wPvM+NF9B+5Y9TzKQCMLEQCzw=; b=nHYRBUybpof6BfO9qgW0DwAlj2Q8hrrwjsx+WrbRtnVElQvMV8mPnKlrB5f1vHCE6K f1YW6WLqfPCzHihvOiBU5+kQFm/N7Ql9wLsJdpI2XMtrKheCjme5CR3XsZhHIdYiiusr x3Hwt+vxQk9fH5cMXiaaGXe/qO7DPFWCCii4vIfjQr4CuVwlmLMYlPj45b+eug9QUGlw M2kzP+M5/BOTbtNMToaUteYtoRH1jXm+Z267vcqLLCVrC0SpkvzNrURe8sgWTJhg/g/9 TpD4sFqfoJ0mdzJBoTT7KEW8YrbNqybsJlF+9F7vzTyMYrWUiuEX6SDNK8RYZqEXIBCw aqNA== In-Reply-To: <1598273705-69124-20-git-send-email-alex.shi-KPsoFbNs7GizrGE5bRqYAgC/G2K4zDHf@public.gmane.org> List-ID: Content-Type: TEXT/PLAIN; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Alex Shi Cc: akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org, mgorman-3eNAlZScCAx27rWaFMvyedHuzzzSOjJt@public.gmane.org, tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org, hughd-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org, khlebnikov-XoJtRXgx1JseBXzfvpsJ4g@public.gmane.org, daniel.m.jordan-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org, willy-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org, hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org, lkp-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org, linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org, linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, shakeelb-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org, iamjoonsoo.kim-Hm3cg6mZ9cc@public.gmane.org, richard.weiyang-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org, kirill-oKw7cIdHH8eLwutG50LtGA@public.gmane.org, alexander.duyck-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org, rong.a.chen-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org, mhocko-IBi9RG/b67k@public.gmane.org, vdavydov.dev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org, shy828301-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org On Mon, 24 Aug 2020, Alex Shi wrote: > 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 have > to serialize mem_cgroup_move_account during pagevec_lru_move_fn. The > possible bad scenario would like: > > cpu 0 cpu 1 > lruvec = mem_cgroup_page_lruvec() > if (!isolate_lru_page()) > mem_cgroup_move_account > > spin_lock_irqsave(&lruvec->lru_lock <== wrong lock. > > So we need the ClearPageLRU to block isolate_lru_page(), that serializes s/the ClearPageLRU/TestClearPageLRU/ > the memcg change. and then removing the PageLRU check in move_fn callee > as the consequence. 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 way." > > Reported-by: Hugh Dickins True. > Signed-off-by: Hugh Dickins I did provide some lines, but I think it's just Acked-by: Hugh Dickins to go below your Signed-off-by. > Signed-off-by: Alex Shi > Cc: Andrew Morton > Cc: linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org > Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org > --- > mm/swap.c | 44 +++++++++++++++++++++++++++++++++++--------- > 1 file changed, 35 insertions(+), 9 deletions(-) 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. > > 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 *pvec, > spin_lock_irqsave(&pgdat->lru_lock, flags); > } > > + /* block memcg migration during page moving between lru */ > + if (!TestClearPageLRU(page)) > + continue; > + > lruvec = 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 *pvec, > > static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec) > { > - 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) > > static void __activate_page(struct page *page, struct lruvec *lruvec) > { > - if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) { > + if (!PageActive(page) && !PageUnevictable(page)) { > int lru = page_lru_base_type(page); > int nr_pages = thp_nr_pages(page); > > @@ -362,7 +368,8 @@ void activate_page(struct page *page) > > page = 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 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 looking 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. > @@ -521,9 +528,6 @@ static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec) > bool active; > int nr_pages = thp_nr_pages(page); > > - if (!PageLRU(page)) > - return; > - > if (PageUnevictable(page)) > return; > > @@ -564,7 +568,7 @@ static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec) > > static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec) > { > - if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) { > + if (PageActive(page) && !PageUnevictable(page)) { > int lru = page_lru_base_type(page); > int nr_pages = thp_nr_pages(page); > > @@ -581,7 +585,7 @@ static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec) > > 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 = PageActive(page); > int nr_pages = thp_nr_pages(page); > @@ -979,7 +983,29 @@ static void __pagevec_lru_add_fn(struct page *page, 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 = NULL; > + struct lruvec *lruvec; > + unsigned long flags = 0; > + > + for (i = 0; i < pagevec_count(pvec); i++) { > + struct page *page = pvec->pages[i]; > + struct pglist_data *pagepgdat = page_pgdat(page); > + > + if (pagepgdat != pgdat) { > + if (pgdat) > + spin_unlock_irqrestore(&pgdat->lru_lock, flags); > + pgdat = pagepgdat; > + spin_lock_irqsave(&pgdat->lru_lock, flags); > + } > + > + lruvec = 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); > } > > /** > -- > 1.8.3.1