vmstat.c 49 KB

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