Skip to contentSkip to conversation

perf/core: Allow global address filtering for kernel code

PATCH In review Andi Kleen 3 participants 4 messages Feb 3, 2017 – Feb 9, 2017 perf events

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/2 perf/core: Allow global address filtering for kernel code

From: Andi Kleen <[email protected]>

The address filter code disallows filtering for per-cpu events, because it would require dynamically changing user address filters in context switches.

For the special case of filtering on kernel code only, we can allow it, as the kernel code always stays at the same addresses.

So move the check for the global filter in the parser code and only check after we know there are user space filters.

In addition also handle this case when applying the filter.

---
 kernel/events/core.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
kernel/events/core.c +17 -9
index 110b38a58493..15fd58177e73 100644
@@ -8016,6 +8016,9 @@ static void perf_event_addr_filters_apply(struct perf_event *event)
 	if (task == TASK_TOMBSTONE)
 		return;
 
+	if (!event->ctx->task)
+		goto restart;
+
 	mm = get_task_mm(event->ctx->task);
 	if (!mm)
 		goto restart;
@@ -8190,6 +8193,20 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
 				goto fail;
 
 			if (!kernel) {
+				/*
+				 * For now, we only support user
+				 * filtering in per-task events; doing
+				 * so for CPU-wide events requires
+				 * additional context switching
+				 * trickery, since same object code
+				 * will be mapped at different virtual
+				 * addresses in different processes.
+				 */
+				ret = -EOPNOTSUPP;
+				if (!event->ctx->task)
+					goto fail;
+
+				ret = -EINVAL;
 				if (!filename)
 					goto fail;
 
@@ -8247,15 +8264,6 @@ perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
 	if (WARN_ON_ONCE(event->parent))
 		return -EINVAL;
 
-	/*
-	 * For now, we only support filtering in per-task events; doing so
-	 * for CPU-wide events requires additional context switching trickery,
-	 * since same object code will be mapped at different virtual
-	 * addresses in different processes.
-	 */
-	if (!event->ctx->task)
-		return -EOPNOTSUPP;
-
 	ret = perf_event_parse_addr_filter(event, filter_str, &filters);
 	if (ret)
 		return ret;
Signature · 1 line
2.9.3
author
#1 lore raw

PATCH 2/2 perf tools: Support end symbols with no size for filters

From: Andi Kleen <[email protected]>

Currently a filter like
--filter "filter _text / _end"
doesn't work because _end doesn't have a size. The filter resolution always wants to use the end of the function as end.

Allow this case by assuming the filter just spawns to the start of the end symbol when there is no size. This makes the above useful filter work.

---
 tools/perf/util/auxtrace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/auxtrace.c +6 -1
index c5a6e0b12452..4c5ccb2b1298 100644
@@ -1840,7 +1840,12 @@ static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
 		if (err)
 			return err;
 		filt->size = start + size - filt->addr;
-		no_size = !!size;
+		/*
+		 * When to has no size assume it's a end symbol.
+		 * This allows filters like _text / _end
+		 */
+		if (size == 0)
+			filt->size = start - filt->addr;
 	}
 
 	/* The very last symbol in kallsyms does not imply a particular size */
Signature · 1 line
2.9.3
reviewer
#2 lore raw

Re: [PATCH 2/2] perf tools: Support end symbols with no size for filters

Quoted 23 lines · Andi Kleen

From: Andi Kleen <[email protected]>

Currently a filter like
--filter "filter _text / _end"
doesn't work because _end doesn't have a size. The filter resolution always wants to use the end of the function as end.

Allow this case by assuming the filter just spawns to the start of the end symbol when there is no size. This makes the above useful filter work.

---
 tools/perf/util/auxtrace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/auxtrace.c +0 -0
index c5a6e0b12452..4c5ccb2b1298 100644

tools/perf/util/auxtrace.c static int addr_filter__resolve_kernel_syms(struct addr_filter *filt) · line 1843

Quoted 4 lines
tools/perf/util/auxtrace.c +0 -1
@@ -1840,7 +1840,12 @@ static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
 		if (err)
 			return err;
 		filt->size = start + size - filt->addr;
-		no_size = !!size;

Erk! Isn't the logic is the wrong way around here. Sorry! i.e. should be:

tools/perf/util/auxtrace.c +2 -2
index c5a6e0b12452..78bd632f144d 100644
@@ -1826,7 +1826,7 @@ static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
 		filt->addr = start;
 		if (filt->range && !filt->size && !filt->sym_to) {
 			filt->size = size;
-			no_size = !!size;
+			no_size = !size;
 		}
 	}
 
@@ -1840,7 +1840,7 @@ static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
 		if (err)
 			return err;
 		filt->size = start + size - filt->addr;
-		no_size = !!size;
+		no_size = !size;
 	}
 
 	/* The very last symbol in kallsyms does not imply a particular size */
Quoted 10 lines
tools/perf/util/auxtrace.c +6 -0
+		/*
+		 * When to has no size assume it's a end symbol.
+		 * This allows filters like _text / _end
+		 */
+		if (size == 0)
+			filt->size = start - filt->addr;
 	}
 
 	/* The very last symbol in kallsyms does not imply a particular size */
#3 lore raw

Re: [PATCH 2/2] perf tools: Support end symbols with no size for filters

Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +0 -1
-		no_size = !!size;

Erk! Isn't the logic is the wrong way around here. Sorry! i.e. should be:

Yes it works with that change too.

Quoted 22 lines
tools/perf/util/auxtrace.c +2 -2
index c5a6e0b12452..78bd632f144d 100644
@@ -1826,7 +1826,7 @@ static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
 		filt->addr = start;
 		if (filt->range && !filt->size && !filt->sym_to) {
 			filt->size = size;
-			no_size = !!size;
+			no_size = !size;
 		}
 	}
 
@@ -1840,7 +1840,7 @@ static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
 		if (err)
 			return err;
 		filt->size = start + size - filt->addr;
-		no_size = !!size;
+		no_size = !size;
 	}