vmstat.c 55 KB

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