blob: 5092e4ef00c832fefb624a1d9d8f618402ad4402 [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;
141 for (i = 0; i < nr_pages; i++)
142 if (pfn_valid_within(pfn + i))
143 break;
144 if (unlikely(i == nr_pages))
145 return NULL;
146 return pfn_to_page(pfn + i);
147}
148
149/*
150 * start_isolate_page_range() -- make page-allocation-type of range of pages
151 * to be MIGRATE_ISOLATE.
152 * @start_pfn: The lower PFN of the range to be isolated.
153 * @end_pfn: The upper PFN of the range to be isolated.
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200154 * @migratetype: migrate type to set in error recovery.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700155 *
156 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
157 * the range will never be allocated. Any free pages and pages freed in the
158 * future will not be allocated again.
159 *
160 * start_pfn/end_pfn must be aligned to pageblock_order.
161 * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
162 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200163int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangb023f462012-12-11 16:00:45 -0800164 unsigned migratetype, bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700165{
166 unsigned long pfn;
167 unsigned long undo_pfn;
168 struct page *page;
169
Naoya Horiguchifec174d2016-01-14 15:22:13 -0800170 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
171 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700172
173 for (pfn = start_pfn;
174 pfn < end_pfn;
175 pfn += pageblock_nr_pages) {
176 page = __first_valid_page(pfn, pageblock_nr_pages);
Wen Congyangb023f462012-12-11 16:00:45 -0800177 if (page &&
178 set_migratetype_isolate(page, skip_hwpoisoned_pages)) {
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700179 undo_pfn = pfn;
180 goto undo;
181 }
182 }
183 return 0;
184undo:
185 for (pfn = start_pfn;
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800186 pfn < undo_pfn;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700187 pfn += pageblock_nr_pages)
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200188 unset_migratetype_isolate(pfn_to_page(pfn), migratetype);
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700189
190 return -EBUSY;
191}
192
193/*
194 * Make isolated pages available again.
195 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200196int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
197 unsigned migratetype)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700198{
199 unsigned long pfn;
200 struct page *page;
Wang Xiaoqiang6f8d2b82016-01-15 16:57:13 -0800201
202 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
203 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
204
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700205 for (pfn = start_pfn;
206 pfn < end_pfn;
207 pfn += pageblock_nr_pages) {
208 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700209 if (!page || !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700210 continue;
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200211 unset_migratetype_isolate(page, migratetype);
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700212 }
213 return 0;
214}
215/*
216 * Test all pages in the range is free(means isolated) or not.
217 * all pages in [start_pfn...end_pfn) must be in the same zone.
218 * zone->lock must be held before call this.
219 *
Neil Zhangec3b6882016-04-01 14:31:37 -0700220 * Returns the last tested pfn.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700221 */
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800222static unsigned long
Wen Congyangb023f462012-12-11 16:00:45 -0800223__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
224 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700225{
226 struct page *page;
227
228 while (pfn < end_pfn) {
229 if (!pfn_valid_within(pfn)) {
230 pfn++;
231 continue;
232 }
233 page = pfn_to_page(pfn);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700234 if (PageBuddy(page))
Minchan Kim435b4052012-10-08 16:32:16 -0700235 /*
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700236 * If the page is on a free list, it has to be on
237 * the correct MIGRATE_ISOLATE freelist. There is no
238 * simple way to verify that as VM_BUG_ON(), though.
Minchan Kim435b4052012-10-08 16:32:16 -0700239 */
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700240 pfn += 1 << page_order(page);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700241 else if (skip_hwpoisoned_pages && PageHWPoison(page))
242 /* A HWPoisoned page cannot be also PageBuddy */
Wen Congyangb023f462012-12-11 16:00:45 -0800243 pfn++;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700244 else
245 break;
246 }
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800247
248 return pfn;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700249}
250
Joonsoo Kimb9eb6312016-05-19 17:12:06 -0700251/* Caller should ensure that requested range is in a single zone */
Wen Congyangb023f462012-12-11 16:00:45 -0800252int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
253 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700254{
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700255 unsigned long pfn, flags;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700256 struct page *page;
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700257 struct zone *zone;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700258
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700259 /*
Tang Chen85dbe702013-06-20 18:10:19 +0800260 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
261 * are not aligned to pageblock_nr_pages.
262 * Then we just check migratetype first.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700263 */
264 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
265 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700266 if (page && !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700267 break;
268 }
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800269 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
270 if ((pfn < end_pfn) || !page)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700271 return -EBUSY;
Tang Chen85dbe702013-06-20 18:10:19 +0800272 /* Check all pages are free or marked as ISOLATED */
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800273 zone = page_zone(page);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700274 spin_lock_irqsave(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800275 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
Wen Congyangb023f462012-12-11 16:00:45 -0800276 skip_hwpoisoned_pages);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700277 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800278
Joonsoo Kim0f0848e2016-01-14 15:18:42 -0800279 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
280
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800281 return pfn < end_pfn ? -EBUSY : 0;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700282}
Minchan Kim723a0642012-10-08 16:32:52 -0700283
284struct page *alloc_migrate_target(struct page *page, unsigned long private,
285 int **resultp)
286{
287 gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;
288
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700289 /*
290 * TODO: allocate a destination hugepage from a nearest neighbor node,
291 * accordance with memory policy of the user process if possible. For
292 * now as a simple work-around, we use the next node for destination.
293 */
Andrew Morton0edaf862016-05-19 17:10:58 -0700294 if (PageHuge(page))
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700295 return alloc_huge_page_node(page_hstate(compound_head(page)),
Andrew Morton0edaf862016-05-19 17:10:58 -0700296 next_node_in(page_to_nid(page),
297 node_online_map));
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700298
Minchan Kim723a0642012-10-08 16:32:52 -0700299 if (PageHighMem(page))
300 gfp_mask |= __GFP_HIGHMEM;
301
302 return alloc_page(gfp_mask);
303}