Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp2927708ybh; Sat, 25 Jul 2020 06:01:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw0w3GVzp9o6uG9H32CKulTS8iLSjk5w86EEJHFCBh/TDK3Uz5TBtOwQSE7VHL8qb27zXFT X-Received: by 2002:a17:906:430b:: with SMTP id j11mr4615837ejm.270.1595682090572; Sat, 25 Jul 2020 06:01:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595682090; cv=none; d=google.com; s=arc-20160816; b=vtd3m4y7vF+ua6WpgOvqD8AaMAg7H0AGBnEyd5dtPQ2AOUKuxxNM8JTl76ADIJqDZj LSncyygHRGLbZ19Iu6dcgUpEjIsfoEuNutBzPfAAXKhdWFpNMRfCEJLjRE7ipIV8m61j EVIxQ29rKVRjrOQS4watoxE/baUIaF0g9NKI05gsde4YparQee+5Bq6fN8UPRL/yTchO wSdzpdPIDhMKrHvUk41OByXWjxD23ZIbB0Q+LjEbEMSJlrkpWbDpBQvQwzVdi7eyb5e0 NmEV3/T8ruzfsdlBt4/6PzB4ayb7zjJXCYAqYHE/u6M2KKol2qZ/JV7I2pUUaqyy9SzD 61yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:to:from; bh=GiufAZSHjgy8coQpuaYaJuwb/j3jFdon8pIfZfQ1zq4=; b=zQoKNlPdvcnwBK713+KUU9sRG50ibXnbG3BFvqZP0UpKxg7Ijt7yXNcEY5h32oLZ+B nvxAWZAXIvrfw2hW3acp9zhvsVxFYV1U58Pel7omVY5lN5ENo0j1j24iZHpU8qFMvZIK ULcjAkVI6YPZGFRaN/OJkqiHt81rA4ZGiYQfRlf+vZkNPDnC+vU7P44rF0hcm5nRuUpY 7F7R8c0Dq0rw201UfKc+iMJ7RQjE3yCL2prVHVPnK2cBj8knFjR2R7Akvdc0ngN2iqtS BZ1VSAK8liXYlajzxIhNS7HqpoJSjmcxWJNKiaf3m9ltLMq3wVikqBf0r2BYUnOZfLOD BQ+w== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n6si2313127ejc.328.2020.07.25.06.01.07; Sat, 25 Jul 2020 06:01:30 -0700 (PDT) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727859AbgGYNAm (ORCPT + 99 others); Sat, 25 Jul 2020 09:00:42 -0400 Received: from out30-133.freemail.mail.aliyun.com ([115.124.30.133]:58033 "EHLO out30-133.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727787AbgGYNAh (ORCPT ); Sat, 25 Jul 2020 09:00:37 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01f04397;MF=alex.shi@linux.alibaba.com;NM=1;PH=DS;RN=19;SR=0;TI=SMTPD_---0U3l6fmD_1595682015; Received: from aliy8.localdomain(mailfrom:alex.shi@linux.alibaba.com fp:SMTPD_---0U3l6fmD_1595682015) by smtp.aliyun-inc.com(127.0.0.1); Sat, 25 Jul 2020 21:00:27 +0800 From: Alex Shi To: akpm@linux-foundation.org, mgorman@techsingularity.net, tj@kernel.org, hughd@google.com, khlebnikov@yandex-team.ru, daniel.m.jordan@oracle.com, yang.shi@linux.alibaba.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 Subject: [PATCH v17 16/21] mm/swap: serialize memcg changes in pagevec_lru_move_fn Date: Sat, 25 Jul 2020 20:59:53 +0800 Message-Id: <1595681998-19193-17-git-send-email-alex.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1595681998-19193-1-git-send-email-alex.shi@linux.alibaba.com> References: <1595681998-19193-1-git-send-email-alex.shi@linux.alibaba.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 the memcg change. and then removing the PageLRU check in move_fn callee as the consequence. Reported-by: Hugh Dickins Signed-off-by: Hugh Dickins Signed-off-by: Alex Shi Cc: Andrew Morton Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- mm/swap.c | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/mm/swap.c b/mm/swap.c index 5092fe9c8c47..3029b3f74811 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 = hpage_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 @@ -520,9 +527,6 @@ static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec) bool active; int nr_pages = hpage_nr_pages(page); - if (!PageLRU(page)) - return; - if (PageUnevictable(page)) return; @@ -563,7 +567,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 = hpage_nr_pages(page); @@ -580,7 +584,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 = hpage_nr_pages(page); @@ -654,7 +658,7 @@ void deactivate_file_page(struct page *page) * In a workload with many unevictable page such as mprotect, * unevictable page deactivation for accelerating reclaim is pointless. */ - if (PageUnevictable(page)) + if (PageUnevictable(page) || !PageLRU(page)) return; if (likely(get_page_unless_zero(page))) { @@ -976,7 +980,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