Skip to contentSkip to conversation

hwmon: sht3x: set initial jiffies to last_update

PATCH Superseded Matt Ranostay 2 participants 6 messages Jul 8, 2016 – Jul 25, 2016

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: sht3x: set initial jiffies to last_update

Handling the wraparound requires the data->last_update to be set to an initial jiffies value. Otherwise you can start in a state where the sensor will never request a reading.

Cc Guenter Roeck <[email protected]>
Cc David Frey <[email protected]>
---
 drivers/hwmon/sht3x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/sht3x.c +1 -1
index 450645b6053d..05a925257938 100644
@@ -722,7 +722,7 @@ static int sht3x_probe(struct i2c_client *client,
 	data->setup.blocking_io = false;
 	data->setup.high_precision = true;
 	data->mode = 0;
-	data->last_update = 0;
+	data->last_update = jiffies;
 	data->client = client;
 	crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
 
Signature · 1 line
2.7.4
#1 lore raw

Hi Matt,

Quoted 3 lines · Matt Ranostay

Handling the wraparound requires the data->last_update to be set to an initial jiffies value. Otherwise you can start in a state where the sensor will never request a reading.

I can't see how. As I read the code, in the worst case, readings can be blocked for interval_ms (2 seconds maximum.)

Quoted 13 lines
Cc Guenter Roeck <[email protected]>
Cc David Frey <[email protected]>
---
 drivers/hwmon/sht3x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/sht3x.c +0 -0
index 450645b6053d..05a925257938 100644

drivers/hwmon/sht3x.c static int sht3x_probe(struct i2c_client *client, · line 728

Quoted 8 lines
drivers/hwmon/sht3x.c +1 -1
@@ -722,7 +722,7 @@ static int sht3x_probe(struct i2c_client *client,
 	data->setup.blocking_io = false;
 	data->setup.high_precision = true;
 	data->mode = 0;
-	data->last_update = 0;
+	data->last_update = jiffies;
 	data->client = client;
 	crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
 

Both look equally wrong to me. With your proposal, accessing the sysfs attributes right after loading the driver will not trigger a reading.

In order to guarantee that the first access will trigger a reading, data->last_update should be initialized to jiffies - msecs_to_jiffies(2000) (the maximum interval value.)

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#2 lore raw
Quoted 9 lines · Jean Delvare

Hi Matt,

Quoted 3 lines · Matt Ranostay

Handling the wraparound requires the data->last_update to be set to an initial jiffies value. Otherwise you can start in a state where the sensor will never request a reading.

I can't see how. As I read the code, in the worst case, readings can be blocked for interval_ms (2 seconds maximum.)

On 64-bit systems this is never an issue because the jiffies counter will never wrap around.

But my system is a 32-bit ARM core, so the the kernel sets the initial value to 0xfffb6c20 so it will wrap around in 5 minutes to find buggy code.

So looking at time_after(0xfffb6c20, 0) will return false always till it finally rolls over.

Quoted 26 lines
Quoted 14 lines
Cc Guenter Roeck <[email protected]>
Cc David Frey <[email protected]>
---
 drivers/hwmon/sht3x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/sht3x.c +0 -0
index 450645b6053d..05a925257938 100644

drivers/hwmon/sht3x.c static int sht3x_probe(struct i2c_client *client, · line 728

Quoted 8 lines
drivers/hwmon/sht3x.c +1 -1
@@ -722,7 +722,7 @@ static int sht3x_probe(struct i2c_client *client,
      data->setup.blocking_io = false;
      data->setup.high_precision = true;
      data->mode = 0;
-     data->last_update = 0;
+     data->last_update = jiffies;
      data->client = client;
      crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
 

Both look equally wrong to me. With your proposal, accessing the sysfs attributes right after loading the driver will not trigger a reading.

Quoted 3 lines

In order to guarantee that the first access will trigger a reading, data->last_update should be initialized to jiffies - msecs_to_jiffies(2000) (the maximum interval value.)

Ok that is fine. Rather do jiffies + (2 * HZ)

Quoted 5 lines

--
Jean Delvare
SUSE L3 Support

#3 lore raw

Hi Matt,

Quoted 12 lines · Matt Ranostay
Quoted 7 lines · Jean Delvare
Quoted 3 lines · Matt Ranostay

Handling the wraparound requires the data->last_update to be set to an initial jiffies value. Otherwise you can start in a state where the sensor will never request a reading.

I can't see how. As I read the code, in the worst case, readings can be blocked for interval_ms (2 seconds maximum.)

On 64-bit systems this is never an issue because the jiffies counter will never wrap around.

Quoted 6 lines

But my system is a 32-bit ARM core, so the the kernel sets the initial value to 0xfffb6c20 so it will wrap around in 5 minutes to find buggy code.

So looking at time_after(0xfffb6c20, 0) will return false always till it finally rolls over.

I've always been confused by how jiffies wrapping is handled. So I tested it, and you are correct.

Quoted 24 lines
Quoted 24 lines
Quoted 12 lines
Cc Guenter Roeck <[email protected]>
Cc David Frey <[email protected]>
---
 drivers/hwmon/sht3x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/sht3x.c +0 -0
index 450645b6053d..05a925257938 100644

drivers/hwmon/sht3x.c static int sht3x_probe(struct i2c_client *client, · line 728

Quoted 8 lines
drivers/hwmon/sht3x.c +1 -1
@@ -722,7 +722,7 @@ static int sht3x_probe(struct i2c_client *client,
      data->setup.blocking_io = false;
      data->setup.high_precision = true;
      data->mode = 0;
-     data->last_update = 0;
+     data->last_update = jiffies;
      data->client = client;
      crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
 

Both look equally wrong to me. With your proposal, accessing the sysfs attributes right after loading the driver will not trigger a reading.

Quoted 5 lines
Quoted 3 lines

In order to guarantee that the first access will trigger a reading, data->last_update should be initialized to jiffies - msecs_to_jiffies(2000) (the maximum interval value.)

Ok that is fine. Rather do jiffies + (2 * HZ)

2 * HZ is fine. But it's minus, not plus. You want to initialize the last update time to 2 seconds BEFORE the driver is being loaded, so that the current time is at least 2 seconds AFTER the (fake) last update at first access. And you should subtract another jiffy to be on the safe side.

Can you please send an updated patch?

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#4 lore raw
Quoted 19 lines · Jean Delvare

Hi Matt,

Quoted 16 lines · Matt Ranostay
Quoted 7 lines · Jean Delvare
Quoted 3 lines · Matt Ranostay

Handling the wraparound requires the data->last_update to be set to an initial jiffies value. Otherwise you can start in a state where the sensor will never request a reading.

I can't see how. As I read the code, in the worst case, readings can be blocked for interval_ms (2 seconds maximum.)

On 64-bit systems this is never an issue because the jiffies counter will never wrap around.

But my system is a 32-bit ARM core, so the the kernel sets the initial value to 0xfffb6c20 so it will wrap around in 5 minutes to find buggy code.

Quoted 6 lines
Quoted 2 lines

So looking at time_after(0xfffb6c20, 0) will return false always till it finally rolls over.

I've always been confused by how jiffies wrapping is handled. So I tested it, and you are correct.

Yeah but of course on 64-bit systems most just use the lower 32-bits of the jiffies counter.

But since that could rollover, and the unlikely event no sensor data was read for ~57 days you could be in the same state that couldn't get a data sample ....

Not sure if we want to check this edge case.. and also this effects a few iio drivers (mostly mine :)) if we really care about this.

Quoted 30 lines
Quoted 24 lines
Quoted 24 lines
Quoted 12 lines
Cc Guenter Roeck <[email protected]>
Cc David Frey <[email protected]>
---
 drivers/hwmon/sht3x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/sht3x.c +0 -0
index 450645b6053d..05a925257938 100644

drivers/hwmon/sht3x.c static int sht3x_probe(struct i2c_client *client, · line 728

Quoted 8 lines
drivers/hwmon/sht3x.c +1 -1
@@ -722,7 +722,7 @@ static int sht3x_probe(struct i2c_client *client,
      data->setup.blocking_io = false;
      data->setup.high_precision = true;
      data->mode = 0;
-     data->last_update = 0;
+     data->last_update = jiffies;
      data->client = client;
      crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
 

Both look equally wrong to me. With your proposal, accessing the sysfs attributes right after loading the driver will not trigger a reading.

Quoted 5 lines
Quoted 3 lines

In order to guarantee that the first access will trigger a reading, data->last_update should be initialized to jiffies - msecs_to_jiffies(2000) (the maximum interval value.)

Ok that is fine. Rather do jiffies + (2 * HZ)

Quoted 7 lines

2 * HZ is fine. But it's minus, not plus. You want to initialize the last update time to 2 seconds BEFORE the driver is being loaded, so that the current time is at least 2 seconds AFTER the (fake) last update at first access. And you should subtract another jiffy to be on the safe side.

Can you please send an updated patch?

Sure will do.

Quoted 4 lines

--
Jean Delvare
SUSE L3 Support

#5 lore raw

Hi Matt,

Quoted 18 lines · Matt Ranostay
Quoted 17 lines · Jean Delvare
Quoted 16 lines · Matt Ranostay
Quoted 7 lines · Jean Delvare
Quoted 3 lines · Matt Ranostay

Handling the wraparound requires the data->last_update to be set to an initial jiffies value. Otherwise you can start in a state where the sensor will never request a reading.

I can't see how. As I read the code, in the worst case, readings can be blocked for interval_ms (2 seconds maximum.)

On 64-bit systems this is never an issue because the jiffies counter will never wrap around.

But my system is a 32-bit ARM core, so the the kernel sets the initial value to 0xfffb6c20 so it will wrap around in 5 minutes to find buggy code.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

So looking at time_after(0xfffb6c20, 0) will return false always till it finally rolls over.

I've always been confused by how jiffies wrapping is handled. So I tested it, and you are correct.

Yeah but of course on 64-bit systems most just use the lower 32-bits of the jiffies counter.

Actually it seems the jiffies are initialized the same on 32-bit and 64-bit systems, so on 64-bit systems you'll get 33-bit values after 5 minutes, and 34-bit values after about 100 days (if HZ=1000.) But you'll never realistically wrap, you are right.

Quoted 3 lines

But since that could rollover, and the unlikely event no sensor data was read for ~57 days you could be in the same state that couldn't get a data sample ....

At HZ=1000 the jiffies wrap after 49 days, IIRC, not 57. Not that it matters ;-)

Quoted 2 lines

Not sure if we want to check this edge case.. and also this effects a few iio drivers (mostly mine :)) if we really care about this.

All other hwmon drivers have the same problem. I can't think of a cheap way to solve this problem, nor do I think we need to. If you monitor your system, you are supposed to poll the device on a regular basis, not once every 49 days.

Signature · 2 lines
Jean Delvare
SUSE L3 Support