Skip to contentSkip to conversation

tracing: don't reference jiffies from writeback TP_printk()

PATCH In review Chaosheng Qu 4 participants 6 messages Oct 26 – Nov 18

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: don't reference jiffies from writeback TP_printk()

From: Chaosheng Qu <[email protected]>
Subject: [PATCH] tracing: don't reference jiffies from writeback TP_printk()
To: Steven Rostedt <[email protected]>
Cc: Masami Hiramatsu <[email protected]>,
Mathieu Desnoyers <[email protected]>,
"Matthew Wilcox (Oracle)" <[email protected]>,
Jan Kara <[email protected]>,
[email protected], [email protected]

writeback_sb_inodes_requeue and writeback_single_inode_start/() compute the inode age with:

	(jiffies - __entry->dirtied_when) / HZ

inside TP_printk(). TP_printk() is not part of the recorded event: it runs when the trace is read back, so the expression is evaluated at formatter time, not at the time the event was recorded. The formatted output therefore reports the interval between recording and reading, not the age of the inode when it was traced.

It is also evaluated outside of the kernel: tracefs renders the format string once and userspace re-evaluates it. A tool that has to resolve every symbol in the expression (trace-cmd and the parser in LISA both do) resolves the bare "jiffies" to its address, so the printed age becomes ((unsigned long)&jiffies - dirtied_when) / HZ, which is nonsense. This was reported as a wrong "age" value in trace-cmd output:

https://bugzilla.kernel.org/show_bug.cgi?id=219839

Nothing in TP_printk() may depend on the state of the machine at read time, which is the same rule that keeps ktime_get() and friends out of it. writeback_queue_io already does the right thing and stores the age in TP_fast_assign(); make these two events do that as well.

The new __entry->age field is appended after the existing fields, so no existing field changes offset and trace consumers that index the event by offset keep working. The "age=%lu" placeholder keeps its position in the output text.

---
 include/trace/events/writeback.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
include/trace/events/writeback.h +6 -2
index 13ee076..23d107c 100644
@@ -745,6 +745,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		__field(u64, cgroup_ino)
 		__field(unsigned long, state)
 		__field(unsigned long, dirtied_when)
+		__field(unsigned long, age)
 	),
 
 	TP_fast_assign(
@@ -753,6 +754,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		__entry->ino		= inode->i_ino;
 		__entry->state		= inode_state_read_once(inode);
 		__entry->dirtied_when	= inode->dirtied_when;
+		__entry->age		= (jiffies - __entry->dirtied_when) / HZ;
 		__entry->cgroup_ino	= __trace_wb_assign_cgroup(inode_to_wb(inode));
 	),
 
@@ -761,7 +763,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		  __entry->ino,
 		  show_inode_state(__entry->state),
 		  __entry->dirtied_when,
-		  (jiffies - __entry->dirtied_when) / HZ,
+		  __entry->age,
 		  __entry->cgroup_ino
 	)
 );
@@ -784,6 +786,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
 		__field(unsigned long, writeback_index)
 		__field(unsigned long, wrote)
 		__field(long, nr_to_write)
+		__field(unsigned long, age)
 	),
 
 	TP_fast_assign(
@@ -795,6 +798,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
 		__entry->writeback_index = inode->i_mapping->writeback_index;
 		__entry->nr_to_write	= nr_to_write;
 		__entry->wrote		= nr_to_write - wbc->nr_to_write;
+		__entry->age		= (jiffies - __entry->dirtied_when) / HZ;
 		__entry->cgroup_ino	= __trace_wbc_assign_cgroup(wbc);
 	),
 
@@ -804,7 +808,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
 		  __entry->ino,
 		  show_inode_state(__entry->state),
 		  __entry->dirtied_when,
-		  (jiffies - __entry->dirtied_when) / HZ,
+		  __entry->age,
 		  __entry->writeback_index,
 		  __entry->nr_to_write,
 		  __entry->wrote,
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 36 lines · Chaosheng Qu

From: Chaosheng Qu <[email protected]>
Subject: [PATCH] tracing: don't reference jiffies from writeback TP_printk()
To: Steven Rostedt <[email protected]>
Cc: Masami Hiramatsu <[email protected]>,
Mathieu Desnoyers <[email protected]>,
"Matthew Wilcox (Oracle)" <[email protected]>,
Jan Kara <[email protected]>,
[email protected], [email protected]

writeback_sb_inodes_requeue and writeback_single_inode_start/() compute the inode age with:

	(jiffies - __entry->dirtied_when) / HZ

inside TP_printk(). TP_printk() is not part of the recorded event: it runs when the trace is read back, so the expression is evaluated at formatter time, not at the time the event was recorded. The formatted output therefore reports the interval between recording and reading, not the age of the inode when it was traced.

It is also evaluated outside of the kernel: tracefs renders the format string once and userspace re-evaluates it. A tool that has to resolve every symbol in the expression (trace-cmd and the parser in LISA both do) resolves the bare "jiffies" to its address, so the printed age becomes ((unsigned long)&jiffies - dirtied_when) / HZ, which is nonsense. This was reported as a wrong "age" value in trace-cmd output:

https://bugzilla.kernel.org/show_bug.cgi?id=219839

Nothing in TP_printk() may depend on the state of the machine at read time, which is the same rule that keeps ktime_get() and friends out of it. writeback_queue_io already does the right thing and stores the age in TP_fast_assign(); make these two events do that as well.

Quoted 7 lines

The new __entry->age field is appended after the existing fields, so no existing field changes offset and trace consumers that index the event by offset keep working. The "age=%lu" placeholder keeps its position in the output text.

Looks good. Feel free to add:

Honza

Quoted 61 lines
---
 include/trace/events/writeback.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
include/trace/events/writeback.h +6 -2
index 13ee076..23d107c 100644
@@ -745,6 +745,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		__field(u64, cgroup_ino)
 		__field(unsigned long, state)
 		__field(unsigned long, dirtied_when)
+		__field(unsigned long, age)
 	),
 
 	TP_fast_assign(
@@ -753,6 +754,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		__entry->ino		= inode->i_ino;
 		__entry->state		= inode_state_read_once(inode);
 		__entry->dirtied_when	= inode->dirtied_when;
+		__entry->age		= (jiffies - __entry->dirtied_when) / HZ;
 		__entry->cgroup_ino	= __trace_wb_assign_cgroup(inode_to_wb(inode));
 	),
 
@@ -761,7 +763,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		  __entry->ino,
 		  show_inode_state(__entry->state),
 		  __entry->dirtied_when,
-		  (jiffies - __entry->dirtied_when) / HZ,
+		  __entry->age,
 		  __entry->cgroup_ino
 	)
 );
@@ -784,6 +786,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
 		__field(unsigned long, writeback_index)
 		__field(unsigned long, wrote)
 		__field(long, nr_to_write)
+		__field(unsigned long, age)
 	),
 
 	TP_fast_assign(
@@ -795,6 +798,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
 		__entry->writeback_index = inode->i_mapping->writeback_index;
 		__entry->nr_to_write	= nr_to_write;
 		__entry->wrote		= nr_to_write - wbc->nr_to_write;
+		__entry->age		= (jiffies - __entry->dirtied_when) / HZ;
 		__entry->cgroup_ino	= __trace_wbc_assign_cgroup(wbc);
 	),
 
@@ -804,7 +808,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
 		  __entry->ino,
 		  show_inode_state(__entry->state),
 		  __entry->dirtied_when,
-		  (jiffies - __entry->dirtied_when) / HZ,
+		  __entry->age,
 		  __entry->writeback_index,
 		  __entry->nr_to_write,
 		  __entry->wrote,
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#2 lore raw
In reply to Chaosheng Qu
Quoted 52 lines · On Mon, 26 Oct 2026 14:17:18 +0800 Chaosheng Qu <[email protected]>…

From: Chaosheng Qu <[email protected]>
Subject: [PATCH] tracing: don't reference jiffies from writeback TP_printk()
To: Steven Rostedt <[email protected]>
Cc: Masami Hiramatsu <[email protected]>,
Mathieu Desnoyers <[email protected]>,
"Matthew Wilcox (Oracle)" <[email protected]>,
Jan Kara <[email protected]>,
[email protected], [email protected]

writeback_sb_inodes_requeue and writeback_single_inode_start/() compute the inode age with:

	(jiffies - __entry->dirtied_when) / HZ

inside TP_printk(). TP_printk() is not part of the recorded event: it runs when the trace is read back, so the expression is evaluated at formatter time, not at the time the event was recorded. The formatted output therefore reports the interval between recording and reading, not the age of the inode when it was traced.

It is also evaluated outside of the kernel: tracefs renders the format string once and userspace re-evaluates it. A tool that has to resolve every symbol in the expression (trace-cmd and the parser in LISA both do) resolves the bare "jiffies" to its address, so the printed age becomes ((unsigned long)&jiffies - dirtied_when) / HZ, which is nonsense. This was reported as a wrong "age" value in trace-cmd output:

https://bugzilla.kernel.org/show_bug.cgi?id=219839

Nothing in TP_printk() may depend on the state of the machine at read time, which is the same rule that keeps ktime_get() and friends out of it. writeback_queue_io already does the right thing and stores the age in TP_fast_assign(); make these two events do that as well.

The new __entry->age field is appended after the existing fields, so no existing field changes offset and trace consumers that index the event by offset keep working. The "age=%lu" placeholder keeps its position in the output text.

---
 include/trace/events/writeback.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
include/trace/events/writeback.h +0 -0
index 13ee076..23d107c 100644

include/trace/events/writeback.h TRACE_EVENT(writeback_sb_inodes_requeue, · line 748

Quoted 4 lines
include/trace/events/writeback.h +1 -0
@@ -745,6 +745,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		__field(u64, cgroup_ino)
 		__field(unsigned long, state)
 		__field(unsigned long, dirtied_when)
+		__field(unsigned long, age)

Actually can we just record jiffies here?

include/trace/events/writeback.h TRACE_EVENT(writeback_sb_inodes_requeue, · line 757

Quoted 8 lines
include/trace/events/writeback.h +1 -0
 	),
 
 	TP_fast_assign(
@@ -753,6 +754,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		__entry->ino		= inode->i_ino;
 		__entry->state		= inode_state_read_once(inode);
 		__entry->dirtied_when	= inode->dirtied_when;
+		__entry->age		= (jiffies - __entry->dirtied_when) / HZ;

Divisions are expensive and this is executed during the trace where we try to keep the trace side effects as small as possible.

include/trace/events/writeback.h TRACE_EVENT(writeback_sb_inodes_requeue, · line 766

Quoted 9 lines
include/trace/events/writeback.h +1 -1
 		__entry->cgroup_ino	= __trace_wb_assign_cgroup(inode_to_wb(inode));
 	),
 
@@ -761,7 +763,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		  __entry->ino,
 		  show_inode_state(__entry->state),
 		  __entry->dirtied_when,
-		  (jiffies - __entry->dirtied_when) / HZ,
+		  __entry->age,

By saving jiffies, this would be:

		  (__entry->jiffies - __entry->dirtied_when) / HZ

Where it would be processed at the time of read (aka "slow path").

As jiffies would be saved in the event, user space would also be able to calculate this.

-- Steve

Quoted 1 line
include/trace/events/writeback.h +0 -0
 		  __entry->cgroup_ino
#3 lore raw

PATCH v2 1/1 tracing: record jiffies in writeback events instead of computing age

From 677560a14dae0d9581effc5cdb9375052902bb21 Mon Sep 17 00:00:00 2001
From: Chaosheng Qu <[email protected]>
Date: Tue, 27 Oct 2026 14:28:09 +0800
Subject: [PATCH] tracing: record jiffies in writeback events instead of
computing age

writeback_sb_inodes_requeue and writeback_single_inode_start/() compute the inode age with:

	(jiffies - __entry->dirtied_when) / HZ

inside TP_printk(). TP_printk() is not part of the recorded event: it runs when the trace is read back, so the expression is evaluated at formatter time, not at the time the event was recorded.

It is also evaluated outside of the kernel: tracefs renders the format string once and userspace re-evaluates it. A tool that has to resolve every symbol in the expression (trace-cmd and the parser in LISA both do) resolves the bare "jiffies" to its address, so the printed age becomes ((unsigned long)&jiffies - dirtied_when) / HZ, which is nonsense. This was reported as a wrong "age" value in trace-cmd output:

https://bugzilla.kernel.org/show_bug.cgi?id=219839

Nothing in TP_printk() may depend on the state of the machine at read time, which is the same rule that keeps ktime_get() and friends out of it.

v1 stored the age itself in TP_fast_assign(). Steve pointed out that the division then runs on the trace path, which is where the side effects have to stay small, and that recording the jiffies snapshot instead lets userspace compute whatever it wants from the raw value. Do that: save jiffies in TP_fast_assign() and keep the subtraction and the division in TP_printk(), where they run on the slow path. This follows the same shape as xprtrdma_req's connect/reconnect fields.

The new __entry->jiffies field is appended after the existing fields, so no existing field changes offset and trace consumers that index the event by offset keep working. The "age=%lu" placeholder keeps its position in the output text, and the value it prints is unchanged.

---
 include/trace/events/writeback.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
include/trace/events/writeback.h +6 -2
index 13ee076cc..b7dfc9f7b 100644
@@ -745,6 +745,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		__field(u64, cgroup_ino)
 		__field(unsigned long, state)
 		__field(unsigned long, dirtied_when)
+		__field(unsigned long, jiffies)
 	),
 
 	TP_fast_assign(
@@ -753,6 +754,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		__entry->ino		= inode->i_ino;
 		__entry->state		= inode_state_read_once(inode);
 		__entry->dirtied_when	= inode->dirtied_when;
+		__entry->jiffies	= jiffies;
 		__entry->cgroup_ino	= __trace_wb_assign_cgroup(inode_to_wb(inode));
 	),
 
@@ -761,7 +763,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
 		  __entry->ino,
 		  show_inode_state(__entry->state),
 		  __entry->dirtied_when,
-		  (jiffies - __entry->dirtied_when) / HZ,
+		  (__entry->jiffies - __entry->dirtied_when) / HZ,
 		  __entry->cgroup_ino
 	)
 );
@@ -784,6 +786,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
 		__field(unsigned long, writeback_index)
 		__field(unsigned long, wrote)
 		__field(long, nr_to_write)
+		__field(unsigned long, jiffies)
 	),
 
 	TP_fast_assign(
@@ -795,6 +798,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
 		__entry->writeback_index = inode->i_mapping->writeback_index;
 		__entry->nr_to_write	= nr_to_write;
 		__entry->wrote		= nr_to_write - wbc->nr_to_write;
+		__entry->jiffies	= jiffies;
 		__entry->cgroup_ino	= __trace_wbc_assign_cgroup(wbc);
 	),
 
@@ -804,7 +808,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
 		  __entry->ino,
 		  show_inode_state(__entry->state),
 		  __entry->dirtied_when,
-		  (jiffies - __entry->dirtied_when) / HZ,
+		  (__entry->jiffies - __entry->dirtied_when) / HZ,
 		  __entry->writeback_index,
 		  __entry->nr_to_write,
 		  __entry->wrote,
Signature · 1 line
2.43.0
maintainer
#4 lore raw

Re: [PATCH v2] tracing: record jiffies in writeback events instead of computing age

Quoted 42 lines · On Wed, 28 Oct 2026 11:25:24 +0800 Chaosheng Qu <[email protected]>…
Quoted 1 line

From 677560a14dae0d9581effc5cdb9375052902bb21 Mon Sep 17 00:00:00 2001

From: Chaosheng Qu <[email protected]>
Date: Tue, 27 Oct 2026 14:28:09 +0800
Subject: [PATCH] tracing: record jiffies in writeback events instead of
computing age

writeback_sb_inodes_requeue and writeback_single_inode_start/() compute the inode age with:

	(jiffies - __entry->dirtied_when) / HZ

inside TP_printk(). TP_printk() is not part of the recorded event: it runs when the trace is read back, so the expression is evaluated at formatter time, not at the time the event was recorded.

It is also evaluated outside of the kernel: tracefs renders the format string once and userspace re-evaluates it. A tool that has to resolve every symbol in the expression (trace-cmd and the parser in LISA both do) resolves the bare "jiffies" to its address, so the printed age becomes ((unsigned long)&jiffies - dirtied_when) / HZ, which is nonsense. This was reported as a wrong "age" value in trace-cmd output:

https://bugzilla.kernel.org/show_bug.cgi?id=219839

Nothing in TP_printk() may depend on the state of the machine at read time, which is the same rule that keeps ktime_get() and friends out of it.

v1 stored the age itself in TP_fast_assign(). Steve pointed out that the division then runs on the trace path, which is where the side effects have to stay small, and that recording the jiffies snapshot instead lets userspace compute whatever it wants from the raw value. Do that: save jiffies in TP_fast_assign() and keep the subtraction and the division in TP_printk(), where they run on the slow path. This follows the same shape as xprtrdma_req's connect/reconnect fields.

The new __entry->jiffies field is appended after the existing fields, so no existing field changes offset and trace consumers that index the event by offset keep working. The "age=%lu" placeholder keeps its position in the output text, and the value it prints is unchanged.

Quoted 5 lines

---

This should likely go through the fs or mm tree.

-- Steve

maintainer Applied
#5 lore raw

Re: [PATCH v2] tracing: record jiffies in writeback events instead of computing age

In reply to Chaosheng Qu
Quoted 10 lines · Chaosheng Qu

From 677560a14dae0d9581effc5cdb9375052902bb21 Mon Sep 17 00:00:00 2001
From: Chaosheng Qu <[email protected]>
Date: Tue, 27 Oct 2026 14:28:09 +0800
Subject: [PATCH] tracing: record jiffies in writeback events instead of
computing age

writeback_sb_inodes_requeue and writeback_single_inode_start/() compute the inode age with:

[...]

Applied to the vfs-7.5.writeback branch of the vfs/vfs.git tree. Patches in the vfs-7.5.writeback branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.5.writeback

[1/1] tracing: record jiffies in writeback events instead of computing age

      https://git.kernel.org/vfs/vfs/c/ea7c5843e798