From patchwork Mon Feb 20 09:16:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13146180 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 DB729C636CC for ; Mon, 20 Feb 2023 09:17:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F5046B0074; Mon, 20 Feb 2023 04:17:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A59D6B0075; Mon, 20 Feb 2023 04:17:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 694D36B0078; Mon, 20 Feb 2023 04:17:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 5A8406B0074 for ; Mon, 20 Feb 2023 04:17:20 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 289FEA0EEF for ; Mon, 20 Feb 2023 09:17:20 +0000 (UTC) X-FDA: 80487116640.28.DBDA568 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf28.hostedemail.com (Postfix) with ESMTP id 4BA05C0002 for ; Mon, 20 Feb 2023 09:17:18 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b="pwzSI/vf"; spf=pass (imf28.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.49 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=1676884638; 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=3zoWZenRiYoNEE8cLwabKUnZ2EMcSBm37bQ/nay5M2Q=; b=BTaTBjI350efGhMWsVHyQ/pLeebrDxGTURtN68apjiGzJjj+cX9sEfLyG2IvciCvRb/hcD MkYw0x1syCcIh9ctUjob+PbW9G4eDAL1deYgqIKjOhjAjGYKb/dAGItGLM0Qo/bYmNc2qg Es47pav0oHxpygkiKT7f7Is11gbwIlY= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b="pwzSI/vf"; spf=pass (imf28.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.49 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=1676884638; a=rsa-sha256; cv=none; b=n5E6g0KmtGVKD6fHHrlnDpgjiQB0fd0eyi1kheO7iU9eYmLvMND+DeeUrvf59bDub3fIZk 0o7A4SZjhxbrPPnWpN/X1mOxGl20jYd5JzjV5bVyaZEBfEqMU9kBs7bXpN7PfAX4C0MKZE VZx9yE3Aa+6UFOq1s6Q8elpSIVf3EfY= Received: by mail-pj1-f49.google.com with SMTP id pt11so1278302pjb.1 for ; Mon, 20 Feb 2023 01:17:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; t=1676884637; 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=3zoWZenRiYoNEE8cLwabKUnZ2EMcSBm37bQ/nay5M2Q=; b=pwzSI/vf4Vj+GnZxNmyMXkgptBLDA76ZJ1o4OmWFlr6dQarsPx2C++HYAz6Dpn2Aht AJqCW7TXaRjGCwXrIkRHBWZUa/9Uq4ShcEkudpvCqCUfsPPUCgYIFXYWtN9++5OxpAqO gLMuad7DyF/pXHMjpwmBdYV9XSfe7U12nWp4ShfpW3jKBm3KNO+XTZT5cAaCcrQzzIV1 oqisd022sPfT5wczUD241WMl4IN/0R3a2LXx3Bxnlvt0dMus9chI20ZmPMJtAtZCf//h 6YyC4HjkwgSHI06kMieLeE16TKJuxDd6RvwRYYtadIFV1TOrOtPRFHlt9t2tMxB1hTiB KYrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1676884637; 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=3zoWZenRiYoNEE8cLwabKUnZ2EMcSBm37bQ/nay5M2Q=; b=L+JEpRDD+bv6IGylhSEQrx7DQimXOKN/k8MFMkJtynsFwKcvdkPEO6DCTFEBB4/3Xm AZgkfhwi5LtCoOGEUqC37xXitpZ7JDRFGf6Fy6BnR2p0cWvs4Nk80RYpXJXAc6/FNBWL myHeSf7pvTwP88D6E/u5aYjR8V39ZuPvGRRvIFICvJ701gjVodRCLVYEBpYJpMPuEJDU 1PsSiXRDuFCjj3sqk5lFhlF2du5ISEUrMXrjGMQ7LjcPZVAEeA7c96Al2BeLNwcUbpNh spLgmJUAAx22QTPHibg6CGwf7KHsnKGnYRukt54lEUQ71WzyxS9UC5rZy376VOm25mgv y9wg== X-Gm-Message-State: AO0yUKUHa5SYQJv7NciZnAe8LEB+Upg940pvUCJ72v8zA8otSCqLVrJc paobGcxHtXzc3CBfq03bnZOreQ== X-Google-Smtp-Source: AK7set/u/nMGrcmsUwC5AW5uACSHbng2msaMYYygQeSDbSQ22MPUA9tjyQkhFmQoA6dgXKjSY0gUIw== X-Received: by 2002:a17:90a:c915:b0:234:175:839a with SMTP id v21-20020a17090ac91500b002340175839amr660167pjt.4.1676884637069; Mon, 20 Feb 2023 01:17:17 -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.17.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Feb 2023 01:17:16 -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 3/5] mm: shrinkers: make count and scan in shrinker debugfs lockless Date: Mon, 20 Feb 2023 17:16:35 +0800 Message-Id: <20230220091637.64865-4-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-Stat-Signature: ncrb45sg7ocf5myezi1jsfmd45thrxis X-Rspam-User: X-Rspamd-Queue-Id: 4BA05C0002 X-Rspamd-Server: rspam06 X-HE-Tag: 1676884638-172083 X-HE-Meta: U2FsdGVkX19/fL/JqZ0P1tfP4d5bniLP9rrSdZjdomEk2JNgKvZH0T6ick5c9xgy+CByFEbqPn+3z8ZCupd8UiuRaSWZ6aIvXoGCZlipHu5CTYFJn/JFAaDA2bp2cYbpE6IWEPu0pM6RGfgKlJ5WULBhzfSTiYEqcX1Kb/9mUKdXF2285+QsrxCuje9EXOE0Dk0OCg/xS3A6YAUvEJdkEEvFr7qC+m5uPNy0+PS7Sf0XCAHn/tl/t6xAe9YgolRyYWnMgmdI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Gf0JVBS7ZztHuGM/0lUe0mIs7sJuVTmp0jlW68qGWIyAlOjFQQCORjgirtTWVd3ZSBWsql3s2XGM5tyrSjU0SmXlGF5+aQplPNOhcnSlhLpUa/Rc48PD8Q87icBq4AXAVVSts= 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: Like global and memcg slab shrink, also use SRCU to make count and scan operations in memory shrinker debugfs lockless. Signed-off-by: Qi Zheng --- mm/shrinker_debug.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/mm/shrinker_debug.c b/mm/shrinker_debug.c index 39c3491e28a3..6a26e8ac40aa 100644 --- a/mm/shrinker_debug.c +++ b/mm/shrinker_debug.c @@ -9,6 +9,7 @@ /* defined in vmscan.c */ extern struct rw_semaphore shrinker_rwsem; extern struct list_head shrinker_list; +extern struct srcu_struct shrinker_srcu; static DEFINE_IDA(shrinker_debugfs_ida); static struct dentry *shrinker_debugfs_root; @@ -49,18 +50,13 @@ static int shrinker_debugfs_count_show(struct seq_file *m, void *v) struct mem_cgroup *memcg; unsigned long total; bool memcg_aware; - int ret, nid; + int ret, nid, srcu_idx; count_per_node = kcalloc(nr_node_ids, sizeof(unsigned long), GFP_KERNEL); if (!count_per_node) return -ENOMEM; - ret = down_read_killable(&shrinker_rwsem); - if (ret) { - kfree(count_per_node); - return ret; - } - rcu_read_lock(); + srcu_idx = srcu_read_lock(&shrinker_srcu); memcg_aware = shrinker->flags & SHRINKER_MEMCG_AWARE; @@ -91,8 +87,7 @@ static int shrinker_debugfs_count_show(struct seq_file *m, void *v) } } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); - rcu_read_unlock(); - up_read(&shrinker_rwsem); + srcu_read_unlock(&shrinker_srcu, srcu_idx); kfree(count_per_node); return ret; @@ -115,9 +110,8 @@ static ssize_t shrinker_debugfs_scan_write(struct file *file, .gfp_mask = GFP_KERNEL, }; struct mem_cgroup *memcg = NULL; - int nid; + int nid, srcu_idx; char kbuf[72]; - ssize_t ret; read_len = size < (sizeof(kbuf) - 1) ? size : (sizeof(kbuf) - 1); if (copy_from_user(kbuf, buf, read_len)) @@ -146,11 +140,7 @@ static ssize_t shrinker_debugfs_scan_write(struct file *file, return -EINVAL; } - ret = down_read_killable(&shrinker_rwsem); - if (ret) { - mem_cgroup_put(memcg); - return ret; - } + srcu_idx = srcu_read_lock(&shrinker_srcu); sc.nid = nid; sc.memcg = memcg; @@ -159,7 +149,7 @@ static ssize_t shrinker_debugfs_scan_write(struct file *file, shrinker->scan_objects(shrinker, &sc); - up_read(&shrinker_rwsem); + srcu_read_unlock(&shrinker_srcu, srcu_idx); mem_cgroup_put(memcg); return size;