From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 A64E73932D1 for ; Sat, 11 Jul 2026 09:33:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783762415; cv=none; b=LgDFrPq3Aeb6+JmMcsZM9fGd6EBNCEkLhgXH0da/Jg00imbptDbQxiB5zt3pWn62mzuU0zgnOCC+jx4p1RBDocsnA5ubdi0YWsnr2EA9prA4/F4w7yG2ieDa/eAK/S/R3b0Yp+qJXH9M2YR0ZMziHWd+SvPiykjYy3BqQ64dxi0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783762415; c=relaxed/simple; bh=PvyDEtDG8TdjDcuAHzeqmpHFrdL1tyXkP2WE36J/VYo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k8fY/SEF57yNOBqRJo1cWsZpScnw/m6NC0g1ZCNnmFhc//WyAYy4o1VGp+lyBtkz2Nbt+eT0mEa98DLeGaii3xaqkR9kwGgFg3AyvjjCxWcpW6hmU1eqrdQO2EMf7IF9wpRRta62pozp5DLeB8WarzS/jkH4tnG1VaocxRUe7Mc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=debian.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2cac59f8b64so19158655ad.0 for ; Sat, 11 Jul 2026 02:33:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783762414; x=1784367214; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Tf+wqnGQpAYU6cY0CxfR0PNyYQLqysQQrgD50QiJkT0=; b=qUwJPyXrlCdlWEMKGecl0eartLh7tsUFQGijiQn8LG2WOKXGnUH2vd90wBIAvtApET tYnTWIwredxFuPOASX1+oBJ8UA/we63A5vlGl+zcaSrayptQU2Y3yE7Zk+0S7Vz08iJ2 +NdKaAvAtX5c9pwh+NJXWT2fM3Y7Y706cCDoGK7S4d48KrgtqzQkjvgJUI+tt+Bd029L 41thE6jYEYDmiz9KZn5C5RTYziT+OrE+dfC2cdB8tY1S7a0+qD0kJPPabhtIitmYDG4l cQy7vYtbkCN+//9GDTGaMjPmZszaYIVjPamNKT6tSyhex+h4Z7CUGc4yL2UTnOuB/jUD cPgg== X-Forwarded-Encrypted: i=1; AHgh+RpYeBO+YZ5qrQu4W0OfLa7tVYFTmKO0P48KVItyGAkD62Td/JrcAP6xDzS26PWM0O8rKHV8KY/Eu7OBNoY=@vger.kernel.org X-Gm-Message-State: AOJu0YwCpOmTP35d7QIFCmkaKaUUTy5HoGS1X4L0ZQ+vjJAoJdxSeuFz oa9sLXR/NdiMWK9lHzRwV8+FRDhyIEBApp1avObMqD6xTeMa5tGilaQ= X-Gm-Gg: AfdE7ck1uxKrBkJAlB1t7OgIIPC9iExbdaCxa6QT3MXXrk/PxASj8BDum5t6fF6WxFF Tp+zv55Z9d3DTAdMJPWu3uPx4ZVZIJDSDXFo6KxOeEZc2rs3QvVpx9A0GrRsYJo0b7lRQaMTq6l Iv41bywZt37QTw7sF8jC3KzuYYfcvzB8bHNnXFXSbbJhO5eAR7p7lkfTQ09K4/lSSKes8d1JS1P Fd4jDD3Qf17abxevNTqH+aMm+cCRvdepow4I+NfA4tkwyD5jITEqKVi3epJ5uvM5P7mbtGzmzYc UPTvCBbBdVopsd1bBcsJ4E1ktprXy1AaEwc5gbv62WjTQIA6tR+8jkrxFAtUCqjNZcsgMYRQoAY TKPwLTad0zn4NRISqOlbFsNl0aiK3b71kpyysN7oVV4leTf3bdyrNT0Wr7W94Bq/jqpNsyH1QLp 9F8t4EtSv/3VuyBjwFQXDFahgzdMMx7jynrg3Z+tUWIK9WvvbM+89+tLzo9Iypt/n68bC+ubx5R dX1g5XINurqSTuRbf/gpxcM7fe0zDI3kNKO X-Received: by 2002:a17:902:f68d:b0:2c9:cf5d:d9bc with SMTP id d9443c01a7336-2ce9f17d4c0mr24607395ad.35.1783762414004; Sat, 11 Jul 2026 02:33:34 -0700 (PDT) Received: from penguin.tail0a1999.ts.net (61-228-52-13.dynamic-ip.hinet.net. [61.228.52.13]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ccc9d3d451sm71389135ad.65.2026.07.11.02.33.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Jul 2026 02:33:33 -0700 (PDT) From: Shih-Yuan Lee To: Henrik Rydberg , Guenter Roeck Cc: Armin Wolf , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, Shih-Yuan Lee Subject: [PATCH v6 2/3] hwmon: (applesmc) Fix lockless cache validation data race Date: Sat, 11 Jul 2026 17:33:22 +0800 Message-Id: <20260711093323.14529-3-fourdollars@debian.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260711093323.14529-1-fourdollars@debian.org> References: <20260710123236.10508-1-fourdollars@debian.org> <20260711093323.14529-1-fourdollars@debian.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In applesmc_get_entry_by_index(), the cache->valid flag is checked locklessly, but setting it to true lacks memory barriers. This can lead to a data race (TOCTOU) where another thread sees cache->valid as true before the actual cache contents (cache->key, cache->len, cache->type, etc.) are fully committed and visible to that CPU, potentially causing it to read uninitialized data and send incorrect keys to the Apple SMC hardware. Introduce memory barriers (smp_load_acquire and smp_store_release) with explanatory comments to ensure cache synchronization is thread-safe and fully visible across all CPUs. Signed-off-by: Shih-Yuan Lee --- drivers/hwmon/applesmc.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c index 9b2d9ecb20c0..317135fc4b73 100644 --- a/drivers/hwmon/applesmc.c +++ b/drivers/hwmon/applesmc.c @@ -33,6 +33,7 @@ #include #include #include +#include /* data port used by Apple SMC */ #define APPLESMC_DATA_PORT 0x300 @@ -373,7 +374,8 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(int index) __be32 be; int ret = 0; - if (cache->valid) + /* Pairs with smp_store_release() to ensure cache contents are visible */ + if (smp_load_acquire(&cache->valid)) return cache; mutex_lock(&smcreg.mutex); @@ -392,7 +394,8 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(int index) cache->len = info[0]; memcpy(cache->type, &info[1], 4); cache->flags = info[5]; - cache->valid = true; + /* Pairs with smp_load_acquire() to commit cache contents before setting valid */ + smp_store_release(&cache->valid, true); out: mutex_unlock(&smcreg.mutex); -- 2.39.5