Skip to contentSkip to conversation

perf, pt: Improve data loss

PATCH Applied Alexander Shishkin 3 participants 6 messages Apr 14, 2021 – Apr 26, 2021 perf events

lore
Share & tools
lore

Alexander Shishkin

author reviewer
#0 lore raw

cover perf, pt: Improve data loss

Hi guys,

v1: fixed whitespace damage in 1/2 and applied Adrian's comment to 2/2.

There is a problem between the PT driver and the AUX allocator that results in smaller buffers consisting of 2 high-order regions, which also means only 2 possibilities of where PMI gets generated and where tracing stops.

This is not good enough for double buffering: when we get a PMI mid-buffer, we update the ->aux_head etc and immediately start a new transaction while observing ->aux_tail to still be zero, which makes the PT driver put a stop bit at the end of the buffer. However quick userspace is to update the ->aux_tail, that second transaction/PERF_RECORD_AUX ends up truncated.

The proposed solution here is to set up attr.aux_watermark to a quarter of the buffer. Unfortunately, at the moment, the PT driver is not equipped to deal with aux_watermark that's smaller than the AUX allocation order. I could fix this in the driver itself, but, seeing as it's the only PMU that actually uses the 'order' from AUX allocations, I'd rather fix the allocator instead, which is done in patch 1/2.

Patch 2/2 could be replaced by instead changing the in-kernel aux_watermark default, but that may interfere with PMU drivers that don't ignore said watermark / handle->wakeup (afaict, that's only arm_spe).

Alexander Shishkin (2):
perf: Cap allocation order at aux_watermark
perf intel-pt: Use aux_watermark

 kernel/events/ring_buffer.c         | 34 +++++++++++++++--------------
 tools/perf/arch/x86/util/intel-pt.c |  6 +++++
 2 files changed, 24 insertions(+), 16 deletions(-)
Signature · 1 line
2.30.2
author reviewer
#1 lore raw

PATCH 1/2 perf: Cap allocation order at aux_watermark

Currently, we start allocating AUX pages half the size of the total requested AUX buffer size, ignoring the attr.aux_watermark setting. This, in turn, makes intel_pt driver disregard the watermark also, as it uses page order for its SG (ToPA) configuration.

Now, this can be fixed in the intel_pt PMU driver, but seeing as it's the only one currently making use of high order allocations, there is no reason not to fix the allocator instead. This way, any other driver wishing to add this support would not have to worry about this.

---
 kernel/events/ring_buffer.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
kernel/events/ring_buffer.c +18 -16
index bd55ccc91373..52868716ec35 100644
@@ -674,21 +674,26 @@ int rb_alloc_aux(struct perf_buffer *rb, struct perf_event *event,
 	if (!has_aux(event))
 		return -EOPNOTSUPP;
 
-	/*
-	 * We need to start with the max_order that fits in nr_pages,
-	 * not the other way around, hence ilog2() and not get_order.
-	 */
-	max_order = ilog2(nr_pages);
-
-	/*
-	 * PMU requests more than one contiguous chunks of memory
-	 * for SW double buffering
-	 */
 	if (!overwrite) {
-		if (!max_order)
-			return -EINVAL;
+		/*
+		 * Watermark defaults to half the buffer, and so does the
+		 * max_order, to aid PMU drivers in double buffering.
+		 */
+		if (!watermark)
+			watermark = nr_pages << (PAGE_SHIFT - 1);
 
-		max_order--;
+		/*
+		 * Use aux_watermark as the basis for chunking to
+		 * help PMU drivers honor the watermark.
+		 */
+		max_order = get_order(watermark);
+	} else {
+		/*
+		 * We need to start with the max_order that fits in nr_pages,
+		 * not the other way around, hence ilog2() and not get_order.
+		 */
+		max_order = ilog2(nr_pages);
+		watermark = 0;
 	}
 
 	rb->aux_pages = kcalloc_node(nr_pages, sizeof(void *), GFP_KERNEL,
@@ -743,9 +748,6 @@ int rb_alloc_aux(struct perf_buffer *rb, struct perf_event *event,
 	rb->aux_overwrite = overwrite;
 	rb->aux_watermark = watermark;
 
-	if (!rb->aux_watermark && !rb->aux_overwrite)
-		rb->aux_watermark = nr_pages << (PAGE_SHIFT - 1);
-
 out:
 	if (!ret)
 		rb->aux_pgoff = pgoff;
Signature · 1 line
2.30.2
#2 lore raw

[tip: perf/core] perf: Cap allocation order at aux_watermark

The following commit has been merged into the perf/core branch of tip:

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     d68e6799a5c87f415d3bfa0dea49caee28ab00d1
Gitweb:        https://git.kernel.org/tip/d68e6799a5c87f415d3bfa0dea49caee28ab00d1
Author:        Alexander Shishkin <[email protected]>
AuthorDate:    Wed, 14 Apr 2021 18:49:54 +03:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 16 Apr 2021 16:32:39 +02:00

perf: Cap allocation order at aux_watermark

Currently, we start allocating AUX pages half the size of the total requested AUX buffer size, ignoring the attr.aux_watermark setting. This, in turn, makes intel_pt driver disregard the watermark also, as it uses page order for its SG (ToPA) configuration.

Now, this can be fixed in the intel_pt PMU driver, but seeing as it's the only one currently making use of high order allocations, there is no reason not to fix the allocator instead. This way, any other driver wishing to add this support would not have to worry about this.

---
 kernel/events/ring_buffer.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
kernel/events/ring_buffer.c +18 -16
index bd55ccc..5286871 100644
@@ -674,21 +674,26 @@ int rb_alloc_aux(struct perf_buffer *rb, struct perf_event *event,
 	if (!has_aux(event))
 		return -EOPNOTSUPP;
 
-	/*
-	 * We need to start with the max_order that fits in nr_pages,
-	 * not the other way around, hence ilog2() and not get_order.
-	 */
-	max_order = ilog2(nr_pages);
-
-	/*
-	 * PMU requests more than one contiguous chunks of memory
-	 * for SW double buffering
-	 */
 	if (!overwrite) {
-		if (!max_order)
-			return -EINVAL;
+		/*
+		 * Watermark defaults to half the buffer, and so does the
+		 * max_order, to aid PMU drivers in double buffering.
+		 */
+		if (!watermark)
+			watermark = nr_pages << (PAGE_SHIFT - 1);
 
-		max_order--;
+		/*
+		 * Use aux_watermark as the basis for chunking to
+		 * help PMU drivers honor the watermark.
+		 */
+		max_order = get_order(watermark);
+	} else {
+		/*
+		 * We need to start with the max_order that fits in nr_pages,
+		 * not the other way around, hence ilog2() and not get_order.
+		 */
+		max_order = ilog2(nr_pages);
+		watermark = 0;
 	}
 
 	rb->aux_pages = kcalloc_node(nr_pages, sizeof(void *), GFP_KERNEL,
@@ -743,9 +748,6 @@ int rb_alloc_aux(struct perf_buffer *rb, struct perf_event *event,
 	rb->aux_overwrite = overwrite;
 	rb->aux_watermark = watermark;
 
-	if (!rb->aux_watermark && !rb->aux_overwrite)
-		rb->aux_watermark = nr_pages << (PAGE_SHIFT - 1);
-
 out:
 	if (!ret)
 		rb->aux_pgoff = pgoff;
author reviewer
#3 lore raw

PATCH 2/2 perf intel-pt: Use aux_watermark

In reply to Alexander Shishkin

Turns out, the default setting of attr.aux_watermark to half of the total buffer size is not very useful, especially with smaller buffers. The problem is that, after half of the buffer is filled up, the kernel updates ->aux_head and sets up the next "transaction", while observing that ->aux_tail is still zero (as userspace haven't had the chance to update it), meaning that the trace will have to stop at the end of this second "transaction". This means, for example, that the second PERF_RECORD_AUX in every trace comes with TRUNCATED flag set.

Setting attr.aux_watermark to quarter of the buffer gives enough space for the ->aux_tail update to be observed and prevents the data loss.

The obligatory before/after showcase:

Quoted 11 lines
# perf_before record -e intel_pt//u -m,8 uname
Linux
[ perf record: Woken up 6 times to write data ]
Warning:
AUX data lost 4 times out of 10!

[ perf record: Captured and wrote 0.099 MB perf.data ]

# perf record -e intel_pt//u -m,8 uname
Linux
[ perf record: Woken up 4 times to write data ]
[ perf record: Captured and wrote 0.039 MB perf.data ]

The effect is still visible with large workloads and large buffers, although less pronounced.

---
 tools/perf/arch/x86/util/intel-pt.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/perf/arch/x86/util/intel-pt.c +6 -0
index a6420c647959..6df0dc00d73a 100644
@@ -776,6 +776,12 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 		}
 	}
 
+	if (!opts->auxtrace_snapshot_mode && !opts->auxtrace_sample_mode) {
+		u32 aux_watermark = opts->auxtrace_mmap_pages * page_size / 4;
+
+		intel_pt_evsel->core.attr.aux_watermark = aux_watermark;
+	}
+
 	intel_pt_parse_terms(intel_pt_pmu->name, &intel_pt_pmu->format,
 			     "tsc", &tsc_bit);
 
Signature · 1 line
2.30.2
#4 lore raw

[tip: perf/core] perf intel-pt: Use aux_watermark

The following commit has been merged into the perf/core branch of tip:

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     874fc35cdd55e2d46161901de43ec58ca2efc5fe
Gitweb:        https://git.kernel.org/tip/874fc35cdd55e2d46161901de43ec58ca2efc5fe
Author:        Alexander Shishkin <[email protected]>
AuthorDate:    Wed, 14 Apr 2021 18:49:55 +03:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 16 Apr 2021 16:32:39 +02:00

perf intel-pt: Use aux_watermark

Turns out, the default setting of attr.aux_watermark to half of the total buffer size is not very useful, especially with smaller buffers. The problem is that, after half of the buffer is filled up, the kernel updates ->aux_head and sets up the next "transaction", while observing that ->aux_tail is still zero (as userspace haven't had the chance to update it), meaning that the trace will have to stop at the end of this second "transaction". This means, for example, that the second PERF_RECORD_AUX in every trace comes with TRUNCATED flag set.

Setting attr.aux_watermark to quarter of the buffer gives enough space for the ->aux_tail update to be observed and prevents the data loss.

The obligatory before/after showcase:

Quoted 11 lines
# perf_before record -e intel_pt//u -m,8 uname
Linux
[ perf record: Woken up 6 times to write data ]
Warning:
AUX data lost 4 times out of 10!

[ perf record: Captured and wrote 0.099 MB perf.data ]

# perf record -e intel_pt//u -m,8 uname
Linux
[ perf record: Woken up 4 times to write data ]
[ perf record: Captured and wrote 0.039 MB perf.data ]

The effect is still visible with large workloads and large buffers, although less pronounced.

---
 tools/perf/arch/x86/util/intel-pt.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/perf/arch/x86/util/intel-pt.c +6 -0
index a6420c6..6df0dc0 100644
@@ -776,6 +776,12 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 		}
 	}
 
+	if (!opts->auxtrace_snapshot_mode && !opts->auxtrace_sample_mode) {
+		u32 aux_watermark = opts->auxtrace_mmap_pages * page_size / 4;
+
+		intel_pt_evsel->core.attr.aux_watermark = aux_watermark;
+	}
+
 	intel_pt_parse_terms(intel_pt_pmu->name, &intel_pt_pmu->format,
 			     "tsc", &tsc_bit);
 
reviewer Acked-by on v1 2/2
#5 lore raw

Re: [PATCH v1 2/2] perf intel-pt: Use aux_watermark

In reply to Alexander Shishkin
Quoted 26 lines · Alexander Shishkin

Turns out, the default setting of attr.aux_watermark to half of the total buffer size is not very useful, especially with smaller buffers. The problem is that, after half of the buffer is filled up, the kernel updates ->aux_head and sets up the next "transaction", while observing that ->aux_tail is still zero (as userspace haven't had the chance to update it), meaning that the trace will have to stop at the end of this second "transaction". This means, for example, that the second PERF_RECORD_AUX in every trace comes with TRUNCATED flag set.

Setting attr.aux_watermark to quarter of the buffer gives enough space for the ->aux_tail update to be observed and prevents the data loss.

The obligatory before/after showcase:

Quoted 11 lines
# perf_before record -e intel_pt//u -m,8 uname
Linux
[ perf record: Woken up 6 times to write data ]
Warning:
AUX data lost 4 times out of 10!

[ perf record: Captured and wrote 0.099 MB perf.data ]

# perf record -e intel_pt//u -m,8 uname
Linux
[ perf record: Woken up 4 times to write data ]
[ perf record: Captured and wrote 0.039 MB perf.data ]
Quoted 4 lines

The effect is still visible with large workloads and large buffers, although less pronounced.

Quoted 22 lines
---
 tools/perf/arch/x86/util/intel-pt.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/perf/arch/x86/util/intel-pt.c +6 -0
index a6420c647959..6df0dc00d73a 100644
@@ -776,6 +776,12 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
 		}
 	}
 
+	if (!opts->auxtrace_snapshot_mode && !opts->auxtrace_sample_mode) {
+		u32 aux_watermark = opts->auxtrace_mmap_pages * page_size / 4;
+
+		intel_pt_evsel->core.attr.aux_watermark = aux_watermark;
+	}
+
 	intel_pt_parse_terms(intel_pt_pmu->name, &intel_pt_pmu->format,
 			     "tsc", &tsc_bit);