From: Ian Bridges <icb@fastmail.org>
To: Namjae Jeon <linkinjeon@kernel.org>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
Tom Talpey <tom@talpey.com>,
ChenXiaoSong <chenxiaosong@chenxiaosong.com>,
linux-cifs@vger.kernel.org, Ian Bridges <icb@fastmail.org>,
stable@vger.kernel.org
Subject: [PATCH v3 2/2] ksmbd: keep the durable scavenger until disconnected handles are removed
Date: Fri, 4 Dec 2026 00:04:42 -0600 [thread overview]
Message-ID: <20261204060442.30849-3-icb@fastmail.org> (raw)
In-Reply-To: <20261204060442.30849-1-icb@fastmail.org>
The durable handle scavenger removes a disconnected durable handle when
its timeout ends. There are three ways the scavenger can stop or sleep
while such a handle is still waiting.
A lookup, such as a reconnect, holds a reference on the handle while
the lookup runs. The scavenger skips a handle with more than one
reference. If the scavenger skips every handle in a pass, the scavenger
stops. When the reconnect then fails, nothing removes the expired handle
until another disconnect starts the scavenger. Until then the handle
keeps its share access, byte-range locks and lease.
A disconnect that finds the scavenger running does not wake the
scavenger. If the scavenger is waiting for the deadline of another
handle, the new handle stays past its own timeout until that wait ends.
The scavenger clears durable_scavenger_running without
durable_scavenger_lock when the scavenger stops. A disconnect in that
window finds the scavenger still running and does not start a new one,
so the handle of that disconnect is not removed at all.
Count a held handle as waiting, and check an expired held handle again
a second later. When a disconnect finds the scavenger running, wake the
scavenger. Before the scavenger stops, check under
durable_scavenger_lock whether such a disconnect came in, and if one
did, run another pass.
Fixes: d484d621d40f ("ksmbd: add durable scavenger timer")
Cc: stable@vger.kernel.org
Signed-off-by: Ian Bridges <icb@fastmail.org>
---
fs/smb/server/vfs_cache.c | 89 ++++++++++++++++++++++++++++++++++-----
1 file changed, 78 insertions(+), 11 deletions(-)
diff --git a/fs/smb/server/vfs_cache.c b/fs/smb/server/vfs_cache.c
index 6bb35d95d495..373a619530f9 100644
--- a/fs/smb/server/vfs_cache.c
+++ b/fs/smb/server/vfs_cache.c
@@ -265,6 +265,13 @@ static int create_proc_files(void) { return 0; }
#endif
static bool durable_scavenger_running;
+/*
+ * Set when a disconnect finds the scavenger thread already started. The thread
+ * may be asleep until the deadline of another handle, so the disconnect sets
+ * this flag and wakes the thread. The thread then runs a pass that sees the
+ * newly disconnected handle, and does not stop before that pass.
+ */
+static bool durable_scavenger_launched;
static DEFINE_MUTEX(durable_scavenger_lock);
static wait_queue_head_t dh_wq;
@@ -1860,6 +1867,35 @@ static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon,
return fp->tcon != tcon;
}
+/* How soon the scavenger looks again at an expired handle that is held */
+#define DURABLE_SCAVENGER_RETRY_MS 1000
+
+/*
+ * Called when the scavenger is about to stop. Return true when a disconnect
+ * found the scavenger running since its last pass. That disconnect did not
+ * start a new scavenger, so this scavenger runs another pass for the handle
+ * that the disconnect left. Otherwise mark the scavenger stopped and return
+ * false.
+ *
+ * A disconnect takes durable_scavenger_lock too. A disconnect before this
+ * check has set durable_scavenger_launched, which this check sees. A
+ * disconnect after this check finds the scavenger stopped and starts a new
+ * one.
+ */
+static bool ksmbd_durable_scavenger_rerun(void)
+{
+ bool rerun;
+
+ mutex_lock(&durable_scavenger_lock);
+ rerun = durable_scavenger_running && durable_scavenger_launched &&
+ !kthread_should_stop();
+ durable_scavenger_launched = false;
+ if (!rerun)
+ durable_scavenger_running = false;
+ mutex_unlock(&durable_scavenger_lock);
+ return rerun;
+}
+
static bool ksmbd_durable_scavenger_alive(void)
{
if (!durable_scavenger_running)
@@ -1910,18 +1946,37 @@ static int ksmbd_durable_scavenger(void *dummy)
__module_get(THIS_MODULE);
set_freezable();
+again:
while (ksmbd_durable_scavenger_alive()) {
if (try_to_freeze())
continue;
remaining_jiffies = wait_event_interruptible_timeout(dh_wq,
- ksmbd_durable_scavenger_alive() == false,
+ ksmbd_durable_scavenger_alive() == false ||
+ READ_ONCE(durable_scavenger_launched),
__msecs_to_jiffies(min_timeout));
if ((long)remaining_jiffies > 0)
min_timeout = jiffies_to_msecs(remaining_jiffies);
else
min_timeout = DURABLE_HANDLE_MAX_TIMEOUT;
+ /*
+ * Clear durable_scavenger_launched before the pass. The pass
+ * scans every handle in the table, so the pass covers the
+ * disconnects that set the flag so far. A disconnect on another
+ * thread can set the flag again while the pass runs, in
+ * ksmbd_launch_ksmbd_durable_scavenger(), and the handle of
+ * that disconnect may come too late for the pass. If the
+ * scavenger goes on, the wait at the top of this loop sees the
+ * flag in its condition and ends at once. If the scavenger is
+ * about to stop, ksmbd_durable_scavenger_rerun() sees the flag,
+ * and the scavenger runs another pass instead of stopping.
+ * Without the clear, that wait would end at once every time.
+ */
+ mutex_lock(&durable_scavenger_lock);
+ durable_scavenger_launched = false;
+ mutex_unlock(&durable_scavenger_lock);
+
do {
expired_fp = NULL;
found_fp_timeout = false;
@@ -1931,17 +1986,20 @@ static int ksmbd_durable_scavenger(void *dummy)
unsigned long durable_timeout;
u64 now;
- if (!fp->durable_timeout)
- continue;
-
- if (atomic_read(&fp->refcount) > 1 ||
- fp->conn)
+ if (!fp->durable_timeout || fp->conn)
continue;
+ /*
+ * A disconnected handle that a lookup holds
+ * still waits for its timeout. Count it, so
+ * that the scavenger does not stop and leave
+ * it behind.
+ */
found_fp_timeout = true;
now = get_jiffies_64();
if (time_after_eq64(now,
- fp->durable_scavenger_timeout)) {
+ fp->durable_scavenger_timeout) &&
+ atomic_read(&fp->refcount) == 1) {
__ksmbd_remove_durable_fd(fp);
/*
* Take a transient reference so fp
@@ -1957,9 +2015,13 @@ static int ksmbd_durable_scavenger(void *dummy)
break;
}
- durable_timeout =
- jiffies64_to_msecs(fp->durable_scavenger_timeout -
- now);
+ if (time_after_eq64(now,
+ fp->durable_scavenger_timeout))
+ durable_timeout = DURABLE_SCAVENGER_RETRY_MS;
+ else
+ durable_timeout =
+ jiffies64_to_msecs(fp->durable_scavenger_timeout -
+ now);
if (min_timeout > durable_timeout)
min_timeout = durable_timeout;
@@ -1974,7 +2036,10 @@ static int ksmbd_durable_scavenger(void *dummy)
break;
}
- durable_scavenger_running = false;
+ if (ksmbd_durable_scavenger_rerun()) {
+ min_timeout = 1;
+ goto again;
+ }
module_put(THIS_MODULE);
@@ -1988,6 +2053,8 @@ void ksmbd_launch_ksmbd_durable_scavenger(void)
mutex_lock(&durable_scavenger_lock);
if (durable_scavenger_running == true) {
+ durable_scavenger_launched = true;
+ wake_up(&dh_wq);
mutex_unlock(&durable_scavenger_lock);
return;
}
--
2.47.3
prev parent reply other threads:[~2026-12-04 6:06 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261204060442.30849-1-icb@fastmail.org>
2026-12-04 6:04 ` [PATCH v3 1/2] ksmbd: use wrap-safe durable handle deadlines Ian Bridges
2026-12-04 6:04 ` Ian Bridges [this message]
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=20261204060442.30849-3-icb@fastmail.org \
--to=icb@fastmail.org \
--cc=chenxiaosong@chenxiaosong.com \
--cc=linkinjeon@kernel.org \
--cc=linux-cifs@vger.kernel.org \
--cc=senozhatsky@chromium.org \
--cc=stable@vger.kernel.org \
--cc=tom@talpey.com \
/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