vmstat.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142
  1. /*
  2. * linux/mm/vmstat.c
  3. *
  4. * Manages VM statistics
  5. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  6. *
  7. * zoned VM statistics
  8. * Copyright (C) 2006 Silicon Graphics, Inc.,
  9. * Christoph Lameter <christoph@lameter.com>
  10. * Copyright (C) 2008-2014 Christoph Lameter
  11. */
  12. #include <linux/fs.h>
  13. #include <linux/mm.h>
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/cpu.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/vmstat.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/sched.h>
  24. #include <linux/math64.h>
  25. #include <linux/writeback.h>
  26. #include <linux/compaction.h>
  27. #include <linux/mm_inline.h>
  28. #include <linux/page_ext.h>
  29. #include <linux/page_owner.h>
  30. #include "internal.h"
  31. #define NUMA_STATS_THRESHOLD (U16_MAX - 2)
  32. #ifdef CONFIG_NUMA
  33. int sysctl_vm_numa_stat = ENABLE_NUMA_STAT;
  34. /* zero numa counters within a zone */
  35. static void zero_zone_numa_counters(struct zone *zone)
  36. {
  37. int item, cpu;
  38. for (item = 0; item < NR_VM_NUMA_STAT_ITEMS; item++) {
  39. atomic_long_set(&zone->vm_numa_stat[item], 0);
  40. for_each_online_cpu(cpu)
  41. per_cpu_ptr(zone->pageset, cpu)->vm_numa_stat_diff[item]
  42. = 0;
  43. }
  44. }
  45. /* zero numa counters of all the populated zones */
  46. static void zero_zones_numa_counters(void)
  47. {
  48. struct zone *zone;
  49. for_each_populated_zone(zone)
  50. zero_zone_numa_counters(zone);
  51. }
  52. /* zero global numa counters */
  53. static void zero_global_numa_counters(void)
  54. {
  55. int item;
  56. for (item = 0; item < NR_VM_NUMA_STAT_ITEMS; item++)
  57. atomic_long_set(&vm_numa_stat[item], 0);
  58. }
  59. static void invalid_numa_statistics(void)
  60. {
  61. zero_zones_numa_counters();
  62. zero_global_numa_counters();
  63. }
  64. static DEFINE_MUTEX(vm_numa_stat_lock);
  65. int sysctl_vm_numa_stat_handler(struct ctl_table *table, int write,
  66. void __user *buffer, size_t *length, loff_t *ppos)
  67. {
  68. int ret, oldval;
  69. mutex_lock(&vm_numa_stat_lock);
  70. if (write)
  71. oldval = sysctl_vm_numa_stat;
  72. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  73. if (ret || !write)
  74. goto out;
  75. if (oldval == sysctl_vm_numa_stat)
  76. goto out;
  77. else if (sysctl_vm_numa_stat == ENABLE_NUMA_STAT) {
  78. static_branch_enable(&vm_numa_stat_key);
  79. pr_info("enable numa statistics\n");
  80. } else {
  81. static_branch_disable(&vm_numa_stat_key);
  82. invalid_numa_statistics();
  83. pr_info("disable numa statistics, and clear numa counters\n");
  84. }
  85. out:
  86. mutex_unlock(&vm_numa_stat_lock);
  87. return ret;
  88. }
  89. #endif
  90. #ifdef CONFIG_VM_EVENT_COUNTERS
  91. DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
  92. EXPORT_PER_CPU_SYMBOL(vm_event_states);
  93. static void sum_vm_events(unsigned long *ret)
  94. {
  95. int cpu;
  96. int i;
  97. memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
  98. for_each_online_cpu(cpu) {
  99. struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
  100. for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
  101. ret[i] += this->event[i];
  102. }
  103. }
  104. /*
  105. * Accumulate the vm event counters across all CPUs.
  106. * The result is unavoidably approximate - it can change
  107. * during and after execution of this function.
  108. */
  109. void all_vm_events(unsigned long *ret)
  110. {
  111. get_online_cpus();
  112. sum_vm_events(ret);
  113. put_online_cpus();
  114. }
  115. EXPORT_SYMBOL_GPL(all_vm_events);
  116. /*
  117. * Fold the foreign cpu events into our own.
  118. *
  119. * This is adding to the events on one processor
  120. * but keeps the global counts constant.
  121. */
  122. void vm_events_fold_cpu(int cpu)
  123. {
  124. struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
  125. int i;
  126. for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
  127. count_vm_events(i, fold_state->event[i]);
  128. fold_state->event[i] = 0;
  129. }
  130. }
  131. #endif /* CONFIG_VM_EVENT_COUNTERS */
  132. /*
  133. * Manage combined zone based / global counters
  134. *
  135. * vm_stat contains the global counters
  136. */
  137. atomic_long_t vm_zone_stat[NR_VM_ZONE_STAT_ITEMS] __cacheline_aligned_in_smp;
  138. atomic_long_t vm_numa_stat[NR_VM_NUMA_STAT_ITEMS] __cacheline_aligned_in_smp;
  139. atomic_long_t vm_node_stat[NR_VM_NODE_STAT_ITEMS] __cacheline_aligned_in_smp;
  140. EXPORT_SYMBOL(vm_zone_stat);
  141. EXPORT_SYMBOL(vm_numa_stat);
  142. EXPORT_SYMBOL(vm_node_stat);
  143. #ifdef CONFIG_SMP
  144. int calculate_pressure_threshold(struct zone *zone)
  145. {
  146. int threshold;
  147. int watermark_distance;
  148. /*
  149. * As vmstats are not up to date, there is drift between the estimated
  150. * and real values. For high thresholds and a high number of CPUs, it
  151. * is possible for the min watermark to be breached while the estimated
  152. * value looks fine. The pressure threshold is a reduced value such
  153. * that even the maximum amount of drift will not accidentally breach
  154. * the min watermark
  155. */
  156. watermark_distance = low_wmark_pages(zone) - min_wmark_pages(zone);
  157. threshold = max(1, (int)(watermark_distance / num_online_cpus()));
  158. /*
  159. * Maximum threshold is 125
  160. */
  161. threshold = min(125, threshold);
  162. return threshold;
  163. }
  164. int calculate_normal_threshold(struct zone *zone)
  165. {
  166. int threshold;
  167. int mem; /* memory in 128 MB units */
  168. /*
  169. * The threshold scales with the number of processors and the amount
  170. * of memory per zone. More memory means that we can defer updates for
  171. * longer, more processors could lead to more contention.
  172. * fls() is used to have a cheap way of logarithmic scaling.
  173. *
  174. * Some sample thresholds:
  175. *
  176. * Threshold Processors (fls) Zonesize fls(mem+1)
  177. * ------------------------------------------------------------------
  178. * 8 1 1 0.9-1 GB 4
  179. * 16 2 2 0.9-1 GB 4
  180. * 20 2 2 1-2 GB 5
  181. * 24 2 2 2-4 GB 6
  182. * 28 2 2 4-8 GB 7
  183. * 32 2 2 8-16 GB 8
  184. * 4 2 2 <128M 1
  185. * 30 4 3 2-4 GB 5
  186. * 48 4 3 8-16 GB 8
  187. * 32 8 4 1-2 GB 4
  188. * 32 8 4 0.9-1GB 4
  189. * 10 16 5 <128M 1
  190. * 40 16 5 900M 4
  191. * 70 64 7 2-4 GB 5
  192. * 84 64 7 4-8 GB 6
  193. * 108 512 9 4-8 GB 6
  194. * 125 1024 10 8-16 GB 8
  195. * 125 1024 10 16-32 GB 9
  196. */
  197. mem = zone->managed_pages >> (27 - PAGE_SHIFT);
  198. threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
  199. /*
  200. * Maximum threshold is 125
  201. */
  202. threshold = min(125, threshold);
  203. return threshold;
  204. }
  205. /*
  206. * Refresh the thresholds for each zone.
  207. */
  208. void refresh_zone_stat_thresholds(void)
  209. {
  210. struct pglist_data *pgdat;
  211. struct zone *zone;
  212. int cpu;
  213. int threshold;
  214. /* Zero current pgdat thresholds */
  215. for_each_online_pgdat(pgdat) {
  216. for_each_online_cpu(cpu) {
  217. per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold = 0;
  218. }
  219. }
  220. for_each_populated_zone(zone) {
  221. struct pglist_data *pgdat = zone->zone_pgdat;
  222. unsigned long max_drift, tolerate_drift;
  223. threshold = calculate_normal_threshold(zone);
  224. for_each_online_cpu(cpu) {
  225. int pgdat_threshold;
  226. per_cpu_ptr(zone->pageset, cpu)->stat_threshold
  227. = threshold;
  228. /* Base nodestat threshold on the largest populated zone. */
  229. pgdat_threshold = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold;
  230. per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold
  231. = max(threshold, pgdat_threshold);
  232. }
  233. /*
  234. * Only set percpu_drift_mark if there is a danger that
  235. * NR_FREE_PAGES reports the low watermark is ok when in fact
  236. * the min watermark could be breached by an allocation
  237. */
  238. tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone);
  239. max_drift = num_online_cpus() * threshold;
  240. if (max_drift > tolerate_drift)
  241. zone->percpu_drift_mark = high_wmark_pages(zone) +
  242. max_drift;
  243. }
  244. }
  245. void set_pgdat_percpu_threshold(pg_data_t *pgdat,
  246. int (*calculate_pressure)(struct zone *))
  247. {
  248. struct zone *zone;
  249. int cpu;
  250. int threshold;
  251. int i;
  252. for (i = 0; i < pgdat->nr_zones; i++) {
  253. zone = &pgdat->node_zones[i];
  254. if (!zone->percpu_drift_mark)
  255. continue;
  256. threshold = (*calculate_pressure)(zone);
  257. for_each_online_cpu(cpu)
  258. per_cpu_ptr(zone->pageset, cpu)->stat_threshold
  259. = threshold;
  260. }
  261. }
  262. /*
  263. * For use when we know that interrupts are disabled,
  264. * or when we know that preemption is disabled and that
  265. * particular counter cannot be updated from interrupt context.
  266. */
  267. void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  268. long delta)
  269. {
  270. struct per_cpu_pageset __percpu *pcp = zone->pageset;
  271. s8 __percpu *p = pcp->vm_stat_diff + item;
  272. long x;
  273. long t;
  274. x = delta + __this_cpu_read(*p);
  275. t = __this_cpu_read(pcp->stat_threshold);
  276. if (unlikely(x > t || x < -t)) {
  277. zone_page_state_add(x, zone, item);
  278. x = 0;
  279. }
  280. __this_cpu_write(*p, x);
  281. }
  282. EXPORT_SYMBOL(__mod_zone_page_state);
  283. void __mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
  284. long delta)
  285. {
  286. struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
  287. s8 __percpu *p = pcp->vm_node_stat_diff + item;
  288. long x;
  289. long t;
  290. x = delta + __this_cpu_read(*p);
  291. t = __this_cpu_read(pcp->stat_threshold);
  292. if (unlikely(x > t || x < -t)) {
  293. node_page_state_add(x, pgdat, item);
  294. x = 0;
  295. }
  296. __this_cpu_write(*p, x);
  297. }
  298. EXPORT_SYMBOL(__mod_node_page_state);
  299. /*
  300. * Optimized increment and decrement functions.
  301. *
  302. * These are only for a single page and therefore can take a struct page *
  303. * argument instead of struct zone *. This allows the inclusion of the code
  304. * generated for page_zone(page) into the optimized functions.
  305. *
  306. * No overflow check is necessary and therefore the differential can be
  307. * incremented or decremented in place which may allow the compilers to
  308. * generate better code.
  309. * The increment or decrement is known and therefore one boundary check can
  310. * be omitted.
  311. *
  312. * NOTE: These functions are very performance sensitive. Change only
  313. * with care.
  314. *
  315. * Some processors have inc/dec instructions that are atomic vs an interrupt.
  316. * However, the code must first determine the differential location in a zone
  317. * based on the processor number and then inc/dec the counter. There is no
  318. * guarantee without disabling preemption that the processor will not change
  319. * in between and therefore the atomicity vs. interrupt cannot be exploited
  320. * in a useful way here.
  321. */
  322. void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  323. {
  324. struct per_cpu_pageset __percpu *pcp = zone->pageset;
  325. s8 __percpu *p = pcp->vm_stat_diff + item;
  326. s8 v, t;
  327. v = __this_cpu_inc_return(*p);
  328. t = __this_cpu_read(pcp->stat_threshold);
  329. if (unlikely(v > t)) {
  330. s8 overstep = t >> 1;
  331. zone_page_state_add(v + overstep, zone, item);
  332. __this_cpu_write(*p, -overstep);
  333. }
  334. }
  335. void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  336. {
  337. struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
  338. s8 __percpu *p = pcp->vm_node_stat_diff + item;
  339. s8 v, t;
  340. v = __this_cpu_inc_return(*p);
  341. t = __this_cpu_read(pcp->stat_threshold);
  342. if (unlikely(v > t)) {
  343. s8 overstep = t >> 1;
  344. node_page_state_add(v + overstep, pgdat, item);
  345. __this_cpu_write(*p, -overstep);
  346. }
  347. }
  348. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  349. {
  350. __inc_zone_state(page_zone(page), item);
  351. }
  352. EXPORT_SYMBOL(__inc_zone_page_state);
  353. void __inc_node_page_state(struct page *page, enum node_stat_item item)
  354. {
  355. __inc_node_state(page_pgdat(page), item);
  356. }
  357. EXPORT_SYMBOL(__inc_node_page_state);
  358. void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  359. {
  360. struct per_cpu_pageset __percpu *pcp = zone->pageset;
  361. s8 __percpu *p = pcp->vm_stat_diff + item;
  362. s8 v, t;
  363. v = __this_cpu_dec_return(*p);
  364. t = __this_cpu_read(pcp->stat_threshold);
  365. if (unlikely(v < - t)) {
  366. s8 overstep = t >> 1;
  367. zone_page_state_add(v - overstep, zone, item);
  368. __this_cpu_write(*p, overstep);
  369. }
  370. }
  371. void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  372. {
  373. struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
  374. s8 __percpu *p = pcp->vm_node_stat_diff + item;
  375. s8 v, t;
  376. v = __this_cpu_dec_return(*p);
  377. t = __this_cpu_read(pcp->stat_threshold);
  378. if (unlikely(v < - t)) {
  379. s8 overstep = t >> 1;
  380. node_page_state_add(v - overstep, pgdat, item);
  381. __this_cpu_write(*p, overstep);
  382. }
  383. }
  384. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  385. {
  386. __dec_zone_state(page_zone(page), item);
  387. }
  388. EXPORT_SYMBOL(__dec_zone_page_state);
  389. void __dec_node_page_state(struct page *page, enum node_stat_item item)
  390. {
  391. __dec_node_state(page_pgdat(page), item);
  392. }
  393. EXPORT_SYMBOL(__dec_node_page_state);
  394. #ifdef CONFIG_HAVE_CMPXCHG_LOCAL
  395. /*
  396. * If we have cmpxchg_local support then we do not need to incur the overhead
  397. * that comes with local_irq_save/restore if we use this_cpu_cmpxchg.
  398. *
  399. * mod_state() modifies the zone counter state through atomic per cpu
  400. * operations.
  401. *
  402. * Overstep mode specifies how overstep should handled:
  403. * 0 No overstepping
  404. * 1 Overstepping half of threshold
  405. * -1 Overstepping minus half of threshold
  406. */
  407. static inline void mod_zone_state(struct zone *zone,
  408. enum zone_stat_item item, long delta, int overstep_mode)
  409. {
  410. struct per_cpu_pageset __percpu *pcp = zone->pageset;
  411. s8 __percpu *p = pcp->vm_stat_diff + item;
  412. long o, n, t, z;
  413. do {
  414. z = 0; /* overflow to zone counters */
  415. /*
  416. * The fetching of the stat_threshold is racy. We may apply
  417. * a counter threshold to the wrong the cpu if we get
  418. * rescheduled while executing here. However, the next
  419. * counter update will apply the threshold again and
  420. * therefore bring the counter under the threshold again.
  421. *
  422. * Most of the time the thresholds are the same anyways
  423. * for all cpus in a zone.
  424. */
  425. t = this_cpu_read(pcp->stat_threshold);
  426. o = this_cpu_read(*p);
  427. n = delta + o;
  428. if (n > t || n < -t) {
  429. int os = overstep_mode * (t >> 1) ;
  430. /* Overflow must be added to zone counters */
  431. z = n + os;
  432. n = -os;
  433. }
  434. } while (this_cpu_cmpxchg(*p, o, n) != o);
  435. if (z)
  436. zone_page_state_add(z, zone, item);
  437. }
  438. void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  439. long delta)
  440. {
  441. mod_zone_state(zone, item, delta, 0);
  442. }
  443. EXPORT_SYMBOL(mod_zone_page_state);
  444. void inc_zone_page_state(struct page *page, enum zone_stat_item item)
  445. {
  446. mod_zone_state(page_zone(page), item, 1, 1);
  447. }
  448. EXPORT_SYMBOL(inc_zone_page_state);
  449. void dec_zone_page_state(struct page *page, enum zone_stat_item item)
  450. {
  451. mod_zone_state(page_zone(page), item, -1, -1);
  452. }
  453. EXPORT_SYMBOL(dec_zone_page_state);
  454. static inline void mod_node_state(struct pglist_data *pgdat,
  455. enum node_stat_item item, int delta, int overstep_mode)
  456. {
  457. struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
  458. s8 __percpu *p = pcp->vm_node_stat_diff + item;
  459. long o, n, t, z;
  460. do {
  461. z = 0; /* overflow to node counters */
  462. /*
  463. * The fetching of the stat_threshold is racy. We may apply
  464. * a counter threshold to the wrong the cpu if we get
  465. * rescheduled while executing here. However, the next
  466. * counter update will apply the threshold again and
  467. * therefore bring the counter under the threshold again.
  468. *
  469. * Most of the time the thresholds are the same anyways
  470. * for all cpus in a node.
  471. */
  472. t = this_cpu_read(pcp->stat_threshold);
  473. o = this_cpu_read(*p);
  474. n = delta + o;
  475. if (n > t || n < -t) {
  476. int os = overstep_mode * (t >> 1) ;
  477. /* Overflow must be added to node counters */
  478. z = n + os;
  479. n = -os;
  480. }
  481. } while (this_cpu_cmpxchg(*p, o, n) != o);
  482. if (z)
  483. node_page_state_add(z, pgdat, item);
  484. }
  485. void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
  486. long delta)
  487. {
  488. mod_node_state(pgdat, item, delta, 0);
  489. }
  490. EXPORT_SYMBOL(mod_node_page_state);
  491. void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  492. {
  493. mod_node_state(pgdat, item, 1, 1);
  494. }
  495. void inc_node_page_state(struct page *page, enum node_stat_item item)
  496. {
  497. mod_node_state(page_pgdat(page), item, 1, 1);
  498. }
  499. EXPORT_SYMBOL(inc_node_page_state);
  500. void dec_node_page_state(struct page *page, enum node_stat_item item)
  501. {
  502. mod_node_state(page_pgdat(page), item, -1, -1);
  503. }
  504. EXPORT_SYMBOL(dec_node_page_state);
  505. #else
  506. /*
  507. * Use interrupt disable to serialize counter updates
  508. */
  509. void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  510. long delta)
  511. {
  512. unsigned long flags;
  513. local_irq_save(flags);
  514. __mod_zone_page_state(zone, item, delta);
  515. local_irq_restore(flags);
  516. }
  517. EXPORT_SYMBOL(mod_zone_page_state);
  518. void inc_zone_page_state(struct page *page, enum zone_stat_item item)
  519. {
  520. unsigned long flags;
  521. struct zone *zone;
  522. zone = page_zone(page);
  523. local_irq_save(flags);
  524. __inc_zone_state(zone, item);
  525. local_irq_restore(flags);
  526. }
  527. EXPORT_SYMBOL(inc_zone_page_state);
  528. void dec_zone_page_state(struct page *page, enum zone_stat_item item)
  529. {
  530. unsigned long flags;
  531. local_irq_save(flags);
  532. __dec_zone_page_state(page, item);
  533. local_irq_restore(flags);
  534. }
  535. EXPORT_SYMBOL(dec_zone_page_state);
  536. void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  537. {
  538. unsigned long flags;
  539. local_irq_save(flags);
  540. __inc_node_state(pgdat, item);
  541. local_irq_restore(flags);
  542. }
  543. EXPORT_SYMBOL(inc_node_state);
  544. void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
  545. long delta)
  546. {
  547. unsigned long flags;
  548. local_irq_save(flags);
  549. __mod_node_page_state(pgdat, item, delta);
  550. local_irq_restore(flags);
  551. }
  552. EXPORT_SYMBOL(mod_node_page_state);
  553. void inc_node_page_state(struct page *page, enum node_stat_item item)
  554. {
  555. unsigned long flags;
  556. struct pglist_data *pgdat;
  557. pgdat = page_pgdat(page);
  558. local_irq_save(flags);
  559. __inc_node_state(pgdat, item);
  560. local_irq_restore(flags);
  561. }
  562. EXPORT_SYMBOL(inc_node_page_state);
  563. void dec_node_page_state(struct page *page, enum node_stat_item item)
  564. {
  565. unsigned long flags;
  566. local_irq_save(flags);
  567. __dec_node_page_state(page, item);
  568. local_irq_restore(flags);
  569. }
  570. EXPORT_SYMBOL(dec_node_page_state);
  571. #endif
  572. /*
  573. * Fold a differential into the global counters.
  574. * Returns the number of counters updated.
  575. */
  576. #ifdef CONFIG_NUMA
  577. static int fold_diff(int *zone_diff, int *numa_diff, int *node_diff)
  578. {
  579. int i;
  580. int changes = 0;
  581. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  582. if (zone_diff[i]) {
  583. atomic_long_add(zone_diff[i], &vm_zone_stat[i]);
  584. changes++;
  585. }
  586. for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
  587. if (numa_diff[i]) {
  588. atomic_long_add(numa_diff[i], &vm_numa_stat[i]);
  589. changes++;
  590. }
  591. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
  592. if (node_diff[i]) {
  593. atomic_long_add(node_diff[i], &vm_node_stat[i]);
  594. changes++;
  595. }
  596. return changes;
  597. }
  598. #else
  599. static int fold_diff(int *zone_diff, int *node_diff)
  600. {
  601. int i;
  602. int changes = 0;
  603. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  604. if (zone_diff[i]) {
  605. atomic_long_add(zone_diff[i], &vm_zone_stat[i]);
  606. changes++;
  607. }
  608. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
  609. if (node_diff[i]) {
  610. atomic_long_add(node_diff[i], &vm_node_stat[i]);
  611. changes++;
  612. }
  613. return changes;
  614. }
  615. #endif /* CONFIG_NUMA */
  616. /*
  617. * Update the zone counters for the current cpu.
  618. *
  619. * Note that refresh_cpu_vm_stats strives to only access
  620. * node local memory. The per cpu pagesets on remote zones are placed
  621. * in the memory local to the processor using that pageset. So the
  622. * loop over all zones will access a series of cachelines local to
  623. * the processor.
  624. *
  625. * The call to zone_page_state_add updates the cachelines with the
  626. * statistics in the remote zone struct as well as the global cachelines
  627. * with the global counters. These could cause remote node cache line
  628. * bouncing and will have to be only done when necessary.
  629. *
  630. * The function returns the number of global counters updated.
  631. */
  632. static int refresh_cpu_vm_stats(bool do_pagesets)
  633. {
  634. struct pglist_data *pgdat;
  635. struct zone *zone;
  636. int i;
  637. int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
  638. #ifdef CONFIG_NUMA
  639. int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
  640. #endif
  641. int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
  642. int changes = 0;
  643. for_each_populated_zone(zone) {
  644. struct per_cpu_pageset __percpu *p = zone->pageset;
  645. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
  646. int v;
  647. v = this_cpu_xchg(p->vm_stat_diff[i], 0);
  648. if (v) {
  649. atomic_long_add(v, &zone->vm_stat[i]);
  650. global_zone_diff[i] += v;
  651. #ifdef CONFIG_NUMA
  652. /* 3 seconds idle till flush */
  653. __this_cpu_write(p->expire, 3);
  654. #endif
  655. }
  656. }
  657. #ifdef CONFIG_NUMA
  658. for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
  659. int v;
  660. v = this_cpu_xchg(p->vm_numa_stat_diff[i], 0);
  661. if (v) {
  662. atomic_long_add(v, &zone->vm_numa_stat[i]);
  663. global_numa_diff[i] += v;
  664. __this_cpu_write(p->expire, 3);
  665. }
  666. }
  667. if (do_pagesets) {
  668. cond_resched();
  669. /*
  670. * Deal with draining the remote pageset of this
  671. * processor
  672. *
  673. * Check if there are pages remaining in this pageset
  674. * if not then there is nothing to expire.
  675. */
  676. if (!__this_cpu_read(p->expire) ||
  677. !__this_cpu_read(p->pcp.count))
  678. continue;
  679. /*
  680. * We never drain zones local to this processor.
  681. */
  682. if (zone_to_nid(zone) == numa_node_id()) {
  683. __this_cpu_write(p->expire, 0);
  684. continue;
  685. }
  686. if (__this_cpu_dec_return(p->expire))
  687. continue;
  688. if (__this_cpu_read(p->pcp.count)) {
  689. drain_zone_pages(zone, this_cpu_ptr(&p->pcp));
  690. changes++;
  691. }
  692. }
  693. #endif
  694. }
  695. for_each_online_pgdat(pgdat) {
  696. struct per_cpu_nodestat __percpu *p = pgdat->per_cpu_nodestats;
  697. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
  698. int v;
  699. v = this_cpu_xchg(p->vm_node_stat_diff[i], 0);
  700. if (v) {
  701. atomic_long_add(v, &pgdat->vm_stat[i]);
  702. global_node_diff[i] += v;
  703. }
  704. }
  705. }
  706. #ifdef CONFIG_NUMA
  707. changes += fold_diff(global_zone_diff, global_numa_diff,
  708. global_node_diff);
  709. #else
  710. changes += fold_diff(global_zone_diff, global_node_diff);
  711. #endif
  712. return changes;
  713. }
  714. /*
  715. * Fold the data for an offline cpu into the global array.
  716. * There cannot be any access by the offline cpu and therefore
  717. * synchronization is simplified.
  718. */
  719. void cpu_vm_stats_fold(int cpu)
  720. {
  721. struct pglist_data *pgdat;
  722. struct zone *zone;
  723. int i;
  724. int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
  725. #ifdef CONFIG_NUMA
  726. int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
  727. #endif
  728. int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
  729. for_each_populated_zone(zone) {
  730. struct per_cpu_pageset *p;
  731. p = per_cpu_ptr(zone->pageset, cpu);
  732. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  733. if (p->vm_stat_diff[i]) {
  734. int v;
  735. v = p->vm_stat_diff[i];
  736. p->vm_stat_diff[i] = 0;
  737. atomic_long_add(v, &zone->vm_stat[i]);
  738. global_zone_diff[i] += v;
  739. }
  740. #ifdef CONFIG_NUMA
  741. for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
  742. if (p->vm_numa_stat_diff[i]) {
  743. int v;
  744. v = p->vm_numa_stat_diff[i];
  745. p->vm_numa_stat_diff[i] = 0;
  746. atomic_long_add(v, &zone->vm_numa_stat[i]);
  747. global_numa_diff[i] += v;
  748. }
  749. #endif
  750. }
  751. for_each_online_pgdat(pgdat) {
  752. struct per_cpu_nodestat *p;
  753. p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu);
  754. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
  755. if (p->vm_node_stat_diff[i]) {
  756. int v;
  757. v = p->vm_node_stat_diff[i];
  758. p->vm_node_stat_diff[i] = 0;
  759. atomic_long_add(v, &pgdat->vm_stat[i]);
  760. global_node_diff[i] += v;
  761. }
  762. }
  763. #ifdef CONFIG_NUMA
  764. fold_diff(global_zone_diff, global_numa_diff, global_node_diff);
  765. #else
  766. fold_diff(global_zone_diff, global_node_diff);
  767. #endif
  768. }
  769. /*
  770. * this is only called if !populated_zone(zone), which implies no other users of
  771. * pset->vm_stat_diff[] exsist.
  772. */
  773. void drain_zonestat(struct zone *zone, struct per_cpu_pageset *pset)
  774. {
  775. int i;
  776. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  777. if (pset->vm_stat_diff[i]) {
  778. int v = pset->vm_stat_diff[i];
  779. pset->vm_stat_diff[i] = 0;
  780. atomic_long_add(v, &zone->vm_stat[i]);
  781. atomic_long_add(v, &vm_zone_stat[i]);
  782. }
  783. #ifdef CONFIG_NUMA
  784. for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
  785. if (pset->vm_numa_stat_diff[i]) {
  786. int v = pset->vm_numa_stat_diff[i];
  787. pset->vm_numa_stat_diff[i] = 0;
  788. atomic_long_add(v, &zone->vm_numa_stat[i]);
  789. atomic_long_add(v, &vm_numa_stat[i]);
  790. }
  791. #endif
  792. }
  793. #endif
  794. #ifdef CONFIG_NUMA
  795. void __inc_numa_state(struct zone *zone,
  796. enum numa_stat_item item)
  797. {
  798. struct per_cpu_pageset __percpu *pcp = zone->pageset;
  799. u16 __percpu *p = pcp->vm_numa_stat_diff + item;
  800. u16 v;
  801. v = __this_cpu_inc_return(*p);
  802. if (unlikely(v > NUMA_STATS_THRESHOLD)) {
  803. zone_numa_state_add(v, zone, item);
  804. __this_cpu_write(*p, 0);
  805. }
  806. }
  807. /*
  808. * Determine the per node value of a stat item. This function
  809. * is called frequently in a NUMA machine, so try to be as
  810. * frugal as possible.
  811. */
  812. unsigned long sum_zone_node_page_state(int node,
  813. enum zone_stat_item item)
  814. {
  815. struct zone *zones = NODE_DATA(node)->node_zones;
  816. int i;
  817. unsigned long count = 0;
  818. for (i = 0; i < MAX_NR_ZONES; i++)
  819. count += zone_page_state(zones + i, item);
  820. return count;
  821. }
  822. /*
  823. * Determine the per node value of a numa stat item. To avoid deviation,
  824. * the per cpu stat number in vm_numa_stat_diff[] is also included.
  825. */
  826. unsigned long sum_zone_numa_state(int node,
  827. enum numa_stat_item item)
  828. {
  829. struct zone *zones = NODE_DATA(node)->node_zones;
  830. int i;
  831. unsigned long count = 0;
  832. for (i = 0; i < MAX_NR_ZONES; i++)
  833. count += zone_numa_state_snapshot(zones + i, item);
  834. return count;
  835. }
  836. /*
  837. * Determine the per node value of a stat item.
  838. */
  839. unsigned long node_page_state(struct pglist_data *pgdat,
  840. enum node_stat_item item)
  841. {
  842. long x = atomic_long_read(&pgdat->vm_stat[item]);
  843. #ifdef CONFIG_SMP
  844. if (x < 0)
  845. x = 0;
  846. #endif
  847. return x;
  848. }
  849. #endif
  850. #ifdef CONFIG_COMPACTION
  851. struct contig_page_info {
  852. unsigned long free_pages;
  853. unsigned long free_blocks_total;
  854. unsigned long free_blocks_suitable;
  855. };
  856. /*
  857. * Calculate the number of free pages in a zone, how many contiguous
  858. * pages are free and how many are large enough to satisfy an allocation of
  859. * the target size. Note that this function makes no attempt to estimate
  860. * how many suitable free blocks there *might* be if MOVABLE pages were
  861. * migrated. Calculating that is possible, but expensive and can be
  862. * figured out from userspace
  863. */
  864. static void fill_contig_page_info(struct zone *zone,
  865. unsigned int suitable_order,
  866. struct contig_page_info *info)
  867. {
  868. unsigned int order;
  869. info->free_pages = 0;
  870. info->free_blocks_total = 0;
  871. info->free_blocks_suitable = 0;
  872. for (order = 0; order < MAX_ORDER; order++) {
  873. unsigned long blocks;
  874. /* Count number of free blocks */
  875. blocks = zone->free_area[order].nr_free;
  876. info->free_blocks_total += blocks;
  877. /* Count free base pages */
  878. info->free_pages += blocks << order;
  879. /* Count the suitable free blocks */
  880. if (order >= suitable_order)
  881. info->free_blocks_suitable += blocks <<
  882. (order - suitable_order);
  883. }
  884. }
  885. /*
  886. * A fragmentation index only makes sense if an allocation of a requested
  887. * size would fail. If that is true, the fragmentation index indicates
  888. * whether external fragmentation or a lack of memory was the problem.
  889. * The value can be used to determine if page reclaim or compaction
  890. * should be used
  891. */
  892. static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
  893. {
  894. unsigned long requested = 1UL << order;
  895. if (WARN_ON_ONCE(order >= MAX_ORDER))
  896. return 0;
  897. if (!info->free_blocks_total)
  898. return 0;
  899. /* Fragmentation index only makes sense when a request would fail */
  900. if (info->free_blocks_suitable)
  901. return -1000;
  902. /*
  903. * Index is between 0 and 1 so return within 3 decimal places
  904. *
  905. * 0 => allocation would fail due to lack of memory
  906. * 1 => allocation would fail due to fragmentation
  907. */
  908. return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
  909. }
  910. /* Same as __fragmentation index but allocs contig_page_info on stack */
  911. int fragmentation_index(struct zone *zone, unsigned int order)
  912. {
  913. struct contig_page_info info;
  914. fill_contig_page_info(zone, order, &info);
  915. return __fragmentation_index(order, &info);
  916. }
  917. #endif
  918. #if defined(CONFIG_PROC_FS) || defined(CONFIG_SYSFS) || defined(CONFIG_NUMA)
  919. #ifdef CONFIG_ZONE_DMA
  920. #define TEXT_FOR_DMA(xx) xx "_dma",
  921. #else
  922. #define TEXT_FOR_DMA(xx)
  923. #endif
  924. #ifdef CONFIG_ZONE_DMA32
  925. #define TEXT_FOR_DMA32(xx) xx "_dma32",
  926. #else
  927. #define TEXT_FOR_DMA32(xx)
  928. #endif
  929. #ifdef CONFIG_HIGHMEM
  930. #define TEXT_FOR_HIGHMEM(xx) xx "_high",
  931. #else
  932. #define TEXT_FOR_HIGHMEM(xx)
  933. #endif
  934. #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
  935. TEXT_FOR_HIGHMEM(xx) xx "_movable",
  936. const char * const vmstat_text[] = {
  937. /* enum zone_stat_item countes */
  938. "nr_free_pages",
  939. "nr_zone_inactive_anon",
  940. "nr_zone_active_anon",
  941. "nr_zone_inactive_file",
  942. "nr_zone_active_file",
  943. "nr_zone_unevictable",
  944. "nr_zone_write_pending",
  945. "nr_mlock",
  946. "nr_page_table_pages",
  947. "nr_kernel_stack",
  948. "nr_bounce",
  949. #if IS_ENABLED(CONFIG_ZSMALLOC)
  950. "nr_zspages",
  951. #endif
  952. "nr_free_cma",
  953. /* enum numa_stat_item counters */
  954. #ifdef CONFIG_NUMA
  955. "numa_hit",
  956. "numa_miss",
  957. "numa_foreign",
  958. "numa_interleave",
  959. "numa_local",
  960. "numa_other",
  961. #endif
  962. /* Node-based counters */
  963. "nr_inactive_anon",
  964. "nr_active_anon",
  965. "nr_inactive_file",
  966. "nr_active_file",
  967. "nr_unevictable",
  968. "nr_slab_reclaimable",
  969. "nr_slab_unreclaimable",
  970. "nr_isolated_anon",
  971. "nr_isolated_file",
  972. "workingset_refault",
  973. "workingset_activate",
  974. "workingset_nodereclaim",
  975. "nr_anon_pages",
  976. "nr_mapped",
  977. "nr_file_pages",
  978. "nr_dirty",
  979. "nr_writeback",
  980. "nr_writeback_temp",
  981. "nr_shmem",
  982. "nr_shmem_hugepages",
  983. "nr_shmem_pmdmapped",
  984. "nr_anon_transparent_hugepages",
  985. "nr_unstable",
  986. "nr_vmscan_write",
  987. "nr_vmscan_immediate_reclaim",
  988. "nr_dirtied",
  989. "nr_written",
  990. "", /* nr_indirectly_reclaimable */
  991. /* enum writeback_stat_item counters */
  992. "nr_dirty_threshold",
  993. "nr_dirty_background_threshold",
  994. #ifdef CONFIG_VM_EVENT_COUNTERS
  995. /* enum vm_event_item counters */
  996. "pgpgin",
  997. "pgpgout",
  998. "pswpin",
  999. "pswpout",
  1000. TEXTS_FOR_ZONES("pgalloc")
  1001. TEXTS_FOR_ZONES("allocstall")
  1002. TEXTS_FOR_ZONES("pgskip")
  1003. "pgfree",
  1004. "pgactivate",
  1005. "pgdeactivate",
  1006. "pglazyfree",
  1007. "pgfault",
  1008. "pgmajfault",
  1009. "pglazyfreed",
  1010. "pgrefill",
  1011. "pgsteal_kswapd",
  1012. "pgsteal_direct",
  1013. "pgscan_kswapd",
  1014. "pgscan_direct",
  1015. "pgscan_direct_throttle",
  1016. #ifdef CONFIG_NUMA
  1017. "zone_reclaim_failed",
  1018. #endif
  1019. "pginodesteal",
  1020. "slabs_scanned",
  1021. "kswapd_inodesteal",
  1022. "kswapd_low_wmark_hit_quickly",
  1023. "kswapd_high_wmark_hit_quickly",
  1024. "pageoutrun",
  1025. "pgrotated",
  1026. "drop_pagecache",
  1027. "drop_slab",
  1028. "oom_kill",
  1029. #ifdef CONFIG_NUMA_BALANCING
  1030. "numa_pte_updates",
  1031. "numa_huge_pte_updates",
  1032. "numa_hint_faults",
  1033. "numa_hint_faults_local",
  1034. "numa_pages_migrated",
  1035. #endif
  1036. #ifdef CONFIG_MIGRATION
  1037. "pgmigrate_success",
  1038. "pgmigrate_fail",
  1039. #endif
  1040. #ifdef CONFIG_COMPACTION
  1041. "compact_migrate_scanned",
  1042. "compact_free_scanned",
  1043. "compact_isolated",
  1044. "compact_stall",
  1045. "compact_fail",
  1046. "compact_success",
  1047. "compact_daemon_wake",
  1048. "compact_daemon_migrate_scanned",
  1049. "compact_daemon_free_scanned",
  1050. #endif
  1051. #ifdef CONFIG_HUGETLB_PAGE
  1052. "htlb_buddy_alloc_success",
  1053. "htlb_buddy_alloc_fail",
  1054. #endif
  1055. "unevictable_pgs_culled",
  1056. "unevictable_pgs_scanned",
  1057. "unevictable_pgs_rescued",
  1058. "unevictable_pgs_mlocked",
  1059. "unevictable_pgs_munlocked",
  1060. "unevictable_pgs_cleared",
  1061. "unevictable_pgs_stranded",
  1062. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1063. "thp_fault_alloc",
  1064. "thp_fault_fallback",
  1065. "thp_collapse_alloc",
  1066. "thp_collapse_alloc_failed",
  1067. "thp_file_alloc",
  1068. "thp_file_mapped",
  1069. "thp_split_page",
  1070. "thp_split_page_failed",
  1071. "thp_deferred_split_page",
  1072. "thp_split_pmd",
  1073. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  1074. "thp_split_pud",
  1075. #endif
  1076. "thp_zero_page_alloc",
  1077. "thp_zero_page_alloc_failed",
  1078. "thp_swpout",
  1079. "thp_swpout_fallback",
  1080. #endif
  1081. #ifdef CONFIG_MEMORY_BALLOON
  1082. "balloon_inflate",
  1083. "balloon_deflate",
  1084. #ifdef CONFIG_BALLOON_COMPACTION
  1085. "balloon_migrate",
  1086. #endif
  1087. #endif /* CONFIG_MEMORY_BALLOON */
  1088. #ifdef CONFIG_DEBUG_TLBFLUSH
  1089. "nr_tlb_remote_flush",
  1090. "nr_tlb_remote_flush_received",
  1091. "nr_tlb_local_flush_all",
  1092. "nr_tlb_local_flush_one",
  1093. #endif /* CONFIG_DEBUG_TLBFLUSH */
  1094. #ifdef CONFIG_DEBUG_VM_VMACACHE
  1095. "vmacache_find_calls",
  1096. "vmacache_find_hits",
  1097. #endif
  1098. #ifdef CONFIG_SWAP
  1099. "swap_ra",
  1100. "swap_ra_hit",
  1101. #endif
  1102. #endif /* CONFIG_VM_EVENTS_COUNTERS */
  1103. };
  1104. #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
  1105. #if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)) || \
  1106. defined(CONFIG_PROC_FS)
  1107. static void *frag_start(struct seq_file *m, loff_t *pos)
  1108. {
  1109. pg_data_t *pgdat;
  1110. loff_t node = *pos;
  1111. for (pgdat = first_online_pgdat();
  1112. pgdat && node;
  1113. pgdat = next_online_pgdat(pgdat))
  1114. --node;
  1115. return pgdat;
  1116. }
  1117. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  1118. {
  1119. pg_data_t *pgdat = (pg_data_t *)arg;
  1120. (*pos)++;
  1121. return next_online_pgdat(pgdat);
  1122. }
  1123. static void frag_stop(struct seq_file *m, void *arg)
  1124. {
  1125. }
  1126. /*
  1127. * Walk zones in a node and print using a callback.
  1128. * If @assert_populated is true, only use callback for zones that are populated.
  1129. */
  1130. static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
  1131. bool assert_populated, bool nolock,
  1132. void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
  1133. {
  1134. struct zone *zone;
  1135. struct zone *node_zones = pgdat->node_zones;
  1136. unsigned long flags;
  1137. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  1138. if (assert_populated && !populated_zone(zone))
  1139. continue;
  1140. if (!nolock)
  1141. spin_lock_irqsave(&zone->lock, flags);
  1142. print(m, pgdat, zone);
  1143. if (!nolock)
  1144. spin_unlock_irqrestore(&zone->lock, flags);
  1145. }
  1146. }
  1147. #endif
  1148. #ifdef CONFIG_PROC_FS
  1149. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  1150. struct zone *zone)
  1151. {
  1152. int order;
  1153. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  1154. for (order = 0; order < MAX_ORDER; ++order)
  1155. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  1156. seq_putc(m, '\n');
  1157. }
  1158. /*
  1159. * This walks the free areas for each zone.
  1160. */
  1161. static int frag_show(struct seq_file *m, void *arg)
  1162. {
  1163. pg_data_t *pgdat = (pg_data_t *)arg;
  1164. walk_zones_in_node(m, pgdat, true, false, frag_show_print);
  1165. return 0;
  1166. }
  1167. static void pagetypeinfo_showfree_print(struct seq_file *m,
  1168. pg_data_t *pgdat, struct zone *zone)
  1169. {
  1170. int order, mtype;
  1171. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  1172. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  1173. pgdat->node_id,
  1174. zone->name,
  1175. migratetype_names[mtype]);
  1176. for (order = 0; order < MAX_ORDER; ++order) {
  1177. unsigned long freecount = 0;
  1178. struct free_area *area;
  1179. struct list_head *curr;
  1180. area = &(zone->free_area[order]);
  1181. list_for_each(curr, &area->free_list[mtype])
  1182. freecount++;
  1183. seq_printf(m, "%6lu ", freecount);
  1184. }
  1185. seq_putc(m, '\n');
  1186. }
  1187. }
  1188. /* Print out the free pages at each order for each migatetype */
  1189. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  1190. {
  1191. int order;
  1192. pg_data_t *pgdat = (pg_data_t *)arg;
  1193. /* Print header */
  1194. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  1195. for (order = 0; order < MAX_ORDER; ++order)
  1196. seq_printf(m, "%6d ", order);
  1197. seq_putc(m, '\n');
  1198. walk_zones_in_node(m, pgdat, true, false, pagetypeinfo_showfree_print);
  1199. return 0;
  1200. }
  1201. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  1202. pg_data_t *pgdat, struct zone *zone)
  1203. {
  1204. int mtype;
  1205. unsigned long pfn;
  1206. unsigned long start_pfn = zone->zone_start_pfn;
  1207. unsigned long end_pfn = zone_end_pfn(zone);
  1208. unsigned long count[MIGRATE_TYPES] = { 0, };
  1209. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  1210. struct page *page;
  1211. page = pfn_to_online_page(pfn);
  1212. if (!page)
  1213. continue;
  1214. /* Watch for unexpected holes punched in the memmap */
  1215. if (!memmap_valid_within(pfn, page, zone))
  1216. continue;
  1217. if (page_zone(page) != zone)
  1218. continue;
  1219. mtype = get_pageblock_migratetype(page);
  1220. if (mtype < MIGRATE_TYPES)
  1221. count[mtype]++;
  1222. }
  1223. /* Print counts */
  1224. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  1225. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1226. seq_printf(m, "%12lu ", count[mtype]);
  1227. seq_putc(m, '\n');
  1228. }
  1229. /* Print out the number of pageblocks for each migratetype */
  1230. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  1231. {
  1232. int mtype;
  1233. pg_data_t *pgdat = (pg_data_t *)arg;
  1234. seq_printf(m, "\n%-23s", "Number of blocks type ");
  1235. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1236. seq_printf(m, "%12s ", migratetype_names[mtype]);
  1237. seq_putc(m, '\n');
  1238. walk_zones_in_node(m, pgdat, true, false,
  1239. pagetypeinfo_showblockcount_print);
  1240. return 0;
  1241. }
  1242. /*
  1243. * Print out the number of pageblocks for each migratetype that contain pages
  1244. * of other types. This gives an indication of how well fallbacks are being
  1245. * contained by rmqueue_fallback(). It requires information from PAGE_OWNER
  1246. * to determine what is going on
  1247. */
  1248. static void pagetypeinfo_showmixedcount(struct seq_file *m, pg_data_t *pgdat)
  1249. {
  1250. #ifdef CONFIG_PAGE_OWNER
  1251. int mtype;
  1252. if (!static_branch_unlikely(&page_owner_inited))
  1253. return;
  1254. drain_all_pages(NULL);
  1255. seq_printf(m, "\n%-23s", "Number of mixed blocks ");
  1256. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1257. seq_printf(m, "%12s ", migratetype_names[mtype]);
  1258. seq_putc(m, '\n');
  1259. walk_zones_in_node(m, pgdat, true, true,
  1260. pagetypeinfo_showmixedcount_print);
  1261. #endif /* CONFIG_PAGE_OWNER */
  1262. }
  1263. /*
  1264. * This prints out statistics in relation to grouping pages by mobility.
  1265. * It is expensive to collect so do not constantly read the file.
  1266. */
  1267. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  1268. {
  1269. pg_data_t *pgdat = (pg_data_t *)arg;
  1270. /* check memoryless node */
  1271. if (!node_state(pgdat->node_id, N_MEMORY))
  1272. return 0;
  1273. seq_printf(m, "Page block order: %d\n", pageblock_order);
  1274. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  1275. seq_putc(m, '\n');
  1276. pagetypeinfo_showfree(m, pgdat);
  1277. pagetypeinfo_showblockcount(m, pgdat);
  1278. pagetypeinfo_showmixedcount(m, pgdat);
  1279. return 0;
  1280. }
  1281. static const struct seq_operations fragmentation_op = {
  1282. .start = frag_start,
  1283. .next = frag_next,
  1284. .stop = frag_stop,
  1285. .show = frag_show,
  1286. };
  1287. static const struct seq_operations pagetypeinfo_op = {
  1288. .start = frag_start,
  1289. .next = frag_next,
  1290. .stop = frag_stop,
  1291. .show = pagetypeinfo_show,
  1292. };
  1293. static bool is_zone_first_populated(pg_data_t *pgdat, struct zone *zone)
  1294. {
  1295. int zid;
  1296. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  1297. struct zone *compare = &pgdat->node_zones[zid];
  1298. if (populated_zone(compare))
  1299. return zone == compare;
  1300. }
  1301. return false;
  1302. }
  1303. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  1304. struct zone *zone)
  1305. {
  1306. int i;
  1307. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  1308. if (is_zone_first_populated(pgdat, zone)) {
  1309. seq_printf(m, "\n per-node stats");
  1310. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
  1311. /* Skip hidden vmstat items. */
  1312. if (*vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
  1313. NR_VM_NUMA_STAT_ITEMS] == '\0')
  1314. continue;
  1315. seq_printf(m, "\n %-12s %lu",
  1316. vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
  1317. NR_VM_NUMA_STAT_ITEMS],
  1318. node_page_state(pgdat, i));
  1319. }
  1320. }
  1321. seq_printf(m,
  1322. "\n pages free %lu"
  1323. "\n min %lu"
  1324. "\n low %lu"
  1325. "\n high %lu"
  1326. "\n spanned %lu"
  1327. "\n present %lu"
  1328. "\n managed %lu",
  1329. zone_page_state(zone, NR_FREE_PAGES),
  1330. min_wmark_pages(zone),
  1331. low_wmark_pages(zone),
  1332. high_wmark_pages(zone),
  1333. zone->spanned_pages,
  1334. zone->present_pages,
  1335. zone->managed_pages);
  1336. seq_printf(m,
  1337. "\n protection: (%ld",
  1338. zone->lowmem_reserve[0]);
  1339. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  1340. seq_printf(m, ", %ld", zone->lowmem_reserve[i]);
  1341. seq_putc(m, ')');
  1342. /* If unpopulated, no other information is useful */
  1343. if (!populated_zone(zone)) {
  1344. seq_putc(m, '\n');
  1345. return;
  1346. }
  1347. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  1348. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  1349. zone_page_state(zone, i));
  1350. #ifdef CONFIG_NUMA
  1351. for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
  1352. seq_printf(m, "\n %-12s %lu",
  1353. vmstat_text[i + NR_VM_ZONE_STAT_ITEMS],
  1354. zone_numa_state_snapshot(zone, i));
  1355. #endif
  1356. seq_printf(m, "\n pagesets");
  1357. for_each_online_cpu(i) {
  1358. struct per_cpu_pageset *pageset;
  1359. pageset = per_cpu_ptr(zone->pageset, i);
  1360. seq_printf(m,
  1361. "\n cpu: %i"
  1362. "\n count: %i"
  1363. "\n high: %i"
  1364. "\n batch: %i",
  1365. i,
  1366. pageset->pcp.count,
  1367. pageset->pcp.high,
  1368. pageset->pcp.batch);
  1369. #ifdef CONFIG_SMP
  1370. seq_printf(m, "\n vm stats threshold: %d",
  1371. pageset->stat_threshold);
  1372. #endif
  1373. }
  1374. seq_printf(m,
  1375. "\n node_unreclaimable: %u"
  1376. "\n start_pfn: %lu",
  1377. pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES,
  1378. zone->zone_start_pfn);
  1379. seq_putc(m, '\n');
  1380. }
  1381. /*
  1382. * Output information about zones in @pgdat. All zones are printed regardless
  1383. * of whether they are populated or not: lowmem_reserve_ratio operates on the
  1384. * set of all zones and userspace would not be aware of such zones if they are
  1385. * suppressed here (zoneinfo displays the effect of lowmem_reserve_ratio).
  1386. */
  1387. static int zoneinfo_show(struct seq_file *m, void *arg)
  1388. {
  1389. pg_data_t *pgdat = (pg_data_t *)arg;
  1390. walk_zones_in_node(m, pgdat, false, false, zoneinfo_show_print);
  1391. return 0;
  1392. }
  1393. static const struct seq_operations zoneinfo_op = {
  1394. .start = frag_start, /* iterate over all zones. The same as in
  1395. * fragmentation. */
  1396. .next = frag_next,
  1397. .stop = frag_stop,
  1398. .show = zoneinfo_show,
  1399. };
  1400. enum writeback_stat_item {
  1401. NR_DIRTY_THRESHOLD,
  1402. NR_DIRTY_BG_THRESHOLD,
  1403. NR_VM_WRITEBACK_STAT_ITEMS,
  1404. };
  1405. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  1406. {
  1407. unsigned long *v;
  1408. int i, stat_items_size;
  1409. if (*pos >= ARRAY_SIZE(vmstat_text))
  1410. return NULL;
  1411. stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
  1412. NR_VM_NUMA_STAT_ITEMS * sizeof(unsigned long) +
  1413. NR_VM_NODE_STAT_ITEMS * sizeof(unsigned long) +
  1414. NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
  1415. #ifdef CONFIG_VM_EVENT_COUNTERS
  1416. stat_items_size += sizeof(struct vm_event_state);
  1417. #endif
  1418. v = kmalloc(stat_items_size, GFP_KERNEL);
  1419. m->private = v;
  1420. if (!v)
  1421. return ERR_PTR(-ENOMEM);
  1422. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  1423. v[i] = global_zone_page_state(i);
  1424. v += NR_VM_ZONE_STAT_ITEMS;
  1425. #ifdef CONFIG_NUMA
  1426. for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
  1427. v[i] = global_numa_state(i);
  1428. v += NR_VM_NUMA_STAT_ITEMS;
  1429. #endif
  1430. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
  1431. v[i] = global_node_page_state(i);
  1432. v += NR_VM_NODE_STAT_ITEMS;
  1433. global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
  1434. v + NR_DIRTY_THRESHOLD);
  1435. v += NR_VM_WRITEBACK_STAT_ITEMS;
  1436. #ifdef CONFIG_VM_EVENT_COUNTERS
  1437. all_vm_events(v);
  1438. v[PGPGIN] /= 2; /* sectors -> kbytes */
  1439. v[PGPGOUT] /= 2;
  1440. #endif
  1441. return (unsigned long *)m->private + *pos;
  1442. }
  1443. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  1444. {
  1445. (*pos)++;
  1446. if (*pos >= ARRAY_SIZE(vmstat_text))
  1447. return NULL;
  1448. return (unsigned long *)m->private + *pos;
  1449. }
  1450. static int vmstat_show(struct seq_file *m, void *arg)
  1451. {
  1452. unsigned long *l = arg;
  1453. unsigned long off = l - (unsigned long *)m->private;
  1454. /* Skip hidden vmstat items. */
  1455. if (*vmstat_text[off] == '\0')
  1456. return 0;
  1457. seq_puts(m, vmstat_text[off]);
  1458. seq_put_decimal_ull(m, " ", *l);
  1459. seq_putc(m, '\n');
  1460. return 0;
  1461. }
  1462. static void vmstat_stop(struct seq_file *m, void *arg)
  1463. {
  1464. kfree(m->private);
  1465. m->private = NULL;
  1466. }
  1467. static const struct seq_operations vmstat_op = {
  1468. .start = vmstat_start,
  1469. .next = vmstat_next,
  1470. .stop = vmstat_stop,
  1471. .show = vmstat_show,
  1472. };
  1473. #endif /* CONFIG_PROC_FS */
  1474. #ifdef CONFIG_SMP
  1475. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  1476. int sysctl_stat_interval __read_mostly = HZ;
  1477. #ifdef CONFIG_PROC_FS
  1478. static void refresh_vm_stats(struct work_struct *work)
  1479. {
  1480. refresh_cpu_vm_stats(true);
  1481. }
  1482. int vmstat_refresh(struct ctl_table *table, int write,
  1483. void __user *buffer, size_t *lenp, loff_t *ppos)
  1484. {
  1485. long val;
  1486. int err;
  1487. int i;
  1488. /*
  1489. * The regular update, every sysctl_stat_interval, may come later
  1490. * than expected: leaving a significant amount in per_cpu buckets.
  1491. * This is particularly misleading when checking a quantity of HUGE
  1492. * pages, immediately after running a test. /proc/sys/vm/stat_refresh,
  1493. * which can equally be echo'ed to or cat'ted from (by root),
  1494. * can be used to update the stats just before reading them.
  1495. *
  1496. * Oh, and since global_zone_page_state() etc. are so careful to hide
  1497. * transiently negative values, report an error here if any of
  1498. * the stats is negative, so we know to go looking for imbalance.
  1499. */
  1500. err = schedule_on_each_cpu(refresh_vm_stats);
  1501. if (err)
  1502. return err;
  1503. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
  1504. val = atomic_long_read(&vm_zone_stat[i]);
  1505. if (val < 0) {
  1506. pr_warn("%s: %s %ld\n",
  1507. __func__, vmstat_text[i], val);
  1508. err = -EINVAL;
  1509. }
  1510. }
  1511. #ifdef CONFIG_NUMA
  1512. for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
  1513. val = atomic_long_read(&vm_numa_stat[i]);
  1514. if (val < 0) {
  1515. pr_warn("%s: %s %ld\n",
  1516. __func__, vmstat_text[i + NR_VM_ZONE_STAT_ITEMS], val);
  1517. err = -EINVAL;
  1518. }
  1519. }
  1520. #endif
  1521. if (err)
  1522. return err;
  1523. if (write)
  1524. *ppos += *lenp;
  1525. else
  1526. *lenp = 0;
  1527. return 0;
  1528. }
  1529. #endif /* CONFIG_PROC_FS */
  1530. static void vmstat_update(struct work_struct *w)
  1531. {
  1532. if (refresh_cpu_vm_stats(true)) {
  1533. /*
  1534. * Counters were updated so we expect more updates
  1535. * to occur in the future. Keep on running the
  1536. * update worker thread.
  1537. */
  1538. queue_delayed_work_on(smp_processor_id(), mm_percpu_wq,
  1539. this_cpu_ptr(&vmstat_work),
  1540. round_jiffies_relative(sysctl_stat_interval));
  1541. }
  1542. }
  1543. /*
  1544. * Switch off vmstat processing and then fold all the remaining differentials
  1545. * until the diffs stay at zero. The function is used by NOHZ and can only be
  1546. * invoked when tick processing is not active.
  1547. */
  1548. /*
  1549. * Check if the diffs for a certain cpu indicate that
  1550. * an update is needed.
  1551. */
  1552. static bool need_update(int cpu)
  1553. {
  1554. struct zone *zone;
  1555. for_each_populated_zone(zone) {
  1556. struct per_cpu_pageset *p = per_cpu_ptr(zone->pageset, cpu);
  1557. BUILD_BUG_ON(sizeof(p->vm_stat_diff[0]) != 1);
  1558. #ifdef CONFIG_NUMA
  1559. BUILD_BUG_ON(sizeof(p->vm_numa_stat_diff[0]) != 2);
  1560. #endif
  1561. /*
  1562. * The fast way of checking if there are any vmstat diffs.
  1563. */
  1564. if (memchr_inv(p->vm_stat_diff, 0, NR_VM_ZONE_STAT_ITEMS *
  1565. sizeof(p->vm_stat_diff[0])))
  1566. return true;
  1567. #ifdef CONFIG_NUMA
  1568. if (memchr_inv(p->vm_numa_stat_diff, 0, NR_VM_NUMA_STAT_ITEMS *
  1569. sizeof(p->vm_numa_stat_diff[0])))
  1570. return true;
  1571. #endif
  1572. }
  1573. return false;
  1574. }
  1575. /*
  1576. * Switch off vmstat processing and then fold all the remaining differentials
  1577. * until the diffs stay at zero. The function is used by NOHZ and can only be
  1578. * invoked when tick processing is not active.
  1579. */
  1580. void quiet_vmstat(void)
  1581. {
  1582. if (system_state != SYSTEM_RUNNING)
  1583. return;
  1584. if (!delayed_work_pending(this_cpu_ptr(&vmstat_work)))
  1585. return;
  1586. if (!need_update(smp_processor_id()))
  1587. return;
  1588. /*
  1589. * Just refresh counters and do not care about the pending delayed
  1590. * vmstat_update. It doesn't fire that often to matter and canceling
  1591. * it would be too expensive from this path.
  1592. * vmstat_shepherd will take care about that for us.
  1593. */
  1594. refresh_cpu_vm_stats(false);
  1595. }
  1596. /*
  1597. * Shepherd worker thread that checks the
  1598. * differentials of processors that have their worker
  1599. * threads for vm statistics updates disabled because of
  1600. * inactivity.
  1601. */
  1602. static void vmstat_shepherd(struct work_struct *w);
  1603. static DECLARE_DEFERRABLE_WORK(shepherd, vmstat_shepherd);
  1604. static void vmstat_shepherd(struct work_struct *w)
  1605. {
  1606. int cpu;
  1607. get_online_cpus();
  1608. /* Check processors whose vmstat worker threads have been disabled */
  1609. for_each_online_cpu(cpu) {
  1610. struct delayed_work *dw = &per_cpu(vmstat_work, cpu);
  1611. if (!delayed_work_pending(dw) && need_update(cpu))
  1612. queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0);
  1613. }
  1614. put_online_cpus();
  1615. schedule_delayed_work(&shepherd,
  1616. round_jiffies_relative(sysctl_stat_interval));
  1617. }
  1618. static void __init start_shepherd_timer(void)
  1619. {
  1620. int cpu;
  1621. for_each_possible_cpu(cpu)
  1622. INIT_DEFERRABLE_WORK(per_cpu_ptr(&vmstat_work, cpu),
  1623. vmstat_update);
  1624. schedule_delayed_work(&shepherd,
  1625. round_jiffies_relative(sysctl_stat_interval));
  1626. }
  1627. static void __init init_cpu_node_state(void)
  1628. {
  1629. int node;
  1630. for_each_online_node(node) {
  1631. if (cpumask_weight(cpumask_of_node(node)) > 0)
  1632. node_set_state(node, N_CPU);
  1633. }
  1634. }
  1635. static int vmstat_cpu_online(unsigned int cpu)
  1636. {
  1637. refresh_zone_stat_thresholds();
  1638. node_set_state(cpu_to_node(cpu), N_CPU);
  1639. return 0;
  1640. }
  1641. static int vmstat_cpu_down_prep(unsigned int cpu)
  1642. {
  1643. cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
  1644. return 0;
  1645. }
  1646. static int vmstat_cpu_dead(unsigned int cpu)
  1647. {
  1648. const struct cpumask *node_cpus;
  1649. int node;
  1650. node = cpu_to_node(cpu);
  1651. refresh_zone_stat_thresholds();
  1652. node_cpus = cpumask_of_node(node);
  1653. if (cpumask_weight(node_cpus) > 0)
  1654. return 0;
  1655. node_clear_state(node, N_CPU);
  1656. return 0;
  1657. }
  1658. #endif
  1659. struct workqueue_struct *mm_percpu_wq;
  1660. void __init init_mm_internals(void)
  1661. {
  1662. int ret __maybe_unused;
  1663. mm_percpu_wq = alloc_workqueue("mm_percpu_wq", WQ_MEM_RECLAIM, 0);
  1664. #ifdef CONFIG_SMP
  1665. ret = cpuhp_setup_state_nocalls(CPUHP_MM_VMSTAT_DEAD, "mm/vmstat:dead",
  1666. NULL, vmstat_cpu_dead);
  1667. if (ret < 0)
  1668. pr_err("vmstat: failed to register 'dead' hotplug state\n");
  1669. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "mm/vmstat:online",
  1670. vmstat_cpu_online,
  1671. vmstat_cpu_down_prep);
  1672. if (ret < 0)
  1673. pr_err("vmstat: failed to register 'online' hotplug state\n");
  1674. get_online_cpus();
  1675. init_cpu_node_state();
  1676. put_online_cpus();
  1677. start_shepherd_timer();
  1678. #endif
  1679. #ifdef CONFIG_PROC_FS
  1680. proc_create_seq("buddyinfo", 0444, NULL, &fragmentation_op);
  1681. proc_create_seq("pagetypeinfo", 0400, NULL, &pagetypeinfo_op);
  1682. proc_create_seq("vmstat", 0444, NULL, &vmstat_op);
  1683. proc_create_seq("zoneinfo", 0444, NULL, &zoneinfo_op);
  1684. #endif
  1685. }
  1686. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  1687. /*
  1688. * Return an index indicating how much of the available free memory is
  1689. * unusable for an allocation of the requested size.
  1690. */
  1691. static int unusable_free_index(unsigned int order,
  1692. struct contig_page_info *info)
  1693. {
  1694. /* No free memory is interpreted as all free memory is unusable */
  1695. if (info->free_pages == 0)
  1696. return 1000;
  1697. /*
  1698. * Index should be a value between 0 and 1. Return a value to 3
  1699. * decimal places.
  1700. *
  1701. * 0 => no fragmentation
  1702. * 1 => high fragmentation
  1703. */
  1704. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  1705. }
  1706. static void unusable_show_print(struct seq_file *m,
  1707. pg_data_t *pgdat, struct zone *zone)
  1708. {
  1709. unsigned int order;
  1710. int index;
  1711. struct contig_page_info info;
  1712. seq_printf(m, "Node %d, zone %8s ",
  1713. pgdat->node_id,
  1714. zone->name);
  1715. for (order = 0; order < MAX_ORDER; ++order) {
  1716. fill_contig_page_info(zone, order, &info);
  1717. index = unusable_free_index(order, &info);
  1718. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1719. }
  1720. seq_putc(m, '\n');
  1721. }
  1722. /*
  1723. * Display unusable free space index
  1724. *
  1725. * The unusable free space index measures how much of the available free
  1726. * memory cannot be used to satisfy an allocation of a given size and is a
  1727. * value between 0 and 1. The higher the value, the more of free memory is
  1728. * unusable and by implication, the worse the external fragmentation is. This
  1729. * can be expressed as a percentage by multiplying by 100.
  1730. */
  1731. static int unusable_show(struct seq_file *m, void *arg)
  1732. {
  1733. pg_data_t *pgdat = (pg_data_t *)arg;
  1734. /* check memoryless node */
  1735. if (!node_state(pgdat->node_id, N_MEMORY))
  1736. return 0;
  1737. walk_zones_in_node(m, pgdat, true, false, unusable_show_print);
  1738. return 0;
  1739. }
  1740. static const struct seq_operations unusable_op = {
  1741. .start = frag_start,
  1742. .next = frag_next,
  1743. .stop = frag_stop,
  1744. .show = unusable_show,
  1745. };
  1746. static int unusable_open(struct inode *inode, struct file *file)
  1747. {
  1748. return seq_open(file, &unusable_op);
  1749. }
  1750. static const struct file_operations unusable_file_ops = {
  1751. .open = unusable_open,
  1752. .read = seq_read,
  1753. .llseek = seq_lseek,
  1754. .release = seq_release,
  1755. };
  1756. static void extfrag_show_print(struct seq_file *m,
  1757. pg_data_t *pgdat, struct zone *zone)
  1758. {
  1759. unsigned int order;
  1760. int index;
  1761. /* Alloc on stack as interrupts are disabled for zone walk */
  1762. struct contig_page_info info;
  1763. seq_printf(m, "Node %d, zone %8s ",
  1764. pgdat->node_id,
  1765. zone->name);
  1766. for (order = 0; order < MAX_ORDER; ++order) {
  1767. fill_contig_page_info(zone, order, &info);
  1768. index = __fragmentation_index(order, &info);
  1769. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1770. }
  1771. seq_putc(m, '\n');
  1772. }
  1773. /*
  1774. * Display fragmentation index for orders that allocations would fail for
  1775. */
  1776. static int extfrag_show(struct seq_file *m, void *arg)
  1777. {
  1778. pg_data_t *pgdat = (pg_data_t *)arg;
  1779. walk_zones_in_node(m, pgdat, true, false, extfrag_show_print);
  1780. return 0;
  1781. }
  1782. static const struct seq_operations extfrag_op = {
  1783. .start = frag_start,
  1784. .next = frag_next,
  1785. .stop = frag_stop,
  1786. .show = extfrag_show,
  1787. };
  1788. static int extfrag_open(struct inode *inode, struct file *file)
  1789. {
  1790. return seq_open(file, &extfrag_op);
  1791. }
  1792. static const struct file_operations extfrag_file_ops = {
  1793. .open = extfrag_open,
  1794. .read = seq_read,
  1795. .llseek = seq_lseek,
  1796. .release = seq_release,
  1797. };
  1798. static int __init extfrag_debug_init(void)
  1799. {
  1800. struct dentry *extfrag_debug_root;
  1801. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1802. if (!extfrag_debug_root)
  1803. return -ENOMEM;
  1804. if (!debugfs_create_file("unusable_index", 0444,
  1805. extfrag_debug_root, NULL, &unusable_file_ops))
  1806. goto fail;
  1807. if (!debugfs_create_file("extfrag_index", 0444,
  1808. extfrag_debug_root, NULL, &extfrag_file_ops))
  1809. goto fail;
  1810. return 0;
  1811. fail:
  1812. debugfs_remove_recursive(extfrag_debug_root);
  1813. return -ENOMEM;
  1814. }
  1815. module_init(extfrag_debug_init);
  1816. #endif