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