Skip to contentSkip to conversation

driver design question

DISCUSSION Jean Delvare 5 participants 11 messages May 19, 2005

lore
Share & tools
lore
#0 lore raw

Hi all,

Reading some code today, I noticed that most of our drivers, if not all, read the current values and the limit values when updated. Why that? I would have read the current values only. We set the limits ourself (either at init time or through procfs/sysfs) so they are unlikely to have changed. Looks like an overhead we could easily get rid of. Is there something obvious I am missing?

Thanks.

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

Reading some code today, I noticed that most of our drivers, if not all, read the current values and the limit values when updated. Why that? I would have read the current values only. We set the limits ourself(either at init time or through procfs/sysfs) so they are unlikely to have changed. Looks like an overhead we could easily get rid of. Is there something obvious I am missing?

Quoted 4 lines

I think we should try to read and report what we know. If we don't read the limits, we probably shouldn't report them either (i.e. make them write-only). Reducing overhead by introducing assumptions in the code (like that limits won't change), is a bad idea, imho.

I think I understand your point. You want us to play it safe and you must be right. However, what could make the limits change? The chip (refering to ADM1025 here, since it's the one I'm working on right now) doesn't change them on his own unless it is power-cycled (a case we don't handle anyway, and I believe we are right to do so). The only way the limits can be changed is through our driver, and in this case we can store it on-the-fly instead of reading it back on every update.

One may object that this doesn't allow us to check wether a round-trip from userspace to the registers and back again is OK (we assume that the right value was written to the register). However, we are never sure the register value is right, even if we read the values back. The two conversions formula can be bad but compensate each other, and it's transparent from user's point of view. Anyway, a compromise is to read the limits back once (at the time we write them) so we don't loose this is-the-conversion-OK information.

Another compromise is to read the limits but not on every update. This would however require to have two "last update" values recorded into the driver. Not very clean IMHO.

Is there any way values can be written to the driver's register without telling us?

Quoted 2 lines

At least, I think this is more important at the driver level than the user-space apps.

Not sure I get you there, is there a similar problematic with user-space apps?

Quoted 1 line

Are we having problems with performance and overhead issues?

No (at least none I am aware of). But I don't like the idea of our driver doing something that could be avoided with no drawback. Please realize that for each update, which can occur typically as often as once every 1.5 second, 1/3 of the work is for the current values, which is legitimate, and 2/3 of the work is for limits, which 100% of the time AFAIK did not change. Multiplying the amount of work by three is more than just overhead, isn't it?

Thus my question, and I ask it again, maybe more clearly than the first time. How could the chip's register be changed without using our driver?

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

Is there any way values can be written to the driver's register without telling us?

I think any other communication with the chip which isn't through our driver (Bios? or some other code?) might change the values, although it is unlikely.

No other code should. If they do, they are bad, not us. As for the BIOS, is there any BIOS code running after boot time? I would have said no, but you seem to think the other way.

Of course, one can use i2cset to write some arbitrary value to any register, and break the driver if it is designed my way, while it woudln't if designed your way. But someone doing so has to know what he/she does, isn't it? I2cset is meant for developing/debugging purposes, not regular use.

Quoted 3 lines

It's also possible that a driver may be misdetected or forced. It would be odd if the driver gave misleading 'correct' limit readings in these cases. I don't think it is worth the risk.

As I was proposing, we still can read the limit back once, at the time we write it at first (and any later time we change it) so if it really is wrong (such as the register is read only) we'll see it exactly the same way we would with the current policy. As far as I know, the chips only seldom, if ever, change their R/W registers on their own, except at init time (none of our business) and reset (this could happen, I admit, but shouldn't after module init). So the only two errors I can think of if the chip isn't what it is supposed to be are writing a limit to a read only register (which can be handled as described right above) and hitting some reset register or bit, which is likely to show the same way (reading the value back probably won't be what was expected) and will have other side effects the user will probably notice.

All these are problems caused by the driver being used with wrong hardware, which will not work anyway. It looks like we "optimize" our drivers for the very very few cases were the things go bad. I don't pretend we must ignore problems and remove error checking from our code ;) but still I believe we should optimize the working, not the faulty.

One thing I'm strongly thinking of is disabling the limit readings *unless* the module is loaded with any force parameter (still mainly thinking of my ADM1025/NE1619 driver). This makes the secure path back in case a user is forcing the driver, but makes the driver faster for regular users.

Quoted 2 lines

In a nutshell, I think lower level drivers which talk to hardware shouldn't do any internal caching or optimizing which isn't nessesary.

And I don't quite agree. We are already caching since we limit the frequency at which new data can be obtained. Also, note that I don't advocate in favor of caching too much. For example, one could think of caching VID readings since it's unlikely to change after boot time, still I think it could in some cases (I guess Speedstep technology and the like could want to change that value) so we have to "sample" it continuously. I just want to limit what I consider useless re-readings, (mainly limits).

Quoted 2 lines

I.e., when values are asked for, it should talk directly with the hardware to ask for them and not make any assumptions.

I understand your point of view. I think it's motivated by the fact that you are a developer (I am too BTW ;)) and you must love the idea of watching whatever happens to the internal registers of every chipset in your computer. I love playing with them too, don't doubt it :) But what about the end-user? He/she wants an efficient driver. He/she event just wants temperature, etc. readings. The fact that a driver is required for that, and how the driver does it, is far beyond his/her interest.

Again, I don't mean we can do anything that would make the driver faster, at any cost. I don't want speed. I want efficiency.

Quoted 3 lines

Now, for user-space apps, it could be more justifiable to cache or assume values. At least, I'd rather see those sorts of hacks done in user-space than kernel driver space.

OK, maybe I see what you mean now, but I think you're missing something. At the time being, the user-space applications cannot cache a given set of values and refresh only the rest, because most of our drivers, if not all, have a single update function. If *one* value is queried and the data isn't up-to-date, *all* values are refreshed. This is the reason why I want to limit what is refreshed.

(Next paragraph is me thinking while writing, not necessary very clear, feel free to plain skip or ask for better explanations)
We could think of splitting all values into groups for separate refreshes, but it ain't that obvious. The groups would be "vertical" (understand: current values form one group, limits form one group...) from the driver's point of view, while they would be "horizontal" (understand: in0, in0_min and in0_max form one group...) from user-space (because for example reading /proc/dev/sys/.../in0 needs these three values). Or we can have individual refreshed (that is, each group only has one value in it), but considering you need one timestamp per group, it will be really painful. What's more, I think most applications will read all values at a time, which justifies there is a single update function in our drivers. This is how I came to the idea of not reading the limits as being the better optimization one could think of.

Quoted 3 lines

For this paricular issue (if we are trying to optimize speed or limit transaction numbers over the I2C bus), it doesn't buy anything so it is a moot point.

I guess the I2C bus isn't overloaded by our drivers, for sure ;) On the other hand, speed (I prefer the term of quickness or reactivity) could be discussed. For example, running "sensors" on my desktop system takes about 500ms (three busses, one chip on each) when an update is needed. OK, 500ms isn't that long, but think of it. It doesn't do that many things in 500ms. It could be faster, isn't it? If it ain't much of an issue for the sensors program, I think it is for apps that monitor the values continuously (such as gkrellm). My proposal could lower the time to say 200ms, which is far better IMHO.

Quoted 8 lines
Quoted 3 lines

Thus my question, and I ask it again, maybe more clearly than the first time. How could the chip's register be changed without using our driver?

Well, I guess it is a matter of ideals. I've thought of kernel hardware drivers as being an inbetween for the user-space apps and the actual hardware with absolutely as little tainting of the data as possible.

We already do many transformations, even between the registers and the internal vars.

Quoted 3 lines

Even if the values in the hardware's registers are unlikely to change or change infrequently, we still should pass values straight through and not make any assumptions on the data in code.

My point isn't about values that are unlikely to change (see my opinion about VID above) but about values that *should* not change. It's slightly different (in the spirit if not in the facts).

Quoted 8 lines

As long as overhead and processor loading isn't an issue, I'd suggest we not cache or code in any value/limit assumptions into the code. I2C transactions are slow, but they cause little (if any) CPU loading or resource hogging. Even as far as percentage usage of the I2C bus, it is small. I can't see any possible significant performance gains by caching things like limits. Also, there are at least some potential drawbacks (at least marginally more than simply 'no drawbacks'), so I see some additional risk with no gain.

Hardly anything I can say against this, you're of course right. It's more about having a "beautiful" driver than anything else.

Quoted 7 lines

BTW- The worst offender of reading values over and over with little chance of change is the eeprom driver. For each device, if I remember correctly, each byte read takes two I2C transactions for 256 bytes! So, if you have 4 dimms (with an eeprom on each), you are doing over 2 thousand transactions. Still, though, this take little time and almost no CPU overhead, so it doesn't seem to hurt anything.

You're definitely right. I guess this is the reason why the update frequency is limited to 1 each 5 minutes as opposed to 1 each 1 or 2 seconds for the rest of our drivers. Refreshing the two eeproms on my laptops takes 5 full seconds (and I was complaining about the 500ms on the other system...) The worst part of the story is that over 50% of the data we retrieve isn't even used. Anyway it's a bit different, the EEPROM driver isn't a sensor-like driver. It isn't meant to be refreshed frequently. All data in the EEPROM are the same (update-frequency-wise), so that a user-space app can cache the whole thing and never use the driver again, while it's different with usual chipsets. While it's useful for our tests, few people want to use that driver (I even wonder if we should tell the users to modprobe that module at the end of sensors-detect).

Quoted 1 line

Anyways, that's my $0.02. :')

I add my 0.02 Eur (not much more ;)). Long way still before we can afford a drink ;) Thanks a lot for the clarifications, I really appreciate when we can exchange our views like that.

(My state of mind after this step: I think I'll use "my" policy in the ADM1025 as a test. It's not widely used AFAIK so we don't risk much. If we have feedback of any kind, we'll could either revert the driver to the usual policy, of extend the experiment to other drivers. Or I can just be quiet and leave the ADM1025 driver as the other ones, if you or anyone doesn't want me to experiment. What is certain after our discussion is that I won't change all drivers to my policy right now ;))

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

I think we should try to read and report what we know. If we don't read the limits, we probably shouldn't report them either (i.e. make them write-only). Reducing overhead by introducing assumptions in the code (like that limits won't change), is a bad idea, imho. At least, I think this is more important at the driver level than the user-space apps.

Are we having problems with performance and overhead issues?

Phil

Quoted 13 lines · Jean Delvare

Hi all,

Reading some code today, I noticed that most of our drivers, if not all, read the current values and the limit values when updated. Why that? I would have read the current values only. We set the limits ourself (either at init time or through procfs/sysfs) so they are unlikely to have changed. Looks like an overhead we could easily get rid of. Is there something obvious I am missing?

Thanks.

#4 lore raw
In reply to Jean Delvare

BTW- I'm glad you're looking for optimizations and clean-ups. I appreciate your efforts. I think this is a topic which is really has valid points either way. As you suggest, I think I am more leaning towards playing it safe. :')

I'll make a few comments and clarifications inline, and perhaps Mark and some others can weigh in.

Quoted 3 lines · Jean Delvare

Is there any way values can be written to the driver's register without telling us?

I think any other communication with the chip which isn't through our driver (Bios? or some other code?) might change the values, although it is unlikely. It's also possible that a driver may be misdetected or forced. It would be odd if the driver gave misleading 'correct' limit readings in these cases. I don't think it is worth the risl

In a nutshell, I think lower level drivers which talk to hardware shouldn't do any internal caching or optimizing which isn't nessesary. I.e., when values are asked for, it should talk directly with the hardware to ask for them and not make any assumptions.

Now, for user-space apps, it could be more justifiable to cache or assume values. At least, I'd rather see those sorts of hacks done in user-space than kernel driver space. For this paricular issue (if we are trying to optimize speed or limit transaction numbers over the I2C bus), it doesn't buy anything so it is a moot point.

Quoted 9 lines

No (at least none I am aware of). But I don't like the idea of our driver doing something that could be avoided with no drawback. Please realize that for each update, which can occur typically as often as once every 1.5 second, 1/3 of the work is for the current values, which is legitimate, and 2/3 of the work is for limits, which 100% of the time AFAIK did not change. Multiplying the amount of work by three is more than just overhead, isn't it?

Quoted 4 lines

Thus my question, and I ask it again, maybe more clearly than the first time. How could the chip's register be changed without using our driver?

Well, I guess it is a matter of ideals. I've thought of kernel hardware drivers as being an inbetween for the user-space apps and the actual hardware with absolutely as little tainting of the data as possible. Even if the values in the hardware's registers are unlikely to change or change infrequently, we still should pass values straight through and not make any assumptions on the data in code.

As long as overhead and processor loading isn't an issue, I'd suggest we not cache or code in any value/limit assumptions into the code. I2C transactions are slow, but they cause little (if any) CPU loading or resource hogging. Even as far as percentage usage of the I2C bus, it is small. I can't see any possible significant performance gains by caching things like limits. Also, there are at least some potential drawbacks (at least marginally more than simply 'no drawbacks'), so I see some additional risk with no gain.

BTW- The worst offender of reading values over and over with little chance of change is the eeprom driver. For each device, if I remember correctly, each byte read takes two I2C transactions for 256 bytes! So, if you have 4 dimms (with an eeprom on each), you are doing over 2 thousand transactions. Still, though, this take little time and almost no CPU overhead, so it doesn't seem to hurt anything.

Anyways, that's my $0.02. :')

Phil

#5 lore raw
In reply to Jean Delvare

Good discussion. Let me toss a few very quick comments:

Quoted 4 lines · Jean Delvare

And I don't quite agree. We are already caching since we limit the

frequency at which new data can be obtained. [...]

Quick note: The refresh frequency limit was introduced because the hardware would give bad results if you polled it too fast. So, depending on what the datasheet for the chip says, we adjust the max polling frequency to match. So this was to ensure accurate results from the hardware, not as a performace optimization.

Quoted 5 lines

Hardly anything I can say against this, you're of course right. It's

more about having a "beautiful" driver than anything else.

My idea of the ideal driver is that it is almost transparent to the hardware. The less caching, the better.

Now, your point about reducing a significant amount of latency is a good one. That's a reasonable reason for such an optimization. I'm not sure if it is a strong enough reason, though. I think we'd need to do a little math to figure out exactly how much it 'costs' to read limits and what it would save us to poll them less frequently.

Phil

#6 lore raw
In reply to Jean Delvare
Quoted 11 lines · Philip Edelbrock
Quoted 3 lines · Jean Delvare

And I don't quite agree. We are already caching since we limit the frequency at which new data can be obtained. [...]

Quick note: The refresh frequency limit was introduced because the hardware would give bad results if you polled it too fast. So, depending on what the datasheet for the chip says, we adjust the max polling frequency to match. So this was to ensure accurate results from the hardware, not as a performace optimization.

Take a look at the ADM1026 driver (by Phil P.) as a counter-example. The update routine is apropos to this thread. He uses two different refresh frequencies - the slower one for config data. I thought this was a good idea when I first saw it.

Quoted 2 lines

My idea of the ideal driver is that it is almost transparent to the hardware. The less caching, the better.

Much as I try, I can't jump off the fence here. OOH, it seems wasteful to keep re-reading what are almost certainly non-volatile h/w registers. OTOH, what are we optimizing here? - a special purpose slow bus that has nothing better to do (I2C); or an insignificant amount of ISA port IO.

Quoted 5 lines

Now, your point about reducing a significant amount of latency is a good one. That's a reasonable reason for such an optimization. I'm not sure if it is a strong enough reason, though. I think we'd need to do a little math to figure out exactly how much it 'costs' to read limits and what it would save us to poll them less frequently.

It depends on the chip of course - but I think the ADM1026 driver might be a good model to follow for others which are deemed worth optimizing.

Regards,

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

Quick note: The refresh frequency limit was introduced because the hardware would give bad results if you polled it too fast. So, depending on what the datasheet for the chip says, we adjust the max polling frequency to match. So this was to ensure accurate results from the hardware, not as a performace optimization.

Take a look at the ADM1026 driver (by Phil P.) as a counter-example. The update routine is apropos to this thread. He uses two different refresh frequencies - the slower one for config data. I thought this was a good idea when I first saw it.

Exactly what I had in mind. I didn't know someone else already did that :)

After some more thinking however, I'm not sure it's what we really want. We are talking (correct me if I'm wrong there) about data that should not change *at all*, not data that should not change *often*. So sampling their value every 5 minutes doesn't make much sense. If they change and we are not aware of the change, it's likely to cause trouble that will solve only up to 5 minutes later. The user will probably get mad about this (by the time he/she tries to figure out what's wrong, the problem will solve by itself). So I think we should opt for one of the following policy:

1* We refresh these values as often as the other ones (what most of our drivers do) and don't care about the performance cost. Play it safe, Phil E. said.

2* We say these values should *NOT* change, we don't sample them, and if something goes wrong, obviously it's not our fault (though the user will certainly complain to us). We can enable a check (that is, sampling the should-not-change data as in 1* and emit a warning if we see a change in the values) in debug mode (or additional parameter?) to catch the problem.

One more thought though: remember that these things are very unlikely to happen. We have the adm1026 driver which as a different policy, now the adm1025 has its own one too. These are good tests. If we have no bad feedback about these, it must mean it's safe to change our global policy to either of those.

Quoted 9 lines
Quoted 5 lines

Now, your point about reducing a significant amount of latency is a good one. That's a reasonable reason for such an optimization. I'm not sure if it is a strong enough reason, though. I think we'd need to do a little math to figure out exactly how much it 'costs' to read limits and what it would save us to poll them less frequently.

It depends on the chip of course - but I think the ADM1026 driver might be a good model to follow for others which are deemed worth optimizing.

What about trying some real-life tests (that's what it's all about after all)? I could try the different policies on the w83781d driver and measure how much time module loads and data refreshs take.

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#8 lore raw
In reply to Jean Delvare
Quoted 10 lines · Mark M. Hoffman
Quoted 3 lines · Philip Edelbrock

Jean Delvare wrote:
My idea of the ideal driver is that it is almost transparent to the hardware. The less caching, the better.

Much as I try, I can't jump off the fence here. OOH, it seems wasteful to keep re-reading what are almost certainly non-volatile h/w registers. OTOH, what are we optimizing here? - a special purpose slow bus that has nothing better to do (I2C); or an insignificant amount of ISA port IO.

Don't under-estimate the delays associated with doing ISA port I/O. It can take *thousands* of clock cycles to perform just one port I/O instruction. The kernel even uses I/O to port 0x80 as a short timing delay.

:v)

Signature · 4 lines
Philip Pokorny, Director of Engineering
Tel: 415-358-2635   Fax: 415-358-2646   Toll Free: 888-PENGUIN
PENGUIN COMPUTING, INC.
www.penguincomputing.com
#9 lore raw
In reply to Jean Delvare
Quoted 16 lines · Jean Delvare
Quoted 7 lines
Quoted 6 lines

Quick note: The refresh frequency limit was introduced because the hardware would give bad results if you polled it too fast. So, depending on what the datasheet for the chip says, we adjust the max polling frequency to match. So this was to ensure accurate results from the hardware, not as a performace optimization.

Quoted 5 lines

Take a look at the ADM1026 driver (by Phil P.) as a counter-example. The update routine is apropos to this thread. He uses two different refresh frequencies - the slower one for config data. I thought this was a good idea when I first saw it.

Exactly what I had in mind. I didn't know someone else already did that :)

Quoted 9 lines

After some more thinking however, I'm not sure it's what we really want. We are talking (correct me if I'm wrong there) about data that should not change *at all*, not data that should not change *often*. So sampling their value every 5 minutes doesn't make much sense. If they change and we are not aware of the change, it's likely to cause trouble that will solve only up to 5 minutes later. The user will probably get mad about this (by the time he/she tries to figure out what's wrong, the problem will solve by itself). So I think we should opt for one of the following policy:

That's one of the reasons that I still poll the data that I don't expect to be different. I made sure to update the cache immediately when I change a configuration parameter so that future reads will return the correct data. But then at some time later, The driver will read the actual hardware so you can always be sure that after some period of time what you see from the driver is what's actually on the chip.

This turned out to be important because it helped me find a programming bug in my driver. I wasn't bit shifting and masking one of the parameters correctly in and out of the chip. The result was that some settings didn't get changed correctly. The cache was what I "wanted" but the chip was set to something different. If I hadn't included the re-read of the chip, I don't think I would have found this bug.

A cache is just a cache. It's not the real thing. You must include a way to update the cache to make it consistent with the "real thing". That might be some other parameter/attribute that you write to to trigger a cache update on demand. Or it can be a timeout as implemented in the adm1026 driver.

With ACPI and IPMI poking at these monitoring chips, I think it's dangerous for us to assume that we know everything that's going on with the sensor. Witness Margit's experience where we programmed the limits incorrectly and the BIOS shut down his machine. Or where we set off alarm beepers because the limits are wrong.

Quoted 3 lines

1* We refresh these values as often as the other ones (what most of our drivers do) and don't care about the performance cost. Play it safe, Phil E. said.

That's reasonable for simple chips with few registers. But consider that just about every chip has twice as many limit values (min and max) as current readings. That means that only 35% of the data is changing rapidly. That's a significant performance optimization to reduce 65% of reads.

Quoted 6 lines

2* We say these values should *NOT* change, we don't sample them, and if something goes wrong, obviously it's not our fault (though the user will certainly complain to us). We can enable a check (that is, sampling the should-not-change data as in 1* and emit a warning if we see a change in the values) in debug mode (or additional parameter?) to catch the problem.

As I said above, I think we should just read the bits once in a while. Adding checks for unexpected changes could add significantly to the code.

Quoted 12 lines

One more thought though: remember that these things are very unlikely to happen. We have the adm1026 driver which as a different policy, now the adm1025 has its own one too. These are good tests. If we have no bad feedback about these, it must mean it's safe to change our global policy to either of those.

Quoted 6 lines
Quoted 6 lines

Now, your point about reducing a significant amount of latency is a good one. That's a reasonable reason for such an optimization. I'm not sure if it is a strong enough reason, though. I think we'd need to do a little math to figure out exactly how much it 'costs' to read limits and what it would save us to poll them less frequently.

Quoted 8 lines
Quoted 3 lines

It depends on the chip of course - but I think the ADM1026 driver might be a good model to follow for others which are deemed worth optimizing.

What about trying some real-life tests (that's what it's all about after all)? I could try the different policies on the w83781d driver and measure how much time module loads and data refreshs take.

Real world test data is always best.

:v)

#10 lore raw
In reply to Jean Delvare

Doing some catching up.

Good discussion, I agree with most of it.
Some thoughts:

- ACPI can indeed go poking around behind our backs.

- Experimenting using one (small) driver is indeed a good thing. Note that adm1021 has my enhancements for returning an alarm if a read fails.

- Reading limits less often than other things is probably good.

- If people have a problem with eeprom performance they can rmmod it.

Quoted 111 lines · Philip Pokorny
Quoted 16 lines · Jean Delvare
Quoted 7 lines
Quoted 6 lines

Quick note: The refresh frequency limit was introduced because the hardware would give bad results if you polled it too fast. So, depending on what the datasheet for the chip says, we adjust the max polling frequency to match. So this was to ensure accurate results from the hardware, not as a performace optimization.

Quoted 5 lines

Take a look at the ADM1026 driver (by Phil P.) as a counter-example. The update routine is apropos to this thread. He uses two different refresh frequencies - the slower one for config data. I thought this was a good idea when I first saw it.

Exactly what I had in mind. I didn't know someone else already did that :)

Quoted 9 lines

After some more thinking however, I'm not sure it's what we really want. We are talking (correct me if I'm wrong there) about data that should not change *at all*, not data that should not change *often*. So sampling their value every 5 minutes doesn't make much sense. If they change and we are not aware of the change, it's likely to cause trouble that will solve only up to 5 minutes later. The user will probably get mad about this (by the time he/she tries to figure out what's wrong, the problem will solve by itself). So I think we should opt for one of the following policy:

That's one of the reasons that I still poll the data that I don't expect to be different. I made sure to update the cache immediately when I change a configuration parameter so that future reads will return the correct data. But then at some time later, The driver will read the actual hardware so you can always be sure that after some period of time what you see from the driver is what's actually on the chip.

This turned out to be important because it helped me find a programming bug in my driver. I wasn't bit shifting and masking one of the parameters correctly in and out of the chip. The result was that some settings didn't get changed correctly. The cache was what I "wanted" but the chip was set to something different. If I hadn't included the re-read of the chip, I don't think I would have found this bug.

A cache is just a cache. It's not the real thing. You must include a way to update the cache to make it consistent with the "real thing". That might be some other parameter/attribute that you write to to trigger a cache update on demand. Or it can be a timeout as implemented in the adm1026 driver.

With ACPI and IPMI poking at these monitoring chips, I think it's dangerous for us to assume that we know everything that's going on with the sensor. Witness Margit's experience where we programmed the limits incorrectly and the BIOS shut down his machine. Or where we set off alarm beepers because the limits are wrong.

Quoted 3 lines

1* We refresh these values as often as the other ones (what most of our drivers do) and don't care about the performance cost. Play it safe, Phil E. said.

That's reasonable for simple chips with few registers. But consider that just about every chip has twice as many limit values (min and max) as current readings. That means that only 35% of the data is changing rapidly. That's a significant performance optimization to reduce 65% of reads.

Quoted 6 lines

2* We say these values should *NOT* change, we don't sample them, and if something goes wrong, obviously it's not our fault (though the user will certainly complain to us). We can enable a check (that is, sampling the should-not-change data as in 1* and emit a warning if we see a change in the values) in debug mode (or additional parameter?) to catch the problem.

As I said above, I think we should just read the bits once in a while. Adding checks for unexpected changes could add significantly to the code.

Quoted 18 lines

One more thought though: remember that these things are very unlikely to happen. We have the adm1026 driver which as a different policy, now the adm1025 has its own one too. These are good tests. If we have no bad feedback about these, it must mean it's safe to change our global policy to either of those.

Quoted 9 lines
Quoted 6 lines

Now, your point about reducing a significant amount of latency is a good one. That's a reasonable reason for such an optimization. I'm not sure if it is a strong enough reason, though. I think we'd need to do a little math to figure out exactly how much it 'costs' to read limits and what it would save us to poll them less frequently.

It depends on the chip of course - but I think the ADM1026 driver might be a good model to follow for others which are deemed worth optimizing.

Quoted 3 lines

What about trying some real-life tests (that's what it's all about after all)? I could try the different policies on the w83781d driver and measure how much time module loads and data refreshs take.

Real world test data is always best.

:v)