Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Documentation/devicetree/bindings/thermal/thermal-zones.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ patternProperties:
Requires a nonzero polling-delay-passive. If omitted, the governor
retains its existing estimate of k_pu divided by ten per poll.

critical-samples:
$ref: /schemas/types.yaml#/definitions/uint32
minimum: 1
description: |
Number of consecutive zone updates that must read at or above a
critical trip before it acts. For a sensor whose single readings
are noisy, this keeps one sample from shutting the system down
while a sustained excursion still does, after about this many
polling periods. Requires polling. If omitted, or 1, a critical
trip acts on the first reading at or above it.

thermal-sensors:
$ref: /schemas/types.yaml#/definitions/phandle-array
maxItems: 1
Expand Down Expand Up @@ -247,6 +258,15 @@ patternProperties:
minimum: 1
required:
- polling-delay-passive
- if:
required:
- critical-samples
then:
properties:
polling-delay:
minimum: 1
required:
- polling-delay

required:
- thermal-sensors
Expand Down
25 changes: 22 additions & 3 deletions arch/arm64/boot/dts/apple/t604x-j614-j616.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,11 @@
* ADT temp-sensor table gives next to the 110 C target for the PMP's
* sensors (the sochot level). The SMC sensors read a few degrees above
* the PMP's, so Linux shuts down at or before the hardware's own limit.
* A hot trip at 120 C only reports. Under any load the per-core
* excursions have peaked at 116.5-118 C, which the former 118 C
* critical trip turned into a shutdown.
* A hot trip at 120 C only reports, and the critical trip acts only
* after three consecutive samples at or above it (critical-samples),
* so one excursion cannot shut the machine down. Under any load the
* per-core excursions have peaked at 116.5-118 C, which the former
* 118 C critical trip turned into a shutdown.
*
* The GPU has no cooling device of its own: its firmware sets the GPU
* performance state and starts limiting at 101 C. So when a GPU die
Expand Down Expand Up @@ -406,6 +408,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <51300>;
thermal-sensors = <&smc_hwmon 0>; /* TCMz */

Expand Down Expand Up @@ -471,6 +474,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <2100>;
thermal-sensors = <&smc_hwmon 1>; /* Tex0 */

Expand Down Expand Up @@ -516,6 +520,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <2100>;
thermal-sensors = <&smc_hwmon 2>; /* Tex1 */

Expand Down Expand Up @@ -561,6 +566,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <2100>;
thermal-sensors = <&smc_hwmon 3>; /* Tex2 */

Expand Down Expand Up @@ -606,6 +612,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <2100>;
thermal-sensors = <&smc_hwmon 4>; /* Tex3 */

Expand Down Expand Up @@ -651,6 +658,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <25300>;
thermal-sensors = <&smc_hwmon 5>; /* Tpx0 */

Expand Down Expand Up @@ -697,6 +705,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <25300>;
thermal-sensors = <&smc_hwmon 6>; /* Tpx1 */

Expand Down Expand Up @@ -743,6 +752,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <25300>;
thermal-sensors = <&smc_hwmon 7>; /* Tpx2 */

Expand Down Expand Up @@ -789,6 +799,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <25300>;
thermal-sensors = <&smc_hwmon 8>; /* Tpx3 */

Expand Down Expand Up @@ -835,6 +846,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <25300>;
thermal-sensors = <&smc_hwmon 9>; /* Tpx4 */

Expand Down Expand Up @@ -881,6 +893,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <25300>;
thermal-sensors = <&smc_hwmon 10>; /* Tpx5 */

Expand Down Expand Up @@ -927,6 +940,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <23900>;
thermal-sensors = <&smc_hwmon 11>; /* Tpx8 */

Expand Down Expand Up @@ -973,6 +987,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <23900>;
thermal-sensors = <&smc_hwmon 12>; /* Tpx9 */

Expand Down Expand Up @@ -1019,6 +1034,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <23900>;
thermal-sensors = <&smc_hwmon 13>; /* TpxA */

Expand Down Expand Up @@ -1065,6 +1081,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <23900>;
thermal-sensors = <&smc_hwmon 14>; /* TpxB */

Expand Down Expand Up @@ -1111,6 +1128,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <23900>;
thermal-sensors = <&smc_hwmon 15>; /* TpxC */

Expand Down Expand Up @@ -1157,6 +1175,7 @@
polling-delay-passive = <1000>;
polling-delay = <1000>;
integral-time-constant-ms = <20000>;
critical-samples = <3>;
sustainable-power = <23900>;
thermal-sensors = <&smc_hwmon 16>; /* TpxD */

Expand Down
10 changes: 10 additions & 0 deletions drivers/thermal/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ config THERMAL_GOV_POWER_ALLOCATOR_KUNIT_TEST
The tests use synthetic thermal zones and do not need hardware.
Only useful for kernel developers; say N unless testing the governor.

config THERMAL_CRITICAL_SAMPLES_KUNIT_TEST
bool "KUnit tests for critical trip confirmation" if !KUNIT_ALL_TESTS
depends on KUNIT=y
default KUNIT_ALL_TESTS
help
Test that a zone with critical-samples acts on a critical trip only
after that many consecutive updates at or above it, once, and again
only after it has dropped below the trip. The tests need no hardware.
Only useful for kernel developers; say N unless testing the core.

config CPU_THERMAL
bool "Generic cpu cooling support"
depends on THERMAL_OF
Expand Down
42 changes: 40 additions & 2 deletions drivers/thermal/thermal_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,37 @@ static void thermal_governor_trip_crossed(struct thermal_governor *governor,
governor->trip_crossed(tz, trip, upward);
}

/*
* A zone with critical_samples above one acts on a critical trip only once
* that many updates in a row have read at or above it, so that one noisy
* sample cannot shut the system down. Polling keeps the updates coming.
*/
static bool thermal_zone_confirms_critical(struct thermal_zone_device *tz)
{
return tz->tzp && tz->tzp->critical_samples > 1 &&
(tz->polling_delay_jiffies || tz->passive_delay_jiffies);
}

static void thermal_zone_confirm_critical(struct thermal_zone_device *tz)
{
struct thermal_trip_desc *td;

if (!thermal_zone_confirms_critical(tz))
return;

for_each_trip_desc(tz, td) {
const struct thermal_trip *trip = &td->trip;

if (trip->type != THERMAL_TRIP_CRITICAL)
continue;

if (thermal_critical_confirm(&td->critical_count, tz->temperature,
trip->temperature,
tz->tzp->critical_samples))
handle_critical_trips(tz, trip);
}
}

static void thermal_trip_crossed(struct thermal_zone_device *tz,
struct thermal_trip_desc *td,
struct thermal_governor *governor,
Expand All @@ -417,8 +448,9 @@ static void thermal_trip_crossed(struct thermal_zone_device *tz,
if (upward) {
if (trip->type == THERMAL_TRIP_PASSIVE)
tz->passive++;
else if (trip->type == THERMAL_TRIP_CRITICAL ||
trip->type == THERMAL_TRIP_HOT)
else if (trip->type == THERMAL_TRIP_HOT ||
(trip->type == THERMAL_TRIP_CRITICAL &&
!thermal_zone_confirms_critical(tz)))
handle_critical_trips(tz, trip);

thermal_notify_tz_trip_up(tz, trip);
Expand Down Expand Up @@ -585,6 +617,8 @@ void __thermal_zone_device_update(struct thermal_zone_device *tz,

thermal_zone_handle_trips(tz, governor, &low, &high);

thermal_zone_confirm_critical(tz);

thermal_thresholds_handle(tz, &low, &high);

thermal_zone_set_trips(tz, low, high);
Expand Down Expand Up @@ -1871,3 +1905,7 @@ static int __init thermal_init(void)
return result;
}
postcore_initcall(thermal_init);

#if IS_ENABLED(CONFIG_THERMAL_CRITICAL_SAMPLES_KUNIT_TEST)
#include "thermal_core_critical_test.c"
#endif
25 changes: 25 additions & 0 deletions drivers/thermal/thermal_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/limits.h>
#include <linux/minmax.h>
#include <linux/thermal.h>

#include "thermal_netlink.h"
Expand All @@ -34,8 +36,31 @@ struct thermal_trip_desc {
struct list_head list_node;
struct list_head thermal_instances;
int threshold;
unsigned int critical_count;
};

/*
* thermal_critical_confirm - count updates at or above a critical trip
* @count: consecutive updates at or above the trip so far
* @temp: zone temperature of this update
* @trip_temp: critical trip temperature
* @samples: updates in a row that confirm the trip, 0 or 1 for the first
*
* Return: true on the update that confirms the trip, and only on that one:
* the zone has to drop below the trip and rise again to confirm it anew.
*/
static inline bool thermal_critical_confirm(unsigned int *count, int temp,
int trip_temp, unsigned int samples)
{
if (temp < trip_temp) {
*count = 0;
return false;
}
if (*count < UINT_MAX)
(*count)++;
return *count == max(samples, 1U);
}

/**
* struct thermal_governor - structure that holds thermal governor information
* @name: name of the governor
Expand Down
78 changes: 78 additions & 0 deletions drivers/thermal/thermal_core_critical_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: GPL-2.0
/* Included by thermal_core.c to exercise critical trip confirmation. */

#include <kunit/test.h>

static void thermal_critical_test_first_sample(struct kunit *test)
{
unsigned int count = 0;

/* 0 and 1 keep the existing behaviour: the first sample acts. */
KUNIT_EXPECT_TRUE(test, thermal_critical_confirm(&count, 125000, 125000, 0));
count = 0;
KUNIT_EXPECT_TRUE(test, thermal_critical_confirm(&count, 125000, 125000, 1));
}

static void thermal_critical_test_consecutive(struct kunit *test)
{
unsigned int count = 0;

KUNIT_EXPECT_FALSE(test, thermal_critical_confirm(&count, 126000, 125000, 3));
KUNIT_EXPECT_FALSE(test, thermal_critical_confirm(&count, 125000, 125000, 3));
KUNIT_EXPECT_TRUE(test, thermal_critical_confirm(&count, 130000, 125000, 3));
/* Confirmed once: staying above does not act again. */
KUNIT_EXPECT_FALSE(test, thermal_critical_confirm(&count, 130000, 125000, 3));
KUNIT_EXPECT_EQ(test, count, 4U);
}

static void thermal_critical_test_noise(struct kunit *test)
{
static const int samples[] = {
119000, 126000, 117000, 125500, 110000, 127000, 124999, 128000,
};
unsigned int count = 0;
int i;

/* Single excursions separated by readings below the trip never act. */
for (i = 0; i < ARRAY_SIZE(samples); i++)
KUNIT_EXPECT_FALSE(test, thermal_critical_confirm(&count, samples[i],
125000, 2));
KUNIT_EXPECT_EQ(test, count, 1U);
}

static void thermal_critical_test_rearm(struct kunit *test)
{
unsigned int count = 0;

KUNIT_EXPECT_FALSE(test, thermal_critical_confirm(&count, 125000, 125000, 2));
KUNIT_EXPECT_TRUE(test, thermal_critical_confirm(&count, 125000, 125000, 2));
KUNIT_EXPECT_FALSE(test, thermal_critical_confirm(&count, 100000, 125000, 2));
KUNIT_EXPECT_EQ(test, count, 0U);
KUNIT_EXPECT_FALSE(test, thermal_critical_confirm(&count, 125000, 125000, 2));
KUNIT_EXPECT_TRUE(test, thermal_critical_confirm(&count, 125000, 125000, 2));
}

static void thermal_critical_test_saturates(struct kunit *test)
{
unsigned int count = UINT_MAX - 1;

KUNIT_EXPECT_FALSE(test, thermal_critical_confirm(&count, 125000, 125000, 3));
KUNIT_EXPECT_FALSE(test, thermal_critical_confirm(&count, 125000, 125000, 3));
KUNIT_EXPECT_EQ(test, count, UINT_MAX);
}

static struct kunit_case thermal_critical_test_cases[] = {
KUNIT_CASE(thermal_critical_test_first_sample),
KUNIT_CASE(thermal_critical_test_consecutive),
KUNIT_CASE(thermal_critical_test_noise),
KUNIT_CASE(thermal_critical_test_rearm),
KUNIT_CASE(thermal_critical_test_saturates),
{}
};

static struct kunit_suite thermal_critical_test_suite = {
.name = "thermal_critical_samples",
.test_cases = thermal_critical_test_cases,
};

kunit_test_suite(thermal_critical_test_suite);
2 changes: 2 additions & 0 deletions drivers/thermal/thermal_of.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ static void thermal_of_parameters_init(struct device_node *np,
of_property_read_u32(np, "integral-time-constant-ms",
&tzp->integral_time_constant_ms);

of_property_read_u32(np, "critical-samples", &tzp->critical_samples);

/*
* For now, the thermal framework supports only one sensor per
* thermal zone. Thus, we are considering only the first two
Expand Down
Loading