Re: [PATCH] overflow: Introduce struct_offset() to get offset of member

Steven Rostedt
2025-11-26T20:07:55Z

On Wed, 26 Nov 2025 14:52:49 -0500
Steven Rostedt <rostedt@goodmis.org> wrote:

> This will likely come in handy for other use cases too.

Just looking at kernel/trace, I can see the following use cases too:

diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c
index e21176f396d5..b6a4c2050127 100644
--- a/kernel/trace/blktrace.c
+++ b/kernel/trace/blktrace.c
@@ -1482,7 +1482,7 @@ static void blk_trace_synthesize_old_trace(struct trace_iterator *iter)
{
struct trace_seq *s = &iter->seq;
struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
- const int offset = offsetof(struct blk_io_trace, sector);
+ const int offset = struct_offset(t, sector);
struct blk_io_trace old = {
.magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
.time = iter->ts,
diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_functions_graph.c
index 17c75cf2348e..7ff2a23e23c9 100644
--- a/kernel/trace/trace_functions_graph.c
+++ b/kernel/trace/trace_functions_graph.c
@@ -955,7 +955,7 @@ print_graph_entry_leaf(struct trace_iterator *iter,
int cpu = iter->cpu;
int i;

- args_size = iter->ent_size - offsetof(struct ftrace_graph_ent_entry, args);
+ args_size = iter->ent_size - struct_offset(entry, args);

graph_ret = &ret_entry->ret;
call = &entry->graph_ent;
@@ -1048,7 +1048,7 @@ print_graph_entry_nested(struct trace_iterator *iter,

trace_seq_printf(s, "%ps", (void *)func);

- args_size = iter->ent_size - offsetof(struct ftrace_graph_ent_entry, args);
+ args_size = iter->ent_size - struct_offset(entry, args);

if (args_size >= FTRACE_REGS_MAX_ARGS * sizeof(long))
print_function_args(s, FGRAPH_ENTRY_ARGS(entry), func);
diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
index ebbab3e9622b..30113756d56f 100644
--- a/kernel/trace/trace_output.c
+++ b/kernel/trace/trace_output.c
@@ -1167,7 +1167,7 @@ static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,

trace_assign_type(field, iter->ent);

- args_size = iter->ent_size - offsetof(struct ftrace_entry, args);
+ args_size = iter->ent_size - struct_offset(field, args);
if (args_size >= FTRACE_REGS_MAX_ARGS * sizeof(long))
args = field->args;
else
@@ -1799,7 +1799,7 @@ static enum print_line_t trace_raw_data(struct trace_iterator *iter, int flags,

trace_seq_printf(&iter->seq, "# %x buf:", field->id);

- for (i = 0; i < iter->ent_size - offsetof(struct raw_data_entry, buf); i++)
+ for (i = 0; i < iter->ent_size - struct_offset(field, buf); i++)
trace_seq_printf(&iter->seq, " %02x",
(unsigned char)field->buf[i]);

It better associates the variable with its member offset.

-- Steve