From patchwork Wed Jun 23 04:22:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Osipenko X-Patchwork-Id: 12338981 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7AEE8C49EA4 for ; Wed, 23 Jun 2021 04:22:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5FECA6135A for ; Wed, 23 Jun 2021 04:22:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230010AbhFWEZI (ORCPT ); Wed, 23 Jun 2021 00:25:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229850AbhFWEZH (ORCPT ); Wed, 23 Jun 2021 00:25:07 -0400 Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80607C061756; Tue, 22 Jun 2021 21:22:49 -0700 (PDT) Received: by mail-lf1-x132.google.com with SMTP id f30so1855080lfj.1; Tue, 22 Jun 2021 21:22:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=c2GV8eATeToxDrzoZV6+700+LfP1LGt3uPIeH8pfaR0=; b=HDJaVspKcZN4I4zKKnVXeIDejNMglmh7TUgW2aBhzcN1P7qK0RuhfLigDKBGg0gXpH nSlQ3e5C1L2Pmu+QXX3u+2nQzUAU6Jatb+OuE6UBaayaDpo96lMQP/C9Yu0m6Kdo4saR Roziu5NPgBahujWHnhtuE0AVcklJyl4ujIlZzYkmACkiqUMIVgQUKVDkARQ5iSNcmytT NZXyZ7mBKr2dLpta6iJzN1rBoYYWAk5JOWGag4nk5s9OAvjGy8UGgGtSktg65K2qilKW eTGZp2PeQQWbfUtHsBvcbDXjjaqBB2YrZPCVRjA/LBCiuoGOm3n44UejW/cRPlcMH4ha uX7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=c2GV8eATeToxDrzoZV6+700+LfP1LGt3uPIeH8pfaR0=; b=j5yAt3kDrCjgUdUt90Lb3QLQ+87oROeLzoORvT4Kkm0fjPeJBfb+4zFo+IF7pai0Nb Xw9bF8IA6RyZ2H2WyHUROdgAJUpY0mWIaJgNcqWsDbdqqH1e7keZuaifdPlJfOEfINHB X0uLxk26314kfBKyEAoZhKqQsOUkxkupks6X6LLs3GydlM24qfR4rdys1ANsuKZWSrKk wO8kdD630bZCVhhZZUcl7DOGw3yZe3TeI8wCvDCymY4o6g7jSDqAEMvCqW/93Fmya1qs 7DoPxTkGcJc3e6H3xJp4WqBHQIb9WPEiFX/VVCKP330PHq15q1ld19jHcjR84fGXieRW fs1w== X-Gm-Message-State: AOAM532BL9PSRPMTwpH2zycBsGHwSkc0DTduG2Xowi/A5lkPlL7Fqr9R o4zCs1ov96qAOWeidJqsQVQ= X-Google-Smtp-Source: ABdhPJzC0uw/+CV0rEkPKaQdUJxwXlikpYC8MRUx8ab63SVI0XrbOKLz5oLgPZPqru43n4QtCQ0gXw== X-Received: by 2002:a19:c743:: with SMTP id x64mr5429256lff.96.1624422167937; Tue, 22 Jun 2021 21:22:47 -0700 (PDT) Received: from localhost.localdomain (94-29-29-31.dynamic.spd-mgts.ru. [94.29.29.31]) by smtp.gmail.com with ESMTPSA id c9sm174027ljb.22.2021.06.22.21.22.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Jun 2021 21:22:47 -0700 (PDT) From: Dmitry Osipenko To: Zhang Rui , Daniel Lezcano , Amit Kucheria , Jean Delvare , Guenter Roeck Cc: linux-hwmon@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-tegra@vger.kernel.org Subject: [PATCH v5 1/2] hwmon: (lm90) Prevent integer underflows of temperature calculations Date: Wed, 23 Jun 2021 07:22:30 +0300 Message-Id: <20210623042231.16008-2-digetx@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623042231.16008-1-digetx@gmail.com> References: <20210623042231.16008-1-digetx@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org The min/max/crit and all other temperature values that are passed to the driver are unlimited and value that is close to INT_MIN results in integer underflow of the temperature calculations made by the driver for LM99 sensor. Temperature hysteresis is among those values that need to be limited, but limiting of hysteresis is independent from the sensor version. Add the missing limits. Signed-off-by: Dmitry Osipenko --- drivers/hwmon/lm90.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/hwmon/lm90.c b/drivers/hwmon/lm90.c index b53f17511b05..567b7c521f38 100644 --- a/drivers/hwmon/lm90.c +++ b/drivers/hwmon/lm90.c @@ -1029,8 +1029,11 @@ static int lm90_set_temp11(struct lm90_data *data, int index, long val) int err; /* +16 degrees offset for temp2 for the LM99 */ - if (data->kind == lm99 && index <= 2) + if (data->kind == lm99 && index <= 2) { + /* prevent integer underflow */ + val = max(val, -128000l); val -= 16000; + } if (data->kind == adt7461 || data->kind == tmp451) data->temp11[index] = temp_to_u16_adt7461(data, val); @@ -1089,8 +1092,11 @@ static int lm90_set_temp8(struct lm90_data *data, int index, long val) int err; /* +16 degrees offset for temp2 for the LM99 */ - if (data->kind == lm99 && index == 3) + if (data->kind == lm99 && index == 3) { + /* prevent integer underflow */ + val = max(val, -128000l); val -= 16000; + } if (data->kind == adt7461 || data->kind == tmp451) data->temp8[index] = temp_to_u8_adt7461(data, val); @@ -1137,6 +1143,9 @@ static int lm90_set_temphyst(struct lm90_data *data, long val) else temp = temp_from_s8(data->temp8[LOCAL_CRIT]); + /* prevent integer underflow */ + val = max(val, -128000l); + data->temp_hyst = hyst_to_reg(temp - val); err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST, data->temp_hyst); From patchwork Wed Jun 23 04:22:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Osipenko X-Patchwork-Id: 12338983 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7FCFBC4743C for ; Wed, 23 Jun 2021 04:22:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5C327613AD for ; Wed, 23 Jun 2021 04:22:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230049AbhFWEZK (ORCPT ); Wed, 23 Jun 2021 00:25:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230004AbhFWEZI (ORCPT ); Wed, 23 Jun 2021 00:25:08 -0400 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B8F7C06175F; Tue, 22 Jun 2021 21:22:50 -0700 (PDT) Received: by mail-lf1-x129.google.com with SMTP id t17so1892937lfq.0; Tue, 22 Jun 2021 21:22:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aJ1Yrh3P0J3boFKwBb0RVLJAPP2FR7myilZdK+WC06w=; b=RsuP/c+LK3gRUticzJF5jDp1VQlDgtgmvkOsnTR/SJI8v1GDX2FsnE0tuMt41KGO4c xYgLUsrzYHG76+F6jQE/s0UHpnJyoptn8QWgquvvginUoJ9dA4x7nKh5hisMu1p3XkFX kW3viZE8tahDdzcIkns/H6mOZ8X/i7VjYKjQPAt9Y7qpuMBsj73dpRB3A9XPAYsoJTUF HFcwKH08b/mztGMJit/4WgR6Km6Mc2J8rI4R2EhtL+1nxFCGnG+Yx6iOHanN2n8heRhA 9NOeLU+sN4MOfRyVf1eIaGrqLkY/vxwRFuroy9mG7/ANwur9JS2WpDXEqZLiJEN6PyXc mgJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aJ1Yrh3P0J3boFKwBb0RVLJAPP2FR7myilZdK+WC06w=; b=ldbtwO5AyHdLjZsTQObwgRXDsHmiylW5pWGBVYYlcL1KxQi8cOOyutDYy3sdR12ErJ uVmNTzoqxaro+oQJMMsjS9PzhfbjT6ll9waQm8zY2JnH345ViTUm6P+OFyF5UQ3lHRjm ZxoBoVw8AipMzEidDFkuolAGjbe2v6Zn7tWmiLPBZ/9rty0QgL3j9Xz7PEet/M5VLUGY NbTVkPxLgp3hl9Ra9HfsHGNMwXHY1Hyu44ArWs/ktc5IjU9PMx6Q0SH9IG/qCIkJDyi7 r0gb8niQ5VdO8Ux+XPJGm2srErSUbs/8bOntSEqZp3e4yDgx/3ohyT/MQgebtPumBCLo CZDg== X-Gm-Message-State: AOAM532saXtQvRmX6BKCfLhm92JQ/9borI5XLooRSde/RRclBuFA2MjY 0F8d+AnFmXrRSchaAkqsYVY= X-Google-Smtp-Source: ABdhPJwj3V4eoGBqFp7xzMGSLGtvGixqfhhA9TZaHqXaN/mi2doqQfx8BeLiYKpkeej2wtSQpiyjRA== X-Received: by 2002:a05:6512:31c5:: with SMTP id j5mr5474058lfe.116.1624422168612; Tue, 22 Jun 2021 21:22:48 -0700 (PDT) Received: from localhost.localdomain (94-29-29-31.dynamic.spd-mgts.ru. [94.29.29.31]) by smtp.gmail.com with ESMTPSA id c9sm174027ljb.22.2021.06.22.21.22.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Jun 2021 21:22:48 -0700 (PDT) From: Dmitry Osipenko To: Zhang Rui , Daniel Lezcano , Amit Kucheria , Jean Delvare , Guenter Roeck Cc: linux-hwmon@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-tegra@vger.kernel.org Subject: [PATCH v5 2/2] hwmon: Support set_trips() of thermal device ops Date: Wed, 23 Jun 2021 07:22:31 +0300 Message-Id: <20210623042231.16008-3-digetx@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623042231.16008-1-digetx@gmail.com> References: <20210623042231.16008-1-digetx@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org Support set_trips() callback of thermal device ops. This allows HWMON device to operatively notify thermal core about temperature changes, which is very handy to have in a case where HWMON sensor is used by CPU thermal zone that performs passive cooling and emergency shutdown on overheat. Thermal core will be able to react faster to temperature changes. The set_trips() callback is entirely optional. If HWMON sensor doesn't support setting thermal trips, then the callback is a NO-OP. The dummy callback has no effect on the thermal core. The temperature trips are either complement the temperature polling mechanism of thermal core or replace the polling if sensor can set the trips and polling is disabled by a particular device in a device-tree. Signed-off-by: Dmitry Osipenko --- drivers/hwmon/hwmon.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c index fd47ab4e6892..8d3b1dae31df 100644 --- a/drivers/hwmon/hwmon.c +++ b/drivers/hwmon/hwmon.c @@ -153,8 +153,44 @@ static int hwmon_thermal_get_temp(void *data, int *temp) return 0; } +static int hwmon_thermal_set_trips(void *data, int low, int high) +{ + struct hwmon_thermal_data *tdata = data; + struct hwmon_device *hwdev = to_hwmon_device(tdata->dev); + const struct hwmon_chip_info *chip = hwdev->chip; + const struct hwmon_channel_info **info = chip->info; + unsigned int i; + int err; + + if (!chip->ops->write) + return 0; + + for (i = 0; info[i] && info[i]->type != hwmon_temp; i++) + continue; + + if (!info[i]) + return 0; + + if (info[i]->config[tdata->index] & HWMON_T_MIN) { + err = chip->ops->write(tdata->dev, hwmon_temp, + hwmon_temp_min, tdata->index, low); + if (err && err != -EOPNOTSUPP) + return err; + } + + if (info[i]->config[tdata->index] & HWMON_T_MAX) { + err = chip->ops->write(tdata->dev, hwmon_temp, + hwmon_temp_max, tdata->index, high); + if (err && err != -EOPNOTSUPP) + return err; + } + + return 0; +} + static const struct thermal_zone_of_device_ops hwmon_thermal_ops = { .get_temp = hwmon_thermal_get_temp, + .set_trips = hwmon_thermal_set_trips, }; static void hwmon_thermal_remove_sensor(void *data)