Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 2 | /* |
| 3 | * mm/balloon_compaction.c |
| 4 | * |
| 5 | * Common interface for making balloon pages movable by compaction. |
| 6 | * |
| 7 | * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com> |
| 8 | */ |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/export.h> |
| 12 | #include <linux/balloon_compaction.h> |
| 13 | |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 14 | static void balloon_page_enqueue_one(struct balloon_dev_info *b_dev_info, |
| 15 | struct page *page) |
| 16 | { |
| 17 | /* |
| 18 | * Block others from accessing the 'page' when we get around to |
| 19 | * establishing additional references. We should be the only one |
| 20 | * holding a reference to the 'page' at this point. If we are not, then |
| 21 | * memory corruption is possible and we should stop execution. |
| 22 | */ |
| 23 | BUG_ON(!trylock_page(page)); |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 24 | balloon_page_insert(b_dev_info, page); |
| 25 | unlock_page(page); |
| 26 | __count_vm_event(BALLOON_INFLATE); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * balloon_page_list_enqueue() - inserts a list of pages into the balloon page |
| 31 | * list. |
| 32 | * @b_dev_info: balloon device descriptor where we will insert a new page to |
| 33 | * @pages: pages to enqueue - allocated using balloon_page_alloc. |
| 34 | * |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 35 | * Driver must call this function to properly enqueue balloon pages before |
| 36 | * definitively removing them from the guest system. |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 37 | * |
| 38 | * Return: number of pages that were enqueued. |
| 39 | */ |
| 40 | size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info, |
| 41 | struct list_head *pages) |
| 42 | { |
| 43 | struct page *page, *tmp; |
| 44 | unsigned long flags; |
| 45 | size_t n_pages = 0; |
| 46 | |
| 47 | spin_lock_irqsave(&b_dev_info->pages_lock, flags); |
| 48 | list_for_each_entry_safe(page, tmp, pages, lru) { |
Wei Wang | dd42290 | 2019-07-18 17:27:20 +0800 | [diff] [blame] | 49 | list_del(&page->lru); |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 50 | balloon_page_enqueue_one(b_dev_info, page); |
| 51 | n_pages++; |
| 52 | } |
| 53 | spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); |
| 54 | return n_pages; |
| 55 | } |
| 56 | EXPORT_SYMBOL_GPL(balloon_page_list_enqueue); |
| 57 | |
| 58 | /** |
| 59 | * balloon_page_list_dequeue() - removes pages from balloon's page list and |
| 60 | * returns a list of the pages. |
| 61 | * @b_dev_info: balloon device decriptor where we will grab a page from. |
| 62 | * @pages: pointer to the list of pages that would be returned to the caller. |
| 63 | * @n_req_pages: number of requested pages. |
| 64 | * |
| 65 | * Driver must call this function to properly de-allocate a previous enlisted |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 66 | * balloon pages before definitively releasing it back to the guest system. |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 67 | * This function tries to remove @n_req_pages from the ballooned pages and |
| 68 | * return them to the caller in the @pages list. |
| 69 | * |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 70 | * Note that this function may fail to dequeue some pages even if the balloon |
| 71 | * isn't empty - since the page list can be temporarily empty due to compaction |
| 72 | * of isolated pages. |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 73 | * |
| 74 | * Return: number of pages that were added to the @pages list. |
| 75 | */ |
| 76 | size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info, |
| 77 | struct list_head *pages, size_t n_req_pages) |
| 78 | { |
| 79 | struct page *page, *tmp; |
| 80 | unsigned long flags; |
| 81 | size_t n_pages = 0; |
| 82 | |
| 83 | spin_lock_irqsave(&b_dev_info->pages_lock, flags); |
| 84 | list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) { |
| 85 | if (n_pages == n_req_pages) |
| 86 | break; |
| 87 | |
| 88 | /* |
| 89 | * Block others from accessing the 'page' while we get around to |
| 90 | * establishing additional references and preparing the 'page' |
| 91 | * to be released by the balloon driver. |
| 92 | */ |
| 93 | if (!trylock_page(page)) |
| 94 | continue; |
| 95 | |
| 96 | if (IS_ENABLED(CONFIG_BALLOON_COMPACTION) && |
| 97 | PageIsolated(page)) { |
| 98 | /* raced with isolation */ |
| 99 | unlock_page(page); |
| 100 | continue; |
| 101 | } |
| 102 | balloon_page_delete(page); |
| 103 | __count_vm_event(BALLOON_DEFLATE); |
| 104 | list_add(&page->lru, pages); |
| 105 | unlock_page(page); |
| 106 | n_pages++; |
| 107 | } |
| 108 | spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); |
| 109 | |
| 110 | return n_pages; |
| 111 | } |
| 112 | EXPORT_SYMBOL_GPL(balloon_page_list_dequeue); |
| 113 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 114 | /* |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 115 | * balloon_page_alloc - allocates a new page for insertion into the balloon |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 116 | * page list. |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 117 | * |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 118 | * Driver must call this function to properly allocate a new balloon page. |
| 119 | * Driver must call balloon_page_enqueue before definitively removing the page |
| 120 | * from the guest system. |
| 121 | * |
| 122 | * Return: struct page for the allocated page or NULL on allocation failure. |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 123 | */ |
| 124 | struct page *balloon_page_alloc(void) |
| 125 | { |
| 126 | struct page *page = alloc_page(balloon_mapping_gfp_mask() | |
Nadav Amit | 02fa5d7b | 2019-08-20 02:16:46 -0700 | [diff] [blame] | 127 | __GFP_NOMEMALLOC | __GFP_NORETRY | |
| 128 | __GFP_NOWARN); |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 129 | return page; |
| 130 | } |
| 131 | EXPORT_SYMBOL_GPL(balloon_page_alloc); |
| 132 | |
| 133 | /* |
Wei Wang | dd42290 | 2019-07-18 17:27:20 +0800 | [diff] [blame] | 134 | * balloon_page_enqueue - inserts a new page into the balloon page list. |
| 135 | * |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 136 | * @b_dev_info: balloon device descriptor where we will insert a new page |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 137 | * @page: new page to enqueue - allocated using balloon_page_alloc. |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 138 | * |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 139 | * Drivers must call this function to properly enqueue a new allocated balloon |
| 140 | * page before definitively removing the page from the guest system. |
Wei Wang | dd42290 | 2019-07-18 17:27:20 +0800 | [diff] [blame] | 141 | * |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 142 | * Drivers must not call balloon_page_enqueue on pages that have been pushed to |
| 143 | * a list with balloon_page_push before removing them with balloon_page_pop. To |
| 144 | * enqueue a list of pages, use balloon_page_list_enqueue instead. |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 145 | */ |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 146 | void balloon_page_enqueue(struct balloon_dev_info *b_dev_info, |
| 147 | struct page *page) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 148 | { |
| 149 | unsigned long flags; |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 150 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 151 | spin_lock_irqsave(&b_dev_info->pages_lock, flags); |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 152 | balloon_page_enqueue_one(b_dev_info, page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 153 | spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 154 | } |
| 155 | EXPORT_SYMBOL_GPL(balloon_page_enqueue); |
| 156 | |
| 157 | /* |
| 158 | * balloon_page_dequeue - removes a page from balloon's page list and returns |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 159 | * its address to allow the driver to release the page. |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 160 | * @b_dev_info: balloon device decriptor where we will grab a page from. |
| 161 | * |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 162 | * Driver must call this function to properly dequeue a previously enqueued page |
| 163 | * before definitively releasing it back to the guest system. |
| 164 | * |
| 165 | * Caller must perform its own accounting to ensure that this |
| 166 | * function is called only if some pages are actually enqueued. |
| 167 | * |
| 168 | * Note that this function may fail to dequeue some pages even if there are |
| 169 | * some enqueued pages - since the page list can be temporarily empty due to |
| 170 | * the compaction of isolated pages. |
| 171 | * |
| 172 | * TODO: remove the caller accounting requirements, and allow caller to wait |
| 173 | * until all pages can be dequeued. |
| 174 | * |
| 175 | * Return: struct page for the dequeued page, or NULL if no page was dequeued. |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 176 | */ |
| 177 | struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info) |
| 178 | { |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 179 | unsigned long flags; |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 180 | LIST_HEAD(pages); |
| 181 | int n_pages; |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 182 | |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 183 | n_pages = balloon_page_list_dequeue(b_dev_info, &pages, 1); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 184 | |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 185 | if (n_pages != 1) { |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 186 | /* |
| 187 | * If we are unable to dequeue a balloon page because the page |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 188 | * list is empty and there are no isolated pages, then something |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 189 | * went out of track and some balloon pages are lost. |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 190 | * BUG() here, otherwise the balloon driver may get stuck in |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 191 | * an infinite loop while attempting to release all its pages. |
| 192 | */ |
| 193 | spin_lock_irqsave(&b_dev_info->pages_lock, flags); |
| 194 | if (unlikely(list_empty(&b_dev_info->pages) && |
| 195 | !b_dev_info->isolated_pages)) |
| 196 | BUG(); |
| 197 | spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 198 | return NULL; |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 199 | } |
Nadav Amit | 418a3ab | 2019-04-25 04:54:42 -0700 | [diff] [blame] | 200 | return list_first_entry(&pages, struct page, lru); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 201 | } |
| 202 | EXPORT_SYMBOL_GPL(balloon_page_dequeue); |
| 203 | |
| 204 | #ifdef CONFIG_BALLOON_COMPACTION |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 205 | |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 206 | bool balloon_page_isolate(struct page *page, isolate_mode_t mode) |
| 207 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 208 | { |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 209 | struct balloon_dev_info *b_dev_info = balloon_page_device(page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 210 | unsigned long flags; |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 211 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 212 | spin_lock_irqsave(&b_dev_info->pages_lock, flags); |
| 213 | list_del(&page->lru); |
| 214 | b_dev_info->isolated_pages++; |
| 215 | spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 216 | |
| 217 | return true; |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 220 | void balloon_page_putback(struct page *page) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 221 | { |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 222 | struct balloon_dev_info *b_dev_info = balloon_page_device(page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 223 | unsigned long flags; |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 224 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 225 | spin_lock_irqsave(&b_dev_info->pages_lock, flags); |
| 226 | list_add(&page->lru, &b_dev_info->pages); |
| 227 | b_dev_info->isolated_pages--; |
| 228 | spin_unlock_irqrestore(&b_dev_info->pages_lock, flags); |
| 229 | } |
| 230 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 231 | |
| 232 | /* move_to_new_page() counterpart for a ballooned page */ |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 233 | int balloon_page_migrate(struct address_space *mapping, |
| 234 | struct page *newpage, struct page *page, |
| 235 | enum migrate_mode mode) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 236 | { |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 237 | struct balloon_dev_info *balloon = balloon_page_device(page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 238 | |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 239 | /* |
| 240 | * We can not easily support the no copy case here so ignore it as it |
Michael S. Tsirkin | cfe6180 | 2019-07-18 08:19:24 -0400 | [diff] [blame] | 241 | * is unlikely to be used with balloon pages. See include/linux/hmm.h |
| 242 | * for a user of the MIGRATE_SYNC_NO_COPY mode. |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 243 | */ |
| 244 | if (mode == MIGRATE_SYNC_NO_COPY) |
| 245 | return -EINVAL; |
| 246 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 247 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 248 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 249 | |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 250 | return balloon->migratepage(balloon, newpage, page, mode); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 251 | } |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 252 | |
| 253 | const struct address_space_operations balloon_aops = { |
| 254 | .migratepage = balloon_page_migrate, |
| 255 | .isolate_page = balloon_page_isolate, |
| 256 | .putback_page = balloon_page_putback, |
| 257 | }; |
| 258 | EXPORT_SYMBOL_GPL(balloon_aops); |
| 259 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 260 | #endif /* CONFIG_BALLOON_COMPACTION */ |