linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] rv: Fix wrong type cast
@ 2025-07-27 17:31 Nam Cao
  2025-07-27 17:31 ` [PATCH 1/2] rv: Fix wrong type cast in monitors_show() Nam Cao
  2025-07-27 17:31 ` [PATCH 2/2] rv: Fix wrong type cast in reactors_show() and monitor_reactor_show() Nam Cao
  0 siblings, 2 replies; 6+ messages in thread
From: Nam Cao @ 2025-07-27 17:31 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	Gabriele Monaco, linux-trace-kernel, linux-kernel
  Cc: Nam Cao

Hi,

After my previous rv simplification series:
https://lore.kernel.org/linux-trace-kernel/cover.1753378331.git.namcao@linutronix.de/T/#t

Some RV's available/enabled reactors/monitors files do not work anymore:

    root@yellow:~# cat /sys/kernel/tracing/rv/available_monitors
    nop: �������`������� ���������Z������u�����-s�����
    nop:���������������� �������@>H�����

    nop:
    ��������
    ��������8�������:��������
    `���������������

    root@yellow:~# cat /sys/kernel/tracing/rv/available_reactors
    ��������8�������
    P�������x�������
    8����������������^�����81s������I?�������������8�������

It turned out that there has been a type cast mistake in RV all this time,
but it still happened to work. Due to the recent refactor, the mistaken
type cast got "promoted" and became a real problem.

This series fixes up this type cast.

Nam Cao (2):
  rv: Fix wrong type cast in monitors_show()
  rv: Fix wrong type cast in reactors_show() and monitor_reactor_show()

 kernel/trace/rv/rv.c          | 2 +-
 kernel/trace/rv/rv_reactors.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

-- 
2.39.5


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

* [PATCH 1/2] rv: Fix wrong type cast in monitors_show()
  2025-07-27 17:31 [PATCH 0/2] rv: Fix wrong type cast Nam Cao
@ 2025-07-27 17:31 ` Nam Cao
  2025-07-28  8:59   ` Gabriele Monaco
  2025-07-27 17:31 ` [PATCH 2/2] rv: Fix wrong type cast in reactors_show() and monitor_reactor_show() Nam Cao
  1 sibling, 1 reply; 6+ messages in thread
From: Nam Cao @ 2025-07-27 17:31 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	Gabriele Monaco, linux-trace-kernel, linux-kernel
  Cc: Nam Cao

Argument 'p' of monitors_show() is not a pointer to struct rv_monitor, it
is actually a pointer to the list_head inside struct rv_monitor. Therefore
it is wrong to cast 'p' to struct rv_monitor *.

This wrong type cast has been there since the beginning. But it still
worked because the list_head was the first field in struct rv_monitor_def.
This is no longer true since commit 24cbfe18d55a ("rv: Merge struct
rv_monitor_def into struct rv_monitor") moved the list_head, and this wrong
type cast became a functional problem.

Properly use container_of() instead.

Fixes: 24cbfe18d55a ("rv: Merge struct rv_monitor_def into struct rv_monitor")
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
 kernel/trace/rv/rv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/trace/rv/rv.c b/kernel/trace/rv/rv.c
index 2ac0e8bf4e60..1482e91c39f4 100644
--- a/kernel/trace/rv/rv.c
+++ b/kernel/trace/rv/rv.c
@@ -458,7 +458,7 @@ static int create_monitor_dir(struct rv_monitor *mon, struct rv_monitor *parent)
  */
 static int monitors_show(struct seq_file *m, void *p)
 {
-	struct rv_monitor *mon = p;
+	struct rv_monitor *mon = container_of(p, struct rv_monitor, list);
 
 	if (mon->parent)
 		seq_printf(m, "%s:%s\n", mon->parent->name, mon->name);
-- 
2.39.5


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

* [PATCH 2/2] rv: Fix wrong type cast in reactors_show() and monitor_reactor_show()
  2025-07-27 17:31 [PATCH 0/2] rv: Fix wrong type cast Nam Cao
  2025-07-27 17:31 ` [PATCH 1/2] rv: Fix wrong type cast in monitors_show() Nam Cao
@ 2025-07-27 17:31 ` Nam Cao
  1 sibling, 0 replies; 6+ messages in thread
From: Nam Cao @ 2025-07-27 17:31 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	Gabriele Monaco, linux-trace-kernel, linux-kernel
  Cc: Nam Cao

Argument 'p' of reactors_show() and monitor_reactor_show() is not a pointer
to struct rv_reactor, it is actually a pointer to the list_head inside
struct rv_reactor. Therefore it's wrong to cast 'p' to struct rv_reactor *.

This wrong type cast has been there since the beginning. But it still
worked because the list_head was the first field in struct rv_reactor_def.
This is no longer true since commit 3d3c376118b5 ("rv: Merge struct
rv_reactor_def into struct rv_reactor") moved the list_head, and this wrong
type cast became a functional problem.

Properly use container_of() instead.

Fixes: 3d3c376118b5 ("rv: Merge struct rv_reactor_def into struct rv_reactor")
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
 kernel/trace/rv/rv_reactors.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/trace/rv/rv_reactors.c b/kernel/trace/rv/rv_reactors.c
index 106f2c4740f2..d32859fec238 100644
--- a/kernel/trace/rv/rv_reactors.c
+++ b/kernel/trace/rv/rv_reactors.c
@@ -86,7 +86,7 @@ static struct rv_reactor *get_reactor_rdef_by_name(char *name)
  */
 static int reactors_show(struct seq_file *m, void *p)
 {
-	struct rv_reactor *reactor = p;
+	struct rv_reactor *reactor = container_of(p, struct rv_reactor, list);
 
 	seq_printf(m, "%s\n", reactor->name);
 	return 0;
@@ -139,7 +139,7 @@ static const struct file_operations available_reactors_ops = {
 static int monitor_reactor_show(struct seq_file *m, void *p)
 {
 	struct rv_monitor *mon = m->private;
-	struct rv_reactor *reactor = p;
+	struct rv_reactor *reactor = container_of(p, struct rv_reactor, list);
 
 	if (mon->reactor == reactor)
 		seq_printf(m, "[%s]\n", reactor->name);
-- 
2.39.5


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

* Re: [PATCH 1/2] rv: Fix wrong type cast in monitors_show()
  2025-07-27 17:31 ` [PATCH 1/2] rv: Fix wrong type cast in monitors_show() Nam Cao
@ 2025-07-28  8:59   ` Gabriele Monaco
  2025-07-28  9:36     ` Nam Cao
  0 siblings, 1 reply; 6+ messages in thread
From: Gabriele Monaco @ 2025-07-28  8:59 UTC (permalink / raw)
  To: Nam Cao, Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	linux-trace-kernel, linux-kernel

On Sun, 2025-07-27 at 19:31 +0200, Nam Cao wrote:
> Argument 'p' of monitors_show() is not a pointer to struct
> rv_monitor, it is actually a pointer to the list_head inside struct
> rv_monitor. Therefore it is wrong to cast 'p' to struct rv_monitor *.
> 
> This wrong type cast has been there since the beginning. But it still
> worked because the list_head was the first field in struct
> rv_monitor_def. This is no longer true since commit 24cbfe18d55a
> ("rv: Merge struct rv_monitor_def into struct rv_monitor") moved the
> list_head, and this wrong type cast became a functional problem.
> 
> Properly use container_of() instead.
> 
> Fixes: 24cbfe18d55a ("rv: Merge struct rv_monitor_def into struct
> rv_monitor")
> Signed-off-by: Nam Cao <namcao@linutronix.de>
> ---
>  kernel/trace/rv/rv.c | 2 +-
> 
> @@ -458,7 +458,7 @@ static int create_monitor_dir(struct rv_monitor
> *mon, struct rv_monitor *parent)
>   */
>  static int monitors_show(struct seq_file *m, void *p)
>  {
> -	struct rv_monitor *mon = p;
> +	struct rv_monitor *mon = container_of(p, struct rv_monitor,
> list);
>  

Good catch, thanks! The container_of is the way to go.
Do you have valid reasons not to move the list_head to the top? It's
not a big deal but it would save computing and summing the offset. It
doesn't seem name (the current first element) really needs to stay
there.

Thanks,
Gabriele

>  	if (mon->parent)
>  		seq_printf(m, "%s:%s\n", mon->parent->name, mon-
> >name);


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

* Re: [PATCH 1/2] rv: Fix wrong type cast in monitors_show()
  2025-07-28  8:59   ` Gabriele Monaco
@ 2025-07-28  9:36     ` Nam Cao
  2025-07-28 11:07       ` Gabriele Monaco
  0 siblings, 1 reply; 6+ messages in thread
From: Nam Cao @ 2025-07-28  9:36 UTC (permalink / raw)
  To: Gabriele Monaco
  Cc: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	linux-trace-kernel, linux-kernel

On Mon, Jul 28, 2025 at 10:59:01AM +0200, Gabriele Monaco wrote:
> Good catch, thanks! The container_of is the way to go.
> Do you have valid reasons not to move the list_head to the top? It's
> not a big deal but it would save computing and summing the offset. It
> doesn't seem name (the current first element) really needs to stay
> there.

I checked x86_64 and riscv64, the generated assembly of this function
before & after moving the list_head on top is almost the same except for
some instructions' intermediate values. Both architectures have
instructions which load data at (pointer + offset), so this offset
computing does not require any extra instruction.

Best regards,
Nam

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

* Re: [PATCH 1/2] rv: Fix wrong type cast in monitors_show()
  2025-07-28  9:36     ` Nam Cao
@ 2025-07-28 11:07       ` Gabriele Monaco
  0 siblings, 0 replies; 6+ messages in thread
From: Gabriele Monaco @ 2025-07-28 11:07 UTC (permalink / raw)
  To: Nam Cao
  Cc: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	linux-trace-kernel, linux-kernel

On Mon, 2025-07-28 at 11:36 +0200, Nam Cao wrote:
> On Mon, Jul 28, 2025 at 10:59:01AM +0200, Gabriele Monaco wrote:
> > Good catch, thanks! The container_of is the way to go.
> > Do you have valid reasons not to move the list_head to the top?
> > It's
> > not a big deal but it would save computing and summing the offset.
> > It
> > doesn't seem name (the current first element) really needs to stay
> > there.
> 
> I checked x86_64 and riscv64, the generated assembly of this function
> before & after moving the list_head on top is almost the same except
> for
> some instructions' intermediate values. Both architectures have
> instructions which load data at (pointer + offset), so this offset
> computing does not require any extra instruction.
> 
> Best regards,
> Nam

Alright then, thanks for checking!

Reviewed-by: Gabriele Monaco <gmonaco@redhat.com>


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

end of thread, other threads:[~2025-07-28 11:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-27 17:31 [PATCH 0/2] rv: Fix wrong type cast Nam Cao
2025-07-27 17:31 ` [PATCH 1/2] rv: Fix wrong type cast in monitors_show() Nam Cao
2025-07-28  8:59   ` Gabriele Monaco
2025-07-28  9:36     ` Nam Cao
2025-07-28 11:07       ` Gabriele Monaco
2025-07-27 17:31 ` [PATCH 2/2] rv: Fix wrong type cast in reactors_show() and monitor_reactor_show() Nam Cao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).