From patchwork Mon Jun 21 18:40:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Osipenko X-Patchwork-Id: 12335487 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 34688C48BE5 for ; Mon, 21 Jun 2021 18:41:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 186E160FF2 for ; Mon, 21 Jun 2021 18:41:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230157AbhFUSnd (ORCPT ); Mon, 21 Jun 2021 14:43:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229876AbhFUSna (ORCPT ); Mon, 21 Jun 2021 14:43:30 -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 58877C061574; Mon, 21 Jun 2021 11:41:15 -0700 (PDT) Received: by mail-lf1-x129.google.com with SMTP id m21so31792847lfg.13; Mon, 21 Jun 2021 11:41:15 -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=1jk2ZclpdBEYX/i3iFJW267F/ek+tRQuhMbQVF0BgrE=; b=RO7zJnbbWCPVzPMhvCitQtX5ZboD2tfKMGxjoo3FQgJ/tjcmitdtafqdwcZgAu8n1Q Y+jVITDQg/VHxVGvNLWCnxooNolmYP8KH9F59KAom9iDJp5kQQwD10Gv/9XsiGEYU+5+ s0eUkp2dYi+RPWBA5WHwh5ean7pIhki+v/LWg1L5yCCOlgmd1xuQcOLzlbIwSDL9Iwfo y6e0aGtoL9i4wWfAXhoYZlosP8nJ4yRgQhikJOkxlOWjevO7+8yZzyOd8BWLDvKZbYqC KZpgY50WSDEPL4RxdfrHGzUwg6oV8+7V4zJkyK+N6tqfKHxnVw8bFLj+eJugmMBZEshX 3gQQ== 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=1jk2ZclpdBEYX/i3iFJW267F/ek+tRQuhMbQVF0BgrE=; b=Cr3q1FHkz0DKVPSONayCYFZrOpcaqEDtS3JM09ZQudQV7pxvQMXqcrkJ8Q88MWLLeZ 89P+PtpNmFNXZHADd8d2YxplpmXLHG/pfEJAiT3Ve2k8tAmnpd5wEOnPtaWnq/jrlffN /JqsCJ6Gf9xUDg4Uv0xdZSxbCtYF4HfEVhzzDblFZDKM743jKi+FHPWywTooRGwywm8/ UPCNWzBnku8Q7HcfecudfLezw8jODX7JRgNsEtAR62wZuR8zhBhtV9GsDODiLT4xk0kj mNKe5B88pHjRcglMmEV4kf9+ImpK+mU5spoVEb+LwoX8moFUPLaJYdhhU19WjHsn0GwV o0YA== X-Gm-Message-State: AOAM530WhKUE0HpeLTMCb5pvpqL4U8+zSATTeP4BdXchXBoR5W7f4fxh Lhzfi30Tct7QKqw6h4+7ync= X-Google-Smtp-Source: ABdhPJxIzTooWK3bftvJAo8/sDoMM4tyc2qMr7vRHi9SUuHdVbQ02muwvy5EsUEbtudZeMz332Y6YA== X-Received: by 2002:a19:d7:: with SMTP id 206mr14745697lfa.457.1624300873719; Mon, 21 Jun 2021 11:41:13 -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 y22sm1950843lfa.145.2021.06.21.11.41.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Jun 2021 11:41:13 -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 v3 1/2] hwmon: (lm90) Prevent integer underflows of temperature calculations Date: Mon, 21 Jun 2021 21:40:57 +0300 Message-Id: <20210621184058.4110-2-digetx@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210621184058.4110-1-digetx@gmail.com> References: <20210621184058.4110-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 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/hwmon/lm90.c b/drivers/hwmon/lm90.c index b53f17511b05..ee6b8190f08e 100644 --- a/drivers/hwmon/lm90.c +++ b/drivers/hwmon/lm90.c @@ -1028,6 +1028,9 @@ static int lm90_set_temp11(struct lm90_data *data, int index, long val) struct reg *regp = ®[index]; int err; + /* prevent integer underflow */ + val = max(val, -128000l); + /* +16 degrees offset for temp2 for the LM99 */ if (data->kind == lm99 && index <= 2) val -= 16000; @@ -1088,6 +1091,9 @@ static int lm90_set_temp8(struct lm90_data *data, int index, long val) struct i2c_client *client = data->client; int err; + /* prevent integer underflow */ + val = max(val, -128000l); + /* +16 degrees offset for temp2 for the LM99 */ if (data->kind == lm99 && index == 3) val -= 16000; @@ -1130,6 +1136,9 @@ static int lm90_set_temphyst(struct lm90_data *data, long val) int temp; int err; + /* prevent integer underflow */ + val = max(val, -128000l); + if (data->kind == adt7461 || data->kind == tmp451) temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]); else if (data->kind == max6646) From patchwork Mon Jun 21 18:40:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Osipenko X-Patchwork-Id: 12335489 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 AD48BC49EAF for ; Mon, 21 Jun 2021 18:41:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A3FAC60FEA for ; Mon, 21 Jun 2021 18:41:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230206AbhFUSnd (ORCPT ); Mon, 21 Jun 2021 14:43:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230057AbhFUSnb (ORCPT ); Mon, 21 Jun 2021 14:43:31 -0400 Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBDA9C06175F; Mon, 21 Jun 2021 11:41:15 -0700 (PDT) Received: by mail-lf1-x12c.google.com with SMTP id u13so12508385lfk.2; Mon, 21 Jun 2021 11:41:15 -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=OaHrTHwOZlGiJZp19fuqPfINJsoeOnFyeJ/MKkylnkI=; b=EfIszghJdm21cMreSbQ996P4YHET0dcRSj3dKOVqDInMzRelDwW2na///uju3wbFB4 L3NnrpvKinrjb2pK/l/SuBtg5aQNPinZRpOY9mIda+IZS4aFdxtKAfUdA4cpmNIC/TTa XrkUgszFd9zc/OWujPxq6c2dFgnRxP3CZiN/oCvkkHOolOvUawfXW586wlfvnzk+78/O Y+M1pqaCt66DBNmESW4qkSmv4jnAYc7MZ3FlkeTx+In/x2uNXthFjDtOO5fDn+bM0nWq yGzcr1bMU6FylSmEVKPfBi6FoXGdzKlj4BG1n7EYZ2eDEMVTDOkM3MUlI0tWiE2Z7rcS NGuA== 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=OaHrTHwOZlGiJZp19fuqPfINJsoeOnFyeJ/MKkylnkI=; b=D7/jO2zagXoG1QOdBeDvBCOdIoFxqzJBtlqEpdIGjq14wYf3PjmgMQ7IhB/og46m4g cD/DGmpn56e0RL5qqWPvsM+hm8a4irM2VDaPtRaLUIFyMrX6MLL/B+EJRh/z35VLDmAF ckkMjW99cvUUo72SOsK6uFbj0yqKUT13TU0uHlLgrxAc3kBYUZzD+9mGuHEPMhqSBn/n 2RZqlzg6Z7ry2PsgquumilKBeOwoM0agHdElzRw46D8f4UCCuelpBPcOxz1akA7y4pCT FgWyrg33+dDxIhhCp4+TwyuvR6h2xieFWxMi4iV71PG2qhL8dhUpdYPE5CUV0ZuUrdRK m5yQ== X-Gm-Message-State: AOAM532ZbDYDC/k6tuTY8IMiMwjeJJka1krJ/q6zZnrWT7Y5XTr2+Esb fV3nDPB2xTfvb09yVPfk3gc= X-Google-Smtp-Source: ABdhPJxwJmm4BeibPP0lzIuhx6wMYOcQrZkZUFKCj4OeTFs2ubahQPT6J+uDdEPtnXKz7H2EBRHwFQ== X-Received: by 2002:a05:6512:92d:: with SMTP id f13mr15320408lft.186.1624300874353; Mon, 21 Jun 2021 11:41:14 -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 y22sm1950843lfa.145.2021.06.21.11.41.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Jun 2021 11:41:14 -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 v3 2/2] hwmon: Support set_trips() of thermal device ops Date: Mon, 21 Jun 2021 21:40:58 +0300 Message-Id: <20210621184058.4110-3-digetx@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210621184058.4110-1-digetx@gmail.com> References: <20210621184058.4110-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. Signed-off-by: Dmitry Osipenko --- drivers/hwmon/hwmon.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c index fd47ab4e6892..e74dc81e650d 100644 --- a/drivers/hwmon/hwmon.c +++ b/drivers/hwmon/hwmon.c @@ -153,8 +153,40 @@ 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; + + if (!chip->ops->write) + return 0; + + for (i = 1; info[i] && info[i]->type != hwmon_temp; i++) + continue; + + if (info[i] && info[i]->config[tdata->index] & HWMON_T_MIN) { + int err = chip->ops->write(tdata->dev, hwmon_temp, + hwmon_temp_min, tdata->index, low); + if (err < 0 && err != -EOPNOTSUPP) + return err; + } + + if (info[i] && info[i]->config[tdata->index] & HWMON_T_MAX) { + int err = chip->ops->write(tdata->dev, hwmon_temp, + hwmon_temp_max, tdata->index, high); + if (err < 0 && 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)