vmstat.c 41 KB

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