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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/pagewalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/ptdump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/kasan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * This is an optimization for KASAN=y case. Since all kasan page tables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * eventually point to the kasan_early_shadow_page we could call note_page()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * right away without walking through lower level page tables. This saves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * us dozens of seconds (minutes for 5-level config) while checking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * W+X mapping or reading kernel_page_tables debugfs file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) static inline int note_kasan_page_table(struct mm_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 					unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	struct ptdump_state *st = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	st->note_page(st, addr, 4, pte_val(kasan_early_shadow_pte[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	walk->action = ACTION_CONTINUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static int ptdump_pgd_entry(pgd_t *pgd, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 			    unsigned long next, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct ptdump_state *st = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	pgd_t val = READ_ONCE(*pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #if CONFIG_PGTABLE_LEVELS > 4 && \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		(defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (pgd_page(val) == virt_to_page(lm_alias(kasan_early_shadow_p4d)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		return note_kasan_page_table(walk, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (st->effective_prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		st->effective_prot(st, 0, pgd_val(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (pgd_leaf(val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		st->note_page(st, addr, 0, pgd_val(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static int ptdump_p4d_entry(p4d_t *p4d, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			    unsigned long next, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct ptdump_state *st = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	p4d_t val = READ_ONCE(*p4d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #if CONFIG_PGTABLE_LEVELS > 3 && \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		(defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (p4d_page(val) == virt_to_page(lm_alias(kasan_early_shadow_pud)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return note_kasan_page_table(walk, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (st->effective_prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		st->effective_prot(st, 1, p4d_val(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (p4d_leaf(val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		st->note_page(st, addr, 1, p4d_val(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static int ptdump_pud_entry(pud_t *pud, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			    unsigned long next, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct ptdump_state *st = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	pud_t val = READ_ONCE(*pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #if CONFIG_PGTABLE_LEVELS > 2 && \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		(defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (pud_page(val) == virt_to_page(lm_alias(kasan_early_shadow_pmd)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return note_kasan_page_table(walk, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (st->effective_prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		st->effective_prot(st, 2, pud_val(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (pud_leaf(val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		st->note_page(st, addr, 2, pud_val(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static int ptdump_pmd_entry(pmd_t *pmd, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			    unsigned long next, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct ptdump_state *st = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	pmd_t val = READ_ONCE(*pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (pmd_page(val) == virt_to_page(lm_alias(kasan_early_shadow_pte)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return note_kasan_page_table(walk, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (st->effective_prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		st->effective_prot(st, 3, pmd_val(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (pmd_leaf(val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		st->note_page(st, addr, 3, pmd_val(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int ptdump_pte_entry(pte_t *pte, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			    unsigned long next, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct ptdump_state *st = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	pte_t val = ptep_get(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (st->effective_prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		st->effective_prot(st, 4, pte_val(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	st->note_page(st, addr, 4, pte_val(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return 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) static int ptdump_hole(unsigned long addr, unsigned long next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		       int depth, struct mm_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct ptdump_state *st = walk->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	st->note_page(st, addr, depth, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static const struct mm_walk_ops ptdump_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	.pgd_entry	= ptdump_pgd_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	.p4d_entry	= ptdump_p4d_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	.pud_entry	= ptdump_pud_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	.pmd_entry	= ptdump_pmd_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	.pte_entry	= ptdump_pte_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	.pte_hole	= ptdump_hole,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm, pgd_t *pgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	const struct ptdump_range *range = st->range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	mmap_read_lock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	while (range->start != range->end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		walk_page_range_novma(mm, range->start, range->end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 				      &ptdump_ops, pgd, st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		range++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	mmap_read_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	/* Flush out the last page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	st->note_page(st, 0, -1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }