Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp4170201rdh; Tue, 28 Nov 2023 14:00:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IGr6r7d5/cFzsqM/NJA7o5L5IuZuivdatKfDYsuaqfdJ++igArj77wFx6+9H4FTn3Pc5AIg X-Received: by 2002:a17:903:1205:b0:1cf:c01d:c056 with SMTP id l5-20020a170903120500b001cfc01dc056mr11409250plh.57.1701208797818; Tue, 28 Nov 2023 13:59:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701208797; cv=none; d=google.com; s=arc-20160816; b=xGBJfxnNdvfZjkjXhIKA4zghhICc0DPhsK5mnbPPNFDNQE2s0S6+8nfTdi9rBDxRUX OiojNZAKDeCF1Fh4Nj/OHvLPw+qKUFOB52q+sv2IeoqZUnaEnqVdbzR/HlYQDEr0pD1r m1dEBPG05hVso+kmFjlgpW+AZzHEfDSX9hHGzJjk6QDde+K48dwN2ejeqYyVJ1/uen7F MF13OG8DQrY0cYRY94C2CSCeARx+50OlVh8Aa5mWLgYvsydOkooyaPdtr5BKrhGi6yaN MBeHf5LwxTDOsBdvx8O26tKWZU2wMh5szSTIQ95aM16KDePLMUwuNbc+REdj0X5XTneY 7PjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :message-id:date:cc:to:from:subject:dkim-signature; bh=z0iUrnDR8VW5FxFwSlb7xiqvQrjoHTqjJ11a8XOFHnM=; fh=xkJJ9G/tfqFUcIeP82drScMuFzbH6I4bp3/iFRpdd9E=; b=ArZKHtYXf9zY8YxzNmryNojX6fuY6uWWrZRe27yBuY30K375Wt9H+qWZ/swbPAXjgV +iA5ISJfKrapqDH9CMyK/m9FLUJE3YM711EAQgFBLPg3NSTbOchDbKSFbAEnKpAfHVRh 5aBMDeFuHJEi0K5P8T1pxruOV88BLeNcuihvpgKhqQDqHOLEQiNZwN1e2jOiFUJ2tgOl g+TK0VpKM6GFAL9ThKPffcU0CE2PfiycxUiw+D0DpttDWm76gZa0hLNHMuhro2GrhOr3 aCIoN4kxamtp/7nk+jDiGhTZ2wZqDqjod8AsaKAYYXvKsOu/whdyt4uaNJa8cd2Epfwc 3Jpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=phqEwuK8; spf=pass (google.com: domain of linux-nfs+bounces-142-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-nfs+bounces-142-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id ik7-20020a170902ab0700b001bc162f3318si12048620plb.640.2023.11.28.13.59.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 13:59:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-nfs+bounces-142-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=phqEwuK8; spf=pass (google.com: domain of linux-nfs+bounces-142-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-nfs+bounces-142-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 0F0B1B212EC for ; Tue, 28 Nov 2023 21:59:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 60C81481AA; Tue, 28 Nov 2023 21:59:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="phqEwuK8" X-Original-To: linux-nfs@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F02A44390; Tue, 28 Nov 2023 21:59:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 99622C433C7; Tue, 28 Nov 2023 21:59:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701208792; bh=y802RwPXiM9fBBO36qY7gEeo1NtU9x/a2IaBBM9TSWA=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=phqEwuK8ug4+sCDjmjHkHkdqIvoN0a4NBdEL1Z8rFgKPp9FDxak7tLugQnHVrTYeu RJG4arL606XSpiWXnapPJXQ4fwzjU9K4FyUHOX+WQoE0gXrcRu+lwLOaMaIwLJ72vA /g0qth1ju7H3qfTJjyW7gV6nEzzOD1vLEWFDSB2R5pd5dlp7jDWJqLpCk2nb9vk5vT 8vOhnSERSyAjWcerM0tX4cgIRAKLGmreQGeahFE8YAQAoLbYPKNpR9FzWv4Rao7huB gKGA1tRj1hfly7azUyNBj54Jo44GbkX/zqCE7jMJc/7Y8Y6tPwjeex3R67jPkQG2LM cqJNb626z5xBA== Subject: [PATCH 3/8] NFSD: Replace nfsd_prune_bucket() From: Chuck Lever To: stable@vger.kernel.org Cc: linux-nfs@vger.kernel.org Date: Tue, 28 Nov 2023 16:59:51 -0500 Message-ID: <170120879169.1515.56299317736755341.stgit@klimt.1015granger.net> In-Reply-To: <170120874713.1515.13712791731008720729.stgit@klimt.1015granger.net> References: <170120874713.1515.13712791731008720729.stgit@klimt.1015granger.net> User-Agent: StGit/1.5 Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit From: Chuck Lever [ Upstream commit a9507f6af1450ed26a4a36d979af518f5bb21e5d ] 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. Reviewed-by: Jeff Layton 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 40b8bbfc0950..9dfa2c03b712 100644 --- a/fs/nfsd/nfscache.c +++ b/fs/nfsd/nfscache.c @@ -117,6 +117,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) @@ -260,6 +275,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) { @@ -283,11 +333,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. @@ -443,6 +488,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; @@ -467,20 +514,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. */ @@ -518,7 +563,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,