perf: Refine barriers for AUX ring buffer
v2 cover perf: Refine barriers for AUX ring buffer
Based on the discussion [1], this patch series is to refine the memory barriers for AUX ring buffer.
Patches 01 ~ 04 to address the barriers usage in the kernel. The first patch is to make clear comment for how to use the barriers between the data store and aux_head store, this asks the driver to make sure the data is visible. Patches 02 ~ 04 is to refine the drivers for barriers after the data store.
Patches 05 ~ 07 is to fix and clean up the memory barries in perf tool for AUX ring buffer.
Since the 64-bit value's atomicity is not promised on 32-bit perf, the last patch is to report error and let perf to directly exit for this case.
Have testes the patches on Arm64 Juno platform.
[1] https://lore.kernel.org/patchwork/patch/1431867/
Leo Yan (8):
perf/ring_buffer: Add comment for barriers on AUX ring buffer
coresight: tmc-etr: Add barrier after updating AUX ring buffer
coresight: tmc-etf: Add comment for store ordering perf/x86: Add barrier after updating bts
perf auxtrace: Change to use SMP memory barriers perf auxtrace: Drop legacy __sync functions
perf auxtrace: Use WRITE_ONCE() for updating aux_tail perf record: Directly bail out for compat case
arch/x86/events/intel/bts.c | 3 +++ .../hwtracing/coresight/coresight-tmc-etf.c | 6 +++++ .../hwtracing/coresight/coresight-tmc-etr.c | 8 ++++++ kernel/events/ring_buffer.c | 9 +++++++ tools/perf/builtin-record.c | 17 ++++++++++++ tools/perf/util/auxtrace.h | 27 +++---------------- 6 files changed, 47 insertions(+), 23 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/8 perf/ring_buffer: Add comment for barriers on AUX ring buffer
AUX ring buffer applies almost the same barriers as perf ring buffer, but there has an exception for ordering between writing the AUX trace data and updating user_page::aux_head.
This patch adds comment for how to use the barriers on AUX ring buffer, and gives comment to ask the drivers to flush the trace data into AUX ring buffer prior to updating user_page::aux_head.
--- kernel/events/ring_buffer.c | 9 +++++++++ 1 file changed, 9 insertions(+)
kernel/events/ring_buffer.c +9 -0
@@ -509,6 +509,15 @@ void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)perf_event_aux_event (handle->event, aux_head, size, handle->aux_flags); +/* +* See perf_output_put_handle(), AUX ring buffer applies the same +* barrier pairing as the perf ring buffer; except for B, since +* AUX ring buffer is written by hardware trace, we cannot simply +* use the generic memory barrier (like smp_wmb()) prior to update +* user_page::aux_head, the hardware trace driver takes the +* responsibility to ensure the trace data has been flushed into +* the AUX buffer before calling perf_aux_output_end(). +*/ WRITE_ONCE (rb->user_page->aux_head, rb->aux_head);if (rb_need_aux_wakeup (rb)) wakeup = true;
Signature · 1 line
2.25.1
Re: [PATCH v2 1/8] perf/ring_buffer: Add comment for barriers on AUX ring buffer
Quoted 9 lines · Leo Yan
AUX ring buffer applies almost the same barriers as perf ring buffer, but there has an exception for ordering between writing the AUX trace data and updating
user_page::aux_head.This patch adds comment for how to use the barriers on AUX ring buffer, and gives comment to ask the drivers to flush the trace data into AUX ring buffer prior to updating
user_page::aux_head.Signed-off-by Leo Yan <[email protected]>
Quoted 27 lines
--- kernel/events/ring_buffer.c | 9 +++++++++ 1 file changed, 9 insertions(+)kernel/events/ring_buffer.c +9
-0@@ -509,6 +509,15 @@ void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)perf_event_aux_event (handle->event, aux_head, size, handle->aux_flags); +/* +* See perf_output_put_handle(), AUX ring buffer applies the same +* barrier pairing as the perf ring buffer; except for B, since +* AUX ring buffer is written by hardware trace, we cannot simply +* use the generic memory barrier (like smp_wmb()) prior to update +* user_page::aux_head, the hardware trace driver takes the +* responsibility to ensure the trace data has been flushed into +* the AUX buffer before calling perf_aux_output_end(). +*/ WRITE_ONCE (rb->user_page->aux_head, rb->aux_head);if (rb_need_aux_wakeup (rb)) wakeup = true;Signature · 1 line
2.25.1
PATCH v2 2/8 coresight: tmc-etr: Add barrier after updating AUX ring buffer
In reply to Leo YanSince a memory barrier is required between AUX trace data store and aux_head store, and the AUX trace data is filled with memcpy(), it's sufficient to use smp_wmb() so can ensure the trace data is visible prior to updating aux_head.
--- drivers/hwtracing/coresight/coresight-tmc-etr.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/hwtracing/coresight/coresight-tmc-etr.c +8 -0
@@ -1563,6 +1563,14 @@ tmc_update_etr_buffer(struct coresight_device *csdev,*/ if (etr_perf->snapshot) handle->head += size; + +/* +* It requires the ordering between the AUX trace data and aux_head +* store, below smp_wmb() ensures the AUX trace data is visible prior +* to updating aux_head. +*/ +smp_wmb (); + out:/* * Don't set the TRUNCATED flag in snapshot mode because 1) the
Signature · 1 line
2.25.1
PATCH v2 3/8 coresight: tmc-etf: Add comment for store ordering
In reply to Leo YanAUX ring buffer is required to separate the data store and aux_head store, since the function CS_LOCK() has contained memory barrier mb(), mb() is a more conservative barrier than smp_wmb() on Arm32/Arm64, thus it's needless to add any explicit barrier anymore.
Add comment to make clear for the barrier usage for ETF.
--- drivers/hwtracing/coresight/coresight-tmc-etf.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/hwtracing/coresight/coresight-tmc-etf.c +6 -0
@@ -553,6 +553,12 @@ static unsigned long tmc_update_etf_buffer(struct coresight_device *csdev,if (buf->snapshot) handle->head += to_read; +/* +* AUX ring buffer requires to use memory barrier to separate the trace +* data store and aux_head store, because CS_LOCK() contains mb() which +* gives more heavy barrier than smp_wmb(), it's not necessary to +* explicitly invoke any barrier. +*/ CS_LOCK (drvdata->base); out:spin_unlock_irqrestore (&drvdata->spinlock, flags);
Signature · 1 line
2.25.1
PATCH v2 4/8 perf/x86: Add barrier after updating bts
In reply to Leo YanAdd barrier wmb() to separate the AUX data store and aux_head store.
--- arch/x86/events/intel/bts.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/events/intel/bts.c +3 -0
@@ -209,6 +209,9 @@ static void bts_update(struct bts_ctx *bts) }else {local_set (&buf->data_size, head); } + +/* The WMB separates data store and aux_head store matches. */ +wmb (); } static int
Signature · 1 line
2.25.1
Peter Zijlstra
maintainerRe: [PATCH v2 4/8] perf/x86: Add barrier after updating bts
Quoted 12 lines · Leo Yan
Add barrier
wmb()to separate the AUX data store andaux_headstore.Signed-off-by Leo Yan <[email protected]>--- arch/x86/events/intel/bts.c | 3 +++ 1 file changed, 3 insertions(+)arch/x86/events/intel/bts.c +0
-0
arch/x86/events/intel/bts.c static void bts_update(struct bts_ctx *bts) · line 214
Quoted 6 lines
arch/x86/events/intel/bts.c +3
-0@@ -209,6 +209,9 @@ static void bts_update(struct bts_ctx *bts) }else {local_set (&buf->data_size, head); } + +/* The WMB separates data store and aux_head store matches. */ +wmb ();
Alexander, do we indeed need an MFENCE here? or is the BTS hardware coherent, in which case a compiler barrier would be sufficient.
PATCH v2 5/8 perf auxtrace: Change to use SMP memory barriers
In reply to Leo YanThe kernel and the userspace tool can access the AUX ring buffer head and tail from different CPUs, thus SMP class of barriers are required on SMP system.
This patch changes to use SMP barriers to replace mb() and rmb() barriers.
--- tools/perf/util/auxtrace.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/auxtrace.h +3 -3
@@ -444,7 +444,7 @@ static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm)u64 head =READ_ONCE (pc->aux_head);/* Ensure all reads are done after we read the head */ -rmb (); +smp_rmb ();return head; }
@@ -458,7 +458,7 @@ static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm)#endif /* Ensure all reads are done after we read the head */ -rmb (); +smp_rmb ();return head; }
@@ -470,7 +470,7 @@ static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)#endif /* Ensure all reads are done before we write the tail out */ -mb (); +smp_mb ();#if BITS_PER_LONG ==64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT ) pc->aux_tail = tail;#else
Signature · 1 line
2.25.1
Re: [PATCH v2 5/8] perf auxtrace: Change to use SMP memory barriers
Quoted 8 lines · Leo Yan
The kernel and the userspace tool can access the AUX ring buffer head and tail from different CPUs, thus SMP class of barriers are required on SMP system.
This patch changes to use SMP barriers to replace
mb()andrmb()barriers.Signed-off-by Leo Yan <[email protected]>
Quoted 36 lines
--- tools/perf/util/auxtrace.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)tools/perf/util/auxtrace.h +3
-3@@ -444,7 +444,7 @@ static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm)u64 head =READ_ONCE (pc->aux_head);/* Ensure all reads are done after we read the head */ -rmb (); +smp_rmb ();return head; }@@ -458,7 +458,7 @@ static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm)#endif /* Ensure all reads are done after we read the head */ -rmb (); +smp_rmb ();return head; }@@ -470,7 +470,7 @@ static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)#endif /* Ensure all reads are done before we write the tail out */ -mb (); +smp_mb ();#if BITS_PER_LONG ==64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT ) pc->aux_tail = tail;#else
Re: [PATCH v2 5/8] perf auxtrace: Change to use SMP memory barriers
In reply to Leo YanQuoted 8 lines · Leo Yan
The kernel and the userspace tool can access the AUX ring buffer head and tail from different CPUs, thus SMP class of barriers are required on SMP system.
This patch changes to use SMP barriers to replace
mb()andrmb()barriers.Signed-off-by Leo Yan <[email protected]>
Re: [PATCH v2 5/8] perf auxtrace: Change to use SMP memory barriers
Em Mon, Jun 07, 2021 at 05:29:55PM +0200, Peter Zijlstra escreveu:
Quoted 11 lines · Leo Yan
Quoted 8 lines
The kernel and the userspace tool can access the AUX ring buffer head and tail from different CPUs, thus SMP class of barriers are required on SMP system.
This patch changes to use SMP barriers to replace
mb()andrmb()barriers.Signed-off-by Leo Yan <[email protected]>
Thanks, applied.
- Arnaldo
PATCH v2 6/8 perf auxtrace: Drop legacy __sync functions
In reply to Leo YanThe main purpose for using __sync built-in functions is to support compat mode for 32-bit perf with 64-bit kernel. But using these built-in functions might cause couple potential issues.
Firstly, __sync functions originally support Intel Itanium processoer [1] but it cannot promise to support all 32-bit archs. Now these functions have become the legacy functions.
As Peter also pointed out the logic issue in the function auxtrace_mmap__write_tail(), it does a cmpxchg with 0 values to load old_tail, and then executes a further cmpxchg with old_tail to write the new tail. If consider the aux_tail might be assigned to '0' in the middle of loops, this can introduce mess for AUX buffer if the kernel fetches the temporary value '0'.
Considering __sync functions cannot really fix the 64-bit value atomicity on 32-bit archs, thus this patch drops __sync functions.
Credits to Peter for detailed analysis.
[1] https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html#g_t_005f_005fsync-Builtins
--- tools/perf/util/auxtrace.h | 19 ------------------- 1 file changed, 19 deletions(-)
tools/perf/util/auxtrace.h +0 -19
@@ -432,12 +432,6 @@ struct auxtrace_cache;#ifdef HAVE_AUXTRACE_SUPPORT -/* -* In snapshot mode the mmapped page is read-only which makes using -* __sync_val_compare_and_swap() problematic. However, snapshot mode expects -* the buffer is not updated while the snapshot is made (e.g. Intel PT disables -* the event) so there is not a race anyway. -*/ static inline u64 auxtrace_mmap__read_snapshot_head (struct auxtrace_mmap *mm) {struct perf_event_mmap_page *pc = mm->userpg;
@@ -451,11 +445,7 @@ static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm)static inline u64 auxtrace_mmap__read_head (struct auxtrace_mmap *mm) {struct perf_event_mmap_page *pc = mm->userpg; -#if BITS_PER_LONG ==64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT )u64 head =READ_ONCE (pc->aux_head); -#else -u64 head =__sync_val_compare_and_swap (&pc->aux_head,0 ,0 ); -#endif /* Ensure all reads are done after we read the head */ smp_rmb ();
@@ -465,19 +455,10 @@ static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm)static inline void auxtrace_mmap__write_tail (struct auxtrace_mmap *mm,u64 tail) {struct perf_event_mmap_page *pc = mm->userpg; -#if BITS_PER_LONG !=64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT ) -u64 old_tail; -#endif /* Ensure all reads are done before we write the tail out */ smp_mb (); -#if BITS_PER_LONG ==64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT ) pc->aux_tail = tail; -#else -do { - old_tail =__sync_val_compare_and_swap (&pc->aux_tail,0 ,0 ); - }while (!__sync_bool_compare_and_swap (&pc->aux_tail, old_tail, tail)); -#endif } int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
Signature · 1 line
2.25.1
Adrian Hunter
reviewerRe: [PATCH v2 6/8] perf auxtrace: Drop legacy __sync functions
Quoted 46 lines · Leo Yan
The main purpose for using
__syncbuilt-in functions is to support compat mode for 32-bit perf with 64-bit kernel. But using these built-in functions might cause couple potential issues.Firstly,
__syncfunctions originally support Intel Itanium processoer [1] but it cannot promise to support all 32-bit archs. Now these functions have become the legacy functions.As Peter also pointed out the logic issue in the function
auxtrace_mmap__write_tail(), it does a cmpxchg with 0 values to loadold_tail, and then executes a further cmpxchg withold_tailto write the new tail. If consider theaux_tailmight be assigned to '0' in the middle of loops, this can introduce mess for AUX buffer if the kernel fetches the temporary value '0'.Considering
__syncfunctions cannot really fix the 64-bit value atomicity on 32-bit archs, thus this patch drops__syncfunctions.Credits to Peter for detailed analysis.
[1] https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html#g_t_005f_005fsync-Builtins
Suggested-by Peter Zijlstra <[email protected]>Signed-off-by Leo Yan <[email protected]>--- tools/perf/util/auxtrace.h | 19 ------------------- 1 file changed, 19 deletions(-)tools/perf/util/auxtrace.h +0
-6@@ -432,12 +432,6 @@ struct auxtrace_cache;#ifdef HAVE_AUXTRACE_SUPPORT -/* -* In snapshot mode the mmapped page is read-only which makes using -* __sync_val_compare_and_swap() problematic. However, snapshot mode expects -* the buffer is not updated while the snapshot is made (e.g. Intel PT disables -* the event) so there is not a race anyway. -*/ static inline u64 auxtrace_mmap__read_snapshot_head (struct auxtrace_mmap *mm) {struct perf_event_mmap_page *pc = mm->userpg;
tools/perf/util/auxtrace.h static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm) · line 454
Quoted 4 lines
tools/perf/util/auxtrace.h +0
-1@@ -451,11 +445,7 @@ static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm)static inline u64 auxtrace_mmap__read_head (struct auxtrace_mmap *mm) {struct perf_event_mmap_page *pc = mm->userpg; -#if BITS_PER_LONG ==64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT )
The test and setup for HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT is not needed anymore either.
Quoted 28 lines
tools/perf/util/auxtrace.h +0
-12u64 head =READ_ONCE (pc->aux_head); -#else -u64 head =__sync_val_compare_and_swap (&pc->aux_head,0 ,0 ); -#endif /* Ensure all reads are done after we read the head */ smp_rmb ();@@ -465,19 +455,10 @@ static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm)static inline void auxtrace_mmap__write_tail (struct auxtrace_mmap *mm,u64 tail) {struct perf_event_mmap_page *pc = mm->userpg; -#if BITS_PER_LONG !=64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT ) -u64 old_tail; -#endif /* Ensure all reads are done before we write the tail out */ smp_mb (); -#if BITS_PER_LONG ==64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT ) pc->aux_tail = tail; -#else -do { - old_tail =__sync_val_compare_and_swap (&pc->aux_tail,0 ,0 ); - }while (!__sync_bool_compare_and_swap (&pc->aux_tail, old_tail, tail)); -#endif } int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
Re: [PATCH v2 6/8] perf auxtrace: Drop legacy __sync functions
Hi Adrian,
On Wed, Jun 02, 2021 at 01:47:42PM +0300, Adrian Hunter wrote:
[...]
Quoted 7 lines
tools/perf/util/auxtrace.h static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm) · line 454
Quoted 5 lines
tools/perf/util/auxtrace.h +0
-1@@ -451,11 +445,7 @@ static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm)static inline u64 auxtrace_mmap__read_head (struct auxtrace_mmap *mm) {struct perf_event_mmap_page *pc = mm->userpg; -#if BITS_PER_LONG ==64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT )The test and setup for
HAVE_SYNC_COMPARE_AND_SWAP_SUPPORTis not needed anymore either.
Yes, I think there have two files should be cleaned:
Makefile.config
util/auxtrace.c
If still miss anything, please let me know (I remembered there have a test case for __sync_xxx_compare_and_swap, but I cannot find it now, so this is why I am concern if I miss anything not).
Thanks for review,
Leo
Adrian Hunter
reviewerRe: [PATCH v2 6/8] perf auxtrace: Drop legacy __sync functions
Quoted 14 lines · Leo Yan
Hi Adrian,
On Wed, Jun 02, 2021 at 01:47:42PM +0300, Adrian Hunter wrote:
[...]
Quoted 7 lines
tools/perf/util/auxtrace.h static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm) · line 454
Quoted 5 lines
tools/perf/util/auxtrace.h +0
-1@@ -451,11 +445,7 @@ static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm)static inline u64 auxtrace_mmap__read_head (struct auxtrace_mmap *mm) {struct perf_event_mmap_page *pc = mm->userpg; -#if BITS_PER_LONG ==64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT )The test and setup for
HAVE_SYNC_COMPARE_AND_SWAP_SUPPORTis not needed anymore either.
Quoted 8 lines
Yes, I think there have two files should be cleaned:
Makefile.config
util/auxtrace.cIf still miss anything, please let me know (I remembered there have a test case for
__sync_xxx_compare_and_swap, but I cannot find it now, so this is why I am concern if I miss anything not).
tools/build/feature/test-sync-compare-and-swap.c
Re: [PATCH v2 6/8] perf auxtrace: Drop legacy __sync functions
Quoted 17 lines · Adrian Hunter
Quoted 16 lines · Leo Yan
Hi Adrian,
On Wed, Jun 02, 2021 at 01:47:42PM +0300, Adrian Hunter wrote:
[...]
Quoted 7 lines
tools/perf/util/auxtrace.h static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm) · line 454
Quoted 5 lines
tools/perf/util/auxtrace.h +0
-1@@ -451,11 +445,7 @@ static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm)static inline u64 auxtrace_mmap__read_head (struct auxtrace_mmap *mm) {struct perf_event_mmap_page *pc = mm->userpg; -#if BITS_PER_LONG ==64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT )The test and setup for
HAVE_SYNC_COMPARE_AND_SWAP_SUPPORTis not needed anymore either.Yes, I think there have two files should be cleaned:
Quoted 8 lines
Quoted 6 lines
Makefile.config
util/auxtrace.cIf still miss anything, please let me know (I remembered there have a test case for
__sync_xxx_compare_and_swap, but I cannot find it now, so this is why I am concern if I miss anything not).
tools/build/feature/test-sync-compare-and-swap.c
Thanks a lot!
PATCH v2 7/8 perf auxtrace: Use WRITE_ONCE() for updating aux_tail
In reply to Leo YanUse WRITE_ONCE() for updating aux_tail, so can avoid unexpected memory behaviour.
--- tools/perf/util/auxtrace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/auxtrace.h +1 -1
@@ -458,7 +458,7 @@ static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)/* Ensure all reads are done before we write the tail out */ smp_mb (); - pc->aux_tail = tail; +WRITE_ONCE (pc->aux_tail, tail); } int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
Signature · 1 line
2.25.1
Re: [PATCH v2 7/8] perf auxtrace: Use WRITE_ONCE() for updating aux_tail
Quoted 4 lines · Leo Yan
Use
WRITE_ONCE()for updatingaux_tail, so can avoid unexpected memory behaviour.Signed-off-by Leo Yan <[email protected]>
Quoted 18 lines
--- tools/perf/util/auxtrace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/auxtrace.h +1
-1@@ -458,7 +458,7 @@ static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)/* Ensure all reads are done before we write the tail out */ smp_mb (); - pc->aux_tail = tail; +WRITE_ONCE (pc->aux_tail, tail); } int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
Re: [PATCH v2 7/8] perf auxtrace: Use WRITE_ONCE() for updating aux_tail
In reply to Leo YanQuoted 4 lines · Leo Yan
Use
WRITE_ONCE()for updatingaux_tail, so can avoid unexpected memory behaviour.Signed-off-by Leo Yan <[email protected]>
Quoted 20 lines
--- tools/perf/util/auxtrace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/auxtrace.h +1
-1@@ -458,7 +458,7 @@ static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)/* Ensure all reads are done before we write the tail out */ smp_mb (); - pc->aux_tail = tail; +WRITE_ONCE (pc->aux_tail, tail); } int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,Signature · 1 line
2.25.1
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v2 7/8] perf auxtrace: Use WRITE_ONCE() for updating aux_tail
Em Mon, Jun 07, 2021 at 05:31:01PM +0200, Peter Zijlstra escreveu:
Quoted 7 lines · Leo Yan
Quoted 4 lines
Use
WRITE_ONCE()for updatingaux_tail, so can avoid unexpected memory behaviour.Signed-off-by Leo Yan <[email protected]>
Leo, this one is dependendant on the 6/8, will wait for a resubmission, keeping 5/8 tho, as was Acked and applies cleanly, perf/core.
Quoted 20 lines
Quoted 20 lines
--- tools/perf/util/auxtrace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/auxtrace.h +1
-1@@ -458,7 +458,7 @@ static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)/* Ensure all reads are done before we write the tail out */ smp_mb (); - pc->aux_tail = tail; +WRITE_ONCE (pc->aux_tail, tail); } int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo
Re: [PATCH v2 7/8] perf auxtrace: Use WRITE_ONCE() for updating aux_tail
Hi Arnaldo,
Quoted 11 lines · Arnaldo Carvalho de Melo
Em Mon, Jun 07, 2021 at 05:31:01PM +0200, Peter Zijlstra escreveu:
Quoted 7 lines · Leo Yan
Quoted 4 lines
Use
WRITE_ONCE()for updatingaux_tail, so can avoid unexpected memory behaviour.Signed-off-by Leo Yan <[email protected]>Leo, this one is dependendant on the 6/8, will wait for a resubmission, keeping 5/8 tho, as was Acked and applies cleanly, perf/core.
Yeah, will respin for patches 6/8 and 7/8.
Thanks for merging patch 5/8.
Leo
PATCH v2 8/8 perf record: Directly bail out for compat case
In reply to Leo YanSince the 64-bit atomicity is not promised in 32-bit perf, directly report the error and bail out for this case.
Now only applies on x86_64 and Arm64 platforms.
--- tools/perf/builtin-record.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
tools/perf/builtin-record.c +17 -0
@@ -74,6 +74,7 @@#include <linux/zalloc.h> #include <linux/bitmap.h> #include <sys/time.h> +#include <sys/utsname.h> struct switch_output {bool enabled;
@@ -848,6 +849,22 @@ static int record__mmap_evlist(struct record *rec, opts->auxtrace_sample_mode;char msg[512 ]; +#ifndef __LP64__ +struct utsname uts; +int ret; + + ret =uname (&uts); +if (ret <0 ) +return ret; + +if (!strncmp (uts.machine,"x86_64" ,6 ) || !strncmp (uts.machine,"aarch64" ,7 ) || + !strncmp (uts.machine,"arm64" ,5 )) { +pr_err ("Error, 32-bit perf cannot record from a 64-bit kernel.\n" +"Please use a 64-bit version of perf instead.\n" ); +return -ENOTSUP ; + } +#endif +if (opts->affinity !=PERF_AFFINITY_SYS )cpu__setup_cpunode_map ();
Signature · 1 line
2.25.1
Adrian Hunter
reviewerRe: [PATCH v2 8/8] perf record: Directly bail out for compat case
Quoted 6 lines · Leo Yan
Since the 64-bit atomicity is not promised in 32-bit perf, directly report the error and bail out for this case.
Now only applies on
x86_64and Arm64 platforms.Suggested-by Adrian Hunter <[email protected]>
Maybe we can do better for the compat case.
We can assume the upper 32-bits change very seldom, and always increase. So for the 'read' case:
u64 first, second, last;
u64 mask = (u64)((u32)-1) << 32;
do {
first = READ_ONCE(pc->aux_head);
rmb();
second = READ_ONCE(pc->aux_head);
rmb();
last = READ_ONCE(pc->aux_head);
} while ((first & mask) != (last & mask));
return second;For the write case, we can cause a fatal error only if the new tail has non-zero upper 32-bits. That gives up to 4GiB of data before aborting:
if (tail & mask) return -1; smp_mb(); WRITE_ONCE(pc->aux_tail, tail);
Quoted 41 lines
Signed-off-by Leo Yan <[email protected]>--- tools/perf/builtin-record.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)tools/perf/builtin-record.c +17
-0@@ -74,6 +74,7 @@#include <linux/zalloc.h> #include <linux/bitmap.h> #include <sys/time.h> +#include <sys/utsname.h> struct switch_output {bool enabled;@@ -848,6 +849,22 @@ static int record__mmap_evlist(struct record *rec, opts->auxtrace_sample_mode;char msg[512 ]; +#ifndef __LP64__ +struct utsname uts; +int ret; + + ret =uname (&uts); +if (ret <0 ) +return ret; + +if (!strncmp (uts.machine,"x86_64" ,6 ) || !strncmp (uts.machine,"aarch64" ,7 ) || + !strncmp (uts.machine,"arm64" ,5 )) { +pr_err ("Error, 32-bit perf cannot record from a 64-bit kernel.\n" +"Please use a 64-bit version of perf instead.\n" ); +return -ENOTSUP ; + } +#endif +if (opts->affinity !=PERF_AFFINITY_SYS )cpu__setup_cpunode_map ();
Re: [PATCH v2 8/8] perf record: Directly bail out for compat case
Hi Adrain,
Quoted 25 lines · Adrian Hunter
Quoted 6 lines · Leo Yan
Since the 64-bit atomicity is not promised in 32-bit perf, directly report the error and bail out for this case.
Now only applies on
x86_64and Arm64 platforms.Suggested-by Adrian Hunter <[email protected]>Maybe we can do better for the compat case.
We can assume the upper 32-bits change very seldom, and always increase. So for the 'read' case:
u64 first, second, last; u64 mask = (u64)((u32)-1) << 32; do { first = READ_ONCE(pc->aux_head); rmb(); second = READ_ONCE(pc->aux_head); rmb(); last = READ_ONCE(pc->aux_head); } while ((first & mask) != (last & mask)); return second;
Quoted 8 lines
For the write case, we can cause a fatal error only if the new tail has non-zero upper 32-bits. That gives up to 4GiB of data before aborting:
if (tail & mask) return -1; smp_mb(); WRITE_ONCE(pc->aux_tail, tail);
Seems to me, it's pointless to only support aux_head for 64-bit and support aux_tail for 32-bit. I understand this can be helpful for the snapshot mode which only uses aux_head, but it still fails to support the normal case for AUX ring buffer using 64-bit head/tail.
Thanks,
Leo
Adrian Hunter
reviewerRe: [PATCH v2 8/8] perf record: Directly bail out for compat case
Quoted 37 lines · Leo Yan
Hi Adrain,
Quoted 25 lines · Adrian Hunter
Quoted 6 lines · Leo Yan
Since the 64-bit atomicity is not promised in 32-bit perf, directly report the error and bail out for this case.
Now only applies on
x86_64and Arm64 platforms.Suggested-by Adrian Hunter <[email protected]>Maybe we can do better for the compat case.
We can assume the upper 32-bits change very seldom, and always increase. So for the 'read' case:
u64 first, second, last; u64 mask = (u64)((u32)-1) << 32; do { first = READ_ONCE(pc->aux_head); rmb(); second = READ_ONCE(pc->aux_head); rmb(); last = READ_ONCE(pc->aux_head); } while ((first & mask) != (last & mask)); return second;Quoted 8 lines
For the write case, we can cause a fatal error only if the new tail has non-zero upper 32-bits. That gives up to 4GiB of data before aborting:
if (tail & mask) return -1; smp_mb(); WRITE_ONCE(pc->aux_tail, tail);
Quoted 4 lines
Seems to me, it's pointless to only support
aux_headfor 64-bit and supportaux_tailfor 32-bit. I understand this can be helpful for the snapshot mode which only usesaux_head, but it still fails to support the normal case for AUX ring buffer using 64-bit head/tail.
I am not sure why you say it is pointless. 'perf record' would still be able to capture up to 4GiB of data. Do you mean you usually capture more than 4GiB of data?
I was thinking we would separate out the compat case:
#if BITS_PER_LONG == 32 if (kernel_is_64_bit) return compat_auxtrace_mmap__[read_head/write_tail]()
#endif
So the non-compat cases would not be affected.
Re: [PATCH v2 8/8] perf record: Directly bail out for compat case
Quoted 38 lines · Adrian Hunter
Quoted 37 lines · Leo Yan
Hi Adrain,
Quoted 33 lines · Adrian Hunter
Quoted 6 lines · Leo Yan
Since the 64-bit atomicity is not promised in 32-bit perf, directly report the error and bail out for this case.
Now only applies on
x86_64and Arm64 platforms.Suggested-by Adrian Hunter <[email protected]>Maybe we can do better for the compat case.
We can assume the upper 32-bits change very seldom, and always increase. So for the 'read' case:
u64 first, second, last; u64 mask = (u64)((u32)-1) << 32; do { first = READ_ONCE(pc->aux_head); rmb(); second = READ_ONCE(pc->aux_head); rmb(); last = READ_ONCE(pc->aux_head); } while ((first & mask) != (last & mask)); return second;For the write case, we can cause a fatal error only if the new tail has non-zero upper 32-bits. That gives up to 4GiB of data before aborting:
if (tail & mask) return -1; smp_mb(); WRITE_ONCE(pc->aux_tail, tail);
Quoted 8 lines
Quoted 4 lines
Seems to me, it's pointless to only support
aux_headfor 64-bit and supportaux_tailfor 32-bit. I understand this can be helpful for the snapshot mode which only usesaux_head, but it still fails to support the normal case for AUX ring buffer using 64-bit head/tail.I am not sure why you say it is pointless. 'perf record' would still be able to capture up to 4GiB of data. Do you mean you usually capture more than 4GiB of data?
Okay, understand. We can support 32-bit perf for compat mode when the trace data is less than 4GiB.
Quoted 8 lines
I was thinking we would separate out the compat case:
#if BITS_PER_LONG == 32 if (kernel_is_64_bit) return compat_auxtrace_mmap__[read_head/write_tail]()#endif
So the non-compat cases would not be affected.
Because I don't want to introduce the complexity for read/write head and tail, and we also need to handle the same issue for the perf ring buffer. So how about below change?
The main idea for below change is it allows the perf to run normally on the compat mode and exitly if detects the buffer head is close to the low 32-bit's overflow: when detect the low 32-bit value is bigger than 0xf0000000 (so we have 256MiB margin to the overflow), it reports error and exit.
tools/perf/util/auxtrace.c +8 -0
@@ -1693,6 +1693,14 @@ static int __auxtrace_mmap__read(struct mmap *map,pr_debug3 ("auxtrace idx %d old %#" PRIx64" head %#" PRIx64" diff %#" PRIx64"\n" , mm->idx, old, head, head - old); +#ifdef BITS_PER_LONG == 32 +if (kernel_is_64bit () && head >=0xf0000000 ) { +pr_err ("32-bit perf cannot read 64-bit value atomically;\n" ); +pr_err ("exit to avoid the 4GB (32-bit) AUX buffer overflow on compat mode.\n" ); +return -ENOMEM ; + } +#endif +if (mm->mask) { head_off = head & mm->mask; old_off = old & mm->mask;
tools/perf/util/env.c +17 -0
@@ -405,3 +405,20 @@ int perf_env__numa_node(struct perf_env *env, int cpu)return cpu >=0 && cpu < env->nr_numa_map ? env->numa_map[cpu] :-1 ; } + +int perf_kernel_is_64bit (void ) +{ +struct utsname uts; +int ret; + + ret =uname (&uts); +if (ret <0 ) +return 0 ; + +if (!strncmp (uts.machine,"x86_64" ,6 ) || + !strncmp (uts.machine,"aarch64" ,7 ) || + !strncmp (uts.machine,"arm64" ,5 )) +return 1 ; + +return 0 ; +}
tools/perf/util/env.h +2 -0
@@ -147,4 +147,6 @@ void perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node);struct btf_node *perf_env__find_btf (struct perf_env *env,__u32 btf_id);int perf_env__numa_node (struct perf_env *env,int cpu); + +int perf_kernel_is_64bit (void );#endif /* __PERF_ENV_H */
tools/perf/util/mmap.c +8 -0
@@ -323,6 +323,14 @@ int perf_mmap__push(struct mmap *md, void *to,if (rc <0 )return (rc == -EAGAIN ) ?1 :-1 ; +#ifdef BITS_PER_LONG == 32 +if (kernel_is_64bit () && head >=0xf0000000 ) { +pr_err ("32-bit perf cannot read 64-bit value atomically;\n" ); +pr_err ("exit to avoid the 4GB (32-bit) buffer overflow on compat mode.\n" ); +return -ENOMEM ; + } +#endif + size = md->core.end - md->core.start;if ((md->core.start & md->core.mask) + size != (md->core.end & md->core.mask)) {
Thanks,
Leo
Adrian Hunter
reviewerRe: [PATCH v2 8/8] perf record: Directly bail out for compat case
Quoted 75 lines · Leo Yan
Quoted 38 lines · Adrian Hunter
Quoted 37 lines · Leo Yan
Hi Adrain,
Quoted 33 lines · Adrian Hunter
Quoted 6 lines · Leo Yan
Since the 64-bit atomicity is not promised in 32-bit perf, directly report the error and bail out for this case.
Now only applies on
x86_64and Arm64 platforms.Suggested-by Adrian Hunter <[email protected]>Maybe we can do better for the compat case.
We can assume the upper 32-bits change very seldom, and always increase. So for the 'read' case:
u64 first, second, last; u64 mask = (u64)((u32)-1) << 32; do { first = READ_ONCE(pc->aux_head); rmb(); second = READ_ONCE(pc->aux_head); rmb(); last = READ_ONCE(pc->aux_head); } while ((first & mask) != (last & mask)); return second;For the write case, we can cause a fatal error only if the new tail has non-zero upper 32-bits. That gives up to 4GiB of data before aborting:
if (tail & mask) return -1; smp_mb(); WRITE_ONCE(pc->aux_tail, tail);Quoted 8 lines
Quoted 4 lines
Seems to me, it's pointless to only support
aux_headfor 64-bit and supportaux_tailfor 32-bit. I understand this can be helpful for the snapshot mode which only usesaux_head, but it still fails to support the normal case for AUX ring buffer using 64-bit head/tail.I am not sure why you say it is pointless. 'perf record' would still be able to capture up to 4GiB of data. Do you mean you usually capture more than 4GiB of data?
Okay, understand. We can support 32-bit perf for compat mode when the trace data is less than 4GiB.
Quoted 8 lines
I was thinking we would separate out the compat case:
#if BITS_PER_LONG == 32 if (kernel_is_64_bit) return compat_auxtrace_mmap__[read_head/write_tail]()#endif
So the non-compat cases would not be affected.
Because I don't want to introduce the complexity for read/write head and tail, and we also need to handle the same issue for the perf ring buffer. So how about below change?
The main idea for below change is it allows the perf to run normally on the compat mode and exitly if detects the buffer head is close to the low 32-bit's overflow: when detect the low 32-bit value is bigger than
0xf0000000(so we have 256MiB margin to the overflow), it reports error and exit.tools/perf/util/auxtrace.c +0
-0
tools/perf/util/auxtrace.c static int __auxtrace_mmap__read(struct mmap *map, · line 1697
Quoted 5 lines
tools/perf/util/auxtrace.c +2
-0@@ -1693,6 +1693,14 @@ static int __auxtrace_mmap__read(struct mmap *map,pr_debug3 ("auxtrace idx %d old %#" PRIx64" head %#" PRIx64" diff %#" PRIx64"\n" , mm->idx, old, head, head - old); +#ifdef BITS_PER_LONG == 32 +if (kernel_is_64bit () && head >=0xf0000000 ) {
You are assuming the head never increases by more than 256MiB which means you should limit the buffer size to 256MiB maximum.
To me this seems a bit too far from an ideal solution.
I would have thought separating out the compat case makes things simpler to understand.
Quoted 24 lines
tools/perf/util/auxtrace.c +6
-0+pr_err ("32-bit perf cannot read 64-bit value atomically;\n" ); +pr_err ("exit to avoid the 4GB (32-bit) AUX buffer overflow on compat mode.\n" ); + return -ENOMEM; + } +#endif +if (mm->mask) { head_off = head & mm->mask; old_off = old & mm->mask;tools/perf/util/env.c +7
-0@@ -405,3 +405,20 @@ int perf_env__numa_node(struct perf_env *env, int cpu)return cpu >=0 && cpu < env->nr_numa_map ? env->numa_map[cpu] :-1 ; } + +int perf_kernel_is_64bit (void ) +{ +struct utsname uts; +int ret; + + ret =uname (&uts);
tools/perf/util/env.c int perf_env__numa_node(struct perf_env *env, int cpu) · line 424
Quoted 10 lines
tools/perf/util/env.c +10
-0+if (ret <0 ) +return 0 ; + +if (!strncmp (uts.machine,"x86_64" ,6 ) || + !strncmp (uts.machine,"aarch64" ,7 ) || + !strncmp (uts.machine,"arm64" ,5 )) +return 1 ; + +return 0 ; +}
Obviously, we don't need to keep checking uname. It could be a global variable that is always set up early.
Quoted 34 lines
tools/perf/util/env.h +2
-0@@ -147,4 +147,6 @@ void perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node);struct btf_node *perf_env__find_btf (struct perf_env *env,__u32 btf_id);int perf_env__numa_node (struct perf_env *env,int cpu); + +int perf_kernel_is_64bit (void );#endif /* __PERF_ENV_H */ tools/perf/util/mmap.c +8
-0@@ -323,6 +323,14 @@ int perf_mmap__push(struct mmap *md, void *to,if (rc <0 )return (rc == -EAGAIN ) ?1 :-1 ; +#ifdef BITS_PER_LONG == 32 +if (kernel_is_64bit () && head >=0xf0000000 ) { +pr_err ("32-bit perf cannot read 64-bit value atomically;\n" ); +pr_err ("exit to avoid the 4GB (32-bit) buffer overflow on compat mode.\n" ); +return -ENOMEM ; + } +#endif + size = md->core.end - md->core.start;if ((md->core.start & md->core.mask) + size != (md->core.end & md->core.mask)) {Thanks,
Leo
Re: [PATCH v2 8/8] perf record: Directly bail out for compat case
Hi Adrian,
On Wed, Jun 09, 2021 at 11:23:25AM +0300, Adrian Hunter wrote:
[...]
Quoted 33 lines
Quoted 24 lines
Quoted 8 lines
I was thinking we would separate out the compat case:
#if BITS_PER_LONG == 32 if (kernel_is_64_bit) return compat_auxtrace_mmap__[read_head/write_tail]()#endif
So the non-compat cases would not be affected.
Because I don't want to introduce the complexity for read/write head and tail, and we also need to handle the same issue for the perf ring buffer. So how about below change?
The main idea for below change is it allows the perf to run normally on the compat mode and exitly if detects the buffer head is close to the low 32-bit's overflow: when detect the low 32-bit value is bigger than
0xf0000000(so we have 256MiB margin to the overflow), it reports error and exit.tools/perf/util/auxtrace.c +0
-0tools/perf/util/auxtrace.c static int __auxtrace_mmap__read(struct mmap *map, · line 1697
Quoted 5 lines
tools/perf/util/auxtrace.c +2
-0@@ -1693,6 +1693,14 @@ static int __auxtrace_mmap__read(struct mmap *map,pr_debug3 ("auxtrace idx %d old %#" PRIx64" head %#" PRIx64" diff %#" PRIx64"\n" , mm->idx, old, head, head - old); +#ifdef BITS_PER_LONG == 32 +if (kernel_is_64bit () && head >=0xf0000000 ) {You are assuming the head never increases by more than 256MiB which means you should limit the buffer size to 256MiB maximum.
Quoted 4 lines
To me this seems a bit too far from an ideal solution.
I would have thought separating out the compat case makes things simpler to understand.
Agreed. I will follow up the suggestions to add compat variants for accessing AUX head and tail, and will distinguish compat case with global env variable for 64-bit kernel.
After get ready, will send out for review. Thanks a lot for suggestions!
Leo