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