summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMichael Dalton <mwdalton@google.com>2014-01-17 07:23:29 +0100
committerDavid S. Miller <davem@davemloft.net>2014-01-17 08:46:06 +0100
commit03144b5869d2921dafbea477246aeb5d4eb5fc38 (patch)
treec0326022d8ccf711afb495c9c556447dfcf845b7 /lib
parentnet-sysfs: add support for device-specific rx queue sysfs attributes (diff)
downloadlinux-03144b5869d2921dafbea477246aeb5d4eb5fc38.tar.xz
linux-03144b5869d2921dafbea477246aeb5d4eb5fc38.zip
lib: Ensure EWMA does not store wrong intermediate values
To ensure ewma_read() without a lock returns a valid but possibly out of date average, modify ewma_add() by using ACCESS_ONCE to prevent intermediate wrong values from being written to avg->internal. Suggested-by: Eric Dumazet <eric.dumazet@gmail.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Acked-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Michael Dalton <mwdalton@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/average.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/lib/average.c b/lib/average.c
index 99a67e662b3c..114d1beae0c7 100644
--- a/lib/average.c
+++ b/lib/average.c
@@ -53,8 +53,10 @@ EXPORT_SYMBOL(ewma_init);
*/
struct ewma *ewma_add(struct ewma *avg, unsigned long val)
{
- avg->internal = avg->internal ?
- (((avg->internal << avg->weight) - avg->internal) +
+ unsigned long internal = ACCESS_ONCE(avg->internal);
+
+ ACCESS_ONCE(avg->internal) = internal ?
+ (((internal << avg->weight) - internal) +
(val << avg->factor)) >> avg->weight :
(val << avg->factor);
return avg;