vmstat.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  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. /* enum writeback_stat_item counters */
  991. "nr_dirty_threshold",
  992. "nr_dirty_background_threshold",
  993. #ifdef CONFIG_VM_EVENT_COUNTERS
  994. /* enum vm_event_item counters */
  995. "pgpgin",
  996. "pgpgout",
  997. "pswpin",
  998. "pswpout",
  999. TEXTS_FOR_ZONES("pgalloc")
  1000. TEXTS_FOR_ZONES("allocstall")
  1001. TEXTS_FOR_ZONES("pgskip")
  1002. "pgfree",
  1003. "pgactivate",
  1004. "pgdeactivate",
  1005. "pglazyfree",
  1006. "pgfault",
  1007. "pgmajfault",
  1008. "pglazyfreed",
  1009. "pgrefill",
  1010. "pgsteal_kswapd",
  1011. "pgsteal_direct",
  1012. "pgscan_kswapd",
  1013. "pgscan_direct",
  1014. "pgscan_direct_throttle",
  1015. #ifdef CONFIG_NUMA
  1016. "zone_reclaim_failed",
  1017. #endif
  1018. "pginodesteal",
  1019. "slabs_scanned",
  1020. "kswapd_inodesteal",
  1021. "kswapd_low_wmark_hit_quickly",
  1022. "kswapd_high_wmark_hit_quickly",
  1023. "pageoutrun",
  1024. "pgrotated",
  1025. "drop_pagecache",
  1026. "drop_slab",
  1027. "oom_kill",
  1028. #ifdef CONFIG_NUMA_BALANCING
  1029. "numa_pte_updates",
  1030. "numa_huge_pte_updates",
  1031. "numa_hint_faults",
  1032. "numa_hint_faults_local",
  1033. "numa_pages_migrated",
  1034. #endif
  1035. #ifdef CONFIG_MIGRATION
  1036. "pgmigrate_success",
  1037. "pgmigrate_fail",
  1038. #endif
  1039. #ifdef CONFIG_COMPACTION
  1040. "compact_migrate_scanned",
  1041. "compact_free_scanned",
  1042. "compact_isolated",
  1043. "compact_stall",
  1044. "compact_fail",
  1045. "compact_success",
  1046. "compact_daemon_wake",
  1047. "compact_daemon_migrate_scanned",
  1048. "compact_daemon_free_scanned",
  1049. #endif
  1050. #ifdef CONFIG_HUGETLB_PAGE
  1051. "htlb_buddy_alloc_success",
  1052. "htlb_buddy_alloc_fail",
  1053. #endif
  1054. "unevictable_pgs_culled",
  1055. "unevictable_pgs_scanned",
  1056. "unevictable_pgs_rescued",
  1057. "unevictable_pgs_mlocked",
  1058. "unevictable_pgs_munlocked",
  1059. "unevictable_pgs_cleared",
  1060. "unevictable_pgs_stranded",
  1061. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1062. "thp_fault_alloc",
  1063. "thp_fault_fallback",
  1064. "thp_collapse_alloc",
  1065. "thp_collapse_alloc_failed",
  1066. "thp_file_alloc",
  1067. "thp_file_mapped",
  1068. "thp_split_page",
  1069. "thp_split_page_failed",
  1070. "thp_deferred_split_page",
  1071. "thp_split_pmd",
  1072. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  1073. "thp_split_pud",
  1074. #endif
  1075. "thp_zero_page_alloc",
  1076. "thp_zero_page_alloc_failed",
  1077. "thp_swpout",
  1078. "thp_swpout_fallback",
  1079. #endif
  1080. #ifdef CONFIG_MEMORY_BALLOON
  1081. "balloon_inflate",
  1082. "balloon_deflate",
  1083. #ifdef CONFIG_BALLOON_COMPACTION
  1084. "balloon_migrate",
  1085. #endif
  1086. #endif /* CONFIG_MEMORY_BALLOON */
  1087. #ifdef CONFIG_DEBUG_TLBFLUSH
  1088. #ifdef CONFIG_SMP
  1089. "nr_tlb_remote_flush",
  1090. "nr_tlb_remote_flush_received",
  1091. #endif /* CONFIG_SMP */
  1092. "nr_tlb_local_flush_all",
  1093. "nr_tlb_local_flush_one",
  1094. #endif /* CONFIG_DEBUG_TLBFLUSH */
  1095. #ifdef CONFIG_DEBUG_VM_VMACACHE
  1096. "vmacache_find_calls",
  1097. "vmacache_find_hits",
  1098. "vmacache_full_flushes",
  1099. #endif
  1100. #ifdef CONFIG_SWAP
  1101. "swap_ra",
  1102. "swap_ra_hit",
  1103. #endif
  1104. #endif /* CONFIG_VM_EVENTS_COUNTERS */
  1105. };
  1106. #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
  1107. #if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)) || \
  1108. defined(CONFIG_PROC_FS)
  1109. static void *frag_start(struct seq_file *m, loff_t *pos)
  1110. {
  1111. pg_data_t *pgdat;
  1112. loff_t node = *pos;
  1113. for (pgdat = first_online_pgdat();
  1114. pgdat && node;
  1115. pgdat = next_online_pgdat(pgdat))
  1116. --node;
  1117. return pgdat;
  1118. }
  1119. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  1120. {
  1121. pg_data_t *pgdat = (pg_data_t *)arg;
  1122. (*pos)++;
  1123. return next_online_pgdat(pgdat);
  1124. }
  1125. static void frag_stop(struct seq_file *m, void *arg)
  1126. {
  1127. }
  1128. /*
  1129. * Walk zones in a node and print using a callback.
  1130. * If @assert_populated is true, only use callback for zones that are populated.
  1131. */
  1132. static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
  1133. bool assert_populated, bool nolock,
  1134. void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
  1135. {
  1136. struct zone *zone;
  1137. struct zone *node_zones = pgdat->node_zones;
  1138. unsigned long flags;
  1139. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  1140. if (assert_populated && !populated_zone(zone))
  1141. continue;
  1142. if (!nolock)
  1143. spin_lock_irqsave(&zone->lock, flags);
  1144. print(m, pgdat, zone);
  1145. if (!nolock)
  1146. spin_unlock_irqrestore(&zone->lock, flags);
  1147. }
  1148. }
  1149. #endif
  1150. #ifdef CONFIG_PROC_FS
  1151. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  1152. struct zone *zone)
  1153. {
  1154. int order;
  1155. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  1156. for (order = 0; order < MAX_ORDER; ++order)
  1157. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  1158. seq_putc(m, '\n');
  1159. }
  1160. /*
  1161. * This walks the free areas for each zone.
  1162. */
  1163. static int frag_show(struct seq_file *m, void *arg)
  1164. {
  1165. pg_data_t *pgdat = (pg_data_t *)arg;
  1166. walk_zones_in_node(m, pgdat, true, false, frag_show_print);
  1167. return 0;
  1168. }
  1169. static void pagetypeinfo_showfree_print(struct seq_file *m,
  1170. pg_data_t *pgdat, struct zone *zone)
  1171. {
  1172. int order, mtype;
  1173. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  1174. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  1175. pgdat->node_id,
  1176. zone->name,
  1177. migratetype_names[mtype]);
  1178. for (order = 0; order < MAX_ORDER; ++order) {
  1179. unsigned long freecount = 0;
  1180. struct free_area *area;
  1181. struct list_head *curr;
  1182. area = &(zone->free_area[order]);
  1183. list_for_each(curr, &area->free_list[mtype])
  1184. freecount++;
  1185. seq_printf(m, "%6lu ", freecount);
  1186. }
  1187. seq_putc(m, '\n');
  1188. }
  1189. }
  1190. /* Print out the free pages at each order for each migatetype */
  1191. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  1192. {
  1193. int order;
  1194. pg_data_t *pgdat = (pg_data_t *)arg;
  1195. /* Print header */
  1196. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  1197. for (order = 0; order < MAX_ORDER; ++order)
  1198. seq_printf(m, "%6d ", order);
  1199. seq_putc(m, '\n');
  1200. walk_zones_in_node(m, pgdat, true, false, pagetypeinfo_showfree_print);
  1201. return 0;
  1202. }
  1203. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  1204. pg_data_t *pgdat, struct zone *zone)
  1205. {
  1206. int mtype;
  1207. unsigned long pfn;
  1208. unsigned long start_pfn = zone->zone_start_pfn;
  1209. unsigned long end_pfn = zone_end_pfn(zone);
  1210. unsigned long count[MIGRATE_TYPES] = { 0, };
  1211. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  1212. struct page *page;
  1213. page = pfn_to_online_page(pfn);
  1214. if (!page)
  1215. continue;
  1216. /* Watch for unexpected holes punched in the memmap */
  1217. if (!memmap_valid_within(pfn, page, zone))
  1218. continue;
  1219. if (page_zone(page) != zone)
  1220. continue;
  1221. mtype = get_pageblock_migratetype(page);
  1222. if (mtype < MIGRATE_TYPES)
  1223. count[mtype]++;
  1224. }
  1225. /* Print counts */
  1226. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  1227. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1228. seq_printf(m, "%12lu ", count[mtype]);
  1229. seq_putc(m, '\n');
  1230. }
  1231. /* Print out the number of pageblocks for each migratetype */
  1232. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  1233. {
  1234. int mtype;
  1235. pg_data_t *pgdat = (pg_data_t *)arg;
  1236. seq_printf(m, "\n%-23s", "Number of blocks type ");
  1237. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1238. seq_printf(m, "%12s ", migratetype_names[mtype]);
  1239. seq_putc(m, '\n');
  1240. walk_zones_in_node(m, pgdat, true, false,
  1241. pagetypeinfo_showblockcount_print);
  1242. return 0;
  1243. }
  1244. /*
  1245. * Print out the number of pageblocks for each migratetype that contain pages
  1246. * of other types. This gives an indication of how well fallbacks are being
  1247. * contained by rmqueue_fallback(). It requires information from PAGE_OWNER
  1248. * to determine what is going on
  1249. */
  1250. static void pagetypeinfo_showmixedcount(struct seq_file *m, pg_data_t *pgdat)
  1251. {
  1252. #ifdef CONFIG_PAGE_OWNER
  1253. int mtype;
  1254. if (!static_branch_unlikely(&page_owner_inited))
  1255. return;
  1256. drain_all_pages(NULL);
  1257. seq_printf(m, "\n%-23s", "Number of mixed blocks ");
  1258. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1259. seq_printf(m, "%12s ", migratetype_names[mtype]);
  1260. seq_putc(m, '\n');
  1261. walk_zones_in_node(m, pgdat, true, true,
  1262. pagetypeinfo_showmixedcount_print);
  1263. #endif /* CONFIG_PAGE_OWNER */
  1264. }
  1265. /*
  1266. * This prints out statistics in relation to grouping pages by mobility.
  1267. * It is expensive to collect so do not constantly read the file.
  1268. */
  1269. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  1270. {
  1271. pg_data_t *pgdat = (pg_data_t *)arg;
  1272. /* check memoryless node */
  1273. if (!node_state(pgdat->node_id, N_MEMORY))
  1274. return 0;
  1275. seq_printf(m, "Page block order: %d\n", pageblock_order);
  1276. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  1277. seq_putc(m, '\n');
  1278. pagetypeinfo_showfree(m, pgdat);
  1279. pagetypeinfo_showblockcount(m, pgdat);
  1280. pagetypeinfo_showmixedcount(m, pgdat);
  1281. return 0;
  1282. }
  1283. static const struct seq_operations fragmentation_op = {
  1284. .start = frag_start,
  1285. .next = frag_next,
  1286. .stop = frag_stop,
  1287. .show = frag_show,
  1288. };
  1289. static int fragmentation_open(struct inode *inode, struct file *file)
  1290. {
  1291. return seq_open(file, &fragmentation_op);
  1292. }
  1293. static const struct file_operations buddyinfo_file_operations = {
  1294. .open = fragmentation_open,
  1295. .read = seq_read,
  1296. .llseek = seq_lseek,
  1297. .release = seq_release,
  1298. };
  1299. static const struct seq_operations pagetypeinfo_op = {
  1300. .start = frag_start,
  1301. .next = frag_next,
  1302. .stop = frag_stop,
  1303. .show = pagetypeinfo_show,
  1304. };
  1305. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  1306. {
  1307. return seq_open(file, &pagetypeinfo_op);
  1308. }
  1309. static const struct file_operations pagetypeinfo_file_operations = {
  1310. .open = pagetypeinfo_open,
  1311. .read = seq_read,
  1312. .llseek = seq_lseek,
  1313. .release = seq_release,
  1314. };
  1315. static bool is_zone_first_populated(pg_data_t *pgdat, struct zone *zone)
  1316. {
  1317. int zid;
  1318. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  1319. struct zone *compare = &pgdat->node_zones[zid];
  1320. if (populated_zone(compare))
  1321. return zone == compare;
  1322. }
  1323. return false;
  1324. }
  1325. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  1326. struct zone *zone)
  1327. {
  1328. int i;
  1329. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  1330. if (is_zone_first_populated(pgdat, zone)) {
  1331. seq_printf(m, "\n per-node stats");
  1332. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
  1333. seq_printf(m, "\n %-12s %lu",
  1334. vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
  1335. NR_VM_NUMA_STAT_ITEMS],
  1336. node_page_state(pgdat, i));
  1337. }
  1338. }
  1339. seq_printf(m,
  1340. "\n pages free %lu"
  1341. "\n min %lu"
  1342. "\n low %lu"
  1343. "\n high %lu"
  1344. "\n spanned %lu"
  1345. "\n present %lu"
  1346. "\n managed %lu",
  1347. zone_page_state(zone, NR_FREE_PAGES),
  1348. min_wmark_pages(zone),
  1349. low_wmark_pages(zone),
  1350. high_wmark_pages(zone),
  1351. zone->spanned_pages,
  1352. zone->present_pages,
  1353. zone->managed_pages);
  1354. seq_printf(m,
  1355. "\n protection: (%ld",
  1356. zone->lowmem_reserve[0]);
  1357. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  1358. seq_printf(m, ", %ld", zone->lowmem_reserve[i]);
  1359. seq_putc(m, ')');
  1360. /* If unpopulated, no other information is useful */
  1361. if (!populated_zone(zone)) {
  1362. seq_putc(m, '\n');
  1363. return;
  1364. }
  1365. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  1366. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  1367. zone_page_state(zone, i));
  1368. #ifdef CONFIG_NUMA
  1369. for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
  1370. seq_printf(m, "\n %-12s %lu",
  1371. vmstat_text[i + NR_VM_ZONE_STAT_ITEMS],
  1372. zone_numa_state_snapshot(zone, i));
  1373. #endif
  1374. seq_printf(m, "\n pagesets");
  1375. for_each_online_cpu(i) {
  1376. struct per_cpu_pageset *pageset;
  1377. pageset = per_cpu_ptr(zone->pageset, i);
  1378. seq_printf(m,
  1379. "\n cpu: %i"
  1380. "\n count: %i"
  1381. "\n high: %i"
  1382. "\n batch: %i",
  1383. i,
  1384. pageset->pcp.count,
  1385. pageset->pcp.high,
  1386. pageset->pcp.batch);
  1387. #ifdef CONFIG_SMP
  1388. seq_printf(m, "\n vm stats threshold: %d",
  1389. pageset->stat_threshold);
  1390. #endif
  1391. }
  1392. seq_printf(m,
  1393. "\n node_unreclaimable: %u"
  1394. "\n start_pfn: %lu",
  1395. pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES,
  1396. zone->zone_start_pfn);
  1397. seq_putc(m, '\n');
  1398. }
  1399. /*
  1400. * Output information about zones in @pgdat. All zones are printed regardless
  1401. * of whether they are populated or not: lowmem_reserve_ratio operates on the
  1402. * set of all zones and userspace would not be aware of such zones if they are
  1403. * suppressed here (zoneinfo displays the effect of lowmem_reserve_ratio).
  1404. */
  1405. static int zoneinfo_show(struct seq_file *m, void *arg)
  1406. {
  1407. pg_data_t *pgdat = (pg_data_t *)arg;
  1408. walk_zones_in_node(m, pgdat, false, false, zoneinfo_show_print);
  1409. return 0;
  1410. }
  1411. static const struct seq_operations zoneinfo_op = {
  1412. .start = frag_start, /* iterate over all zones. The same as in
  1413. * fragmentation. */
  1414. .next = frag_next,
  1415. .stop = frag_stop,
  1416. .show = zoneinfo_show,
  1417. };
  1418. static int zoneinfo_open(struct inode *inode, struct file *file)
  1419. {
  1420. return seq_open(file, &zoneinfo_op);
  1421. }
  1422. static const struct file_operations zoneinfo_file_operations = {
  1423. .open = zoneinfo_open,
  1424. .read = seq_read,
  1425. .llseek = seq_lseek,
  1426. .release = seq_release,
  1427. };
  1428. enum writeback_stat_item {
  1429. NR_DIRTY_THRESHOLD,
  1430. NR_DIRTY_BG_THRESHOLD,
  1431. NR_VM_WRITEBACK_STAT_ITEMS,
  1432. };
  1433. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  1434. {
  1435. unsigned long *v;
  1436. int i, stat_items_size;
  1437. if (*pos >= ARRAY_SIZE(vmstat_text))
  1438. return NULL;
  1439. stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
  1440. NR_VM_NUMA_STAT_ITEMS * sizeof(unsigned long) +
  1441. NR_VM_NODE_STAT_ITEMS * sizeof(unsigned long) +
  1442. NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
  1443. #ifdef CONFIG_VM_EVENT_COUNTERS
  1444. stat_items_size += sizeof(struct vm_event_state);
  1445. #endif
  1446. v = kmalloc(stat_items_size, GFP_KERNEL);
  1447. m->private = v;
  1448. if (!v)
  1449. return ERR_PTR(-ENOMEM);
  1450. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  1451. v[i] = global_zone_page_state(i);
  1452. v += NR_VM_ZONE_STAT_ITEMS;
  1453. #ifdef CONFIG_NUMA
  1454. for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
  1455. v[i] = global_numa_state(i);
  1456. v += NR_VM_NUMA_STAT_ITEMS;
  1457. #endif
  1458. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
  1459. v[i] = global_node_page_state(i);
  1460. v += NR_VM_NODE_STAT_ITEMS;
  1461. global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
  1462. v + NR_DIRTY_THRESHOLD);
  1463. v += NR_VM_WRITEBACK_STAT_ITEMS;
  1464. #ifdef CONFIG_VM_EVENT_COUNTERS
  1465. all_vm_events(v);
  1466. v[PGPGIN] /= 2; /* sectors -> kbytes */
  1467. v[PGPGOUT] /= 2;
  1468. #endif
  1469. return (unsigned long *)m->private + *pos;
  1470. }
  1471. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  1472. {
  1473. (*pos)++;
  1474. if (*pos >= ARRAY_SIZE(vmstat_text))
  1475. return NULL;
  1476. return (unsigned long *)m->private + *pos;
  1477. }
  1478. static int vmstat_show(struct seq_file *m, void *arg)
  1479. {
  1480. unsigned long *l = arg;
  1481. unsigned long off = l - (unsigned long *)m->private;
  1482. seq_puts(m, vmstat_text[off]);
  1483. seq_put_decimal_ull(m, " ", *l);
  1484. seq_putc(m, '\n');
  1485. return 0;
  1486. }
  1487. static void vmstat_stop(struct seq_file *m, void *arg)
  1488. {
  1489. kfree(m->private);
  1490. m->private = NULL;
  1491. }
  1492. static const struct seq_operations vmstat_op = {
  1493. .start = vmstat_start,
  1494. .next = vmstat_next,
  1495. .stop = vmstat_stop,
  1496. .show = vmstat_show,
  1497. };
  1498. static int vmstat_open(struct inode *inode, struct file *file)
  1499. {
  1500. return seq_open(file, &vmstat_op);
  1501. }
  1502. static const struct file_operations vmstat_file_operations = {
  1503. .open = vmstat_open,
  1504. .read = seq_read,
  1505. .llseek = seq_lseek,
  1506. .release = seq_release,
  1507. };
  1508. #endif /* CONFIG_PROC_FS */
  1509. #ifdef CONFIG_SMP
  1510. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  1511. int sysctl_stat_interval __read_mostly = HZ;
  1512. #ifdef CONFIG_PROC_FS
  1513. static void refresh_vm_stats(struct work_struct *work)
  1514. {
  1515. refresh_cpu_vm_stats(true);
  1516. }
  1517. int vmstat_refresh(struct ctl_table *table, int write,
  1518. void __user *buffer, size_t *lenp, loff_t *ppos)
  1519. {
  1520. long val;
  1521. int err;
  1522. int i;
  1523. /*
  1524. * The regular update, every sysctl_stat_interval, may come later
  1525. * than expected: leaving a significant amount in per_cpu buckets.
  1526. * This is particularly misleading when checking a quantity of HUGE
  1527. * pages, immediately after running a test. /proc/sys/vm/stat_refresh,
  1528. * which can equally be echo'ed to or cat'ted from (by root),
  1529. * can be used to update the stats just before reading them.
  1530. *
  1531. * Oh, and since global_zone_page_state() etc. are so careful to hide
  1532. * transiently negative values, report an error here if any of
  1533. * the stats is negative, so we know to go looking for imbalance.
  1534. */
  1535. err = schedule_on_each_cpu(refresh_vm_stats);
  1536. if (err)
  1537. return err;
  1538. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
  1539. val = atomic_long_read(&vm_zone_stat[i]);
  1540. if (val < 0) {
  1541. pr_warn("%s: %s %ld\n",
  1542. __func__, vmstat_text[i], val);
  1543. err = -EINVAL;
  1544. }
  1545. }
  1546. #ifdef CONFIG_NUMA
  1547. for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
  1548. val = atomic_long_read(&vm_numa_stat[i]);
  1549. if (val < 0) {
  1550. pr_warn("%s: %s %ld\n",
  1551. __func__, vmstat_text[i + NR_VM_ZONE_STAT_ITEMS], val);
  1552. err = -EINVAL;
  1553. }
  1554. }
  1555. #endif
  1556. if (err)
  1557. return err;
  1558. if (write)
  1559. *ppos += *lenp;
  1560. else
  1561. *lenp = 0;
  1562. return 0;
  1563. }
  1564. #endif /* CONFIG_PROC_FS */
  1565. static void vmstat_update(struct work_struct *w)
  1566. {
  1567. if (refresh_cpu_vm_stats(true)) {
  1568. /*
  1569. * Counters were updated so we expect more updates
  1570. * to occur in the future. Keep on running the
  1571. * update worker thread.
  1572. */
  1573. queue_delayed_work_on(smp_processor_id(), mm_percpu_wq,
  1574. this_cpu_ptr(&vmstat_work),
  1575. round_jiffies_relative(sysctl_stat_interval));
  1576. }
  1577. }
  1578. /*
  1579. * Switch off vmstat processing and then fold all the remaining differentials
  1580. * until the diffs stay at zero. The function is used by NOHZ and can only be
  1581. * invoked when tick processing is not active.
  1582. */
  1583. /*
  1584. * Check if the diffs for a certain cpu indicate that
  1585. * an update is needed.
  1586. */
  1587. static bool need_update(int cpu)
  1588. {
  1589. struct zone *zone;
  1590. for_each_populated_zone(zone) {
  1591. struct per_cpu_pageset *p = per_cpu_ptr(zone->pageset, cpu);
  1592. BUILD_BUG_ON(sizeof(p->vm_stat_diff[0]) != 1);
  1593. #ifdef CONFIG_NUMA
  1594. BUILD_BUG_ON(sizeof(p->vm_numa_stat_diff[0]) != 2);
  1595. #endif
  1596. /*
  1597. * The fast way of checking if there are any vmstat diffs.
  1598. * This works because the diffs are byte sized items.
  1599. */
  1600. if (memchr_inv(p->vm_stat_diff, 0, NR_VM_ZONE_STAT_ITEMS))
  1601. return true;
  1602. #ifdef CONFIG_NUMA
  1603. if (memchr_inv(p->vm_numa_stat_diff, 0, NR_VM_NUMA_STAT_ITEMS))
  1604. return true;
  1605. #endif
  1606. }
  1607. return false;
  1608. }
  1609. /*
  1610. * Switch off vmstat processing and then fold all the remaining differentials
  1611. * until the diffs stay at zero. The function is used by NOHZ and can only be
  1612. * invoked when tick processing is not active.
  1613. */
  1614. void quiet_vmstat(void)
  1615. {
  1616. if (system_state != SYSTEM_RUNNING)
  1617. return;
  1618. if (!delayed_work_pending(this_cpu_ptr(&vmstat_work)))
  1619. return;
  1620. if (!need_update(smp_processor_id()))
  1621. return;
  1622. /*
  1623. * Just refresh counters and do not care about the pending delayed
  1624. * vmstat_update. It doesn't fire that often to matter and canceling
  1625. * it would be too expensive from this path.
  1626. * vmstat_shepherd will take care about that for us.
  1627. */
  1628. refresh_cpu_vm_stats(false);
  1629. }
  1630. /*
  1631. * Shepherd worker thread that checks the
  1632. * differentials of processors that have their worker
  1633. * threads for vm statistics updates disabled because of
  1634. * inactivity.
  1635. */
  1636. static void vmstat_shepherd(struct work_struct *w);
  1637. static DECLARE_DEFERRABLE_WORK(shepherd, vmstat_shepherd);
  1638. static void vmstat_shepherd(struct work_struct *w)
  1639. {
  1640. int cpu;
  1641. get_online_cpus();
  1642. /* Check processors whose vmstat worker threads have been disabled */
  1643. for_each_online_cpu(cpu) {
  1644. struct delayed_work *dw = &per_cpu(vmstat_work, cpu);
  1645. if (!delayed_work_pending(dw) && need_update(cpu))
  1646. queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0);
  1647. }
  1648. put_online_cpus();
  1649. schedule_delayed_work(&shepherd,
  1650. round_jiffies_relative(sysctl_stat_interval));
  1651. }
  1652. static void __init start_shepherd_timer(void)
  1653. {
  1654. int cpu;
  1655. for_each_possible_cpu(cpu)
  1656. INIT_DEFERRABLE_WORK(per_cpu_ptr(&vmstat_work, cpu),
  1657. vmstat_update);
  1658. schedule_delayed_work(&shepherd,
  1659. round_jiffies_relative(sysctl_stat_interval));
  1660. }
  1661. static void __init init_cpu_node_state(void)
  1662. {
  1663. int node;
  1664. for_each_online_node(node) {
  1665. if (cpumask_weight(cpumask_of_node(node)) > 0)
  1666. node_set_state(node, N_CPU);
  1667. }
  1668. }
  1669. static int vmstat_cpu_online(unsigned int cpu)
  1670. {
  1671. refresh_zone_stat_thresholds();
  1672. node_set_state(cpu_to_node(cpu), N_CPU);
  1673. return 0;
  1674. }
  1675. static int vmstat_cpu_down_prep(unsigned int cpu)
  1676. {
  1677. cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
  1678. return 0;
  1679. }
  1680. static int vmstat_cpu_dead(unsigned int cpu)
  1681. {
  1682. const struct cpumask *node_cpus;
  1683. int node;
  1684. node = cpu_to_node(cpu);
  1685. refresh_zone_stat_thresholds();
  1686. node_cpus = cpumask_of_node(node);
  1687. if (cpumask_weight(node_cpus) > 0)
  1688. return 0;
  1689. node_clear_state(node, N_CPU);
  1690. return 0;
  1691. }
  1692. #endif
  1693. struct workqueue_struct *mm_percpu_wq;
  1694. void __init init_mm_internals(void)
  1695. {
  1696. int ret __maybe_unused;
  1697. mm_percpu_wq = alloc_workqueue("mm_percpu_wq", WQ_MEM_RECLAIM, 0);
  1698. #ifdef CONFIG_SMP
  1699. ret = cpuhp_setup_state_nocalls(CPUHP_MM_VMSTAT_DEAD, "mm/vmstat:dead",
  1700. NULL, vmstat_cpu_dead);
  1701. if (ret < 0)
  1702. pr_err("vmstat: failed to register 'dead' hotplug state\n");
  1703. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "mm/vmstat:online",
  1704. vmstat_cpu_online,
  1705. vmstat_cpu_down_prep);
  1706. if (ret < 0)
  1707. pr_err("vmstat: failed to register 'online' hotplug state\n");
  1708. get_online_cpus();
  1709. init_cpu_node_state();
  1710. put_online_cpus();
  1711. start_shepherd_timer();
  1712. #endif
  1713. #ifdef CONFIG_PROC_FS
  1714. proc_create("buddyinfo", 0444, NULL, &buddyinfo_file_operations);
  1715. proc_create("pagetypeinfo", 0444, NULL, &pagetypeinfo_file_operations);
  1716. proc_create("vmstat", 0444, NULL, &vmstat_file_operations);
  1717. proc_create("zoneinfo", 0444, NULL, &zoneinfo_file_operations);
  1718. #endif
  1719. }
  1720. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  1721. /*
  1722. * Return an index indicating how much of the available free memory is
  1723. * unusable for an allocation of the requested size.
  1724. */
  1725. static int unusable_free_index(unsigned int order,
  1726. struct contig_page_info *info)
  1727. {
  1728. /* No free memory is interpreted as all free memory is unusable */
  1729. if (info->free_pages == 0)
  1730. return 1000;
  1731. /*
  1732. * Index should be a value between 0 and 1. Return a value to 3
  1733. * decimal places.
  1734. *
  1735. * 0 => no fragmentation
  1736. * 1 => high fragmentation
  1737. */
  1738. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  1739. }
  1740. static void unusable_show_print(struct seq_file *m,
  1741. pg_data_t *pgdat, struct zone *zone)
  1742. {
  1743. unsigned int order;
  1744. int index;
  1745. struct contig_page_info info;
  1746. seq_printf(m, "Node %d, zone %8s ",
  1747. pgdat->node_id,
  1748. zone->name);
  1749. for (order = 0; order < MAX_ORDER; ++order) {
  1750. fill_contig_page_info(zone, order, &info);
  1751. index = unusable_free_index(order, &info);
  1752. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1753. }
  1754. seq_putc(m, '\n');
  1755. }
  1756. /*
  1757. * Display unusable free space index
  1758. *
  1759. * The unusable free space index measures how much of the available free
  1760. * memory cannot be used to satisfy an allocation of a given size and is a
  1761. * value between 0 and 1. The higher the value, the more of free memory is
  1762. * unusable and by implication, the worse the external fragmentation is. This
  1763. * can be expressed as a percentage by multiplying by 100.
  1764. */
  1765. static int unusable_show(struct seq_file *m, void *arg)
  1766. {
  1767. pg_data_t *pgdat = (pg_data_t *)arg;
  1768. /* check memoryless node */
  1769. if (!node_state(pgdat->node_id, N_MEMORY))
  1770. return 0;
  1771. walk_zones_in_node(m, pgdat, true, false, unusable_show_print);
  1772. return 0;
  1773. }
  1774. static const struct seq_operations unusable_op = {
  1775. .start = frag_start,
  1776. .next = frag_next,
  1777. .stop = frag_stop,
  1778. .show = unusable_show,
  1779. };
  1780. static int unusable_open(struct inode *inode, struct file *file)
  1781. {
  1782. return seq_open(file, &unusable_op);
  1783. }
  1784. static const struct file_operations unusable_file_ops = {
  1785. .open = unusable_open,
  1786. .read = seq_read,
  1787. .llseek = seq_lseek,
  1788. .release = seq_release,
  1789. };
  1790. static void extfrag_show_print(struct seq_file *m,
  1791. pg_data_t *pgdat, struct zone *zone)
  1792. {
  1793. unsigned int order;
  1794. int index;
  1795. /* Alloc on stack as interrupts are disabled for zone walk */
  1796. struct contig_page_info info;
  1797. seq_printf(m, "Node %d, zone %8s ",
  1798. pgdat->node_id,
  1799. zone->name);
  1800. for (order = 0; order < MAX_ORDER; ++order) {
  1801. fill_contig_page_info(zone, order, &info);
  1802. index = __fragmentation_index(order, &info);
  1803. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1804. }
  1805. seq_putc(m, '\n');
  1806. }
  1807. /*
  1808. * Display fragmentation index for orders that allocations would fail for
  1809. */
  1810. static int extfrag_show(struct seq_file *m, void *arg)
  1811. {
  1812. pg_data_t *pgdat = (pg_data_t *)arg;
  1813. walk_zones_in_node(m, pgdat, true, false, extfrag_show_print);
  1814. return 0;
  1815. }
  1816. static const struct seq_operations extfrag_op = {
  1817. .start = frag_start,
  1818. .next = frag_next,
  1819. .stop = frag_stop,
  1820. .show = extfrag_show,
  1821. };
  1822. static int extfrag_open(struct inode *inode, struct file *file)
  1823. {
  1824. return seq_open(file, &extfrag_op);
  1825. }
  1826. static const struct file_operations extfrag_file_ops = {
  1827. .open = extfrag_open,
  1828. .read = seq_read,
  1829. .llseek = seq_lseek,
  1830. .release = seq_release,
  1831. };
  1832. static int __init extfrag_debug_init(void)
  1833. {
  1834. struct dentry *extfrag_debug_root;
  1835. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1836. if (!extfrag_debug_root)
  1837. return -ENOMEM;
  1838. if (!debugfs_create_file("unusable_index", 0444,
  1839. extfrag_debug_root, NULL, &unusable_file_ops))
  1840. goto fail;
  1841. if (!debugfs_create_file("extfrag_index", 0444,
  1842. extfrag_debug_root, NULL, &extfrag_file_ops))
  1843. goto fail;
  1844. return 0;
  1845. fail:
  1846. debugfs_remove_recursive(extfrag_debug_root);
  1847. return -ENOMEM;
  1848. }
  1849. module_init(extfrag_debug_init);
  1850. #endif