page_isolation.c 8.4 KB
Newer Older
K
KAMEZAWA Hiroyuki 已提交
1 2 3 4 5 6 7
/*
 * linux/mm/page_isolation.c
 */

#include <linux/mm.h>
#include <linux/page-isolation.h>
#include <linux/pageblock-flags.h>
8
#include <linux/memory.h>
9
#include <linux/hugetlb.h>
10
#include <linux/page_owner.h>
K
KAMEZAWA Hiroyuki 已提交
11 12
#include "internal.h"

13 14 15
#define CREATE_TRACE_POINTS
#include <trace/events/page_isolation.h>

16 17
static int set_migratetype_isolate(struct page *page,
				bool skip_hwpoisoned_pages)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
{
	struct zone *zone;
	unsigned long flags, pfn;
	struct memory_isolate_notify arg;
	int notifier_ret;
	int ret = -EBUSY;

	zone = page_zone(page);

	spin_lock_irqsave(&zone->lock, flags);

	pfn = page_to_pfn(page);
	arg.start_pfn = pfn;
	arg.nr_pages = pageblock_nr_pages;
	arg.pages_found = 0;

	/*
	 * It may be possible to isolate a pageblock even if the
	 * migratetype is not MIGRATE_MOVABLE. The memory isolation
	 * notifier chain is used by balloon drivers to return the
	 * number of pages in a range that are held by the balloon
	 * driver to shrink memory. If all the pages are accounted for
	 * by balloons, are free, or on the LRU, isolation can continue.
	 * Later, for example, when memory hotplug notifier runs, these
	 * pages reported as "can be isolated" should be isolated(freed)
	 * by the balloon driver through the memory notifier chain.
	 */
	notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
	notifier_ret = notifier_to_errno(notifier_ret);
	if (notifier_ret)
		goto out;
	/*
	 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
	 * We just check MOVABLE pages.
	 */
53 54
	if (!has_unmovable_pages(zone, page, arg.pages_found,
				 skip_hwpoisoned_pages))
55 56 57
		ret = 0;

	/*
58
	 * immobile means "not-on-lru" pages. If immobile is larger than
59 60 61 62 63
	 * removable-by-driver pages reported by notifier, we'll fail.
	 */

out:
	if (!ret) {
64
		unsigned long nr_pages;
65
		int migratetype = get_pageblock_migratetype(page);
66

67
		set_pageblock_migratetype(page, MIGRATE_ISOLATE);
68
		zone->nr_isolate_pageblock++;
69 70
		nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
									NULL);
71

72
		__mod_zone_freepage_state(zone, -nr_pages, migratetype);
73 74 75 76
	}

	spin_unlock_irqrestore(&zone->lock, flags);
	if (!ret)
77
		drain_all_pages(zone);
78 79 80
	return ret;
}

81
static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
82 83
{
	struct zone *zone;
84
	unsigned long flags, nr_pages;
85
	bool isolated_page = false;
86
	unsigned int order;
87
	unsigned long pfn, buddy_pfn;
88
	struct page *buddy;
89

90 91
	zone = page_zone(page);
	spin_lock_irqsave(&zone->lock, flags);
92
	if (!is_migrate_isolate_page(page))
93
		goto out;
94 95 96 97 98 99 100 101 102 103 104 105

	/*
	 * Because freepage with more than pageblock_order on isolated
	 * pageblock is restricted to merge due to freepage counting problem,
	 * it is possible that there is free buddy page.
	 * move_freepages_block() doesn't care of merge so we need other
	 * approach in order to merge them. Isolation and free will make
	 * these pages to be merged.
	 */
	if (PageBuddy(page)) {
		order = page_order(page);
		if (order >= pageblock_order) {
106 107 108
			pfn = page_to_pfn(page);
			buddy_pfn = __find_buddy_pfn(pfn, order);
			buddy = page + (buddy_pfn - pfn);
109

110
			if (pfn_valid_within(buddy_pfn) &&
111
			    !is_migrate_isolate_page(buddy)) {
112
				__isolate_free_page(page, order);
113
				isolated_page = true;
114 115 116 117 118 119 120 121 122 123
			}
		}
	}

	/*
	 * If we isolate freepage with more than pageblock_order, there
	 * should be no freepage in the range, so we could avoid costly
	 * pageblock scanning for freepage moving.
	 */
	if (!isolated_page) {
124
		nr_pages = move_freepages_block(zone, page, migratetype, NULL);
125 126
		__mod_zone_freepage_state(zone, nr_pages, migratetype);
	}
127
	set_pageblock_migratetype(page, migratetype);
128
	zone->nr_isolate_pageblock--;
129 130
out:
	spin_unlock_irqrestore(&zone->lock, flags);
131
	if (isolated_page) {
132
		post_alloc_hook(page, order, __GFP_MOVABLE);
133
		__free_pages(page, order);
134
	}
135 136
}

K
KAMEZAWA Hiroyuki 已提交
137 138 139 140
static inline struct page *
__first_valid_page(unsigned long pfn, unsigned long nr_pages)
{
	int i;
141 142 143 144 145 146 147 148 149 150 151 152

	for (i = 0; i < nr_pages; i++) {
		struct page *page;

		if (!pfn_valid_within(pfn + i))
			continue;
		page = pfn_to_online_page(pfn + i);
		if (!page)
			continue;
		return page;
	}
	return NULL;
K
KAMEZAWA Hiroyuki 已提交
153 154 155 156 157 158 159
}

/*
 * start_isolate_page_range() -- make page-allocation-type of range of pages
 * to be MIGRATE_ISOLATE.
 * @start_pfn: The lower PFN of the range to be isolated.
 * @end_pfn: The upper PFN of the range to be isolated.
160
 * @migratetype: migrate type to set in error recovery.
K
KAMEZAWA Hiroyuki 已提交
161 162 163 164 165 166 167 168
 *
 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
 * the range will never be allocated. Any free pages and pages freed in the
 * future will not be allocated again.
 *
 * start_pfn/end_pfn must be aligned to pageblock_order.
 * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
 */
169
int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
170
			     unsigned migratetype, bool skip_hwpoisoned_pages)
K
KAMEZAWA Hiroyuki 已提交
171 172 173 174 175
{
	unsigned long pfn;
	unsigned long undo_pfn;
	struct page *page;

176 177
	BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
	BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
K
KAMEZAWA Hiroyuki 已提交
178 179 180 181 182

	for (pfn = start_pfn;
	     pfn < end_pfn;
	     pfn += pageblock_nr_pages) {
		page = __first_valid_page(pfn, pageblock_nr_pages);
183 184
		if (page &&
		    set_migratetype_isolate(page, skip_hwpoisoned_pages)) {
K
KAMEZAWA Hiroyuki 已提交
185 186 187 188 189 190 191
			undo_pfn = pfn;
			goto undo;
		}
	}
	return 0;
undo:
	for (pfn = start_pfn;
192
	     pfn < undo_pfn;
193 194 195 196 197 198
	     pfn += pageblock_nr_pages) {
		struct page *page = pfn_to_online_page(pfn);
		if (!page)
			continue;
		unset_migratetype_isolate(page, migratetype);
	}
K
KAMEZAWA Hiroyuki 已提交
199 200 201 202 203 204 205

	return -EBUSY;
}

/*
 * Make isolated pages available again.
 */
206 207
int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
			    unsigned migratetype)
K
KAMEZAWA Hiroyuki 已提交
208 209 210
{
	unsigned long pfn;
	struct page *page;
211 212 213 214

	BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
	BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));

K
KAMEZAWA Hiroyuki 已提交
215 216 217 218
	for (pfn = start_pfn;
	     pfn < end_pfn;
	     pfn += pageblock_nr_pages) {
		page = __first_valid_page(pfn, pageblock_nr_pages);
219
		if (!page || !is_migrate_isolate_page(page))
K
KAMEZAWA Hiroyuki 已提交
220
			continue;
221
		unset_migratetype_isolate(page, migratetype);
K
KAMEZAWA Hiroyuki 已提交
222 223 224 225 226 227 228 229
	}
	return 0;
}
/*
 * Test all pages in the range is free(means isolated) or not.
 * all pages in [start_pfn...end_pfn) must be in the same zone.
 * zone->lock must be held before call this.
 *
230
 * Returns the last tested pfn.
K
KAMEZAWA Hiroyuki 已提交
231
 */
232
static unsigned long
233 234
__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
				  bool skip_hwpoisoned_pages)
K
KAMEZAWA Hiroyuki 已提交
235 236 237 238 239 240 241 242 243
{
	struct page *page;

	while (pfn < end_pfn) {
		if (!pfn_valid_within(pfn)) {
			pfn++;
			continue;
		}
		page = pfn_to_page(pfn);
244
		if (PageBuddy(page))
245
			/*
246 247 248
			 * If the page is on a free list, it has to be on
			 * the correct MIGRATE_ISOLATE freelist. There is no
			 * simple way to verify that as VM_BUG_ON(), though.
249
			 */
K
KAMEZAWA Hiroyuki 已提交
250
			pfn += 1 << page_order(page);
251 252
		else if (skip_hwpoisoned_pages && PageHWPoison(page))
			/* A HWPoisoned page cannot be also PageBuddy */
253
			pfn++;
K
KAMEZAWA Hiroyuki 已提交
254 255 256
		else
			break;
	}
257 258

	return pfn;
K
KAMEZAWA Hiroyuki 已提交
259 260
}

261
/* Caller should ensure that requested range is in a single zone */
262 263
int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
			bool skip_hwpoisoned_pages)
K
KAMEZAWA Hiroyuki 已提交
264
{
265
	unsigned long pfn, flags;
K
KAMEZAWA Hiroyuki 已提交
266
	struct page *page;
267
	struct zone *zone;
K
KAMEZAWA Hiroyuki 已提交
268 269

	/*
270 271 272
	 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
	 * are not aligned to pageblock_nr_pages.
	 * Then we just check migratetype first.
K
KAMEZAWA Hiroyuki 已提交
273 274 275
	 */
	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
		page = __first_valid_page(pfn, pageblock_nr_pages);
276
		if (page && !is_migrate_isolate_page(page))
K
KAMEZAWA Hiroyuki 已提交
277 278
			break;
	}
279 280
	page = __first_valid_page(start_pfn, end_pfn - start_pfn);
	if ((pfn < end_pfn) || !page)
K
KAMEZAWA Hiroyuki 已提交
281
		return -EBUSY;
282
	/* Check all pages are free or marked as ISOLATED */
283
	zone = page_zone(page);
284
	spin_lock_irqsave(&zone->lock, flags);
285
	pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
286
						skip_hwpoisoned_pages);
287
	spin_unlock_irqrestore(&zone->lock, flags);
288

289 290
	trace_test_pages_isolated(start_pfn, end_pfn, pfn);

291
	return pfn < end_pfn ? -EBUSY : 0;
K
KAMEZAWA Hiroyuki 已提交
292
}
293 294 295 296 297 298

struct page *alloc_migrate_target(struct page *page, unsigned long private,
				  int **resultp)
{
	gfp_t gfp_mask = GFP_USER | __GFP_MOVABLE;

299 300 301 302 303
	/*
	 * TODO: allocate a destination hugepage from a nearest neighbor node,
	 * accordance with memory policy of the user process if possible. For
	 * now as a simple work-around, we use the next node for destination.
	 */
304
	if (PageHuge(page))
305
		return alloc_huge_page_node(page_hstate(compound_head(page)),
306 307
					    next_node_in(page_to_nid(page),
							 node_online_map));
308

309 310 311 312 313
	if (PageHighMem(page))
		gfp_mask |= __GFP_HIGHMEM;

	return alloc_page(gfp_mask);
}