Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp472786ybc; Tue, 19 Nov 2019 04:25:48 -0800 (PST) X-Google-Smtp-Source: APXvYqzjsRhVRWN7U8f5h7X2uGLqI+r8qtR2kDkKJQP3EzNBF4r5/flzzWgbNISlhozJkgb3hW4Z X-Received: by 2002:a17:906:dda:: with SMTP id p26mr33651804eji.77.1574166348504; Tue, 19 Nov 2019 04:25:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574166348; cv=none; d=google.com; s=arc-20160816; b=bQUvxea2hYn+y+i6OeDVyP1yN707rkxwPv7Oms1G7Pl0GczZXF44LpIilNdMfzdXTZ AZFTAsRBwdWNwOn77bjaUTxeswCSH8cUMbpkm+uKQJ6eMzzRQSW+bdz17iTQVJezfZMa 2xRkW4RZfduuS+RtRTkdTPxpcqnYmfHugnlr2pFze6T3MMvi8XYtrXV1jUxb2wfGpiag IDqpe/v9Th/co5oystgXOpl/BibXXPmoO1N59QOtY0rGhSbXJFrpMFZElgWHqepCe8Z+ G7iHpPuBxO4t4vAd1EFLl9PCSB2Zxv4ZdxE7Eisyklo8lhsQRQUXntHWC0/F8YAc35ec ckCQ== 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:cc:to:from; bh=lIAo3h2+Nu0p8HAQ7uI+w5AdVozod1/ruFZ8Jy13NWM=; b=TWwhG3KWYkkvCeBxFcLcG7oQExQ+Mb9KPhznsDMqIkZ3tn4LysOp3yetgtkVM/9PId UXGiNJXwycP/sjNFbr13IqR3pTVWoXJPHPPFwjoADE65zEjWFyCimj6VPZ8i8Tpp/rxa Mbup0MWBUaXxyqSYP3pfzSvIKo9sjTTRQmHRrA2wvL02NF+bxmmWHYDru7WpSmQ1xNQm ehXrAaDSqvNOomRm7ZDZBCGs6z5q8RTmoR+Z3QLACXXeuw5gv7NhSM/AVYGzFdE4sM9e bMvt6k1q05xU8SqexJKGe9rApGvyWoiBnpXlnhjH9JMDueLWEU6+C8xaGJZVgFArXetG FIMQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id y16si13430483ejw.142.2019.11.19.04.25.23; Tue, 19 Nov 2019 04:25:48 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1727973AbfKSMYP (ORCPT + 99 others); Tue, 19 Nov 2019 07:24:15 -0500 Received: from out30-57.freemail.mail.aliyun.com ([115.124.30.57]:46067 "EHLO out30-57.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727829AbfKSMYO (ORCPT ); Tue, 19 Nov 2019 07:24:14 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;CH=green;DM=||false|;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01f04446;MF=alex.shi@linux.alibaba.com;NM=1;PH=DS;RN=30;SR=0;TI=SMTPD_---0TiZ35.N_1574166246; Received: from localhost(mailfrom:alex.shi@linux.alibaba.com fp:SMTPD_---0TiZ35.N_1574166246) by smtp.aliyun-inc.com(127.0.0.1); Tue, 19 Nov 2019 20:24:06 +0800 From: Alex Shi To: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, 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, shakeelb@google.com, hannes@cmpxchg.org Cc: Alex Shi , Michal Hocko , Vladimir Davydov , Roman Gushchin , Chris Down , Thomas Gleixner , Vlastimil Babka , Andrey Ryabinin , swkhack , "Potyra, Stefan" , Jason Gunthorpe , Mauro Carvalho Chehab , Peng Fan , Nikolay Borisov , Ira Weiny , Kirill Tkhai , Yafang Shao Subject: [PATCH v4 4/9] mm/mlock: only change the lru_lock iff page's lruvec is different Date: Tue, 19 Nov 2019 20:23:18 +0800 Message-Id: <1574166203-151975-5-git-send-email-alex.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1574166203-151975-1-git-send-email-alex.shi@linux.alibaba.com> References: <1574166203-151975-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 During the pagevec locking, a new page's lruvec is may same as previous one. Thus we could save a re-locking, and only change lock iff lruvec is new. Function named relock_page_lruvec following Hugh Dickins patch. The first version of this patch used rcu_read_lock to guard lruvec assign and comparsion with locked_lruvev in relock_page_lruvec. But Rong Chen report a regression with PROVE_LOCKING config. The rcu_read locking causes qspinlock waiting to be locked for too long. Since we had hold a spinlock, rcu_read locking isn't necessary. [lkp@intel.com: Fix RCU-related regression reported by LKP robot] Signed-off-by: Alex Shi Cc: Johannes Weiner Cc: Michal Hocko Cc: Vladimir Davydov Cc: Andrew Morton Cc: Roman Gushchin Cc: Shakeel Butt Cc: Chris Down Cc: Thomas Gleixner Cc: Vlastimil Babka Cc: Andrey Ryabinin Cc: swkhack Cc: "Potyra, Stefan" Cc: Jason Gunthorpe Cc: Matthew Wilcox Cc: Mauro Carvalho Chehab Cc: Peng Fan Cc: Nikolay Borisov Cc: Ira Weiny Cc: Kirill Tkhai Cc: Yang Shi Cc: Yafang Shao Cc: Mel Gorman Cc: Konstantin Khlebnikov Cc: Hugh Dickins Cc: Tejun Heo Cc: linux-kernel@vger.kernel.org Cc: cgroups@vger.kernel.org Cc: linux-mm@kvack.org --- include/linux/memcontrol.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ mm/mlock.c | 16 +++++++++------- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 9538253998a6..19ff453e2822 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1291,6 +1291,50 @@ static inline void dec_lruvec_page_state(struct page *page, mod_lruvec_page_state(page, idx, -1); } +/* Don't lock again iff page's lruvec locked */ +static inline struct lruvec *relock_page_lruvec_irq(struct page *page, + struct lruvec *locked_lruvec) +{ + struct pglist_data *pgdat = page_pgdat(page); + struct lruvec *lruvec; + + if (!locked_lruvec) + goto lock; + + lruvec = mem_cgroup_page_lruvec(page, pgdat); + + if (locked_lruvec == lruvec) + return lruvec; + + spin_unlock_irq(&locked_lruvec->lru_lock); + +lock: + lruvec = lock_page_lruvec_irq(page, pgdat); + return lruvec; +} + +/* Don't lock again iff page's lruvec locked */ +static inline struct lruvec *relock_page_lruvec_irqsave(struct page *page, + struct lruvec *locked_lruvec, unsigned long *flags) +{ + struct pglist_data *pgdat = page_pgdat(page); + struct lruvec *lruvec; + + if (!locked_lruvec) + goto lock; + + lruvec = mem_cgroup_page_lruvec(page, pgdat); + + if (locked_lruvec == lruvec) + return lruvec; + + spin_unlock_irqrestore(&locked_lruvec->lru_lock, *flags); + +lock: + lruvec = lock_page_lruvec_irqsave(page, pgdat, flags); + return lruvec; +} + #ifdef CONFIG_CGROUP_WRITEBACK struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb); diff --git a/mm/mlock.c b/mm/mlock.c index b509b80b8513..8b3a97b62c0a 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -290,6 +290,7 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone) { int i; int nr = pagevec_count(pvec); + int delta_munlocked = -nr; struct pagevec pvec_putback; struct lruvec *lruvec = NULL; int pgrescued = 0; @@ -300,20 +301,19 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone) for (i = 0; i < nr; i++) { struct page *page = pvec->pages[i]; - lruvec = lock_page_lruvec_irq(page, page_pgdat(page)); + lruvec = relock_page_lruvec_irq(page, lruvec); if (TestClearPageMlocked(page)) { /* * We already have pin from follow_page_mask() * so we can spare the get_page() here. */ - if (__munlock_isolate_lru_page(page, lruvec, false)) { - __mod_zone_page_state(zone, NR_MLOCK, -1); - spin_unlock_irq(&lruvec->lru_lock); + if (__munlock_isolate_lru_page(page, lruvec, false)) continue; - } else + else __munlock_isolation_failed(page); - } + } else + delta_munlocked++; /* * We won't be munlocking this page in the next phase @@ -323,8 +323,10 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone) */ pagevec_add(&pvec_putback, pvec->pages[i]); pvec->pages[i] = NULL; - spin_unlock_irq(&lruvec->lru_lock); } + __mod_zone_page_state(zone, NR_MLOCK, delta_munlocked); + if (lruvec) + spin_unlock_irq(&lruvec->lru_lock); /* Now we can release pins of pages that we are not munlocking */ pagevec_release(&pvec_putback); -- 1.8.3.1