@@ -111,6 +111,8 @@ acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
111111 union acpi_operand_object * region_obj2 ;
112112 void * region_context = NULL ;
113113 struct acpi_connection_info * context ;
114+ acpi_mutex context_mutex ;
115+ u8 context_locked ;
114116 acpi_physical_address address ;
115117
116118 ACPI_FUNCTION_TRACE (ev_address_space_dispatch );
@@ -135,6 +137,8 @@ acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
135137 }
136138
137139 context = handler_desc -> address_space .context ;
140+ context_mutex = handler_desc -> address_space .context_mutex ;
141+ context_locked = FALSE;
138142
139143 /*
140144 * It may be the case that the region has never been initialized.
@@ -203,6 +207,23 @@ acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
203207 handler = handler_desc -> address_space .handler ;
204208 address = (region_obj -> region .address + region_offset );
205209
210+ ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION ,
211+ "Handler %p (@%p) Address %8.8X%8.8X [%s]\n" ,
212+ & region_obj -> region .handler -> address_space , handler ,
213+ ACPI_FORMAT_UINT64 (address ),
214+ acpi_ut_get_region_name (region_obj -> region .
215+ space_id )));
216+
217+ if (!(handler_desc -> address_space .handler_flags &
218+ ACPI_ADDR_HANDLER_DEFAULT_INSTALLED )) {
219+ /*
220+ * For handlers other than the default (supplied) handlers, we must
221+ * exit the interpreter because the handler *might* block -- we don't
222+ * know what it will do, so we can't hold the lock on the interpreter.
223+ */
224+ acpi_ex_exit_interpreter ();
225+ }
226+
206227 /*
207228 * Special handling for generic_serial_bus and general_purpose_io:
208229 * There are three extra parameters that must be passed to the
@@ -211,6 +232,11 @@ acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
211232 * 2) Length of the above buffer
212233 * 3) Actual access length from the access_as() op
213234 *
235+ * Since we pass these extra parameters via the context, which is
236+ * shared between threads, we must lock the context to avoid these
237+ * parameters being changed from another thread before the handler
238+ * has completed running.
239+ *
214240 * In addition, for general_purpose_io, the Address and bit_width fields
215241 * are defined as follows:
216242 * 1) Address is the pin number index of the field (bit offset from
@@ -220,6 +246,14 @@ acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
220246 if ((region_obj -> region .space_id == ACPI_ADR_SPACE_GSBUS ) &&
221247 context && field_obj ) {
222248
249+ status =
250+ acpi_os_acquire_mutex (context_mutex , ACPI_WAIT_FOREVER );
251+ if (ACPI_FAILURE (status )) {
252+ goto re_enter_interpreter ;
253+ }
254+
255+ context_locked = TRUE;
256+
223257 /* Get the Connection (resource_template) buffer */
224258
225259 context -> connection = field_obj -> field .resource_buffer ;
@@ -229,6 +263,14 @@ acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
229263 if ((region_obj -> region .space_id == ACPI_ADR_SPACE_GPIO ) &&
230264 context && field_obj ) {
231265
266+ status =
267+ acpi_os_acquire_mutex (context_mutex , ACPI_WAIT_FOREVER );
268+ if (ACPI_FAILURE (status )) {
269+ goto re_enter_interpreter ;
270+ }
271+
272+ context_locked = TRUE;
273+
232274 /* Get the Connection (resource_template) buffer */
233275
234276 context -> connection = field_obj -> field .resource_buffer ;
@@ -238,28 +280,15 @@ acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
238280 bit_width = field_obj -> field .bit_length ;
239281 }
240282
241- ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION ,
242- "Handler %p (@%p) Address %8.8X%8.8X [%s]\n" ,
243- & region_obj -> region .handler -> address_space , handler ,
244- ACPI_FORMAT_UINT64 (address ),
245- acpi_ut_get_region_name (region_obj -> region .
246- space_id )));
247-
248- if (!(handler_desc -> address_space .handler_flags &
249- ACPI_ADDR_HANDLER_DEFAULT_INSTALLED )) {
250- /*
251- * For handlers other than the default (supplied) handlers, we must
252- * exit the interpreter because the handler *might* block -- we don't
253- * know what it will do, so we can't hold the lock on the interpreter.
254- */
255- acpi_ex_exit_interpreter ();
256- }
257-
258283 /* Call the handler */
259284
260285 status = handler (function , address , bit_width , value , context ,
261286 region_obj2 -> extra .region_context );
262287
288+ if (context_locked ) {
289+ acpi_os_release_mutex (context_mutex );
290+ }
291+
263292 if (ACPI_FAILURE (status )) {
264293 ACPI_EXCEPTION ((AE_INFO , status , "Returned by Handler for [%s]" ,
265294 acpi_ut_get_region_name (region_obj -> region .
@@ -276,6 +305,7 @@ acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
276305 }
277306 }
278307
308+ re_enter_interpreter :
279309 if (!(handler_desc -> address_space .handler_flags &
280310 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED )) {
281311 /*
0 commit comments