From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCA8F286D57 for ; Mon, 4 May 2026 13:51:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777902713; cv=none; b=DlS2khihNwRT8Kf8yzffUPUSGRYqT+ZTUSRvhBdW4q/8f4ZmLyquKxx9PF5qrGD4Wo8/yfZlcTAC2O2T6iyvlbWQ0v6TeaDFvuptd5q7AVq/pnKHBC8c0LFwQrCxaDnKyWk7h9yqidIACqPQuMMvrxI0RLqcuiGxQs+ltOGi8gA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777902713; c=relaxed/simple; bh=pYfIPqorxDE0R0e1j+wNyGgwgZvWB98Ly7fIhs/gcfc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ZhaQ4dvVPaFfwuzEWUmFpPlZQ8fH0wqP+u09Y29LdLj6G65rga7m+DrREEYpVlvjpG+q9T77iwmpqYc7R4ibVnOYEFT8fe1OxhNu2S1zg3X05sGR9vcoAdchfChKC9/r/vjbeXLs2Zd7gwlMMido9WpaXH3KvRGxkkvs7b8oThU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=B1+p02RZ; arc=none smtp.client-ip=209.85.216.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="B1+p02RZ" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-36505450d0dso2472108a91.1 for ; Mon, 04 May 2026 06:51:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777902711; x=1778507511; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=LzRxffteXPHemRjDMy2wtgMLKIg+EY5vkIJp2WjdWj0=; b=B1+p02RZ/RU564lyAniHTq6vP0z6LCZpbbc9v069p4dgj8Yy9AtcECWnTOkY2DgoUe yApOiL62FH8i/gFtEMSxiFpO2VT7l7gQDW3LKmzpcePHi/Ys8DzReJf3kFowiqkKnMic kgOsXg2XPavpz2pce1ZdA0Z1T1/oEfdrWFQDqZCZVZ6TweK5eitvLiDM2M7Ocq6OmpkV TS8d4sWxTbBXOewMQDvrd1a4LuHZbc9xG/Lk1woAajMJxtSBSz93d8KZuDrS4PX+287x Oos62oK4Dd9GHo0A8eYQBR6XK4R7U/iDVdZXSwEoP+qQO++dmfAnHNMehtFpKerEq0zl FSQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777902711; x=1778507511; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=LzRxffteXPHemRjDMy2wtgMLKIg+EY5vkIJp2WjdWj0=; b=gt9S/gpNYwgvgVfiyZ0eNS6xOR1yaa+7iCMNTGj24QGqCQ2Rr3y3psy1sj07l3fL6R atL9K0Cc3FXomd4acnmneu+8XCzqELD5Un7Cy+Kj39yO6OBbOKfbdN0uveoh+GeIwZuz 4RiGW312PFPRyQ65ABfgc88OzR0HvpE00yEmCoNSFyeIcjW/VTXQS3xFy2MPg1eGiqYg buqfRUaAogp3MHE8LLRLTVVMwxmTXlz+E10p5z2VvUQ7qz3Qxrl/tngUs9sIrviYNXjx xnOA9/fieiQo8Fzon36aseMRaRVoX4Ic7qGazcNaF3eGxd9kLCw6zPBWzS5I5It5B34s rE2g== X-Forwarded-Encrypted: i=1; AFNElJ8VvW8wJKOCpYUhbWKD/py1kI7JzQdcvXZ0rYg27K5VxOzQAkO9GeSN8HWQI+3qfQSg0s6TbDHi1HjR8TM=@vger.kernel.org X-Gm-Message-State: AOJu0YyyGOxCIwjrZIylBMoNPSMY2vgWBhEUmMPAfT1yXWCAnhRNE+MO yr26gY9Fyq4kgRbEM/HSPpzcG9WhFTa6uqqj1fwKEb3eM0cl9vXOm0fF X-Gm-Gg: AeBDieuChinxAYopBn04KdHqqwO1QYlYTfjmgO2Dq8BacU2msVqprkeR4buvJ5KSPpb QR8On7Xc5NP/mlZDSGDF3Abqt0fZT+H6PgS4IuKlojtnKxLlWei7qIXCpfotOlK5fBwd5QJnW2N 4LmZEz9YBCqFWnLhL4EUycRXTLHk91YnavZi2cKCli8FTj46h+7mUA7wI6Mt7rXf/oUvLm4bzrF DNyv9gmTh+801217YcBJOhcNCI+VGYJUE/L4WQKVZHra1aSsuY6YDCGAo/brhpwT9HhXO+INWlI bzNhFFcCmK8mjwe/wJ6dmzQcyvS+1Y6WCjjIG7NcyHmbOLfGsagLjmOKLQhN+4jE6XMUwlmtOSk +XgSLPLzotdb9ED0R24xu2aHYjZO/yTmVtG7xsHCuDyr4GfX+9JAaAGyIbvhbpoFCkYgMa+cWy5 OkOO5Fkxc6aSQRAJXPt606wfKKwJ2FC3+HaPtHfbp0oQ== X-Received: by 2002:a17:90a:ec86:b0:35f:bd29:75b9 with SMTP id 98e67ed59e1d1-3650cee7ca6mr9603800a91.22.1777902711104; Mon, 04 May 2026 06:51:51 -0700 (PDT) Received: from archlinux ([205.254.163.52]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b9cae15fb6sm109688945ad.43.2026.05.04.06.51.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 May 2026 06:51:50 -0700 (PDT) From: Suchit Karunakaran To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, james.clark@linaro.org Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Suchit Karunakaran Subject: [PATCH RESEND] perf/lock: Fix non-atomic max/time and min_time updates in contention_data Date: Mon, 4 May 2026 19:21:38 +0530 Message-ID: <20260504135138.17050-1-suchitkarunakaran@gmail.com> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Acked-by: Namhyung Kim Signed-off-by: Suchit Karunakaran --- .../perf/util/bpf_skel/lock_contention.bpf.c | 50 +++++++++++++++++-- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c index 96e7d853b9ed..5c8431be674a 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -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) -- 2.53.0