Linux Trace Kernel
 help / color / mirror / Atom feed
From: Tianyi Chen <hi@tychen.cc>
To: Jeff Layton <jlayton@kernel.org>, Chuck Lever <cel@kernel.org>
Cc: Alexander Aring <alex.aring@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
	linux-fsdevel@vger.kernel.org,
	linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org,
	hi@tychen.cc
Subject: [RFC PATCH] filelock: trace the conflicting lock when a request is refused or deferred
Date: Wed,  2 Dec 2026 01:36:02 +0900	[thread overview]
Message-ID: <20261201163611.467129-1-hi@tychen.cc> (raw)

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


             reply	other threads:[~2026-12-01 16:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 16:36 Tianyi Chen [this message]
2026-12-01 17:26 ` [RFC PATCH] filelock: trace the conflicting lock when a request is refused or deferred Jeff Layton

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=20261201163611.467129-1-hi@tychen.cc \
    --to=hi@tychen.cc \
    --cc=alex.aring@gmail.com \
    --cc=brauner@kernel.org \
    --cc=cel@kernel.org \
    --cc=jack@suse.cz \
    --cc=jlayton@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=rostedt@goodmis.org \
    /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