From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796477085; b=RkkHpTTThtO8lFyIkEjKYZI7bVGf/JTQWTQsmOYr2UkBa6fEi30B8zYhyOSR1Tek6yUr UfC1knu/XtjbAc2zZcqPsKXB3qZLK+JS9QauFx19retd1uUycqAO8LEIr3DN1LUctL4vz /EzTmzmMiuOxHkWG5r33Q8Z8PS5GKoT5LaqinnWLRGFUI9RLSjg8AT7g7EOrwvaKcSaJK APaqKJsh1MiCC9Jr6maXKSENE3NnQbU2//AIP4ejfJla2oSucgQQNS+TkMkhbSXSnXIIM 8Q631/RkSpgftZdbX6bVvXC7xXOKAkWuYbLgLa9neBUB0Wf+gWFtNws0FPna1Zk3gPw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796477085; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=W1pu1hbaTUbxkbctShAOKw5R0CnXCILHvFpIhkhrolE=; b=cbRlSyGFUH/ZueN4YhCOis2Xk1zEnMM+ZjOIOSL4I8cSgtXXINk6Xkh5E7DcH22G8+aX MaHIgEjXJcv4zwDx3GMv96tTUkouGPEw21GBlPWgSxvlg/H8For/Xk8kStVPLG57h0mKU QxIuT7zb9vGzh+yIYkrBGDhjOc/1kn9yBH/wuLR7VWYBE4pv4ZFn7qTbTwxT4sEsqRMEz DGsvulD+3R8GIvEOyg//96yVr2br2s5POmeDKrhmYheJg7euFs6QzxQb95/6TUCrW3LFM xl8WiaZTshpeL163jLpcFuL4aBLUxykQAyCoB8Olk+9/m8+uJw35jRTfEAGkvJK96nA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--zhenwenliang.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=BKu2FfGZ; arc=none smtp.remote-ip=209.85.214.199 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2421606A322 for ; Sat, 5 Dec 2026 13:24:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B2421606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zhenwenliang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=BKu2FfGZ Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2f20fbb4420so2713025ad.1 for ; Sat, 05 Dec 2026 05:24:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796477084; x=1797081884; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=W1pu1hbaTUbxkbctShAOKw5R0CnXCILHvFpIhkhrolE=; b=BKu2FfGZS8MCEj83SF9dEKF3ZHtiyH3Ryl9HySpXaf8oU8+wD7IkYlEVXDfAnySflM bhH3gFG2C/Dv9MTP5TB7BNGieAdy5AIEQd/aziN71OXeRF+pVVEVQusHB/rLvlo1Mr4l 3Mje6sX07w85CvozQQ7rIgc/48WXl6yp/1ODweLTHrktI7JpMRl2ug4tzXHJu7AcnJr3 ReAs5E3pVQxI4hiFRXzS7CjsGDahswUNbWAg9PEQdjzr0yqPxhKuFYzwgCXH84Go9gkz 9+m4NYIDCq03d4bhiBLYQRLRCTsNxY2L9xKWS5FpU1s7poCzlxLaRB1Z4T04sjnQT/ld FyrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796477084; x=1797081884; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=W1pu1hbaTUbxkbctShAOKw5R0CnXCILHvFpIhkhrolE=; b=AEB9uDECHTl81DaSvvpOnDA1XbYWnE1dW3h/0R7TrEK/BehtGCAWaVpkRw1yoCvu2l 16lGGGb4znMAxUem0bjjj8LDEVpRgMdMbVAiWI/LHnTFlkY73YrqXITw41G2ZgTWQAFT KiGqVew7msRQyslDbO9c9dTSb1alpzCcgL+0A0MpVR4UakJ1C4ySYXGbWknww+SisPm9 M/STbX0nU+FYoM0Uy2U7P90/HWQ2pUXDtXC9nP3JcISYD9wX04RF1S2NQ6B8bZuG3kkp FR0GiUNYuO1j2Ibk3JjjdfoOBsEmBzhEL8uo56O5hVk6QyUlCwTLHBKo9UIyS+OJ/fMw v3+Q== X-Forwarded-Encrypted: i=1; AL+hErF2vyVgLe0oAfEdO1VfzbtDaF8J04lhKptu6xO74nCmtnjw2BrUeavoK8HvVklrCiZV2PuySodcNdpMT+U=@vger.kernel.org X-Gm-Message-State: AB4q+uG0RvD2LbQuBJBxpsdzWoYqHneBDZDLTLP5Or4abIYGiej2FiOg 8r9STuaIaQmkntKpz3YCLs5t0gAPLwC6IWlEBWsUiFofHEvAGqnrTTSc/+XrqKN/mHFA3G7h9NC /Yrprv6H4nHharrd0ueKOb6TmBtXaSA== X-Received: from plin14.prod.google.com ([2002:a17:903:388e:b0:2ef:bdf5:f0ce]) (user=zhenwenliang job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e5ce:b0:2ed:1cb7:79a2 with SMTP id d9443c01a7336-2f1ba2bc52amr35552695ad.25.1796477083704; Sat, 05 Dec 2026 05:24:43 -0800 (PST) Date: Sat, 5 Dec 2026 13:24:41 +0000 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261205132441.3471472-1-zhenwenliang@google.com> Subject: [PATCH] tracing: Order var_set against the variable value in tracing_map From: Zhenwen Liang To: Steven Rostedt , Masami Hiramatsu , linux-trace-kernel@vger.kernel.org Cc: Mathieu Desnoyers , Tom Zanussi , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" tracing_map_set_var() stores a variable's value and then sets its var_set flag; resolve_var_refs() tests the flag with tracing_map_var_set() and then reads the value. Nothing orders these accesses, so a histogram trigger on another CPU can see the flag set but read the old value (0 on a fresh element) and record a wrong user-visible value, such as a wrong latency. This needs two events with the same key on different CPUs that are not otherwise ordered, so it should be rare. Set the flag with smp_store_release(), test it with smp_load_acquire(), and use WRITE_ONCE() for the clear in tracing_map_read_var_once(). The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch. Fixes: 2734b629525a ("tracing: Add per-element variable support to tracing_map") Signed-off-by: Zhenwen Liang --- Compile-tested only (arm64, W=1); the flag store and load are emitted as stlrb and ldarb. kernel/trace/tracing_map.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/kernel/trace/tracing_map.c b/kernel/trace/tracing_map.c index d7922f4..035527f 100644 --- a/kernel/trace/tracing_map.c +++ b/kernel/trace/tracing_map.c @@ -71,7 +71,8 @@ u64 tracing_map_read_sum(struct tracing_map_elt *elt, unsigned int i) void tracing_map_set_var(struct tracing_map_elt *elt, unsigned int i, u64 n) { atomic64_set(&elt->vars[i], n); - elt->var_set[i] = true; + /* Pairs with smp_load_acquire() in tracing_map_var_set() */ + smp_store_release(&elt->var_set[i], true); } /** @@ -85,7 +86,8 @@ void tracing_map_set_var(struct tracing_map_elt *elt, unsigned int i, u64 n) */ bool tracing_map_var_set(struct tracing_map_elt *elt, unsigned int i) { - return elt->var_set[i]; + /* Pairs with smp_store_release() in tracing_map_set_var() */ + return smp_load_acquire(&elt->var_set[i]); } /** @@ -121,7 +123,7 @@ u64 tracing_map_read_var(struct tracing_map_elt *elt, unsigned int i) */ u64 tracing_map_read_var_once(struct tracing_map_elt *elt, unsigned int i) { - elt->var_set[i] = false; + WRITE_ONCE(elt->var_set[i], false); return (u64)atomic64_read(&elt->vars[i]); }