Commit f16b34aa authored by Peter Zijlstra's avatar Peter Zijlstra Committed by Linus Torvalds

lib: proportion: fix underflow in prop_norm_percpu()

Zhe Jiang noticed that its possible to underflow pl->events in
prop_norm_percpu() when the value returned by percpu_counter_read() is less
than the error on that read and the period delay > 1.  In that case half might
not trigger the batch increment and the value will be identical on the next
iteration, causing the same half to be subtracted again and again.

Fix this by rewriting the division as a single subtraction instead of a
subtraction loop and using percpu_counter_sum() when the value returned by
percpu_counter_read() is smaller than the error.

The latter is still needed if we want pl->events to shrink properly in the
error region.

[akpm@linux-foundation.org: cleanups]
Signed-off-by: default avatarPeter Zijlstra <a.p.zijlstra@chello.nl>
Reviewed-by: default avatarJiang Zhe <zhe.jiang@intel.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent cc295d0e
...@@ -190,6 +190,8 @@ prop_adjust_shift(int *pl_shift, unsigned long *pl_period, int new_shift) ...@@ -190,6 +190,8 @@ prop_adjust_shift(int *pl_shift, unsigned long *pl_period, int new_shift)
* PERCPU * PERCPU
*/ */
#define PROP_BATCH (8*(1+ilog2(nr_cpu_ids)))
int prop_local_init_percpu(struct prop_local_percpu *pl) int prop_local_init_percpu(struct prop_local_percpu *pl)
{ {
spin_lock_init(&pl->lock); spin_lock_init(&pl->lock);
...@@ -230,31 +232,24 @@ void prop_norm_percpu(struct prop_global *pg, struct prop_local_percpu *pl) ...@@ -230,31 +232,24 @@ void prop_norm_percpu(struct prop_global *pg, struct prop_local_percpu *pl)
spin_lock_irqsave(&pl->lock, flags); spin_lock_irqsave(&pl->lock, flags);
prop_adjust_shift(&pl->shift, &pl->period, pg->shift); prop_adjust_shift(&pl->shift, &pl->period, pg->shift);
/* /*
* For each missed period, we half the local counter. * For each missed period, we half the local counter.
* basically: * basically:
* pl->events >> (global_period - pl->period); * pl->events >> (global_period - pl->period);
*
* but since the distributed nature of percpu counters make division
* rather hard, use a regular subtraction loop. This is safe, because
* the events will only every be incremented, hence the subtraction
* can never result in a negative number.
*/ */
while (pl->period != global_period) { period = (global_period - pl->period) >> (pg->shift - 1);
unsigned long val = percpu_counter_read(&pl->events); if (period < BITS_PER_LONG) {
unsigned long half = (val + 1) >> 1; s64 val = percpu_counter_read(&pl->events);
/* if (val < (nr_cpu_ids * PROP_BATCH))
* Half of zero won't be much less, break out. val = percpu_counter_sum(&pl->events);
* This limits the loop to shift iterations, even
* if we missed a million. __percpu_counter_add(&pl->events, -val + (val >> period),
*/ PROP_BATCH);
if (!val) } else
break; percpu_counter_set(&pl->events, 0);
percpu_counter_add(&pl->events, -half);
pl->period += period;
}
pl->period = global_period; pl->period = global_period;
spin_unlock_irqrestore(&pl->lock, flags); spin_unlock_irqrestore(&pl->lock, flags);
} }
...@@ -267,7 +262,7 @@ void __prop_inc_percpu(struct prop_descriptor *pd, struct prop_local_percpu *pl) ...@@ -267,7 +262,7 @@ void __prop_inc_percpu(struct prop_descriptor *pd, struct prop_local_percpu *pl)
struct prop_global *pg = prop_get_global(pd); struct prop_global *pg = prop_get_global(pd);
prop_norm_percpu(pg, pl); prop_norm_percpu(pg, pl);
percpu_counter_add(&pl->events, 1); __percpu_counter_add(&pl->events, 1, PROP_BATCH);
percpu_counter_add(&pg->events, 1); percpu_counter_add(&pg->events, 1);
prop_put_global(pd, pg); prop_put_global(pd, pg);
} }
......
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