blob: a9fd7c740c23894bc94e57fc380778880f923722 [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{
Qian Cai4a55c042020-01-30 22:14:57 -080020 struct page *unmovable = NULL;
Minchan Kimee6f5092012-07-31 16:43:50 -070021 struct zone *zone;
David Hildenbrand3f9903b2020-01-30 22:14:01 -080022 unsigned long flags;
Minchan Kimee6f5092012-07-31 16:43:50 -070023 int ret = -EBUSY;
24
25 zone = page_zone(page);
26
27 spin_lock_irqsave(&zone->lock, flags);
28
Mike Kravetz2c7452a2018-04-05 16:25:26 -070029 /*
30 * We assume the caller intended to SET migrate type to isolate.
31 * If it is already set, then someone else must have raced and
32 * set it before us. Return -EBUSY
33 */
34 if (is_migrate_isolate_page(page))
35 goto out;
36
Minchan Kimee6f5092012-07-31 16:43:50 -070037 /*
38 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
39 * We just check MOVABLE pages.
40 */
Qian Cai4a55c042020-01-30 22:14:57 -080041 unmovable = has_unmovable_pages(zone, page, migratetype, isol_flags);
42 if (!unmovable) {
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070043 unsigned long nr_pages;
Michal Hocko4da2ce22017-11-15 17:33:26 -080044 int mt = get_pageblock_migratetype(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070045
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -080046 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
Joonsoo Kimad53f922014-11-13 15:19:11 -080047 zone->nr_isolate_pageblock++;
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -070048 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
49 NULL);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070050
Michal Hocko4da2ce22017-11-15 17:33:26 -080051 __mod_zone_freepage_state(zone, -nr_pages, mt);
David Hildenbrand3f9903b2020-01-30 22:14:01 -080052 ret = 0;
Minchan Kimee6f5092012-07-31 16:43:50 -070053 }
54
David Hildenbrand3f9903b2020-01-30 22:14:01 -080055out:
Minchan Kimee6f5092012-07-31 16:43:50 -070056 spin_unlock_irqrestore(&zone->lock, flags);
Qian Cai3d680bd2020-01-30 22:15:01 -080057 if (!ret) {
Vlastimil Babkaec25af82014-12-10 15:43:04 -080058 drain_all_pages(zone);
Qian Cai3d680bd2020-01-30 22:15:01 -080059 } else {
60 WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
61
62 if ((isol_flags & REPORT_FAILURE) && unmovable)
63 /*
64 * printk() with zone->lock held will likely trigger a
65 * lockdep splat, so defer it here.
66 */
67 dump_page(unmovable, "unmovable page");
68 }
Qian Cai4a55c042020-01-30 22:14:57 -080069
Minchan Kimee6f5092012-07-31 16:43:50 -070070 return ret;
71}
72
Naoya Horiguchic5b4e1b2015-09-08 15:02:09 -070073static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
Minchan Kimee6f5092012-07-31 16:43:50 -070074{
75 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070076 unsigned long flags, nr_pages;
Joonsoo Kime3a27132016-07-26 15:24:01 -070077 bool isolated_page = false;
Joonsoo Kim3c605092014-11-13 15:19:21 -080078 unsigned int order;
Vlastimil Babka76741e72017-02-22 15:41:48 -080079 unsigned long pfn, buddy_pfn;
Joonsoo Kim3c605092014-11-13 15:19:21 -080080 struct page *buddy;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070081
Minchan Kimee6f5092012-07-31 16:43:50 -070082 zone = page_zone(page);
83 spin_lock_irqsave(&zone->lock, flags);
Xishi Qiubbf9ce92017-05-03 14:52:55 -070084 if (!is_migrate_isolate_page(page))
Minchan Kimee6f5092012-07-31 16:43:50 -070085 goto out;
Joonsoo Kim3c605092014-11-13 15:19:21 -080086
87 /*
88 * Because freepage with more than pageblock_order on isolated
89 * pageblock is restricted to merge due to freepage counting problem,
90 * it is possible that there is free buddy page.
91 * move_freepages_block() doesn't care of merge so we need other
92 * approach in order to merge them. Isolation and free will make
93 * these pages to be merged.
94 */
95 if (PageBuddy(page)) {
96 order = page_order(page);
97 if (order >= pageblock_order) {
Vlastimil Babka76741e72017-02-22 15:41:48 -080098 pfn = page_to_pfn(page);
99 buddy_pfn = __find_buddy_pfn(pfn, order);
100 buddy = page + (buddy_pfn - pfn);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800101
Vlastimil Babka13ad59d2017-02-22 15:41:51 -0800102 if (pfn_valid_within(buddy_pfn) &&
Hui Zhu1ae70132015-05-14 15:17:04 -0700103 !is_migrate_isolate_page(buddy)) {
Joonsoo Kim3c605092014-11-13 15:19:21 -0800104 __isolate_free_page(page, order);
Joonsoo Kime3a27132016-07-26 15:24:01 -0700105 isolated_page = true;
Joonsoo Kim3c605092014-11-13 15:19:21 -0800106 }
107 }
108 }
109
110 /*
111 * If we isolate freepage with more than pageblock_order, there
112 * should be no freepage in the range, so we could avoid costly
113 * pageblock scanning for freepage moving.
114 */
115 if (!isolated_page) {
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -0700116 nr_pages = move_freepages_block(zone, page, migratetype, NULL);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800117 __mod_zone_freepage_state(zone, nr_pages, migratetype);
118 }
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -0800119 set_pageblock_migratetype(page, migratetype);
Joonsoo Kimad53f922014-11-13 15:19:11 -0800120 zone->nr_isolate_pageblock--;
Minchan Kimee6f5092012-07-31 16:43:50 -0700121out:
122 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kim83358ec2016-07-26 15:23:43 -0700123 if (isolated_page) {
Joonsoo Kim46f24fd2016-07-26 15:23:58 -0700124 post_alloc_hook(page, order, __GFP_MOVABLE);
Joonsoo Kime3a27132016-07-26 15:24:01 -0700125 __free_pages(page, order);
Joonsoo Kim83358ec2016-07-26 15:23:43 -0700126 }
Minchan Kimee6f5092012-07-31 16:43:50 -0700127}
128
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700129static inline struct page *
130__first_valid_page(unsigned long pfn, unsigned long nr_pages)
131{
132 int i;
Michal Hocko2ce13642017-07-06 15:38:04 -0700133
134 for (i = 0; i < nr_pages; i++) {
135 struct page *page;
136
Michal Hocko2ce13642017-07-06 15:38:04 -0700137 page = pfn_to_online_page(pfn + i);
138 if (!page)
139 continue;
140 return page;
141 }
142 return NULL;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700143}
144
Qian Cai9b7ea462019-03-28 20:43:34 -0700145/**
146 * start_isolate_page_range() - make page-allocation-type of range of pages to
147 * be MIGRATE_ISOLATE.
148 * @start_pfn: The lower PFN of the range to be isolated.
149 * @end_pfn: The upper PFN of the range to be isolated.
150 * start_pfn/end_pfn must be aligned to pageblock_order.
151 * @migratetype: Migrate type to set in error recovery.
152 * @flags: The following flags are allowed (they can be combined in
153 * a bit mask)
David Hildenbrand756d25b2019-11-30 17:54:07 -0800154 * MEMORY_OFFLINE - isolate to offline (!allocate) memory
155 * e.g., skip over PageHWPoison() pages
Qian Cai9b7ea462019-03-28 20:43:34 -0700156 * REPORT_FAILURE - report details about the failure to
157 * isolate the range
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700158 *
159 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
160 * the range will never be allocated. Any free pages and pages freed in the
Qian Cai9b7ea462019-03-28 20:43:34 -0700161 * future will not be allocated again. If specified range includes migrate types
162 * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
163 * pages in the range finally, the caller have to free all pages in the range.
164 * test_page_isolated() can be used for test it.
Mike Kravetz2c7452a2018-04-05 16:25:26 -0700165 *
166 * There is no high level synchronization mechanism that prevents two threads
Qian Cai9b7ea462019-03-28 20:43:34 -0700167 * from trying to isolate overlapping ranges. If this happens, one thread
Mike Kravetz2c7452a2018-04-05 16:25:26 -0700168 * will notice pageblocks in the overlapping range already set to isolate.
169 * This happens in set_migratetype_isolate, and set_migratetype_isolate
Qian Cai9b7ea462019-03-28 20:43:34 -0700170 * returns an error. We then clean up by restoring the migration type on
171 * pageblocks we may have modified and return -EBUSY to caller. This
Mike Kravetz2c7452a2018-04-05 16:25:26 -0700172 * prevents two threads from simultaneously working on overlapping ranges.
Qian Cai9b7ea462019-03-28 20:43:34 -0700173 *
174 * Return: the number of isolated pageblocks on success and -EBUSY if any part
175 * of range cannot be isolated.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700176 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200177int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
Michal Hockod381c542018-12-28 00:33:56 -0800178 unsigned migratetype, int flags)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700179{
180 unsigned long pfn;
181 unsigned long undo_pfn;
182 struct page *page;
Qian Cai9b7ea462019-03-28 20:43:34 -0700183 int nr_isolate_pageblock = 0;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700184
Naoya Horiguchifec174d2016-01-14 15:22:13 -0800185 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
186 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700187
188 for (pfn = start_pfn;
189 pfn < end_pfn;
190 pfn += pageblock_nr_pages) {
191 page = __first_valid_page(pfn, pageblock_nr_pages);
Qian Cai9b7ea462019-03-28 20:43:34 -0700192 if (page) {
193 if (set_migratetype_isolate(page, migratetype, flags)) {
194 undo_pfn = pfn;
195 goto undo;
196 }
197 nr_isolate_pageblock++;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700198 }
199 }
Qian Cai9b7ea462019-03-28 20:43:34 -0700200 return nr_isolate_pageblock;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700201undo:
202 for (pfn = start_pfn;
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800203 pfn < undo_pfn;
Michal Hocko2ce13642017-07-06 15:38:04 -0700204 pfn += pageblock_nr_pages) {
205 struct page *page = pfn_to_online_page(pfn);
206 if (!page)
207 continue;
208 unset_migratetype_isolate(page, migratetype);
209 }
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700210
211 return -EBUSY;
212}
213
214/*
215 * Make isolated pages available again.
216 */
Pingfan Liu1fcf0a52019-07-11 20:54:49 -0700217void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200218 unsigned migratetype)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700219{
220 unsigned long pfn;
221 struct page *page;
Wang Xiaoqiang6f8d2b82016-01-15 16:57:13 -0800222
223 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
224 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
225
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700226 for (pfn = start_pfn;
227 pfn < end_pfn;
228 pfn += pageblock_nr_pages) {
229 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700230 if (!page || !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700231 continue;
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200232 unset_migratetype_isolate(page, migratetype);
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700233 }
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700234}
235/*
236 * Test all pages in the range is free(means isolated) or not.
237 * all pages in [start_pfn...end_pfn) must be in the same zone.
238 * zone->lock must be held before call this.
239 *
Neil Zhangec3b6882016-04-01 14:31:37 -0700240 * Returns the last tested pfn.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700241 */
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800242static unsigned long
Wen Congyangb023f462012-12-11 16:00:45 -0800243__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
David Hildenbrand756d25b2019-11-30 17:54:07 -0800244 int flags)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700245{
246 struct page *page;
247
248 while (pfn < end_pfn) {
249 if (!pfn_valid_within(pfn)) {
250 pfn++;
251 continue;
252 }
253 page = pfn_to_page(pfn);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700254 if (PageBuddy(page))
Minchan Kim435b4052012-10-08 16:32:16 -0700255 /*
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700256 * If the page is on a free list, it has to be on
257 * the correct MIGRATE_ISOLATE freelist. There is no
258 * simple way to verify that as VM_BUG_ON(), though.
Minchan Kim435b4052012-10-08 16:32:16 -0700259 */
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700260 pfn += 1 << page_order(page);
David Hildenbrand756d25b2019-11-30 17:54:07 -0800261 else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700262 /* A HWPoisoned page cannot be also PageBuddy */
Wen Congyangb023f462012-12-11 16:00:45 -0800263 pfn++;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700264 else
265 break;
266 }
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800267
268 return pfn;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700269}
270
Joonsoo Kimb9eb6312016-05-19 17:12:06 -0700271/* Caller should ensure that requested range is in a single zone */
Wen Congyangb023f462012-12-11 16:00:45 -0800272int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
David Hildenbrand756d25b2019-11-30 17:54:07 -0800273 int isol_flags)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700274{
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700275 unsigned long pfn, flags;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700276 struct page *page;
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700277 struct zone *zone;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700278
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700279 /*
Tang Chen85dbe702013-06-20 18:10:19 +0800280 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
281 * are not aligned to pageblock_nr_pages.
282 * Then we just check migratetype first.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700283 */
284 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
285 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700286 if (page && !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700287 break;
288 }
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800289 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
290 if ((pfn < end_pfn) || !page)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700291 return -EBUSY;
Tang Chen85dbe702013-06-20 18:10:19 +0800292 /* Check all pages are free or marked as ISOLATED */
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800293 zone = page_zone(page);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700294 spin_lock_irqsave(&zone->lock, flags);
David Hildenbrand756d25b2019-11-30 17:54:07 -0800295 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700296 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800297
Joonsoo Kim0f0848e2016-01-14 15:18:42 -0800298 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
299
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800300 return pfn < end_pfn ? -EBUSY : 0;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700301}
Minchan Kim723a0642012-10-08 16:32:52 -0700302
Michal Hocko666feb22018-04-10 16:30:03 -0700303struct page *alloc_migrate_target(struct page *page, unsigned long private)
Minchan Kim723a0642012-10-08 16:32:52 -0700304{
Michal Hocko8b913232017-07-10 15:48:47 -0700305 return new_page_nodemask(page, numa_node_id(), &node_states[N_MEMORY]);
Minchan Kim723a0642012-10-08 16:32:52 -0700306}