blob: bddf788f45bff8cb2f1768dc3930a24355566658 [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{
David Hildenbrand1c31cb42020-10-13 16:55:28 -070020 struct zone *zone = page_zone(page);
21 struct page *unmovable;
David Hildenbrand3f9903b92020-01-30 22:14:01 -080022 unsigned long flags;
Minchan Kimee6f5092012-07-31 16:43:50 -070023
24 spin_lock_irqsave(&zone->lock, flags);
25
Mike Kravetz2c7452a2018-04-05 16:25:26 -070026 /*
27 * We assume the caller intended to SET migrate type to isolate.
28 * If it is already set, then someone else must have raced and
David Hildenbrand51030a52020-10-13 16:55:21 -070029 * set it before us.
Mike Kravetz2c7452a2018-04-05 16:25:26 -070030 */
David Hildenbrand51030a52020-10-13 16:55:21 -070031 if (is_migrate_isolate_page(page)) {
32 spin_unlock_irqrestore(&zone->lock, flags);
33 return -EBUSY;
34 }
Mike Kravetz2c7452a2018-04-05 16:25:26 -070035
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 */
Qian Cai4a55c042020-01-30 22:14:57 -080040 unmovable = has_unmovable_pages(zone, page, migratetype, isol_flags);
41 if (!unmovable) {
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070042 unsigned long nr_pages;
Michal Hocko4da2ce22017-11-15 17:33:26 -080043 int mt = get_pageblock_migratetype(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070044
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -080045 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
Joonsoo Kimad53f922014-11-13 15:19:11 -080046 zone->nr_isolate_pageblock++;
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -070047 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
48 NULL);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070049
Michal Hocko4da2ce22017-11-15 17:33:26 -080050 __mod_zone_freepage_state(zone, -nr_pages, mt);
David Hildenbrand1c31cb42020-10-13 16:55:28 -070051 spin_unlock_irqrestore(&zone->lock, flags);
David Hildenbrand1c31cb42020-10-13 16:55:28 -070052 return 0;
Minchan Kimee6f5092012-07-31 16:43:50 -070053 }
54
55 spin_unlock_irqrestore(&zone->lock, flags);
David Hildenbrand1c31cb42020-10-13 16:55:28 -070056 if (isol_flags & REPORT_FAILURE) {
David Hildenbrand48381d72020-10-13 16:55:24 -070057 /*
58 * printk() with zone->lock held will likely trigger a
59 * lockdep splat, so defer it here.
60 */
61 dump_page(unmovable, "unmovable page");
Qian Cai3d680bd2020-01-30 22:15:01 -080062 }
Qian Cai4a55c042020-01-30 22:14:57 -080063
David Hildenbrand1c31cb42020-10-13 16:55:28 -070064 return -EBUSY;
Minchan Kimee6f5092012-07-31 16:43:50 -070065}
66
Naoya Horiguchic5b4e1b2015-09-08 15:02:09 -070067static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
Minchan Kimee6f5092012-07-31 16:43:50 -070068{
69 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070070 unsigned long flags, nr_pages;
Joonsoo Kime3a27132016-07-26 15:24:01 -070071 bool isolated_page = false;
Joonsoo Kim3c605092014-11-13 15:19:21 -080072 unsigned int order;
Vlastimil Babka76741e72017-02-22 15:41:48 -080073 unsigned long pfn, buddy_pfn;
Joonsoo Kim3c605092014-11-13 15:19:21 -080074 struct page *buddy;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070075
Minchan Kimee6f5092012-07-31 16:43:50 -070076 zone = page_zone(page);
77 spin_lock_irqsave(&zone->lock, flags);
Xishi Qiubbf9ce92017-05-03 14:52:55 -070078 if (!is_migrate_isolate_page(page))
Minchan Kimee6f5092012-07-31 16:43:50 -070079 goto out;
Joonsoo Kim3c605092014-11-13 15:19:21 -080080
81 /*
82 * Because freepage with more than pageblock_order on isolated
83 * pageblock is restricted to merge due to freepage counting problem,
84 * it is possible that there is free buddy page.
85 * move_freepages_block() doesn't care of merge so we need other
86 * approach in order to merge them. Isolation and free will make
87 * these pages to be merged.
88 */
89 if (PageBuddy(page)) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -070090 order = buddy_order(page);
Muchun Song2484be02020-12-14 19:12:27 -080091 if (order >= pageblock_order && order < MAX_ORDER - 1) {
Vlastimil Babka76741e72017-02-22 15:41:48 -080092 pfn = page_to_pfn(page);
93 buddy_pfn = __find_buddy_pfn(pfn, order);
94 buddy = page + (buddy_pfn - pfn);
Joonsoo Kim3c605092014-11-13 15:19:21 -080095
Vlastimil Babka13ad59d2017-02-22 15:41:51 -080096 if (pfn_valid_within(buddy_pfn) &&
Hui Zhu1ae70132015-05-14 15:17:04 -070097 !is_migrate_isolate_page(buddy)) {
Joonsoo Kim3c605092014-11-13 15:19:21 -080098 __isolate_free_page(page, order);
Joonsoo Kime3a27132016-07-26 15:24:01 -070099 isolated_page = true;
Joonsoo Kim3c605092014-11-13 15:19:21 -0800100 }
101 }
102 }
103
104 /*
105 * If we isolate freepage with more than pageblock_order, there
106 * should be no freepage in the range, so we could avoid costly
107 * pageblock scanning for freepage moving.
David Hildenbrand293ffa52020-10-15 20:09:30 -0700108 *
109 * We didn't actually touch any of the isolated pages, so place them
110 * to the tail of the freelist. This is an optimization for memory
111 * onlining - just onlined memory won't immediately be considered for
112 * allocation.
Joonsoo Kim3c605092014-11-13 15:19:21 -0800113 */
114 if (!isolated_page) {
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -0700115 nr_pages = move_freepages_block(zone, page, migratetype, NULL);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800116 __mod_zone_freepage_state(zone, nr_pages, migratetype);
117 }
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -0800118 set_pageblock_migratetype(page, migratetype);
Alexander Duyck624f58d2020-04-06 20:04:53 -0700119 if (isolated_page)
120 __putback_isolated_page(page, order, migratetype);
Joonsoo Kimad53f922014-11-13 15:19:11 -0800121 zone->nr_isolate_pageblock--;
Minchan Kimee6f5092012-07-31 16:43:50 -0700122out:
123 spin_unlock_irqrestore(&zone->lock, flags);
124}
125
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700126static inline struct page *
127__first_valid_page(unsigned long pfn, unsigned long nr_pages)
128{
129 int i;
Michal Hocko2ce13642017-07-06 15:38:04 -0700130
131 for (i = 0; i < nr_pages; i++) {
132 struct page *page;
133
Michal Hocko2ce13642017-07-06 15:38:04 -0700134 page = pfn_to_online_page(pfn + i);
135 if (!page)
136 continue;
137 return page;
138 }
139 return NULL;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700140}
141
Qian Cai9b7ea462019-03-28 20:43:34 -0700142/**
143 * start_isolate_page_range() - make page-allocation-type of range of pages to
144 * be MIGRATE_ISOLATE.
145 * @start_pfn: The lower PFN of the range to be isolated.
146 * @end_pfn: The upper PFN of the range to be isolated.
147 * start_pfn/end_pfn must be aligned to pageblock_order.
148 * @migratetype: Migrate type to set in error recovery.
149 * @flags: The following flags are allowed (they can be combined in
150 * a bit mask)
David Hildenbrand756d25b2019-11-30 17:54:07 -0800151 * MEMORY_OFFLINE - isolate to offline (!allocate) memory
152 * e.g., skip over PageHWPoison() pages
David Hildenbrandaa2187952020-05-07 16:01:30 +0200153 * and PageOffline() pages.
Qian Cai9b7ea462019-03-28 20:43:34 -0700154 * REPORT_FAILURE - report details about the failure to
155 * isolate the range
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700156 *
157 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
158 * the range will never be allocated. Any free pages and pages freed in the
Qian Cai9b7ea462019-03-28 20:43:34 -0700159 * future will not be allocated again. If specified range includes migrate types
160 * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
161 * pages in the range finally, the caller have to free all pages in the range.
162 * test_page_isolated() can be used for test it.
Mike Kravetz2c7452a2018-04-05 16:25:26 -0700163 *
164 * There is no high level synchronization mechanism that prevents two threads
Qian Cai9b7ea462019-03-28 20:43:34 -0700165 * from trying to isolate overlapping ranges. If this happens, one thread
Mike Kravetz2c7452a2018-04-05 16:25:26 -0700166 * will notice pageblocks in the overlapping range already set to isolate.
167 * This happens in set_migratetype_isolate, and set_migratetype_isolate
Qian Cai9b7ea462019-03-28 20:43:34 -0700168 * returns an error. We then clean up by restoring the migration type on
169 * pageblocks we may have modified and return -EBUSY to caller. This
Mike Kravetz2c7452a2018-04-05 16:25:26 -0700170 * prevents two threads from simultaneously working on overlapping ranges.
Qian Cai9b7ea462019-03-28 20:43:34 -0700171 *
Pavel Tatashin96831822020-09-18 21:20:31 -0700172 * Please note that there is no strong synchronization with the page allocator
173 * either. Pages might be freed while their page blocks are marked ISOLATED.
Vlastimil Babka76129212020-12-14 19:10:56 -0800174 * A call to drain_all_pages() after isolation can flush most of them. However
175 * in some cases pages might still end up on pcp lists and that would allow
Pavel Tatashin96831822020-09-18 21:20:31 -0700176 * for their allocation even when they are in fact isolated already. Depending
Vlastimil Babkaec6e8c7e2020-12-14 19:10:59 -0800177 * on how strong of a guarantee the caller needs, zone_pcp_disable/enable()
178 * might be used to flush and disable pcplist before isolation and enable after
179 * unisolation.
Pavel Tatashin96831822020-09-18 21:20:31 -0700180 *
David Hildenbrand3fa0c7c2020-10-15 20:08:07 -0700181 * Return: 0 on success and -EBUSY if any part of range cannot be isolated.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700182 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200183int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
Michal Hockod381c542018-12-28 00:33:56 -0800184 unsigned migratetype, int flags)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700185{
186 unsigned long pfn;
187 unsigned long undo_pfn;
188 struct page *page;
189
Naoya Horiguchifec174d2016-01-14 15:22:13 -0800190 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
191 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700192
193 for (pfn = start_pfn;
194 pfn < end_pfn;
195 pfn += pageblock_nr_pages) {
196 page = __first_valid_page(pfn, pageblock_nr_pages);
Qian Cai9b7ea462019-03-28 20:43:34 -0700197 if (page) {
198 if (set_migratetype_isolate(page, migratetype, flags)) {
199 undo_pfn = pfn;
200 goto undo;
201 }
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700202 }
203 }
David Hildenbrand3fa0c7c2020-10-15 20:08:07 -0700204 return 0;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700205undo:
206 for (pfn = start_pfn;
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800207 pfn < undo_pfn;
Michal Hocko2ce13642017-07-06 15:38:04 -0700208 pfn += pageblock_nr_pages) {
209 struct page *page = pfn_to_online_page(pfn);
210 if (!page)
211 continue;
212 unset_migratetype_isolate(page, migratetype);
213 }
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700214
215 return -EBUSY;
216}
217
218/*
219 * Make isolated pages available again.
220 */
Pingfan Liu1fcf0a52019-07-11 20:54:49 -0700221void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200222 unsigned migratetype)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700223{
224 unsigned long pfn;
225 struct page *page;
Wang Xiaoqiang6f8d2b82016-01-15 16:57:13 -0800226
227 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
228 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
229
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700230 for (pfn = start_pfn;
231 pfn < end_pfn;
232 pfn += pageblock_nr_pages) {
233 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700234 if (!page || !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700235 continue;
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200236 unset_migratetype_isolate(page, migratetype);
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700237 }
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700238}
239/*
240 * Test all pages in the range is free(means isolated) or not.
241 * all pages in [start_pfn...end_pfn) must be in the same zone.
242 * zone->lock must be held before call this.
243 *
Neil Zhangec3b6882016-04-01 14:31:37 -0700244 * Returns the last tested pfn.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700245 */
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800246static unsigned long
Wen Congyangb023f462012-12-11 16:00:45 -0800247__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
David Hildenbrand756d25b2019-11-30 17:54:07 -0800248 int flags)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700249{
250 struct page *page;
251
252 while (pfn < end_pfn) {
253 if (!pfn_valid_within(pfn)) {
254 pfn++;
255 continue;
256 }
257 page = pfn_to_page(pfn);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700258 if (PageBuddy(page))
Minchan Kim435b4052012-10-08 16:32:16 -0700259 /*
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700260 * If the page is on a free list, it has to be on
261 * the correct MIGRATE_ISOLATE freelist. There is no
262 * simple way to verify that as VM_BUG_ON(), though.
Minchan Kim435b4052012-10-08 16:32:16 -0700263 */
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700264 pfn += 1 << buddy_order(page);
David Hildenbrand756d25b2019-11-30 17:54:07 -0800265 else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700266 /* A HWPoisoned page cannot be also PageBuddy */
Wen Congyangb023f462012-12-11 16:00:45 -0800267 pfn++;
David Hildenbrandaa2187952020-05-07 16:01:30 +0200268 else if ((flags & MEMORY_OFFLINE) && PageOffline(page) &&
269 !page_count(page))
270 /*
271 * The responsible driver agreed to skip PageOffline()
272 * pages when offlining memory by dropping its
273 * reference in MEM_GOING_OFFLINE.
274 */
275 pfn++;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700276 else
277 break;
278 }
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800279
280 return pfn;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700281}
282
Joonsoo Kimb9eb6312016-05-19 17:12:06 -0700283/* Caller should ensure that requested range is in a single zone */
Wen Congyangb023f462012-12-11 16:00:45 -0800284int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
David Hildenbrand756d25b2019-11-30 17:54:07 -0800285 int isol_flags)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700286{
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700287 unsigned long pfn, flags;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700288 struct page *page;
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700289 struct zone *zone;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700290
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700291 /*
Tang Chen85dbe702013-06-20 18:10:19 +0800292 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
293 * are not aligned to pageblock_nr_pages.
294 * Then we just check migratetype first.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700295 */
296 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
297 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700298 if (page && !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700299 break;
300 }
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800301 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
302 if ((pfn < end_pfn) || !page)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700303 return -EBUSY;
Tang Chen85dbe702013-06-20 18:10:19 +0800304 /* Check all pages are free or marked as ISOLATED */
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800305 zone = page_zone(page);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700306 spin_lock_irqsave(&zone->lock, flags);
David Hildenbrand756d25b2019-11-30 17:54:07 -0800307 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700308 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800309
Joonsoo Kim0f0848e2016-01-14 15:18:42 -0800310 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
311
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800312 return pfn < end_pfn ? -EBUSY : 0;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700313}