^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/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/ksm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/mmzone.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/huge_mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/hugetlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/memcontrol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mmu_notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/page_idle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/kernel-page-flags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define KPMSIZE sizeof(u64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define KPMMASK (KPMSIZE - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define KPMBITS (KPMSIZE * BITS_PER_BYTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static inline unsigned long get_max_dump_pfn(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #ifdef CONFIG_SPARSEMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * The memmap of early sections is completely populated and marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * online even if max_pfn does not fall on a section boundary -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * pfn_to_online_page() will succeed on all pages. Allow inspecting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * these memmaps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return round_up(max_pfn, PAGES_PER_SECTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return max_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* /proc/kpagecount - an array exposing page counts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * Each entry is a u64 representing the corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * physical page count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static ssize_t kpagecount_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) const unsigned long max_dump_pfn = get_max_dump_pfn();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u64 __user *out = (u64 __user *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct page *ppage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned long src = *ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) u64 pcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) pfn = src / KPMSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (src & KPMMASK || count & KPMMASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (src >= max_dump_pfn * KPMSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) while (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * TODO: ZONE_DEVICE support requires to identify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * memmaps that were actually initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ppage = pfn_to_online_page(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!ppage || PageSlab(ppage) || page_has_type(ppage))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) pcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) pcount = page_mapcount(ppage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (put_user(pcount, out)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) pfn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) out++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) count -= KPMSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) *ppos += (char __user *)out - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ret = (char __user *)out - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static const struct proc_ops kpagecount_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .proc_lseek = mem_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .proc_read = kpagecount_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* /proc/kpageflags - an array exposing page flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * Each entry is a u64 representing the corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * physical page flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return ((kflags >> kbit) & 1) << ubit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) u64 stable_page_flags(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) u64 k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) u64 u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * pseudo flag: KPF_NOPAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * it differentiates a memory hole from a page with no flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return 1 << KPF_NOPAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) k = page->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) u = 0;
^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) * pseudo flags for the well known (anonymous) memory mapped pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * Note that page->_mapcount is overloaded in SLOB/SLUB/SLQB, so the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * simple test in page_mapped() is not enough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!PageSlab(page) && page_mapped(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) u |= 1 << KPF_MMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (PageAnon(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) u |= 1 << KPF_ANON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (PageKsm(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) u |= 1 << KPF_KSM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * compound pages: export both head/tail info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * they together define a compound page's start/end pos and order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (PageHead(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) u |= 1 << KPF_COMPOUND_HEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (PageTail(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) u |= 1 << KPF_COMPOUND_TAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (PageHuge(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) u |= 1 << KPF_HUGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * PageTransCompound can be true for non-huge compound pages (slab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * pages or pages allocated by drivers with __GFP_COMP) because it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * just checks PG_head/PG_tail, so we need to check PageLRU/PageAnon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * to make sure a given page is a thp, not a non-huge compound page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) else if (PageTransCompound(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct page *head = compound_head(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (PageLRU(head) || PageAnon(head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) u |= 1 << KPF_THP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) else if (is_huge_zero_page(head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) u |= 1 << KPF_ZERO_PAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) u |= 1 << KPF_THP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) } else if (is_zero_pfn(page_to_pfn(page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) u |= 1 << KPF_ZERO_PAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * Caveats on high order pages: page->_refcount will only be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * -1 on the head page; SLUB/SLQB do the same for PG_slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * SLOB won't set PG_slab at all on compound pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (PageBuddy(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) u |= 1 << KPF_BUDDY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) else if (page_count(page) == 0 && is_free_buddy_page(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) u |= 1 << KPF_BUDDY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (PageOffline(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) u |= 1 << KPF_OFFLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (PageTable(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) u |= 1 << KPF_PGTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (page_is_idle(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) u |= 1 << KPF_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (PageTail(page) && PageSlab(compound_head(page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) u |= 1 << KPF_SLAB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) u |= kpf_copy_bit(k, KPF_UPTODATE, PG_uptodate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) u |= kpf_copy_bit(k, KPF_WRITEBACK, PG_writeback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) u |= kpf_copy_bit(k, KPF_LRU, PG_lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) u |= kpf_copy_bit(k, KPF_REFERENCED, PG_referenced);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) u |= kpf_copy_bit(k, KPF_ACTIVE, PG_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (PageSwapCache(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) u |= 1 << KPF_SWAPCACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) u |= kpf_copy_bit(k, KPF_MLOCKED, PG_mlocked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) #ifdef CONFIG_MEMORY_FAILURE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) u |= kpf_copy_bit(k, KPF_HWPOISON, PG_hwpoison);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) #ifdef CONFIG_ARCH_USES_PG_UNCACHED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) u |= kpf_copy_bit(k, KPF_UNCACHED, PG_uncached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) u |= kpf_copy_bit(k, KPF_RESERVED, PG_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) u |= kpf_copy_bit(k, KPF_MAPPEDTODISK, PG_mappedtodisk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) u |= kpf_copy_bit(k, KPF_PRIVATE, PG_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) u |= kpf_copy_bit(k, KPF_PRIVATE_2, PG_private_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) u |= kpf_copy_bit(k, KPF_OWNER_PRIVATE, PG_owner_priv_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) u |= kpf_copy_bit(k, KPF_ARCH, PG_arch_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) u |= kpf_copy_bit(k, KPF_ARCH_2, PG_arch_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static ssize_t kpageflags_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) const unsigned long max_dump_pfn = get_max_dump_pfn();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) u64 __user *out = (u64 __user *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct page *ppage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) unsigned long src = *ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) pfn = src / KPMSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (src & KPMMASK || count & KPMMASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (src >= max_dump_pfn * KPMSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) while (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * TODO: ZONE_DEVICE support requires to identify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * memmaps that were actually initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ppage = pfn_to_online_page(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (put_user(stable_page_flags(ppage), out)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) pfn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) out++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) count -= KPMSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) *ppos += (char __user *)out - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) ret = (char __user *)out - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static const struct proc_ops kpageflags_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) .proc_lseek = mem_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) .proc_read = kpageflags_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) #ifdef CONFIG_MEMCG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static ssize_t kpagecgroup_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) const unsigned long max_dump_pfn = get_max_dump_pfn();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) u64 __user *out = (u64 __user *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct page *ppage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) unsigned long src = *ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) u64 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) pfn = src / KPMSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (src & KPMMASK || count & KPMMASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (src >= max_dump_pfn * KPMSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) while (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * TODO: ZONE_DEVICE support requires to identify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * memmaps that were actually initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ppage = pfn_to_online_page(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (ppage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) ino = page_cgroup_ino(ppage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (put_user(ino, out)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) pfn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) out++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) count -= KPMSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) *ppos += (char __user *)out - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) ret = (char __user *)out - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static const struct proc_ops kpagecgroup_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) .proc_lseek = mem_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) .proc_read = kpagecgroup_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) #endif /* CONFIG_MEMCG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static int __init proc_page_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) proc_create("kpagecount", S_IRUSR, NULL, &kpagecount_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) proc_create("kpageflags", S_IRUSR, NULL, &kpageflags_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) #ifdef CONFIG_MEMCG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) proc_create("kpagecgroup", S_IRUSR, NULL, &kpagecgroup_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) fs_initcall(proc_page_init);