]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - mm/page_isolation.c
mm/memory_hotplug.c: fix notification in offline error path
[mirror_ubuntu-focal-kernel.git] / mm / page_isolation.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
a5d76b54
KH
2/*
3 * linux/mm/page_isolation.c
4 */
5
a5d76b54
KH
6#include <linux/mm.h>
7#include <linux/page-isolation.h>
8#include <linux/pageblock-flags.h>
ee6f509c 9#include <linux/memory.h>
c8721bbb 10#include <linux/hugetlb.h>
83358ece 11#include <linux/page_owner.h>
8b913238 12#include <linux/migrate.h>
a5d76b54
KH
13#include "internal.h"
14
0f0848e5
JK
15#define CREATE_TRACE_POINTS
16#include <trace/events/page_isolation.h>
17
d381c547 18static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
ee6f509c
MK
19{
20 struct zone *zone;
21 unsigned long flags, pfn;
22 struct memory_isolate_notify arg;
23 int notifier_ret;
24 int ret = -EBUSY;
25
26 zone = page_zone(page);
27
28 spin_lock_irqsave(&zone->lock, flags);
29
2c7452a0
MK
30 /*
31 * We assume the caller intended to SET migrate type to isolate.
32 * If it is already set, then someone else must have raced and
33 * set it before us. Return -EBUSY
34 */
35 if (is_migrate_isolate_page(page))
36 goto out;
37
ee6f509c
MK
38 pfn = page_to_pfn(page);
39 arg.start_pfn = pfn;
40 arg.nr_pages = pageblock_nr_pages;
41 arg.pages_found = 0;
42
43 /*
44 * It may be possible to isolate a pageblock even if the
45 * migratetype is not MIGRATE_MOVABLE. The memory isolation
46 * notifier chain is used by balloon drivers to return the
47 * number of pages in a range that are held by the balloon
48 * driver to shrink memory. If all the pages are accounted for
49 * by balloons, are free, or on the LRU, isolation can continue.
50 * Later, for example, when memory hotplug notifier runs, these
51 * pages reported as "can be isolated" should be isolated(freed)
52 * by the balloon driver through the memory notifier chain.
53 */
54 notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
55 notifier_ret = notifier_to_errno(notifier_ret);
56 if (notifier_ret)
57 goto out;
58 /*
59 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
60 * We just check MOVABLE pages.
61 */
d381c547 62 if (!has_unmovable_pages(zone, page, arg.pages_found, migratetype, flags))
ee6f509c
MK
63 ret = 0;
64
65 /*
ac34dcd2 66 * immobile means "not-on-lru" pages. If immobile is larger than
ee6f509c
MK
67 * removable-by-driver pages reported by notifier, we'll fail.
68 */
69
70out:
71 if (!ret) {
2139cbe6 72 unsigned long nr_pages;
4da2ce25 73 int mt = get_pageblock_migratetype(page);
2139cbe6 74
a458431e 75 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
ad53f92e 76 zone->nr_isolate_pageblock++;
02aa0cdd
VB
77 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
78 NULL);
2139cbe6 79
4da2ce25 80 __mod_zone_freepage_state(zone, -nr_pages, mt);
ee6f509c
MK
81 }
82
83 spin_unlock_irqrestore(&zone->lock, flags);
84 if (!ret)
ec25af84 85 drain_all_pages(zone);
ee6f509c
MK
86 return ret;
87}
88
c5b4e1b0 89static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
ee6f509c
MK
90{
91 struct zone *zone;
2139cbe6 92 unsigned long flags, nr_pages;
e3a2713c 93 bool isolated_page = false;
3c605096 94 unsigned int order;
76741e77 95 unsigned long pfn, buddy_pfn;
3c605096 96 struct page *buddy;
2139cbe6 97
ee6f509c
MK
98 zone = page_zone(page);
99 spin_lock_irqsave(&zone->lock, flags);
bbf9ce97 100 if (!is_migrate_isolate_page(page))
ee6f509c 101 goto out;
3c605096
JK
102
103 /*
104 * Because freepage with more than pageblock_order on isolated
105 * pageblock is restricted to merge due to freepage counting problem,
106 * it is possible that there is free buddy page.
107 * move_freepages_block() doesn't care of merge so we need other
108 * approach in order to merge them. Isolation and free will make
109 * these pages to be merged.
110 */
111 if (PageBuddy(page)) {
112 order = page_order(page);
113 if (order >= pageblock_order) {
76741e77
VB
114 pfn = page_to_pfn(page);
115 buddy_pfn = __find_buddy_pfn(pfn, order);
116 buddy = page + (buddy_pfn - pfn);
3c605096 117
13ad59df 118 if (pfn_valid_within(buddy_pfn) &&
1ae7013d 119 !is_migrate_isolate_page(buddy)) {
3c605096 120 __isolate_free_page(page, order);
e3a2713c 121 isolated_page = true;
3c605096
JK
122 }
123 }
124 }
125
126 /*
127 * If we isolate freepage with more than pageblock_order, there
128 * should be no freepage in the range, so we could avoid costly
129 * pageblock scanning for freepage moving.
130 */
131 if (!isolated_page) {
02aa0cdd 132 nr_pages = move_freepages_block(zone, page, migratetype, NULL);
3c605096
JK
133 __mod_zone_freepage_state(zone, nr_pages, migratetype);
134 }
a458431e 135 set_pageblock_migratetype(page, migratetype);
ad53f92e 136 zone->nr_isolate_pageblock--;
ee6f509c
MK
137out:
138 spin_unlock_irqrestore(&zone->lock, flags);
83358ece 139 if (isolated_page) {
46f24fd8 140 post_alloc_hook(page, order, __GFP_MOVABLE);
e3a2713c 141 __free_pages(page, order);
83358ece 142 }
ee6f509c
MK
143}
144
a5d76b54
KH
145static inline struct page *
146__first_valid_page(unsigned long pfn, unsigned long nr_pages)
147{
148 int i;
2ce13640
MH
149
150 for (i = 0; i < nr_pages; i++) {
151 struct page *page;
152
153 if (!pfn_valid_within(pfn + i))
154 continue;
155 page = pfn_to_online_page(pfn + i);
156 if (!page)
157 continue;
158 return page;
159 }
160 return NULL;
a5d76b54
KH
161}
162
9b7ea46a
QC
163/**
164 * start_isolate_page_range() - make page-allocation-type of range of pages to
165 * be MIGRATE_ISOLATE.
166 * @start_pfn: The lower PFN of the range to be isolated.
167 * @end_pfn: The upper PFN of the range to be isolated.
168 * start_pfn/end_pfn must be aligned to pageblock_order.
169 * @migratetype: Migrate type to set in error recovery.
170 * @flags: The following flags are allowed (they can be combined in
171 * a bit mask)
172 * SKIP_HWPOISON - ignore hwpoison pages
173 * REPORT_FAILURE - report details about the failure to
174 * isolate the range
a5d76b54
KH
175 *
176 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
177 * the range will never be allocated. Any free pages and pages freed in the
9b7ea46a
QC
178 * future will not be allocated again. If specified range includes migrate types
179 * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
180 * pages in the range finally, the caller have to free all pages in the range.
181 * test_page_isolated() can be used for test it.
2c7452a0
MK
182 *
183 * There is no high level synchronization mechanism that prevents two threads
9b7ea46a 184 * from trying to isolate overlapping ranges. If this happens, one thread
2c7452a0
MK
185 * will notice pageblocks in the overlapping range already set to isolate.
186 * This happens in set_migratetype_isolate, and set_migratetype_isolate
9b7ea46a
QC
187 * returns an error. We then clean up by restoring the migration type on
188 * pageblocks we may have modified and return -EBUSY to caller. This
2c7452a0 189 * prevents two threads from simultaneously working on overlapping ranges.
9b7ea46a
QC
190 *
191 * Return: the number of isolated pageblocks on success and -EBUSY if any part
192 * of range cannot be isolated.
a5d76b54 193 */
0815f3d8 194int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
d381c547 195 unsigned migratetype, int flags)
a5d76b54
KH
196{
197 unsigned long pfn;
198 unsigned long undo_pfn;
199 struct page *page;
9b7ea46a 200 int nr_isolate_pageblock = 0;
a5d76b54 201
fec174d6
NH
202 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
203 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
a5d76b54
KH
204
205 for (pfn = start_pfn;
206 pfn < end_pfn;
207 pfn += pageblock_nr_pages) {
208 page = __first_valid_page(pfn, pageblock_nr_pages);
9b7ea46a
QC
209 if (page) {
210 if (set_migratetype_isolate(page, migratetype, flags)) {
211 undo_pfn = pfn;
212 goto undo;
213 }
214 nr_isolate_pageblock++;
a5d76b54
KH
215 }
216 }
9b7ea46a 217 return nr_isolate_pageblock;
a5d76b54
KH
218undo:
219 for (pfn = start_pfn;
dbc0e4ce 220 pfn < undo_pfn;
2ce13640
MH
221 pfn += pageblock_nr_pages) {
222 struct page *page = pfn_to_online_page(pfn);
223 if (!page)
224 continue;
225 unset_migratetype_isolate(page, migratetype);
226 }
a5d76b54
KH
227
228 return -EBUSY;
229}
230
231/*
232 * Make isolated pages available again.
233 */
0815f3d8
MN
234int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
235 unsigned migratetype)
a5d76b54
KH
236{
237 unsigned long pfn;
238 struct page *page;
6f8d2b8a
WX
239
240 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
241 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
242
a5d76b54
KH
243 for (pfn = start_pfn;
244 pfn < end_pfn;
245 pfn += pageblock_nr_pages) {
246 page = __first_valid_page(pfn, pageblock_nr_pages);
bbf9ce97 247 if (!page || !is_migrate_isolate_page(page))
a5d76b54 248 continue;
0815f3d8 249 unset_migratetype_isolate(page, migratetype);
a5d76b54
KH
250 }
251 return 0;
252}
253/*
254 * Test all pages in the range is free(means isolated) or not.
255 * all pages in [start_pfn...end_pfn) must be in the same zone.
256 * zone->lock must be held before call this.
257 *
ec3b6882 258 * Returns the last tested pfn.
a5d76b54 259 */
fea85cff 260static unsigned long
b023f468
WC
261__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
262 bool skip_hwpoisoned_pages)
a5d76b54
KH
263{
264 struct page *page;
265
266 while (pfn < end_pfn) {
267 if (!pfn_valid_within(pfn)) {
268 pfn++;
269 continue;
270 }
271 page = pfn_to_page(pfn);
aa016d14 272 if (PageBuddy(page))
435b405c 273 /*
aa016d14
VB
274 * If the page is on a free list, it has to be on
275 * the correct MIGRATE_ISOLATE freelist. There is no
276 * simple way to verify that as VM_BUG_ON(), though.
435b405c 277 */
a5d76b54 278 pfn += 1 << page_order(page);
aa016d14
VB
279 else if (skip_hwpoisoned_pages && PageHWPoison(page))
280 /* A HWPoisoned page cannot be also PageBuddy */
b023f468 281 pfn++;
a5d76b54
KH
282 else
283 break;
284 }
fea85cff
JK
285
286 return pfn;
a5d76b54
KH
287}
288
b9eb6319 289/* Caller should ensure that requested range is in a single zone */
b023f468
WC
290int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
291 bool skip_hwpoisoned_pages)
a5d76b54 292{
6c1b7f68 293 unsigned long pfn, flags;
a5d76b54 294 struct page *page;
6c1b7f68 295 struct zone *zone;
a5d76b54 296
a5d76b54 297 /*
85dbe706
TC
298 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
299 * are not aligned to pageblock_nr_pages.
300 * Then we just check migratetype first.
a5d76b54
KH
301 */
302 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
303 page = __first_valid_page(pfn, pageblock_nr_pages);
bbf9ce97 304 if (page && !is_migrate_isolate_page(page))
a5d76b54
KH
305 break;
306 }
a70dcb96
GS
307 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
308 if ((pfn < end_pfn) || !page)
a5d76b54 309 return -EBUSY;
85dbe706 310 /* Check all pages are free or marked as ISOLATED */
a70dcb96 311 zone = page_zone(page);
6c1b7f68 312 spin_lock_irqsave(&zone->lock, flags);
fea85cff 313 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
b023f468 314 skip_hwpoisoned_pages);
6c1b7f68 315 spin_unlock_irqrestore(&zone->lock, flags);
fea85cff 316
0f0848e5
JK
317 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
318
fea85cff 319 return pfn < end_pfn ? -EBUSY : 0;
a5d76b54 320}
723a0644 321
666feb21 322struct page *alloc_migrate_target(struct page *page, unsigned long private)
723a0644 323{
8b913238 324 return new_page_nodemask(page, numa_node_id(), &node_states[N_MEMORY]);
723a0644 325}