Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp551836pxb; Wed, 11 Nov 2020 10:05:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJx9pQrTnFEOWL1mL8nPnk/qIe4bhe00oBQWTg6rePbbaLvGAafSuaaHYh+pgpF9Bn4us3QW X-Received: by 2002:a17:906:4807:: with SMTP id w7mr25897248ejq.306.1605117908997; Wed, 11 Nov 2020 10:05:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605117908; cv=none; d=google.com; s=arc-20160816; b=VgFA3GUT1JOmVgfTaBfh8zXG1lWxjxpNVIet+Lsx8ZYtkIQw8nR7uy1fSoJm4zF19i WGkniid93v/JO+4YIue92IYZJQvf8BSOusLspSTkSHNcy7b7lNg3fC2d0OBnVopABFba x8szbK8zLxhCBq/vHBVv4zOO0piq6co4lBdRVoqDOfAnYxJWgdozRFlMHs16/lwwMMop A9b4V5gxXBxn41svd9OyYB5WTq6SqYcVvZIwaNZsDpVl46rtgdZCBIy4HsUWmVESkrhz yTB6VDx+8t0XYuYAa69yWVRyrV2EXgW+jd6mjBNmwXGzsnMF9XXwLun8InbYz3n5pVbP WBDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :to:subject; bh=w5Hoqv1K4iGZyNxQaNAGLLTPtIMuWceI06Cn1Vtm5gs=; b=nP5P9QF2FtvLeXDWYJiiI6ZgWRN9WIR+2ZZErAK31tmbLImBEblu/AP1gUQi/VE0Go EqrMKozC4qnJCiaVa52gYXxvsMvPTCPKFWSu6QgSXHDIVvfwdyMqCMHg35KTAmfuThOE K8JWXLAcNJuu3PGx5FYZzGzc4CuZFrl/RMjGjn1dA2V/2u9kdFfWjGH8PCqceK0NS9sq 47FgpH1Pyeih6svpqLYUVhfXzziKGR0T+DBcfxsLBI/D/eXOHBQVWeZfNFuHbbOSO0A9 Ktfj3elajsSTjcKzazHaBAkohL3meq8LA0vHwQzJ1JJB3OCcUNtZbggegwGDSburKFLE pfbw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m30si2031518edj.103.2020.11.11.10.04.44; Wed, 11 Nov 2020 10:05:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726821AbgKKSAQ (ORCPT + 99 others); Wed, 11 Nov 2020 13:00:16 -0500 Received: from mx2.suse.de ([195.135.220.15]:39386 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725966AbgKKSAN (ORCPT ); Wed, 11 Nov 2020 13:00:13 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 46FD6ABD1; Wed, 11 Nov 2020 18:00:11 +0000 (UTC) Subject: Re: [PATCH v21 16/19] mm/swap.c: serialize memcg changes in pagevec_lru_move_fn To: Alex Shi , 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 References: <1604566549-62481-1-git-send-email-alex.shi@linux.alibaba.com> <1604566549-62481-17-git-send-email-alex.shi@linux.alibaba.com> From: Vlastimil Babka Message-ID: <0af28702-8f75-6974-5b66-c0633eb83d7c@suse.cz> Date: Wed, 11 Nov 2020 19:00:10 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: <1604566549-62481-17-git-send-email-alex.shi@linux.alibaba.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/5/20 9:55 AM, 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 TestClearPageLRU to block isolate_lru_page(), that serializes > the memcg change. and then removing the PageLRU check in move_fn callee > as the consequence. > > __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 > Signed-off-by: Alex Shi > Acked-by: Hugh Dickins > Acked-by: Johannes Weiner > Cc: Andrew Morton > Cc: linux-mm@kvack.org > Cc: linux-kernel@vger.kernel.org Acked-by: Vlastimil Babka > --- > mm/swap.c | 44 +++++++++++++++++++++++++++++++++++--------- > 1 file changed, 35 insertions(+), 9 deletions(-) > > diff --git a/mm/swap.c b/mm/swap.c > index 2681d9023998..1838a9535703 100644 > --- a/mm/swap.c > +++ b/mm/swap.c > @@ -222,8 +222,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); > @@ -233,7 +239,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 @@ static 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 > @@ -519,9 +526,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; > > @@ -562,7 +566,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); > > @@ -579,7 +583,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); > @@ -1021,7 +1025,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); > } > > /** >