xref: /openbmc/linux/fs/proc/meminfo.c (revision 47f8f9297d2247d65ee46d8403a73b30f8d0249b)
1e1759c21SAlexey Dobriyan #include <linux/fs.h>
2e1759c21SAlexey Dobriyan #include <linux/init.h>
3e1759c21SAlexey Dobriyan #include <linux/kernel.h>
4e1759c21SAlexey Dobriyan #include <linux/mm.h>
5cb900f41SKirill A. Shutemov #include <linux/hugetlb.h>
6e1759c21SAlexey Dobriyan #include <linux/mman.h>
7e1759c21SAlexey Dobriyan #include <linux/mmzone.h>
8e1759c21SAlexey Dobriyan #include <linux/proc_fs.h>
9e1759c21SAlexey Dobriyan #include <linux/quicklist.h>
10e1759c21SAlexey Dobriyan #include <linux/seq_file.h>
11e1759c21SAlexey Dobriyan #include <linux/swap.h>
12e1759c21SAlexey Dobriyan #include <linux/vmstat.h>
1360063497SArun Sharma #include <linux/atomic.h>
14db3808c1SJoonsoo Kim #include <linux/vmalloc.h>
15*47f8f929SPintu Kumar #ifdef CONFIG_CMA
16*47f8f929SPintu Kumar #include <linux/cma.h>
17*47f8f929SPintu Kumar #endif
18e1759c21SAlexey Dobriyan #include <asm/page.h>
19e1759c21SAlexey Dobriyan #include <asm/pgtable.h>
20e1759c21SAlexey Dobriyan #include "internal.h"
21e1759c21SAlexey Dobriyan 
22e1759c21SAlexey Dobriyan void __attribute__((weak)) arch_report_meminfo(struct seq_file *m)
23e1759c21SAlexey Dobriyan {
24e1759c21SAlexey Dobriyan }
25e1759c21SAlexey Dobriyan 
26e1759c21SAlexey Dobriyan static int meminfo_proc_show(struct seq_file *m, void *v)
27e1759c21SAlexey Dobriyan {
28e1759c21SAlexey Dobriyan 	struct sysinfo i;
29e1759c21SAlexey Dobriyan 	unsigned long committed;
30e1759c21SAlexey Dobriyan 	struct vmalloc_info vmi;
31e1759c21SAlexey Dobriyan 	long cached;
3234e431b0SRik van Riel 	long available;
3334e431b0SRik van Riel 	unsigned long pagecache;
3434e431b0SRik van Riel 	unsigned long wmark_low = 0;
35e1759c21SAlexey Dobriyan 	unsigned long pages[NR_LRU_LISTS];
3634e431b0SRik van Riel 	struct zone *zone;
37e1759c21SAlexey Dobriyan 	int lru;
38e1759c21SAlexey Dobriyan 
39e1759c21SAlexey Dobriyan /*
40e1759c21SAlexey Dobriyan  * display in kilobytes.
41e1759c21SAlexey Dobriyan  */
42e1759c21SAlexey Dobriyan #define K(x) ((x) << (PAGE_SHIFT - 10))
43e1759c21SAlexey Dobriyan 	si_meminfo(&i);
44e1759c21SAlexey Dobriyan 	si_swapinfo(&i);
4500a62ce9SKOSAKI Motohiro 	committed = percpu_counter_read_positive(&vm_committed_as);
46e1759c21SAlexey Dobriyan 
47e1759c21SAlexey Dobriyan 	cached = global_page_state(NR_FILE_PAGES) -
4833806f06SShaohua Li 			total_swapcache_pages() - i.bufferram;
49e1759c21SAlexey Dobriyan 	if (cached < 0)
50e1759c21SAlexey Dobriyan 		cached = 0;
51e1759c21SAlexey Dobriyan 
52e1759c21SAlexey Dobriyan 	get_vmalloc_info(&vmi);
53e1759c21SAlexey Dobriyan 
54e1759c21SAlexey Dobriyan 	for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
55e1759c21SAlexey Dobriyan 		pages[lru] = global_page_state(NR_LRU_BASE + lru);
56e1759c21SAlexey Dobriyan 
5734e431b0SRik van Riel 	for_each_zone(zone)
5834e431b0SRik van Riel 		wmark_low += zone->watermark[WMARK_LOW];
5934e431b0SRik van Riel 
6034e431b0SRik van Riel 	/*
6134e431b0SRik van Riel 	 * Estimate the amount of memory available for userspace allocations,
6234e431b0SRik van Riel 	 * without causing swapping.
6334e431b0SRik van Riel 	 *
6434e431b0SRik van Riel 	 * Free memory cannot be taken below the low watermark, before the
6534e431b0SRik van Riel 	 * system starts swapping.
6634e431b0SRik van Riel 	 */
6734e431b0SRik van Riel 	available = i.freeram - wmark_low;
6834e431b0SRik van Riel 
6934e431b0SRik van Riel 	/*
7034e431b0SRik van Riel 	 * Not all the page cache can be freed, otherwise the system will
7134e431b0SRik van Riel 	 * start swapping. Assume at least half of the page cache, or the
7234e431b0SRik van Riel 	 * low watermark worth of cache, needs to stay.
7334e431b0SRik van Riel 	 */
7434e431b0SRik van Riel 	pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE];
7534e431b0SRik van Riel 	pagecache -= min(pagecache / 2, wmark_low);
7634e431b0SRik van Riel 	available += pagecache;
7734e431b0SRik van Riel 
7834e431b0SRik van Riel 	/*
79f0b5664bSLuiz Capitulino 	 * Part of the reclaimable slab consists of items that are in use,
8034e431b0SRik van Riel 	 * and cannot be freed. Cap this estimate at the low watermark.
8134e431b0SRik van Riel 	 */
8234e431b0SRik van Riel 	available += global_page_state(NR_SLAB_RECLAIMABLE) -
8334e431b0SRik van Riel 		     min(global_page_state(NR_SLAB_RECLAIMABLE) / 2, wmark_low);
8434e431b0SRik van Riel 
8534e431b0SRik van Riel 	if (available < 0)
8634e431b0SRik van Riel 		available = 0;
8734e431b0SRik van Riel 
88e1759c21SAlexey Dobriyan 	/*
89e1759c21SAlexey Dobriyan 	 * Tagged format, for easy grepping and expansion.
90e1759c21SAlexey Dobriyan 	 */
91e1759c21SAlexey Dobriyan 	seq_printf(m,
92e1759c21SAlexey Dobriyan 		"MemTotal:       %8lu kB\n"
93e1759c21SAlexey Dobriyan 		"MemFree:        %8lu kB\n"
9434e431b0SRik van Riel 		"MemAvailable:   %8lu kB\n"
95e1759c21SAlexey Dobriyan 		"Buffers:        %8lu kB\n"
96e1759c21SAlexey Dobriyan 		"Cached:         %8lu kB\n"
97e1759c21SAlexey Dobriyan 		"SwapCached:     %8lu kB\n"
98e1759c21SAlexey Dobriyan 		"Active:         %8lu kB\n"
99e1759c21SAlexey Dobriyan 		"Inactive:       %8lu kB\n"
100e1759c21SAlexey Dobriyan 		"Active(anon):   %8lu kB\n"
101e1759c21SAlexey Dobriyan 		"Inactive(anon): %8lu kB\n"
102e1759c21SAlexey Dobriyan 		"Active(file):   %8lu kB\n"
103e1759c21SAlexey Dobriyan 		"Inactive(file): %8lu kB\n"
104e1759c21SAlexey Dobriyan 		"Unevictable:    %8lu kB\n"
105e1759c21SAlexey Dobriyan 		"Mlocked:        %8lu kB\n"
106e1759c21SAlexey Dobriyan #ifdef CONFIG_HIGHMEM
107e1759c21SAlexey Dobriyan 		"HighTotal:      %8lu kB\n"
108e1759c21SAlexey Dobriyan 		"HighFree:       %8lu kB\n"
109e1759c21SAlexey Dobriyan 		"LowTotal:       %8lu kB\n"
110e1759c21SAlexey Dobriyan 		"LowFree:        %8lu kB\n"
111e1759c21SAlexey Dobriyan #endif
1128feae131SDavid Howells #ifndef CONFIG_MMU
1138feae131SDavid Howells 		"MmapCopy:       %8lu kB\n"
1148feae131SDavid Howells #endif
115e1759c21SAlexey Dobriyan 		"SwapTotal:      %8lu kB\n"
116e1759c21SAlexey Dobriyan 		"SwapFree:       %8lu kB\n"
117e1759c21SAlexey Dobriyan 		"Dirty:          %8lu kB\n"
118e1759c21SAlexey Dobriyan 		"Writeback:      %8lu kB\n"
119e1759c21SAlexey Dobriyan 		"AnonPages:      %8lu kB\n"
120e1759c21SAlexey Dobriyan 		"Mapped:         %8lu kB\n"
1214b02108aSKOSAKI Motohiro 		"Shmem:          %8lu kB\n"
122e1759c21SAlexey Dobriyan 		"Slab:           %8lu kB\n"
123e1759c21SAlexey Dobriyan 		"SReclaimable:   %8lu kB\n"
124e1759c21SAlexey Dobriyan 		"SUnreclaim:     %8lu kB\n"
125c6a7f572SKOSAKI Motohiro 		"KernelStack:    %8lu kB\n"
126e1759c21SAlexey Dobriyan 		"PageTables:     %8lu kB\n"
127e1759c21SAlexey Dobriyan #ifdef CONFIG_QUICKLIST
128e1759c21SAlexey Dobriyan 		"Quicklists:     %8lu kB\n"
129e1759c21SAlexey Dobriyan #endif
130e1759c21SAlexey Dobriyan 		"NFS_Unstable:   %8lu kB\n"
131e1759c21SAlexey Dobriyan 		"Bounce:         %8lu kB\n"
132e1759c21SAlexey Dobriyan 		"WritebackTmp:   %8lu kB\n"
133e1759c21SAlexey Dobriyan 		"CommitLimit:    %8lu kB\n"
134e1759c21SAlexey Dobriyan 		"Committed_AS:   %8lu kB\n"
135e1759c21SAlexey Dobriyan 		"VmallocTotal:   %8lu kB\n"
136e1759c21SAlexey Dobriyan 		"VmallocUsed:    %8lu kB\n"
1376a46079cSAndi Kleen 		"VmallocChunk:   %8lu kB\n"
1386a46079cSAndi Kleen #ifdef CONFIG_MEMORY_FAILURE
139370c28deSHugh Dickins 		"HardwareCorrupted: %5lu kB\n"
1406a46079cSAndi Kleen #endif
14179134171SAndrea Arcangeli #ifdef CONFIG_TRANSPARENT_HUGEPAGE
14279134171SAndrea Arcangeli 		"AnonHugePages:  %8lu kB\n"
14379134171SAndrea Arcangeli #endif
144*47f8f929SPintu Kumar #ifdef CONFIG_CMA
145*47f8f929SPintu Kumar 		"CmaTotal:       %8lu kB\n"
146*47f8f929SPintu Kumar 		"CmaFree:        %8lu kB\n"
147*47f8f929SPintu Kumar #endif
1486a46079cSAndi Kleen 		,
149e1759c21SAlexey Dobriyan 		K(i.totalram),
150e1759c21SAlexey Dobriyan 		K(i.freeram),
15134e431b0SRik van Riel 		K(available),
152e1759c21SAlexey Dobriyan 		K(i.bufferram),
153e1759c21SAlexey Dobriyan 		K(cached),
15433806f06SShaohua Li 		K(total_swapcache_pages()),
155e1759c21SAlexey Dobriyan 		K(pages[LRU_ACTIVE_ANON]   + pages[LRU_ACTIVE_FILE]),
156e1759c21SAlexey Dobriyan 		K(pages[LRU_INACTIVE_ANON] + pages[LRU_INACTIVE_FILE]),
157e1759c21SAlexey Dobriyan 		K(pages[LRU_ACTIVE_ANON]),
158e1759c21SAlexey Dobriyan 		K(pages[LRU_INACTIVE_ANON]),
159e1759c21SAlexey Dobriyan 		K(pages[LRU_ACTIVE_FILE]),
160e1759c21SAlexey Dobriyan 		K(pages[LRU_INACTIVE_FILE]),
161e1759c21SAlexey Dobriyan 		K(pages[LRU_UNEVICTABLE]),
162e1759c21SAlexey Dobriyan 		K(global_page_state(NR_MLOCK)),
163e1759c21SAlexey Dobriyan #ifdef CONFIG_HIGHMEM
164e1759c21SAlexey Dobriyan 		K(i.totalhigh),
165e1759c21SAlexey Dobriyan 		K(i.freehigh),
166e1759c21SAlexey Dobriyan 		K(i.totalram-i.totalhigh),
167e1759c21SAlexey Dobriyan 		K(i.freeram-i.freehigh),
168e1759c21SAlexey Dobriyan #endif
1698feae131SDavid Howells #ifndef CONFIG_MMU
17033e5d769SDavid Howells 		K((unsigned long) atomic_long_read(&mmap_pages_allocated)),
1718feae131SDavid Howells #endif
172e1759c21SAlexey Dobriyan 		K(i.totalswap),
173e1759c21SAlexey Dobriyan 		K(i.freeswap),
174e1759c21SAlexey Dobriyan 		K(global_page_state(NR_FILE_DIRTY)),
175e1759c21SAlexey Dobriyan 		K(global_page_state(NR_WRITEBACK)),
176b53fc7c2SClaudio Scordino 		K(global_page_state(NR_ANON_PAGES)),
177e1759c21SAlexey Dobriyan 		K(global_page_state(NR_FILE_MAPPED)),
178cc7452b6SRafael Aquini 		K(i.sharedram),
179e1759c21SAlexey Dobriyan 		K(global_page_state(NR_SLAB_RECLAIMABLE) +
180e1759c21SAlexey Dobriyan 				global_page_state(NR_SLAB_UNRECLAIMABLE)),
181e1759c21SAlexey Dobriyan 		K(global_page_state(NR_SLAB_RECLAIMABLE)),
182e1759c21SAlexey Dobriyan 		K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
183c6a7f572SKOSAKI Motohiro 		global_page_state(NR_KERNEL_STACK) * THREAD_SIZE / 1024,
184e1759c21SAlexey Dobriyan 		K(global_page_state(NR_PAGETABLE)),
185e1759c21SAlexey Dobriyan #ifdef CONFIG_QUICKLIST
186e1759c21SAlexey Dobriyan 		K(quicklist_total_size()),
187e1759c21SAlexey Dobriyan #endif
188e1759c21SAlexey Dobriyan 		K(global_page_state(NR_UNSTABLE_NFS)),
189e1759c21SAlexey Dobriyan 		K(global_page_state(NR_BOUNCE)),
190e1759c21SAlexey Dobriyan 		K(global_page_state(NR_WRITEBACK_TEMP)),
19100619bccSJerome Marchand 		K(vm_commit_limit()),
192e1759c21SAlexey Dobriyan 		K(committed),
193e1759c21SAlexey Dobriyan 		(unsigned long)VMALLOC_TOTAL >> 10,
194e1759c21SAlexey Dobriyan 		vmi.used >> 10,
195e1759c21SAlexey Dobriyan 		vmi.largest_chunk >> 10
1966a46079cSAndi Kleen #ifdef CONFIG_MEMORY_FAILURE
197293c07e3SXishi Qiu 		, atomic_long_read(&num_poisoned_pages) << (PAGE_SHIFT - 10)
1986a46079cSAndi Kleen #endif
19979134171SAndrea Arcangeli #ifdef CONFIG_TRANSPARENT_HUGEPAGE
20079134171SAndrea Arcangeli 		, K(global_page_state(NR_ANON_TRANSPARENT_HUGEPAGES) *
20179134171SAndrea Arcangeli 		   HPAGE_PMD_NR)
20279134171SAndrea Arcangeli #endif
203*47f8f929SPintu Kumar #ifdef CONFIG_CMA
204*47f8f929SPintu Kumar 		, K(totalcma_pages)
205*47f8f929SPintu Kumar 		, K(global_page_state(NR_FREE_CMA_PAGES))
206*47f8f929SPintu Kumar #endif
207e1759c21SAlexey Dobriyan 		);
208e1759c21SAlexey Dobriyan 
209e1759c21SAlexey Dobriyan 	hugetlb_report_meminfo(m);
210e1759c21SAlexey Dobriyan 
211e1759c21SAlexey Dobriyan 	arch_report_meminfo(m);
212e1759c21SAlexey Dobriyan 
213e1759c21SAlexey Dobriyan 	return 0;
214e1759c21SAlexey Dobriyan #undef K
215e1759c21SAlexey Dobriyan }
216e1759c21SAlexey Dobriyan 
217e1759c21SAlexey Dobriyan static int meminfo_proc_open(struct inode *inode, struct file *file)
218e1759c21SAlexey Dobriyan {
219e1759c21SAlexey Dobriyan 	return single_open(file, meminfo_proc_show, NULL);
220e1759c21SAlexey Dobriyan }
221e1759c21SAlexey Dobriyan 
222e1759c21SAlexey Dobriyan static const struct file_operations meminfo_proc_fops = {
223e1759c21SAlexey Dobriyan 	.open		= meminfo_proc_open,
224e1759c21SAlexey Dobriyan 	.read		= seq_read,
225e1759c21SAlexey Dobriyan 	.llseek		= seq_lseek,
226e1759c21SAlexey Dobriyan 	.release	= single_release,
227e1759c21SAlexey Dobriyan };
228e1759c21SAlexey Dobriyan 
229e1759c21SAlexey Dobriyan static int __init proc_meminfo_init(void)
230e1759c21SAlexey Dobriyan {
231e1759c21SAlexey Dobriyan 	proc_create("meminfo", 0, NULL, &meminfo_proc_fops);
232e1759c21SAlexey Dobriyan 	return 0;
233e1759c21SAlexey Dobriyan }
234abaf3787SPaul Gortmaker fs_initcall(proc_meminfo_init);
235