Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp3982855pxb; Mon, 4 Oct 2021 14:23:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwKMo7M1b7gxb4UCBxndoUDpOl0nBd0Q+hFKT6ZH9Vuipy1OUfHKwUfZcQ0/bjQ9HBVTQ5K X-Received: by 2002:a63:2b8c:: with SMTP id r134mr12440328pgr.420.1633382601741; Mon, 04 Oct 2021 14:23:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633382601; cv=none; d=google.com; s=arc-20160816; b=JsPbLXf8GtZcu0BfFdEupReW9B7dGKdtodZT/St6ahki6RSGBJ/D/Wq9uZtnRY5oX+ U6mT88iPf2N+n6BHjwaAlHtqhXfEkEOKVAGMKFaV2R6htPd7GvFPqj+3JDkvMGsACEim GbgUjx74zvnmd6l+C6A2wwVsvBV0dQNWIeCze6FqEAqCJ0yu51bGdnXLyruLl2JxuaJq Kbh/8bhe60kNV5mP6Tiyt/v3DarmohEmM0ZEjMyQbf08DrkHzR5wM8I2A6tdq6RdI4aq 0O8rXFQdr/ulidf5Pcz3fQHYEyIeMB/YI0lU+UgOOeumaQqeJ888yfFdYKPbwAS9zbxF 3gqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=UWZlwUB/MCzecrieue83osK2ye++St4NW4aEz2takbY=; b=zvNDRSWScnVv3zQ/KJwgK5TKu/eQnOT6vqHqJGFkJN9PJ7DP7jBAoqxmXLcun6XfdE aa8tZkBb5ig/gZSwuhv5eDXFSjddfQM16/IwNRcqqK2rT6xN5iVlw+eHACRzIZWxG5We KDA43tTUcOGa7in6Cz+GwiX9Bg6BgQIaAvPDvts6juhkeprfQJdm3TARc1cttulX7DAl R7KKCrB8ZD9bCs5vp+9j6iPeBd0jyhebbwcgZOfyTpXFoAA5upU+OAXBMjlpK2ahrfAl gK1Ed3uuYxEartepYd3xDFdhBPCXATu0FCQhSvvDduV6O7BpFoTeZw0JcB4Wigktu6lZ Aufw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Isrzjro2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p5si18575923plo.204.2021.10.04.14.23.08; Mon, 04 Oct 2021 14:23:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Isrzjro2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237328AbhJDNcB (ORCPT + 99 others); Mon, 4 Oct 2021 09:32:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:43308 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235536AbhJDNaI (ORCPT ); Mon, 4 Oct 2021 09:30:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D863F61BA4; Mon, 4 Oct 2021 13:13:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633353212; bh=4tqAWzyryKYmgALVAX8+oJ1ZlePLWu4Bus+uT4VdClg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Isrzjro2zecsGR03UJCrtDflriK9nWKJNFhWVp2XBrJ4ItthQ1RJ5kuZyzBOJTFIV YDYlJUvziiTpV445nI7h6DJ/875A993YSmprW3nyeSegGVH/BSp5RxldxQi+EjDQpm o1TjWgE4gXbJJM0qHTj3tpyP7kFZwtpz/Kjd1fqo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Paul Fertser , Guenter Roeck Subject: [PATCH 5.14 040/172] hwmon: (tmp421) handle I2C errors Date: Mon, 4 Oct 2021 14:51:30 +0200 Message-Id: <20211004125046.280573956@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211004125044.945314266@linuxfoundation.org> References: <20211004125044.945314266@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Paul Fertser commit 2938b2978a70d4cc10777ee71c9e512ffe4e0f4b upstream. Function i2c_smbus_read_byte_data() can return a negative error number instead of the data read if I2C transaction failed for whatever reason. Lack of error checking can lead to serious issues on production hardware, e.g. errors treated as temperatures produce spurious critical temperature-crossed-threshold errors in BMC logs for OCP server hardware. The patch was tested with Mellanox OCP Mezzanine card emulating TMP421 protocol for temperature sensing which sometimes leads to I2C protocol error during early boot up stage. Fixes: 9410700b881f ("hwmon: Add driver for Texas Instruments TMP421/422/423 sensor chips") Cc: stable@vger.kernel.org Signed-off-by: Paul Fertser Link: https://lore.kernel.org/r/20210924093011.26083-1-fercerpav@gmail.com [groeck: dropped unnecessary line breaks] Signed-off-by: Guenter Roeck Signed-off-by: Greg Kroah-Hartman --- drivers/hwmon/tmp421.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) --- a/drivers/hwmon/tmp421.c +++ b/drivers/hwmon/tmp421.c @@ -119,38 +119,56 @@ static int temp_from_u16(u16 reg) return (temp * 1000 + 128) / 256; } -static struct tmp421_data *tmp421_update_device(struct device *dev) +static int tmp421_update_device(struct tmp421_data *data) { - struct tmp421_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client; + int ret = 0; int i; mutex_lock(&data->update_lock); if (time_after(jiffies, data->last_updated + (HZ / 2)) || !data->valid) { - data->config = i2c_smbus_read_byte_data(client, - TMP421_CONFIG_REG_1); + ret = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1); + if (ret < 0) + goto exit; + data->config = ret; for (i = 0; i < data->channels; i++) { - data->temp[i] = i2c_smbus_read_byte_data(client, - TMP421_TEMP_MSB[i]) << 8; - data->temp[i] |= i2c_smbus_read_byte_data(client, - TMP421_TEMP_LSB[i]); + ret = i2c_smbus_read_byte_data(client, TMP421_TEMP_MSB[i]); + if (ret < 0) + goto exit; + data->temp[i] = ret << 8; + + ret = i2c_smbus_read_byte_data(client, TMP421_TEMP_LSB[i]); + if (ret < 0) + goto exit; + data->temp[i] |= ret; } data->last_updated = jiffies; data->valid = 1; } +exit: mutex_unlock(&data->update_lock); - return data; + if (ret < 0) { + data->valid = 0; + return ret; + } + + return 0; } static int tmp421_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) { - struct tmp421_data *tmp421 = tmp421_update_device(dev); + struct tmp421_data *tmp421 = dev_get_drvdata(dev); + int ret = 0; + + ret = tmp421_update_device(tmp421); + if (ret) + return ret; switch (attr) { case hwmon_temp_input: