From patchwork Thu Jun 22 08:53:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13288591 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 1413DEB64DA for ; Thu, 22 Jun 2023 08:55:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9913A8D0007; Thu, 22 Jun 2023 04:55:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9415E8D0001; Thu, 22 Jun 2023 04:55:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 809C48D0007; Thu, 22 Jun 2023 04:55:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6F1A38D0001 for ; Thu, 22 Jun 2023 04:55:43 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 410C916040A for ; Thu, 22 Jun 2023 08:55:43 +0000 (UTC) X-FDA: 80929775766.12.9709BF1 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf18.hostedemail.com (Postfix) with ESMTP id 401461C001E for ; Thu, 22 Jun 2023 08:55:41 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=DTUuH0I9; spf=pass (imf18.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687424141; a=rsa-sha256; cv=none; b=k293em/gfhrjQU/GmT4wKnQOfuTZ6RC2J7CfrRdanYG9qaGiB07s0A0aMX4xn3kR+4wuMf rjr0iekRRnRHbQDQkFVJ4vM+8uHP1tb0KXRvFgLl//2SYPcMiMnn43r/YYrmPzAOpkGU4e bPHb12Jq4P0mbPEGK4kWlpA75hx00QU= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=DTUuH0I9; spf=pass (imf18.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687424141; 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=Qqb5b2GNvTgVTRVLR7iEMYBQ9KFY39GKHYb23c1SNH4=; b=JCEtgunJ3okQ4W0TrudqmXOvQ+0UD649w7ac+KNTdE48LnSgFbNkoZR1/r2FvcrtiDST3I jknODRD+xXbnDYLI3re4hk2BcY9baxBmerF9AnrS1SiuUsYTtsrEa7clSJjqhwVEdVFrZD atE/qM1y63+zH2UVuX+0d7wq9/PZBJU= Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1b5585e84b4so6827855ad.0 for ; Thu, 22 Jun 2023 01:55:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1687424140; x=1690016140; 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=Qqb5b2GNvTgVTRVLR7iEMYBQ9KFY39GKHYb23c1SNH4=; b=DTUuH0I9uHLii3LJ9Zyuu9c+EPbxYZC20xJ5+3ma+Kp5qpBNFOFTSMnclhdsEVCqLi IhMw89U+Z8kUvjOX0p1/U1TPShfVW/vJXGuaCCTRG1bGCD9nXSC4/ZiagSGalcNeTARf jURjtWkQSw5PrzWP1wBPTMrMWiplRo47rrBJgWE68ZF11RySU51PM2cKEbziuuvrByZZ Zy6z/i7b8JtNNsvUGe9g7/sl4BzjGsT0dMzcCmwPn8WMxyE8EEE7gXWjStqzol2ehPl/ 69+rSZ9IFQ8XiOW8vY+908jjX5pcw5aN354YWworQkitYsT+fnzRPZItmEL7YHvgl8Dv 6USw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687424140; x=1690016140; 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=Qqb5b2GNvTgVTRVLR7iEMYBQ9KFY39GKHYb23c1SNH4=; b=Co/MBgRKJuBu1ASXzFDET1tLx51WAVxy9r5hl2uXefH1SF+igtni1mmoqC6no0R0mR oCONQ9RfuYKv+beIgctmvm7ygMGooFlWrI8/bNkqfeyGJYLZ73UAC9XqESIcoYRqbYsR WF8iaCnyy3eKWSYCcp+5XA1ARFGplEJnVXRrbNAOTSDP9vxnpu3sf5WWgyr2n2ZU+/7I Rq6j7vdV/27q/GAjE//Nw3gqmSnka3/ER799ArDhVoOo1QxmQ9Xh8BV+ke4ZdJ0VFZAg RxFPdTjJWbHx7a+JbFeDugz8AT8XmUpu21pOx0gKGynRMfm2YRm9kjhplMNzdi5Uk8hc XkbQ== X-Gm-Message-State: AC+VfDxkFX170gJ5NHdzVGFR51JqqzZC4nz7K4/hZcUm2WdFsSmVRX5b vO1IMfVljUz/41e5EYR61AXmBA== X-Google-Smtp-Source: ACHHUZ4U5RlW8/2AChRXKukLEKek4tD/ljCLASAoWCRPvf/eu3Wdfis/k+W/YQGMZOvsRYh9ePd5+g== X-Received: by 2002:a17:902:d509:b0:1af:a349:3f31 with SMTP id b9-20020a170902d50900b001afa3493f31mr21732029plg.3.1687424140189; Thu, 22 Jun 2023 01:55:40 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.254]) by smtp.gmail.com with ESMTPSA id h2-20020a170902f7c200b001b549fce345sm4806971plw.230.2023.06.22.01.55.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jun 2023 01:55:39 -0700 (PDT) From: Qi Zheng To: akpm@linux-foundation.org, david@fromorbit.com, tkhai@ya.ru, vbabka@suse.cz, roman.gushchin@linux.dev, djwong@kernel.org, brauner@kernel.org, paulmck@kernel.org, tytso@mit.edu Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-arm-msm@vger.kernel.org, dm-devel@redhat.com, linux-raid@vger.kernel.org, linux-bcache@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-nfs@vger.kernel.org, linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, Qi Zheng Subject: [PATCH 12/29] mbcache: dynamically allocate the mbcache shrinker Date: Thu, 22 Jun 2023 16:53:18 +0800 Message-Id: <20230622085335.77010-13-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20230622085335.77010-1-zhengqi.arch@bytedance.com> References: <20230622085335.77010-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 401461C001E X-Stat-Signature: yghzp9yk7mxbszi1dgjog3xbg41gm4ni X-Rspam-User: X-HE-Tag: 1687424141-18888 X-HE-Meta: U2FsdGVkX189m1H6D0/VI4j9Wyhu5t7oeK253Qhcxhw9zKYohVYq+BBKOajed4deHdzZ2Mm0Ruk7/3jmekjKZCGOe7F4S353ZFf5hXiZSF3L0pp5MoMgg9P93mS+u+jtWA9BpCtV2pun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UggVc0Fy66BGMnSS1mKL8GaJGvX/eDn9qmXBpEZ5pRYVwOcEhXkiSr6nbeqTaD3k65m3MrHAMYxCUqAK6mvr+SzWT8qfM80I+4WAjb+Hr4oOPEq3dyNgieODDGGodGgTnNJ6IAC7UJ7p3iZWP7ZTvLHeQbdKM+jZ+9QeQtkxtY4Q/35pbv1fq/fD0abt 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: In preparation for implementing lockless slab shrink, we need to dynamically allocate the mbcache shrinker, so that it can be freed asynchronously using kfree_rcu(). Then it doesn't need to wait for RCU read-side critical section when releasing the struct mb_cache. Signed-off-by: Qi Zheng --- fs/mbcache.c | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/fs/mbcache.c b/fs/mbcache.c index 2a4b8b549e93..fec393e55a66 100644 --- a/fs/mbcache.c +++ b/fs/mbcache.c @@ -37,7 +37,7 @@ struct mb_cache { struct list_head c_list; /* Number of entries in cache */ unsigned long c_entry_count; - struct shrinker c_shrink; + struct shrinker *c_shrink; /* Work for shrinking when the cache has too many entries */ struct work_struct c_shrink_work; }; @@ -293,8 +293,7 @@ EXPORT_SYMBOL(mb_cache_entry_touch); static unsigned long mb_cache_count(struct shrinker *shrink, struct shrink_control *sc) { - struct mb_cache *cache = container_of(shrink, struct mb_cache, - c_shrink); + struct mb_cache *cache = shrink->private_data; return cache->c_entry_count; } @@ -333,8 +332,8 @@ static unsigned long mb_cache_shrink(struct mb_cache *cache, static unsigned long mb_cache_scan(struct shrinker *shrink, struct shrink_control *sc) { - struct mb_cache *cache = container_of(shrink, struct mb_cache, - c_shrink); + struct mb_cache *cache = shrink->private_data; + return mb_cache_shrink(cache, sc->nr_to_scan); } @@ -370,26 +369,30 @@ struct mb_cache *mb_cache_create(int bucket_bits) cache->c_hash = kmalloc_array(bucket_count, sizeof(struct hlist_bl_head), GFP_KERNEL); - if (!cache->c_hash) { - kfree(cache); - goto err_out; - } + if (!cache->c_hash) + goto err_c_hash; + for (i = 0; i < bucket_count; i++) INIT_HLIST_BL_HEAD(&cache->c_hash[i]); - cache->c_shrink.count_objects = mb_cache_count; - cache->c_shrink.scan_objects = mb_cache_scan; - cache->c_shrink.seeks = DEFAULT_SEEKS; - if (register_shrinker(&cache->c_shrink, "mbcache-shrinker")) { - kfree(cache->c_hash); - kfree(cache); - goto err_out; - } + cache->c_shrink = shrinker_alloc_and_init(mb_cache_count, mb_cache_scan, + 0, DEFAULT_SEEKS, 0, cache); + if (!cache->c_shrink) + goto err_shrinker; + + if (register_shrinker(cache->c_shrink, "mbcache-shrinker")) + goto err_register; INIT_WORK(&cache->c_shrink_work, mb_cache_shrink_worker); return cache; +err_register: + shrinker_free(cache->c_shrink); +err_shrinker: + kfree(cache->c_hash); +err_c_hash: + kfree(cache); err_out: return NULL; } @@ -406,7 +409,7 @@ void mb_cache_destroy(struct mb_cache *cache) { struct mb_cache_entry *entry, *next; - unregister_shrinker(&cache->c_shrink); + unregister_and_free_shrinker(cache->c_shrink); /* * We don't bother with any locking. Cache must not be used at this