Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 2 | #include <linux/debugfs.h> |
| 3 | #include <linux/mm.h> |
| 4 | #include <linux/slab.h> |
| 5 | #include <linux/uaccess.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 6 | #include <linux/memblock.h> |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 7 | #include <linux/stacktrace.h> |
| 8 | #include <linux/page_owner.h> |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 9 | #include <linux/jump_label.h> |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 10 | #include <linux/migrate.h> |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 11 | #include <linux/stackdepot.h> |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 12 | #include <linux/seq_file.h> |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 13 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 14 | #include "internal.h" |
| 15 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 16 | /* |
| 17 | * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack) |
| 18 | * to use off stack temporal storage |
| 19 | */ |
| 20 | #define PAGE_OWNER_STACK_DEPTH (16) |
| 21 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 22 | struct page_owner { |
Ayush Mittal | 6b4c54e | 2017-11-15 17:34:30 -0800 | [diff] [blame] | 23 | unsigned short order; |
| 24 | short last_migrate_reason; |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 25 | gfp_t gfp_mask; |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 26 | depot_stack_handle_t handle; |
| 27 | }; |
| 28 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 29 | static bool page_owner_disabled = true; |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 30 | DEFINE_STATIC_KEY_FALSE(page_owner_inited); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 31 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 32 | static depot_stack_handle_t dummy_handle; |
| 33 | static depot_stack_handle_t failure_handle; |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 34 | static depot_stack_handle_t early_handle; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 35 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 36 | static void init_early_allocated_pages(void); |
| 37 | |
Dou Liyang | 1173194 | 2018-04-05 16:23:49 -0700 | [diff] [blame] | 38 | static int __init early_page_owner_param(char *buf) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 39 | { |
| 40 | if (!buf) |
| 41 | return -EINVAL; |
| 42 | |
| 43 | if (strcmp(buf, "on") == 0) |
| 44 | page_owner_disabled = false; |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | early_param("page_owner", early_page_owner_param); |
| 49 | |
| 50 | static bool need_page_owner(void) |
| 51 | { |
| 52 | if (page_owner_disabled) |
| 53 | return false; |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 58 | static __always_inline depot_stack_handle_t create_dummy_stack(void) |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 59 | { |
| 60 | unsigned long entries[4]; |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 61 | unsigned int nr_entries; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 62 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 63 | nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); |
| 64 | return stack_depot_save(entries, nr_entries, GFP_KERNEL); |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static noinline void register_dummy_stack(void) |
| 68 | { |
| 69 | dummy_handle = create_dummy_stack(); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static noinline void register_failure_stack(void) |
| 73 | { |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 74 | failure_handle = create_dummy_stack(); |
| 75 | } |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 76 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 77 | static noinline void register_early_stack(void) |
| 78 | { |
| 79 | early_handle = create_dummy_stack(); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 82 | static void init_page_owner(void) |
| 83 | { |
| 84 | if (page_owner_disabled) |
| 85 | return; |
| 86 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 87 | register_dummy_stack(); |
| 88 | register_failure_stack(); |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 89 | register_early_stack(); |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 90 | static_branch_enable(&page_owner_inited); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 91 | init_early_allocated_pages(); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | struct page_ext_operations page_owner_ops = { |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 95 | .size = sizeof(struct page_owner), |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 96 | .need = need_page_owner, |
| 97 | .init = init_page_owner, |
| 98 | }; |
| 99 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 100 | static inline struct page_owner *get_page_owner(struct page_ext *page_ext) |
| 101 | { |
| 102 | return (void *)page_ext + page_owner_ops.offset; |
| 103 | } |
| 104 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 105 | void __reset_page_owner(struct page *page, unsigned int order) |
| 106 | { |
| 107 | int i; |
| 108 | struct page_ext *page_ext; |
| 109 | |
| 110 | for (i = 0; i < (1 << order); i++) { |
| 111 | page_ext = lookup_page_ext(page + i); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 112 | if (unlikely(!page_ext)) |
| 113 | continue; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 114 | __clear_bit(PAGE_EXT_OWNER, &page_ext->flags); |
| 115 | } |
| 116 | } |
| 117 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 118 | static inline bool check_recursive_alloc(unsigned long *entries, |
| 119 | unsigned int nr_entries, |
| 120 | unsigned long ip) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 121 | { |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 122 | unsigned int i; |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 123 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 124 | for (i = 0; i < nr_entries; i++) { |
| 125 | if (entries[i] == ip) |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 126 | return true; |
| 127 | } |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
| 131 | static noinline depot_stack_handle_t save_stack(gfp_t flags) |
| 132 | { |
| 133 | unsigned long entries[PAGE_OWNER_STACK_DEPTH]; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 134 | depot_stack_handle_t handle; |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 135 | unsigned int nr_entries; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 136 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 137 | nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 138 | |
| 139 | /* |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 140 | * We need to check recursion here because our request to |
| 141 | * stackdepot could trigger memory allocation to save new |
| 142 | * entry. New memory allocation would reach here and call |
| 143 | * stack_depot_save_entries() again if we don't catch it. There is |
| 144 | * still not enough memory in stackdepot so it would try to |
| 145 | * allocate memory again and loop forever. |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 146 | */ |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 147 | if (check_recursive_alloc(entries, nr_entries, _RET_IP_)) |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 148 | return dummy_handle; |
| 149 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 150 | handle = stack_depot_save(entries, nr_entries, flags); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 151 | if (!handle) |
| 152 | handle = failure_handle; |
| 153 | |
| 154 | return handle; |
| 155 | } |
| 156 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 157 | static inline void __set_page_owner_handle(struct page_ext *page_ext, |
| 158 | depot_stack_handle_t handle, unsigned int order, gfp_t gfp_mask) |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 159 | { |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 160 | struct page_owner *page_owner; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 161 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 162 | page_owner = get_page_owner(page_ext); |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 163 | page_owner->handle = handle; |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 164 | page_owner->order = order; |
| 165 | page_owner->gfp_mask = gfp_mask; |
| 166 | page_owner->last_migrate_reason = -1; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 167 | |
| 168 | __set_bit(PAGE_EXT_OWNER, &page_ext->flags); |
| 169 | } |
| 170 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 171 | noinline void __set_page_owner(struct page *page, unsigned int order, |
| 172 | gfp_t gfp_mask) |
| 173 | { |
| 174 | struct page_ext *page_ext = lookup_page_ext(page); |
| 175 | depot_stack_handle_t handle; |
| 176 | |
| 177 | if (unlikely(!page_ext)) |
| 178 | return; |
| 179 | |
| 180 | handle = save_stack(gfp_mask); |
| 181 | __set_page_owner_handle(page_ext, handle, order, gfp_mask); |
| 182 | } |
| 183 | |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 184 | void __set_page_owner_migrate_reason(struct page *page, int reason) |
| 185 | { |
| 186 | struct page_ext *page_ext = lookup_page_ext(page); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 187 | struct page_owner *page_owner; |
| 188 | |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 189 | if (unlikely(!page_ext)) |
| 190 | return; |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 191 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 192 | page_owner = get_page_owner(page_ext); |
| 193 | page_owner->last_migrate_reason = reason; |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Joonsoo Kim | a9627bc | 2016-07-26 15:23:49 -0700 | [diff] [blame] | 196 | void __split_page_owner(struct page *page, unsigned int order) |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 197 | { |
Joonsoo Kim | a9627bc | 2016-07-26 15:23:49 -0700 | [diff] [blame] | 198 | int i; |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 199 | struct page_ext *page_ext = lookup_page_ext(page); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 200 | struct page_owner *page_owner; |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 201 | |
Joonsoo Kim | a9627bc | 2016-07-26 15:23:49 -0700 | [diff] [blame] | 202 | if (unlikely(!page_ext)) |
| 203 | return; |
| 204 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 205 | page_owner = get_page_owner(page_ext); |
| 206 | page_owner->order = 0; |
Joonsoo Kim | a9627bc | 2016-07-26 15:23:49 -0700 | [diff] [blame] | 207 | for (i = 1; i < (1 << order); i++) |
| 208 | __copy_page_owner(page, page + i); |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 211 | void __copy_page_owner(struct page *oldpage, struct page *newpage) |
| 212 | { |
| 213 | struct page_ext *old_ext = lookup_page_ext(oldpage); |
| 214 | struct page_ext *new_ext = lookup_page_ext(newpage); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 215 | struct page_owner *old_page_owner, *new_page_owner; |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 216 | |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 217 | if (unlikely(!old_ext || !new_ext)) |
| 218 | return; |
| 219 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 220 | old_page_owner = get_page_owner(old_ext); |
| 221 | new_page_owner = get_page_owner(new_ext); |
| 222 | new_page_owner->order = old_page_owner->order; |
| 223 | new_page_owner->gfp_mask = old_page_owner->gfp_mask; |
| 224 | new_page_owner->last_migrate_reason = |
| 225 | old_page_owner->last_migrate_reason; |
| 226 | new_page_owner->handle = old_page_owner->handle; |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * We don't clear the bit on the oldpage as it's going to be freed |
| 230 | * after migration. Until then, the info can be useful in case of |
| 231 | * a bug, and the overal stats will be off a bit only temporarily. |
| 232 | * Also, migrate_misplaced_transhuge_page() can still fail the |
| 233 | * migration and then we want the oldpage to retain the info. But |
| 234 | * in that case we also don't need to explicitly clear the info from |
| 235 | * the new page, which will be freed. |
| 236 | */ |
| 237 | __set_bit(PAGE_EXT_OWNER, &new_ext->flags); |
| 238 | } |
| 239 | |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 240 | void pagetypeinfo_showmixedcount_print(struct seq_file *m, |
| 241 | pg_data_t *pgdat, struct zone *zone) |
| 242 | { |
| 243 | struct page *page; |
| 244 | struct page_ext *page_ext; |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 245 | struct page_owner *page_owner; |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 246 | unsigned long pfn = zone->zone_start_pfn, block_end_pfn; |
| 247 | unsigned long end_pfn = pfn + zone->spanned_pages; |
| 248 | unsigned long count[MIGRATE_TYPES] = { 0, }; |
| 249 | int pageblock_mt, page_mt; |
| 250 | int i; |
| 251 | |
| 252 | /* Scan block by block. First and last block may be incomplete */ |
| 253 | pfn = zone->zone_start_pfn; |
| 254 | |
| 255 | /* |
| 256 | * Walk the zone in pageblock_nr_pages steps. If a page block spans |
| 257 | * a zone boundary, it will be double counted between zones. This does |
| 258 | * not matter as the mixed block count will still be correct |
| 259 | */ |
| 260 | for (; pfn < end_pfn; ) { |
| 261 | if (!pfn_valid(pfn)) { |
| 262 | pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES); |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); |
| 267 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 268 | |
| 269 | page = pfn_to_page(pfn); |
| 270 | pageblock_mt = get_pageblock_migratetype(page); |
| 271 | |
| 272 | for (; pfn < block_end_pfn; pfn++) { |
| 273 | if (!pfn_valid_within(pfn)) |
| 274 | continue; |
| 275 | |
| 276 | page = pfn_to_page(pfn); |
| 277 | |
| 278 | if (page_zone(page) != zone) |
| 279 | continue; |
| 280 | |
| 281 | if (PageBuddy(page)) { |
Vinayak Menon | 727c080 | 2017-07-10 15:49:17 -0700 | [diff] [blame] | 282 | unsigned long freepage_order; |
| 283 | |
| 284 | freepage_order = page_order_unsafe(page); |
| 285 | if (freepage_order < MAX_ORDER) |
| 286 | pfn += (1UL << freepage_order) - 1; |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 287 | continue; |
| 288 | } |
| 289 | |
| 290 | if (PageReserved(page)) |
| 291 | continue; |
| 292 | |
| 293 | page_ext = lookup_page_ext(page); |
| 294 | if (unlikely(!page_ext)) |
| 295 | continue; |
| 296 | |
| 297 | if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) |
| 298 | continue; |
| 299 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 300 | page_owner = get_page_owner(page_ext); |
| 301 | page_mt = gfpflags_to_migratetype( |
| 302 | page_owner->gfp_mask); |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 303 | if (pageblock_mt != page_mt) { |
| 304 | if (is_migrate_cma(pageblock_mt)) |
| 305 | count[MIGRATE_MOVABLE]++; |
| 306 | else |
| 307 | count[pageblock_mt]++; |
| 308 | |
| 309 | pfn = block_end_pfn; |
| 310 | break; |
| 311 | } |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 312 | pfn += (1UL << page_owner->order) - 1; |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | /* Print counts */ |
| 317 | seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name); |
| 318 | for (i = 0; i < MIGRATE_TYPES; i++) |
| 319 | seq_printf(m, "%12lu ", count[i]); |
| 320 | seq_putc(m, '\n'); |
| 321 | } |
| 322 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 323 | static ssize_t |
| 324 | print_page_owner(char __user *buf, size_t count, unsigned long pfn, |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 325 | struct page *page, struct page_owner *page_owner, |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 326 | depot_stack_handle_t handle) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 327 | { |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 328 | int ret, pageblock_mt, page_mt; |
| 329 | unsigned long *entries; |
| 330 | unsigned int nr_entries; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 331 | char *kbuf; |
| 332 | |
Miles Chen | c8f61cf | 2018-12-28 00:33:21 -0800 | [diff] [blame] | 333 | count = min_t(size_t, count, PAGE_SIZE); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 334 | kbuf = kmalloc(count, GFP_KERNEL); |
| 335 | if (!kbuf) |
| 336 | return -ENOMEM; |
| 337 | |
| 338 | ret = snprintf(kbuf, count, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 339 | "Page allocated via order %u, mask %#x(%pGg)\n", |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 340 | page_owner->order, page_owner->gfp_mask, |
| 341 | &page_owner->gfp_mask); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 342 | |
| 343 | if (ret >= count) |
| 344 | goto err; |
| 345 | |
| 346 | /* Print information relevant to grouping pages by mobility */ |
Mel Gorman | 0b423ca | 2016-05-19 17:14:27 -0700 | [diff] [blame] | 347 | pageblock_mt = get_pageblock_migratetype(page); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 348 | page_mt = gfpflags_to_migratetype(page_owner->gfp_mask); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 349 | ret += snprintf(kbuf + ret, count - ret, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 350 | "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n", |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 351 | pfn, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 352 | migratetype_names[page_mt], |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 353 | pfn >> pageblock_order, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 354 | migratetype_names[pageblock_mt], |
| 355 | page->flags, &page->flags); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 356 | |
| 357 | if (ret >= count) |
| 358 | goto err; |
| 359 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 360 | nr_entries = stack_depot_fetch(handle, &entries); |
| 361 | ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 362 | if (ret >= count) |
| 363 | goto err; |
| 364 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 365 | if (page_owner->last_migrate_reason != -1) { |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 366 | ret += snprintf(kbuf + ret, count - ret, |
| 367 | "Page has been migrated, last migrate reason: %s\n", |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 368 | migrate_reason_names[page_owner->last_migrate_reason]); |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 369 | if (ret >= count) |
| 370 | goto err; |
| 371 | } |
| 372 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 373 | ret += snprintf(kbuf + ret, count - ret, "\n"); |
| 374 | if (ret >= count) |
| 375 | goto err; |
| 376 | |
| 377 | if (copy_to_user(buf, kbuf, ret)) |
| 378 | ret = -EFAULT; |
| 379 | |
| 380 | kfree(kbuf); |
| 381 | return ret; |
| 382 | |
| 383 | err: |
| 384 | kfree(kbuf); |
| 385 | return -ENOMEM; |
| 386 | } |
| 387 | |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 388 | void __dump_page_owner(struct page *page) |
| 389 | { |
| 390 | struct page_ext *page_ext = lookup_page_ext(page); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 391 | struct page_owner *page_owner; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 392 | depot_stack_handle_t handle; |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 393 | unsigned long *entries; |
| 394 | unsigned int nr_entries; |
Sudip Mukherjee | 8285027 | 2016-06-24 14:50:24 -0700 | [diff] [blame] | 395 | gfp_t gfp_mask; |
| 396 | int mt; |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 397 | |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 398 | if (unlikely(!page_ext)) { |
| 399 | pr_alert("There is not page extension available.\n"); |
| 400 | return; |
| 401 | } |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 402 | |
| 403 | page_owner = get_page_owner(page_ext); |
| 404 | gfp_mask = page_owner->gfp_mask; |
Sudip Mukherjee | 8285027 | 2016-06-24 14:50:24 -0700 | [diff] [blame] | 405 | mt = gfpflags_to_migratetype(gfp_mask); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 406 | |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 407 | if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) { |
| 408 | pr_alert("page_owner info is not active (free page?)\n"); |
| 409 | return; |
| 410 | } |
| 411 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 412 | handle = READ_ONCE(page_owner->handle); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 413 | if (!handle) { |
| 414 | pr_alert("page_owner info is not active (free page?)\n"); |
| 415 | return; |
| 416 | } |
| 417 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 418 | nr_entries = stack_depot_fetch(handle, &entries); |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 419 | pr_alert("page allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n", |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 420 | page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask); |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame^] | 421 | stack_trace_print(entries, nr_entries, 0); |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 422 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 423 | if (page_owner->last_migrate_reason != -1) |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 424 | pr_alert("page has been migrated, last migrate reason: %s\n", |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 425 | migrate_reason_names[page_owner->last_migrate_reason]); |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 428 | static ssize_t |
| 429 | read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 430 | { |
| 431 | unsigned long pfn; |
| 432 | struct page *page; |
| 433 | struct page_ext *page_ext; |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 434 | struct page_owner *page_owner; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 435 | depot_stack_handle_t handle; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 436 | |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 437 | if (!static_branch_unlikely(&page_owner_inited)) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 438 | return -EINVAL; |
| 439 | |
| 440 | page = NULL; |
| 441 | pfn = min_low_pfn + *ppos; |
| 442 | |
| 443 | /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */ |
| 444 | while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0) |
| 445 | pfn++; |
| 446 | |
| 447 | drain_all_pages(NULL); |
| 448 | |
| 449 | /* Find an allocated page */ |
| 450 | for (; pfn < max_pfn; pfn++) { |
| 451 | /* |
| 452 | * If the new page is in a new MAX_ORDER_NR_PAGES area, |
| 453 | * validate the area as existing, skip it if not |
| 454 | */ |
| 455 | if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) { |
| 456 | pfn += MAX_ORDER_NR_PAGES - 1; |
| 457 | continue; |
| 458 | } |
| 459 | |
| 460 | /* Check for holes within a MAX_ORDER area */ |
| 461 | if (!pfn_valid_within(pfn)) |
| 462 | continue; |
| 463 | |
| 464 | page = pfn_to_page(pfn); |
| 465 | if (PageBuddy(page)) { |
| 466 | unsigned long freepage_order = page_order_unsafe(page); |
| 467 | |
| 468 | if (freepage_order < MAX_ORDER) |
| 469 | pfn += (1UL << freepage_order) - 1; |
| 470 | continue; |
| 471 | } |
| 472 | |
| 473 | page_ext = lookup_page_ext(page); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 474 | if (unlikely(!page_ext)) |
| 475 | continue; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 476 | |
| 477 | /* |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 478 | * Some pages could be missed by concurrent allocation or free, |
| 479 | * because we don't hold the zone lock. |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 480 | */ |
| 481 | if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) |
| 482 | continue; |
| 483 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 484 | page_owner = get_page_owner(page_ext); |
| 485 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 486 | /* |
| 487 | * Access to page_ext->handle isn't synchronous so we should |
| 488 | * be careful to access it. |
| 489 | */ |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 490 | handle = READ_ONCE(page_owner->handle); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 491 | if (!handle) |
| 492 | continue; |
| 493 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 494 | /* Record the next PFN to read in the file offset */ |
| 495 | *ppos = (pfn - min_low_pfn) + 1; |
| 496 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 497 | return print_page_owner(buf, count, pfn, page, |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 498 | page_owner, handle); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 504 | static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone) |
| 505 | { |
Oscar Salvador | 6787c1d | 2018-01-31 16:20:11 -0800 | [diff] [blame] | 506 | unsigned long pfn = zone->zone_start_pfn; |
| 507 | unsigned long end_pfn = zone_end_pfn(zone); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 508 | unsigned long count = 0; |
| 509 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 510 | /* |
| 511 | * Walk the zone in pageblock_nr_pages steps. If a page block spans |
| 512 | * a zone boundary, it will be double counted between zones. This does |
| 513 | * not matter as the mixed block count will still be correct |
| 514 | */ |
| 515 | for (; pfn < end_pfn; ) { |
Oscar Salvador | 6787c1d | 2018-01-31 16:20:11 -0800 | [diff] [blame] | 516 | unsigned long block_end_pfn; |
| 517 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 518 | if (!pfn_valid(pfn)) { |
| 519 | pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES); |
| 520 | continue; |
| 521 | } |
| 522 | |
| 523 | block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); |
| 524 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 525 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 526 | for (; pfn < block_end_pfn; pfn++) { |
Oscar Salvador | 6787c1d | 2018-01-31 16:20:11 -0800 | [diff] [blame] | 527 | struct page *page; |
| 528 | struct page_ext *page_ext; |
| 529 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 530 | if (!pfn_valid_within(pfn)) |
| 531 | continue; |
| 532 | |
| 533 | page = pfn_to_page(pfn); |
| 534 | |
Joonsoo Kim | 9d43f5a | 2016-05-19 17:12:13 -0700 | [diff] [blame] | 535 | if (page_zone(page) != zone) |
| 536 | continue; |
| 537 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 538 | /* |
Vlastimil Babka | 1090302 | 2017-09-06 16:20:51 -0700 | [diff] [blame] | 539 | * To avoid having to grab zone->lock, be a little |
| 540 | * careful when reading buddy page order. The only |
| 541 | * danger is that we skip too much and potentially miss |
| 542 | * some early allocated pages, which is better than |
| 543 | * heavy lock contention. |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 544 | */ |
| 545 | if (PageBuddy(page)) { |
Vlastimil Babka | 1090302 | 2017-09-06 16:20:51 -0700 | [diff] [blame] | 546 | unsigned long order = page_order_unsafe(page); |
| 547 | |
| 548 | if (order > 0 && order < MAX_ORDER) |
| 549 | pfn += (1UL << order) - 1; |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 550 | continue; |
| 551 | } |
| 552 | |
| 553 | if (PageReserved(page)) |
| 554 | continue; |
| 555 | |
| 556 | page_ext = lookup_page_ext(page); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 557 | if (unlikely(!page_ext)) |
| 558 | continue; |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 559 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 560 | /* Maybe overlapping zone */ |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 561 | if (test_bit(PAGE_EXT_OWNER, &page_ext->flags)) |
| 562 | continue; |
| 563 | |
| 564 | /* Found early allocated page */ |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 565 | __set_page_owner_handle(page_ext, early_handle, 0, 0); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 566 | count++; |
| 567 | } |
Vlastimil Babka | 1090302 | 2017-09-06 16:20:51 -0700 | [diff] [blame] | 568 | cond_resched(); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n", |
| 572 | pgdat->node_id, zone->name, count); |
| 573 | } |
| 574 | |
| 575 | static void init_zones_in_node(pg_data_t *pgdat) |
| 576 | { |
| 577 | struct zone *zone; |
| 578 | struct zone *node_zones = pgdat->node_zones; |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 579 | |
| 580 | for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) { |
| 581 | if (!populated_zone(zone)) |
| 582 | continue; |
| 583 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 584 | init_pages_in_zone(pgdat, zone); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
| 588 | static void init_early_allocated_pages(void) |
| 589 | { |
| 590 | pg_data_t *pgdat; |
| 591 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 592 | for_each_online_pgdat(pgdat) |
| 593 | init_zones_in_node(pgdat); |
| 594 | } |
| 595 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 596 | static const struct file_operations proc_page_owner_operations = { |
| 597 | .read = read_page_owner, |
| 598 | }; |
| 599 | |
| 600 | static int __init pageowner_init(void) |
| 601 | { |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 602 | if (!static_branch_unlikely(&page_owner_inited)) { |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 603 | pr_info("page_owner is disabled\n"); |
| 604 | return 0; |
| 605 | } |
| 606 | |
Greg Kroah-Hartman | d9f7979 | 2019-03-05 15:46:09 -0800 | [diff] [blame] | 607 | debugfs_create_file("page_owner", 0400, NULL, NULL, |
| 608 | &proc_page_owner_operations); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 609 | |
Greg Kroah-Hartman | d9f7979 | 2019-03-05 15:46:09 -0800 | [diff] [blame] | 610 | return 0; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 611 | } |
Paul Gortmaker | 44c5af9 | 2015-05-01 21:57:34 -0400 | [diff] [blame] | 612 | late_initcall(pageowner_init) |