Commit 03144b58 authored by Michael Dalton's avatar Michael Dalton Committed by David S. Miller

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: default avatarEric Dumazet <eric.dumazet@gmail.com>
Acked-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Acked-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarMichael Dalton <mwdalton@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent a953be53
...@@ -53,8 +53,10 @@ EXPORT_SYMBOL(ewma_init); ...@@ -53,8 +53,10 @@ EXPORT_SYMBOL(ewma_init);
*/ */
struct ewma *ewma_add(struct ewma *avg, unsigned long val) struct ewma *ewma_add(struct ewma *avg, unsigned long val)
{ {
avg->internal = avg->internal ? unsigned long internal = ACCESS_ONCE(avg->internal);
(((avg->internal << avg->weight) - avg->internal) +
ACCESS_ONCE(avg->internal) = internal ?
(((internal << avg->weight) - internal) +
(val << avg->factor)) >> avg->weight : (val << avg->factor)) >> avg->weight :
(val << avg->factor); (val << avg->factor);
return avg; return avg;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment