Blame view
mm/page_isolation.c
9.17 KB
b24413180 License cleanup: ... |
1 |
// SPDX-License-Identifier: GPL-2.0 |
a5d76b54a memory unplug: pa... |
2 3 4 |
/* * linux/mm/page_isolation.c */ |
a5d76b54a memory unplug: pa... |
5 6 7 |
#include <linux/mm.h> #include <linux/page-isolation.h> #include <linux/pageblock-flags.h> |
ee6f509c3 mm: factor out me... |
8 |
#include <linux/memory.h> |
c8721bbbd mm: memory-hotplu... |
9 |
#include <linux/hugetlb.h> |
83358ece2 mm/page_owner: in... |
10 |
#include <linux/page_owner.h> |
8b9132388 mm: unify new_nod... |
11 |
#include <linux/migrate.h> |
a5d76b54a memory unplug: pa... |
12 |
#include "internal.h" |
0f0848e51 mm/page_isolation... |
13 14 |
#define CREATE_TRACE_POINTS #include <trace/events/page_isolation.h> |
d381c5476 mm: only report i... |
15 |
static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags) |
ee6f509c3 mm: factor out me... |
16 |
{ |
1c31cb493 mm/page_isolation... |
17 18 |
struct zone *zone = page_zone(page); struct page *unmovable; |
3f9903b9c mm: remove the me... |
19 |
unsigned long flags; |
ee6f509c3 mm: factor out me... |
20 21 |
spin_lock_irqsave(&zone->lock, flags); |
2c7452a07 mm/page_isolation... |
22 23 24 |
/* * We assume the caller intended to SET migrate type to isolate. * If it is already set, then someone else must have raced and |
51030a53d mm/page_isolation... |
25 |
* set it before us. |
2c7452a07 mm/page_isolation... |
26 |
*/ |
51030a53d mm/page_isolation... |
27 28 29 30 |
if (is_migrate_isolate_page(page)) { spin_unlock_irqrestore(&zone->lock, flags); return -EBUSY; } |
2c7452a07 mm/page_isolation... |
31 |
|
ee6f509c3 mm: factor out me... |
32 33 34 35 |
/* * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself. * We just check MOVABLE pages. */ |
4a55c0474 mm/hotplug: silen... |
36 37 |
unmovable = has_unmovable_pages(zone, page, migratetype, isol_flags); if (!unmovable) { |
2139cbe62 cma: fix counting... |
38 |
unsigned long nr_pages; |
4da2ce250 mm: distinguish C... |
39 |
int mt = get_pageblock_migratetype(page); |
2139cbe62 cma: fix counting... |
40 |
|
a458431e1 mm: fix zone_wate... |
41 |
set_pageblock_migratetype(page, MIGRATE_ISOLATE); |
ad53f92eb mm/page_alloc: fi... |
42 |
zone->nr_isolate_pageblock++; |
02aa0cdd7 mm, page_alloc: c... |
43 44 |
nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE, NULL); |
2139cbe62 cma: fix counting... |
45 |
|
4da2ce250 mm: distinguish C... |
46 |
__mod_zone_freepage_state(zone, -nr_pages, mt); |
1c31cb493 mm/page_isolation... |
47 48 49 |
spin_unlock_irqrestore(&zone->lock, flags); drain_all_pages(zone); return 0; |
ee6f509c3 mm: factor out me... |
50 51 52 |
} spin_unlock_irqrestore(&zone->lock, flags); |
1c31cb493 mm/page_isolation... |
53 |
if (isol_flags & REPORT_FAILURE) { |
48381d7e4 mm/page_isolation... |
54 55 56 57 58 |
/* * printk() with zone->lock held will likely trigger a * lockdep splat, so defer it here. */ dump_page(unmovable, "unmovable page"); |
3d680bdf6 mm/page_isolation... |
59 |
} |
4a55c0474 mm/hotplug: silen... |
60 |
|
1c31cb493 mm/page_isolation... |
61 |
return -EBUSY; |
ee6f509c3 mm: factor out me... |
62 |
} |
c5b4e1b02 mm, page_isolatio... |
63 |
static void unset_migratetype_isolate(struct page *page, unsigned migratetype) |
ee6f509c3 mm: factor out me... |
64 65 |
{ struct zone *zone; |
2139cbe62 cma: fix counting... |
66 |
unsigned long flags, nr_pages; |
e3a2713c3 mm/page_isolation... |
67 |
bool isolated_page = false; |
3c605096d mm/page_alloc: re... |
68 |
unsigned int order; |
76741e776 mm, page_alloc: d... |
69 |
unsigned long pfn, buddy_pfn; |
3c605096d mm/page_alloc: re... |
70 |
struct page *buddy; |
2139cbe62 cma: fix counting... |
71 |
|
ee6f509c3 mm: factor out me... |
72 73 |
zone = page_zone(page); spin_lock_irqsave(&zone->lock, flags); |
bbf9ce971 mm: use is_migrat... |
74 |
if (!is_migrate_isolate_page(page)) |
ee6f509c3 mm: factor out me... |
75 |
goto out; |
3c605096d mm/page_alloc: re... |
76 77 78 79 80 81 82 83 84 85 |
/* * Because freepage with more than pageblock_order on isolated * pageblock is restricted to merge due to freepage counting problem, * it is possible that there is free buddy page. * move_freepages_block() doesn't care of merge so we need other * approach in order to merge them. Isolation and free will make * these pages to be merged. */ if (PageBuddy(page)) { |
ab130f910 mm: rename page_o... |
86 |
order = buddy_order(page); |
3c605096d mm/page_alloc: re... |
87 |
if (order >= pageblock_order) { |
76741e776 mm, page_alloc: d... |
88 89 90 |
pfn = page_to_pfn(page); buddy_pfn = __find_buddy_pfn(pfn, order); buddy = page + (buddy_pfn - pfn); |
3c605096d mm/page_alloc: re... |
91 |
|
13ad59df6 mm, page_alloc: a... |
92 |
if (pfn_valid_within(buddy_pfn) && |
1ae7013df CMA: page_isolati... |
93 |
!is_migrate_isolate_page(buddy)) { |
3c605096d mm/page_alloc: re... |
94 |
__isolate_free_page(page, order); |
e3a2713c3 mm/page_isolation... |
95 |
isolated_page = true; |
3c605096d mm/page_alloc: re... |
96 97 98 99 100 101 102 103 |
} } } /* * If we isolate freepage with more than pageblock_order, there * should be no freepage in the range, so we could avoid costly * pageblock scanning for freepage moving. |
293ffa5eb mm/page_alloc: mo... |
104 105 106 107 108 |
* * We didn't actually touch any of the isolated pages, so place them * to the tail of the freelist. This is an optimization for memory * onlining - just onlined memory won't immediately be considered for * allocation. |
3c605096d mm/page_alloc: re... |
109 110 |
*/ if (!isolated_page) { |
02aa0cdd7 mm, page_alloc: c... |
111 |
nr_pages = move_freepages_block(zone, page, migratetype, NULL); |
3c605096d mm/page_alloc: re... |
112 113 |
__mod_zone_freepage_state(zone, nr_pages, migratetype); } |
a458431e1 mm: fix zone_wate... |
114 |
set_pageblock_migratetype(page, migratetype); |
624f58d8f mm: add function ... |
115 116 |
if (isolated_page) __putback_isolated_page(page, order, migratetype); |
ad53f92eb mm/page_alloc: fi... |
117 |
zone->nr_isolate_pageblock--; |
ee6f509c3 mm: factor out me... |
118 119 120 |
out: spin_unlock_irqrestore(&zone->lock, flags); } |
a5d76b54a memory unplug: pa... |
121 122 123 124 |
static inline struct page * __first_valid_page(unsigned long pfn, unsigned long nr_pages) { int i; |
2ce13640b mm: __first_valid... |
125 126 127 |
for (i = 0; i < nr_pages; i++) { struct page *page; |
2ce13640b mm: __first_valid... |
128 129 130 131 132 133 |
page = pfn_to_online_page(pfn + i); if (!page) continue; return page; } return NULL; |
a5d76b54a memory unplug: pa... |
134 |
} |
9b7ea46a8 mm/hotplug: fix o... |
135 136 137 138 139 140 141 142 143 |
/** * start_isolate_page_range() - make page-allocation-type of range of pages to * be MIGRATE_ISOLATE. * @start_pfn: The lower PFN of the range to be isolated. * @end_pfn: The upper PFN of the range to be isolated. * start_pfn/end_pfn must be aligned to pageblock_order. * @migratetype: Migrate type to set in error recovery. * @flags: The following flags are allowed (they can be combined in * a bit mask) |
756d25be4 mm/page_isolation... |
144 145 |
* MEMORY_OFFLINE - isolate to offline (!allocate) memory * e.g., skip over PageHWPoison() pages |
aa218795c mm: Allow to offl... |
146 |
* and PageOffline() pages. |
9b7ea46a8 mm/hotplug: fix o... |
147 148 |
* REPORT_FAILURE - report details about the failure to * isolate the range |
a5d76b54a memory unplug: pa... |
149 150 151 |
* * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in * the range will never be allocated. Any free pages and pages freed in the |
9b7ea46a8 mm/hotplug: fix o... |
152 153 154 155 |
* future will not be allocated again. If specified range includes migrate types * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all * pages in the range finally, the caller have to free all pages in the range. * test_page_isolated() can be used for test it. |
2c7452a07 mm/page_isolation... |
156 157 |
* * There is no high level synchronization mechanism that prevents two threads |
9b7ea46a8 mm/hotplug: fix o... |
158 |
* from trying to isolate overlapping ranges. If this happens, one thread |
2c7452a07 mm/page_isolation... |
159 160 |
* will notice pageblocks in the overlapping range already set to isolate. * This happens in set_migratetype_isolate, and set_migratetype_isolate |
9b7ea46a8 mm/hotplug: fix o... |
161 162 |
* returns an error. We then clean up by restoring the migration type on * pageblocks we may have modified and return -EBUSY to caller. This |
2c7452a07 mm/page_isolation... |
163 |
* prevents two threads from simultaneously working on overlapping ranges. |
9b7ea46a8 mm/hotplug: fix o... |
164 |
* |
968318261 mm/memory_hotplug... |
165 166 167 168 169 170 171 172 |
* Please note that there is no strong synchronization with the page allocator * either. Pages might be freed while their page blocks are marked ISOLATED. * In some cases pages might still end up on pcp lists and that would allow * for their allocation even when they are in fact isolated already. Depending * on how strong of a guarantee the caller needs drain_all_pages might be needed * (e.g. __offline_pages will need to call it after check for isolated range for * a next retry). * |
3fa0c7c79 mm/page_isolation... |
173 |
* Return: 0 on success and -EBUSY if any part of range cannot be isolated. |
a5d76b54a memory unplug: pa... |
174 |
*/ |
0815f3d81 mm: page_isolatio... |
175 |
int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn, |
d381c5476 mm: only report i... |
176 |
unsigned migratetype, int flags) |
a5d76b54a memory unplug: pa... |
177 178 179 180 |
{ unsigned long pfn; unsigned long undo_pfn; struct page *page; |
fec174d66 mm/page_isolation... |
181 182 |
BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages)); BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages)); |
a5d76b54a memory unplug: pa... |
183 184 185 186 187 |
for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { page = __first_valid_page(pfn, pageblock_nr_pages); |
9b7ea46a8 mm/hotplug: fix o... |
188 189 190 191 192 |
if (page) { if (set_migratetype_isolate(page, migratetype, flags)) { undo_pfn = pfn; goto undo; } |
a5d76b54a memory unplug: pa... |
193 194 |
} } |
3fa0c7c79 mm/page_isolation... |
195 |
return 0; |
a5d76b54a memory unplug: pa... |
196 197 |
undo: for (pfn = start_pfn; |
dbc0e4cef memory hotremove:... |
198 |
pfn < undo_pfn; |
2ce13640b mm: __first_valid... |
199 200 201 202 203 204 |
pfn += pageblock_nr_pages) { struct page *page = pfn_to_online_page(pfn); if (!page) continue; unset_migratetype_isolate(page, migratetype); } |
a5d76b54a memory unplug: pa... |
205 206 207 208 209 210 211 |
return -EBUSY; } /* * Make isolated pages available again. */ |
1fcf0a561 mm/page_isolation... |
212 |
void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn, |
0815f3d81 mm: page_isolatio... |
213 |
unsigned migratetype) |
a5d76b54a memory unplug: pa... |
214 215 216 |
{ unsigned long pfn; struct page *page; |
6f8d2b8a2 mm/page_isolation... |
217 218 219 |
BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages)); BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages)); |
a5d76b54a memory unplug: pa... |
220 221 222 223 |
for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { page = __first_valid_page(pfn, pageblock_nr_pages); |
bbf9ce971 mm: use is_migrat... |
224 |
if (!page || !is_migrate_isolate_page(page)) |
a5d76b54a memory unplug: pa... |
225 |
continue; |
0815f3d81 mm: page_isolatio... |
226 |
unset_migratetype_isolate(page, migratetype); |
a5d76b54a memory unplug: pa... |
227 |
} |
a5d76b54a memory unplug: pa... |
228 229 230 231 232 233 |
} /* * Test all pages in the range is free(means isolated) or not. * all pages in [start_pfn...end_pfn) must be in the same zone. * zone->lock must be held before call this. * |
ec3b68825 mm/page_isolation... |
234 |
* Returns the last tested pfn. |
a5d76b54a memory unplug: pa... |
235 |
*/ |
fea85cff1 mm/page_isolation... |
236 |
static unsigned long |
b023f4681 memory-hotplug: s... |
237 |
__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn, |
756d25be4 mm/page_isolation... |
238 |
int flags) |
a5d76b54a memory unplug: pa... |
239 240 241 242 243 244 245 246 247 |
{ struct page *page; while (pfn < end_pfn) { if (!pfn_valid_within(pfn)) { pfn++; continue; } page = pfn_to_page(pfn); |
aa016d145 mm, page_isolatio... |
248 |
if (PageBuddy(page)) |
435b405c0 memory-hotplug: f... |
249 |
/* |
aa016d145 mm, page_isolatio... |
250 251 252 |
* If the page is on a free list, it has to be on * the correct MIGRATE_ISOLATE freelist. There is no * simple way to verify that as VM_BUG_ON(), though. |
435b405c0 memory-hotplug: f... |
253 |
*/ |
ab130f910 mm: rename page_o... |
254 |
pfn += 1 << buddy_order(page); |
756d25be4 mm/page_isolation... |
255 |
else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page)) |
aa016d145 mm, page_isolatio... |
256 |
/* A HWPoisoned page cannot be also PageBuddy */ |
b023f4681 memory-hotplug: s... |
257 |
pfn++; |
aa218795c mm: Allow to offl... |
258 259 260 261 262 263 264 265 |
else if ((flags & MEMORY_OFFLINE) && PageOffline(page) && !page_count(page)) /* * The responsible driver agreed to skip PageOffline() * pages when offlining memory by dropping its * reference in MEM_GOING_OFFLINE. */ pfn++; |
a5d76b54a memory unplug: pa... |
266 267 268 |
else break; } |
fea85cff1 mm/page_isolation... |
269 270 |
return pfn; |
a5d76b54a memory unplug: pa... |
271 |
} |
b9eb63191 mm/memory_hotplug... |
272 |
/* Caller should ensure that requested range is in a single zone */ |
b023f4681 memory-hotplug: s... |
273 |
int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn, |
756d25be4 mm/page_isolation... |
274 |
int isol_flags) |
a5d76b54a memory unplug: pa... |
275 |
{ |
6c1b7f680 memory hotplug: m... |
276 |
unsigned long pfn, flags; |
a5d76b54a memory unplug: pa... |
277 |
struct page *page; |
6c1b7f680 memory hotplug: m... |
278 |
struct zone *zone; |
a5d76b54a memory unplug: pa... |
279 |
|
a5d76b54a memory unplug: pa... |
280 |
/* |
85dbe7060 page_isolation: F... |
281 282 283 |
* Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages * are not aligned to pageblock_nr_pages. * Then we just check migratetype first. |
a5d76b54a memory unplug: pa... |
284 285 286 |
*/ for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { page = __first_valid_page(pfn, pageblock_nr_pages); |
bbf9ce971 mm: use is_migrat... |
287 |
if (page && !is_migrate_isolate_page(page)) |
a5d76b54a memory unplug: pa... |
288 289 |
break; } |
a70dcb969 memory hotplug: f... |
290 291 |
page = __first_valid_page(start_pfn, end_pfn - start_pfn); if ((pfn < end_pfn) || !page) |
a5d76b54a memory unplug: pa... |
292 |
return -EBUSY; |
85dbe7060 page_isolation: F... |
293 |
/* Check all pages are free or marked as ISOLATED */ |
a70dcb969 memory hotplug: f... |
294 |
zone = page_zone(page); |
6c1b7f680 memory hotplug: m... |
295 |
spin_lock_irqsave(&zone->lock, flags); |
756d25be4 mm/page_isolation... |
296 |
pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags); |
6c1b7f680 memory hotplug: m... |
297 |
spin_unlock_irqrestore(&zone->lock, flags); |
fea85cff1 mm/page_isolation... |
298 |
|
0f0848e51 mm/page_isolation... |
299 |
trace_test_pages_isolated(start_pfn, end_pfn, pfn); |
fea85cff1 mm/page_isolation... |
300 |
return pfn < end_pfn ? -EBUSY : 0; |
a5d76b54a memory unplug: pa... |
301 |
} |