vmstat.c 42 KB

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