page_isolation.c 8.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
K
KAMEZAWA Hiroyuki 已提交
2 3 4 5 6 7 8
/*
 * linux/mm/page_isolation.c
 */

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

15 16 17
#define CREATE_TRACE_POINTS
#include <trace/events/page_isolation.h>

18
static int set_migratetype_isolate(struct page *page, int migratetype,
19
				bool skip_hwpoisoned_pages)
20 21 22 23 24 25 26 27 28 29 30
{
	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);

31 32 33 34 35 36 37 38
	/*
	 * We assume the caller intended to SET migrate type to isolate.
	 * If it is already set, then someone else must have raced and
	 * set it before us.  Return -EBUSY
	 */
	if (is_migrate_isolate_page(page))
		goto out;

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	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.
	 */
63
	if (!has_unmovable_pages(zone, page, arg.pages_found, migratetype,
64
				 skip_hwpoisoned_pages))
65 66 67
		ret = 0;

	/*
68
	 * immobile means "not-on-lru" pages. If immobile is larger than
69 70 71 72 73
	 * removable-by-driver pages reported by notifier, we'll fail.
	 */

out:
	if (!ret) {
74
		unsigned long nr_pages;
75
		int mt = get_pageblock_migratetype(page);
76

77
		set_pageblock_migratetype(page, MIGRATE_ISOLATE);
78
		zone->nr_isolate_pageblock++;
79 80
		nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
									NULL);
81

82
		__mod_zone_freepage_state(zone, -nr_pages, mt);
83 84 85 86
	}

	spin_unlock_irqrestore(&zone->lock, flags);
	if (!ret)
87
		drain_all_pages(zone);
88 89 90
	return ret;
}

91
static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
92 93
{
	struct zone *zone;
94
	unsigned long flags, nr_pages;
95
	bool isolated_page = false;
96
	unsigned int order;
97
	unsigned long pfn, buddy_pfn;
98
	struct page *buddy;
99

100 101
	zone = page_zone(page);
	spin_lock_irqsave(&zone->lock, flags);
102
	if (!is_migrate_isolate_page(page))
103
		goto out;
104 105 106 107 108 109 110 111 112 113 114 115

	/*
	 * 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) {
116 117 118
			pfn = page_to_pfn(page);
			buddy_pfn = __find_buddy_pfn(pfn, order);
			buddy = page + (buddy_pfn - pfn);
119

120
			if (pfn_valid_within(buddy_pfn) &&
121
			    !is_migrate_isolate_page(buddy)) {
122
				__isolate_free_page(page, order);
123
				isolated_page = true;
124 125 126 127 128 129 130 131 132 133
			}
		}
	}

	/*
	 * 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) {
134
		nr_pages = move_freepages_block(zone, page, migratetype, NULL);
135 136
		__mod_zone_freepage_state(zone, nr_pages, migratetype);
	}
137
	set_pageblock_migratetype(page, migratetype);
138 139
	if (isolated_page)
		__putback_isolated_page(page, order, migratetype);
140
	zone->nr_isolate_pageblock--;
141 142 143 144
out:
	spin_unlock_irqrestore(&zone->lock, flags);
}

K
KAMEZAWA Hiroyuki 已提交
145 146 147 148
static inline struct page *
__first_valid_page(unsigned long pfn, unsigned long nr_pages)
{
	int i;
149 150 151 152 153 154 155 156 157 158 159 160

	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 已提交
161 162 163 164 165 166 167
}

/*
 * 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.
168
 * @migratetype: migrate type to set in error recovery.
K
KAMEZAWA Hiroyuki 已提交
169 170 171 172 173 174
 *
 * 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.
175 176 177 178 179 180 181 182 183
 * Return 0 on success and -EBUSY if any part of range cannot be isolated.
 *
 * There is no high level synchronization mechanism that prevents two threads
 * from trying to isolate overlapping ranges.  If this happens, one thread
 * will notice pageblocks in the overlapping range already set to isolate.
 * This happens in set_migratetype_isolate, and set_migratetype_isolate
 * returns an error.  We then clean up by restoring the migration type on
 * pageblocks we may have modified and return -EBUSY to caller.  This
 * prevents two threads from simultaneously working on overlapping ranges.
K
KAMEZAWA Hiroyuki 已提交
184
 */
185
int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
186
			     unsigned migratetype, bool skip_hwpoisoned_pages)
K
KAMEZAWA Hiroyuki 已提交
187 188 189 190 191
{
	unsigned long pfn;
	unsigned long undo_pfn;
	struct page *page;

192 193
	BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
	BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
K
KAMEZAWA Hiroyuki 已提交
194 195 196 197 198

	for (pfn = start_pfn;
	     pfn < end_pfn;
	     pfn += pageblock_nr_pages) {
		page = __first_valid_page(pfn, pageblock_nr_pages);
199
		if (page &&
200
		    set_migratetype_isolate(page, migratetype, skip_hwpoisoned_pages)) {
K
KAMEZAWA Hiroyuki 已提交
201 202 203 204 205 206 207
			undo_pfn = pfn;
			goto undo;
		}
	}
	return 0;
undo:
	for (pfn = start_pfn;
208
	     pfn < undo_pfn;
209 210 211 212 213 214
	     pfn += pageblock_nr_pages) {
		struct page *page = pfn_to_online_page(pfn);
		if (!page)
			continue;
		unset_migratetype_isolate(page, migratetype);
	}
K
KAMEZAWA Hiroyuki 已提交
215 216 217 218 219 220 221

	return -EBUSY;
}

/*
 * Make isolated pages available again.
 */
222 223
int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
			    unsigned migratetype)
K
KAMEZAWA Hiroyuki 已提交
224 225 226
{
	unsigned long pfn;
	struct page *page;
227 228 229 230

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

K
KAMEZAWA Hiroyuki 已提交
231 232 233 234
	for (pfn = start_pfn;
	     pfn < end_pfn;
	     pfn += pageblock_nr_pages) {
		page = __first_valid_page(pfn, pageblock_nr_pages);
235
		if (!page || !is_migrate_isolate_page(page))
K
KAMEZAWA Hiroyuki 已提交
236
			continue;
237
		unset_migratetype_isolate(page, migratetype);
K
KAMEZAWA Hiroyuki 已提交
238 239 240 241 242 243 244 245
	}
	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.
 *
246
 * Returns the last tested pfn.
K
KAMEZAWA Hiroyuki 已提交
247
 */
248
static unsigned long
249 250
__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
				  bool skip_hwpoisoned_pages)
K
KAMEZAWA Hiroyuki 已提交
251 252 253 254 255 256 257 258 259
{
	struct page *page;

	while (pfn < end_pfn) {
		if (!pfn_valid_within(pfn)) {
			pfn++;
			continue;
		}
		page = pfn_to_page(pfn);
260
		if (PageBuddy(page))
261
			/*
262 263 264
			 * 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.
265
			 */
K
KAMEZAWA Hiroyuki 已提交
266
			pfn += 1 << page_order(page);
267 268
		else if (skip_hwpoisoned_pages && PageHWPoison(page))
			/* A HWPoisoned page cannot be also PageBuddy */
269
			pfn++;
K
KAMEZAWA Hiroyuki 已提交
270 271 272
		else
			break;
	}
273 274

	return pfn;
K
KAMEZAWA Hiroyuki 已提交
275 276
}

277
/* Caller should ensure that requested range is in a single zone */
278 279
int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
			bool skip_hwpoisoned_pages)
K
KAMEZAWA Hiroyuki 已提交
280
{
281
	unsigned long pfn, flags;
K
KAMEZAWA Hiroyuki 已提交
282
	struct page *page;
283
	struct zone *zone;
K
KAMEZAWA Hiroyuki 已提交
284 285

	/*
286 287 288
	 * 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 已提交
289 290 291
	 */
	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
		page = __first_valid_page(pfn, pageblock_nr_pages);
292
		if (page && !is_migrate_isolate_page(page))
K
KAMEZAWA Hiroyuki 已提交
293 294
			break;
	}
295 296
	page = __first_valid_page(start_pfn, end_pfn - start_pfn);
	if ((pfn < end_pfn) || !page)
K
KAMEZAWA Hiroyuki 已提交
297
		return -EBUSY;
298
	/* Check all pages are free or marked as ISOLATED */
299
	zone = page_zone(page);
300
	spin_lock_irqsave(&zone->lock, flags);
301
	pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
302
						skip_hwpoisoned_pages);
303
	spin_unlock_irqrestore(&zone->lock, flags);
304

305 306
	trace_test_pages_isolated(start_pfn, end_pfn, pfn);

307
	return pfn < end_pfn ? -EBUSY : 0;
K
KAMEZAWA Hiroyuki 已提交
308
}
309

310
struct page *alloc_migrate_target(struct page *page, unsigned long private)
311
{
312
	return new_page_nodemask(page, numa_node_id(), &node_states[N_MEMORY]);
313
}