blob: 21af88b718aaac534d0507ebe95ff9c3ddc1c9d7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -07002/*
3 * linux/mm/page_isolation.c
4 */
5
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -07006#include <linux/mm.h>
7#include <linux/page-isolation.h>
8#include <linux/pageblock-flags.h>
Minchan Kimee6f5092012-07-31 16:43:50 -07009#include <linux/memory.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070010#include <linux/hugetlb.h>
Joonsoo Kim83358ec2016-07-26 15:23:43 -070011#include <linux/page_owner.h>
Michal Hocko8b913232017-07-10 15:48:47 -070012#include <linux/migrate.h>
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -070013#include "internal.h"
14
Joonsoo Kim0f0848e2016-01-14 15:18:42 -080015#define CREATE_TRACE_POINTS
16#include <trace/events/page_isolation.h>
17
Michal Hockod381c542018-12-28 00:33:56 -080018static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
Minchan Kimee6f5092012-07-31 16:43:50 -070019{
20 struct zone *zone;
David Hildenbrand3f9903b2020-01-30 22:14:01 -080021 unsigned long flags;
Minchan Kimee6f5092012-07-31 16:43:50 -070022 int ret = -EBUSY;
23
24 zone = page_zone(page);
25
26 spin_lock_irqsave(&zone->lock, flags);
27
Mike Kravetz2c7452a2018-04-05 16:25:26 -070028 /*
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
Minchan Kimee6f5092012-07-31 16:43:50 -070036 /*
37 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
38 * We just check MOVABLE pages.
39 */
David Hildenbrand3f9903b2020-01-30 22:14:01 -080040 if (!has_unmovable_pages(zone, page, 0, migratetype, isol_flags)) {
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070041 unsigned long nr_pages;
Michal Hocko4da2ce22017-11-15 17:33:26 -080042 int mt = get_pageblock_migratetype(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070043
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -080044 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
Joonsoo Kimad53f922014-11-13 15:19:11 -080045 zone->nr_isolate_pageblock++;
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -070046 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
47 NULL);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070048
Michal Hocko4da2ce22017-11-15 17:33:26 -080049 __mod_zone_freepage_state(zone, -nr_pages, mt);
David Hildenbrand3f9903b2020-01-30 22:14:01 -080050 ret = 0;
Minchan Kimee6f5092012-07-31 16:43:50 -070051 }
52
David Hildenbrand3f9903b2020-01-30 22:14:01 -080053out:
Minchan Kimee6f5092012-07-31 16:43:50 -070054 spin_unlock_irqrestore(&zone->lock, flags);
55 if (!ret)
Vlastimil Babkaec25af82014-12-10 15:43:04 -080056 drain_all_pages(zone);
Minchan Kimee6f5092012-07-31 16:43:50 -070057 return ret;
58}
59
Naoya Horiguchic5b4e1b2015-09-08 15:02:09 -070060static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
Minchan Kimee6f5092012-07-31 16:43:50 -070061{
62 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070063 unsigned long flags, nr_pages;
Joonsoo Kime3a27132016-07-26 15:24:01 -070064 bool isolated_page = false;
Joonsoo Kim3c605092014-11-13 15:19:21 -080065 unsigned int order;
Vlastimil Babka76741e72017-02-22 15:41:48 -080066 unsigned long pfn, buddy_pfn;
Joonsoo Kim3c605092014-11-13 15:19:21 -080067 struct page *buddy;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070068
Minchan Kimee6f5092012-07-31 16:43:50 -070069 zone = page_zone(page);
70 spin_lock_irqsave(&zone->lock, flags);
Xishi Qiubbf9ce92017-05-03 14:52:55 -070071 if (!is_migrate_isolate_page(page))
Minchan Kimee6f5092012-07-31 16:43:50 -070072 goto out;
Joonsoo Kim3c605092014-11-13 15:19:21 -080073
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) {
Vlastimil Babka76741e72017-02-22 15:41:48 -080085 pfn = page_to_pfn(page);
86 buddy_pfn = __find_buddy_pfn(pfn, order);
87 buddy = page + (buddy_pfn - pfn);
Joonsoo Kim3c605092014-11-13 15:19:21 -080088
Vlastimil Babka13ad59d2017-02-22 15:41:51 -080089 if (pfn_valid_within(buddy_pfn) &&
Hui Zhu1ae70132015-05-14 15:17:04 -070090 !is_migrate_isolate_page(buddy)) {
Joonsoo Kim3c605092014-11-13 15:19:21 -080091 __isolate_free_page(page, order);
Joonsoo Kime3a27132016-07-26 15:24:01 -070092 isolated_page = true;
Joonsoo Kim3c605092014-11-13 15:19:21 -080093 }
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) {
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -0700103 nr_pages = move_freepages_block(zone, page, migratetype, NULL);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800104 __mod_zone_freepage_state(zone, nr_pages, migratetype);
105 }
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -0800106 set_pageblock_migratetype(page, migratetype);
Joonsoo Kimad53f922014-11-13 15:19:11 -0800107 zone->nr_isolate_pageblock--;
Minchan Kimee6f5092012-07-31 16:43:50 -0700108out:
109 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kim83358ec2016-07-26 15:23:43 -0700110 if (isolated_page) {
Joonsoo Kim46f24fd2016-07-26 15:23:58 -0700111 post_alloc_hook(page, order, __GFP_MOVABLE);
Joonsoo Kime3a27132016-07-26 15:24:01 -0700112 __free_pages(page, order);
Joonsoo Kim83358ec2016-07-26 15:23:43 -0700113 }
Minchan Kimee6f5092012-07-31 16:43:50 -0700114}
115
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700116static inline struct page *
117__first_valid_page(unsigned long pfn, unsigned long nr_pages)
118{
119 int i;
Michal Hocko2ce13642017-07-06 15:38:04 -0700120
121 for (i = 0; i < nr_pages; i++) {
122 struct page *page;
123
Michal Hocko2ce13642017-07-06 15:38:04 -0700124 page = pfn_to_online_page(pfn + i);
125 if (!page)
126 continue;
127 return page;
128 }
129 return NULL;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700130}
131
Qian Cai9b7ea462019-03-28 20:43:34 -0700132/**
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)
David Hildenbrand756d25b2019-11-30 17:54:07 -0800141 * MEMORY_OFFLINE - isolate to offline (!allocate) memory
142 * e.g., skip over PageHWPoison() pages
Qian Cai9b7ea462019-03-28 20:43:34 -0700143 * REPORT_FAILURE - report details about the failure to
144 * isolate the range
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700145 *
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
Qian Cai9b7ea462019-03-28 20:43:34 -0700148 * 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.
Mike Kravetz2c7452a2018-04-05 16:25:26 -0700152 *
153 * There is no high level synchronization mechanism that prevents two threads
Qian Cai9b7ea462019-03-28 20:43:34 -0700154 * from trying to isolate overlapping ranges. If this happens, one thread
Mike Kravetz2c7452a2018-04-05 16:25:26 -0700155 * will notice pageblocks in the overlapping range already set to isolate.
156 * This happens in set_migratetype_isolate, and set_migratetype_isolate
Qian Cai9b7ea462019-03-28 20:43:34 -0700157 * 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
Mike Kravetz2c7452a2018-04-05 16:25:26 -0700159 * prevents two threads from simultaneously working on overlapping ranges.
Qian Cai9b7ea462019-03-28 20:43:34 -0700160 *
161 * Return: the number of isolated pageblocks on success and -EBUSY if any part
162 * of range cannot be isolated.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700163 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200164int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
Michal Hockod381c542018-12-28 00:33:56 -0800165 unsigned migratetype, int flags)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700166{
167 unsigned long pfn;
168 unsigned long undo_pfn;
169 struct page *page;
Qian Cai9b7ea462019-03-28 20:43:34 -0700170 int nr_isolate_pageblock = 0;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700171
Naoya Horiguchifec174d2016-01-14 15:22:13 -0800172 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
173 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700174
175 for (pfn = start_pfn;
176 pfn < end_pfn;
177 pfn += pageblock_nr_pages) {
178 page = __first_valid_page(pfn, pageblock_nr_pages);
Qian Cai9b7ea462019-03-28 20:43:34 -0700179 if (page) {
180 if (set_migratetype_isolate(page, migratetype, flags)) {
181 undo_pfn = pfn;
182 goto undo;
183 }
184 nr_isolate_pageblock++;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700185 }
186 }
Qian Cai9b7ea462019-03-28 20:43:34 -0700187 return nr_isolate_pageblock;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700188undo:
189 for (pfn = start_pfn;
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800190 pfn < undo_pfn;
Michal Hocko2ce13642017-07-06 15:38:04 -0700191 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 }
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700197
198 return -EBUSY;
199}
200
201/*
202 * Make isolated pages available again.
203 */
Pingfan Liu1fcf0a52019-07-11 20:54:49 -0700204void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200205 unsigned migratetype)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700206{
207 unsigned long pfn;
208 struct page *page;
Wang Xiaoqiang6f8d2b82016-01-15 16:57:13 -0800209
210 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
211 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
212
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700213 for (pfn = start_pfn;
214 pfn < end_pfn;
215 pfn += pageblock_nr_pages) {
216 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700217 if (!page || !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700218 continue;
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200219 unset_migratetype_isolate(page, migratetype);
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700220 }
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700221}
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 *
Neil Zhangec3b6882016-04-01 14:31:37 -0700227 * Returns the last tested pfn.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700228 */
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800229static unsigned long
Wen Congyangb023f462012-12-11 16:00:45 -0800230__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
David Hildenbrand756d25b2019-11-30 17:54:07 -0800231 int flags)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700232{
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);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700241 if (PageBuddy(page))
Minchan Kim435b4052012-10-08 16:32:16 -0700242 /*
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700243 * 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.
Minchan Kim435b4052012-10-08 16:32:16 -0700246 */
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700247 pfn += 1 << page_order(page);
David Hildenbrand756d25b2019-11-30 17:54:07 -0800248 else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700249 /* A HWPoisoned page cannot be also PageBuddy */
Wen Congyangb023f462012-12-11 16:00:45 -0800250 pfn++;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700251 else
252 break;
253 }
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800254
255 return pfn;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700256}
257
Joonsoo Kimb9eb6312016-05-19 17:12:06 -0700258/* Caller should ensure that requested range is in a single zone */
Wen Congyangb023f462012-12-11 16:00:45 -0800259int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
David Hildenbrand756d25b2019-11-30 17:54:07 -0800260 int isol_flags)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700261{
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700262 unsigned long pfn, flags;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700263 struct page *page;
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700264 struct zone *zone;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700265
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700266 /*
Tang Chen85dbe702013-06-20 18:10:19 +0800267 * 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.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700270 */
271 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
272 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700273 if (page && !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700274 break;
275 }
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800276 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
277 if ((pfn < end_pfn) || !page)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700278 return -EBUSY;
Tang Chen85dbe702013-06-20 18:10:19 +0800279 /* Check all pages are free or marked as ISOLATED */
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800280 zone = page_zone(page);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700281 spin_lock_irqsave(&zone->lock, flags);
David Hildenbrand756d25b2019-11-30 17:54:07 -0800282 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700283 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800284
Joonsoo Kim0f0848e2016-01-14 15:18:42 -0800285 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
286
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800287 return pfn < end_pfn ? -EBUSY : 0;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700288}
Minchan Kim723a0642012-10-08 16:32:52 -0700289
Michal Hocko666feb22018-04-10 16:30:03 -0700290struct page *alloc_migrate_target(struct page *page, unsigned long private)
Minchan Kim723a0642012-10-08 16:32:52 -0700291{
Michal Hocko8b913232017-07-10 15:48:47 -0700292 return new_page_nodemask(page, numa_node_id(), &node_states[N_MEMORY]);
Minchan Kim723a0642012-10-08 16:32:52 -0700293}