Skip to content

Commit cb0e52b

Browse files
brianc118Peter Zijlstra
authored andcommitted
psi: Fix PSI_MEM_FULL state when tasks are in memstall and doing reclaim
We've noticed cases where tasks in a cgroup are stalled on memory but there is little memory FULL pressure since tasks stay on the runqueue in reclaim. A simple example involves a single threaded program that keeps leaking and touching large amounts of memory. It runs in a cgroup with swap enabled, memory.high set at 10M and cpu.max ratio set at 5%. Though there is significant CPU pressure and memory SOME, there is barely any memory FULL since the task enters reclaim and stays on the runqueue. However, this memory-bound task is effectively stalled on memory and we expect memory FULL to match memory SOME in this scenario. The code is confused about memstall && running, thinking there is a stalled task and a productive task when there's only one task: a reclaimer that's counted as both. To fix this, we redefine the condition for PSI_MEM_FULL to check that all running tasks are in an active memstall instead of checking that there are no running tasks. case PSI_MEM_FULL: - return unlikely(tasks[NR_MEMSTALL] && !tasks[NR_RUNNING]); + return unlikely(tasks[NR_MEMSTALL] && + tasks[NR_RUNNING] == tasks[NR_MEMSTALL_RUNNING]); This will capture reclaimers. It will also capture tasks that called psi_memstall_enter() and are about to sleep, but this should be negligible noise. Signed-off-by: Brian Chen <brianchen118@gmail.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Johannes Weiner <hannes@cmpxchg.org> Link: https://lore.kernel.org/r/20211110213312.310243-1-brianchen118@gmail.com
1 parent 4feee7d commit cb0e52b

3 files changed

Lines changed: 44 additions & 19 deletions

File tree

‎include/linux/psi_types.h‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,25 @@ enum psi_task_count {
2222
* don't have to special case any state tracking for it.
2323
*/
2424
NR_ONCPU,
25-
NR_PSI_TASK_COUNTS = 4,
25+
/*
26+
* For IO and CPU stalls the presence of running/oncpu tasks
27+
* in the domain means a partial rather than a full stall.
28+
* For memory it's not so simple because of page reclaimers:
29+
* they are running/oncpu while representing a stall. To tell
30+
* whether a domain has productivity left or not, we need to
31+
* distinguish between regular running (i.e. productive)
32+
* threads and memstall ones.
33+
*/
34+
NR_MEMSTALL_RUNNING,
35+
NR_PSI_TASK_COUNTS = 5,
2636
};
2737

2838
/* Task state bitmasks */
2939
#define TSK_IOWAIT (1 << NR_IOWAIT)
3040
#define TSK_MEMSTALL (1 << NR_MEMSTALL)
3141
#define TSK_RUNNING (1 << NR_RUNNING)
3242
#define TSK_ONCPU (1 << NR_ONCPU)
43+
#define TSK_MEMSTALL_RUNNING (1 << NR_MEMSTALL_RUNNING)
3344

3445
/* Resources that workloads could be stalled on */
3546
enum psi_res {

‎kernel/sched/psi.c‎

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@
3535
* delayed on that resource such that nobody is advancing and the CPU
3636
* goes idle. This leaves both workload and CPU unproductive.
3737
*
38-
* Naturally, the FULL state doesn't exist for the CPU resource at the
39-
* system level, but exist at the cgroup level, means all non-idle tasks
40-
* in a cgroup are delayed on the CPU resource which used by others outside
41-
* of the cgroup or throttled by the cgroup cpu.max configuration.
42-
*
4338
* SOME = nr_delayed_tasks != 0
44-
* FULL = nr_delayed_tasks != 0 && nr_running_tasks == 0
39+
* FULL = nr_delayed_tasks != 0 && nr_productive_tasks == 0
40+
*
41+
* What it means for a task to be productive is defined differently
42+
* for each resource. For IO, productive means a running task. For
43+
* memory, productive means a running task that isn't a reclaimer. For
44+
* CPU, productive means an oncpu task.
45+
*
46+
* Naturally, the FULL state doesn't exist for the CPU resource at the
47+
* system level, but exist at the cgroup level. At the cgroup level,
48+
* FULL means all non-idle tasks in the cgroup are delayed on the CPU
49+
* resource which is being used by others outside of the cgroup or
50+
* throttled by the cgroup cpu.max configuration.
4551
*
4652
* The percentage of wallclock time spent in those compound stall
4753
* states gives pressure numbers between 0 and 100 for each resource,
@@ -82,13 +88,13 @@
8288
*
8389
* threads = min(nr_nonidle_tasks, nr_cpus)
8490
* SOME = min(nr_delayed_tasks / threads, 1)
85-
* FULL = (threads - min(nr_running_tasks, threads)) / threads
91+
* FULL = (threads - min(nr_productive_tasks, threads)) / threads
8692
*
8793
* For the 257 number crunchers on 256 CPUs, this yields:
8894
*
8995
* threads = min(257, 256)
9096
* SOME = min(1 / 256, 1) = 0.4%
91-
* FULL = (256 - min(257, 256)) / 256 = 0%
97+
* FULL = (256 - min(256, 256)) / 256 = 0%
9298
*
9399
* For the 1 out of 4 memory-delayed tasks, this yields:
94100
*
@@ -113,7 +119,7 @@
113119
* For each runqueue, we track:
114120
*
115121
* tSOME[cpu] = time(nr_delayed_tasks[cpu] != 0)
116-
* tFULL[cpu] = time(nr_delayed_tasks[cpu] && !nr_running_tasks[cpu])
122+
* tFULL[cpu] = time(nr_delayed_tasks[cpu] && !nr_productive_tasks[cpu])
117123
* tNONIDLE[cpu] = time(nr_nonidle_tasks[cpu] != 0)
118124
*
119125
* and then periodically aggregate:
@@ -234,7 +240,8 @@ static bool test_state(unsigned int *tasks, enum psi_states state)
234240
case PSI_MEM_SOME:
235241
return unlikely(tasks[NR_MEMSTALL]);
236242
case PSI_MEM_FULL:
237-
return unlikely(tasks[NR_MEMSTALL] && !tasks[NR_RUNNING]);
243+
return unlikely(tasks[NR_MEMSTALL] &&
244+
tasks[NR_RUNNING] == tasks[NR_MEMSTALL_RUNNING]);
238245
case PSI_CPU_SOME:
239246
return unlikely(tasks[NR_RUNNING] > tasks[NR_ONCPU]);
240247
case PSI_CPU_FULL:
@@ -711,10 +718,11 @@ static void psi_group_change(struct psi_group *group, int cpu,
711718
if (groupc->tasks[t]) {
712719
groupc->tasks[t]--;
713720
} else if (!psi_bug) {
714-
printk_deferred(KERN_ERR "psi: task underflow! cpu=%d t=%d tasks=[%u %u %u %u] clear=%x set=%x\n",
721+
printk_deferred(KERN_ERR "psi: task underflow! cpu=%d t=%d tasks=[%u %u %u %u %u] clear=%x set=%x\n",
715722
cpu, t, groupc->tasks[0],
716723
groupc->tasks[1], groupc->tasks[2],
717-
groupc->tasks[3], clear, set);
724+
groupc->tasks[3], groupc->tasks[4],
725+
clear, set);
718726
psi_bug = 1;
719727
}
720728
}
@@ -854,12 +862,15 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
854862
int clear = TSK_ONCPU, set = 0;
855863

856864
/*
857-
* When we're going to sleep, psi_dequeue() lets us handle
858-
* TSK_RUNNING and TSK_IOWAIT here, where we can combine it
859-
* with TSK_ONCPU and save walking common ancestors twice.
865+
* When we're going to sleep, psi_dequeue() lets us
866+
* handle TSK_RUNNING, TSK_MEMSTALL_RUNNING and
867+
* TSK_IOWAIT here, where we can combine it with
868+
* TSK_ONCPU and save walking common ancestors twice.
860869
*/
861870
if (sleep) {
862871
clear |= TSK_RUNNING;
872+
if (prev->in_memstall)
873+
clear |= TSK_MEMSTALL_RUNNING;
863874
if (prev->in_iowait)
864875
set |= TSK_IOWAIT;
865876
}
@@ -908,7 +919,7 @@ void psi_memstall_enter(unsigned long *flags)
908919
rq = this_rq_lock_irq(&rf);
909920

910921
current->in_memstall = 1;
911-
psi_task_change(current, 0, TSK_MEMSTALL);
922+
psi_task_change(current, 0, TSK_MEMSTALL | TSK_MEMSTALL_RUNNING);
912923

913924
rq_unlock_irq(rq, &rf);
914925
}
@@ -937,7 +948,7 @@ void psi_memstall_leave(unsigned long *flags)
937948
rq = this_rq_lock_irq(&rf);
938949

939950
current->in_memstall = 0;
940-
psi_task_change(current, TSK_MEMSTALL, 0);
951+
psi_task_change(current, TSK_MEMSTALL | TSK_MEMSTALL_RUNNING, 0);
941952

942953
rq_unlock_irq(rq, &rf);
943954
}

‎kernel/sched/stats.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ static inline void psi_enqueue(struct task_struct *p, bool wakeup)
118118
if (static_branch_likely(&psi_disabled))
119119
return;
120120

121+
if (p->in_memstall)
122+
set |= TSK_MEMSTALL_RUNNING;
123+
121124
if (!wakeup || p->sched_psi_wake_requeue) {
122125
if (p->in_memstall)
123126
set |= TSK_MEMSTALL;
@@ -148,7 +151,7 @@ static inline void psi_dequeue(struct task_struct *p, bool sleep)
148151
return;
149152

150153
if (p->in_memstall)
151-
clear |= TSK_MEMSTALL;
154+
clear |= (TSK_MEMSTALL | TSK_MEMSTALL_RUNNING);
152155

153156
psi_task_change(p, clear, 0);
154157
}

0 commit comments

Comments
 (0)