mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thorsten Blum <thorsten.blum@linux.dev>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Guillaume Nault <gnault@redhat.com>,
	Paolo Abeni <pabeni@redhat.com>, Ido Schimmel <idosch@nvidia.com>,
	Petr Machata <petrm@nvidia.com>,
	linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH net-next] tracing: ipv6: Replace deprecated strcpy() with strscpy()
Date: Wed, 23 Jul 2025 14:15:02 -0700	[thread overview]
Message-ID: <AE36F453-DEAE-432C-9CDA-0D2DA875CDA2@linux.dev> (raw)
In-Reply-To: <20250723143625.79ab2c16@batman.local.home>

On 23. Jul 2025, at 11:36, Steven Rostedt wrote:
> On Wed, 23 Jul 2025 10:46:12 -0700 Thorsten Blum wrote:
> 
>> Your commit fca8300f68fe3 changed it from __dynamic_array() to __array()
>> and __string() seems to be just a special version of __dynamic_array()
>> with a length of -1.
>> 
>> In the commit description you wrote: "Since the size of the name is at
>> most 16 bytes (defined by IFNAMSIZ), it is not worth spending the effort
>> to determine the size of the string."
> 
> So the original had:
> 
> 	__dynamic_array(char,  name,   IFNAMSIZ )
> 
> Which is not dynamic at all. A dynamic_array (like __string) saves the
> size in meta data within the event. So basically the above is wasting
> bytes to save a fixed size. If you are going to use a dynamic array,
> might as well make it dynamic!
> 
> I was doing various clean ups back then so I didn't look too deeply
> into this event when I made that change. I just saw the obvious waste
> of space in the ring buffer.
> 
> Just to explain it in more detail. A dynamic_array has in the ring buffer:
> 
> 	short offset;
> 	short len;
> 	[..]
> 	char  name[len];
> 
> That is, 4 bytes are used to know the size of the array and where in
> the event it is located. Thus the __dynamic_array() usage basically had:
> 
> 	short offset;
> 	short len = IFNAMSIZ;
> 	[..]
> 	char name[IFNAMSIZ];
> 
> Why have the offset and length? with just __array(char, name, IFNAMSIZ}
> it would be just:
> 
> 	char name[IFNAMSIZ];
> 
> See why I changed it?
> 
> Now, the change I'm suggesting now would make the __string() be dynamic!
> 
> 	short offset;
> 	short len = strlen(res->nh && res->nh->fib_nh_dev ? res->nh->fib_nh_dev->name : "-") + 1;
> 	[..]
> 	char name[len];
> 
> As IFNAMSIZ is 16, and the above adds 4 bytes to the name, if the name
> is less than 7 bytes or less, you save memory on the ring buffer.
> 
> 	2 bytes: offset
> 	2 bytes: len;
> 	7 bytes + '\0'
> 
> total: 12 bytes
> 
> Note, if there's only one dynamic value, it is always at least 4 bytes aligned.

Thanks for the detailed explanation.

I think the better change would be this then:

diff --git a/include/trace/events/fib6.h b/include/trace/events/fib6.h
index 8d22b2e98d48..3f95df1fd155 100644
--- a/include/trace/events/fib6.h
+++ b/include/trace/events/fib6.h
@@ -32,8 +32,9 @@ TRACE_EVENT(fib6_table_lookup,
		__field(        u16,	dport		)
		__field(        u8,	proto		)
		__field(        u8,	rt_type		)
-		__array(		char,	name,	IFNAMSIZ )
-		__array(		__u8,	gw,	16	 )
+		__string(	name,	res->nh && res->nh->fib_nh_dev ?
+					res->nh->fib_nh_dev->name : "-"	)
+		__array(	__u8,	gw,	16	)
	),

	TP_fast_assign(
@@ -64,11 +65,8 @@ TRACE_EVENT(fib6_table_lookup,
			__entry->dport = 0;
		}

-		if (res->nh && res->nh->fib_nh_dev) {
-			strscpy(__entry->name, res->nh->fib_nh_dev->name, IFNAMSIZ);
-		} else {
-			strcpy(__entry->name, "-");
-		}
+		__assign_str(name);
+
		if (res->f6i == net->ipv6.fib6_null_entry) {
			in6 = (struct in6_addr *)__entry->gw;
			*in6 = in6addr_any;
@@ -82,7 +80,7 @@ TRACE_EVENT(fib6_table_lookup,
		  __entry->tb_id, __entry->oif, __entry->iif, __entry->proto,
		  __entry->src, __entry->sport, __entry->dst, __entry->dport,
		  __entry->flowlabel, __entry->tos, __entry->scope,
-		  __entry->flags, __entry->name, __entry->gw, __entry->err)
+		  __entry->flags, __get_str(name), __entry->gw, __entry->err)
);

#endif /* _TRACE_FIB6_H */


I'll submit a v2 if you agree that this is correct.

Thanks,
Thorsten


  reply	other threads:[~2025-07-23 21:15 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-14  7:54 Thorsten Blum
2025-07-14 11:42 ` Guillaume Nault
2025-07-14 16:38 ` Steven Rostedt
2025-07-23 17:46   ` Thorsten Blum
2025-07-23 18:36     ` Steven Rostedt
2025-07-23 21:15       ` Thorsten Blum [this message]
2025-07-23 21:21         ` Steven Rostedt
2025-07-23 21:30           ` Thorsten Blum

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=AE36F453-DEAE-432C-9CDA-0D2DA875CDA2@linux.dev \
    --to=thorsten.blum@linux.dev \
    --cc=gnault@redhat.com \
    --cc=idosch@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=pabeni@redhat.com \
    --cc=petrm@nvidia.com \
    --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

all inboxes | Powered by JetHome®