blob: 338aa27e4773b5fdf5793b039ffd9c1b975a247f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Rafael Aquini18468d92012-12-11 16:02:38 -08002/*
3 * include/linux/balloon_compaction.h
4 *
5 * Common interface definitions for making balloon pages movable by compaction.
6 *
David Hildenbrand4d3467e2019-03-05 15:42:18 -08007 * 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.
Rafael Aquini18468d92012-12-11 16:02:38 -080013 *
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,
David Hildenbrand4d3467e2019-03-05 15:42:18 -080018 * ensure following these simple rules:
Rafael Aquini18468d92012-12-11 16:02:38 -080019 *
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 *
David Hildenbrand4d3467e2019-03-05 15:42:18 -080027 * ii. isolation or dequeueing procedure must remove the page from balloon
28 * device page list under b_dev_info->pages_lock.
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -070029 *
Rafael Aquini18468d92012-12-11 16:02:38 -080030 * 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>
Minchan Kimdd4123f2016-07-26 15:26:50 -070041#include <linux/migrate.h>
Rafael Aquini18468d92012-12-11 16:02:38 -080042#include <linux/gfp.h>
43#include <linux/err.h>
Minchan Kimb1123ea62016-07-26 15:23:09 -070044#include <linux/fs.h>
Michael S. Tsirkinc7cdff02017-10-13 16:11:48 +030045#include <linux/list.h>
Rafael Aquini18468d92012-12-11 16:02:38 -080046
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 */
54struct balloon_dev_info {
Rafael Aquini18468d92012-12-11 16:02:38 -080055 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 */
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070058 int (*migratepage)(struct balloon_dev_info *, struct page *newpage,
59 struct page *page, enum migrate_mode mode);
Minchan Kimb1123ea62016-07-26 15:23:09 -070060 struct inode *inode;
Rafael Aquini18468d92012-12-11 16:02:38 -080061};
62
Michael S. Tsirkinc7cdff02017-10-13 16:11:48 +030063extern struct page *balloon_page_alloc(void);
64extern void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
65 struct page *page);
Rafael Aquini18468d92012-12-11 16:02:38 -080066extern struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info);
Nadav Amit418a3ab2019-04-25 04:54:42 -070067extern size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,
68 struct list_head *pages);
69extern size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,
70 struct list_head *pages, size_t n_req_pages);
Rafael Aquini18468d92012-12-11 16:02:38 -080071
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070072static inline void balloon_devinfo_init(struct balloon_dev_info *balloon)
Rafael Aquini18468d92012-12-11 16:02:38 -080073{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070074 balloon->isolated_pages = 0;
75 spin_lock_init(&balloon->pages_lock);
76 INIT_LIST_HEAD(&balloon->pages);
77 balloon->migratepage = NULL;
Minchan Kimb1123ea62016-07-26 15:23:09 -070078 balloon->inode = NULL;
Rafael Aquini18468d92012-12-11 16:02:38 -080079}
80
Rafael Aquini18468d92012-12-11 16:02:38 -080081#ifdef CONFIG_BALLOON_COMPACTION
Minchan Kimb1123ea62016-07-26 15:23:09 -070082extern const struct address_space_operations balloon_aops;
83extern bool balloon_page_isolate(struct page *page,
84 isolate_mode_t mode);
Rafael Aquini18468d92012-12-11 16:02:38 -080085extern void balloon_page_putback(struct page *page);
Minchan Kimb1123ea62016-07-26 15:23:09 -070086extern int balloon_page_migrate(struct address_space *mapping,
87 struct page *newpage,
Rafael Aquini18468d92012-12-11 16:02:38 -080088 struct page *page, enum migrate_mode mode);
Rafael Aquini18468d92012-12-11 16:02:38 -080089
90/*
Rafael Aquini18468d92012-12-11 16:02:38 -080091 * balloon_page_insert - insert a page into the balloon's page list and make
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070092 * the page->private assignment accordingly.
93 * @balloon : pointer to balloon device
Rafael Aquini18468d92012-12-11 16:02:38 -080094 * @page : page to be assigned as a 'balloon page'
Rafael Aquini18468d92012-12-11 16:02:38 -080095 *
96 * Caller must ensure the page is locked and the spin_lock protecting balloon
97 * pages list is held before inserting a page into the balloon device.
98 */
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -070099static inline void balloon_page_insert(struct balloon_dev_info *balloon,
100 struct page *page)
Rafael Aquini18468d92012-12-11 16:02:38 -0800101{
David Hildenbrandca215082019-03-05 15:42:23 -0800102 __SetPageOffline(page);
Minchan Kimb1123ea62016-07-26 15:23:09 -0700103 __SetPageMovable(page, balloon->inode->i_mapping);
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700104 set_page_private(page, (unsigned long)balloon);
105 list_add(&page->lru, &balloon->pages);
Rafael Aquini18468d92012-12-11 16:02:38 -0800106}
107
108/*
109 * balloon_page_delete - delete a page from balloon's page list and clear
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700110 * the page->private assignement accordingly.
Rafael Aquini18468d92012-12-11 16:02:38 -0800111 * @page : page to be released from balloon's page list
112 *
113 * Caller must ensure the page is locked and the spin_lock protecting balloon
114 * pages list is held before deleting a page from the balloon device.
115 */
116static inline void balloon_page_delete(struct page *page)
117{
David Hildenbrandca215082019-03-05 15:42:23 -0800118 __ClearPageOffline(page);
Minchan Kimb1123ea62016-07-26 15:23:09 -0700119 __ClearPageMovable(page);
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700120 set_page_private(page, 0);
Minchan Kimb1123ea62016-07-26 15:23:09 -0700121 /*
122 * No touch page.lru field once @page has been isolated
123 * because VM is using the field.
124 */
125 if (!PageIsolated(page))
Konstantin Khlebnikovd6d86c02014-10-09 15:29:27 -0700126 list_del(&page->lru);
Rafael Aquini18468d92012-12-11 16:02:38 -0800127}
128
129/*
130 * balloon_page_device - get the b_dev_info descriptor for the balloon device
131 * that enqueues the given page.
132 */
133static inline struct balloon_dev_info *balloon_page_device(struct page *page)
134{
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700135 return (struct balloon_dev_info *)page_private(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800136}
137
138static inline gfp_t balloon_mapping_gfp_mask(void)
139{
140 return GFP_HIGHUSER_MOVABLE;
141}
142
Rafael Aquini18468d92012-12-11 16:02:38 -0800143#else /* !CONFIG_BALLOON_COMPACTION */
144
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700145static inline void balloon_page_insert(struct balloon_dev_info *balloon,
146 struct page *page)
Rafael Aquini18468d92012-12-11 16:02:38 -0800147{
David Hildenbrandca215082019-03-05 15:42:23 -0800148 __SetPageOffline(page);
Konstantin Khlebnikov9d1ba802014-10-09 15:29:29 -0700149 list_add(&page->lru, &balloon->pages);
Rafael Aquini18468d92012-12-11 16:02:38 -0800150}
151
152static inline void balloon_page_delete(struct page *page)
153{
David Hildenbrandca215082019-03-05 15:42:23 -0800154 __ClearPageOffline(page);
Rafael Aquini18468d92012-12-11 16:02:38 -0800155 list_del(&page->lru);
156}
157
Rafael Aquini18468d92012-12-11 16:02:38 -0800158static inline bool balloon_page_isolate(struct page *page)
159{
160 return false;
161}
162
163static inline void balloon_page_putback(struct page *page)
164{
165 return;
166}
167
168static inline int balloon_page_migrate(struct page *newpage,
169 struct page *page, enum migrate_mode mode)
170{
171 return 0;
172}
173
174static inline gfp_t balloon_mapping_gfp_mask(void)
175{
176 return GFP_HIGHUSER;
177}
178
Rafael Aquini18468d92012-12-11 16:02:38 -0800179#endif /* CONFIG_BALLOON_COMPACTION */
Michael S. Tsirkinc7cdff02017-10-13 16:11:48 +0300180
181/*
182 * balloon_page_push - insert a page into a page list.
183 * @head : pointer to list
184 * @page : page to be added
185 *
186 * Caller must ensure the page is private and protect the list.
187 */
188static inline void balloon_page_push(struct list_head *pages, struct page *page)
189{
190 list_add(&page->lru, pages);
191}
192
193/*
194 * balloon_page_pop - remove a page from a page list.
195 * @head : pointer to list
196 * @page : page to be added
197 *
198 * Caller must ensure the page is private and protect the list.
199 */
200static inline struct page *balloon_page_pop(struct list_head *pages)
201{
202 struct page *page = list_first_entry_or_null(pages, struct page, lru);
203
204 if (!page)
205 return NULL;
206
207 list_del(&page->lru);
208 return page;
209}
Rafael Aquini18468d92012-12-11 16:02:38 -0800210#endif /* _LINUX_BALLOON_COMPACTION_H */