Skip to contentSkip to conversation

Subject: thermal: Fix potential race condition in suspend/resume

PATCH In review Bo Ye 3 participants 9 messages Sep 16, 2023 – Dec 6, 2023

Subsystems (2)
lore
Share & tools
lore

Bo Ye

author
#0 lore raw

PATCH 1/1 Subject: thermal: Fix potential race condition in suspend/resume

From: "yugang.wang" <[email protected]>

Body:
This patch fixes a race condition during system resume. It occurs if the system is exiting a suspend state and a user is trying to register/unregister a thermal zone concurrently. The root cause is that both actions access the thermal_tz_list.

In detail:

1. At PM_POST_SUSPEND during the resume, the system reads all thermal zones in thermal_tz_list, then resets and updates their temperatures.
2. When registering/unregistering a thermal zone, the thermal_tz_list gets manipulated.

These two actions might occur concurrently, causing a race condition. To solve this issue, we introduce a mutex lock to protect thermal_tz_list from being modified while it's being read and updated during the resume from suspend.

Kernel oops excerpt related to this fix:

[ 5201.869845] [T316822] pc: [0xffffffeb7d4876f0] mutex_lock+0x34/0x170
[ 5201.869856] [T316822] lr: [0xffffffeb7ca98a84] thermal_pm_notify+0xd4/0x26c
[... cut for brevity ...]
[ 5201.871061] [T316822]  suspend_prepare+0x150/0x470
[ 5201.871067] [T316822]  enter_state+0x84/0x6f4
[ 5201.871076] [T316822]  state_store+0x15c/0x1e8
Change-Id Ifdbdecba17093f91eab7e36ce04b46d311ca6568
---
 drivers/thermal/thermal_core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/thermal/thermal_core.c +2 -0
index 8717a3343512..a7a18ed57b6d 100644
@@ -1529,12 +1529,14 @@ static int thermal_pm_notify(struct notifier_block *nb,
 	case PM_POST_HIBERNATION:
 	case PM_POST_RESTORE:
 	case PM_POST_SUSPEND:
+		mutex_lock(&thermal_list_lock);
 		atomic_set(&in_suspend, 0);
 		list_for_each_entry(tz, &thermal_tz_list, node) {
 			thermal_zone_device_init(tz);
 			thermal_zone_device_update(tz,
 						   THERMAL_EVENT_UNSPECIFIED);
 		}
+		mutex_unlock(&thermal_list_lock);
 		break;
 	default:
 		break;
Signature · 1 line
2.17.0
#1 lore raw

Re: [PATCH] thermal: Fix potential race condition in suspend/resume

On Sat, 2023-09-16 at 19:33 +0800, Bo Ye wrote:

Correct mail title format: remove "Subject:" from mail title.

Quoted 61 lines

From: "yugang.wang" <[email protected]>

Body:
This patch fixes a race condition during system resume. It occurs if the system is exiting a suspend state and a user is trying to register/unregister a thermal zone concurrently. The root cause is that both actions access the thermal_tz_list.

In detail:

1. At PM_POST_SUSPEND during the resume, the system reads all thermal zones in thermal_tz_list, then resets and updates their temperatures.
2. When registering/unregistering a thermal zone, the thermal_tz_list gets manipulated.

These two actions might occur concurrently, causing a race condition. To solve this issue, we introduce a mutex lock to protect thermal_tz_list from being modified while it's being read and updated during the resume from suspend.

Kernel oops excerpt related to this fix:

[ 5201.869845] [T316822] pc: [0xffffffeb7d4876f0]
mutex_lock+0x34/0x170
[ 5201.869856] [T316822] lr: [0xffffffeb7ca98a84]
thermal_pm_notify+0xd4/0x26c
[... cut for brevity ...]
[ 5201.871061] [T316822]  suspend_prepare+0x150/0x470
[ 5201.871067] [T316822]  enter_state+0x84/0x6f4
[ 5201.871076] [T316822]  state_store+0x15c/0x1e8
Change-Id Ifdbdecba17093f91eab7e36ce04b46d311ca6568
---
 drivers/thermal/thermal_core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/thermal/thermal_core.c +2 -0
index 8717a3343512..a7a18ed57b6d 100644
@@ -1529,12 +1529,14 @@ static int thermal_pm_notify(struct
 notifier_block *nb,
 	case PM_POST_HIBERNATION:
 	case PM_POST_RESTORE:
 	case PM_POST_SUSPEND:
+		mutex_lock(&thermal_list_lock);
 		atomic_set(&in_suspend, 0);
 		list_for_each_entry(tz, &thermal_tz_list, node) {
 			thermal_zone_device_init(tz);
 			thermal_zone_device_update(tz,
 						   THERMAL_EVENT_UNSPEC
 IFIED);
 		}
+		mutex_unlock(&thermal_list_lock);
 		break;
drivers/thermal/thermal_core.c +0 -0
 	default:
 		break;
#2 lore raw

Re: [PATCH] thermal: Fix potential race condition in suspend/resume

Yes, it is observed issue.
Firstly, it needs to be clarified that this issue occurs in a real- world environment. By analyzing the logs, we inferred that the issue occurred just as the system was entering suspend mode, and the user was switching the thermal policy (this action causes all thermal zones to unregister/register). In addition, we conducted degradation tests and also reproduced this issue. The specific method is to first switch the thermal policy through a command, and then immediately put the system into suspend state through another command. This method can also reproduce the issue.

Quoted 68 lines · Bo Ye (叶波)

On Sat, 2023-09-16 at 19:33 +0800, Bo Ye wrote:

Correct mail title format: remove "Subject:" from mail title.

Quoted 64 lines

From: "yugang.wang" <[email protected]>

Body:
This patch fixes a race condition during system resume. It occurs if
the system is exiting a suspend state and a user is trying to register/unregister a thermal zone concurrently. The root cause is that both actions access the thermal_tz_list.

In detail:

1. At PM_POST_SUSPEND during the resume, the system reads all thermal
zones in thermal_tz_list, then resets and updates their temperatures.
2. When registering/unregistering a thermal zone, the thermal_tz_list gets manipulated.

These two actions might occur concurrently, causing a race condition.
To solve this issue, we introduce a mutex lock to protect thermal_tz_list from being modified while it's being read and updated during the resume from suspend.

Kernel oops excerpt related to this fix:

[ 5201.869845] [T316822] pc: [0xffffffeb7d4876f0]
mutex_lock+0x34/0x170
[ 5201.869856] [T316822] lr: [0xffffffeb7ca98a84]
thermal_pm_notify+0xd4/0x26c
[... cut for brevity ...]
[ 5201.871061] [T316822]  suspend_prepare+0x150/0x470
[ 5201.871067] [T316822]  enter_state+0x84/0x6f4
[ 5201.871076] [T316822]  state_store+0x15c/0x1e8
Change-Id Ifdbdecba17093f91eab7e36ce04b46d311ca6568
---
 drivers/thermal/thermal_core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/thermal/thermal_core.c +2 -0
index 8717a3343512..a7a18ed57b6d 100644
@@ -1529,12 +1529,14 @@ static int thermal_pm_notify(struct
 notifier_block *nb,
 	case PM_POST_HIBERNATION:
 	case PM_POST_RESTORE:
 	case PM_POST_SUSPEND:
+		mutex_lock(&thermal_list_lock);
 		atomic_set(&in_suspend, 0);
 		list_for_each_entry(tz, &thermal_tz_list, node) {
 			thermal_zone_device_init(tz);
 			thermal_zone_device_update(tz,
 						   THERMAL_EVENT_UNSPEC
 IFIED);
 		}
+		mutex_unlock(&thermal_list_lock);
 		break;
drivers/thermal/thermal_core.c +0 -0
 	default:
 		break;
#3 lore raw

Re: [PATCH] thermal: Fix potential race condition in suspend/resume

Quoted 2 lines · Bo Ye (叶波)

Yes, it is observed issue.

It does happen, so it's not just "potential" and the subject of the patch is slightly misleading. Please adjust it.

Quoted 9 lines

Firstly, it needs to be clarified that this issue occurs in a real- world environment. By analyzing the logs, we inferred that the issue occurred just as the system was entering suspend mode, and the user was switching the thermal policy (this action causes all thermal zones to unregister/register). In addition, we conducted degradation tests and also reproduced this issue. The specific method is to first switch the thermal policy through a command, and then immediately put the system into suspend state through another command. This method can also reproduce the issue.

OK, so please add this information to the patch changelog.

Quoted 31 lines · Bo Ye (叶波)
Quoted 30 lines · Bo Ye

Correct mail title format: remove "Subject:" from mail title.

Quoted 26 lines

From: "yugang.wang" <[email protected]>

Body:
This patch fixes a race condition during system resume. It occurs if
the system is exiting a suspend state and a user is trying to register/unregister a thermal zone concurrently. The root cause is that both actions access the thermal_tz_list.

In detail:

1. At PM_POST_SUSPEND during the resume, the system reads all thermal
zones in thermal_tz_list, then resets and updates their temperatures.
2. When registering/unregistering a thermal zone, the thermal_tz_list gets manipulated.

These two actions might occur concurrently, causing a race condition.
To solve this issue, we introduce a mutex lock to protect thermal_tz_list from being modified while it's being read and updated during the resume from suspend.

Kernel oops excerpt related to this fix:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
[ 5201.869845] [T316822] pc: [0xffffffeb7d4876f0]
mutex_lock+0x34/0x170
[ 5201.869856] [T316822] lr: [0xffffffeb7ca98a84]
thermal_pm_notify+0xd4/0x26c
[... cut for brevity ...]
[ 5201.871061] [T316822]  suspend_prepare+0x150/0x470
[ 5201.871067] [T316822]  enter_state+0x84/0x6f4
[ 5201.871076] [T316822]  state_store+0x15c/0x1e8

Well, the connection between the above log snippet and the issue addressed by the patch is rather hard to establish. Please include more of the oops information.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Change-Id Ifdbdecba17093f91eab7e36ce04b46d311ca6568
---
 drivers/thermal/thermal_core.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
drivers/thermal/thermal_core.c +0 -1
index 8717a3343512..a7a18ed57b6d 100644
--- a/drivers/thermal/thermal_core.c
Quoted 8 lines

drivers/thermal/thermal_core.c static int thermal_pm_notify(struct · line 1534

Quoted 8 lines

+++ b/drivers/thermal/thermal_core.c

drivers/thermal/thermal_core.c +1 -0
@@ -1529,12 +1529,14 @@ static int thermal_pm_notify(struct
 notifier_block *nb,
    case PM_POST_HIBERNATION:
    case PM_POST_RESTORE:
    case PM_POST_SUSPEND:
+           mutex_lock(&thermal_list_lock);
            atomic_set(&in_suspend, 0);

It is not clear to me why the above statement needs to be under the lock.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/thermal/thermal_core.c +1 -0
            list_for_each_entry(tz, &thermal_tz_list, node) {
                    thermal_zone_device_init(tz);
                    thermal_zone_device_update(tz,
                                               THERMAL_EVENT_UNSPEC
 IFIED);
            }
+           mutex_unlock(&thermal_list_lock);
            break;
    default:
            break;
#4 lore raw

Re: [PATCH] thermal: Fix race condition in suspend/resume

Quoted 10 lines · Rafael J. Wysocki

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 2 lines · Bo Ye (叶波)

Yes, it is observed issue.

It does happen, so it's not just "potential" and the subject of the patch is slightly misleading. Please adjust it.

Done

Quoted 61 lines
Quoted 3 lines

Firstly, it needs to be clarified that this issue occurs in a real- world environment. By analyzing the logs, we inferred that the

issue

Quoted 1 line

occurred just as the system was entering suspend mode, and the user

was

Quoted 1 line

switching the thermal policy (this action causes all thermal zones

to

Quoted 1 line

unregister/register). In addition, we conducted degradation tests

and

Quoted 1 line

also reproduced this issue. The specific method is to first switch

the

Quoted 1 line

thermal policy through a command, and then immediately put the

system

Quoted 2 lines

into suspend state through another command. This method can also reproduce the issue.

OK, so please add this information to the patch changelog.

Quoted 9 lines · Bo Ye (叶波)
Quoted 8 lines · Bo Ye

Correct mail title format: remove "Subject:" from mail title.

Quoted 4 lines

From: "yugang.wang" <[email protected]>

Body:
This patch fixes a race condition during system resume. It

occurs

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

if
the system is exiting a suspend state and a user is trying to register/unregister a thermal zone concurrently. The root cause

is

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

that both actions access the thermal_tz_list.

In detail:

1. At PM_POST_SUSPEND during the resume, the system reads all thermal
zones in thermal_tz_list, then resets and updates their temperatures.
2. When registering/unregistering a thermal zone, the thermal_tz_list gets manipulated.

These two actions might occur concurrently, causing a race condition.
To solve this issue, we introduce a mutex lock to protect thermal_tz_list from being modified while it's being read and updated during the resume from suspend.

Kernel oops excerpt related to this fix:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
[ 5201.869845] [T316822] pc: [0xffffffeb7d4876f0]
mutex_lock+0x34/0x170
[ 5201.869856] [T316822] lr: [0xffffffeb7ca98a84]
thermal_pm_notify+0xd4/0x26c
[... cut for brevity ...]
[ 5201.871061] [T316822]  suspend_prepare+0x150/0x470
[ 5201.871067] [T316822]  enter_state+0x84/0x6f4
[ 5201.871076] [T316822]  state_store+0x15c/0x1e8
Quoted 3 lines

Well, the connection between the above log snippet and the issue addressed by the patch is rather hard to establish. Please include more of the oops information.

Thank you very much for reviewing the additional explanations.

1.Enable thermal policy operation will unregister/register all thermal zones
10-21 06:13:59.280 854 922 I libMtcLoader: enable thermal policy
thermal_policy_09.

2.System suspend entry time is 2023-10-20 22:13:59.242

[ 4106.364175][T609387] binder:534_2: [name:spm&][SPM] PM: suspend
entry 2023-10-20 22:13:59.242898243 UTC
[ 4106.366185][T609387] binder:534_2: PM: [name:wakeup&]PM: Pending

Wakeup Sources: NETLINK

3. It can be proven that the absence of a switch strategy will perform unregister/register operations on thermal zones (android time is 2023- 10-20 22:13:59.282),
Because the logs for other thermal zones switching are not enabled by default, we cannot see the logs related to other thermal zones.

[ 4106.404167][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_unbind unbinding OK
[ 4106.404215][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_unbind unbinding OK
[ 4106.404225][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_unbind unbinding OK
[ 4106.404504][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_bind binding OK, 0
[ 4106.404545][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_bind binding OK, 2
[ 4106.404566][T600922] mtkPowerMsgHdl:

[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_bind binding OK, 1

4. thermal_pm_notify trigger KE(android time: 2023-10-20 22:13:59. 315894)

[ 4106.437171][T209387] binder:534_2: [name:mrdump&]Kernel Offset:
0x289cc80000 from 0xffffffc008000000
[ 4106.437182][T209387] binder:534_2: [name:mrdump&]PHYS_OFFSET:
0x40000000
[ 4106.437191][T209387] binder:534_2: [name:mrdump&]pstate: 80400005
(Nzcv daif +PAN -UAO)
[ 4106.437204][T209387] binder:534_2: [name:mrdump&]pc :
[0xffffffe8a6688200] mutex_lock+0x34/0x184
[ 4106.437214][T209387] binder:534_2: [name:mrdump&]lr :
[0xffffffe8a5ce66bc] thermal_pm_notify+0xd4/0x26c
[ 4106.437220][T209387] binder:534_2: [name:mrdump&]sp :
ffffffc01bab3ae0
[ 4106.437226][T209387] binder:534_2: [name:mrdump&]x29:

ffffffc01bab3af0 x28: 0000000000000001

Quoted 36 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Change-Id Ifdbdecba17093f91eab7e36ce04b46d311ca6568
---
 drivers/thermal/thermal_core.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
drivers/thermal/thermal_core.c +0 -1
index 8717a3343512..a7a18ed57b6d 100644
--- a/drivers/thermal/thermal_core.c
Quoted 8 lines

drivers/thermal/thermal_core.c static int thermal_pm_notify(struct · line 1534

Quoted 8 lines

+++ b/drivers/thermal/thermal_core.c

drivers/thermal/thermal_core.c +1 -0
@@ -1529,12 +1529,14 @@ static int thermal_pm_notify(struct
 notifier_block *nb,
    case PM_POST_HIBERNATION:
    case PM_POST_RESTORE:
    case PM_POST_SUSPEND:
+           mutex_lock(&thermal_list_lock);
            atomic_set(&in_suspend, 0);

It is not clear to me why the above statement needs to be under the lock.

Quoted 11 lines
Quoted 4 lines

drivers/thermal/thermal_core.c static int thermal_pm_notify(struct notifier_block *nb, · line 1536

Quoted 3 lines
drivers/thermal/thermal_core.c +0 -0
            list_for_each_entry(tz, &thermal_tz_list, node) {
                    thermal_zone_device_init(tz);
                    thermal_zone_device_update(tz,

THERMAL_EVENT_UNSPEC

Quoted 6 lines
Quoted 6 lines
drivers/thermal/thermal_core.c +1 -0
 IFIED);
            }
+           mutex_unlock(&thermal_list_lock);
            break;
    default:
            break;
#5 lore raw

Re: [PATCH] thermal: Fix race condition in suspend/resume

Hi Rafael sir,

Could you help review this patch, thanks a lot.

Best Regards
Bo Ye

Quoted 182 lines · Bo Ye
Quoted 11 lines · Rafael J. Wysocki

External email : Please do not click links or open attachments until
you have verified the sender or the content.

Quoted 2 lines · Bo Ye (叶波)

Yes, it is observed issue.

It does happen, so it's not just "potential" and the subject of the patch is slightly misleading. Please adjust it.

Done

Quoted 75 lines
Quoted 4 lines

Firstly, it needs to be clarified that this issue occurs in a real-
world environment. By analyzing the logs, we inferred that the

issue

Quoted 2 lines

occurred just as the system was entering suspend mode, and the user

was

Quoted 2 lines

switching the thermal policy (this action causes all thermal zones

to

Quoted 1 line

unregister/register). In addition, we conducted degradation tests

and

Quoted 2 lines

also reproduced this issue. The specific method is to first switch

the

Quoted 1 line

thermal policy through a command, and then immediately put the

system

Quoted 2 lines

into suspend state through another command. This method can also reproduce the issue.

OK, so please add this information to the patch changelog.

Quoted 9 lines · Bo Ye (叶波)
Quoted 8 lines · Bo Ye

Correct mail title format: remove "Subject:" from mail title.

Quoted 4 lines

From: "yugang.wang" <[email protected]>

Body:
This patch fixes a race condition during system resume. It

occurs

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

if
the system is exiting a suspend state and a user is trying to register/unregister a thermal zone concurrently. The root cause

is

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

that both actions access the thermal_tz_list.

In detail:

1. At PM_POST_SUSPEND during the resume, the system reads all thermal
zones in thermal_tz_list, then resets and updates their temperatures.
2. When registering/unregistering a thermal zone, the thermal_tz_list gets manipulated.

These two actions might occur concurrently, causing a race condition.
To solve this issue, we introduce a mutex lock to protect thermal_tz_list from being modified while it's being read and
updated during the resume from suspend.

Kernel oops excerpt related to this fix:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
[ 5201.869845] [T316822] pc: [0xffffffeb7d4876f0]
mutex_lock+0x34/0x170
[ 5201.869856] [T316822] lr: [0xffffffeb7ca98a84]
thermal_pm_notify+0xd4/0x26c
[... cut for brevity ...]
[ 5201.871061] [T316822]  suspend_prepare+0x150/0x470
[ 5201.871067] [T316822]  enter_state+0x84/0x6f4
[ 5201.871076] [T316822]  state_store+0x15c/0x1e8
Quoted 3 lines

Well, the connection between the above log snippet and the issue addressed by the patch is rather hard to establish. Please include more of the oops information.

Thank you very much for reviewing the additional explanations.

1.Enable thermal policy operation will unregister/register all thermal
zones
10-21 06:13:59.280 854 922 I libMtcLoader: enable thermal policy
thermal_policy_09.

2.System suspend entry time is 2023-10-20 22:13:59.242

[ 4106.364175][T609387] binder:534_2: [name:spm&][SPM] PM: suspend
entry 2023-10-20 22:13:59.242898243 UTC
[ 4106.366185][T609387] binder:534_2: PM: [name:wakeup&]PM: Pending

Wakeup Sources: NETLINK

3. It can be proven that the absence of a switch strategy will perform
unregister/register operations on thermal zones (android time is 2023-
10-20 22:13:59.282),
Because the logs for other thermal zones switching are not enabled by default, we cannot see the logs related to other thermal zones.

[ 4106.404167][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_unbind unbinding OK
[ 4106.404215][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_unbind unbinding OK
[ 4106.404225][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_unbind unbinding OK
[ 4106.404504][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_bind binding OK, 0
[ 4106.404545][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_bind binding OK, 2
[ 4106.404566][T600922] mtkPowerMsgHdl:

[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_bind binding OK, 1

4. thermal_pm_notify trigger KE(android time: 2023-10-20 22:13:59. 315894)

[ 4106.437171][T209387] binder:534_2: [name:mrdump&]Kernel Offset:
0x289cc80000 from 0xffffffc008000000
[ 4106.437182][T209387] binder:534_2: [name:mrdump&]PHYS_OFFSET:
0x40000000
[ 4106.437191][T209387] binder:534_2: [name:mrdump&]pstate: 80400005
(Nzcv daif +PAN -UAO)
[ 4106.437204][T209387] binder:534_2: [name:mrdump&]pc :
[0xffffffe8a6688200] mutex_lock+0x34/0x184
[ 4106.437214][T209387] binder:534_2: [name:mrdump&]lr :
[0xffffffe8a5ce66bc] thermal_pm_notify+0xd4/0x26c
[ 4106.437220][T209387] binder:534_2: [name:mrdump&]sp :
ffffffc01bab3ae0
[ 4106.437226][T209387] binder:534_2: [name:mrdump&]x29:

ffffffc01bab3af0 x28: 0000000000000001

Quoted 37 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Change-Id Ifdbdecba17093f91eab7e36ce04b46d311ca6568
---
 drivers/thermal/thermal_core.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
drivers/thermal/thermal_core.c +0 -1
index 8717a3343512..a7a18ed57b6d 100644
--- a/drivers/thermal/thermal_core.c
Quoted 8 lines

drivers/thermal/thermal_core.c static int thermal_pm_notify(struct · line 1534

Quoted 8 lines

+++ b/drivers/thermal/thermal_core.c

drivers/thermal/thermal_core.c +1 -0
@@ -1529,12 +1529,14 @@ static int thermal_pm_notify(struct
 notifier_block *nb,
    case PM_POST_HIBERNATION:
    case PM_POST_RESTORE:
    case PM_POST_SUSPEND:
+           mutex_lock(&thermal_list_lock);
            atomic_set(&in_suspend, 0);

It is not clear to me why the above statement needs to be under the lock.

Quoted 12 lines
Quoted 3 lines

drivers/thermal/thermal_core.c static int thermal_pm_notify(struct notifier_block *nb, · line 1536

Quoted 3 lines
drivers/thermal/thermal_core.c +0 -0
            list_for_each_entry(tz, &thermal_tz_list, node) {
                    thermal_zone_device_init(tz);
                    thermal_zone_device_update(tz,

THERMAL_EVENT_UNSP
EC

Quoted 6 lines
Quoted 6 lines
drivers/thermal/thermal_core.c +1 -0
 IFIED);
            }
+           mutex_unlock(&thermal_list_lock);
            break;
    default:
            break;
#6 lore raw

Re: [PATCH] thermal: Fix race condition in suspend/resume

On Thu, 2023-11-16 at 23:06 +0800, Bo Ye wrote:
Hi Reviewers,

Please help review this change, or provide your suggestion. Thanks a
lot

Quoted 202 lines

Hi Rafael sir,

Could you help review this patch, thanks a lot.

Best Regards
Bo Ye

Quoted 194 lines · Bo Ye
Quoted 7 lines · Rafael J. Wysocki

External email : Please do not click links or open attachments until
you have verified the sender or the content.

Quoted 1 line · Bo Ye (叶波)
Quoted 5 lines
Quoted 1 line

Yes, it is observed issue.

It does happen, so it's not just "potential" and the subject of the
patch is slightly misleading. Please adjust it.

Done

Quoted 80 lines
Quoted 4 lines

Firstly, it needs to be clarified that this issue occurs in a real-
world environment. By analyzing the logs, we inferred that the

issue

Quoted 2 lines

occurred just as the system was entering suspend mode, and the user

was

Quoted 2 lines

switching the thermal policy (this action causes all thermal zones

to

Quoted 2 lines

unregister/register). In addition, we conducted degradation tests

and

Quoted 2 lines

also reproduced this issue. The specific method is to first switch

the

Quoted 1 line

thermal policy through a command, and then immediately put the

system

Quoted 3 lines

into suspend state through another command. This method can also
reproduce the issue.

OK, so please add this information to the patch changelog.

Quoted 9 lines · Bo Ye (叶波)
Quoted 8 lines · Bo Ye

Correct mail title format: remove "Subject:" from mail title.

Quoted 4 lines

From: "yugang.wang" <[email protected]>

Body:
This patch fixes a race condition during system resume. It

occurs

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

if
the system is exiting a suspend state and a user is trying to
register/unregister a thermal zone concurrently. The root cause

is

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines

that both actions access the thermal_tz_list.

In detail:

1. At PM_POST_SUSPEND during the resume, the system reads all
thermal
zones in thermal_tz_list, then resets and updates
their
temperatures.
2. When registering/unregistering a thermal zone, the thermal_tz_list gets manipulated.

These two actions might occur concurrently, causing a race condition.
To solve this issue, we introduce a mutex lock to protect thermal_tz_list from being modified while it's being read and
updated during the resume from suspend.

Kernel oops excerpt related to this fix:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
[ 5201.869845] [T316822] pc: [0xffffffeb7d4876f0]
mutex_lock+0x34/0x170
[ 5201.869856] [T316822] lr: [0xffffffeb7ca98a84]
thermal_pm_notify+0xd4/0x26c
[... cut for brevity ...]
[ 5201.871061] [T316822]  suspend_prepare+0x150/0x470
[ 5201.871067] [T316822]  enter_state+0x84/0x6f4
[ 5201.871076] [T316822]  state_store+0x15c/0x1e8
Quoted 4 lines

Well, the connection between the above log snippet and the issue addressed by the patch is rather hard to establish. Please include
more of the oops information.

Thank you very much for reviewing the additional explanations.

1.Enable thermal policy operation will unregister/register all thermal
zones
10-21 06:13:59.280 854 922 I libMtcLoader: enable thermal
policy
thermal_policy_09.

2.System suspend entry time is 2023-10-20 22:13:59.242

[ 4106.364175][T609387] binder:534_2: [name:spm&][SPM] PM: suspend
entry 2023-10-20 22:13:59.242898243 UTC
[ 4106.366185][T609387] binder:534_2: PM: [name:wakeup&]PM: Pending

Wakeup Sources: NETLINK

3. It can be proven that the absence of a switch strategy will perform
unregister/register operations on thermal zones (android time is 2023-
10-20 22:13:59.282),
Because the logs for other thermal zones switching are not enabled by
default, we cannot see the logs related to other thermal zones.

[ 4106.404167][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_unbind unbinding OK
[ 4106.404215][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_unbind unbinding OK
[ 4106.404225][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_unbind unbinding OK
[ 4106.404504][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_bind binding OK, 0
[ 4106.404545][T600922] mtkPowerMsgHdl:
[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_bind binding OK, 2
[ 4106.404566][T600922] mtkPowerMsgHdl:

[name:thermal_monitor&][Thermal/TZ/CPU]tscpu_bind binding OK, 1

4. thermal_pm_notify trigger KE(android time: 2023-10-20 22:13:59. 315894)

[ 4106.437171][T209387] binder:534_2: [name:mrdump&]Kernel Offset:
0x289cc80000 from 0xffffffc008000000
[ 4106.437182][T209387] binder:534_2: [name:mrdump&]PHYS_OFFSET:
0x40000000
[ 4106.437191][T209387] binder:534_2: [name:mrdump&]pstate:
80400005
(Nzcv daif +PAN -UAO)
[ 4106.437204][T209387] binder:534_2: [name:mrdump&]pc :
[0xffffffe8a6688200] mutex_lock+0x34/0x184
[ 4106.437214][T209387] binder:534_2: [name:mrdump&]lr :
[0xffffffe8a5ce66bc] thermal_pm_notify+0xd4/0x26c
[ 4106.437220][T209387] binder:534_2: [name:mrdump&]sp :
ffffffc01bab3ae0
[ 4106.437226][T209387] binder:534_2: [name:mrdump&]x29:

ffffffc01bab3af0 x28: 0000000000000001

Quoted 39 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
---
 drivers/thermal/thermal_core.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
drivers/thermal/thermal_core.c +0 -1
index 8717a3343512..a7a18ed57b6d 100644
--- a/drivers/thermal/thermal_core.c
Quoted 8 lines

drivers/thermal/thermal_core.c static int thermal_pm_notify(struct · line 1534

Quoted 8 lines

+++ b/drivers/thermal/thermal_core.c

drivers/thermal/thermal_core.c +1 -0
@@ -1529,12 +1529,14 @@ static int thermal_pm_notify(struct
 notifier_block *nb,
    case PM_POST_HIBERNATION:
    case PM_POST_RESTORE:
    case PM_POST_SUSPEND:
+           mutex_lock(&thermal_list_lock);
            atomic_set(&in_suspend, 0);

It is not clear to me why the above statement needs to be under the
lock.

Quoted 14 lines
Quoted 4 lines

drivers/thermal/thermal_core.c static int thermal_pm_notify(struct notifier_block *nb, · line 1536

Quoted 4 lines
drivers/thermal/thermal_core.c +0 -0
            list_for_each_entry(tz, &thermal_tz_list, node)
 {
                    thermal_zone_device_init(tz);
                    thermal_zone_device_update(tz,

THERMAL_EVENT_UN
SP
EC

Quoted 6 lines
Quoted 6 lines
drivers/thermal/thermal_core.c +1 -0
 IFIED);
            }
+           mutex_unlock(&thermal_list_lock);
            break;
    default:
            break;
#7 lore raw
In reply to Bo Ye
Quoted 7 lines · Bo Ye

From: "yugang.wang" <[email protected]>

Body:
This patch fixes a race condition during system resume. It occurs if the system is exiting a suspend state and a user is trying to register/unregister a thermal zone concurrently. The root cause is that both actions access the thermal_tz_list.

I'm not sure the tasks are already thawed during POST_RESTORE, so no user can unload a driver and then reaching the race window.

Is that an observed issue?

Quoted 48 lines

In detail:

1. At PM_POST_SUSPEND during the resume, the system reads all thermal zones in thermal_tz_list, then resets and updates their temperatures.
2. When registering/unregistering a thermal zone, the thermal_tz_list gets manipulated.

These two actions might occur concurrently, causing a race condition. To solve this issue, we introduce a mutex lock to protect thermal_tz_list from being modified while it's being read and updated during the resume from suspend.

Kernel oops excerpt related to this fix:

[ 5201.869845] [T316822] pc: [0xffffffeb7d4876f0] mutex_lock+0x34/0x170
[ 5201.869856] [T316822] lr: [0xffffffeb7ca98a84] thermal_pm_notify+0xd4/0x26c
[... cut for brevity ...]
[ 5201.871061] [T316822]  suspend_prepare+0x150/0x470
[ 5201.871067] [T316822]  enter_state+0x84/0x6f4
[ 5201.871076] [T316822]  state_store+0x15c/0x1e8
Change-Id Ifdbdecba17093f91eab7e36ce04b46d311ca6568
---
  drivers/thermal/thermal_core.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/thermal/thermal_core.c +2 -0
index 8717a3343512..a7a18ed57b6d 100644
@@ -1529,12 +1529,14 @@ static int thermal_pm_notify(struct notifier_block *nb,
  	case PM_POST_HIBERNATION:
  	case PM_POST_RESTORE:
  	case PM_POST_SUSPEND:
+		mutex_lock(&thermal_list_lock);
  		atomic_set(&in_suspend, 0);
  		list_for_each_entry(tz, &thermal_tz_list, node) {
  			thermal_zone_device_init(tz);
  			thermal_zone_device_update(tz,
  						   THERMAL_EVENT_UNSPECIFIED);
  		}
+		mutex_unlock(&thermal_list_lock);
  		break;
  	default:
  		break;
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#8 lore raw
Quoted 4 lines · Daniel Lezcano
Quoted 2 lines · Bo Ye

From: "yugang.wang" <[email protected]>

Quoted 8 lines
Quoted 5 lines

Body:
This patch fixes a race condition during system resume. It occurs if the system is exiting a suspend state and a user is trying to register/unregister a thermal zone concurrently. The root cause is that both actions access the thermal_tz_list.

I'm not sure the tasks are already thawed during POST_RESTORE, so no user can unload a driver and then reaching the race window.

Yes, they are.

Quoted 1 line

Is that an observed issue?

Good question, but the patch looks correct to me.

Quoted 50 lines
Quoted 38 lines

In detail:

1. At PM_POST_SUSPEND during the resume, the system reads all thermal zones in thermal_tz_list, then resets and updates their temperatures.
2. When registering/unregistering a thermal zone, the thermal_tz_list gets manipulated.

These two actions might occur concurrently, causing a race condition. To solve this issue, we introduce a mutex lock to protect thermal_tz_list from being modified while it's being read and updated during the resume from suspend.

Kernel oops excerpt related to this fix:

[ 5201.869845] [T316822] pc: [0xffffffeb7d4876f0] mutex_lock+0x34/0x170
[ 5201.869856] [T316822] lr: [0xffffffeb7ca98a84] thermal_pm_notify+0xd4/0x26c
[... cut for brevity ...]
[ 5201.871061] [T316822]  suspend_prepare+0x150/0x470
[ 5201.871067] [T316822]  enter_state+0x84/0x6f4
[ 5201.871076] [T316822]  state_store+0x15c/0x1e8
Change-Id Ifdbdecba17093f91eab7e36ce04b46d311ca6568
---
  drivers/thermal/thermal_core.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/thermal/thermal_core.c +1 -0
index 8717a3343512..a7a18ed57b6d 100644
@@ -1529,12 +1529,14 @@ static int thermal_pm_notify(struct notifier_block *nb,
      case PM_POST_HIBERNATION:
      case PM_POST_RESTORE:
      case PM_POST_SUSPEND:
+             mutex_lock(&thermal_list_lock);

drivers/thermal/thermal_core.c static int thermal_pm_notify(struct notifier_block *nb, · line 1542

Quoted 10 lines
drivers/thermal/thermal_core.c +1 -0
              atomic_set(&in_suspend, 0);
              list_for_each_entry(tz, &thermal_tz_list, node) {
                      thermal_zone_device_init(tz);
                      thermal_zone_device_update(tz,
                                                 THERMAL_EVENT_UNSPECIFIED);
              }
+             mutex_unlock(&thermal_list_lock);
              break;
      default:
              break;

--