psi: Optimize task switch inside shared cgroups
[linux-2.6-microblaze.git] / kernel / sched / psi.c
1 /*
2  * Pressure stall information for CPU, memory and IO
3  *
4  * Copyright (c) 2018 Facebook, Inc.
5  * Author: Johannes Weiner <hannes@cmpxchg.org>
6  *
7  * Polling support by Suren Baghdasaryan <surenb@google.com>
8  * Copyright (c) 2018 Google, Inc.
9  *
10  * When CPU, memory and IO are contended, tasks experience delays that
11  * reduce throughput and introduce latencies into the workload. Memory
12  * and IO contention, in addition, can cause a full loss of forward
13  * progress in which the CPU goes idle.
14  *
15  * This code aggregates individual task delays into resource pressure
16  * metrics that indicate problems with both workload health and
17  * resource utilization.
18  *
19  *                      Model
20  *
21  * The time in which a task can execute on a CPU is our baseline for
22  * productivity. Pressure expresses the amount of time in which this
23  * potential cannot be realized due to resource contention.
24  *
25  * This concept of productivity has two components: the workload and
26  * the CPU. To measure the impact of pressure on both, we define two
27  * contention states for a resource: SOME and FULL.
28  *
29  * In the SOME state of a given resource, one or more tasks are
30  * delayed on that resource. This affects the workload's ability to
31  * perform work, but the CPU may still be executing other tasks.
32  *
33  * In the FULL state of a given resource, all non-idle tasks are
34  * delayed on that resource such that nobody is advancing and the CPU
35  * goes idle. This leaves both workload and CPU unproductive.
36  *
37  * Naturally, the FULL state doesn't exist for the CPU resource at the
38  * system level, but exist at the cgroup level, means all non-idle tasks
39  * in a cgroup are delayed on the CPU resource which used by others outside
40  * of the cgroup or throttled by the cgroup cpu.max configuration.
41  *
42  *      SOME = nr_delayed_tasks != 0
43  *      FULL = nr_delayed_tasks != 0 && nr_running_tasks == 0
44  *
45  * The percentage of wallclock time spent in those compound stall
46  * states gives pressure numbers between 0 and 100 for each resource,
47  * where the SOME percentage indicates workload slowdowns and the FULL
48  * percentage indicates reduced CPU utilization:
49  *
50  *      %SOME = time(SOME) / period
51  *      %FULL = time(FULL) / period
52  *
53  *                      Multiple CPUs
54  *
55  * The more tasks and available CPUs there are, the more work can be
56  * performed concurrently. This means that the potential that can go
57  * unrealized due to resource contention *also* scales with non-idle
58  * tasks and CPUs.
59  *
60  * Consider a scenario where 257 number crunching tasks are trying to
61  * run concurrently on 256 CPUs. If we simply aggregated the task
62  * states, we would have to conclude a CPU SOME pressure number of
63  * 100%, since *somebody* is waiting on a runqueue at all
64  * times. However, that is clearly not the amount of contention the
65  * workload is experiencing: only one out of 256 possible exceution
66  * threads will be contended at any given time, or about 0.4%.
67  *
68  * Conversely, consider a scenario of 4 tasks and 4 CPUs where at any
69  * given time *one* of the tasks is delayed due to a lack of memory.
70  * Again, looking purely at the task state would yield a memory FULL
71  * pressure number of 0%, since *somebody* is always making forward
72  * progress. But again this wouldn't capture the amount of execution
73  * potential lost, which is 1 out of 4 CPUs, or 25%.
74  *
75  * To calculate wasted potential (pressure) with multiple processors,
76  * we have to base our calculation on the number of non-idle tasks in
77  * conjunction with the number of available CPUs, which is the number
78  * of potential execution threads. SOME becomes then the proportion of
79  * delayed tasks to possibe threads, and FULL is the share of possible
80  * threads that are unproductive due to delays:
81  *
82  *      threads = min(nr_nonidle_tasks, nr_cpus)
83  *         SOME = min(nr_delayed_tasks / threads, 1)
84  *         FULL = (threads - min(nr_running_tasks, threads)) / threads
85  *
86  * For the 257 number crunchers on 256 CPUs, this yields:
87  *
88  *      threads = min(257, 256)
89  *         SOME = min(1 / 256, 1)             = 0.4%
90  *         FULL = (256 - min(257, 256)) / 256 = 0%
91  *
92  * For the 1 out of 4 memory-delayed tasks, this yields:
93  *
94  *      threads = min(4, 4)
95  *         SOME = min(1 / 4, 1)               = 25%
96  *         FULL = (4 - min(3, 4)) / 4         = 25%
97  *
98  * [ Substitute nr_cpus with 1, and you can see that it's a natural
99  *   extension of the single-CPU model. ]
100  *
101  *                      Implementation
102  *
103  * To assess the precise time spent in each such state, we would have
104  * to freeze the system on task changes and start/stop the state
105  * clocks accordingly. Obviously that doesn't scale in practice.
106  *
107  * Because the scheduler aims to distribute the compute load evenly
108  * among the available CPUs, we can track task state locally to each
109  * CPU and, at much lower frequency, extrapolate the global state for
110  * the cumulative stall times and the running averages.
111  *
112  * For each runqueue, we track:
113  *
114  *         tSOME[cpu] = time(nr_delayed_tasks[cpu] != 0)
115  *         tFULL[cpu] = time(nr_delayed_tasks[cpu] && !nr_running_tasks[cpu])
116  *      tNONIDLE[cpu] = time(nr_nonidle_tasks[cpu] != 0)
117  *
118  * and then periodically aggregate:
119  *
120  *      tNONIDLE = sum(tNONIDLE[i])
121  *
122  *         tSOME = sum(tSOME[i] * tNONIDLE[i]) / tNONIDLE
123  *         tFULL = sum(tFULL[i] * tNONIDLE[i]) / tNONIDLE
124  *
125  *         %SOME = tSOME / period
126  *         %FULL = tFULL / period
127  *
128  * This gives us an approximation of pressure that is practical
129  * cost-wise, yet way more sensitive and accurate than periodic
130  * sampling of the aggregate task states would be.
131  */
132
133 #include "../workqueue_internal.h"
134 #include <linux/sched/loadavg.h>
135 #include <linux/seq_file.h>
136 #include <linux/proc_fs.h>
137 #include <linux/seqlock.h>
138 #include <linux/uaccess.h>
139 #include <linux/cgroup.h>
140 #include <linux/module.h>
141 #include <linux/sched.h>
142 #include <linux/ctype.h>
143 #include <linux/file.h>
144 #include <linux/poll.h>
145 #include <linux/psi.h>
146 #include "sched.h"
147
148 static int psi_bug __read_mostly;
149
150 DEFINE_STATIC_KEY_FALSE(psi_disabled);
151
152 #ifdef CONFIG_PSI_DEFAULT_DISABLED
153 static bool psi_enable;
154 #else
155 static bool psi_enable = true;
156 #endif
157 static int __init setup_psi(char *str)
158 {
159         return kstrtobool(str, &psi_enable) == 0;
160 }
161 __setup("psi=", setup_psi);
162
163 /* Running averages - we need to be higher-res than loadavg */
164 #define PSI_FREQ        (2*HZ+1)        /* 2 sec intervals */
165 #define EXP_10s         1677            /* 1/exp(2s/10s) as fixed-point */
166 #define EXP_60s         1981            /* 1/exp(2s/60s) */
167 #define EXP_300s        2034            /* 1/exp(2s/300s) */
168
169 /* PSI trigger definitions */
170 #define WINDOW_MIN_US 500000    /* Min window size is 500ms */
171 #define WINDOW_MAX_US 10000000  /* Max window size is 10s */
172 #define UPDATES_PER_WINDOW 10   /* 10 updates per window */
173
174 /* Sampling frequency in nanoseconds */
175 static u64 psi_period __read_mostly;
176
177 /* System-level pressure and stall tracking */
178 static DEFINE_PER_CPU(struct psi_group_cpu, system_group_pcpu);
179 struct psi_group psi_system = {
180         .pcpu = &system_group_pcpu,
181 };
182
183 static void psi_avgs_work(struct work_struct *work);
184
185 static void group_init(struct psi_group *group)
186 {
187         int cpu;
188
189         for_each_possible_cpu(cpu)
190                 seqcount_init(&per_cpu_ptr(group->pcpu, cpu)->seq);
191         group->avg_last_update = sched_clock();
192         group->avg_next_update = group->avg_last_update + psi_period;
193         INIT_DELAYED_WORK(&group->avgs_work, psi_avgs_work);
194         mutex_init(&group->avgs_lock);
195         /* Init trigger-related members */
196         mutex_init(&group->trigger_lock);
197         INIT_LIST_HEAD(&group->triggers);
198         memset(group->nr_triggers, 0, sizeof(group->nr_triggers));
199         group->poll_states = 0;
200         group->poll_min_period = U32_MAX;
201         memset(group->polling_total, 0, sizeof(group->polling_total));
202         group->polling_next_update = ULLONG_MAX;
203         group->polling_until = 0;
204         rcu_assign_pointer(group->poll_task, NULL);
205 }
206
207 void __init psi_init(void)
208 {
209         if (!psi_enable) {
210                 static_branch_enable(&psi_disabled);
211                 return;
212         }
213
214         psi_period = jiffies_to_nsecs(PSI_FREQ);
215         group_init(&psi_system);
216 }
217
218 static bool test_state(unsigned int *tasks, enum psi_states state)
219 {
220         switch (state) {
221         case PSI_IO_SOME:
222                 return unlikely(tasks[NR_IOWAIT]);
223         case PSI_IO_FULL:
224                 return unlikely(tasks[NR_IOWAIT] && !tasks[NR_RUNNING]);
225         case PSI_MEM_SOME:
226                 return unlikely(tasks[NR_MEMSTALL]);
227         case PSI_MEM_FULL:
228                 return unlikely(tasks[NR_MEMSTALL] && !tasks[NR_RUNNING]);
229         case PSI_CPU_SOME:
230                 return unlikely(tasks[NR_RUNNING] > tasks[NR_ONCPU]);
231         case PSI_CPU_FULL:
232                 return unlikely(tasks[NR_RUNNING] && !tasks[NR_ONCPU]);
233         case PSI_NONIDLE:
234                 return tasks[NR_IOWAIT] || tasks[NR_MEMSTALL] ||
235                         tasks[NR_RUNNING];
236         default:
237                 return false;
238         }
239 }
240
241 static void get_recent_times(struct psi_group *group, int cpu,
242                              enum psi_aggregators aggregator, u32 *times,
243                              u32 *pchanged_states)
244 {
245         struct psi_group_cpu *groupc = per_cpu_ptr(group->pcpu, cpu);
246         u64 now, state_start;
247         enum psi_states s;
248         unsigned int seq;
249         u32 state_mask;
250
251         *pchanged_states = 0;
252
253         /* Snapshot a coherent view of the CPU state */
254         do {
255                 seq = read_seqcount_begin(&groupc->seq);
256                 now = cpu_clock(cpu);
257                 memcpy(times, groupc->times, sizeof(groupc->times));
258                 state_mask = groupc->state_mask;
259                 state_start = groupc->state_start;
260         } while (read_seqcount_retry(&groupc->seq, seq));
261
262         /* Calculate state time deltas against the previous snapshot */
263         for (s = 0; s < NR_PSI_STATES; s++) {
264                 u32 delta;
265                 /*
266                  * In addition to already concluded states, we also
267                  * incorporate currently active states on the CPU,
268                  * since states may last for many sampling periods.
269                  *
270                  * This way we keep our delta sampling buckets small
271                  * (u32) and our reported pressure close to what's
272                  * actually happening.
273                  */
274                 if (state_mask & (1 << s))
275                         times[s] += now - state_start;
276
277                 delta = times[s] - groupc->times_prev[aggregator][s];
278                 groupc->times_prev[aggregator][s] = times[s];
279
280                 times[s] = delta;
281                 if (delta)
282                         *pchanged_states |= (1 << s);
283         }
284 }
285
286 static void calc_avgs(unsigned long avg[3], int missed_periods,
287                       u64 time, u64 period)
288 {
289         unsigned long pct;
290
291         /* Fill in zeroes for periods of no activity */
292         if (missed_periods) {
293                 avg[0] = calc_load_n(avg[0], EXP_10s, 0, missed_periods);
294                 avg[1] = calc_load_n(avg[1], EXP_60s, 0, missed_periods);
295                 avg[2] = calc_load_n(avg[2], EXP_300s, 0, missed_periods);
296         }
297
298         /* Sample the most recent active period */
299         pct = div_u64(time * 100, period);
300         pct *= FIXED_1;
301         avg[0] = calc_load(avg[0], EXP_10s, pct);
302         avg[1] = calc_load(avg[1], EXP_60s, pct);
303         avg[2] = calc_load(avg[2], EXP_300s, pct);
304 }
305
306 static void collect_percpu_times(struct psi_group *group,
307                                  enum psi_aggregators aggregator,
308                                  u32 *pchanged_states)
309 {
310         u64 deltas[NR_PSI_STATES - 1] = { 0, };
311         unsigned long nonidle_total = 0;
312         u32 changed_states = 0;
313         int cpu;
314         int s;
315
316         /*
317          * Collect the per-cpu time buckets and average them into a
318          * single time sample that is normalized to wallclock time.
319          *
320          * For averaging, each CPU is weighted by its non-idle time in
321          * the sampling period. This eliminates artifacts from uneven
322          * loading, or even entirely idle CPUs.
323          */
324         for_each_possible_cpu(cpu) {
325                 u32 times[NR_PSI_STATES];
326                 u32 nonidle;
327                 u32 cpu_changed_states;
328
329                 get_recent_times(group, cpu, aggregator, times,
330                                 &cpu_changed_states);
331                 changed_states |= cpu_changed_states;
332
333                 nonidle = nsecs_to_jiffies(times[PSI_NONIDLE]);
334                 nonidle_total += nonidle;
335
336                 for (s = 0; s < PSI_NONIDLE; s++)
337                         deltas[s] += (u64)times[s] * nonidle;
338         }
339
340         /*
341          * Integrate the sample into the running statistics that are
342          * reported to userspace: the cumulative stall times and the
343          * decaying averages.
344          *
345          * Pressure percentages are sampled at PSI_FREQ. We might be
346          * called more often when the user polls more frequently than
347          * that; we might be called less often when there is no task
348          * activity, thus no data, and clock ticks are sporadic. The
349          * below handles both.
350          */
351
352         /* total= */
353         for (s = 0; s < NR_PSI_STATES - 1; s++)
354                 group->total[aggregator][s] +=
355                                 div_u64(deltas[s], max(nonidle_total, 1UL));
356
357         if (pchanged_states)
358                 *pchanged_states = changed_states;
359 }
360
361 static u64 update_averages(struct psi_group *group, u64 now)
362 {
363         unsigned long missed_periods = 0;
364         u64 expires, period;
365         u64 avg_next_update;
366         int s;
367
368         /* avgX= */
369         expires = group->avg_next_update;
370         if (now - expires >= psi_period)
371                 missed_periods = div_u64(now - expires, psi_period);
372
373         /*
374          * The periodic clock tick can get delayed for various
375          * reasons, especially on loaded systems. To avoid clock
376          * drift, we schedule the clock in fixed psi_period intervals.
377          * But the deltas we sample out of the per-cpu buckets above
378          * are based on the actual time elapsing between clock ticks.
379          */
380         avg_next_update = expires + ((1 + missed_periods) * psi_period);
381         period = now - (group->avg_last_update + (missed_periods * psi_period));
382         group->avg_last_update = now;
383
384         for (s = 0; s < NR_PSI_STATES - 1; s++) {
385                 u32 sample;
386
387                 sample = group->total[PSI_AVGS][s] - group->avg_total[s];
388                 /*
389                  * Due to the lockless sampling of the time buckets,
390                  * recorded time deltas can slip into the next period,
391                  * which under full pressure can result in samples in
392                  * excess of the period length.
393                  *
394                  * We don't want to report non-sensical pressures in
395                  * excess of 100%, nor do we want to drop such events
396                  * on the floor. Instead we punt any overage into the
397                  * future until pressure subsides. By doing this we
398                  * don't underreport the occurring pressure curve, we
399                  * just report it delayed by one period length.
400                  *
401                  * The error isn't cumulative. As soon as another
402                  * delta slips from a period P to P+1, by definition
403                  * it frees up its time T in P.
404                  */
405                 if (sample > period)
406                         sample = period;
407                 group->avg_total[s] += sample;
408                 calc_avgs(group->avg[s], missed_periods, sample, period);
409         }
410
411         return avg_next_update;
412 }
413
414 static void psi_avgs_work(struct work_struct *work)
415 {
416         struct delayed_work *dwork;
417         struct psi_group *group;
418         u32 changed_states;
419         bool nonidle;
420         u64 now;
421
422         dwork = to_delayed_work(work);
423         group = container_of(dwork, struct psi_group, avgs_work);
424
425         mutex_lock(&group->avgs_lock);
426
427         now = sched_clock();
428
429         collect_percpu_times(group, PSI_AVGS, &changed_states);
430         nonidle = changed_states & (1 << PSI_NONIDLE);
431         /*
432          * If there is task activity, periodically fold the per-cpu
433          * times and feed samples into the running averages. If things
434          * are idle and there is no data to process, stop the clock.
435          * Once restarted, we'll catch up the running averages in one
436          * go - see calc_avgs() and missed_periods.
437          */
438         if (now >= group->avg_next_update)
439                 group->avg_next_update = update_averages(group, now);
440
441         if (nonidle) {
442                 schedule_delayed_work(dwork, nsecs_to_jiffies(
443                                 group->avg_next_update - now) + 1);
444         }
445
446         mutex_unlock(&group->avgs_lock);
447 }
448
449 /* Trigger tracking window manupulations */
450 static void window_reset(struct psi_window *win, u64 now, u64 value,
451                          u64 prev_growth)
452 {
453         win->start_time = now;
454         win->start_value = value;
455         win->prev_growth = prev_growth;
456 }
457
458 /*
459  * PSI growth tracking window update and growth calculation routine.
460  *
461  * This approximates a sliding tracking window by interpolating
462  * partially elapsed windows using historical growth data from the
463  * previous intervals. This minimizes memory requirements (by not storing
464  * all the intermediate values in the previous window) and simplifies
465  * the calculations. It works well because PSI signal changes only in
466  * positive direction and over relatively small window sizes the growth
467  * is close to linear.
468  */
469 static u64 window_update(struct psi_window *win, u64 now, u64 value)
470 {
471         u64 elapsed;
472         u64 growth;
473
474         elapsed = now - win->start_time;
475         growth = value - win->start_value;
476         /*
477          * After each tracking window passes win->start_value and
478          * win->start_time get reset and win->prev_growth stores
479          * the average per-window growth of the previous window.
480          * win->prev_growth is then used to interpolate additional
481          * growth from the previous window assuming it was linear.
482          */
483         if (elapsed > win->size)
484                 window_reset(win, now, value, growth);
485         else {
486                 u32 remaining;
487
488                 remaining = win->size - elapsed;
489                 growth += div64_u64(win->prev_growth * remaining, win->size);
490         }
491
492         return growth;
493 }
494
495 static void init_triggers(struct psi_group *group, u64 now)
496 {
497         struct psi_trigger *t;
498
499         list_for_each_entry(t, &group->triggers, node)
500                 window_reset(&t->win, now,
501                                 group->total[PSI_POLL][t->state], 0);
502         memcpy(group->polling_total, group->total[PSI_POLL],
503                    sizeof(group->polling_total));
504         group->polling_next_update = now + group->poll_min_period;
505 }
506
507 static u64 update_triggers(struct psi_group *group, u64 now)
508 {
509         struct psi_trigger *t;
510         bool new_stall = false;
511         u64 *total = group->total[PSI_POLL];
512
513         /*
514          * On subsequent updates, calculate growth deltas and let
515          * watchers know when their specified thresholds are exceeded.
516          */
517         list_for_each_entry(t, &group->triggers, node) {
518                 u64 growth;
519
520                 /* Check for stall activity */
521                 if (group->polling_total[t->state] == total[t->state])
522                         continue;
523
524                 /*
525                  * Multiple triggers might be looking at the same state,
526                  * remember to update group->polling_total[] once we've
527                  * been through all of them. Also remember to extend the
528                  * polling time if we see new stall activity.
529                  */
530                 new_stall = true;
531
532                 /* Calculate growth since last update */
533                 growth = window_update(&t->win, now, total[t->state]);
534                 if (growth < t->threshold)
535                         continue;
536
537                 /* Limit event signaling to once per window */
538                 if (now < t->last_event_time + t->win.size)
539                         continue;
540
541                 /* Generate an event */
542                 if (cmpxchg(&t->event, 0, 1) == 0)
543                         wake_up_interruptible(&t->event_wait);
544                 t->last_event_time = now;
545         }
546
547         if (new_stall)
548                 memcpy(group->polling_total, total,
549                                 sizeof(group->polling_total));
550
551         return now + group->poll_min_period;
552 }
553
554 /* Schedule polling if it's not already scheduled. */
555 static void psi_schedule_poll_work(struct psi_group *group, unsigned long delay)
556 {
557         struct task_struct *task;
558
559         /*
560          * Do not reschedule if already scheduled.
561          * Possible race with a timer scheduled after this check but before
562          * mod_timer below can be tolerated because group->polling_next_update
563          * will keep updates on schedule.
564          */
565         if (timer_pending(&group->poll_timer))
566                 return;
567
568         rcu_read_lock();
569
570         task = rcu_dereference(group->poll_task);
571         /*
572          * kworker might be NULL in case psi_trigger_destroy races with
573          * psi_task_change (hotpath) which can't use locks
574          */
575         if (likely(task))
576                 mod_timer(&group->poll_timer, jiffies + delay);
577
578         rcu_read_unlock();
579 }
580
581 static void psi_poll_work(struct psi_group *group)
582 {
583         u32 changed_states;
584         u64 now;
585
586         mutex_lock(&group->trigger_lock);
587
588         now = sched_clock();
589
590         collect_percpu_times(group, PSI_POLL, &changed_states);
591
592         if (changed_states & group->poll_states) {
593                 /* Initialize trigger windows when entering polling mode */
594                 if (now > group->polling_until)
595                         init_triggers(group, now);
596
597                 /*
598                  * Keep the monitor active for at least the duration of the
599                  * minimum tracking window as long as monitor states are
600                  * changing.
601                  */
602                 group->polling_until = now +
603                         group->poll_min_period * UPDATES_PER_WINDOW;
604         }
605
606         if (now > group->polling_until) {
607                 group->polling_next_update = ULLONG_MAX;
608                 goto out;
609         }
610
611         if (now >= group->polling_next_update)
612                 group->polling_next_update = update_triggers(group, now);
613
614         psi_schedule_poll_work(group,
615                 nsecs_to_jiffies(group->polling_next_update - now) + 1);
616
617 out:
618         mutex_unlock(&group->trigger_lock);
619 }
620
621 static int psi_poll_worker(void *data)
622 {
623         struct psi_group *group = (struct psi_group *)data;
624
625         sched_set_fifo_low(current);
626
627         while (true) {
628                 wait_event_interruptible(group->poll_wait,
629                                 atomic_cmpxchg(&group->poll_wakeup, 1, 0) ||
630                                 kthread_should_stop());
631                 if (kthread_should_stop())
632                         break;
633
634                 psi_poll_work(group);
635         }
636         return 0;
637 }
638
639 static void poll_timer_fn(struct timer_list *t)
640 {
641         struct psi_group *group = from_timer(group, t, poll_timer);
642
643         atomic_set(&group->poll_wakeup, 1);
644         wake_up_interruptible(&group->poll_wait);
645 }
646
647 static void record_times(struct psi_group_cpu *groupc, int cpu)
648 {
649         u32 delta;
650         u64 now;
651
652         now = cpu_clock(cpu);
653         delta = now - groupc->state_start;
654         groupc->state_start = now;
655
656         if (groupc->state_mask & (1 << PSI_IO_SOME)) {
657                 groupc->times[PSI_IO_SOME] += delta;
658                 if (groupc->state_mask & (1 << PSI_IO_FULL))
659                         groupc->times[PSI_IO_FULL] += delta;
660         }
661
662         if (groupc->state_mask & (1 << PSI_MEM_SOME)) {
663                 groupc->times[PSI_MEM_SOME] += delta;
664                 if (groupc->state_mask & (1 << PSI_MEM_FULL))
665                         groupc->times[PSI_MEM_FULL] += delta;
666         }
667
668         if (groupc->state_mask & (1 << PSI_CPU_SOME)) {
669                 groupc->times[PSI_CPU_SOME] += delta;
670                 if (groupc->state_mask & (1 << PSI_CPU_FULL))
671                         groupc->times[PSI_CPU_FULL] += delta;
672         }
673
674         if (groupc->state_mask & (1 << PSI_NONIDLE))
675                 groupc->times[PSI_NONIDLE] += delta;
676 }
677
678 static void psi_group_change(struct psi_group *group, int cpu,
679                              unsigned int clear, unsigned int set,
680                              bool wake_clock)
681 {
682         struct psi_group_cpu *groupc;
683         u32 state_mask = 0;
684         unsigned int t, m;
685         enum psi_states s;
686
687         groupc = per_cpu_ptr(group->pcpu, cpu);
688
689         /*
690          * First we assess the aggregate resource states this CPU's
691          * tasks have been in since the last change, and account any
692          * SOME and FULL time these may have resulted in.
693          *
694          * Then we update the task counts according to the state
695          * change requested through the @clear and @set bits.
696          */
697         write_seqcount_begin(&groupc->seq);
698
699         record_times(groupc, cpu);
700
701         for (t = 0, m = clear; m; m &= ~(1 << t), t++) {
702                 if (!(m & (1 << t)))
703                         continue;
704                 if (groupc->tasks[t] == 0 && !psi_bug) {
705                         printk_deferred(KERN_ERR "psi: task underflow! cpu=%d t=%d tasks=[%u %u %u %u] clear=%x set=%x\n",
706                                         cpu, t, groupc->tasks[0],
707                                         groupc->tasks[1], groupc->tasks[2],
708                                         groupc->tasks[3], clear, set);
709                         psi_bug = 1;
710                 }
711                 groupc->tasks[t]--;
712         }
713
714         for (t = 0; set; set &= ~(1 << t), t++)
715                 if (set & (1 << t))
716                         groupc->tasks[t]++;
717
718         /* Calculate state mask representing active states */
719         for (s = 0; s < NR_PSI_STATES; s++) {
720                 if (test_state(groupc->tasks, s))
721                         state_mask |= (1 << s);
722         }
723
724         /*
725          * Since we care about lost potential, a memstall is FULL
726          * when there are no other working tasks, but also when
727          * the CPU is actively reclaiming and nothing productive
728          * could run even if it were runnable. So when the current
729          * task in a cgroup is in_memstall, the corresponding groupc
730          * on that cpu is in PSI_MEM_FULL state.
731          */
732         if (unlikely(groupc->tasks[NR_ONCPU] && cpu_curr(cpu)->in_memstall))
733                 state_mask |= (1 << PSI_MEM_FULL);
734
735         groupc->state_mask = state_mask;
736
737         write_seqcount_end(&groupc->seq);
738
739         if (state_mask & group->poll_states)
740                 psi_schedule_poll_work(group, 1);
741
742         if (wake_clock && !delayed_work_pending(&group->avgs_work))
743                 schedule_delayed_work(&group->avgs_work, PSI_FREQ);
744 }
745
746 static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
747 {
748 #ifdef CONFIG_CGROUPS
749         struct cgroup *cgroup = NULL;
750
751         if (!*iter)
752                 cgroup = task->cgroups->dfl_cgrp;
753         else if (*iter == &psi_system)
754                 return NULL;
755         else
756                 cgroup = cgroup_parent(*iter);
757
758         if (cgroup && cgroup_parent(cgroup)) {
759                 *iter = cgroup;
760                 return cgroup_psi(cgroup);
761         }
762 #else
763         if (*iter)
764                 return NULL;
765 #endif
766         *iter = &psi_system;
767         return &psi_system;
768 }
769
770 static void psi_flags_change(struct task_struct *task, int clear, int set)
771 {
772         if (((task->psi_flags & set) ||
773              (task->psi_flags & clear) != clear) &&
774             !psi_bug) {
775                 printk_deferred(KERN_ERR "psi: inconsistent task state! task=%d:%s cpu=%d psi_flags=%x clear=%x set=%x\n",
776                                 task->pid, task->comm, task_cpu(task),
777                                 task->psi_flags, clear, set);
778                 psi_bug = 1;
779         }
780
781         task->psi_flags &= ~clear;
782         task->psi_flags |= set;
783 }
784
785 void psi_task_change(struct task_struct *task, int clear, int set)
786 {
787         int cpu = task_cpu(task);
788         struct psi_group *group;
789         bool wake_clock = true;
790         void *iter = NULL;
791
792         if (!task->pid)
793                 return;
794
795         psi_flags_change(task, clear, set);
796
797         /*
798          * Periodic aggregation shuts off if there is a period of no
799          * task changes, so we wake it back up if necessary. However,
800          * don't do this if the task change is the aggregation worker
801          * itself going to sleep, or we'll ping-pong forever.
802          */
803         if (unlikely((clear & TSK_RUNNING) &&
804                      (task->flags & PF_WQ_WORKER) &&
805                      wq_worker_last_func(task) == psi_avgs_work))
806                 wake_clock = false;
807
808         while ((group = iterate_groups(task, &iter)))
809                 psi_group_change(group, cpu, clear, set, wake_clock);
810 }
811
812 void psi_task_switch(struct task_struct *prev, struct task_struct *next,
813                      bool sleep)
814 {
815         struct psi_group *group, *common = NULL;
816         int cpu = task_cpu(prev);
817         void *iter;
818
819         if (next->pid) {
820                 bool identical_state;
821
822                 psi_flags_change(next, 0, TSK_ONCPU);
823                 /*
824                  * When switching between tasks that have an identical
825                  * runtime state, the cgroup that contains both tasks
826                  * runtime state, the cgroup that contains both tasks
827                  * we reach the first common ancestor. Iterate @next's
828                  * ancestors only until we encounter @prev's ONCPU.
829                  */
830                 identical_state = prev->psi_flags == next->psi_flags;
831                 iter = NULL;
832                 while ((group = iterate_groups(next, &iter))) {
833                         if (identical_state &&
834                             per_cpu_ptr(group->pcpu, cpu)->tasks[NR_ONCPU]) {
835                                 common = group;
836                                 break;
837                         }
838
839                         psi_group_change(group, cpu, 0, TSK_ONCPU, true);
840                 }
841         }
842
843         if (prev->pid) {
844                 int clear = TSK_ONCPU, set = 0;
845
846                 /*
847                  * When we're going to sleep, psi_dequeue() lets us handle
848                  * TSK_RUNNING and TSK_IOWAIT here, where we can combine it
849                  * with TSK_ONCPU and save walking common ancestors twice.
850                  */
851                 if (sleep) {
852                         clear |= TSK_RUNNING;
853                         if (prev->in_iowait)
854                                 set |= TSK_IOWAIT;
855                 }
856
857                 psi_flags_change(prev, clear, set);
858
859                 iter = NULL;
860                 while ((group = iterate_groups(prev, &iter)) && group != common)
861                         psi_group_change(group, cpu, clear, set, true);
862
863                 /*
864                  * TSK_ONCPU is handled up to the common ancestor. If we're tasked
865                  * with dequeuing too, finish that for the rest of the hierarchy.
866                  */
867                 if (sleep) {
868                         clear &= ~TSK_ONCPU;
869                         for (; group; group = iterate_groups(prev, &iter))
870                                 psi_group_change(group, cpu, clear, set, true);
871                 }
872         }
873 }
874
875 /**
876  * psi_memstall_enter - mark the beginning of a memory stall section
877  * @flags: flags to handle nested sections
878  *
879  * Marks the calling task as being stalled due to a lack of memory,
880  * such as waiting for a refault or performing reclaim.
881  */
882 void psi_memstall_enter(unsigned long *flags)
883 {
884         struct rq_flags rf;
885         struct rq *rq;
886
887         if (static_branch_likely(&psi_disabled))
888                 return;
889
890         *flags = current->in_memstall;
891         if (*flags)
892                 return;
893         /*
894          * in_memstall setting & accounting needs to be atomic wrt
895          * changes to the task's scheduling state, otherwise we can
896          * race with CPU migration.
897          */
898         rq = this_rq_lock_irq(&rf);
899
900         current->in_memstall = 1;
901         psi_task_change(current, 0, TSK_MEMSTALL);
902
903         rq_unlock_irq(rq, &rf);
904 }
905
906 /**
907  * psi_memstall_leave - mark the end of an memory stall section
908  * @flags: flags to handle nested memdelay sections
909  *
910  * Marks the calling task as no longer stalled due to lack of memory.
911  */
912 void psi_memstall_leave(unsigned long *flags)
913 {
914         struct rq_flags rf;
915         struct rq *rq;
916
917         if (static_branch_likely(&psi_disabled))
918                 return;
919
920         if (*flags)
921                 return;
922         /*
923          * in_memstall clearing & accounting needs to be atomic wrt
924          * changes to the task's scheduling state, otherwise we could
925          * race with CPU migration.
926          */
927         rq = this_rq_lock_irq(&rf);
928
929         current->in_memstall = 0;
930         psi_task_change(current, TSK_MEMSTALL, 0);
931
932         rq_unlock_irq(rq, &rf);
933 }
934
935 #ifdef CONFIG_CGROUPS
936 int psi_cgroup_alloc(struct cgroup *cgroup)
937 {
938         if (static_branch_likely(&psi_disabled))
939                 return 0;
940
941         cgroup->psi.pcpu = alloc_percpu(struct psi_group_cpu);
942         if (!cgroup->psi.pcpu)
943                 return -ENOMEM;
944         group_init(&cgroup->psi);
945         return 0;
946 }
947
948 void psi_cgroup_free(struct cgroup *cgroup)
949 {
950         if (static_branch_likely(&psi_disabled))
951                 return;
952
953         cancel_delayed_work_sync(&cgroup->psi.avgs_work);
954         free_percpu(cgroup->psi.pcpu);
955         /* All triggers must be removed by now */
956         WARN_ONCE(cgroup->psi.poll_states, "psi: trigger leak\n");
957 }
958
959 /**
960  * cgroup_move_task - move task to a different cgroup
961  * @task: the task
962  * @to: the target css_set
963  *
964  * Move task to a new cgroup and safely migrate its associated stall
965  * state between the different groups.
966  *
967  * This function acquires the task's rq lock to lock out concurrent
968  * changes to the task's scheduling state and - in case the task is
969  * running - concurrent changes to its stall state.
970  */
971 void cgroup_move_task(struct task_struct *task, struct css_set *to)
972 {
973         unsigned int task_flags = 0;
974         struct rq_flags rf;
975         struct rq *rq;
976
977         if (static_branch_likely(&psi_disabled)) {
978                 /*
979                  * Lame to do this here, but the scheduler cannot be locked
980                  * from the outside, so we move cgroups from inside sched/.
981                  */
982                 rcu_assign_pointer(task->cgroups, to);
983                 return;
984         }
985
986         rq = task_rq_lock(task, &rf);
987
988         if (task_on_rq_queued(task)) {
989                 task_flags = TSK_RUNNING;
990                 if (task_current(rq, task))
991                         task_flags |= TSK_ONCPU;
992         } else if (task->in_iowait)
993                 task_flags = TSK_IOWAIT;
994
995         if (task->in_memstall)
996                 task_flags |= TSK_MEMSTALL;
997
998         if (task_flags)
999                 psi_task_change(task, task_flags, 0);
1000
1001         /* See comment above */
1002         rcu_assign_pointer(task->cgroups, to);
1003
1004         if (task_flags)
1005                 psi_task_change(task, 0, task_flags);
1006
1007         task_rq_unlock(rq, task, &rf);
1008 }
1009 #endif /* CONFIG_CGROUPS */
1010
1011 int psi_show(struct seq_file *m, struct psi_group *group, enum psi_res res)
1012 {
1013         int full;
1014         u64 now;
1015
1016         if (static_branch_likely(&psi_disabled))
1017                 return -EOPNOTSUPP;
1018
1019         /* Update averages before reporting them */
1020         mutex_lock(&group->avgs_lock);
1021         now = sched_clock();
1022         collect_percpu_times(group, PSI_AVGS, NULL);
1023         if (now >= group->avg_next_update)
1024                 group->avg_next_update = update_averages(group, now);
1025         mutex_unlock(&group->avgs_lock);
1026
1027         for (full = 0; full < 2; full++) {
1028                 unsigned long avg[3];
1029                 u64 total;
1030                 int w;
1031
1032                 for (w = 0; w < 3; w++)
1033                         avg[w] = group->avg[res * 2 + full][w];
1034                 total = div_u64(group->total[PSI_AVGS][res * 2 + full],
1035                                 NSEC_PER_USEC);
1036
1037                 seq_printf(m, "%s avg10=%lu.%02lu avg60=%lu.%02lu avg300=%lu.%02lu total=%llu\n",
1038                            full ? "full" : "some",
1039                            LOAD_INT(avg[0]), LOAD_FRAC(avg[0]),
1040                            LOAD_INT(avg[1]), LOAD_FRAC(avg[1]),
1041                            LOAD_INT(avg[2]), LOAD_FRAC(avg[2]),
1042                            total);
1043         }
1044
1045         return 0;
1046 }
1047
1048 static int psi_io_show(struct seq_file *m, void *v)
1049 {
1050         return psi_show(m, &psi_system, PSI_IO);
1051 }
1052
1053 static int psi_memory_show(struct seq_file *m, void *v)
1054 {
1055         return psi_show(m, &psi_system, PSI_MEM);
1056 }
1057
1058 static int psi_cpu_show(struct seq_file *m, void *v)
1059 {
1060         return psi_show(m, &psi_system, PSI_CPU);
1061 }
1062
1063 static int psi_io_open(struct inode *inode, struct file *file)
1064 {
1065         return single_open(file, psi_io_show, NULL);
1066 }
1067
1068 static int psi_memory_open(struct inode *inode, struct file *file)
1069 {
1070         return single_open(file, psi_memory_show, NULL);
1071 }
1072
1073 static int psi_cpu_open(struct inode *inode, struct file *file)
1074 {
1075         return single_open(file, psi_cpu_show, NULL);
1076 }
1077
1078 struct psi_trigger *psi_trigger_create(struct psi_group *group,
1079                         char *buf, size_t nbytes, enum psi_res res)
1080 {
1081         struct psi_trigger *t;
1082         enum psi_states state;
1083         u32 threshold_us;
1084         u32 window_us;
1085
1086         if (static_branch_likely(&psi_disabled))
1087                 return ERR_PTR(-EOPNOTSUPP);
1088
1089         if (sscanf(buf, "some %u %u", &threshold_us, &window_us) == 2)
1090                 state = PSI_IO_SOME + res * 2;
1091         else if (sscanf(buf, "full %u %u", &threshold_us, &window_us) == 2)
1092                 state = PSI_IO_FULL + res * 2;
1093         else
1094                 return ERR_PTR(-EINVAL);
1095
1096         if (state >= PSI_NONIDLE)
1097                 return ERR_PTR(-EINVAL);
1098
1099         if (window_us < WINDOW_MIN_US ||
1100                 window_us > WINDOW_MAX_US)
1101                 return ERR_PTR(-EINVAL);
1102
1103         /* Check threshold */
1104         if (threshold_us == 0 || threshold_us > window_us)
1105                 return ERR_PTR(-EINVAL);
1106
1107         t = kmalloc(sizeof(*t), GFP_KERNEL);
1108         if (!t)
1109                 return ERR_PTR(-ENOMEM);
1110
1111         t->group = group;
1112         t->state = state;
1113         t->threshold = threshold_us * NSEC_PER_USEC;
1114         t->win.size = window_us * NSEC_PER_USEC;
1115         window_reset(&t->win, 0, 0, 0);
1116
1117         t->event = 0;
1118         t->last_event_time = 0;
1119         init_waitqueue_head(&t->event_wait);
1120         kref_init(&t->refcount);
1121
1122         mutex_lock(&group->trigger_lock);
1123
1124         if (!rcu_access_pointer(group->poll_task)) {
1125                 struct task_struct *task;
1126
1127                 task = kthread_create(psi_poll_worker, group, "psimon");
1128                 if (IS_ERR(task)) {
1129                         kfree(t);
1130                         mutex_unlock(&group->trigger_lock);
1131                         return ERR_CAST(task);
1132                 }
1133                 atomic_set(&group->poll_wakeup, 0);
1134                 init_waitqueue_head(&group->poll_wait);
1135                 wake_up_process(task);
1136                 timer_setup(&group->poll_timer, poll_timer_fn, 0);
1137                 rcu_assign_pointer(group->poll_task, task);
1138         }
1139
1140         list_add(&t->node, &group->triggers);
1141         group->poll_min_period = min(group->poll_min_period,
1142                 div_u64(t->win.size, UPDATES_PER_WINDOW));
1143         group->nr_triggers[t->state]++;
1144         group->poll_states |= (1 << t->state);
1145
1146         mutex_unlock(&group->trigger_lock);
1147
1148         return t;
1149 }
1150
1151 static void psi_trigger_destroy(struct kref *ref)
1152 {
1153         struct psi_trigger *t = container_of(ref, struct psi_trigger, refcount);
1154         struct psi_group *group = t->group;
1155         struct task_struct *task_to_destroy = NULL;
1156
1157         if (static_branch_likely(&psi_disabled))
1158                 return;
1159
1160         /*
1161          * Wakeup waiters to stop polling. Can happen if cgroup is deleted
1162          * from under a polling process.
1163          */
1164         wake_up_interruptible(&t->event_wait);
1165
1166         mutex_lock(&group->trigger_lock);
1167
1168         if (!list_empty(&t->node)) {
1169                 struct psi_trigger *tmp;
1170                 u64 period = ULLONG_MAX;
1171
1172                 list_del(&t->node);
1173                 group->nr_triggers[t->state]--;
1174                 if (!group->nr_triggers[t->state])
1175                         group->poll_states &= ~(1 << t->state);
1176                 /* reset min update period for the remaining triggers */
1177                 list_for_each_entry(tmp, &group->triggers, node)
1178                         period = min(period, div_u64(tmp->win.size,
1179                                         UPDATES_PER_WINDOW));
1180                 group->poll_min_period = period;
1181                 /* Destroy poll_task when the last trigger is destroyed */
1182                 if (group->poll_states == 0) {
1183                         group->polling_until = 0;
1184                         task_to_destroy = rcu_dereference_protected(
1185                                         group->poll_task,
1186                                         lockdep_is_held(&group->trigger_lock));
1187                         rcu_assign_pointer(group->poll_task, NULL);
1188                 }
1189         }
1190
1191         mutex_unlock(&group->trigger_lock);
1192
1193         /*
1194          * Wait for both *trigger_ptr from psi_trigger_replace and
1195          * poll_task RCUs to complete their read-side critical sections
1196          * before destroying the trigger and optionally the poll_task
1197          */
1198         synchronize_rcu();
1199         /*
1200          * Destroy the kworker after releasing trigger_lock to prevent a
1201          * deadlock while waiting for psi_poll_work to acquire trigger_lock
1202          */
1203         if (task_to_destroy) {
1204                 /*
1205                  * After the RCU grace period has expired, the worker
1206                  * can no longer be found through group->poll_task.
1207                  * But it might have been already scheduled before
1208                  * that - deschedule it cleanly before destroying it.
1209                  */
1210                 del_timer_sync(&group->poll_timer);
1211                 kthread_stop(task_to_destroy);
1212         }
1213         kfree(t);
1214 }
1215
1216 void psi_trigger_replace(void **trigger_ptr, struct psi_trigger *new)
1217 {
1218         struct psi_trigger *old = *trigger_ptr;
1219
1220         if (static_branch_likely(&psi_disabled))
1221                 return;
1222
1223         rcu_assign_pointer(*trigger_ptr, new);
1224         if (old)
1225                 kref_put(&old->refcount, psi_trigger_destroy);
1226 }
1227
1228 __poll_t psi_trigger_poll(void **trigger_ptr,
1229                                 struct file *file, poll_table *wait)
1230 {
1231         __poll_t ret = DEFAULT_POLLMASK;
1232         struct psi_trigger *t;
1233
1234         if (static_branch_likely(&psi_disabled))
1235                 return DEFAULT_POLLMASK | EPOLLERR | EPOLLPRI;
1236
1237         rcu_read_lock();
1238
1239         t = rcu_dereference(*(void __rcu __force **)trigger_ptr);
1240         if (!t) {
1241                 rcu_read_unlock();
1242                 return DEFAULT_POLLMASK | EPOLLERR | EPOLLPRI;
1243         }
1244         kref_get(&t->refcount);
1245
1246         rcu_read_unlock();
1247
1248         poll_wait(file, &t->event_wait, wait);
1249
1250         if (cmpxchg(&t->event, 1, 0) == 1)
1251                 ret |= EPOLLPRI;
1252
1253         kref_put(&t->refcount, psi_trigger_destroy);
1254
1255         return ret;
1256 }
1257
1258 static ssize_t psi_write(struct file *file, const char __user *user_buf,
1259                          size_t nbytes, enum psi_res res)
1260 {
1261         char buf[32];
1262         size_t buf_size;
1263         struct seq_file *seq;
1264         struct psi_trigger *new;
1265
1266         if (static_branch_likely(&psi_disabled))
1267                 return -EOPNOTSUPP;
1268
1269         if (!nbytes)
1270                 return -EINVAL;
1271
1272         buf_size = min(nbytes, sizeof(buf));
1273         if (copy_from_user(buf, user_buf, buf_size))
1274                 return -EFAULT;
1275
1276         buf[buf_size - 1] = '\0';
1277
1278         new = psi_trigger_create(&psi_system, buf, nbytes, res);
1279         if (IS_ERR(new))
1280                 return PTR_ERR(new);
1281
1282         seq = file->private_data;
1283         /* Take seq->lock to protect seq->private from concurrent writes */
1284         mutex_lock(&seq->lock);
1285         psi_trigger_replace(&seq->private, new);
1286         mutex_unlock(&seq->lock);
1287
1288         return nbytes;
1289 }
1290
1291 static ssize_t psi_io_write(struct file *file, const char __user *user_buf,
1292                             size_t nbytes, loff_t *ppos)
1293 {
1294         return psi_write(file, user_buf, nbytes, PSI_IO);
1295 }
1296
1297 static ssize_t psi_memory_write(struct file *file, const char __user *user_buf,
1298                                 size_t nbytes, loff_t *ppos)
1299 {
1300         return psi_write(file, user_buf, nbytes, PSI_MEM);
1301 }
1302
1303 static ssize_t psi_cpu_write(struct file *file, const char __user *user_buf,
1304                              size_t nbytes, loff_t *ppos)
1305 {
1306         return psi_write(file, user_buf, nbytes, PSI_CPU);
1307 }
1308
1309 static __poll_t psi_fop_poll(struct file *file, poll_table *wait)
1310 {
1311         struct seq_file *seq = file->private_data;
1312
1313         return psi_trigger_poll(&seq->private, file, wait);
1314 }
1315
1316 static int psi_fop_release(struct inode *inode, struct file *file)
1317 {
1318         struct seq_file *seq = file->private_data;
1319
1320         psi_trigger_replace(&seq->private, NULL);
1321         return single_release(inode, file);
1322 }
1323
1324 static const struct proc_ops psi_io_proc_ops = {
1325         .proc_open      = psi_io_open,
1326         .proc_read      = seq_read,
1327         .proc_lseek     = seq_lseek,
1328         .proc_write     = psi_io_write,
1329         .proc_poll      = psi_fop_poll,
1330         .proc_release   = psi_fop_release,
1331 };
1332
1333 static const struct proc_ops psi_memory_proc_ops = {
1334         .proc_open      = psi_memory_open,
1335         .proc_read      = seq_read,
1336         .proc_lseek     = seq_lseek,
1337         .proc_write     = psi_memory_write,
1338         .proc_poll      = psi_fop_poll,
1339         .proc_release   = psi_fop_release,
1340 };
1341
1342 static const struct proc_ops psi_cpu_proc_ops = {
1343         .proc_open      = psi_cpu_open,
1344         .proc_read      = seq_read,
1345         .proc_lseek     = seq_lseek,
1346         .proc_write     = psi_cpu_write,
1347         .proc_poll      = psi_fop_poll,
1348         .proc_release   = psi_fop_release,
1349 };
1350
1351 static int __init psi_proc_init(void)
1352 {
1353         if (psi_enable) {
1354                 proc_mkdir("pressure", NULL);
1355                 proc_create("pressure/io", 0, NULL, &psi_io_proc_ops);
1356                 proc_create("pressure/memory", 0, NULL, &psi_memory_proc_ops);
1357                 proc_create("pressure/cpu", 0, NULL, &psi_cpu_proc_ops);
1358         }
1359         return 0;
1360 }
1361 module_init(psi_proc_init);