psi.c 36 KB

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