Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * mm/balloon_compaction.c |
| 3 | * |
| 4 | * Common interface for making balloon pages movable by compaction. |
| 5 | * |
| 6 | * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com> |
| 7 | */ |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/export.h> |
| 11 | #include <linux/balloon_compaction.h> |
| 12 | |
| 13 | /* |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 14 | * balloon_page_alloc - allocates a new page for insertion into the balloon |
| 15 | * page list. |
| 16 | * |
| 17 | * Driver must call it to properly allocate a new enlisted balloon page. |
| 18 | * Driver must call balloon_page_enqueue before definitively removing it from |
| 19 | * the guest system. This function returns the page address for the recently |
| 20 | * allocated page or NULL in the case we fail to allocate a new page this turn. |
| 21 | */ |
| 22 | struct page *balloon_page_alloc(void) |
| 23 | { |
| 24 | struct page *page = alloc_page(balloon_mapping_gfp_mask() | |
| 25 | __GFP_NOMEMALLOC | __GFP_NORETRY); |
| 26 | return page; |
| 27 | } |
| 28 | EXPORT_SYMBOL_GPL(balloon_page_alloc); |
| 29 | |
| 30 | /* |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 31 | * balloon_page_enqueue - allocates a new page and inserts it into the balloon |
| 32 | * page list. |
Bogdan Sikora | bdb428c | 2015-12-27 14:58:23 +0100 | [diff] [blame] | 33 | * @b_dev_info: balloon device descriptor where we will insert a new page to |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 34 | * @page: new page to enqueue - allocated using balloon_page_alloc. |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 35 | * |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 36 | * Driver must call it to properly enqueue a new allocated balloon page |
Bogdan Sikora | bdb428c | 2015-12-27 14:58:23 +0100 | [diff] [blame] | 37 | * before definitively removing it from the guest system. |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 38 | * This function returns the page address for the recently enqueued page or |
| 39 | * NULL in the case we fail to allocate a new page this turn. |
| 40 | */ |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 41 | void balloon_page_enqueue(struct balloon_dev_info *b_dev_info, |
| 42 | struct page *page) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 43 | { |
| 44 | unsigned long flags; |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Block others from accessing the 'page' when we get around to |
| 48 | * establishing additional references. We should be the only one |
| 49 | * holding a reference to the 'page' at this point. |
| 50 | */ |
| 51 | BUG_ON(!trylock_page(page)); |
| 52 | spin_lock_irqsave(&b_dev_info->pages_lock, flags); |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 53 | balloon_page_insert(b_dev_info, page); |
Konstantin Khlebnikov | 09316c0 | 2014-10-09 15:29:32 -0700 | [diff] [blame] | 54 | __count_vm_event(BALLOON_INFLATE); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 55 | spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); |
| 56 | unlock_page(page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 57 | } |
| 58 | EXPORT_SYMBOL_GPL(balloon_page_enqueue); |
| 59 | |
| 60 | /* |
| 61 | * balloon_page_dequeue - removes a page from balloon's page list and returns |
| 62 | * the its address to allow the driver release the page. |
| 63 | * @b_dev_info: balloon device decriptor where we will grab a page from. |
| 64 | * |
| 65 | * Driver must call it to properly de-allocate a previous enlisted balloon page |
| 66 | * before definetively releasing it back to the guest system. |
| 67 | * This function returns the page address for the recently dequeued page or |
| 68 | * NULL in the case we find balloon's page list temporarily empty due to |
| 69 | * compaction isolated pages. |
| 70 | */ |
| 71 | struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info) |
| 72 | { |
| 73 | struct page *page, *tmp; |
| 74 | unsigned long flags; |
| 75 | bool dequeued_page; |
| 76 | |
| 77 | dequeued_page = false; |
Minchan Kim | 21ea9fb | 2015-12-28 08:35:13 +0900 | [diff] [blame] | 78 | spin_lock_irqsave(&b_dev_info->pages_lock, flags); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 79 | list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) { |
| 80 | /* |
| 81 | * Block others from accessing the 'page' while we get around |
| 82 | * establishing additional references and preparing the 'page' |
| 83 | * to be released by the balloon driver. |
| 84 | */ |
| 85 | if (trylock_page(page)) { |
Konstantin Khlebnikov | 4d88e6f | 2014-10-29 14:51:02 -0700 | [diff] [blame] | 86 | #ifdef CONFIG_BALLOON_COMPACTION |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 87 | if (PageIsolated(page)) { |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 88 | /* raced with isolation */ |
| 89 | unlock_page(page); |
| 90 | continue; |
| 91 | } |
Konstantin Khlebnikov | 4d88e6f | 2014-10-29 14:51:02 -0700 | [diff] [blame] | 92 | #endif |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 93 | balloon_page_delete(page); |
Konstantin Khlebnikov | 09316c0 | 2014-10-09 15:29:32 -0700 | [diff] [blame] | 94 | __count_vm_event(BALLOON_DEFLATE); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 95 | unlock_page(page); |
| 96 | dequeued_page = true; |
| 97 | break; |
| 98 | } |
| 99 | } |
Minchan Kim | 21ea9fb | 2015-12-28 08:35:13 +0900 | [diff] [blame] | 100 | spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 101 | |
| 102 | if (!dequeued_page) { |
| 103 | /* |
| 104 | * If we are unable to dequeue a balloon page because the page |
| 105 | * list is empty and there is no isolated pages, then something |
| 106 | * went out of track and some balloon pages are lost. |
| 107 | * BUG() here, otherwise the balloon driver may get stuck into |
| 108 | * an infinite loop while attempting to release all its pages. |
| 109 | */ |
| 110 | spin_lock_irqsave(&b_dev_info->pages_lock, flags); |
| 111 | if (unlikely(list_empty(&b_dev_info->pages) && |
| 112 | !b_dev_info->isolated_pages)) |
| 113 | BUG(); |
| 114 | spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); |
| 115 | page = NULL; |
| 116 | } |
| 117 | return page; |
| 118 | } |
| 119 | EXPORT_SYMBOL_GPL(balloon_page_dequeue); |
| 120 | |
| 121 | #ifdef CONFIG_BALLOON_COMPACTION |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 122 | |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 123 | bool balloon_page_isolate(struct page *page, isolate_mode_t mode) |
| 124 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 125 | { |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 126 | struct balloon_dev_info *b_dev_info = balloon_page_device(page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 127 | unsigned long flags; |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 128 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 129 | spin_lock_irqsave(&b_dev_info->pages_lock, flags); |
| 130 | list_del(&page->lru); |
| 131 | b_dev_info->isolated_pages++; |
| 132 | spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 133 | |
| 134 | return true; |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 137 | void balloon_page_putback(struct page *page) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 138 | { |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 139 | struct balloon_dev_info *b_dev_info = balloon_page_device(page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 140 | unsigned long flags; |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 141 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 142 | spin_lock_irqsave(&b_dev_info->pages_lock, flags); |
| 143 | list_add(&page->lru, &b_dev_info->pages); |
| 144 | b_dev_info->isolated_pages--; |
| 145 | spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); |
| 146 | } |
| 147 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 148 | |
| 149 | /* move_to_new_page() counterpart for a ballooned page */ |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 150 | int balloon_page_migrate(struct address_space *mapping, |
| 151 | struct page *newpage, struct page *page, |
| 152 | enum migrate_mode mode) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 153 | { |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 154 | struct balloon_dev_info *balloon = balloon_page_device(page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 155 | |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 156 | /* |
| 157 | * We can not easily support the no copy case here so ignore it as it |
| 158 | * is unlikely to be use with ballon pages. See include/linux/hmm.h for |
| 159 | * user of the MIGRATE_SYNC_NO_COPY mode. |
| 160 | */ |
| 161 | if (mode == MIGRATE_SYNC_NO_COPY) |
| 162 | return -EINVAL; |
| 163 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 164 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 165 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 166 | |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 167 | return balloon->migratepage(balloon, newpage, page, mode); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 168 | } |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 169 | |
| 170 | const struct address_space_operations balloon_aops = { |
| 171 | .migratepage = balloon_page_migrate, |
| 172 | .isolate_page = balloon_page_isolate, |
| 173 | .putback_page = balloon_page_putback, |
| 174 | }; |
| 175 | EXPORT_SYMBOL_GPL(balloon_aops); |
| 176 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 177 | #endif /* CONFIG_BALLOON_COMPACTION */ |