Durable handle deadlines are stored as 32-bit millisecond counts and compared with ordinary integer comparisons. If a timeout crosses the counter wrap, the handle appears expired immediately. Reconnect then fails with STATUS_OBJECT_NAME_NOT_FOUND, and the scavenger can close the handle before its timeout expires. With HZ of 100, 250 or 1000, the millisecond count first wraps five minutes after boot because of the initial jiffies value, and then every 49.7 days. Store the deadline in jiffies and use wrap-safe time comparisons. Use durable_timeout to identify timed handles, since zero is a valid deadline. Fixes: 520da3c488c5 ("ksmbd: avoid reclaiming expired durable opens by the client") Cc: stable@vger.kernel.org Signed-off-by: Ian Bridges --- Tested on ksmbd-for-next 030c481c019b, on x86_64 with KASAN, in freshly booted guests. On the base, a durable handle whose timeout ran past the first wrap after boot could not be reconnected. With the patch, the same reconnect succeeded, and a reconnect after the wrap succeeded too. Before the wrap, the durable handle tests now give the results that the base gives after the wrap. Otherwise smbtorture gives the same results as on the base, and KASAN reported nothing. This also affects smbtorture. Its durable handle tests ask for the maximum timeout of five minutes, so on the base they fail when they run within five minutes of boot. This patch was developed with LLM assistance. fs/smb/server/vfs_cache.c | 17 ++++++++--------- fs/smb/server/vfs_cache.h | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/fs/smb/server/vfs_cache.c b/fs/smb/server/vfs_cache.c index 7db0a52dd4ae..b157aabc7828 100644 --- a/fs/smb/server/vfs_cache.c +++ b/fs/smb/server/vfs_cache.c @@ -1234,9 +1234,8 @@ struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id) fp = __ksmbd_lookup_fd(&global_ft, id); if (fp && (fp->durable_reconnect_disabled || fp->conn || - (fp->durable_scavenger_timeout && - (fp->durable_scavenger_timeout < - jiffies_to_msecs(jiffies))))) { + (fp->durable_timeout && + time_after(jiffies, fp->durable_scavenger_timeout)))) { ksmbd_put_durable_fd(fp); fp = NULL; } @@ -1408,7 +1407,7 @@ int ksmbd_invalidate_durable_fd(unsigned long long id) } fp->durable_timeout = 1; - fp->durable_scavenger_timeout = jiffies_to_msecs(jiffies); + fp->durable_scavenger_timeout = jiffies; ksmbd_put_durable_fd(fp); if (waitqueue_active(&dh_wq)) wake_up(&dh_wq); @@ -1916,8 +1915,8 @@ static int ksmbd_durable_scavenger(void *dummy) continue; found_fp_timeout = true; - if (fp->durable_scavenger_timeout <= - jiffies_to_msecs(jiffies)) { + if (time_after_eq(jiffies, + fp->durable_scavenger_timeout)) { __ksmbd_remove_durable_fd(fp); /* * Take a transient reference so fp @@ -1934,8 +1933,8 @@ static int ksmbd_durable_scavenger(void *dummy) } durable_timeout = - fp->durable_scavenger_timeout - - jiffies_to_msecs(jiffies); + jiffies_to_msecs(fp->durable_scavenger_timeout - + jiffies); if (min_timeout > durable_timeout) min_timeout = durable_timeout; @@ -2111,7 +2110,7 @@ static bool session_fd_check(struct ksmbd_tree_connect *tcon, if (fp->durable_timeout) fp->durable_scavenger_timeout = - jiffies_to_msecs(jiffies) + fp->durable_timeout; + jiffies + msecs_to_jiffies(fp->durable_timeout); atomic_dec(&conn->stats.open_files_count); write_unlock(&global_ft.lock); up_write(&ci->m_lock); diff --git a/fs/smb/server/vfs_cache.h b/fs/smb/server/vfs_cache.h index bc1955b87855..26e10093b2ae 100644 --- a/fs/smb/server/vfs_cache.h +++ b/fs/smb/server/vfs_cache.h @@ -152,7 +152,7 @@ struct ksmbd_file { bool stream_del_pending; unsigned int durable_timeout; - unsigned int durable_scavenger_timeout; + unsigned long durable_scavenger_timeout; /* CREATE action returned when this durable handle was established. */ __le32 create_action; base-commit: 030c481c019bec1fcb3fd66287ad80dfbdc1b62f -- 2.47.3