summaryrefslogtreecommitdiffstats
path: root/drivers/staging/greybus/loopback.c
diff options
context:
space:
mode:
authorAlexandre Bailon <abailon@baylibre.com>2016-02-25 18:19:16 +0100
committerGreg Kroah-Hartman <gregkh@google.com>2016-02-25 17:05:31 -0800
commitfb37f137b78f186275b15e8ce1e5a26b51a6e6ad (patch)
tree11812af9c747f7eed2c013ed6825a4b5802132db /drivers/staging/greybus/loopback.c
parentab81bb9c68d9fb899b79fa622fedf294e66fa53d (diff)
downloadlinux-fb37f137b78f186275b15e8ce1e5a26b51a6e6ad.tar.bz2
greybus: loopback: Fix averaging
Currently, we are adding 0.5 to the average to round the average. But we are using the remainder to calculate the decimal, so we do not need to round the average. In addition, use a u64 type for the remainder to avoid overflow that might happen when stats->sum value is too big, usually for requests per seconds and the throughput. Signed-off-by: Alexandre Bailon <abailon@baylibre.com> Reviewed-by: Johan Hovold <johan@hovoldconsulting.com> Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
Diffstat (limited to 'drivers/staging/greybus/loopback.c')
-rw-r--r--drivers/staging/greybus/loopback.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/drivers/staging/greybus/loopback.c b/drivers/staging/greybus/loopback.c
index 85d3e35e6a5f..9f0da577d842 100644
--- a/drivers/staging/greybus/loopback.c
+++ b/drivers/staging/greybus/loopback.c
@@ -157,14 +157,15 @@ static ssize_t name##_avg_show(struct device *dev, \
{ \
struct gb_loopback_stats *stats; \
struct gb_loopback *gb; \
- u64 avg; \
- u32 count, rem; \
+ u64 avg, rem; \
+ u32 count; \
gb = dev_get_drvdata(dev); \
stats = &gb->name; \
count = stats->count ? stats->count : 1; \
- avg = stats->sum + count / 2; /* round closest */ \
+ avg = stats->sum; \
rem = do_div(avg, count); \
- return sprintf(buf, "%llu.%06u\n", avg, 1000000 * rem / count); \
+ rem = 1000000 * rem / count; \
+ return sprintf(buf, "%llu.%06u\n", avg, (u32)rem); \
} \
static DEVICE_ATTR_RO(name##_avg)