Skip to contentSkip to conversation

Hans's generic serial bus fix

PATCH In review Erik Kaneda 2 participants 4 messages Feb 18, 2021 – Feb 19, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Hans's generic serial bus fix

Hi Hans and Rafael,

Sorry about the late responses. I've accidentally dropped the ball on the comunication of this pull request. Bob has merged it now and here are the linux-ized versions of them.

Rafael, if you haven't merged the patches, please consider taking these as it will keep things easier to maintain on my end..

Thanks and apologies,
Erik Kaneda

Hans de Goede (2):
ACPICA: Fix race in generic_serial_bus (I2C) and GPIO op_region
parameter handling
ACPICA: Remove some code duplication from
acpi_ev_address_space_dispatch

 drivers/acpi/acpica/acobject.h  |  1 +
 drivers/acpi/acpica/evhandler.c |  7 ++++
 drivers/acpi/acpica/evregion.c  | 69 ++++++++++++++++++++-------------
 drivers/acpi/acpica/evxfregn.c  |  2 +
 4 files changed, 53 insertions(+), 26 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH 1/2 ACPICA: Fix race in generic_serial_bus (I2C) and GPIO op_region parameter handling

From: Hans de Goede <[email protected]>

ACPICA commit c9e0116952363b0fa815143dca7e9a2eb4fefa61

The handling of the generic_serial_bus (I2C) and GPIO op_regions in acpi_ev_address_space_dispatch() passes a number of extra parameters to the address-space handler through the address-space Context pointer (instead of using more function parameters).

The Context is shared between threads, so if multiple threads try to call the handler for the same address-space at the same time, then a second thread could change the parameters of a first thread while the handler is running for the first thread.

An example of this race hitting is the Lenovo Yoga Tablet2 1015L, where there are both attrib_bytes accesses and attrib_byte accesses to the same address-space. The attrib_bytes access stores the number of bytes to transfer in Context->access_length. Where as for the attrib_byte access the number of bytes to transfer is always 1 and field_obj->Field.access_length is unused (so 0). Both types of accesses racing from different threads leads to the following problem:

1. Thread a. starts an attrib_bytes access, stores a non 0 value from field_obj->Field.access_length in Context->access_length
2. Thread b. starts an attrib_byte access, stores 0 in Context->access_length
3. Thread a. calls i2c_acpi_space_handler() (under Linux). Which sees that the access-type is ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE and calls acpi_gsb_i2c_read_bytes(..., Context->access_length)
4. At this point Context->access_length is 0 (set by thread b.) rather then the field_obj->Field.access_length value from thread a. This 0 length reads leads to the following errors being logged:

i2c i2c-0: adapter quirk: no zero length (addr 0x0078, size 0, read) i2c i2c-0: i2c read 0 bytes from client@0x78 starting at reg 0x0 failed, error: -95

Note this is just an example of the problems which this race can cause. There are likely many more (sporadic) problems caused by this race.

This commit adds a new context_mutex to struct acpi_object_addr_handler and makes acpi_ev_address_space_dispatch() take that mutex when using the shared Context to pass extra parameters to an address-space handler, fixing this race.

Note the new mutex must be taken *after* exiting the interpreter, therefor the existing acpi_ex_exit_interpreter() call is moved to above the code which stores the extra parameters in the Context.

---
 drivers/acpi/acpica/acobject.h  |  1 +
 drivers/acpi/acpica/evhandler.c |  7 ++++
 drivers/acpi/acpica/evregion.c  | 64 ++++++++++++++++++++++++---------
 drivers/acpi/acpica/evxfregn.c  |  2 ++
 4 files changed, 57 insertions(+), 17 deletions(-)
drivers/acpi/acpica/acobject.h +1 -0
index af47a3ffd2a4..9db5ae0f79ea 100644
@@ -284,6 +284,7 @@ struct acpi_object_addr_handler {
 	acpi_adr_space_handler handler;
 	struct acpi_namespace_node *node;	/* Parent device */
 	void *context;
+	acpi_mutex context_mutex;
 	acpi_adr_space_setup setup;
 	union acpi_operand_object *region_list;	/* Regions using this handler */
 	union acpi_operand_object *next;
drivers/acpi/acpica/evhandler.c +7 -0
index ea9485e6a475..c0cd7147a5a3 100644
@@ -489,6 +489,13 @@ acpi_ev_install_space_handler(struct acpi_namespace_node *node,
 
 	/* Init handler obj */
 
+	status =
+	    acpi_os_create_mutex(&handler_obj->address_space.context_mutex);
+	if (ACPI_FAILURE(status)) {
+		acpi_ut_remove_reference(handler_obj);
+		goto unlock_and_exit;
+	}
+
 	handler_obj->address_space.space_id = (u8)space_id;
 	handler_obj->address_space.handler_flags = flags;
 	handler_obj->address_space.region_list = NULL;
drivers/acpi/acpica/evregion.c +47 -17

117 lines not on this page · raw message

drivers/acpi/acpica/evxfregn.c +2 -0
index 7672d70da850..b1ff0a8f9c14 100644
@@ -201,6 +201,8 @@ acpi_remove_address_space_handler(acpi_handle device,
 
 			/* Now we can delete the handler object */
 
+			acpi_os_release_mutex(handler_obj->address_space.
+					      context_mutex);
 			acpi_ut_remove_reference(handler_obj);
 			goto unlock_and_exit;
 		}
Signature · 1 line
2.29.2
#2 lore raw

PATCH 2/2 ACPICA: Remove some code duplication from acpi_ev_address_space_dispatch

In reply to Erik Kaneda

From: Hans de Goede <[email protected]>

ACPICA commit 383f50ff8cb7424ca16a6c0234f103b41d4a783e

The handling of the space_id == ACPI_ADR_SPACE_GSBUS and space_id == ACPI_ADR_SPACE_GPIO cases is almost identical, fold the 2 cases into 1 to remove some code duplication.

---
 drivers/acpi/acpica/evregion.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)
drivers/acpi/acpica/evregion.c +5 -18
index 2a3c13e97555..4ef43c8ef5e7 100644
@@ -244,7 +244,8 @@ acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
 	 *      the previous Connection)
 	 *   2) bit_width is the actual bit length of the field (number of pins)
 	 */
-	if ((region_obj->region.space_id == ACPI_ADR_SPACE_GSBUS) &&
+	if ((region_obj->region.space_id == ACPI_ADR_SPACE_GSBUS ||
+	     region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) &&
 	    context && field_obj) {
 
 		status =
@@ -260,25 +261,11 @@ acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
 		context->connection = field_obj->field.resource_buffer;
 		context->length = field_obj->field.resource_length;
 		context->access_length = field_obj->field.access_length;
-	}
-	if ((region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) &&
-	    context && field_obj) {
 
-		status =
-		    acpi_os_acquire_mutex(context_mutex, ACPI_WAIT_FOREVER);
-		if (ACPI_FAILURE(status)) {
-			goto re_enter_interpreter;
+		if (region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) {
+			address = field_obj->field.pin_number_index;
+			bit_width = field_obj->field.bit_length;
 		}
-
-		context_locked = TRUE;
-
-		/* Get the Connection (resource_template) buffer */
-
-		context->connection = field_obj->field.resource_buffer;
-		context->length = field_obj->field.resource_length;
-		context->access_length = field_obj->field.access_length;
-		address = field_obj->field.pin_number_index;
-		bit_width = field_obj->field.bit_length;
 	}
 
 	/* Call the handler */
Signature · 1 line
2.29.2
#3 lore raw
In reply to Erik Kaneda
Quoted 20 lines · Erik Kaneda

Hi Hans and Rafael,

Sorry about the late responses. I've accidentally dropped the ball on the comunication of this pull request. Bob has merged it now and here are the linux-ized versions of them.

Rafael, if you haven't merged the patches, please consider taking these as it will keep things easier to maintain on my end..

Thanks and apologies,
Erik Kaneda

Hans de Goede (2):
ACPICA: Fix race in generic_serial_bus (I2C) and GPIO op_region
parameter handling
ACPICA: Remove some code duplication from
acpi_ev_address_space_dispatch

Quoted 7 lines
 drivers/acpi/acpica/acobject.h  |  1 +
 drivers/acpi/acpica/evhandler.c |  7 ++++
 drivers/acpi/acpica/evregion.c  | 69 ++++++++++++++++++++-------------
 drivers/acpi/acpica/evxfregn.c  |  2 +
 4 files changed, 53 insertions(+), 26 deletions(-)

--

Both applied, thanks!

Note that I will send a pull request containing them only after the previous ACPI pull request has been processed, though.