1
2
3
4
5
6
7
8
9#include <linux/mm.h>
10#include <linux/slab.h>
11#include <linux/export.h>
12#include <linux/balloon_compaction.h>
13
14static void balloon_page_enqueue_one(struct balloon_dev_info *b_dev_info,
15 struct page *page)
16{
17
18
19
20
21
22
23 BUG_ON(!trylock_page(page));
24 balloon_page_insert(b_dev_info, page);
25 unlock_page(page);
26 __count_vm_event(BALLOON_INFLATE);
27}
28
29
30
31
32
33
34
35
36
37
38
39
40size_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) {
49 list_del(&page->lru);
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}
56EXPORT_SYMBOL_GPL(balloon_page_list_enqueue);
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76size_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
90
91
92
93 if (!trylock_page(page))
94 continue;
95
96 if (IS_ENABLED(CONFIG_BALLOON_COMPACTION) &&
97 PageIsolated(page)) {
98
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}
112EXPORT_SYMBOL_GPL(balloon_page_list_dequeue);
113
114
115
116
117
118
119
120
121
122
123
124struct page *balloon_page_alloc(void)
125{
126 struct page *page = alloc_page(balloon_mapping_gfp_mask() |
127 __GFP_NOMEMALLOC | __GFP_NORETRY |
128 __GFP_NOWARN);
129 return page;
130}
131EXPORT_SYMBOL_GPL(balloon_page_alloc);
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
147 struct page *page)
148{
149 unsigned long flags;
150
151 spin_lock_irqsave(&b_dev_info->pages_lock, flags);
152 balloon_page_enqueue_one(b_dev_info, page);
153 spin_unlock_irqrestore(&b_dev_info->pages_lock, flags);
154}
155EXPORT_SYMBOL_GPL(balloon_page_enqueue);
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info)
178{
179 unsigned long flags;
180 LIST_HEAD(pages);
181 int n_pages;
182
183 n_pages = balloon_page_list_dequeue(b_dev_info, &pages, 1);
184
185 if (n_pages != 1) {
186
187
188
189
190
191
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);
198 return NULL;
199 }
200 return list_first_entry(&pages, struct page, lru);
201}
202EXPORT_SYMBOL_GPL(balloon_page_dequeue);
203
204#ifdef CONFIG_BALLOON_COMPACTION
205
206bool balloon_page_isolate(struct page *page, isolate_mode_t mode)
207
208{
209 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
210 unsigned long flags;
211
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);
216
217 return true;
218}
219
220void balloon_page_putback(struct page *page)
221{
222 struct balloon_dev_info *b_dev_info = balloon_page_device(page);
223 unsigned long flags;
224
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
231
232
233int balloon_page_migrate(struct address_space *mapping,
234 struct page *newpage, struct page *page,
235 enum migrate_mode mode)
236{
237 struct balloon_dev_info *balloon = balloon_page_device(page);
238
239
240
241
242
243
244 if (mode == MIGRATE_SYNC_NO_COPY)
245 return -EINVAL;
246
247 VM_BUG_ON_PAGE(!PageLocked(page), page);
248 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
249
250 return balloon->migratepage(balloon, newpage, page, mode);
251}
252
253const struct address_space_operations balloon_aops = {
254 .migratepage = balloon_page_migrate,
255 .isolate_page = balloon_page_isolate,
256 .putback_page = balloon_page_putback,
257};
258EXPORT_SYMBOL_GPL(balloon_aops);
259
260#endif
261