ACPICA: Add auto-serialization support for ill-behaved control methods.
authorBob Moore <robert.moore@intel.com>
Mon, 24 Mar 2014 06:49:00 +0000 (14:49 +0800)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Wed, 26 Mar 2014 15:25:59 +0000 (16:25 +0100)
This change adds support to automatically mark a control method as
"serialized" if the method creates any named objects. This will
positively prevent the method from being entered by more than one
thread and thus preventing a possible abort when an attempt is
made to create an object twice.

Implemented by parsing all non-serialize control methods at table
load time.

This feature is disabled by default and this patch also adds a new
Linux kernel parameter "acpi_auto_serialize" to allow this feature
to be turned on for a specific boot.

References: https://bugzilla.kernel.org/show_bug.cgi?id=52191
Signed-off-by: Bob Moore <robert.moore@intel.com>
Signed-off-by: Lv Zheng <lv.zheng@intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
13 files changed:
Documentation/kernel-parameters.txt
drivers/acpi/acpica/acdispat.h
drivers/acpi/acpica/acglobal.h
drivers/acpi/acpica/acstruct.h
drivers/acpi/acpica/dsinit.c
drivers/acpi/acpica/dsmethod.c
drivers/acpi/acpica/dswload.c
drivers/acpi/acpica/nsinit.c
drivers/acpi/acpica/nsload.c
drivers/acpi/acpica/psloop.c
drivers/acpi/acpica/psobject.c
drivers/acpi/osl.c
include/acpi/acpixf.h

index 3eae32f341225b9e8be91faf8587b56a84f0b810..a159537ea8807e08fe2b523dff015961fbb3f4cd 100644 (file)
@@ -229,6 +229,14 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
                        use by PCI
                        Format: <irq>,<irq>...
 
+       acpi_auto_serialize     [HW,ACPI]
+                       Enable auto-serialization of AML methods
+                       AML control methods that contain the opcodes to create
+                       named objects will be marked as "Serialized" by the
+                       auto-serialization feature.
+                       This feature is disabled by default.
+                       This option allows to turn on the feature.
+
        acpi_no_auto_ssdt       [HW,ACPI] Disable automatic loading of SSDT
 
        acpica_no_return_repair [HW, ACPI]
index 5b472c43c31da376e14e039f4583521e68b184da..d3e2cc395d7fad8bbd862b72162dd6b74c0515d9 100644 (file)
@@ -139,20 +139,21 @@ acpi_ds_init_field_objects(union acpi_parse_object *op,
                           struct acpi_walk_state *walk_state);
 
 /*
- * dsload - Parser/Interpreter interface, pass 1 namespace load callbacks
+ * dsload - Parser/Interpreter interface
  */
 acpi_status
 acpi_ds_init_callbacks(struct acpi_walk_state *walk_state, u32 pass_number);
 
+/* dsload - pass 1 namespace load callbacks */
+
 acpi_status
 acpi_ds_load1_begin_op(struct acpi_walk_state *walk_state,
                       union acpi_parse_object **out_op);
 
 acpi_status acpi_ds_load1_end_op(struct acpi_walk_state *walk_state);
 
-/*
- * dsload - Parser/Interpreter interface, pass 2 namespace load callbacks
- */
+/* dsload - pass 2 namespace load callbacks */
+
 acpi_status
 acpi_ds_load2_begin_op(struct acpi_walk_state *walk_state,
                       union acpi_parse_object **out_op);
@@ -200,7 +201,9 @@ void acpi_ds_method_data_init(struct acpi_walk_state *walk_state);
 /*
  * dsmethod - Parser/Interpreter interface - control method parsing
  */
-acpi_status acpi_ds_parse_method(struct acpi_namespace_node *node);
+acpi_status
+acpi_ds_auto_serialize_method(struct acpi_namespace_node *node,
+                             union acpi_operand_object *obj_desc);
 
 acpi_status
 acpi_ds_call_control_method(struct acpi_thread_state *thread,
index 767556cb14484696721f0915784407a4fd2de70b..ea0f8389ccdecec1309ed9d0fccd3f7719c39eb7 100644 (file)
  */
 ACPI_INIT_GLOBAL(u8, acpi_gbl_enable_interpreter_slack, FALSE);
 
+/*
+ * Automatically serialize all methods that create named objects? Default
+ * is TRUE, meaning that all non_serialized methods are scanned once at
+ * table load time to determine those that create named objects. Methods
+ * that create named objects are marked Serialized in order to prevent
+ * possible run-time problems if they are entered by more than one thread.
+ */
+ACPI_INIT_GLOBAL(u8, acpi_gbl_auto_serialize_methods, FALSE);
+
 /*
  * Create the predefined _OSI method in the namespace? Default is TRUE
  * because ACPI CA is fully compatible with other ACPI implementations.
index 5d2989a1b68c965a8649243b20837dd5104f74a5..cf7346110bd825e2cd1d514d6eed507b2e262421 100644 (file)
@@ -133,6 +133,9 @@ struct acpi_init_walk_info {
        u32 table_index;
        u32 object_count;
        u32 method_count;
+       u32 serial_method_count;
+       u32 non_serial_method_count;
+       u32 serialized_method_count;
        u32 device_count;
        u32 op_region_count;
        u32 field_count;
index 96644d5ac0e1fa3852cdcb28f934cdf259f92e4e..aee5e45f6d35ecd39eb751e0008baeb9f252cfcc 100644 (file)
@@ -83,8 +83,8 @@ acpi_ds_init_one_object(acpi_handle obj_handle,
            (struct acpi_init_walk_info *)context;
        struct acpi_namespace_node *node =
            (struct acpi_namespace_node *)obj_handle;
-       acpi_object_type type;
        acpi_status status;
+       union acpi_operand_object *obj_desc;
 
        ACPI_FUNCTION_ENTRY();
 
@@ -100,9 +100,7 @@ acpi_ds_init_one_object(acpi_handle obj_handle,
 
        /* And even then, we are only interested in a few object types */
 
-       type = acpi_ns_get_type(obj_handle);
-
-       switch (type) {
+       switch (acpi_ns_get_type(obj_handle)) {
        case ACPI_TYPE_REGION:
 
                status = acpi_ds_initialize_region(obj_handle);
@@ -117,8 +115,44 @@ acpi_ds_init_one_object(acpi_handle obj_handle,
                break;
 
        case ACPI_TYPE_METHOD:
-
+               /*
+                * Auto-serialization support. We will examine each method that is
+                * not_serialized to determine if it creates any Named objects. If
+                * it does, it will be marked serialized to prevent problems if
+                * the method is entered by two or more threads and an attempt is
+                * made to create the same named object twice -- which results in
+                * an AE_ALREADY_EXISTS exception and method abort.
+                */
                info->method_count++;
+               obj_desc = acpi_ns_get_attached_object(node);
+               if (!obj_desc) {
+                       break;
+               }
+
+               /* Ignore if already serialized */
+
+               if (obj_desc->method.info_flags & ACPI_METHOD_SERIALIZED) {
+                       info->serial_method_count++;
+                       break;
+               }
+
+               if (acpi_gbl_auto_serialize_methods) {
+
+                       /* Parse/scan method and serialize it if necessary */
+
+                       acpi_ds_auto_serialize_method(node, obj_desc);
+                       if (obj_desc->method.
+                           info_flags & ACPI_METHOD_SERIALIZED) {
+
+                               /* Method was just converted to Serialized */
+
+                               info->serial_method_count++;
+                               info->serialized_method_count++;
+                               break;
+                       }
+               }
+
+               info->non_serial_method_count++;
                break;
 
        case ACPI_TYPE_DEVICE:
@@ -170,7 +204,6 @@ acpi_ds_initialize_objects(u32 table_index,
 
        ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
                          "**** Starting initialization of namespace objects ****\n"));
-       ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT, "Parsing all Control Methods:"));
 
        /* Set all init info to zero */
 
@@ -205,14 +238,16 @@ acpi_ds_initialize_objects(u32 table_index,
        }
 
        ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
-                             "\nTable [%4.4s](id %4.4X) - %u Objects with %u Devices %u Methods %u Regions\n",
+                             "Table [%4.4s] (id %4.4X) - %4u Objects with %3u Devices, "
+                             "%3u Regions, %3u Methods (%u/%u/%u Serial/Non/Cvt)\n",
                              table->signature, owner_id, info.object_count,
-                             info.device_count, info.method_count,
-                             info.op_region_count));
+                             info.device_count, info.op_region_count,
+                             info.method_count, info.serial_method_count,
+                             info.non_serial_method_count,
+                             info.serialized_method_count));
 
-       ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
-                         "%u Methods, %u Regions\n", info.method_count,
-                         info.op_region_count));
+       ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "%u Methods, %u Regions\n",
+                         info.method_count, info.op_region_count));
 
        return_ACPI_STATUS(AE_OK);
 }
index 2c6d42c2bc01a4b7f10fcb1424479a6dbab8b667..97ed86aee0dc79e75218eb2f28642e80519c769b 100644 (file)
 #ifdef ACPI_DISASSEMBLER
 #include "acdisasm.h"
 #endif
+#include "acparser.h"
+#include "amlcode.h"
 
 #define _COMPONENT          ACPI_DISPATCHER
 ACPI_MODULE_NAME("dsmethod")
 
 /* Local prototypes */
+static acpi_status
+acpi_ds_detect_named_opcodes(struct acpi_walk_state *walk_state,
+                            union acpi_parse_object **out_op);
+
 static acpi_status
 acpi_ds_create_method_mutex(union acpi_operand_object *method_desc);
 
+/*******************************************************************************
+ *
+ * FUNCTION:    acpi_ds_auto_serialize_method
+ *
+ * PARAMETERS:  node                        - Namespace Node of the method
+ *              obj_desc                    - Method object attached to node
+ *
+ * RETURN:      Status
+ *
+ * DESCRIPTION: Parse a control method AML to scan for control methods that
+ *              need serialization due to the creation of named objects.
+ *
+ * NOTE: It is a bit of overkill to mark all such methods serialized, since
+ * there is only a problem if the method actually blocks during execution.
+ * A blocking operation is, for example, a Sleep() operation, or any access
+ * to an operation region. However, it is probably not possible to easily
+ * detect whether a method will block or not, so we simply mark all suspicious
+ * methods as serialized.
+ *
+ * NOTE2: This code is essentially a generic routine for parsing a single
+ * control method.
+ *
+ ******************************************************************************/
+
+acpi_status
+acpi_ds_auto_serialize_method(struct acpi_namespace_node *node,
+                             union acpi_operand_object *obj_desc)
+{
+       acpi_status status;
+       union acpi_parse_object *op = NULL;
+       struct acpi_walk_state *walk_state;
+
+       ACPI_FUNCTION_TRACE_PTR(ds_auto_serialize_method, node);
+
+       ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
+                         "Method auto-serialization parse [%4.4s] %p\n",
+                         acpi_ut_get_node_name(node), node));
+
+       /* Create/Init a root op for the method parse tree */
+
+       op = acpi_ps_alloc_op(AML_METHOD_OP);
+       if (!op) {
+               return_ACPI_STATUS(AE_NO_MEMORY);
+       }
+
+       acpi_ps_set_name(op, node->name.integer);
+       op->common.node = node;
+
+       /* Create and initialize a new walk state */
+
+       walk_state =
+           acpi_ds_create_walk_state(node->owner_id, NULL, NULL, NULL);
+       if (!walk_state) {
+               return_ACPI_STATUS(AE_NO_MEMORY);
+       }
+
+       status =
+           acpi_ds_init_aml_walk(walk_state, op, node,
+                                 obj_desc->method.aml_start,
+                                 obj_desc->method.aml_length, NULL, 0);
+       if (ACPI_FAILURE(status)) {
+               acpi_ds_delete_walk_state(walk_state);
+               return_ACPI_STATUS(status);
+       }
+
+       walk_state->descending_callback = acpi_ds_detect_named_opcodes;
+
+       /* Parse the method, scan for creation of named objects */
+
+       status = acpi_ps_parse_aml(walk_state);
+       if (ACPI_FAILURE(status)) {
+               return_ACPI_STATUS(status);
+       }
+
+       acpi_ps_delete_parse_tree(op);
+       return_ACPI_STATUS(status);
+}
+
+/*******************************************************************************
+ *
+ * FUNCTION:    acpi_ds_detect_named_opcodes
+ *
+ * PARAMETERS:  walk_state      - Current state of the parse tree walk
+ *              out_op          - Unused, required for parser interface
+ *
+ * RETURN:      Status
+ *
+ * DESCRIPTION: Descending callback used during the loading of ACPI tables.
+ *              Currently used to detect methods that must be marked serialized
+ *              in order to avoid problems with the creation of named objects.
+ *
+ ******************************************************************************/
+
+static acpi_status
+acpi_ds_detect_named_opcodes(struct acpi_walk_state *walk_state,
+                            union acpi_parse_object **out_op)
+{
+
+       ACPI_FUNCTION_NAME(acpi_ds_detect_named_opcodes);
+
+       /* We are only interested in opcodes that have an associated name */
+
+       if (!(walk_state->op_info->flags & AML_NAMED)) {
+               return (AE_OK);
+       }
+
+       /*
+        * At this point, we know we have a Named object opcode.
+        * Mark the method as serialized. Later code will create a mutex for
+        * this method to enforce serialization.
+        */
+       walk_state->method_desc->method.info_flags |= ACPI_METHOD_SERIALIZED;
+
+       ACPI_DEBUG_PRINT((ACPI_DB_INFO,
+                         "Method serialized [%4.4s] %p - [%s] (%4.4X)\n",
+                         walk_state->method_node->name.ascii,
+                         walk_state->method_node, walk_state->op_info->name,
+                         walk_state->opcode));
+
+       /* Abort the parse, no need to examine this method any further */
+
+       return (AE_CTRL_TERMINATE);
+}
+
 /*******************************************************************************
  *
  * FUNCTION:    acpi_ds_method_error
@@ -74,7 +204,7 @@ acpi_ds_create_method_mutex(union acpi_operand_object *method_desc);
  ******************************************************************************/
 
 acpi_status
-acpi_ds_method_error(acpi_status status, struct acpi_walk_state *walk_state)
+acpi_ds_method_error(acpi_status status, struct acpi_walk_state * walk_state)
 {
        ACPI_FUNCTION_ENTRY();
 
index bd7811c64169f8aa2c4be03fd61ef1d9bdffee6f..15623da26200cdf852c90f72aa22648b4535ba09 100644 (file)
@@ -73,8 +73,20 @@ acpi_ds_init_callbacks(struct acpi_walk_state *walk_state, u32 pass_number)
 {
 
        switch (pass_number) {
+       case 0:
+
+               /* Parse only - caller will setup callbacks */
+
+               walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
+                   ACPI_PARSE_DELETE_TREE | ACPI_PARSE_DISASSEMBLE;
+               walk_state->descending_callback = NULL;
+               walk_state->ascending_callback = NULL;
+               break;
+
        case 1:
 
+               /* Load pass 1 */
+
                walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
                    ACPI_PARSE_DELETE_TREE;
                walk_state->descending_callback = acpi_ds_load1_begin_op;
@@ -83,6 +95,8 @@ acpi_ds_init_callbacks(struct acpi_walk_state *walk_state, u32 pass_number)
 
        case 2:
 
+               /* Load pass 2 */
+
                walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
                    ACPI_PARSE_DELETE_TREE;
                walk_state->descending_callback = acpi_ds_load2_begin_op;
@@ -91,6 +105,8 @@ acpi_ds_init_callbacks(struct acpi_walk_state *walk_state, u32 pass_number)
 
        case 3:
 
+               /* Execution pass */
+
 #ifndef ACPI_NO_METHOD_EXECUTION
                walk_state->parse_flags |= ACPI_PARSE_EXECUTE |
                    ACPI_PARSE_DELETE_TREE;
index 5b74677bf74dddfb48d160f390b0aada33eea458..a3fb7e4c08092d05abe5162b85d3c1a6297b9c94 100644 (file)
@@ -111,9 +111,8 @@ acpi_status acpi_ns_initialize_objects(void)
                              info.object_count));
 
        ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
-                         "%u Control Methods found\n", info.method_count));
-       ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
-                         "%u Op Regions found\n", info.op_region_count));
+                         "%u Control Methods found\n%u Op Regions found\n",
+                         info.method_count, info.op_region_count));
 
        return_ACPI_STATUS(AE_OK);
 }
index 7ae521ce8d3f3c09d0f58f95cf4bf52a59d4c91f..7c9d0181f341ea5f762858dac80f5cadb28e24c8 100644 (file)
@@ -128,12 +128,12 @@ unlock:
         * parse trees.
         */
        ACPI_DEBUG_PRINT((ACPI_DB_INFO,
-                         "**** Begin Table Method Parsing and Object Initialization\n"));
+                         "**** Begin Table Object Initialization\n"));
 
        status = acpi_ds_initialize_objects(table_index, node);
 
        ACPI_DEBUG_PRINT((ACPI_DB_INFO,
-                         "**** Completed Table Method Parsing and Object Initialization\n"));
+                         "**** Completed Table Object Initialization\n"));
 
        return_ACPI_STATUS(status);
 }
index 646d1a3f6e27b7e5695aaec30746ba4bd4c9aa53..b058e2390fdd6f3558f15e628273558fedd5acda 100644 (file)
@@ -480,6 +480,10 @@ acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
                                        status = AE_OK;
                                }
 
+                               if (status == AE_CTRL_TERMINATE) {
+                                       return_ACPI_STATUS(status);
+                               }
+
                                status =
                                    acpi_ps_complete_op(walk_state, &op,
                                                        status);
index af1f46cd37a5ad3620a165351eac2f061da104e0..a6885077d59ee7373c7ff8c6fcc1fb327a455eb3 100644 (file)
@@ -219,7 +219,10 @@ acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
 
        status = walk_state->descending_callback(walk_state, op);
        if (ACPI_FAILURE(status)) {
-               ACPI_EXCEPTION((AE_INFO, status, "During name lookup/catalog"));
+               if (status != AE_CTRL_TERMINATE) {
+                       ACPI_EXCEPTION((AE_INFO, status,
+                                       "During name lookup/catalog"));
+               }
                return_ACPI_STATUS(status);
        }
 
@@ -230,7 +233,7 @@ acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
        status = acpi_ps_next_parse_state(walk_state, *op, status);
        if (ACPI_FAILURE(status)) {
                if (status == AE_CTRL_PENDING) {
-                       return_ACPI_STATUS(AE_CTRL_PARSE_PENDING);
+                       status = AE_CTRL_PARSE_PENDING;
                }
                return_ACPI_STATUS(status);
        }
index b7af3b758f32eabe1f5481f38d82e0223e00eea5..74a160cf85b992a601072a14ebabf3181efe9381 100644 (file)
@@ -1539,6 +1539,22 @@ static int __init osi_setup(char *str)
 
 __setup("acpi_osi=", osi_setup);
 
+/*
+ * Enable the auto-serialization of named objects creation methods.
+ *
+ * This feature is disabled by default.  It marks the AML control methods
+ * that contain the opcodes to create named objects as "Serialized".
+ */
+static int __init acpi_auto_serialize_setup(char *str)
+{
+       acpi_gbl_auto_serialize_methods = TRUE;
+       pr_info("ACPI: auto-serialization enabled\n");
+
+       return 1;
+}
+
+__setup("acpi_auto_serialize", acpi_auto_serialize_setup);
+
 /* Check of resource interference between native drivers and ACPI
  * OperationRegions (SystemIO and System Memory only).
  * IO ports and memory declared in ACPI might be used by the ACPI subsystem
index a3a8dae1cd612da409dcd74694179ca06ebbaea4..fc017dff1ba06b0e781442e4d220a6d9b7c10ecf 100644 (file)
@@ -71,6 +71,7 @@ extern u32 acpi_dbg_layer;
 
 /* ACPICA runtime options */
 
+extern u8 acpi_gbl_auto_serialize_methods;
 extern u8 acpi_gbl_copy_dsdt_locally;
 extern u8 acpi_gbl_create_osi_method;
 extern u8 acpi_gbl_disable_auto_repair;