From patchwork Thu Feb 23 13:27:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13150328 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 8092BC636D6 for ; Thu, 23 Feb 2023 13:28:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0F7CD6B007E; Thu, 23 Feb 2023 08:28:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0A8466B0080; Thu, 23 Feb 2023 08:28:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8BCB6B0081; Thu, 23 Feb 2023 08:28:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D97116B007E for ; Thu, 23 Feb 2023 08:28:16 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AC39B160342 for ; Thu, 23 Feb 2023 13:28:16 +0000 (UTC) X-FDA: 80498635392.24.179E589 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) by imf06.hostedemail.com (Postfix) with ESMTP id C90F0180003 for ; Thu, 23 Feb 2023 13:28:14 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=DpDn8Y+E; spf=pass (imf06.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.179 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=1677158894; a=rsa-sha256; cv=none; b=UVx1GlqLgvFySY1tFIk1xSAutZkhEzGOjpns4/LZvQqvg/sdICgidzbY7yS5ROnN8Z4KEd IK8+/xN1o9g6svoNcuHI1f4tWXxHdDURBH/HTrOjKozITJPYZ9d08ju72DnOWKz879bMUf EnmGfDFUMYJM67I35x6rWO5IvVfxI3g= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=DpDn8Y+E; spf=pass (imf06.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.179 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=1677158894; 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=4ra+jS9qPdJDcLL3tCNVjDgmNt8q5h7NH0ipc1XhyKg=; b=WxsEf3ArUmu6e/BfFMsnjspBjNFOoDUsaPKi2lSR1910/CKUKz0AV2XbbIwsvwBI6aQYpr bkmNJto3kkAajgqUsOn7i+lgLbC4OJJD05u7G+bWN9LaB7VxIDw+pr2W9IKnEXOhGXqN8x t4hrAZiCnMhGJcpHT+7xTF6aEsAdmCo= Received: by mail-pg1-f179.google.com with SMTP id bn17so78562pgb.10 for ; Thu, 23 Feb 2023 05:28:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; 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=4ra+jS9qPdJDcLL3tCNVjDgmNt8q5h7NH0ipc1XhyKg=; b=DpDn8Y+EU7riH4+ntcDG6C1rbsBVGtUkMmoAHDS+pC4P++8EynweyAV0JYqlyYGOtq X1Q+BLwoJwcnB84jpi52eVKNQ4Z7H9xuUGTHfERfoIzzzU6ACyTdYigUXyW6IU9Ious4 Dcv7MyI3TgRfz/xnXu5LeFECNtdpthD72ekZGWDFwliGf58bj+lRnE/qeFjqbyzPR/dU QS9YrLbbOfovo0TIt/QhGWwwvNLxWcvBspvW0zUIZAUavOJoYjXfSG6Wy0VfFlInlS7B HNvoy7xqBZa0nU3FIEoV776Q0WiC55EpZXUOtrYeZXGMXYVopQbU7AkcvEji/pZV0uXq Rv+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=4ra+jS9qPdJDcLL3tCNVjDgmNt8q5h7NH0ipc1XhyKg=; b=ngEcht1NyWixXvIAwMGyP8ddOhB0WD3J9vjB7DEgoi33C5el99CXouJ9hsmbZ63DyH L2Zjz37PQkqxEbx1ajpzZFzZ9mEsfkhxZg5+jXC4omcmfqfz88e643mhnLJ7S7n4PxQE HFidi7c6ZdVIFbiE7YqhHCyNDBdkkluiqtcpSGfsA4RgF10WC7SFxGOLP2Y6kFd5DHSe 2ikfpyHW+LZKxhAOoFThlXPJlWWTcsYTEfLWoysUL2mJSmNxkLdM12AzwJWd9notjZsR WLLy9M2i/cy/Zoz5anouzJT9PvyHnkAAUugWI09AKBhRUFoTa8zKZjOhNE1De3uyWhRD IpIg== X-Gm-Message-State: AO0yUKX5OE0fd3T3IGuxQiTE0Ld8F/HBokRrS/aU3aMw+32ITJ4Vytqd TJvhB/dWAYYmUie7sbxLQDCEqQ== X-Google-Smtp-Source: AK7set8Z/82YLh8rwyoUePgrhBmvgU8h/2aIZeBSX/0142PpIqvK6RYABRkBwNOTr58e5iHS5l9UsQ== X-Received: by 2002:a05:6a00:299b:b0:5a8:ae97:25f2 with SMTP id cj27-20020a056a00299b00b005a8ae9725f2mr12832291pfb.0.1677158893792; Thu, 23 Feb 2023 05:28:13 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.245]) by smtp.gmail.com with ESMTPSA id g18-20020aa78752000000b005a9bf65b591sm3848591pfo.135.2023.02.23.05.28.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 05:28:13 -0800 (PST) From: Qi Zheng To: akpm@linux-foundation.org, tkhai@ya.ru, hannes@cmpxchg.org, shakeelb@google.com, mhocko@kernel.org, roman.gushchin@linux.dev, muchun.song@linux.dev, david@redhat.com, shy828301@gmail.com Cc: 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 v2 4/7] mm: shrinkers: make count and scan in shrinker debugfs lockless Date: Thu, 23 Feb 2023 21:27:22 +0800 Message-Id: <20230223132725.11685-5-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20230223132725.11685-1-zhengqi.arch@bytedance.com> References: <20230223132725.11685-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: C90F0180003 X-Rspamd-Server: rspam01 X-Stat-Signature: 4pdofrpf5c9k3gacpqo4f99x6a4n3fc6 X-HE-Tag: 1677158894-551687 X-HE-Meta: U2FsdGVkX1+Xk2HiVpa9abqLLmBz5Xqh6Nztpz/8Nj7yARh0UzPn30JyTGmH/JDm7bNle2AR/H1M5NayPHQcB55YhmHjvK8P/oDf+8GDRnGDGX0ssfCSdcEPZytEofMp0nbVZEYYziE0GoPQVpFhaaDtflKpnmJJMk6fhWlpwwyUAIf8cK1ETFUgvHHmmoRNNIxx95Nfrb+EE7KV4rICN3nW5oth8bCGk1tkmepc049G24yEOizFnOsJ3Wm/MCvCqf+MKt0elS/iPsNsMJxGftAt8HRF5Qz1XLt7MIZpbutCST3Z6aRoSP04R6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0+rrtTC23OUX2xHrp2sjEyDuRxr48UCuKxuNLoiU= 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..6aa7a7ec69da 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 = 0, 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;