Re: [PATCH] sched/vtime: Fix guest/system mis-accounting on task switch
From: Peter Zijlstra
Date: Thu Sep 26 2019 - 04:14:20 EST
On Wed, Sep 25, 2019 at 11:42:42PM +0200, Frederic Weisbecker wrote:
> vtime_account_system() assumes that the target task to account cputime
> to is always the current task. This is most often true indeed except on
> task switch where we call:
>
> vtime_common_task_switch(prev)
> vtime_account_system(prev)
>
> Here prev is the scheduling-out task where we account the cputime to. It
> doesn't match current that is already the scheduling-in task at this
> stage of the context switch.
>
> So we end up checking the wrong task flags to determine if we are
> accounting guest or system time to the previous task.
>
> As a result the wrong task is used to check if the target is running in
> guest mode. We may then spuriously account or leak either system or
> guest time on task switch.
>
> Fix this assumption and also turn vtime_guest_enter/exit() to use the
> task passed in parameter as well to avoid future similar issues.
>
> Fixes: 2a42eb9594a1 ("sched/cputime: Accumulate vtime on top of nsec clocksource")
> Signed-off-by: Frederic Weisbecker <frederic@xxxxxxxxxx>
> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> Cc: Rik van Riel <riel@xxxxxxxxxx>
> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Cc: Wanpeng Li <wanpengli@xxxxxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Thanks!