1 // SPDX-License-Identifier: GPL-2.0
3 * linux/mm/page_isolation.c
7 #include <linux/page-isolation.h>
8 #include <linux/pageblock-flags.h>
9 #include <linux/memory.h>
10 #include <linux/hugetlb.h>
11 #include <linux/page_owner.h>
12 #include <linux/migrate.h>
15 #define CREATE_TRACE_POINTS
16 #include <trace/events/page_isolation.h>
18 static int set_migratetype_isolate(struct page
*page
, int migratetype
, int isol_flags
)
21 unsigned long flags
, pfn
;
22 struct memory_isolate_notify arg
;
26 zone
= page_zone(page
);
28 spin_lock_irqsave(&zone
->lock
, flags
);
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
35 if (is_migrate_isolate_page(page
))
38 pfn
= page_to_pfn(page
);
40 arg
.nr_pages
= pageblock_nr_pages
;
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.
54 notifier_ret
= memory_isolate_notify(MEM_ISOLATE_COUNT
, &arg
);
55 notifier_ret
= notifier_to_errno(notifier_ret
);
59 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
60 * We just check MOVABLE pages.
62 if (!has_unmovable_pages(zone
, page
, arg
.pages_found
, migratetype
, flags
))
66 * immobile means "not-on-lru" pages. If immobile is larger than
67 * removable-by-driver pages reported by notifier, we'll fail.
72 unsigned long nr_pages
;
73 int mt
= get_pageblock_migratetype(page
);
75 set_pageblock_migratetype(page
, MIGRATE_ISOLATE
);
76 zone
->nr_isolate_pageblock
++;
77 nr_pages
= move_freepages_block(zone
, page
, MIGRATE_ISOLATE
,
80 __mod_zone_freepage_state(zone
, -nr_pages
, mt
);
83 spin_unlock_irqrestore(&zone
->lock
, flags
);
85 drain_all_pages(zone
);
89 static void unset_migratetype_isolate(struct page
*page
, unsigned migratetype
)
92 unsigned long flags
, nr_pages
;
93 bool isolated_page
= false;
95 unsigned long pfn
, buddy_pfn
;
98 zone
= page_zone(page
);
99 spin_lock_irqsave(&zone
->lock
, flags
);
100 if (!is_migrate_isolate_page(page
))
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.
111 if (PageBuddy(page
)) {
112 order
= page_order(page
);
113 if (order
>= pageblock_order
) {
114 pfn
= page_to_pfn(page
);
115 buddy_pfn
= __find_buddy_pfn(pfn
, order
);
116 buddy
= page
+ (buddy_pfn
- pfn
);
118 if (pfn_valid_within(buddy_pfn
) &&
119 !is_migrate_isolate_page(buddy
)) {
120 __isolate_free_page(page
, order
);
121 isolated_page
= true;
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.
131 if (!isolated_page
) {
132 nr_pages
= move_freepages_block(zone
, page
, migratetype
, NULL
);
133 __mod_zone_freepage_state(zone
, nr_pages
, migratetype
);
135 set_pageblock_migratetype(page
, migratetype
);
136 zone
->nr_isolate_pageblock
--;
138 spin_unlock_irqrestore(&zone
->lock
, flags
);
140 post_alloc_hook(page
, order
, __GFP_MOVABLE
);
141 __free_pages(page
, order
);
145 static inline struct page
*
146 __first_valid_page(unsigned long pfn
, unsigned long nr_pages
)
150 for (i
= 0; i
< nr_pages
; i
++) {
153 if (!pfn_valid_within(pfn
+ i
))
155 page
= pfn_to_online_page(pfn
+ i
);
164 * start_isolate_page_range() -- make page-allocation-type of range of pages
165 * to 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 * @migratetype: migrate type to set in error recovery.
170 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
171 * the range will never be allocated. Any free pages and pages freed in the
172 * future will not be allocated again.
174 * start_pfn/end_pfn must be aligned to pageblock_order.
175 * Return 0 on success and -EBUSY if any part of range cannot be isolated.
177 * There is no high level synchronization mechanism that prevents two threads
178 * from trying to isolate overlapping ranges. If this happens, one thread
179 * will notice pageblocks in the overlapping range already set to isolate.
180 * This happens in set_migratetype_isolate, and set_migratetype_isolate
181 * returns an error. We then clean up by restoring the migration type on
182 * pageblocks we may have modified and return -EBUSY to caller. This
183 * prevents two threads from simultaneously working on overlapping ranges.
185 int start_isolate_page_range(unsigned long start_pfn
, unsigned long end_pfn
,
186 unsigned migratetype
, int flags
)
189 unsigned long undo_pfn
;
192 BUG_ON(!IS_ALIGNED(start_pfn
, pageblock_nr_pages
));
193 BUG_ON(!IS_ALIGNED(end_pfn
, pageblock_nr_pages
));
195 for (pfn
= start_pfn
;
197 pfn
+= pageblock_nr_pages
) {
198 page
= __first_valid_page(pfn
, pageblock_nr_pages
);
200 set_migratetype_isolate(page
, migratetype
, flags
)) {
207 for (pfn
= start_pfn
;
209 pfn
+= pageblock_nr_pages
) {
210 struct page
*page
= pfn_to_online_page(pfn
);
213 unset_migratetype_isolate(page
, migratetype
);
220 * Make isolated pages available again.
222 int undo_isolate_page_range(unsigned long start_pfn
, unsigned long end_pfn
,
223 unsigned migratetype
)
228 BUG_ON(!IS_ALIGNED(start_pfn
, pageblock_nr_pages
));
229 BUG_ON(!IS_ALIGNED(end_pfn
, pageblock_nr_pages
));
231 for (pfn
= start_pfn
;
233 pfn
+= pageblock_nr_pages
) {
234 page
= __first_valid_page(pfn
, pageblock_nr_pages
);
235 if (!page
|| !is_migrate_isolate_page(page
))
237 unset_migratetype_isolate(page
, migratetype
);
242 * Test all pages in the range is free(means isolated) or not.
243 * all pages in [start_pfn...end_pfn) must be in the same zone.
244 * zone->lock must be held before call this.
246 * Returns the last tested pfn.
249 __test_page_isolated_in_pageblock(unsigned long pfn
, unsigned long end_pfn
,
250 bool skip_hwpoisoned_pages
)
254 while (pfn
< end_pfn
) {
255 if (!pfn_valid_within(pfn
)) {
259 page
= pfn_to_page(pfn
);
262 * If the page is on a free list, it has to be on
263 * the correct MIGRATE_ISOLATE freelist. There is no
264 * simple way to verify that as VM_BUG_ON(), though.
266 pfn
+= 1 << page_order(page
);
267 else if (skip_hwpoisoned_pages
&& PageHWPoison(page
))
268 /* A HWPoisoned page cannot be also PageBuddy */
277 /* Caller should ensure that requested range is in a single zone */
278 int test_pages_isolated(unsigned long start_pfn
, unsigned long end_pfn
,
279 bool skip_hwpoisoned_pages
)
281 unsigned long pfn
, flags
;
286 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
287 * are not aligned to pageblock_nr_pages.
288 * Then we just check migratetype first.
290 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= pageblock_nr_pages
) {
291 page
= __first_valid_page(pfn
, pageblock_nr_pages
);
292 if (page
&& !is_migrate_isolate_page(page
))
295 page
= __first_valid_page(start_pfn
, end_pfn
- start_pfn
);
296 if ((pfn
< end_pfn
) || !page
)
298 /* Check all pages are free or marked as ISOLATED */
299 zone
= page_zone(page
);
300 spin_lock_irqsave(&zone
->lock
, flags
);
301 pfn
= __test_page_isolated_in_pageblock(start_pfn
, end_pfn
,
302 skip_hwpoisoned_pages
);
303 spin_unlock_irqrestore(&zone
->lock
, flags
);
305 trace_test_pages_isolated(start_pfn
, end_pfn
, pfn
);
307 return pfn
< end_pfn
? -EBUSY
: 0;
310 struct page
*alloc_migrate_target(struct page
*page
, unsigned long private)
312 return new_page_nodemask(page
, numa_node_id(), &node_states
[N_MEMORY
]);