Skip to contentSkip to conversation

hwmon: pwm-fan: Fix RPM calculation

PATCH Superseded Paul Barker 2 participants 3 messages Nov 5, 2020 – Nov 8, 2020 Hardware monitoring

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hwmon: pwm-fan: Fix RPM calculation

To convert the number of pulses counted into an RPM estimation, we need to divide by the width of our measurement interval instead of multiplying by it.

We also don't need to do 64-bit division, with 32-bits we can handle a fan running at over 4 million RPM.

As the sample_timer() function is called once per second, delta will usually be just over 1000 and should never be zero, avoiding the risk of a divide by zero exception.

---
 drivers/hwmon/pwm-fan.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/hwmon/pwm-fan.c +3 -4
index bdba2143021a..2f2950629892 100644
@@ -54,14 +54,13 @@ static irqreturn_t pulse_handler(int irq, void *dev_id)
 static void sample_timer(struct timer_list *t)
 {
 	struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
+	unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
 	int pulses;
-	u64 tmp;
 
 	pulses = atomic_read(&ctx->pulses);
 	atomic_sub(pulses, &ctx->pulses);
-	tmp = (u64)pulses * ktime_ms_delta(ktime_get(), ctx->sample_start) * 60;
-	do_div(tmp, ctx->pulses_per_revolution * 1000);
-	ctx->rpm = tmp;
+	ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
+		(ctx->pulses_per_revolution * delta);
 
 	ctx->sample_start = ktime_get();
 	mod_timer(&ctx->rpm_timer, jiffies + HZ);
Signature · 1 line
2.29.2
maintainer
#1 lore raw
Quoted 25 lines · Paul Barker

To convert the number of pulses counted into an RPM estimation, we need to divide by the width of our measurement interval instead of multiplying by it.

We also don't need to do 64-bit division, with 32-bits we can handle a fan running at over 4 million RPM.

As the sample_timer() function is called once per second, delta will usually be just over 1000 and should never be zero, avoiding the risk of a divide by zero exception.

---
 drivers/hwmon/pwm-fan.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/hwmon/pwm-fan.c +1 -0
index bdba2143021a..2f2950629892 100644
@@ -54,14 +54,13 @@ static irqreturn_t pulse_handler(int irq, void *dev_id)
 static void sample_timer(struct timer_list *t)
 {
 	struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
+	unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);

drivers/hwmon/pwm-fan.c static irqreturn_t pulse_handler(int irq, void *dev_id) · line 63

Quoted 10 lines
drivers/hwmon/pwm-fan.c +2 -4
 	int pulses;
-	u64 tmp;
 
 	pulses = atomic_read(&ctx->pulses);
 	atomic_sub(pulses, &ctx->pulses);
-	tmp = (u64)pulses * ktime_ms_delta(ktime_get(), ctx->sample_start) * 60;
-	do_div(tmp, ctx->pulses_per_revolution * 1000);
-	ctx->rpm = tmp;
+	ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
+		(ctx->pulses_per_revolution * delta);

delta can, at least in theory, be 0. While that is quite unlikely to happen, the code should handle this situation.

Quoted 3 lines
drivers/hwmon/pwm-fan.c +0 -0
 	ctx->sample_start = ktime_get();
 	mod_timer(&ctx->rpm_timer, jiffies + HZ);
#2 lore raw
Quoted 30 lines · Guenter Roeck
Quoted 28 lines · Paul Barker

To convert the number of pulses counted into an RPM estimation, we need to divide by the width of our measurement interval instead of multiplying by it.

We also don't need to do 64-bit division, with 32-bits we can handle a fan running at over 4 million RPM.

As the sample_timer() function is called once per second, delta will usually be just over 1000 and should never be zero, avoiding the risk of a divide by zero exception.

---
 drivers/hwmon/pwm-fan.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/hwmon/pwm-fan.c +1 -1
index bdba2143021a..2f2950629892 100644
@@ -54,14 +54,13 @@ static irqreturn_t pulse_handler(int irq, void *dev_id)
 static void sample_timer(struct timer_list *t)
 {
      struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
+     unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
      int pulses;
-     u64 tmp;
 
Quoted 10 lines

drivers/hwmon/pwm-fan.c static irqreturn_t pulse_handler(int irq, void *dev_id) · line 63

Quoted 7 lines
drivers/hwmon/pwm-fan.c +2 -3
      pulses = atomic_read(&ctx->pulses);
      atomic_sub(pulses, &ctx->pulses);
-     tmp = (u64)pulses * ktime_ms_delta(ktime_get(), ctx->sample_start) * 60;
-     do_div(tmp, ctx->pulses_per_revolution * 1000);
-     ctx->rpm = tmp;
+     ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
+             (ctx->pulses_per_revolution * delta);

delta can, at least in theory, be 0. While that is quite unlikely to happen, the code should handle this situation.

I'll send a v3 which bails out if delta=0.

Thanks,

Signature · 2 lines
Paul Barker
Konsulko Group