2 * linux/mm/page_isolation.c
6 #include <linux/page-isolation.h>
7 #include <linux/pageblock-flags.h>
8 #include <linux/memory.h>
11 int set_migratetype_isolate(struct page *page)
14 unsigned long flags, pfn;
15 struct memory_isolate_notify arg;
19 zone = page_zone(page);
21 spin_lock_irqsave(&zone->lock, flags);
23 pfn = page_to_pfn(page);
25 arg.nr_pages = pageblock_nr_pages;
29 * It may be possible to isolate a pageblock even if the
30 * migratetype is not MIGRATE_MOVABLE. The memory isolation
31 * notifier chain is used by balloon drivers to return the
32 * number of pages in a range that are held by the balloon
33 * driver to shrink memory. If all the pages are accounted for
34 * by balloons, are free, or on the LRU, isolation can continue.
35 * Later, for example, when memory hotplug notifier runs, these
36 * pages reported as "can be isolated" should be isolated(freed)
37 * by the balloon driver through the memory notifier chain.
39 notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
40 notifier_ret = notifier_to_errno(notifier_ret);
44 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
45 * We just check MOVABLE pages.
47 if (!has_unmovable_pages(zone, page, arg.pages_found))
51 * immobile means "not-on-lru" paes. If immobile is larger than
52 * removable-by-driver pages reported by notifier, we'll fail.
57 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
58 move_freepages_block(zone, page, MIGRATE_ISOLATE);
61 spin_unlock_irqrestore(&zone->lock, flags);
67 void unset_migratetype_isolate(struct page *page, unsigned migratetype)
71 zone = page_zone(page);
72 spin_lock_irqsave(&zone->lock, flags);
73 if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
75 set_pageblock_migratetype(page, migratetype);
76 move_freepages_block(zone, page, migratetype);
78 spin_unlock_irqrestore(&zone->lock, flags);
81 static inline struct page *
82 __first_valid_page(unsigned long pfn, unsigned long nr_pages)
85 for (i = 0; i < nr_pages; i++)
86 if (pfn_valid_within(pfn + i))
88 if (unlikely(i == nr_pages))
90 return pfn_to_page(pfn + i);
94 * start_isolate_page_range() -- make page-allocation-type of range of pages
95 * to be MIGRATE_ISOLATE.
96 * @start_pfn: The lower PFN of the range to be isolated.
97 * @end_pfn: The upper PFN of the range to be isolated.
98 * @migratetype: migrate type to set in error recovery.
100 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
101 * the range will never be allocated. Any free pages and pages freed in the
102 * future will not be allocated again.
104 * start_pfn/end_pfn must be aligned to pageblock_order.
105 * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
107 int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
108 unsigned migratetype)
111 unsigned long undo_pfn;
114 BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
115 BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
117 for (pfn = start_pfn;
119 pfn += pageblock_nr_pages) {
120 page = __first_valid_page(pfn, pageblock_nr_pages);
121 if (page && set_migratetype_isolate(page)) {
128 for (pfn = start_pfn;
130 pfn += pageblock_nr_pages)
131 unset_migratetype_isolate(pfn_to_page(pfn), migratetype);
137 * Make isolated pages available again.
139 int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
140 unsigned migratetype)
144 BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
145 BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
146 for (pfn = start_pfn;
148 pfn += pageblock_nr_pages) {
149 page = __first_valid_page(pfn, pageblock_nr_pages);
150 if (!page || get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
152 unset_migratetype_isolate(page, migratetype);
157 * Test all pages in the range is free(means isolated) or not.
158 * all pages in [start_pfn...end_pfn) must be in the same zone.
159 * zone->lock must be held before call this.
161 * Returns 1 if all pages in the range are isolated.
164 __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn)
168 while (pfn < end_pfn) {
169 if (!pfn_valid_within(pfn)) {
173 page = pfn_to_page(pfn);
175 pfn += 1 << page_order(page);
176 else if (page_count(page) == 0 &&
177 page_private(page) == MIGRATE_ISOLATE)
187 int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
189 unsigned long pfn, flags;
195 * Note: pageblock_nr_page != MAX_ORDER. Then, chunks of free page
196 * is not aligned to pageblock_nr_pages.
197 * Then we just check pagetype fist.
199 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
200 page = __first_valid_page(pfn, pageblock_nr_pages);
201 if (page && get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
204 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
205 if ((pfn < end_pfn) || !page)
207 /* Check all pages are free or Marked as ISOLATED */
208 zone = page_zone(page);
209 spin_lock_irqsave(&zone->lock, flags);
210 ret = __test_page_isolated_in_pageblock(start_pfn, end_pfn);
211 spin_unlock_irqrestore(&zone->lock, flags);
212 return ret ? 0 : -EBUSY;