Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 2 | #include <linux/mm.h> |
| 3 | #include <linux/mmzone.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 4 | #include <linux/memblock.h> |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 5 | #include <linux/page_ext.h> |
| 6 | #include <linux/memory.h> |
| 7 | #include <linux/vmalloc.h> |
| 8 | #include <linux/kmemleak.h> |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 9 | #include <linux/page_owner.h> |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 10 | #include <linux/page_idle.h> |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 11 | #include <linux/rcupdate.h> |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 12 | /* |
| 13 | * struct page extension |
| 14 | * |
| 15 | * This is the feature to manage memory for extended data per page. |
| 16 | * |
| 17 | * Until now, we must modify struct page itself to store extra data per page. |
| 18 | * This requires rebuilding the kernel and it is really time consuming process. |
| 19 | * And, sometimes, rebuild is impossible due to third party module dependency. |
| 20 | * At last, enlarging struct page could cause un-wanted system behaviour change. |
| 21 | * |
| 22 | * This feature is intended to overcome above mentioned problems. This feature |
| 23 | * allocates memory for extended data per page in certain place rather than |
| 24 | * the struct page itself. This memory can be accessed by the accessor |
| 25 | * functions provided by this code. During the boot process, it checks whether |
| 26 | * allocation of huge chunk of memory is needed or not. If not, it avoids |
| 27 | * allocating memory at all. With this advantage, we can include this feature |
| 28 | * into the kernel in default and can avoid rebuild and solve related problems. |
| 29 | * |
| 30 | * To help these things to work well, there are two callbacks for clients. One |
| 31 | * is the need callback which is mandatory if user wants to avoid useless |
| 32 | * memory allocation at boot-time. The other is optional, init callback, which |
| 33 | * is used to do proper initialization after memory is allocated. |
| 34 | * |
| 35 | * The need callback is used to decide whether extended memory allocation is |
| 36 | * needed or not. Sometimes users want to deactivate some features in this |
| 37 | * boot and extra memory would be unneccessary. In this case, to avoid |
| 38 | * allocating huge chunk of memory, each clients represent their need of |
| 39 | * extra memory through the need callback. If one of the need callbacks |
| 40 | * returns true, it means that someone needs extra memory so that |
| 41 | * page extension core should allocates memory for page extension. If |
| 42 | * none of need callbacks return true, memory isn't needed at all in this boot |
| 43 | * and page extension core can skip to allocate memory. As result, |
| 44 | * none of memory is wasted. |
| 45 | * |
Joonsoo Kim | 980ac16 | 2016-10-07 16:58:27 -0700 | [diff] [blame] | 46 | * When need callback returns true, page_ext checks if there is a request for |
| 47 | * extra memory through size in struct page_ext_operations. If it is non-zero, |
| 48 | * extra space is allocated for each page_ext entry and offset is returned to |
| 49 | * user through offset in struct page_ext_operations. |
| 50 | * |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 51 | * The init callback is used to do proper initialization after page extension |
| 52 | * is completely initialized. In sparse memory system, extra memory is |
| 53 | * allocated some time later than memmap is allocated. In other words, lifetime |
| 54 | * of memory for page extension isn't same with memmap for struct page. |
| 55 | * Therefore, clients can't store extra data until page extension is |
| 56 | * initialized, even if pages are allocated and used freely. This could |
| 57 | * cause inadequate state of extra data per page, so, to prevent it, client |
| 58 | * can utilize this callback to initialize the state of it correctly. |
| 59 | */ |
| 60 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 61 | #ifdef CONFIG_SPARSEMEM |
| 62 | #define PAGE_EXT_INVALID (0x1) |
| 63 | #endif |
| 64 | |
SeongJae Park | 75e13ba | 2021-09-07 19:56:40 -0700 | [diff] [blame] | 65 | #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT) |
| 66 | static bool need_page_idle(void) |
| 67 | { |
| 68 | return true; |
| 69 | } |
| 70 | struct page_ext_operations page_idle_ops = { |
| 71 | .need = need_page_idle, |
| 72 | }; |
| 73 | #endif |
| 74 | |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 75 | static struct page_ext_operations *page_ext_ops[] = { |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 76 | #ifdef CONFIG_PAGE_OWNER |
| 77 | &page_owner_ops, |
| 78 | #endif |
SeongJae Park | 75e13ba | 2021-09-07 19:56:40 -0700 | [diff] [blame] | 79 | #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT) |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 80 | &page_idle_ops, |
| 81 | #endif |
Minchan Kim | 6e12c5b | 2021-03-18 09:56:10 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_PAGE_PINNER |
| 83 | &page_pinner_ops, |
| 84 | #endif |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 87 | unsigned long page_ext_size = sizeof(struct page_ext); |
| 88 | |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 89 | static unsigned long total_usage; |
| 90 | |
| 91 | static bool __init invoke_need_callbacks(void) |
| 92 | { |
| 93 | int i; |
| 94 | int entries = ARRAY_SIZE(page_ext_ops); |
Joonsoo Kim | 980ac16 | 2016-10-07 16:58:27 -0700 | [diff] [blame] | 95 | bool need = false; |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 96 | |
| 97 | for (i = 0; i < entries; i++) { |
Joonsoo Kim | 980ac16 | 2016-10-07 16:58:27 -0700 | [diff] [blame] | 98 | if (page_ext_ops[i]->need && page_ext_ops[i]->need()) { |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 99 | page_ext_ops[i]->offset = page_ext_size; |
| 100 | page_ext_size += page_ext_ops[i]->size; |
Joonsoo Kim | 980ac16 | 2016-10-07 16:58:27 -0700 | [diff] [blame] | 101 | need = true; |
| 102 | } |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Joonsoo Kim | 980ac16 | 2016-10-07 16:58:27 -0700 | [diff] [blame] | 105 | return need; |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void __init invoke_init_callbacks(void) |
| 109 | { |
| 110 | int i; |
| 111 | int entries = ARRAY_SIZE(page_ext_ops); |
| 112 | |
| 113 | for (i = 0; i < entries; i++) { |
| 114 | if (page_ext_ops[i]->init) |
| 115 | page_ext_ops[i]->init(); |
| 116 | } |
| 117 | } |
| 118 | |
Zhenhua Huang | 1fe13c1 | 2020-12-14 19:04:46 -0800 | [diff] [blame] | 119 | #ifndef CONFIG_SPARSEMEM |
| 120 | void __init page_ext_init_flatmem_late(void) |
| 121 | { |
| 122 | invoke_init_callbacks(); |
| 123 | } |
| 124 | #endif |
| 125 | |
Joonsoo Kim | 980ac16 | 2016-10-07 16:58:27 -0700 | [diff] [blame] | 126 | static inline struct page_ext *get_entry(void *base, unsigned long index) |
| 127 | { |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 128 | return base + page_ext_size * index; |
Joonsoo Kim | 980ac16 | 2016-10-07 16:58:27 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 131 | /** |
| 132 | * page_ext_get() - Get the extended information for a page. |
| 133 | * @page: The page we're interested in. |
| 134 | * |
| 135 | * Ensures that the page_ext will remain valid until page_ext_put() |
| 136 | * is called. |
| 137 | * |
| 138 | * Return: NULL if no page_ext exists for this page. |
| 139 | * Context: Any context. Caller may not sleep until they have called |
| 140 | * page_ext_put(). |
| 141 | */ |
| 142 | struct page_ext *page_ext_get(struct page *page) |
| 143 | { |
| 144 | struct page_ext *page_ext; |
| 145 | |
| 146 | rcu_read_lock(); |
| 147 | page_ext = lookup_page_ext(page); |
| 148 | if (!page_ext) { |
| 149 | rcu_read_unlock(); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | return page_ext; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * page_ext_put() - Working with page extended information is done. |
Charan Teja Kalla | 9a59540 | 2022-11-08 10:46:22 +0530 | [diff] [blame] | 158 | * @page_ext: Page extended information received from page_ext_get(). |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 159 | * |
| 160 | * The page extended information of the page may not be valid after this |
| 161 | * function is called. |
| 162 | * |
| 163 | * Return: None. |
| 164 | * Context: Any context with corresponding page_ext_get() is called. |
| 165 | */ |
| 166 | void page_ext_put(struct page_ext *page_ext) |
| 167 | { |
| 168 | if (unlikely(!page_ext)) |
| 169 | return; |
| 170 | |
| 171 | rcu_read_unlock(); |
| 172 | } |
Zhenhua Huang | 1fe13c1 | 2020-12-14 19:04:46 -0800 | [diff] [blame] | 173 | #ifndef CONFIG_SPARSEMEM |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 174 | |
| 175 | |
| 176 | void __meminit pgdat_page_ext_init(struct pglist_data *pgdat) |
| 177 | { |
| 178 | pgdat->node_page_ext = NULL; |
| 179 | } |
| 180 | |
Kirill A. Shutemov | 10ed634 | 2018-08-17 15:45:15 -0700 | [diff] [blame] | 181 | struct page_ext *lookup_page_ext(const struct page *page) |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 182 | { |
| 183 | unsigned long pfn = page_to_pfn(page); |
Joonsoo Kim | 0b06bb3 | 2016-10-07 16:58:24 -0700 | [diff] [blame] | 184 | unsigned long index; |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 185 | struct page_ext *base; |
| 186 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 187 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 188 | base = NODE_DATA(page_to_nid(page))->node_page_ext; |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 189 | /* |
| 190 | * The sanity checks the page allocator does upon freeing a |
| 191 | * page can reach here before the page_ext arrays are |
| 192 | * allocated when feeding a range of pages to the allocator |
| 193 | * for the first time during bootup or memory hotplug. |
| 194 | */ |
| 195 | if (unlikely(!base)) |
| 196 | return NULL; |
Joonsoo Kim | 0b06bb3 | 2016-10-07 16:58:24 -0700 | [diff] [blame] | 197 | index = pfn - round_down(node_start_pfn(page_to_nid(page)), |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 198 | MAX_ORDER_NR_PAGES); |
Joonsoo Kim | 980ac16 | 2016-10-07 16:58:27 -0700 | [diff] [blame] | 199 | return get_entry(base, index); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 200 | } |
Vijayanand Jitta | 0a7166a | 2021-01-05 11:27:28 +0530 | [diff] [blame] | 201 | EXPORT_SYMBOL_GPL(lookup_page_ext); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 202 | |
| 203 | static int __init alloc_node_page_ext(int nid) |
| 204 | { |
| 205 | struct page_ext *base; |
| 206 | unsigned long table_size; |
| 207 | unsigned long nr_pages; |
| 208 | |
| 209 | nr_pages = NODE_DATA(nid)->node_spanned_pages; |
| 210 | if (!nr_pages) |
| 211 | return 0; |
| 212 | |
| 213 | /* |
| 214 | * Need extra space if node range is not aligned with |
| 215 | * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm |
| 216 | * checks buddy's status, range could be out of exact node range. |
| 217 | */ |
| 218 | if (!IS_ALIGNED(node_start_pfn(nid), MAX_ORDER_NR_PAGES) || |
| 219 | !IS_ALIGNED(node_end_pfn(nid), MAX_ORDER_NR_PAGES)) |
| 220 | nr_pages += MAX_ORDER_NR_PAGES; |
| 221 | |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 222 | table_size = page_ext_size * nr_pages; |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 223 | |
Mike Rapoport | 26fb3da | 2019-03-11 23:30:42 -0700 | [diff] [blame] | 224 | base = memblock_alloc_try_nid( |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 225 | table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS), |
Mike Rapoport | 97ad108 | 2018-10-30 15:09:44 -0700 | [diff] [blame] | 226 | MEMBLOCK_ALLOC_ACCESSIBLE, nid); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 227 | if (!base) |
| 228 | return -ENOMEM; |
| 229 | NODE_DATA(nid)->node_page_ext = base; |
| 230 | total_usage += table_size; |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | void __init page_ext_init_flatmem(void) |
| 235 | { |
| 236 | |
| 237 | int nid, fail; |
| 238 | |
| 239 | if (!invoke_need_callbacks()) |
| 240 | return; |
| 241 | |
| 242 | for_each_online_node(nid) { |
| 243 | fail = alloc_node_page_ext(nid); |
| 244 | if (fail) |
| 245 | goto fail; |
| 246 | } |
| 247 | pr_info("allocated %ld bytes of page_ext\n", total_usage); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 248 | return; |
| 249 | |
| 250 | fail: |
| 251 | pr_crit("allocation of page_ext failed.\n"); |
| 252 | panic("Out of memory"); |
| 253 | } |
| 254 | |
| 255 | #else /* CONFIG_FLAT_NODE_MEM_MAP */ |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 256 | static bool page_ext_invalid(struct page_ext *page_ext) |
| 257 | { |
| 258 | return !page_ext || (((unsigned long)page_ext & PAGE_EXT_INVALID) == PAGE_EXT_INVALID); |
| 259 | } |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 260 | |
Kirill A. Shutemov | 10ed634 | 2018-08-17 15:45:15 -0700 | [diff] [blame] | 261 | struct page_ext *lookup_page_ext(const struct page *page) |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 262 | { |
| 263 | unsigned long pfn = page_to_pfn(page); |
| 264 | struct mem_section *section = __pfn_to_section(pfn); |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 265 | struct page_ext *page_ext = READ_ONCE(section->page_ext); |
| 266 | |
| 267 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 268 | /* |
| 269 | * The sanity checks the page allocator does upon freeing a |
| 270 | * page can reach here before the page_ext arrays are |
| 271 | * allocated when feeding a range of pages to the allocator |
| 272 | * for the first time during bootup or memory hotplug. |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 273 | */ |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 274 | if (page_ext_invalid(page_ext)) |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 275 | return NULL; |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 276 | return get_entry(page_ext, pfn); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 277 | } |
Vijayanand Jitta | 0a7166a | 2021-01-05 11:27:28 +0530 | [diff] [blame] | 278 | EXPORT_SYMBOL_GPL(lookup_page_ext); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 279 | |
| 280 | static void *__meminit alloc_page_ext(size_t size, int nid) |
| 281 | { |
| 282 | gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN; |
| 283 | void *addr = NULL; |
| 284 | |
| 285 | addr = alloc_pages_exact_nid(nid, size, flags); |
| 286 | if (addr) { |
| 287 | kmemleak_alloc(addr, size, 1, flags); |
| 288 | return addr; |
| 289 | } |
| 290 | |
Michal Hocko | b95046b | 2017-09-06 16:20:41 -0700 | [diff] [blame] | 291 | addr = vzalloc_node(size, nid); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 292 | |
| 293 | return addr; |
| 294 | } |
| 295 | |
| 296 | static int __meminit init_section_page_ext(unsigned long pfn, int nid) |
| 297 | { |
| 298 | struct mem_section *section; |
| 299 | struct page_ext *base; |
| 300 | unsigned long table_size; |
| 301 | |
| 302 | section = __pfn_to_section(pfn); |
| 303 | |
| 304 | if (section->page_ext) |
| 305 | return 0; |
| 306 | |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 307 | table_size = page_ext_size * PAGES_PER_SECTION; |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 308 | base = alloc_page_ext(table_size, nid); |
| 309 | |
| 310 | /* |
| 311 | * The value stored in section->page_ext is (base - pfn) |
| 312 | * and it does not point to the memory block allocated above, |
| 313 | * causing kmemleak false positives. |
| 314 | */ |
| 315 | kmemleak_not_leak(base); |
| 316 | |
| 317 | if (!base) { |
| 318 | pr_err("page ext allocation failure\n"); |
| 319 | return -ENOMEM; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * The passed "pfn" may not be aligned to SECTION. For the calculation |
| 324 | * we need to apply a mask. |
| 325 | */ |
| 326 | pfn &= PAGE_SECTION_MASK; |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 327 | section->page_ext = (void *)base - page_ext_size * pfn; |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 328 | total_usage += table_size; |
| 329 | return 0; |
| 330 | } |
| 331 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 332 | static void free_page_ext(void *addr) |
| 333 | { |
| 334 | if (is_vmalloc_addr(addr)) { |
| 335 | vfree(addr); |
| 336 | } else { |
| 337 | struct page *page = virt_to_page(addr); |
| 338 | size_t table_size; |
| 339 | |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 340 | table_size = page_ext_size * PAGES_PER_SECTION; |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 341 | |
| 342 | BUG_ON(PageReserved(page)); |
Qian Cai | 0c81585 | 2019-03-05 15:49:46 -0800 | [diff] [blame] | 343 | kmemleak_free(addr); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 344 | free_pages_exact(addr, table_size); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static void __free_page_ext(unsigned long pfn) |
| 349 | { |
| 350 | struct mem_section *ms; |
| 351 | struct page_ext *base; |
| 352 | |
| 353 | ms = __pfn_to_section(pfn); |
| 354 | if (!ms || !ms->page_ext) |
| 355 | return; |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 356 | |
| 357 | base = READ_ONCE(ms->page_ext); |
| 358 | /* |
| 359 | * page_ext here can be valid while doing the roll back |
| 360 | * operation in online_page_ext(). |
| 361 | */ |
| 362 | if (page_ext_invalid(base)) |
| 363 | base = (void *)base - PAGE_EXT_INVALID; |
| 364 | WRITE_ONCE(ms->page_ext, NULL); |
| 365 | |
| 366 | base = get_entry(base, pfn); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 367 | free_page_ext(base); |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void __invalidate_page_ext(unsigned long pfn) |
| 371 | { |
| 372 | struct mem_section *ms; |
| 373 | void *val; |
| 374 | |
| 375 | ms = __pfn_to_section(pfn); |
| 376 | if (!ms || !ms->page_ext) |
| 377 | return; |
| 378 | val = (void *)ms->page_ext + PAGE_EXT_INVALID; |
| 379 | WRITE_ONCE(ms->page_ext, val); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int __meminit online_page_ext(unsigned long start_pfn, |
| 383 | unsigned long nr_pages, |
| 384 | int nid) |
| 385 | { |
| 386 | unsigned long start, end, pfn; |
| 387 | int fail = 0; |
| 388 | |
| 389 | start = SECTION_ALIGN_DOWN(start_pfn); |
| 390 | end = SECTION_ALIGN_UP(start_pfn + nr_pages); |
| 391 | |
Anshuman Khandual | 98fa15f | 2019-03-05 15:42:58 -0800 | [diff] [blame] | 392 | if (nid == NUMA_NO_NODE) { |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 393 | /* |
| 394 | * In this case, "nid" already exists and contains valid memory. |
| 395 | * "start_pfn" passed to us is a pfn which is an arg for |
| 396 | * online__pages(), and start_pfn should exist. |
| 397 | */ |
| 398 | nid = pfn_to_nid(start_pfn); |
| 399 | VM_BUG_ON(!node_state(nid, N_ONLINE)); |
| 400 | } |
| 401 | |
David Hildenbrand | dccacf8 | 2020-04-06 20:06:47 -0700 | [diff] [blame] | 402 | for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 403 | fail = init_section_page_ext(pfn, nid); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 404 | if (!fail) |
| 405 | return 0; |
| 406 | |
| 407 | /* rollback */ |
| 408 | for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) |
| 409 | __free_page_ext(pfn); |
| 410 | |
| 411 | return -ENOMEM; |
| 412 | } |
| 413 | |
| 414 | static int __meminit offline_page_ext(unsigned long start_pfn, |
| 415 | unsigned long nr_pages, int nid) |
| 416 | { |
| 417 | unsigned long start, end, pfn; |
| 418 | |
| 419 | start = SECTION_ALIGN_DOWN(start_pfn); |
| 420 | end = SECTION_ALIGN_UP(start_pfn + nr_pages); |
| 421 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 422 | /* |
| 423 | * Freeing of page_ext is done in 3 steps to avoid |
| 424 | * use-after-free of it: |
| 425 | * 1) Traverse all the sections and mark their page_ext |
| 426 | * as invalid. |
| 427 | * 2) Wait for all the existing users of page_ext who |
| 428 | * started before invalidation to finish. |
| 429 | * 3) Free the page_ext. |
| 430 | */ |
| 431 | for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) |
| 432 | __invalidate_page_ext(pfn); |
| 433 | |
| 434 | synchronize_rcu(); |
| 435 | |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 436 | for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) |
| 437 | __free_page_ext(pfn); |
| 438 | return 0; |
| 439 | |
| 440 | } |
| 441 | |
| 442 | static int __meminit page_ext_callback(struct notifier_block *self, |
| 443 | unsigned long action, void *arg) |
| 444 | { |
| 445 | struct memory_notify *mn = arg; |
| 446 | int ret = 0; |
| 447 | |
| 448 | switch (action) { |
| 449 | case MEM_GOING_ONLINE: |
| 450 | ret = online_page_ext(mn->start_pfn, |
| 451 | mn->nr_pages, mn->status_change_nid); |
| 452 | break; |
| 453 | case MEM_OFFLINE: |
| 454 | offline_page_ext(mn->start_pfn, |
| 455 | mn->nr_pages, mn->status_change_nid); |
| 456 | break; |
| 457 | case MEM_CANCEL_ONLINE: |
| 458 | offline_page_ext(mn->start_pfn, |
| 459 | mn->nr_pages, mn->status_change_nid); |
| 460 | break; |
| 461 | case MEM_GOING_OFFLINE: |
| 462 | break; |
| 463 | case MEM_ONLINE: |
| 464 | case MEM_CANCEL_OFFLINE: |
| 465 | break; |
| 466 | } |
| 467 | |
| 468 | return notifier_from_errno(ret); |
| 469 | } |
| 470 | |
| 471 | #endif |
| 472 | |
| 473 | void __init page_ext_init(void) |
| 474 | { |
| 475 | unsigned long pfn; |
| 476 | int nid; |
| 477 | |
| 478 | if (!invoke_need_callbacks()) |
| 479 | return; |
| 480 | |
| 481 | for_each_node_state(nid, N_MEMORY) { |
| 482 | unsigned long start_pfn, end_pfn; |
| 483 | |
| 484 | start_pfn = node_start_pfn(nid); |
| 485 | end_pfn = node_end_pfn(nid); |
| 486 | /* |
| 487 | * start_pfn and end_pfn may not be aligned to SECTION and the |
| 488 | * page->flags of out of node pages are not initialized. So we |
| 489 | * scan [start_pfn, the biggest section's pfn < end_pfn) here. |
| 490 | */ |
| 491 | for (pfn = start_pfn; pfn < end_pfn; |
| 492 | pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) { |
| 493 | |
| 494 | if (!pfn_valid(pfn)) |
| 495 | continue; |
| 496 | /* |
| 497 | * Nodes's pfns can be overlapping. |
| 498 | * We know some arch can have a nodes layout such as |
| 499 | * -------------pfn--------------> |
| 500 | * N0 | N1 | N2 | N0 | N1 | N2|.... |
| 501 | */ |
Qian Cai | 2f1ee09 | 2019-02-12 15:36:03 -0800 | [diff] [blame] | 502 | if (pfn_to_nid(pfn) != nid) |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 503 | continue; |
| 504 | if (init_section_page_ext(pfn, nid)) |
| 505 | goto oom; |
Vlastimil Babka | 0fc542b | 2017-09-06 16:20:48 -0700 | [diff] [blame] | 506 | cond_resched(); |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | hotplug_memory_notifier(page_ext_callback, 0); |
| 510 | pr_info("allocated %ld bytes of page_ext\n", total_usage); |
| 511 | invoke_init_callbacks(); |
| 512 | return; |
| 513 | |
| 514 | oom: |
| 515 | panic("Out of memory"); |
| 516 | } |
| 517 | |
| 518 | void __meminit pgdat_page_ext_init(struct pglist_data *pgdat) |
| 519 | { |
| 520 | } |
| 521 | |
| 522 | #endif |