From patchwork Mon Dec 14 22:37:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 11973291 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 23402C4361B for ; Mon, 14 Dec 2020 22:37:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CCFFB20731 for ; Mon, 14 Dec 2020 22:37:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CCFFB20731 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 61AF56B006C; Mon, 14 Dec 2020 17:37:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 57D196B006E; Mon, 14 Dec 2020 17:37:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 448C46B0070; Mon, 14 Dec 2020 17:37:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0025.hostedemail.com [216.40.44.25]) by kanga.kvack.org (Postfix) with ESMTP id 20C846B006C for ; Mon, 14 Dec 2020 17:37:49 -0500 (EST) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id DA9FA8249980 for ; Mon, 14 Dec 2020 22:37:48 +0000 (UTC) X-FDA: 77593351416.05.power30_360dbce2741e Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id B7F5518024CFD for ; Mon, 14 Dec 2020 22:37:48 +0000 (UTC) X-HE-Tag: power30_360dbce2741e X-Filterd-Recvd-Size: 5344 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Mon, 14 Dec 2020 22:37:48 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id 4so9747782plk.5 for ; Mon, 14 Dec 2020 14:37:48 -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=RnXK+gZ1C2/dQ0iAczaSS+4uBnqZQVQH1YKLl+NZ61M=; b=GGQmRYRtTlBV1mtp1fkqgUKL0n34FOR26CqFeArLaZtWKB97t753yJch9PooXHpLPB FFL31FyLa6SpRP+3/Z0kWL3bXc0aEaQP7CmkOOgzgIxDiSSIJTe9E0UDD+D7+IOMXNdq G32XeAoRxQ4Gj9cZLsbnwzlpUj1jOUS1JaEU/TlA8GRMDiBhfEAq6+Z8gPbS7KQNUQO+ 047aDRXN/m70MRnprmi6cLoOwFaRYNUAcDtCC4tLBVY/sYP3ED7zOjg9FjRrwMyYi+K1 YIh/vfq2/gW4y2Tdc7GdFKtnUOKWimotpGDHPEzQGyU0c2quOU94BM3rJ0TF8VKN2gK9 6y3A== 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=RnXK+gZ1C2/dQ0iAczaSS+4uBnqZQVQH1YKLl+NZ61M=; b=mYJ4rQa49esM+2JzxEn5ZkeVyQYdRZfms330f6v+D+z+GXtIfJyvnX2ZsrUAm6LT6Q F0Eg37jpY08KEvKBWlnCtjM38C73oPSX5y66S8g/NtEkcPImTu5lwvgIz7kQrdCo08tF PNrjlJRvVjoHBPX8k8bYx3msp473LGFfxkW2Tw83UIv+bsWr7uC5pMvhLUTculFxL5Wc C4eGGU8mKab2UqPZOj8GaDBIxNEQjwSUcRriByCJJcdxdmYTQaM0fPot6J3oiFi8jxjI dkTkUyhU7cIeTN6EZf3JZC6pMeAbfUCc9QlFXknnPJJ7wm8SwBQFMclFNSUTapIBqYcO cqyA== X-Gm-Message-State: AOAM533BIOy+Hu7ZPCFaC7hAlKsFWOW64TgkpZkslF8kucrZD2XrJxCO Rt/bzfrb8PUmVgzn5qBnfrA= X-Google-Smtp-Source: ABdhPJx2Cv9p2Tq9vADJWiKsvIEIe5Ww975Xm0kVR2bMbuwE9HC7vTBHlQ9cBXkEeK2Pe9I1qyYDNg== X-Received: by 2002:a17:90a:e7cc:: with SMTP id kb12mr27236213pjb.234.1607985467358; Mon, 14 Dec 2020 14:37:47 -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.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Dec 2020 14:37:46 -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 3/9] mm: vmscan: guarantee shrinker_slab_memcg() sees valid shrinker_maps for online memcg Date: Mon, 14 Dec 2020 14:37:16 -0800 Message-Id: <20201214223722.232537-4-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: The shrink_slab_memcg() races with mem_cgroup_css_online(). A visibility of CSS_ONLINE flag in shrink_slab_memcg()->mem_cgroup_online() does not guarantee that we will see memcg->nodeinfo[nid]->shrinker_maps != NULL. This may occur because of processor reordering on !x86. This seems like the below case: CPU A CPU B store shrinker_map load CSS_ONLINE store CSS_ONLINE load shrinker_map So the memory ordering could be guaranteed by smp_wmb()/smp_rmb() pair. The memory barriers pair will guarantee the ordering between shrinker_deferred and CSS_ONLINE for the following patches as well. Signed-off-by: Yang Shi --- mm/memcontrol.c | 7 +++++++ mm/vmscan.c | 8 +++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index ed942734235f..3d4ddbb84a01 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5406,6 +5406,13 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css) return -ENOMEM; } + /* + * Barrier for CSS_ONLINE, so that shrink_slab_memcg() sees shirnker_maps + * and shrinker_deferred before CSS_ONLINE. It pairs with the read barrier + * in shrink_slab_memcg(). + */ + smp_wmb(); + /* Online state pins memcg ID, memcg ID pins CSS */ refcount_set(&memcg->id.ref, 1); css_get(css); diff --git a/mm/vmscan.c b/mm/vmscan.c index 912c044301dd..9b31b9c419ec 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -552,13 +552,15 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, if (!mem_cgroup_online(memcg)) return 0; + /* Pairs with write barrier in mem_cgroup_css_online() */ + smp_rmb(); + if (!down_read_trylock(&shrinker_rwsem)) return 0; + /* Once memcg is online it can't be NULL */ map = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_map, true); - if (unlikely(!map)) - goto unlock; for_each_set_bit(i, map->map, shrinker_nr_max) { struct shrink_control sc = { @@ -612,7 +614,7 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, break; } } -unlock: + up_read(&shrinker_rwsem); return freed; }