show_mem.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. struct zone *zone = &pgdat->node_zones[zone_type];
  85. managed_pages += zone_managed_pages(zone);
  86. if (is_highmem(zone)) {
  87. managed_highpages += zone_managed_pages(zone);
  88. free_highpages += zone_page_state(zone, NR_FREE_PAGES);
  89. }
  90. }
  91. val->totalram = managed_pages;
  92. val->sharedram = node_page_state(pgdat, NR_SHMEM);
  93. val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
  94. val->totalhigh = managed_highpages;
  95. val->freehigh = free_highpages;
  96. val->mem_unit = PAGE_SIZE;
  97. }
  98. #endif
  99. /*
  100. * Determine whether the node should be displayed or not, depending on whether
  101. * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
  102. */
  103. static bool show_mem_node_skip(unsigned int flags, int nid, nodemask_t *nodemask)
  104. {
  105. if (!(flags & SHOW_MEM_FILTER_NODES))
  106. return false;
  107. /*
  108. * no node mask - aka implicit memory numa policy. Do not bother with
  109. * the synchronization - read_mems_allowed_begin - because we do not
  110. * have to be precise here.
  111. */
  112. if (!nodemask)
  113. nodemask = &cpuset_current_mems_allowed;
  114. return !node_isset(nid, *nodemask);
  115. }
  116. static void show_migration_types(unsigned char type)
  117. {
  118. static const char types[MIGRATE_TYPES] = {
  119. [MIGRATE_UNMOVABLE] = 'U',
  120. [MIGRATE_MOVABLE] = 'M',
  121. [MIGRATE_RECLAIMABLE] = 'E',
  122. [MIGRATE_HIGHATOMIC] = 'H',
  123. #ifdef CONFIG_CMA
  124. [MIGRATE_CMA] = 'C',
  125. #endif
  126. #ifdef CONFIG_MEMORY_ISOLATION
  127. [MIGRATE_ISOLATE] = 'I',
  128. #endif
  129. };
  130. char tmp[MIGRATE_TYPES + 1];
  131. char *p = tmp;
  132. int i;
  133. for (i = 0; i < MIGRATE_TYPES; i++) {
  134. if (type & (1 << i))
  135. *p++ = types[i];
  136. }
  137. *p = '\0';
  138. printk(KERN_CONT "(%s) ", tmp);
  139. }
  140. static bool node_has_managed_zones(pg_data_t *pgdat, int max_zone_idx)
  141. {
  142. int zone_idx;
  143. for (zone_idx = 0; zone_idx <= max_zone_idx; zone_idx++)
  144. if (zone_managed_pages(pgdat->node_zones + zone_idx))
  145. return true;
  146. return false;
  147. }
  148. /*
  149. * Show free area list (used inside shift_scroll-lock stuff)
  150. * We also calculate the percentage fragmentation. We do this by counting the
  151. * memory on each free list with the exception of the first item on the list.
  152. *
  153. * Bits in @filter:
  154. * SHOW_MEM_FILTER_NODES: suppress nodes that are not allowed by current's
  155. * cpuset.
  156. */
  157. static void show_free_areas(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
  158. {
  159. unsigned long free_pcp = 0;
  160. int cpu, nid;
  161. struct zone *zone;
  162. pg_data_t *pgdat;
  163. for_each_populated_zone(zone) {
  164. if (zone_idx(zone) > max_zone_idx)
  165. continue;
  166. if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
  167. continue;
  168. for_each_online_cpu(cpu)
  169. free_pcp += per_cpu_ptr(zone->per_cpu_pageset, cpu)->count;
  170. }
  171. printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
  172. " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
  173. " unevictable:%lu dirty:%lu writeback:%lu\n"
  174. " slab_reclaimable:%lu slab_unreclaimable:%lu\n"
  175. " mapped:%lu shmem:%lu pagetables:%lu\n"
  176. " sec_pagetables:%lu bounce:%lu\n"
  177. " kernel_misc_reclaimable:%lu\n"
  178. " free:%lu free_pcp:%lu free_cma:%lu\n",
  179. global_node_page_state(NR_ACTIVE_ANON),
  180. global_node_page_state(NR_INACTIVE_ANON),
  181. global_node_page_state(NR_ISOLATED_ANON),
  182. global_node_page_state(NR_ACTIVE_FILE),
  183. global_node_page_state(NR_INACTIVE_FILE),
  184. global_node_page_state(NR_ISOLATED_FILE),
  185. global_node_page_state(NR_UNEVICTABLE),
  186. global_node_page_state(NR_FILE_DIRTY),
  187. global_node_page_state(NR_WRITEBACK),
  188. global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B),
  189. global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B),
  190. global_node_page_state(NR_FILE_MAPPED),
  191. global_node_page_state(NR_SHMEM),
  192. global_node_page_state(NR_PAGETABLE),
  193. global_node_page_state(NR_SECONDARY_PAGETABLE),
  194. 0UL,
  195. global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE),
  196. global_zone_page_state(NR_FREE_PAGES),
  197. free_pcp,
  198. global_zone_page_state(NR_FREE_CMA_PAGES));
  199. for_each_online_pgdat(pgdat) {
  200. if (show_mem_node_skip(filter, pgdat->node_id, nodemask))
  201. continue;
  202. if (!node_has_managed_zones(pgdat, max_zone_idx))
  203. continue;
  204. printk("Node %d"
  205. " active_anon:%lukB"
  206. " inactive_anon:%lukB"
  207. " active_file:%lukB"
  208. " inactive_file:%lukB"
  209. " unevictable:%lukB"
  210. " isolated(anon):%lukB"
  211. " isolated(file):%lukB"
  212. " mapped:%lukB"
  213. " dirty:%lukB"
  214. " writeback:%lukB"
  215. " shmem:%lukB"
  216. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  217. " shmem_thp:%lukB"
  218. " shmem_pmdmapped:%lukB"
  219. " anon_thp:%lukB"
  220. #endif
  221. " kernel_stack:%lukB"
  222. #ifdef CONFIG_SHADOW_CALL_STACK
  223. " shadow_call_stack:%lukB"
  224. #endif
  225. " pagetables:%lukB"
  226. " sec_pagetables:%lukB"
  227. " all_unreclaimable? %s"
  228. " Balloon:%lukB"
  229. "\n",
  230. pgdat->node_id,
  231. K(node_page_state(pgdat, NR_ACTIVE_ANON)),
  232. K(node_page_state(pgdat, NR_INACTIVE_ANON)),
  233. K(node_page_state(pgdat, NR_ACTIVE_FILE)),
  234. K(node_page_state(pgdat, NR_INACTIVE_FILE)),
  235. K(node_page_state(pgdat, NR_UNEVICTABLE)),
  236. K(node_page_state(pgdat, NR_ISOLATED_ANON)),
  237. K(node_page_state(pgdat, NR_ISOLATED_FILE)),
  238. K(node_page_state(pgdat, NR_FILE_MAPPED)),
  239. K(node_page_state(pgdat, NR_FILE_DIRTY)),
  240. K(node_page_state(pgdat, NR_WRITEBACK)),
  241. K(node_page_state(pgdat, NR_SHMEM)),
  242. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  243. K(node_page_state(pgdat, NR_SHMEM_THPS)),
  244. K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)),
  245. K(node_page_state(pgdat, NR_ANON_THPS)),
  246. #endif
  247. node_page_state(pgdat, NR_KERNEL_STACK_KB),
  248. #ifdef CONFIG_SHADOW_CALL_STACK
  249. node_page_state(pgdat, NR_KERNEL_SCS_KB),
  250. #endif
  251. K(node_page_state(pgdat, NR_PAGETABLE)),
  252. K(node_page_state(pgdat, NR_SECONDARY_PAGETABLE)),
  253. str_yes_no(kswapd_test_hopeless(pgdat)),
  254. K(node_page_state(pgdat, NR_BALLOON_PAGES)));
  255. }
  256. for_each_populated_zone(zone) {
  257. int i;
  258. if (zone_idx(zone) > max_zone_idx)
  259. continue;
  260. if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
  261. continue;
  262. free_pcp = 0;
  263. for_each_online_cpu(cpu)
  264. free_pcp += per_cpu_ptr(zone->per_cpu_pageset, cpu)->count;
  265. show_node(zone);
  266. printk(KERN_CONT
  267. "%s"
  268. " free:%lukB"
  269. " boost:%lukB"
  270. " min:%lukB"
  271. " low:%lukB"
  272. " high:%lukB"
  273. " reserved_highatomic:%luKB"
  274. " free_highatomic:%luKB"
  275. " active_anon:%lukB"
  276. " inactive_anon:%lukB"
  277. " active_file:%lukB"
  278. " inactive_file:%lukB"
  279. " unevictable:%lukB"
  280. " writepending:%lukB"
  281. " zspages:%lukB"
  282. " present:%lukB"
  283. " managed:%lukB"
  284. " mlocked:%lukB"
  285. " bounce:%lukB"
  286. " free_pcp:%lukB"
  287. " local_pcp:%ukB"
  288. " free_cma:%lukB"
  289. "\n",
  290. zone->name,
  291. K(zone_page_state(zone, NR_FREE_PAGES)),
  292. K(zone->watermark_boost),
  293. K(min_wmark_pages(zone)),
  294. K(low_wmark_pages(zone)),
  295. K(high_wmark_pages(zone)),
  296. K(zone->nr_reserved_highatomic),
  297. K(zone->nr_free_highatomic),
  298. K(zone_page_state(zone, NR_ZONE_ACTIVE_ANON)),
  299. K(zone_page_state(zone, NR_ZONE_INACTIVE_ANON)),
  300. K(zone_page_state(zone, NR_ZONE_ACTIVE_FILE)),
  301. K(zone_page_state(zone, NR_ZONE_INACTIVE_FILE)),
  302. K(zone_page_state(zone, NR_ZONE_UNEVICTABLE)),
  303. K(zone_page_state(zone, NR_ZONE_WRITE_PENDING)),
  304. #if IS_ENABLED(CONFIG_ZSMALLOC)
  305. K(zone_page_state(zone, NR_ZSPAGES)),
  306. #else
  307. 0UL,
  308. #endif
  309. K(zone->present_pages),
  310. K(zone_managed_pages(zone)),
  311. K(zone_page_state(zone, NR_MLOCK)),
  312. 0UL,
  313. K(free_pcp),
  314. K(this_cpu_read(zone->per_cpu_pageset->count)),
  315. K(zone_page_state(zone, NR_FREE_CMA_PAGES)));
  316. printk("lowmem_reserve[]:");
  317. for (i = 0; i < MAX_NR_ZONES; i++)
  318. printk(KERN_CONT " %ld", zone->lowmem_reserve[i]);
  319. printk(KERN_CONT "\n");
  320. }
  321. for_each_populated_zone(zone) {
  322. unsigned int order;
  323. unsigned long nr[NR_PAGE_ORDERS], flags, total = 0;
  324. unsigned char types[NR_PAGE_ORDERS];
  325. if (zone_idx(zone) > max_zone_idx)
  326. continue;
  327. if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
  328. continue;
  329. show_node(zone);
  330. printk(KERN_CONT "%s: ", zone->name);
  331. spin_lock_irqsave(&zone->lock, flags);
  332. for (order = 0; order < NR_PAGE_ORDERS; order++) {
  333. struct free_area *area = &zone->free_area[order];
  334. int type;
  335. nr[order] = area->nr_free;
  336. total += nr[order] << order;
  337. types[order] = 0;
  338. for (type = 0; type < MIGRATE_TYPES; type++) {
  339. if (!free_area_empty(area, type))
  340. types[order] |= 1 << type;
  341. }
  342. }
  343. spin_unlock_irqrestore(&zone->lock, flags);
  344. for (order = 0; order < NR_PAGE_ORDERS; order++) {
  345. printk(KERN_CONT "%lu*%lukB ",
  346. nr[order], K(1UL) << order);
  347. if (nr[order])
  348. show_migration_types(types[order]);
  349. }
  350. printk(KERN_CONT "= %lukB\n", K(total));
  351. }
  352. for_each_online_node(nid) {
  353. if (show_mem_node_skip(filter, nid, nodemask))
  354. continue;
  355. hugetlb_show_meminfo_node(nid);
  356. }
  357. printk("%ld total pagecache pages\n", global_node_page_state(NR_FILE_PAGES));
  358. show_swap_cache_info();
  359. }
  360. void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
  361. {
  362. unsigned long total = 0, reserved = 0, highmem = 0;
  363. struct zone *zone;
  364. printk("Mem-Info:\n");
  365. show_free_areas(filter, nodemask, max_zone_idx);
  366. for_each_populated_zone(zone) {
  367. total += zone->present_pages;
  368. reserved += zone->present_pages - zone_managed_pages(zone);
  369. if (is_highmem(zone))
  370. highmem += zone->present_pages;
  371. }
  372. printk("%lu pages RAM\n", total);
  373. printk("%lu pages HighMem/MovableOnly\n", highmem);
  374. printk("%lu pages reserved\n", reserved);
  375. #ifdef CONFIG_CMA
  376. printk("%lu pages cma reserved\n", totalcma_pages);
  377. #endif
  378. #ifdef CONFIG_MEMORY_FAILURE
  379. printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages));
  380. #endif
  381. #ifdef CONFIG_MEM_ALLOC_PROFILING
  382. static DEFINE_SPINLOCK(mem_alloc_profiling_spinlock);
  383. if (spin_trylock(&mem_alloc_profiling_spinlock)) {
  384. struct codetag_bytes tags[10];
  385. size_t i, nr;
  386. nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false);
  387. if (nr) {
  388. pr_notice("Memory allocations (profiling is currently turned %s):\n",
  389. mem_alloc_profiling_enabled() ? "on" : "off");
  390. for (i = 0; i < nr; i++) {
  391. struct codetag *ct = tags[i].ct;
  392. struct alloc_tag *tag = ct_to_alloc_tag(ct);
  393. struct alloc_tag_counters counter = alloc_tag_read(tag);
  394. char bytes[10];
  395. string_get_size(counter.bytes, 1, STRING_UNITS_2, bytes, sizeof(bytes));
  396. /* Same as alloc_tag_to_text() but w/o intermediate buffer */
  397. if (ct->modname)
  398. pr_notice("%12s %8llu %s:%u [%s] func:%s\n",
  399. bytes, counter.calls, ct->filename,
  400. ct->lineno, ct->modname, ct->function);
  401. else
  402. pr_notice("%12s %8llu %s:%u func:%s\n",
  403. bytes, counter.calls, ct->filename,
  404. ct->lineno, ct->function);
  405. }
  406. }
  407. spin_unlock(&mem_alloc_profiling_spinlock);
  408. }
  409. #endif
  410. }