vmstat.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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_page_table_pages",
  791. "nr_kernel_stack",
  792. "nr_bounce",
  793. #if IS_ENABLED(CONFIG_ZSMALLOC)
  794. "nr_zspages",
  795. #endif
  796. #ifdef CONFIG_NUMA
  797. "numa_hit",
  798. "numa_miss",
  799. "numa_foreign",
  800. "numa_interleave",
  801. "numa_local",
  802. "numa_other",
  803. #endif
  804. "nr_free_cma",
  805. /* Node-based counters */
  806. "nr_inactive_anon",
  807. "nr_active_anon",
  808. "nr_inactive_file",
  809. "nr_active_file",
  810. "nr_unevictable",
  811. "nr_slab_reclaimable",
  812. "nr_slab_unreclaimable",
  813. "nr_isolated_anon",
  814. "nr_isolated_file",
  815. "workingset_refault",
  816. "workingset_activate",
  817. "workingset_nodereclaim",
  818. "nr_anon_pages",
  819. "nr_mapped",
  820. "nr_file_pages",
  821. "nr_dirty",
  822. "nr_writeback",
  823. "nr_writeback_temp",
  824. "nr_shmem",
  825. "nr_shmem_hugepages",
  826. "nr_shmem_pmdmapped",
  827. "nr_anon_transparent_hugepages",
  828. "nr_unstable",
  829. "nr_vmscan_write",
  830. "nr_vmscan_immediate_reclaim",
  831. "nr_dirtied",
  832. "nr_written",
  833. /* enum writeback_stat_item counters */
  834. "nr_dirty_threshold",
  835. "nr_dirty_background_threshold",
  836. #ifdef CONFIG_VM_EVENT_COUNTERS
  837. /* enum vm_event_item counters */
  838. "pgpgin",
  839. "pgpgout",
  840. "pswpin",
  841. "pswpout",
  842. TEXTS_FOR_ZONES("pgalloc")
  843. TEXTS_FOR_ZONES("allocstall")
  844. TEXTS_FOR_ZONES("pgskip")
  845. "pgfree",
  846. "pgactivate",
  847. "pgdeactivate",
  848. "pglazyfree",
  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. "oom_kill",
  871. #ifdef CONFIG_NUMA_BALANCING
  872. "numa_pte_updates",
  873. "numa_huge_pte_updates",
  874. "numa_hint_faults",
  875. "numa_hint_faults_local",
  876. "numa_pages_migrated",
  877. #endif
  878. #ifdef CONFIG_MIGRATION
  879. "pgmigrate_success",
  880. "pgmigrate_fail",
  881. #endif
  882. #ifdef CONFIG_COMPACTION
  883. "compact_migrate_scanned",
  884. "compact_free_scanned",
  885. "compact_isolated",
  886. "compact_stall",
  887. "compact_fail",
  888. "compact_success",
  889. "compact_daemon_wake",
  890. "compact_daemon_migrate_scanned",
  891. "compact_daemon_free_scanned",
  892. #endif
  893. #ifdef CONFIG_HUGETLB_PAGE
  894. "htlb_buddy_alloc_success",
  895. "htlb_buddy_alloc_fail",
  896. #endif
  897. "unevictable_pgs_culled",
  898. "unevictable_pgs_scanned",
  899. "unevictable_pgs_rescued",
  900. "unevictable_pgs_mlocked",
  901. "unevictable_pgs_munlocked",
  902. "unevictable_pgs_cleared",
  903. "unevictable_pgs_stranded",
  904. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  905. "thp_fault_alloc",
  906. "thp_fault_fallback",
  907. "thp_collapse_alloc",
  908. "thp_collapse_alloc_failed",
  909. "thp_file_alloc",
  910. "thp_file_mapped",
  911. "thp_split_page",
  912. "thp_split_page_failed",
  913. "thp_deferred_split_page",
  914. "thp_split_pmd",
  915. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  916. "thp_split_pud",
  917. #endif
  918. "thp_zero_page_alloc",
  919. "thp_zero_page_alloc_failed",
  920. #endif
  921. #ifdef CONFIG_MEMORY_BALLOON
  922. "balloon_inflate",
  923. "balloon_deflate",
  924. #ifdef CONFIG_BALLOON_COMPACTION
  925. "balloon_migrate",
  926. #endif
  927. #endif /* CONFIG_MEMORY_BALLOON */
  928. #ifdef CONFIG_DEBUG_TLBFLUSH
  929. #ifdef CONFIG_SMP
  930. "nr_tlb_remote_flush",
  931. "nr_tlb_remote_flush_received",
  932. #endif /* CONFIG_SMP */
  933. "nr_tlb_local_flush_all",
  934. "nr_tlb_local_flush_one",
  935. #endif /* CONFIG_DEBUG_TLBFLUSH */
  936. #ifdef CONFIG_DEBUG_VM_VMACACHE
  937. "vmacache_find_calls",
  938. "vmacache_find_hits",
  939. "vmacache_full_flushes",
  940. #endif
  941. #endif /* CONFIG_VM_EVENTS_COUNTERS */
  942. };
  943. #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
  944. #if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)) || \
  945. defined(CONFIG_PROC_FS)
  946. static void *frag_start(struct seq_file *m, loff_t *pos)
  947. {
  948. pg_data_t *pgdat;
  949. loff_t node = *pos;
  950. for (pgdat = first_online_pgdat();
  951. pgdat && node;
  952. pgdat = next_online_pgdat(pgdat))
  953. --node;
  954. return pgdat;
  955. }
  956. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  957. {
  958. pg_data_t *pgdat = (pg_data_t *)arg;
  959. (*pos)++;
  960. return next_online_pgdat(pgdat);
  961. }
  962. static void frag_stop(struct seq_file *m, void *arg)
  963. {
  964. }
  965. /*
  966. * Walk zones in a node and print using a callback.
  967. * If @assert_populated is true, only use callback for zones that are populated.
  968. */
  969. static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
  970. bool assert_populated, bool nolock,
  971. void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
  972. {
  973. struct zone *zone;
  974. struct zone *node_zones = pgdat->node_zones;
  975. unsigned long flags;
  976. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  977. if (assert_populated && !populated_zone(zone))
  978. continue;
  979. if (!nolock)
  980. spin_lock_irqsave(&zone->lock, flags);
  981. print(m, pgdat, zone);
  982. if (!nolock)
  983. spin_unlock_irqrestore(&zone->lock, flags);
  984. }
  985. }
  986. #endif
  987. #ifdef CONFIG_PROC_FS
  988. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  989. struct zone *zone)
  990. {
  991. int order;
  992. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  993. for (order = 0; order < MAX_ORDER; ++order)
  994. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  995. seq_putc(m, '\n');
  996. }
  997. /*
  998. * This walks the free areas for each zone.
  999. */
  1000. static int frag_show(struct seq_file *m, void *arg)
  1001. {
  1002. pg_data_t *pgdat = (pg_data_t *)arg;
  1003. walk_zones_in_node(m, pgdat, true, false, frag_show_print);
  1004. return 0;
  1005. }
  1006. static void pagetypeinfo_showfree_print(struct seq_file *m,
  1007. pg_data_t *pgdat, struct zone *zone)
  1008. {
  1009. int order, mtype;
  1010. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  1011. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  1012. pgdat->node_id,
  1013. zone->name,
  1014. migratetype_names[mtype]);
  1015. for (order = 0; order < MAX_ORDER; ++order) {
  1016. unsigned long freecount = 0;
  1017. struct free_area *area;
  1018. struct list_head *curr;
  1019. area = &(zone->free_area[order]);
  1020. list_for_each(curr, &area->free_list[mtype])
  1021. freecount++;
  1022. seq_printf(m, "%6lu ", freecount);
  1023. }
  1024. seq_putc(m, '\n');
  1025. }
  1026. }
  1027. /* Print out the free pages at each order for each migatetype */
  1028. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  1029. {
  1030. int order;
  1031. pg_data_t *pgdat = (pg_data_t *)arg;
  1032. /* Print header */
  1033. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  1034. for (order = 0; order < MAX_ORDER; ++order)
  1035. seq_printf(m, "%6d ", order);
  1036. seq_putc(m, '\n');
  1037. walk_zones_in_node(m, pgdat, true, false, pagetypeinfo_showfree_print);
  1038. return 0;
  1039. }
  1040. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  1041. pg_data_t *pgdat, struct zone *zone)
  1042. {
  1043. int mtype;
  1044. unsigned long pfn;
  1045. unsigned long start_pfn = zone->zone_start_pfn;
  1046. unsigned long end_pfn = zone_end_pfn(zone);
  1047. unsigned long count[MIGRATE_TYPES] = { 0, };
  1048. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  1049. struct page *page;
  1050. page = pfn_to_online_page(pfn);
  1051. if (!page)
  1052. continue;
  1053. /* Watch for unexpected holes punched in the memmap */
  1054. if (!memmap_valid_within(pfn, page, zone))
  1055. continue;
  1056. if (page_zone(page) != zone)
  1057. continue;
  1058. mtype = get_pageblock_migratetype(page);
  1059. if (mtype < MIGRATE_TYPES)
  1060. count[mtype]++;
  1061. }
  1062. /* Print counts */
  1063. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  1064. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1065. seq_printf(m, "%12lu ", count[mtype]);
  1066. seq_putc(m, '\n');
  1067. }
  1068. /* Print out the free pages at each order for each migratetype */
  1069. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  1070. {
  1071. int mtype;
  1072. pg_data_t *pgdat = (pg_data_t *)arg;
  1073. seq_printf(m, "\n%-23s", "Number of blocks type ");
  1074. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1075. seq_printf(m, "%12s ", migratetype_names[mtype]);
  1076. seq_putc(m, '\n');
  1077. walk_zones_in_node(m, pgdat, true, false,
  1078. pagetypeinfo_showblockcount_print);
  1079. return 0;
  1080. }
  1081. /*
  1082. * Print out the number of pageblocks for each migratetype that contain pages
  1083. * of other types. This gives an indication of how well fallbacks are being
  1084. * contained by rmqueue_fallback(). It requires information from PAGE_OWNER
  1085. * to determine what is going on
  1086. */
  1087. static void pagetypeinfo_showmixedcount(struct seq_file *m, pg_data_t *pgdat)
  1088. {
  1089. #ifdef CONFIG_PAGE_OWNER
  1090. int mtype;
  1091. if (!static_branch_unlikely(&page_owner_inited))
  1092. return;
  1093. drain_all_pages(NULL);
  1094. seq_printf(m, "\n%-23s", "Number of mixed blocks ");
  1095. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1096. seq_printf(m, "%12s ", migratetype_names[mtype]);
  1097. seq_putc(m, '\n');
  1098. walk_zones_in_node(m, pgdat, true, true,
  1099. pagetypeinfo_showmixedcount_print);
  1100. #endif /* CONFIG_PAGE_OWNER */
  1101. }
  1102. /*
  1103. * This prints out statistics in relation to grouping pages by mobility.
  1104. * It is expensive to collect so do not constantly read the file.
  1105. */
  1106. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  1107. {
  1108. pg_data_t *pgdat = (pg_data_t *)arg;
  1109. /* check memoryless node */
  1110. if (!node_state(pgdat->node_id, N_MEMORY))
  1111. return 0;
  1112. seq_printf(m, "Page block order: %d\n", pageblock_order);
  1113. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  1114. seq_putc(m, '\n');
  1115. pagetypeinfo_showfree(m, pgdat);
  1116. pagetypeinfo_showblockcount(m, pgdat);
  1117. pagetypeinfo_showmixedcount(m, pgdat);
  1118. return 0;
  1119. }
  1120. static const struct seq_operations fragmentation_op = {
  1121. .start = frag_start,
  1122. .next = frag_next,
  1123. .stop = frag_stop,
  1124. .show = frag_show,
  1125. };
  1126. static int fragmentation_open(struct inode *inode, struct file *file)
  1127. {
  1128. return seq_open(file, &fragmentation_op);
  1129. }
  1130. static const struct file_operations buddyinfo_file_operations = {
  1131. .open = fragmentation_open,
  1132. .read = seq_read,
  1133. .llseek = seq_lseek,
  1134. .release = seq_release,
  1135. };
  1136. static const struct seq_operations pagetypeinfo_op = {
  1137. .start = frag_start,
  1138. .next = frag_next,
  1139. .stop = frag_stop,
  1140. .show = pagetypeinfo_show,
  1141. };
  1142. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  1143. {
  1144. return seq_open(file, &pagetypeinfo_op);
  1145. }
  1146. static const struct file_operations pagetypeinfo_file_operations = {
  1147. .open = pagetypeinfo_open,
  1148. .read = seq_read,
  1149. .llseek = seq_lseek,
  1150. .release = seq_release,
  1151. };
  1152. static bool is_zone_first_populated(pg_data_t *pgdat, struct zone *zone)
  1153. {
  1154. int zid;
  1155. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  1156. struct zone *compare = &pgdat->node_zones[zid];
  1157. if (populated_zone(compare))
  1158. return zone == compare;
  1159. }
  1160. return false;
  1161. }
  1162. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  1163. struct zone *zone)
  1164. {
  1165. int i;
  1166. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  1167. if (is_zone_first_populated(pgdat, zone)) {
  1168. seq_printf(m, "\n per-node stats");
  1169. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
  1170. seq_printf(m, "\n %-12s %lu",
  1171. vmstat_text[i + NR_VM_ZONE_STAT_ITEMS],
  1172. node_page_state(pgdat, i));
  1173. }
  1174. }
  1175. seq_printf(m,
  1176. "\n pages free %lu"
  1177. "\n min %lu"
  1178. "\n low %lu"
  1179. "\n high %lu"
  1180. "\n spanned %lu"
  1181. "\n present %lu"
  1182. "\n managed %lu",
  1183. zone_page_state(zone, NR_FREE_PAGES),
  1184. min_wmark_pages(zone),
  1185. low_wmark_pages(zone),
  1186. high_wmark_pages(zone),
  1187. zone->spanned_pages,
  1188. zone->present_pages,
  1189. zone->managed_pages);
  1190. seq_printf(m,
  1191. "\n protection: (%ld",
  1192. zone->lowmem_reserve[0]);
  1193. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  1194. seq_printf(m, ", %ld", zone->lowmem_reserve[i]);
  1195. seq_putc(m, ')');
  1196. /* If unpopulated, no other information is useful */
  1197. if (!populated_zone(zone)) {
  1198. seq_putc(m, '\n');
  1199. return;
  1200. }
  1201. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  1202. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  1203. zone_page_state(zone, i));
  1204. seq_printf(m, "\n pagesets");
  1205. for_each_online_cpu(i) {
  1206. struct per_cpu_pageset *pageset;
  1207. pageset = per_cpu_ptr(zone->pageset, i);
  1208. seq_printf(m,
  1209. "\n cpu: %i"
  1210. "\n count: %i"
  1211. "\n high: %i"
  1212. "\n batch: %i",
  1213. i,
  1214. pageset->pcp.count,
  1215. pageset->pcp.high,
  1216. pageset->pcp.batch);
  1217. #ifdef CONFIG_SMP
  1218. seq_printf(m, "\n vm stats threshold: %d",
  1219. pageset->stat_threshold);
  1220. #endif
  1221. }
  1222. seq_printf(m,
  1223. "\n node_unreclaimable: %u"
  1224. "\n start_pfn: %lu"
  1225. "\n node_inactive_ratio: %u",
  1226. pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES,
  1227. zone->zone_start_pfn,
  1228. zone->zone_pgdat->inactive_ratio);
  1229. seq_putc(m, '\n');
  1230. }
  1231. /*
  1232. * Output information about zones in @pgdat. All zones are printed regardless
  1233. * of whether they are populated or not: lowmem_reserve_ratio operates on the
  1234. * set of all zones and userspace would not be aware of such zones if they are
  1235. * suppressed here (zoneinfo displays the effect of lowmem_reserve_ratio).
  1236. */
  1237. static int zoneinfo_show(struct seq_file *m, void *arg)
  1238. {
  1239. pg_data_t *pgdat = (pg_data_t *)arg;
  1240. walk_zones_in_node(m, pgdat, false, false, zoneinfo_show_print);
  1241. return 0;
  1242. }
  1243. static const struct seq_operations zoneinfo_op = {
  1244. .start = frag_start, /* iterate over all zones. The same as in
  1245. * fragmentation. */
  1246. .next = frag_next,
  1247. .stop = frag_stop,
  1248. .show = zoneinfo_show,
  1249. };
  1250. static int zoneinfo_open(struct inode *inode, struct file *file)
  1251. {
  1252. return seq_open(file, &zoneinfo_op);
  1253. }
  1254. static const struct file_operations zoneinfo_file_operations = {
  1255. .open = zoneinfo_open,
  1256. .read = seq_read,
  1257. .llseek = seq_lseek,
  1258. .release = seq_release,
  1259. };
  1260. enum writeback_stat_item {
  1261. NR_DIRTY_THRESHOLD,
  1262. NR_DIRTY_BG_THRESHOLD,
  1263. NR_VM_WRITEBACK_STAT_ITEMS,
  1264. };
  1265. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  1266. {
  1267. unsigned long *v;
  1268. int i, stat_items_size;
  1269. if (*pos >= ARRAY_SIZE(vmstat_text))
  1270. return NULL;
  1271. stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
  1272. NR_VM_NODE_STAT_ITEMS * sizeof(unsigned long) +
  1273. NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
  1274. #ifdef CONFIG_VM_EVENT_COUNTERS
  1275. stat_items_size += sizeof(struct vm_event_state);
  1276. #endif
  1277. v = kmalloc(stat_items_size, GFP_KERNEL);
  1278. m->private = v;
  1279. if (!v)
  1280. return ERR_PTR(-ENOMEM);
  1281. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  1282. v[i] = global_page_state(i);
  1283. v += NR_VM_ZONE_STAT_ITEMS;
  1284. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
  1285. v[i] = global_node_page_state(i);
  1286. v += NR_VM_NODE_STAT_ITEMS;
  1287. global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
  1288. v + NR_DIRTY_THRESHOLD);
  1289. v += NR_VM_WRITEBACK_STAT_ITEMS;
  1290. #ifdef CONFIG_VM_EVENT_COUNTERS
  1291. all_vm_events(v);
  1292. v[PGPGIN] /= 2; /* sectors -> kbytes */
  1293. v[PGPGOUT] /= 2;
  1294. #endif
  1295. return (unsigned long *)m->private + *pos;
  1296. }
  1297. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  1298. {
  1299. (*pos)++;
  1300. if (*pos >= ARRAY_SIZE(vmstat_text))
  1301. return NULL;
  1302. return (unsigned long *)m->private + *pos;
  1303. }
  1304. static int vmstat_show(struct seq_file *m, void *arg)
  1305. {
  1306. unsigned long *l = arg;
  1307. unsigned long off = l - (unsigned long *)m->private;
  1308. seq_puts(m, vmstat_text[off]);
  1309. seq_put_decimal_ull(m, " ", *l);
  1310. seq_putc(m, '\n');
  1311. return 0;
  1312. }
  1313. static void vmstat_stop(struct seq_file *m, void *arg)
  1314. {
  1315. kfree(m->private);
  1316. m->private = NULL;
  1317. }
  1318. static const struct seq_operations vmstat_op = {
  1319. .start = vmstat_start,
  1320. .next = vmstat_next,
  1321. .stop = vmstat_stop,
  1322. .show = vmstat_show,
  1323. };
  1324. static int vmstat_open(struct inode *inode, struct file *file)
  1325. {
  1326. return seq_open(file, &vmstat_op);
  1327. }
  1328. static const struct file_operations vmstat_file_operations = {
  1329. .open = vmstat_open,
  1330. .read = seq_read,
  1331. .llseek = seq_lseek,
  1332. .release = seq_release,
  1333. };
  1334. #endif /* CONFIG_PROC_FS */
  1335. #ifdef CONFIG_SMP
  1336. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  1337. int sysctl_stat_interval __read_mostly = HZ;
  1338. #ifdef CONFIG_PROC_FS
  1339. static void refresh_vm_stats(struct work_struct *work)
  1340. {
  1341. refresh_cpu_vm_stats(true);
  1342. }
  1343. int vmstat_refresh(struct ctl_table *table, int write,
  1344. void __user *buffer, size_t *lenp, loff_t *ppos)
  1345. {
  1346. long val;
  1347. int err;
  1348. int i;
  1349. /*
  1350. * The regular update, every sysctl_stat_interval, may come later
  1351. * than expected: leaving a significant amount in per_cpu buckets.
  1352. * This is particularly misleading when checking a quantity of HUGE
  1353. * pages, immediately after running a test. /proc/sys/vm/stat_refresh,
  1354. * which can equally be echo'ed to or cat'ted from (by root),
  1355. * can be used to update the stats just before reading them.
  1356. *
  1357. * Oh, and since global_page_state() etc. are so careful to hide
  1358. * transiently negative values, report an error here if any of
  1359. * the stats is negative, so we know to go looking for imbalance.
  1360. */
  1361. err = schedule_on_each_cpu(refresh_vm_stats);
  1362. if (err)
  1363. return err;
  1364. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
  1365. val = atomic_long_read(&vm_zone_stat[i]);
  1366. if (val < 0) {
  1367. pr_warn("%s: %s %ld\n",
  1368. __func__, vmstat_text[i], val);
  1369. err = -EINVAL;
  1370. }
  1371. }
  1372. if (err)
  1373. return err;
  1374. if (write)
  1375. *ppos += *lenp;
  1376. else
  1377. *lenp = 0;
  1378. return 0;
  1379. }
  1380. #endif /* CONFIG_PROC_FS */
  1381. static void vmstat_update(struct work_struct *w)
  1382. {
  1383. if (refresh_cpu_vm_stats(true)) {
  1384. /*
  1385. * Counters were updated so we expect more updates
  1386. * to occur in the future. Keep on running the
  1387. * update worker thread.
  1388. */
  1389. queue_delayed_work_on(smp_processor_id(), mm_percpu_wq,
  1390. this_cpu_ptr(&vmstat_work),
  1391. round_jiffies_relative(sysctl_stat_interval));
  1392. }
  1393. }
  1394. /*
  1395. * Switch off vmstat processing and then fold all the remaining differentials
  1396. * until the diffs stay at zero. The function is used by NOHZ and can only be
  1397. * invoked when tick processing is not active.
  1398. */
  1399. /*
  1400. * Check if the diffs for a certain cpu indicate that
  1401. * an update is needed.
  1402. */
  1403. static bool need_update(int cpu)
  1404. {
  1405. struct zone *zone;
  1406. for_each_populated_zone(zone) {
  1407. struct per_cpu_pageset *p = per_cpu_ptr(zone->pageset, cpu);
  1408. BUILD_BUG_ON(sizeof(p->vm_stat_diff[0]) != 1);
  1409. /*
  1410. * The fast way of checking if there are any vmstat diffs.
  1411. * This works because the diffs are byte sized items.
  1412. */
  1413. if (memchr_inv(p->vm_stat_diff, 0, NR_VM_ZONE_STAT_ITEMS))
  1414. return true;
  1415. }
  1416. return false;
  1417. }
  1418. /*
  1419. * Switch off vmstat processing and then fold all the remaining differentials
  1420. * until the diffs stay at zero. The function is used by NOHZ and can only be
  1421. * invoked when tick processing is not active.
  1422. */
  1423. void quiet_vmstat(void)
  1424. {
  1425. if (system_state != SYSTEM_RUNNING)
  1426. return;
  1427. if (!delayed_work_pending(this_cpu_ptr(&vmstat_work)))
  1428. return;
  1429. if (!need_update(smp_processor_id()))
  1430. return;
  1431. /*
  1432. * Just refresh counters and do not care about the pending delayed
  1433. * vmstat_update. It doesn't fire that often to matter and canceling
  1434. * it would be too expensive from this path.
  1435. * vmstat_shepherd will take care about that for us.
  1436. */
  1437. refresh_cpu_vm_stats(false);
  1438. }
  1439. /*
  1440. * Shepherd worker thread that checks the
  1441. * differentials of processors that have their worker
  1442. * threads for vm statistics updates disabled because of
  1443. * inactivity.
  1444. */
  1445. static void vmstat_shepherd(struct work_struct *w);
  1446. static DECLARE_DEFERRABLE_WORK(shepherd, vmstat_shepherd);
  1447. static void vmstat_shepherd(struct work_struct *w)
  1448. {
  1449. int cpu;
  1450. get_online_cpus();
  1451. /* Check processors whose vmstat worker threads have been disabled */
  1452. for_each_online_cpu(cpu) {
  1453. struct delayed_work *dw = &per_cpu(vmstat_work, cpu);
  1454. if (!delayed_work_pending(dw) && need_update(cpu))
  1455. queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0);
  1456. }
  1457. put_online_cpus();
  1458. schedule_delayed_work(&shepherd,
  1459. round_jiffies_relative(sysctl_stat_interval));
  1460. }
  1461. static void __init start_shepherd_timer(void)
  1462. {
  1463. int cpu;
  1464. for_each_possible_cpu(cpu)
  1465. INIT_DEFERRABLE_WORK(per_cpu_ptr(&vmstat_work, cpu),
  1466. vmstat_update);
  1467. schedule_delayed_work(&shepherd,
  1468. round_jiffies_relative(sysctl_stat_interval));
  1469. }
  1470. static void __init init_cpu_node_state(void)
  1471. {
  1472. int node;
  1473. for_each_online_node(node) {
  1474. if (cpumask_weight(cpumask_of_node(node)) > 0)
  1475. node_set_state(node, N_CPU);
  1476. }
  1477. }
  1478. static int vmstat_cpu_online(unsigned int cpu)
  1479. {
  1480. refresh_zone_stat_thresholds();
  1481. node_set_state(cpu_to_node(cpu), N_CPU);
  1482. return 0;
  1483. }
  1484. static int vmstat_cpu_down_prep(unsigned int cpu)
  1485. {
  1486. cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
  1487. return 0;
  1488. }
  1489. static int vmstat_cpu_dead(unsigned int cpu)
  1490. {
  1491. const struct cpumask *node_cpus;
  1492. int node;
  1493. node = cpu_to_node(cpu);
  1494. refresh_zone_stat_thresholds();
  1495. node_cpus = cpumask_of_node(node);
  1496. if (cpumask_weight(node_cpus) > 0)
  1497. return 0;
  1498. node_clear_state(node, N_CPU);
  1499. return 0;
  1500. }
  1501. #endif
  1502. struct workqueue_struct *mm_percpu_wq;
  1503. void __init init_mm_internals(void)
  1504. {
  1505. int ret __maybe_unused;
  1506. mm_percpu_wq = alloc_workqueue("mm_percpu_wq", WQ_MEM_RECLAIM, 0);
  1507. #ifdef CONFIG_SMP
  1508. ret = cpuhp_setup_state_nocalls(CPUHP_MM_VMSTAT_DEAD, "mm/vmstat:dead",
  1509. NULL, vmstat_cpu_dead);
  1510. if (ret < 0)
  1511. pr_err("vmstat: failed to register 'dead' hotplug state\n");
  1512. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "mm/vmstat:online",
  1513. vmstat_cpu_online,
  1514. vmstat_cpu_down_prep);
  1515. if (ret < 0)
  1516. pr_err("vmstat: failed to register 'online' hotplug state\n");
  1517. get_online_cpus();
  1518. init_cpu_node_state();
  1519. put_online_cpus();
  1520. start_shepherd_timer();
  1521. #endif
  1522. #ifdef CONFIG_PROC_FS
  1523. proc_create("buddyinfo", 0444, NULL, &buddyinfo_file_operations);
  1524. proc_create("pagetypeinfo", 0444, NULL, &pagetypeinfo_file_operations);
  1525. proc_create("vmstat", 0444, NULL, &vmstat_file_operations);
  1526. proc_create("zoneinfo", 0444, NULL, &zoneinfo_file_operations);
  1527. #endif
  1528. }
  1529. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  1530. /*
  1531. * Return an index indicating how much of the available free memory is
  1532. * unusable for an allocation of the requested size.
  1533. */
  1534. static int unusable_free_index(unsigned int order,
  1535. struct contig_page_info *info)
  1536. {
  1537. /* No free memory is interpreted as all free memory is unusable */
  1538. if (info->free_pages == 0)
  1539. return 1000;
  1540. /*
  1541. * Index should be a value between 0 and 1. Return a value to 3
  1542. * decimal places.
  1543. *
  1544. * 0 => no fragmentation
  1545. * 1 => high fragmentation
  1546. */
  1547. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  1548. }
  1549. static void unusable_show_print(struct seq_file *m,
  1550. pg_data_t *pgdat, struct zone *zone)
  1551. {
  1552. unsigned int order;
  1553. int index;
  1554. struct contig_page_info info;
  1555. seq_printf(m, "Node %d, zone %8s ",
  1556. pgdat->node_id,
  1557. zone->name);
  1558. for (order = 0; order < MAX_ORDER; ++order) {
  1559. fill_contig_page_info(zone, order, &info);
  1560. index = unusable_free_index(order, &info);
  1561. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1562. }
  1563. seq_putc(m, '\n');
  1564. }
  1565. /*
  1566. * Display unusable free space index
  1567. *
  1568. * The unusable free space index measures how much of the available free
  1569. * memory cannot be used to satisfy an allocation of a given size and is a
  1570. * value between 0 and 1. The higher the value, the more of free memory is
  1571. * unusable and by implication, the worse the external fragmentation is. This
  1572. * can be expressed as a percentage by multiplying by 100.
  1573. */
  1574. static int unusable_show(struct seq_file *m, void *arg)
  1575. {
  1576. pg_data_t *pgdat = (pg_data_t *)arg;
  1577. /* check memoryless node */
  1578. if (!node_state(pgdat->node_id, N_MEMORY))
  1579. return 0;
  1580. walk_zones_in_node(m, pgdat, true, false, unusable_show_print);
  1581. return 0;
  1582. }
  1583. static const struct seq_operations unusable_op = {
  1584. .start = frag_start,
  1585. .next = frag_next,
  1586. .stop = frag_stop,
  1587. .show = unusable_show,
  1588. };
  1589. static int unusable_open(struct inode *inode, struct file *file)
  1590. {
  1591. return seq_open(file, &unusable_op);
  1592. }
  1593. static const struct file_operations unusable_file_ops = {
  1594. .open = unusable_open,
  1595. .read = seq_read,
  1596. .llseek = seq_lseek,
  1597. .release = seq_release,
  1598. };
  1599. static void extfrag_show_print(struct seq_file *m,
  1600. pg_data_t *pgdat, struct zone *zone)
  1601. {
  1602. unsigned int order;
  1603. int index;
  1604. /* Alloc on stack as interrupts are disabled for zone walk */
  1605. struct contig_page_info info;
  1606. seq_printf(m, "Node %d, zone %8s ",
  1607. pgdat->node_id,
  1608. zone->name);
  1609. for (order = 0; order < MAX_ORDER; ++order) {
  1610. fill_contig_page_info(zone, order, &info);
  1611. index = __fragmentation_index(order, &info);
  1612. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1613. }
  1614. seq_putc(m, '\n');
  1615. }
  1616. /*
  1617. * Display fragmentation index for orders that allocations would fail for
  1618. */
  1619. static int extfrag_show(struct seq_file *m, void *arg)
  1620. {
  1621. pg_data_t *pgdat = (pg_data_t *)arg;
  1622. walk_zones_in_node(m, pgdat, true, false, extfrag_show_print);
  1623. return 0;
  1624. }
  1625. static const struct seq_operations extfrag_op = {
  1626. .start = frag_start,
  1627. .next = frag_next,
  1628. .stop = frag_stop,
  1629. .show = extfrag_show,
  1630. };
  1631. static int extfrag_open(struct inode *inode, struct file *file)
  1632. {
  1633. return seq_open(file, &extfrag_op);
  1634. }
  1635. static const struct file_operations extfrag_file_ops = {
  1636. .open = extfrag_open,
  1637. .read = seq_read,
  1638. .llseek = seq_lseek,
  1639. .release = seq_release,
  1640. };
  1641. static int __init extfrag_debug_init(void)
  1642. {
  1643. struct dentry *extfrag_debug_root;
  1644. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1645. if (!extfrag_debug_root)
  1646. return -ENOMEM;
  1647. if (!debugfs_create_file("unusable_index", 0444,
  1648. extfrag_debug_root, NULL, &unusable_file_ops))
  1649. goto fail;
  1650. if (!debugfs_create_file("extfrag_index", 0444,
  1651. extfrag_debug_root, NULL, &extfrag_file_ops))
  1652. goto fail;
  1653. return 0;
  1654. fail:
  1655. debugfs_remove_recursive(extfrag_debug_root);
  1656. return -ENOMEM;
  1657. }
  1658. module_init(extfrag_debug_init);
  1659. #endif