show_mem.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic show_mem() implementation
  4. *
  5. * Copyright (C) 2008 Johannes Weiner <hannes@saeurebad.de>
  6. */
  7. #include <linux/blkdev.h>
  8. #include <linux/cma.h>
  9. #include <linux/cpuset.h>
  10. #include <linux/highmem.h>
  11. #include <linux/hugetlb.h>
  12. #include <linux/mm.h>
  13. #include <linux/mmzone.h>
  14. #include <linux/swap.h>
  15. #include <linux/vmstat.h>
  16. #include "internal.h"
  17. #include "swap.h"
  18. atomic_long_t _totalram_pages __read_mostly;
  19. EXPORT_SYMBOL(_totalram_pages);
  20. unsigned long totalreserve_pages __read_mostly;
  21. unsigned long totalcma_pages __read_mostly;
  22. static inline void show_node(struct zone *zone)
  23. {
  24. if (IS_ENABLED(CONFIG_NUMA))
  25. printk("Node %d ", zone_to_nid(zone));
  26. }
  27. long si_mem_available(void)
  28. {
  29. long available;
  30. unsigned long pagecache;
  31. unsigned long wmark_low = 0;
  32. unsigned long reclaimable;
  33. struct zone *zone;
  34. for_each_zone(zone)
  35. wmark_low += low_wmark_pages(zone);
  36. /*
  37. * Estimate the amount of memory available for userspace allocations,
  38. * without causing swapping or OOM.
  39. */
  40. available = global_zone_page_state(NR_FREE_PAGES) - totalreserve_pages;
  41. /*
  42. * Not all the page cache can be freed, otherwise the system will
  43. * start swapping or thrashing. Assume at least half of the page
  44. * cache, or the low watermark worth of cache, needs to stay.
  45. */
  46. pagecache = global_node_page_state(NR_ACTIVE_FILE) +
  47. global_node_page_state(NR_INACTIVE_FILE);
  48. pagecache -= min(pagecache / 2, wmark_low);
  49. available += pagecache;
  50. /*
  51. * Part of the reclaimable slab and other kernel memory consists of
  52. * items that are in use, and cannot be freed. Cap this estimate at the
  53. * low watermark.
  54. */
  55. reclaimable = global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B) +
  56. global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE);
  57. reclaimable -= min(reclaimable / 2, wmark_low);
  58. available += reclaimable;
  59. if (available < 0)
  60. available = 0;
  61. return available;
  62. }
  63. EXPORT_SYMBOL_GPL(si_mem_available);
  64. void si_meminfo(struct sysinfo *val)
  65. {
  66. val->totalram = totalram_pages();
  67. val->sharedram = global_node_page_state(NR_SHMEM);
  68. val->freeram = global_zone_page_state(NR_FREE_PAGES);
  69. val->bufferram = nr_blockdev_pages();
  70. val->totalhigh = totalhigh_pages();
  71. val->freehigh = nr_free_highpages();
  72. val->mem_unit = PAGE_SIZE;
  73. }
  74. EXPORT_SYMBOL(si_meminfo);
  75. #ifdef CONFIG_NUMA
  76. void si_meminfo_node(struct sysinfo *val, int nid)
  77. {
  78. int zone_type; /* needs to be signed */
  79. unsigned long managed_pages = 0;
  80. unsigned long managed_highpages = 0;
  81. unsigned long free_highpages = 0;
  82. pg_data_t *pgdat = NODE_DATA(nid);
  83. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
  84. managed_pages += zone_managed_pages(&pgdat->node_zones[zone_type]);
  85. val->totalram = managed_pages;
  86. val->sharedram = node_page_state(pgdat, NR_SHMEM);
  87. val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
  88. #ifdef CONFIG_HIGHMEM
  89. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  90. struct zone *zone = &pgdat->node_zones[zone_type];
  91. if (is_highmem(zone)) {
  92. managed_highpages += zone_managed_pages(zone);
  93. free_highpages += zone_page_state(zone, NR_FREE_PAGES);
  94. }
  95. }
  96. val->totalhigh = managed_highpages;
  97. val->freehigh = free_highpages;
  98. #else
  99. val->totalhigh = managed_highpages;
  100. val->freehigh = free_highpages;
  101. #endif
  102. val->mem_unit = PAGE_SIZE;
  103. }
  104. #endif
  105. /*
  106. * Determine whether the node should be displayed or not, depending on whether
  107. * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
  108. */
  109. static bool show_mem_node_skip(unsigned int flags, int nid, nodemask_t *nodemask)
  110. {
  111. if (!(flags & SHOW_MEM_FILTER_NODES))
  112. return false;
  113. /*
  114. * no node mask - aka implicit memory numa policy. Do not bother with
  115. * the synchronization - read_mems_allowed_begin - because we do not
  116. * have to be precise here.
  117. */
  118. if (!nodemask)
  119. nodemask = &cpuset_current_mems_allowed;
  120. return !node_isset(nid, *nodemask);
  121. }
  122. static void show_migration_types(unsigned char type)
  123. {
  124. static const char types[MIGRATE_TYPES] = {
  125. [MIGRATE_UNMOVABLE] = 'U',
  126. [MIGRATE_MOVABLE] = 'M',
  127. [MIGRATE_RECLAIMABLE] = 'E',
  128. [MIGRATE_HIGHATOMIC] = 'H',
  129. #ifdef CONFIG_CMA
  130. [MIGRATE_CMA] = 'C',
  131. #endif
  132. #ifdef CONFIG_MEMORY_ISOLATION
  133. [MIGRATE_ISOLATE] = 'I',
  134. #endif
  135. };
  136. char tmp[MIGRATE_TYPES + 1];
  137. char *p = tmp;
  138. int i;
  139. for (i = 0; i < MIGRATE_TYPES; i++) {
  140. if (type & (1 << i))
  141. *p++ = types[i];
  142. }
  143. *p = '\0';
  144. printk(KERN_CONT "(%s) ", tmp);
  145. }
  146. static bool node_has_managed_zones(pg_data_t *pgdat, int max_zone_idx)
  147. {
  148. int zone_idx;
  149. for (zone_idx = 0; zone_idx <= max_zone_idx; zone_idx++)
  150. if (zone_managed_pages(pgdat->node_zones + zone_idx))
  151. return true;
  152. return false;
  153. }
  154. /*
  155. * Show free area list (used inside shift_scroll-lock stuff)
  156. * We also calculate the percentage fragmentation. We do this by counting the
  157. * memory on each free list with the exception of the first item on the list.
  158. *
  159. * Bits in @filter:
  160. * SHOW_MEM_FILTER_NODES: suppress nodes that are not allowed by current's
  161. * cpuset.
  162. */
  163. static void show_free_areas(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
  164. {
  165. unsigned long free_pcp = 0;
  166. int cpu, nid;
  167. struct zone *zone;
  168. pg_data_t *pgdat;
  169. for_each_populated_zone(zone) {
  170. if (zone_idx(zone) > max_zone_idx)
  171. continue;
  172. if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
  173. continue;
  174. for_each_online_cpu(cpu)
  175. free_pcp += per_cpu_ptr(zone->per_cpu_pageset, cpu)->count;
  176. }
  177. printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
  178. " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
  179. " unevictable:%lu dirty:%lu writeback:%lu\n"
  180. " slab_reclaimable:%lu slab_unreclaimable:%lu\n"
  181. " mapped:%lu shmem:%lu pagetables:%lu\n"
  182. " sec_pagetables:%lu bounce:%lu\n"
  183. " kernel_misc_reclaimable:%lu\n"
  184. " free:%lu free_pcp:%lu free_cma:%lu\n",
  185. global_node_page_state(NR_ACTIVE_ANON),
  186. global_node_page_state(NR_INACTIVE_ANON),
  187. global_node_page_state(NR_ISOLATED_ANON),
  188. global_node_page_state(NR_ACTIVE_FILE),
  189. global_node_page_state(NR_INACTIVE_FILE),
  190. global_node_page_state(NR_ISOLATED_FILE),
  191. global_node_page_state(NR_UNEVICTABLE),
  192. global_node_page_state(NR_FILE_DIRTY),
  193. global_node_page_state(NR_WRITEBACK),
  194. global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B),
  195. global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B),
  196. global_node_page_state(NR_FILE_MAPPED),
  197. global_node_page_state(NR_SHMEM),
  198. global_node_page_state(NR_PAGETABLE),
  199. global_node_page_state(NR_SECONDARY_PAGETABLE),
  200. global_zone_page_state(NR_BOUNCE),
  201. global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE),
  202. global_zone_page_state(NR_FREE_PAGES),
  203. free_pcp,
  204. global_zone_page_state(NR_FREE_CMA_PAGES));
  205. for_each_online_pgdat(pgdat) {
  206. if (show_mem_node_skip(filter, pgdat->node_id, nodemask))
  207. continue;
  208. if (!node_has_managed_zones(pgdat, max_zone_idx))
  209. continue;
  210. printk("Node %d"
  211. " active_anon:%lukB"
  212. " inactive_anon:%lukB"
  213. " active_file:%lukB"
  214. " inactive_file:%lukB"
  215. " unevictable:%lukB"
  216. " isolated(anon):%lukB"
  217. " isolated(file):%lukB"
  218. " mapped:%lukB"
  219. " dirty:%lukB"
  220. " writeback:%lukB"
  221. " shmem:%lukB"
  222. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  223. " shmem_thp:%lukB"
  224. " shmem_pmdmapped:%lukB"
  225. " anon_thp:%lukB"
  226. #endif
  227. " writeback_tmp:%lukB"
  228. " kernel_stack:%lukB"
  229. #ifdef CONFIG_SHADOW_CALL_STACK
  230. " shadow_call_stack:%lukB"
  231. #endif
  232. " pagetables:%lukB"
  233. " sec_pagetables:%lukB"
  234. " all_unreclaimable? %s"
  235. "\n",
  236. pgdat->node_id,
  237. K(node_page_state(pgdat, NR_ACTIVE_ANON)),
  238. K(node_page_state(pgdat, NR_INACTIVE_ANON)),
  239. K(node_page_state(pgdat, NR_ACTIVE_FILE)),
  240. K(node_page_state(pgdat, NR_INACTIVE_FILE)),
  241. K(node_page_state(pgdat, NR_UNEVICTABLE)),
  242. K(node_page_state(pgdat, NR_ISOLATED_ANON)),
  243. K(node_page_state(pgdat, NR_ISOLATED_FILE)),
  244. K(node_page_state(pgdat, NR_FILE_MAPPED)),
  245. K(node_page_state(pgdat, NR_FILE_DIRTY)),
  246. K(node_page_state(pgdat, NR_WRITEBACK)),
  247. K(node_page_state(pgdat, NR_SHMEM)),
  248. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  249. K(node_page_state(pgdat, NR_SHMEM_THPS)),
  250. K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)),
  251. K(node_page_state(pgdat, NR_ANON_THPS)),
  252. #endif
  253. K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
  254. node_page_state(pgdat, NR_KERNEL_STACK_KB),
  255. #ifdef CONFIG_SHADOW_CALL_STACK
  256. node_page_state(pgdat, NR_KERNEL_SCS_KB),
  257. #endif
  258. K(node_page_state(pgdat, NR_PAGETABLE)),
  259. K(node_page_state(pgdat, NR_SECONDARY_PAGETABLE)),
  260. pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ?
  261. "yes" : "no");
  262. }
  263. for_each_populated_zone(zone) {
  264. int i;
  265. if (zone_idx(zone) > max_zone_idx)
  266. continue;
  267. if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
  268. continue;
  269. free_pcp = 0;
  270. for_each_online_cpu(cpu)
  271. free_pcp += per_cpu_ptr(zone->per_cpu_pageset, cpu)->count;
  272. show_node(zone);
  273. printk(KERN_CONT
  274. "%s"
  275. " free:%lukB"
  276. " boost:%lukB"
  277. " min:%lukB"
  278. " low:%lukB"
  279. " high:%lukB"
  280. " reserved_highatomic:%luKB"
  281. " active_anon:%lukB"
  282. " inactive_anon:%lukB"
  283. " active_file:%lukB"
  284. " inactive_file:%lukB"
  285. " unevictable:%lukB"
  286. " writepending:%lukB"
  287. " present:%lukB"
  288. " managed:%lukB"
  289. " mlocked:%lukB"
  290. " bounce:%lukB"
  291. " free_pcp:%lukB"
  292. " local_pcp:%ukB"
  293. " free_cma:%lukB"
  294. "\n",
  295. zone->name,
  296. K(zone_page_state(zone, NR_FREE_PAGES)),
  297. K(zone->watermark_boost),
  298. K(min_wmark_pages(zone)),
  299. K(low_wmark_pages(zone)),
  300. K(high_wmark_pages(zone)),
  301. K(zone->nr_reserved_highatomic),
  302. K(zone_page_state(zone, NR_ZONE_ACTIVE_ANON)),
  303. K(zone_page_state(zone, NR_ZONE_INACTIVE_ANON)),
  304. K(zone_page_state(zone, NR_ZONE_ACTIVE_FILE)),
  305. K(zone_page_state(zone, NR_ZONE_INACTIVE_FILE)),
  306. K(zone_page_state(zone, NR_ZONE_UNEVICTABLE)),
  307. K(zone_page_state(zone, NR_ZONE_WRITE_PENDING)),
  308. K(zone->present_pages),
  309. K(zone_managed_pages(zone)),
  310. K(zone_page_state(zone, NR_MLOCK)),
  311. K(zone_page_state(zone, NR_BOUNCE)),
  312. K(free_pcp),
  313. K(this_cpu_read(zone->per_cpu_pageset->count)),
  314. K(zone_page_state(zone, NR_FREE_CMA_PAGES)));
  315. printk("lowmem_reserve[]:");
  316. for (i = 0; i < MAX_NR_ZONES; i++)
  317. printk(KERN_CONT " %ld", zone->lowmem_reserve[i]);
  318. printk(KERN_CONT "\n");
  319. }
  320. for_each_populated_zone(zone) {
  321. unsigned int order;
  322. unsigned long nr[NR_PAGE_ORDERS], flags, total = 0;
  323. unsigned char types[NR_PAGE_ORDERS];
  324. if (zone_idx(zone) > max_zone_idx)
  325. continue;
  326. if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
  327. continue;
  328. show_node(zone);
  329. printk(KERN_CONT "%s: ", zone->name);
  330. spin_lock_irqsave(&zone->lock, flags);
  331. for (order = 0; order < NR_PAGE_ORDERS; order++) {
  332. struct free_area *area = &zone->free_area[order];
  333. int type;
  334. nr[order] = area->nr_free;
  335. total += nr[order] << order;
  336. types[order] = 0;
  337. for (type = 0; type < MIGRATE_TYPES; type++) {
  338. if (!free_area_empty(area, type))
  339. types[order] |= 1 << type;
  340. }
  341. }
  342. spin_unlock_irqrestore(&zone->lock, flags);
  343. for (order = 0; order < NR_PAGE_ORDERS; order++) {
  344. printk(KERN_CONT "%lu*%lukB ",
  345. nr[order], K(1UL) << order);
  346. if (nr[order])
  347. show_migration_types(types[order]);
  348. }
  349. printk(KERN_CONT "= %lukB\n", K(total));
  350. }
  351. for_each_online_node(nid) {
  352. if (show_mem_node_skip(filter, nid, nodemask))
  353. continue;
  354. hugetlb_show_meminfo_node(nid);
  355. }
  356. printk("%ld total pagecache pages\n", global_node_page_state(NR_FILE_PAGES));
  357. show_swap_cache_info();
  358. }
  359. void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
  360. {
  361. unsigned long total = 0, reserved = 0, highmem = 0;
  362. struct zone *zone;
  363. printk("Mem-Info:\n");
  364. show_free_areas(filter, nodemask, max_zone_idx);
  365. for_each_populated_zone(zone) {
  366. total += zone->present_pages;
  367. reserved += zone->present_pages - zone_managed_pages(zone);
  368. if (is_highmem(zone))
  369. highmem += zone->present_pages;
  370. }
  371. printk("%lu pages RAM\n", total);
  372. printk("%lu pages HighMem/MovableOnly\n", highmem);
  373. printk("%lu pages reserved\n", reserved);
  374. #ifdef CONFIG_CMA
  375. printk("%lu pages cma reserved\n", totalcma_pages);
  376. #endif
  377. #ifdef CONFIG_MEMORY_FAILURE
  378. printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages));
  379. #endif
  380. #ifdef CONFIG_MEM_ALLOC_PROFILING
  381. {
  382. struct codetag_bytes tags[10];
  383. size_t i, nr;
  384. nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false);
  385. if (nr) {
  386. pr_notice("Memory allocations:\n");
  387. for (i = 0; i < nr; i++) {
  388. struct codetag *ct = tags[i].ct;
  389. struct alloc_tag *tag = ct_to_alloc_tag(ct);
  390. struct alloc_tag_counters counter = alloc_tag_read(tag);
  391. char bytes[10];
  392. string_get_size(counter.bytes, 1, STRING_UNITS_2, bytes, sizeof(bytes));
  393. /* Same as alloc_tag_to_text() but w/o intermediate buffer */
  394. if (ct->modname)
  395. pr_notice("%12s %8llu %s:%u [%s] func:%s\n",
  396. bytes, counter.calls, ct->filename,
  397. ct->lineno, ct->modname, ct->function);
  398. else
  399. pr_notice("%12s %8llu %s:%u func:%s\n",
  400. bytes, counter.calls, ct->filename,
  401. ct->lineno, ct->function);
  402. }
  403. }
  404. }
  405. #endif
  406. }