blob: 504bd724e6ab3fe9592ad02836ab455a2c09d8f7 [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>
Minchan Kimb1123ea62016-07-26 15:23:09 -070048#include <linux/node.h>
49#include <linux/compaction.h>
Rafael Aquini18468d92012-12-11 16:02:38 -080050#include <linux/gfp.h>
51#include <linux/err.h>
Minchan Kimb1123ea62016-07-26 15:23:09 -070052#include <linux/fs.h>
Rafael Aquini18468d92012-12-11 16:02:38 -080053
54/*
55 * Balloon device information descriptor.
56 * This struct is used to allow the common balloon compaction interface
57 * procedures to find the proper balloon device holding memory pages they'll
58 * have to cope for page compaction / migration, as well as it serves the
59 * balloon driver as a page book-keeper for its registered balloon devices.
60 */
61struct balloon_dev_info {
Rafael Aquini18468d92012-12-11 16:02:38 -080062 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 */
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070065 int (*migratepage)(struct balloon_dev_info *, struct page *newpage,
66 struct page *page, enum migrate_mode mode);
Minchan Kimb1123ea62016-07-26 15:23:09 -070067 struct inode *inode;
Rafael Aquini18468d92012-12-11 16:02:38 -080068};
69
70extern struct page *balloon_page_enqueue(struct balloon_dev_info *b_dev_info);
71extern struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info);
Rafael Aquini18468d92012-12-11 16:02:38 -080072
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070073static inline void balloon_devinfo_init(struct balloon_dev_info *balloon)
Rafael Aquini18468d92012-12-11 16:02:38 -080074{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070075 balloon->isolated_pages = 0;
76 spin_lock_init(&balloon->pages_lock);
77 INIT_LIST_HEAD(&balloon->pages);
78 balloon->migratepage = NULL;
Minchan Kimb1123ea62016-07-26 15:23:09 -070079 balloon->inode = NULL;
Rafael Aquini18468d92012-12-11 16:02:38 -080080}
81
Rafael Aquini18468d92012-12-11 16:02:38 -080082#ifdef CONFIG_BALLOON_COMPACTION
Minchan Kimb1123ea62016-07-26 15:23:09 -070083extern const struct address_space_operations balloon_aops;
84extern bool balloon_page_isolate(struct page *page,
85 isolate_mode_t mode);
Rafael Aquini18468d92012-12-11 16:02:38 -080086extern void balloon_page_putback(struct page *page);
Minchan Kimb1123ea62016-07-26 15:23:09 -070087extern int balloon_page_migrate(struct address_space *mapping,
88 struct page *newpage,
Rafael Aquini18468d92012-12-11 16:02:38 -080089 struct page *page, enum migrate_mode mode);
Rafael Aquini18468d92012-12-11 16:02:38 -080090
91/*
Rafael Aquini18468d92012-12-11 16:02:38 -080092 * balloon_page_insert - insert a page into the balloon's page list and make
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070093 * the page->private assignment accordingly.
94 * @balloon : pointer to balloon device
Rafael Aquini18468d92012-12-11 16:02:38 -080095 * @page : page to be assigned as a 'balloon page'
Rafael Aquini18468d92012-12-11 16:02:38 -080096 *
97 * Caller must ensure the page is locked and the spin_lock protecting balloon
98 * pages list is held before inserting a page into the balloon device.
99 */
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700100static inline void balloon_page_insert(struct balloon_dev_info *balloon,
101 struct page *page)
Rafael Aquini18468d92012-12-11 16:02:38 -0800102{
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700103 __SetPageBalloon(page);
Minchan Kimb1123ea62016-07-26 15:23:09 -0700104 __SetPageMovable(page, balloon->inode->i_mapping);
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700105 set_page_private(page, (unsigned long)balloon);
106 list_add(&page->lru, &balloon->pages);
Rafael Aquini18468d92012-12-11 16:02:38 -0800107}
108
109/*
110 * balloon_page_delete - delete a page from balloon's page list and clear
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700111 * the page->private assignement accordingly.
Rafael Aquini18468d92012-12-11 16:02:38 -0800112 * @page : page to be released from balloon's page list
113 *
114 * Caller must ensure the page is locked and the spin_lock protecting balloon
115 * pages list is held before deleting a page from the balloon device.
116 */
117static inline void balloon_page_delete(struct page *page)
118{
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700119 __ClearPageBalloon(page);
Minchan Kimb1123ea62016-07-26 15:23:09 -0700120 __ClearPageMovable(page);
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700121 set_page_private(page, 0);
Minchan Kimb1123ea62016-07-26 15:23:09 -0700122 /*
123 * No touch page.lru field once @page has been isolated
124 * because VM is using the field.
125 */
126 if (!PageIsolated(page))
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700127 list_del(&page->lru);
Rafael Aquini18468d92012-12-11 16:02:38 -0800128}
129
130/*
131 * balloon_page_device - get the b_dev_info descriptor for the balloon device
132 * that enqueues the given page.
133 */
134static inline struct balloon_dev_info *balloon_page_device(struct page *page)
135{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700136 return (struct balloon_dev_info *)page_private(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800137}
138
139static inline gfp_t balloon_mapping_gfp_mask(void)
140{
141 return GFP_HIGHUSER_MOVABLE;
142}
143
Rafael Aquini18468d92012-12-11 16:02:38 -0800144#else /* !CONFIG_BALLOON_COMPACTION */
145
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700146static inline void balloon_page_insert(struct balloon_dev_info *balloon,
147 struct page *page)
Rafael Aquini18468d92012-12-11 16:02:38 -0800148{
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -0700149 __SetPageBalloon(page);
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700150 list_add(&page->lru, &balloon->pages);
Rafael Aquini18468d92012-12-11 16:02:38 -0800151}
152
153static inline void balloon_page_delete(struct page *page)
154{
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -0700155 __ClearPageBalloon(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800156 list_del(&page->lru);
157}
158
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700159static inline bool __is_movable_balloon_page(struct page *page)
160{
161 return false;
162}
163
Rafael Aquini18468d92012-12-11 16:02:38 -0800164static inline bool balloon_page_movable(struct page *page)
165{
166 return false;
167}
168
Rafael Aquini117aad12013-09-30 13:45:16 -0700169static inline bool isolated_balloon_page(struct page *page)
170{
171 return false;
172}
173
Rafael Aquini18468d92012-12-11 16:02:38 -0800174static inline bool balloon_page_isolate(struct page *page)
175{
176 return false;
177}
178
179static inline void balloon_page_putback(struct page *page)
180{
181 return;
182}
183
184static inline int balloon_page_migrate(struct page *newpage,
185 struct page *page, enum migrate_mode mode)
186{
187 return 0;
188}
189
190static inline gfp_t balloon_mapping_gfp_mask(void)
191{
192 return GFP_HIGHUSER;
193}
194
Rafael Aquini18468d92012-12-11 16:02:38 -0800195#endif /* CONFIG_BALLOON_COMPACTION */
196#endif /* _LINUX_BALLOON_COMPACTION_H */