Skip to contentSkip to conversation

Simplifying hwmon drivers locking

lm-sensors Jean Delvare 2 participants 3 messages Dec 15, 2006 – Dec 21, 2006

lore
Share & tools
lore
#0 lore raw

Hi all,

Many of our hardware monitoring drivers use two different mutexes for each device. The second mutex was introduced for ISA-based devices which use only 2 I/O ports (e.g. LM78 or SiS5595), or for devices which use banked addressing internally (most Winbond and Asus devices). Since we added proper locking on "set" sysfs callbacks several months ago, it happens that in most cases, we end up holding both mutexes (lock and update_lock) almost always at the same time, so they are redundant. Having a single mutex would make the code more simple, and slightly faster. The new w83793 driver is using a single mutex, and I converted the f71805f and it87 drivers (see attachement.) The former was straightforward, the latter needed only one adjustment.

I guess that most other drivers using two mutexes could be converted the same way. The list of candidates follows:
drivers/hwmon/asb100.c
drivers/hwmon/lm78.c
drivers/hwmon/pc87360.c
drivers/hwmon/sis5595.c
drivers/hwmon/smsc47b397.c
drivers/hwmon/smsc47m1.c
drivers/hwmon/w83627ehf.c
drivers/hwmon/w83627hf.c
drivers/hwmon/w83781d.c

To convert a driver, you need to make sure that all non-init register accesses are done with update_lock held, and adjust the code otherwise (assuming there are only a few adjustments needed, else it's just not worth it.) Then lock can simply be dropped. Contributions are welcome, especially if you are a regular user of the driver. It's an easy task, just use the provided patch as an example.

Thanks,

Signature · 5 lines
Jean Delvare
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: hwmon-simplify-locking-1.patch
Url: http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20061215/acfaa412/attachment.pl 
#1 lore raw

Hi Jean:

One comment about this...

Quoted 40 lines · Jean Delvare

Many hardware monitoring drivers use two different mutexes, one to protect their per-device data structure, and one to protect the access to the device registers. These mutexes are essentially redundant, as the drivers are transfering values between the device registers and the data cache, so it almost always ends up holding both mutexes at the same time. Using a single mutex will make the mode more simple and faster.

Signed-off-by Jean Delvare <khali at linux-fr.org>
---
 drivers/hwmon/f71805f.c |   22 ++++++----------------
 drivers/hwmon/it87.c    |   22 +++++-----------------
 2 files changed, 11 insertions(+), 33 deletions(-)
drivers/hwmon/f71805f.c +2 -8
@@ -146,7 +146,6 @@
 struct f71805f_data {
 	unsigned short addr;
 	const char *name;
-	struct mutex lock;
 	struct class_device *class_dev;
 
 	struct mutex update_lock;
@@ -271,50 +270,42 @@
  * Device I/O access
  */
 
+/* Must be called with data->update_lock held, except during initialization */
 static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
 {
-	u8 val;
-
-	mutex_lock(&data->lock);
 	outb(reg, data->addr + ADDR_REG_OFFSET);
-	val = inb(data->addr + DATA_REG_OFFSET);
-	mutex_unlock(&data->lock);
-
-	return val;
+	return inb(data->addr + DATA_REG_OFFSET);

drivers/hwmon/f71805f.c line 285

Quoted 10 lines
drivers/hwmon/f71805f.c +1 -2
 }
 
+/* Must be called with data->update_lock held, except during initialization */
 static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
 {
-	mutex_lock(&data->lock);
 	outb(reg, data->addr + ADDR_REG_OFFSET);
 	outb(val, data->addr + DATA_REG_OFFSET);
-	mutex_unlock(&data->lock);
 }

Better than just a comment, let's add WARN_ON(!mutex_is_locked(&data->lock)) at the front of the read/write routines. Overhead should be negligible.

Regards,

Signature · 2 lines
Mark M. Hoffman
mhoffman at lightlink.com
#2 lore raw
In reply to Jean Delvare

Hi Mark,

Quoted 47 lines · Mark M. Hoffman

Hi Jean:

One comment about this...

Quoted 42 lines · Jean Delvare

Many hardware monitoring drivers use two different mutexes, one to protect their per-device data structure, and one to protect the access to the device registers. These mutexes are essentially redundant, as the drivers are transfering values between the device registers and the data cache, so it almost always ends up holding both mutexes at the same time. Using a single mutex will make the mode more simple and faster.

Signed-off-by Jean Delvare <khali at linux-fr.org>
---
 drivers/hwmon/f71805f.c |   22 ++++++----------------
 drivers/hwmon/it87.c    |   22 +++++-----------------
 2 files changed, 11 insertions(+), 33 deletions(-)
drivers/hwmon/f71805f.c +2 -8
@@ -146,7 +146,6 @@
 struct f71805f_data {
 	unsigned short addr;
 	const char *name;
-	struct mutex lock;
 	struct class_device *class_dev;
 
 	struct mutex update_lock;
@@ -271,50 +270,42 @@
  * Device I/O access
  */
 
+/* Must be called with data->update_lock held, except during initialization */
 static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
 {
-	u8 val;
-
-	mutex_lock(&data->lock);
 	outb(reg, data->addr + ADDR_REG_OFFSET);
-	val = inb(data->addr + DATA_REG_OFFSET);
-	mutex_unlock(&data->lock);
-
-	return val;
+	return inb(data->addr + DATA_REG_OFFSET);
 }
 
Quoted 11 lines

drivers/hwmon/f71805f.c line 285

Quoted 8 lines
drivers/hwmon/f71805f.c +1 -2
+/* Must be called with data->update_lock held, except during initialization */
 static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
 {
-	mutex_lock(&data->lock);
 	outb(reg, data->addr + ADDR_REG_OFFSET);
 	outb(val, data->addr + DATA_REG_OFFSET);
-	mutex_unlock(&data->lock);
 }

Better than just a comment, let's add WARN_ON(!mutex_is_locked(&data->lock)) at the front of the read/write routines. Overhead should be negligible.

This increases the .text size of driver f71805f by 4.5%, which is more than what my patch saves (2.8%). Not sure the overhead is really negligible either, checking a mutex is not significantly different from taking it, is it? The read functions can be called relatively frequently, the whole idea was to lower the overhead on register access, if we need to increase it again, it's void.

Additionally, as the comment says, calling without data->update_lock held is OK during initialization, so the WARN_ON() would trigger here. Unless we change the initialization path to hold the lock just for validation purpose.

One might argue that performance isn't an issue here and it's more important to make sure we get the locking right - but in that case I'd rather stick to the code we have at the moment (unless someone demonstrates that WARN_ON(!mutex_is_locked()) has actually a lower overhead than taking and releasing a mutex.)

As far as I am concerned I feel safe with the single-mutex version and no additional check, because both mutexes were redundant almost by construction. I only had to move one register access (out of 102) in these two drivers to get it right. But if you don't feel safe with that, I respect that and I'll leave the other drivers untouched.

Thanks,

Signature · 1 line
Jean Delvare