Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2021, Google LLC. |
| 5 | * Pasha Tatashin <pasha.tatashin@soleen.com> |
| 6 | */ |
Christophe JAILLET | f15be1b | 2022-11-01 22:14:09 +0100 | [diff] [blame] | 7 | #include <linux/kstrtox.h> |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 8 | #include <linux/mm.h> |
| 9 | #include <linux/page_table_check.h> |
Peter Xu | 8430557 | 2024-04-17 17:25:49 -0400 | [diff] [blame] | 10 | #include <linux/swap.h> |
| 11 | #include <linux/swapops.h> |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 12 | |
| 13 | #undef pr_fmt |
| 14 | #define pr_fmt(fmt) "page_table_check: " fmt |
| 15 | |
| 16 | struct page_table_check { |
| 17 | atomic_t anon_map_count; |
| 18 | atomic_t file_map_count; |
| 19 | }; |
| 20 | |
| 21 | static bool __page_table_check_enabled __initdata = |
| 22 | IS_ENABLED(CONFIG_PAGE_TABLE_CHECK_ENFORCED); |
| 23 | |
| 24 | DEFINE_STATIC_KEY_TRUE(page_table_check_disabled); |
| 25 | EXPORT_SYMBOL(page_table_check_disabled); |
| 26 | |
| 27 | static int __init early_page_table_check_param(char *buf) |
| 28 | { |
Christophe JAILLET | f15be1b | 2022-11-01 22:14:09 +0100 | [diff] [blame] | 29 | return kstrtobool(buf, &__page_table_check_enabled); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | early_param("page_table_check", early_page_table_check_param); |
| 33 | |
| 34 | static bool __init need_page_table_check(void) |
| 35 | { |
| 36 | return __page_table_check_enabled; |
| 37 | } |
| 38 | |
| 39 | static void __init init_page_table_check(void) |
| 40 | { |
| 41 | if (!__page_table_check_enabled) |
| 42 | return; |
| 43 | static_branch_disable(&page_table_check_disabled); |
| 44 | } |
| 45 | |
| 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, |
Pasha Tatashin | 6189eb8 | 2023-01-13 15:42:53 +0000 | [diff] [blame] | 50 | .need_shared_flags = false, |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static struct page_table_check *get_page_table_check(struct page_ext *page_ext) |
| 54 | { |
| 55 | BUG_ON(!page_ext); |
Kemeng Shi | d981e28 | 2023-07-18 22:58:11 +0800 | [diff] [blame] | 56 | return page_ext_data(page_ext, &page_table_check_ops); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 59 | /* |
Chih-En Lin | 3ae6d3e | 2022-09-16 17:04:34 +0800 | [diff] [blame] | 60 | * An entry is removed from the page table, decrement the counters for that page |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 61 | * verify that it is of correct type and counters do not become negative. |
| 62 | */ |
Kemeng Shi | 34c876c | 2023-07-14 01:26:29 +0800 | [diff] [blame] | 63 | static void page_table_check_clear(unsigned long pfn, unsigned long pgcnt) |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 64 | { |
| 65 | struct page_ext *page_ext; |
| 66 | struct page *page; |
Pasha Tatashin | 64d8b9e1 | 2022-02-03 20:49:15 -0800 | [diff] [blame] | 67 | unsigned long i; |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 68 | bool anon; |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 69 | |
| 70 | if (!pfn_valid(pfn)) |
| 71 | return; |
| 72 | |
| 73 | page = pfn_to_page(pfn); |
Charan Teja Kalla | b1d5488 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 74 | page_ext = page_ext_get(page); |
Ruihan Li | 44d0fb3 | 2023-05-15 21:09:58 +0800 | [diff] [blame] | 75 | |
Peter Xu | 8bb592c | 2024-06-05 17:21:46 -0400 | [diff] [blame] | 76 | if (!page_ext) |
| 77 | return; |
| 78 | |
Ruihan Li | 44d0fb3 | 2023-05-15 21:09:58 +0800 | [diff] [blame] | 79 | BUG_ON(PageSlab(page)); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 80 | anon = PageAnon(page); |
| 81 | |
| 82 | for (i = 0; i < pgcnt; i++) { |
| 83 | struct page_table_check *ptc = get_page_table_check(page_ext); |
| 84 | |
| 85 | if (anon) { |
| 86 | BUG_ON(atomic_read(&ptc->file_map_count)); |
| 87 | BUG_ON(atomic_dec_return(&ptc->anon_map_count) < 0); |
| 88 | } else { |
| 89 | BUG_ON(atomic_read(&ptc->anon_map_count)); |
| 90 | BUG_ON(atomic_dec_return(&ptc->file_map_count) < 0); |
| 91 | } |
| 92 | page_ext = page_ext_next(page_ext); |
| 93 | } |
Charan Teja Kalla | b1d5488 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 94 | page_ext_put(page_ext); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
Chih-En Lin | 3ae6d3e | 2022-09-16 17:04:34 +0800 | [diff] [blame] | 98 | * A new entry is added to the page table, increment the counters for that page |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 99 | * verify that it is of correct type and is not being mapped with a different |
| 100 | * type to a different process. |
| 101 | */ |
Kemeng Shi | 2f933ea | 2023-07-14 01:26:30 +0800 | [diff] [blame] | 102 | static void page_table_check_set(unsigned long pfn, unsigned long pgcnt, |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 103 | bool rw) |
| 104 | { |
| 105 | struct page_ext *page_ext; |
| 106 | struct page *page; |
Pasha Tatashin | 64d8b9e1 | 2022-02-03 20:49:15 -0800 | [diff] [blame] | 107 | unsigned long i; |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 108 | bool anon; |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 109 | |
| 110 | if (!pfn_valid(pfn)) |
| 111 | return; |
| 112 | |
| 113 | page = pfn_to_page(pfn); |
Charan Teja Kalla | b1d5488 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 114 | page_ext = page_ext_get(page); |
Ruihan Li | 44d0fb3 | 2023-05-15 21:09:58 +0800 | [diff] [blame] | 115 | |
Peter Xu | 8bb592c | 2024-06-05 17:21:46 -0400 | [diff] [blame] | 116 | if (!page_ext) |
| 117 | return; |
| 118 | |
Ruihan Li | 44d0fb3 | 2023-05-15 21:09:58 +0800 | [diff] [blame] | 119 | BUG_ON(PageSlab(page)); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 120 | anon = PageAnon(page); |
| 121 | |
| 122 | for (i = 0; i < pgcnt; i++) { |
| 123 | struct page_table_check *ptc = get_page_table_check(page_ext); |
| 124 | |
| 125 | if (anon) { |
| 126 | BUG_ON(atomic_read(&ptc->file_map_count)); |
| 127 | BUG_ON(atomic_inc_return(&ptc->anon_map_count) > 1 && rw); |
| 128 | } else { |
| 129 | BUG_ON(atomic_read(&ptc->anon_map_count)); |
| 130 | BUG_ON(atomic_inc_return(&ptc->file_map_count) < 0); |
| 131 | } |
| 132 | page_ext = page_ext_next(page_ext); |
| 133 | } |
Charan Teja Kalla | b1d5488 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 134 | page_ext_put(page_ext); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* |
| 138 | * page is on free list, or is being allocated, verify that counters are zeroes |
| 139 | * crash if they are not. |
| 140 | */ |
| 141 | void __page_table_check_zero(struct page *page, unsigned int order) |
| 142 | { |
Charan Teja Kalla | b1d5488 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 143 | struct page_ext *page_ext; |
Pasha Tatashin | 64d8b9e1 | 2022-02-03 20:49:15 -0800 | [diff] [blame] | 144 | unsigned long i; |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 145 | |
Ruihan Li | 44d0fb3 | 2023-05-15 21:09:58 +0800 | [diff] [blame] | 146 | BUG_ON(PageSlab(page)); |
| 147 | |
Charan Teja Kalla | b1d5488 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 148 | page_ext = page_ext_get(page); |
Peter Xu | 8bb592c | 2024-06-05 17:21:46 -0400 | [diff] [blame] | 149 | |
| 150 | if (!page_ext) |
| 151 | return; |
| 152 | |
Pasha Tatashin | 64d8b9e1 | 2022-02-03 20:49:15 -0800 | [diff] [blame] | 153 | for (i = 0; i < (1ul << order); i++) { |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 154 | struct page_table_check *ptc = get_page_table_check(page_ext); |
| 155 | |
| 156 | BUG_ON(atomic_read(&ptc->anon_map_count)); |
| 157 | BUG_ON(atomic_read(&ptc->file_map_count)); |
| 158 | page_ext = page_ext_next(page_ext); |
| 159 | } |
Charan Teja Kalla | b1d5488 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 160 | page_ext_put(page_ext); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Kemeng Shi | aa23220 | 2023-07-14 01:26:31 +0800 | [diff] [blame] | 163 | void __page_table_check_pte_clear(struct mm_struct *mm, pte_t pte) |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 164 | { |
| 165 | if (&init_mm == mm) |
| 166 | return; |
| 167 | |
| 168 | if (pte_user_accessible_page(pte)) { |
Kemeng Shi | 34c876c | 2023-07-14 01:26:29 +0800 | [diff] [blame] | 169 | page_table_check_clear(pte_pfn(pte), PAGE_SIZE >> PAGE_SHIFT); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | EXPORT_SYMBOL(__page_table_check_pte_clear); |
| 173 | |
Kemeng Shi | 1831414 | 2023-07-14 01:26:32 +0800 | [diff] [blame] | 174 | void __page_table_check_pmd_clear(struct mm_struct *mm, pmd_t pmd) |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 175 | { |
| 176 | if (&init_mm == mm) |
| 177 | return; |
| 178 | |
| 179 | if (pmd_user_accessible_page(pmd)) { |
Kemeng Shi | 34c876c | 2023-07-14 01:26:29 +0800 | [diff] [blame] | 180 | page_table_check_clear(pmd_pfn(pmd), PMD_SIZE >> PAGE_SHIFT); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | EXPORT_SYMBOL(__page_table_check_pmd_clear); |
| 184 | |
Kemeng Shi | 931c38e | 2023-07-14 01:26:33 +0800 | [diff] [blame] | 185 | void __page_table_check_pud_clear(struct mm_struct *mm, pud_t pud) |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 186 | { |
| 187 | if (&init_mm == mm) |
| 188 | return; |
| 189 | |
| 190 | if (pud_user_accessible_page(pud)) { |
Kemeng Shi | 34c876c | 2023-07-14 01:26:29 +0800 | [diff] [blame] | 191 | page_table_check_clear(pud_pfn(pud), PUD_SIZE >> PAGE_SHIFT); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | EXPORT_SYMBOL(__page_table_check_pud_clear); |
| 195 | |
Peter Xu | 8430557 | 2024-04-17 17:25:49 -0400 | [diff] [blame] | 196 | /* Whether the swap entry cached writable information */ |
| 197 | static inline bool swap_cached_writable(swp_entry_t entry) |
| 198 | { |
| 199 | return is_writable_device_exclusive_entry(entry) || |
| 200 | is_writable_device_private_entry(entry) || |
| 201 | is_writable_migration_entry(entry); |
| 202 | } |
| 203 | |
| 204 | static inline void page_table_check_pte_flags(pte_t pte) |
| 205 | { |
| 206 | if (pte_present(pte) && pte_uffd_wp(pte)) |
| 207 | WARN_ON_ONCE(pte_write(pte)); |
| 208 | else if (is_swap_pte(pte) && pte_swp_uffd_wp(pte)) |
| 209 | WARN_ON_ONCE(swap_cached_writable(pte_to_swp_entry(pte))); |
| 210 | } |
| 211 | |
Matthew Wilcox (Oracle) | a379322 | 2023-08-02 16:13:30 +0100 | [diff] [blame] | 212 | void __page_table_check_ptes_set(struct mm_struct *mm, pte_t *ptep, pte_t pte, |
| 213 | unsigned int nr) |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 214 | { |
Matthew Wilcox (Oracle) | a379322 | 2023-08-02 16:13:30 +0100 | [diff] [blame] | 215 | unsigned int i; |
| 216 | |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 217 | if (&init_mm == mm) |
| 218 | return; |
| 219 | |
Peter Xu | 8430557 | 2024-04-17 17:25:49 -0400 | [diff] [blame] | 220 | page_table_check_pte_flags(pte); |
| 221 | |
Matthew Wilcox (Oracle) | a379322 | 2023-08-02 16:13:30 +0100 | [diff] [blame] | 222 | for (i = 0; i < nr; i++) |
| 223 | __page_table_check_pte_clear(mm, ptep_get(ptep + i)); |
| 224 | if (pte_user_accessible_page(pte)) |
| 225 | page_table_check_set(pte_pfn(pte), nr, pte_write(pte)); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 226 | } |
Matthew Wilcox (Oracle) | a379322 | 2023-08-02 16:13:30 +0100 | [diff] [blame] | 227 | EXPORT_SYMBOL(__page_table_check_ptes_set); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 228 | |
Peter Xu | 8430557 | 2024-04-17 17:25:49 -0400 | [diff] [blame] | 229 | static inline void page_table_check_pmd_flags(pmd_t pmd) |
| 230 | { |
| 231 | if (pmd_present(pmd) && pmd_uffd_wp(pmd)) |
| 232 | WARN_ON_ONCE(pmd_write(pmd)); |
| 233 | else if (is_swap_pmd(pmd) && pmd_swp_uffd_wp(pmd)) |
| 234 | WARN_ON_ONCE(swap_cached_writable(pmd_to_swp_entry(pmd))); |
| 235 | } |
| 236 | |
Kemeng Shi | a3b8371 | 2023-07-14 01:26:35 +0800 | [diff] [blame] | 237 | void __page_table_check_pmd_set(struct mm_struct *mm, pmd_t *pmdp, pmd_t pmd) |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 238 | { |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 239 | if (&init_mm == mm) |
| 240 | return; |
| 241 | |
Peter Xu | 8430557 | 2024-04-17 17:25:49 -0400 | [diff] [blame] | 242 | page_table_check_pmd_flags(pmd); |
| 243 | |
Kemeng Shi | 1831414 | 2023-07-14 01:26:32 +0800 | [diff] [blame] | 244 | __page_table_check_pmd_clear(mm, *pmdp); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 245 | if (pmd_user_accessible_page(pmd)) { |
Kemeng Shi | 2f933ea | 2023-07-14 01:26:30 +0800 | [diff] [blame] | 246 | page_table_check_set(pmd_pfn(pmd), PMD_SIZE >> PAGE_SHIFT, |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 247 | pmd_write(pmd)); |
| 248 | } |
| 249 | } |
| 250 | EXPORT_SYMBOL(__page_table_check_pmd_set); |
| 251 | |
Kemeng Shi | 6d144436 | 2023-07-14 01:26:36 +0800 | [diff] [blame] | 252 | void __page_table_check_pud_set(struct mm_struct *mm, pud_t *pudp, pud_t pud) |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 253 | { |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 254 | if (&init_mm == mm) |
| 255 | return; |
| 256 | |
Kemeng Shi | 931c38e | 2023-07-14 01:26:33 +0800 | [diff] [blame] | 257 | __page_table_check_pud_clear(mm, *pudp); |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 258 | if (pud_user_accessible_page(pud)) { |
Kemeng Shi | 2f933ea | 2023-07-14 01:26:30 +0800 | [diff] [blame] | 259 | page_table_check_set(pud_pfn(pud), PUD_SIZE >> PAGE_SHIFT, |
Pasha Tatashin | df4e817 | 2022-01-14 14:06:37 -0800 | [diff] [blame] | 260 | pud_write(pud)); |
| 261 | } |
| 262 | } |
| 263 | EXPORT_SYMBOL(__page_table_check_pud_set); |
Pasha Tatashin | 80110bb | 2022-02-03 20:49:24 -0800 | [diff] [blame] | 264 | |
| 265 | void __page_table_check_pte_clear_range(struct mm_struct *mm, |
| 266 | unsigned long addr, |
| 267 | pmd_t pmd) |
| 268 | { |
| 269 | if (&init_mm == mm) |
| 270 | return; |
| 271 | |
| 272 | if (!pmd_bad(pmd) && !pmd_leaf(pmd)) { |
| 273 | pte_t *ptep = pte_offset_map(&pmd, addr); |
| 274 | unsigned long i; |
| 275 | |
Hugh Dickins | 9f2bad0 | 2023-06-08 18:27:52 -0700 | [diff] [blame] | 276 | if (WARN_ON(!ptep)) |
| 277 | return; |
Pasha Tatashin | 80110bb | 2022-02-03 20:49:24 -0800 | [diff] [blame] | 278 | for (i = 0; i < PTRS_PER_PTE; i++) { |
Kemeng Shi | aa23220 | 2023-07-14 01:26:31 +0800 | [diff] [blame] | 279 | __page_table_check_pte_clear(mm, ptep_get(ptep)); |
Pasha Tatashin | 80110bb | 2022-02-03 20:49:24 -0800 | [diff] [blame] | 280 | addr += PAGE_SIZE; |
| 281 | ptep++; |
| 282 | } |
Miaohe Lin | 24c8e27 | 2022-05-26 19:33:50 +0800 | [diff] [blame] | 283 | pte_unmap(ptep - PTRS_PER_PTE); |
Pasha Tatashin | 80110bb | 2022-02-03 20:49:24 -0800 | [diff] [blame] | 284 | } |
| 285 | } |