Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp34295360rwd; Sun, 9 Jul 2023 08:56:02 -0700 (PDT) X-Google-Smtp-Source: APBJJlHlouvn9prXJkaA4JHpdTI9xSts8xph+tw8f1L5rJdUSvCW5jz1TZX9UhKLUmfTNRfMN95+ X-Received: by 2002:a17:906:297:b0:982:c8d0:683f with SMTP id 23-20020a170906029700b00982c8d0683fmr9447988ejf.18.1688918161704; Sun, 09 Jul 2023 08:56:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688918161; cv=none; d=google.com; s=arc-20160816; b=HWvPsywFv//sE88dr+MxmPni0tIBcaljaFEbSH67pMd+FgUxa9+ceOWV8mjU8nAL3B O4BOhpJEJq29HwBEHeH2aRg+trOt4rmk4RgmTmY667Rv+DnVeP21u/d0SNL9JcWiEtHM g7ipag7d1OzfLZQXXInhTOfVT6MnMHlhwZTCygdhYqb/IsG5Ogjpp44m/uiJS7UFQ24T HZEWlla57G8T45FAQIpeI+Ak69v0xB9gz8VzOGMerBA+R16ykPl3ZfId/c+a/f1w3W+n hZUuKjSkPPakxaSec7aLK39f4OnaQE4qf+xLNxAeRDjkQRMKgnIqJD3FtvjnZtwBhdpu iyrQ== 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 :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:dkim-signature; bh=01vJNyFlKMVYvBluvgCH29HUJbTBpR43whwrssUZFJ8=; fh=JQd50hxhsnDf0UEbJWlcTOJpDnhhNX6CqFfxPRL5eAI=; b=um5bXCcGBsc98iU9eznIXIMNwGK+zlEUbwGO6w8D+k9ws6U7B2h+Fk95IqONskM1o7 p3kn9GLWaiLs+SKdKti99u5ajdRc2TEIQZPzQ9TVju46zjX3NAHTqeUAHauRSI72+g6V QT5hN9to7HtA1DYGTWqRsXZRjX1bFkp0bzgP0D0P0eai3rvQKzBwp3SGuam80+TQ8fPK 0zMXcWLzUZftuh/wBhaPTEHJmaXoV3o7Ax8bgpUQF6obNOET//Ai47yU+hUJvHPqLRAe iHG+0hnxUKI7SfLB/yec7TqY7l8JBLNuL/4RGsVze4KmTxWQPa+GYFCPJwlPjrHcIVLT nAgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rguEMf9l; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qw5-20020a170906fca500b00992acf124b3si5763903ejb.926.2023.07.09.08.55.37; Sun, 09 Jul 2023 08:56:01 -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=@kernel.org header.s=k20201202 header.b=rguEMf9l; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230155AbjGIPpk (ORCPT + 99 others); Sun, 9 Jul 2023 11:45:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233020AbjGIPpj (ORCPT ); Sun, 9 Jul 2023 11:45:39 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD4A791 for ; Sun, 9 Jul 2023 08:45:37 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 41E4560BCC for ; Sun, 9 Jul 2023 15:45:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E5CBC433C9; Sun, 9 Jul 2023 15:45:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1688917536; bh=ESyCxUz+L4CLWq/U+M6xm5NiCuhMJDeWLU2DOm1WrZY=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=rguEMf9lihj+7wqKm2/6fLPzRhJ+nNWLtbhZe95KTCAij5xuDnHMWf7wrzcFfK2cJ LOKUbVoL9t0jTeVfwg8h1LWpIvP2vvyiSlfirV5pufk9fB7ktlVOgRHyh2ldeAGcON uQWzq/3LNvHUmLWrQDIQWsnE6+09CYejc3sFyVedJIOUBYlwoBagz5/sKIsZVrLOO+ Ytz19zAxPo95lp5MXSPou51upkfskjo8599WlK00cbIjio6esdlCtz0kjQXyGwqyLV 3bMxYO0O+mXaleOzjeRNJI3wFF2LK+j+2F6Xw2PNx0DJnuPRNg2az0RPCZF4QSQLJs dOlFGv+iXhboA== Subject: [PATCH v1 4/6] NFSD: Refactor the duplicate reply cache shrinker From: Chuck Lever To: linux-nfs@vger.kernel.org Cc: Chuck Lever Date: Sun, 09 Jul 2023 11:45:35 -0400 Message-ID: <168891753555.3964.17240379490969380455.stgit@manet.1015granger.net> In-Reply-To: <168891733570.3964.15456501153247760888.stgit@manet.1015granger.net> References: <168891733570.3964.15456501153247760888.stgit@manet.1015granger.net> User-Agent: StGit/1.5 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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: Chuck Lever Avoid holding the bucket lock while freeing cache entries. This change also caps the number of entries that are freed when the shrinker calls to reduce the shrinker's impact on the cache's effectiveness. Signed-off-by: Chuck Lever --- fs/nfsd/nfscache.c | 82 +++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/fs/nfsd/nfscache.c b/fs/nfsd/nfscache.c index c8b572d2c72a..c08078ac9284 100644 --- a/fs/nfsd/nfscache.c +++ b/fs/nfsd/nfscache.c @@ -309,68 +309,64 @@ nfsd_prune_bucket_locked(struct nfsd_net *nn, struct nfsd_drc_bucket *b, } } -static long prune_bucket(struct nfsd_drc_bucket *b, struct nfsd_net *nn, - unsigned int max) +/** + * nfsd_reply_cache_count - count_objects method for the DRC shrinker + * @shrink: our registered shrinker context + * @sc: garbage collection parameters + * + * Returns the total number of entries in the duplicate reply cache. To + * keep things simple and quick, this is not the number of expired entries + * in the cache (ie, the number that would be removed by a call to + * nfsd_reply_cache_scan). + */ +static unsigned long +nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc) { - struct svc_cacherep *rp, *tmp; - long freed = 0; + struct nfsd_net *nn = container_of(shrink, + struct nfsd_net, nfsd_reply_cache_shrinker); - list_for_each_entry_safe(rp, tmp, &b->lru_head, c_lru) { - /* - * Don't free entries attached to calls that are still - * in-progress, but do keep scanning the list. - */ - if (rp->c_state == RC_INPROG) - continue; - if (atomic_read(&nn->num_drc_entries) <= nn->max_drc_entries && - time_before(jiffies, rp->c_timestamp + RC_EXPIRE)) - break; - nfsd_reply_cache_free_locked(b, rp, nn); - if (max && freed++ > max) - break; - } - return freed; + return atomic_read(&nn->num_drc_entries); } -/* - * Walk the LRU list and prune off entries that are older than RC_EXPIRE. - * Also prune the oldest ones when the total exceeds the max number of entries. +/** + * nfsd_reply_cache_scan - scan_objects method for the DRC shrinker + * @shrink: our registered shrinker context + * @sc: garbage collection parameters + * + * Free expired entries on each bucket's LRU list until we've released + * nr_to_scan freed objects. Nothing will be released if the cache + * has not exceeded it's max_drc_entries limit. + * + * Returns the number of entries released by this call. */ -static long -prune_cache_entries(struct nfsd_net *nn) +static unsigned long +nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc) { + struct nfsd_net *nn = container_of(shrink, + struct nfsd_net, nfsd_reply_cache_shrinker); + unsigned long freed = 0; + LIST_HEAD(dispose); unsigned int i; - long freed = 0; for (i = 0; i < nn->drc_hashsize; i++) { struct nfsd_drc_bucket *b = &nn->drc_hashtbl[i]; if (list_empty(&b->lru_head)) continue; + spin_lock(&b->cache_lock); - freed += prune_bucket(b, nn, 0); + nfsd_prune_bucket_locked(nn, b, 0, &dispose); spin_unlock(&b->cache_lock); - } - return freed; -} -static unsigned long -nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc) -{ - struct nfsd_net *nn = container_of(shrink, - struct nfsd_net, nfsd_reply_cache_shrinker); + freed += nfsd_cacherep_dispose(&dispose); + if (freed > sc->nr_to_scan) + break; + } - return atomic_read(&nn->num_drc_entries); + trace_nfsd_drc_gc(nn, freed); + return freed; } -static unsigned long -nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc) -{ - struct nfsd_net *nn = container_of(shrink, - struct nfsd_net, nfsd_reply_cache_shrinker); - - return prune_cache_entries(nn); -} /* * Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes */