Skip to content

Commit 9686f04

Browse files
committed
thermal: ACPI: Discard trips table after zone registration
Because the thermal core creates and uses its own copy of the trips table passed to thermal_zone_device_register_with_trips(), it is not necessary to hold on to a local copy of it any more after the given thermal zone has been registered. Accordingly, modify the ACPI thermal driver to store the trips table passed to thermal_zone_device_register_with_trips() in a local variable which is automatically discarded when acpi_thermal_add() returns to its caller. Also make some additional code simplifications unlocked by the above change. Signed-off-by: Rafael J. Wysocki <[email protected]> Reviewed-by: Stanislaw Gruszka <[email protected]> Acked-by: Daniel Lezcano <[email protected]>
1 parent 9b0a627 commit 9686f04

File tree

1 file changed

+20
-37
lines changed

1 file changed

+20
-37
lines changed

drivers/acpi/thermal.c

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747

4848
#define ACPI_THERMAL_TRIP_PASSIVE (-1)
4949

50+
#define ACPI_THERMAL_MAX_NR_TRIPS (ACPI_THERMAL_MAX_ACTIVE + 3)
51+
5052
/*
5153
* This exception is thrown out in two cases:
5254
* 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
@@ -112,7 +114,6 @@ struct acpi_thermal {
112114
unsigned long polling_frequency;
113115
volatile u8 zombie;
114116
struct acpi_thermal_trips trips;
115-
struct thermal_trip *trip_table;
116117
struct thermal_zone_device *thermal_zone;
117118
int kelvin_offset; /* in millidegrees */
118119
struct work_struct thermal_check_work;
@@ -451,26 +452,19 @@ static bool acpi_thermal_init_trip(struct acpi_thermal *tz, int index)
451452
return false;
452453
}
453454

454-
static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
455+
static void acpi_thermal_get_trip_points(struct acpi_thermal *tz)
455456
{
456-
unsigned int count = 0;
457457
int i;
458458

459-
if (acpi_thermal_init_trip(tz, ACPI_THERMAL_TRIP_PASSIVE))
460-
count++;
459+
acpi_thermal_init_trip(tz, ACPI_THERMAL_TRIP_PASSIVE);
461460

462461
for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
463-
if (acpi_thermal_init_trip(tz, i))
464-
count++;
465-
else
462+
if (!acpi_thermal_init_trip(tz, i))
466463
break;
467-
468464
}
469465

470466
while (++i < ACPI_THERMAL_MAX_ACTIVE)
471467
tz->trips.active[i].trip.temp_dk = THERMAL_TEMP_INVALID;
472-
473-
return count;
474468
}
475469

476470
/* sys I/F for generic thermal sysfs support */
@@ -662,13 +656,14 @@ static void acpi_thermal_zone_sysfs_remove(struct acpi_thermal *tz)
662656
}
663657

664658
static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz,
659+
const struct thermal_trip *trip_table,
665660
unsigned int trip_count,
666661
int passive_delay)
667662
{
668663
int result;
669664

670665
tz->thermal_zone = thermal_zone_device_register_with_trips("acpitz",
671-
tz->trip_table,
666+
trip_table,
672667
trip_count,
673668
0, tz,
674669
&acpi_thermal_zone_ops,
@@ -823,10 +818,10 @@ static void acpi_thermal_free_thermal_zone(struct acpi_thermal *tz)
823818

824819
static int acpi_thermal_add(struct acpi_device *device)
825820
{
821+
struct thermal_trip trip_table[ACPI_THERMAL_MAX_NR_TRIPS] = { 0 };
826822
struct acpi_thermal_trip *acpi_trip;
827823
struct thermal_trip *trip;
828824
struct acpi_thermal *tz;
829-
unsigned int trip_count;
830825
int crit_temp, hot_temp;
831826
int passive_delay = 0;
832827
int result;
@@ -848,21 +843,10 @@ static int acpi_thermal_add(struct acpi_device *device)
848843
acpi_thermal_aml_dependency_fix(tz);
849844

850845
/* Get trip points [_CRT, _PSV, etc.] (required). */
851-
trip_count = acpi_thermal_get_trip_points(tz);
846+
acpi_thermal_get_trip_points(tz);
852847

853848
crit_temp = acpi_thermal_get_critical_trip(tz);
854-
if (crit_temp != THERMAL_TEMP_INVALID)
855-
trip_count++;
856-
857849
hot_temp = acpi_thermal_get_hot_trip(tz);
858-
if (hot_temp != THERMAL_TEMP_INVALID)
859-
trip_count++;
860-
861-
if (!trip_count) {
862-
pr_warn(FW_BUG "No valid trip points!\n");
863-
result = -ENODEV;
864-
goto free_memory;
865-
}
866850

867851
/* Get temperature [_TMP] (required). */
868852
result = acpi_thermal_get_temperature(tz);
@@ -881,13 +865,7 @@ static int acpi_thermal_add(struct acpi_device *device)
881865

882866
acpi_thermal_guess_offset(tz, crit_temp);
883867

884-
trip = kcalloc(trip_count, sizeof(*trip), GFP_KERNEL);
885-
if (!trip) {
886-
result = -ENOMEM;
887-
goto free_memory;
888-
}
889-
890-
tz->trip_table = trip;
868+
trip = trip_table;
891869

892870
if (crit_temp != THERMAL_TEMP_INVALID) {
893871
trip->type = THERMAL_TRIP_CRITICAL;
@@ -923,9 +901,17 @@ static int acpi_thermal_add(struct acpi_device *device)
923901
trip++;
924902
}
925903

926-
result = acpi_thermal_register_thermal_zone(tz, trip_count, passive_delay);
904+
if (trip == trip_table) {
905+
pr_warn(FW_BUG "No valid trip points!\n");
906+
result = -ENODEV;
907+
goto free_memory;
908+
}
909+
910+
result = acpi_thermal_register_thermal_zone(tz, trip_table,
911+
trip - trip_table,
912+
passive_delay);
927913
if (result)
928-
goto free_trips;
914+
goto free_memory;
929915

930916
refcount_set(&tz->thermal_check_count, 3);
931917
mutex_init(&tz->thermal_check_lock);
@@ -944,8 +930,6 @@ static int acpi_thermal_add(struct acpi_device *device)
944930
flush_wq:
945931
flush_workqueue(acpi_thermal_pm_queue);
946932
acpi_thermal_unregister_thermal_zone(tz);
947-
free_trips:
948-
kfree(tz->trip_table);
949933
free_memory:
950934
acpi_thermal_free_thermal_zone(tz);
951935

@@ -966,7 +950,6 @@ static void acpi_thermal_remove(struct acpi_device *device)
966950

967951
flush_workqueue(acpi_thermal_pm_queue);
968952
acpi_thermal_unregister_thermal_zone(tz);
969-
kfree(tz->trip_table);
970953
acpi_thermal_free_thermal_zone(tz);
971954
}
972955

0 commit comments

Comments
 (0)