[PATCH] fs/locks: avoid allocation in fcntl_getlk() and fcntl_getlk64()

From: Kris Pan

Date: Thu Aug 20 2026 - 19:43:34 EST


fcntl_getlk() and fcntl_getlk64() allocate a struct file_lock from
filelock_cache on every call, adding kmem_cache alloc/free overhead to
the F_GETLK hot path.

This was introduced by commit 52306e882f77 ("fs/locks: Use allocation
rather than the stack in fcntl_getlk()") to save stack space, but struct
file_lock has since shrunk to 192 bytes (after the file_lock_core split),
so a stack allocation is fine.

Use locks_init_lock() to initialize a stack-allocated struct file_lock.
The fcntl() -> do_fcntl() -> fcntl_getlk() call chain has a combined
stack frame of under 500 bytes, well within even an 8KB stack.

On a Meteor Lake system, fcntl(F_GETLK) throughput improves from 11.0M
to 12.5M calls/sec (+13.6%), matching the ~11% regression reported by
the kernel test robot for stress-ng lockofd.

Fixes: 52306e882f77 ("fs/locks: Use allocation rather than the stack in fcntl_getlk()")
Reported-by: kernel test robot <lkp@xxxxxxxxx>
Closes: https://lore.kernel.org/all/20171108072233.GA3816@xxxxxxxxx/
Signed-off-by: Kris Pan <kris.pan@xxxxxxxxx>
---
fs/locks.c | 44 ++++++++++++++++++++------------------------
1 file changed, 20 insertions(+), 24 deletions(-)

diff --git a/fs/locks.c b/fs/locks.c
index 6e4ff7fcec053..12f8d569b7749 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -2436,18 +2436,16 @@ static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
*/
int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
{
- struct file_lock *fl;
+ struct file_lock fl;
int error;

- fl = locks_alloc_lock();
- if (fl == NULL)
- return -ENOMEM;
+ locks_init_lock(&fl);
error = -EINVAL;
if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK
&& flock->l_type != F_WRLCK)
goto out;

- error = flock_to_posix_lock(filp, fl, flock);
+ error = flock_to_posix_lock(filp, &fl, flock);
if (error)
goto out;

@@ -2456,22 +2454,22 @@ int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
if (flock->l_pid != 0)
goto out;

- fl->c.flc_flags |= FL_OFDLCK;
- fl->c.flc_owner = filp;
+ fl.c.flc_flags |= FL_OFDLCK;
+ fl.c.flc_owner = filp;
}

- error = vfs_test_lock(filp, fl);
+ error = vfs_test_lock(filp, &fl);
if (error)
goto out;

- flock->l_type = fl->c.flc_type;
- if (fl->c.flc_type != F_UNLCK) {
- error = posix_lock_to_flock(flock, fl);
+ flock->l_type = fl.c.flc_type;
+ if (fl.c.flc_type != F_UNLCK) {
+ error = posix_lock_to_flock(flock, &fl);
if (error)
goto out;
}
out:
- locks_free_lock(fl);
+ locks_release_private(&fl);
return error;
}

@@ -2644,19 +2642,17 @@ int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
*/
int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
{
- struct file_lock *fl;
+ struct file_lock fl;
int error;

- fl = locks_alloc_lock();
- if (fl == NULL)
- return -ENOMEM;
+ locks_init_lock(&fl);

error = -EINVAL;
if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK
&& flock->l_type != F_WRLCK)
goto out;

- error = flock64_to_posix_lock(filp, fl, flock);
+ error = flock64_to_posix_lock(filp, &fl, flock);
if (error)
goto out;

@@ -2665,20 +2661,20 @@ int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
if (flock->l_pid != 0)
goto out;

- fl->c.flc_flags |= FL_OFDLCK;
- fl->c.flc_owner = filp;
+ fl.c.flc_flags |= FL_OFDLCK;
+ fl.c.flc_owner = filp;
}

- error = vfs_test_lock(filp, fl);
+ error = vfs_test_lock(filp, &fl);
if (error)
goto out;

- flock->l_type = fl->c.flc_type;
- if (fl->c.flc_type != F_UNLCK)
- posix_lock_to_flock64(flock, fl);
+ flock->l_type = fl.c.flc_type;
+ if (fl.c.flc_type != F_UNLCK)
+ posix_lock_to_flock64(flock, &fl);

out:
- locks_free_lock(fl);
+ locks_release_private(&fl);
return error;
}

--
2.43.0