Skip to content

Commit

Permalink
lib: Ensure EWMA does not store wrong intermediate values
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Michael Dalton authored and David S. Miller committed Jan 17, 2014
1 parent a953be5 commit 03144b5
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/average.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 03144b5

Please sign in to comment.