xref: /openbmc/linux/fs/proc/meminfo.c (revision feac8c8b)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fs.h>
3 #include <linux/init.h>
4 #include <linux/kernel.h>
5 #include <linux/mm.h>
6 #include <linux/hugetlb.h>
7 #include <linux/mman.h>
8 #include <linux/mmzone.h>
9 #include <linux/proc_fs.h>
10 #include <linux/quicklist.h>
11 #include <linux/seq_file.h>
12 #include <linux/swap.h>
13 #include <linux/vmstat.h>
14 #include <linux/atomic.h>
15 #include <linux/vmalloc.h>
16 #ifdef CONFIG_CMA
17 #include <linux/cma.h>
18 #endif
19 #include <asm/page.h>
20 #include <asm/pgtable.h>
21 #include "internal.h"
22 
23 void __attribute__((weak)) arch_report_meminfo(struct seq_file *m)
24 {
25 }
26 
27 static void show_val_kb(struct seq_file *m, const char *s, unsigned long num)
28 {
29 	char v[32];
30 	static const char blanks[7] = {' ', ' ', ' ', ' ',' ', ' ', ' '};
31 	int len;
32 
33 	len = num_to_str(v, sizeof(v), num << (PAGE_SHIFT - 10));
34 
35 	seq_write(m, s, 16);
36 
37 	if (len > 0) {
38 		if (len < 8)
39 			seq_write(m, blanks, 8 - len);
40 
41 		seq_write(m, v, len);
42 	}
43 	seq_write(m, " kB\n", 4);
44 }
45 
46 static int meminfo_proc_show(struct seq_file *m, void *v)
47 {
48 	struct sysinfo i;
49 	unsigned long committed;
50 	long cached;
51 	long available;
52 	unsigned long pages[NR_LRU_LISTS];
53 	int lru;
54 
55 	si_meminfo(&i);
56 	si_swapinfo(&i);
57 	committed = percpu_counter_read_positive(&vm_committed_as);
58 
59 	cached = global_node_page_state(NR_FILE_PAGES) -
60 			total_swapcache_pages() - i.bufferram;
61 	if (cached < 0)
62 		cached = 0;
63 
64 	for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
65 		pages[lru] = global_node_page_state(NR_LRU_BASE + lru);
66 
67 	available = si_mem_available();
68 
69 	show_val_kb(m, "MemTotal:       ", i.totalram);
70 	show_val_kb(m, "MemFree:        ", i.freeram);
71 	show_val_kb(m, "MemAvailable:   ", available);
72 	show_val_kb(m, "Buffers:        ", i.bufferram);
73 	show_val_kb(m, "Cached:         ", cached);
74 	show_val_kb(m, "SwapCached:     ", total_swapcache_pages());
75 	show_val_kb(m, "Active:         ", pages[LRU_ACTIVE_ANON] +
76 					   pages[LRU_ACTIVE_FILE]);
77 	show_val_kb(m, "Inactive:       ", pages[LRU_INACTIVE_ANON] +
78 					   pages[LRU_INACTIVE_FILE]);
79 	show_val_kb(m, "Active(anon):   ", pages[LRU_ACTIVE_ANON]);
80 	show_val_kb(m, "Inactive(anon): ", pages[LRU_INACTIVE_ANON]);
81 	show_val_kb(m, "Active(file):   ", pages[LRU_ACTIVE_FILE]);
82 	show_val_kb(m, "Inactive(file): ", pages[LRU_INACTIVE_FILE]);
83 	show_val_kb(m, "Unevictable:    ", pages[LRU_UNEVICTABLE]);
84 	show_val_kb(m, "Mlocked:        ", global_zone_page_state(NR_MLOCK));
85 
86 #ifdef CONFIG_HIGHMEM
87 	show_val_kb(m, "HighTotal:      ", i.totalhigh);
88 	show_val_kb(m, "HighFree:       ", i.freehigh);
89 	show_val_kb(m, "LowTotal:       ", i.totalram - i.totalhigh);
90 	show_val_kb(m, "LowFree:        ", i.freeram - i.freehigh);
91 #endif
92 
93 #ifndef CONFIG_MMU
94 	show_val_kb(m, "MmapCopy:       ",
95 		    (unsigned long)atomic_long_read(&mmap_pages_allocated));
96 #endif
97 
98 	show_val_kb(m, "SwapTotal:      ", i.totalswap);
99 	show_val_kb(m, "SwapFree:       ", i.freeswap);
100 	show_val_kb(m, "Dirty:          ",
101 		    global_node_page_state(NR_FILE_DIRTY));
102 	show_val_kb(m, "Writeback:      ",
103 		    global_node_page_state(NR_WRITEBACK));
104 	show_val_kb(m, "AnonPages:      ",
105 		    global_node_page_state(NR_ANON_MAPPED));
106 	show_val_kb(m, "Mapped:         ",
107 		    global_node_page_state(NR_FILE_MAPPED));
108 	show_val_kb(m, "Shmem:          ", i.sharedram);
109 	show_val_kb(m, "Slab:           ",
110 		    global_node_page_state(NR_SLAB_RECLAIMABLE) +
111 		    global_node_page_state(NR_SLAB_UNRECLAIMABLE));
112 
113 	show_val_kb(m, "SReclaimable:   ",
114 		    global_node_page_state(NR_SLAB_RECLAIMABLE));
115 	show_val_kb(m, "SUnreclaim:     ",
116 		    global_node_page_state(NR_SLAB_UNRECLAIMABLE));
117 	seq_printf(m, "KernelStack:    %8lu kB\n",
118 		   global_zone_page_state(NR_KERNEL_STACK_KB));
119 	show_val_kb(m, "PageTables:     ",
120 		    global_zone_page_state(NR_PAGETABLE));
121 #ifdef CONFIG_QUICKLIST
122 	show_val_kb(m, "Quicklists:     ", quicklist_total_size());
123 #endif
124 
125 	show_val_kb(m, "NFS_Unstable:   ",
126 		    global_node_page_state(NR_UNSTABLE_NFS));
127 	show_val_kb(m, "Bounce:         ",
128 		    global_zone_page_state(NR_BOUNCE));
129 	show_val_kb(m, "WritebackTmp:   ",
130 		    global_node_page_state(NR_WRITEBACK_TEMP));
131 	show_val_kb(m, "CommitLimit:    ", vm_commit_limit());
132 	show_val_kb(m, "Committed_AS:   ", committed);
133 	seq_printf(m, "VmallocTotal:   %8lu kB\n",
134 		   (unsigned long)VMALLOC_TOTAL >> 10);
135 	show_val_kb(m, "VmallocUsed:    ", 0ul);
136 	show_val_kb(m, "VmallocChunk:   ", 0ul);
137 
138 #ifdef CONFIG_MEMORY_FAILURE
139 	seq_printf(m, "HardwareCorrupted: %5lu kB\n",
140 		   atomic_long_read(&num_poisoned_pages) << (PAGE_SHIFT - 10));
141 #endif
142 
143 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
144 	show_val_kb(m, "AnonHugePages:  ",
145 		    global_node_page_state(NR_ANON_THPS) * HPAGE_PMD_NR);
146 	show_val_kb(m, "ShmemHugePages: ",
147 		    global_node_page_state(NR_SHMEM_THPS) * HPAGE_PMD_NR);
148 	show_val_kb(m, "ShmemPmdMapped: ",
149 		    global_node_page_state(NR_SHMEM_PMDMAPPED) * HPAGE_PMD_NR);
150 #endif
151 
152 #ifdef CONFIG_CMA
153 	show_val_kb(m, "CmaTotal:       ", totalcma_pages);
154 	show_val_kb(m, "CmaFree:        ",
155 		    global_zone_page_state(NR_FREE_CMA_PAGES));
156 #endif
157 
158 	hugetlb_report_meminfo(m);
159 
160 	arch_report_meminfo(m);
161 
162 	return 0;
163 }
164 
165 static int meminfo_proc_open(struct inode *inode, struct file *file)
166 {
167 	return single_open(file, meminfo_proc_show, NULL);
168 }
169 
170 static const struct file_operations meminfo_proc_fops = {
171 	.open		= meminfo_proc_open,
172 	.read		= seq_read,
173 	.llseek		= seq_lseek,
174 	.release	= single_release,
175 };
176 
177 static int __init proc_meminfo_init(void)
178 {
179 	proc_create("meminfo", 0, NULL, &meminfo_proc_fops);
180 	return 0;
181 }
182 fs_initcall(proc_meminfo_init);
183