vmstat.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  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_overhead",
  795. "nr_bounce",
  796. #if IS_ENABLED(CONFIG_ZSMALLOC)
  797. "nr_zspages",
  798. #endif
  799. #ifdef CONFIG_NUMA
  800. "numa_hit",
  801. "numa_miss",
  802. "numa_foreign",
  803. "numa_interleave",
  804. "numa_local",
  805. "numa_other",
  806. #endif
  807. "nr_free_cma",
  808. /* Node-based counters */
  809. "nr_inactive_anon",
  810. "nr_active_anon",
  811. "nr_inactive_file",
  812. "nr_active_file",
  813. "nr_unevictable",
  814. "nr_isolated_anon",
  815. "nr_isolated_file",
  816. "nr_pages_scanned",
  817. "workingset_refault",
  818. "workingset_activate",
  819. "workingset_nodereclaim",
  820. "nr_anon_pages",
  821. "nr_mapped",
  822. "nr_file_pages",
  823. "nr_dirty",
  824. "nr_writeback",
  825. "nr_writeback_temp",
  826. "nr_shmem",
  827. "nr_shmem_hugepages",
  828. "nr_shmem_pmdmapped",
  829. "nr_anon_transparent_hugepages",
  830. "nr_unstable",
  831. "nr_vmscan_write",
  832. "nr_vmscan_immediate_reclaim",
  833. "nr_dirtied",
  834. "nr_written",
  835. /* enum writeback_stat_item counters */
  836. "nr_dirty_threshold",
  837. "nr_dirty_background_threshold",
  838. #ifdef CONFIG_VM_EVENT_COUNTERS
  839. /* enum vm_event_item counters */
  840. "pgpgin",
  841. "pgpgout",
  842. "pswpin",
  843. "pswpout",
  844. TEXTS_FOR_ZONES("pgalloc")
  845. TEXTS_FOR_ZONES("allocstall")
  846. TEXTS_FOR_ZONES("pgskip")
  847. "pgfree",
  848. "pgactivate",
  849. "pgdeactivate",
  850. "pgfault",
  851. "pgmajfault",
  852. "pglazyfreed",
  853. "pgrefill",
  854. "pgsteal_kswapd",
  855. "pgsteal_direct",
  856. "pgscan_kswapd",
  857. "pgscan_direct",
  858. "pgscan_direct_throttle",
  859. #ifdef CONFIG_NUMA
  860. "zone_reclaim_failed",
  861. #endif
  862. "pginodesteal",
  863. "slabs_scanned",
  864. "kswapd_inodesteal",
  865. "kswapd_low_wmark_hit_quickly",
  866. "kswapd_high_wmark_hit_quickly",
  867. "pageoutrun",
  868. "pgrotated",
  869. "drop_pagecache",
  870. "drop_slab",
  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. #endif
  891. #ifdef CONFIG_HUGETLB_PAGE
  892. "htlb_buddy_alloc_success",
  893. "htlb_buddy_alloc_fail",
  894. #endif
  895. "unevictable_pgs_culled",
  896. "unevictable_pgs_scanned",
  897. "unevictable_pgs_rescued",
  898. "unevictable_pgs_mlocked",
  899. "unevictable_pgs_munlocked",
  900. "unevictable_pgs_cleared",
  901. "unevictable_pgs_stranded",
  902. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  903. "thp_fault_alloc",
  904. "thp_fault_fallback",
  905. "thp_collapse_alloc",
  906. "thp_collapse_alloc_failed",
  907. "thp_file_alloc",
  908. "thp_file_mapped",
  909. "thp_split_page",
  910. "thp_split_page_failed",
  911. "thp_deferred_split_page",
  912. "thp_split_pmd",
  913. "thp_zero_page_alloc",
  914. "thp_zero_page_alloc_failed",
  915. #endif
  916. #ifdef CONFIG_MEMORY_BALLOON
  917. "balloon_inflate",
  918. "balloon_deflate",
  919. #ifdef CONFIG_BALLOON_COMPACTION
  920. "balloon_migrate",
  921. #endif
  922. #endif /* CONFIG_MEMORY_BALLOON */
  923. #ifdef CONFIG_DEBUG_TLBFLUSH
  924. #ifdef CONFIG_SMP
  925. "nr_tlb_remote_flush",
  926. "nr_tlb_remote_flush_received",
  927. #endif /* CONFIG_SMP */
  928. "nr_tlb_local_flush_all",
  929. "nr_tlb_local_flush_one",
  930. #endif /* CONFIG_DEBUG_TLBFLUSH */
  931. #ifdef CONFIG_DEBUG_VM_VMACACHE
  932. "vmacache_find_calls",
  933. "vmacache_find_hits",
  934. "vmacache_full_flushes",
  935. #endif
  936. #endif /* CONFIG_VM_EVENTS_COUNTERS */
  937. };
  938. #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
  939. #if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)) || \
  940. defined(CONFIG_PROC_FS)
  941. static void *frag_start(struct seq_file *m, loff_t *pos)
  942. {
  943. pg_data_t *pgdat;
  944. loff_t node = *pos;
  945. for (pgdat = first_online_pgdat();
  946. pgdat && node;
  947. pgdat = next_online_pgdat(pgdat))
  948. --node;
  949. return pgdat;
  950. }
  951. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  952. {
  953. pg_data_t *pgdat = (pg_data_t *)arg;
  954. (*pos)++;
  955. return next_online_pgdat(pgdat);
  956. }
  957. static void frag_stop(struct seq_file *m, void *arg)
  958. {
  959. }
  960. /* Walk all the zones in a node and print using a callback */
  961. static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
  962. void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
  963. {
  964. struct zone *zone;
  965. struct zone *node_zones = pgdat->node_zones;
  966. unsigned long flags;
  967. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  968. if (!populated_zone(zone))
  969. continue;
  970. spin_lock_irqsave(&zone->lock, flags);
  971. print(m, pgdat, zone);
  972. spin_unlock_irqrestore(&zone->lock, flags);
  973. }
  974. }
  975. #endif
  976. #ifdef CONFIG_PROC_FS
  977. static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
  978. struct zone *zone)
  979. {
  980. int order;
  981. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  982. for (order = 0; order < MAX_ORDER; ++order)
  983. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  984. seq_putc(m, '\n');
  985. }
  986. /*
  987. * This walks the free areas for each zone.
  988. */
  989. static int frag_show(struct seq_file *m, void *arg)
  990. {
  991. pg_data_t *pgdat = (pg_data_t *)arg;
  992. walk_zones_in_node(m, pgdat, frag_show_print);
  993. return 0;
  994. }
  995. static void pagetypeinfo_showfree_print(struct seq_file *m,
  996. pg_data_t *pgdat, struct zone *zone)
  997. {
  998. int order, mtype;
  999. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
  1000. seq_printf(m, "Node %4d, zone %8s, type %12s ",
  1001. pgdat->node_id,
  1002. zone->name,
  1003. migratetype_names[mtype]);
  1004. for (order = 0; order < MAX_ORDER; ++order) {
  1005. unsigned long freecount = 0;
  1006. struct free_area *area;
  1007. struct list_head *curr;
  1008. area = &(zone->free_area[order]);
  1009. list_for_each(curr, &area->free_list[mtype])
  1010. freecount++;
  1011. seq_printf(m, "%6lu ", freecount);
  1012. }
  1013. seq_putc(m, '\n');
  1014. }
  1015. }
  1016. /* Print out the free pages at each order for each migatetype */
  1017. static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
  1018. {
  1019. int order;
  1020. pg_data_t *pgdat = (pg_data_t *)arg;
  1021. /* Print header */
  1022. seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
  1023. for (order = 0; order < MAX_ORDER; ++order)
  1024. seq_printf(m, "%6d ", order);
  1025. seq_putc(m, '\n');
  1026. walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
  1027. return 0;
  1028. }
  1029. static void pagetypeinfo_showblockcount_print(struct seq_file *m,
  1030. pg_data_t *pgdat, struct zone *zone)
  1031. {
  1032. int mtype;
  1033. unsigned long pfn;
  1034. unsigned long start_pfn = zone->zone_start_pfn;
  1035. unsigned long end_pfn = zone_end_pfn(zone);
  1036. unsigned long count[MIGRATE_TYPES] = { 0, };
  1037. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  1038. struct page *page;
  1039. if (!pfn_valid(pfn))
  1040. continue;
  1041. page = pfn_to_page(pfn);
  1042. /* Watch for unexpected holes punched in the memmap */
  1043. if (!memmap_valid_within(pfn, page, zone))
  1044. continue;
  1045. if (page_zone(page) != zone)
  1046. continue;
  1047. mtype = get_pageblock_migratetype(page);
  1048. if (mtype < MIGRATE_TYPES)
  1049. count[mtype]++;
  1050. }
  1051. /* Print counts */
  1052. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  1053. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1054. seq_printf(m, "%12lu ", count[mtype]);
  1055. seq_putc(m, '\n');
  1056. }
  1057. /* Print out the free pages at each order for each migratetype */
  1058. static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
  1059. {
  1060. int mtype;
  1061. pg_data_t *pgdat = (pg_data_t *)arg;
  1062. seq_printf(m, "\n%-23s", "Number of blocks type ");
  1063. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1064. seq_printf(m, "%12s ", migratetype_names[mtype]);
  1065. seq_putc(m, '\n');
  1066. walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
  1067. return 0;
  1068. }
  1069. /*
  1070. * Print out the number of pageblocks for each migratetype that contain pages
  1071. * of other types. This gives an indication of how well fallbacks are being
  1072. * contained by rmqueue_fallback(). It requires information from PAGE_OWNER
  1073. * to determine what is going on
  1074. */
  1075. static void pagetypeinfo_showmixedcount(struct seq_file *m, pg_data_t *pgdat)
  1076. {
  1077. #ifdef CONFIG_PAGE_OWNER
  1078. int mtype;
  1079. if (!static_branch_unlikely(&page_owner_inited))
  1080. return;
  1081. drain_all_pages(NULL);
  1082. seq_printf(m, "\n%-23s", "Number of mixed blocks ");
  1083. for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
  1084. seq_printf(m, "%12s ", migratetype_names[mtype]);
  1085. seq_putc(m, '\n');
  1086. walk_zones_in_node(m, pgdat, pagetypeinfo_showmixedcount_print);
  1087. #endif /* CONFIG_PAGE_OWNER */
  1088. }
  1089. /*
  1090. * This prints out statistics in relation to grouping pages by mobility.
  1091. * It is expensive to collect so do not constantly read the file.
  1092. */
  1093. static int pagetypeinfo_show(struct seq_file *m, void *arg)
  1094. {
  1095. pg_data_t *pgdat = (pg_data_t *)arg;
  1096. /* check memoryless node */
  1097. if (!node_state(pgdat->node_id, N_MEMORY))
  1098. return 0;
  1099. seq_printf(m, "Page block order: %d\n", pageblock_order);
  1100. seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
  1101. seq_putc(m, '\n');
  1102. pagetypeinfo_showfree(m, pgdat);
  1103. pagetypeinfo_showblockcount(m, pgdat);
  1104. pagetypeinfo_showmixedcount(m, pgdat);
  1105. return 0;
  1106. }
  1107. static const struct seq_operations fragmentation_op = {
  1108. .start = frag_start,
  1109. .next = frag_next,
  1110. .stop = frag_stop,
  1111. .show = frag_show,
  1112. };
  1113. static int fragmentation_open(struct inode *inode, struct file *file)
  1114. {
  1115. return seq_open(file, &fragmentation_op);
  1116. }
  1117. static const struct file_operations fragmentation_file_operations = {
  1118. .open = fragmentation_open,
  1119. .read = seq_read,
  1120. .llseek = seq_lseek,
  1121. .release = seq_release,
  1122. };
  1123. static const struct seq_operations pagetypeinfo_op = {
  1124. .start = frag_start,
  1125. .next = frag_next,
  1126. .stop = frag_stop,
  1127. .show = pagetypeinfo_show,
  1128. };
  1129. static int pagetypeinfo_open(struct inode *inode, struct file *file)
  1130. {
  1131. return seq_open(file, &pagetypeinfo_op);
  1132. }
  1133. static const struct file_operations pagetypeinfo_file_ops = {
  1134. .open = pagetypeinfo_open,
  1135. .read = seq_read,
  1136. .llseek = seq_lseek,
  1137. .release = seq_release,
  1138. };
  1139. static bool is_zone_first_populated(pg_data_t *pgdat, struct zone *zone)
  1140. {
  1141. int zid;
  1142. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  1143. struct zone *compare = &pgdat->node_zones[zid];
  1144. if (populated_zone(compare))
  1145. return zone == compare;
  1146. }
  1147. return false;
  1148. }
  1149. static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
  1150. struct zone *zone)
  1151. {
  1152. int i;
  1153. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  1154. if (is_zone_first_populated(pgdat, zone)) {
  1155. seq_printf(m, "\n per-node stats");
  1156. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
  1157. seq_printf(m, "\n %-12s %lu",
  1158. vmstat_text[i + NR_VM_ZONE_STAT_ITEMS],
  1159. node_page_state(pgdat, i));
  1160. }
  1161. }
  1162. seq_printf(m,
  1163. "\n pages free %lu"
  1164. "\n min %lu"
  1165. "\n low %lu"
  1166. "\n high %lu"
  1167. "\n node_scanned %lu"
  1168. "\n spanned %lu"
  1169. "\n present %lu"
  1170. "\n managed %lu",
  1171. zone_page_state(zone, NR_FREE_PAGES),
  1172. min_wmark_pages(zone),
  1173. low_wmark_pages(zone),
  1174. high_wmark_pages(zone),
  1175. node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED),
  1176. zone->spanned_pages,
  1177. zone->present_pages,
  1178. zone->managed_pages);
  1179. seq_printf(m,
  1180. "\n protection: (%ld",
  1181. zone->lowmem_reserve[0]);
  1182. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  1183. seq_printf(m, ", %ld", zone->lowmem_reserve[i]);
  1184. seq_putc(m, ')');
  1185. /* If unpopulated, no other information is useful */
  1186. if (!populated_zone(zone)) {
  1187. seq_putc(m, '\n');
  1188. return;
  1189. }
  1190. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  1191. seq_printf(m, "\n %-12s %lu", vmstat_text[i],
  1192. zone_page_state(zone, i));
  1193. seq_printf(m, "\n pagesets");
  1194. for_each_online_cpu(i) {
  1195. struct per_cpu_pageset *pageset;
  1196. pageset = per_cpu_ptr(zone->pageset, i);
  1197. seq_printf(m,
  1198. "\n cpu: %i"
  1199. "\n count: %i"
  1200. "\n high: %i"
  1201. "\n batch: %i",
  1202. i,
  1203. pageset->pcp.count,
  1204. pageset->pcp.high,
  1205. pageset->pcp.batch);
  1206. #ifdef CONFIG_SMP
  1207. seq_printf(m, "\n vm stats threshold: %d",
  1208. pageset->stat_threshold);
  1209. #endif
  1210. }
  1211. seq_printf(m,
  1212. "\n node_unreclaimable: %u"
  1213. "\n start_pfn: %lu"
  1214. "\n node_inactive_ratio: %u",
  1215. pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES,
  1216. zone->zone_start_pfn,
  1217. zone->zone_pgdat->inactive_ratio);
  1218. seq_putc(m, '\n');
  1219. }
  1220. /*
  1221. * Output information about zones in @pgdat.
  1222. */
  1223. static int zoneinfo_show(struct seq_file *m, void *arg)
  1224. {
  1225. pg_data_t *pgdat = (pg_data_t *)arg;
  1226. walk_zones_in_node(m, pgdat, zoneinfo_show_print);
  1227. return 0;
  1228. }
  1229. static const struct seq_operations zoneinfo_op = {
  1230. .start = frag_start, /* iterate over all zones. The same as in
  1231. * fragmentation. */
  1232. .next = frag_next,
  1233. .stop = frag_stop,
  1234. .show = zoneinfo_show,
  1235. };
  1236. static int zoneinfo_open(struct inode *inode, struct file *file)
  1237. {
  1238. return seq_open(file, &zoneinfo_op);
  1239. }
  1240. static const struct file_operations proc_zoneinfo_file_operations = {
  1241. .open = zoneinfo_open,
  1242. .read = seq_read,
  1243. .llseek = seq_lseek,
  1244. .release = seq_release,
  1245. };
  1246. enum writeback_stat_item {
  1247. NR_DIRTY_THRESHOLD,
  1248. NR_DIRTY_BG_THRESHOLD,
  1249. NR_VM_WRITEBACK_STAT_ITEMS,
  1250. };
  1251. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  1252. {
  1253. unsigned long *v;
  1254. int i, stat_items_size;
  1255. if (*pos >= ARRAY_SIZE(vmstat_text))
  1256. return NULL;
  1257. stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
  1258. NR_VM_NODE_STAT_ITEMS * sizeof(unsigned long) +
  1259. NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
  1260. #ifdef CONFIG_VM_EVENT_COUNTERS
  1261. stat_items_size += sizeof(struct vm_event_state);
  1262. #endif
  1263. v = kmalloc(stat_items_size, GFP_KERNEL);
  1264. m->private = v;
  1265. if (!v)
  1266. return ERR_PTR(-ENOMEM);
  1267. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  1268. v[i] = global_page_state(i);
  1269. v += NR_VM_ZONE_STAT_ITEMS;
  1270. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
  1271. v[i] = global_node_page_state(i);
  1272. v += NR_VM_NODE_STAT_ITEMS;
  1273. global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
  1274. v + NR_DIRTY_THRESHOLD);
  1275. v += NR_VM_WRITEBACK_STAT_ITEMS;
  1276. #ifdef CONFIG_VM_EVENT_COUNTERS
  1277. all_vm_events(v);
  1278. v[PGPGIN] /= 2; /* sectors -> kbytes */
  1279. v[PGPGOUT] /= 2;
  1280. #endif
  1281. return (unsigned long *)m->private + *pos;
  1282. }
  1283. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  1284. {
  1285. (*pos)++;
  1286. if (*pos >= ARRAY_SIZE(vmstat_text))
  1287. return NULL;
  1288. return (unsigned long *)m->private + *pos;
  1289. }
  1290. static int vmstat_show(struct seq_file *m, void *arg)
  1291. {
  1292. unsigned long *l = arg;
  1293. unsigned long off = l - (unsigned long *)m->private;
  1294. seq_puts(m, vmstat_text[off]);
  1295. seq_put_decimal_ull(m, " ", *l);
  1296. seq_putc(m, '\n');
  1297. return 0;
  1298. }
  1299. static void vmstat_stop(struct seq_file *m, void *arg)
  1300. {
  1301. kfree(m->private);
  1302. m->private = NULL;
  1303. }
  1304. static const struct seq_operations vmstat_op = {
  1305. .start = vmstat_start,
  1306. .next = vmstat_next,
  1307. .stop = vmstat_stop,
  1308. .show = vmstat_show,
  1309. };
  1310. static int vmstat_open(struct inode *inode, struct file *file)
  1311. {
  1312. return seq_open(file, &vmstat_op);
  1313. }
  1314. static const struct file_operations proc_vmstat_file_operations = {
  1315. .open = vmstat_open,
  1316. .read = seq_read,
  1317. .llseek = seq_lseek,
  1318. .release = seq_release,
  1319. };
  1320. #endif /* CONFIG_PROC_FS */
  1321. #ifdef CONFIG_SMP
  1322. static struct workqueue_struct *vmstat_wq;
  1323. static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
  1324. int sysctl_stat_interval __read_mostly = HZ;
  1325. #ifdef CONFIG_PROC_FS
  1326. static void refresh_vm_stats(struct work_struct *work)
  1327. {
  1328. refresh_cpu_vm_stats(true);
  1329. }
  1330. int vmstat_refresh(struct ctl_table *table, int write,
  1331. void __user *buffer, size_t *lenp, loff_t *ppos)
  1332. {
  1333. long val;
  1334. int err;
  1335. int i;
  1336. /*
  1337. * The regular update, every sysctl_stat_interval, may come later
  1338. * than expected: leaving a significant amount in per_cpu buckets.
  1339. * This is particularly misleading when checking a quantity of HUGE
  1340. * pages, immediately after running a test. /proc/sys/vm/stat_refresh,
  1341. * which can equally be echo'ed to or cat'ted from (by root),
  1342. * can be used to update the stats just before reading them.
  1343. *
  1344. * Oh, and since global_page_state() etc. are so careful to hide
  1345. * transiently negative values, report an error here if any of
  1346. * the stats is negative, so we know to go looking for imbalance.
  1347. */
  1348. err = schedule_on_each_cpu(refresh_vm_stats);
  1349. if (err)
  1350. return err;
  1351. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
  1352. val = atomic_long_read(&vm_zone_stat[i]);
  1353. if (val < 0) {
  1354. switch (i) {
  1355. case NR_PAGES_SCANNED:
  1356. /*
  1357. * This is often seen to go negative in
  1358. * recent kernels, but not to go permanently
  1359. * negative. Whilst it would be nicer not to
  1360. * have exceptions, rooting them out would be
  1361. * another task, of rather low priority.
  1362. */
  1363. break;
  1364. default:
  1365. pr_warn("%s: %s %ld\n",
  1366. __func__, vmstat_text[i], val);
  1367. err = -EINVAL;
  1368. break;
  1369. }
  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(), vmstat_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, vmstat_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. vmstat_wq = alloc_workqueue("vmstat", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1468. schedule_delayed_work(&shepherd,
  1469. round_jiffies_relative(sysctl_stat_interval));
  1470. }
  1471. static void __init init_cpu_node_state(void)
  1472. {
  1473. int cpu;
  1474. get_online_cpus();
  1475. for_each_online_cpu(cpu)
  1476. node_set_state(cpu_to_node(cpu), N_CPU);
  1477. put_online_cpus();
  1478. }
  1479. static void vmstat_cpu_dead(int node)
  1480. {
  1481. int cpu;
  1482. get_online_cpus();
  1483. for_each_online_cpu(cpu)
  1484. if (cpu_to_node(cpu) == node)
  1485. goto end;
  1486. node_clear_state(node, N_CPU);
  1487. end:
  1488. put_online_cpus();
  1489. }
  1490. /*
  1491. * Use the cpu notifier to insure that the thresholds are recalculated
  1492. * when necessary.
  1493. */
  1494. static int vmstat_cpuup_callback(struct notifier_block *nfb,
  1495. unsigned long action,
  1496. void *hcpu)
  1497. {
  1498. long cpu = (long)hcpu;
  1499. switch (action) {
  1500. case CPU_ONLINE:
  1501. case CPU_ONLINE_FROZEN:
  1502. refresh_zone_stat_thresholds();
  1503. node_set_state(cpu_to_node(cpu), N_CPU);
  1504. break;
  1505. case CPU_DOWN_PREPARE:
  1506. case CPU_DOWN_PREPARE_FROZEN:
  1507. cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
  1508. break;
  1509. case CPU_DOWN_FAILED:
  1510. case CPU_DOWN_FAILED_FROZEN:
  1511. break;
  1512. case CPU_DEAD:
  1513. case CPU_DEAD_FROZEN:
  1514. refresh_zone_stat_thresholds();
  1515. vmstat_cpu_dead(cpu_to_node(cpu));
  1516. break;
  1517. default:
  1518. break;
  1519. }
  1520. return NOTIFY_OK;
  1521. }
  1522. static struct notifier_block vmstat_notifier =
  1523. { &vmstat_cpuup_callback, NULL, 0 };
  1524. #endif
  1525. static int __init setup_vmstat(void)
  1526. {
  1527. #ifdef CONFIG_SMP
  1528. cpu_notifier_register_begin();
  1529. __register_cpu_notifier(&vmstat_notifier);
  1530. init_cpu_node_state();
  1531. start_shepherd_timer();
  1532. cpu_notifier_register_done();
  1533. #endif
  1534. #ifdef CONFIG_PROC_FS
  1535. proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
  1536. proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
  1537. proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
  1538. proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
  1539. #endif
  1540. return 0;
  1541. }
  1542. module_init(setup_vmstat)
  1543. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
  1544. /*
  1545. * Return an index indicating how much of the available free memory is
  1546. * unusable for an allocation of the requested size.
  1547. */
  1548. static int unusable_free_index(unsigned int order,
  1549. struct contig_page_info *info)
  1550. {
  1551. /* No free memory is interpreted as all free memory is unusable */
  1552. if (info->free_pages == 0)
  1553. return 1000;
  1554. /*
  1555. * Index should be a value between 0 and 1. Return a value to 3
  1556. * decimal places.
  1557. *
  1558. * 0 => no fragmentation
  1559. * 1 => high fragmentation
  1560. */
  1561. return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
  1562. }
  1563. static void unusable_show_print(struct seq_file *m,
  1564. pg_data_t *pgdat, struct zone *zone)
  1565. {
  1566. unsigned int order;
  1567. int index;
  1568. struct contig_page_info info;
  1569. seq_printf(m, "Node %d, zone %8s ",
  1570. pgdat->node_id,
  1571. zone->name);
  1572. for (order = 0; order < MAX_ORDER; ++order) {
  1573. fill_contig_page_info(zone, order, &info);
  1574. index = unusable_free_index(order, &info);
  1575. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1576. }
  1577. seq_putc(m, '\n');
  1578. }
  1579. /*
  1580. * Display unusable free space index
  1581. *
  1582. * The unusable free space index measures how much of the available free
  1583. * memory cannot be used to satisfy an allocation of a given size and is a
  1584. * value between 0 and 1. The higher the value, the more of free memory is
  1585. * unusable and by implication, the worse the external fragmentation is. This
  1586. * can be expressed as a percentage by multiplying by 100.
  1587. */
  1588. static int unusable_show(struct seq_file *m, void *arg)
  1589. {
  1590. pg_data_t *pgdat = (pg_data_t *)arg;
  1591. /* check memoryless node */
  1592. if (!node_state(pgdat->node_id, N_MEMORY))
  1593. return 0;
  1594. walk_zones_in_node(m, pgdat, unusable_show_print);
  1595. return 0;
  1596. }
  1597. static const struct seq_operations unusable_op = {
  1598. .start = frag_start,
  1599. .next = frag_next,
  1600. .stop = frag_stop,
  1601. .show = unusable_show,
  1602. };
  1603. static int unusable_open(struct inode *inode, struct file *file)
  1604. {
  1605. return seq_open(file, &unusable_op);
  1606. }
  1607. static const struct file_operations unusable_file_ops = {
  1608. .open = unusable_open,
  1609. .read = seq_read,
  1610. .llseek = seq_lseek,
  1611. .release = seq_release,
  1612. };
  1613. static void extfrag_show_print(struct seq_file *m,
  1614. pg_data_t *pgdat, struct zone *zone)
  1615. {
  1616. unsigned int order;
  1617. int index;
  1618. /* Alloc on stack as interrupts are disabled for zone walk */
  1619. struct contig_page_info info;
  1620. seq_printf(m, "Node %d, zone %8s ",
  1621. pgdat->node_id,
  1622. zone->name);
  1623. for (order = 0; order < MAX_ORDER; ++order) {
  1624. fill_contig_page_info(zone, order, &info);
  1625. index = __fragmentation_index(order, &info);
  1626. seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
  1627. }
  1628. seq_putc(m, '\n');
  1629. }
  1630. /*
  1631. * Display fragmentation index for orders that allocations would fail for
  1632. */
  1633. static int extfrag_show(struct seq_file *m, void *arg)
  1634. {
  1635. pg_data_t *pgdat = (pg_data_t *)arg;
  1636. walk_zones_in_node(m, pgdat, extfrag_show_print);
  1637. return 0;
  1638. }
  1639. static const struct seq_operations extfrag_op = {
  1640. .start = frag_start,
  1641. .next = frag_next,
  1642. .stop = frag_stop,
  1643. .show = extfrag_show,
  1644. };
  1645. static int extfrag_open(struct inode *inode, struct file *file)
  1646. {
  1647. return seq_open(file, &extfrag_op);
  1648. }
  1649. static const struct file_operations extfrag_file_ops = {
  1650. .open = extfrag_open,
  1651. .read = seq_read,
  1652. .llseek = seq_lseek,
  1653. .release = seq_release,
  1654. };
  1655. static int __init extfrag_debug_init(void)
  1656. {
  1657. struct dentry *extfrag_debug_root;
  1658. extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
  1659. if (!extfrag_debug_root)
  1660. return -ENOMEM;
  1661. if (!debugfs_create_file("unusable_index", 0444,
  1662. extfrag_debug_root, NULL, &unusable_file_ops))
  1663. goto fail;
  1664. if (!debugfs_create_file("extfrag_index", 0444,
  1665. extfrag_debug_root, NULL, &extfrag_file_ops))
  1666. goto fail;
  1667. return 0;
  1668. fail:
  1669. debugfs_remove_recursive(extfrag_debug_root);
  1670. return -ENOMEM;
  1671. }
  1672. module_init(extfrag_debug_init);
  1673. #endif