Thermal: do thermal zone update after a cooling device registered
Subsystems (2)
PATCH 3/3 Thermal: do thermal zone update after a cooling device registered
From: Zhang Rui <[email protected]>
When a new cooling device is registered, we need to update the thermal zone to set the new registered cooling device to a proper state.
This fixes a problem that the system is cool, while the fan devices are left running on full speed after boot, if fan device is registered after thermal zone device.
CC: <[email protected]> #3.18+ Reference:https://bugzilla.kernel.org/show_bug.cgi?id=92431
--- drivers/thermal/thermal_core.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/thermal/thermal_core.c +10 -0
@@ -1450,6 +1450,7 @@ __thermal_cooling_device_register(struct device_node *np, const struct thermal_cooling_device_ops *ops) {struct thermal_cooling_device *cdev; +struct thermal_instance *pos, *next;int result;if (type &&strlen (type) >=THERMAL_NAME_LENGTH )
@@ -1494,6 +1495,15 @@ __thermal_cooling_device_register(struct device_node *np,/* Update binding information for 'this' new cdev */ bind_cdev (cdev); +list_for_each_entry_safe (pos, next, &cdev->thermal_instances, cdev_node) { +if (next->cdev_node.next == &cdev->thermal_instances) { +thermal_zone_device_update (next->tz); +break ; + } +if (pos->tz != next->tz) +thermal_zone_device_update (pos->tz); + } +return cdev; }
Signature · 1 line
1.8.4.2
Quoted 35 lines · Chen Yu
From: Zhang Rui <[email protected]>
When a new cooling device is registered, we need to update the thermal zone to set the new registered cooling device to a proper state.
This fixes a problem that the system is cool, while the fan devices are left running on full speed after boot, if fan device is registered after thermal zone device.
CC: <[email protected]> #3.18+ Reference:https://bugzilla.kernel.org/show_bug.cgi?id=92431
Tested-by Manuel Krause <[email protected]>Tested-by szegad <[email protected]>Tested-by prash <[email protected]>Tested-by amish <[email protected]>Signed-off-by Zhang Rui <[email protected]>Signed-off-by Chen Yu <[email protected]>--- drivers/thermal/thermal_core.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/thermal/thermal_core.c +1
-0@@ -1450,6 +1450,7 @@ __thermal_cooling_device_register(struct device_node *np, const struct thermal_cooling_device_ops *ops) {struct thermal_cooling_device *cdev; +struct thermal_instance *pos, *next;int result;if (type &&strlen (type) >=THERMAL_NAME_LENGTH )
drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 1497
Quoted 3 lines
drivers/thermal/thermal_core.c +0
-0@@ -1494,6 +1495,15 @@ __thermal_cooling_device_register(struct device_node *np,/* Update binding information for 'this' new cdev */ bind_cdev (cdev);
I think you need to hold cdev->lock here, to make sure that no thermal zone is added or removed from cdev->thermal_instances while you are looping.
drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 1498
Quoted 1 line
drivers/thermal/thermal_core.c +1
-0+list_for_each_entry_safe (pos, next, &cdev->thermal_instances, cdev_node) {
Why list_for_each_entry_safe() ? You are not going to remove any entry, so you can just use list_for_each_entry()
drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct device_node *np, · line 1505
Quoted 7 lines
drivers/thermal/thermal_core.c +7
-0+if (next->cdev_node.next == &cdev->thermal_instances) { +thermal_zone_device_update (next->tz); +break ; + } +if (pos->tz != next->tz) +thermal_zone_device_update (pos->tz); + }
Why is this so complicated? Can't you just do:
list_for_each_entry(pos, &cdev->thermal_instances, cdev_node)
thermal_zone_device_update(pos->tz);Cheers,
Javi
Hi, Javi,
Quoted 9 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Monday, September 28, 2015 10:29 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registered
Quoted 8 lines
Quoted 3 lines · Chen Yu
From: Zhang Rui <[email protected]>
I think you need to hold
cdev->lockhere, to make sure that no thermal zone is added or removed fromcdev->thermal_instanceswhile you are looping.
Ah right, will add. If I add the cdev ->lock here, will there be a AB-BA lock with thermal_zone_unbind_cooling_device?
Quoted 10 lines
Why
list_for_each_entry_safe()? You are not going to remove any entry, so you can just uselist_for_each_entry()Why is this so complicated? Can't you just do:
list_for_each_entry(pos, &cdev->thermal_instances, cdev_node) thermal_zone_device_update(pos->tz);
This is an optimization here:
Ignore thermal instance that refers to the same thermal zone in this loop, this works because bind_cdev() always binds the cooling device to one thermal zone first, and then binds to the next thermal zone.
Best Regards,
Yu
Hi Yu,
Quoted 16 lines · Yu C
Hi, Javi,
Quoted 14 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Monday, September 28, 2015 10:29 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 3 lines · Chen Yu
From: Zhang Rui <[email protected]>
Quoted 5 lines
Quoted 3 lines
I think you need to hold
cdev->lockhere, to make sure that no thermal zone is added or removed fromcdev->thermal_instanceswhile you are looping.Ah right, will add. If I add the cdev ->lock here, will there be a AB-BA lock with
thermal_zone_unbind_cooling_device?
You're right, it could lead to a deadlock. The locks can't be swapped because that won't work in step_wise.
The best way that I can think of accessing thermal_instances atomically is by making it RCU protected instead of with mutexes. What do you think?
Quoted 6 lines
Quoted 6 lines
Why
list_for_each_entry_safe()? You are not going to remove any entry, so you can just uselist_for_each_entry()Why is this so complicated? Can't you just do:
Quoted 8 lines
Quoted 3 lines
list_for_each_entry(pos, &cdev->thermal_instances, cdev_node) thermal_zone_device_update(pos->tz);This is an optimization here:
Ignore thermal instance that refers to the same thermal zone in this loop, this works becausebind_cdev()always binds the cooling device to one thermal zone first, and then binds to the next thermal zone.
It has taken me a while to understand this optimization. Please document both "if"s in the code. For the first "if" maybe you can use list_is_last() to make it easier to understand that you're looking for the last element in the list:
if (list_is_last(&pos->cdev_node, &cdev->thermal_instances)) {
thermal_zone_device_update(pos->tz);For the second "if" you can say that you only need to run thermal_zone_device_update() once per thermal zone, even though multiple thermal instances may refer to the same thermal zone.
Cheers,
Javi
Hi, Javi
Sorry for my late response,
Quoted 35 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Wednesday, September 30, 2015 12:02 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredHi Yu,
Quoted 18 lines · Yu C
Hi, Javi,
Quoted 16 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Monday, September 28, 2015 10:29 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 3 lines · Chen Yu
From: Zhang Rui <[email protected]>
I think you need to hold
cdev->lockhere, to make sure that no thermal zone is added or removed fromcdev->thermal_instanceswhileyou are looping.
Quoted 3 lines
Quoted 1 line
Ah right, will add. If I add the cdev ->lock here, will there be a AB-BA lock with
thermal_zone_unbind_cooling_device?
Quoted 7 lines
You're right, it could lead to a deadlock. The locks can't be swapped because that won't work in
step_wise.The best way that I can think of accessing
thermal_instancesatomically is by making it RCU protected instead of with mutexes.
What do you think?
RCU would need extra spinlocks to protect the list, and need to sync_rcu after we delete one instance from thermal_instance list, I think it is too complicated for me to rewrite: ( How about using thermal_list_lock instead of cdev ->lock?
This guy should be big enough to protect the device.thermal_instance list.
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
Why
list_for_each_entry_safe()? You are not going to remove any entry, so you can just uselist_for_each_entry()Why is this so complicated? Can't you just do:
Quoted 8 lines
Quoted 3 lines
list_for_each_entry(pos, &cdev->thermal_instances, cdev_node) thermal_zone_device_update(pos->tz);This is an optimization here:
Ignore thermal instance that refers to the same thermal zone in this loop, this works becausebind_cdev()always binds the cooling device to one thermal zone first, and then binds to the next thermal zone.
Quoted 9 lines
It has taken me a while to understand this optimization. Please document both "if"s in the code. For the first "if" maybe you can use
list_is_last()to make it easier to understand that you're looking for the last element in the list:if (list_is_last(&pos->cdev_node, &cdev-Quoted 1 line
thermal_instances)) {thermal_zone_device_update(pos->tz);
Sure, ok
Quoted 4 lines
For the second "if" you can say that you only need to run
thermal_zone_device_update()once per thermal zone, even though multiple thermal instances may refer to the same thermal zone.
OK
Best Regards,
Yu
Quoted 41 lines · Yu C
Hi, Javi
Sorry for my late response,Quoted 38 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Wednesday, September 30, 2015 12:02 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredHi Yu,
Quoted 18 lines · Yu C
Hi, Javi,
Quoted 16 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Monday, September 28, 2015 10:29 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 3 lines · Chen Yu
From: Zhang Rui <[email protected]>
I think you need to hold
cdev->lockhere, to make sure that no thermal zone is added or removed fromcdev->thermal_instanceswhileyou are looping.
Quoted 3 lines
Quoted 1 line
Ah right, will add. If I add the cdev ->lock here, will there be a AB-BA lock with
thermal_zone_unbind_cooling_device?You're right, it could lead to a deadlock. The locks can't be swapped because that won't work in
step_wise.
Quoted 8 lines
Quoted 4 lines
The best way that I can think of accessing
thermal_instancesatomically is by making it RCU protected instead of with mutexes.
What do you think?RCU would need extra spinlocks to protect the list, and need to
sync_rcuafter we delete one instance fromthermal_instancelist, I think it is too complicated for me to rewrite: ( How about usingthermal_list_lockinstead of cdev ->lock?
This guy should be big enough to protect the device.thermal_instance list.
thermal_list_lock protects thermal_tz_list and thermal_cdev_list, but it doesn't protect the thermal_instances list. For example, thermal_zone_bind_cooling_device() adds a cooling device to the cdev->thermal_instances list without taking thermal_tz_list.
To sum up, you have to protect accessing the cdev->thermal_instances list but with the current locking scheme, you would create an AB-BA deadlock. As I see it you would have to change the locking scheme to either RCU or add a new mutex that protects the cdev->thermal_instances and tz->thermal_instances lists and change all accesses to them to make sure they comply with the new locking scheme.
Is there a better way of solving this? Cheers,
Javi
Hi Javi,
Quoted 59 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Thursday, October 15, 2015 1:08 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]; Pandruvada, Srinivas
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 42 lines · Yu C
Hi, Javi
Sorry for my late response,Quoted 39 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Wednesday, September 30, 2015 12:02 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredHi Yu,
Quoted 19 lines · Yu C
Hi, Javi,
Quoted 17 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Monday, September 28, 2015 10:29 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 3 lines · Chen Yu
From: Zhang Rui <[email protected]>
I think you need to hold
cdev->lockhere, to make sure that no thermal zone is added or removed fromcdev->thermal_instanceswhileyou are looping.
Quoted 3 lines
Quoted 1 line
Ah right, will add. If I add the cdev ->lock here, will there be a AB-BA lock with
thermal_zone_unbind_cooling_device?You're right, it could lead to a deadlock. The locks can't be swapped because that won't work in
step_wise.Quoted 7 lines
Quoted 4 lines
The best way that I can think of accessing
thermal_instancesatomically is by making it RCU protected instead of with mutexes. What do you think?RCU would need extra spinlocks to protect the list, and need to
sync_rcuafter we delete one instance fromthermal_instancelist, I think it is too complicated for me to rewrite: ( How about using
Quoted 8 lines
thermal_list_lockinstead of cdev ->lock?Quoted 1 line
This guy should be big enough to protect the device.thermal_instance list.
thermal_list_lockprotectsthermal_tz_listandthermal_cdev_list, but it doesn't protect thethermal_instanceslist. For example,thermal_zone_bind_cooling_device()adds a cooling device to thecdev->thermal_instanceslist without takingthermal_tz_list.
Before thermal_zone_bind_cooling_device is invoked, the thermal_list_lock will be firstly gripped:
static void bind_cdev(struct thermal_cooling_device *cdev) {
mutex_lock(&thermal_list_lock);
either tz->ops->bind : thermal_zone_bind_cooling_device
or __bind() : thermal_zone_bind_cooling_devicemutex_unlock(&thermal_list_lock);
}
And it is the same as in passive_store.
So when code is trying to add/delete thermal_instance of cdev, he has already hold thermal_list_lock IMO. Or do I miss anything?
Best Regards,
Yu
Hi,Javi
Quoted 59 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Thursday, October 15, 2015 1:08 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]; Pandruvada, Srinivas
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 42 lines · Yu C
Hi, Javi
Sorry for my late response,Quoted 39 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Wednesday, September 30, 2015 12:02 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredHi Yu,
Quoted 19 lines · Yu C
Hi, Javi,
Quoted 17 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Monday, September 28, 2015 10:29 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 3 lines · Chen Yu
From: Zhang Rui <[email protected]>
I think you need to hold
cdev->lockhere, to make sure that no thermal zone is added or removed fromcdev->thermal_instanceswhileyou are looping.
Quoted 3 lines
Quoted 1 line
Ah right, will add. If I add the cdev ->lock here, will there be a AB-BA lock with
thermal_zone_unbind_cooling_device?You're right, it could lead to a deadlock. The locks can't be swapped because that won't work in
step_wise.Quoted 7 lines
Quoted 4 lines
The best way that I can think of accessing
thermal_instancesatomically is by making it RCU protected instead of with mutexes. What do you think?RCU would need extra spinlocks to protect the list, and need to
sync_rcuafter we delete one instance fromthermal_instancelist, I think it is too complicated for me to rewrite: ( How about using
Quoted 8 lines
thermal_list_lockinstead of cdev ->lock?Quoted 1 line
This guy should be big enough to protect the device.thermal_instance list.
thermal_list_lockprotectsthermal_tz_listandthermal_cdev_list, but it doesn't protect thethermal_instanceslist. For example,thermal_zone_bind_cooling_device()adds a cooling device to thecdev->thermal_instanceslist without takingthermal_tz_list.
Before thermal_zone_bind_cooling_device is invoked, the thermal_list_lock will be firstly gripped:
static void bind_cdev(struct thermal_cooling_device *cdev) {mutex_lock(&thermal_list_lock);
either tz->ops->bind : thermal_zone_bind_cooling_device
or __bind() : thermal_zone_bind_cooling_devicemutex_unlock(&thermal_list_lock);
}
And it is the same as in passive_store.
So when code is trying to add/delete thermal_instance of cdev, he has already hold thermal_list_lock IMO. Or do I miss anything?
Best Regards,
Yu
Quoted 78 lines · Yu C
Quoted 59 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Thursday, October 15, 2015 1:08 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]; Pandruvada, Srinivas
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 42 lines · Yu C
Hi, Javi
Sorry for my late response,Quoted 39 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Wednesday, September 30, 2015 12:02 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredHi Yu,
Quoted 19 lines · Yu C
Hi, Javi,
Quoted 17 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Monday, September 28, 2015 10:29 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 3 lines · Chen Yu
From: Zhang Rui <[email protected]>
I think you need to hold
cdev->lockhere, to make sure that no thermal zone is added or removed fromcdev->thermal_instanceswhileyou are looping.
Quoted 3 lines
Quoted 1 line
Ah right, will add. If I add the cdev ->lock here, will there be a AB-BA lock with
thermal_zone_unbind_cooling_device?You're right, it could lead to a deadlock. The locks can't be swapped because that won't work in
step_wise.Quoted 7 lines
Quoted 4 lines
The best way that I can think of accessing
thermal_instancesatomically is by making it RCU protected instead of with mutexes. What do you think?RCU would need extra spinlocks to protect the list, and need to
sync_rcuafter we delete one instance fromthermal_instancelist, I think it is too complicated for me to rewrite: ( How about usingQuoted 8 lines
thermal_list_lockinstead of cdev ->lock?Quoted 1 line
This guy should be big enough to protect the device.thermal_instance list.
thermal_list_lockprotectsthermal_tz_listandthermal_cdev_list, but it doesn't protect thethermal_instanceslist. For example,thermal_zone_bind_cooling_device()adds a cooling device to thecdev->thermal_instanceslist without takingthermal_tz_list.Before
thermal_zone_bind_cooling_deviceis invoked, thethermal_list_lockwill be firstly gripped:static void
bind_cdev(struct thermal_cooling_device *cdev){mutex_lock(&thermal_list_lock);
eithertz->ops->bind:thermal_zone_bind_cooling_device
or__bind():thermal_zone_bind_cooling_devicemutex_unlock(&thermal_list_lock);
}
Quoted 3 lines
And it is the same as in
passive_store.
So when code is trying to add/deletethermal_instanceof cdev, he has already holdthermal_list_lockIMO. Or do I miss anything?
thermal_zone_bind_cooling_device() is exported, so you can't really rely on the static thermal_list_lock being acquired in every single call.
thermal_list_lock and protects the lists thermal_tz_list and thermal_cdev_list. Making it implicitly protect the cooling device's and thermal zone device's instances list because no sensible code would call thermal_zone_bind_cooling_device() outside of a bind function is just asking for trouble.
Locking is hard to understand and easy to get wrong so let's keep it simple.
Cheers,
Javi
Hi,
Quoted 94 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Thursday, October 15, 2015 10:05 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]; Pandruvada, Srinivas
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 58 lines · Yu C
Quoted 58 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Thursday, October 15, 2015 1:08 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]; Pandruvada, Srinivas
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 47 lines · Yu C
Hi, Javi
Sorry for my late response,Quoted 44 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Wednesday, September 30, 2015 12:02 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredHi Yu,
Quoted 20 lines · Yu C
Hi, Javi,
Quoted 18 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Monday, September 28, 2015 10:29 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui;
linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 3 lines · Chen Yu
From: Zhang Rui <[email protected]>
I think you need to hold
cdev->lockhere, to make sure that no thermal zone is added or removed fromcdev->thermal_instanceswhileyou are looping.
Quoted 3 lines
Quoted 1 line
Ah right, will add. If I add the cdev ->lock here, will there be a AB-BA lock with
thermal_zone_unbind_cooling_device?You're right, it could lead to a deadlock. The locks can't be swapped because that won't work in
step_wise.The best way that I can think of accessing
thermal_instancesatomically is by making it RCU protected instead of with mutexes. What do you think?Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
RCU would need extra spinlocks to protect the list, and need to
sync_rcuafter we delete one instance fromthermal_instancelist, I think it is too complicated for me to rewrite: ( How about using
thermal_list_lockinstead of cdev ->lock?Quoted 1 line
This guy should be big enough to protect the device.thermal_instance
list.
Quoted 16 lines
Quoted 6 lines
thermal_list_lockprotectsthermal_tz_listandthermal_cdev_list, but it doesn't protect thethermal_instanceslist. For example,thermal_zone_bind_cooling_device()adds a cooling device to thecdev->thermal_instanceslist without takingthermal_tz_list.Before
thermal_zone_bind_cooling_deviceis invoked, thethermal_list_lockwill be firstly gripped:static void
bind_cdev(struct thermal_cooling_device *cdev){mutex_lock(&thermal_list_lock);
eithertz->ops->bind:thermal_zone_bind_cooling_device
or__bind():thermal_zone_bind_cooling_devicemutex_unlock(&thermal_list_lock);
}Quoted 3 lines
And it is the same as in
passive_store.
So when code is trying to add/deletethermal_instanceof cdev, he has already holdthermal_list_lockIMO. Or do I miss anything?
Quoted 9 lines
thermal_zone_bind_cooling_device()is exported, so you can't really rely on the staticthermal_list_lockbeing acquired in every single call.
thermal_list_lockand protects the liststhermal_tz_listandthermal_cdev_list. Making it implicitly protect the cooling device's and thermal zone device's instances list because no sensible code would callthermal_zone_bind_cooling_device()outside of a bind function is just asking for trouble.
Yes, from this point of view,it is true.
Quoted 2 lines
Locking is hard to understand and easy to get wrong so let's keep it simple.
How about the following 2 methods:
1. avoid accessing device's thermal_instance,but
access all thermal_zone_device directly, although there might be some redundancy, some thermal zones do not need to be updated, but we can avoid gripping dev->lock:
mutex_lock(&thermal_list_lock); list_for_each_entry(pos, &thermal_tz_list, node) thermal_zone_device_update(tz); mutex_unlock(&thermal_list_lock);
or,
2. Once we bind the new device with the thermal_zone_device, we can record that thermal_zone_device, and update that thermal_zone_device alone.
BTW, since thermal_zone_device_update is not atomic, we might need another patch to make it into atomic or something like that, but for now, I think these three patches are just for fixing the regressions.
Thanks
Best Regards,
Yu
(resend for broken display)
Hi,
Quoted 96 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Thursday, October 15, 2015 10:05 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]; Pandruvada, Srinivas
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 58 lines · Yu C
Quoted 58 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Thursday, October 15, 2015 1:08 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]; Pandruvada, Srinivas
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 47 lines · Yu C
Hi, Javi
Sorry for my late response,Quoted 44 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Wednesday, September 30, 2015 12:02 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredHi Yu,
Quoted 20 lines · Yu C
Hi, Javi,
Quoted 18 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Monday, September 28, 2015 10:29 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui;
linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 3 lines · Chen Yu
From: Zhang Rui <[email protected]>
I think you need to hold
cdev->lockhere, to make sure that no thermal zone is added or removed fromcdev->thermal_instanceswhileyou are looping.
Quoted 3 lines
Quoted 1 line
Ah right, will add. If I add the cdev ->lock here, will there be a AB-BA lock with
thermal_zone_unbind_cooling_device?You're right, it could lead to a deadlock. The locks can't be swapped because that won't work in
step_wise.The best way that I can think of accessing
thermal_instancesatomically is by making it RCU protected instead of with mutexes. What do you think?Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
RCU would need extra spinlocks to protect the list, and need to
sync_rcuafter we delete one instance fromthermal_instancelist, I think it is too complicated for me to rewrite: ( How about using
thermal_list_lockinstead of cdev ->lock?Quoted 2 lines
This guy should be big enough to protect the
device.thermal_instancelist.
Quoted 16 lines
Quoted 6 lines
thermal_list_lockprotectsthermal_tz_listandthermal_cdev_list, but it doesn't protect thethermal_instanceslist. For example,thermal_zone_bind_cooling_device()adds a cooling device to thecdev->thermal_instanceslist without takingthermal_tz_list.Before
thermal_zone_bind_cooling_deviceis invoked, thethermal_list_lockwill be firstly gripped:static void
bind_cdev(struct thermal_cooling_device *cdev){mutex_lock(&thermal_list_lock);
eithertz->ops->bind:thermal_zone_bind_cooling_device
or__bind():thermal_zone_bind_cooling_devicemutex_unlock(&thermal_list_lock);
}Quoted 3 lines
And it is the same as in
passive_store.
So when code is trying to add/deletethermal_instanceof cdev, he has already holdthermal_list_lockIMO. Or do I miss anything?
Quoted 9 lines
thermal_zone_bind_cooling_device()is exported, so you can't really rely on the staticthermal_list_lockbeing acquired in every single call.
thermal_list_lockand protects the liststhermal_tz_listandthermal_cdev_list. Making it implicitly protect the cooling device's and thermal zone device's instances list because no sensible code would callthermal_zone_bind_cooling_device()outside of a bind function is just asking for trouble.
Yes, from this point of view,it is true.
Quoted 2 lines
Locking is hard to understand and easy to get wrong so let's keep it simple.
How about the following 2 methods:
1. avoid accessing device's thermal_instance,
but access all thermal_zone_device directly,
although there might be some redundancy,
some thermal zones do not need to be updated,
but we can avoid gripping dev->lock:
mutex_lock(&thermal_list_lock); list_for_each_entry(pos, &thermal_tz_list, node) thermal_zone_device_update(tz); mutex_unlock(&thermal_list_lock);
or,
2. Once we bind the new device with the thermal_zone_device, we can record that thermal_zone_device,
and update that thermal_zone_device alone,the the code would be:
mutex_lock(&thermal_list_lock);
list_for_each_entry(pos, &thermal_tz_list, node){
if (tz->need_update)
thermal_zone_device_update(tz);
}
mutex_unlock(&thermal_list_lock);BTW, since thermal_zone_device_update is not atomic, we might need another patch to make it into atomic or something like that, but for now, I think these three patches are just for fixing the regressions.
Thanks
Best Regards,
Yu
Hi Yu,
Quoted 126 lines · Yu C
Quoted 96 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Thursday, October 15, 2015 10:05 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]; Pandruvada, Srinivas
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 58 lines · Yu C
Quoted 58 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Thursday, October 15, 2015 1:08 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]; Pandruvada, Srinivas
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 47 lines · Yu C
Hi, Javi
Sorry for my late response,Quoted 44 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Wednesday, September 30, 2015 12:02 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui; linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredHi Yu,
Quoted 20 lines · Yu C
Hi, Javi,
Quoted 18 lines
-----Original Message-----
From: Javi Merino [mailto:[email protected]]
Sent: Monday, September 28, 2015 10:29 PM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Zhang, Rui;
linux- [email protected]; [email protected]
Subject: Re: [PATCH 3/3] Thermal: do thermal zone update after a cooling device registeredQuoted 3 lines · Chen Yu
From: Zhang Rui <[email protected]>
I think you need to hold
cdev->lockhere, to make sure that no thermal zone is added or removed fromcdev->thermal_instanceswhileyou are looping.
Quoted 3 lines
Quoted 1 line
Ah right, will add. If I add the cdev ->lock here, will there be a AB-BA lock with
thermal_zone_unbind_cooling_device?You're right, it could lead to a deadlock. The locks can't be swapped because that won't work in
step_wise.The best way that I can think of accessing
thermal_instancesatomically is by making it RCU protected instead of with mutexes. What do you think?Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
RCU would need extra spinlocks to protect the list, and need to
sync_rcuafter we delete one instance fromthermal_instancelist, I think it is too complicated for me to rewrite: ( How about using
thermal_list_lockinstead of cdev ->lock?Quoted 2 lines
This guy should be big enough to protect the
device.thermal_instancelist.
Quoted 16 lines
Quoted 6 lines
thermal_list_lockprotectsthermal_tz_listandthermal_cdev_list, but it doesn't protect thethermal_instanceslist. For example,thermal_zone_bind_cooling_device()adds a cooling device to thecdev->thermal_instanceslist without takingthermal_tz_list.Before
thermal_zone_bind_cooling_deviceis invoked, thethermal_list_lockwill be firstly gripped:static void
bind_cdev(struct thermal_cooling_device *cdev){mutex_lock(&thermal_list_lock);
eithertz->ops->bind:thermal_zone_bind_cooling_device
or__bind():thermal_zone_bind_cooling_devicemutex_unlock(&thermal_list_lock);
}Quoted 3 lines
And it is the same as in
passive_store.
So when code is trying to add/deletethermal_instanceof cdev, he has already holdthermal_list_lockIMO. Or do I miss anything?Quoted 9 lines
thermal_zone_bind_cooling_device()is exported, so you can't really rely on the staticthermal_list_lockbeing acquired in every single call.
thermal_list_lockand protects the liststhermal_tz_listandthermal_cdev_list. Making it implicitly protect the cooling device's and thermal zone device's instances list because no sensible code would callthermal_zone_bind_cooling_device()outside of a bind function is just asking for trouble.Yes, from this point of view,it is true.
Quoted 2 lines
Locking is hard to understand and easy to get wrong so let's keep it simple.
How about the following 2 methods:
1. avoid accessing device'sthermal_instance,
but access allthermal_zone_devicedirectly,
although there might be some redundancy,
some thermal zones do not need to be updated,
but we can avoid grippingdev->lock:mutex_lock(&thermal_list_lock); list_for_each_entry(pos, &thermal_tz_list, node) thermal_zone_device_update(tz); mutex_unlock(&thermal_list_lock);or,
2. Once we bind the new device with thethermal_zone_device, we can record thatthermal_zone_device,
and update thatthermal_zone_devicealone,the the code would be:
Quoted 6 lines
mutex_lock(&thermal_list_lock);
list_for_each_entry(pos, &thermal_tz_list, node){
if (tz->need_update)
thermal_zone_device_update(tz);
}
mutex_unlock(&thermal_list_lock);This sounds like a better alternative to me. I was thinking whether we could add the thremal_zone_device_update() directly in bind_cdev() to avoid the need_update field but I don't think it's any better: you would have to put it in two places (for the bind() and tbp.match() paths).
With the solution you propose above you only have to put it in __thermal_cooling_device_register(), which is simpler. I vote for your solution (2) above.
Quoted 4 lines
BTW, since
thermal_zone_device_updateis not atomic, we might need another patch to make it into atomic or something like that, but for now, I think these three patches are just for fixing the regressions.
Yeah, we can fix that in another series.
Cheers,
Javi