Commit | Line | Data |
---|---|---|
b20a3503 CL |
1 | /* |
2 | * Memory Migration functionality - linux/mm/migration.c | |
3 | * | |
4 | * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter | |
5 | * | |
6 | * Page migration was first developed in the context of the memory hotplug | |
7 | * project. The main authors of the migration code are: | |
8 | * | |
9 | * IWAMOTO Toshihiro <iwamoto@valinux.co.jp> | |
10 | * Hirokazu Takahashi <taka@valinux.co.jp> | |
11 | * Dave Hansen <haveblue@us.ibm.com> | |
12 | * Christoph Lameter <clameter@sgi.com> | |
13 | */ | |
14 | ||
15 | #include <linux/migrate.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/swap.h> | |
0697212a | 18 | #include <linux/swapops.h> |
b20a3503 | 19 | #include <linux/pagemap.h> |
e23ca00b | 20 | #include <linux/buffer_head.h> |
b20a3503 CL |
21 | #include <linux/mm_inline.h> |
22 | #include <linux/pagevec.h> | |
23 | #include <linux/rmap.h> | |
24 | #include <linux/topology.h> | |
25 | #include <linux/cpu.h> | |
26 | #include <linux/cpuset.h> | |
b20a3503 CL |
27 | |
28 | #include "internal.h" | |
29 | ||
b20a3503 CL |
30 | /* The maximum number of pages to take off the LRU for migration */ |
31 | #define MIGRATE_CHUNK_SIZE 256 | |
32 | ||
33 | #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) | |
34 | ||
35 | /* | |
36 | * Isolate one page from the LRU lists. If successful put it onto | |
37 | * the indicated list with elevated page count. | |
38 | * | |
39 | * Result: | |
40 | * -EBUSY: page not on LRU list | |
41 | * 0: page removed from LRU list and added to the specified list. | |
42 | */ | |
43 | int isolate_lru_page(struct page *page, struct list_head *pagelist) | |
44 | { | |
45 | int ret = -EBUSY; | |
46 | ||
47 | if (PageLRU(page)) { | |
48 | struct zone *zone = page_zone(page); | |
49 | ||
50 | spin_lock_irq(&zone->lru_lock); | |
51 | if (PageLRU(page)) { | |
52 | ret = 0; | |
53 | get_page(page); | |
54 | ClearPageLRU(page); | |
55 | if (PageActive(page)) | |
56 | del_page_from_active_list(zone, page); | |
57 | else | |
58 | del_page_from_inactive_list(zone, page); | |
59 | list_add_tail(&page->lru, pagelist); | |
60 | } | |
61 | spin_unlock_irq(&zone->lru_lock); | |
62 | } | |
63 | return ret; | |
64 | } | |
65 | ||
66 | /* | |
67 | * migrate_prep() needs to be called after we have compiled the list of pages | |
68 | * to be migrated using isolate_lru_page() but before we begin a series of calls | |
69 | * to migrate_pages(). | |
70 | */ | |
71 | int migrate_prep(void) | |
72 | { | |
b20a3503 CL |
73 | /* |
74 | * Clear the LRU lists so pages can be isolated. | |
75 | * Note that pages may be moved off the LRU after we have | |
76 | * drained them. Those pages will fail to migrate like other | |
77 | * pages that may be busy. | |
78 | */ | |
79 | lru_add_drain_all(); | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
84 | static inline void move_to_lru(struct page *page) | |
85 | { | |
86 | list_del(&page->lru); | |
87 | if (PageActive(page)) { | |
88 | /* | |
89 | * lru_cache_add_active checks that | |
90 | * the PG_active bit is off. | |
91 | */ | |
92 | ClearPageActive(page); | |
93 | lru_cache_add_active(page); | |
94 | } else { | |
95 | lru_cache_add(page); | |
96 | } | |
97 | put_page(page); | |
98 | } | |
99 | ||
100 | /* | |
101 | * Add isolated pages on the list back to the LRU. | |
102 | * | |
103 | * returns the number of pages put back. | |
104 | */ | |
105 | int putback_lru_pages(struct list_head *l) | |
106 | { | |
107 | struct page *page; | |
108 | struct page *page2; | |
109 | int count = 0; | |
110 | ||
111 | list_for_each_entry_safe(page, page2, l, lru) { | |
112 | move_to_lru(page); | |
113 | count++; | |
114 | } | |
115 | return count; | |
116 | } | |
117 | ||
0697212a CL |
118 | static inline int is_swap_pte(pte_t pte) |
119 | { | |
120 | return !pte_none(pte) && !pte_present(pte) && !pte_file(pte); | |
121 | } | |
122 | ||
123 | /* | |
124 | * Restore a potential migration pte to a working pte entry | |
125 | */ | |
126 | static void remove_migration_pte(struct vm_area_struct *vma, unsigned long addr, | |
127 | struct page *old, struct page *new) | |
128 | { | |
129 | struct mm_struct *mm = vma->vm_mm; | |
130 | swp_entry_t entry; | |
131 | pgd_t *pgd; | |
132 | pud_t *pud; | |
133 | pmd_t *pmd; | |
134 | pte_t *ptep, pte; | |
135 | spinlock_t *ptl; | |
136 | ||
137 | pgd = pgd_offset(mm, addr); | |
138 | if (!pgd_present(*pgd)) | |
139 | return; | |
140 | ||
141 | pud = pud_offset(pgd, addr); | |
142 | if (!pud_present(*pud)) | |
143 | return; | |
144 | ||
145 | pmd = pmd_offset(pud, addr); | |
146 | if (!pmd_present(*pmd)) | |
147 | return; | |
148 | ||
149 | ptep = pte_offset_map(pmd, addr); | |
150 | ||
151 | if (!is_swap_pte(*ptep)) { | |
152 | pte_unmap(ptep); | |
153 | return; | |
154 | } | |
155 | ||
156 | ptl = pte_lockptr(mm, pmd); | |
157 | spin_lock(ptl); | |
158 | pte = *ptep; | |
159 | if (!is_swap_pte(pte)) | |
160 | goto out; | |
161 | ||
162 | entry = pte_to_swp_entry(pte); | |
163 | ||
164 | if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old) | |
165 | goto out; | |
166 | ||
167 | inc_mm_counter(mm, anon_rss); | |
168 | get_page(new); | |
169 | pte = pte_mkold(mk_pte(new, vma->vm_page_prot)); | |
170 | if (is_write_migration_entry(entry)) | |
171 | pte = pte_mkwrite(pte); | |
172 | set_pte_at(mm, addr, ptep, pte); | |
173 | page_add_anon_rmap(new, vma, addr); | |
174 | out: | |
175 | pte_unmap_unlock(ptep, ptl); | |
176 | } | |
177 | ||
178 | /* | |
179 | * Get rid of all migration entries and replace them by | |
180 | * references to the indicated page. | |
181 | * | |
182 | * Must hold mmap_sem lock on at least one of the vmas containing | |
183 | * the page so that the anon_vma cannot vanish. | |
184 | */ | |
185 | static void remove_migration_ptes(struct page *old, struct page *new) | |
186 | { | |
187 | struct anon_vma *anon_vma; | |
188 | struct vm_area_struct *vma; | |
189 | unsigned long mapping; | |
190 | ||
191 | mapping = (unsigned long)new->mapping; | |
192 | ||
193 | if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0) | |
194 | return; | |
195 | ||
196 | /* | |
197 | * We hold the mmap_sem lock. So no need to call page_lock_anon_vma. | |
198 | */ | |
199 | anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON); | |
200 | spin_lock(&anon_vma->lock); | |
201 | ||
202 | list_for_each_entry(vma, &anon_vma->head, anon_vma_node) | |
203 | remove_migration_pte(vma, page_address_in_vma(new, vma), | |
204 | old, new); | |
205 | ||
206 | spin_unlock(&anon_vma->lock); | |
207 | } | |
208 | ||
209 | /* | |
210 | * Something used the pte of a page under migration. We need to | |
211 | * get to the page and wait until migration is finished. | |
212 | * When we return from this function the fault will be retried. | |
213 | * | |
214 | * This function is called from do_swap_page(). | |
215 | */ | |
216 | void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, | |
217 | unsigned long address) | |
218 | { | |
219 | pte_t *ptep, pte; | |
220 | spinlock_t *ptl; | |
221 | swp_entry_t entry; | |
222 | struct page *page; | |
223 | ||
224 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); | |
225 | pte = *ptep; | |
226 | if (!is_swap_pte(pte)) | |
227 | goto out; | |
228 | ||
229 | entry = pte_to_swp_entry(pte); | |
230 | if (!is_migration_entry(entry)) | |
231 | goto out; | |
232 | ||
233 | page = migration_entry_to_page(entry); | |
234 | ||
235 | get_page(page); | |
236 | pte_unmap_unlock(ptep, ptl); | |
237 | wait_on_page_locked(page); | |
238 | put_page(page); | |
239 | return; | |
240 | out: | |
241 | pte_unmap_unlock(ptep, ptl); | |
242 | } | |
243 | ||
b20a3503 | 244 | /* |
c3fcf8a5 | 245 | * Replace the page in the mapping. |
5b5c7120 CL |
246 | * |
247 | * The number of remaining references must be: | |
248 | * 1 for anonymous pages without a mapping | |
249 | * 2 for pages with a mapping | |
250 | * 3 for pages with a mapping and PagePrivate set. | |
b20a3503 | 251 | */ |
2d1db3b1 CL |
252 | static int migrate_page_move_mapping(struct address_space *mapping, |
253 | struct page *newpage, struct page *page) | |
b20a3503 | 254 | { |
b20a3503 CL |
255 | struct page **radix_pointer; |
256 | ||
b20a3503 CL |
257 | write_lock_irq(&mapping->tree_lock); |
258 | ||
259 | radix_pointer = (struct page **)radix_tree_lookup_slot( | |
260 | &mapping->page_tree, | |
261 | page_index(page)); | |
262 | ||
5b5c7120 CL |
263 | if (!page_mapping(page) || |
264 | page_count(page) != 2 + !!PagePrivate(page) || | |
b20a3503 CL |
265 | *radix_pointer != page) { |
266 | write_unlock_irq(&mapping->tree_lock); | |
e23ca00b | 267 | return -EAGAIN; |
b20a3503 CL |
268 | } |
269 | ||
270 | /* | |
271 | * Now we know that no one else is looking at the page. | |
b20a3503 CL |
272 | */ |
273 | get_page(newpage); | |
b20a3503 CL |
274 | if (PageSwapCache(page)) { |
275 | SetPageSwapCache(newpage); | |
276 | set_page_private(newpage, page_private(page)); | |
277 | } | |
278 | ||
279 | *radix_pointer = newpage; | |
280 | __put_page(page); | |
281 | write_unlock_irq(&mapping->tree_lock); | |
282 | ||
283 | return 0; | |
284 | } | |
b20a3503 CL |
285 | |
286 | /* | |
287 | * Copy the page to its new location | |
288 | */ | |
e7340f73 | 289 | static void migrate_page_copy(struct page *newpage, struct page *page) |
b20a3503 CL |
290 | { |
291 | copy_highpage(newpage, page); | |
292 | ||
293 | if (PageError(page)) | |
294 | SetPageError(newpage); | |
295 | if (PageReferenced(page)) | |
296 | SetPageReferenced(newpage); | |
297 | if (PageUptodate(page)) | |
298 | SetPageUptodate(newpage); | |
299 | if (PageActive(page)) | |
300 | SetPageActive(newpage); | |
301 | if (PageChecked(page)) | |
302 | SetPageChecked(newpage); | |
303 | if (PageMappedToDisk(page)) | |
304 | SetPageMappedToDisk(newpage); | |
305 | ||
306 | if (PageDirty(page)) { | |
307 | clear_page_dirty_for_io(page); | |
308 | set_page_dirty(newpage); | |
309 | } | |
310 | ||
311 | ClearPageSwapCache(page); | |
312 | ClearPageActive(page); | |
313 | ClearPagePrivate(page); | |
314 | set_page_private(page, 0); | |
315 | page->mapping = NULL; | |
316 | ||
317 | /* | |
318 | * If any waiters have accumulated on the new page then | |
319 | * wake them up. | |
320 | */ | |
321 | if (PageWriteback(newpage)) | |
322 | end_page_writeback(newpage); | |
323 | } | |
b20a3503 | 324 | |
1d8b85cc CL |
325 | /************************************************************ |
326 | * Migration functions | |
327 | ***********************************************************/ | |
328 | ||
329 | /* Always fail migration. Used for mappings that are not movable */ | |
2d1db3b1 CL |
330 | int fail_migrate_page(struct address_space *mapping, |
331 | struct page *newpage, struct page *page) | |
1d8b85cc CL |
332 | { |
333 | return -EIO; | |
334 | } | |
335 | EXPORT_SYMBOL(fail_migrate_page); | |
336 | ||
b20a3503 CL |
337 | /* |
338 | * Common logic to directly migrate a single page suitable for | |
339 | * pages that do not use PagePrivate. | |
340 | * | |
341 | * Pages are locked upon entry and exit. | |
342 | */ | |
2d1db3b1 CL |
343 | int migrate_page(struct address_space *mapping, |
344 | struct page *newpage, struct page *page) | |
b20a3503 CL |
345 | { |
346 | int rc; | |
347 | ||
348 | BUG_ON(PageWriteback(page)); /* Writeback must be complete */ | |
349 | ||
2d1db3b1 | 350 | rc = migrate_page_move_mapping(mapping, newpage, page); |
b20a3503 CL |
351 | |
352 | if (rc) | |
353 | return rc; | |
354 | ||
355 | migrate_page_copy(newpage, page); | |
356 | ||
357 | /* | |
358 | * Remove auxiliary swap entries and replace | |
359 | * them with real ptes. | |
360 | * | |
361 | * Note that a real pte entry will allow processes that are not | |
362 | * waiting on the page lock to use the new page via the page tables | |
363 | * before the new page is unlocked. | |
364 | */ | |
365 | remove_from_swap(newpage); | |
366 | return 0; | |
367 | } | |
368 | EXPORT_SYMBOL(migrate_page); | |
369 | ||
1d8b85cc CL |
370 | /* |
371 | * Migration function for pages with buffers. This function can only be used | |
372 | * if the underlying filesystem guarantees that no other references to "page" | |
373 | * exist. | |
374 | */ | |
2d1db3b1 CL |
375 | int buffer_migrate_page(struct address_space *mapping, |
376 | struct page *newpage, struct page *page) | |
1d8b85cc | 377 | { |
1d8b85cc CL |
378 | struct buffer_head *bh, *head; |
379 | int rc; | |
380 | ||
1d8b85cc | 381 | if (!page_has_buffers(page)) |
2d1db3b1 | 382 | return migrate_page(mapping, newpage, page); |
1d8b85cc CL |
383 | |
384 | head = page_buffers(page); | |
385 | ||
2d1db3b1 | 386 | rc = migrate_page_move_mapping(mapping, newpage, page); |
1d8b85cc CL |
387 | |
388 | if (rc) | |
389 | return rc; | |
390 | ||
391 | bh = head; | |
392 | do { | |
393 | get_bh(bh); | |
394 | lock_buffer(bh); | |
395 | bh = bh->b_this_page; | |
396 | ||
397 | } while (bh != head); | |
398 | ||
399 | ClearPagePrivate(page); | |
400 | set_page_private(newpage, page_private(page)); | |
401 | set_page_private(page, 0); | |
402 | put_page(page); | |
403 | get_page(newpage); | |
404 | ||
405 | bh = head; | |
406 | do { | |
407 | set_bh_page(bh, newpage, bh_offset(bh)); | |
408 | bh = bh->b_this_page; | |
409 | ||
410 | } while (bh != head); | |
411 | ||
412 | SetPagePrivate(newpage); | |
413 | ||
414 | migrate_page_copy(newpage, page); | |
415 | ||
416 | bh = head; | |
417 | do { | |
418 | unlock_buffer(bh); | |
419 | put_bh(bh); | |
420 | bh = bh->b_this_page; | |
421 | ||
422 | } while (bh != head); | |
423 | ||
424 | return 0; | |
425 | } | |
426 | EXPORT_SYMBOL(buffer_migrate_page); | |
427 | ||
8351a6e4 CL |
428 | static int fallback_migrate_page(struct address_space *mapping, |
429 | struct page *newpage, struct page *page) | |
430 | { | |
431 | /* | |
432 | * Default handling if a filesystem does not provide | |
433 | * a migration function. We can only migrate clean | |
434 | * pages so try to write out any dirty pages first. | |
435 | */ | |
436 | if (PageDirty(page)) { | |
437 | switch (pageout(page, mapping)) { | |
438 | case PAGE_KEEP: | |
439 | case PAGE_ACTIVATE: | |
440 | return -EAGAIN; | |
441 | ||
442 | case PAGE_SUCCESS: | |
443 | /* Relock since we lost the lock */ | |
444 | lock_page(page); | |
445 | /* Must retry since page state may have changed */ | |
446 | return -EAGAIN; | |
447 | ||
448 | case PAGE_CLEAN: | |
449 | ; /* try to migrate the page below */ | |
450 | } | |
451 | } | |
452 | ||
453 | /* | |
454 | * Buffers may be managed in a filesystem specific way. | |
455 | * We must have no buffers or drop them. | |
456 | */ | |
457 | if (page_has_buffers(page) && | |
458 | !try_to_release_page(page, GFP_KERNEL)) | |
459 | return -EAGAIN; | |
460 | ||
461 | return migrate_page(mapping, newpage, page); | |
462 | } | |
463 | ||
b20a3503 CL |
464 | /* |
465 | * migrate_pages | |
466 | * | |
467 | * Two lists are passed to this function. The first list | |
468 | * contains the pages isolated from the LRU to be migrated. | |
469 | * The second list contains new pages that the pages isolated | |
d75a0fcd | 470 | * can be moved to. |
b20a3503 CL |
471 | * |
472 | * The function returns after 10 attempts or if no pages | |
473 | * are movable anymore because to has become empty | |
474 | * or no retryable pages exist anymore. | |
475 | * | |
476 | * Return: Number of pages not migrated when "to" ran empty. | |
477 | */ | |
478 | int migrate_pages(struct list_head *from, struct list_head *to, | |
479 | struct list_head *moved, struct list_head *failed) | |
480 | { | |
481 | int retry; | |
482 | int nr_failed = 0; | |
483 | int pass = 0; | |
484 | struct page *page; | |
485 | struct page *page2; | |
486 | int swapwrite = current->flags & PF_SWAPWRITE; | |
487 | int rc; | |
488 | ||
489 | if (!swapwrite) | |
490 | current->flags |= PF_SWAPWRITE; | |
491 | ||
492 | redo: | |
493 | retry = 0; | |
494 | ||
495 | list_for_each_entry_safe(page, page2, from, lru) { | |
496 | struct page *newpage = NULL; | |
497 | struct address_space *mapping; | |
498 | ||
499 | cond_resched(); | |
500 | ||
501 | rc = 0; | |
502 | if (page_count(page) == 1) | |
503 | /* page was freed from under us. So we are done. */ | |
504 | goto next; | |
505 | ||
506 | if (to && list_empty(to)) | |
507 | break; | |
508 | ||
509 | /* | |
510 | * Skip locked pages during the first two passes to give the | |
511 | * functions holding the lock time to release the page. Later we | |
512 | * use lock_page() to have a higher chance of acquiring the | |
513 | * lock. | |
514 | */ | |
515 | rc = -EAGAIN; | |
516 | if (pass > 2) | |
517 | lock_page(page); | |
518 | else | |
519 | if (TestSetPageLocked(page)) | |
520 | goto next; | |
521 | ||
522 | /* | |
523 | * Only wait on writeback if we have already done a pass where | |
524 | * we we may have triggered writeouts for lots of pages. | |
525 | */ | |
d75a0fcd | 526 | if (pass > 0) |
b20a3503 | 527 | wait_on_page_writeback(page); |
d75a0fcd | 528 | else |
b20a3503 CL |
529 | if (PageWriteback(page)) |
530 | goto unlock_page; | |
b20a3503 | 531 | |
c3fcf8a5 CL |
532 | /* |
533 | * Establish swap ptes for anonymous pages or destroy pte | |
534 | * maps for files. | |
535 | * | |
536 | * In order to reestablish file backed mappings the fault handlers | |
537 | * will take the radix tree_lock which may then be used to stop | |
538 | * processses from accessing this page until the new page is ready. | |
539 | * | |
540 | * A process accessing via a swap pte (an anonymous page) will take a | |
541 | * page_lock on the old page which will block the process until the | |
542 | * migration attempt is complete. At that time the PageSwapCache bit | |
543 | * will be examined. If the page was migrated then the PageSwapCache | |
544 | * bit will be clear and the operation to retrieve the page will be | |
545 | * retried which will find the new page in the radix tree. Then a new | |
546 | * direct mapping may be generated based on the radix tree contents. | |
547 | * | |
548 | * If the page was not migrated then the PageSwapCache bit | |
549 | * is still set and the operation may continue. | |
550 | */ | |
551 | rc = -EPERM; | |
552 | if (try_to_unmap(page, 1) == SWAP_FAIL) | |
553 | /* A vma has VM_LOCKED set -> permanent failure */ | |
2d1db3b1 | 554 | goto unlock_page; |
c3fcf8a5 CL |
555 | |
556 | rc = -EAGAIN; | |
557 | if (page_mapped(page)) | |
2d1db3b1 CL |
558 | goto unlock_page; |
559 | ||
560 | newpage = lru_to_page(to); | |
561 | lock_page(newpage); | |
562 | /* Prepare mapping for the new page.*/ | |
563 | newpage->index = page->index; | |
564 | newpage->mapping = page->mapping; | |
565 | ||
b20a3503 CL |
566 | /* |
567 | * Pages are properly locked and writeback is complete. | |
568 | * Try to migrate the page. | |
569 | */ | |
570 | mapping = page_mapping(page); | |
571 | if (!mapping) | |
572 | goto unlock_both; | |
573 | ||
8351a6e4 | 574 | if (mapping->a_ops->migratepage) |
b20a3503 CL |
575 | /* |
576 | * Most pages have a mapping and most filesystems | |
577 | * should provide a migration function. Anonymous | |
578 | * pages are part of swap space which also has its | |
579 | * own migration function. This is the most common | |
580 | * path for page migration. | |
581 | */ | |
2d1db3b1 CL |
582 | rc = mapping->a_ops->migratepage(mapping, |
583 | newpage, page); | |
8351a6e4 CL |
584 | else |
585 | rc = fallback_migrate_page(mapping, newpage, page); | |
b20a3503 CL |
586 | |
587 | unlock_both: | |
588 | unlock_page(newpage); | |
589 | ||
590 | unlock_page: | |
591 | unlock_page(page); | |
592 | ||
593 | next: | |
2d1db3b1 CL |
594 | if (rc) { |
595 | if (newpage) | |
596 | newpage->mapping = NULL; | |
597 | ||
598 | if (rc == -EAGAIN) | |
599 | retry++; | |
600 | else { | |
601 | /* Permanent failure */ | |
602 | list_move(&page->lru, failed); | |
603 | nr_failed++; | |
604 | } | |
b20a3503 CL |
605 | } else { |
606 | if (newpage) { | |
607 | /* Successful migration. Return page to LRU */ | |
608 | move_to_lru(newpage); | |
609 | } | |
610 | list_move(&page->lru, moved); | |
611 | } | |
612 | } | |
613 | if (retry && pass++ < 10) | |
614 | goto redo; | |
615 | ||
616 | if (!swapwrite) | |
617 | current->flags &= ~PF_SWAPWRITE; | |
618 | ||
619 | return nr_failed + retry; | |
620 | } | |
621 | ||
b20a3503 CL |
622 | /* |
623 | * Migrate the list 'pagelist' of pages to a certain destination. | |
624 | * | |
625 | * Specify destination with either non-NULL vma or dest_node >= 0 | |
626 | * Return the number of pages not migrated or error code | |
627 | */ | |
628 | int migrate_pages_to(struct list_head *pagelist, | |
629 | struct vm_area_struct *vma, int dest) | |
630 | { | |
631 | LIST_HEAD(newlist); | |
632 | LIST_HEAD(moved); | |
633 | LIST_HEAD(failed); | |
634 | int err = 0; | |
635 | unsigned long offset = 0; | |
636 | int nr_pages; | |
637 | struct page *page; | |
638 | struct list_head *p; | |
639 | ||
640 | redo: | |
641 | nr_pages = 0; | |
642 | list_for_each(p, pagelist) { | |
643 | if (vma) { | |
644 | /* | |
645 | * The address passed to alloc_page_vma is used to | |
646 | * generate the proper interleave behavior. We fake | |
647 | * the address here by an increasing offset in order | |
648 | * to get the proper distribution of pages. | |
649 | * | |
650 | * No decision has been made as to which page | |
651 | * a certain old page is moved to so we cannot | |
652 | * specify the correct address. | |
653 | */ | |
654 | page = alloc_page_vma(GFP_HIGHUSER, vma, | |
655 | offset + vma->vm_start); | |
656 | offset += PAGE_SIZE; | |
657 | } | |
658 | else | |
659 | page = alloc_pages_node(dest, GFP_HIGHUSER, 0); | |
660 | ||
661 | if (!page) { | |
662 | err = -ENOMEM; | |
663 | goto out; | |
664 | } | |
665 | list_add_tail(&page->lru, &newlist); | |
666 | nr_pages++; | |
667 | if (nr_pages > MIGRATE_CHUNK_SIZE) | |
668 | break; | |
669 | } | |
670 | err = migrate_pages(pagelist, &newlist, &moved, &failed); | |
671 | ||
672 | putback_lru_pages(&moved); /* Call release pages instead ?? */ | |
673 | ||
674 | if (err >= 0 && list_empty(&newlist) && !list_empty(pagelist)) | |
675 | goto redo; | |
676 | out: | |
677 | /* Return leftover allocated pages */ | |
678 | while (!list_empty(&newlist)) { | |
679 | page = list_entry(newlist.next, struct page, lru); | |
680 | list_del(&page->lru); | |
681 | __free_page(page); | |
682 | } | |
683 | list_splice(&failed, pagelist); | |
684 | if (err < 0) | |
685 | return err; | |
686 | ||
687 | /* Calculate number of leftover pages */ | |
688 | nr_pages = 0; | |
689 | list_for_each(p, pagelist) | |
690 | nr_pages++; | |
691 | return nr_pages; | |
692 | } |