2 * mm/balloon_compaction.c
4 * Common interface for making balloon pages movable by compaction.
6 * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
9 #include <linux/slab.h>
10 #include <linux/export.h>
11 #include <linux/balloon_compaction.h>
14 * balloon_page_enqueue - allocates a new page and inserts it into the balloon
16 * @b_dev_info: balloon device descriptor where we will insert a new page to
18 * Driver must call it to properly allocate a new enlisted balloon page
19 * before definitively removing it from the guest system.
20 * This function returns the page address for the recently enqueued page or
21 * NULL in the case we fail to allocate a new page this turn.
23 struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info)
26 struct page *page = alloc_page(balloon_mapping_gfp_mask() |
27 __GFP_NOMEMALLOC | __GFP_NORETRY);
32 * Block others from accessing the 'page' when we get around to
33 * establishing additional references. We should be the only one
34 * holding a reference to the 'page' at this point.
36 BUG_ON(!trylock_page(page));
37 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
38 balloon_page_insert(b_dev_info, page);
39 __count_vm_event(BALLOON_INFLATE);
40 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
44 EXPORT_SYMBOL_GPL(balloon_page_enqueue);
47 * balloon_page_dequeue - removes a page from balloon's page list and returns
48 * the its address to allow the driver release the page.
49 * @b_dev_info: balloon device decriptor where we will grab a page from.
51 * Driver must call it to properly de-allocate a previous enlisted balloon page
52 * before definetively releasing it back to the guest system.
53 * This function returns the page address for the recently dequeued page or
54 * NULL in the case we find balloon's page list temporarily empty due to
55 * compaction isolated pages.
57 struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
59 struct page *page, *tmp;
63 dequeued_page = false;
64 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
65 list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
67 * Block others from accessing the 'page' while we get around
68 * establishing additional references and preparing the 'page'
69 * to be released by the balloon driver.
71 if (trylock_page(page)) {
72 #ifdef CONFIG_BALLOON_COMPACTION
73 if (PageIsolated(page)) {
74 /* raced with isolation */
79 balloon_page_delete(page);
80 __count_vm_event(BALLOON_DEFLATE);
86 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
90 * If we are unable to dequeue a balloon page because the page
91 * list is empty and there is no isolated pages, then something
92 * went out of track and some balloon pages are lost.
93 * BUG() here, otherwise the balloon driver may get stuck into
94 * an infinite loop while attempting to release all its pages.
96 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
97 if (unlikely(list_empty(&b_dev_info->pages) &&
98 !b_dev_info->isolated_pages))
100 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
105 EXPORT_SYMBOL_GPL(balloon_page_dequeue);
107 #ifdef CONFIG_BALLOON_COMPACTION
109 bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
112 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
115 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
116 list_del(&page->lru);
117 b_dev_info->isolated_pages++;
118 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
123 void balloon_page_putback(struct page *page)
125 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
128 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
129 list_add(&page->lru, &b_dev_info->pages);
130 b_dev_info->isolated_pages--;
131 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
135 /* move_to_new_page() counterpart for a ballooned page */
136 int balloon_page_migrate(struct address_space *mapping,
137 struct page *newpage, struct page *page,
138 enum migrate_mode mode)
140 struct balloon_dev_info *balloon = balloon_page_device(page);
143 * We can not easily support the no copy case here so ignore it as it
144 * is unlikely to be use with ballon pages. See include/linux/hmm.h for
145 * user of the MIGRATE_SYNC_NO_COPY mode.
147 if (mode == MIGRATE_SYNC_NO_COPY)
150 VM_BUG_ON_PAGE(!PageLocked(page), page);
151 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
153 return balloon->migratepage(balloon, newpage, page, mode);
156 const struct address_space_operations balloon_aops = {
157 .migratepage = balloon_page_migrate,
158 .isolate_page = balloon_page_isolate,
159 .putback_page = balloon_page_putback,
161 EXPORT_SYMBOL_GPL(balloon_aops);
163 #endif /* CONFIG_BALLOON_COMPACTION */