blob: 927f5ee24c879a96e5c604b8b6c1a328d9c5d8c8 [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 /*
58 * immobile means "not-on-lru" paes. If immobile is larger than
59 * 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++;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070069 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE);
70
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -070071 __mod_zone_freepage_state(zone, -nr_pages, migratetype);
Minchan Kimee6f5092012-07-31 16:43:50 -070072 }
73
74 spin_unlock_irqrestore(&zone->lock, flags);
75 if (!ret)
Vlastimil Babkaec25af82014-12-10 15:43:04 -080076 drain_all_pages(zone);
Minchan Kimee6f5092012-07-31 16:43:50 -070077 return ret;
78}
79
Naoya Horiguchic5b4e1b2015-09-08 15:02:09 -070080static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
Minchan Kimee6f5092012-07-31 16:43:50 -070081{
82 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070083 unsigned long flags, nr_pages;
Joonsoo Kim3c605092014-11-13 15:19:21 -080084 struct page *isolated_page = NULL;
85 unsigned int order;
86 unsigned long page_idx, buddy_idx;
87 struct page *buddy;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070088
Minchan Kimee6f5092012-07-31 16:43:50 -070089 zone = page_zone(page);
90 spin_lock_irqsave(&zone->lock, flags);
91 if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
92 goto out;
Joonsoo Kim3c605092014-11-13 15:19:21 -080093
94 /*
95 * Because freepage with more than pageblock_order on isolated
96 * pageblock is restricted to merge due to freepage counting problem,
97 * it is possible that there is free buddy page.
98 * move_freepages_block() doesn't care of merge so we need other
99 * approach in order to merge them. Isolation and free will make
100 * these pages to be merged.
101 */
102 if (PageBuddy(page)) {
103 order = page_order(page);
104 if (order >= pageblock_order) {
105 page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
106 buddy_idx = __find_buddy_index(page_idx, order);
107 buddy = page + (buddy_idx - page_idx);
108
Hui Zhu1ae70132015-05-14 15:17:04 -0700109 if (pfn_valid_within(page_to_pfn(buddy)) &&
110 !is_migrate_isolate_page(buddy)) {
Joonsoo Kim3c605092014-11-13 15:19:21 -0800111 __isolate_free_page(page, order);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800112 isolated_page = page;
113 }
114 }
115 }
116
117 /*
118 * If we isolate freepage with more than pageblock_order, there
119 * should be no freepage in the range, so we could avoid costly
120 * pageblock scanning for freepage moving.
121 */
122 if (!isolated_page) {
123 nr_pages = move_freepages_block(zone, page, migratetype);
124 __mod_zone_freepage_state(zone, nr_pages, migratetype);
125 }
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -0800126 set_pageblock_migratetype(page, migratetype);
Joonsoo Kimad53f922014-11-13 15:19:11 -0800127 zone->nr_isolate_pageblock--;
Minchan Kimee6f5092012-07-31 16:43:50 -0700128out:
129 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kim83358ec2016-07-26 15:23:43 -0700130 if (isolated_page) {
131 kernel_map_pages(page, (1 << order), 1);
132 set_page_refcounted(page);
133 set_page_owner(page, order, __GFP_MOVABLE);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800134 __free_pages(isolated_page, order);
Joonsoo Kim83358ec2016-07-26 15:23:43 -0700135 }
Minchan Kimee6f5092012-07-31 16:43:50 -0700136}
137
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700138static inline struct page *
139__first_valid_page(unsigned long pfn, unsigned long nr_pages)
140{
141 int i;
142 for (i = 0; i < nr_pages; i++)
143 if (pfn_valid_within(pfn + i))
144 break;
145 if (unlikely(i == nr_pages))
146 return NULL;
147 return pfn_to_page(pfn + i);
148}
149
150/*
151 * start_isolate_page_range() -- make page-allocation-type of range of pages
152 * to be MIGRATE_ISOLATE.
153 * @start_pfn: The lower PFN of the range to be isolated.
154 * @end_pfn: The upper PFN of the range to be isolated.
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200155 * @migratetype: migrate type to set in error recovery.
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
159 * future will not be allocated again.
160 *
161 * start_pfn/end_pfn must be aligned to pageblock_order.
162 * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
163 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200164int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangb023f462012-12-11 16:00:45 -0800165 unsigned migratetype, bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700166{
167 unsigned long pfn;
168 unsigned long undo_pfn;
169 struct page *page;
170
Naoya Horiguchifec174d2016-01-14 15:22:13 -0800171 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
172 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700173
174 for (pfn = start_pfn;
175 pfn < end_pfn;
176 pfn += pageblock_nr_pages) {
177 page = __first_valid_page(pfn, pageblock_nr_pages);
Wen Congyangb023f462012-12-11 16:00:45 -0800178 if (page &&
179 set_migratetype_isolate(page, skip_hwpoisoned_pages)) {
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700180 undo_pfn = pfn;
181 goto undo;
182 }
183 }
184 return 0;
185undo:
186 for (pfn = start_pfn;
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800187 pfn < undo_pfn;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700188 pfn += pageblock_nr_pages)
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200189 unset_migratetype_isolate(pfn_to_page(pfn), migratetype);
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700190
191 return -EBUSY;
192}
193
194/*
195 * Make isolated pages available again.
196 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200197int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
198 unsigned migratetype)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700199{
200 unsigned long pfn;
201 struct page *page;
Wang Xiaoqiang6f8d2b82016-01-15 16:57:13 -0800202
203 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
204 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
205
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700206 for (pfn = start_pfn;
207 pfn < end_pfn;
208 pfn += pageblock_nr_pages) {
209 page = __first_valid_page(pfn, pageblock_nr_pages);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800210 if (!page || get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700211 continue;
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200212 unset_migratetype_isolate(page, migratetype);
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700213 }
214 return 0;
215}
216/*
217 * Test all pages in the range is free(means isolated) or not.
218 * all pages in [start_pfn...end_pfn) must be in the same zone.
219 * zone->lock must be held before call this.
220 *
Neil Zhangec3b6882016-04-01 14:31:37 -0700221 * Returns the last tested pfn.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700222 */
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800223static unsigned long
Wen Congyangb023f462012-12-11 16:00:45 -0800224__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
225 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700226{
227 struct page *page;
228
229 while (pfn < end_pfn) {
230 if (!pfn_valid_within(pfn)) {
231 pfn++;
232 continue;
233 }
234 page = pfn_to_page(pfn);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700235 if (PageBuddy(page))
Minchan Kim435b4052012-10-08 16:32:16 -0700236 /*
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700237 * If the page is on a free list, it has to be on
238 * the correct MIGRATE_ISOLATE freelist. There is no
239 * simple way to verify that as VM_BUG_ON(), though.
Minchan Kim435b4052012-10-08 16:32:16 -0700240 */
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700241 pfn += 1 << page_order(page);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700242 else if (skip_hwpoisoned_pages && PageHWPoison(page))
243 /* A HWPoisoned page cannot be also PageBuddy */
Wen Congyangb023f462012-12-11 16:00:45 -0800244 pfn++;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700245 else
246 break;
247 }
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800248
249 return pfn;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700250}
251
Joonsoo Kimb9eb6312016-05-19 17:12:06 -0700252/* Caller should ensure that requested range is in a single zone */
Wen Congyangb023f462012-12-11 16:00:45 -0800253int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
254 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700255{
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700256 unsigned long pfn, flags;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700257 struct page *page;
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700258 struct zone *zone;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700259
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700260 /*
Tang Chen85dbe702013-06-20 18:10:19 +0800261 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
262 * are not aligned to pageblock_nr_pages.
263 * Then we just check migratetype first.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700264 */
265 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
266 page = __first_valid_page(pfn, pageblock_nr_pages);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800267 if (page && get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700268 break;
269 }
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800270 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
271 if ((pfn < end_pfn) || !page)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700272 return -EBUSY;
Tang Chen85dbe702013-06-20 18:10:19 +0800273 /* Check all pages are free or marked as ISOLATED */
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800274 zone = page_zone(page);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700275 spin_lock_irqsave(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800276 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
Wen Congyangb023f462012-12-11 16:00:45 -0800277 skip_hwpoisoned_pages);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700278 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800279
Joonsoo Kim0f0848e2016-01-14 15:18:42 -0800280 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
281
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800282 return pfn < end_pfn ? -EBUSY : 0;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700283}
Minchan Kim723a0642012-10-08 16:32:52 -0700284
285struct page *alloc_migrate_target(struct page *page, unsigned long private,
286 int **resultp)
287{
288 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
289
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700290 /*
291 * TODO: allocate a destination hugepage from a nearest neighbor node,
292 * accordance with memory policy of the user process if possible. For
293 * now as a simple work-around, we use the next node for destination.
294 */
Andrew Morton0edaf862016-05-19 17:10:58 -0700295 if (PageHuge(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700296 return alloc_huge_page_node(page_hstate(compound_head(page)),
Andrew Morton0edaf862016-05-19 17:10:58 -0700297 next_node_in(page_to_nid(page),
298 node_online_map));
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700299
Minchan Kim723a0642012-10-08 16:32:52 -0700300 if (PageHighMem(page))
301 gfp_mask |= __GFP_HIGHMEM;
302
303 return alloc_page(gfp_mask);
304}