vfs: dodge smp_mb in break_lease and break_deleg in the common case
authorMateusz Guzik <mjguzik@gmail.com>
Tue, 6 Aug 2024 17:28:46 +0000 (19:28 +0200)
committerChristian Brauner <brauner@kernel.org>
Fri, 30 Aug 2024 06:22:33 +0000 (08:22 +0200)
These inlines show up in the fast path (e.g., in do_dentry_open()) and
induce said full barrier regarding i_flctx access when in most cases the
pointer is NULL.

The pointer can be safely checked before issuing the barrier, dodging it
in most cases as a result.

It is plausible the consume fence would be sufficient, but I don't want
to go audit all callers regarding what they before calling here.

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
Link: https://lore.kernel.org/r/20240806172846.886570-1-mjguzik@gmail.com
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
include/linux/filelock.h

index daee999d05f390a9fa28e83ff2643c2b1e5d1e96..bb44224c66763c7e0fe9f6a92f3bcd9a74f2b9a7 100644 (file)
@@ -420,28 +420,38 @@ static inline int locks_lock_file_wait(struct file *filp, struct file_lock *fl)
 #ifdef CONFIG_FILE_LOCKING
 static inline int break_lease(struct inode *inode, unsigned int mode)
 {
+       struct file_lock_context *flctx;
+
        /*
         * Since this check is lockless, we must ensure that any refcounts
         * taken are done before checking i_flctx->flc_lease. Otherwise, we
         * could end up racing with tasks trying to set a new lease on this
         * file.
         */
+       flctx = READ_ONCE(inode->i_flctx);
+       if (!flctx)
+               return 0;
        smp_mb();
-       if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
+       if (!list_empty_careful(&flctx->flc_lease))
                return __break_lease(inode, mode, FL_LEASE);
        return 0;
 }
 
 static inline int break_deleg(struct inode *inode, unsigned int mode)
 {
+       struct file_lock_context *flctx;
+
        /*
         * Since this check is lockless, we must ensure that any refcounts
         * taken are done before checking i_flctx->flc_lease. Otherwise, we
         * could end up racing with tasks trying to set a new lease on this
         * file.
         */
+       flctx = READ_ONCE(inode->i_flctx);
+       if (!flctx)
+               return 0;
        smp_mb();
-       if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
+       if (!list_empty_careful(&flctx->flc_lease))
                return __break_lease(inode, mode, FL_DELEG);
        return 0;
 }