Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
18468d93 RA |
2 | /* |
3 | * include/linux/balloon_compaction.h | |
4 | * | |
5 | * Common interface definitions for making balloon pages movable by compaction. | |
6 | * | |
4d3467e1 DH |
7 | * Balloon page migration makes use of the general non-lru movable page |
8 | * feature. | |
9 | * | |
10 | * page->private is used to reference the responsible balloon device. | |
11 | * page->mapping is used in context of non-lru page migration to reference | |
12 | * the address space operations for page isolation/migration/compaction. | |
18468d93 RA |
13 | * |
14 | * As the page isolation scanning step a compaction thread does is a lockless | |
15 | * procedure (from a page standpoint), it might bring some racy situations while | |
16 | * performing balloon page compaction. In order to sort out these racy scenarios | |
17 | * and safely perform balloon's page compaction and migration we must, always, | |
4d3467e1 | 18 | * ensure following these simple rules: |
18468d93 RA |
19 | * |
20 | * i. when updating a balloon's page ->mapping element, strictly do it under | |
21 | * the following lock order, independently of the far superior | |
22 | * locking scheme (lru_lock, balloon_lock): | |
23 | * +-page_lock(page); | |
24 | * +--spin_lock_irq(&b_dev_info->pages_lock); | |
25 | * ... page->mapping updates here ... | |
26 | * | |
4d3467e1 DH |
27 | * ii. isolation or dequeueing procedure must remove the page from balloon |
28 | * device page list under b_dev_info->pages_lock. | |
d6d86c0a | 29 | * |
18468d93 RA |
30 | * The functions provided by this interface are placed to help on coping with |
31 | * the aforementioned balloon page corner case, as well as to ensure the simple | |
32 | * set of exposed rules are satisfied while we are dealing with balloon pages | |
33 | * compaction / migration. | |
34 | * | |
35 | * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com> | |
36 | */ | |
37 | #ifndef _LINUX_BALLOON_COMPACTION_H | |
38 | #define _LINUX_BALLOON_COMPACTION_H | |
39 | #include <linux/pagemap.h> | |
40 | #include <linux/page-flags.h> | |
dd4123f3 | 41 | #include <linux/migrate.h> |
18468d93 RA |
42 | #include <linux/gfp.h> |
43 | #include <linux/err.h> | |
b1123ea6 | 44 | #include <linux/fs.h> |
c7cdff0e | 45 | #include <linux/list.h> |
18468d93 RA |
46 | |
47 | /* | |
48 | * Balloon device information descriptor. | |
49 | * This struct is used to allow the common balloon compaction interface | |
50 | * procedures to find the proper balloon device holding memory pages they'll | |
51 | * have to cope for page compaction / migration, as well as it serves the | |
52 | * balloon driver as a page book-keeper for its registered balloon devices. | |
53 | */ | |
54 | struct balloon_dev_info { | |
18468d93 RA |
55 | unsigned long isolated_pages; /* # of isolated pages for migration */ |
56 | spinlock_t pages_lock; /* Protection to pages list */ | |
57 | struct list_head pages; /* Pages enqueued & handled to Host */ | |
9d1ba805 KK |
58 | int (*migratepage)(struct balloon_dev_info *, struct page *newpage, |
59 | struct page *page, enum migrate_mode mode); | |
18468d93 RA |
60 | }; |
61 | ||
c7cdff0e MT |
62 | extern struct page *balloon_page_alloc(void); |
63 | extern void balloon_page_enqueue(struct balloon_dev_info *b_dev_info, | |
64 | struct page *page); | |
18468d93 | 65 | extern struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info); |
418a3ab1 NA |
66 | extern size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info, |
67 | struct list_head *pages); | |
68 | extern size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info, | |
69 | struct list_head *pages, size_t n_req_pages); | |
18468d93 | 70 | |
9d1ba805 | 71 | static inline void balloon_devinfo_init(struct balloon_dev_info *balloon) |
18468d93 | 72 | { |
9d1ba805 KK |
73 | balloon->isolated_pages = 0; |
74 | spin_lock_init(&balloon->pages_lock); | |
75 | INIT_LIST_HEAD(&balloon->pages); | |
76 | balloon->migratepage = NULL; | |
18468d93 RA |
77 | } |
78 | ||
18468d93 | 79 | #ifdef CONFIG_BALLOON_COMPACTION |
68f2736a | 80 | extern const struct movable_operations balloon_mops; |
18468d93 | 81 | |
18468d93 RA |
82 | /* |
83 | * balloon_page_insert - insert a page into the balloon's page list and make | |
9d1ba805 KK |
84 | * the page->private assignment accordingly. |
85 | * @balloon : pointer to balloon device | |
18468d93 | 86 | * @page : page to be assigned as a 'balloon page' |
18468d93 RA |
87 | * |
88 | * Caller must ensure the page is locked and the spin_lock protecting balloon | |
89 | * pages list is held before inserting a page into the balloon device. | |
90 | */ | |
9d1ba805 KK |
91 | static inline void balloon_page_insert(struct balloon_dev_info *balloon, |
92 | struct page *page) | |
18468d93 | 93 | { |
ca215086 | 94 | __SetPageOffline(page); |
68f2736a | 95 | __SetPageMovable(page, &balloon_mops); |
9d1ba805 KK |
96 | set_page_private(page, (unsigned long)balloon); |
97 | list_add(&page->lru, &balloon->pages); | |
18468d93 RA |
98 | } |
99 | ||
100 | /* | |
101 | * balloon_page_delete - delete a page from balloon's page list and clear | |
9d1ba805 | 102 | * the page->private assignement accordingly. |
18468d93 RA |
103 | * @page : page to be released from balloon's page list |
104 | * | |
105 | * Caller must ensure the page is locked and the spin_lock protecting balloon | |
106 | * pages list is held before deleting a page from the balloon device. | |
107 | */ | |
108 | static inline void balloon_page_delete(struct page *page) | |
109 | { | |
ca215086 | 110 | __ClearPageOffline(page); |
b1123ea6 | 111 | __ClearPageMovable(page); |
9d1ba805 | 112 | set_page_private(page, 0); |
b1123ea6 MK |
113 | /* |
114 | * No touch page.lru field once @page has been isolated | |
115 | * because VM is using the field. | |
116 | */ | |
117 | if (!PageIsolated(page)) | |
d6d86c0a | 118 | list_del(&page->lru); |
18468d93 RA |
119 | } |
120 | ||
121 | /* | |
122 | * balloon_page_device - get the b_dev_info descriptor for the balloon device | |
123 | * that enqueues the given page. | |
124 | */ | |
125 | static inline struct balloon_dev_info *balloon_page_device(struct page *page) | |
126 | { | |
9d1ba805 | 127 | return (struct balloon_dev_info *)page_private(page); |
18468d93 RA |
128 | } |
129 | ||
130 | static inline gfp_t balloon_mapping_gfp_mask(void) | |
131 | { | |
132 | return GFP_HIGHUSER_MOVABLE; | |
133 | } | |
134 | ||
18468d93 RA |
135 | #else /* !CONFIG_BALLOON_COMPACTION */ |
136 | ||
9d1ba805 KK |
137 | static inline void balloon_page_insert(struct balloon_dev_info *balloon, |
138 | struct page *page) | |
18468d93 | 139 | { |
ca215086 | 140 | __SetPageOffline(page); |
9d1ba805 | 141 | list_add(&page->lru, &balloon->pages); |
18468d93 RA |
142 | } |
143 | ||
144 | static inline void balloon_page_delete(struct page *page) | |
145 | { | |
ca215086 | 146 | __ClearPageOffline(page); |
18468d93 RA |
147 | list_del(&page->lru); |
148 | } | |
149 | ||
18468d93 RA |
150 | static inline gfp_t balloon_mapping_gfp_mask(void) |
151 | { | |
152 | return GFP_HIGHUSER; | |
153 | } | |
154 | ||
18468d93 | 155 | #endif /* CONFIG_BALLOON_COMPACTION */ |
c7cdff0e MT |
156 | |
157 | /* | |
158 | * balloon_page_push - insert a page into a page list. | |
159 | * @head : pointer to list | |
160 | * @page : page to be added | |
161 | * | |
162 | * Caller must ensure the page is private and protect the list. | |
163 | */ | |
164 | static inline void balloon_page_push(struct list_head *pages, struct page *page) | |
165 | { | |
166 | list_add(&page->lru, pages); | |
167 | } | |
168 | ||
169 | /* | |
170 | * balloon_page_pop - remove a page from a page list. | |
171 | * @head : pointer to list | |
172 | * @page : page to be added | |
173 | * | |
174 | * Caller must ensure the page is private and protect the list. | |
175 | */ | |
176 | static inline struct page *balloon_page_pop(struct list_head *pages) | |
177 | { | |
178 | struct page *page = list_first_entry_or_null(pages, struct page, lru); | |
179 | ||
180 | if (!page) | |
181 | return NULL; | |
182 | ||
183 | list_del(&page->lru); | |
184 | return page; | |
185 | } | |
18468d93 | 186 | #endif /* _LINUX_BALLOON_COMPACTION_H */ |