From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 7297133AD88 for ; Sun, 8 Feb 2026 11:48:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770551293; cv=none; b=XJf5k1GPiGe04nrahW9R9ymlnn5s3RGUzExwtKUpKFdsG3JWsYpckflHITdbLXSD+sCnoDyo73bQuSd/hyVPt6GfEhIAhy9Og0jm57GBgHgnmwDeLOBBsc28yl5IggOD8t7/uB9iuZZZ6L/qMuuYVrvgiGhS9vx/hrvqMpOumm4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770551293; c=relaxed/simple; bh=kOtJUkHc7QaeZVHQeI7ehTWWIzpadGTi8Vudy6UBKfQ=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iCnTvi9l9Y6dkGWcOYg++7X3Wy86u3wT1Tznsd1xQ2IPdTOFC+g+fqYPDwqd1cdXopBUItZk48USoLOGm0H5oULoMaJi5E4ky48VZKQhRN4xSo05ew1yFOA5qJSJnmgWuFdPvhXLqbsYNKUJaL7k6kGOdATHhGuuj/4e5pIe7Cw= 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=e+ZHYATC; arc=none smtp.client-ip=209.85.128.46 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="e+ZHYATC" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-47ee76e8656so54721515e9.0 for ; Sun, 08 Feb 2026 03:48:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770551292; x=1771156092; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=vYOGoHWlR/eu7M6OQ2kE1xOB0jjtOqAsLw5aYMd4kfY=; b=e+ZHYATCDil7QXla5wldWTQb5Mw8evJ/+FvxcrGSHrdNTuDDER64nn/baIwNuWRKoB uxYw4F656JSMrFLzkLVDUFakNUaS3yLpopdHzHX4HRMMLkC0vvFTNCh7x8FgbFORLMUa abizx0D8+ViJbwBL5JboCX+VjCINu+EFMCF185nPKRuPp8G5vxaxCwS4++e2BVm+hmoC tHjotq+FdEQV4C6v6VN4ey9rpmL6RNSCKI9OarUsfRf2+NwzhXTjJySN5sqGL8k+Vvpn XzWxZWF3/wHa54Fo40hJCGVbLYK735Qg+J8kWU3S2wuUBrUJv0T9CSyXoq3efM+uNNja l4GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770551292; x=1771156092; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=vYOGoHWlR/eu7M6OQ2kE1xOB0jjtOqAsLw5aYMd4kfY=; b=jOxpDg6XIu8u/Y+/FlEG4EF3NVCyjHRxyaG8k1tI8xXbmPodJBS2tGeSqsOPTsDbn1 xexhE2ysijNYLrcsHqNY6Ur/YlKxGXF2GLJL6ZZLuLYkKx9DZBV/PMTnfMfRCkzZNCTD fDWbOg0WJL1Nt9EptUbRu9lrQO29DCgtTlZmWPMMQQSIxo+PWGh0x+b3fD4raEknOE5G MxzVfQz65YKAvfiLZCWSerTiY06Q+c6Yg12dSFyu8VQu0lbp67fSfbSVmvlHUnM4IE1E M/U0q2bl8DnpQJG5ntvVnqyY7oyI1kgZljOz7hq8IZsRlf7LzEfJyzNZvdfDhokw7P1B XpGg== X-Forwarded-Encrypted: i=1; AJvYcCVIolHK4ejuCdtnFu0yyaTPrZ/+6ebXFwIkJknpO44frWWgFYryAvxKynOcVu5D2jMDjI96OEOu7AgZcLU=@vger.kernel.org X-Gm-Message-State: AOJu0YyvdTrAeUCn0BVXWfLysB/uAnSvQE4D4FfZb2fli2JnRIb+hodC ASb099NQwmYN/IMyOMK3FJHY+OxD6O6ZXs5DLcBz6lc+V6Ii5FFRXzPl X-Gm-Gg: AZuq6aK7Cvs4F93mcFkSQeIdrhG8nw9S1FTgHftokP/5YUGZCLjZA4Bko7Oqe+sHtYV 1Fg4POGgV3ZYAVVmHefIbjbvQps8zon0FGqhdeQTdmOj6xSz8GaJ7VLzvz+O8xbembEWcCOS4K1 d+Im7Lp2PsAQf+Nt/6MKPecz5dUD4LeqEQpSCOwC13Afj9brzqVQyh7NyKUH+Uy6m579felsD3N muHneX+38Ghwvd0O15Ie3bwCZmEIHnH5NE40+EU0tSnW5vbCNLbq1qMt6SUY50Af7ciZHCTN7sc yGNejzuIREp2uHTzjO0EFuujzGgQMN8dpEoctR1lTxbIc2LTaYO0U98REAjySqyRdqnl8YuJliq iQyR3TresGW7oKpRAAbcsKnhn0fqZpgdN+9lFrEgKT2MIXn0OLLoMSvUxt7nbLchJy3qW7IcgHu emZhv06C9JAE7ngri+blDuIbwp8X7vrZpm5mPhnWZ4cUNErtZFwmmd X-Received: by 2002:a05:600c:c4a7:b0:480:f27c:6335 with SMTP id 5b1f17b1804b1-48320236645mr117866695e9.25.1770551291467; Sun, 08 Feb 2026 03:48:11 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48317d3e245sm276610505e9.8.2026.02.08.03.48.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Feb 2026 03:48:11 -0800 (PST) Date: Sun, 8 Feb 2026 11:48:10 +0000 From: David Laight To: Ben Hutchings Cc: Gui-Dong Han , linux@roeck-us.net, linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, baijiaju1990@gmail.com, stable@vger.kernel.org Subject: Re: [PATCH] hwmon: (max16065) Use READ/WRITE_ONCE to avoid compiler optimization induced race Message-ID: <20260208114810.3709364b@pumpkin> In-Reply-To: References: <20260203121443.5482-1-hanguidong02@gmail.com> <20260207104308.1bc31102@pumpkin> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sat, 07 Feb 2026 12:43:29 +0100 Ben Hutchings wrote: > On Sat, 2026-02-07 at 10:43 +0000, David Laight wrote: > > On Tue, 3 Feb 2026 20:14:43 +0800 > > Gui-Dong Han wrote: > > > > > Simply copying shared data to a local variable cannot prevent data > > > races. The compiler is allowed to optimize away the local copy and > > > re-read the shared memory, causing a Time-of-Check Time-of-Use (TOCTOU) > > > issue if the data changes between the check and the usage. > > > > While the compiler is allowed to do this, is there any indication > > that either gcc or clang have ever done it? > > ISTR someone saying that they never did - although I thought that > > was the original justification for adding ACCESS_ONCE(). > > They do it sometimes and it's precisely why these maros were added. It > makes no sense to me to look at what these compilers currrently do (for > some particular versions, optimisation settings, and targets) and > extrapolate that to the assertion that they will never optimise away a > copy. > > > READ_ONCE() also includes barriers to guarantee ordering between cpu. > > These are empty on x86 but add code to architectures where the cpu > > can (IIRC) re-order writes. > > This is worst on alpha but affects arm and probably ppc. > > No, READ_ONCE() and WRITE_ONCE() don't include any CPU memory barriers. Look at the alpha version and the arm64 LTO code. The latter changes the reads to have 'acquire' semantics to stop re-ordering. Needed for LTO, but the thought is it might be needed in other cases. David > > > For these cases is it enough to add the compile-time barrier() after > > reading the variable to a local. > > That will also generate better code on x86. > > > > The WRITE_ONCE() aren't needed at all, the compilers definitely > > guarantee to do a single memory access for aligned accesses that are > > less than the size of a word. > > I think in this case WRITE_ONCE() might not be needed, but it's also > harmless and it's much easier to reason about {READ,WRITE}_ONCE() being > paired up. > > > This all stinks of being an AI generated patch. > > This is a follow-up to an earlier patch that claimed to fix the TOCTOU > issue. I objected to that because in the absense of READ_ONCE() it was > not guaranteed to do so. > > Ben. >