Linux Trace Kernel
 help / color / mirror / Atom feed
* [RFC PATCH] filelock: trace the conflicting lock when a request is refused or deferred
@ 2026-12-01 16:36 Tianyi Chen
  2026-12-01 17:26 ` Jeff Layton
  0 siblings, 1 reply; 2+ messages in thread
From: Tianyi Chen @ 2026-12-01 16:36 UTC (permalink / raw)
  To: Jeff Layton, Chuck Lever
  Cc: Alexander Aring, Steven Rostedt, Masami Hiramatsu,
	Mathieu Desnoyers, Christian Brauner, Jan Kara, linux-fsdevel,
	linux-trace-kernel, linux-kernel, hi

A SQLite writer retrying F_SETLK never joins the kernel wait queue.
When a frozen process held the conflicting lock in my test, the writer
got EAGAIN but /proc/locks showed no waiter. The new event identified
the blocker on byte 120. A later F_GETLK can only describe a conflict
at query time, not the one that caused the refusal.

Trace the request and conflicting lock together while fs/locks.c still
holds the file-lock context lock. This also records blocking requests
that are deferred and POSIX deadlock refusals; it does not change lock
or freezer policy. For OFD and flock locks, blocker_pid is the recorded
PID, not the identity of every task that can release the lock.

This patch was developed with coding-assistant help.

Signed-off-by: Tianyi Chen <hi@tychen.cc>
---

Would you prefer to limit this event to non-blocking refusals?

Built and tested on mainline 19cc08c1a6a6 in one KVM boot (no dmesg
warnings): no event on uncontended acquisition and release; the expected
conflicts appeared for POSIX, OFD and flock refusals, deferred POSIX
and flock requests, a POSIX deadlock, and the frozen SQLite writer case.

 fs/locks.c                      | 16 +++++++---
 include/trace/events/filelock.h | 55 +++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 5 deletions(-)

diff --git a/fs/locks.c b/fs/locks.c
index 6e4ff7fce..5c3ab48c3 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -1178,10 +1178,12 @@ static int flock_lock_inode(struct inode *inode, struct file_lock *request)
 		if (!flock_locks_conflict(&request->c, &fl->c))
 			continue;
 		error = -EAGAIN;
-		if (!(request->c.flc_flags & FL_SLEEP))
-			goto out;
-		error = FILE_LOCK_DEFERRED;
-		locks_insert_block(&fl->c, &request->c, flock_locks_conflict);
+		if (request->c.flc_flags & FL_SLEEP) {
+			error = FILE_LOCK_DEFERRED;
+			locks_insert_block(&fl->c, &request->c,
+					   flock_locks_conflict);
+		}
+		trace_filelock_conflict(inode, request, fl, error);
 		goto out;
 	}
 	if (request->c.flc_flags & FL_ACCESS)
@@ -1260,8 +1262,11 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request,
 			if (conflock)
 				locks_copy_conflock(conflock, fl);
 			error = -EAGAIN;
-			if (!(request->c.flc_flags & FL_SLEEP))
+			if (!(request->c.flc_flags & FL_SLEEP)) {
+				trace_filelock_conflict(inode, request, fl,
+							error);
 				goto out;
+			}
 			/*
 			 * Deadlock detection and insertion into the blocked
 			 * locks list must be done while holding the same lock!
@@ -1279,6 +1284,7 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request,
 						     posix_locks_conflict);
 			}
 			spin_unlock(&blocked_lock_lock);
+			trace_filelock_conflict(inode, request, fl, error);
 			goto out;
 		}
 	}
diff --git a/include/trace/events/filelock.h b/include/trace/events/filelock.h
index f11284be0..93a1f064b 100644
--- a/include/trace/events/filelock.h
+++ b/include/trace/events/filelock.h
@@ -120,6 +120,61 @@ DEFINE_EVENT(filelock_lock, flock_lock_inode,
 		TP_PROTO(struct inode *inode, struct file_lock *fl, int ret),
 		TP_ARGS(inode, fl, ret));
 
+TRACE_EVENT(filelock_conflict,
+	TP_PROTO(struct inode *inode, struct file_lock *req,
+		 struct file_lock *blocker, int ret),
+
+	TP_ARGS(inode, req, blocker, ret),
+
+	TP_STRUCT__entry(
+		__field(u64, i_ino)
+		__field(loff_t, req_start)
+		__field(loff_t, req_end)
+		__field(loff_t, blocker_start)
+		__field(loff_t, blocker_end)
+		__field(fl_owner_t, req_owner)
+		__field(fl_owner_t, blocker_owner)
+		__field(dev_t, s_dev)
+		__field(pid_t, req_pid)
+		__field(pid_t, blocker_pid)
+		__field(unsigned int, req_flags)
+		__field(unsigned int, blocker_flags)
+		__field(int, ret)
+		__field(unsigned char, req_type)
+		__field(unsigned char, blocker_type)
+	),
+
+	TP_fast_assign(
+		__entry->s_dev = inode->i_sb->s_dev;
+		__entry->i_ino = inode->i_ino;
+		__entry->req_owner = req->c.flc_owner;
+		__entry->req_pid = req->c.flc_pid;
+		__entry->req_flags = req->c.flc_flags;
+		__entry->req_type = req->c.flc_type;
+		__entry->req_start = req->fl_start;
+		__entry->req_end = req->fl_end;
+		__entry->blocker_owner = blocker->c.flc_owner;
+		__entry->blocker_pid = blocker->c.flc_pid;
+		__entry->blocker_flags = blocker->c.flc_flags;
+		__entry->blocker_type = blocker->c.flc_type;
+		__entry->blocker_start = blocker->fl_start;
+		__entry->blocker_end = blocker->fl_end;
+		__entry->ret = ret;
+	),
+
+	TP_printk("dev=0x%x:0x%x ino=0x%llx req_owner=%p req_pid=%d req_flags=%s req_type=%s req_start=%lld req_end=%lld blocker_owner=%p blocker_pid=%d blocker_flags=%s blocker_type=%s blocker_start=%lld blocker_end=%lld ret=%d",
+		MAJOR(__entry->s_dev), MINOR(__entry->s_dev), __entry->i_ino,
+		__entry->req_owner, __entry->req_pid,
+		show_fl_flags(__entry->req_flags),
+		show_fl_type(__entry->req_type),
+		__entry->req_start, __entry->req_end,
+		__entry->blocker_owner, __entry->blocker_pid,
+		show_fl_flags(__entry->blocker_flags),
+		show_fl_type(__entry->blocker_type),
+		__entry->blocker_start, __entry->blocker_end,
+		__entry->ret)
+);
+
 #define show_lease_break_flags(val)					\
 	__print_flags(val, "|",						\
 		{ LEASE_BREAK_LEASE,		"LEASE" },		\

base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [RFC PATCH] filelock: trace the conflicting lock when a request is refused or deferred
  2026-12-01 16:36 [RFC PATCH] filelock: trace the conflicting lock when a request is refused or deferred Tianyi Chen
@ 2026-12-01 17:26 ` Jeff Layton
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Layton @ 2026-12-01 17:26 UTC (permalink / raw)
  To: Tianyi Chen, Chuck Lever
  Cc: Alexander Aring, Steven Rostedt, Masami Hiramatsu,
	Mathieu Desnoyers, Christian Brauner, Jan Kara, linux-fsdevel,
	linux-trace-kernel, linux-kernel

On Wed, 2026-12-02 at 01:36 +0900, Tianyi Chen wrote:
> A SQLite writer retrying F_SETLK never joins the kernel wait queue.
> When a frozen process held the conflicting lock in my test, the writer
> got EAGAIN but /proc/locks showed no waiter. The new event identified
> the blocker on byte 120. A later F_GETLK can only describe a conflict
> at query time, not the one that caused the refusal.
> 
> Trace the request and conflicting lock together while fs/locks.c still
> holds the file-lock context lock. This also records blocking requests
> that are deferred and POSIX deadlock refusals; it does not change lock
> or freezer policy. For OFD and flock locks, blocker_pid is the recorded
> PID, not the identity of every task that can release the lock.
> 
> This patch was developed with coding-assistant help.
> 
> Signed-off-by: Tianyi Chen <hi@tychen.cc>
> ---
> 
> Would you prefer to limit this event to non-blocking refusals?
> 
> Built and tested on mainline 19cc08c1a6a6 in one KVM boot (no dmesg
> warnings): no event on uncontended acquisition and release; the expected
> conflicts appeared for POSIX, OFD and flock refusals, deferred POSIX
> and flock requests, a POSIX deadlock, and the frozen SQLite writer case.
> 
>  fs/locks.c                      | 16 +++++++---
>  include/trace/events/filelock.h | 55 +++++++++++++++++++++++++++++++++
>  2 files changed, 66 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/locks.c b/fs/locks.c
> index 6e4ff7fce..5c3ab48c3 100644
> --- a/fs/locks.c
> +++ b/fs/locks.c
> @@ -1178,10 +1178,12 @@ static int flock_lock_inode(struct inode *inode, struct file_lock *request)
>  		if (!flock_locks_conflict(&request->c, &fl->c))
>  			continue;
>  		error = -EAGAIN;
> -		if (!(request->c.flc_flags & FL_SLEEP))
> -			goto out;
> -		error = FILE_LOCK_DEFERRED;
> -		locks_insert_block(&fl->c, &request->c, flock_locks_conflict);
> +		if (request->c.flc_flags & FL_SLEEP) {
> +			error = FILE_LOCK_DEFERRED;
> +			locks_insert_block(&fl->c, &request->c,
> +					   flock_locks_conflict);
> +		}
> +		trace_filelock_conflict(inode, request, fl, error);
>  		goto out;
>  	}
>  	if (request->c.flc_flags & FL_ACCESS)
> @@ -1260,8 +1262,11 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request,
>  			if (conflock)
>  				locks_copy_conflock(conflock, fl);
>  			error = -EAGAIN;
> -			if (!(request->c.flc_flags & FL_SLEEP))
> +			if (!(request->c.flc_flags & FL_SLEEP)) {
> +				trace_filelock_conflict(inode, request, fl,
> +							error);
>  				goto out;
> +			}
>  			/*
>  			 * Deadlock detection and insertion into the blocked
>  			 * locks list must be done while holding the same lock!
> @@ -1279,6 +1284,7 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request,
>  						     posix_locks_conflict);
>  			}
>  			spin_unlock(&blocked_lock_lock);
> +			trace_filelock_conflict(inode, request, fl, error);
>  			goto out;
>  		}
>  	}
> diff --git a/include/trace/events/filelock.h b/include/trace/events/filelock.h
> index f11284be0..93a1f064b 100644
> --- a/include/trace/events/filelock.h
> +++ b/include/trace/events/filelock.h
> @@ -120,6 +120,61 @@ DEFINE_EVENT(filelock_lock, flock_lock_inode,
>  		TP_PROTO(struct inode *inode, struct file_lock *fl, int ret),
>  		TP_ARGS(inode, fl, ret));
>  
> +TRACE_EVENT(filelock_conflict,
> +	TP_PROTO(struct inode *inode, struct file_lock *req,
> +		 struct file_lock *blocker, int ret),
> +
> +	TP_ARGS(inode, req, blocker, ret),
> +
> +	TP_STRUCT__entry(
> +		__field(u64, i_ino)
> +		__field(loff_t, req_start)
> +		__field(loff_t, req_end)
> +		__field(loff_t, blocker_start)
> +		__field(loff_t, blocker_end)
> +		__field(fl_owner_t, req_owner)
> +		__field(fl_owner_t, blocker_owner)
> +		__field(dev_t, s_dev)
> +		__field(pid_t, req_pid)
> +		__field(pid_t, blocker_pid)
> +		__field(unsigned int, req_flags)
> +		__field(unsigned int, blocker_flags)
> +		__field(int, ret)
> +		__field(unsigned char, req_type)
> +		__field(unsigned char, blocker_type)
> +	),
> +
> +	TP_fast_assign(
> +		__entry->s_dev = inode->i_sb->s_dev;
> +		__entry->i_ino = inode->i_ino;
> +		__entry->req_owner = req->c.flc_owner;
> +		__entry->req_pid = req->c.flc_pid;
> +		__entry->req_flags = req->c.flc_flags;
> +		__entry->req_type = req->c.flc_type;
> +		__entry->req_start = req->fl_start;
> +		__entry->req_end = req->fl_end;
> +		__entry->blocker_owner = blocker->c.flc_owner;
> +		__entry->blocker_pid = blocker->c.flc_pid;
> +		__entry->blocker_flags = blocker->c.flc_flags;
> +		__entry->blocker_type = blocker->c.flc_type;
> +		__entry->blocker_start = blocker->fl_start;
> +		__entry->blocker_end = blocker->fl_end;
> +		__entry->ret = ret;
> +	),
> +
> +	TP_printk("dev=0x%x:0x%x ino=0x%llx req_owner=%p req_pid=%d req_flags=%s req_type=%s req_start=%lld req_end=%lld blocker_owner=%p blocker_pid=%d blocker_flags=%s blocker_type=%s blocker_start=%lld blocker_end=%lld ret=%d",
> +		MAJOR(__entry->s_dev), MINOR(__entry->s_dev), __entry->i_ino,
> +		__entry->req_owner, __entry->req_pid,
> +		show_fl_flags(__entry->req_flags),
> +		show_fl_type(__entry->req_type),
> +		__entry->req_start, __entry->req_end,
> +		__entry->blocker_owner, __entry->blocker_pid,
> +		show_fl_flags(__entry->blocker_flags),
> +		show_fl_type(__entry->blocker_type),
> +		__entry->blocker_start, __entry->blocker_end,
> +		__entry->ret)
> +);
> +
>  #define show_lease_break_flags(val)					\
>  	__print_flags(val, "|",						\
>  		{ LEASE_BREAK_LEASE,		"LEASE" },		\
> 
> base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d

Looks reasonable

Reviewed-by: Jeff Layton <jlayton@kernel.org>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 17:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 16:36 [RFC PATCH] filelock: trace the conflicting lock when a request is refused or deferred Tianyi Chen
2026-12-01 17:26 ` Jeff Layton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox