Re: [REGRESSION] [Linux 3.2] top/htop and all other CPU usage

Michal Hocko
2011-12-02T10:39:24Z

On Thu 01-12-11 15:07:49, Michal Hocko wrote:
[...]
> While implementation is not race free (we better not use locks in that
> path...) so we might race:
>
> E.g.
>
> CPU1 CPU2
> now = ktime_get
> tick_nohz_start_idle
> ts->idle_entrytime = now;
> if (ts->idle_active)
> ts->idle_active = 1
> [...]
> return idle_sleeptime
>
> But this is OK because sleeptime will be more or less accurate. We just
> skip few ticks.
>
> It would be worse if we had a race like:
> CPU1 CPU2
> now = ktime_get
> tick_nohz_start_idle
> now = ktime_get
> update_ts_time_stats()
> ts->idle_entrytime = now;
> ts->idle_active = 1
> if (ts->idle_active)
> delta = ktime_sub(now, idle_entrytime)
> ktime_add(idle_sleeptime, delta)
>
> In this case we might get an overflow from ktime_sub but AFAIU the
> ktime_* magic the overflow should cause to get smaller idle_sleeptime
> in the end after ktime_add (we do not add a small number but rather
> subtract it), right?

Scratch that. Dunno why but I thought that ktime_t has unsigned values
but it is apparently not true (tv64 is s64). Anyway the above races should
be safe.