From nobody Wed Oct 8 23:48:14 2025 Received: from mail.nfschina.com (unknown [42.101.60.213]) by smtp.subspace.kernel.org (Postfix) with SMTP id E7AD21F09B3; Mon, 23 Jun 2025 12:22:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=42.101.60.213 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750681370; cv=none; b=DVcohrgbTtEx/uzbHQOU0NuioCzQNEEVmddxw0OjOtIBKMf4kSuc9TbBFNsUzFt0CgfWN1X53YWOE/M8Z+LY0mfuLyjFnvLNNmaQTXfsuE9qFkk23hPMGaCbB8TD1uY6TlZWr/dPT7PtdQDmxjxLABeVl301QgF9A9RzgvlT+90= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750681370; c=relaxed/simple; bh=KesN4i+iw9iOvPBik8PMpmEBSOySopVhrh240s9MJKE=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=tjnwn8++J8DpxwC9XgzR3o5JBJYkj9ONzGya4iZQMH1tlLXi8fSoS6CaDZq8RZJczMwqD6LzF1QxT796nRgiuOIfE/fqB4lT3quYUgMwGH1qprsfQGcrO1TVYwIAAZ+ohpMRvhWrOEIeiGLlY1uXDXXGZCTCrYqi7cG3QPaP6W4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=nfschina.com; spf=pass smtp.mailfrom=nfschina.com; arc=none smtp.client-ip=42.101.60.213 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=nfschina.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nfschina.com Received: from longsh.shanghai.nfschina.local (unknown [180.167.10.98]) by mail.nfschina.com (MailData Gateway V2.8.8) with ESMTPSA id 4A98D603062FA; Mon, 23 Jun 2025 20:22:37 +0800 (CST) X-MD-Sfrom: suhui@nfschina.com X-MD-SrcIP: 180.167.10.98 From: Su Hui To: chuck.lever@oracle.com, jlayton@kernel.org, neil@brown.name, okorniev@redhat.com, Dai.Ngo@oracle.com, tom@talpey.com Cc: Su Hui , linux-nfs@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org Subject: [PATCH] nfsd: Using guard() to simplify nfsd_cache_lookup() Date: Mon, 23 Jun 2025 20:22:27 +0800 Message-Id: <20250623122226.3720564-1-suhui@nfschina.com> X-Mailer: git-send-email 2.30.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Using guard() to replace *unlock* label. guard() makes lock/unlock code more clear. Change the order of the code to let all lock code in the same scope. No functional changes. Signed-off-by: Su Hui --- fs/nfsd/nfscache.c | 99 ++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 51 deletions(-) diff --git a/fs/nfsd/nfscache.c b/fs/nfsd/nfscache.c index ba9d326b3de6..2d92adf3e6b0 100644 --- a/fs/nfsd/nfscache.c +++ b/fs/nfsd/nfscache.c @@ -489,7 +489,7 @@ int nfsd_cache_lookup(struct svc_rqst *rqstp, unsigned = int start, =20 if (type =3D=3D RC_NOCACHE) { nfsd_stats_rc_nocache_inc(nn); - goto out; + return rtn; } =20 csum =3D nfsd_cache_csum(&rqstp->rq_arg, start, len); @@ -500,64 +500,61 @@ int nfsd_cache_lookup(struct svc_rqst *rqstp, unsigne= d int start, */ rp =3D nfsd_cacherep_alloc(rqstp, csum, nn); if (!rp) - goto out; + return rtn; =20 b =3D nfsd_cache_bucket_find(rqstp->rq_xid, nn); - spin_lock(&b->cache_lock); - found =3D nfsd_cache_insert(b, rp, nn); - if (found !=3D rp) - goto found_entry; - *cacherep =3D rp; - rp->c_state =3D RC_INPROG; - nfsd_prune_bucket_locked(nn, b, 3, &dispose); - spin_unlock(&b->cache_lock); + scoped_guard(spinlock, &b->cache_lock) { + found =3D nfsd_cache_insert(b, rp, nn); + if (found =3D=3D rp) { + *cacherep =3D rp; + rp->c_state =3D RC_INPROG; + nfsd_prune_bucket_locked(nn, b, 3, &dispose); + goto out; + } + /* We found a matching entry which is either in progress or done. */ + nfsd_reply_cache_free_locked(NULL, rp, nn); + nfsd_stats_rc_hits_inc(nn); + rtn =3D RC_DROPIT; + rp =3D found; + + /* Request being processed */ + if (rp->c_state =3D=3D RC_INPROG) + goto out_trace; + + /* From the hall of fame of impractical attacks: + * Is this a user who tries to snoop on the cache? + */ + rtn =3D RC_DOIT; + if (!test_bit(RQ_SECURE, &rqstp->rq_flags) && rp->c_secure) + goto out_trace; =20 + /* Compose RPC reply header */ + switch (rp->c_type) { + case RC_NOCACHE: + break; + case RC_REPLSTAT: + xdr_stream_encode_be32(&rqstp->rq_res_stream, rp->c_replstat); + rtn =3D RC_REPLY; + break; + case RC_REPLBUFF: + if (!nfsd_cache_append(rqstp, &rp->c_replvec)) + return rtn; /* should not happen */ + rtn =3D RC_REPLY; + break; + default: + WARN_ONCE(1, "nfsd: bad repcache type %d\n", rp->c_type); + } + +out_trace: + trace_nfsd_drc_found(nn, rqstp, rtn); + return rtn; + } +out: nfsd_cacherep_dispose(&dispose); =20 nfsd_stats_rc_misses_inc(nn); atomic_inc(&nn->num_drc_entries); nfsd_stats_drc_mem_usage_add(nn, sizeof(*rp)); - goto out; - -found_entry: - /* We found a matching entry which is either in progress or done. */ - nfsd_reply_cache_free_locked(NULL, rp, nn); - nfsd_stats_rc_hits_inc(nn); - rtn =3D RC_DROPIT; - rp =3D found; - - /* Request being processed */ - if (rp->c_state =3D=3D RC_INPROG) - goto out_trace; - - /* From the hall of fame of impractical attacks: - * Is this a user who tries to snoop on the cache? */ - rtn =3D RC_DOIT; - if (!test_bit(RQ_SECURE, &rqstp->rq_flags) && rp->c_secure) - goto out_trace; - - /* Compose RPC reply header */ - switch (rp->c_type) { - case RC_NOCACHE: - break; - case RC_REPLSTAT: - xdr_stream_encode_be32(&rqstp->rq_res_stream, rp->c_replstat); - rtn =3D RC_REPLY; - break; - case RC_REPLBUFF: - if (!nfsd_cache_append(rqstp, &rp->c_replvec)) - goto out_unlock; /* should not happen */ - rtn =3D RC_REPLY; - break; - default: - WARN_ONCE(1, "nfsd: bad repcache type %d\n", rp->c_type); - } - -out_trace: - trace_nfsd_drc_found(nn, rqstp, rtn); -out_unlock: - spin_unlock(&b->cache_lock); -out: return rtn; } =20 --=20 2.30.2