1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2021, Google LLC.
5 * Pasha Tatashin <pasha.tatashin@soleen.com>
7 #include <linux/kstrtox.h>
9 #include <linux/page_table_check.h>
10 #include <linux/swap.h>
11 #include <linux/swapops.h>
14 #define pr_fmt(fmt) "page_table_check: " fmt
16 struct page_table_check {
17 atomic_t anon_map_count;
18 atomic_t file_map_count;
21 static bool __page_table_check_enabled __initdata =
22 IS_ENABLED(CONFIG_PAGE_TABLE_CHECK_ENFORCED);
24 DEFINE_STATIC_KEY_TRUE(page_table_check_disabled);
25 EXPORT_SYMBOL(page_table_check_disabled);
27 static int __init early_page_table_check_param(char *buf)
29 return kstrtobool(buf, &__page_table_check_enabled);
32 early_param("page_table_check", early_page_table_check_param);
34 static bool __init need_page_table_check(void)
36 return __page_table_check_enabled;
39 static void __init init_page_table_check(void)
41 if (!__page_table_check_enabled)
43 static_branch_disable(&page_table_check_disabled);
46 struct page_ext_operations page_table_check_ops = {
47 .size = sizeof(struct page_table_check),
48 .need = need_page_table_check,
49 .init = init_page_table_check,
50 .need_shared_flags = false,
53 static struct page_table_check *get_page_table_check(struct page_ext *page_ext)
56 return page_ext_data(page_ext, &page_table_check_ops);
60 * An entry is removed from the page table, decrement the counters for that page
61 * verify that it is of correct type and counters do not become negative.
63 static void page_table_check_clear(unsigned long pfn, unsigned long pgcnt)
65 struct page_ext_iter iter;
66 struct page_ext *page_ext;
73 page = pfn_to_page(pfn);
74 BUG_ON(PageSlab(page));
75 anon = PageAnon(page);
78 for_each_page_ext(page, pgcnt, page_ext, iter) {
79 struct page_table_check *ptc = get_page_table_check(page_ext);
82 BUG_ON(atomic_read(&ptc->file_map_count));
83 BUG_ON(atomic_dec_return(&ptc->anon_map_count) < 0);
85 BUG_ON(atomic_read(&ptc->anon_map_count));
86 BUG_ON(atomic_dec_return(&ptc->file_map_count) < 0);
93 * A new entry is added to the page table, increment the counters for that page
94 * verify that it is of correct type and is not being mapped with a different
95 * type to a different process.
97 static void page_table_check_set(unsigned long pfn, unsigned long pgcnt,
100 struct page_ext_iter iter;
101 struct page_ext *page_ext;
108 page = pfn_to_page(pfn);
109 BUG_ON(PageSlab(page));
110 anon = PageAnon(page);
113 for_each_page_ext(page, pgcnt, page_ext, iter) {
114 struct page_table_check *ptc = get_page_table_check(page_ext);
117 BUG_ON(atomic_read(&ptc->file_map_count));
118 BUG_ON(atomic_inc_return(&ptc->anon_map_count) > 1 && rw);
120 BUG_ON(atomic_read(&ptc->anon_map_count));
121 BUG_ON(atomic_inc_return(&ptc->file_map_count) < 0);
128 * page is on free list, or is being allocated, verify that counters are zeroes
129 * crash if they are not.
131 void __page_table_check_zero(struct page *page, unsigned int order)
133 struct page_ext_iter iter;
134 struct page_ext *page_ext;
136 BUG_ON(PageSlab(page));
139 for_each_page_ext(page, 1 << order, page_ext, iter) {
140 struct page_table_check *ptc = get_page_table_check(page_ext);
142 BUG_ON(atomic_read(&ptc->anon_map_count));
143 BUG_ON(atomic_read(&ptc->file_map_count));
148 void __page_table_check_pte_clear(struct mm_struct *mm, pte_t pte)
153 if (pte_user_accessible_page(pte)) {
154 page_table_check_clear(pte_pfn(pte), PAGE_SIZE >> PAGE_SHIFT);
157 EXPORT_SYMBOL(__page_table_check_pte_clear);
159 void __page_table_check_pmd_clear(struct mm_struct *mm, pmd_t pmd)
164 if (pmd_user_accessible_page(pmd)) {
165 page_table_check_clear(pmd_pfn(pmd), PMD_SIZE >> PAGE_SHIFT);
168 EXPORT_SYMBOL(__page_table_check_pmd_clear);
170 void __page_table_check_pud_clear(struct mm_struct *mm, pud_t pud)
175 if (pud_user_accessible_page(pud)) {
176 page_table_check_clear(pud_pfn(pud), PUD_SIZE >> PAGE_SHIFT);
179 EXPORT_SYMBOL(__page_table_check_pud_clear);
181 /* Whether the swap entry cached writable information */
182 static inline bool swap_cached_writable(swp_entry_t entry)
184 return is_writable_device_private_entry(entry) ||
185 is_writable_migration_entry(entry);
188 static inline void page_table_check_pte_flags(pte_t pte)
190 if (pte_present(pte) && pte_uffd_wp(pte))
191 WARN_ON_ONCE(pte_write(pte));
192 else if (is_swap_pte(pte) && pte_swp_uffd_wp(pte))
193 WARN_ON_ONCE(swap_cached_writable(pte_to_swp_entry(pte)));
196 void __page_table_check_ptes_set(struct mm_struct *mm, pte_t *ptep, pte_t pte,
204 page_table_check_pte_flags(pte);
206 for (i = 0; i < nr; i++)
207 __page_table_check_pte_clear(mm, ptep_get(ptep + i));
208 if (pte_user_accessible_page(pte))
209 page_table_check_set(pte_pfn(pte), nr, pte_write(pte));
211 EXPORT_SYMBOL(__page_table_check_ptes_set);
213 static inline void page_table_check_pmd_flags(pmd_t pmd)
215 if (pmd_present(pmd) && pmd_uffd_wp(pmd))
216 WARN_ON_ONCE(pmd_write(pmd));
217 else if (is_swap_pmd(pmd) && pmd_swp_uffd_wp(pmd))
218 WARN_ON_ONCE(swap_cached_writable(pmd_to_swp_entry(pmd)));
221 void __page_table_check_pmds_set(struct mm_struct *mm, pmd_t *pmdp, pmd_t pmd,
224 unsigned long stride = PMD_SIZE >> PAGE_SHIFT;
230 page_table_check_pmd_flags(pmd);
232 for (i = 0; i < nr; i++)
233 __page_table_check_pmd_clear(mm, *(pmdp + i));
234 if (pmd_user_accessible_page(pmd))
235 page_table_check_set(pmd_pfn(pmd), stride * nr, pmd_write(pmd));
237 EXPORT_SYMBOL(__page_table_check_pmds_set);
239 void __page_table_check_puds_set(struct mm_struct *mm, pud_t *pudp, pud_t pud,
242 unsigned long stride = PUD_SIZE >> PAGE_SHIFT;
248 for (i = 0; i < nr; i++)
249 __page_table_check_pud_clear(mm, *(pudp + i));
250 if (pud_user_accessible_page(pud))
251 page_table_check_set(pud_pfn(pud), stride * nr, pud_write(pud));
253 EXPORT_SYMBOL(__page_table_check_puds_set);
255 void __page_table_check_pte_clear_range(struct mm_struct *mm,
262 if (!pmd_bad(pmd) && !pmd_leaf(pmd)) {
263 pte_t *ptep = pte_offset_map(&pmd, addr);
268 for (i = 0; i < PTRS_PER_PTE; i++) {
269 __page_table_check_pte_clear(mm, ptep_get(ptep));
273 pte_unmap(ptep - PTRS_PER_PTE);