Received: by 2002:a05:6a10:c7d3:0:0:0:0 with SMTP id h19csp14268pxy; Fri, 13 Aug 2021 22:27:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzWzrLlamCD3L6JkI0Y+8hp3bCcRh26DVfTOoDJzXrWXdF1Bz18uMFd/kuQgR1j/7qBP4fC X-Received: by 2002:a92:c5c8:: with SMTP id s8mr4159181ilt.140.1628918821771; Fri, 13 Aug 2021 22:27:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628918821; cv=none; d=google.com; s=arc-20160816; b=w+IEINJ6n50ZsU1u++CpZ4bkiFRjct+zr2mqgN5Kv9uEP6NaHNsP9UPI0L6aRv96r+ AWklKEdEJiH06d1vyKM5umwD6emBEYvv0Wu/dnHu0LaGVNGC2hUj45EXC3nYY215eHZx uIuNxy6n161nz7i/yN/z3HOZvwFNwh6oA2H86DFXWskIYVrR+hH6xsYHLSWbuhk+Atu+ WxUaqv5moklScfqzKuwUzKJv4fjMh991lImU/RSiZV1DwjQx8ECjSree02gDzN+4cD5X umAFYX4ROUVi026FyiD82NMKyr4MhBrAOm7GGp2Aqxc4/HFhR8NlA5LKSW+K5+rRPjFE KybA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=srhzdC6dpfDSqfVf+RD/0prfJUSgfxeDUXo+j1ykPgc=; b=ydXXmT4pdoxEs5IXG9Esc/OCTeXZsVCelsplQyb5p6nk7x0MfA+4MfPQERfaFaCIWP /pigJfFBOU4qg+xnJTEG1DdZymPUYYNGOurLcRIt9fDpu1ESfN2xTsrEIrbV2x61cEKE N3qJJ7O2pZcj3Po1LSi+R43LeYPs6QbBsaaefTBwBN2hqpcEAN1BhBrimunsp/UMQhut cFe0qhBSNRdFMJDC1coT84fJgOuJ8rebJbJpNjgL6sjwSgOQ0vhRPrw2E4Z97/9A7n0p R5zTDCwSw1r2QzS2R+R+PY1PNP8/xppmEt+M5M7Qqmen7UIswBwWoUKG3LuKWXw2h8jP sBzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=Ebm1NQwK; 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=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x26si3575817jai.112.2021.08.13.22.26.51; Fri, 13 Aug 2021 22:27:01 -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; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=Ebm1NQwK; 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=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237073AbhHNF0T (ORCPT + 99 others); Sat, 14 Aug 2021 01:26:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237035AbhHNF0T (ORCPT ); Sat, 14 Aug 2021 01:26:19 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56BC8C061575 for ; Fri, 13 Aug 2021 22:25:51 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id e15so14664145plh.8 for ; Fri, 13 Aug 2021 22:25:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=srhzdC6dpfDSqfVf+RD/0prfJUSgfxeDUXo+j1ykPgc=; b=Ebm1NQwKcXSarqSFsJgcWgom5FuU3zoKjjle2EaDp0vepGN+D/VQaW+WlMGe/fb+YX W1dNsip3hRd0esRbu3h7yKdAKg9oMyfbkGSx1bc6A17BLSlfMzVDB+4Gdy4GeLC+g+55 NQZeSiXlj+XoKf/vy3gz9YDJXiwK/0t7gToJFBS/E9+aKkSruTNkM3QlmzUKDr0ccgOQ CJcouMtFk64wxIeykIxrsdyVl0oor/KH6kc6Le1QQPIOTmERu2+oN4M6rA05eMcypoWP z5MCehzKD3ylHYwl130/jajV+RTJ5HG6gX8QkHoXOL3fcURdA7rhwsr6aZwvTppr4yka 8TFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=srhzdC6dpfDSqfVf+RD/0prfJUSgfxeDUXo+j1ykPgc=; b=O4ZzPf5BAhNgJKzocfx+N0sGW+NKyJVx8yut3iUacn7a3F/fmfgPAXjOaCNJZO1dnS K3hqi6yB/nlefjYcI32nZdiWjeyUq72arEPcBrfDdBhOtlc8kNC5XbehRgkkxVBJx9vF H3altfm+WinRAEuTNjwqkiHln3A/rbQVpqbDPP35Q8TjQpOaMnqhyaUygYDnkCr6LsaH Y7FTYhLyD9KV+REYp8XyqCfw7ZH17ex4ZCU9Lx8RU4JC9GCMCW/Fw/qwbCPL1wUubfJV FwAbtPWrHh/JpL1uNTXBQ+77Jb4Q3ciE33GFttU9ajFLmyQ+Jt/96cSfdaHEe1cDoAD3 mzWA== X-Gm-Message-State: AOAM533EE2gUI2vsQukUCPI3PhzjKvs+d+NbVnQ8vCFv0SFrakkWY2g4 zDgDPyuN/ufwE1pqmECvnRR57A== X-Received: by 2002:a63:5fd4:: with SMTP id t203mr5486107pgb.141.1628918750893; Fri, 13 Aug 2021 22:25:50 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.237]) by smtp.gmail.com with ESMTPSA id s5sm4783133pgp.81.2021.08.13.22.25.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Aug 2021 22:25:50 -0700 (PDT) From: Muchun Song To: guro@fb.com, hannes@cmpxchg.org, mhocko@kernel.org, akpm@linux-foundation.org, shakeelb@google.com, vdavydov.dev@gmail.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, fam.zheng@bytedance.com, bsingharora@gmail.com, shy828301@gmail.com, alexs@kernel.org, smuchun@gmail.com, zhengqi.arch@bytedance.com, Muchun Song Subject: [PATCH v1 03/12] mm: memcontrol: make lruvec lock safe when LRU pages are reparented Date: Sat, 14 Aug 2021 13:25:10 +0800 Message-Id: <20210814052519.86679-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20210814052519.86679-1-songmuchun@bytedance.com> References: <20210814052519.86679-1-songmuchun@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The diagram below shows how to make the folio lruvec lock safe when LRU pages are reparented. folio_lruvec_lock(folio) retry: lruvec = folio_lruvec(folio); // The folio is reparented at this time. spin_lock(&lruvec->lru_lock); if (unlikely(lruvec_memcg(lruvec) != folio_memcg(folio))) // Acquired the wrong lruvec lock and need to retry. // Because this folio is on the parent memcg lruvec list. goto retry; // If we reach here, it means that folio_memcg(folio) is stable. memcg_reparent_objcgs(memcg) // lruvec belongs to memcg and lruvec_parent belongs to parent memcg. spin_lock(&lruvec->lru_lock); spin_lock(&lruvec_parent->lru_lock); // Move all the pages from the lruvec list to the parent lruvec list. spin_unlock(&lruvec_parent->lru_lock); spin_unlock(&lruvec->lru_lock); After we acquire the lruvec lock, we need to check whether the folio is reparented. If so, we need to reacquire the new lruvec lock. On the routine of the LRU pages reparenting, we will also acquire the lruvec lock (will be implemented in the later patch). So folio_memcg() cannot be changed when we hold the lruvec lock. Since lruvec_memcg(lruvec) is always equal to folio_memcg(folio) after we hold the lruvec lock, lruvec_memcg_debug() check is pointless. So remove it. This is a preparation for reparenting the LRU pages. Signed-off-by: Muchun Song Acked-by: Roman Gushchin --- include/linux/memcontrol.h | 18 +++----------- mm/compaction.c | 10 +++++++- mm/memcontrol.c | 62 +++++++++++++++++++++++++++++----------------- mm/swap.c | 4 +++ 4 files changed, 55 insertions(+), 39 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 41a35de93d75..5a8f85bd9bbf 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -769,7 +769,9 @@ static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg, * folio_lruvec - return lruvec for isolating/putting an LRU folio * @folio: Pointer to the folio. * - * This function relies on folio->mem_cgroup being stable. + * The lruvec can be changed to its parent lruvec when the page reparented. + * The caller need to recheck if it cares about this changes (just like + * folio_lruvec_lock() does). */ static inline struct lruvec *folio_lruvec(struct folio *folio) { @@ -788,15 +790,6 @@ struct lruvec *folio_lruvec_lock_irq(struct folio *folio); struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, unsigned long *flags); -#ifdef CONFIG_DEBUG_VM -void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio); -#else -static inline -void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) -{ -} -#endif - static inline struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){ return css ? container_of(css, struct mem_cgroup, css) : NULL; @@ -1243,11 +1236,6 @@ static inline struct lruvec *folio_lruvec(struct folio *folio) return &pgdat->__lruvec; } -static inline -void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) -{ -} - static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) { return NULL; diff --git a/mm/compaction.c b/mm/compaction.c index 3131558a7c31..97d72f6b3dd5 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -515,6 +515,8 @@ compact_folio_lruvec_lock_irqsave(struct folio *folio, unsigned long *flags, { struct lruvec *lruvec; + rcu_read_lock(); +retry: lruvec = folio_lruvec(folio); /* Track if the lock is contended in async mode */ @@ -527,7 +529,13 @@ compact_folio_lruvec_lock_irqsave(struct folio *folio, unsigned long *flags, spin_lock_irqsave(&lruvec->lru_lock, *flags); out: - lruvec_memcg_debug(lruvec, folio); + if (unlikely(lruvec_memcg(lruvec) != folio_memcg(folio))) { + spin_unlock_irqrestore(&lruvec->lru_lock, *flags); + goto retry; + } + + /* See the comments in folio_lruvec_lock(). */ + rcu_read_unlock(); return lruvec; } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7df2176e4f02..7955da38e385 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1146,23 +1146,6 @@ int mem_cgroup_scan_tasks(struct mem_cgroup *memcg, return ret; } -#ifdef CONFIG_DEBUG_VM -void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) -{ - struct mem_cgroup *memcg; - - if (mem_cgroup_disabled()) - return; - - memcg = folio_memcg(folio); - - if (!memcg) - VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != root_mem_cgroup, folio); - else - VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != memcg, folio); -} -#endif - /** * folio_lruvec_lock - lock and return lruvec for a given folio. * @folio: Pointer to the folio. @@ -1175,20 +1158,43 @@ void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) */ struct lruvec *folio_lruvec_lock(struct folio *folio) { - struct lruvec *lruvec = folio_lruvec(folio); + struct lruvec *lruvec; + rcu_read_lock(); +retry: + lruvec = folio_lruvec(folio); spin_lock(&lruvec->lru_lock); - lruvec_memcg_debug(lruvec, folio); + + if (unlikely(lruvec_memcg(lruvec) != folio_memcg(folio))) { + spin_unlock(&lruvec->lru_lock); + goto retry; + } + + /* + * Preemption is disabled in the internal of spin_lock, which can serve + * as RCU read-side critical sections. + */ + rcu_read_unlock(); return lruvec; } struct lruvec *folio_lruvec_lock_irq(struct folio *folio) { - struct lruvec *lruvec = folio_lruvec(folio); + struct lruvec *lruvec; + rcu_read_lock(); +retry: + lruvec = folio_lruvec(folio); spin_lock_irq(&lruvec->lru_lock); - lruvec_memcg_debug(lruvec, folio); + + if (unlikely(lruvec_memcg(lruvec) != folio_memcg(folio))) { + spin_unlock_irq(&lruvec->lru_lock); + goto retry; + } + + /* See the comments in folio_lruvec_lock(). */ + rcu_read_unlock(); return lruvec; } @@ -1196,10 +1202,20 @@ struct lruvec *folio_lruvec_lock_irq(struct folio *folio) struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, unsigned long *flags) { - struct lruvec *lruvec = folio_lruvec(folio); + struct lruvec *lruvec; + rcu_read_lock(); +retry: + lruvec = folio_lruvec(folio); spin_lock_irqsave(&lruvec->lru_lock, *flags); - lruvec_memcg_debug(lruvec, folio); + + if (unlikely(lruvec_memcg(lruvec) != folio_memcg(folio))) { + spin_unlock_irqrestore(&lruvec->lru_lock, *flags); + goto retry; + } + + /* See the comments in folio_lruvec_lock(). */ + rcu_read_unlock(); return lruvec; } diff --git a/mm/swap.c b/mm/swap.c index 2bca632b73df..9554ff008fe6 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -295,6 +295,10 @@ void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages) void lru_note_cost_folio(struct folio *folio) { + /* + * The rcu read lock is held by the caller, so we do not need to + * care about the lruvec returned by folio_lruvec() being released. + */ lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio), folio_nr_pages(folio)); } -- 2.11.0