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 F0E993921C6 for ; Sun, 2 Aug 2026 15:00:19 +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=1785682821; cv=none; b=FOMs7mY8s6etfhVCclqhEJkuIRdbTCWR8E5LfQ3zG5Lr0DuJXjaKbf8BsX92hfCRTWcQxh88KRrKwzK+M3Snmt8g0KmwFKQlVmMluaiOgMExvMv9mI+EtZIVQaxPuMUhyCazVO4pIKDvEDs+3Dr/Lz1kxv/x45w7wXo9ui/p1f8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785682821; c=relaxed/simple; bh=1ABiPp4UdOFHDmR/kVqOSaBai6KDqRepZunqk3UqW+o=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=iSOeumz5IodzwrxlFdPngtFHpR9YmE2tqDe1r4l6wZcY1DN818yWWBv+FMMmuIpvTozoZ7cZA11hlRHsQw5QaYty34DczyLB0/a/Odo4epK4fBkQIycog/rWXQyCW30mGrz8sgWnVaSFXa0/KEZjQMQkYa7Ujmjodnr/A3/RLQc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FdCWJWtv; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net 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="FdCWJWtv" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2cc73e322dbso23245895ad.1 for ; Sun, 02 Aug 2026 08:00:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785682819; x=1786287619; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:sender:from:to:cc:subject:date:message-id:reply-to :content-type; bh=m7GubX2oexnzGSUu+7RCqdftEDvzwkCvHfcYB+mewBQ=; b=FdCWJWtvfSawpyugPc2/UNCCX8f5j9jCnMiCqIlxcyhx/GCzhTZ2ciOELTYmivrcS/ jW6JeCDUdPBTn35pMkKRlvF5lZllYTj9O6JhA3ZX2h7VqT+jhgNUpXVCYO+AECJ3lEEn /ty9bHMCYlgyUhAVesxa8VM1Tgp8eyEzg5DIoeJibGfwgscAcdjYbei2xsBhFpLleUxe FTVk7mmloDSBK6QNa45KMc87EMVQRDakmd723VYfEZAZ3TEq6tQAggInJbGd92bLkU/S pHfkeyTxK+zAd+U3wjjozkeX13yEbh1rZbdeh32uNXR/LXx3qVyQ+PVB7FigROO8wxg5 NHFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785682819; x=1786287619; h=content-transfer-encoding:content-type:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:sender:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=m7GubX2oexnzGSUu+7RCqdftEDvzwkCvHfcYB+mewBQ=; b=DiC7+FX/I4UdhBKbrhsVkouw9bazkaL2epTCQ+gB3JKFOSkFudhefp23Xo/pm+PI5G EFabEAn3fsTZYGZLMOoCtEdnRSvmDFZIU/jX9HcTTu1Ypns2PL+MviSVWwjMr3ZBZB0V J1y+Aj62hevl+zJdr6jBuSJ/eSV5HucD26oW/WB3VRrtvzPr2OXu+7sLeeUhaLtiaC8l N3R4uOJ/m0hOzx8urnvXQbGohXYvHmQori9crwpoF31Ab1wEbX8eNgLm1/wLcs2eOTPS caZ2GUzcdydpGpTiB96B+mbydcTr4T4MdouIZHEBORWt1HCuITBo1IfT5jF7qxt186o2 4Gvw== X-Forwarded-Encrypted: i=1; AHgh+RobSxClAbFEWJ18CoyU7KWk5W3a5nJHdIz6HfoDyNveKXN1nKaErJcw8teKL7Ng/4EasiZ+EWbgE/0kYrY=@vger.kernel.org X-Gm-Message-State: AOJu0Yy5X3JwR87qNgFA+pdBFaoa2/RxGaBreRXcZ5qJvyr1iUSUNPo/ Zws2BX/2s0Zrc3duKLMO5B+SoIjHtYOPVRggYAwTqxt3lu+Dr6rcTh0Q X-Gm-Gg: AR+sD12pStq+v6UCDtr9XZG/S4fdQyKumwMsDTx1cl5v4DNKQ/GFPktRkm0OODPQKk2 G2dQUJV6FhQKUKuiwAB3yUI+f5UrgOYXp6qNrANE3yEJXgMc62O2cRwDZ+4kqGFf1a7ves14vdh i0TGlW2saEYhasxt/UnR5WstBNTN5uma65ellA+crgO7LY6tG4/PEG2ryl5jAjq6Ab26LxJ4mt8 7ytrvVRRH6jcOcGuwo1XMzh2pjFeCx3Kh1nLffTniVEvF1F8eOsgWGRRvHsKhLFxDJOm5fQBl12 yP2pjIC0xHVgwDjGNe0RlXfFBTQ3E9oyf7/E5q4TDJzJyjSjqDk9HPvmyFpKeIkrvc5KPBxs5z6 7fhC1er0WyX+q3bl0y1PiiW1XeL/OyAHodVbRxJ6KV4sbiNyf2Lj0DpBgo3PROgmfjjbv0b9h6I xGwsQZt/hdYCUsouI6BAIe+45bYzk1cDC1gPE54o0Ykb9YWF3kzgErk0IJzjTVg5LF1LUqqaSXN iETMgR/8r6XcBNI4mRDzae5xUqBd5io2Q== X-Received: by 2002:a17:902:f612:b0:2ca:f44b:63be with SMTP id d9443c01a7336-2d0521bc1b4mr67661545ad.3.1785682816962; Sun, 02 Aug 2026 08:00:16 -0700 (PDT) Received: from ?IPV6:2600:1700:e321:62f0:da43:aeff:fecc:bfd5? ([2600:1700:e321:62f0:da43:aeff:fecc:bfd5]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2d04ae5ac3dsm26830925ad.23.2026.08.02.08.00.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 02 Aug 2026 08:00:16 -0700 (PDT) Sender: Guenter Roeck Message-ID: <8f25c37b-8408-47f2-8069-87a1ef96ce7e@roeck-us.net> Date: Sun, 2 Aug 2026 08:00:15 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] hwmon: (sg2042-mcu) reject short uptime reads To: Ali Ahmet Memis Cc: Inochi Amaoto , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20260802130728.25721-1-ali@iusegentoo.com> Content-Language: en-US From: Guenter Roeck Autocrypt: addr=linux@roeck-us.net; keydata= xsFNBE6H1WcBEACu6jIcw5kZ5dGeJ7E7B2uweQR/4FGxH10/H1O1+ApmcQ9i87XdZQiB9cpN RYHA7RCEK2dh6dDccykQk3bC90xXMPg+O3R+C/SkwcnUak1UZaeK/SwQbq/t0tkMzYDRxfJ7 nyFiKxUehbNF3r9qlJgPqONwX5vJy4/GvDHdddSCxV41P/ejsZ8PykxyJs98UWhF54tGRWFl 7i1xvaDB9lN5WTLRKSO7wICuLiSz5WZHXMkyF4d+/O5ll7yz/o/JxK5vO/sduYDIlFTvBZDh gzaEtNf5tQjsjG4io8E0Yq0ViobLkS2RTNZT8ICq/Jmvl0SpbHRvYwa2DhNsK0YjHFQBB0FX IdhdUEzNefcNcYvqigJpdICoP2e4yJSyflHFO4dr0OrdnGLe1Zi/8Xo/2+M1dSSEt196rXaC kwu2KgIgmkRBb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scPJ0OVY7ly5Vm1nnoH4licaU5Y1nSkFR/j2douI5P7Cj687WuNMC6CcFd6j72kRfxklOqXw zvy+2NEcXyziiLXp84130yxAKXfluax9sZhhrhKT6VrD45S6N3HxJpXQ/RY/EX35neH2/F7B RgSloce2+zWfpELyS1qRkCUTt1tlGV2p+y2BPfXzrHn2vxvbhEn1QpQ6t+85FKN8YEhJEygJ F0WaMvQMNrk9UAUziVcUkLU52NS9SXqpVg8vgrO0JKx97IXFPcNh0DWsSj/0Y8HO/RDkGXYn FDMj7fZSPKyPQPmEHg+W/KzxSSfdgWIHF2QaQ0b2q1wOSec4Rti52ohmNSY+KNIW/zODhugJ np3900V20aS7eD9K8GTU0TGC1pyz6IVJwIE= In-Reply-To: <20260802130728.25721-1-ali@iusegentoo.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 8/2/26 06:07, Ali Ahmet Memis wrote: > uptime_show() asks for two bytes and only rejects a negative return: > > u8 time_val[2]; > > ret = i2c_smbus_read_i2c_block_data(mcu->client, REG_UPTIME, > sizeof(time_val), time_val); > if (ret < 0) > return ret; > > return sprintf(buf, "%d\n", > (time_val[0]) | (time_val[1] << 8)); > > i2c_smbus_read_i2c_block_data() returns the number of bytes the transfer > actually produced, which the device supplies and which can be shorter > than the length asked for: > > memcpy(values, &data.block[1], data.block[0]); > return data.block[0]; > > time_val is not initialised, so a reply of one byte leaves the high half > of the reported uptime as whatever was on the stack, and a reply of zero > bytes leaks both halves. Either way the value ends up in sysfs. > > Require the full two bytes. The other registers this driver reads go > through i2c_smbus_read_byte_data(), which returns the byte itself, so > the existing negative-only checks are right there. > I ended up asking AI: Does i2c_smbus_read_i2c_block_data() in ./drivers/i2c/i2c-core-smbus.c ever return less than the requested number of bytes unless there is an error ? Here is what it tells me: No, as long as the requested length is <= I2C_SMBUS_BLOCK_MAX (32 bytes), i2c-core-smbus.c will never return fewer bytes than requested unless a negative error code is returned. The only scenario where it returns a positive byte count less than the caller's requested length is if the caller passes length > 32, because the function clamps the request to I2C_SMBUS_BLOCK_MAX. ### Why this is the case 1. No Count Byte in I2C Block Read (I2C_SMBUS_I2C_BLOCK_DATA): Unlike an SMBus Block Read (smbus-protocol.rst), where the slave device sends a length/count byte as the first byte of data transferred, an I2C Block Read does not include a length byte from the target device. The master reads a fixed number of bytes determined by the caller. 2. In i2c-core-smbus.c: if (length > I2C_SMBUS_BLOCK_MAX) length = I2C_SMBUS_BLOCK_MAX; data.block[0] = length; status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, I2C_SMBUS_READ, command, I2C_SMBUS_I2C_BLOCK_DATA, &data); if (status < 0) return status; memcpy(values, &data.block[1], data.block[0]); return data.block[0]; Before calling i2c_smbus_xfer(), data.block[0] is initialized to the requested length (clamped to 32). Upon success (status >= 0), it returns data.block[0]. 3. In I2C Emulation (i2c-core-smbus.c): • The read message is set up for exactly data->block[0] bytes (msg[1].len = data->block[0];). • Unlike SMBus Block Data, it does not set I2C_M_RECV_LEN. • When copying the buffer back upon completion (i2c-core-smbus.c), data->block[0] is never modified and remains exactly equal to the requested length. 4. In Native Adapter Drivers (adapter->algo->smbus_xfer): • None of the bus drivers in drivers/i2c/busses/ implementing I2C_SMBUS_I2C_BLOCK_DATA (i2c-i801, i2c-viapro, i2c-amd8111, i2c-ismt, i2c-mlxbf, etc.) modify data->block[0] to a smaller value on success. • If a target device NAKs before the requested length bytes are read, the controller driver fails the transaction and returns a negative errno (e.g., -ENXIO, -EIO, or -EPROTO). Consequently, any successful call such as i2c_smbus_read_i2c_block_data(client, reg, I2C_SMBUS_BLOCK_MAX, buf) will always return exactly 32 (I2C_SMBUS_BLOCK_MAX) on success. Please refrain from sending fixes for non-issues. Thanks, Guenter