From patchwork Mon Dec 14 22:37:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 11973289 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96EBCC4361B for ; Mon, 14 Dec 2020 22:37:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4322C20731 for ; Mon, 14 Dec 2020 22:37:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4322C20731 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AFC196B0068; Mon, 14 Dec 2020 17:37:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A860A6B006C; Mon, 14 Dec 2020 17:37:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 928A46B006E; Mon, 14 Dec 2020 17:37:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0167.hostedemail.com [216.40.44.167]) by kanga.kvack.org (Postfix) with ESMTP id 7CE1F6B0068 for ; Mon, 14 Dec 2020 17:37:46 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 44F3B8249980 for ; Mon, 14 Dec 2020 22:37:46 +0000 (UTC) X-FDA: 77593351332.20.order81_600b4212741e Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id 23636180C07AB for ; Mon, 14 Dec 2020 22:37:46 +0000 (UTC) X-HE-Tag: order81_600b4212741e X-Filterd-Recvd-Size: 6376 Received: from mail-pj1-f67.google.com (mail-pj1-f67.google.com [209.85.216.67]) by imf05.hostedemail.com (Postfix) with ESMTP for ; Mon, 14 Dec 2020 22:37:45 +0000 (UTC) Received: by mail-pj1-f67.google.com with SMTP id j13so7669816pjz.3 for ; Mon, 14 Dec 2020 14:37:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ozVKgrz/cuTdhl0CMZyCRzGO4Sb1PcBOLEI+PfNUSHM=; b=dXTQDcP72TdGCCJV+w/cgvaAaD/MLr2B5g5F2KSNONWtAZWOrf4mUN2HiA0vEeHEcK mIdVk7cX8wqk32sJglGFovFRe9g17sNd9lz4PWd6gnOEUagZ7k0cfxk9LsNabtlnHoIH K5V13cL6igyAu0466QaJrJHXMIXQaEoAlJ2rCYucdM8mhULTLteNYDGA14m2Sb1m15Ur cDzckkG3rRz9pDmouO3zJm9mPEaPtswg3Ql+oC+VsTkihgfEfbOVXn6tSZkZZHZpHwCJ yDgYlgKsPsUFqLDbti8/VKsZZ3l139LvxDf/arZ3LGUfghanuwCfHYoAKv10FumTEAmX 5ajg== 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=ozVKgrz/cuTdhl0CMZyCRzGO4Sb1PcBOLEI+PfNUSHM=; b=eMXXpHHUw5ZBhkcjqi17mOdQTTYrDVuCBEVJTUGopL/quw06fDITvLiYJxu//MCpbd SjYnuipTMMoHxDH8d469xcXhBCwELVwAgD/RaUnczhAFy7yz3/Y9GBET5GAryFUuAV8c kdNRB01xNujx9pxC+MbWiSMQqAXABcC9G1tpjr6CtlTQshaKnVKrEkYS3t7zUvcKMNGh K+wCKzeWhjEJEvpoDUJ1uauYUPsBWyd91a+e8ycJ/s5wMGWb3hfKh3S5U5c8c8OGed4X X8lE2f9ytCGpQVUIJa1vI0i52OlrRh9DkfEC9CvrCeDnsz9UPcZJbyDjbwyicuUy0lc+ eu0g== X-Gm-Message-State: AOAM530KFs0xFLI19TSI7IT+yIJQEmTcR46ZolRLLBQ5Wdl5nGQg/XxA G1eCoRBOJZ31+sEvFE/RwtA= X-Google-Smtp-Source: ABdhPJwNKKhYDEpsImErYCx1i9yXj/MdY2yg4ExDO6Zf1q/mFSBm62tiTbO3AGPAWepsMKNUSLxTfw== X-Received: by 2002:a17:90a:f0c5:: with SMTP id fa5mr27964263pjb.144.1607985464867; Mon, 14 Dec 2020 14:37:44 -0800 (PST) Received: from localhost.localdomain (c-73-93-239-127.hsd1.ca.comcast.net. [73.93.239.127]) by smtp.gmail.com with ESMTPSA id d4sm20610758pfo.127.2020.12.14.14.37.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Dec 2020 14:37:43 -0800 (PST) From: Yang Shi To: guro@fb.com, ktkhai@virtuozzo.com, shakeelb@google.com, david@fromorbit.com, hannes@cmpxchg.org, mhocko@suse.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [v2 PATCH 2/9] mm: memcontrol: use shrinker_rwsem to protect shrinker_maps allocation Date: Mon, 14 Dec 2020 14:37:15 -0800 Message-Id: <20201214223722.232537-3-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201214223722.232537-1-shy828301@gmail.com> References: <20201214223722.232537-1-shy828301@gmail.com> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Since memcg_shrinker_map_size just can be changd under holding shrinker_rwsem exclusively, the read side can be protected by holding read lock, so it sounds superfluous to have a dedicated mutex. This should not exacerbate the contention to shrinker_rwsem since just one read side critical section is added. Signed-off-by: Yang Shi Acked-by: Johannes Weiner --- mm/internal.h | 1 + mm/memcontrol.c | 17 +++++++---------- mm/vmscan.c | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index c43ccdddb0f6..10c79d199aaa 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -108,6 +108,7 @@ extern unsigned long highest_memmap_pfn; /* * in mm/vmscan.c: */ +extern struct rw_semaphore shrinker_rwsem; extern int isolate_lru_page(struct page *page); extern void putback_lru_page(struct page *page); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 29459a6ce1c7..ed942734235f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -394,8 +394,8 @@ DEFINE_STATIC_KEY_FALSE(memcg_kmem_enabled_key); EXPORT_SYMBOL(memcg_kmem_enabled_key); #endif +/* It is only can be changed with holding shrinker_rwsem exclusively */ static int memcg_shrinker_map_size; -static DEFINE_MUTEX(memcg_shrinker_map_mutex); static void memcg_free_shrinker_map_rcu(struct rcu_head *head) { @@ -408,8 +408,6 @@ static int memcg_expand_one_shrinker_map(struct mem_cgroup *memcg, struct memcg_shrinker_map *new, *old; int nid; - lockdep_assert_held(&memcg_shrinker_map_mutex); - for_each_node(nid) { old = rcu_dereference_protected( mem_cgroup_nodeinfo(memcg, nid)->shrinker_map, true); @@ -458,7 +456,7 @@ static int memcg_alloc_shrinker_maps(struct mem_cgroup *memcg) if (mem_cgroup_is_root(memcg)) return 0; - mutex_lock(&memcg_shrinker_map_mutex); + down_read(&shrinker_rwsem); size = memcg_shrinker_map_size; for_each_node(nid) { map = kvzalloc_node(sizeof(*map) + size, GFP_KERNEL, nid); @@ -469,7 +467,7 @@ static int memcg_alloc_shrinker_maps(struct mem_cgroup *memcg) } rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_map, map); } - mutex_unlock(&memcg_shrinker_map_mutex); + up_read(&shrinker_rwsem); return ret; } @@ -484,9 +482,8 @@ int memcg_expand_shrinker_maps(int new_id) if (size <= old_size) return 0; - mutex_lock(&memcg_shrinker_map_mutex); if (!root_mem_cgroup) - goto unlock; + goto out; for_each_mem_cgroup(memcg) { if (mem_cgroup_is_root(memcg)) @@ -494,13 +491,13 @@ int memcg_expand_shrinker_maps(int new_id) ret = memcg_expand_one_shrinker_map(memcg, size, old_size); if (ret) { mem_cgroup_iter_break(NULL, memcg); - goto unlock; + goto out; } } -unlock: +out: if (!ret) memcg_shrinker_map_size = size; - mutex_unlock(&memcg_shrinker_map_mutex); + return ret; } diff --git a/mm/vmscan.c b/mm/vmscan.c index 48c06c48b97e..912c044301dd 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -184,7 +184,7 @@ static void set_task_reclaim_state(struct task_struct *task, } static LIST_HEAD(shrinker_list); -static DECLARE_RWSEM(shrinker_rwsem); +DECLARE_RWSEM(shrinker_rwsem); #ifdef CONFIG_MEMCG /*