Skip to content

Commit 2e57d10

Browse files
committed
ACPI: utils: Dynamically determine acpi_handle_list size
Address a long-standing "TBD" comment in the ACPI headers regarding the number of handles in struct acpi_handle_list. The number 10, which along with the comment dates back to 2.4.23, seems like it may have been arbitrarily chosen and isn't sufficient in all cases [1]. Finally change the code to dynamically determine the size of the handles table in struct acpi_handle_list and allocate it accordingly. Update the users of to struct acpi_handle_list to take the additional dynamic allocation into account. Link: https://lore.kernel.org/linux-acpi/[email protected] # [1] Co-developed-by: Vicki Pfau <[email protected]> Signed-off-by: Vicki Pfau <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 3e7d6f3 commit 2e57d10

File tree

6 files changed

+101
-18
lines changed

6 files changed

+101
-18
lines changed

drivers/acpi/acpi_lpss.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ static bool acpi_lpss_dep(struct acpi_device *adev, acpi_handle handle)
578578
{
579579
struct acpi_handle_list dep_devices;
580580
acpi_status status;
581+
bool ret = false;
581582
int i;
582583

583584
if (!acpi_has_method(adev->handle, "_DEP"))
@@ -591,11 +592,14 @@ static bool acpi_lpss_dep(struct acpi_device *adev, acpi_handle handle)
591592
}
592593

593594
for (i = 0; i < dep_devices.count; i++) {
594-
if (dep_devices.handles[i] == handle)
595-
return true;
595+
if (dep_devices.handles[i] == handle) {
596+
ret = true;
597+
break;
598+
}
596599
}
597600

598-
return false;
601+
acpi_handle_list_free(&dep_devices);
602+
return ret;
599603
}
600604

601605
static void acpi_lpss_link_consumer(struct device *dev1,

drivers/acpi/scan.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,7 @@ static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
20322032
mutex_unlock(&acpi_dep_list_lock);
20332033
}
20342034

2035+
acpi_handle_list_free(&dep_devices);
20352036
return count;
20362037
}
20372038

drivers/acpi/thermal.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static bool update_trip_devices(struct acpi_thermal *tz,
208208
struct acpi_thermal_trip *acpi_trip,
209209
int index, bool compare)
210210
{
211-
struct acpi_handle_list devices;
211+
struct acpi_handle_list devices = { 0 };
212212
char method[] = "_PSL";
213213
acpi_status status;
214214

@@ -218,18 +218,21 @@ static bool update_trip_devices(struct acpi_thermal *tz,
218218
method[3] = '0' + index;
219219
}
220220

221-
memset(&devices, 0, sizeof(devices));
222-
223221
status = acpi_evaluate_reference(tz->device->handle, method, NULL, &devices);
224222
if (ACPI_FAILURE(status)) {
225223
acpi_handle_info(tz->device->handle, "%s evaluation failure\n", method);
226224
return false;
227225
}
228226

229-
if (compare && memcmp(&acpi_trip->devices, &devices, sizeof(devices)))
227+
if (acpi_handle_list_equal(&acpi_trip->devices, &devices)) {
228+
acpi_handle_list_free(&devices);
229+
return true;
230+
}
231+
232+
if (compare)
230233
ACPI_THERMAL_TRIPS_EXCEPTION(tz, "device");
231234

232-
memcpy(&acpi_trip->devices, &devices, sizeof(devices));
235+
acpi_handle_list_replace(&acpi_trip->devices, &devices);
233236
return true;
234237
}
235238

@@ -842,6 +845,17 @@ static void acpi_thermal_check_fn(struct work_struct *work)
842845
mutex_unlock(&tz->thermal_check_lock);
843846
}
844847

848+
static void acpi_thermal_free_thermal_zone(struct acpi_thermal *tz)
849+
{
850+
int i;
851+
852+
acpi_handle_list_free(&tz->trips.passive.trip.devices);
853+
for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
854+
acpi_handle_list_free(&tz->trips.active[i].trip.devices);
855+
856+
kfree(tz);
857+
}
858+
845859
static int acpi_thermal_add(struct acpi_device *device)
846860
{
847861
struct acpi_thermal_trip *acpi_trip;
@@ -968,7 +982,7 @@ static int acpi_thermal_add(struct acpi_device *device)
968982
free_trips:
969983
kfree(tz->trip_table);
970984
free_memory:
971-
kfree(tz);
985+
acpi_thermal_free_thermal_zone(tz);
972986

973987
return result;
974988
}
@@ -988,7 +1002,7 @@ static void acpi_thermal_remove(struct acpi_device *device)
9881002
flush_workqueue(acpi_thermal_pm_queue);
9891003
acpi_thermal_unregister_thermal_zone(tz);
9901004

991-
kfree(tz);
1005+
acpi_thermal_free_thermal_zone(tz);
9921006
}
9931007

9941008
#ifdef CONFIG_PM_SLEEP

drivers/acpi/utils.c

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ acpi_evaluate_reference(acpi_handle handle,
370370
goto end;
371371
}
372372

373-
if (package->package.count > ACPI_MAX_HANDLES) {
373+
list->handles = kcalloc(package->package.count, sizeof(*list->handles), GFP_KERNEL);
374+
if (!list->handles) {
374375
kfree(package);
375376
return AE_NO_MEMORY;
376377
}
@@ -402,7 +403,8 @@ acpi_evaluate_reference(acpi_handle handle,
402403
end:
403404
if (ACPI_FAILURE(status)) {
404405
list->count = 0;
405-
//kfree(list->handles);
406+
kfree(list->handles);
407+
list->handles = NULL;
406408
}
407409

408410
kfree(buffer.pointer);
@@ -412,6 +414,61 @@ acpi_evaluate_reference(acpi_handle handle,
412414

413415
EXPORT_SYMBOL(acpi_evaluate_reference);
414416

417+
/**
418+
* acpi_handle_list_equal - Check if two ACPI handle lists are the same
419+
* @list1: First list to compare.
420+
* @list2: Second list to compare.
421+
*
422+
* Return true if the given ACPI handle lists are of the same size and
423+
* contain the same ACPI handles in the same order. Otherwise, return false.
424+
*/
425+
bool acpi_handle_list_equal(struct acpi_handle_list *list1,
426+
struct acpi_handle_list *list2)
427+
{
428+
return list1->count == list2->count &&
429+
!memcmp(list1->handles, list2->handles,
430+
list1->count * sizeof(acpi_handle));
431+
}
432+
EXPORT_SYMBOL_GPL(acpi_handle_list_equal);
433+
434+
/**
435+
* acpi_handle_list_replace - Replace one ACPI handle list with another
436+
* @dst: ACPI handle list to replace.
437+
* @src: Source ACPI handle list.
438+
*
439+
* Free the handles table in @dst, move the handles table from @src to @dst,
440+
* copy count from @src to @dst and clear @src.
441+
*/
442+
void acpi_handle_list_replace(struct acpi_handle_list *dst,
443+
struct acpi_handle_list *src)
444+
{
445+
if (dst->count)
446+
kfree(dst->handles);
447+
448+
dst->count = src->count;
449+
dst->handles = src->handles;
450+
451+
src->handles = NULL;
452+
src->count = 0;
453+
}
454+
EXPORT_SYMBOL_GPL(acpi_handle_list_replace);
455+
456+
/**
457+
* acpi_handle_list_free - Free the handles table in an ACPI handle list
458+
* @list: ACPI handle list to free.
459+
*
460+
* Free the handles table in @list and clear its count field.
461+
*/
462+
void acpi_handle_list_free(struct acpi_handle_list *list)
463+
{
464+
if (!list->count)
465+
return;
466+
467+
kfree(list->handles);
468+
list->count = 0;
469+
}
470+
EXPORT_SYMBOL_GPL(acpi_handle_list_free);
471+
415472
acpi_status
416473
acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
417474
{

drivers/platform/surface/surface_acpi_notify.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ static bool is_san_consumer(struct platform_device *pdev, acpi_handle handle)
741741
struct acpi_handle_list dep_devices;
742742
acpi_handle supplier = ACPI_HANDLE(&pdev->dev);
743743
acpi_status status;
744+
bool ret = false;
744745
int i;
745746

746747
if (!acpi_has_method(handle, "_DEP"))
@@ -753,11 +754,14 @@ static bool is_san_consumer(struct platform_device *pdev, acpi_handle handle)
753754
}
754755

755756
for (i = 0; i < dep_devices.count; i++) {
756-
if (dep_devices.handles[i] == supplier)
757-
return true;
757+
if (dep_devices.handles[i] == supplier) {
758+
ret = true;
759+
break;
760+
}
758761
}
759762

760-
return false;
763+
acpi_handle_list_free(&dep_devices);
764+
return ret;
761765
}
762766

763767
static acpi_status san_consumer_setup(acpi_handle handle, u32 lvl,

include/acpi/acpi_bus.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
#include <linux/device.h>
1313
#include <linux/property.h>
1414

15-
/* TBD: Make dynamic */
16-
#define ACPI_MAX_HANDLES 10
1715
struct acpi_handle_list {
1816
u32 count;
19-
acpi_handle handles[ACPI_MAX_HANDLES];
17+
acpi_handle* handles;
2018
};
2119

2220
/* acpi_utils.h */
@@ -32,6 +30,11 @@ acpi_evaluate_reference(acpi_handle handle,
3230
acpi_string pathname,
3331
struct acpi_object_list *arguments,
3432
struct acpi_handle_list *list);
33+
bool acpi_handle_list_equal(struct acpi_handle_list *list1,
34+
struct acpi_handle_list *list2);
35+
void acpi_handle_list_replace(struct acpi_handle_list *dst,
36+
struct acpi_handle_list *src);
37+
void acpi_handle_list_free(struct acpi_handle_list *list);
3538
acpi_status
3639
acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
3740
struct acpi_buffer *status_buf);

0 commit comments

Comments
 (0)