blob: 3606104893e0b8dce843d5ac8fcf3f48434ab5f9 [file] [log] [blame]
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -07001/*
2 * linux/mm/page_isolation.c
3 */
4
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -07005#include <linux/mm.h>
6#include <linux/page-isolation.h>
7#include <linux/pageblock-flags.h>
Minchan Kimee6f5092012-07-31 16:43:50 -07008#include <linux/memory.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07009#include <linux/hugetlb.h>
Joonsoo Kim83358ec2016-07-26 15:23:43 -070010#include <linux/page_owner.h>
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -070011#include "internal.h"
12
Joonsoo Kim0f0848e2016-01-14 15:18:42 -080013#define CREATE_TRACE_POINTS
14#include <trace/events/page_isolation.h>
15
Naoya Horiguchic5b4e1b2015-09-08 15:02:09 -070016static int set_migratetype_isolate(struct page *page,
17 bool skip_hwpoisoned_pages)
Minchan Kimee6f5092012-07-31 16:43:50 -070018{
19 struct zone *zone;
20 unsigned long flags, pfn;
21 struct memory_isolate_notify arg;
22 int notifier_ret;
23 int ret = -EBUSY;
24
25 zone = page_zone(page);
26
27 spin_lock_irqsave(&zone->lock, flags);
28
29 pfn = page_to_pfn(page);
30 arg.start_pfn = pfn;
31 arg.nr_pages = pageblock_nr_pages;
32 arg.pages_found = 0;
33
34 /*
35 * It may be possible to isolate a pageblock even if the
36 * migratetype is not MIGRATE_MOVABLE. The memory isolation
37 * notifier chain is used by balloon drivers to return the
38 * number of pages in a range that are held by the balloon
39 * driver to shrink memory. If all the pages are accounted for
40 * by balloons, are free, or on the LRU, isolation can continue.
41 * Later, for example, when memory hotplug notifier runs, these
42 * pages reported as "can be isolated" should be isolated(freed)
43 * by the balloon driver through the memory notifier chain.
44 */
45 notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
46 notifier_ret = notifier_to_errno(notifier_ret);
47 if (notifier_ret)
48 goto out;
49 /*
50 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
51 * We just check MOVABLE pages.
52 */
Wen Congyangb023f462012-12-11 16:00:45 -080053 if (!has_unmovable_pages(zone, page, arg.pages_found,
54 skip_hwpoisoned_pages))
Minchan Kimee6f5092012-07-31 16:43:50 -070055 ret = 0;
56
57 /*
Yisheng Xieac34dcd2016-10-07 17:01:16 -070058 * immobile means "not-on-lru" pages. If immobile is larger than
Minchan Kimee6f5092012-07-31 16:43:50 -070059 * removable-by-driver pages reported by notifier, we'll fail.
60 */
61
62out:
63 if (!ret) {
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070064 unsigned long nr_pages;
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -070065 int migratetype = get_pageblock_migratetype(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070066
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -080067 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
Joonsoo Kimad53f922014-11-13 15:19:11 -080068 zone->nr_isolate_pageblock++;
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -070069 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
70 NULL);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070071
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -070072 __mod_zone_freepage_state(zone, -nr_pages, migratetype);
Minchan Kimee6f5092012-07-31 16:43:50 -070073 }
74
75 spin_unlock_irqrestore(&zone->lock, flags);
76 if (!ret)
Vlastimil Babkaec25af82014-12-10 15:43:04 -080077 drain_all_pages(zone);
Minchan Kimee6f5092012-07-31 16:43:50 -070078 return ret;
79}
80
Naoya Horiguchic5b4e1b2015-09-08 15:02:09 -070081static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
Minchan Kimee6f5092012-07-31 16:43:50 -070082{
83 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070084 unsigned long flags, nr_pages;
Joonsoo Kime3a27132016-07-26 15:24:01 -070085 bool isolated_page = false;
Joonsoo Kim3c605092014-11-13 15:19:21 -080086 unsigned int order;
Vlastimil Babka76741e72017-02-22 15:41:48 -080087 unsigned long pfn, buddy_pfn;
Joonsoo Kim3c605092014-11-13 15:19:21 -080088 struct page *buddy;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070089
Minchan Kimee6f5092012-07-31 16:43:50 -070090 zone = page_zone(page);
91 spin_lock_irqsave(&zone->lock, flags);
Xishi Qiubbf9ce92017-05-03 14:52:55 -070092 if (!is_migrate_isolate_page(page))
Minchan Kimee6f5092012-07-31 16:43:50 -070093 goto out;
Joonsoo Kim3c605092014-11-13 15:19:21 -080094
95 /*
96 * Because freepage with more than pageblock_order on isolated
97 * pageblock is restricted to merge due to freepage counting problem,
98 * it is possible that there is free buddy page.
99 * move_freepages_block() doesn't care of merge so we need other
100 * approach in order to merge them. Isolation and free will make
101 * these pages to be merged.
102 */
103 if (PageBuddy(page)) {
104 order = page_order(page);
105 if (order >= pageblock_order) {
Vlastimil Babka76741e72017-02-22 15:41:48 -0800106 pfn = page_to_pfn(page);
107 buddy_pfn = __find_buddy_pfn(pfn, order);
108 buddy = page + (buddy_pfn - pfn);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800109
Vlastimil Babka13ad59d2017-02-22 15:41:51 -0800110 if (pfn_valid_within(buddy_pfn) &&
Hui Zhu1ae70132015-05-14 15:17:04 -0700111 !is_migrate_isolate_page(buddy)) {
Joonsoo Kim3c605092014-11-13 15:19:21 -0800112 __isolate_free_page(page, order);
Joonsoo Kime3a27132016-07-26 15:24:01 -0700113 isolated_page = true;
Joonsoo Kim3c605092014-11-13 15:19:21 -0800114 }
115 }
116 }
117
118 /*
119 * If we isolate freepage with more than pageblock_order, there
120 * should be no freepage in the range, so we could avoid costly
121 * pageblock scanning for freepage moving.
122 */
123 if (!isolated_page) {
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -0700124 nr_pages = move_freepages_block(zone, page, migratetype, NULL);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800125 __mod_zone_freepage_state(zone, nr_pages, migratetype);
126 }
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -0800127 set_pageblock_migratetype(page, migratetype);
Joonsoo Kimad53f922014-11-13 15:19:11 -0800128 zone->nr_isolate_pageblock--;
Minchan Kimee6f5092012-07-31 16:43:50 -0700129out:
130 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kim83358ec2016-07-26 15:23:43 -0700131 if (isolated_page) {
Joonsoo Kim46f24fd2016-07-26 15:23:58 -0700132 post_alloc_hook(page, order, __GFP_MOVABLE);
Joonsoo Kime3a27132016-07-26 15:24:01 -0700133 __free_pages(page, order);
Joonsoo Kim83358ec2016-07-26 15:23:43 -0700134 }
Minchan Kimee6f5092012-07-31 16:43:50 -0700135}
136
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700137static inline struct page *
138__first_valid_page(unsigned long pfn, unsigned long nr_pages)
139{
140 int i;
Michal Hocko2ce13642017-07-06 15:38:04 -0700141
142 for (i = 0; i < nr_pages; i++) {
143 struct page *page;
144
145 if (!pfn_valid_within(pfn + i))
146 continue;
147 page = pfn_to_online_page(pfn + i);
148 if (!page)
149 continue;
150 return page;
151 }
152 return NULL;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700153}
154
155/*
156 * start_isolate_page_range() -- make page-allocation-type of range of pages
157 * to be MIGRATE_ISOLATE.
158 * @start_pfn: The lower PFN of the range to be isolated.
159 * @end_pfn: The upper PFN of the range to be isolated.
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200160 * @migratetype: migrate type to set in error recovery.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700161 *
162 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
163 * the range will never be allocated. Any free pages and pages freed in the
164 * future will not be allocated again.
165 *
166 * start_pfn/end_pfn must be aligned to pageblock_order.
167 * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
168 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200169int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangb023f462012-12-11 16:00:45 -0800170 unsigned migratetype, bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700171{
172 unsigned long pfn;
173 unsigned long undo_pfn;
174 struct page *page;
175
Naoya Horiguchifec174d2016-01-14 15:22:13 -0800176 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
177 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700178
179 for (pfn = start_pfn;
180 pfn < end_pfn;
181 pfn += pageblock_nr_pages) {
182 page = __first_valid_page(pfn, pageblock_nr_pages);
Wen Congyangb023f462012-12-11 16:00:45 -0800183 if (page &&
184 set_migratetype_isolate(page, skip_hwpoisoned_pages)) {
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700185 undo_pfn = pfn;
186 goto undo;
187 }
188 }
189 return 0;
190undo:
191 for (pfn = start_pfn;
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800192 pfn < undo_pfn;
Michal Hocko2ce13642017-07-06 15:38:04 -0700193 pfn += pageblock_nr_pages) {
194 struct page *page = pfn_to_online_page(pfn);
195 if (!page)
196 continue;
197 unset_migratetype_isolate(page, migratetype);
198 }
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700199
200 return -EBUSY;
201}
202
203/*
204 * Make isolated pages available again.
205 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200206int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
207 unsigned migratetype)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700208{
209 unsigned long pfn;
210 struct page *page;
Wang Xiaoqiang6f8d2b82016-01-15 16:57:13 -0800211
212 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
213 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
214
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700215 for (pfn = start_pfn;
216 pfn < end_pfn;
217 pfn += pageblock_nr_pages) {
218 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700219 if (!page || !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700220 continue;
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200221 unset_migratetype_isolate(page, migratetype);
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700222 }
223 return 0;
224}
225/*
226 * Test all pages in the range is free(means isolated) or not.
227 * all pages in [start_pfn...end_pfn) must be in the same zone.
228 * zone->lock must be held before call this.
229 *
Neil Zhangec3b6882016-04-01 14:31:37 -0700230 * Returns the last tested pfn.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700231 */
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800232static unsigned long
Wen Congyangb023f462012-12-11 16:00:45 -0800233__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
234 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700235{
236 struct page *page;
237
238 while (pfn < end_pfn) {
239 if (!pfn_valid_within(pfn)) {
240 pfn++;
241 continue;
242 }
243 page = pfn_to_page(pfn);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700244 if (PageBuddy(page))
Minchan Kim435b4052012-10-08 16:32:16 -0700245 /*
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700246 * If the page is on a free list, it has to be on
247 * the correct MIGRATE_ISOLATE freelist. There is no
248 * simple way to verify that as VM_BUG_ON(), though.
Minchan Kim435b4052012-10-08 16:32:16 -0700249 */
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700250 pfn += 1 << page_order(page);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700251 else if (skip_hwpoisoned_pages && PageHWPoison(page))
252 /* A HWPoisoned page cannot be also PageBuddy */
Wen Congyangb023f462012-12-11 16:00:45 -0800253 pfn++;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700254 else
255 break;
256 }
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800257
258 return pfn;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700259}
260
Joonsoo Kimb9eb6312016-05-19 17:12:06 -0700261/* Caller should ensure that requested range is in a single zone */
Wen Congyangb023f462012-12-11 16:00:45 -0800262int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
263 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700264{
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700265 unsigned long pfn, flags;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700266 struct page *page;
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700267 struct zone *zone;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700268
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700269 /*
Tang Chen85dbe702013-06-20 18:10:19 +0800270 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
271 * are not aligned to pageblock_nr_pages.
272 * Then we just check migratetype first.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700273 */
274 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
275 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700276 if (page && !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700277 break;
278 }
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800279 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
280 if ((pfn < end_pfn) || !page)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700281 return -EBUSY;
Tang Chen85dbe702013-06-20 18:10:19 +0800282 /* Check all pages are free or marked as ISOLATED */
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800283 zone = page_zone(page);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700284 spin_lock_irqsave(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800285 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
Wen Congyangb023f462012-12-11 16:00:45 -0800286 skip_hwpoisoned_pages);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700287 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800288
Joonsoo Kim0f0848e2016-01-14 15:18:42 -0800289 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
290
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800291 return pfn < end_pfn ? -EBUSY : 0;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700292}
Minchan Kim723a0642012-10-08 16:32:52 -0700293
294struct page *alloc_migrate_target(struct page *page, unsigned long private,
295 int **resultp)
296{
297 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
298
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700299 /*
300 * TODO: allocate a destination hugepage from a nearest neighbor node,
301 * accordance with memory policy of the user process if possible. For
302 * now as a simple work-around, we use the next node for destination.
303 */
Andrew Morton0edaf862016-05-19 17:10:58 -0700304 if (PageHuge(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700305 return alloc_huge_page_node(page_hstate(compound_head(page)),
Andrew Morton0edaf862016-05-19 17:10:58 -0700306 next_node_in(page_to_nid(page),
307 node_online_map));
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700308
Minchan Kim723a0642012-10-08 16:32:52 -0700309 if (PageHighMem(page))
310 gfp_mask |= __GFP_HIGHMEM;
311
312 return alloc_page(gfp_mask);
313}