blob: 38aa07d5b81cfa19fe309e69214dacd22a3973c7 [file] [log] [blame]
Rafael Aquini18468d92012-12-11 16:02:38 -08001/*
2 * include/linux/balloon_compaction.h
3 *
4 * Common interface definitions for making balloon pages movable by compaction.
5 *
6 * Despite being perfectly possible to perform ballooned pages migration, they
7 * make a special corner case to compaction scans because balloon pages are not
8 * enlisted at any LRU list like the other pages we do compact / migrate.
9 *
10 * As the page isolation scanning step a compaction thread does is a lockless
11 * procedure (from a page standpoint), it might bring some racy situations while
12 * performing balloon page compaction. In order to sort out these racy scenarios
13 * and safely perform balloon's page compaction and migration we must, always,
14 * ensure following these three simple rules:
15 *
16 * i. when updating a balloon's page ->mapping element, strictly do it under
17 * the following lock order, independently of the far superior
18 * locking scheme (lru_lock, balloon_lock):
19 * +-page_lock(page);
20 * +--spin_lock_irq(&b_dev_info->pages_lock);
21 * ... page->mapping updates here ...
22 *
23 * ii. before isolating or dequeueing a balloon page from the balloon device
24 * pages list, the page reference counter must be raised by one and the
25 * extra refcount must be dropped when the page is enqueued back into
26 * the balloon device page list, thus a balloon page keeps its reference
27 * counter raised only while it is under our special handling;
28 *
29 * iii. after the lockless scan step have selected a potential balloon page for
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -070030 * isolation, re-test the PageBalloon mark and the PagePrivate flag
Rafael Aquini18468d92012-12-11 16:02:38 -080031 * under the proper page lock, to ensure isolating a valid balloon page
32 * (not yet isolated, nor under release procedure)
33 *
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -070034 * iv. isolation or dequeueing procedure must clear PagePrivate flag under
35 * page lock together with removing page from balloon device page list.
36 *
Rafael Aquini18468d92012-12-11 16:02:38 -080037 * The functions provided by this interface are placed to help on coping with
38 * the aforementioned balloon page corner case, as well as to ensure the simple
39 * set of exposed rules are satisfied while we are dealing with balloon pages
40 * compaction / migration.
41 *
42 * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
43 */
44#ifndef _LINUX_BALLOON_COMPACTION_H
45#define _LINUX_BALLOON_COMPACTION_H
46#include <linux/pagemap.h>
47#include <linux/page-flags.h>
48#include <linux/migrate.h>
49#include <linux/gfp.h>
50#include <linux/err.h>
51
52/*
53 * Balloon device information descriptor.
54 * This struct is used to allow the common balloon compaction interface
55 * procedures to find the proper balloon device holding memory pages they'll
56 * have to cope for page compaction / migration, as well as it serves the
57 * balloon driver as a page book-keeper for its registered balloon devices.
58 */
59struct balloon_dev_info {
60 void *balloon_device; /* balloon device descriptor */
61 struct address_space *mapping; /* balloon special page->mapping */
62 unsigned long isolated_pages; /* # of isolated pages for migration */
63 spinlock_t pages_lock; /* Protection to pages list */
64 struct list_head pages; /* Pages enqueued & handled to Host */
65};
66
67extern struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info);
68extern struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info);
69extern struct balloon_dev_info *balloon_devinfo_alloc(
70 void *balloon_dev_descriptor);
71
72static inline void balloon_devinfo_free(struct balloon_dev_info *b_dev_info)
73{
74 kfree(b_dev_info);
75}
76
Rafael Aquini18468d92012-12-11 16:02:38 -080077#ifdef CONFIG_BALLOON_COMPACTION
78extern bool balloon_page_isolate(struct page *page);
79extern void balloon_page_putback(struct page *page);
80extern int balloon_page_migrate(struct page *newpage,
81 struct page *page, enum migrate_mode mode);
82extern struct address_space
83*balloon_mapping_alloc(struct balloon_dev_info *b_dev_info,
84 const struct address_space_operations *a_ops);
85
86static inline void balloon_mapping_free(struct address_space *balloon_mapping)
87{
88 kfree(balloon_mapping);
89}
90
91/*
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -070092 * __is_movable_balloon_page - helper to perform @page PageBalloon tests
Rafael Aquini18468d92012-12-11 16:02:38 -080093 */
94static inline bool __is_movable_balloon_page(struct page *page)
95{
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -070096 return PageBalloon(page);
Rafael Aquini18468d92012-12-11 16:02:38 -080097}
98
99/*
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700100 * balloon_page_movable - test PageBalloon to identify balloon pages
101 * and PagePrivate to check that the page is not
102 * isolated and can be moved by compaction/migration.
Rafael Aquini18468d92012-12-11 16:02:38 -0800103 *
104 * As we might return false positives in the case of a balloon page being just
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700105 * released under us, this need to be re-tested later, under the page lock.
Rafael Aquini18468d92012-12-11 16:02:38 -0800106 */
107static inline bool balloon_page_movable(struct page *page)
108{
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700109 return PageBalloon(page) && PagePrivate(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800110}
111
112/*
Rafael Aquini117aad12013-09-30 13:45:16 -0700113 * isolated_balloon_page - identify an isolated balloon page on private
114 * compaction/migration page lists.
Rafael Aquini117aad12013-09-30 13:45:16 -0700115 */
116static inline bool isolated_balloon_page(struct page *page)
117{
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700118 return PageBalloon(page);
Rafael Aquini117aad12013-09-30 13:45:16 -0700119}
120
121/*
Rafael Aquini18468d92012-12-11 16:02:38 -0800122 * balloon_page_insert - insert a page into the balloon's page list and make
123 * the page->mapping assignment accordingly.
124 * @page : page to be assigned as a 'balloon page'
125 * @mapping : allocated special 'balloon_mapping'
126 * @head : balloon's device page list head
127 *
128 * Caller must ensure the page is locked and the spin_lock protecting balloon
129 * pages list is held before inserting a page into the balloon device.
130 */
131static inline void balloon_page_insert(struct page *page,
132 struct address_space *mapping,
133 struct list_head *head)
134{
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700135 __SetPageBalloon(page);
136 SetPagePrivate(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800137 page->mapping = mapping;
138 list_add(&page->lru, head);
139}
140
141/*
142 * balloon_page_delete - delete a page from balloon's page list and clear
143 * the page->mapping assignement accordingly.
144 * @page : page to be released from balloon's page list
145 *
146 * Caller must ensure the page is locked and the spin_lock protecting balloon
147 * pages list is held before deleting a page from the balloon device.
148 */
149static inline void balloon_page_delete(struct page *page)
150{
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700151 __ClearPageBalloon(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800152 page->mapping = NULL;
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700153 if (PagePrivate(page)) {
154 ClearPagePrivate(page);
155 list_del(&page->lru);
156 }
Rafael Aquini18468d92012-12-11 16:02:38 -0800157}
158
159/*
160 * balloon_page_device - get the b_dev_info descriptor for the balloon device
161 * that enqueues the given page.
162 */
163static inline struct balloon_dev_info *balloon_page_device(struct page *page)
164{
165 struct address_space *mapping = page->mapping;
166 if (likely(mapping))
167 return mapping->private_data;
168
169 return NULL;
170}
171
172static inline gfp_t balloon_mapping_gfp_mask(void)
173{
174 return GFP_HIGHUSER_MOVABLE;
175}
176
177static inline bool balloon_compaction_check(void)
178{
179 return true;
180}
181
182#else /* !CONFIG_BALLOON_COMPACTION */
183
184static inline void *balloon_mapping_alloc(void *balloon_device,
185 const struct address_space_operations *a_ops)
186{
187 return ERR_PTR(-EOPNOTSUPP);
188}
189
190static inline void balloon_mapping_free(struct address_space *balloon_mapping)
191{
192 return;
193}
194
195static inline void balloon_page_insert(struct page *page,
196 struct address_space *mapping,
197 struct list_head *head)
198{
199 list_add(&page->lru, head);
200}
201
202static inline void balloon_page_delete(struct page *page)
203{
204 list_del(&page->lru);
205}
206
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700207static inline bool __is_movable_balloon_page(struct page *page)
208{
209 return false;
210}
211
Rafael Aquini18468d92012-12-11 16:02:38 -0800212static inline bool balloon_page_movable(struct page *page)
213{
214 return false;
215}
216
Rafael Aquini117aad12013-09-30 13:45:16 -0700217static inline bool isolated_balloon_page(struct page *page)
218{
219 return false;
220}
221
Rafael Aquini18468d92012-12-11 16:02:38 -0800222static inline bool balloon_page_isolate(struct page *page)
223{
224 return false;
225}
226
227static inline void balloon_page_putback(struct page *page)
228{
229 return;
230}
231
232static inline int balloon_page_migrate(struct page *newpage,
233 struct page *page, enum migrate_mode mode)
234{
235 return 0;
236}
237
238static inline gfp_t balloon_mapping_gfp_mask(void)
239{
240 return GFP_HIGHUSER;
241}
242
243static inline bool balloon_compaction_check(void)
244{
245 return false;
246}
247#endif /* CONFIG_BALLOON_COMPACTION */
248#endif /* _LINUX_BALLOON_COMPACTION_H */