dmaengine: at_hdmac: drop useless LIST_HEAD
[linux-2.6-block.git] / mm / page_isolation.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
a5d76b54
KH
2/*
3 * linux/mm/page_isolation.c
4 */
5
a5d76b54
KH
6#include <linux/mm.h>
7#include <linux/page-isolation.h>
8#include <linux/pageblock-flags.h>
ee6f509c 9#include <linux/memory.h>
c8721bbb 10#include <linux/hugetlb.h>
83358ece 11#include <linux/page_owner.h>
8b913238 12#include <linux/migrate.h>
a5d76b54
KH
13#include "internal.h"
14
0f0848e5
JK
15#define CREATE_TRACE_POINTS
16#include <trace/events/page_isolation.h>
17
d381c547 18static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
ee6f509c
MK
19{
20 struct zone *zone;
21 unsigned long flags, pfn;
22 struct memory_isolate_notify arg;
23 int notifier_ret;
24 int ret = -EBUSY;
25
26 zone = page_zone(page);
27
28 spin_lock_irqsave(&zone->lock, flags);
29
2c7452a0
MK
30 /*
31 * We assume the caller intended to SET migrate type to isolate.
32 * If it is already set, then someone else must have raced and
33 * set it before us. Return -EBUSY
34 */
35 if (is_migrate_isolate_page(page))
36 goto out;
37
ee6f509c
MK
38 pfn = page_to_pfn(page);
39 arg.start_pfn = pfn;
40 arg.nr_pages = pageblock_nr_pages;
41 arg.pages_found = 0;
42
43 /*
44 * It may be possible to isolate a pageblock even if the
45 * migratetype is not MIGRATE_MOVABLE. The memory isolation
46 * notifier chain is used by balloon drivers to return the
47 * number of pages in a range that are held by the balloon
48 * driver to shrink memory. If all the pages are accounted for
49 * by balloons, are free, or on the LRU, isolation can continue.
50 * Later, for example, when memory hotplug notifier runs, these
51 * pages reported as "can be isolated" should be isolated(freed)
52 * by the balloon driver through the memory notifier chain.
53 */
54 notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
55 notifier_ret = notifier_to_errno(notifier_ret);
56 if (notifier_ret)
57 goto out;
58 /*
59 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
60 * We just check MOVABLE pages.
61 */
d381c547 62 if (!has_unmovable_pages(zone, page, arg.pages_found, migratetype, flags))
ee6f509c
MK
63 ret = 0;
64
65 /*
ac34dcd2 66 * immobile means "not-on-lru" pages. If immobile is larger than
ee6f509c
MK
67 * removable-by-driver pages reported by notifier, we'll fail.
68 */
69
70out:
71 if (!ret) {
2139cbe6 72 unsigned long nr_pages;
4da2ce25 73 int mt = get_pageblock_migratetype(page);
2139cbe6 74
a458431e 75 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
ad53f92e 76 zone->nr_isolate_pageblock++;
02aa0cdd
VB
77 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
78 NULL);
2139cbe6 79
4da2ce25 80 __mod_zone_freepage_state(zone, -nr_pages, mt);
ee6f509c
MK
81 }
82
83 spin_unlock_irqrestore(&zone->lock, flags);
84 if (!ret)
ec25af84 85 drain_all_pages(zone);
ee6f509c
MK
86 return ret;
87}
88
c5b4e1b0 89static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
ee6f509c
MK
90{
91 struct zone *zone;
2139cbe6 92 unsigned long flags, nr_pages;
e3a2713c 93 bool isolated_page = false;
3c605096 94 unsigned int order;
76741e77 95 unsigned long pfn, buddy_pfn;
3c605096 96 struct page *buddy;
2139cbe6 97
ee6f509c
MK
98 zone = page_zone(page);
99 spin_lock_irqsave(&zone->lock, flags);
bbf9ce97 100 if (!is_migrate_isolate_page(page))
ee6f509c 101 goto out;
3c605096
JK
102
103 /*
104 * Because freepage with more than pageblock_order on isolated
105 * pageblock is restricted to merge due to freepage counting problem,
106 * it is possible that there is free buddy page.
107 * move_freepages_block() doesn't care of merge so we need other
108 * approach in order to merge them. Isolation and free will make
109 * these pages to be merged.
110 */
111 if (PageBuddy(page)) {
112 order = page_order(page);
113 if (order >= pageblock_order) {
76741e77
VB
114 pfn = page_to_pfn(page);
115 buddy_pfn = __find_buddy_pfn(pfn, order);
116 buddy = page + (buddy_pfn - pfn);
3c605096 117
13ad59df 118 if (pfn_valid_within(buddy_pfn) &&
1ae7013d 119 !is_migrate_isolate_page(buddy)) {
3c605096 120 __isolate_free_page(page, order);
e3a2713c 121 isolated_page = true;
3c605096
JK
122 }
123 }
124 }
125
126 /*
127 * If we isolate freepage with more than pageblock_order, there
128 * should be no freepage in the range, so we could avoid costly
129 * pageblock scanning for freepage moving.
130 */
131 if (!isolated_page) {
02aa0cdd 132 nr_pages = move_freepages_block(zone, page, migratetype, NULL);
3c605096
JK
133 __mod_zone_freepage_state(zone, nr_pages, migratetype);
134 }
a458431e 135 set_pageblock_migratetype(page, migratetype);
ad53f92e 136 zone->nr_isolate_pageblock--;
ee6f509c
MK
137out:
138 spin_unlock_irqrestore(&zone->lock, flags);
83358ece 139 if (isolated_page) {
46f24fd8 140 post_alloc_hook(page, order, __GFP_MOVABLE);
e3a2713c 141 __free_pages(page, order);
83358ece 142 }
ee6f509c
MK
143}
144
a5d76b54
KH
145static inline struct page *
146__first_valid_page(unsigned long pfn, unsigned long nr_pages)
147{
148 int i;
2ce13640
MH
149
150 for (i = 0; i < nr_pages; i++) {
151 struct page *page;
152
153 if (!pfn_valid_within(pfn + i))
154 continue;
155 page = pfn_to_online_page(pfn + i);
156 if (!page)
157 continue;
158 return page;
159 }
160 return NULL;
a5d76b54
KH
161}
162
163/*
164 * start_isolate_page_range() -- make page-allocation-type of range of pages
165 * to be MIGRATE_ISOLATE.
166 * @start_pfn: The lower PFN of the range to be isolated.
167 * @end_pfn: The upper PFN of the range to be isolated.
0815f3d8 168 * @migratetype: migrate type to set in error recovery.
a5d76b54
KH
169 *
170 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
171 * the range will never be allocated. Any free pages and pages freed in the
172 * future will not be allocated again.
173 *
174 * start_pfn/end_pfn must be aligned to pageblock_order.
2c7452a0
MK
175 * Return 0 on success and -EBUSY if any part of range cannot be isolated.
176 *
177 * There is no high level synchronization mechanism that prevents two threads
178 * from trying to isolate overlapping ranges. If this happens, one thread
179 * will notice pageblocks in the overlapping range already set to isolate.
180 * This happens in set_migratetype_isolate, and set_migratetype_isolate
181 * returns an error. We then clean up by restoring the migration type on
182 * pageblocks we may have modified and return -EBUSY to caller. This
183 * prevents two threads from simultaneously working on overlapping ranges.
a5d76b54 184 */
0815f3d8 185int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
d381c547 186 unsigned migratetype, int flags)
a5d76b54
KH
187{
188 unsigned long pfn;
189 unsigned long undo_pfn;
190 struct page *page;
191
fec174d6
NH
192 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
193 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
a5d76b54
KH
194
195 for (pfn = start_pfn;
196 pfn < end_pfn;
197 pfn += pageblock_nr_pages) {
198 page = __first_valid_page(pfn, pageblock_nr_pages);
b023f468 199 if (page &&
d381c547 200 set_migratetype_isolate(page, migratetype, flags)) {
a5d76b54
KH
201 undo_pfn = pfn;
202 goto undo;
203 }
204 }
205 return 0;
206undo:
207 for (pfn = start_pfn;
dbc0e4ce 208 pfn < undo_pfn;
2ce13640
MH
209 pfn += pageblock_nr_pages) {
210 struct page *page = pfn_to_online_page(pfn);
211 if (!page)
212 continue;
213 unset_migratetype_isolate(page, migratetype);
214 }
a5d76b54
KH
215
216 return -EBUSY;
217}
218
219/*
220 * Make isolated pages available again.
221 */
0815f3d8
MN
222int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
223 unsigned migratetype)
a5d76b54
KH
224{
225 unsigned long pfn;
226 struct page *page;
6f8d2b8a
WX
227
228 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
229 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
230
a5d76b54
KH
231 for (pfn = start_pfn;
232 pfn < end_pfn;
233 pfn += pageblock_nr_pages) {
234 page = __first_valid_page(pfn, pageblock_nr_pages);
bbf9ce97 235 if (!page || !is_migrate_isolate_page(page))
a5d76b54 236 continue;
0815f3d8 237 unset_migratetype_isolate(page, migratetype);
a5d76b54
KH
238 }
239 return 0;
240}
241/*
242 * Test all pages in the range is free(means isolated) or not.
243 * all pages in [start_pfn...end_pfn) must be in the same zone.
244 * zone->lock must be held before call this.
245 *
ec3b6882 246 * Returns the last tested pfn.
a5d76b54 247 */
fea85cff 248static unsigned long
b023f468
WC
249__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
250 bool skip_hwpoisoned_pages)
a5d76b54
KH
251{
252 struct page *page;
253
254 while (pfn < end_pfn) {
255 if (!pfn_valid_within(pfn)) {
256 pfn++;
257 continue;
258 }
259 page = pfn_to_page(pfn);
aa016d14 260 if (PageBuddy(page))
435b405c 261 /*
aa016d14
VB
262 * If the page is on a free list, it has to be on
263 * the correct MIGRATE_ISOLATE freelist. There is no
264 * simple way to verify that as VM_BUG_ON(), though.
435b405c 265 */
a5d76b54 266 pfn += 1 << page_order(page);
aa016d14
VB
267 else if (skip_hwpoisoned_pages && PageHWPoison(page))
268 /* A HWPoisoned page cannot be also PageBuddy */
b023f468 269 pfn++;
a5d76b54
KH
270 else
271 break;
272 }
fea85cff
JK
273
274 return pfn;
a5d76b54
KH
275}
276
b9eb6319 277/* Caller should ensure that requested range is in a single zone */
b023f468
WC
278int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
279 bool skip_hwpoisoned_pages)
a5d76b54 280{
6c1b7f68 281 unsigned long pfn, flags;
a5d76b54 282 struct page *page;
6c1b7f68 283 struct zone *zone;
a5d76b54 284
a5d76b54 285 /*
85dbe706
TC
286 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
287 * are not aligned to pageblock_nr_pages.
288 * Then we just check migratetype first.
a5d76b54
KH
289 */
290 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
291 page = __first_valid_page(pfn, pageblock_nr_pages);
bbf9ce97 292 if (page && !is_migrate_isolate_page(page))
a5d76b54
KH
293 break;
294 }
a70dcb96
GS
295 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
296 if ((pfn < end_pfn) || !page)
a5d76b54 297 return -EBUSY;
85dbe706 298 /* Check all pages are free or marked as ISOLATED */
a70dcb96 299 zone = page_zone(page);
6c1b7f68 300 spin_lock_irqsave(&zone->lock, flags);
fea85cff 301 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
b023f468 302 skip_hwpoisoned_pages);
6c1b7f68 303 spin_unlock_irqrestore(&zone->lock, flags);
fea85cff 304
0f0848e5
JK
305 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
306
fea85cff 307 return pfn < end_pfn ? -EBUSY : 0;
a5d76b54 308}
723a0644 309
666feb21 310struct page *alloc_migrate_target(struct page *page, unsigned long private)
723a0644 311{
8b913238 312 return new_page_nodemask(page, numa_node_id(), &node_states[N_MEMORY]);
723a0644 313}