lib: Ensure EWMA does not store wrong intermediate values
authorMichael Dalton <mwdalton@google.com>
Fri, 17 Jan 2014 06:23:29 +0000 (22:23 -0800)
committerDavid S. Miller <davem@davemloft.net>
Fri, 17 Jan 2014 07:46:06 +0000 (23:46 -0800)
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>
lib/average.c

index 99a67e662b3c6b747e2ccb81f576e8e5ba040b6b..114d1beae0c785a358bf709dd6f9a1e7ee46ce1d 100644 (file)
@@ -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;