Skip to content

Commit f1dfdab

Browse files
ickleingomolnar
authored andcommitted
sched/vtime: Prevent unstable evaluation of WARN(vtime->state)
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
1 parent 1b10d38 commit f1dfdab

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
@@ -909,8 +909,10 @@ void task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
909909
} while (read_seqcount_retry(&vtime->seqcount, seq));
910910
}
911911

912-
static int vtime_state_check(struct vtime *vtime, int cpu)
912+
static int vtime_state_fetch(struct vtime *vtime, int cpu)
913913
{
914+
int state = READ_ONCE(vtime->state);
915+
914916
/*
915917
* We raced against a context switch, fetch the
916918
* kcpustat task again.
@@ -927,10 +929,10 @@ static int vtime_state_check(struct vtime *vtime, int cpu)
927929
*
928930
* Case 1) is ok but 2) is not. So wait for a safe VTIME state.
929931
*/
930-
if (vtime->state == VTIME_INACTIVE)
932+
if (state == VTIME_INACTIVE)
931933
return -EAGAIN;
932934

933-
return 0;
935+
return state;
934936
}
935937

936938
static u64 kcpustat_user_vtime(struct vtime *vtime)
@@ -949,14 +951,15 @@ static int kcpustat_field_vtime(u64 *cpustat,
949951
{
950952
struct vtime *vtime = &tsk->vtime;
951953
unsigned int seq;
952-
int err;
953954

954955
do {
956+
int state;
957+
955958
seq = read_seqcount_begin(&vtime->seqcount);
956959

957-
err = vtime_state_check(vtime, cpu);
958-
if (err < 0)
959-
return err;
960+
state = vtime_state_fetch(vtime, cpu);
961+
if (state < 0)
962+
return state;
960963

961964
*val = cpustat[usage];
962965

@@ -969,7 +972,7 @@ static int kcpustat_field_vtime(u64 *cpustat,
969972
*/
970973
switch (usage) {
971974
case CPUTIME_SYSTEM:
972-
if (vtime->state == VTIME_SYS)
975+
if (state == VTIME_SYS)
973976
*val += vtime->stime + vtime_delta(vtime);
974977
break;
975978
case CPUTIME_USER:
@@ -981,11 +984,11 @@ static int kcpustat_field_vtime(u64 *cpustat,
981984
*val += kcpustat_user_vtime(vtime);
982985
break;
983986
case CPUTIME_GUEST:
984-
if (vtime->state == VTIME_GUEST && task_nice(tsk) <= 0)
987+
if (state == VTIME_GUEST && task_nice(tsk) <= 0)
985988
*val += vtime->gtime + vtime_delta(vtime);
986989
break;
987990
case CPUTIME_GUEST_NICE:
988-
if (vtime->state == VTIME_GUEST && task_nice(tsk) > 0)
991+
if (state == VTIME_GUEST && task_nice(tsk) > 0)
989992
*val += vtime->gtime + vtime_delta(vtime);
990993
break;
991994
default:
@@ -1036,23 +1039,23 @@ static int kcpustat_cpu_fetch_vtime(struct kernel_cpustat *dst,
10361039
{
10371040
struct vtime *vtime = &tsk->vtime;
10381041
unsigned int seq;
1039-
int err;
10401042

10411043
do {
10421044
u64 *cpustat;
10431045
u64 delta;
1046+
int state;
10441047

10451048
seq = read_seqcount_begin(&vtime->seqcount);
10461049

1047-
err = vtime_state_check(vtime, cpu);
1048-
if (err < 0)
1049-
return err;
1050+
state = vtime_state_fetch(vtime, cpu);
1051+
if (state < 0)
1052+
return state;
10501053

10511054
*dst = *src;
10521055
cpustat = dst->cpustat;
10531056

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

10581061
delta = vtime_delta(vtime);
@@ -1061,15 +1064,15 @@ static int kcpustat_cpu_fetch_vtime(struct kernel_cpustat *dst,
10611064
* Task runs either in user (including guest) or kernel space,
10621065
* add pending nohz time to the right place.
10631066
*/
1064-
if (vtime->state == VTIME_SYS) {
1067+
if (state == VTIME_SYS) {
10651068
cpustat[CPUTIME_SYSTEM] += vtime->stime + delta;
1066-
} else if (vtime->state == VTIME_USER) {
1069+
} else if (state == VTIME_USER) {
10671070
if (task_nice(tsk) > 0)
10681071
cpustat[CPUTIME_NICE] += vtime->utime + delta;
10691072
else
10701073
cpustat[CPUTIME_USER] += vtime->utime + delta;
10711074
} else {
1072-
WARN_ON_ONCE(vtime->state != VTIME_GUEST);
1075+
WARN_ON_ONCE(state != VTIME_GUEST);
10731076
if (task_nice(tsk) > 0) {
10741077
cpustat[CPUTIME_GUEST_NICE] += vtime->gtime + delta;
10751078
cpustat[CPUTIME_NICE] += vtime->gtime + delta;
@@ -1080,7 +1083,7 @@ static int kcpustat_cpu_fetch_vtime(struct kernel_cpustat *dst,
10801083
}
10811084
} while (read_seqcount_retry(&vtime->seqcount, seq));
10821085

1083-
return err;
1086+
return 0;
10841087
}
10851088

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

0 commit comments

Comments
 (0)