blob: da91df50ba31abfbe742afd9611682d5740a9afb [file] [log] [blame]
Rafael Aquini18468d92012-12-11 16:02:38 -08001/*
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/*
Rafael Aquini18468d92012-12-11 16:02:38 -080014 * balloon_page_enqueue - allocates a new page and inserts it into the balloon
15 * page list.
Bogdan Sikorabdb428c2015-12-27 14:58:23 +010016 * @b_dev_info: balloon device descriptor where we will insert a new page to
Rafael Aquini18468d92012-12-11 16:02:38 -080017 *
18 * Driver must call it to properly allocate a new enlisted balloon page
Bogdan Sikorabdb428c2015-12-27 14:58:23 +010019 * before definitively removing it from the guest system.
Rafael Aquini18468d92012-12-11 16:02:38 -080020 * 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.
22 */
23struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info)
24{
25 unsigned long flags;
26 struct page *page = alloc_page(balloon_mapping_gfp_mask() |
27 __GFP_NOMEMALLOC | __GFP_NORETRY);
28 if (!page)
29 return NULL;
30
31 /*
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.
35 */
36 BUG_ON(!trylock_page(page));
37 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070038 balloon_page_insert(b_dev_info, page);
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -070039 __count_vm_event(BALLOON_INFLATE);
Rafael Aquini18468d92012-12-11 16:02:38 -080040 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
41 unlock_page(page);
42 return page;
43}
44EXPORT_SYMBOL_GPL(balloon_page_enqueue);
45
46/*
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.
50 *
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.
56 */
57struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
58{
59 struct page *page, *tmp;
60 unsigned long flags;
61 bool dequeued_page;
62
63 dequeued_page = false;
Minchan Kim21ea9fb2015-12-28 08:35:13 +090064 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
Rafael Aquini18468d92012-12-11 16:02:38 -080065 list_for_each_entry_safe(page, tmp, &b_dev_info->pages, lru) {
66 /*
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.
70 */
71 if (trylock_page(page)) {
Konstantin Khlebnikov4d88e6f2014-10-29 14:51:02 -070072#ifdef CONFIG_BALLOON_COMPACTION
Minchan Kimb1123ea62016-07-26 15:23:09 -070073 if (PageIsolated(page)) {
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -070074 /* raced with isolation */
75 unlock_page(page);
76 continue;
77 }
Konstantin Khlebnikov4d88e6f2014-10-29 14:51:02 -070078#endif
Rafael Aquini18468d92012-12-11 16:02:38 -080079 balloon_page_delete(page);
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -070080 __count_vm_event(BALLOON_DEFLATE);
Rafael Aquini18468d92012-12-11 16:02:38 -080081 unlock_page(page);
82 dequeued_page = true;
83 break;
84 }
85 }
Minchan Kim21ea9fb2015-12-28 08:35:13 +090086 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
Rafael Aquini18468d92012-12-11 16:02:38 -080087
88 if (!dequeued_page) {
89 /*
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.
95 */
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))
99 BUG();
100 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
101 page = NULL;
102 }
103 return page;
104}
105EXPORT_SYMBOL_GPL(balloon_page_dequeue);
106
107#ifdef CONFIG_BALLOON_COMPACTION
Rafael Aquini18468d92012-12-11 16:02:38 -0800108
Minchan Kimb1123ea62016-07-26 15:23:09 -0700109bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
110
Rafael Aquini18468d92012-12-11 16:02:38 -0800111{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700112 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800113 unsigned long flags;
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700114
Rafael Aquini18468d92012-12-11 16:02:38 -0800115 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);
Minchan Kimb1123ea62016-07-26 15:23:09 -0700119
120 return true;
Rafael Aquini18468d92012-12-11 16:02:38 -0800121}
122
Minchan Kimb1123ea62016-07-26 15:23:09 -0700123void balloon_page_putback(struct page *page)
Rafael Aquini18468d92012-12-11 16:02:38 -0800124{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700125 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800126 unsigned long flags;
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700127
Rafael Aquini18468d92012-12-11 16:02:38 -0800128 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);
132}
133
Rafael Aquini18468d92012-12-11 16:02:38 -0800134
135/* move_to_new_page() counterpart for a ballooned page */
Minchan Kimb1123ea62016-07-26 15:23:09 -0700136int balloon_page_migrate(struct address_space *mapping,
137 struct page *newpage, struct page *page,
138 enum migrate_mode mode)
Rafael Aquini18468d92012-12-11 16:02:38 -0800139{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700140 struct balloon_dev_info *balloon = balloon_page_device(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800141
Hugh Dickins7db76712015-11-05 18:49:49 -0800142 VM_BUG_ON_PAGE(!PageLocked(page), page);
143 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Rafael Aquini18468d92012-12-11 16:02:38 -0800144
Minchan Kimb1123ea62016-07-26 15:23:09 -0700145 return balloon->migratepage(balloon, newpage, page, mode);
Rafael Aquini18468d92012-12-11 16:02:38 -0800146}
Minchan Kimb1123ea62016-07-26 15:23:09 -0700147
148const struct address_space_operations balloon_aops = {
149 .migratepage = balloon_page_migrate,
150 .isolate_page = balloon_page_isolate,
151 .putback_page = balloon_page_putback,
152};
153EXPORT_SYMBOL_GPL(balloon_aops);
154
Rafael Aquini18468d92012-12-11 16:02:38 -0800155#endif /* CONFIG_BALLOON_COMPACTION */