Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * linux/mm/page_isolation.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/page-isolation.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/pageblock-flags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/hugetlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/page_owner.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/migrate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <trace/events/page_isolation.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct zone *zone = page_zone(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct page *unmovable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	spin_lock_irqsave(&zone->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	 * We assume the caller intended to SET migrate type to isolate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	 * If it is already set, then someone else must have raced and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	 * set it before us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if (is_migrate_isolate_page(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		spin_unlock_irqrestore(&zone->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	 * We just check MOVABLE pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	unmovable = has_unmovable_pages(zone, page, migratetype, isol_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (!unmovable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		unsigned long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		int mt = get_pageblock_migratetype(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		set_pageblock_migratetype(page, MIGRATE_ISOLATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		zone->nr_isolate_pageblock++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 									NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		__mod_zone_freepage_state(zone, -nr_pages, mt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		spin_unlock_irqrestore(&zone->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	spin_unlock_irqrestore(&zone->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (isol_flags & REPORT_FAILURE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		 * printk() with zone->lock held will likely trigger a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		 * lockdep splat, so defer it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		dump_page(unmovable, "unmovable page");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct zone *zone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	unsigned long flags, nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	bool isolated_page = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	unsigned int order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	unsigned long pfn, buddy_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct page *buddy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	zone = page_zone(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	spin_lock_irqsave(&zone->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (!is_migrate_isolate_page(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 * Because freepage with more than pageblock_order on isolated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	 * pageblock is restricted to merge due to freepage counting problem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	 * it is possible that there is free buddy page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	 * move_freepages_block() doesn't care of merge so we need other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	 * approach in order to merge them. Isolation and free will make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	 * these pages to be merged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (PageBuddy(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		order = buddy_order(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		if (order >= pageblock_order) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			pfn = page_to_pfn(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			buddy_pfn = __find_buddy_pfn(pfn, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			buddy = page + (buddy_pfn - pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			if (pfn_valid_within(buddy_pfn) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			    !is_migrate_isolate_page(buddy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 				__isolate_free_page(page, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 				isolated_page = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * If we isolate freepage with more than pageblock_order, there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 * should be no freepage in the range, so we could avoid costly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 * pageblock scanning for freepage moving.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	 * We didn't actually touch any of the isolated pages, so place them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	 * to the tail of the freelist. This is an optimization for memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	 * onlining - just onlined memory won't immediately be considered for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	 * allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (!isolated_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		nr_pages = move_freepages_block(zone, page, migratetype, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		__mod_zone_freepage_state(zone, nr_pages, migratetype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	set_pageblock_migratetype(page, migratetype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (isolated_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		__putback_isolated_page(page, order, migratetype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	zone->nr_isolate_pageblock--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	spin_unlock_irqrestore(&zone->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static inline struct page *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) __first_valid_page(unsigned long pfn, unsigned long nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		page = pfn_to_online_page(pfn + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  * start_isolate_page_range() - make page-allocation-type of range of pages to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * be MIGRATE_ISOLATE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  * @start_pfn:		The lower PFN of the range to be isolated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  * @end_pfn:		The upper PFN of the range to be isolated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  *			start_pfn/end_pfn must be aligned to pageblock_order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  * @migratetype:	Migrate type to set in error recovery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  * @flags:		The following flags are allowed (they can be combined in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  *			a bit mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  *			MEMORY_OFFLINE - isolate to offline (!allocate) memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  *					 e.g., skip over PageHWPoison() pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  *					 and PageOffline() pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  *			REPORT_FAILURE - report details about the failure to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  *			isolate the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * the range will never be allocated. Any free pages and pages freed in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  * future will not be allocated again. If specified range includes migrate types
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * pages in the range finally, the caller have to free all pages in the range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * test_page_isolated() can be used for test it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  * There is no high level synchronization mechanism that prevents two threads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * from trying to isolate overlapping ranges. If this happens, one thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * will notice pageblocks in the overlapping range already set to isolate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * This happens in set_migratetype_isolate, and set_migratetype_isolate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * returns an error. We then clean up by restoring the migration type on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  * pageblocks we may have modified and return -EBUSY to caller. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * prevents two threads from simultaneously working on overlapping ranges.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  * Please note that there is no strong synchronization with the page allocator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  * either. Pages might be freed while their page blocks are marked ISOLATED.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  * A call to drain_all_pages() after isolation can flush most of them. However
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  * in some cases pages might still end up on pcp lists and that would allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * for their allocation even when they are in fact isolated already. Depending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * on how strong of a guarantee the caller needs, further drain_all_pages()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  * might be needed (e.g. __offline_pages will need to call it after check for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * isolated range for a next retry).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * Return: 0 on success and -EBUSY if any part of range cannot be isolated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			     unsigned migratetype, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			     unsigned long *failed_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	unsigned long undo_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	for (pfn = start_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	     pfn < end_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	     pfn += pageblock_nr_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		page = __first_valid_page(pfn, pageblock_nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		if (page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			if (set_migratetype_isolate(page, migratetype, flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 				undo_pfn = pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 				if (failed_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 					*failed_pfn = page_to_pfn(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				goto undo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) undo:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	for (pfn = start_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	     pfn < undo_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	     pfn += pageblock_nr_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		struct page *page = pfn_to_online_page(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		unset_migratetype_isolate(page, migratetype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  * Make isolated pages available again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			    unsigned migratetype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	for (pfn = start_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	     pfn < end_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	     pfn += pageblock_nr_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		page = __first_valid_page(pfn, pageblock_nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		if (!page || !is_migrate_isolate_page(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		unset_migratetype_isolate(page, migratetype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  * Test all pages in the range is free(means isolated) or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  * all pages in [start_pfn...end_pfn) must be in the same zone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  * zone->lock must be held before call this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  * Returns the last tested pfn.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 				  int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	while (pfn < end_pfn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		if (!pfn_valid_within(pfn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			pfn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		page = pfn_to_page(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		if (PageBuddy(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			 * If the page is on a free list, it has to be on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			 * the correct MIGRATE_ISOLATE freelist. There is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			 * simple way to verify that as VM_BUG_ON(), though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			pfn += 1 << buddy_order(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			/* A HWPoisoned page cannot be also PageBuddy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			pfn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		else if ((flags & MEMORY_OFFLINE) && PageOffline(page) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			 !page_count(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			 * The responsible driver agreed to skip PageOffline()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			 * pages when offlining memory by dropping its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			 * reference in MEM_GOING_OFFLINE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			pfn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) #ifdef CONFIG_NO_GKI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (pfn < end_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		dump_page_owner(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	return pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /* Caller should ensure that requested range is in a single zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			int isol_flags, unsigned long *failed_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	unsigned long pfn, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	struct zone *zone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	 * are not aligned to pageblock_nr_pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	 * Then we just check migratetype first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		page = __first_valid_page(pfn, pageblock_nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		if (page && !is_migrate_isolate_page(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	page = __first_valid_page(start_pfn, end_pfn - start_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if ((pfn < end_pfn) || !page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	/* Check all pages are free or marked as ISOLATED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	zone = page_zone(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	spin_lock_irqsave(&zone->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	spin_unlock_irqrestore(&zone->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	trace_test_pages_isolated(start_pfn, end_pfn, pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (pfn < end_pfn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		page_pinner_failure_detect(pfn_to_page(pfn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		if (failed_pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			*failed_pfn = pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }