Message ID | 20230703171502.44657-1-nfraprado@collabora.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp687695vqx; Mon, 3 Jul 2023 10:44:59 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7vgICTBMVvyQouz6Txq0OgA39F2+5fTt0Z8SfS/T0g6r4yCN/EHsIw5JeK4e2yTy8V8QlV X-Received: by 2002:a05:6a20:8e19:b0:12d:77e:ba3 with SMTP id y25-20020a056a208e1900b0012d077e0ba3mr15512854pzj.0.1688406299646; Mon, 03 Jul 2023 10:44:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688406299; cv=none; d=google.com; s=arc-20160816; b=x74vqfjeHBG6JbfBDJJExRhQm7VPSeklUgEZ1G0Hz4olRy/JErlkyZkJ22zKlgtFvF piP0Mrgagt5oxed0BjClvScphCnSfDX/9PrmsUYF3h41dFz7F9ynq2tt5Z6gkeYgakOb t8ndl3k1HqD/Vwa4GNSDJ9JfSKNRhFJCDwDgDn2ADO0IF0UQXlOf3lP9yRCfn2hVR3w1 MJA+tiU7k6MqZsW8KUvPfAwcjRtzCNQ25I2WOyJMghlR04q5Ye3lSwvO6WFuk3YUl2RG 4toLad4/FH3mnQ9s6nfUOEqd6so2/9nBK1PXVHZh8LzkMUY8mjW4QRVXuTnZAkqiczwj uMAw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=YMUtXxpxYpRox2xlhZwJpwL9gOg+UbEnP2BzFu07fHo=; fh=n+dst8OFdcbzZVUMjC0Ih6eai1i6aQ4o6uubDHqGtZk=; b=0w3bd9wCEFmQrhX0VeY5vvnjUPeYzTbr1sstilRVIp+xXTWw8oKptNpqPdjlFWVKGg Wuzp7kwvJUoGSX5sj3nhdunzVM8dwYbuc/mpCNzKF2iVn/5vHYe971d/EEkoL4aFL2V5 vBxAHfYVYhGcGkcqnuM8HSH2PaxlIYBcFp7Axj6LdmPnhz4Ak5wcnIJrteZiTv2VMs7s lQRgcjYF0BQ5W6hi4vqwdXvIyl5jadh8X1JX3UsvG8WtBfn9/NPwQHFs4iRCgU9mYzb9 Wu3T+2V0HdjlvjeGiYk7/kXOPZvXOP3HCUGl/0ogvm8OHc7d4sIkwd5E4i/Crkx+OHJ7 yiVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=eShCOhwf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y28-20020a634b1c000000b00553ebb05d25si18026618pga.80.2023.07.03.10.44.43; Mon, 03 Jul 2023 10:44:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=eShCOhwf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230437AbjGCRPV (ORCPT <rfc822;ivan.orlov0322@gmail.com> + 99 others); Mon, 3 Jul 2023 13:15:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229804AbjGCRPU (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 3 Jul 2023 13:15:20 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA4B9CD; Mon, 3 Jul 2023 10:15:19 -0700 (PDT) Received: from notapiano.myfiosgateway.com (zone.collabora.co.uk [167.235.23.81]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: nfraprado) by madras.collabora.co.uk (Postfix) with ESMTPSA id BB7CE6606F7D; Mon, 3 Jul 2023 18:15:16 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1688404518; bh=+FspP4SEXLpd8UGez/YnH27IKQxsKfy3tcMyJKUz3yk=; h=From:To:Cc:Subject:Date:From; b=eShCOhwfHuWkARISle8RvontgqPMwOzK0A+y0Swu8MYAf2U4nHZgrYGt/m7XnOWH1 /T909DFVycl40YdEOV0Aapa17mXExSkGDaZX0/1dp11i0YqEKfeAhovI4kZsNtPLCC cTNMxpS4LKJe2Tn4Zkqldnapn/piPFIBJJtMXSTBU7aiDh18V5QdtoUa9AjJMUCOcs trPcGvU2IcCsAVIKsAXy+9ZuHokvhUx5mFwhEu2bLyi/5jWI+sUZKnuizIOyw1+g2d BBW7T57VY23pQM+1gsjcgAsSmt30uVQ6sm0Dk5F9mO83+1A2cShO72aMGI40pf57dq k5yuX1Vy94Rsg== From: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= <nfraprado@collabora.com> To: "Rafael J . Wysocki" <rafael@kernel.org>, Daniel Lezcano <daniel.lezcano@linaro.org> Cc: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>, kernel@collabora.com, =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= <nfraprado@collabora.com>, Amit Kucheria <amitk@kernel.org>, Zhang Rui <rui.zhang@intel.com>, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org Subject: [PATCH] thermal/core: Don't update trip points inside the hysteresis range Date: Mon, 3 Jul 2023 13:14:44 -0400 Message-ID: <20230703171502.44657-1-nfraprado@collabora.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770422323986720078?= X-GMAIL-MSGID: =?utf-8?q?1770422323986720078?= |
Series |
thermal/core: Don't update trip points inside the hysteresis range
|
|
Commit Message
Nícolas F. R. A. Prado
July 3, 2023, 5:14 p.m. UTC
When searching for the trip points that need to be set, the nearest trip
point's temperature is used for the high trip, while the nearest trip
point's temperature minus the hysteresis is used for the low trip. The
issue with this logic is that when the current temperature is inside a
trip point's hysteresis range, both high and low trips will come from
the same trip point. As a consequence instability can still occur like
this:
* the temperature rises slightly and enters the hysteresis range of a
trip point
* polling happens and updates the trip points to the hysteresis range
* the temperature falls slightly, exiting the hysteresis range, crossing
the trip point and triggering an IRQ, the trip points are updated
* repeat
So even though the current hysteresis implementation prevents
instability from happening due to IRQs triggering on the same
temperature value, both ways, it doesn't prevent it from happening due
to an IRQ on one way and polling on the other.
To properly implement a hysteresis behavior, when inside the hysteresis
range, don't update the trip points. This way, the previously set trip
points will stay in effect, which will in a way remember the previous
state (if the temperature signal came from above or below the range) and
therefore have the right trip point already set. The exception is if
there was no previous trip point set, in which case a previous state
doesn't exist, and so it's sensible to allow the hysteresis range as
trip points.
Signed-off-by: Nícolas F. R. A. Prado <nfraprado@collabora.com>
---
drivers/thermal/thermal_trip.c | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
Comments
On Mon, Jul 3, 2023 at 7:15 PM Nícolas F. R. A. Prado <nfraprado@collabora.com> wrote: > > When searching for the trip points that need to be set, the nearest trip > point's temperature is used for the high trip, while the nearest trip > point's temperature minus the hysteresis is used for the low trip. The > issue with this logic is that when the current temperature is inside a > trip point's hysteresis range, both high and low trips will come from > the same trip point. As a consequence instability can still occur like > this: > * the temperature rises slightly and enters the hysteresis range of a > trip point > * polling happens and updates the trip points to the hysteresis range > * the temperature falls slightly, exiting the hysteresis range, crossing > the trip point and triggering an IRQ, the trip points are updated > * repeat > > So even though the current hysteresis implementation prevents > instability from happening due to IRQs triggering on the same > temperature value, both ways, it doesn't prevent it from happening due > to an IRQ on one way and polling on the other. > > To properly implement a hysteresis behavior, when inside the hysteresis > range, don't update the trip points. This way, the previously set trip > points will stay in effect, which will in a way remember the previous > state (if the temperature signal came from above or below the range) and > therefore have the right trip point already set. The exception is if > there was no previous trip point set, in which case a previous state > doesn't exist, and so it's sensible to allow the hysteresis range as > trip points. > > Signed-off-by: Nícolas F. R. A. Prado <nfraprado@collabora.com> > > --- > > drivers/thermal/thermal_trip.c | 21 +++++++++++++++++++-- > 1 file changed, 19 insertions(+), 2 deletions(-) > > diff --git a/drivers/thermal/thermal_trip.c b/drivers/thermal/thermal_trip.c > index 907f3a4d7bc8..c386ac5d8bad 100644 > --- a/drivers/thermal/thermal_trip.c > +++ b/drivers/thermal/thermal_trip.c > @@ -57,6 +57,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz) > { > struct thermal_trip trip; > int low = -INT_MAX, high = INT_MAX; > + int low_trip_id = -1, high_trip_id = -2; > int i, ret; > > lockdep_assert_held(&tz->lock); > @@ -73,18 +74,34 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz) > > trip_low = trip.temperature - trip.hysteresis; > > - if (trip_low < tz->temperature && trip_low > low) > + if (trip_low < tz->temperature && trip_low > low) { > low = trip_low; > + low_trip_id = i; > + } > I think I get the idea, but wouldn't a similar effect be achieved by adding an "else" here? > if (trip.temperature > tz->temperature && > - trip.temperature < high) > + trip.temperature < high) { > high = trip.temperature; > + high_trip_id = i; > + } > } > > /* No need to change trip points */ > if (tz->prev_low_trip == low && tz->prev_high_trip == high) > return; > > + /* > + * If the current temperature is inside a trip point's hysteresis range, > + * don't update the trip points, rely on the previously set ones to > + * rememember the previous state. > + * > + * Unless no previous trip point was set, in which case there's no > + * previous state to remember. > + */ > + if ((tz->prev_low_trip > -INT_MAX || tz->prev_high_trip < INT_MAX) && > + low_trip_id == high_trip_id) > + return; > + > tz->prev_low_trip = low; > tz->prev_high_trip = high; > > --
On Tue, Aug 22, 2023 at 12:25 AM Nícolas F. R. A. Prado <nfraprado@collabora.com> wrote: > > On Mon, Aug 21, 2023 at 11:10:27PM +0200, Rafael J. Wysocki wrote: > > On Mon, Jul 3, 2023 at 7:15 PM Nícolas F. R. A. Prado > > <nfraprado@collabora.com> wrote: > > > > > > When searching for the trip points that need to be set, the nearest trip > > > point's temperature is used for the high trip, while the nearest trip > > > point's temperature minus the hysteresis is used for the low trip. The > > > issue with this logic is that when the current temperature is inside a > > > trip point's hysteresis range, both high and low trips will come from > > > the same trip point. As a consequence instability can still occur like > > > this: > > > * the temperature rises slightly and enters the hysteresis range of a > > > trip point > > > * polling happens and updates the trip points to the hysteresis range > > > * the temperature falls slightly, exiting the hysteresis range, crossing > > > the trip point and triggering an IRQ, the trip points are updated > > > * repeat > > > > > > So even though the current hysteresis implementation prevents > > > instability from happening due to IRQs triggering on the same > > > temperature value, both ways, it doesn't prevent it from happening due > > > to an IRQ on one way and polling on the other. > > > > > > To properly implement a hysteresis behavior, when inside the hysteresis > > > range, don't update the trip points. This way, the previously set trip > > > points will stay in effect, which will in a way remember the previous > > > state (if the temperature signal came from above or below the range) and > > > therefore have the right trip point already set. The exception is if > > > there was no previous trip point set, in which case a previous state > > > doesn't exist, and so it's sensible to allow the hysteresis range as > > > trip points. > > > > > > Signed-off-by: Nícolas F. R. A. Prado <nfraprado@collabora.com> > > > > > > --- > > > > > > drivers/thermal/thermal_trip.c | 21 +++++++++++++++++++-- > > > 1 file changed, 19 insertions(+), 2 deletions(-) > > > > > > diff --git a/drivers/thermal/thermal_trip.c b/drivers/thermal/thermal_trip.c > > > index 907f3a4d7bc8..c386ac5d8bad 100644 > > > --- a/drivers/thermal/thermal_trip.c > > > +++ b/drivers/thermal/thermal_trip.c > > > @@ -57,6 +57,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz) > > > { > > > struct thermal_trip trip; > > > int low = -INT_MAX, high = INT_MAX; > > > + int low_trip_id = -1, high_trip_id = -2; > > > int i, ret; > > > > > > lockdep_assert_held(&tz->lock); > > > @@ -73,18 +74,34 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz) > > > > > > trip_low = trip.temperature - trip.hysteresis; > > > > > > - if (trip_low < tz->temperature && trip_low > low) > > > + if (trip_low < tz->temperature && trip_low > low) { > > > low = trip_low; > > > + low_trip_id = i; > > > + } > > > > > > > I think I get the idea, but wouldn't a similar effect be achieved by > > adding an "else" here? > > No. That would only fix the problem in one direction, namely, when the > temperature entered the hysteresis range from above. But when the temperature > entered the range from below, we'd need to check the high threshold first to > achieve the same result. > > The way I've implemented here is the simplest I could think of that works for > both directions. Well, what about the replacement patch below (untested)? --- drivers/thermal/thermal_trip.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) Index: linux-pm/drivers/thermal/thermal_trip.c =================================================================== --- linux-pm.orig/drivers/thermal/thermal_trip.c +++ linux-pm/drivers/thermal/thermal_trip.c @@ -55,6 +55,7 @@ void __thermal_zone_set_trips(struct the { struct thermal_trip trip; int low = -INT_MAX, high = INT_MAX; + bool same_trip = false; int i, ret; lockdep_assert_held(&tz->lock); @@ -63,6 +64,7 @@ void __thermal_zone_set_trips(struct the return; for (i = 0; i < tz->num_trips; i++) { + bool low_set = false; int trip_low; ret = __thermal_zone_get_trip(tz, i , &trip); @@ -71,18 +73,31 @@ void __thermal_zone_set_trips(struct the trip_low = trip.temperature - trip.hysteresis; - if (trip_low < tz->temperature && trip_low > low) + if (trip_low < tz->temperature && trip_low > low) { low = trip_low; + low_set = true; + same_trip = false; + } if (trip.temperature > tz->temperature && - trip.temperature < high) + trip.temperature < high) { high = trip.temperature; + same_trip = low_set; + } } /* No need to change trip points */ if (tz->prev_low_trip == low && tz->prev_high_trip == high) return; + /* + * If "high" and "low" are the same, skip the change unless this is the + * first time. + */ + if (same_trip && (tz->prev_low_trip != -INT_MAX || + tz->prev_high_trip != INT_MAX)) + return; + tz->prev_low_trip = low; tz->prev_high_trip = high;
diff --git a/drivers/thermal/thermal_trip.c b/drivers/thermal/thermal_trip.c index 907f3a4d7bc8..c386ac5d8bad 100644 --- a/drivers/thermal/thermal_trip.c +++ b/drivers/thermal/thermal_trip.c @@ -57,6 +57,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz) { struct thermal_trip trip; int low = -INT_MAX, high = INT_MAX; + int low_trip_id = -1, high_trip_id = -2; int i, ret; lockdep_assert_held(&tz->lock); @@ -73,18 +74,34 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz) trip_low = trip.temperature - trip.hysteresis; - if (trip_low < tz->temperature && trip_low > low) + if (trip_low < tz->temperature && trip_low > low) { low = trip_low; + low_trip_id = i; + } if (trip.temperature > tz->temperature && - trip.temperature < high) + trip.temperature < high) { high = trip.temperature; + high_trip_id = i; + } } /* No need to change trip points */ if (tz->prev_low_trip == low && tz->prev_high_trip == high) return; + /* + * If the current temperature is inside a trip point's hysteresis range, + * don't update the trip points, rely on the previously set ones to + * rememember the previous state. + * + * Unless no previous trip point was set, in which case there's no + * previous state to remember. + */ + if ((tz->prev_low_trip > -INT_MAX || tz->prev_high_trip < INT_MAX) && + low_trip_id == high_trip_id) + return; + tz->prev_low_trip = low; tz->prev_high_trip = high;