• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * include/linux/balloon_compaction.h
4  *
5  * Common interface definitions for making balloon pages movable by compaction.
6  *
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.
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,
18  * ensure following these simple rules:
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  *
27  *  ii. isolation or dequeueing procedure must remove the page from balloon
28  *      device page list under b_dev_info->pages_lock.
29  *
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>
41 #include <linux/migrate.h>
42 #include <linux/gfp.h>
43 #include <linux/err.h>
44 #include <linux/fs.h>
45 #include <linux/list.h>
46 #include <linux/mem_relinquish.h>
47 
48 /*
49  * Balloon device information descriptor.
50  * This struct is used to allow the common balloon compaction interface
51  * procedures to find the proper balloon device holding memory pages they'll
52  * have to cope for page compaction / migration, as well as it serves the
53  * balloon driver as a page book-keeper for its registered balloon devices.
54  */
55 struct balloon_dev_info {
56 	unsigned long isolated_pages;	/* # of isolated pages for migration */
57 	spinlock_t pages_lock;		/* Protection to pages list */
58 	struct list_head pages;		/* Pages enqueued & handled to Host */
59 	int (*migratepage)(struct balloon_dev_info *, struct page *newpage,
60 			struct page *page, enum migrate_mode mode);
61 };
62 
63 extern struct page *balloon_page_alloc(void);
64 extern void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
65 				 struct page *page);
66 extern struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info);
67 extern size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,
68 				      struct list_head *pages);
69 extern size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,
70 				     struct list_head *pages, size_t n_req_pages);
71 
balloon_devinfo_init(struct balloon_dev_info * balloon)72 static inline void balloon_devinfo_init(struct balloon_dev_info *balloon)
73 {
74 	balloon->isolated_pages = 0;
75 	spin_lock_init(&balloon->pages_lock);
76 	INIT_LIST_HEAD(&balloon->pages);
77 	balloon->migratepage = NULL;
78 }
79 
80 #ifdef CONFIG_BALLOON_COMPACTION
81 extern const struct movable_operations balloon_mops;
82 
83 /*
84  * balloon_page_insert - insert a page into the balloon's page list and make
85  *			 the page->private assignment accordingly.
86  * @balloon : pointer to balloon device
87  * @page    : page to be assigned as a 'balloon page'
88  *
89  * Caller must ensure the page is locked and the spin_lock protecting balloon
90  * pages list is held before inserting a page into the balloon device.
91  */
balloon_page_insert(struct balloon_dev_info * balloon,struct page * page)92 static inline void balloon_page_insert(struct balloon_dev_info *balloon,
93 				       struct page *page)
94 {
95 	__SetPageOffline(page);
96 	__SetPageMovable(page, &balloon_mops);
97 	set_page_private(page, (unsigned long)balloon);
98 	list_add(&page->lru, &balloon->pages);
99 	page_relinquish(page);
100 }
101 
102 /*
103  * balloon_page_delete - delete a page from balloon's page list and clear
104  *			 the page->private assignement accordingly.
105  * @page    : page to be released from balloon's page list
106  *
107  * Caller must ensure the page is locked and the spin_lock protecting balloon
108  * pages list is held before deleting a page from the balloon device.
109  */
balloon_page_delete(struct page * page)110 static inline void balloon_page_delete(struct page *page)
111 {
112 	__ClearPageOffline(page);
113 	__ClearPageMovable(page);
114 	set_page_private(page, 0);
115 	/*
116 	 * No touch page.lru field once @page has been isolated
117 	 * because VM is using the field.
118 	 */
119 	if (!PageIsolated(page))
120 		list_del(&page->lru);
121 }
122 
123 /*
124  * balloon_page_device - get the b_dev_info descriptor for the balloon device
125  *			 that enqueues the given page.
126  */
balloon_page_device(struct page * page)127 static inline struct balloon_dev_info *balloon_page_device(struct page *page)
128 {
129 	return (struct balloon_dev_info *)page_private(page);
130 }
131 
balloon_mapping_gfp_mask(void)132 static inline gfp_t balloon_mapping_gfp_mask(void)
133 {
134 	return GFP_HIGHUSER_MOVABLE;
135 }
136 
137 #else /* !CONFIG_BALLOON_COMPACTION */
138 
balloon_page_insert(struct balloon_dev_info * balloon,struct page * page)139 static inline void balloon_page_insert(struct balloon_dev_info *balloon,
140 				       struct page *page)
141 {
142 	__SetPageOffline(page);
143 	list_add(&page->lru, &balloon->pages);
144 	page_relinquish(page);
145 }
146 
balloon_page_delete(struct page * page)147 static inline void balloon_page_delete(struct page *page)
148 {
149 	__ClearPageOffline(page);
150 	list_del(&page->lru);
151 }
152 
balloon_mapping_gfp_mask(void)153 static inline gfp_t balloon_mapping_gfp_mask(void)
154 {
155 	return GFP_HIGHUSER;
156 }
157 
158 #endif /* CONFIG_BALLOON_COMPACTION */
159 
160 /*
161  * balloon_page_push - insert a page into a page list.
162  * @head : pointer to list
163  * @page : page to be added
164  *
165  * Caller must ensure the page is private and protect the list.
166  */
balloon_page_push(struct list_head * pages,struct page * page)167 static inline void balloon_page_push(struct list_head *pages, struct page *page)
168 {
169 	list_add(&page->lru, pages);
170 }
171 
172 /*
173  * balloon_page_pop - remove a page from a page list.
174  * @head : pointer to list
175  * @page : page to be added
176  *
177  * Caller must ensure the page is private and protect the list.
178  */
balloon_page_pop(struct list_head * pages)179 static inline struct page *balloon_page_pop(struct list_head *pages)
180 {
181 	struct page *page = list_first_entry_or_null(pages, struct page, lru);
182 
183 	if (!page)
184 		return NULL;
185 
186 	list_del(&page->lru);
187 	return page;
188 }
189 #endif /* _LINUX_BALLOON_COMPACTION_H */
190