Skip to contentSkip to conversation

ftrace: Fix MATCH_END_ONLY filter matching

PATCH In review Li Zefan 4 participants 19 messages Dec 11, 2009 – Dec 14, 2009

Subsystems (2)
lore
Share & tools
lore

Li Zefan

author
#0 lore raw

PATCH 1/4 ftrace: Fix MATCH_END_ONLY filter matching

For '*foo' pattern, we should allow any string ending with 'foo', but ftrace filter incorrectly disallows strings like bar_foo_foo:

  # echo '*io' > set_ftrace_filter
  # cat set_ftrace_filter | grep 'req_bio_endio'
  # cat available_filter_functions | grep 'req_bio_endio'
  req_bio_endio
---
 kernel/trace/ftrace.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
kernel/trace/ftrace.c +3 -3
index e51a1bc..613dfb1 100644
@@ -1690,7 +1690,7 @@ ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
 static int ftrace_match(char *str, char *regex, int len, int type)
 {
 	int matched = 0;
-	char *ptr;
+	int slen;
 
 	switch (type) {
 	case MATCH_FULL:
@@ -1706,8 +1706,8 @@ static int ftrace_match(char *str, char *regex, int len, int type)
 			matched = 1;
 		break;
 	case MATCH_END_ONLY:
-		ptr = strstr(str, regex);
-		if (ptr && (ptr[len] == 0))
+		slen = strlen(str);
+		if (slen >= len && memcpy(str + slen - len, regex, len))
 			matched = 1;
 		break;
 	}
Signature · 1 line
1.6.3
author
#1 lore raw

PATCH 2/4 tracing/filters: Fix MATCH_FULL filter matching for PTR_STRING

MATCH_FULL matching for PTR_STRING is not working correctly:

  # echo 'func == vt' > events/bkl/lock_kernel/filter
  # echo 1 > events/bkl/lock_kernel/enable
  ...
  # cat trace
   Xorg-1484  [000]  1973.392586: lock_kernel: ... func=vt_ioctl()
    gpm-1402  [001]  1974.027740: lock_kernel: ... func=vt_ioctl()

We should pass to regex.match(..., len) the length (including '\0') of the source string instead of the length of the pattern string.

---
 kernel/trace/trace_events_filter.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
kernel/trace/trace_events_filter.c +3 -4
index 50504cb..c8eb1c0 100644
@@ -211,8 +211,9 @@ static int filter_pred_pchar(struct filter_pred *pred, void *event,
 {
 	char **addr = (char **)(event + pred->offset);
 	int cmp, match;
+	int len = strlen(*addr) + 1;	/* including tailing '\0' */
 
-	cmp = pred->regex.match(*addr, &pred->regex, pred->regex.field_len);
+	cmp = pred->regex.match(*addr, &pred->regex, len);
 
 	match = cmp ^ pred->not;
 
@@ -781,10 +782,8 @@ static int filter_add_pred(struct filter_parse_state *ps,
 			pred->regex.field_len = field->size;
 		} else if (field->filter_type == FILTER_DYN_STRING)
 			fn = filter_pred_strloc;
-		else {
+		else
 			fn = filter_pred_pchar;
-			pred->regex.field_len = strlen(pred->regex.pattern);
-		}
 	} else {
 		if (field->is_signed)
 			ret = strict_strtoll(pred->regex.pattern, 0, &val);
Signature · 1 line
1.6.3
author
#6 lore raw

PATCH 3/4 tracing/filters: Fix MATCH_FRONT_ONLY filter matching

In reply to Li Zefan

MATCH_FRONT_ONLY matching works exactly as MATCH_FULL.

We should pass the length of the pattern to strncmp().

---
 kernel/trace/trace_events_filter.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/trace/trace_events_filter.c +1 -1
index c8eb1c0..bd492ce 100644
@@ -262,7 +262,7 @@ static int regex_match_full(char *str, struct regex *r, int len)
 
 static int regex_match_front(char *str, struct regex *r, int len)
 {
-	if (strncmp(str, r->pattern, len) == 0)
+	if (strncmp(str, r->pattern, r->len) == 0)
 		return 1;
 	return 0;
 }
Signature · 1 line
1.6.3
author
#10 lore raw

PATCH 4/4 tracing/filters: Fix MATCH_EBD_ONLY filter matching

In reply to Li Zefan

For '*foo' pattern, we should allow any string ending with 'foo', but tracing filter incorrectly disallows strings matching regex expression ".*foo.*foo".

---
 kernel/trace/trace_events_filter.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
kernel/trace/trace_events_filter.c +3 -2
index bd492ce..9f96339 100644
@@ -276,9 +276,10 @@ static int regex_match_middle(char *str, struct regex *r, int len)
 
 static int regex_match_end(char *str, struct regex *r, int len)
 {
-	char *ptr = strstr(str, r->pattern);
+	int strlen = len - 1;
 
-	if (ptr && (ptr[r->len] == 0))
+	if (strlen >= r->len &&
+	    !memcmp(str + strlen - r->len, r->pattern, r->len))
 		return 1;
 	return 0;
 }
Signature · 1 line
1.6.3
#7 lore raw

Re: [PATCH 3/4] tracing/filters: Fix MATCH_FRONT_ONLY filter matching

In reply to Li Zefan
Quoted 14 lines · Li Zefan

MATCH_FRONT_ONLY matching works exactly as MATCH_FULL.

We should pass the length of the pattern to strncmp().

---
 kernel/trace/trace_events_filter.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/trace/trace_events_filter.c +0 -0
index c8eb1c0..bd492ce 100644

kernel/trace/trace_events_filter.c static int regex_match_full(char *str, struct regex *r, int len) · line 268

Quoted 8 lines
kernel/trace/trace_events_filter.c +1 -1
@@ -262,7 +262,7 @@ static int regex_match_full(char *str, struct regex *r, int len)
 
 static int regex_match_front(char *str, struct regex *r, int len)
 {
-	if (strncmp(str, r->pattern, len) == 0)
+	if (strncmp(str, r->pattern, r->len) == 0)
 		return 1;
 	return 0;
 }

But it was working before the fix to match full. The MATCH_FULL fix is nice but it also brings a new bug that is fixed in this patch.

Could you perhaps rework the MATCH_FULL fix to avoid that?

Thanks.

#2 lore raw

Re: [PATCH 2/4] tracing/filters: Fix MATCH_FULL filter matching for PTR_STRING

In reply to Li Zefan
Quoted 9 lines · Li Zefan

MATCH_FULL matching for PTR_STRING is not working correctly:

  # echo 'func == vt' > events/bkl/lock_kernel/filter
  # echo 1 > events/bkl/lock_kernel/enable
  ...
  # cat trace
   Xorg-1484  [000]  1973.392586: lock_kernel: ... func=vt_ioctl()
    gpm-1402  [001]  1974.027740: lock_kernel: ... func=vt_ioctl()
Quoted 4 lines

We should pass to regex.match(..., len) the length (including '\0') of the source string instead of the length of the pattern string.

This patch is cool but it seems to also break middle and end matching (at least what was working with end matching :)

I know you fix that in the subsequent patches, but please avoid that. A fix should not bring another known bug, event if it's fixed in the same batch.

Thanks.

#13 lore raw
In reply to Li Zefan
Quoted 10 lines · Li Zefan

For '*foo' pattern, we should allow any string ending with 'foo', but ftrace filter incorrectly disallows strings like bar_foo_foo:

  # echo '*io' > set_ftrace_filter
  # cat set_ftrace_filter | grep 'req_bio_endio'
  # cat available_filter_functions | grep 'req_bio_endio'
  req_bio_endio

I have pending fixes in queue, I'm taking this one too.

Thanks!

#14 lore raw
In reply to Li Zefan

2009/12/11 Li Zefan <[email protected]>:

Quoted 3 lines

For '*foo' pattern, we should allow any string ending with 'foo', but ftrace filter incorrectly disallows strings like bar_foo_foo:

[...]

Quoted 6 lines

       case MATCH_END_ONLY:
-               ptr = strstr(str, regex);
-               if (ptr && (ptr[len] == 0))
+               slen = strlen(str);
+               if (slen >= len && memcpy(str + slen - len, regex, len)) matched = 1;

Shouldn't that be memcmp() == 0? I don't see how memcpy might serve as a compare operator.

Fred.

#15 lore raw
Quoted 12 lines · Frederic Riss

2009/12/11 Li Zefan <[email protected]>:

Quoted 3 lines

For '*foo' pattern, we should allow any string ending with 'foo', but ftrace filter incorrectly disallows strings like bar_foo_foo:

[...]

Quoted 6 lines

       case MATCH_END_ONLY:
-               ptr = strstr(str, regex);
-               if (ptr && (ptr[len] == 0))
+               slen = strlen(str);
+               if (slen >= len && memcpy(str + slen - len, regex, len)) matched = 1;

Quoted 4 lines

Shouldn't that be memcmp() == 0? I don't see how memcpy might serve as a compare operator.

Fred.

Oh you're right, I reviewed it too quickly. Perhaps the star's alignment made it possible for only one Frederic to review it the right way...

maintainer
#16 lore raw
Quoted 16 lines · Frederic Weisbecker
Quoted 15 lines · Frederic Riss

2009/12/11 Li Zefan <[email protected]>:

Quoted 3 lines

For '*foo' pattern, we should allow any string ending with 'foo', but ftrace filter incorrectly disallows strings like bar_foo_foo:

[...]

kernel/trace/ftrace.c static int ftrace_match(char *str, char *regex, int len, int type) · line 1711

Quoted 6 lines

case MATCH_END_ONLY:

kernel/trace/ftrace.c +2 -2
-               ptr = strstr(str, regex);
-               if (ptr && (ptr[len] == 0))
+               slen = strlen(str);
+               if (slen >= len && memcpy(str + slen - len, regex, len))
                       matched = 1;

Shouldn't that be memcmp() == 0? I don't see how memcpy might serve as a compare operator.

Quoted 7 lines
Quoted 1 line

Fred.

Oh you're right, I reviewed it too quickly. Perhaps the star's alignment made it possible for only one Frederic to review it the right way...

The stars are still fuzzy to me. How does memcpy work here?

-- Steve

maintainer
#11 lore raw

Re: [PATCH 4/4] tracing/filters: Fix MATCH_EBD_ONLY filter matching

In reply to Li Zefan
Quoted 14 lines · Li Zefan

For '*foo' pattern, we should allow any string ending with 'foo', but tracing filter incorrectly disallows strings matching regex expression ".*foo.*foo".

---
 kernel/trace/trace_events_filter.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
kernel/trace/trace_events_filter.c +0 -0
index bd492ce..9f96339 100644

kernel/trace/trace_events_filter.c static int regex_match_middle(char *str, struct regex *r, int len) · line 282

Quoted 9 lines
kernel/trace/trace_events_filter.c +3 -2
@@ -276,9 +276,10 @@ static int regex_match_middle(char *str, struct regex *r, int len)
 
 static int regex_match_end(char *str, struct regex *r, int len)
 {
-	char *ptr = strstr(str, r->pattern);
+	int strlen = len - 1;
 
-	if (ptr && (ptr[r->len] == 0))
+	if (strlen >= r->len &&
+	    !memcmp(str + strlen - r->len, r->pattern, r->len))

Please use memcmp() == 0, I've seen too many bugs with !*cmp as well as with *cmp, because humans tend to think instinctively when reading this that ! is not a match. With "== 0" we think that "==" is a match and
"!=" is a miss.

Thanks,

-- Steve

Quoted 3 lines
kernel/trace/trace_events_filter.c +0 -0
 		return 1;
 	return 0;
 }
#17 lore raw
In reply to Steven Rostedt
Quoted 20 lines · Steven Rostedt
Quoted 19 lines · Frederic Weisbecker
Quoted 16 lines · Frederic Riss

2009/12/11 Li Zefan <[email protected]>:

Quoted 3 lines

For '*foo' pattern, we should allow any string ending with 'foo', but ftrace filter incorrectly disallows strings like bar_foo_foo:

[...]

kernel/trace/ftrace.c static int ftrace_match(char *str, char *regex, int len, int type) · line 1711

Quoted 6 lines

case MATCH_END_ONLY:

kernel/trace/ftrace.c +2 -2
-               ptr = strstr(str, regex);
-               if (ptr && (ptr[len] == 0))
+               slen = strlen(str);
+               if (slen >= len && memcpy(str + slen - len, regex, len))
                       matched = 1;

Shouldn't that be memcmp() == 0? I don't see how memcpy might serve as a compare operator.

Fred.

Quoted 6 lines
Quoted 4 lines

Oh you're right, I reviewed it too quickly. Perhaps the star's alignment made it possible for only one Frederic to review it the right way...

The stars are still fuzzy to me. How does memcpy work here?

It doesn't, I guess Li intended to use memcmp.

author
#18 lore raw
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 3 lines

For '*foo' pattern, we should allow any string ending with 'foo', but ftrace filter incorrectly disallows strings like bar_foo_foo:

[...]

kernel/trace/ftrace.c static int ftrace_match(char *str, char *regex, int len, int type) · line 1711

Quoted 6 lines

case MATCH_END_ONLY:

kernel/trace/ftrace.c +2 -2
-               ptr = strstr(str, regex);
-               if (ptr && (ptr[len] == 0))
+               slen = strlen(str);
+               if (slen >= len && memcpy(str + slen - len, regex, len))
                       matched = 1;

Shouldn't that be memcmp() == 0? I don't see how memcpy might serve as a compare operator.

Fred.

Quoted 9 lines
Quoted 5 lines
Quoted 4 lines

Oh you're right, I reviewed it too quickly. Perhaps the star's alignment made it possible for only one Frederic to review it the right way...

The stars are still fuzzy to me. How does memcpy work here?

It doesn't, I guess Li intended to use memcmp.

Oops, I forgot to fix this before sending the patch out.. :(

author
#12 lore raw

Re: [PATCH 4/4] tracing/filters: Fix MATCH_EBD_ONLY filter matching

In reply to Steven Rostedt
Quoted 5 lines
Quoted 5 lines

static int regex_match_end(char *str, struct regex *r, int len) {

kernel/trace/trace_events_filter.c +1 -1
-	char *ptr = strstr(str, r->pattern);
+	int strlen = len - 1;
Quoted 9 lines

kernel/trace/trace_events_filter.c static int regex_match_middle(char *str, struct regex *r, int len) · line 282

Quoted 3 lines
kernel/trace/trace_events_filter.c +2 -1
-	if (ptr && (ptr[r->len] == 0))
+	if (strlen >= r->len &&
+	    !memcmp(str + strlen - r->len, r->pattern, r->len))

Please use memcmp() == 0, I've seen too many bugs with !*cmp as well as with *cmp, because humans tend to think instinctively when reading this that ! is not a match. With "== 0" we think that "==" is a match and
"!=" is a miss.

Though I sometimes don't care much about this, I do think memcmp() == 0 is better.

author
#3 lore raw

Re: [PATCH 2/4] tracing/filters: Fix MATCH_FULL filter matching for PTR_STRING

In reply to Frederic Weisbecker
Quoted 10 lines · Frederic Weisbecker
Quoted 9 lines · Li Zefan

MATCH_FULL matching for PTR_STRING is not working correctly:

  # echo 'func == vt' > events/bkl/lock_kernel/filter
  # echo 1 > events/bkl/lock_kernel/enable
  ...
  # cat trace
   Xorg-1484  [000]  1973.392586: lock_kernel: ... func=vt_ioctl()
    gpm-1402  [001]  1974.027740: lock_kernel: ... func=vt_ioctl()
Quoted 8 lines
Quoted 4 lines

We should pass to regex.match(..., len) the length (including '\0') of the source string instead of the length of the pattern string.

This patch is cool but it seems to also break middle and end matching (at least what was working with end matching :)

No, it won't, unless I miss something.

I changed what value ptr_string passes the length param to match(), but this param is not used in match_middle and match_end.

It does break match_front for ptr_string, but that's because the mixture of 2 bugs happened to make things right. I can sort this out by reordering the 2 patches.

Quoted 4 lines

I know you fix that in the subsequent patches, but please avoid that. A fix should not bring another known bug, event if it's fixed in the same batch.

author
#8 lore raw

Re: [PATCH 3/4] tracing/filters: Fix MATCH_FRONT_ONLY filter matching

In reply to Frederic Weisbecker
Quoted 16 lines · Frederic Weisbecker
Quoted 15 lines · Li Zefan

MATCH_FRONT_ONLY matching works exactly as MATCH_FULL.

We should pass the length of the pattern to strncmp().

---
 kernel/trace/trace_events_filter.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/trace/trace_events_filter.c +0 -0
index c8eb1c0..bd492ce 100644
@@ -262,7 +262,7 @@ static int regex_match_full(char *str, struct regex *r, int len)
 
Quoted 10 lines

kernel/trace/trace_events_filter.c static int regex_match_full(char *str, struct regex *r, int len) · line 268

Quoted 7 lines
kernel/trace/trace_events_filter.c +1 -1
 static int regex_match_front(char *str, struct regex *r, int len)
 {
-	if (strncmp(str, r->pattern, len) == 0)
+	if (strncmp(str, r->pattern, r->len) == 0)
 		return 1;
 	return 0;
 }

But it was working before the fix to match full.

No, it was not working for dyn_string and static_string. And as I explained it happened to work for ptr_string by the mixture of 2 bugs.

Quoted 3 lines

The MATCH_FULL fix is nice but it also brings a new bug that is fixed in this patch.

#4 lore raw

Re: [PATCH 2/4] tracing/filters: Fix MATCH_FULL filter matching for PTR_STRING

In reply to Li Zefan
Quoted 20 lines · Li Zefan
Quoted 10 lines · Frederic Weisbecker
Quoted 9 lines · Li Zefan

MATCH_FULL matching for PTR_STRING is not working correctly:

  # echo 'func == vt' > events/bkl/lock_kernel/filter
  # echo 1 > events/bkl/lock_kernel/enable
  ...
  # cat trace
   Xorg-1484  [000]  1973.392586: lock_kernel: ... func=vt_ioctl()
    gpm-1402  [001]  1974.027740: lock_kernel: ... func=vt_ioctl()
Quoted 8 lines
Quoted 4 lines

We should pass to regex.match(..., len) the length (including '\0') of the source string instead of the length of the pattern string.

This patch is cool but it seems to also break middle and end matching (at least what was working with end matching :)

Quoted 8 lines

No, it won't, unless I miss something.

I changed what value ptr_string passes the length param to match(), but this param is not used in match_middle and match_end.

It does break match_front for ptr_string, but that's because the mixture of 2 bugs happened to make things right. I can sort this out by reordering the 2 patches.

I made a mistake. I meant it breaks match front. As previously it was taking the pattern length as a parameter and now it takes the string length to compare.

But well, the things were so broken already that I'm not sure we need bisectable deltas... :)

#9 lore raw

Re: [PATCH 3/4] tracing/filters: Fix MATCH_FRONT_ONLY filter matching

In reply to Li Zefan
Quoted 28 lines · Li Zefan
Quoted 16 lines · Frederic Weisbecker
Quoted 15 lines · Li Zefan

MATCH_FRONT_ONLY matching works exactly as MATCH_FULL.

We should pass the length of the pattern to strncmp().

---
 kernel/trace/trace_events_filter.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/trace/trace_events_filter.c +0 -0
index c8eb1c0..bd492ce 100644
@@ -262,7 +262,7 @@ static int regex_match_full(char *str, struct regex *r, int len)
 
Quoted 10 lines

kernel/trace/trace_events_filter.c static int regex_match_full(char *str, struct regex *r, int len) · line 268

Quoted 7 lines
kernel/trace/trace_events_filter.c +1 -1
 static int regex_match_front(char *str, struct regex *r, int len)
 {
-	if (strncmp(str, r->pattern, len) == 0)
+	if (strncmp(str, r->pattern, r->len) == 0)
 		return 1;
 	return 0;
 }

But it was working before the fix to match full.

Quoted 3 lines

No, it was not working for dyn_string and static_string. And as I explained it happened to work for ptr_string by the mixture of 2 bugs.

Ah, you're right.

#5 lore raw

Re: [PATCH 2/4] tracing/filters: Fix MATCH_FULL filter matching for PTR_STRING

In reply to Li Zefan
Quoted 20 lines · Li Zefan
Quoted 10 lines · Frederic Weisbecker
Quoted 9 lines · Li Zefan

MATCH_FULL matching for PTR_STRING is not working correctly:

  # echo 'func == vt' > events/bkl/lock_kernel/filter
  # echo 1 > events/bkl/lock_kernel/enable
  ...
  # cat trace
   Xorg-1484  [000]  1973.392586: lock_kernel: ... func=vt_ioctl()
    gpm-1402  [001]  1974.027740: lock_kernel: ... func=vt_ioctl()
Quoted 8 lines
Quoted 4 lines

We should pass to regex.match(..., len) the length (including '\0') of the source string instead of the length of the pattern string.

This patch is cool but it seems to also break middle and end matching (at least what was working with end matching :)

Quoted 8 lines

No, it won't, unless I miss something.

I changed what value ptr_string passes the length param to match(), but this param is not used in match_middle and match_end.

It does break match_front for ptr_string, but that's because the mixture of 2 bugs happened to make things right. I can sort this out by reordering the 2 patches.

No, no need to. I was just confused :)