Skip to contentSkip to conversation

update_lock

DISCUSSION Jean Delvare 4 participants 14 messages May 19, 2005

lore
Share & tools
lore
#0 lore raw

Hi all,

I have a question about the locking mechanism we have in all our i2c client drivers, 2.4 and 2.6 as well.

The data->update_lock semaphore is used in the *_update_dev function, and only there. There are various other places all over the drivers where we access the same internal data, (in get callback functions) and even write to it (in store callback functions typically), and also access the i2c bus (in store functions too).

Why is it that we don't use the lock here?

What are we trying to protect exactly?

Thanks.

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#1 lore raw
Quoted 11 lines · Jean Delvare

Hi all,

I have a question about the locking mechanism we have in all our i2c client drivers, 2.4 and 2.6 as well.

The data->update_lock semaphore is used in the *_update_dev function, and only there. There are various other places all over the drivers where we access the same internal data, (in get callback functions) and even write to it (in store callback functions typically), and also access the i2c bus (in store functions too).

Quoted 3 lines

Why is it that we don't use the lock here?

What are we trying to protect exactly?

I remember trying to figure this out a while ago too, and didn't come up with an answer. I sent in some patches that I thought were correct, but others said no.

It would be good to figure this out.

thanks,

greg k-h

#2 lore raw
In reply to Jean Delvare
Quoted 5 lines · Jean Delvare
Quoted 3 lines

I remember trying to figure this out a while ago too, and didn't come up with an answer. I sent in some patches that I thought were correct, but others said no.

Anywhere we can take a look at these?

They are in the archives, sorry I don't remember the name of the thread :(

Quoted 1 line

Who were the others?

I think it was Mark, but again, don't really remember, sorry.

Too many patches, too little time...

thanks,

greg k-h

#3 lore raw
In reply to Jean Delvare
Quoted 3 lines

I remember trying to figure this out a while ago too, and didn't come up with an answer. I sent in some patches that I thought were correct, but others said no.

Anywhere we can take a look at these?

Who were the others?

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#4 lore raw
In reply to Jean Delvare
Quoted 4 lines
Quoted 1 line

Anywhere we can take a look at these?

They are in the archives, sorry I don't remember the name of the thread:(

I searched subjects for "lock" but couldn't find it. On the other hand, I found this:
http://archives.andrew.net.au/lm-sensors/msg03529.html

Same question, me again, 8 months ago. Which basically proves I have a very bad memory...

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#5 lore raw
In reply to Jean Delvare

According to Mark M. Hoffman:

First of all, thanks for pointing out the relevant thread. Interesting read, to say the least.

To answer a question raised there about the read_word capability:
There are two kinds of word reads if I understood correctly. The real one is implemented in the LM75 for example, where the regiesters are actually 16-bit wide. The second relies on the auto-increment ability of some chips, and will return the values of two consecutive 8-bit wide registers. This is quite different.

I wouldn't use read_word in the second case, because you never know wether you read the registers from the same hardware update. In various cases, the two registers that are supposed to assemble into a 9- to 16-bit value are not even adjacent. See what I did in the lm90 driver to workaround the problem (basically read register A, then B, then A again, if both A match the read is considered valid, else retry).

Anyway that's not necessarily on-topic.

Quoted 4 lines
Quoted 2 lines

The data->update_lock semaphore is used in the *_update_dev function, and only there.

Right, but that is not always sufficient.

So we agree.

Quoted 18 lines
Quoted 7 lines

and only there. There are various other places all over the drivers where we access the same internal data, (in get callback functions) and
even write to it (in store callback functions typically), and also access the i2c bus (in store functions too).

Why is it that we don't use the lock here?

It's a bug that's in all the drivers... e.g. from lm78.c:

254: static ssize_t set_in_min(struct device *dev, const char *buf,
255: size_t count, int nr)
256: {

257:         struct i2c_client *client = to_i2c_client(dev);
258:         struct lm78_data *data = i2c_get_clientdata(client);
259:         unsigned long val = simple_strtoul(buf, NULL, 10);
260:         data->in_min[nr] = IN_TO_REG(val);
261:         lm78_write_value(client, LM78_REG_IN_MIN(nr),
Quoted 6 lines

data->in_min[nr]);
262: return count;
263: }

Lines 260 & 261 need to be protected against a simultaneous lm78_update_device(), but they're obviously not.

Correct, I missed that possibility.

Quoted 5 lines
Quoted 1 line

What are we trying to protect exactly?

As they're written today, the update_lock only prevents multiple _update_device() calls from occurring at once. This is necessary, but not sufficient.

Agreed.

Quoted 8 lines

For the above case, you could do this:

259:         unsigned long val = simple_strtoul(buf, NULL, 10);
260:         /* deleted */
261:         lm78_write_value(client, LM78_REG_IN_MIN(nr),

IN_TO_REG(val));
261.5: data->valid = 0;
262: return count;

I don't like this much. This breaks our caching scheme. Basically, you invalidate all values just because of one value. Seems too high a price to pay, especially since you mostly *know* the missing value, you just wrote it. Why don't we just swap lines 260 and 261? I.e. basically don't trust data->in_min[nr] (because it can be changed by _update_device()) and use our own, local temporary variable (var). Write to the chip first, then update data->in_min[nr]. It could still be trashed by _update_device() after that, unless we also lock on writes. See below.

Quoted 4 lines

However, there are still cases where you need to write two different locations atomically, where an intervening _update_device() would be bad. Those need to use update_lock. An example is the fan_div/fan_min store.

What do we have to fear exactly. Please give the details of a troublesome scneario.

What about using my method described above for 9+-bit temperature values? I don't see anything bad happening if we write a value, then update the whole thing, then write the second value.

I think that the more elegant solution would be to:
1* Keep locking in _update_device().
2* Lock in _write_value(). Drivers that don't have such a function (which includes mines) will need one.
3* Never use data->x as a temporary step between scanning user input (from sysfs) and writing the value to the chip. The correct order is scan to a temporary value, write it to the chip, and then write it to data->x, if I understood it properly.

This also assumes that no sysfs callback function is actually allowed to read from the chip directly (unless the read value is not part of update_device() realm, but I don't think we have such cases).

BTW I reiterate my agreement with Phil Pokorny's registers split between input values and limits. Having two different caching periods for these groups makes much much sense and is likely to speed chip updates up. I also agree that not updating limits at all wouldn't be clever, since sometimes motherboards write to them in our backs and it's something we have to be informed of.

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#6 lore raw
In reply to Jean Delvare

According to Mark M. Hoffman:

Quoted 4 lines

In my opinion, the "cached update" scheme serves only one purpose, and it's not speed. Internally, some of these sensor chips actually pause the sampling and conversion in order to answer an i2c/smbus request.

You're correct, that's something I tend to forget.

Quoted 10 lines

If we didn't have this scheme in place and some user did this:

while [ true ]; do
cat /sys/bus/i2c/devices/xyz/temp1_in > /dev/null
done

... the value would in fact never change. But worse, it could also halt the alarms, QFAN, smartfan, or whatever - i.e. a DOS attack against the hardware. Keep in mind that the proc/sysfs files are world readable.

That's right. BTW, Aurelien Jarno and I decided to get rid of the caching mechanism in the pcf8574 and pcf8591 drivers as we ported them to 2.6. It seemed to be a problem not being able to get the values in real-time for such devices, since they are used in very particular conditions. I don't think it is a problem because such devices are not present on motherboards and I don't expect many people to use these drivers. And these devices don't seem to stop when you read from them (actually it's the contrary, they sample the inputs as you read them.

Aurelien, can you confirm this?

That said there's still a problem. The pcf* drivers accept all chips within their address ranges, because the chips are plain undetectable. So if someone was to load them and they actually try to drive a hardware monitoring chip instead, continuous readings from the sysfs files could prevent the chip from working (left apart the fact that the driver is likely to confuse the chip even without continuously reading, but there's nothing we can do there).

Maybe we could add a security warning in the kernel configuration help text for these two drivers? (something like "This driver is potentially dangerous if used on inappropriate hardware. Including it into the kernel is discouraged, module is prefered. Do not load this module unless you know you need it.")

Quoted 6 lines

All that said, I think that any further optimization of the update routine for speed is just added complexity for little or no gain. The data being reported by these drivers are not real-time critical w.r.t. their present application as PC hardware monitors. And in my experience, the precision of PC hardware sensors is questionable at best anyway.

I have to agree here too (this becomes anoying, can't you stop being right? ;)). I like simple things too, especially since "our" code is likely to be maintained by others in the future. But I have one objection though.

Philip Pokorny's approach of splitting the registers in two sets doesn't add much complexity IMHO. One more jiffies-value member in the data structure, and two ifs instead of one in the update function, that's not a high price to pay. OTOH it tends to clarify the different nature of these registers. Not only it speeds up the update, but it also limits the race conditions we are discussing these days, and, more importantly, it reduces the usage of the i2c bus (this obviously doesn't matter for ISA-only chips). I am much more sensible to this problem since I wrote the w83l785ts driver, since in various cases, reading from its registers collides with the BIOS doing the same. I had to implement some tweaks in this driver to workaround the problem. We are lucky that this chip only has two registers, and read-only. For this chip driver at least, I want to limit accesses to the i2c bus (so I plan to apply Philip Pokorny's method).

More on that in another answer.

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#8 lore raw
In reply to Jean Delvare
Quoted 7 lines · Jean Delvare

Hi all,

I have a question about the locking mechanism we have in all our i2c client drivers, 2.4 and 2.6 as well.

The data->update_lock semaphore is used in the *_update_dev function, and only there.

Right, but that is not always sufficient.

Quoted 6 lines

and only there. There are various other places all over the drivers where we access the same internal data, (in get callback functions) and even write to it (in store callback functions typically), and also access the i2c bus (in store functions too).

Why is it that we don't use the lock here?

It's a bug that's in all the drivers... e.g. from lm78.c:

254: static ssize_t set_in_min(struct device *dev, const char *buf,
255: size_t count, int nr)
256: {

257:         struct i2c_client *client = to_i2c_client(dev);
258:         struct lm78_data *data = i2c_get_clientdata(client);
259:         unsigned long val = simple_strtoul(buf, NULL, 10);
260:         data->in_min[nr] = IN_TO_REG(val);
261:         lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
262:         return count;

263: }

Lines 260 & 261 need to be protected against a simultaneous lm78_update_device(), but they're obviously not.

Quoted 1 line

What are we trying to protect exactly?

As they're written today, the update_lock only prevents multiple _update_device() calls from occurring at once. This is necessary, but not sufficient.

For the above case, you could do this:

259:         unsigned long val = simple_strtoul(buf, NULL, 10);
260:         /* deleted */
261:         lm78_write_value(client, LM78_REG_IN_MIN(nr), IN_TO_REG(val));
261.5:       data->valid = 0;
262:         return count;

However, there are still cases where you need to write two different locations atomically, where an intervening _update_device() would be bad. Those need to use update_lock. An example is the fan_div/fan_min store.

I'll try to send a patch later in the week.

Regards,

Signature · 2 lines
Mark M. Hoffman
[email protected]
#9 lore raw
In reply to Jean Delvare

* Jean Delvare <[email protected]> [2004-03-30 11:22:39 +0200]:

(snipped a lot of other stuff, so I can comment here first...)

Quoted 6 lines

BTW I reiterate my agreement with Phil Pokorny's registers split between input values and limits. Having two different caching periods for these groups makes much much sense and is likely to speed chip updates up. I also agree that not updating limits at all wouldn't be clever, since sometimes motherboards write to them in our backs and it's something we have to be informed of.

In my opinion, the "cached update" scheme serves only one purpose, and it's not speed. Internally, some of these sensor chips actually pause the sampling and conversion in order to answer an i2c/smbus request. If we didn't have this scheme in place and some user did this:

while [ true ]; do
cat /sys/bus/i2c/devices/xyz/temp1_in > /dev/null
done

.. the value would in fact never change. But worse, it could also halt the alarms, QFAN, smartfan, or whatever - i.e. a DOS attack against the hardware. Keep in mind that the proc/sysfs files are world readable.

All that said, I think that any further optimization of the update routine for speed is just added complexity for little or no gain. The data being reported by these drivers are not real-time critical w.r.t. their present application as PC hardware monitors. And in my experience, the precision of PC hardware sensors is questionable at best anyway.

Bottom line: I don't think these drivers should be any more complex than necessary. Furthermore, I think that any tradeoff of simplicity for update speed is bad for this class of drivers. K.I.S.S.

Regards,

Signature · 2 lines
Mark M. Hoffman
[email protected]
#10 lore raw
In reply to Jean Delvare
Quoted 10 lines · Jean Delvare

According to Mark M. Hoffman:

Quoted 8 lines

For the above case, you could do this:

259:         unsigned long val = simple_strtoul(buf, NULL, 10);
260:         /* deleted */
261:         lm78_write_value(client, LM78_REG_IN_MIN(nr),

IN_TO_REG(val));
261.5: data->valid = 0;
262: return count;

First let me point out there's still a race in my suggestion above... but the worst that can happen is that you would read an old value once. In the existing implementation, a write could get lost.

Quoted 8 lines

I don't like this much. This breaks our caching scheme. Basically, you invalidate all values just because of one value. Seems too high a price to pay, especially since you mostly *know* the missing value, you just wrote it. Why don't we just swap lines 260 and 261? I.e. basically don't trust data->in_min[nr] (because it can be changed by _update_device()) and use our own, local temporary variable (var). Write to the chip first, then update data->in_min[nr]. It could still be trashed by _update_device() after that, unless we also lock on writes. See below.

I understand about the caching, but it's much more simple this way. Especially it would be very easy for someone to review one or all of our drivers and see a common idiom...

	down(&data->update_lock);
	lm78_write_value(foo);
	/* or maybe read/write multiple registers */
	data->valid = 0;
	up(&data->update_lock);

and *know* that there are no races there... at a glance.

Quoted 7 lines
Quoted 4 lines

However, there are still cases where you need to write two different locations atomically, where an intervening _update_device() would be bad. Those need to use update_lock. An example is the fan_div/fan_min store.

What do we have to fear exactly. Please give the details of a troublesome scneario.

Actually, it doesn't even need to be two seperate locations. E.g. from asb100.c set_fan_div()...

        case 2: /* fan 3 */
                reg = asb100_read_value(client, ASB100_REG_PIN);
                reg = (reg & 0x3f) | (data->fan_div[2] << 6);
                asb100_write_value(client, ASB100_REG_PIN, reg);
                break;

This is a classic read-modify-write race. It's literally impossible to get rid of that race if you have something like the BIOS fiddling with the chip behind our backs. But at least our own update_device() calls shouldn't get in the way.

Quoted 6 lines

What about using my method described above for 9+-bit temperature values? I don't see anything bad happening if we write a value, then update the whole thing, then write the second value.

I think that the more elegant solution would be to:
1* Keep locking in _update_device().

No dispute there, see my other message. [1]

Quoted 2 lines

2* Lock in _write_value(). Drivers that don't have such a function (which includes mines) will need one.

At least in asb100.c, the entire write_value function runs holding a different lock because of the bank switching. Holding the update_lock here would accomplish nothing AFAICT.

Quoted 4 lines

3* Never use data->x as a temporary step between scanning user input (from sysfs) and writing the value to the chip. The correct order is scan to a temporary value, write it to the chip, and then write it to data->x, if I understood it properly.

This is still not sufficient in case you need to do a read-modify-write like above. That applies to all registers whose bits map to more than one sysfs file.

So, I think we should grab the lock, do the work, clear data->valid, and then let go of the lock. Are there any objections other than forcing a full update_device on the next read? I don't see that update as a problem. (Again, see [1])

I'll try to send an example patch later this week.

Quoted 3 lines

This also assumes that no sysfs callback function is actually allowed to read from the chip directly (unless the read value is not part of update_device() realm, but I don't think we have such cases).

Yes, this is relatively clean and we should keep it that way.

[1] http://archives.andrew.net.au/lm-sensors/msg07255.html

Regards,

Signature · 2 lines
Mark M. Hoffman
[email protected]
#11 lore raw
In reply to Jean Delvare

According to Mark M. Hoffman:

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines

For the above case, you could do this:

259:         unsigned long val = simple_strtoul(buf, NULL, 10);
260:         /* deleted */
261:         lm78_write_value(client, LM78_REG_IN_MIN(nr),

IN_TO_REG(val));
261.5: data->valid = 0;
262: return count;

Quoted 3 lines

First let me point out there's still a race in my suggestion above... but the worst that can happen is that you would read an old value once. In the existing implementation, a write could get lost.

I wouldn't call that a race. I _update_device() is called between the write and valid=0, you simply get the value you would have gotten before the write.

A more relevant case would be when setting fan divisors, because you are
also changing fan mins. If there's an update inbetween the two writes, the returned set of values is incoherent. This is what I call a race condition. And that cannot be solved by your data->valid = 0 trick, (which I definitely don't like). We have to lock on write.

Quoted 11 lines

I understand about the caching, but it's much more simple this way. Especially it would be very easy for someone to review one or all of our drivers and see a common idiom...

	down(&data->update_lock);
	lm78_write_value(foo);
	/* or maybe read/write multiple registers */
	data->valid = 0;
	up(&data->update_lock);

and *know* that there are no races there... at a glance.

The absence of race is guaranteed by the use of the lock, not by the invalidation of the cached data. I still see no benefit in invalidating all the cache over simply storing the written value in the cache by ourselves.

Quoted 9 lines

Actually, it doesn't even need to be two seperate locations. E.g. from asb100.c set_fan_div()...

        case 2: /* fan 3 */
                reg = asb100_read_value(client, ASB100_REG_PIN);
                reg = (reg & 0x3f) | (data->fan_div[2] << 6);
                asb100_write_value(client, ASB100_REG_PIN, reg);
                break;
Quoted 4 lines

This is a classic read-modify-write race. It's literally impossible to get rid of that race if you have something like the BIOS fiddling with the chip behind our backs. But at least our own update_device() calls shouldn't get in the way.

I guess you mean, if someone else is *writing* to the same register between our read and our write? (if only reading, I don't see any race, like above: you get the old value instead of the new one, andt hat's all). This simply confirms what I we seem to agree on since the
beginning: we need to lock on write.

Quoted 3 lines

At least in asb100.c, the entire write_value function runs holding a different lock because of the bank switching. Holding the update_lock here would accomplish nothing AFAICT.

I think you're correct. Same is true for w83781d and w83627hf, of course. But that's not the most common case.

Quoted 7 lines
Quoted 4 lines

3* Never use data->x as a temporary step between scanning user input (from sysfs) and writing the value to the chip. The correct order is scan to a temporary value, write it to the chip, and then write it to data->x, if I understood it properly.

This is still not sufficient in case you need to do a read-modify-write like above.

Correct, not sufficient *and* useless if update and writes all use the lock. Let's forget about that.

Quoted 2 lines

That applies to all registers whose bits map to more than one sysfs file.

I don't get you here. I tend to think this affects all the registers. Two processes could want to write to the same sysfs file at the same time.

Quoted 4 lines

So, I think we should grab the lock, do the work, clear data->valid, and then let go of the lock. Are there any objections other than forcing a full update_device on the next read? I don't see that update as a problem.

Objection! ;)

As stated several times before, I still don't see how forcing a full update helps in any way. If update and writing functions all use the lock, the races problem is solved. All you have to do is update the cache with the single value you just wrote. I don't much like the idea of generating activity on the i2c for no reason, speed considerations left apart.

Thanks.

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#12 lore raw
In reply to Jean Delvare

Selon Aurelien Jarno :

Quoted 3 lines

You're right that it can cause some problems, on my system I need to load the ds1621 module before the pcf8591 otherwise my ds1621 chip is recognized as a pcf8591.

In case you didn't know that, you can add a line like this in your /etc/modules.conf file:
options pcf8591 ignore=0,0x42
where 0 is the bus number and 0x42 the i2c address of your DS1621 chip.

Then you can load the driver modules in any order.

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#13 lore raw
In reply to Jean Delvare

On Wed, Mar 31, 2004 at 10:45:47AM +0200, Jean Delvare wrote:

[...]

Quoted 10 lines

That's right. BTW, Aurelien Jarno and I decided to get rid of the caching mechanism in the pcf8574 and pcf8591 drivers as we ported them to 2.6. It seemed to be a problem not being able to get the values in real-time for such devices, since they are used in very particular conditions. I don't think it is a problem because such devices are not present on motherboards and I don't expect many people to use these drivers. And these devices don't seem to stop when you read from them (actually it's the contrary, they sample the inputs as you read them.

Aurelien, can you confirm this?

I confirm that. For the pcf8574, the 8-bit input port is sampled during the first clock of the data byte, and for the pcf8591, each bit is sampled just before being sent.

Quoted 7 lines

That said there's still a problem. The pcf* drivers accept all chips within their address ranges, because the chips are plain undetectable. So if someone was to load them and they actually try to drive a hardware monitoring chip instead, continuous readings from the sysfs files could prevent the chip from working (left apart the fact that the driver is likely to confuse the chip even without continuously reading, but there's nothing we can do there).

You're right that it can cause some problems, on my system I need to load the ds1621 module before the pcf8591 otherwise my ds1621 chip is recognized as a pcf8591.

Quoted 5 lines

Maybe we could add a security warning in the kernel configuration help text for these two drivers? (something like "This driver is potentially dangerous if used on inappropriate hardware. Including it into the kernel is discouraged, module is prefered. Do not load this module unless you know you need it.")

That could be an idea. To be honest, I don't see other alternative as the chips are very stupids; the pcf8574 doesn't have any control register, and the pcf8591 has one control register but only writable.

Signature · 4 lines
  .''`.  Aurelien Jarno	              GPG: 1024D/F1BCDB73
 : :' :  Debian GNU/Linux developer | Electrical Engineering Student 
 `. `'   [email protected]         | [email protected]
   `-    people.debian.org/~aurel32 | www.aurel32.net