From patchwork Wed Mar 10 17:45:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 12128815 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,URIBL_BLOCKED, 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 1ACE9C433DB for ; Wed, 10 Mar 2021 17:46:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A7F6964F45 for ; Wed, 10 Mar 2021 17:46:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A7F6964F45 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 287F58D01E3; Wed, 10 Mar 2021 12:46:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 20FC58D01D5; Wed, 10 Mar 2021 12:46:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 08A7F8D01E3; Wed, 10 Mar 2021 12:46:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0091.hostedemail.com [216.40.44.91]) by kanga.kvack.org (Postfix) with ESMTP id E30998D01D5 for ; Wed, 10 Mar 2021 12:46:22 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 9F7F02484 for ; Wed, 10 Mar 2021 17:46:22 +0000 (UTC) X-FDA: 77904693804.14.C28E7B6 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf04.hostedemail.com (Postfix) with ESMTP id 4205E3EB for ; Wed, 10 Mar 2021 17:46:21 +0000 (UTC) Received: by mail-pg1-f177.google.com with SMTP id w34so10807671pga.8 for ; Wed, 10 Mar 2021 09:46:21 -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=aiM+lLYNubKNlhU5HOyGVLNEOVuVKT3O2pdKzgZrP7U=; b=L38uPzNxzeHdFm5yL8UeAwlmZ/6ul0Jc5NXK/TEsJNntKbqK6cuERyzxVNFntfJzpR MSzJpTbHGADjG5e0kPevhzKwvDipYOrp83GOb6zMtXAc21yCbXlVx3+JOat8amDsl9A3 PTMGkBWbFpO1O8w1MTpxwQqsKgxG5OIZKCfOOWjUzgU9BKA9d0QQUT7HX3agjv/wcoj5 lgrUlMfRhBiBUexwdNJayXurHC/gLCKLU2IpjXJzlbwNUW9qxioTi4laOh/qlTn5pyaw Oj9wV5XtmrJLnwyqWdj0wRAnfRG/19ZxvcBPqxOIF1QB4cPnGlMFJtNoKATCeAEGAeNN pfqQ== 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=aiM+lLYNubKNlhU5HOyGVLNEOVuVKT3O2pdKzgZrP7U=; b=ewYHjOPv/gzokLkhcuwWWoPCN6w0zDCowB35Nhk1DO+Z68H8xKmSpCI8a8gez6Hdba +bC8C4bvIeQy+eJj7WJ7w3IGuh5DKtSyRGAAal/Uo1GOCSlZiUOcoq2vyrRBScas5o35 6xX5O0HD3Zh7IyaGf+/SMMRd62PNkbsZ1mDidSALtW1sZEWJyJ2rVan0CPjBoqdJtgPE I52zZGGqkgCJB0SKXFob2DCzE0QR0BAtLfKJy/PCLB4phDhD/rUdokOS5gSiE1s5Dmdc Q0xy/3JO8gDqTzVtRKiK61hxYnE0B7aPLLrC/2r4PBphzi+968WL2htTCOz59JlKPnfC U5Og== X-Gm-Message-State: AOAM5310Q7MHnLWTGcttSgthJ9gRPPnh0ZX7BqarVw3/cBMTNyf3lOeT nF+cQrIIvUOkheRM9V3R5fs= X-Google-Smtp-Source: ABdhPJyCE7pWMJCZS0OOk6anncpki25SoIBg7kTzYHtJ/2KgB4+490RPqKke3EkJ2ZeqQpgNIbP8rQ== X-Received: by 2002:aa7:888b:0:b029:1ec:df4a:4da2 with SMTP id z11-20020aa7888b0000b02901ecdf4a4da2mr3830929pfe.66.1615398381083; Wed, 10 Mar 2021 09:46:21 -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 d6sm145804pfq.109.2021.03.10.09.46.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Mar 2021 09:46:20 -0800 (PST) From: Yang Shi To: guro@fb.com, ktkhai@virtuozzo.com, vbabka@suse.cz, 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: [v9 PATCH 03/13] mm: vmscan: use shrinker_rwsem to protect shrinker_maps allocation Date: Wed, 10 Mar 2021 09:45:53 -0800 Message-Id: <20210310174603.5093-4-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210310174603.5093-1-shy828301@gmail.com> References: <20210310174603.5093-1-shy828301@gmail.com> MIME-Version: 1.0 X-Stat-Signature: jz975x8uceiqz9x44x5739bqtjzfrwb7 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 4205E3EB Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf04; identity=mailfrom; envelope-from=""; helo=mail-pg1-f177.google.com; client-ip=209.85.215.177 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1615398381-869911 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 changed under holding shrinker_rwsem exclusively, the read side can be protected by holding read lock, so it sounds superfluous to have a dedicated mutex. Kirill Tkhai suggested use write lock since: * We want the assignment to shrinker_maps is visible for shrink_slab_memcg(). * The rcu_dereference_protected() dereferrencing in shrink_slab_memcg(), but in case of we use READ lock in alloc_shrinker_maps(), the dereferrencing is not actually protected. * READ lock makes alloc_shrinker_info() racy against memory allocation fail. alloc_shrinker_info()->free_shrinker_info() may free memory right after shrink_slab_memcg() dereferenced it. You may say shrink_slab_memcg()->mem_cgroup_online() protects us from it? Yes, sure, but this is not the thing we want to remember in the future, since this spreads modularity. And a test with heavy paging workload didn't show write lock makes things worse. Acked-by: Vlastimil Babka Acked-by: Kirill Tkhai Acked-by: Roman Gushchin Reviewed-by: Shakeel Butt Signed-off-by: Yang Shi --- mm/vmscan.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index ad164f3af9a0..75fd8038a6c8 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -187,7 +187,6 @@ static DECLARE_RWSEM(shrinker_rwsem); #ifdef CONFIG_MEMCG static int memcg_shrinker_map_size; -static DEFINE_MUTEX(memcg_shrinker_map_mutex); static void free_shrinker_map_rcu(struct rcu_head *head) { @@ -200,8 +199,6 @@ static int 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); @@ -249,7 +246,7 @@ int alloc_shrinker_maps(struct mem_cgroup *memcg) if (mem_cgroup_is_root(memcg)) return 0; - mutex_lock(&memcg_shrinker_map_mutex); + down_write(&shrinker_rwsem); size = memcg_shrinker_map_size; for_each_node(nid) { map = kvzalloc_node(sizeof(*map) + size, GFP_KERNEL, nid); @@ -260,7 +257,7 @@ int alloc_shrinker_maps(struct mem_cgroup *memcg) } rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_map, map); } - mutex_unlock(&memcg_shrinker_map_mutex); + up_write(&shrinker_rwsem); return ret; } @@ -275,9 +272,10 @@ static int 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; + + lockdep_assert_held(&shrinker_rwsem); memcg = mem_cgroup_iter(NULL, NULL, NULL); do { @@ -286,13 +284,13 @@ static int expand_shrinker_maps(int new_id) ret = expand_one_shrinker_map(memcg, size, old_size); if (ret) { mem_cgroup_iter_break(NULL, memcg); - goto unlock; + goto out; } } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); -unlock: +out: if (!ret) memcg_shrinker_map_size = size; - mutex_unlock(&memcg_shrinker_map_mutex); + return ret; }