2019-12-11 15:37:17

by Steven Rostedt

[permalink] [raw]
Subject: Re: ftrace histogram sorting broken on BE architecures

On Wed, 11 Dec 2019 13:33:16 +0100
Sven Schnelle <[email protected]> wrote:

> Hi List,

Hi Sven,

>
> i was looking into a ftracetest failure on s390:
>
> # ./ftracetest test.d/trigger/trigger-hist.tc
> === Ftrace unit tests ===
> [1] event trigger - test histogram trigger [FAIL]
> [2] (instance) event trigger - test histogram trigger [FAIL]
>
> from the -vvv log: ++ fail 'sort param on sched_process_fork did not work'
>
> # cat events/sched/sched_process_fork/hist
>
> # event histogram
> #
> # trigger info: hist:keys=parent_pid,child_pid:vals=hitcount:sort=child_pid:size=2048 [active]
> #
>
> { parent_pid: 1406, child_pid: 1428 } hitcount: 1
> { parent_pid: 1406, child_pid: 1430 } hitcount: 1
> { parent_pid: 1406, child_pid: 1427 } hitcount: 1
> { parent_pid: 1406, child_pid: 1432 } hitcount: 1
> { parent_pid: 1406, child_pid: 1431 } hitcount: 1
> { parent_pid: 1406, child_pid: 1429 } hitcount: 1
>
> So the test is right, the entries are not sorted. After digging into the
> ftrace code i noticed that integer values always get extended to 64 bit
> in event_hist_trigger(), but cmp_entries_key() from tracing_map.c uses the
> type of the field (which is a pid_t, and therefore 4 bytes).
>
> On Little Endian this doesn't hurt, but on BE s390 this makes the compare
> function compare 4 zero bytes, which is the reason why sorting doesn't
> work. As a test i forced the compare function used in cmp_entries_key() to
> tracing_map_cmp_s64(), which made the ftrace tests pass.
>
> I also tested this on 64 bit parisc with the same results, so the architecture
> doesn't seem make a difference (besides LE vs. BE)
>
> Any thoughts on how to fix this? I'm not sure whether i fully understand the
> ftrace maps... ;-)

Your analysis makes sense. I'll take a deeper look at it.

Thanks for reporting it!

-- Steve


2019-12-11 16:11:06

by Steven Rostedt

[permalink] [raw]
Subject: Re: ftrace histogram sorting broken on BE architecures

On Wed, 11 Dec 2019 10:35:57 -0500
Steven Rostedt <[email protected]> wrote:

> > Any thoughts on how to fix this? I'm not sure whether i fully understand the
> > ftrace maps... ;-)
>
> Your analysis makes sense. I'll take a deeper look at it.

Sven,

Does this patch fix it for you?

Tom,

Correct me if I'm wrong, from what I can tell, all sums and keys are
u64 unless they are a string. Thus, I believe this patch should not
have any issues.

-- Steve

diff --git a/kernel/trace/tracing_map.c b/kernel/trace/tracing_map.c
index 9a1c22310323..9e31bfc818ff 100644
--- a/kernel/trace/tracing_map.c
+++ b/kernel/trace/tracing_map.c
@@ -148,8 +148,8 @@ static int tracing_map_cmp_atomic64(void *val_a, void *val_b)
#define DEFINE_TRACING_MAP_CMP_FN(type) \
static int tracing_map_cmp_##type(void *val_a, void *val_b) \
{ \
- type a = *(type *)val_a; \
- type b = *(type *)val_b; \
+ type a = (type)(*(u64 *)val_a); \
+ type b = (type)(*(u64 *)val_b); \
\
return (a > b) ? 1 : ((a < b) ? -1 : 0); \
}

2019-12-11 16:38:34

by Tom Zanussi

[permalink] [raw]
Subject: Re: ftrace histogram sorting broken on BE architecures

Hi Steve, Sven,

On Wed, 2019-12-11 at 11:09 -0500, Steven Rostedt wrote:
> On Wed, 11 Dec 2019 10:35:57 -0500
> Steven Rostedt <[email protected]> wrote:
>
> > > Any thoughts on how to fix this? I'm not sure whether i fully
> > > understand the
> > > ftrace maps... ;-)
> >
> > Your analysis makes sense. I'll take a deeper look at it.
>
> Sven,
>
> Does this patch fix it for you?
>
> Tom,
>
> Correct me if I'm wrong, from what I can tell, all sums and keys are
> u64 unless they are a string. Thus, I believe this patch should not
> have any issues.

The sums are u64, but the keys may not be. I'll take a look and see,
but I'm out today and won't be able to look into it until tomorrow, if
that's ok.

Tom

>
> -- Steve
>
> diff --git a/kernel/trace/tracing_map.c b/kernel/trace/tracing_map.c
> index 9a1c22310323..9e31bfc818ff 100644
> --- a/kernel/trace/tracing_map.c
> +++ b/kernel/trace/tracing_map.c
> @@ -148,8 +148,8 @@ static int tracing_map_cmp_atomic64(void *val_a,
> void *val_b)
> #define DEFINE_TRACING_MAP_CMP_FN(type)
> \
> static int tracing_map_cmp_##type(void *val_a, void *val_b)
> \
> {
> \
> - type a = *(type *)val_a;
> \
> - type b = *(type *)val_b;
> \
> + type a = (type)(*(u64 *)val_a);
> \
> + type b = (type)(*(u64 *)val_b);
> \
>
> \
> return (a > b) ? 1 : ((a < b) ? -1 : 0);
> \
> }

2019-12-11 17:02:34

by Steven Rostedt

[permalink] [raw]
Subject: Re: ftrace histogram sorting broken on BE architecures

On Wed, 11 Dec 2019 10:37:18 -0600
Tom Zanussi <[email protected]> wrote:

> > Tom,
> >
> > Correct me if I'm wrong, from what I can tell, all sums and keys are
> > u64 unless they are a string. Thus, I believe this patch should not
> > have any issues.
>
> The sums are u64, but the keys may not be. I'll take a look and see,

Are they? I see in create_key_field:

key_size = ALIGN(key_size, sizeof(u64));

Which to me seems that we'll have nothing smaller than sizeof(u64).

> but I'm out today and won't be able to look into it until tomorrow, if
> that's ok.

No rush.

I'll start the testing of this patch if you come back and say its
fine ;-) That takes a full day.

-- Steve

2019-12-11 18:56:41

by Sven Schnelle

[permalink] [raw]
Subject: Re: ftrace histogram sorting broken on BE architecures

Hi Steven,

On Wed, Dec 11, 2019 at 11:09:59AM -0500, Steven Rostedt wrote:
> On Wed, 11 Dec 2019 10:35:57 -0500
> Steven Rostedt <[email protected]> wrote:
>
> > > Any thoughts on how to fix this? I'm not sure whether i fully understand the
> > > ftrace maps... ;-)
> >
> > Your analysis makes sense. I'll take a deeper look at it.
>
> Does this patch fix it for you?

Yes, it does. Thanks for looking into this.

> Correct me if I'm wrong, from what I can tell, all sums and keys are
> u64 unless they are a string. Thus, I believe this patch should not
> have any issues.

I'll retest if Tom comes up with another patch.

Thanks,
Sven

2019-12-11 19:27:54

by Tom Zanussi

[permalink] [raw]
Subject: Re: ftrace histogram sorting broken on BE architecures

Hi Steve,

On Wed, 2019-12-11 at 12:00 -0500, Steven Rostedt wrote:
> On Wed, 11 Dec 2019 10:37:18 -0600
> Tom Zanussi <[email protected]> wrote:
>
> > > Tom,
> > >
> > > Correct me if I'm wrong, from what I can tell, all sums and keys
> > > are
> > > u64 unless they are a string. Thus, I believe this patch should
> > > not
> > > have any issues.
> >
> > The sums are u64, but the keys may not be. I'll take a look and
> > see,
>
> Are they? I see in create_key_field:
>
> key_size = ALIGN(key_size, sizeof(u64));
>
> Which to me seems that we'll have nothing smaller than sizeof(u64).
>

Yeah, that makes them effectively u64 in this case, so I'd agree.

Tom

2019-12-12 18:08:35

by Steven Rostedt

[permalink] [raw]
Subject: Re: ftrace histogram sorting broken on BE architecures

On Wed, 11 Dec 2019 13:26:03 -0600
Tom Zanussi <[email protected]> wrote:

> > Are they? I see in create_key_field:
> >
> > key_size = ALIGN(key_size, sizeof(u64));
> >
> > Which to me seems that we'll have nothing smaller than sizeof(u64).
> >
>
> Yeah, that makes them effectively u64 in this case, so I'd agree.

Hi Tom,

Does this mean it's good to go? It just passed my test suite, and I can
send it to Linus now.

-- Steve

2019-12-12 19:17:29

by Tom Zanussi

[permalink] [raw]
Subject: Re: ftrace histogram sorting broken on BE architecures

Hi Steve,

On Thu, 2019-12-12 at 13:07 -0500, Steven Rostedt wrote:
> On Wed, 11 Dec 2019 13:26:03 -0600
> Tom Zanussi <[email protected]> wrote:
>
> > > Are they? I see in create_key_field:
> > >
> > > key_size = ALIGN(key_size, sizeof(u64));
> > >
> > > Which to me seems that we'll have nothing smaller than
> > > sizeof(u64).
> > >
> >
> > Yeah, that makes them effectively u64 in this case, so I'd agree.
>
> Hi Tom,
>
> Does this mean it's good to go? It just passed my test suite, and I
> can
> send it to Linus now.
>

Yes, I think so. I'll go and ack the patch.

Thanks,

Tom

> -- Steve

2019-12-12 19:18:48

by Tom Zanussi

[permalink] [raw]
Subject: Re: ftrace histogram sorting broken on BE architecures

On Wed, 2019-12-11 at 11:09 -0500, Steven Rostedt wrote:
> On Wed, 11 Dec 2019 10:35:57 -0500
> Steven Rostedt <[email protected]> wrote:
>
> > > Any thoughts on how to fix this? I'm not sure whether i fully
> > > understand the
> > > ftrace maps... ;-)
> >
> > Your analysis makes sense. I'll take a deeper look at it.
>
> Sven,
>
> Does this patch fix it for you?
>
> Tom,
>
> Correct me if I'm wrong, from what I can tell, all sums and keys are
> u64 unless they are a string. Thus, I believe this patch should not
> have any issues.
>
> -- Steve
>
> diff --git a/kernel/trace/tracing_map.c b/kernel/trace/tracing_map.c
> index 9a1c22310323..9e31bfc818ff 100644
> --- a/kernel/trace/tracing_map.c
> +++ b/kernel/trace/tracing_map.c
> @@ -148,8 +148,8 @@ static int tracing_map_cmp_atomic64(void *val_a,
> void *val_b)
> #define DEFINE_TRACING_MAP_CMP_FN(type)
> \
> static int tracing_map_cmp_##type(void *val_a, void *val_b)
> \
> {
> \
> - type a = *(type *)val_a;
> \
> - type b = *(type *)val_b;
> \
> + type a = (type)(*(u64 *)val_a);
> \
> + type b = (type)(*(u64 *)val_b);
> \
>
> \
> return (a > b) ? 1 : ((a < b) ? -1 : 0);
> \
> }

Acked-by: Tom Zanussi <[email protected]>