public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 0/4] ACPI: utils: Improvements related to struct acpi_handle_list
@ 2023-12-08 20:03 Rafael J. Wysocki
  2023-12-08 20:05 ` [PATCH v1 1/4] ACPI: utils: Rearrange in acpi_evaluate_reference() Rafael J. Wysocki
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Rafael J. Wysocki @ 2023-12-08 20:03 UTC (permalink / raw)
  To: Linux ACPI; +Cc: LKML, Hans de Goede, Andy Shevchenko, Mika Westerberg

Hi All,

This patch series modifies some code related to struct acpi_handle_list.

The first two patches update acpi_evaluate_reference(), which is the only
place where struct acpi_handle_list objects are created, and its callers
to make the code easier to follow, reduce code duplication and get rid of
some useless code and local variables.

The other two patches are a minor modification of a helper function
comparing the contents of two struct acpi_handle_list objects and a white
space fix for the struct acpi_handle_list definition.

The patches in this series are not expected to change the observable behavior
of the kernel.

The series is based on linux-next from today.

Thanks!




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v1 1/4] ACPI: utils: Rearrange in acpi_evaluate_reference()
  2023-12-08 20:03 [PATCH v1 0/4] ACPI: utils: Improvements related to struct acpi_handle_list Rafael J. Wysocki
@ 2023-12-08 20:05 ` Rafael J. Wysocki
  2023-12-08 20:06 ` [PATCH v1 2/4] ACPI: utils: Return bool from acpi_evaluate_reference() Rafael J. Wysocki
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Rafael J. Wysocki @ 2023-12-08 20:05 UTC (permalink / raw)
  To: Linux ACPI; +Cc: LKML, Hans de Goede, Andy Shevchenko, Mika Westerberg

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

The code in acpi_evaluate_reference() can be improved in some ways
without changing its observable behavior.  Among other things:

 * None of the local variables in that function except for buffer
   needs to be initialized.

 * The element local variable is only used in the for () loop block,
   so it can be defined there.

 * Multiple checks can be combined.

 * Code duplication related to error handling can be eliminated.

 * Redundant inner parens can be dropped.

Modify the function as per the above.

No intentional functional impact.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/acpi/utils.c |   58 +++++++++++++++++++++------------------------------
 1 file changed, 24 insertions(+), 34 deletions(-)

Index: linux-pm/drivers/acpi/utils.c
===================================================================
--- linux-pm.orig/drivers/acpi/utils.c
+++ linux-pm/drivers/acpi/utils.c
@@ -335,12 +335,10 @@ acpi_evaluate_reference(acpi_handle hand
 			struct acpi_object_list *arguments,
 			struct acpi_handle_list *list)
 {
-	acpi_status status = AE_OK;
-	union acpi_object *package = NULL;
-	union acpi_object *element = NULL;
 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
-	u32 i = 0;
-
+	union acpi_object *package;
+	acpi_status status;
+	u32 i;
 
 	if (!list)
 		return AE_BAD_PARAMETER;
@@ -353,45 +351,32 @@ acpi_evaluate_reference(acpi_handle hand
 
 	package = buffer.pointer;
 
-	if ((buffer.length == 0) || !package) {
-		status = AE_BAD_DATA;
-		acpi_util_eval_error(handle, pathname, status);
-		goto end;
-	}
-	if (package->type != ACPI_TYPE_PACKAGE) {
-		status = AE_BAD_DATA;
-		acpi_util_eval_error(handle, pathname, status);
-		goto end;
-	}
-	if (!package->package.count) {
+	if (buffer.length == 0 || !package ||
+	    package->type != ACPI_TYPE_PACKAGE || !package->package.count) {
 		status = AE_BAD_DATA;
-		acpi_util_eval_error(handle, pathname, status);
-		goto end;
+		goto err;
 	}
 
-	list->handles = kcalloc(package->package.count, sizeof(*list->handles), GFP_KERNEL);
+	list->count = package->package.count;
+	list->handles = kcalloc(list->count, sizeof(*list->handles), GFP_KERNEL);
 	if (!list->handles) {
-		kfree(package);
-		return AE_NO_MEMORY;
+		status = AE_NO_MEMORY;
+		goto err_clear;
 	}
-	list->count = package->package.count;
 
 	/* Extract package data. */
 
 	for (i = 0; i < list->count; i++) {
-
-		element = &(package->package.elements[i]);
+		union acpi_object *element = &(package->package.elements[i]);
 
 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
 			status = AE_BAD_DATA;
-			acpi_util_eval_error(handle, pathname, status);
-			break;
+			goto err_free;
 		}
 
 		if (!element->reference.handle) {
 			status = AE_NULL_ENTRY;
-			acpi_util_eval_error(handle, pathname, status);
-			break;
+			goto err_free;
 		}
 		/* Get the  acpi_handle. */
 
@@ -399,16 +384,21 @@ acpi_evaluate_reference(acpi_handle hand
 		acpi_handle_debug(list->handles[i], "Found in reference list\n");
 	}
 
-	if (ACPI_FAILURE(status)) {
-		list->count = 0;
-		kfree(list->handles);
-		list->handles = NULL;
-	}
-
 end:
 	kfree(buffer.pointer);
 
 	return status;
+
+err_free:
+	kfree(list->handles);
+	list->handles = NULL;
+
+err_clear:
+	list->count = 0;
+
+err:
+	acpi_util_eval_error(handle, pathname, status);
+	goto end;
 }
 
 EXPORT_SYMBOL(acpi_evaluate_reference);




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v1 2/4] ACPI: utils: Return bool from acpi_evaluate_reference()
  2023-12-08 20:03 [PATCH v1 0/4] ACPI: utils: Improvements related to struct acpi_handle_list Rafael J. Wysocki
  2023-12-08 20:05 ` [PATCH v1 1/4] ACPI: utils: Rearrange in acpi_evaluate_reference() Rafael J. Wysocki
@ 2023-12-08 20:06 ` Rafael J. Wysocki
  2023-12-08 20:06 ` [PATCH v1 3/4] ACPI: utils: Refine acpi_handle_list_equal() slightly Rafael J. Wysocki
  2023-12-08 20:07 ` [PATCH v1 4/4] ACPI: utils: Fix white space in struct acpi_handle_list definition Rafael J. Wysocki
  3 siblings, 0 replies; 5+ messages in thread
From: Rafael J. Wysocki @ 2023-12-08 20:06 UTC (permalink / raw)
  To: Linux ACPI; +Cc: LKML, Hans de Goede, Andy Shevchenko, Mika Westerberg

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

There are only 4 users of acpi_evaluate_reference() and none of them
actually cares about the reason why it fails.  All of them are only
interested in whether or not it is successful, so it can return a bool
value indicating that.

Modify acpi_evaluate_reference() as per the observation above and update
its callers accordingly so as to get rid of useless code and local
variables.

The observable behavior of the kernel is not expected to change after
this modification of the code.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/acpi/acpi_lpss.c                       |    5 ---
 drivers/acpi/scan.c                            |    5 +--
 drivers/acpi/thermal.c                         |    4 ---
 drivers/acpi/utils.c                           |   32 +++++++++----------------
 drivers/platform/surface/surface_acpi_notify.c |    4 ---
 include/acpi/acpi_bus.h                        |    8 ++----
 6 files changed, 20 insertions(+), 38 deletions(-)

Index: linux-pm/drivers/acpi/utils.c
===================================================================
--- linux-pm.orig/drivers/acpi/utils.c
+++ linux-pm/drivers/acpi/utils.c
@@ -329,19 +329,18 @@ const char *acpi_get_subsystem_id(acpi_h
 }
 EXPORT_SYMBOL_GPL(acpi_get_subsystem_id);
 
-acpi_status
-acpi_evaluate_reference(acpi_handle handle,
-			acpi_string pathname,
-			struct acpi_object_list *arguments,
-			struct acpi_handle_list *list)
+bool acpi_evaluate_reference(acpi_handle handle, acpi_string pathname,
+			     struct acpi_object_list *arguments,
+			     struct acpi_handle_list *list)
 {
 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 	union acpi_object *package;
 	acpi_status status;
+	bool ret = false;
 	u32 i;
 
 	if (!list)
-		return AE_BAD_PARAMETER;
+		return false;
 
 	/* Evaluate object. */
 
@@ -352,42 +351,35 @@ acpi_evaluate_reference(acpi_handle hand
 	package = buffer.pointer;
 
 	if (buffer.length == 0 || !package ||
-	    package->type != ACPI_TYPE_PACKAGE || !package->package.count) {
-		status = AE_BAD_DATA;
+	    package->type != ACPI_TYPE_PACKAGE || !package->package.count)
 		goto err;
-	}
 
 	list->count = package->package.count;
 	list->handles = kcalloc(list->count, sizeof(*list->handles), GFP_KERNEL);
-	if (!list->handles) {
-		status = AE_NO_MEMORY;
+	if (!list->handles)
 		goto err_clear;
-	}
 
 	/* Extract package data. */
 
 	for (i = 0; i < list->count; i++) {
 		union acpi_object *element = &(package->package.elements[i]);
 
-		if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
-			status = AE_BAD_DATA;
+		if (element->type != ACPI_TYPE_LOCAL_REFERENCE ||
+		    !element->reference.handle)
 			goto err_free;
-		}
 
-		if (!element->reference.handle) {
-			status = AE_NULL_ENTRY;
-			goto err_free;
-		}
 		/* Get the  acpi_handle. */
 
 		list->handles[i] = element->reference.handle;
 		acpi_handle_debug(list->handles[i], "Found in reference list\n");
 	}
 
+	ret = true;
+
 end:
 	kfree(buffer.pointer);
 
-	return status;
+	return ret;
 
 err_free:
 	kfree(list->handles);
Index: linux-pm/drivers/acpi/acpi_lpss.c
===================================================================
--- linux-pm.orig/drivers/acpi/acpi_lpss.c
+++ linux-pm/drivers/acpi/acpi_lpss.c
@@ -565,16 +565,13 @@ static struct device *acpi_lpss_find_dev
 static bool acpi_lpss_dep(struct acpi_device *adev, acpi_handle handle)
 {
 	struct acpi_handle_list dep_devices;
-	acpi_status status;
 	bool ret = false;
 	int i;
 
 	if (!acpi_has_method(adev->handle, "_DEP"))
 		return false;
 
-	status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
-					 &dep_devices);
-	if (ACPI_FAILURE(status)) {
+	if (!acpi_evaluate_reference(adev->handle, "_DEP", NULL, &dep_devices)) {
 		dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
 		return false;
 	}
Index: linux-pm/include/acpi/acpi_bus.h
===================================================================
--- linux-pm.orig/include/acpi/acpi_bus.h
+++ linux-pm/include/acpi/acpi_bus.h
@@ -25,11 +25,9 @@ acpi_status
 acpi_evaluate_integer(acpi_handle handle,
 		      acpi_string pathname,
 		      struct acpi_object_list *arguments, unsigned long long *data);
-acpi_status
-acpi_evaluate_reference(acpi_handle handle,
-			acpi_string pathname,
-			struct acpi_object_list *arguments,
-			struct acpi_handle_list *list);
+bool acpi_evaluate_reference(acpi_handle handle, acpi_string pathname,
+			     struct acpi_object_list *arguments,
+			     struct acpi_handle_list *list);
 bool acpi_handle_list_equal(struct acpi_handle_list *list1,
 			    struct acpi_handle_list *list2);
 void acpi_handle_list_replace(struct acpi_handle_list *dst,
Index: linux-pm/drivers/acpi/scan.c
===================================================================
--- linux-pm.orig/drivers/acpi/scan.c
+++ linux-pm/drivers/acpi/scan.c
@@ -1984,7 +1984,6 @@ static void acpi_scan_init_hotplug(struc
 static u32 acpi_scan_check_dep(acpi_handle handle)
 {
 	struct acpi_handle_list dep_devices;
-	acpi_status status;
 	u32 count;
 	int i;
 
@@ -1997,8 +1996,7 @@ static u32 acpi_scan_check_dep(acpi_hand
 	if (!acpi_has_method(handle, "_DEP") || !acpi_has_method(handle, "_HID"))
 		return 0;
 
-	status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
-	if (ACPI_FAILURE(status)) {
+	if (!acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices)) {
 		acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
 		return 0;
 	}
@@ -2007,6 +2005,7 @@ static u32 acpi_scan_check_dep(acpi_hand
 		struct acpi_device_info *info;
 		struct acpi_dep_data *dep;
 		bool skip, honor_dep;
+		acpi_status status;
 
 		status = acpi_get_object_info(dep_devices.handles[i], &info);
 		if (ACPI_FAILURE(status)) {
Index: linux-pm/drivers/acpi/thermal.c
===================================================================
--- linux-pm.orig/drivers/acpi/thermal.c
+++ linux-pm/drivers/acpi/thermal.c
@@ -244,7 +244,6 @@ static bool update_trip_devices(struct a
 {
 	struct acpi_handle_list devices = { 0 };
 	char method[] = "_PSL";
-	acpi_status status;
 
 	if (index != ACPI_THERMAL_TRIP_PASSIVE) {
 		method[1] = 'A';
@@ -252,8 +251,7 @@ static bool update_trip_devices(struct a
 		method[3] = '0' + index;
 	}
 
-	status = acpi_evaluate_reference(tz->device->handle, method, NULL, &devices);
-	if (ACPI_FAILURE(status)) {
+	if (!acpi_evaluate_reference(tz->device->handle, method, NULL, &devices)) {
 		acpi_handle_info(tz->device->handle, "%s evaluation failure\n", method);
 		return false;
 	}
Index: linux-pm/drivers/platform/surface/surface_acpi_notify.c
===================================================================
--- linux-pm.orig/drivers/platform/surface/surface_acpi_notify.c
+++ linux-pm/drivers/platform/surface/surface_acpi_notify.c
@@ -740,15 +740,13 @@ static bool is_san_consumer(struct platf
 {
 	struct acpi_handle_list dep_devices;
 	acpi_handle supplier = ACPI_HANDLE(&pdev->dev);
-	acpi_status status;
 	bool ret = false;
 	int i;
 
 	if (!acpi_has_method(handle, "_DEP"))
 		return false;
 
-	status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
-	if (ACPI_FAILURE(status)) {
+	if (!acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices)) {
 		san_consumer_dbg(&pdev->dev, handle, "failed to evaluate _DEP\n");
 		return false;
 	}




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v1 3/4] ACPI: utils: Refine acpi_handle_list_equal() slightly
  2023-12-08 20:03 [PATCH v1 0/4] ACPI: utils: Improvements related to struct acpi_handle_list Rafael J. Wysocki
  2023-12-08 20:05 ` [PATCH v1 1/4] ACPI: utils: Rearrange in acpi_evaluate_reference() Rafael J. Wysocki
  2023-12-08 20:06 ` [PATCH v1 2/4] ACPI: utils: Return bool from acpi_evaluate_reference() Rafael J. Wysocki
@ 2023-12-08 20:06 ` Rafael J. Wysocki
  2023-12-08 20:07 ` [PATCH v1 4/4] ACPI: utils: Fix white space in struct acpi_handle_list definition Rafael J. Wysocki
  3 siblings, 0 replies; 5+ messages in thread
From: Rafael J. Wysocki @ 2023-12-08 20:06 UTC (permalink / raw)
  To: Linux ACPI; +Cc: LKML, Hans de Goede, Andy Shevchenko, Mika Westerberg

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

It is somewhat better to use the size of the first array element for
computing the size of the entire array than to rely on the array
element data type definition knowledge and the former is also
consistent with the array allocation in acpi_evaluate_reference(),
so modify the code accordingly.

No intentional functional impact.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/acpi/utils.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: linux-pm/drivers/acpi/utils.c
===================================================================
--- linux-pm.orig/drivers/acpi/utils.c
+++ linux-pm/drivers/acpi/utils.c
@@ -408,7 +408,7 @@ bool acpi_handle_list_equal(struct acpi_
 {
 	return list1->count == list2->count &&
 		!memcmp(list1->handles, list2->handles,
-		        list1->count * sizeof(acpi_handle));
+		        list1->count * sizeof(*list1->handles));
 }
 EXPORT_SYMBOL_GPL(acpi_handle_list_equal);
 




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v1 4/4] ACPI: utils: Fix white space in struct acpi_handle_list definition
  2023-12-08 20:03 [PATCH v1 0/4] ACPI: utils: Improvements related to struct acpi_handle_list Rafael J. Wysocki
                   ` (2 preceding siblings ...)
  2023-12-08 20:06 ` [PATCH v1 3/4] ACPI: utils: Refine acpi_handle_list_equal() slightly Rafael J. Wysocki
@ 2023-12-08 20:07 ` Rafael J. Wysocki
  3 siblings, 0 replies; 5+ messages in thread
From: Rafael J. Wysocki @ 2023-12-08 20:07 UTC (permalink / raw)
  To: Linux ACPI; +Cc: LKML, Hans de Goede, Andy Shevchenko, Mika Westerberg

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

Fix inadvertently introduced white space damage in the struct
acpi_handle_list definition.

No functional impact.

Fixes: 2e57d10a6591 ("ACPI: utils: Dynamically determine acpi_handle_list size")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 include/acpi/acpi_bus.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: linux-pm/include/acpi/acpi_bus.h
===================================================================
--- linux-pm.orig/include/acpi/acpi_bus.h
+++ linux-pm/include/acpi/acpi_bus.h
@@ -14,7 +14,7 @@
 
 struct acpi_handle_list {
 	u32 count;
-	acpi_handle* handles;
+	acpi_handle *handles;
 };
 
 /* acpi_utils.h */




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-12-08 20:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-08 20:03 [PATCH v1 0/4] ACPI: utils: Improvements related to struct acpi_handle_list Rafael J. Wysocki
2023-12-08 20:05 ` [PATCH v1 1/4] ACPI: utils: Rearrange in acpi_evaluate_reference() Rafael J. Wysocki
2023-12-08 20:06 ` [PATCH v1 2/4] ACPI: utils: Return bool from acpi_evaluate_reference() Rafael J. Wysocki
2023-12-08 20:06 ` [PATCH v1 3/4] ACPI: utils: Refine acpi_handle_list_equal() slightly Rafael J. Wysocki
2023-12-08 20:07 ` [PATCH v1 4/4] ACPI: utils: Fix white space in struct acpi_handle_list definition Rafael J. Wysocki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox