Skip to contentSkip to conversation

perf/lock: Fix non-atomic max/time and min_time updates in contention_data

PATCH In review Suchit Karunakaran 4 participants 9 messages May 7 – Jun 4

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 perf/lock: Fix non-atomic max/time and min_time updates in contention_data

The update_contention_data() had a FIXME noting that max_time and min_time updates lacked atomicity. Two CPUs could simultaneously read a stale value, pass the comparison check and race on the write-back, with the smaller value potentially overwriting the larger one and silently corrupting the statistics.

Fix this by replacing the bare conditional assignments with a bpf_loop()-based CAS retry loop. Each field tracks its own convergence independently via max_done/min_done flags in cas_ctx, so a successful CAS on one field is never retried even if the other field needs more attempts.

---
 .../perf/util/bpf_skel/lock_contention.bpf.c  | 50 +++++++++++++++++--
 1 file changed, 45 insertions(+), 5 deletions(-)
tools/perf/util/bpf_skel/lock_contention.bpf.c +45 -5
index 96e7d853b9ed..5c8431be674a 100644
@@ -175,6 +175,13 @@ struct mm_struct___new {
 	struct rw_semaphore mmap_lock;
 } __attribute__((preserve_access_index));
 
+struct cas_ctx {
+	struct contention_data *data;
+	u64 duration;
+	int max_done;
+	int min_done;
+};
+
 extern struct kmem_cache *bpf_get_kmem_cache(u64 addr) __ksym __weak;
 
 /* control flags */
@@ -486,16 +493,49 @@ static inline s32 get_owner_stack_id(u64 *stacktrace)
 	return -1;
 }
 
+static long cas_min_max_cb(u64 idx, void *arg)
+{
+	struct cas_ctx *ctx = arg;
+
+	if (!ctx->max_done) {
+		u64 old_max = ctx->data->max_time;
+		if (old_max >= ctx->duration) {
+			ctx->max_done = 1;
+		} else {
+			u64 r = __sync_val_compare_and_swap(
+				&ctx->data->max_time, old_max, ctx->duration);
+			if (r == old_max)
+				ctx->max_done = 1;
+		}
+	}
+
+	if (!ctx->min_done) {
+		u64 old_min = ctx->data->min_time;
+		if (old_min <= ctx->duration) {
+			ctx->min_done = 1;
+		} else {
+			u64 r = __sync_val_compare_and_swap(
+				&ctx->data->min_time, old_min, ctx->duration);
+			if (r == old_min)
+				ctx->min_done = 1;
+		}
+	}
+
+	return (ctx->max_done && ctx->min_done) ? 1 : 0;
+}
+
 static inline void update_contention_data(struct contention_data *data, u64 duration, u32 count)
 {
 	__sync_fetch_and_add(&data->total_time, duration);
 	__sync_fetch_and_add(&data->count, count);
 
-	/* FIXME: need atomic operations */
-	if (data->max_time < duration)
-		data->max_time = duration;
-	if (data->min_time > duration)
-		data->min_time = duration;
+	struct cas_ctx ctx = {
+		.data     = data,
+		.duration = duration,
+		.max_done = 0,
+		.min_done = 0,
+	};
+	bpf_loop(64, cas_min_max_cb, &ctx, 0);
 }
 
 static inline void update_owner_stat(u32 id, u64 duration, u32 flags)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 2/2 perf build: compile BPF skeletons with -mcpu=v3

The lock_contention BPF program uses __sync_val_compare_and_swap() to atomically update the max_time and min_time fields in contention_data. This builtin lowers to the BPF_CMPXCHG instruction, which is only available in BPF ISA v3. Without an explicit -mcpu flag, Clang targets BPF v1/v2 by default on older toolchains (Clang < 18), causing build errors when v3 instructions are emitted.

Add -mcpu=v3 to CLANG_OPTIONS, which is used exclusively in the BPF skeleton compilation rule.

---
 tools/perf/Makefile.perf | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/Makefile.perf +1 -1
index cee19c923c06..a9a8c84b6b00 100644
@@ -1195,7 +1195,7 @@ ifneq ($(CROSS_COMPILE),)
 CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
 endif
 
-CLANG_OPTIONS = -Wall
+CLANG_OPTIONS = -Wall -mcpu=v3
 CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH))
 BPF_INCLUDE := -I$(SKEL_TMP_OUT)/.. -I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES)
 TOOLS_UAPI_INCLUDE := -I$(srctree)/tools/include/uapi
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH 2/2] perf build: compile BPF skeletons with -mcpu=v3

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Adding `-mcpu=v3` to `CLANG_OPTIONS` forces all BPF skeletons to use the BPF v3 ISA, breaking…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Adding -mcpu=v3 to CLANG_OPTIONS forces all BPF skeletons to use the BPF v3 ISA, breaking backward compatibility for perf tools designed to run on older kernels. --

commit 2e8791aeccf694e1ae9ed9fda46e3a6f73332838
Author: Suchit Karunakaran <[email protected]>

perf build: compile BPF skeletons with -mcpu=v3

This commit adds -mcpu=v3 to CLANG_OPTIONS to support the atomic cmpxchg instruction required by the lock_contention BPF program. This forces Clang to emit BPF ISA v3 instructions during skeleton compilation.

Quoted 4 lines
tools/perf/Makefile.perf +0 -0
index cee19c923c060..a9a8c84b6b006 100644

[ ... ]

tools/perf/Makefile.perf ifneq ($(CROSS_COMPILE),) · line 1198

Quoted 6 lines
tools/perf/Makefile.perf +1 -1
@@ -1195,7 +1195,7 @@ ifneq ($(CROSS_COMPILE),)
 CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
 endif
 
-CLANG_OPTIONS = -Wall
+CLANG_OPTIONS = -Wall -mcpu=v3

Does this change break backward compatibility for other perf tools running on older kernels?

Since CLANG_OPTIONS is used in the pattern rule for compiling all BPF skeletons, this forces every BPF program in perf (such as augmented_raw_syscalls.bpf.c and off_cpu.bpf.c) to target the BPF ISA v3.

Kernels older than 5.1 do not support v3 instructions and will reject these programs during the verifier load phase.

Should the -mcpu=v3 flag be restricted exclusively to the target that needs it, perhaps using a target-specific append for lock_contention.bpf.o?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#3 lore raw

Re: [PATCH 2/2] perf build: compile BPF skeletons with -mcpu=v3

Hi Arnaldo,

Quoted 31 lines · On Thu, May 07, 2026 at 11:49:59PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Adding -mcpu=v3 to CLANG_OPTIONS forces all BPF skeletons to use the BPF v3 ISA, breaking backward compatibility for perf tools designed to run on older kernels. --

commit 2e8791aeccf694e1ae9ed9fda46e3a6f73332838
Author: Suchit Karunakaran <[email protected]>

perf build: compile BPF skeletons with -mcpu=v3

This commit adds -mcpu=v3 to CLANG_OPTIONS to support the atomic cmpxchg instruction required by the lock_contention BPF program. This forces Clang to emit BPF ISA v3 instructions during skeleton compilation.

Quoted 4 lines
tools/perf/Makefile.perf +0 -0
index cee19c923c060..a9a8c84b6b006 100644

[ ... ]

tools/perf/Makefile.perf ifneq ($(CROSS_COMPILE),) · line 1198

Quoted 6 lines
tools/perf/Makefile.perf +1 -1
@@ -1195,7 +1195,7 @@ ifneq ($(CROSS_COMPILE),)
 CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
 endif
 
-CLANG_OPTIONS = -Wall
+CLANG_OPTIONS = -Wall -mcpu=v3

Does this change break backward compatibility for other perf tools running on older kernels?

Quoted 6 lines

Since CLANG_OPTIONS is used in the pattern rule for compiling all BPF skeletons, this forces every BPF program in perf (such as augmented_raw_syscalls.bpf.c and off_cpu.bpf.c) to target the BPF ISA v3.

Kernels older than 5.1 do not support v3 instructions and will reject these programs during the verifier load phase.

I think it's old enough and we can move on to ensure v3 instructions.

Thanks,
Namhyung

Quoted 6 lines

Should the -mcpu=v3 flag be restricted exclusively to the target that needs it, perhaps using a target-specific append for lock_contention.bpf.o?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

Re: [PATCH 2/2] perf build: compile BPF skeletons with -mcpu=v3

Quoted 34 lines · Namhyung Kim

Hi Arnaldo,

Quoted 31 lines · On Thu, May 07, 2026 at 11:49:59PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Adding -mcpu=v3 to CLANG_OPTIONS forces all BPF skeletons to use the BPF v3 ISA, breaking backward compatibility for perf tools designed to run on older kernels. --

commit 2e8791aeccf694e1ae9ed9fda46e3a6f73332838
Author: Suchit Karunakaran <[email protected]>

perf build: compile BPF skeletons with -mcpu=v3

This commit adds -mcpu=v3 to CLANG_OPTIONS to support the atomic cmpxchg instruction required by the lock_contention BPF program. This forces Clang to emit BPF ISA v3 instructions during skeleton compilation.

Quoted 4 lines
tools/perf/Makefile.perf +0 -0
index cee19c923c060..a9a8c84b6b006 100644

[ ... ]

tools/perf/Makefile.perf ifneq ($(CROSS_COMPILE),) · line 1198

Quoted 6 lines
tools/perf/Makefile.perf +1 -1
@@ -1195,7 +1195,7 @@ ifneq ($(CROSS_COMPILE),)
 CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
 endif
 
-CLANG_OPTIONS = -Wall
+CLANG_OPTIONS = -Wall -mcpu=v3

Does this change break backward compatibility for other perf tools running on older kernels?

Quoted 8 lines
Quoted 6 lines

Since CLANG_OPTIONS is used in the pattern rule for compiling all BPF skeletons, this forces every BPF program in perf (such as augmented_raw_syscalls.bpf.c and off_cpu.bpf.c) to target the BPF ISA v3.

Kernels older than 5.1 do not support v3 instructions and will reject these programs during the verifier load phase.

I think it's old enough and we can move on to ensure v3 instructions.

Can I take this as an Reviewed-by or Acked-by?

- Arnaldo

Quoted 9 lines

Thanks,
Namhyung

Quoted 6 lines

Should the -mcpu=v3 flag be restricted exclusively to the target that needs it, perhaps using a target-specific append for lock_contention.bpf.o?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer Reviewed-by
#5 lore raw

Re: [PATCH 2/2] perf build: compile BPF skeletons with -mcpu=v3

Quoted 39 lines · Arnaldo Carvalho de Melo
Quoted 38 lines · Namhyung Kim

Hi Arnaldo,

Quoted 35 lines · On Thu, May 07, 2026 at 11:49:59PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Adding -mcpu=v3 to CLANG_OPTIONS forces all BPF skeletons to use the BPF v3 ISA, breaking backward compatibility for perf tools designed to run on older kernels. --

commit 2e8791aeccf694e1ae9ed9fda46e3a6f73332838
Author: Suchit Karunakaran <[email protected]>

perf build: compile BPF skeletons with -mcpu=v3

This commit adds -mcpu=v3 to CLANG_OPTIONS to support the atomic cmpxchg instruction required by the lock_contention BPF program. This forces Clang to emit BPF ISA v3 instructions during skeleton compilation.

Quoted 4 lines
tools/perf/Makefile.perf +0 -0
index cee19c923c060..a9a8c84b6b006 100644

[ ... ]

tools/perf/Makefile.perf ifneq ($(CROSS_COMPILE),) · line 1198

Quoted 6 lines
tools/perf/Makefile.perf +1 -1
@@ -1195,7 +1195,7 @@ ifneq ($(CROSS_COMPILE),)
 CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
 endif
 
-CLANG_OPTIONS = -Wall
+CLANG_OPTIONS = -Wall -mcpu=v3

Does this change break backward compatibility for other perf tools running on older kernels?

Since CLANG_OPTIONS is used in the pattern rule for compiling all BPF skeletons, this forces every BPF program in perf (such as augmented_raw_syscalls.bpf.c and off_cpu.bpf.c) to target the BPF ISA v3.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Kernels older than 5.1 do not support v3 instructions and will reject these programs during the verifier load phase.

I think it's old enough and we can move on to ensure v3 instructions.

Can I take this as an Reviewed-by or Acked-by?

Yep,

Thanks,
Namhyung

#6 lore raw

Re: [PATCH 2/2] perf build: compile BPF skeletons with -mcpu=v3

Quoted 43 lines · Namhyung Kim
Quoted 42 lines · Arnaldo Carvalho de Melo
Quoted 41 lines · Namhyung Kim

Hi Arnaldo,

Quoted 37 lines · On Thu, May 07, 2026 at 11:49:59PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Adding -mcpu=v3 to CLANG_OPTIONS forces all BPF skeletons to use the BPF v3 ISA, breaking backward compatibility for perf tools designed to run on older kernels. --

commit 2e8791aeccf694e1ae9ed9fda46e3a6f73332838
Author: Suchit Karunakaran <[email protected]>

perf build: compile BPF skeletons with -mcpu=v3

This commit adds -mcpu=v3 to CLANG_OPTIONS to support the atomic cmpxchg instruction required by the lock_contention BPF program. This forces Clang to emit BPF ISA v3 instructions during skeleton compilation.

Quoted 4 lines
tools/perf/Makefile.perf +0 -0
index cee19c923c060..a9a8c84b6b006 100644

[ ... ]

tools/perf/Makefile.perf ifneq ($(CROSS_COMPILE),) · line 1198

Quoted 6 lines
tools/perf/Makefile.perf +1 -1
@@ -1195,7 +1195,7 @@ ifneq ($(CROSS_COMPILE),)
 CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
 endif
 
-CLANG_OPTIONS = -Wall
+CLANG_OPTIONS = -Wall -mcpu=v3

Does this change break backward compatibility for other perf tools running on older kernels?

Since CLANG_OPTIONS is used in the pattern rule for compiling all BPF skeletons, this forces every BPF program in perf (such as augmented_raw_syscalls.bpf.c and off_cpu.bpf.c) to target the BPF ISA v3.

Kernels older than 5.1 do not support v3 instructions and will reject these programs during the verifier load phase.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I think it's old enough and we can move on to ensure v3 instructions.

Can I take this as an Reviewed-by or Acked-by?

Yep,

So this got refactored, Suchit, can you take a look at perf-tools-next to see what is needed to have this in place after Ian's changes?

Thanks in advance,

- Arnaldo

#7 lore raw

Re: [PATCH 2/2] perf build: compile BPF skeletons with -mcpu=v3

Quoted 51 lines · Arnaldo Carvalho de Melo
Quoted 49 lines · Namhyung Kim
Quoted 39 lines · Arnaldo Carvalho de Melo
Quoted 38 lines · Namhyung Kim

Hi Arnaldo,

Quoted 35 lines · On Thu, May 07, 2026 at 11:49:59PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Adding -mcpu=v3 to CLANG_OPTIONS forces all BPF skeletons to use the BPF v3 ISA, breaking backward compatibility for perf tools designed to run on older kernels. --

commit 2e8791aeccf694e1ae9ed9fda46e3a6f73332838
Author: Suchit Karunakaran <[email protected]>

perf build: compile BPF skeletons with -mcpu=v3

This commit adds -mcpu=v3 to CLANG_OPTIONS to support the atomic cmpxchg instruction required by the lock_contention BPF program. This forces Clang to emit BPF ISA v3 instructions during skeleton compilation.

Quoted 4 lines
tools/perf/Makefile.perf +0 -0
index cee19c923c060..a9a8c84b6b006 100644

[ ... ]

tools/perf/Makefile.perf ifneq ($(CROSS_COMPILE),) · line 1198

Quoted 6 lines
tools/perf/Makefile.perf +1 -1
@@ -1195,7 +1195,7 @@ ifneq ($(CROSS_COMPILE),)
 CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
 endif
 
-CLANG_OPTIONS = -Wall
+CLANG_OPTIONS = -Wall -mcpu=v3

Does this change break backward compatibility for other perf tools running on older kernels?

Since CLANG_OPTIONS is used in the pattern rule for compiling all BPF skeletons, this forces every BPF program in perf (such as augmented_raw_syscalls.bpf.c and off_cpu.bpf.c) to target the BPF ISA v3.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Kernels older than 5.1 do not support v3 instructions and will reject these programs during the verifier load phase.

I think it's old enough and we can move on to ensure v3 instructions.

Can I take this as an Reviewed-by or Acked-by?

Yep,

Quoted 8 lines
Quoted 1 line

So this got refactored, Suchit, can you take a look at perf-tools-next to see what is needed to have this in place after Ian's changes?

Thanks in advance,

- Arnaldo

Hi Arnaldo. Sure, I'll take a look at perf-tools-next and send a revised version.

#8 lore raw

Re: [PATCH 2/2] perf build: compile BPF skeletons with -mcpu=v3

In reply to Namhyung Kim
Quoted 43 lines · Namhyung Kim
Quoted 42 lines · Arnaldo Carvalho de Melo
Quoted 41 lines · Namhyung Kim

Hi Arnaldo,

Quoted 37 lines · On Thu, May 07, 2026 at 11:49:59PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Adding -mcpu=v3 to CLANG_OPTIONS forces all BPF skeletons to use the BPF v3 ISA, breaking backward compatibility for perf tools designed to run on older kernels. --

commit 2e8791aeccf694e1ae9ed9fda46e3a6f73332838
Author: Suchit Karunakaran <[email protected]>

perf build: compile BPF skeletons with -mcpu=v3

This commit adds -mcpu=v3 to CLANG_OPTIONS to support the atomic cmpxchg instruction required by the lock_contention BPF program. This forces Clang to emit BPF ISA v3 instructions during skeleton compilation.

Quoted 4 lines
tools/perf/Makefile.perf +0 -0
index cee19c923c060..a9a8c84b6b006 100644

[ ... ]

tools/perf/Makefile.perf ifneq ($(CROSS_COMPILE),) · line 1198

Quoted 6 lines
tools/perf/Makefile.perf +1 -1
@@ -1195,7 +1195,7 @@ ifneq ($(CROSS_COMPILE),)
 CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
 endif
 
-CLANG_OPTIONS = -Wall
+CLANG_OPTIONS = -Wall -mcpu=v3

Does this change break backward compatibility for other perf tools running on older kernels?

Since CLANG_OPTIONS is used in the pattern rule for compiling all BPF skeletons, this forces every BPF program in perf (such as augmented_raw_syscalls.bpf.c and off_cpu.bpf.c) to target the BPF ISA v3.

Kernels older than 5.1 do not support v3 instructions and will reject these programs during the verifier load phase.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I think it's old enough and we can move on to ensure v3 instructions.

Can I take this as an Reviewed-by or Acked-by?

Yep,

Thanks, added to the patch, that is in perf-tools-next, for v7.2.

- Arnaldo