From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 B1EB0381E8E for ; Sat, 11 Jul 2026 07:58:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783756687; cv=none; b=TlLg09TrTcnxg8Y8JvhGpEXYmmJ0sWHK+fHLBenND9TaeoFIP8gcbxv00KQ+OhqWkuRTi/Bsou9KvPBvJm6wfRnpkAHB79LEMk08wy3gVY6OXuQ2vTkxRAQy7fpHEfRdKvyHU8x0Id4ar5G+m7boj/g0lsUS99el153Q1FVyxvc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783756687; c=relaxed/simple; bh=PvyDEtDG8TdjDcuAHzeqmpHFrdL1tyXkP2WE36J/VYo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iJipdRaFKTLEc4uijaxRvCJ+wFTmQMv6GKb0EvnkvipOyjfDzv8wFFjnIM73h9HdQerA/ePr21ZUP24ad7lTxvxLB992rIP6Vm5z4S/pFiA8lx8J3Aa5ltHGI0GYVytYdLcdlVwChSAB42KnlwI/04buktDTHtGbHI6uDC4U4xs= 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.182 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-f182.google.com with SMTP id d9443c01a7336-2cc7ef7ec27so17990265ad.1 for ; Sat, 11 Jul 2026 00:58:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783756684; x=1784361484; 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=NLq/J/D8IMrA0CYNQSNl2naKw1DTKBqrbopVe2qAXSlI2UgEIHlEynpoVBFYn39h3c YvhO1siJ2E45+GjcFC2amucsUyVdriz/2Pm+0DRTtJzsnRM5snq0ohfNj8Z0bIlMtEux JT0i+xgiyyM1mAx9lcACwTryQPNiIFrgT7wjl05uF//eRfOjiUZYJCCsqJzrwQyrKvMA CkoNkPNLpe93g+IMQXOYCsyMehGzk5kWb+kOWkZutEZKxRKZUkf9YtGdnvQPeMm2c5zW hqdNjRp5x9ZYy8EHY7jlT8d4ailNVtj4yaTvsZkbJoACmPSKIZpoHGklAHxRZ8zVOChu zIgQ== X-Forwarded-Encrypted: i=1; AHgh+RrBCxVn72vaK53GYWkRevPi84cK6BrgB8J02zz89ILSDdUFYuTeBv3V0ujEpWXcPWACVavHk0jQ5yglp2I=@vger.kernel.org X-Gm-Message-State: AOJu0YyZJpyLA3itI2RvX4+/Mr/dn3LodI856ln0xT3Zo51gOLtds/xQ GWx46tmvW7/4DyGVMhxIzlehpJkf1e5IqO5fXmxSTVMS8rxYxUhSTIY= X-Gm-Gg: AfdE7ckNpEF/cEddsaPfuV3q5on4fhh5ep1WrgpP9XSeSYzYToAq7S76kdHJvE2E+nN WH+1cIdLOMRh+D8e1HbgnGxl5HyDAdQ1LImwxHcMiSXDpKTKC9OV34Hxl7qXR9TkZ/r3Q63Cz/w ozuI2J+FyKzppoeLpH0n8Hq8qXYDZVvMg4Q4ZksL3MuieIvQEvGXTnFEGi6VJrz8fwyvU7HWmix 2/7gWQh3ORtHOYMXpoZLRDg4CBm5xtYXw9qevQYr8bd61iVv3wluju1/cAnSrYFEzAlLnmqH5tU bUcWw3P9T9AuvuoOriHUQfNVkmGsbOuUadkRBhPa91RKAsuQyXdBEbBQOq0UD7WrtblZelLS1jt UFSsVsvk2xaKMfQ2+FH91oCjW84N11JB8KSzVQZgsLdK7DiHReJKetAahrgfmfK9Y+Ji2P/vjuk BCksL785U5cTMIMnkcAJTeuN+967HXuJ2HBRpj6LtVhhaFuatQfdc/bEcJNjFlKgsouj5UEH4pI jmwCcTx1ix/Cuf7+cjw6PbiIg== X-Received: by 2002:a17:903:2a88:b0:2cc:6018:f030 with SMTP id d9443c01a7336-2ce9e9b2507mr22689155ad.14.1783756684085; Sat, 11 Jul 2026 00:58:04 -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-2ccc9d1e279sm71130285ad.45.2026.07.11.00.58.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Jul 2026 00:58:03 -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 v5 2/3] hwmon: (applesmc) Fix lockless cache validation data race Date: Sat, 11 Jul 2026 15:57:53 +0800 Message-Id: <20260711075754.11358-3-fourdollars@debian.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260711075754.11358-1-fourdollars@debian.org> References: <20260710123236.10508-1-fourdollars@debian.org> <20260711075754.11358-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