From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: Alexander Viro <viro@zeniv.linux.org.uk>, Jan Kara <jack@suse.cz>,
Peter Zijlstra <peterz@infradead.org>,
"Paul E. McKenney" <paulmck@kernel.org>,
"Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH 3/5] file_ref: document why file_ref_put() stays fully ordered
Date: Thu, 05 Nov 2026 14:18:07 +0100 [thread overview]
Message-ID: <20261105-work-file_ref-ordering-v1-3-8713f4876b7b@kernel.org> (raw)
In-Reply-To: <20261105-work-file_ref-ordering-v1-0-8713f4876b7b@kernel.org>
The comment above file_ref_put() describes release ordering on the way
in and acquire ordering on success. file_ref_put() uses the fully
ordered atomic_long_dec_return() instead. Document why we need it.
Files are SLAB_TYPESAFE_BY_RCU. When the last reference goes
synchronously, in __fput_sync() and on the fallback path of
fput_close_sync(), the file is freed right after the decrement and
another CPU can get it from the slab and reinitialize the counter at
once. The lookup that increments that recycled counter relies on seeing
the stores the closer made before its put. A release orders those stores
before the decrement, not before the slab handoff that follows it. The
full barrier does. The deferred fput() path has the cmpxchg() in
task_work_add() between the decrement and the free, the synchronous
paths have nothing else.
No functional changes.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
include/linux/file_ref.h | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/include/linux/file_ref.h b/include/linux/file_ref.h
index 4cffb24257e2..e5fc45714fe2 100644
--- a/include/linux/file_ref.h
+++ b/include/linux/file_ref.h
@@ -133,6 +133,17 @@ static __always_inline void file_ref_inc(file_ref_t *ref)
* are done before, and provides an acquire ordering on success such
* that free() must come after.
*
+ * The decrement is fully ordered though and it has to stay that way.
+ * When the last reference goes synchronously, in __fput_sync() and on
+ * the fallback path of fput_close_sync(), the full barrier is what
+ * orders the caller's earlier stores, in close(2) the cleared slot in
+ * the descriptor table, before the slab handoff that lets another CPU
+ * reallocate the file. A lookup that increments the recycled counter
+ * then sees the cleared slot and fails its recheck. The deferred
+ * fput() has the cmpxchg() of task_work_add() for that, the
+ * synchronous paths have nothing else. So this is not a candidate for
+ * atomic_long_dec_return_release().
+ *
* Return: True if this was the last reference with no future references
* possible. This signals the caller that it can safely release
* the object which is protected by the reference counter.
--
2.53.0
next prev parent reply other threads:[~2026-11-05 13:18 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 13:18 [PATCH 0/5] file: fix and document ordering for lockless fd lookup wrt to file_ref Christian Brauner
2026-11-05 13:18 ` [PATCH 1/5] file: depend on the reloaded pointer in __fget_files_rcu() Christian Brauner
2026-11-05 13:18 ` [PATCH 2/5] file_ref: use acquire ordering in file_ref_get() Christian Brauner
2026-11-05 13:18 ` Christian Brauner [this message]
2026-11-05 13:18 ` [PATCH 4/5] Documentation/litmus-tests: add the file reference count tests Christian Brauner
2026-11-05 13:18 ` [PATCH 5/5] Documentation: update the lock-free file lookup in files.rst Christian Brauner
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=20261105-work-file_ref-ordering-v1-3-8713f4876b7b@kernel.org \
--to=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=viro@zeniv.linux.org.uk \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.