Skip to contentSkip to conversation

ACPICA: Add mutex to avoid race condition of reference count of notify object

PATCH In review Lee, Chun-Yi 4 participants 9 messages Jun 16, 2014 – Jun 4, 2015

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

PATCH 1/1 ACPICA: Add mutex to avoid race condition of reference count of notify object

This issue found on v3.0 kernel, unfortunately there was no chance to test latest kernel on issue mchine. This patch tested on v3.0 kernel then sent to linux-acpi for review and note, maybe latest kernel also need.

The problem happened when acpi thermal driver evaluate _PSL, but acpi processor driver install notify handler at the same time. In the code path of evaluate _PSL, it updates reference count of processor and its notify objects. When the notify handler installation done after the reference count of processor updated, it causes the ref_count of processor doesn't sync with its notify object's ref_count value.

Here is an debugging log when issue reproduced:

[    3.481773] ACPI_TYPE_PROCESSOR set ACPI_DEVICE_NOTIFY, object_desc->common.reference_count: 3, notify_obj->common.reference_count: 1
[    3.481958] PROCESSOR device_hid: LNXCPU
...
[    3.487427] ACPI_TYPE_PROCESSOR, action = 1
[    3.487428] Update device_notify ref_count
[    3.487429] REF_DECREMENT ACPI_TYPE_LOCAL_NOTIFY original_count: 0
[    3.487431] ACPI Warning: Obj ffff8800b0f40b28, Reference Count is already zero, cannot decrement
[    3.487433]  (20110413/utdelete-431)
[    3.487434] REF_DECREMENT ACPI_TYPE_PROCESSOR original_count: 2

Accroding log, found the reference_count of parent object
(it's processor in this case) is 3, it doesn't match with notify_object's reference_count, value is 1. It triggered "Reference Count is already zero" warning, then happen object double free issue later.

To avoid rece condition, this patch introded ACPI_MTX_NOTIFY_REF_COUNT mutex to keep the ref_count of notify object sync with its parent object. And, it also set the reference_count value of new notify object equals to its parent object's reference_count.

---
 drivers/acpi/acpica/aclocal.h  | 3 ++-
 drivers/acpi/acpica/evxface.c  | 7 ++++++-
 drivers/acpi/acpica/utdelete.c | 6 ++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
drivers/acpi/acpica/aclocal.h +2 -1
index c7f743c..e25a4af 100644
@@ -85,8 +85,9 @@ union acpi_parse_object;
 #define ACPI_MTX_MEMORY                 5	/* Debug memory tracking lists */
 #define ACPI_MTX_DEBUG_CMD_COMPLETE     6	/* AML debugger */
 #define ACPI_MTX_DEBUG_CMD_READY        7	/* AML debugger */
+#define ACPI_MTX_NOTIFY_REF_COUNT	8	/* Reference count of notify object */
 
-#define ACPI_MAX_MUTEX                  7
+#define ACPI_MAX_MUTEX                  8
 #define ACPI_NUM_MUTEX                  ACPI_MAX_MUTEX+1
 
 /* Lock structure for reader/writer interfaces */
drivers/acpi/acpica/evxface.c +6 -1
index e114140..213fe1a 100644
@@ -495,6 +495,10 @@ acpi_install_notify_handler(acpi_handle device,
 						handler, context,
 						NULL);
 
+		acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
+
+		notify_obj->common.reference_count = obj_desc->common.reference_count;
+
 		if (handler_type & ACPI_SYSTEM_NOTIFY) {
 			obj_desc->common_notify.system_notify = notify_obj;
 		}
@@ -503,8 +507,9 @@ acpi_install_notify_handler(acpi_handle device,
 			obj_desc->common_notify.device_notify = notify_obj;
 		}
 
-		if (handler_type == ACPI_ALL_NOTIFY) {
+		acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 
+		if (handler_type == ACPI_ALL_NOTIFY) {
 			/* Extra ref if installed in both */
 
 			acpi_ut_add_reference(notify_obj);
drivers/acpi/acpica/utdelete.c +6 -0
index 31f5a78..7559813 100644
@@ -504,6 +504,7 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 
 			/* Update the notify objects for these types (if present) */
 
+			acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 			acpi_ut_update_ref_count(object->common_notify.
 						 system_notify, action);
 			acpi_ut_update_ref_count(object->common_notify.
@@ -592,6 +593,11 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 		 * main object to be deleted.
 		 */
 		acpi_ut_update_ref_count(object, action);
+		if (object->common.type == ACPI_TYPE_PROCESSOR ||
+		    object->common.type == ACPI_TYPE_DEVICE ||
+		    object->common.type == ACPI_TYPE_POWER ||
+		    object->common.type == ACPI_TYPE_THERMAL)
+			acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 		object = NULL;
 
 		/* Move on to the next object to be updated */
Signature · 1 line
1.8.4.5
#1 lore raw

Hi,

Quoted 65 lines

From: Lee, Chun-Yi [mailto:[email protected]]
Sent: Monday, June 16, 2014 10:12 AM

This issue found on v3.0 kernel, unfortunately there was no chance to test latest kernel on issue mchine. This patch tested on v3.0 kernel then sent to linux-acpi for review and note, maybe latest kernel also need.

The problem happened when acpi thermal driver evaluate _PSL, but acpi processor driver install notify handler at the same time. In the code path of evaluate _PSL, it updates reference count of processor and its notify objects. When the notify handler installation done after the reference count of processor updated, it causes the ref_count of processor doesn't sync with its notify object's ref_count value.

Here is an debugging log when issue reproduced:

[    3.481773] ACPI_TYPE_PROCESSOR set ACPI_DEVICE_NOTIFY, object_desc->common.reference_count: 3, notify_obj-
Quoted 1 line

common.reference_count: 1

[    3.481958] PROCESSOR device_hid: LNXCPU
...
[    3.487427] ACPI_TYPE_PROCESSOR, action = 1
[    3.487428] Update device_notify ref_count
[    3.487429] REF_DECREMENT ACPI_TYPE_LOCAL_NOTIFY original_count: 0
[    3.487431] ACPI Warning: Obj ffff8800b0f40b28, Reference Count is already zero, cannot decrement
[    3.487433]  (20110413/utdelete-431)
[    3.487434] REF_DECREMENT ACPI_TYPE_PROCESSOR original_count: 2

Accroding log, found the reference_count of parent object
(it's processor in this case) is 3, it doesn't match with notify_object's reference_count, value is 1. It triggered "Reference Count is already zero" warning, then happen object double free issue later.

To avoid rece condition, this patch introded ACPI_MTX_NOTIFY_REF_COUNT mutex to keep the ref_count of notify object sync with its parent object. And, it also set the reference_count value of new notify object equals to its parent object's reference_count.

---
 drivers/acpi/acpica/aclocal.h  | 3 ++-
 drivers/acpi/acpica/evxface.c  | 7 ++++++-
 drivers/acpi/acpica/utdelete.c | 6 ++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
drivers/acpi/acpica/aclocal.h +2 -1
index c7f743c..e25a4af 100644
@@ -85,8 +85,9 @@ union acpi_parse_object;
 #define ACPI_MTX_MEMORY                 5	/* Debug memory tracking lists */
 #define ACPI_MTX_DEBUG_CMD_COMPLETE     6	/* AML debugger */
 #define ACPI_MTX_DEBUG_CMD_READY        7	/* AML debugger */
+#define ACPI_MTX_NOTIFY_REF_COUNT	8	/* Reference count of notify object */
 
-#define ACPI_MAX_MUTEX                  7
+#define ACPI_MAX_MUTEX                  8
 #define ACPI_NUM_MUTEX                  ACPI_MAX_MUTEX+1
 
 /* Lock structure for reader/writer interfaces */
drivers/acpi/acpica/evxface.c +0 -0
index e114140..213fe1a 100644

drivers/acpi/acpica/evxface.c acpi_install_notify_handler(acpi_handle device, · line 501

Quoted 7 lines
drivers/acpi/acpica/evxface.c +4 -0
@@ -495,6 +495,10 @@ acpi_install_notify_handler(acpi_handle device,
 						handler, context,
 						NULL);
 
+		acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
+
+		notify_obj->common.reference_count = obj_desc->common.reference_count;
+

Should be converted to spin_lock here.

Quoted 19 lines
drivers/acpi/acpica/evxface.c +2 -1
 		if (handler_type & ACPI_SYSTEM_NOTIFY) {
 			obj_desc->common_notify.system_notify = notify_obj;
 		}
@@ -503,8 +507,9 @@ acpi_install_notify_handler(acpi_handle device,
 			obj_desc->common_notify.device_notify = notify_obj;
 		}
 
-		if (handler_type == ACPI_ALL_NOTIFY) {
+		acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 
+		if (handler_type == ACPI_ALL_NOTIFY) {
 			/* Extra ref if installed in both */
 
 			acpi_ut_add_reference(notify_obj);
drivers/acpi/acpica/utdelete.c +0 -0
index 31f5a78..7559813 100644

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 509

Quoted 6 lines
drivers/acpi/acpica/utdelete.c +1 -0
@@ -504,6 +504,7 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 
 			/* Update the notify objects for these types (if present) */
 
+			acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 			acpi_ut_update_ref_count(object->common_notify.
 						 system_notify, action);

If you take a look at acpi_ut_update_ref_count, there is already a spin_lock held around the code to update the reference count.

Thanks
-Lv

Quoted 15 lines
drivers/acpi/acpica/utdelete.c +5 -1
 			acpi_ut_update_ref_count(object->common_notify.
@@ -592,6 +593,11 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 		 * main object to be deleted.
 		 */
 		acpi_ut_update_ref_count(object, action);
+		if (object->common.type == ACPI_TYPE_PROCESSOR ||
+		    object->common.type == ACPI_TYPE_DEVICE ||
+		    object->common.type == ACPI_TYPE_POWER ||
+		    object->common.type == ACPI_TYPE_THERMAL)
+			acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 		object = NULL;
 
 		/* Move on to the next object to be updated */
--

1.8.4.5

#2 lore raw

Hi Lv Zheng,

Thanks for your response.

Quoted 70 lines · Lv

Hi,

Quoted 68 lines

From: Lee, Chun-Yi [mailto:[email protected]]
Sent: Monday, June 16, 2014 10:12 AM

This issue found on v3.0 kernel, unfortunately there was no chance to test latest kernel on issue mchine. This patch tested on v3.0 kernel then sent to linux-acpi for review and note, maybe latest kernel also need.

The problem happened when acpi thermal driver evaluate _PSL, but acpi processor driver install notify handler at the same time. In the code path of evaluate _PSL, it updates reference count of processor and its notify objects. When the notify handler installation done after the reference count of processor updated, it causes the ref_count of processor doesn't sync with its notify object's ref_count value.

Here is an debugging log when issue reproduced:

[    3.481773] ACPI_TYPE_PROCESSOR set ACPI_DEVICE_NOTIFY, object_desc->common.reference_count: 3, notify_obj-
Quoted 1 line

common.reference_count: 1

[    3.481958] PROCESSOR device_hid: LNXCPU
...
[    3.487427] ACPI_TYPE_PROCESSOR, action = 1
[    3.487428] Update device_notify ref_count
[    3.487429] REF_DECREMENT ACPI_TYPE_LOCAL_NOTIFY original_count: 0
[    3.487431] ACPI Warning: Obj ffff8800b0f40b28, Reference Count is already zero, cannot decrement
[    3.487433]  (20110413/utdelete-431)
[    3.487434] REF_DECREMENT ACPI_TYPE_PROCESSOR original_count: 2

Accroding log, found the reference_count of parent object
(it's processor in this case) is 3, it doesn't match with notify_object's reference_count, value is 1. It triggered "Reference Count is already zero" warning, then happen object double free issue later.

To avoid rece condition, this patch introded ACPI_MTX_NOTIFY_REF_COUNT mutex to keep the ref_count of notify object sync with its parent object. And, it also set the reference_count value of new notify object equals to its parent object's reference_count.

---
 drivers/acpi/acpica/aclocal.h  | 3 ++-
 drivers/acpi/acpica/evxface.c  | 7 ++++++-
 drivers/acpi/acpica/utdelete.c | 6 ++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
drivers/acpi/acpica/aclocal.h +2 -1
index c7f743c..e25a4af 100644
@@ -85,8 +85,9 @@ union acpi_parse_object;
 #define ACPI_MTX_MEMORY                 5	/* Debug memory tracking lists */
 #define ACPI_MTX_DEBUG_CMD_COMPLETE     6	/* AML debugger */
 #define ACPI_MTX_DEBUG_CMD_READY        7	/* AML debugger */
+#define ACPI_MTX_NOTIFY_REF_COUNT	8	/* Reference count of notify object */
 
-#define ACPI_MAX_MUTEX                  7
+#define ACPI_MAX_MUTEX                  8
 #define ACPI_NUM_MUTEX                  ACPI_MAX_MUTEX+1
 
 /* Lock structure for reader/writer interfaces */
drivers/acpi/acpica/evxface.c +0 -0
index e114140..213fe1a 100644
@@ -495,6 +495,10 @@ acpi_install_notify_handler(acpi_handle device,
 						handler, context,
 						NULL);
 
Quoted 7 lines

drivers/acpi/acpica/evxface.c acpi_install_notify_handler(acpi_handle device, · line 501

Quoted 4 lines
drivers/acpi/acpica/evxface.c +4 -0
+		acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
+
+		notify_obj->common.reference_count = obj_desc->common.reference_count;
+

Should be converted to spin_lock here.

Did you mean using acpi_gbl_reference_count_lock ?

Quoted 20 lines
Quoted 20 lines
drivers/acpi/acpica/evxface.c +2 -1
 		if (handler_type & ACPI_SYSTEM_NOTIFY) {
 			obj_desc->common_notify.system_notify = notify_obj;
 		}
@@ -503,8 +507,9 @@ acpi_install_notify_handler(acpi_handle device,
 			obj_desc->common_notify.device_notify = notify_obj;
 		}
 
-		if (handler_type == ACPI_ALL_NOTIFY) {
+		acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 
+		if (handler_type == ACPI_ALL_NOTIFY) {
 			/* Extra ref if installed in both */
 
 			acpi_ut_add_reference(notify_obj);
drivers/acpi/acpica/utdelete.c +0 -0
index 31f5a78..7559813 100644
@@ -504,6 +504,7 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 
Quoted 3 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 507

Quoted 3 lines
drivers/acpi/acpica/utdelete.c +1 -0
 			/* Update the notify objects for these types (if present) */
 
+			acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

Can I acquire acpi_gbl_reference_count_lock here? Does is not cause recursive lock? Or direct acquire remove mutex here?

Quoted 7 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 509

Quoted 2 lines
drivers/acpi/acpica/utdelete.c +0 -0
 			acpi_ut_update_ref_count(object->common_notify.
 						 system_notify, action);

If you take a look at acpi_ut_update_ref_count, there is already a spin_lock held around the code to update the reference count.

Thanks
-Lv

hm.... I am not sure can using acpi_gbl_reference_count_lock.

When updating parent object's ref_count, it should atomic with its notify objects's ref_count updating. The situation I want to avoid is:

a. acpi_ut_update_object_reference() update ref_count of system_notify/device_notify. (Thermal driver)
Assume the notify object didn't install to parent object yet. So, the ref_count doesn't increase.

b. acpi_install_notify_handler() install system_notify/device_notify. (Processor driver)
Processor driver install notify handler of parent object in this window. The ref_count of notify object is 1.

c. acpi_ut_update_object_reference() call acpi_ut_update_ref_count() to update ref_count of parent object. (Thermal driver)
The ref_count of parent object update to 2.
<=== this ref_count doesn't match with ref_count of notify object, it's 1.

Due to a. and c. steps should atomic, so I add ACPI_MTX_NOTIFY_REF_COUNT to bind them together, when the object type is processor, device, power or thermal.

Quoted 11 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 600

Quoted 11 lines
drivers/acpi/acpica/utdelete.c +5 -0
 			acpi_ut_update_ref_count(object->common_notify.
@@ -592,6 +593,11 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 		 * main object to be deleted.
 		 */
 		acpi_ut_update_ref_count(object, action);
+		if (object->common.type == ACPI_TYPE_PROCESSOR ||
+		    object->common.type == ACPI_TYPE_DEVICE ||
+		    object->common.type == ACPI_TYPE_POWER ||
+		    object->common.type == ACPI_TYPE_THERMAL)
+			acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

here also. I think need remove this release?

Quoted 6 lines
Quoted 5 lines
drivers/acpi/acpica/utdelete.c +0 -1
 		object = NULL;
 
 		/* Move on to the next object to be updated */
--

1.8.4.5

Please let me know if I miss understood.

Thanks a lot!
Joey Lee

#3 lore raw

Hi,

Quoted 86 lines

From: [email protected] [mailto:[email protected]] On Behalf Of joeyli
Sent: Monday, June 16, 2014 5:14 PM

Hi Lv Zheng,

Thanks for your response.

Quoted 70 lines · Lv

Hi,

Quoted 68 lines

From: Lee, Chun-Yi [mailto:[email protected]]
Sent: Monday, June 16, 2014 10:12 AM

This issue found on v3.0 kernel, unfortunately there was no chance to test latest kernel on issue mchine. This patch tested on v3.0 kernel then sent to linux-acpi for review and note, maybe latest kernel also need.

The problem happened when acpi thermal driver evaluate _PSL, but acpi processor driver install notify handler at the same time. In the code path of evaluate _PSL, it updates reference count of processor and its notify objects. When the notify handler installation done after the reference count of processor updated, it causes the ref_count of processor doesn't sync with its notify object's ref_count value.

Here is an debugging log when issue reproduced:

[    3.481773] ACPI_TYPE_PROCESSOR set ACPI_DEVICE_NOTIFY, object_desc->common.reference_count: 3, notify_obj-
Quoted 1 line

common.reference_count: 1

[    3.481958] PROCESSOR device_hid: LNXCPU
...
[    3.487427] ACPI_TYPE_PROCESSOR, action = 1
[    3.487428] Update device_notify ref_count
[    3.487429] REF_DECREMENT ACPI_TYPE_LOCAL_NOTIFY original_count: 0
[    3.487431] ACPI Warning: Obj ffff8800b0f40b28, Reference Count is already zero, cannot decrement
[    3.487433]  (20110413/utdelete-431)
[    3.487434] REF_DECREMENT ACPI_TYPE_PROCESSOR original_count: 2

Accroding log, found the reference_count of parent object
(it's processor in this case) is 3, it doesn't match with notify_object's reference_count, value is 1. It triggered "Reference Count is already zero" warning, then happen object double free issue later.

To avoid rece condition, this patch introded ACPI_MTX_NOTIFY_REF_COUNT mutex to keep the ref_count of notify object sync with its parent object. And, it also set the reference_count value of new notify object equals to its parent object's reference_count.

---
 drivers/acpi/acpica/aclocal.h  | 3 ++-
 drivers/acpi/acpica/evxface.c  | 7 ++++++-
 drivers/acpi/acpica/utdelete.c | 6 ++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
drivers/acpi/acpica/aclocal.h +2 -1
index c7f743c..e25a4af 100644
@@ -85,8 +85,9 @@ union acpi_parse_object;
 #define ACPI_MTX_MEMORY                 5	/* Debug memory tracking lists */
 #define ACPI_MTX_DEBUG_CMD_COMPLETE     6	/* AML debugger */
 #define ACPI_MTX_DEBUG_CMD_READY        7	/* AML debugger */
+#define ACPI_MTX_NOTIFY_REF_COUNT	8	/* Reference count of notify object */
 
-#define ACPI_MAX_MUTEX                  7
+#define ACPI_MAX_MUTEX                  8
 #define ACPI_NUM_MUTEX                  ACPI_MAX_MUTEX+1
 
 /* Lock structure for reader/writer interfaces */
drivers/acpi/acpica/evxface.c +0 -0
index e114140..213fe1a 100644
@@ -495,6 +495,10 @@ acpi_install_notify_handler(acpi_handle device,
 						handler, context,
 						NULL);
 
Quoted 7 lines

drivers/acpi/acpica/evxface.c acpi_install_notify_handler(acpi_handle device, · line 501

Quoted 4 lines
drivers/acpi/acpica/evxface.c +4 -0
+		acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
+
+		notify_obj->common.reference_count = obj_desc->common.reference_count;
+

Should be converted to spin_lock here.

Quoted 1 line

Did you mean using acpi_gbl_reference_count_lock ?

Yes.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
drivers/acpi/acpica/evxface.c +2 -1
 		if (handler_type & ACPI_SYSTEM_NOTIFY) {
 			obj_desc->common_notify.system_notify = notify_obj;
 		}
@@ -503,8 +507,9 @@ acpi_install_notify_handler(acpi_handle device,
 			obj_desc->common_notify.device_notify = notify_obj;
 		}
 
-		if (handler_type == ACPI_ALL_NOTIFY) {
+		acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 
+		if (handler_type == ACPI_ALL_NOTIFY) {
 			/* Extra ref if installed in both */
 
 			acpi_ut_add_reference(notify_obj);
drivers/acpi/acpica/utdelete.c +0 -0
index 31f5a78..7559813 100644
@@ -504,6 +504,7 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 
Quoted 6 lines
Quoted 3 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 507

Quoted 3 lines
drivers/acpi/acpica/utdelete.c +1 -0
 			/* Update the notify objects for these types (if present) */
 
+			acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

Can I acquire acpi_gbl_reference_count_lock here? Does is not cause recursive lock? Or direct acquire remove mutex here?

You can.
If you just want to hold the lock longer, you can introduce a lockless API to replace the one invoked below.

Quoted 20 lines
Quoted 8 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 509

Quoted 3 lines
drivers/acpi/acpica/utdelete.c +0 -0
 			acpi_ut_update_ref_count(object->common_notify.
 						 system_notify, action);

If you take a look at acpi_ut_update_ref_count, there is already a spin_lock held around the code to update the reference count.

Thanks
-Lv

hm.... I am not sure can using acpi_gbl_reference_count_lock.

When updating parent object's ref_count, it should atomic with its notify objects's ref_count updating. The situation I want to avoid is:

a. acpi_ut_update_object_reference() update ref_count of system_notify/device_notify. (Thermal driver)
Assume the notify object didn't install to parent object yet. So, the ref_count doesn't increase.

b. acpi_install_notify_handler() install system_notify/device_notify. (Processor driver)
Processor driver install notify handler of parent object in this window. The ref_count of notify object is 1.

Quoted 6 lines

c. acpi_ut_update_object_reference() call acpi_ut_update_ref_count() to update ref_count of parent object. (Thermal driver)
The ref_count of parent object update to 2.
<=== this ref_count doesn't match with ref_count of notify object, it's 1.

Due to a. and c. steps should atomic, so I add ACPI_MTX_NOTIFY_REF_COUNT to bind them together, when the object type is processor, device, power or thermal.

I can see that the only problem is the sub-object's reference count updates.
It shouldn't be increased each time the parent object's reference count is increased, but should be increased only once when it is linked to the parent object (referenced by the parent). That seems to be the root cause.
The current code might be a hackish result to work around some design defects.
So what you do in this patch might be dangerous, they can break ACPICA interpreter and you need to do it in the ACPICA source base.
There is a recursive unit test facility - ASLTS to help determine if your change doesn't introduce regressions.
I think you need to file a bug in ACPICA bugzilla and start a discussion there.

Thanks and best regards
-Lv

Quoted 31 lines
Quoted 12 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 600

Quoted 12 lines
drivers/acpi/acpica/utdelete.c +5 -0
 			acpi_ut_update_ref_count(object->common_notify.
@@ -592,6 +593,11 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 		 * main object to be deleted.
 		 */
 		acpi_ut_update_ref_count(object, action);
+		if (object->common.type == ACPI_TYPE_PROCESSOR ||
+		    object->common.type == ACPI_TYPE_DEVICE ||
+		    object->common.type == ACPI_TYPE_POWER ||
+		    object->common.type == ACPI_TYPE_THERMAL)
+			acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

here also. I think need remove this release?

Quoted 6 lines
Quoted 5 lines
drivers/acpi/acpica/utdelete.c +0 -1
 		object = NULL;
 
 		/* Move on to the next object to be updated */
--

1.8.4.5

Please let me know if I miss understood.

Thanks a lot!
Joey Lee
--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 115 lines · Lv

Hi,

Quoted 86 lines

From: [email protected] [mailto:[email protected]] On Behalf Of joeyli
Sent: Monday, June 16, 2014 5:14 PM

Hi Lv Zheng,

Thanks for your response.

Quoted 70 lines · Lv

Hi,

Quoted 68 lines

From: Lee, Chun-Yi [mailto:[email protected]]
Sent: Monday, June 16, 2014 10:12 AM

This issue found on v3.0 kernel, unfortunately there was no chance to test latest kernel on issue mchine. This patch tested on v3.0 kernel then sent to linux-acpi for review and note, maybe latest kernel also need.

The problem happened when acpi thermal driver evaluate _PSL, but acpi processor driver install notify handler at the same time. In the code path of evaluate _PSL, it updates reference count of processor and its notify objects. When the notify handler installation done after the reference count of processor updated, it causes the ref_count of processor doesn't sync with its notify object's ref_count value.

Here is an debugging log when issue reproduced:

[    3.481773] ACPI_TYPE_PROCESSOR set ACPI_DEVICE_NOTIFY, object_desc->common.reference_count: 3, notify_obj-
Quoted 1 line

common.reference_count: 1

[    3.481958] PROCESSOR device_hid: LNXCPU
...
[    3.487427] ACPI_TYPE_PROCESSOR, action = 1
[    3.487428] Update device_notify ref_count
[    3.487429] REF_DECREMENT ACPI_TYPE_LOCAL_NOTIFY original_count: 0
[    3.487431] ACPI Warning: Obj ffff8800b0f40b28, Reference Count is already zero, cannot decrement
[    3.487433]  (20110413/utdelete-431)
[    3.487434] REF_DECREMENT ACPI_TYPE_PROCESSOR original_count: 2

Accroding log, found the reference_count of parent object
(it's processor in this case) is 3, it doesn't match with notify_object's reference_count, value is 1. It triggered "Reference Count is already zero" warning, then happen object double free issue later.

To avoid rece condition, this patch introded ACPI_MTX_NOTIFY_REF_COUNT mutex to keep the ref_count of notify object sync with its parent object. And, it also set the reference_count value of new notify object equals to its parent object's reference_count.

---
 drivers/acpi/acpica/aclocal.h  | 3 ++-
 drivers/acpi/acpica/evxface.c  | 7 ++++++-
 drivers/acpi/acpica/utdelete.c | 6 ++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
drivers/acpi/acpica/aclocal.h +2 -1
index c7f743c..e25a4af 100644
@@ -85,8 +85,9 @@ union acpi_parse_object;
 #define ACPI_MTX_MEMORY                 5	/* Debug memory tracking lists */
 #define ACPI_MTX_DEBUG_CMD_COMPLETE     6	/* AML debugger */
 #define ACPI_MTX_DEBUG_CMD_READY        7	/* AML debugger */
+#define ACPI_MTX_NOTIFY_REF_COUNT	8	/* Reference count of notify object */
 
-#define ACPI_MAX_MUTEX                  7
+#define ACPI_MAX_MUTEX                  8
 #define ACPI_NUM_MUTEX                  ACPI_MAX_MUTEX+1
 
 /* Lock structure for reader/writer interfaces */
drivers/acpi/acpica/evxface.c +0 -0
index e114140..213fe1a 100644
@@ -495,6 +495,10 @@ acpi_install_notify_handler(acpi_handle device,
 						handler, context,
 						NULL);
 
Quoted 7 lines

drivers/acpi/acpica/evxface.c acpi_install_notify_handler(acpi_handle device, · line 501

Quoted 4 lines
drivers/acpi/acpica/evxface.c +4 -0
+		acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
+
+		notify_obj->common.reference_count = obj_desc->common.reference_count;
+

Should be converted to spin_lock here.

Quoted 1 line

Did you mean using acpi_gbl_reference_count_lock ?

Yes.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
drivers/acpi/acpica/evxface.c +2 -1
 		if (handler_type & ACPI_SYSTEM_NOTIFY) {
 			obj_desc->common_notify.system_notify = notify_obj;
 		}
@@ -503,8 +507,9 @@ acpi_install_notify_handler(acpi_handle device,
 			obj_desc->common_notify.device_notify = notify_obj;
 		}
 
-		if (handler_type == ACPI_ALL_NOTIFY) {
+		acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 
+		if (handler_type == ACPI_ALL_NOTIFY) {
 			/* Extra ref if installed in both */
 
 			acpi_ut_add_reference(notify_obj);
drivers/acpi/acpica/utdelete.c +0 -0
index 31f5a78..7559813 100644
@@ -504,6 +504,7 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 
 			/* Update the notify objects for these types (if present) */
 
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 507

Quoted 1 line
drivers/acpi/acpica/utdelete.c +1 -0
+			acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

Can I acquire acpi_gbl_reference_count_lock here? Does is not cause recursive lock? Or direct acquire remove mutex here?

You can.
If you just want to hold the lock longer, you can introduce a lockless API to replace the one invoked below.

I just tried to change my mutex code to acquire acpi_gbl_reference_count_lock in acpi_ut_update_object_reference(). But when system boot, kernel pending on updating ref_count of parent object, in acpi_ut_update_ref_count() when acquire acpi_gbl_reference_count_lock.

On the other hand,
I didn't find lockless spinlock API in acpi subsystem, and even in acpica project git. Could you please help teach me where is the lockless function?

Quoted 24 lines
Quoted 24 lines
Quoted 8 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 509

Quoted 3 lines
drivers/acpi/acpica/utdelete.c +0 -0
 			acpi_ut_update_ref_count(object->common_notify.
 						 system_notify, action);

If you take a look at acpi_ut_update_ref_count, there is already a spin_lock held around the code to update the reference count.

Thanks
-Lv

hm.... I am not sure can using acpi_gbl_reference_count_lock.

When updating parent object's ref_count, it should atomic with its notify objects's ref_count updating. The situation I want to avoid is:

a. acpi_ut_update_object_reference() update ref_count of system_notify/device_notify. (Thermal driver)
Assume the notify object didn't install to parent object yet. So, the ref_count doesn't increase.

b. acpi_install_notify_handler() install system_notify/device_notify. (Processor driver)
Processor driver install notify handler of parent object in this window. The ref_count of notify object is 1.

c. acpi_ut_update_object_reference() call acpi_ut_update_ref_count() to update ref_count of parent object. (Thermal driver)
The ref_count of parent object update to 2.
<=== this ref_count doesn't match with ref_count of notify object, it's 1.

Quoted 6 lines
Quoted 2 lines

Due to a. and c. steps should atomic, so I add ACPI_MTX_NOTIFY_REF_COUNT to bind them together, when the object type is processor, device, power or thermal.

I can see that the only problem is the sub-object's reference count updates.
It shouldn't be increased each time the parent object's reference count is increased, but should be increased only once when it is linked to the parent object (referenced by the parent). That seems to be the root cause.

Yes, this is the main problem, either increase or decrease ref_count of parent object, the sub-objects(here is notify object) will also updating either increase or decrease.

In the case of my issue machine, when ref_count of parent object is 2 but sub-object's ref_count is 1, later decrease parent object's ref_count to 0 will causes sub-object's ref_count is -1. <=== it triggers the double free issue then slab will emit oops.

Quoted 8 lines

The current code might be a hackish result to work around some design defects.
So what you do in this patch might be dangerous, they can break ACPICA interpreter and you need to do it in the ACPICA source base. There is a recursive unit test facility - ASLTS to help determine if your change doesn't introduce regressions. I think you need to file a bug in ACPICA bugzilla and start a discussion there.

Thanks and best regards
-Lv

Thanks for your information, I will try ASLTS and file bug about the atomic between main object's ref_count with sub-object's ref_count.

Quoted 22 lines
Quoted 22 lines
Quoted 12 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 600

Quoted 12 lines
drivers/acpi/acpica/utdelete.c +5 -0
 			acpi_ut_update_ref_count(object->common_notify.
@@ -592,6 +593,11 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 		 * main object to be deleted.
 		 */
 		acpi_ut_update_ref_count(object, action);
+		if (object->common.type == ACPI_TYPE_PROCESSOR ||
+		    object->common.type == ACPI_TYPE_DEVICE ||
+		    object->common.type == ACPI_TYPE_POWER ||
+		    object->common.type == ACPI_TYPE_THERMAL)
+			acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

here also. I think need remove this release?

Quoted 6 lines
Quoted 5 lines
drivers/acpi/acpica/utdelete.c +0 -1
 		object = NULL;
 
 		/* Move on to the next object to be updated */
--

1.8.4.5

Quoted 5 lines
Quoted 5 lines

Please let me know if I miss understood.

Thanks a lot!
Joey Lee

Regards
Joey Lee

#5 lore raw

Hi,

Quoted 128 lines

From: joeyli [mailto:[email protected]]
Sent: Tuesday, June 17, 2014 5:19 PM

Quoted 115 lines · Lv

Hi,

Quoted 86 lines

From: [email protected] [mailto:[email protected]] On Behalf Of joeyli
Sent: Monday, June 16, 2014 5:14 PM

Hi Lv Zheng,

Thanks for your response.

Quoted 70 lines · Lv

Hi,

Quoted 68 lines

From: Lee, Chun-Yi [mailto:[email protected]]
Sent: Monday, June 16, 2014 10:12 AM

This issue found on v3.0 kernel, unfortunately there was no chance to test latest kernel on issue mchine. This patch tested on v3.0 kernel then sent to linux-acpi for review and note, maybe latest kernel also need.

The problem happened when acpi thermal driver evaluate _PSL, but acpi processor driver install notify handler at the same time. In the code path of evaluate _PSL, it updates reference count of processor and its notify objects. When the notify handler installation done after the reference count of processor updated, it causes the ref_count of processor doesn't sync with its notify object's ref_count value.

Here is an debugging log when issue reproduced:

[    3.481773] ACPI_TYPE_PROCESSOR set ACPI_DEVICE_NOTIFY, object_desc->common.reference_count: 3, notify_obj-
Quoted 1 line

common.reference_count: 1

[    3.481958] PROCESSOR device_hid: LNXCPU
...
[    3.487427] ACPI_TYPE_PROCESSOR, action = 1
[    3.487428] Update device_notify ref_count
[    3.487429] REF_DECREMENT ACPI_TYPE_LOCAL_NOTIFY original_count: 0
[    3.487431] ACPI Warning: Obj ffff8800b0f40b28, Reference Count is already zero, cannot decrement
[    3.487433]  (20110413/utdelete-431)
[    3.487434] REF_DECREMENT ACPI_TYPE_PROCESSOR original_count: 2

Accroding log, found the reference_count of parent object
(it's processor in this case) is 3, it doesn't match with notify_object's reference_count, value is 1. It triggered "Reference Count is already zero" warning, then happen object double free issue later.

To avoid rece condition, this patch introded ACPI_MTX_NOTIFY_REF_COUNT mutex to keep the ref_count of notify object sync with its parent object. And, it also set the reference_count value of new notify object equals to its parent object's reference_count.

---
 drivers/acpi/acpica/aclocal.h  | 3 ++-
 drivers/acpi/acpica/evxface.c  | 7 ++++++-
 drivers/acpi/acpica/utdelete.c | 6 ++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
drivers/acpi/acpica/aclocal.h +2 -1
index c7f743c..e25a4af 100644
@@ -85,8 +85,9 @@ union acpi_parse_object;
 #define ACPI_MTX_MEMORY                 5	/* Debug memory tracking lists */
 #define ACPI_MTX_DEBUG_CMD_COMPLETE     6	/* AML debugger */
 #define ACPI_MTX_DEBUG_CMD_READY        7	/* AML debugger */
+#define ACPI_MTX_NOTIFY_REF_COUNT	8	/* Reference count of notify object */
 
-#define ACPI_MAX_MUTEX                  7
+#define ACPI_MAX_MUTEX                  8
 #define ACPI_NUM_MUTEX                  ACPI_MAX_MUTEX+1
 
 /* Lock structure for reader/writer interfaces */
drivers/acpi/acpica/evxface.c +0 -0
index e114140..213fe1a 100644
@@ -495,6 +495,10 @@ acpi_install_notify_handler(acpi_handle device,
 						handler, context,
 						NULL);
 
Quoted 7 lines

drivers/acpi/acpica/evxface.c acpi_install_notify_handler(acpi_handle device, · line 501

Quoted 4 lines
drivers/acpi/acpica/evxface.c +4 -0
+		acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
+
+		notify_obj->common.reference_count = obj_desc->common.reference_count;
+

Should be converted to spin_lock here.

Quoted 1 line

Did you mean using acpi_gbl_reference_count_lock ?

Yes.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
drivers/acpi/acpica/evxface.c +2 -1
 		if (handler_type & ACPI_SYSTEM_NOTIFY) {
 			obj_desc->common_notify.system_notify = notify_obj;
 		}
@@ -503,8 +507,9 @@ acpi_install_notify_handler(acpi_handle device,
 			obj_desc->common_notify.device_notify = notify_obj;
 		}
 
-		if (handler_type == ACPI_ALL_NOTIFY) {
+		acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 
+		if (handler_type == ACPI_ALL_NOTIFY) {
 			/* Extra ref if installed in both */
 
 			acpi_ut_add_reference(notify_obj);
drivers/acpi/acpica/utdelete.c +0 -0
index 31f5a78..7559813 100644
@@ -504,6 +504,7 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 
 			/* Update the notify objects for these types (if present) */
 
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 507

Quoted 1 line
drivers/acpi/acpica/utdelete.c +1 -0
+			acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

Can I acquire acpi_gbl_reference_count_lock here? Does is not cause recursive lock? Or direct acquire remove mutex here?

You can.
If you just want to hold the lock longer, you can introduce a lockless API to replace the one invoked below.

Quoted 8 lines

I just tried to change my mutex code to acquire acpi_gbl_reference_count_lock in acpi_ut_update_object_reference(). But when system boot, kernel pending on updating ref_count of parent object, in acpi_ut_update_ref_count() when acquire acpi_gbl_reference_count_lock.

On the other hand,
I didn't find lockless spinlock API in acpi subsystem, and even in acpica project git. Could you please help teach me where is the lockless function?

I agree this is not a suitable case for acpi_gbl_reference_count_lock usage.

I just want to say, this is an open source project, if you see any case need to extend the locking period, you can do any modification you want. For example, if you find such a case: copying the reference from its original owner to a copy of the owner and try to increase it. You can introduce an API to ensure atomicity by holding the acpi_gbl_reference_count_lock for the 2 operations.

Quoted 29 lines
Quoted 24 lines
Quoted 24 lines
Quoted 8 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 509

Quoted 3 lines
drivers/acpi/acpica/utdelete.c +0 -0
 			acpi_ut_update_ref_count(object->common_notify.
 						 system_notify, action);

If you take a look at acpi_ut_update_ref_count, there is already a spin_lock held around the code to update the reference count.

Thanks
-Lv

hm.... I am not sure can using acpi_gbl_reference_count_lock.

When updating parent object's ref_count, it should atomic with its notify objects's ref_count updating. The situation I want to avoid is:

a. acpi_ut_update_object_reference() update ref_count of system_notify/device_notify. (Thermal driver)
Assume the notify object didn't install to parent object yet. So, the ref_count doesn't increase.

b. acpi_install_notify_handler() install system_notify/device_notify. (Processor driver)
Processor driver install notify handler of parent object in this window. The ref_count of notify object is 1.

c. acpi_ut_update_object_reference() call acpi_ut_update_ref_count() to update ref_count of parent object. (Thermal driver)
The ref_count of parent object update to 2.
<=== this ref_count doesn't match with ref_count of notify object, it's 1.

Quoted 5 lines
Quoted 2 lines

Due to a. and c. steps should atomic, so I add ACPI_MTX_NOTIFY_REF_COUNT to bind them together, when the object type is processor, device, power or thermal.

I can see that the only problem is the sub-object's reference count updates.
It shouldn't be increased each time the parent object's reference count is increased, but should be increased only once when it is

Quoted 8 lines

linked to the parent object (referenced by the parent).

Quoted 1 line

That seems to be the root cause.

Yes, this is the main problem, either increase or decrease ref_count of parent object, the sub-objects(here is notify object) will also updating either increase or decrease.

In the case of my issue machine, when ref_count of parent object is 2 but sub-object's ref_count is 1, later decrease parent object's ref_count to 0 will causes sub-object's ref_count is -1. <=== it triggers the double free issue then slab will emit oops.

Can I say that:
The issue is caused by the current ACPICA locking implementation.
For this case, we need 2 locks:
1. The first lock need to be used each time we are going to update the members of an object.
2. The second lock need to be used to mutual exclusively executing the code that can affect the object's state, ensure it is not messed up from different call context. This should be achieved by some artificial locking facility. In many cases, the call path is locked without holding any locks that can lead to an atomic context.

Currently we may just doing wrong by utilizing the 2nd lock to meet the 1st requirement.

For ACPICA, shall we first ask:
1. do we have a per-object lock to meet the first requirement?
2. do we have a global lock to protect the consistencies of the namespace?

I think you should fix this by tuning the locking granularity.
The patch can do so unless there is no other mean can be used to fix this issue.

Thanks and best regards
-Lv

Quoted 44 lines
Quoted 3 lines

The current code might be a hackish result to work around some design defects.
So what you do in this patch might be dangerous, they can break ACPICA interpreter and you need to do it in the ACPICA source

base.

Quoted 6 lines

There is a recursive unit test facility - ASLTS to help determine if your change doesn't introduce regressions. I think you need to file a bug in ACPICA bugzilla and start a discussion there.

Thanks and best regards
-Lv

Thanks for your information, I will try ASLTS and file bug about the atomic between main object's ref_count with sub-object's ref_count.

Quoted 22 lines
Quoted 22 lines
Quoted 12 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 600

Quoted 12 lines
drivers/acpi/acpica/utdelete.c +5 -0
 			acpi_ut_update_ref_count(object->common_notify.
@@ -592,6 +593,11 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 		 * main object to be deleted.
 		 */
 		acpi_ut_update_ref_count(object, action);
+		if (object->common.type == ACPI_TYPE_PROCESSOR ||
+		    object->common.type == ACPI_TYPE_DEVICE ||
+		    object->common.type == ACPI_TYPE_POWER ||
+		    object->common.type == ACPI_TYPE_THERMAL)
+			acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

here also. I think need remove this release?

Quoted 6 lines
Quoted 5 lines
drivers/acpi/acpica/utdelete.c +0 -1
 		object = NULL;
 
 		/* Move on to the next object to be updated */
--

1.8.4.5

Quoted 5 lines
Quoted 5 lines

Please let me know if I miss understood.

Thanks a lot!
Joey Lee

Regards
Joey Lee

#6 lore raw

Hi Lv,

Any updates for this issue?

Thanks
Hanjun

Quoted 230 lines · Lv

Hi,

Quoted 121 lines

From: joeyli [mailto:[email protected]]
Sent: Tuesday, June 17, 2014 5:19 PM

Quoted 109 lines · Lv

Hi,

Quoted 78 lines

From: [email protected] [mailto:[email protected]] On Behalf Of joeyli
Sent: Monday, June 16, 2014 5:14 PM

Hi Lv Zheng,

Thanks for your response.

Quoted 70 lines · Lv

Hi,

Quoted 68 lines

From: Lee, Chun-Yi [mailto:[email protected]]
Sent: Monday, June 16, 2014 10:12 AM

This issue found on v3.0 kernel, unfortunately there was no chance to test latest kernel on issue mchine. This patch tested on v3.0 kernel then sent to linux-acpi for review and note, maybe latest kernel also need.

The problem happened when acpi thermal driver evaluate _PSL, but acpi processor driver install notify handler at the same time. In the code path of evaluate _PSL, it updates reference count of processor and its notify objects. When the notify handler installation done after the reference count of processor updated, it causes the ref_count of processor doesn't sync with its notify object's ref_count value.

Here is an debugging log when issue reproduced:

[    3.481773] ACPI_TYPE_PROCESSOR set ACPI_DEVICE_NOTIFY, object_desc->common.reference_count: 3, notify_obj-
Quoted 1 line

common.reference_count: 1

[    3.481958] PROCESSOR device_hid: LNXCPU
...
[    3.487427] ACPI_TYPE_PROCESSOR, action = 1
[    3.487428] Update device_notify ref_count
[    3.487429] REF_DECREMENT ACPI_TYPE_LOCAL_NOTIFY original_count: 0
[    3.487431] ACPI Warning: Obj ffff8800b0f40b28, Reference Count is already zero, cannot decrement
[    3.487433]  (20110413/utdelete-431)
[    3.487434] REF_DECREMENT ACPI_TYPE_PROCESSOR original_count: 2

Accroding log, found the reference_count of parent object
(it's processor in this case) is 3, it doesn't match with notify_object's reference_count, value is 1. It triggered "Reference Count is already zero" warning, then happen object double free issue later.

To avoid rece condition, this patch introded ACPI_MTX_NOTIFY_REF_COUNT mutex to keep the ref_count of notify object sync with its parent object. And, it also set the reference_count value of new notify object equals to its parent object's reference_count.

---
 drivers/acpi/acpica/aclocal.h  | 3 ++-
 drivers/acpi/acpica/evxface.c  | 7 ++++++-
 drivers/acpi/acpica/utdelete.c | 6 ++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
drivers/acpi/acpica/aclocal.h +2 -1
index c7f743c..e25a4af 100644
@@ -85,8 +85,9 @@ union acpi_parse_object;
 #define ACPI_MTX_MEMORY                 5	/* Debug memory tracking lists */
 #define ACPI_MTX_DEBUG_CMD_COMPLETE     6	/* AML debugger */
 #define ACPI_MTX_DEBUG_CMD_READY        7	/* AML debugger */
+#define ACPI_MTX_NOTIFY_REF_COUNT	8	/* Reference count of notify object */
 
-#define ACPI_MAX_MUTEX                  7
+#define ACPI_MAX_MUTEX                  8
 #define ACPI_NUM_MUTEX                  ACPI_MAX_MUTEX+1
 
 /* Lock structure for reader/writer interfaces */
drivers/acpi/acpica/evxface.c +0 -0
index e114140..213fe1a 100644
@@ -495,6 +495,10 @@ acpi_install_notify_handler(acpi_handle device,
 						handler, context,
 						NULL);
 
Quoted 7 lines
Quoted 6 lines

drivers/acpi/acpica/evxface.c acpi_install_notify_handler(acpi_handle device, · line 501

Quoted 4 lines
drivers/acpi/acpica/evxface.c +4 -0
+		acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
+
+		notify_obj->common.reference_count = obj_desc->common.reference_count;
+

Should be converted to spin_lock here.

Did you mean using acpi_gbl_reference_count_lock ?

Yes.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
drivers/acpi/acpica/evxface.c +2 -1
 		if (handler_type & ACPI_SYSTEM_NOTIFY) {
 			obj_desc->common_notify.system_notify = notify_obj;
 		}
@@ -503,8 +507,9 @@ acpi_install_notify_handler(acpi_handle device,
 			obj_desc->common_notify.device_notify = notify_obj;
 		}
 
-		if (handler_type == ACPI_ALL_NOTIFY) {
+		acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 
+		if (handler_type == ACPI_ALL_NOTIFY) {
 			/* Extra ref if installed in both */
 
 			acpi_ut_add_reference(notify_obj);
drivers/acpi/acpica/utdelete.c +0 -0
index 31f5a78..7559813 100644
@@ -504,6 +504,7 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 507

Quoted 3 lines
drivers/acpi/acpica/utdelete.c +1 -0
 			/* Update the notify objects for these types (if present) */
 
+			acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

Can I acquire acpi_gbl_reference_count_lock here? Does is not cause recursive lock? Or direct acquire remove mutex here?

You can.
If you just want to hold the lock longer, you can introduce a lockless API to replace the one invoked below.

Quoted 8 lines

I just tried to change my mutex code to acquire acpi_gbl_reference_count_lock in acpi_ut_update_object_reference(). But when system boot, kernel pending on updating ref_count of parent object, in acpi_ut_update_ref_count() when acquire acpi_gbl_reference_count_lock.

On the other hand,
I didn't find lockless spinlock API in acpi subsystem, and even in acpica project git. Could you please help teach me where is the lockless function?

I agree this is not a suitable case for acpi_gbl_reference_count_lock usage.

I just want to say, this is an open source project, if you see any case need to extend the locking period, you can do any modification you want. For example, if you find such a case: copying the reference from its original owner to a copy of the owner and try to increase it. You can introduce an API to ensure atomicity by holding the acpi_gbl_reference_count_lock for the 2 operations.

Quoted 25 lines
Quoted 17 lines
Quoted 17 lines
Quoted 6 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 509

Quoted 2 lines
drivers/acpi/acpica/utdelete.c +0 -0
 			acpi_ut_update_ref_count(object->common_notify.
 						 system_notify, action);

If you take a look at acpi_ut_update_ref_count, there is already a spin_lock held around the code to update the reference count.

Thanks
-Lv

hm.... I am not sure can using acpi_gbl_reference_count_lock.

When updating parent object's ref_count, it should atomic with its notify objects's ref_count updating. The situation I want to avoid is:

a. acpi_ut_update_object_reference() update ref_count of system_notify/device_notify. (Thermal driver)
Assume the notify object didn't install to parent object yet. So, the ref_count doesn't increase.

b. acpi_install_notify_handler() install system_notify/device_notify. (Processor driver)
Processor driver install notify handler of parent object in this window. The ref_count of notify object is 1.

Quoted 8 lines
Quoted 6 lines

c. acpi_ut_update_object_reference() call acpi_ut_update_ref_count() to update ref_count of parent object. (Thermal driver)
The ref_count of parent object update to 2.
<=== this ref_count doesn't match with ref_count of notify object, it's 1.

Due to a. and c. steps should atomic, so I add ACPI_MTX_NOTIFY_REF_COUNT to bind them together, when the object type is processor, device, power or thermal.

I can see that the only problem is the sub-object's reference count updates.
It shouldn't be increased each time the parent object's reference count is increased, but should be increased only once when it is

Quoted 7 lines

linked to the parent object (referenced by the parent).

Quoted 1 line

That seems to be the root cause.

Yes, this is the main problem, either increase or decrease ref_count of parent object, the sub-objects(here is notify object) will also updating either increase or decrease.

In the case of my issue machine, when ref_count of parent object is 2 but sub-object's ref_count is 1, later decrease parent object's ref_count to 0 will causes sub-object's ref_count is -1. <=== it triggers the double free issue then slab will emit oops.

Can I say that:
The issue is caused by the current ACPICA locking implementation.
For this case, we need 2 locks:
1. The first lock need to be used each time we are going to update the members of an object.
2. The second lock need to be used to mutual exclusively executing the code that can affect the object's state, ensure it is not messed up from different call context. This should be achieved by some artificial locking facility. In many cases, the call path is locked without holding any locks that can lead to an atomic context.

Currently we may just doing wrong by utilizing the 2nd lock to meet the 1st requirement.

For ACPICA, shall we first ask:
1. do we have a per-object lock to meet the first requirement?
2. do we have a global lock to protect the consistencies of the namespace?

I think you should fix this by tuning the locking granularity.
The patch can do so unless there is no other mean can be used to fix this issue.

Thanks and best regards
-Lv

Quoted 29 lines
Quoted 2 lines

The current code might be a hackish result to work around some design defects.
So what you do in this patch might be dangerous, they can break ACPICA interpreter and you need to do it in the ACPICA source

base.

Quoted 6 lines

There is a recursive unit test facility - ASLTS to help determine if your change doesn't introduce regressions. I think you need to file a bug in ACPICA bugzilla and start a discussion there.

Thanks and best regards
-Lv

Thanks for your information, I will try ASLTS and file bug about the atomic between main object's ref_count with sub-object's ref_count.

Quoted 17 lines
Quoted 17 lines
Quoted 10 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 600

Quoted 10 lines
drivers/acpi/acpica/utdelete.c +5 -0
 			acpi_ut_update_ref_count(object->common_notify.
@@ -592,6 +593,11 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 		 * main object to be deleted.
 		 */
 		acpi_ut_update_ref_count(object, action);
+		if (object->common.type == ACPI_TYPE_PROCESSOR ||
+		    object->common.type == ACPI_TYPE_DEVICE ||
+		    object->common.type == ACPI_TYPE_POWER ||
+		    object->common.type == ACPI_TYPE_THERMAL)
+			acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

here also. I think need remove this release?

Quoted 5 lines
Quoted 5 lines
drivers/acpi/acpica/utdelete.c +0 -1
 		object = NULL;
 
 		/* Move on to the next object to be updated */
--

1.8.4.5

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

Please let me know if I miss understood.

Thanks a lot!
Joey Lee

Regards
Joey Lee

--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

.

#7 lore raw

Hi,

There is an ACPICA bug tracking this issue:
http://bugs.acpica.org/show_bug.cgi?id=1115
We'll be back working on this after finishing other tasks.

Thanks and best regards
-Lv

Quoted 247 lines

From: Hanjun Guo [mailto:[email protected]]
Sent: Wednesday, June 03, 2015 2:53 PM

Hi Lv,

Any updates for this issue?

Thanks
Hanjun

Quoted 127 lines · Lv

Hi,

Quoted 125 lines

From: joeyli [mailto:[email protected]]
Sent: Tuesday, June 17, 2014 5:19 PM

Quoted 109 lines · Lv

Hi,

Quoted 78 lines

From: [email protected] [mailto:[email protected]] On Behalf Of joeyli
Sent: Monday, June 16, 2014 5:14 PM

Hi Lv Zheng,

Thanks for your response.

Quoted 70 lines · Lv

Hi,

Quoted 68 lines

From: Lee, Chun-Yi [mailto:[email protected]]
Sent: Monday, June 16, 2014 10:12 AM

This issue found on v3.0 kernel, unfortunately there was no chance to test latest kernel on issue mchine. This patch tested on v3.0 kernel then sent to linux-acpi for review and note, maybe latest kernel also need.

The problem happened when acpi thermal driver evaluate _PSL, but acpi processor driver install notify handler at the same time. In the code path of evaluate _PSL, it updates reference count of processor and its notify objects. When the notify handler installation done after the reference count of processor updated, it causes the ref_count of processor doesn't sync with its notify object's ref_count value.

Here is an debugging log when issue reproduced:

[    3.481773] ACPI_TYPE_PROCESSOR set ACPI_DEVICE_NOTIFY, object_desc->common.reference_count: 3, notify_obj-
Quoted 1 line

common.reference_count: 1

[    3.481958] PROCESSOR device_hid: LNXCPU
...
[    3.487427] ACPI_TYPE_PROCESSOR, action = 1
[    3.487428] Update device_notify ref_count
[    3.487429] REF_DECREMENT ACPI_TYPE_LOCAL_NOTIFY original_count: 0
[    3.487431] ACPI Warning: Obj ffff8800b0f40b28, Reference Count is already zero, cannot decrement
[    3.487433]  (20110413/utdelete-431)
[    3.487434] REF_DECREMENT ACPI_TYPE_PROCESSOR original_count: 2

Accroding log, found the reference_count of parent object
(it's processor in this case) is 3, it doesn't match with notify_object's reference_count, value is 1. It triggered "Reference Count is already zero" warning, then happen object double free issue later.

To avoid rece condition, this patch introded ACPI_MTX_NOTIFY_REF_COUNT mutex to keep the ref_count of notify object sync with its parent object. And, it also set the reference_count value of new notify object equals to its parent object's reference_count.

---
 drivers/acpi/acpica/aclocal.h  | 3 ++-
 drivers/acpi/acpica/evxface.c  | 7 ++++++-
 drivers/acpi/acpica/utdelete.c | 6 ++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
drivers/acpi/acpica/aclocal.h +2 -1
index c7f743c..e25a4af 100644
@@ -85,8 +85,9 @@ union acpi_parse_object;
 #define ACPI_MTX_MEMORY                 5	/* Debug memory tracking lists */
 #define ACPI_MTX_DEBUG_CMD_COMPLETE     6	/* AML debugger */
 #define ACPI_MTX_DEBUG_CMD_READY        7	/* AML debugger */
+#define ACPI_MTX_NOTIFY_REF_COUNT	8	/* Reference count of notify object */
 
-#define ACPI_MAX_MUTEX                  7
+#define ACPI_MAX_MUTEX                  8
 #define ACPI_NUM_MUTEX                  ACPI_MAX_MUTEX+1
 
 /* Lock structure for reader/writer interfaces */
drivers/acpi/acpica/evxface.c +0 -0
index e114140..213fe1a 100644
@@ -495,6 +495,10 @@ acpi_install_notify_handler(acpi_handle device,
 						handler, context,
 						NULL);
 
Quoted 7 lines
Quoted 6 lines

drivers/acpi/acpica/evxface.c acpi_install_notify_handler(acpi_handle device, · line 501

Quoted 4 lines
drivers/acpi/acpica/evxface.c +4 -0
+		acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
+
+		notify_obj->common.reference_count = obj_desc->common.reference_count;
+

Should be converted to spin_lock here.

Did you mean using acpi_gbl_reference_count_lock ?

Yes.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
drivers/acpi/acpica/evxface.c +2 -1
 		if (handler_type & ACPI_SYSTEM_NOTIFY) {
 			obj_desc->common_notify.system_notify = notify_obj;
 		}
@@ -503,8 +507,9 @@ acpi_install_notify_handler(acpi_handle device,
 			obj_desc->common_notify.device_notify = notify_obj;
 		}
 
-		if (handler_type == ACPI_ALL_NOTIFY) {
+		acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 
+		if (handler_type == ACPI_ALL_NOTIFY) {
 			/* Extra ref if installed in both */
 
 			acpi_ut_add_reference(notify_obj);
drivers/acpi/acpica/utdelete.c +0 -0
index 31f5a78..7559813 100644
@@ -504,6 +504,7 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 507

Quoted 3 lines
drivers/acpi/acpica/utdelete.c +1 -0
 			/* Update the notify objects for these types (if present) */
 
+			acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

Can I acquire acpi_gbl_reference_count_lock here? Does is not cause recursive lock? Or direct acquire remove mutex here?

You can.
If you just want to hold the lock longer, you can introduce a lockless API to replace the one invoked below.

I just tried to change my mutex code to acquire acpi_gbl_reference_count_lock in acpi_ut_update_object_reference(). But when system boot, kernel pending on updating ref_count of parent object, in acpi_ut_update_ref_count() when acquire acpi_gbl_reference_count_lock.

Quoted 7 lines
Quoted 4 lines

On the other hand,
I didn't find lockless spinlock API in acpi subsystem, and even in acpica project git. Could you please help teach me where is the lockless function?

I agree this is not a suitable case for acpi_gbl_reference_count_lock usage.

I just want to say, this is an open source project, if you see any case need to extend the locking period, you can do any modification

you want.

Quoted 6 lines

For example, if you find such a case: copying the reference from its original owner to a copy of the owner and try to increase it. You can introduce an API to ensure atomicity by holding the acpi_gbl_reference_count_lock for the 2 operations.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 509

Quoted 2 lines
drivers/acpi/acpica/utdelete.c +0 -0
 			acpi_ut_update_ref_count(object->common_notify.
 						 system_notify, action);

If you take a look at acpi_ut_update_ref_count, there is already a spin_lock held around the code to update the reference

count.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 3 lines

Thanks
-Lv

hm.... I am not sure can using acpi_gbl_reference_count_lock.

When updating parent object's ref_count, it should atomic with its notify objects's ref_count updating. The situation I want to avoid is:

a. acpi_ut_update_object_reference() update ref_count of system_notify/device_notify. (Thermal driver)
Assume the notify object didn't install to parent object yet. So, the ref_count doesn't increase.

b. acpi_install_notify_handler() install system_notify/device_notify. (Processor driver)
Processor driver install notify handler of parent object in this window. The ref_count of notify object is 1.

c. acpi_ut_update_object_reference() call acpi_ut_update_ref_count() to update ref_count of parent object. (Thermal driver)
The ref_count of parent object update to 2.
<=== this ref_count doesn't match with ref_count of notify object, it's 1.

Due to a. and c. steps should atomic, so I add ACPI_MTX_NOTIFY_REF_COUNT to bind them together, when the object type is processor, device, power or thermal.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

I can see that the only problem is the sub-object's reference count updates.
It shouldn't be increased each time the parent object's reference count is increased, but should be increased only once when it is

linked to the parent object (referenced by the parent).

Quoted 1 line

That seems to be the root cause.

Yes, this is the main problem, either increase or decrease ref_count of parent object, the sub-objects(here is notify object) will also updating either increase or decrease.

In the case of my issue machine, when ref_count of parent object is 2 but sub-object's ref_count is 1, later decrease parent

object's

Quoted 6 lines
Quoted 1 line

ref_count to 0 will causes sub-object's ref_count is -1. <=== it triggers the double free issue then slab will emit oops.

Can I say that:
The issue is caused by the current ACPICA locking implementation.
For this case, we need 2 locks:
1. The first lock need to be used each time we are going to update the members of an object.
2. The second lock need to be used to mutual exclusively executing the code that can affect the object's state, ensure it is not

messed up from different call context. This should be achieved by some artificial locking facility. In many cases, the call path is locked without holding any locks that can lead to an atomic context.

Quoted 56 lines

Currently we may just doing wrong by utilizing the 2nd lock to meet the 1st requirement.

For ACPICA, shall we first ask:
1. do we have a per-object lock to meet the first requirement?
2. do we have a global lock to protect the consistencies of the namespace?

I think you should fix this by tuning the locking granularity.
The patch can do so unless there is no other mean can be used to fix this issue.

Thanks and best regards
-Lv

Quoted 29 lines
Quoted 2 lines

The current code might be a hackish result to work around some design defects.
So what you do in this patch might be dangerous, they can break ACPICA interpreter and you need to do it in the ACPICA source

base.

Quoted 6 lines

There is a recursive unit test facility - ASLTS to help determine if your change doesn't introduce regressions. I think you need to file a bug in ACPICA bugzilla and start a discussion there.

Thanks and best regards
-Lv

Thanks for your information, I will try ASLTS and file bug about the atomic between main object's ref_count with sub-object's ref_count.

Quoted 17 lines
Quoted 17 lines
Quoted 10 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 600

Quoted 10 lines
drivers/acpi/acpica/utdelete.c +5 -0
 			acpi_ut_update_ref_count(object->common_notify.
@@ -592,6 +593,11 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 		 * main object to be deleted.
 		 */
 		acpi_ut_update_ref_count(object, action);
+		if (object->common.type == ACPI_TYPE_PROCESSOR ||
+		    object->common.type == ACPI_TYPE_DEVICE ||
+		    object->common.type == ACPI_TYPE_POWER ||
+		    object->common.type == ACPI_TYPE_THERMAL)
+			acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

here also. I think need remove this release?

Quoted 5 lines
Quoted 5 lines
drivers/acpi/acpica/utdelete.c +0 -1
 		object = NULL;
 
 		/* Move on to the next object to be updated */
--

1.8.4.5

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

Please let me know if I miss understood.

Thanks a lot!
Joey Lee

Regards
Joey Lee

--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

.

#8 lore raw
Quoted 5 lines · Lv

Hi,

There is an ACPICA bug tracking this issue:
http://bugs.acpica.org/show_bug.cgi?id=1115
We'll be back working on this after finishing other tasks.

Thanks for the update, and your work on this :)

Hanjun

Quoted 255 lines

Thanks and best regards
-Lv

Quoted 181 lines

From: Hanjun Guo [mailto:[email protected]]
Sent: Wednesday, June 03, 2015 2:53 PM

Hi Lv,

Any updates for this issue?

Thanks
Hanjun

Quoted 127 lines · Lv

Hi,

Quoted 125 lines

From: joeyli [mailto:[email protected]]
Sent: Tuesday, June 17, 2014 5:19 PM

Quoted 109 lines · Lv

Hi,

Quoted 78 lines

From: [email protected] [mailto:[email protected]] On Behalf Of joeyli
Sent: Monday, June 16, 2014 5:14 PM

Hi Lv Zheng,

Thanks for your response.

Quoted 70 lines · Lv

Hi,

Quoted 68 lines

From: Lee, Chun-Yi [mailto:[email protected]]
Sent: Monday, June 16, 2014 10:12 AM

This issue found on v3.0 kernel, unfortunately there was no chance to test latest kernel on issue mchine. This patch tested on v3.0 kernel then sent to linux-acpi for review and note, maybe latest kernel also need.

The problem happened when acpi thermal driver evaluate _PSL, but acpi processor driver install notify handler at the same time. In the code path of evaluate _PSL, it updates reference count of processor and its notify objects. When the notify handler installation done after the reference count of processor updated, it causes the ref_count of processor doesn't sync with its notify object's ref_count value.

Here is an debugging log when issue reproduced:

[    3.481773] ACPI_TYPE_PROCESSOR set ACPI_DEVICE_NOTIFY, object_desc->common.reference_count: 3, notify_obj-
Quoted 1 line

common.reference_count: 1

[    3.481958] PROCESSOR device_hid: LNXCPU
...
[    3.487427] ACPI_TYPE_PROCESSOR, action = 1
[    3.487428] Update device_notify ref_count
[    3.487429] REF_DECREMENT ACPI_TYPE_LOCAL_NOTIFY original_count: 0
[    3.487431] ACPI Warning: Obj ffff8800b0f40b28, Reference Count is already zero, cannot decrement
[    3.487433]  (20110413/utdelete-431)
[    3.487434] REF_DECREMENT ACPI_TYPE_PROCESSOR original_count: 2

Accroding log, found the reference_count of parent object
(it's processor in this case) is 3, it doesn't match with notify_object's reference_count, value is 1. It triggered "Reference Count is already zero" warning, then happen object double free issue later.

To avoid rece condition, this patch introded ACPI_MTX_NOTIFY_REF_COUNT mutex to keep the ref_count of notify object sync with its parent object. And, it also set the reference_count value of new notify object equals to its parent object's reference_count.

---
 drivers/acpi/acpica/aclocal.h  | 3 ++-
 drivers/acpi/acpica/evxface.c  | 7 ++++++-
 drivers/acpi/acpica/utdelete.c | 6 ++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
drivers/acpi/acpica/aclocal.h +2 -1
index c7f743c..e25a4af 100644
@@ -85,8 +85,9 @@ union acpi_parse_object;
 #define ACPI_MTX_MEMORY                 5	/* Debug memory tracking lists */
 #define ACPI_MTX_DEBUG_CMD_COMPLETE     6	/* AML debugger */
 #define ACPI_MTX_DEBUG_CMD_READY        7	/* AML debugger */
+#define ACPI_MTX_NOTIFY_REF_COUNT	8	/* Reference count of notify object */
 
-#define ACPI_MAX_MUTEX                  7
+#define ACPI_MAX_MUTEX                  8
 #define ACPI_NUM_MUTEX                  ACPI_MAX_MUTEX+1
 
 /* Lock structure for reader/writer interfaces */
drivers/acpi/acpica/evxface.c +0 -0
index e114140..213fe1a 100644
@@ -495,6 +495,10 @@ acpi_install_notify_handler(acpi_handle device,
 						handler, context,
 						NULL);
 
Quoted 7 lines
Quoted 6 lines

drivers/acpi/acpica/evxface.c acpi_install_notify_handler(acpi_handle device, · line 501

Quoted 4 lines
drivers/acpi/acpica/evxface.c +4 -0
+		acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
+
+		notify_obj->common.reference_count = obj_desc->common.reference_count;
+

Should be converted to spin_lock here.

Did you mean using acpi_gbl_reference_count_lock ?

Yes.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
drivers/acpi/acpica/evxface.c +2 -1
 		if (handler_type & ACPI_SYSTEM_NOTIFY) {
 			obj_desc->common_notify.system_notify = notify_obj;
 		}
@@ -503,8 +507,9 @@ acpi_install_notify_handler(acpi_handle device,
 			obj_desc->common_notify.device_notify = notify_obj;
 		}
 
-		if (handler_type == ACPI_ALL_NOTIFY) {
+		acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);
 
+		if (handler_type == ACPI_ALL_NOTIFY) {
 			/* Extra ref if installed in both */
 
 			acpi_ut_add_reference(notify_obj);
drivers/acpi/acpica/utdelete.c +0 -0
index 31f5a78..7559813 100644
@@ -504,6 +504,7 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 507

Quoted 3 lines
drivers/acpi/acpica/utdelete.c +1 -0
 			/* Update the notify objects for these types (if present) */
 
+			acpi_ut_acquire_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

Can I acquire acpi_gbl_reference_count_lock here? Does is not cause recursive lock? Or direct acquire remove mutex here?

You can.
If you just want to hold the lock longer, you can introduce a lockless API to replace the one invoked below.

I just tried to change my mutex code to acquire acpi_gbl_reference_count_lock in acpi_ut_update_object_reference(). But when system boot, kernel pending on updating ref_count of parent object, in acpi_ut_update_ref_count() when acquire acpi_gbl_reference_count_lock.

Quoted 7 lines
Quoted 4 lines

On the other hand,
I didn't find lockless spinlock API in acpi subsystem, and even in acpica project git. Could you please help teach me where is the lockless function?

I agree this is not a suitable case for acpi_gbl_reference_count_lock usage.

I just want to say, this is an open source project, if you see any case need to extend the locking period, you can do any modification

you want.

Quoted 6 lines

For example, if you find such a case: copying the reference from its original owner to a copy of the owner and try to increase it. You can introduce an API to ensure atomicity by holding the acpi_gbl_reference_count_lock for the 2 operations.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 509

Quoted 2 lines
drivers/acpi/acpica/utdelete.c +0 -0
 			acpi_ut_update_ref_count(object->common_notify.
 						 system_notify, action);

If you take a look at acpi_ut_update_ref_count, there is already a spin_lock held around the code to update the reference

count.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 2 lines

Thanks
-Lv

hm.... I am not sure can using acpi_gbl_reference_count_lock.

When updating parent object's ref_count, it should atomic with its notify objects's ref_count updating. The situation I want to avoid is:

a. acpi_ut_update_object_reference() update ref_count of system_notify/device_notify. (Thermal driver)
Assume the notify object didn't install to parent object yet. So, the ref_count doesn't increase.

b. acpi_install_notify_handler() install system_notify/device_notify. (Processor driver)
Processor driver install notify handler of parent object in this window. The ref_count of notify object is 1.

c. acpi_ut_update_object_reference() call acpi_ut_update_ref_count() to update ref_count of parent object. (Thermal driver)
The ref_count of parent object update to 2.
<=== this ref_count doesn't match with ref_count of notify object, it's 1.

Due to a. and c. steps should atomic, so I add ACPI_MTX_NOTIFY_REF_COUNT to bind them together, when the object type is processor, device, power or thermal.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

I can see that the only problem is the sub-object's reference count updates.
It shouldn't be increased each time the parent object's reference count is increased, but should be increased only once when it is

linked to the parent object (referenced by the parent).

Quoted 1 line

That seems to be the root cause.

Yes, this is the main problem, either increase or decrease ref_count of parent object, the sub-objects(here is notify object) will also updating either increase or decrease.

In the case of my issue machine, when ref_count of parent object is 2 but sub-object's ref_count is 1, later decrease parent

object's

Quoted 63 lines
Quoted 6 lines
Quoted 1 line

ref_count to 0 will causes sub-object's ref_count is -1. <=== it triggers the double free issue then slab will emit oops.

Can I say that:
The issue is caused by the current ACPICA locking implementation.
For this case, we need 2 locks:
1. The first lock need to be used each time we are going to update the members of an object.
2. The second lock need to be used to mutual exclusively executing the code that can affect the object's state, ensure it is not

messed up from different call context. This should be achieved by some artificial locking facility. In many cases, the call path is locked without holding any locks that can lead to an atomic context.

Quoted 55 lines

Currently we may just doing wrong by utilizing the 2nd lock to meet the 1st requirement.

For ACPICA, shall we first ask:
1. do we have a per-object lock to meet the first requirement?
2. do we have a global lock to protect the consistencies of the namespace?

I think you should fix this by tuning the locking granularity.
The patch can do so unless there is no other mean can be used to fix this issue.

Thanks and best regards
-Lv

Quoted 29 lines
Quoted 2 lines

The current code might be a hackish result to work around some design defects.
So what you do in this patch might be dangerous, they can break ACPICA interpreter and you need to do it in the ACPICA source

base.

Quoted 6 lines

There is a recursive unit test facility - ASLTS to help determine if your change doesn't introduce regressions. I think you need to file a bug in ACPICA bugzilla and start a discussion there.

Thanks and best regards
-Lv

Thanks for your information, I will try ASLTS and file bug about the atomic between main object's ref_count with sub-object's ref_count.

Quoted 17 lines
Quoted 17 lines
Quoted 10 lines

drivers/acpi/acpica/utdelete.c acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action) · line 600

Quoted 10 lines
drivers/acpi/acpica/utdelete.c +5 -0
 			acpi_ut_update_ref_count(object->common_notify.
@@ -592,6 +593,11 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 		 * main object to be deleted.
 		 */
 		acpi_ut_update_ref_count(object, action);
+		if (object->common.type == ACPI_TYPE_PROCESSOR ||
+		    object->common.type == ACPI_TYPE_DEVICE ||
+		    object->common.type == ACPI_TYPE_POWER ||
+		    object->common.type == ACPI_TYPE_THERMAL)
+			acpi_ut_release_mutex(ACPI_MTX_NOTIFY_REF_COUNT);

here also. I think need remove this release?

Quoted 5 lines
Quoted 5 lines
drivers/acpi/acpica/utdelete.c +0 -1
 		object = NULL;
 
 		/* Move on to the next object to be updated */
--

1.8.4.5

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

Please let me know if I miss understood.

Thanks a lot!
Joey Lee

Regards
Joey Lee

--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

.

--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

.