blob: 5f34a9053ce0028d275fef67a07837166138dd54 [file] [log] [blame]
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07001/*
2 * linux/mm/page_isolation.c
3 */
4
KAMEZAWA Hiroyukia5d76b52007-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>
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -07009#include "internal.h"
10
Minchan Kim702d1a62012-07-31 16:43:56 -070011/* called while holding zone->lock */
12static void set_pageblock_isolate(struct page *page)
13{
14 if (get_pageblock_migratetype(page) == MIGRATE_ISOLATE)
15 return;
16
17 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
18 page_zone(page)->nr_pageblock_isolate++;
19}
20
21/* called while holding zone->lock */
22static void restore_pageblock_isolate(struct page *page, int migratetype)
23{
24 struct zone *zone = page_zone(page);
25 if (WARN_ON(get_pageblock_migratetype(page) != MIGRATE_ISOLATE))
26 return;
27
28 BUG_ON(zone->nr_pageblock_isolate <= 0);
29 set_pageblock_migratetype(page, migratetype);
30 zone->nr_pageblock_isolate--;
31}
32
Minchan Kimee6f5092012-07-31 16:43:50 -070033int set_migratetype_isolate(struct page *page)
34{
35 struct zone *zone;
36 unsigned long flags, pfn;
37 struct memory_isolate_notify arg;
38 int notifier_ret;
39 int ret = -EBUSY;
40
41 zone = page_zone(page);
42
43 spin_lock_irqsave(&zone->lock, flags);
44
45 pfn = page_to_pfn(page);
46 arg.start_pfn = pfn;
47 arg.nr_pages = pageblock_nr_pages;
48 arg.pages_found = 0;
49
50 /*
51 * It may be possible to isolate a pageblock even if the
52 * migratetype is not MIGRATE_MOVABLE. The memory isolation
53 * notifier chain is used by balloon drivers to return the
54 * number of pages in a range that are held by the balloon
55 * driver to shrink memory. If all the pages are accounted for
56 * by balloons, are free, or on the LRU, isolation can continue.
57 * Later, for example, when memory hotplug notifier runs, these
58 * pages reported as "can be isolated" should be isolated(freed)
59 * by the balloon driver through the memory notifier chain.
60 */
61 notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
62 notifier_ret = notifier_to_errno(notifier_ret);
63 if (notifier_ret)
64 goto out;
65 /*
66 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
67 * We just check MOVABLE pages.
68 */
69 if (!has_unmovable_pages(zone, page, arg.pages_found))
70 ret = 0;
71
72 /*
73 * immobile means "not-on-lru" paes. If immobile is larger than
74 * removable-by-driver pages reported by notifier, we'll fail.
75 */
76
77out:
78 if (!ret) {
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070079 unsigned long nr_pages;
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -070080 int migratetype = get_pageblock_migratetype(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070081
Minchan Kim702d1a62012-07-31 16:43:56 -070082 set_pageblock_isolate(page);
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070083 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE);
84
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -070085 __mod_zone_freepage_state(zone, -nr_pages, migratetype);
Minchan Kimee6f5092012-07-31 16:43:50 -070086 }
87
88 spin_unlock_irqrestore(&zone->lock, flags);
89 if (!ret)
90 drain_all_pages();
91 return ret;
92}
93
94void unset_migratetype_isolate(struct page *page, unsigned migratetype)
95{
96 struct zone *zone;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -070097 unsigned long flags, nr_pages;
98
Minchan Kimee6f5092012-07-31 16:43:50 -070099 zone = page_zone(page);
100 spin_lock_irqsave(&zone->lock, flags);
101 if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
102 goto out;
Bartlomiej Zolnierkiewicz2139cbe2012-10-08 16:32:00 -0700103 nr_pages = move_freepages_block(zone, page, migratetype);
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700104 __mod_zone_freepage_state(zone, nr_pages, migratetype);
Minchan Kim702d1a62012-07-31 16:43:56 -0700105 restore_pageblock_isolate(page, migratetype);
Minchan Kimee6f5092012-07-31 16:43:50 -0700106out:
107 spin_unlock_irqrestore(&zone->lock, flags);
108}
109
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700110static inline struct page *
111__first_valid_page(unsigned long pfn, unsigned long nr_pages)
112{
113 int i;
114 for (i = 0; i < nr_pages; i++)
115 if (pfn_valid_within(pfn + i))
116 break;
117 if (unlikely(i == nr_pages))
118 return NULL;
119 return pfn_to_page(pfn + i);
120}
121
122/*
123 * start_isolate_page_range() -- make page-allocation-type of range of pages
124 * to be MIGRATE_ISOLATE.
125 * @start_pfn: The lower PFN of the range to be isolated.
126 * @end_pfn: The upper PFN of the range to be isolated.
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200127 * @migratetype: migrate type to set in error recovery.
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700128 *
129 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
130 * the range will never be allocated. Any free pages and pages freed in the
131 * future will not be allocated again.
132 *
133 * start_pfn/end_pfn must be aligned to pageblock_order.
134 * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
135 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200136int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
137 unsigned migratetype)
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700138{
139 unsigned long pfn;
140 unsigned long undo_pfn;
141 struct page *page;
142
143 BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
144 BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
145
146 for (pfn = start_pfn;
147 pfn < end_pfn;
148 pfn += pageblock_nr_pages) {
149 page = __first_valid_page(pfn, pageblock_nr_pages);
150 if (page && set_migratetype_isolate(page)) {
151 undo_pfn = pfn;
152 goto undo;
153 }
154 }
155 return 0;
156undo:
157 for (pfn = start_pfn;
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800158 pfn < undo_pfn;
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700159 pfn += pageblock_nr_pages)
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200160 unset_migratetype_isolate(pfn_to_page(pfn), migratetype);
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700161
162 return -EBUSY;
163}
164
165/*
166 * Make isolated pages available again.
167 */
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200168int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
169 unsigned migratetype)
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700170{
171 unsigned long pfn;
172 struct page *page;
173 BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
174 BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
175 for (pfn = start_pfn;
176 pfn < end_pfn;
177 pfn += pageblock_nr_pages) {
178 page = __first_valid_page(pfn, pageblock_nr_pages);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800179 if (!page || get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700180 continue;
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200181 unset_migratetype_isolate(page, migratetype);
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700182 }
183 return 0;
184}
185/*
186 * Test all pages in the range is free(means isolated) or not.
187 * all pages in [start_pfn...end_pfn) must be in the same zone.
188 * zone->lock must be held before call this.
189 *
Michal Nazarewicz0815f3d2012-04-03 15:06:15 +0200190 * Returns 1 if all pages in the range are isolated.
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700191 */
192static int
193__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn)
194{
195 struct page *page;
196
197 while (pfn < end_pfn) {
198 if (!pfn_valid_within(pfn)) {
199 pfn++;
200 continue;
201 }
202 page = pfn_to_page(pfn);
Minchan Kim41d575a2012-10-08 16:32:14 -0700203 if (PageBuddy(page)) {
Minchan Kim435b4052012-10-08 16:32:16 -0700204 /*
205 * If race between isolatation and allocation happens,
206 * some free pages could be in MIGRATE_MOVABLE list
207 * although pageblock's migratation type of the page
208 * is MIGRATE_ISOLATE. Catch it and move the page into
209 * MIGRATE_ISOLATE list.
210 */
211 if (get_freepage_migratetype(page) != MIGRATE_ISOLATE) {
212 struct page *end_page;
213
214 end_page = page + (1 << page_order(page)) - 1;
215 move_freepages(page_zone(page), page, end_page,
216 MIGRATE_ISOLATE);
217 }
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700218 pfn += 1 << page_order(page);
Minchan Kim41d575a2012-10-08 16:32:14 -0700219 }
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700220 else if (page_count(page) == 0 &&
Minchan Kimb12c4ad2012-10-08 16:32:08 -0700221 get_freepage_migratetype(page) == MIGRATE_ISOLATE)
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700222 pfn += 1;
223 else
224 break;
225 }
226 if (pfn < end_pfn)
227 return 0;
228 return 1;
229}
230
231int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
232{
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700233 unsigned long pfn, flags;
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700234 struct page *page;
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700235 struct zone *zone;
236 int ret;
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700237
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700238 /*
239 * Note: pageblock_nr_page != MAX_ORDER. Then, chunks of free page
240 * is not aligned to pageblock_nr_pages.
241 * Then we just check pagetype fist.
242 */
243 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
244 page = __first_valid_page(pfn, pageblock_nr_pages);
KAMEZAWA Hiroyukidbc0e4c2007-11-14 16:59:12 -0800245 if (page && get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700246 break;
247 }
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800248 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
249 if ((pfn < end_pfn) || !page)
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700250 return -EBUSY;
251 /* Check all pages are free or Marked as ISOLATED */
Gerald Schaefera70dcb92008-11-06 12:53:36 -0800252 zone = page_zone(page);
Gerald Schaefer6c1b7f62008-10-02 14:50:16 -0700253 spin_lock_irqsave(&zone->lock, flags);
254 ret = __test_page_isolated_in_pageblock(start_pfn, end_pfn);
255 spin_unlock_irqrestore(&zone->lock, flags);
256 return ret ? 0 : -EBUSY;
KAMEZAWA Hiroyukia5d76b52007-10-16 01:26:11 -0700257}