Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp10563077rwd; Thu, 22 Jun 2023 01:27:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7Hxj4T5nxb8HlTCOmzbLPN4GT1tXzDqw29lT3sIniTzr2bce3LDq1BLQKxokE/8yvMB6i6 X-Received: by 2002:a05:6214:2625:b0:630:6926:d020 with SMTP id gv5-20020a056214262500b006306926d020mr15149737qvb.56.1687422473037; Thu, 22 Jun 2023 01:27:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687422472; cv=none; d=google.com; s=arc-20160816; b=nb04y+mouuXVjORvRbHsIZf43mAhN77YXSUSGDNOn61vb6RGVKONE3bq3zfRHWf4rU mt0O6/4yUvuU/4oCycbdX//rGm28GOsBa2BQasGFAWI+8GU2ImHFKd1p00pi3vNx+gZ0 cllC+8QwN6uUFrLk4SUQpBgUBi3L2HjyScQN/zdFndeKSuSkwb+Ma+AAFgU0cBBOF2zB nHMILB47cJU6AfAnMfqimBFOJ3OrhwM9mVybMRmI/2Mv7sdeETDHXIj+l7VoZrFB37Ul qMT/F60cuGB5XzYuv9Z6B2GpaFHyMZci5ailTV+f6ezsC6jILZuSDm/UfpjUCSNI3RDI Lz4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oQpOCkSVz0jjdFm4oEP2NJJ+zKFOVCxqbVN/b6qKeiM=; b=z18pH450f5kPGezpdzUJ3CpdODB/B1TLgiGDCMbl6VHOnItiKNs0awrHXKLnkSeeNS Bqg+N6Q1EWWl1PfPdgp/qJ/IN1Q3JUiWoVgGVlMSiQEVxGtN3nr+PUSMhZXG3SxvVy+2 6C7SDF/wMvc/Gh2KSBHy6ITyCJsLsW1ijzQRDqM5el3w9yHGQPZVW0G5UR18Ge+XBZaI FKFClkXIC1LEVWoG8rX1yFQHb1XQCumW1T8ruE9NQs7PYK/OctQYi1CgUQQEDdEzyWBD pAOeWVAwFkfYS/JpPqHNJYCTrg6zjCYH+QDNSPcYcx+TzCa9eua8H/C7A36uQ3dZOqWA pa1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Vou+QBcB; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j73-20020a638b4c000000b0055355a29740si4960203pge.718.2023.06.22.01.27.38; Thu, 22 Jun 2023 01:27:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Vou+QBcB; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230252AbjFVI0m (ORCPT + 99 others); Thu, 22 Jun 2023 04:26:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230216AbjFVI0c (ORCPT ); Thu, 22 Jun 2023 04:26:32 -0400 Received: from out-10.mta0.migadu.com (out-10.mta0.migadu.com [IPv6:2001:41d0:1004:224b::a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F243E1BE1 for ; Thu, 22 Jun 2023 01:26:30 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1687422388; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oQpOCkSVz0jjdFm4oEP2NJJ+zKFOVCxqbVN/b6qKeiM=; b=Vou+QBcBsFfrycM2PkKySs/6vc7t4z2y+ESRPtdGGBu1nh2ELX2n3XwLyStu2J7FwIsey0 GRSdEt3Uk6O3X+qjfRuw6k2zIEyamPbwTm+TOHkS5CEaR6KvBgQJdhpJF9uZHGex1bKqla yWmLNi3h067slmAnbRZ3GfDMps2hTHE= 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: airlied@gmail.com, daniel@ffwll.ch, robdclark@gmail.com, quic_abhinavk@quicinc.com, dmitry.baryshkov@linaro.org, sean@poorly.run, marijn.suijten@somainline.org, robh@kernel.org, tomeu.vizoso@collabora.com, steven.price@arm.com, alyssa.rosenzweig@collabora.com, agk@redhat.com, snitzer@kernel.org, song@kernel.org, colyli@suse.de, kent.overstreet@gmail.com, namit@vmware.com, gregkh@linuxfoundation.org, mst@redhat.com, david@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, viro@zeniv.linux.org.uk, adilger.kernel@dilger.ca, jack@suse.com, chuck.lever@oracle.com, neilb@suse.de, kolga@netapp.com, minchan@kernel.org, senozhatsky@chromium.org, clm@fb.com, josef@toxicpanda.com, dsterba@suse.com, christian.koenig@amd.com, ray.huang@amd.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, freedreno@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 02/29] mm: vmscan: introduce some helpers for dynamically allocating shrinker Date: Thu, 22 Jun 2023 08:24:27 +0000 Message-Id: <20230622082454.4090236-3-qi.zheng@linux.dev> In-Reply-To: <20230622082454.4090236-1-qi.zheng@linux.dev> References: <20230622082454.4090236-1-qi.zheng@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org From: Qi Zheng Introduce some helpers for dynamically allocating shrinker instance, and their uses are as follows: 1. shrinker_alloc_and_init() Used to allocate and initialize a shrinker instance, the priv_data parameter is used to pass the pointer of the previously embedded structure of the shrinker instance. 2. shrinker_free() Used to free the shrinker instance when the registration of shrinker fails. 3. unregister_and_free_shrinker() Used to unregister and free the shrinker instance, and the kfree() will be changed to kfree_rcu() later. Signed-off-by: Qi Zheng --- include/linux/shrinker.h | 12 ++++++++++++ mm/vmscan.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h index 43e6fcabbf51..8e9ba6fa3fcc 100644 --- a/include/linux/shrinker.h +++ b/include/linux/shrinker.h @@ -107,6 +107,18 @@ extern void unregister_shrinker(struct shrinker *shrinker); extern void free_prealloced_shrinker(struct shrinker *shrinker); extern void synchronize_shrinkers(void); +typedef unsigned long (*count_objects_cb)(struct shrinker *s, + struct shrink_control *sc); +typedef unsigned long (*scan_objects_cb)(struct shrinker *s, + struct shrink_control *sc); + +struct shrinker *shrinker_alloc_and_init(count_objects_cb count, + scan_objects_cb scan, long batch, + int seeks, unsigned flags, + void *priv_data); +void shrinker_free(struct shrinker *shrinker); +void unregister_and_free_shrinker(struct shrinker *shrinker); + #ifdef CONFIG_SHRINKER_DEBUG extern int shrinker_debugfs_add(struct shrinker *shrinker); extern struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker, diff --git a/mm/vmscan.c b/mm/vmscan.c index 45d17c7cc555..64ff598fbad9 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -809,6 +809,41 @@ void unregister_shrinker(struct shrinker *shrinker) } EXPORT_SYMBOL(unregister_shrinker); +struct shrinker *shrinker_alloc_and_init(count_objects_cb count, + scan_objects_cb scan, long batch, + int seeks, unsigned flags, + void *priv_data) +{ + struct shrinker *shrinker; + + shrinker = kzalloc(sizeof(struct shrinker), GFP_KERNEL); + if (!shrinker) + return NULL; + + shrinker->count_objects = count; + shrinker->scan_objects = scan; + shrinker->batch = batch; + shrinker->seeks = seeks; + shrinker->flags = flags; + shrinker->private_data = priv_data; + + return shrinker; +} +EXPORT_SYMBOL(shrinker_alloc_and_init); + +void shrinker_free(struct shrinker *shrinker) +{ + kfree(shrinker); +} +EXPORT_SYMBOL(shrinker_free); + +void unregister_and_free_shrinker(struct shrinker *shrinker) +{ + unregister_shrinker(shrinker); + kfree(shrinker); +} +EXPORT_SYMBOL(unregister_and_free_shrinker); + /** * synchronize_shrinkers - Wait for all running shrinkers to complete. * -- 2.30.2