From: Christoph Hellwig <hch@lst.de>
To: Trond Myklebust <trondmy@kernel.org>, Anna Schumaker <anna@kernel.org>
Cc: linux-nfs@vger.kernel.org
Subject: [PATCH 17/24] NFS: take a delegation reference in nfs4_get_valid_delegation
Date: Thu, 18 Dec 2025 06:56:21 +0100 [thread overview]
Message-ID: <20251218055633.1532159-18-hch@lst.de> (raw)
In-Reply-To: <20251218055633.1532159-1-hch@lst.de>
Currently most work on struct nfs_delegation happens directly under RCU
protection. This is generally fine, despite that long RCU sections are
not good for performance. But for operations later taking a reference
to the delegation to perform blocking work, refcount_inc is used, which
can be racy against dropping the last reference and thus lead to use
after frees in extremely rare cases.
Fix this by taking a reference in nfs4_get_valid_delegation using
refcount_inc_not_zero so that the callers have a stabilized reference
they can work with and can be moved outside the RCU critical section.
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
fs/nfs/callback_proc.c | 13 ++++++---
fs/nfs/delegation.c | 62 ++++++++++++++++++++++--------------------
fs/nfs/delegation.h | 1 +
fs/nfs/nfs4proc.c | 26 +++++++++---------
4 files changed, 56 insertions(+), 46 deletions(-)
diff --git a/fs/nfs/callback_proc.c b/fs/nfs/callback_proc.c
index 8397c43358bd..57550020c819 100644
--- a/fs/nfs/callback_proc.c
+++ b/fs/nfs/callback_proc.c
@@ -51,12 +51,18 @@ __be32 nfs4_callback_getattr(void *argp, void *resp,
-ntohl(res->status));
goto out;
}
- rcu_read_lock();
+
delegation = nfs4_get_valid_delegation(inode);
- if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
+ if (!delegation)
goto out_iput;
- res->size = i_size_read(inode);
+ if ((delegation->type & FMODE_WRITE) == 0) {
+ nfs_put_delegation(delegation);
+ goto out_iput;
+ }
res->change_attr = delegation->change_attr;
+ nfs_put_delegation(delegation);
+
+ res->size = i_size_read(inode);
if (nfs_have_writebacks(inode))
res->change_attr++;
res->atime = inode_get_atime(inode);
@@ -71,7 +77,6 @@ __be32 nfs4_callback_getattr(void *argp, void *resp,
FATTR4_WORD2_TIME_DELEG_MODIFY) & args->bitmap[2];
res->status = 0;
out_iput:
- rcu_read_unlock();
trace_nfs4_cb_getattr(cps->clp, &args->fh, inode, -ntohl(res->status));
nfs_iput_and_deactive(inode);
out:
diff --git a/fs/nfs/delegation.c b/fs/nfs/delegation.c
index f7d5622c625a..811e84b559ee 100644
--- a/fs/nfs/delegation.c
+++ b/fs/nfs/delegation.c
@@ -66,7 +66,7 @@ static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegati
return delegation;
}
-static void nfs_put_delegation(struct nfs_delegation *delegation)
+void nfs_put_delegation(struct nfs_delegation *delegation)
{
if (refcount_dec_and_test(&delegation->refcount))
__nfs_free_delegation(delegation);
@@ -104,10 +104,14 @@ struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
{
struct nfs_delegation *delegation;
+ rcu_read_lock();
delegation = rcu_dereference(NFS_I(inode)->delegation);
- if (nfs4_is_valid_delegation(delegation, 0))
- return delegation;
- return NULL;
+ if (!nfs4_is_valid_delegation(delegation, 0) ||
+ !refcount_inc_not_zero(&delegation->refcount))
+ delegation = NULL;
+ rcu_read_unlock();
+
+ return delegation;
}
static int nfs4_do_check_delegation(struct inode *inode, fmode_t type,
@@ -789,10 +793,11 @@ void nfs4_inode_set_return_delegation_on_close(struct inode *inode)
if (!inode)
return;
- rcu_read_lock();
+
delegation = nfs4_get_valid_delegation(inode);
if (!delegation)
- goto out;
+ return;
+
spin_lock(&delegation->lock);
if (!delegation->inode)
goto out_unlock;
@@ -806,8 +811,7 @@ void nfs4_inode_set_return_delegation_on_close(struct inode *inode)
spin_unlock(&delegation->lock);
if (ret)
nfs_clear_verifier_delegated(inode);
-out:
- rcu_read_unlock();
+ nfs_put_delegation(delegation);
nfs_end_delegation_return(inode, ret, 0);
}
@@ -823,10 +827,10 @@ void nfs4_inode_return_delegation_on_close(struct inode *inode)
struct nfs_delegation *delegation;
struct nfs_delegation *ret = NULL;
- rcu_read_lock();
delegation = nfs4_get_valid_delegation(inode);
if (!delegation)
- goto out;
+ return;
+
if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
atomic_long_read(&NFS_SERVER(inode)->nr_active_delegations) >=
nfs_delegation_watermark) {
@@ -842,8 +846,8 @@ void nfs4_inode_return_delegation_on_close(struct inode *inode)
if (ret)
nfs_clear_verifier_delegated(inode);
}
-out:
- rcu_read_unlock();
+
+ nfs_put_delegation(delegation);
nfs_end_delegation_return(inode, ret, 0);
}
@@ -858,17 +862,17 @@ void nfs4_inode_return_delegation_on_close(struct inode *inode)
int nfs4_inode_make_writeable(struct inode *inode)
{
struct nfs_delegation *delegation;
+ int error = 0;
- rcu_read_lock();
delegation = nfs4_get_valid_delegation(inode);
- if (delegation == NULL ||
- (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
- (delegation->type & FMODE_WRITE))) {
- rcu_read_unlock();
+ if (!delegation)
return 0;
- }
- rcu_read_unlock();
- return nfs4_inode_return_delegation(inode);
+
+ if (!nfs4_has_session(NFS_SERVER(inode)->nfs_client) ||
+ !(delegation->type & FMODE_WRITE))
+ error = nfs4_inode_return_delegation(inode);
+ nfs_put_delegation(delegation);
+ return error;
}
static void
@@ -1111,24 +1115,24 @@ int nfs_async_inode_return_delegation(struct inode *inode,
struct nfs_client *clp = server->nfs_client;
struct nfs_delegation *delegation;
- rcu_read_lock();
delegation = nfs4_get_valid_delegation(inode);
- if (delegation == NULL)
- goto out_enoent;
+ if (!delegation)
+ return -ENOENT;
+
if (stateid != NULL &&
- !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
- goto out_enoent;
+ !clp->cl_mvops->match_stateid(&delegation->stateid, stateid)) {
+ nfs_put_delegation(delegation);
+ return -ENOENT;
+ }
+
nfs_mark_return_delegation(server, delegation);
- rcu_read_unlock();
+ nfs_put_delegation(delegation);
/* If there are any application leases or delegations, recall them */
break_lease(inode, O_WRONLY | O_RDWR | O_NONBLOCK);
nfs_delegation_run_state_manager(clp);
return 0;
-out_enoent:
- rcu_read_unlock();
- return -ENOENT;
}
static struct inode *
diff --git a/fs/nfs/delegation.h b/fs/nfs/delegation.h
index fef1f4126e8f..d1c5da3e66ea 100644
--- a/fs/nfs/delegation.h
+++ b/fs/nfs/delegation.h
@@ -80,6 +80,7 @@ bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags, nfs4_state
bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode);
struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode);
+void nfs_put_delegation(struct nfs_delegation *delegation);
void nfs_mark_delegation_referenced(struct nfs_delegation *delegation);
int nfs4_have_delegation(struct inode *inode, fmode_t type, int flags);
int nfs4_check_delegation(struct inode *inode, fmode_t type);
diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c
index 03b1f98eb989..2b28f56d8544 100644
--- a/fs/nfs/nfs4proc.c
+++ b/fs/nfs/nfs4proc.c
@@ -1615,10 +1615,11 @@ static bool can_open_delegated(const struct inode *inode, fmode_t fmode,
struct nfs_delegation *delegation;
bool ret = false;
- rcu_read_lock();
delegation = nfs4_get_valid_delegation(inode);
- if (!delegation || (delegation->type & fmode) != fmode)
- goto out_unlock;
+ if (!delegation)
+ return false;
+ if ((delegation->type & fmode) != fmode)
+ goto out_put_delegation;
switch (claim) {
case NFS4_OPEN_CLAIM_PREVIOUS:
@@ -1637,8 +1638,8 @@ static bool can_open_delegated(const struct inode *inode, fmode_t fmode,
break;
}
-out_unlock:
- rcu_read_unlock();
+out_put_delegation:
+ nfs_put_delegation(delegation);
return ret;
}
@@ -1913,10 +1914,11 @@ int update_open_stateid(struct nfs4_state *state,
fmode &= (FMODE_READ|FMODE_WRITE);
- rcu_read_lock();
spin_lock(&state->owner->so_lock);
if (open_stateid != NULL) {
+ rcu_read_lock();
nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
+ rcu_read_unlock();
ret = 1;
}
@@ -1940,11 +1942,11 @@ int update_open_stateid(struct nfs4_state *state,
ret = 1;
no_delegation_unlock:
spin_unlock(&deleg_cur->lock);
+ nfs_put_delegation(deleg_cur);
no_delegation:
if (ret)
update_open_stateflags(state, fmode);
spin_unlock(&state->owner->so_lock);
- rcu_read_unlock();
if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
nfs4_schedule_state_manager(clp);
@@ -1978,14 +1980,12 @@ static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmo
struct nfs_delegation *delegation;
fmode &= FMODE_READ|FMODE_WRITE;
- rcu_read_lock();
delegation = nfs4_get_valid_delegation(inode);
- if (delegation == NULL || (delegation->type & fmode) == fmode) {
- rcu_read_unlock();
+ if (!delegation)
return;
- }
- rcu_read_unlock();
- nfs4_inode_return_delegation(inode);
+ if ((delegation->type & fmode) != fmode)
+ nfs4_inode_return_delegation(inode);
+ nfs_put_delegation(delegation);
}
static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
--
2.47.3
next prev parent reply other threads:[~2025-12-18 5:57 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-18 5:56 add a LRU for delegations Christoph Hellwig
2025-12-18 5:56 ` [PATCH 01/24] NFS: remove __nfs_client_for_each_server Christoph Hellwig
2025-12-18 5:56 ` [PATCH 02/24] NFS: remove nfs_client_mark_return_unused_delegation_types Christoph Hellwig
2025-12-18 5:56 ` [PATCH 03/24] NFS: remove nfs_client_mark_return_all_delegations Christoph Hellwig
2025-12-18 5:56 ` [PATCH 04/24] NFS: remove the NULL inode check in nfs4_inode_return_delegation_on_close Christoph Hellwig
2025-12-18 5:56 ` [PATCH 05/24] NFS: remove nfs_inode_detach_delegation Christoph Hellwig
2025-12-18 5:56 ` [PATCH 06/24] NFS: remove nfs_start_delegation_return Christoph Hellwig
2025-12-18 5:56 ` [PATCH 07/24] NFS: assert rcu_read_lock is held in nfs_start_delegation_return_locked Christoph Hellwig
2025-12-18 5:56 ` [PATCH 08/24] NFS: drop the _locked postfix from nfs_start_delegation_return Christoph Hellwig
2025-12-18 5:56 ` [PATCH 09/24] NFS: remove NFS_DELEGATION_INODE_FREEING Christoph Hellwig
2025-12-18 5:56 ` [PATCH 10/24] NFS: open code nfs_delegation_need_return Christoph Hellwig
2025-12-18 5:56 ` [PATCH 11/24] NFS: remove nfs_free_delegation Christoph Hellwig
2025-12-18 5:56 ` [PATCH 12/24] NFS: rewrite nfs_delegations_present in terms of nr_active_delegations Christoph Hellwig
2025-12-18 5:56 ` [PATCH 13/24] NFS: move delegation lookup into can_open_delegated Christoph Hellwig
2025-12-18 5:56 ` [PATCH 14/24] NFS: return bool from nfs_detach_delegation{,_locked} Christoph Hellwig
2025-12-18 5:56 ` [PATCH 15/24] NFS: move the deleg_cur check out of nfs_detach_delegation_locked Christoph Hellwig
2025-12-18 5:56 ` [PATCH 16/24] NFS: simplify the detached delegation check in update_open_stateid Christoph Hellwig
2025-12-18 5:56 ` Christoph Hellwig [this message]
2025-12-18 5:56 ` [PATCH 18/24] NFS: don't consume a delegation reference in nfs_end_delegation_return Christoph Hellwig
2025-12-18 5:56 ` [PATCH 19/24] NFS: use refcount_inc_not_zero nfs_start_delegation_return Christoph Hellwig
2025-12-18 5:56 ` [PATCH 20/24] NFS: use a local RCU critical section in nfs_start_delegation_return Christoph Hellwig
2025-12-18 5:56 ` [PATCH 21/24] NFS: reformat nfs_mark_delegation_revoked Christoph Hellwig
2025-12-18 5:56 ` [PATCH 22/24] NFS: add a separate delegation return list Christoph Hellwig
2025-12-18 5:56 ` [PATCH 23/24] NFS: return delegations from the end of a LRU when over the watermark Christoph Hellwig
2025-12-18 22:02 ` Anna Schumaker
2025-12-19 5:21 ` Christoph Hellwig
2025-12-19 11:14 ` Christoph Hellwig
2025-12-19 14:29 ` Anna Schumaker
2025-12-18 5:56 ` [PATCH 24/24] NFS: make nfs_mark_return_unreferenced_delegations less aggressive Christoph Hellwig
-- strict thread matches above, loose matches on Subject: below --
2026-01-07 7:26 add a LRU for delegations Christoph Hellwig
2026-01-07 7:27 ` [PATCH 17/24] NFS: take a delegation reference in nfs4_get_valid_delegation Christoph Hellwig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251218055633.1532159-18-hch@lst.de \
--to=hch@lst.de \
--cc=anna@kernel.org \
--cc=linux-nfs@vger.kernel.org \
--cc=trondmy@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox