From patchwork Thu Jan 21 23:06:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 12037933 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 B4096C433E6 for ; Thu, 21 Jan 2021 23:06:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2D1DB2389F for ; Thu, 21 Jan 2021 23:06:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2D1DB2389F 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 943F36B000A; Thu, 21 Jan 2021 18:06:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F6186B000C; Thu, 21 Jan 2021 18:06:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7BFCF6B000D; Thu, 21 Jan 2021 18:06:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0121.hostedemail.com [216.40.44.121]) by kanga.kvack.org (Postfix) with ESMTP id 61A606B000A for ; Thu, 21 Jan 2021 18:06:44 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 26B27180ACF75 for ; Thu, 21 Jan 2021 23:06:44 +0000 (UTC) X-FDA: 77731318728.27.stem98_29157de27567 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin27.hostedemail.com (Postfix) with ESMTP id 036703D663 for ; Thu, 21 Jan 2021 23:06:43 +0000 (UTC) X-HE-Tag: stem98_29157de27567 X-Filterd-Recvd-Size: 6162 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Thu, 21 Jan 2021 23:06:43 +0000 (UTC) Received: by mail-pf1-f182.google.com with SMTP id w18so2459378pfu.9 for ; Thu, 21 Jan 2021 15:06:43 -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=9KysAzfeahGm6mPyUWMF7gAQ+XHowxRld+CYS+eHKLY=; b=WBYhztIOeeY04TJWXqc9qQSLVH5omlKxjvbotA3q3BZOdZBcZCRKzackoXnxcq2Yc6 mCRwOBZO38UOqjX5/5IJUOPjU4gC+inxt6mAIO1dFghM0SscgNdi53CVxVhFDQPiD4D/ QBZLyzueyDathFogij0nRBuVX7E0x0Uin5KKSYcr6/BRyfvoWZ+jFQseMDrwzoZ6O1F3 TpjGO57KsySxbwmqEJDl3637iLv3aOk9ITA2ZWPzggbPaZVNHW5E1ElmenJQ0akR4kF0 bBxK+jUom8OB1WgHgQGWXA6agAIsNwesmz0Gy4wRmaqMnTjWFThw4oDgbyD6yEXwqHZe TrKg== 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=9KysAzfeahGm6mPyUWMF7gAQ+XHowxRld+CYS+eHKLY=; b=ounvjrvnKwx1rSi8h4KuJ9cnQhZUBJu67rQpCpw7oJRSHN6aHYvALGQT/pV+8Zp+7h mFN71xDeHJrPqBlcVXZO7vlruMbwzp1d0hfLl8xctuSAeSv3qfrNEeXBIlGUZHsAglHu Vr8Q+O581CyTqsGzWN5V/SZ2MkPdQWg59LBenlVZ9lrzPNs0Z3TEhNyxPp4zEogRXw6n QhN2CnadHVUUqY1e/MF6YXP+WORWDw4QfYmNARdHNW7nqsFcuzaO0QW3nz0Q7WGQxqkS 3GflV6w1GlqRo78aCUf6PLpLCCNWonPzIrjWmCK8BY84U0M8JYUAQ5B5B/8UM/6/gKhg zCaA== X-Gm-Message-State: AOAM5315U+lh9eVZEmcjNwoP63o4J3bTuQEvYbt1qF8tRCHhzRlIbM8n hz7gGLj2rM6BS2K1Q1ZzW5c= X-Google-Smtp-Source: ABdhPJywYL+McJDZGYLd5Wt25nO1Tlxegu3RhSomNZ37WaqnKe9HHRYdl+ws3Opt5V3VAcwEmyqujA== X-Received: by 2002:a63:4c52:: with SMTP id m18mr1642106pgl.280.1611270402606; Thu, 21 Jan 2021 15:06:42 -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 y16sm6722921pfb.83.2021.01.21.15.06.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jan 2021 15:06:41 -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: [v4 PATCH 03/11] mm: vmscan: use shrinker_rwsem to protect shrinker_maps allocation Date: Thu, 21 Jan 2021 15:06:13 -0800 Message-Id: <20210121230621.654304-4-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210121230621.654304-1-shy828301@gmail.com> References: <20210121230621.654304-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 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. Signed-off-by: Yang Shi --- mm/vmscan.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index d950cead66ca..d3f3701dfcd2 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); @@ -250,7 +247,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); @@ -261,7 +258,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; } @@ -276,9 +273,8 @@ 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; memcg = mem_cgroup_iter(NULL, NULL, NULL); do { @@ -287,13 +283,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; }