blob: ba739b76e6c52e5584af1cdf46984a34fb6fcca1 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Rafael Aquini18468d92012-12-11 16:02:38 -08002/*
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
14/*
Michael S. Tsirkinc7cdff02017-10-13 16:11:48 +030015 * balloon_page_alloc - allocates a new page for insertion into the balloon
16 * page list.
17 *
18 * Driver must call it to properly allocate a new enlisted balloon page.
19 * Driver must call balloon_page_enqueue before definitively removing it from
20 * the guest system. This function returns the page address for the recently
21 * allocated page or NULL in the case we fail to allocate a new page this turn.
22 */
23struct page *balloon_page_alloc(void)
24{
25 struct page *page = alloc_page(balloon_mapping_gfp_mask() |
26 __GFP_NOMEMALLOC | __GFP_NORETRY);
27 return page;
28}
29EXPORT_SYMBOL_GPL(balloon_page_alloc);
30
31/*
Rafael Aquini18468d92012-12-11 16:02:38 -080032 * balloon_page_enqueue - allocates a new page and inserts it into the balloon
33 * page list.
Bogdan Sikorabdb428c2015-12-27 14:58:23 +010034 * @b_dev_info: balloon device descriptor where we will insert a new page to
Michael S. Tsirkinc7cdff02017-10-13 16:11:48 +030035 * @page: new page to enqueue - allocated using balloon_page_alloc.
Rafael Aquini18468d92012-12-11 16:02:38 -080036 *
Michael S. Tsirkinc7cdff02017-10-13 16:11:48 +030037 * Driver must call it to properly enqueue a new allocated balloon page
Bogdan Sikorabdb428c2015-12-27 14:58:23 +010038 * before definitively removing it from the guest system.
Rafael Aquini18468d92012-12-11 16:02:38 -080039 * This function returns the page address for the recently enqueued page or
40 * NULL in the case we fail to allocate a new page this turn.
41 */
Michael S. Tsirkinc7cdff02017-10-13 16:11:48 +030042void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
43 struct page *page)
Rafael Aquini18468d92012-12-11 16:02:38 -080044{
45 unsigned long flags;
Rafael Aquini18468d92012-12-11 16:02:38 -080046
47 /*
48 * Block others from accessing the 'page' when we get around to
49 * establishing additional references. We should be the only one
50 * holding a reference to the 'page' at this point.
51 */
52 BUG_ON(!trylock_page(page));
53 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070054 balloon_page_insert(b_dev_info, page);
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -070055 __count_vm_event(BALLOON_INFLATE);
Rafael Aquini18468d92012-12-11 16:02:38 -080056 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
57 unlock_page(page);
Rafael Aquini18468d92012-12-11 16:02:38 -080058}
59EXPORT_SYMBOL_GPL(balloon_page_enqueue);
60
61/*
62 * balloon_page_dequeue - removes a page from balloon's page list and returns
63 * the its address to allow the driver release the page.
64 * @b_dev_info: balloon device decriptor where we will grab a page from.
65 *
66 * Driver must call it to properly de-allocate a previous enlisted balloon page
67 * before definetively releasing it back to the guest system.
68 * This function returns the page address for the recently dequeued page or
69 * NULL in the case we find balloon's page list temporarily empty due to
70 * compaction isolated pages.
71 */
72struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
73{
74 struct page *page, *tmp;
75 unsigned long flags;
76 bool dequeued_page;
77
78 dequeued_page = false;
Minchan Kim21ea9fb2015-12-28 08:35:13 +090079 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
Rafael Aquini18468d92012-12-11 16:02:38 -080080 list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
81 /*
82 * Block others from accessing the 'page' while we get around
83 * establishing additional references and preparing the 'page'
84 * to be released by the balloon driver.
85 */
86 if (trylock_page(page)) {
Konstantin Khlebnikov4d88e6f2014-10-29 14:51:02 -070087#ifdef CONFIG_BALLOON_COMPACTION
Minchan Kimb1123ea62016-07-26 15:23:09 -070088 if (PageIsolated(page)) {
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -070089 /* raced with isolation */
90 unlock_page(page);
91 continue;
92 }
Konstantin Khlebnikov4d88e6f2014-10-29 14:51:02 -070093#endif
Rafael Aquini18468d92012-12-11 16:02:38 -080094 balloon_page_delete(page);
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -070095 __count_vm_event(BALLOON_DEFLATE);
Rafael Aquini18468d92012-12-11 16:02:38 -080096 unlock_page(page);
97 dequeued_page = true;
98 break;
99 }
100 }
Minchan Kim21ea9fb2015-12-28 08:35:13 +0900101 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
Rafael Aquini18468d92012-12-11 16:02:38 -0800102
103 if (!dequeued_page) {
104 /*
105 * If we are unable to dequeue a balloon page because the page
106 * list is empty and there is no isolated pages, then something
107 * went out of track and some balloon pages are lost.
108 * BUG() here, otherwise the balloon driver may get stuck into
109 * an infinite loop while attempting to release all its pages.
110 */
111 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
112 if (unlikely(list_empty(&b_dev_info->pages) &&
113 !b_dev_info->isolated_pages))
114 BUG();
115 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
116 page = NULL;
117 }
118 return page;
119}
120EXPORT_SYMBOL_GPL(balloon_page_dequeue);
121
122#ifdef CONFIG_BALLOON_COMPACTION
Rafael Aquini18468d92012-12-11 16:02:38 -0800123
Minchan Kimb1123ea62016-07-26 15:23:09 -0700124bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
125
Rafael Aquini18468d92012-12-11 16:02:38 -0800126{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700127 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800128 unsigned long flags;
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700129
Rafael Aquini18468d92012-12-11 16:02:38 -0800130 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
131 list_del(&page->lru);
132 b_dev_info->isolated_pages++;
133 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
Minchan Kimb1123ea62016-07-26 15:23:09 -0700134
135 return true;
Rafael Aquini18468d92012-12-11 16:02:38 -0800136}
137
Minchan Kimb1123ea62016-07-26 15:23:09 -0700138void balloon_page_putback(struct page *page)
Rafael Aquini18468d92012-12-11 16:02:38 -0800139{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700140 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800141 unsigned long flags;
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700142
Rafael Aquini18468d92012-12-11 16:02:38 -0800143 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
144 list_add(&page->lru, &b_dev_info->pages);
145 b_dev_info->isolated_pages--;
146 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
147}
148
Rafael Aquini18468d92012-12-11 16:02:38 -0800149
150/* move_to_new_page() counterpart for a ballooned page */
Minchan Kimb1123ea62016-07-26 15:23:09 -0700151int balloon_page_migrate(struct address_space *mapping,
152 struct page *newpage, struct page *page,
153 enum migrate_mode mode)
Rafael Aquini18468d92012-12-11 16:02:38 -0800154{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700155 struct balloon_dev_info *balloon = balloon_page_device(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800156
Jérôme Glisse2916ecc2017-09-08 16:12:06 -0700157 /*
158 * We can not easily support the no copy case here so ignore it as it
159 * is unlikely to be use with ballon pages. See include/linux/hmm.h for
160 * user of the MIGRATE_SYNC_NO_COPY mode.
161 */
162 if (mode == MIGRATE_SYNC_NO_COPY)
163 return -EINVAL;
164
Hugh Dickins7db76712015-11-05 18:49:49 -0800165 VM_BUG_ON_PAGE(!PageLocked(page), page);
166 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Rafael Aquini18468d92012-12-11 16:02:38 -0800167
Minchan Kimb1123ea62016-07-26 15:23:09 -0700168 return balloon->migratepage(balloon, newpage, page, mode);
Rafael Aquini18468d92012-12-11 16:02:38 -0800169}
Minchan Kimb1123ea62016-07-26 15:23:09 -0700170
171const struct address_space_operations balloon_aops = {
172 .migratepage = balloon_page_migrate,
173 .isolate_page = balloon_page_isolate,
174 .putback_page = balloon_page_putback,
175};
176EXPORT_SYMBOL_GPL(balloon_aops);
177
Rafael Aquini18468d92012-12-11 16:02:38 -0800178#endif /* CONFIG_BALLOON_COMPACTION */