Skip to content

Commit b465366

Browse files
icklegregkh
authored andcommitted
sched/vtime: Prevent unstable evaluation of WARN(vtime->state)
[ Upstream commit f1dfdab ] As the vtime is sampled under loose seqcount protection by kcpustat, the vtime fields may change as the code flows. Where logic dictates a field has a static value, use a READ_ONCE. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Signed-off-by: Frederic Weisbecker <frederic@kernel.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Fixes: 74722bb ("sched/vtime: Bring up complete kcpustat accessor") Link: https://lkml.kernel.org/r/20200123180849.28486-1-frederic@kernel.org Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent ba4e1d0 commit b465366

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

‎kernel/sched/cputime.c‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,10 @@ void task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
912912
} while (read_seqcount_retry(&vtime->seqcount, seq));
913913
}
914914

915-
static int vtime_state_check(struct vtime *vtime, int cpu)
915+
static int vtime_state_fetch(struct vtime *vtime, int cpu)
916916
{
917+
int state = READ_ONCE(vtime->state);
918+
917919
/*
918920
* We raced against a context switch, fetch the
919921
* kcpustat task again.
@@ -930,10 +932,10 @@ static int vtime_state_check(struct vtime *vtime, int cpu)
930932
*
931933
* Case 1) is ok but 2) is not. So wait for a safe VTIME state.
932934
*/
933-
if (vtime->state == VTIME_INACTIVE)
935+
if (state == VTIME_INACTIVE)
934936
return -EAGAIN;
935937

936-
return 0;
938+
return state;
937939
}
938940

939941
static u64 kcpustat_user_vtime(struct vtime *vtime)
@@ -952,14 +954,15 @@ static int kcpustat_field_vtime(u64 *cpustat,
952954
{
953955
struct vtime *vtime = &tsk->vtime;
954956
unsigned int seq;
955-
int err;
956957

957958
do {
959+
int state;
960+
958961
seq = read_seqcount_begin(&vtime->seqcount);
959962

960-
err = vtime_state_check(vtime, cpu);
961-
if (err < 0)
962-
return err;
963+
state = vtime_state_fetch(vtime, cpu);
964+
if (state < 0)
965+
return state;
963966

964967
*val = cpustat[usage];
965968

@@ -972,7 +975,7 @@ static int kcpustat_field_vtime(u64 *cpustat,
972975
*/
973976
switch (usage) {
974977
case CPUTIME_SYSTEM:
975-
if (vtime->state == VTIME_SYS)
978+
if (state == VTIME_SYS)
976979
*val += vtime->stime + vtime_delta(vtime);
977980
break;
978981
case CPUTIME_USER:
@@ -984,11 +987,11 @@ static int kcpustat_field_vtime(u64 *cpustat,
984987
*val += kcpustat_user_vtime(vtime);
985988
break;
986989
case CPUTIME_GUEST:
987-
if (vtime->state == VTIME_GUEST && task_nice(tsk) <= 0)
990+
if (state == VTIME_GUEST && task_nice(tsk) <= 0)
988991
*val += vtime->gtime + vtime_delta(vtime);
989992
break;
990993
case CPUTIME_GUEST_NICE:
991-
if (vtime->state == VTIME_GUEST && task_nice(tsk) > 0)
994+
if (state == VTIME_GUEST && task_nice(tsk) > 0)
992995
*val += vtime->gtime + vtime_delta(vtime);
993996
break;
994997
default:
@@ -1039,23 +1042,23 @@ static int kcpustat_cpu_fetch_vtime(struct kernel_cpustat *dst,
10391042
{
10401043
struct vtime *vtime = &tsk->vtime;
10411044
unsigned int seq;
1042-
int err;
10431045

10441046
do {
10451047
u64 *cpustat;
10461048
u64 delta;
1049+
int state;
10471050

10481051
seq = read_seqcount_begin(&vtime->seqcount);
10491052

1050-
err = vtime_state_check(vtime, cpu);
1051-
if (err < 0)
1052-
return err;
1053+
state = vtime_state_fetch(vtime, cpu);
1054+
if (state < 0)
1055+
return state;
10531056

10541057
*dst = *src;
10551058
cpustat = dst->cpustat;
10561059

10571060
/* Task is sleeping, dead or idle, nothing to add */
1058-
if (vtime->state < VTIME_SYS)
1061+
if (state < VTIME_SYS)
10591062
continue;
10601063

10611064
delta = vtime_delta(vtime);
@@ -1064,15 +1067,15 @@ static int kcpustat_cpu_fetch_vtime(struct kernel_cpustat *dst,
10641067
* Task runs either in user (including guest) or kernel space,
10651068
* add pending nohz time to the right place.
10661069
*/
1067-
if (vtime->state == VTIME_SYS) {
1070+
if (state == VTIME_SYS) {
10681071
cpustat[CPUTIME_SYSTEM] += vtime->stime + delta;
1069-
} else if (vtime->state == VTIME_USER) {
1072+
} else if (state == VTIME_USER) {
10701073
if (task_nice(tsk) > 0)
10711074
cpustat[CPUTIME_NICE] += vtime->utime + delta;
10721075
else
10731076
cpustat[CPUTIME_USER] += vtime->utime + delta;
10741077
} else {
1075-
WARN_ON_ONCE(vtime->state != VTIME_GUEST);
1078+
WARN_ON_ONCE(state != VTIME_GUEST);
10761079
if (task_nice(tsk) > 0) {
10771080
cpustat[CPUTIME_GUEST_NICE] += vtime->gtime + delta;
10781081
cpustat[CPUTIME_NICE] += vtime->gtime + delta;
@@ -1083,7 +1086,7 @@ static int kcpustat_cpu_fetch_vtime(struct kernel_cpustat *dst,
10831086
}
10841087
} while (read_seqcount_retry(&vtime->seqcount, seq));
10851088

1086-
return err;
1089+
return 0;
10871090
}
10881091

10891092
void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)

0 commit comments

Comments
 (0)