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=OwbXH0lHFRL6dtkugcSotey0pqg9KCn7mfRv4q5rS/kiMNQwt+GV19s14EvPMrWZK7ZH W64ESyM2XuKcbloRMtIpZmd3zXoA0GiLxzAApKXa9/OtImjJmy91QTsJxeVb2G4Ww2MMv aO6qmvqopJICOlBMPMCXf2QFV0i9urqhXtN32Cya+jGAe49yHGpS4i2WPrHOOH+qP0PKo ANLOps974efpYWd016RhNFWe7LRTI2C/hLAmpbcrU3Po6yIEIQzBUGTiMh3nqSbbPWEKL /x/w/5nTW4wNbYm5dwmW1BAx7fLM8XleDycWWGYkQa1VKDNLbKrhDmtpIriLZOKC0jQ== 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=RpeJKvEozsavTra5gjglrvzHP9OPxQ99cV1kbqB/OEg/AIaHRk+hduhnbES8IQ9xsOEF wYzhKSx6O94apFQ3aeWorO8ttZwyyI9j3YBettMBKolWZbGMcsOgu29pA7yPgg3imi0Zl BUjlCwBszACESMxBT0TjPHhRKr3HCE+MLKTCu+APoo+oUjThm4jhvieA+dUrfD0v4Vmrq ROAxXhh7ygw1eHQaf1F9vnWnsifOw79H0FaPT0/nGi7t9HUdF8MrG1ajpeh4kpV/ThvOl xv21lV/tbXNzta803B3v8ncXI0leg9LwH9sB5x3t56B9MhgiObGcTE2AHwWLl5vG8Ew== 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 B2500606A32F for ; Sat, 5 Dec 2026 13:24:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B2500606A32F 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-2f022026e60so20006545ad.0 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=bUVnzKcaAO+xQWZ1MyqEKZ06uyFo8rib1I8q6ixNCESl/rPKPL/MnrFFlQxyizJfn6 kLbD4RuKR4q3XeY2CUILHYH4yl9cu2JShQnF+mVqlWyXBli/C1xsNlSFvXyJZ5FXzkn1 qv+mK9LOaeQ+SRTHAt7IMIpFsqnaRRseH1mC9sOny5Pax/E+NtIgLNaEqV4QAjnjw6f0 IOePAmtA6xh8BiH0hRcFv2+OUBNpbC9DtUWD/3JxGJXSzDfprUp0PO7/Ti76Zrdt0El2 vpq+6YdBZLNR7zfICkwKGMKeqDgrdl0UdXMkCbstDsaiHvz/OiG3+pGFVWF+o/jI5uP+ Q6Fg== X-Forwarded-Encrypted: i=1; AL+hErECy/LS0exU8gQrC8XIrcSja1iBe3kDZHkaZotCRD3YG8bxJgOeo4AJobHJO8QOnm2B2EJKR2sMGLSUxjeAuVvPvRY=@vger.kernel.org X-Gm-Message-State: AB4q+uExFrXg6ktFA+6WeNh5ZGhY6sLrSPWiyGJDjwF4Rs0Jk9ASzJoI DBl8uDl0tIXm+Up195aciYe36a0aWZAQRsEG4L/mkWtSKjpI3ZULyqMj5hwVg1BZA9DQU7tCXrr zwL2T3BIMiVWyDLG4mpCxC/aZn/9+Wg== 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-trace-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]); }