blob: 165ed8117bd1da371e58f00094017ec2ebcfc790 [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 Hocko4da2ce22017-11-15 17:33:26 -080018static int set_migratetype_isolate(struct page *page, int migratetype,
Naoya Horiguchic5b4e1b2015-09-08 15:02:09 -070019 bool skip_hwpoisoned_pages)
Minchan Kimee6f5092012-07-31 16:43:50 -070020{
21 struct zone *zone;
22 unsigned long flags, pfn;
23 struct memory_isolate_notify arg;
24 int notifier_ret;
25 int ret = -EBUSY;
26
27 zone = page_zone(page);
28
29 spin_lock_irqsave(&zone->lock, flags);
30
31 pfn = page_to_pfn(page);
32 arg.start_pfn = pfn;
33 arg.nr_pages = pageblock_nr_pages;
34 arg.pages_found = 0;
35
36 /*
37 * It may be possible to isolate a pageblock even if the
38 * migratetype is not MIGRATE_MOVABLE. The memory isolation
39 * notifier chain is used by balloon drivers to return the
40 * number of pages in a range that are held by the balloon
41 * driver to shrink memory. If all the pages are accounted for
42 * by balloons, are free, or on the LRU, isolation can continue.
43 * Later, for example, when memory hotplug notifier runs, these
44 * pages reported as "can be isolated" should be isolated(freed)
45 * by the balloon driver through the memory notifier chain.
46 */
47 notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
48 notifier_ret = notifier_to_errno(notifier_ret);
49 if (notifier_ret)
50 goto out;
51 /*
52 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
53 * We just check MOVABLE pages.
54 */
Michal Hocko4da2ce22017-11-15 17:33:26 -080055 if (!has_unmovable_pages(zone, page, arg.pages_found, migratetype,
Wen Congyangb023f462012-12-11 16:00:45 -080056 skip_hwpoisoned_pages))
Minchan Kimee6f5092012-07-31 16:43:50 -070057 ret = 0;
58
59 /*
Yisheng Xieac34dcd2016-10-07 17:01:16 -070060 * immobile means "not-on-lru" pages. If immobile is larger than
Minchan Kimee6f5092012-07-31 16:43:50 -070061 * removable-by-driver pages reported by notifier, we'll fail.
62 */
63
64out:
65 if (!ret) {
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070066 unsigned long nr_pages;
Michal Hocko4da2ce22017-11-15 17:33:26 -080067 int mt = get_pageblock_migratetype(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070068
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -080069 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
Joonsoo Kimad53f922014-11-13 15:19:11 -080070 zone->nr_isolate_pageblock++;
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -070071 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
72 NULL);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070073
Michal Hocko4da2ce22017-11-15 17:33:26 -080074 __mod_zone_freepage_state(zone, -nr_pages, mt);
Minchan Kimee6f5092012-07-31 16:43:50 -070075 }
76
77 spin_unlock_irqrestore(&zone->lock, flags);
78 if (!ret)
Vlastimil Babkaec25af82014-12-10 15:43:04 -080079 drain_all_pages(zone);
Minchan Kimee6f5092012-07-31 16:43:50 -070080 return ret;
81}
82
Naoya Horiguchic5b4e1b2015-09-08 15:02:09 -070083static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
Minchan Kimee6f5092012-07-31 16:43:50 -070084{
85 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070086 unsigned long flags, nr_pages;
Joonsoo Kime3a27132016-07-26 15:24:01 -070087 bool isolated_page = false;
Joonsoo Kim3c605092014-11-13 15:19:21 -080088 unsigned int order;
Vlastimil Babka76741e72017-02-22 15:41:48 -080089 unsigned long pfn, buddy_pfn;
Joonsoo Kim3c605092014-11-13 15:19:21 -080090 struct page *buddy;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070091
Minchan Kimee6f5092012-07-31 16:43:50 -070092 zone = page_zone(page);
93 spin_lock_irqsave(&zone->lock, flags);
Xishi Qiubbf9ce92017-05-03 14:52:55 -070094 if (!is_migrate_isolate_page(page))
Minchan Kimee6f5092012-07-31 16:43:50 -070095 goto out;
Joonsoo Kim3c605092014-11-13 15:19:21 -080096
97 /*
98 * Because freepage with more than pageblock_order on isolated
99 * pageblock is restricted to merge due to freepage counting problem,
100 * it is possible that there is free buddy page.
101 * move_freepages_block() doesn't care of merge so we need other
102 * approach in order to merge them. Isolation and free will make
103 * these pages to be merged.
104 */
105 if (PageBuddy(page)) {
106 order = page_order(page);
107 if (order >= pageblock_order) {
Vlastimil Babka76741e72017-02-22 15:41:48 -0800108 pfn = page_to_pfn(page);
109 buddy_pfn = __find_buddy_pfn(pfn, order);
110 buddy = page + (buddy_pfn - pfn);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800111
Vlastimil Babka13ad59d2017-02-22 15:41:51 -0800112 if (pfn_valid_within(buddy_pfn) &&
Hui Zhu1ae70132015-05-14 15:17:04 -0700113 !is_migrate_isolate_page(buddy)) {
Joonsoo Kim3c605092014-11-13 15:19:21 -0800114 __isolate_free_page(page, order);
Joonsoo Kime3a27132016-07-26 15:24:01 -0700115 isolated_page = true;
Joonsoo Kim3c605092014-11-13 15:19:21 -0800116 }
117 }
118 }
119
120 /*
121 * If we isolate freepage with more than pageblock_order, there
122 * should be no freepage in the range, so we could avoid costly
123 * pageblock scanning for freepage moving.
124 */
125 if (!isolated_page) {
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -0700126 nr_pages = move_freepages_block(zone, page, migratetype, NULL);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800127 __mod_zone_freepage_state(zone, nr_pages, migratetype);
128 }
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -0800129 set_pageblock_migratetype(page, migratetype);
Joonsoo Kimad53f922014-11-13 15:19:11 -0800130 zone->nr_isolate_pageblock--;
Minchan Kimee6f5092012-07-31 16:43:50 -0700131out:
132 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kim83358ec2016-07-26 15:23:43 -0700133 if (isolated_page) {
Joonsoo Kim46f24fd2016-07-26 15:23:58 -0700134 post_alloc_hook(page, order, __GFP_MOVABLE);
Joonsoo Kime3a27132016-07-26 15:24:01 -0700135 __free_pages(page, order);
Joonsoo Kim83358ec2016-07-26 15:23:43 -0700136 }
Minchan Kimee6f5092012-07-31 16:43:50 -0700137}
138
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700139static inline struct page *
140__first_valid_page(unsigned long pfn, unsigned long nr_pages)
141{
142 int i;
Michal Hocko2ce13642017-07-06 15:38:04 -0700143
144 for (i = 0; i < nr_pages; i++) {
145 struct page *page;
146
147 if (!pfn_valid_within(pfn + i))
148 continue;
149 page = pfn_to_online_page(pfn + i);
150 if (!page)
151 continue;
152 return page;
153 }
154 return NULL;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700155}
156
157/*
158 * start_isolate_page_range() -- make page-allocation-type of range of pages
159 * to be MIGRATE_ISOLATE.
160 * @start_pfn: The lower PFN of the range to be isolated.
161 * @end_pfn: The upper PFN of the range to be isolated.
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200162 * @migratetype: migrate type to set in error recovery.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700163 *
164 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
165 * the range will never be allocated. Any free pages and pages freed in the
166 * future will not be allocated again.
167 *
168 * start_pfn/end_pfn must be aligned to pageblock_order.
169 * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
170 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200171int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangb023f462012-12-11 16:00:45 -0800172 unsigned migratetype, bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700173{
174 unsigned long pfn;
175 unsigned long undo_pfn;
176 struct page *page;
177
Naoya Horiguchifec174d2016-01-14 15:22:13 -0800178 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
179 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700180
181 for (pfn = start_pfn;
182 pfn < end_pfn;
183 pfn += pageblock_nr_pages) {
184 page = __first_valid_page(pfn, pageblock_nr_pages);
Wen Congyangb023f462012-12-11 16:00:45 -0800185 if (page &&
Michal Hocko4da2ce22017-11-15 17:33:26 -0800186 set_migratetype_isolate(page, migratetype, skip_hwpoisoned_pages)) {
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700187 undo_pfn = pfn;
188 goto undo;
189 }
190 }
191 return 0;
192undo:
193 for (pfn = start_pfn;
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800194 pfn < undo_pfn;
Michal Hocko2ce13642017-07-06 15:38:04 -0700195 pfn += pageblock_nr_pages) {
196 struct page *page = pfn_to_online_page(pfn);
197 if (!page)
198 continue;
199 unset_migratetype_isolate(page, migratetype);
200 }
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700201
202 return -EBUSY;
203}
204
205/*
206 * Make isolated pages available again.
207 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200208int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
209 unsigned migratetype)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700210{
211 unsigned long pfn;
212 struct page *page;
Wang Xiaoqiang6f8d2b82016-01-15 16:57:13 -0800213
214 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
215 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
216
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700217 for (pfn = start_pfn;
218 pfn < end_pfn;
219 pfn += pageblock_nr_pages) {
220 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700221 if (!page || !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700222 continue;
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200223 unset_migratetype_isolate(page, migratetype);
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700224 }
225 return 0;
226}
227/*
228 * Test all pages in the range is free(means isolated) or not.
229 * all pages in [start_pfn...end_pfn) must be in the same zone.
230 * zone->lock must be held before call this.
231 *
Neil Zhangec3b6882016-04-01 14:31:37 -0700232 * Returns the last tested pfn.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700233 */
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800234static unsigned long
Wen Congyangb023f462012-12-11 16:00:45 -0800235__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
236 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700237{
238 struct page *page;
239
240 while (pfn < end_pfn) {
241 if (!pfn_valid_within(pfn)) {
242 pfn++;
243 continue;
244 }
245 page = pfn_to_page(pfn);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700246 if (PageBuddy(page))
Minchan Kim435b4052012-10-08 16:32:16 -0700247 /*
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700248 * If the page is on a free list, it has to be on
249 * the correct MIGRATE_ISOLATE freelist. There is no
250 * simple way to verify that as VM_BUG_ON(), though.
Minchan Kim435b4052012-10-08 16:32:16 -0700251 */
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700252 pfn += 1 << page_order(page);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700253 else if (skip_hwpoisoned_pages && PageHWPoison(page))
254 /* A HWPoisoned page cannot be also PageBuddy */
Wen Congyangb023f462012-12-11 16:00:45 -0800255 pfn++;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700256 else
257 break;
258 }
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800259
260 return pfn;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700261}
262
Joonsoo Kimb9eb6312016-05-19 17:12:06 -0700263/* Caller should ensure that requested range is in a single zone */
Wen Congyangb023f462012-12-11 16:00:45 -0800264int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
265 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700266{
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700267 unsigned long pfn, flags;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700268 struct page *page;
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700269 struct zone *zone;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700270
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700271 /*
Tang Chen85dbe702013-06-20 18:10:19 +0800272 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
273 * are not aligned to pageblock_nr_pages.
274 * Then we just check migratetype first.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700275 */
276 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
277 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700278 if (page && !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700279 break;
280 }
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800281 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
282 if ((pfn < end_pfn) || !page)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700283 return -EBUSY;
Tang Chen85dbe702013-06-20 18:10:19 +0800284 /* Check all pages are free or marked as ISOLATED */
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800285 zone = page_zone(page);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700286 spin_lock_irqsave(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800287 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
Wen Congyangb023f462012-12-11 16:00:45 -0800288 skip_hwpoisoned_pages);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700289 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800290
Joonsoo Kim0f0848e2016-01-14 15:18:42 -0800291 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
292
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800293 return pfn < end_pfn ? -EBUSY : 0;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700294}
Minchan Kim723a0642012-10-08 16:32:52 -0700295
296struct page *alloc_migrate_target(struct page *page, unsigned long private,
297 int **resultp)
298{
Michal Hocko8b913232017-07-10 15:48:47 -0700299 return new_page_nodemask(page, numa_node_id(), &node_states[N_MEMORY]);
Minchan Kim723a0642012-10-08 16:32:52 -0700300}