blob: 757410d9f758a22ca6306b84d00c5929dc3fa79a [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>
Michal Hocko8b913232017-07-10 15:48:47 -070011#include <linux/migrate.h>
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -070012#include "internal.h"
13
Joonsoo Kim0f0848e2016-01-14 15:18:42 -080014#define CREATE_TRACE_POINTS
15#include <trace/events/page_isolation.h>
16
Naoya Horiguchic5b4e1b2015-09-08 15:02:09 -070017static int set_migratetype_isolate(struct page *page,
18 bool skip_hwpoisoned_pages)
Minchan Kimee6f5092012-07-31 16:43:50 -070019{
20 struct zone *zone;
21 unsigned long flags, pfn;
22 struct memory_isolate_notify arg;
23 int notifier_ret;
24 int ret = -EBUSY;
25
26 zone = page_zone(page);
27
28 spin_lock_irqsave(&zone->lock, flags);
29
30 pfn = page_to_pfn(page);
31 arg.start_pfn = pfn;
32 arg.nr_pages = pageblock_nr_pages;
33 arg.pages_found = 0;
34
35 /*
36 * It may be possible to isolate a pageblock even if the
37 * migratetype is not MIGRATE_MOVABLE. The memory isolation
38 * notifier chain is used by balloon drivers to return the
39 * number of pages in a range that are held by the balloon
40 * driver to shrink memory. If all the pages are accounted for
41 * by balloons, are free, or on the LRU, isolation can continue.
42 * Later, for example, when memory hotplug notifier runs, these
43 * pages reported as "can be isolated" should be isolated(freed)
44 * by the balloon driver through the memory notifier chain.
45 */
46 notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
47 notifier_ret = notifier_to_errno(notifier_ret);
48 if (notifier_ret)
49 goto out;
50 /*
51 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
52 * We just check MOVABLE pages.
53 */
Wen Congyangb023f462012-12-11 16:00:45 -080054 if (!has_unmovable_pages(zone, page, arg.pages_found,
55 skip_hwpoisoned_pages))
Minchan Kimee6f5092012-07-31 16:43:50 -070056 ret = 0;
57
58 /*
Yisheng Xieac34dcd2016-10-07 17:01:16 -070059 * immobile means "not-on-lru" pages. If immobile is larger than
Minchan Kimee6f5092012-07-31 16:43:50 -070060 * removable-by-driver pages reported by notifier, we'll fail.
61 */
62
63out:
64 if (!ret) {
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070065 unsigned long nr_pages;
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -070066 int migratetype = get_pageblock_migratetype(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070067
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -080068 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
Joonsoo Kimad53f922014-11-13 15:19:11 -080069 zone->nr_isolate_pageblock++;
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -070070 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
71 NULL);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070072
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -070073 __mod_zone_freepage_state(zone, -nr_pages, migratetype);
Minchan Kimee6f5092012-07-31 16:43:50 -070074 }
75
76 spin_unlock_irqrestore(&zone->lock, flags);
77 if (!ret)
Vlastimil Babkaec25af82014-12-10 15:43:04 -080078 drain_all_pages(zone);
Minchan Kimee6f5092012-07-31 16:43:50 -070079 return ret;
80}
81
Naoya Horiguchic5b4e1b2015-09-08 15:02:09 -070082static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
Minchan Kimee6f5092012-07-31 16:43:50 -070083{
84 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070085 unsigned long flags, nr_pages;
Joonsoo Kime3a27132016-07-26 15:24:01 -070086 bool isolated_page = false;
Joonsoo Kim3c605092014-11-13 15:19:21 -080087 unsigned int order;
Vlastimil Babka76741e72017-02-22 15:41:48 -080088 unsigned long pfn, buddy_pfn;
Joonsoo Kim3c605092014-11-13 15:19:21 -080089 struct page *buddy;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070090
Minchan Kimee6f5092012-07-31 16:43:50 -070091 zone = page_zone(page);
92 spin_lock_irqsave(&zone->lock, flags);
Xishi Qiubbf9ce92017-05-03 14:52:55 -070093 if (!is_migrate_isolate_page(page))
Minchan Kimee6f5092012-07-31 16:43:50 -070094 goto out;
Joonsoo Kim3c605092014-11-13 15:19:21 -080095
96 /*
97 * Because freepage with more than pageblock_order on isolated
98 * pageblock is restricted to merge due to freepage counting problem,
99 * it is possible that there is free buddy page.
100 * move_freepages_block() doesn't care of merge so we need other
101 * approach in order to merge them. Isolation and free will make
102 * these pages to be merged.
103 */
104 if (PageBuddy(page)) {
105 order = page_order(page);
106 if (order >= pageblock_order) {
Vlastimil Babka76741e72017-02-22 15:41:48 -0800107 pfn = page_to_pfn(page);
108 buddy_pfn = __find_buddy_pfn(pfn, order);
109 buddy = page + (buddy_pfn - pfn);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800110
Vlastimil Babka13ad59d2017-02-22 15:41:51 -0800111 if (pfn_valid_within(buddy_pfn) &&
Hui Zhu1ae70132015-05-14 15:17:04 -0700112 !is_migrate_isolate_page(buddy)) {
Joonsoo Kim3c605092014-11-13 15:19:21 -0800113 __isolate_free_page(page, order);
Joonsoo Kime3a27132016-07-26 15:24:01 -0700114 isolated_page = true;
Joonsoo Kim3c605092014-11-13 15:19:21 -0800115 }
116 }
117 }
118
119 /*
120 * If we isolate freepage with more than pageblock_order, there
121 * should be no freepage in the range, so we could avoid costly
122 * pageblock scanning for freepage moving.
123 */
124 if (!isolated_page) {
Vlastimil Babka02aa0cd2017-05-08 15:54:40 -0700125 nr_pages = move_freepages_block(zone, page, migratetype, NULL);
Joonsoo Kim3c605092014-11-13 15:19:21 -0800126 __mod_zone_freepage_state(zone, nr_pages, migratetype);
127 }
Bartlomiej Zolnierkiewicza4584312013-01-04 15:35:08 -0800128 set_pageblock_migratetype(page, migratetype);
Joonsoo Kimad53f922014-11-13 15:19:11 -0800129 zone->nr_isolate_pageblock--;
Minchan Kimee6f5092012-07-31 16:43:50 -0700130out:
131 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kim83358ec2016-07-26 15:23:43 -0700132 if (isolated_page) {
Joonsoo Kim46f24fd2016-07-26 15:23:58 -0700133 post_alloc_hook(page, order, __GFP_MOVABLE);
Joonsoo Kime3a27132016-07-26 15:24:01 -0700134 __free_pages(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;
Michal Hocko2ce13642017-07-06 15:38:04 -0700142
143 for (i = 0; i < nr_pages; i++) {
144 struct page *page;
145
146 if (!pfn_valid_within(pfn + i))
147 continue;
148 page = pfn_to_online_page(pfn + i);
149 if (!page)
150 continue;
151 return page;
152 }
153 return NULL;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700154}
155
156/*
157 * start_isolate_page_range() -- make page-allocation-type of range of pages
158 * to be MIGRATE_ISOLATE.
159 * @start_pfn: The lower PFN of the range to be isolated.
160 * @end_pfn: The upper PFN of the range to be isolated.
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200161 * @migratetype: migrate type to set in error recovery.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700162 *
163 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
164 * the range will never be allocated. Any free pages and pages freed in the
165 * future will not be allocated again.
166 *
167 * start_pfn/end_pfn must be aligned to pageblock_order.
168 * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
169 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200170int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
Wen Congyangb023f462012-12-11 16:00:45 -0800171 unsigned migratetype, bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700172{
173 unsigned long pfn;
174 unsigned long undo_pfn;
175 struct page *page;
176
Naoya Horiguchifec174d2016-01-14 15:22:13 -0800177 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
178 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700179
180 for (pfn = start_pfn;
181 pfn < end_pfn;
182 pfn += pageblock_nr_pages) {
183 page = __first_valid_page(pfn, pageblock_nr_pages);
Wen Congyangb023f462012-12-11 16:00:45 -0800184 if (page &&
185 set_migratetype_isolate(page, skip_hwpoisoned_pages)) {
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700186 undo_pfn = pfn;
187 goto undo;
188 }
189 }
190 return 0;
191undo:
192 for (pfn = start_pfn;
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800193 pfn < undo_pfn;
Michal Hocko2ce13642017-07-06 15:38:04 -0700194 pfn += pageblock_nr_pages) {
195 struct page *page = pfn_to_online_page(pfn);
196 if (!page)
197 continue;
198 unset_migratetype_isolate(page, migratetype);
199 }
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700200
201 return -EBUSY;
202}
203
204/*
205 * Make isolated pages available again.
206 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200207int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
208 unsigned migratetype)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700209{
210 unsigned long pfn;
211 struct page *page;
Wang Xiaoqiang6f8d2b82016-01-15 16:57:13 -0800212
213 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
214 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
215
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700216 for (pfn = start_pfn;
217 pfn < end_pfn;
218 pfn += pageblock_nr_pages) {
219 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700220 if (!page || !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700221 continue;
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200222 unset_migratetype_isolate(page, migratetype);
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700223 }
224 return 0;
225}
226/*
227 * Test all pages in the range is free(means isolated) or not.
228 * all pages in [start_pfn...end_pfn) must be in the same zone.
229 * zone->lock must be held before call this.
230 *
Neil Zhangec3b6882016-04-01 14:31:37 -0700231 * Returns the last tested pfn.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700232 */
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800233static unsigned long
Wen Congyangb023f462012-12-11 16:00:45 -0800234__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
235 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700236{
237 struct page *page;
238
239 while (pfn < end_pfn) {
240 if (!pfn_valid_within(pfn)) {
241 pfn++;
242 continue;
243 }
244 page = pfn_to_page(pfn);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700245 if (PageBuddy(page))
Minchan Kim435b4052012-10-08 16:32:16 -0700246 /*
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700247 * If the page is on a free list, it has to be on
248 * the correct MIGRATE_ISOLATE freelist. There is no
249 * simple way to verify that as VM_BUG_ON(), though.
Minchan Kim435b4052012-10-08 16:32:16 -0700250 */
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700251 pfn += 1 << page_order(page);
Vlastimil Babkaaa016d12015-09-08 15:01:22 -0700252 else if (skip_hwpoisoned_pages && PageHWPoison(page))
253 /* A HWPoisoned page cannot be also PageBuddy */
Wen Congyangb023f462012-12-11 16:00:45 -0800254 pfn++;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700255 else
256 break;
257 }
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800258
259 return pfn;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700260}
261
Joonsoo Kimb9eb6312016-05-19 17:12:06 -0700262/* Caller should ensure that requested range is in a single zone */
Wen Congyangb023f462012-12-11 16:00:45 -0800263int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
264 bool skip_hwpoisoned_pages)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700265{
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700266 unsigned long pfn, flags;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700267 struct page *page;
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700268 struct zone *zone;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700269
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700270 /*
Tang Chen85dbe702013-06-20 18:10:19 +0800271 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
272 * are not aligned to pageblock_nr_pages.
273 * Then we just check migratetype first.
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700274 */
275 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
276 page = __first_valid_page(pfn, pageblock_nr_pages);
Xishi Qiubbf9ce92017-05-03 14:52:55 -0700277 if (page && !is_migrate_isolate_page(page))
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700278 break;
279 }
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800280 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
281 if ((pfn < end_pfn) || !page)
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700282 return -EBUSY;
Tang Chen85dbe702013-06-20 18:10:19 +0800283 /* Check all pages are free or marked as ISOLATED */
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800284 zone = page_zone(page);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700285 spin_lock_irqsave(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800286 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
Wen Congyangb023f462012-12-11 16:00:45 -0800287 skip_hwpoisoned_pages);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700288 spin_unlock_irqrestore(&zone->lock, flags);
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800289
Joonsoo Kim0f0848e2016-01-14 15:18:42 -0800290 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
291
Joonsoo Kimfea85cf2016-01-14 15:18:39 -0800292 return pfn < end_pfn ? -EBUSY : 0;
KAMEZAWA Hiroyukia5d76b542007-10-16 01:26:11 -0700293}
Minchan Kim723a0642012-10-08 16:32:52 -0700294
295struct page *alloc_migrate_target(struct page *page, unsigned long private,
296 int **resultp)
297{
Michal Hocko8b913232017-07-10 15:48:47 -0700298 return new_page_nodemask(page, numa_node_id(), &node_states[N_MEMORY]);
Minchan Kim723a0642012-10-08 16:32:52 -0700299}