Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp34294910rwd; Sun, 9 Jul 2023 08:55:25 -0700 (PDT) X-Google-Smtp-Source: APBJJlEgJIb1I8MS6WdUZhKjoiE7EWmFxpINekPuGR7v5ayGbUQX3jCA9DV3Xsj7YlS7jid138ti X-Received: by 2002:a17:906:154:b0:993:eb4b:ed51 with SMTP id 20-20020a170906015400b00993eb4bed51mr5523860ejh.27.1688918125632; Sun, 09 Jul 2023 08:55:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688918125; cv=none; d=google.com; s=arc-20160816; b=DUCGE5qDeI6xcP8wYHr0e7Jpm+valAIPwC4baUk7QoUDTDGYdk0Cnn/lO41Kkch45Q XurjJRSFSAx8BYfEvBA8IcqKv2gDzqY36mJsx8LiaaogWOOf3wkhZ10Wq6SjYQuP3zWd o6UpCRwDlfcQg39nxcWNbiXMpMFUMyeSl9AzXdxluYkG9enFtGIGyzDpS+x2tUkM3ttU ankQlRlbh4wmUCM1I+Ket9aLAWaIxDq/4u+P5CTjPwe5bJL8NoHNbuj7q8KlRrDzX5fE X8GqO8tDYl/BUlrYiPgWEcChR8SiJGqWZpee7ZLvhgsQOrToEwlc+Zp8p7w1zPGpgRJ2 qQfA== 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=f0j5wh3TdVkalC4vxH+YMUf4VBM+FJzx4pMwV8iaoZU=; fh=JQd50hxhsnDf0UEbJWlcTOJpDnhhNX6CqFfxPRL5eAI=; b=gWAglDZo5vgiTyt+x224AoZpychlrI2AMl8CU9oGDQ4Xd96spYX5pb3Tcuy82zWsk0 foiyHH+S7RSke4L1dQFGmuuaSO8T9AzuEgpqfcqi2rH+yDntCP4DnbppXjNYJy71m0tR IgrPZADDtDwix4NffbmJ6OzKre46g5/7U/dBFMKtD58HAN2Pb6xmwTYF9V84CiKb11Qw Dsaj1QFxjJTAMKBxH3nwZBr2I4Q0qBDQ4B9vurXmaltv7jcXv1brYEM2E0VhWTY5PtZS XV2CdkdEn8QLOvvyEie73++kvTCgDEzL/rdpOGupEZ3DDNi/CWGPoOyZ4QPywCXa5Ef4 qQNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YGSuyu6K; 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.01; Sun, 09 Jul 2023 08:55:25 -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=YGSuyu6K; 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 S232154AbjGIPph (ORCPT + 99 others); Sun, 9 Jul 2023 11:45:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231185AbjGIPpg (ORCPT ); Sun, 9 Jul 2023 11:45:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53E6DE0 for ; Sun, 9 Jul 2023 08:45:31 -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 E45B960B51 for ; Sun, 9 Jul 2023 15:45:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31251C433C7; Sun, 9 Jul 2023 15:45:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1688917530; bh=MAI6hufWFkizHOQGdFQeAwuxD15pXz/kWs3sDukUJYA=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=YGSuyu6Kn+dw/rUFv4T/wrn0KUQicixiIUJll3qhU8f2JgYOVmXfnHAmSPhjUZQx5 sY9b67QGHw004UcTJTaPSHKYRqV3OTL0bS61VFebqTDW644EjKM0UePFGB3iBICipF 1CmSHZXY0yeNeuC5X39/T4McllXxiPbIr8+AyN1njRoGOaSyBsWmeww9A3y1O7x/gR 7gVDwgywCeP6S6z5tjFGTG0Op6UhmjBhSVHH0EKq8wfpbujWPxmRmo13rUCmFqKZJo jZ5nFvpoM9T+nNoU9hQjBWoutXnKK4Mm7Wgs+YO5pcQ0+M1amEGr/teKIhwB0YUPGy jkce8gNyK1EeQ== Subject: [PATCH v1 3/6] NFSD: Replace nfsd_prune_bucket() From: Chuck Lever To: linux-nfs@vger.kernel.org Cc: Chuck Lever Date: Sun, 09 Jul 2023 11:45:29 -0400 Message-ID: <168891752919.3964.14131293897081561227.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 Enable nfsd_prune_bucket() to drop the bucket lock while calling kfree(). Use the same pattern that Jeff recently introduced in the NFSD filecache. A few percpu operations are moved outside the lock since they temporarily disable local IRQs which is expensive and does not need to be done while the lock is held. Signed-off-by: Chuck Lever --- fs/nfsd/nfscache.c | 78 ++++++++++++++++++++++++++++++++++++++++++---------- fs/nfsd/trace.h | 22 +++++++++++++++ 2 files changed, 85 insertions(+), 15 deletions(-) diff --git a/fs/nfsd/nfscache.c b/fs/nfsd/nfscache.c index 74fc9d9eeb1e..c8b572d2c72a 100644 --- a/fs/nfsd/nfscache.c +++ b/fs/nfsd/nfscache.c @@ -116,6 +116,21 @@ static void nfsd_cacherep_free(struct svc_cacherep *rp) kmem_cache_free(drc_slab, rp); } +static unsigned long +nfsd_cacherep_dispose(struct list_head *dispose) +{ + struct svc_cacherep *rp; + unsigned long freed = 0; + + while (!list_empty(dispose)) { + rp = list_first_entry(dispose, struct svc_cacherep, c_lru); + list_del(&rp->c_lru); + nfsd_cacherep_free(rp); + freed++; + } + return freed; +} + static void nfsd_cacherep_unlink_locked(struct nfsd_net *nn, struct nfsd_drc_bucket *b, struct svc_cacherep *rp) @@ -259,6 +274,41 @@ nfsd_cache_bucket_find(__be32 xid, struct nfsd_net *nn) return &nn->drc_hashtbl[hash]; } +/* + * Remove and return no more than @max expired entries in bucket @b. + * If @max is zero, do not limit the number of removed entries. + */ +static void +nfsd_prune_bucket_locked(struct nfsd_net *nn, struct nfsd_drc_bucket *b, + unsigned int max, struct list_head *dispose) +{ + unsigned long expiry = jiffies - RC_EXPIRE; + struct svc_cacherep *rp, *tmp; + unsigned int freed = 0; + + lockdep_assert_held(&b->cache_lock); + + /* The bucket LRU is ordered oldest-first. */ + 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(expiry, rp->c_timestamp)) + break; + + nfsd_cacherep_unlink_locked(nn, b, rp); + list_add(&rp->c_lru, dispose); + + if (max && ++freed > max) + break; + } +} + static long prune_bucket(struct nfsd_drc_bucket *b, struct nfsd_net *nn, unsigned int max) { @@ -282,11 +332,6 @@ static long prune_bucket(struct nfsd_drc_bucket *b, struct nfsd_net *nn, return freed; } -static long nfsd_prune_bucket(struct nfsd_drc_bucket *b, struct nfsd_net *nn) -{ - return prune_bucket(b, nn, 3); -} - /* * 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. @@ -442,6 +487,8 @@ int nfsd_cache_lookup(struct svc_rqst *rqstp) __wsum csum; struct nfsd_drc_bucket *b; int type = rqstp->rq_cachetype; + unsigned long freed; + LIST_HEAD(dispose); int rtn = RC_DOIT; rqstp->rq_cacherep = NULL; @@ -466,20 +513,18 @@ int nfsd_cache_lookup(struct svc_rqst *rqstp) found = nfsd_cache_insert(b, rp, nn); if (found != rp) goto found_entry; - - nfsd_stats_rc_misses_inc(); rqstp->rq_cacherep = rp; rp->c_state = RC_INPROG; + nfsd_prune_bucket_locked(nn, b, 3, &dispose); + spin_unlock(&b->cache_lock); + freed = nfsd_cacherep_dispose(&dispose); + trace_nfsd_drc_gc(nn, freed); + + nfsd_stats_rc_misses_inc(); atomic_inc(&nn->num_drc_entries); nfsd_stats_drc_mem_usage_add(nn, sizeof(*rp)); - - nfsd_prune_bucket(b, nn); - -out_unlock: - spin_unlock(&b->cache_lock); -out: - return rtn; + goto out; found_entry: /* We found a matching entry which is either in progress or done. */ @@ -517,7 +562,10 @@ int nfsd_cache_lookup(struct svc_rqst *rqstp) out_trace: trace_nfsd_drc_found(nn, rqstp, rtn); - goto out_unlock; +out_unlock: + spin_unlock(&b->cache_lock); +out: + return rtn; } /** diff --git a/fs/nfsd/trace.h b/fs/nfsd/trace.h index 2af74983f146..c06c505d04fb 100644 --- a/fs/nfsd/trace.h +++ b/fs/nfsd/trace.h @@ -1261,6 +1261,28 @@ TRACE_EVENT(nfsd_drc_mismatch, __entry->ingress) ); +TRACE_EVENT_CONDITION(nfsd_drc_gc, + TP_PROTO( + const struct nfsd_net *nn, + unsigned long freed + ), + TP_ARGS(nn, freed), + TP_CONDITION(freed > 0), + TP_STRUCT__entry( + __field(unsigned long long, boot_time) + __field(unsigned long, freed) + __field(int, total) + ), + TP_fast_assign( + __entry->boot_time = nn->boot_time; + __entry->freed = freed; + __entry->total = atomic_read(&nn->num_drc_entries); + ), + TP_printk("boot_time=%16llx total=%d freed=%lu", + __entry->boot_time, __entry->total, __entry->freed + ) +); + TRACE_EVENT(nfsd_cb_args, TP_PROTO( const struct nfs4_client *clp,