Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp2927489ybh; Sat, 25 Jul 2020 06:01:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxvMdT47/okRv5ilB/BmLCGWz4DTiiCSlEUYJWwNHhcSbmr6LV40WXiCxBsy1jBintvyKQZ X-Received: by 2002:a50:ee84:: with SMTP id f4mr12691337edr.183.1595682077243; Sat, 25 Jul 2020 06:01:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595682077; cv=none; d=google.com; s=arc-20160816; b=BN1VIXa7qCv08G1FrnovaqoB+4AqdmL/DzFo18RgbCBaLTgQw+ypPJl3TSqaeXUV8Q 7UufldzQV65VMgm0TGXyT99hlcNSV3Vs6giTAJfKstOKVAm7ZJ0hXxXnTyIW9Q2Z1iSV sxbJpZsExKDlMHLJb9pyqcfFWpxfGf7MEdPrrckpljyVbcmcQd4B0CqIVTps0FkdrtPQ kB415NTWVrcHtYeKXlBKDE1lx5dvl7Dr4fPCQNSXr65xuYQaECEyfTn5bh7SAKKXM/qE hJqqbgts3MbfYjsCGZOR6+2Wh2qfJXVfD8kXNLjRLz5Sd593me2N97AS6Xf/6yX0Mu7x z8QQ== 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=HfB9l5tGlS/mMBJTNzV/hcCE7d8UU7wVALKjkBU6J6M=; b=d01aB/ILrOobc1hdP954+TK02Fb1S9XFyn/cMu+DrM2p+JQ/IjlI1yyaSSvBN4ZBla 8/kSsf73YPvaP4FWA5ZIf9isPKeKZ7ntZrIXTZySmyoiR4qeKSaC3R/QJcpd9HyrmgGD n7pvlR5yD/SFVAs4Ful1NuYrMyEKz4JJUA/O4v4u/MIc6Z7dNuRP3j33KYMQnT3giMb+ C9Bi2pVBpikYxc6mUJZtyo57MmmzdZ5Owuy9umtcDaTvFbKat9XpAHvOCNGUuek1zC0V t3KFsPOjK39q+VglgmUJqd5zGaXsM45a5GJ+lG/Pb7+clmyx2VR1AJsZEyHN4Gtnqqef sdLA== 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 rv4si2253039ejb.366.2020.07.25.06.00.54; Sat, 25 Jul 2020 06:01:17 -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 S1727834AbgGYNAh (ORCPT + 99 others); Sat, 25 Jul 2020 09:00:37 -0400 Received: from out30-54.freemail.mail.aliyun.com ([115.124.30.54]:35520 "EHLO out30-54.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727777AbgGYNAe (ORCPT ); Sat, 25 Jul 2020 09:00:34 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01f04427;MF=alex.shi@linux.alibaba.com;NM=1;PH=DS;RN=21;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:28 +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 Cc: Thomas Gleixner , Andrey Ryabinin Subject: [PATCH v17 18/21] mm/lru: introduce the relock_page_lruvec function Date: Sat, 25 Jul 2020 20:59:55 +0800 Message-Id: <1595681998-19193-19-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 Use this new function to replace repeated same code, no func change. Signed-off-by: Alex Shi Cc: Johannes Weiner Cc: Andrew Morton Cc: Thomas Gleixner Cc: Andrey Ryabinin Cc: Matthew Wilcox 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 | 40 ++++++++++++++++++++++++++++++++++++++++ mm/mlock.c | 9 +-------- mm/swap.c | 33 +++++++-------------------------- mm/vmscan.c | 8 +------- 4 files changed, 49 insertions(+), 41 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 258901021c6c..6e670f991b42 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1313,6 +1313,46 @@ static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec, spin_unlock_irqrestore(&lruvec->lru_lock, flags); } +/* 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); + bool locked; + + rcu_read_lock(); + locked = mem_cgroup_page_lruvec(page, pgdat) == locked_lruvec; + rcu_read_unlock(); + + if (locked) + return locked_lruvec; + + if (locked_lruvec) + unlock_page_lruvec_irq(locked_lruvec); + + return lock_page_lruvec_irq(page); +} + +/* 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); + bool locked; + + rcu_read_lock(); + locked = mem_cgroup_page_lruvec(page, pgdat) == locked_lruvec; + rcu_read_unlock(); + + if (locked) + return locked_lruvec; + + if (locked_lruvec) + unlock_page_lruvec_irqrestore(locked_lruvec, *flags); + + return lock_page_lruvec_irqsave(page, flags); +} + #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 5d40d259a931..bc2fb3bfbe7a 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -303,17 +303,10 @@ static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone) /* Phase 1: page isolation */ for (i = 0; i < nr; i++) { struct page *page = pvec->pages[i]; - struct lruvec *new_lruvec; /* block memcg change in mem_cgroup_move_account */ lock_page_memcg(page); - new_lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); - if (new_lruvec != lruvec) { - if (lruvec) - unlock_page_lruvec_irq(lruvec); - lruvec = lock_page_lruvec_irq(page); - } - + lruvec = relock_page_lruvec_irq(page, lruvec); if (TestClearPageMlocked(page)) { /* * We already have pin from follow_page_mask() diff --git a/mm/swap.c b/mm/swap.c index 09edac441eb6..6d9c7288f7de 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -209,19 +209,12 @@ static void pagevec_lru_move_fn(struct pagevec *pvec, for (i = 0; i < pagevec_count(pvec); i++) { struct page *page = pvec->pages[i]; - struct lruvec *new_lruvec; /* block memcg migration during page moving between lru */ if (!TestClearPageLRU(page)) continue; - new_lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); - if (lruvec != new_lruvec) { - if (lruvec) - unlock_page_lruvec_irqrestore(lruvec, flags); - lruvec = lock_page_lruvec_irqsave(page, &flags); - } - + lruvec = relock_page_lruvec_irqsave(page, lruvec, &flags); (*move_fn)(page, lruvec); SetPageLRU(page); @@ -864,17 +857,12 @@ void release_pages(struct page **pages, int nr) } if (PageLRU(page)) { - struct lruvec *new_lruvec; - - new_lruvec = mem_cgroup_page_lruvec(page, - page_pgdat(page)); - if (new_lruvec != lruvec) { - if (lruvec) - unlock_page_lruvec_irqrestore(lruvec, - flags); + struct lruvec *prev_lruvec = lruvec; + + lruvec = relock_page_lruvec_irqsave(page, lruvec, + &flags); + if (prev_lruvec != lruvec) lock_batch = 0; - lruvec = lock_page_lruvec_irqsave(page, &flags); - } __ClearPageLRU(page); del_page_from_lru_list(page, lruvec, page_off_lru(page)); @@ -980,15 +968,8 @@ void __pagevec_lru_add(struct pagevec *pvec) for (i = 0; i < pagevec_count(pvec); i++) { struct page *page = pvec->pages[i]; - struct lruvec *new_lruvec; - - new_lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); - if (lruvec != new_lruvec) { - if (lruvec) - unlock_page_lruvec_irqrestore(lruvec, flags); - lruvec = lock_page_lruvec_irqsave(page, &flags); - } + lruvec = relock_page_lruvec_irqsave(page, lruvec, &flags); __pagevec_lru_add_fn(page, lruvec); } if (lruvec) diff --git a/mm/vmscan.c b/mm/vmscan.c index 168c1659e430..bdb53a678e7e 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4292,15 +4292,9 @@ void check_move_unevictable_pages(struct pagevec *pvec) for (i = 0; i < pvec->nr; i++) { struct page *page = pvec->pages[i]; - struct lruvec *new_lruvec; pgscanned++; - new_lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); - if (lruvec != new_lruvec) { - if (lruvec) - unlock_page_lruvec_irq(lruvec); - lruvec = lock_page_lruvec_irq(page); - } + lruvec = relock_page_lruvec_irq(page, lruvec); if (!PageLRU(page) || !PageUnevictable(page)) continue; -- 1.8.3.1