From patchwork Mon Feb 20 09:16:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13146178 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C35BAC6379F for ; Mon, 20 Feb 2023 09:17:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C1E66B0072; Mon, 20 Feb 2023 04:17:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4715A6B0073; Mon, 20 Feb 2023 04:17:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3124E6B0074; Mon, 20 Feb 2023 04:17:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 229896B0072 for ; Mon, 20 Feb 2023 04:17:07 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E6A76160F41 for ; Mon, 20 Feb 2023 09:17:06 +0000 (UTC) X-FDA: 80487116052.09.DE9D78C Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf30.hostedemail.com (Postfix) with ESMTP id 20EA780013 for ; Mon, 20 Feb 2023 09:17:04 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=neiTcExI; spf=pass (imf30.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676884625; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OSu6LoDi3bt+On14RPrDb/428OOXZNPo77LIFm1Bi28=; b=8icQKIjl7/+TByCDbVV0/CN7SXIMNfEwqoZsBGOKLZrinFpxzV4ipGWFW+QTGT3lbmiXwh wyIadoAFxQHJnziq4NBsiyTCiOA4DFtg8a3fZr89ManfZl+WY5V/l8OppAzhljzAQYvfgR XOCXba8ZnoEaFTA69Z1N6Ziy4PDCAEw= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=neiTcExI; spf=pass (imf30.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676884625; a=rsa-sha256; cv=none; b=KIcZ/M9iSo+KRf5j63HVHS5tUPLHbAPNMBh5arjMwky6Vp8YRMe5zUKsk3yWIBGZEYuKVI EXQaZ3WMMe5FeEN7rkvn/BBmI+8ywyJC7LXmfCzYHRFRgjwpz6DDxs6EcSfSw0lQpJl38N c0rJKhrH+fuXTTrxejtXjPtIyXIYvm4= Received: by mail-pl1-f174.google.com with SMTP id z2so750782plf.12 for ; Mon, 20 Feb 2023 01:17:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; t=1676884624; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OSu6LoDi3bt+On14RPrDb/428OOXZNPo77LIFm1Bi28=; b=neiTcExIi57fl2R7QOvc3r3ELyzAtnMxW9GBNNCltkuMHmkXl5ZK+7Q0xCR6QpS+bt hP0XX6ZlR6iZxJOKblWlpeGKlK0XFXTJ6YTh8xf01BgvCpVSzBAdsU1eNWAu/zmWpkI3 9p1TCdObRxhi5YMuGSLQ1T3HSRXNro+TSsI76hsUHs5DW20YstK9espDPHnwdEXtao3l lajZuuPnoZK/Rm/vt/YViwwLTzZW/RtdXkdVPp+DvpozOWqhFL7zaeOqjL7CkS5IB/Uq XEBgboArtxfsBttqiHviM+ZB06ohsgBSRsBrlb3EsBlg+YZoeTyyZ3T9K3z2UfsI7PLd kbmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1676884624; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OSu6LoDi3bt+On14RPrDb/428OOXZNPo77LIFm1Bi28=; b=nv6rGaYszQDymq52HuwtNo23SNtEoqL5HECYllCrs5uMZAT7d+S25mtad6sJzAhZLR hw2oSaAGQar4qquBicabeWVZKUL376LeBzZhtKrnNR8VGti/wuXtABNzWmlJo+BWeda9 WEfj511maK/TrWI9seXCHPycbVDhIdBRpphCmnVSO//m4qkqM8c8/l/f3PwcXoL4OlML m7QlhclL9U3FWmehPY8Qyv7mUOnWnURwDlx9piNLEK7JvM6HVbcDOjFHiplVGyciQVBW Wc/i45bKyfWToHkpVDFmCzPwnY3Z0x9kFJiMcj6/LDjt/fDj8z53t/M3mYyTC/UQ9o9P nJcQ== X-Gm-Message-State: AO0yUKWX2wpCqc/KywFkUmZS8dRKOflQOHNHalFOcYLriH97t7wzOl08 DrNw518wGRqttQqcOncMENmRIw== X-Google-Smtp-Source: AK7set/Qfx+rPSFCLub0/LUO6j0gUaT7gq5PlaRfKArK9fwOowwRmHnhYd2zrA9jqT1wfk9fQyhMBA== X-Received: by 2002:a17:90a:bf0d:b0:233:a836:15f4 with SMTP id c13-20020a17090abf0d00b00233a83615f4mr528696pjs.1.1676884623852; Mon, 20 Feb 2023 01:17:03 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.229]) by smtp.gmail.com with ESMTPSA id fs13-20020a17090af28d00b002339195a47bsm432382pjb.53.2023.02.20.01.16.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Feb 2023 01:17:03 -0800 (PST) From: Qi Zheng To: akpm@linux-foundation.org, hannes@cmpxchg.org, shakeelb@google.com, mhocko@kernel.org, roman.gushchin@linux.dev, muchun.song@linux.dev, david@redhat.com, shy828301@gmail.com Cc: tkhai@ya.ru, sultan@kerneltoast.com, dave@stgolabs.net, penguin-kernel@I-love.SAKURA.ne.jp, paulmck@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qi Zheng Subject: [PATCH 1/5] mm: vmscan: make global slab shrink lockless Date: Mon, 20 Feb 2023 17:16:33 +0800 Message-Id: <20230220091637.64865-2-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20230220091637.64865-1-zhengqi.arch@bytedance.com> References: <20230220091637.64865-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 20EA780013 X-Stat-Signature: kw9k8qeq6hoxbbzd5ypx8i5skabpqf3r X-HE-Tag: 1676884624-264074 X-HE-Meta: U2FsdGVkX1/8tWpjXpApI2cjVEwDbk6zsu4EN6QIPHQPfp8DtqASypVvNyCLkEv+2emVoetupmQZuLBgoDNoh/bJY3W2rLDeNFjDRhnjmxX0ffJUP1V/Y/T5UtGJeIYAT8/qYw6TszQquWNNO1k4EkkaQIJc+U3v+sJvyPrqqcJY8n6yTBoXlS0LXp7mzeYnHVBmYSWnnyz6nwTfcFw+XTj5bD2Ip6F0WCyS1ygfrHe/3WZMVieY/C/bDLS0y/ybw8c7yAmsUXN5PXfZj00jpnTLkyoDmlxRRDDVUB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4Y5Q8VmF6gfWkTnAtNnDjkYKS8vOu4uw+I+7koO06U0MOvUqCZlsqflPa9Naj/2hPBOGTQkGYyM6TnB+T0cCkgh1iPzWmcuGSuko0YD/si+eG2Fou/e+Sr3LrRNe6SLVpXAutk/ihZUeWm0XFrCxQBxJonRFLteKlY1HtvvfTwrDmC84KmpcycTyNsxgqWdSqLQJl8+PYtXneO8rWU0QhpcQDGh03JGIjAqVcJqiM54EfSWKiP9K3k/HI110mgtYIr7+Z9PYf7VuMvE4YhrYupgbLvW75ztZ8gi9nRCNmXxXr+75LPdcZBVa4w8TnTzLbCeOto8LWepikAk8UeqhrFCdt2O0Yrkq0DttKZD/Rp 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 shrinker_rwsem is a global lock in shrinkers subsystem, it is easy to cause blocking in the following cases: a. the write lock of shrinker_rwsem was held for too long. For example, there are many memcgs in the system, which causes some paths to hold locks and traverse it for too long. (e.g. expand_shrinker_info()) b. the read lock of shrinker_rwsem was held for too long, and a writer came at this time. Then this writer will be forced to wait and block all subsequent readers. For example: - be scheduled when the read lock of shrinker_rwsem is held in do_shrink_slab() - some shrinker are blocked for too long. Like the case mentioned in the patchset[1]. Therefore, many times in history ([2],[3],[4],[5]), some people wanted to replace shrinker_rwsem reader with SRCU, but they all gave up because SRCU was not unconditionally enabled. But now, since commit 1cd0bd06093c ("rcu: Remove CONFIG_SRCU"), the SRCU is unconditionally enabled. So it's time to use SRCU to protect readers who previously held shrinker_rwsem. [1]. https://lore.kernel.org/lkml/20191129214541.3110-1-ptikhomirov@virtuozzo.com/ [2]. https://lore.kernel.org/all/1437080113.3596.2.camel@stgolabs.net/ [3]. https://lore.kernel.org/lkml/1510609063-3327-1-git-send-email-penguin-kernel@I-love.SAKURA.ne.jp/ [4]. https://lore.kernel.org/lkml/153365347929.19074.12509495712735843805.stgit@localhost.localdomain/ [5]. https://lore.kernel.org/lkml/20210927074823.5825-1-sultan@kerneltoast.com/ Signed-off-by: Qi Zheng --- mm/vmscan.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 9c1c5e8b24b8..95a3d6ddc6c1 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -202,6 +202,7 @@ static void set_task_reclaim_state(struct task_struct *task, LIST_HEAD(shrinker_list); DECLARE_RWSEM(shrinker_rwsem); +DEFINE_SRCU(shrinker_srcu); #ifdef CONFIG_MEMCG static int shrinker_nr_max; @@ -699,7 +700,7 @@ void free_prealloced_shrinker(struct shrinker *shrinker) void register_shrinker_prepared(struct shrinker *shrinker) { down_write(&shrinker_rwsem); - list_add_tail(&shrinker->list, &shrinker_list); + list_add_tail_rcu(&shrinker->list, &shrinker_list); shrinker->flags |= SHRINKER_REGISTERED; shrinker_debugfs_add(shrinker); up_write(&shrinker_rwsem); @@ -753,13 +754,15 @@ void unregister_shrinker(struct shrinker *shrinker) return; down_write(&shrinker_rwsem); - list_del(&shrinker->list); + list_del_rcu(&shrinker->list); shrinker->flags &= ~SHRINKER_REGISTERED; if (shrinker->flags & SHRINKER_MEMCG_AWARE) unregister_memcg_shrinker(shrinker); debugfs_entry = shrinker_debugfs_remove(shrinker); up_write(&shrinker_rwsem); + synchronize_srcu(&shrinker_srcu); + debugfs_remove_recursive(debugfs_entry); kfree(shrinker->nr_deferred); @@ -779,6 +782,7 @@ void synchronize_shrinkers(void) { down_write(&shrinker_rwsem); up_write(&shrinker_rwsem); + synchronize_srcu(&shrinker_srcu); } EXPORT_SYMBOL(synchronize_shrinkers); @@ -989,6 +993,7 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid, { unsigned long ret, freed = 0; struct shrinker *shrinker; + int srcu_idx; /* * The root memcg might be allocated even though memcg is disabled @@ -1000,10 +1005,10 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid, if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) return shrink_slab_memcg(gfp_mask, nid, memcg, priority); - if (!down_read_trylock(&shrinker_rwsem)) - goto out; + srcu_idx = srcu_read_lock(&shrinker_srcu); - list_for_each_entry(shrinker, &shrinker_list, list) { + list_for_each_entry_srcu(shrinker, &shrinker_list, list, + srcu_read_lock_held(&shrinker_srcu)) { struct shrink_control sc = { .gfp_mask = gfp_mask, .nid = nid, @@ -1014,19 +1019,9 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid, if (ret == SHRINK_EMPTY) ret = 0; freed += ret; - /* - * Bail out if someone want to register a new shrinker to - * prevent the registration from being stalled for long periods - * by parallel ongoing shrinking. - */ - if (rwsem_is_contended(&shrinker_rwsem)) { - freed = freed ? : 1; - break; - } } - up_read(&shrinker_rwsem); -out: + srcu_read_unlock(&shrinker_srcu, srcu_idx); cond_resched(); return freed; }