Skip to contentSkip to conversation

Thermal: do thermal zone update after a cooling device registered

PATCH Superseded Chen Yu 2 participants 12 messages Sep 27, 2015 – Oct 20, 2015

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Chen Yu

author
#0 lore raw

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
index c3bdb48..09c78a4 100644
@@ -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
#1 lore raw
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

---
 drivers/thermal/thermal_core.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/thermal/thermal_core.c +1 -0
index c3bdb48..09c78a4 100644
@@ -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

author
#2 lore raw

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->lock here, to make sure that no thermal zone is added or removed from cdev->thermal_instances while 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 use list_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

#3 lore raw

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 registered

Quoted 3 lines · Chen Yu

From: Zhang Rui <[email protected]>

Quoted 5 lines
Quoted 3 lines

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.

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 use list_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 because bind_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

author
#4 lore raw

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 registered

Hi 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 registered

Quoted 3 lines · Chen Yu

From: Zhang Rui <[email protected]>

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.

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_instances atomically 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 use list_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 because bind_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

#5 lore raw
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 registered

Hi 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 registered

Quoted 3 lines · Chen Yu

From: Zhang Rui <[email protected]>

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.

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_instances atomically 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.

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

author
#6 lore raw

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 registered

Quoted 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 registered

Hi 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 registered

Quoted 3 lines · Chen Yu

From: Zhang Rui <[email protected]>

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.

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_instances atomically 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

Quoted 8 lines

thermal_list_lock instead of cdev ->lock?

Quoted 1 line

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.

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_device
mutex_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

author
#7 lore raw
In reply to Javi Merino

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 registered

Quoted 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 registered

Hi 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 registered

Quoted 3 lines · Chen Yu

From: Zhang Rui <[email protected]>

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.

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_instances atomically 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

Quoted 8 lines

thermal_list_lock instead of cdev ->lock?

Quoted 1 line

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.

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_device
mutex_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

#8 lore raw
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 registered

Quoted 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 registered

Hi 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 registered

Quoted 3 lines · Chen Yu

From: Zhang Rui <[email protected]>

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.

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_instances atomically 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

Quoted 8 lines

thermal_list_lock instead of cdev ->lock?

Quoted 1 line

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.

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_device
mutex_unlock(&thermal_list_lock);
}

Quoted 3 lines

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?

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

author
#9 lore raw

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 registered

Quoted 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 registered

Quoted 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 registered

Hi 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 registered

Quoted 3 lines · Chen Yu

From: Zhang Rui <[email protected]>

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.

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_instances atomically 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_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?

Quoted 1 line

This guy should be big enough to protect the device.thermal_instance

list.

Quoted 16 lines
Quoted 6 lines

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.

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_device
mutex_unlock(&thermal_list_lock);
}

Quoted 3 lines

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?

Quoted 9 lines

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.

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

author
#10 lore raw
In reply to Javi Merino

(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 registered

Quoted 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 registered

Quoted 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 registered

Hi 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 registered

Quoted 3 lines · Chen Yu

From: Zhang Rui <[email protected]>

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.

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_instances atomically 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_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?

Quoted 2 lines

This guy should be big enough to protect the
device.thermal_instance

list.

Quoted 16 lines
Quoted 6 lines

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.

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_device
mutex_unlock(&thermal_list_lock);
}

Quoted 3 lines

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?

Quoted 9 lines

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.

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

#11 lore raw

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 registered

Quoted 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 registered

Quoted 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 registered

Hi 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 registered

Quoted 3 lines · Chen Yu

From: Zhang Rui <[email protected]>

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.

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_instances atomically 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_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?

Quoted 2 lines

This guy should be big enough to protect the
device.thermal_instance

list.

Quoted 16 lines
Quoted 6 lines

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.

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_device
mutex_unlock(&thermal_list_lock);
}

Quoted 3 lines

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?

Quoted 9 lines

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.

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:

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_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.

Yeah, we can fix that in another series.

Cheers,
Javi