Skip to content

Commit 68e7a4d

Browse files
Frederic WeisbeckerIngo Molnar
authored andcommitted
sched/vtime: Fix guest/system mis-accounting on task switch
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. Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Rik van Riel <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Wanpeng Li <[email protected]> Fixes: 2a42eb9 ("sched/cputime: Accumulate vtime on top of nsec clocksource") Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 4929a4e commit 68e7a4d

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

kernel/sched/cputime.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ void vtime_account_system(struct task_struct *tsk)
740740

741741
write_seqcount_begin(&vtime->seqcount);
742742
/* We might have scheduled out from guest path */
743-
if (current->flags & PF_VCPU)
743+
if (tsk->flags & PF_VCPU)
744744
vtime_account_guest(tsk, vtime);
745745
else
746746
__vtime_account_system(tsk, vtime);
@@ -783,7 +783,7 @@ void vtime_guest_enter(struct task_struct *tsk)
783783
*/
784784
write_seqcount_begin(&vtime->seqcount);
785785
__vtime_account_system(tsk, vtime);
786-
current->flags |= PF_VCPU;
786+
tsk->flags |= PF_VCPU;
787787
write_seqcount_end(&vtime->seqcount);
788788
}
789789
EXPORT_SYMBOL_GPL(vtime_guest_enter);
@@ -794,7 +794,7 @@ void vtime_guest_exit(struct task_struct *tsk)
794794

795795
write_seqcount_begin(&vtime->seqcount);
796796
vtime_account_guest(tsk, vtime);
797-
current->flags &= ~PF_VCPU;
797+
tsk->flags &= ~PF_VCPU;
798798
write_seqcount_end(&vtime->seqcount);
799799
}
800800
EXPORT_SYMBOL_GPL(vtime_guest_exit);

0 commit comments

Comments
 (0)