189 lines
6.2 KiB
Diff
189 lines
6.2 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Tyler Smith <74526874+tylertms@users.noreply.github.com>
|
|
Date: Tue, 3 Nov 2026 04:12:58 +0000
|
|
Subject: [PATCH] refactor(fs): separate the file reference slowpath
|
|
|
|
---
|
|
fs/Makefile | 2 +-
|
|
fs/file.c | 67 -------------------------------------------
|
|
fs/file_ref.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
3 files changed, 80 insertions(+), 68 deletions(-)
|
|
create mode 100644 fs/file_ref.c
|
|
|
|
diff --git a/fs/Makefile b/fs/Makefile
|
|
index 416f56e7e154592c6e3eec54b17722cb799983ee..355e6b5cb30c2dab23aed52e41a9c8bea3d2fcd9 100644
|
|
--- a/fs/Makefile
|
|
+++ b/fs/Makefile
|
|
@@ -10,7 +10,7 @@
|
|
obj-y := open.o read_write.o file_table.o super.o \
|
|
char_dev.o stat.o exec.o task_comm.o pipe.o namei.o fcntl.o \
|
|
ioctl.o readdir.o select.o dcache.o inode.o \
|
|
- attr.o bad_inode.o file.o init_fdtable.o filesystems.o namespace.o \
|
|
+ attr.o bad_inode.o file.o file_ref.o init_fdtable.o filesystems.o namespace.o \
|
|
seq_file.o seq_lifetime.o seq_output.o xattr.o libfs.o fs-writeback.o \
|
|
pnode.o splice.o sync.o utimes.o d_path.o \
|
|
stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \
|
|
diff --git a/fs/file.c b/fs/file.c
|
|
index 6c8acc2c57fe171480f6221b15faca9ea61fcb8c..44c82867be854ec9df62478346a56ada842f98f1 100644
|
|
--- a/fs/file.c
|
|
+++ b/fs/file.c
|
|
@@ -26,73 +26,6 @@
|
|
|
|
#include "internal.h"
|
|
|
|
-static noinline bool __file_ref_put_badval(file_ref_t *ref, unsigned long cnt)
|
|
-{
|
|
- /*
|
|
- * If the reference count was already in the dead zone, then this
|
|
- * put() operation is imbalanced. Warn, put the reference count back to
|
|
- * DEAD and tell the caller to not deconstruct the object.
|
|
- */
|
|
- if (WARN_ONCE(cnt >= FILE_REF_RELEASED, "imbalanced put on file reference count")) {
|
|
- atomic_long_set(&ref->refcnt, FILE_REF_DEAD);
|
|
- return false;
|
|
- }
|
|
-
|
|
- /*
|
|
- * This is a put() operation on a saturated refcount. Restore the
|
|
- * mean saturation value and tell the caller to not deconstruct the
|
|
- * object.
|
|
- */
|
|
- if (cnt > FILE_REF_MAXREF)
|
|
- atomic_long_set(&ref->refcnt, FILE_REF_SATURATED);
|
|
- return false;
|
|
-}
|
|
-
|
|
-/**
|
|
- * __file_ref_put - Slowpath of file_ref_put()
|
|
- * @ref: Pointer to the reference count
|
|
- * @cnt: Current reference count
|
|
- *
|
|
- * Invoked when the reference count is outside of the valid zone.
|
|
- *
|
|
- * Return:
|
|
- * True if this was the last reference with no future references
|
|
- * possible. This signals the caller that it can safely schedule the
|
|
- * object, which is protected by the reference counter, for
|
|
- * deconstruction.
|
|
- *
|
|
- * False if there are still active references or the put() raced
|
|
- * with a concurrent get()/put() pair. Caller is not allowed to
|
|
- * deconstruct the protected object.
|
|
- */
|
|
-bool __file_ref_put(file_ref_t *ref, unsigned long cnt)
|
|
-{
|
|
- /* Did this drop the last reference? */
|
|
- if (likely(cnt == FILE_REF_NOREF)) {
|
|
- /*
|
|
- * Carefully try to set the reference count to FILE_REF_DEAD.
|
|
- *
|
|
- * This can fail if a concurrent get() operation has
|
|
- * elevated it again or the corresponding put() even marked
|
|
- * it dead already. Both are valid situations and do not
|
|
- * require a retry. If this fails the caller is not
|
|
- * allowed to deconstruct the object.
|
|
- */
|
|
- if (!atomic_long_try_cmpxchg_release(&ref->refcnt, &cnt, FILE_REF_DEAD))
|
|
- return false;
|
|
-
|
|
- /*
|
|
- * The caller can safely schedule the object for
|
|
- * deconstruction. Provide acquire ordering.
|
|
- */
|
|
- smp_acquire__after_ctrl_dep();
|
|
- return true;
|
|
- }
|
|
-
|
|
- return __file_ref_put_badval(ref, cnt);
|
|
-}
|
|
-EXPORT_SYMBOL_GPL(__file_ref_put);
|
|
-
|
|
unsigned int sysctl_nr_open __read_mostly = 1024*1024;
|
|
unsigned int sysctl_nr_open_min = BITS_PER_LONG;
|
|
/* our min() is unusable in constant expressions ;-/ */
|
|
diff --git a/fs/file_ref.c b/fs/file_ref.c
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..7ea103c1525c049ed04f55173e2679c8286a36fb
|
|
--- /dev/null
|
|
+++ b/fs/file_ref.c
|
|
@@ -0,0 +1,79 @@
|
|
+// SPDX-License-Identifier: GPL-2.0
|
|
+/*
|
|
+ * linux/fs/file.c
|
|
+ *
|
|
+ * Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
|
|
+ *
|
|
+ * Manage the dynamic fd arrays in the process files_struct.
|
|
+ */
|
|
+
|
|
+#include <linux/export.h>
|
|
+#include <linux/file_ref.h>
|
|
+#include <linux/bug.h>
|
|
+
|
|
+static noinline bool __file_ref_put_badval(file_ref_t *ref, unsigned long cnt)
|
|
+{
|
|
+ /*
|
|
+ * If the reference count was already in the dead zone, then this
|
|
+ * put() operation is imbalanced. Warn, put the reference count back to
|
|
+ * DEAD and tell the caller to not deconstruct the object.
|
|
+ */
|
|
+ if (WARN_ONCE(cnt >= FILE_REF_RELEASED, "imbalanced put on file reference count")) {
|
|
+ atomic_long_set(&ref->refcnt, FILE_REF_DEAD);
|
|
+ return false;
|
|
+ }
|
|
+
|
|
+ /*
|
|
+ * This is a put() operation on a saturated refcount. Restore the
|
|
+ * mean saturation value and tell the caller to not deconstruct the
|
|
+ * object.
|
|
+ */
|
|
+ if (cnt > FILE_REF_MAXREF)
|
|
+ atomic_long_set(&ref->refcnt, FILE_REF_SATURATED);
|
|
+ return false;
|
|
+}
|
|
+
|
|
+/**
|
|
+ * __file_ref_put - Slowpath of file_ref_put()
|
|
+ * @ref: Pointer to the reference count
|
|
+ * @cnt: Current reference count
|
|
+ *
|
|
+ * Invoked when the reference count is outside of the valid zone.
|
|
+ *
|
|
+ * Return:
|
|
+ * True if this was the last reference with no future references
|
|
+ * possible. This signals the caller that it can safely schedule the
|
|
+ * object, which is protected by the reference counter, for
|
|
+ * deconstruction.
|
|
+ *
|
|
+ * False if there are still active references or the put() raced
|
|
+ * with a concurrent get()/put() pair. Caller is not allowed to
|
|
+ * deconstruct the protected object.
|
|
+ */
|
|
+bool __file_ref_put(file_ref_t *ref, unsigned long cnt)
|
|
+{
|
|
+ /* Did this drop the last reference? */
|
|
+ if (likely(cnt == FILE_REF_NOREF)) {
|
|
+ /*
|
|
+ * Carefully try to set the reference count to FILE_REF_DEAD.
|
|
+ *
|
|
+ * This can fail if a concurrent get() operation has
|
|
+ * elevated it again or the corresponding put() even marked
|
|
+ * it dead already. Both are valid situations and do not
|
|
+ * require a retry. If this fails the caller is not
|
|
+ * allowed to deconstruct the object.
|
|
+ */
|
|
+ if (!atomic_long_try_cmpxchg_release(&ref->refcnt, &cnt, FILE_REF_DEAD))
|
|
+ return false;
|
|
+
|
|
+ /*
|
|
+ * The caller can safely schedule the object for
|
|
+ * deconstruction. Provide acquire ordering.
|
|
+ */
|
|
+ smp_acquire__after_ctrl_dep();
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ return __file_ref_put_badval(ref, cnt);
|
|
+}
|
|
+EXPORT_SYMBOL_GPL(__file_ref_put);
|