blob: da751448d0e4ef219717294882df9bfb33a66174 [file] [log] [blame]
Steven Price30d621f2020-02-03 17:36:20 -08001// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/pagewalk.h>
4#include <linux/ptdump.h>
5#include <linux/kasan.h>
6
Andrey Konovalov0fea6e92020-12-22 12:02:06 -08007#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
Steven Price30d621f2020-02-03 17:36:20 -08008/*
9 * This is an optimization for KASAN=y case. Since all kasan page tables
10 * eventually point to the kasan_early_shadow_page we could call note_page()
11 * right away without walking through lower level page tables. This saves
12 * us dozens of seconds (minutes for 5-level config) while checking for
13 * W+X mapping or reading kernel_page_tables debugfs file.
14 */
15static inline int note_kasan_page_table(struct mm_walk *walk,
16 unsigned long addr)
17{
18 struct ptdump_state *st = walk->private;
19
Steven Pricef8f0d0b2020-02-03 17:36:38 -080020 st->note_page(st, addr, 4, pte_val(kasan_early_shadow_pte[0]));
Steven Price30d621f2020-02-03 17:36:20 -080021
22 walk->action = ACTION_CONTINUE;
23
24 return 0;
25}
26#endif
27
28static int ptdump_pgd_entry(pgd_t *pgd, unsigned long addr,
29 unsigned long next, struct mm_walk *walk)
30{
31 struct ptdump_state *st = walk->private;
32 pgd_t val = READ_ONCE(*pgd);
33
Andrey Konovalov0fea6e92020-12-22 12:02:06 -080034#if CONFIG_PGTABLE_LEVELS > 4 && \
35 (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS))
Steven Price30d621f2020-02-03 17:36:20 -080036 if (pgd_page(val) == virt_to_page(lm_alias(kasan_early_shadow_p4d)))
37 return note_kasan_page_table(walk, addr);
38#endif
39
Steven Price1494e0c2020-06-01 21:49:58 -070040 if (st->effective_prot)
41 st->effective_prot(st, 0, pgd_val(val));
42
Steven Price30d621f2020-02-03 17:36:20 -080043 if (pgd_leaf(val))
Steven Pricef8f0d0b2020-02-03 17:36:38 -080044 st->note_page(st, addr, 0, pgd_val(val));
Steven Price30d621f2020-02-03 17:36:20 -080045
46 return 0;
47}
48
49static int ptdump_p4d_entry(p4d_t *p4d, unsigned long addr,
50 unsigned long next, struct mm_walk *walk)
51{
52 struct ptdump_state *st = walk->private;
53 p4d_t val = READ_ONCE(*p4d);
54
Andrey Konovalov0fea6e92020-12-22 12:02:06 -080055#if CONFIG_PGTABLE_LEVELS > 3 && \
56 (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS))
Steven Price30d621f2020-02-03 17:36:20 -080057 if (p4d_page(val) == virt_to_page(lm_alias(kasan_early_shadow_pud)))
58 return note_kasan_page_table(walk, addr);
59#endif
60
Steven Price1494e0c2020-06-01 21:49:58 -070061 if (st->effective_prot)
62 st->effective_prot(st, 1, p4d_val(val));
63
Steven Price30d621f2020-02-03 17:36:20 -080064 if (p4d_leaf(val))
Steven Pricef8f0d0b2020-02-03 17:36:38 -080065 st->note_page(st, addr, 1, p4d_val(val));
Steven Price30d621f2020-02-03 17:36:20 -080066
67 return 0;
68}
69
70static int ptdump_pud_entry(pud_t *pud, unsigned long addr,
71 unsigned long next, struct mm_walk *walk)
72{
73 struct ptdump_state *st = walk->private;
74 pud_t val = READ_ONCE(*pud);
75
Andrey Konovalov0fea6e92020-12-22 12:02:06 -080076#if CONFIG_PGTABLE_LEVELS > 2 && \
77 (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS))
Steven Price30d621f2020-02-03 17:36:20 -080078 if (pud_page(val) == virt_to_page(lm_alias(kasan_early_shadow_pmd)))
79 return note_kasan_page_table(walk, addr);
80#endif
81
Steven Price1494e0c2020-06-01 21:49:58 -070082 if (st->effective_prot)
83 st->effective_prot(st, 2, pud_val(val));
84
Steven Price30d621f2020-02-03 17:36:20 -080085 if (pud_leaf(val))
Steven Pricef8f0d0b2020-02-03 17:36:38 -080086 st->note_page(st, addr, 2, pud_val(val));
Steven Price30d621f2020-02-03 17:36:20 -080087
88 return 0;
89}
90
91static int ptdump_pmd_entry(pmd_t *pmd, unsigned long addr,
92 unsigned long next, struct mm_walk *walk)
93{
94 struct ptdump_state *st = walk->private;
95 pmd_t val = READ_ONCE(*pmd);
96
Andrey Konovalov0fea6e92020-12-22 12:02:06 -080097#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
Steven Price30d621f2020-02-03 17:36:20 -080098 if (pmd_page(val) == virt_to_page(lm_alias(kasan_early_shadow_pte)))
99 return note_kasan_page_table(walk, addr);
100#endif
101
Steven Price1494e0c2020-06-01 21:49:58 -0700102 if (st->effective_prot)
103 st->effective_prot(st, 3, pmd_val(val));
Steven Price30d621f2020-02-03 17:36:20 -0800104 if (pmd_leaf(val))
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800105 st->note_page(st, addr, 3, pmd_val(val));
Steven Price30d621f2020-02-03 17:36:20 -0800106
107 return 0;
108}
109
110static int ptdump_pte_entry(pte_t *pte, unsigned long addr,
111 unsigned long next, struct mm_walk *walk)
112{
113 struct ptdump_state *st = walk->private;
Christophe Leroy45837692021-04-16 15:46:20 -0700114 pte_t val = ptep_get(pte);
Steven Price30d621f2020-02-03 17:36:20 -0800115
Steven Price1494e0c2020-06-01 21:49:58 -0700116 if (st->effective_prot)
117 st->effective_prot(st, 4, pte_val(val));
118
119 st->note_page(st, addr, 4, pte_val(val));
Steven Price30d621f2020-02-03 17:36:20 -0800120
121 return 0;
122}
123
124static int ptdump_hole(unsigned long addr, unsigned long next,
125 int depth, struct mm_walk *walk)
126{
127 struct ptdump_state *st = walk->private;
128
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800129 st->note_page(st, addr, depth, 0);
Steven Price30d621f2020-02-03 17:36:20 -0800130
131 return 0;
132}
133
134static const struct mm_walk_ops ptdump_ops = {
135 .pgd_entry = ptdump_pgd_entry,
136 .p4d_entry = ptdump_p4d_entry,
137 .pud_entry = ptdump_pud_entry,
138 .pmd_entry = ptdump_pmd_entry,
139 .pte_entry = ptdump_pte_entry,
140 .pte_hole = ptdump_hole,
141};
142
Steven Pricee47690d2020-02-03 17:36:42 -0800143void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm, pgd_t *pgd)
Steven Price30d621f2020-02-03 17:36:20 -0800144{
145 const struct ptdump_range *range = st->range;
146
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700147 mmap_read_lock(mm);
Steven Price30d621f2020-02-03 17:36:20 -0800148 while (range->start != range->end) {
149 walk_page_range_novma(mm, range->start, range->end,
Steven Pricee47690d2020-02-03 17:36:42 -0800150 &ptdump_ops, pgd, st);
Steven Price30d621f2020-02-03 17:36:20 -0800151 range++;
152 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700153 mmap_read_unlock(mm);
Steven Price30d621f2020-02-03 17:36:20 -0800154
155 /* Flush out the last page */
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800156 st->note_page(st, 0, -1, 0);
Steven Price30d621f2020-02-03 17:36:20 -0800157}