Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) #include <linux/pagewalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3) #include <linux/vmacache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4) #include <linux/hugetlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) #include <linux/huge_mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/mempolicy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/rmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/swapops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/mmu_notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/page_idle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/shmem_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/pkeys.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <asm/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <asm/tlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #define SEQ_PUT_DEC(str, val) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 		seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) void task_mem(struct seq_file *m, struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	unsigned long text, lib, swap, anon, file, shmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	anon = get_mm_counter(mm, MM_ANONPAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	file = get_mm_counter(mm, MM_FILEPAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	shmem = get_mm_counter(mm, MM_SHMEMPAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	 * Note: to minimize their overhead, mm maintains hiwater_vm and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	 * hiwater_rss only when about to *lower* total_vm or rss.  Any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	 * collector of these hiwater stats must therefore get total_vm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	 * and rss too, which will usually be the higher.  Barriers? not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	 * worth the effort, such snapshots can always be inconsistent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	hiwater_vm = total_vm = mm->total_vm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	if (hiwater_vm < mm->hiwater_vm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 		hiwater_vm = mm->hiwater_vm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	hiwater_rss = total_rss = anon + file + shmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	if (hiwater_rss < mm->hiwater_rss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 		hiwater_rss = mm->hiwater_rss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	/* split executable areas between text and lib */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	text = PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	text = min(text, mm->exec_vm << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	lib = (mm->exec_vm << PAGE_SHIFT) - text;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	swap = get_mm_counter(mm, MM_SWAPENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	SEQ_PUT_DEC("VmPeak:\t", hiwater_vm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	SEQ_PUT_DEC(" kB\nVmSize:\t", total_vm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	SEQ_PUT_DEC(" kB\nVmLck:\t", mm->locked_vm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	SEQ_PUT_DEC(" kB\nVmPin:\t", atomic64_read(&mm->pinned_vm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	SEQ_PUT_DEC(" kB\nVmHWM:\t", hiwater_rss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	SEQ_PUT_DEC(" kB\nVmRSS:\t", total_rss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	SEQ_PUT_DEC(" kB\nRssAnon:\t", anon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	SEQ_PUT_DEC(" kB\nRssFile:\t", file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	SEQ_PUT_DEC(" kB\nRssShmem:\t", shmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	SEQ_PUT_DEC(" kB\nVmData:\t", mm->data_vm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	SEQ_PUT_DEC(" kB\nVmStk:\t", mm->stack_vm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	seq_put_decimal_ull_width(m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 		    " kB\nVmExe:\t", text >> 10, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	seq_put_decimal_ull_width(m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 		    " kB\nVmLib:\t", lib >> 10, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	seq_put_decimal_ull_width(m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		    " kB\nVmPTE:\t", mm_pgtables_bytes(mm) >> 10, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	SEQ_PUT_DEC(" kB\nVmSwap:\t", swap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	seq_puts(m, " kB\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	hugetlb_report_usage(m, mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #undef SEQ_PUT_DEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) unsigned long task_vsize(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	return PAGE_SIZE * mm->total_vm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) unsigned long task_statm(struct mm_struct *mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 			 unsigned long *shared, unsigned long *text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 			 unsigned long *data, unsigned long *resident)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	*shared = get_mm_counter(mm, MM_FILEPAGES) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 			get_mm_counter(mm, MM_SHMEMPAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	*text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 								>> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	*data = mm->data_vm + mm->stack_vm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	*resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	return mm->total_vm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) #ifdef CONFIG_NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * Save get_task_policy() for show_numa_map().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) static void hold_task_mempolicy(struct proc_maps_private *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	struct task_struct *task = priv->task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	task_lock(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	priv->task_mempolicy = get_task_policy(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	mpol_get(priv->task_mempolicy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	task_unlock(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) static void release_task_mempolicy(struct proc_maps_private *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	mpol_put(priv->task_mempolicy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) static void hold_task_mempolicy(struct proc_maps_private *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) static void release_task_mempolicy(struct proc_maps_private *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) static void seq_print_vma_name(struct seq_file *m, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	const char __user *name = vma_get_anon_name(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	struct mm_struct *mm = vma->vm_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	unsigned long page_start_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	unsigned long page_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	unsigned long num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	unsigned long max_len = NAME_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	page_start_vaddr = (unsigned long)name & PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	page_offset = (unsigned long)name - page_start_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	num_pages = DIV_ROUND_UP(page_offset + max_len, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	seq_puts(m, "[anon:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		int write_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		const char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		long pages_pinned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		pages_pinned = get_user_pages_remote(mm, page_start_vaddr, 1, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 						     &page, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		if (pages_pinned < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 			seq_puts(m, "<fault>]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		kaddr = (const char *)kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		len = min(max_len, PAGE_SIZE - page_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		write_len = strnlen(kaddr + page_offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		seq_write(m, kaddr + page_offset, write_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		put_user_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		/* if strnlen hit a null terminator then we're done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		if (write_len != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		max_len -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		page_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		page_start_vaddr += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	seq_putc(m, ']');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) static void *m_start(struct seq_file *m, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	struct proc_maps_private *priv = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	unsigned long last_addr = *ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	struct mm_struct *mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	struct vm_area_struct *vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	/* See m_next(). Zero at the start or after lseek. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	if (last_addr == -1UL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	priv->task = get_proc_task(priv->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	if (!priv->task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		return ERR_PTR(-ESRCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	mm = priv->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	if (!mm || !mmget_not_zero(mm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		put_task_struct(priv->task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		priv->task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	if (mmap_read_lock_killable(mm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		mmput(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		put_task_struct(priv->task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		priv->task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		return ERR_PTR(-EINTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	hold_task_mempolicy(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	priv->tail_vma = get_gate_vma(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	vma = find_vma(mm, last_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	if (vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		return vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	return priv->tail_vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) static void *m_next(struct seq_file *m, void *v, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	struct proc_maps_private *priv = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	struct vm_area_struct *next, *vma = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	if (vma == priv->tail_vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	else if (vma->vm_next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		next = vma->vm_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		next = priv->tail_vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	*ppos = next ? next->vm_start : -1UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	return next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) static void m_stop(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	struct proc_maps_private *priv = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	struct mm_struct *mm = priv->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	if (!priv->task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	release_task_mempolicy(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	mmap_read_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	mmput(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	put_task_struct(priv->task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	priv->task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) static int proc_maps_open(struct inode *inode, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 			const struct seq_operations *ops, int psize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	struct proc_maps_private *priv = __seq_open_private(file, ops, psize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	priv->inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	if (IS_ERR(priv->mm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		int err = PTR_ERR(priv->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		seq_release_private(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) static int proc_map_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	struct seq_file *seq = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	struct proc_maps_private *priv = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	if (priv->mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		mmdrop(priv->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	return seq_release_private(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) static int do_maps_open(struct inode *inode, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 			const struct seq_operations *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	return proc_maps_open(inode, file, ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 				sizeof(struct proc_maps_private));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286)  * Indicate if the VMA is a stack for the given task; for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287)  * /proc/PID/maps that is the stack of the main task.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) static int is_stack(struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	 * We make no effort to guess what a given thread considers to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	 * its "stack".  It's not even well-defined for programs written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	 * languages like Go.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	return vma->vm_start <= vma->vm_mm->start_stack &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		vma->vm_end >= vma->vm_mm->start_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) static void show_vma_header_prefix(struct seq_file *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 				   unsigned long start, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 				   vm_flags_t flags, unsigned long long pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 				   dev_t dev, unsigned long ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	seq_put_hex_ll(m, NULL, start, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	seq_put_hex_ll(m, "-", end, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	seq_putc(m, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	seq_putc(m, flags & VM_READ ? 'r' : '-');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	seq_putc(m, flags & VM_WRITE ? 'w' : '-');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	seq_putc(m, flags & VM_EXEC ? 'x' : '-');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	seq_putc(m, flags & VM_MAYSHARE ? 's' : 'p');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	seq_put_hex_ll(m, " ", pgoff, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	seq_put_hex_ll(m, " ", MAJOR(dev), 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	seq_put_hex_ll(m, ":", MINOR(dev), 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	seq_put_decimal_ull(m, " ", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	seq_putc(m, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	struct mm_struct *mm = vma->vm_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	struct file *file = vma->vm_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	vm_flags_t flags = vma->vm_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	unsigned long ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	unsigned long long pgoff = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	unsigned long start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	dev_t dev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	const char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	if (file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		struct inode *inode = file_inode(vma->vm_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		dev = inode->i_sb->s_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		ino = inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	start = vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	end = vma->vm_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	 * Print the dentry name for named mappings, and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	 * special [heap] marker for the heap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	if (file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		seq_pad(m, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		seq_file_path(m, file, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	if (vma->vm_ops && vma->vm_ops->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		name = vma->vm_ops->name(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		if (name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	name = arch_vma_name(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		if (!mm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 			name = "[vdso]";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		if (vma->vm_start <= mm->brk &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		    vma->vm_end >= mm->start_brk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 			name = "[heap]";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		if (is_stack(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 			name = "[stack]";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		if (vma_get_anon_name(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 			seq_pad(m, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 			seq_print_vma_name(m, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		seq_pad(m, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		seq_puts(m, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	seq_putc(m, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) static int show_map(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	show_map_vma(m, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) static const struct seq_operations proc_pid_maps_op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	.start	= m_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	.next	= m_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	.stop	= m_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	.show	= show_map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) static int pid_maps_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	return do_maps_open(inode, file, &proc_pid_maps_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) const struct file_operations proc_pid_maps_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	.open		= pid_maps_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	.read		= seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	.llseek		= seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	.release	= proc_map_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417)  * Proportional Set Size(PSS): my share of RSS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  * PSS of a process is the count of pages it has in memory, where each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  * page is divided by the number of processes sharing it.  So if a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  * process has 1000 pages all to itself, and 1000 shared with one other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422)  * process, its PSS will be 1500.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424)  * To keep (accumulated) division errors low, we adopt a 64bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425)  * fixed-point pss counter to minimize division errors. So (pss >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426)  * PSS_SHIFT) would be the real byte count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428)  * A shift of 12 before division means (assuming 4K page size):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429)  * 	- 1M 3-user-pages add up to 8KB errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  * 	- supports mapcount up to 2^24, or 16M;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  * 	- supports PSS up to 2^52 bytes, or 4PB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) #define PSS_SHIFT 12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) #ifdef CONFIG_PROC_PAGE_MONITOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) struct mem_size_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	unsigned long resident;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	unsigned long shared_clean;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	unsigned long shared_dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	unsigned long private_clean;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	unsigned long private_dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	unsigned long referenced;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	unsigned long anonymous;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	unsigned long lazyfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	unsigned long anonymous_thp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	unsigned long shmem_thp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	unsigned long file_thp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	unsigned long swap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	unsigned long shared_hugetlb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	unsigned long private_hugetlb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	u64 pss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	u64 pss_anon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	u64 pss_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	u64 pss_shmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	u64 pss_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	u64 swap_pss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	bool check_shmem_swap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) static void smaps_page_accumulate(struct mem_size_stats *mss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		struct page *page, unsigned long size, unsigned long pss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		bool dirty, bool locked, bool private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	mss->pss += pss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	if (PageAnon(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		mss->pss_anon += pss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	else if (PageSwapBacked(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		mss->pss_shmem += pss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		mss->pss_file += pss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	if (locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		mss->pss_locked += pss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	if (dirty || PageDirty(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		if (private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 			mss->private_dirty += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 			mss->shared_dirty += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		if (private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 			mss->private_clean += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 			mss->shared_clean += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) static void smaps_account(struct mem_size_stats *mss, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		bool compound, bool young, bool dirty, bool locked,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		bool migration)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	int i, nr = compound ? compound_nr(page) : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	unsigned long size = nr * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	 * First accumulate quantities that depend only on |size| and the type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	 * of the compound page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (PageAnon(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		mss->anonymous += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		if (!PageSwapBacked(page) && !dirty && !PageDirty(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 			mss->lazyfree += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	mss->resident += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	/* Accumulate the size in pages that have been accessed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	if (young || page_is_young(page) || PageReferenced(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		mss->referenced += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	 * Then accumulate quantities that may depend on sharing, or that may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	 * differ page-by-page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	 * page_count(page) == 1 guarantees the page is mapped exactly once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	 * If any subpage of the compound page mapped with PTE it would elevate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	 * page_count().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	 * The page_mapcount() is called to get a snapshot of the mapcount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	 * Without holding the page lock this snapshot can be slightly wrong as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	 * we cannot always read the mapcount atomically.  It is not safe to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	 * call page_mapcount() even with PTL held if the page is not mapped,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	 * especially for migration entries.  Treat regular migration entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	 * as mapcount == 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	if ((page_count(page) == 1) || migration) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		smaps_page_accumulate(mss, page, size, size << PSS_SHIFT, dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 			locked, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	for (i = 0; i < nr; i++, page++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		int mapcount = page_mapcount(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		unsigned long pss = PAGE_SIZE << PSS_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		if (mapcount >= 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 			pss /= mapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		smaps_page_accumulate(mss, page, PAGE_SIZE, pss, dirty, locked,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 				      mapcount < 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) #ifdef CONFIG_SHMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) static int smaps_pte_hole(unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			  __always_unused int depth, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	struct mem_size_stats *mss = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	mss->swap += shmem_partial_swap_usage(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			walk->vma->vm_file->f_mapping, addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) #define smaps_pte_hole		NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) #endif /* CONFIG_SHMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) static void smaps_pte_entry(pte_t *pte, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	struct mem_size_stats *mss = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	struct vm_area_struct *vma = walk->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	bool locked = !!(vma->vm_flags & VM_LOCKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	bool migration = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	if (pte_present(*pte)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		page = vm_normal_page(vma, addr, *pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	} else if (is_swap_pte(*pte)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		swp_entry_t swpent = pte_to_swp_entry(*pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		if (!non_swap_entry(swpent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 			int mapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 			mss->swap += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 			mapcount = swp_swapcount(swpent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 			if (mapcount >= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 				u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 				do_div(pss_delta, mapcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 				mss->swap_pss += pss_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 				mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		} else if (is_migration_entry(swpent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 			migration = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			page = migration_entry_to_page(swpent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		} else if (is_device_private_entry(swpent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 			page = device_private_entry_to_page(swpent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	} else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 							&& pte_none(*pte))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		page = xa_load(&vma->vm_file->f_mapping->i_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 						linear_page_index(vma, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		if (xa_is_value(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 			mss->swap += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	smaps_account(mss, page, false, pte_young(*pte), pte_dirty(*pte),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		      locked, migration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) #ifdef CONFIG_TRANSPARENT_HUGEPAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	struct mem_size_stats *mss = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	struct vm_area_struct *vma = walk->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	bool locked = !!(vma->vm_flags & VM_LOCKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	bool migration = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	if (pmd_present(*pmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		/* FOLL_DUMP will return -EFAULT on huge zero page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		page = follow_trans_huge_pmd(vma, addr, pmd, FOLL_DUMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	} else if (unlikely(thp_migration_supported() && is_swap_pmd(*pmd))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		swp_entry_t entry = pmd_to_swp_entry(*pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		if (is_migration_entry(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 			migration = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 			page = migration_entry_to_page(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	if (IS_ERR_OR_NULL(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	if (PageAnon(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		mss->anonymous_thp += HPAGE_PMD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	else if (PageSwapBacked(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		mss->shmem_thp += HPAGE_PMD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	else if (is_zone_device_page(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		/* pass */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		mss->file_thp += HPAGE_PMD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		      locked, migration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 			   struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	struct vm_area_struct *vma = walk->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	spinlock_t *ptl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	ptl = pmd_trans_huge_lock(pmd, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	if (ptl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		smaps_pmd_entry(pmd, addr, walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		spin_unlock(ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	if (pmd_trans_unstable(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	 * The mmap_lock held all the way back in m_start() is what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	 * keeps khugepaged out of here and from collapsing things
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	 * in here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	for (; addr != end; pte++, addr += PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		smaps_pte_entry(pte, addr, walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	pte_unmap_unlock(pte - 1, ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	 * Don't forget to update Documentation/ on changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	static const char mnemonics[BITS_PER_LONG][2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		 * In case if we meet a flag we don't know about.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		[0 ... (BITS_PER_LONG-1)] = "??",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		[ilog2(VM_READ)]	= "rd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		[ilog2(VM_WRITE)]	= "wr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		[ilog2(VM_EXEC)]	= "ex",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		[ilog2(VM_SHARED)]	= "sh",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		[ilog2(VM_MAYREAD)]	= "mr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		[ilog2(VM_MAYWRITE)]	= "mw",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		[ilog2(VM_MAYEXEC)]	= "me",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		[ilog2(VM_MAYSHARE)]	= "ms",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		[ilog2(VM_GROWSDOWN)]	= "gd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		[ilog2(VM_PFNMAP)]	= "pf",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		[ilog2(VM_DENYWRITE)]	= "dw",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		[ilog2(VM_LOCKED)]	= "lo",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		[ilog2(VM_IO)]		= "io",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		[ilog2(VM_SEQ_READ)]	= "sr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		[ilog2(VM_RAND_READ)]	= "rr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		[ilog2(VM_DONTCOPY)]	= "dc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		[ilog2(VM_DONTEXPAND)]	= "de",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		[ilog2(VM_ACCOUNT)]	= "ac",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		[ilog2(VM_NORESERVE)]	= "nr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		[ilog2(VM_HUGETLB)]	= "ht",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		[ilog2(VM_SYNC)]	= "sf",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		[ilog2(VM_ARCH_1)]	= "ar",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		[ilog2(VM_WIPEONFORK)]	= "wf",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		[ilog2(VM_DONTDUMP)]	= "dd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) #ifdef CONFIG_ARM64_BTI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		[ilog2(VM_ARM64_BTI)]	= "bt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) #ifdef CONFIG_MEM_SOFT_DIRTY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		[ilog2(VM_SOFTDIRTY)]	= "sd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		[ilog2(VM_MIXEDMAP)]	= "mm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		[ilog2(VM_HUGEPAGE)]	= "hg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		[ilog2(VM_NOHUGEPAGE)]	= "nh",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		[ilog2(VM_MERGEABLE)]	= "mg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		[ilog2(VM_UFFD_MISSING)]= "um",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		[ilog2(VM_UFFD_WP)]	= "uw",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) #ifdef CONFIG_ARM64_MTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		[ilog2(VM_MTE)]		= "mt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		[ilog2(VM_MTE_ALLOWED)]	= "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) #ifdef CONFIG_ARCH_HAS_PKEYS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		/* These come out via ProtectionKey: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		[ilog2(VM_PKEY_BIT0)]	= "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		[ilog2(VM_PKEY_BIT1)]	= "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		[ilog2(VM_PKEY_BIT2)]	= "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		[ilog2(VM_PKEY_BIT3)]	= "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) #if VM_PKEY_BIT4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		[ilog2(VM_PKEY_BIT4)]	= "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) #endif /* CONFIG_ARCH_HAS_PKEYS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		[ilog2(VM_UFFD_MINOR)]	= "ui",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	seq_puts(m, "VmFlags: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	for (i = 0; i < BITS_PER_LONG; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		if (!mnemonics[i][0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		if (vma->vm_flags & (1UL << i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			seq_putc(m, mnemonics[i][0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			seq_putc(m, mnemonics[i][1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			seq_putc(m, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	seq_putc(m, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) #ifdef CONFIG_HUGETLB_PAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 				 unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 				 struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	struct mem_size_stats *mss = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	struct vm_area_struct *vma = walk->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	if (pte_present(*pte)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		page = vm_normal_page(vma, addr, *pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	} else if (is_swap_pte(*pte)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		swp_entry_t swpent = pte_to_swp_entry(*pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		if (is_migration_entry(swpent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			page = migration_entry_to_page(swpent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		else if (is_device_private_entry(swpent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 			page = device_private_entry_to_page(swpent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	if (page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		int mapcount = page_mapcount(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		if (mapcount >= 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 			mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 			mss->private_hugetlb += huge_page_size(hstate_vma(vma));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) #define smaps_hugetlb_range	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) #endif /* HUGETLB_PAGE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) static const struct mm_walk_ops smaps_walk_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	.pmd_entry		= smaps_pte_range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	.hugetlb_entry		= smaps_hugetlb_range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) static const struct mm_walk_ops smaps_shmem_walk_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	.pmd_entry		= smaps_pte_range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	.hugetlb_entry		= smaps_hugetlb_range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	.pte_hole		= smaps_pte_hole,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801)  * Gather mem stats from @vma with the indicated beginning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802)  * address @start, and keep them in @mss.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804)  * Use vm_start of @vma as the beginning address if @start is 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) static void smap_gather_stats(struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		struct mem_size_stats *mss, unsigned long start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	const struct mm_walk_ops *ops = &smaps_walk_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	/* Invalid start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	if (start >= vma->vm_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) #ifdef CONFIG_SHMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	/* In case of smaps_rollup, reset the value from previous vma */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	mss->check_shmem_swap = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	if (vma->vm_file && shmem_mapping(vma->vm_file->f_mapping)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		 * For shared or readonly shmem mappings we know that all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		 * swapped out pages belong to the shmem object, and we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		 * obtain the swap value much more efficiently. For private
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		 * writable mappings, we might have COW pages that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		 * not affected by the parent swapped out pages of the shmem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		 * object, so we have to distinguish them during the page walk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		 * Unless we know that the shmem object (or the part mapped by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		 * our VMA) has no swapped out pages at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		unsigned long shmem_swapped = shmem_swap_usage(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		if (!start && (!shmem_swapped || (vma->vm_flags & VM_SHARED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 					!(vma->vm_flags & VM_WRITE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 			mss->swap += shmem_swapped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			mss->check_shmem_swap = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			ops = &smaps_shmem_walk_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	/* mmap_lock is held in m_start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	if (!start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		walk_page_vma(vma, ops, mss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		walk_page_range(vma->vm_mm, start, vma->vm_end, ops, mss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) #define SEQ_PUT_DEC(str, val) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		seq_put_decimal_ull_width(m, str, (val) >> 10, 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) /* Show the contents common for smaps and smaps_rollup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	bool rollup_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	SEQ_PUT_DEC("Rss:            ", mss->resident);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	SEQ_PUT_DEC(" kB\nPss:            ", mss->pss >> PSS_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	if (rollup_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		 * These are meaningful only for smaps_rollup, otherwise two of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		 * them are zero, and the other one is the same as Pss.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		SEQ_PUT_DEC(" kB\nPss_Anon:       ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 			mss->pss_anon >> PSS_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		SEQ_PUT_DEC(" kB\nPss_File:       ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			mss->pss_file >> PSS_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		SEQ_PUT_DEC(" kB\nPss_Shmem:      ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			mss->pss_shmem >> PSS_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	SEQ_PUT_DEC(" kB\nShared_Clean:   ", mss->shared_clean);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	SEQ_PUT_DEC(" kB\nShared_Dirty:   ", mss->shared_dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	SEQ_PUT_DEC(" kB\nPrivate_Clean:  ", mss->private_clean);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	SEQ_PUT_DEC(" kB\nPrivate_Dirty:  ", mss->private_dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	SEQ_PUT_DEC(" kB\nReferenced:     ", mss->referenced);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	SEQ_PUT_DEC(" kB\nAnonymous:      ", mss->anonymous);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	SEQ_PUT_DEC(" kB\nLazyFree:       ", mss->lazyfree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	SEQ_PUT_DEC(" kB\nAnonHugePages:  ", mss->anonymous_thp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	SEQ_PUT_DEC(" kB\nFilePmdMapped:  ", mss->file_thp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	SEQ_PUT_DEC(" kB\nShared_Hugetlb: ", mss->shared_hugetlb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	seq_put_decimal_ull_width(m, " kB\nPrivate_Hugetlb: ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 				  mss->private_hugetlb >> 10, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	SEQ_PUT_DEC(" kB\nSwap:           ", mss->swap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	SEQ_PUT_DEC(" kB\nSwapPss:        ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 					mss->swap_pss >> PSS_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	SEQ_PUT_DEC(" kB\nLocked:         ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 					mss->pss_locked >> PSS_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	seq_puts(m, " kB\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) static int show_smap(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	struct vm_area_struct *vma = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	struct mem_size_stats mss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	memset(&mss, 0, sizeof(mss));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	smap_gather_stats(vma, &mss, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	show_map_vma(m, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	if (vma_get_anon_name(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		seq_puts(m, "Name:           ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		seq_print_vma_name(m, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		seq_putc(m, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	SEQ_PUT_DEC("Size:           ", vma->vm_end - vma->vm_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	SEQ_PUT_DEC(" kB\nKernelPageSize: ", vma_kernel_pagesize(vma));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	SEQ_PUT_DEC(" kB\nMMUPageSize:    ", vma_mmu_pagesize(vma));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	seq_puts(m, " kB\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	__show_smap(m, &mss, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	seq_printf(m, "THPeligible:    %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		   transparent_hugepage_active(vma));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	if (arch_pkeys_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		seq_printf(m, "ProtectionKey:  %8u\n", vma_pkey(vma));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	show_smap_vma_flags(m, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) static int show_smaps_rollup(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	struct proc_maps_private *priv = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	struct mem_size_stats mss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	struct mm_struct *mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	struct vm_area_struct *vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	unsigned long last_vma_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	priv->task = get_proc_task(priv->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	if (!priv->task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	mm = priv->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	if (!mm || !mmget_not_zero(mm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		ret = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		goto out_put_task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	memset(&mss, 0, sizeof(mss));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	ret = mmap_read_lock_killable(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		goto out_put_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	hold_task_mempolicy(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	for (vma = priv->mm->mmap; vma;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		smap_gather_stats(vma, &mss, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		last_vma_end = vma->vm_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		 * Release mmap_lock temporarily if someone wants to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		 * access it for write request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		if (mmap_lock_is_contended(mm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			mmap_read_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			ret = mmap_read_lock_killable(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 				release_task_mempolicy(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 				goto out_put_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 			 * After dropping the lock, there are four cases to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			 * consider. See the following example for explanation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			 *   +------+------+-----------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			 *   | VMA1 | VMA2 | VMA3      |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			 *   +------+------+-----------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			 *   |      |      |           |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			 *  4k     8k     16k         400k
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			 * Suppose we drop the lock after reading VMA2 due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			 * contention, then we get:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 			 *	last_vma_end = 16k
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			 * 1) VMA2 is freed, but VMA3 exists:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 			 *    find_vma(mm, 16k - 1) will return VMA3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			 *    In this case, just continue from VMA3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 			 * 2) VMA2 still exists:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 			 *    find_vma(mm, 16k - 1) will return VMA2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			 *    Iterate the loop like the original one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 			 * 3) No more VMAs can be found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			 *    find_vma(mm, 16k - 1) will return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			 *    No more things to do, just break.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 			 * 4) (last_vma_end - 1) is the middle of a vma (VMA'):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 			 *    find_vma(mm, 16k - 1) will return VMA' whose range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 			 *    contains last_vma_end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 			 *    Iterate VMA' from last_vma_end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 			vma = find_vma(mm, last_vma_end - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			/* Case 3 above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 			if (!vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 			/* Case 1 above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 			if (vma->vm_start >= last_vma_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 			/* Case 4 above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			if (vma->vm_end > last_vma_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 				smap_gather_stats(vma, &mss, last_vma_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		/* Case 2 above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		vma = vma->vm_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	show_vma_header_prefix(m, priv->mm->mmap->vm_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 			       last_vma_end, 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	seq_pad(m, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	seq_puts(m, "[rollup]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	__show_smap(m, &mss, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	release_task_mempolicy(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	mmap_read_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) out_put_mm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	mmput(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) out_put_task:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	put_task_struct(priv->task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	priv->task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) #undef SEQ_PUT_DEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) static const struct seq_operations proc_pid_smaps_op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	.start	= m_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	.next	= m_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	.stop	= m_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	.show	= show_smap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) static int pid_smaps_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	return do_maps_open(inode, file, &proc_pid_smaps_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) static int smaps_rollup_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	struct proc_maps_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	priv = kzalloc(sizeof(*priv), GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	ret = single_open(file, show_smaps_rollup, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	priv->inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	if (IS_ERR(priv->mm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		ret = PTR_ERR(priv->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		single_release(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) static int smaps_rollup_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	struct seq_file *seq = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	struct proc_maps_private *priv = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	if (priv->mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		mmdrop(priv->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	return single_release(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) const struct file_operations proc_pid_smaps_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	.open		= pid_smaps_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	.read		= seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	.llseek		= seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	.release	= proc_map_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) const struct file_operations proc_pid_smaps_rollup_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	.open		= smaps_rollup_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	.read		= seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	.llseek		= seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	.release	= smaps_rollup_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) enum clear_refs_types {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	CLEAR_REFS_ALL = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	CLEAR_REFS_ANON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	CLEAR_REFS_MAPPED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	CLEAR_REFS_SOFT_DIRTY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	CLEAR_REFS_MM_HIWATER_RSS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	CLEAR_REFS_LAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) struct clear_refs_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	enum clear_refs_types type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) #ifdef CONFIG_MEM_SOFT_DIRTY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) #define is_cow_mapping(flags) (((flags) & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	if (!pte_write(pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	if (!is_cow_mapping(vma->vm_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	if (likely(!atomic_read(&vma->vm_mm->has_pinned)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	page = vm_normal_page(vma, addr, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	return page_maybe_dma_pinned(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) static inline void clear_soft_dirty(struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		unsigned long addr, pte_t *pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	 * The soft-dirty tracker uses #PF-s to catch writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	 * to pages, so write-protect the pte as well. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	 * Documentation/admin-guide/mm/soft-dirty.rst for full description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	 * of how soft-dirty works.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	pte_t ptent = *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	if (pte_present(ptent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		pte_t old_pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		if (pte_is_pinned(vma, addr, ptent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		old_pte = ptep_modify_prot_start(vma, addr, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		ptent = pte_wrprotect(old_pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		ptent = pte_clear_soft_dirty(ptent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	} else if (is_swap_pte(ptent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		ptent = pte_swp_clear_soft_dirty(ptent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		set_pte_at(vma->vm_mm, addr, pte, ptent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) static inline void clear_soft_dirty(struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		unsigned long addr, pte_t *pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) #if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		unsigned long addr, pmd_t *pmdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	pmd_t old, pmd = *pmdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	if (pmd_present(pmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		/* See comment in change_huge_pmd() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		old = pmdp_invalidate(vma, addr, pmdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		if (pmd_dirty(old))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			pmd = pmd_mkdirty(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		if (pmd_young(old))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 			pmd = pmd_mkyoung(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		pmd = pmd_wrprotect(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		pmd = pmd_clear_soft_dirty(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	} else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		pmd = pmd_swp_clear_soft_dirty(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		unsigned long addr, pmd_t *pmdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 				unsigned long end, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	struct clear_refs_private *cp = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	struct vm_area_struct *vma = walk->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	pte_t *pte, ptent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	spinlock_t *ptl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	ptl = pmd_trans_huge_lock(pmd, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	if (ptl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 			clear_soft_dirty_pmd(vma, addr, pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		if (!pmd_present(*pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		page = pmd_page(*pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		/* Clear accessed and referenced bits. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		pmdp_test_and_clear_young(vma, addr, pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		test_and_clear_page_young(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		ClearPageReferenced(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		spin_unlock(ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	if (pmd_trans_unstable(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	for (; addr != end; pte++, addr += PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		ptent = *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 			clear_soft_dirty(vma, addr, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		if (!pte_present(ptent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		page = vm_normal_page(vma, addr, ptent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		/* Clear accessed and referenced bits. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		ptep_test_and_clear_young(vma, addr, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		test_and_clear_page_young(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		ClearPageReferenced(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	pte_unmap_unlock(pte - 1, ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) static int clear_refs_test_walk(unsigned long start, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 				struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	struct clear_refs_private *cp = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	struct vm_area_struct *vma = walk->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	if (vma->vm_flags & VM_PFNMAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	 * Writing 1 to /proc/pid/clear_refs affects all pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	 * Writing 2 to /proc/pid/clear_refs only affects anonymous pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	 * Writing 3 to /proc/pid/clear_refs only affects file mapped pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	 * Writing 4 to /proc/pid/clear_refs affects all pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	if (cp->type == CLEAR_REFS_ANON && vma->vm_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	if (cp->type == CLEAR_REFS_MAPPED && !vma->vm_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) static const struct mm_walk_ops clear_refs_walk_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	.pmd_entry		= clear_refs_pte_range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	.test_walk		= clear_refs_test_walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) static ssize_t clear_refs_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 				size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	char buffer[PROC_NUMBUF];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	struct mm_struct *mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	struct vm_area_struct *vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	enum clear_refs_types type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	int itype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	memset(buffer, 0, sizeof(buffer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	if (count > sizeof(buffer) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		count = sizeof(buffer) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	if (copy_from_user(buffer, buf, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	rv = kstrtoint(strstrip(buffer), 10, &itype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	type = (enum clear_refs_types)itype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	if (type < CLEAR_REFS_ALL || type >= CLEAR_REFS_LAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	task = get_proc_task(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	if (!task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	mm = get_task_mm(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	if (mm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		struct mmu_notifier_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		struct clear_refs_private cp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 			.type = type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		if (mmap_write_lock_killable(mm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			count = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 			goto out_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		if (type == CLEAR_REFS_MM_HIWATER_RSS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 			 * Writing 5 to /proc/pid/clear_refs resets the peak
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 			 * resident set size to this mm's current rss value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			reset_mm_hiwater_rss(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		if (type == CLEAR_REFS_SOFT_DIRTY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 			for (vma = mm->mmap; vma; vma = vma->vm_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 				if (!(vma->vm_flags & VM_SOFTDIRTY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 				vm_write_begin(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 				WRITE_ONCE(vma->vm_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 					vma->vm_flags & ~VM_SOFTDIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 				vma_set_page_prot(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 				vm_write_end(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 			inc_tlb_flush_pending(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 			mmu_notifier_range_init(&range, MMU_NOTIFY_SOFT_DIRTY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 						0, NULL, mm, 0, -1UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			mmu_notifier_invalidate_range_start(&range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		walk_page_range(mm, 0, mm->highest_vm_end, &clear_refs_walk_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 				&cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		if (type == CLEAR_REFS_SOFT_DIRTY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			mmu_notifier_invalidate_range_end(&range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 			flush_tlb_mm(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			dec_tlb_flush_pending(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		mmap_write_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) out_mm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		mmput(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	put_task_struct(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) const struct file_operations proc_clear_refs_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	.write		= clear_refs_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	.llseek		= noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	u64 pme;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) } pagemap_entry_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) struct pagemapread {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	int pos, len;		/* units: PM_ENTRY_BYTES, not bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	pagemap_entry_t *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	bool show_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) #define PAGEMAP_WALK_SIZE	(PMD_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) #define PAGEMAP_WALK_MASK	(PMD_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) #define PM_ENTRY_BYTES		sizeof(pagemap_entry_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) #define PM_PFRAME_BITS		55
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) #define PM_PFRAME_MASK		GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) #define PM_SOFT_DIRTY		BIT_ULL(55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) #define PM_MMAP_EXCLUSIVE	BIT_ULL(56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) #define PM_FILE			BIT_ULL(61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) #define PM_SWAP			BIT_ULL(62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) #define PM_PRESENT		BIT_ULL(63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) #define PM_END_OF_BUFFER    1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) static inline pagemap_entry_t make_pme(u64 frame, u64 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	return (pagemap_entry_t) { .pme = (frame & PM_PFRAME_MASK) | flags };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) static int add_to_pagemap(unsigned long addr, pagemap_entry_t *pme,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 			  struct pagemapread *pm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	pm->buffer[pm->pos++] = *pme;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	if (pm->pos >= pm->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		return PM_END_OF_BUFFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) static int pagemap_pte_hole(unsigned long start, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 			    __always_unused int depth, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	struct pagemapread *pm = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	unsigned long addr = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	while (addr < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		struct vm_area_struct *vma = find_vma(walk->mm, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		pagemap_entry_t pme = make_pme(0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		/* End of address space hole, which we mark as non-present. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		unsigned long hole_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		if (vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 			hole_end = min(end, vma->vm_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 			hole_end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		for (; addr < hole_end; addr += PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 			err = add_to_pagemap(addr, &pme, pm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		if (!vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		/* Addresses in the VMA. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		if (vma->vm_flags & VM_SOFTDIRTY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 			pme = make_pme(0, PM_SOFT_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			err = add_to_pagemap(addr, &pme, pm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		struct vm_area_struct *vma, unsigned long addr, pte_t pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	u64 frame = 0, flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	bool migration = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	if (pte_present(pte)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		if (pm->show_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 			frame = pte_pfn(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		flags |= PM_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		page = vm_normal_page(vma, addr, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		if (pte_soft_dirty(pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 			flags |= PM_SOFT_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	} else if (is_swap_pte(pte)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		swp_entry_t entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		if (pte_swp_soft_dirty(pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 			flags |= PM_SOFT_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		entry = pte_to_swp_entry(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 		if (pm->show_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 			frame = swp_type(entry) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 				(swp_offset(entry) << MAX_SWAPFILES_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		flags |= PM_SWAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		if (is_migration_entry(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 			migration = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 			page = migration_entry_to_page(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		if (is_device_private_entry(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 			page = device_private_entry_to_page(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	if (page && !PageAnon(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		flags |= PM_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	if (page && !migration && page_mapcount(page) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		flags |= PM_MMAP_EXCLUSIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	if (vma->vm_flags & VM_SOFTDIRTY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		flags |= PM_SOFT_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	return make_pme(frame, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 			     struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	struct vm_area_struct *vma = walk->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	struct pagemapread *pm = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	spinlock_t *ptl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	pte_t *pte, *orig_pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) #ifdef CONFIG_TRANSPARENT_HUGEPAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	bool migration = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	ptl = pmd_trans_huge_lock(pmdp, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	if (ptl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		u64 flags = 0, frame = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 		pmd_t pmd = *pmdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		if (vma->vm_flags & VM_SOFTDIRTY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 			flags |= PM_SOFT_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		if (pmd_present(pmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 			page = pmd_page(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 			flags |= PM_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 			if (pmd_soft_dirty(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 				flags |= PM_SOFT_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 			if (pm->show_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 				frame = pmd_pfn(pmd) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 					((addr & ~PMD_MASK) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		else if (is_swap_pmd(pmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 			swp_entry_t entry = pmd_to_swp_entry(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 			unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 			if (pm->show_pfn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 				offset = swp_offset(entry) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 					((addr & ~PMD_MASK) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 				frame = swp_type(entry) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 					(offset << MAX_SWAPFILES_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 			flags |= PM_SWAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 			if (pmd_swp_soft_dirty(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 				flags |= PM_SOFT_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 			VM_BUG_ON(!is_pmd_migration_entry(pmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 			migration = is_migration_entry(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 			page = migration_entry_to_page(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		if (page && !migration && page_mapcount(page) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 			flags |= PM_MMAP_EXCLUSIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		for (; addr != end; addr += PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 			pagemap_entry_t pme = make_pme(frame, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			err = add_to_pagemap(addr, &pme, pm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 			if (pm->show_pfn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 				if (flags & PM_PRESENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 					frame++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 				else if (flags & PM_SWAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 					frame += (1 << MAX_SWAPFILES_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		spin_unlock(ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	if (pmd_trans_unstable(pmdp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	 * We can assume that @vma always points to a valid one and @end never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	 * goes beyond vma->vm_end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	orig_pte = pte = pte_offset_map_lock(walk->mm, pmdp, addr, &ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	for (; addr < end; pte++, addr += PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		pagemap_entry_t pme;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		pme = pte_to_pagemap_entry(pm, vma, addr, *pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		err = add_to_pagemap(addr, &pme, pm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	pte_unmap_unlock(orig_pte, ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) #ifdef CONFIG_HUGETLB_PAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) /* This function walks within one hugetlb entry in the single call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 				 unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 				 struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	struct pagemapread *pm = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	struct vm_area_struct *vma = walk->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	u64 flags = 0, frame = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	pte_t pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	if (vma->vm_flags & VM_SOFTDIRTY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		flags |= PM_SOFT_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	pte = huge_ptep_get(ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	if (pte_present(pte)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		struct page *page = pte_page(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		if (!PageAnon(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 			flags |= PM_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 		if (page_mapcount(page) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 			flags |= PM_MMAP_EXCLUSIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 		flags |= PM_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 		if (pm->show_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 			frame = pte_pfn(pte) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 				((addr & ~hmask) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	for (; addr != end; addr += PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		pagemap_entry_t pme = make_pme(frame, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 		err = add_to_pagemap(addr, &pme, pm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		if (pm->show_pfn && (flags & PM_PRESENT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 			frame++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) #define pagemap_hugetlb_range	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) #endif /* HUGETLB_PAGE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) static const struct mm_walk_ops pagemap_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	.pmd_entry	= pagemap_pmd_range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	.pte_hole	= pagemap_pte_hole,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	.hugetlb_entry	= pagemap_hugetlb_range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)  * /proc/pid/pagemap - an array mapping virtual pages to pfns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642)  * For each page in the address space, this file contains one 64-bit entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)  * consisting of the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645)  * Bits 0-54  page frame number (PFN) if present
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646)  * Bits 0-4   swap type if swapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647)  * Bits 5-54  swap offset if swapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)  * Bit  55    pte is soft-dirty (see Documentation/admin-guide/mm/soft-dirty.rst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)  * Bit  56    page exclusively mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650)  * Bits 57-60 zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)  * Bit  61    page is file-page or shared-anon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652)  * Bit  62    page swapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653)  * Bit  63    page present
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655)  * If the page is not present but in swap, then the PFN contains an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656)  * encoding of the swap file number and the page's offset into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657)  * swap. Unmapped pages return a null PFN. This allows determining
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658)  * precisely which pages are mapped (or in swap) and comparing mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659)  * pages between processes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661)  * Efficient users of this interface will use /proc/pid/maps to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662)  * determine which areas of memory are actually mapped and llseek to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)  * skip over unmapped regions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) static ssize_t pagemap_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 			    size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	struct mm_struct *mm = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	struct pagemapread pm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	unsigned long src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	unsigned long svpfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	unsigned long start_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	unsigned long end_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	int ret = 0, copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	if (!mm || !mmget_not_zero(mm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	/* file position must be aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 		goto out_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		goto out_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	/* do not disclose physical addresses: attack vector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	pm.show_pfn = file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	pm.len = (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	pm.buffer = kmalloc_array(pm.len, PM_ENTRY_BYTES, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	if (!pm.buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 		goto out_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	src = *ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	svpfn = src / PM_ENTRY_BYTES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	end_vaddr = mm->task_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	/* watch out for wraparound */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	start_vaddr = end_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	if (svpfn <= (ULONG_MAX >> PAGE_SHIFT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		start_vaddr = untagged_addr(svpfn << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	/* Ensure the address is inside the task */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	if (start_vaddr > mm->task_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 		start_vaddr = end_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	 * The odds are that this will stop walking way
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	 * before end_vaddr, because the length of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	 * user buffer is tracked in "pm", and the walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	 * will stop when we hit the end of the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	while (count && (start_vaddr < end_vaddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 		unsigned long end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		pm.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		/* overflow ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 		if (end < start_vaddr || end > end_vaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 			end = end_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		ret = mmap_read_lock_killable(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		ret = walk_page_range(mm, start_vaddr, end, &pagemap_ops, &pm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		mmap_read_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		start_vaddr = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 		len = min(count, PM_ENTRY_BYTES * pm.pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		if (copy_to_user(buf, pm.buffer, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		copied += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		buf += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		count -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	*ppos += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	if (!ret || ret == PM_END_OF_BUFFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		ret = copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	kfree(pm.buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) out_mm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	mmput(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) static int pagemap_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	struct mm_struct *mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	mm = proc_mem_open(inode, PTRACE_MODE_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	if (IS_ERR(mm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 		return PTR_ERR(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	file->private_data = mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) static int pagemap_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	struct mm_struct *mm = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	if (mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 		mmdrop(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) const struct file_operations proc_pagemap_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	.llseek		= mem_lseek, /* borrow this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	.read		= pagemap_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	.open		= pagemap_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	.release	= pagemap_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) #endif /* CONFIG_PROC_PAGE_MONITOR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) #ifdef CONFIG_NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) struct numa_maps {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	unsigned long pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	unsigned long anon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	unsigned long active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	unsigned long writeback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	unsigned long mapcount_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	unsigned long dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	unsigned long swapcache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	unsigned long node[MAX_NUMNODES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) struct numa_maps_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	struct proc_maps_private proc_maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	struct numa_maps md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) static void gather_stats(struct page *page, struct numa_maps *md, int pte_dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 			unsigned long nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	int count = page_mapcount(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	md->pages += nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	if (pte_dirty || PageDirty(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		md->dirty += nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	if (PageSwapCache(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		md->swapcache += nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	if (PageActive(page) || PageUnevictable(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		md->active += nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	if (PageWriteback(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		md->writeback += nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	if (PageAnon(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		md->anon += nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	if (count > md->mapcount_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		md->mapcount_max = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	md->node[page_to_nid(page)] += nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 		unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	int nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	if (!pte_present(pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	page = vm_normal_page(vma, addr, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	if (PageReserved(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	nid = page_to_nid(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	if (!node_isset(nid, node_states[N_MEMORY]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) #ifdef CONFIG_TRANSPARENT_HUGEPAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) static struct page *can_gather_numa_stats_pmd(pmd_t pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 					      struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 					      unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	int nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	if (!pmd_present(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	page = vm_normal_page_pmd(vma, addr, pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	if (PageReserved(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	nid = page_to_nid(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	if (!node_isset(nid, node_states[N_MEMORY]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		unsigned long end, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	struct numa_maps *md = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	struct vm_area_struct *vma = walk->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	spinlock_t *ptl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	pte_t *orig_pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) #ifdef CONFIG_TRANSPARENT_HUGEPAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	ptl = pmd_trans_huge_lock(pmd, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	if (ptl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 		page = can_gather_numa_stats_pmd(*pmd, vma, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 		if (page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 			gather_stats(page, md, pmd_dirty(*pmd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 				     HPAGE_PMD_SIZE/PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 		spin_unlock(ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	if (pmd_trans_unstable(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	orig_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		struct page *page = can_gather_numa_stats(*pte, vma, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		gather_stats(page, md, pte_dirty(*pte), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	} while (pte++, addr += PAGE_SIZE, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	pte_unmap_unlock(orig_pte, ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) #ifdef CONFIG_HUGETLB_PAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 		unsigned long addr, unsigned long end, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	pte_t huge_pte = huge_ptep_get(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	struct numa_maps *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	if (!pte_present(huge_pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	page = pte_page(huge_pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	md = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	gather_stats(page, md, pte_dirty(huge_pte), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 		unsigned long addr, unsigned long end, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) static const struct mm_walk_ops show_numa_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	.hugetlb_entry = gather_hugetlb_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	.pmd_entry = gather_pte_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947)  * Display pages allocated per node and memory policy via /proc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) static int show_numa_map(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	struct numa_maps_private *numa_priv = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	struct vm_area_struct *vma = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	struct numa_maps *md = &numa_priv->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	struct file *file = vma->vm_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	struct mm_struct *mm = vma->vm_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	struct mempolicy *pol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	char buffer[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	int nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	if (!mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	/* Ensure we start with an empty set of numa_maps statistics. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	memset(md, 0, sizeof(*md));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	pol = __get_vma_policy(vma, vma->vm_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	if (pol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 		mpol_to_str(buffer, sizeof(buffer), pol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		mpol_cond_put(pol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		mpol_to_str(buffer, sizeof(buffer), proc_priv->task_mempolicy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	seq_printf(m, "%08lx %s", vma->vm_start, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	if (file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		seq_puts(m, " file=");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 		seq_file_path(m, file, "\n\t= ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	} else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 		seq_puts(m, " heap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	} else if (is_stack(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 		seq_puts(m, " stack");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	if (is_vm_hugetlb_page(vma))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 		seq_puts(m, " huge");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	/* mmap_lock is held by m_start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	walk_page_vma(vma, &show_numa_ops, md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	if (!md->pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	if (md->anon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		seq_printf(m, " anon=%lu", md->anon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	if (md->dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 		seq_printf(m, " dirty=%lu", md->dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	if (md->pages != md->anon && md->pages != md->dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		seq_printf(m, " mapped=%lu", md->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	if (md->mapcount_max > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		seq_printf(m, " mapmax=%lu", md->mapcount_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	if (md->swapcache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 		seq_printf(m, " swapcache=%lu", md->swapcache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	if (md->active < md->pages && !is_vm_hugetlb_page(vma))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 		seq_printf(m, " active=%lu", md->active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	if (md->writeback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		seq_printf(m, " writeback=%lu", md->writeback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	for_each_node_state(nid, N_MEMORY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 		if (md->node[nid])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 			seq_printf(m, " N%d=%lu", nid, md->node[nid]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	seq_printf(m, " kernelpagesize_kB=%lu", vma_kernel_pagesize(vma) >> 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	seq_putc(m, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) static const struct seq_operations proc_pid_numa_maps_op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	.start  = m_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	.next   = m_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	.stop   = m_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	.show   = show_numa_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) static int pid_numa_maps_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	return proc_maps_open(inode, file, &proc_pid_numa_maps_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 				sizeof(struct numa_maps_private));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) const struct file_operations proc_pid_numa_maps_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	.open		= pid_numa_maps_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	.read		= seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	.llseek		= seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	.release	= proc_map_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) #endif /* CONFIG_NUMA */