Re: [PATCH] sched/cputime: Fix using smp_processor_id() in preemptible

From: Wanpeng Li
Date: Fri Jul 07 2017 - 04:44:11 EST


2017-07-07 16:11 GMT+08:00 Peter Zijlstra <peterz@xxxxxxxxxxxxx>:
> On Thu, Jul 06, 2017 at 08:18:41PM -0700, Wanpeng Li wrote:
>> --- a/kernel/sched/cputime.c
>> +++ b/kernel/sched/cputime.c
>> @@ -683,7 +683,7 @@ static u64 vtime_delta(struct vtime *vtime)
>> {
>> unsigned long long clock;
>>
>> - clock = sched_clock_cpu(smp_processor_id());
>> + clock = sched_clock();
>> if (clock < vtime->starttime)
>> return 0;
>>
>
> This is broken. sched_clock_cpu() and sched_clock() do not have the
> exact same value :-/
>
> So this ends up doing:
>
> vtime->starttime = sched_clock_cpu(cpu);
>
> /* ... */
>
> clock = sched_clock();
>
> delta = clock - vtime->starttime;
>
>
> And you compute a delta using two different clocks.

How about adding preempt_disable/enable() pair around vtime_delta() in
task_cputime()?

Regards,
Wanpeng Li