[PATCH v1 1/8] ACPI: bus: Fix handling of _OSC errors in acpi_run_osc()

Rafael J. Wysocki posted 1 patch 1 month, 2 weeks ago
drivers/acpi/bus.c |   25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)
[PATCH v1 1/8] ACPI: bus: Fix handling of _OSC errors in acpi_run_osc()
Posted by Rafael J. Wysocki 1 month, 2 weeks ago
From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

The handling of _OSC errors in acpi_run_osc() is inconsistent.

For example, if OSC_CAPABILITIES_MASK_ERROR is set in the return buffer
and OSC_QUERY_ENABLE is set in the capabilities buffer, all of the
_OSC errors will be ignored even though some of them indicate that
_OSC evaluation has failed.  However, if OSC_QUERY_ENABLE is not set in
the capabilities buffer, all _OSC errors will be reported, but the error
value returned by acpi_run_osc() depends on whether or not
OSC_CAPABILITIES_MASK_ERROR is set in the return buffer.

Adress this by making acpi_run_osc() clear OSC_CAPABILITIES_MASK_ERROR
in the return buffer if OSC_QUERY_ENABLE is set in the capabilities
buffer and then check if any other _OSC errors have been returned.  Also
make it use the same error return value for all _OSC errors and print
a message for OSC_CAPABILITIES_MASK_ERROR like for the other error
types.

Moreover, make acpi_run_osc() only take the defined _OSC error bits into
account when checking _OSC errors.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/acpi/bus.c |   25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

--- a/drivers/acpi/bus.c
+++ b/drivers/acpi/bus.c
@@ -194,6 +194,10 @@ static void acpi_print_osc_error(acpi_ha
 	pr_debug("\n");
 }
 
+#define OSC_ERROR_MASK 	(OSC_REQUEST_ERROR | OSC_INVALID_UUID_ERROR | \
+			 OSC_INVALID_REVISION_ERROR | \
+			 OSC_CAPABILITIES_MASK_ERROR)
+
 acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
 {
 	acpi_status status;
@@ -240,8 +244,12 @@ acpi_status acpi_run_osc(acpi_handle han
 		status = AE_TYPE;
 		goto out_kfree;
 	}
-	/* Need to ignore the bit0 in result code */
-	errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
+	/* Only take defined error bits into account. */
+	errors = *((u32 *)out_obj->buffer.pointer) & OSC_ERROR_MASK;
+	/* If OSC_QUERY_ENABLE is set, ignore the "capabilities masked" bit. */
+	if (((u32 *)context->cap.pointer)[OSC_QUERY_DWORD] & OSC_QUERY_ENABLE)
+		errors &= ~OSC_CAPABILITIES_MASK_ERROR;
+
 	if (errors) {
 		if (errors & OSC_REQUEST_ERROR)
 			acpi_print_osc_error(handle, context,
@@ -252,17 +260,14 @@ acpi_status acpi_run_osc(acpi_handle han
 		if (errors & OSC_INVALID_REVISION_ERROR)
 			acpi_print_osc_error(handle, context,
 				"_OSC invalid revision");
-		if (errors & OSC_CAPABILITIES_MASK_ERROR) {
-			if (((u32 *)context->cap.pointer)[OSC_QUERY_DWORD]
-			    & OSC_QUERY_ENABLE)
-				goto out_success;
-			status = AE_SUPPORT;
-			goto out_kfree;
-		}
+		if (errors & OSC_CAPABILITIES_MASK_ERROR)
+			acpi_print_osc_error(handle, context,
+				"_OSC capability bits masked");
+
 		status = AE_ERROR;
 		goto out_kfree;
 	}
-out_success:
+
 	context->ret.length = out_obj->buffer.length;
 	context->ret.pointer = kmemdup(out_obj->buffer.pointer,
 				       context->ret.length, GFP_KERNEL);
Re: [PATCH v1 1/8] ACPI: bus: Fix handling of _OSC errors in acpi_run_osc()
Posted by Jonathan Cameron 1 month, 2 weeks ago
On Thu, 18 Dec 2025 21:34:26 +0100
"Rafael J. Wysocki" <rafael@kernel.org> wrote:

> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> 
> The handling of _OSC errors in acpi_run_osc() is inconsistent.

I wonder if this would be easier to follow with a brief statement
of why we threat OSC_CAPABILITIES_MASK_ERROR as an error in the first
place for non query cases?  It took me a brief think and spec read
to figure that out, but maybe more coffee needed.

> 
> For example, if OSC_CAPABILITIES_MASK_ERROR is set in the return buffer
> and OSC_QUERY_ENABLE is set in the capabilities buffer, all of the
> _OSC errors will be ignored even though some of them indicate that
> _OSC evaluation has failed.  However, if OSC_QUERY_ENABLE is not set in
> the capabilities buffer, all _OSC errors will be reported, but the error
> value returned by acpi_run_osc() depends on whether or not
> OSC_CAPABILITIES_MASK_ERROR is set in the return buffer.
> 
> Adress this by making acpi_run_osc() clear OSC_CAPABILITIES_MASK_ERROR
> in the return buffer if OSC_QUERY_ENABLE is set in the capabilities
> buffer and then check if any other _OSC errors have been returned.  Also
> make it use the same error return value for all _OSC errors and print
> a message for OSC_CAPABILITIES_MASK_ERROR like for the other error
> types.
> 
> Moreover, make acpi_run_osc() only take the defined _OSC error bits into
> account when checking _OSC errors.
> 
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Changes look good to me.
Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
Re: [PATCH v1 1/8] ACPI: bus: Fix handling of _OSC errors in acpi_run_osc()
Posted by Rafael J. Wysocki 1 month, 2 weeks ago
On Fri, Dec 19, 2025 at 1:26 PM Jonathan Cameron
<jonathan.cameron@huawei.com> wrote:
>
> On Thu, 18 Dec 2025 21:34:26 +0100
> "Rafael J. Wysocki" <rafael@kernel.org> wrote:
>
> > From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> >
> > The handling of _OSC errors in acpi_run_osc() is inconsistent.
>
> I wonder if this would be easier to follow with a brief statement
> of why we threat OSC_CAPABILITIES_MASK_ERROR as an error in the first
> place for non query cases?  It took me a brief think and spec read
> to figure that out, but maybe more coffee needed.

Well, this is a good question and it is not obvious IMV.

The current code treats it as an error, but arguably it is not really an error.

If it is a query, it doesn't even make sense to print a debug message
for it, but if it is not a query, the feature mask in the _OSC return
buffer still represents the feature that the OS is expected to
control.  So print the debug messages, but do not fail in that case.

I'll update the patch.