Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 2 | /* |
| 3 | * include/linux/balloon_compaction.h |
| 4 | * |
| 5 | * Common interface definitions for making balloon pages movable by compaction. |
| 6 | * |
| 7 | * Despite being perfectly possible to perform ballooned pages migration, they |
| 8 | * make a special corner case to compaction scans because balloon pages are not |
| 9 | * enlisted at any LRU list like the other pages we do compact / migrate. |
| 10 | * |
| 11 | * As the page isolation scanning step a compaction thread does is a lockless |
| 12 | * procedure (from a page standpoint), it might bring some racy situations while |
| 13 | * performing balloon page compaction. In order to sort out these racy scenarios |
| 14 | * and safely perform balloon's page compaction and migration we must, always, |
| 15 | * ensure following these three simple rules: |
| 16 | * |
| 17 | * i. when updating a balloon's page ->mapping element, strictly do it under |
| 18 | * the following lock order, independently of the far superior |
| 19 | * locking scheme (lru_lock, balloon_lock): |
| 20 | * +-page_lock(page); |
| 21 | * +--spin_lock_irq(&b_dev_info->pages_lock); |
| 22 | * ... page->mapping updates here ... |
| 23 | * |
| 24 | * ii. before isolating or dequeueing a balloon page from the balloon device |
| 25 | * pages list, the page reference counter must be raised by one and the |
| 26 | * extra refcount must be dropped when the page is enqueued back into |
| 27 | * the balloon device page list, thus a balloon page keeps its reference |
| 28 | * counter raised only while it is under our special handling; |
| 29 | * |
| 30 | * iii. after the lockless scan step have selected a potential balloon page for |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 31 | * isolation, re-test the PageBalloon mark and the PagePrivate flag |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 32 | * under the proper page lock, to ensure isolating a valid balloon page |
| 33 | * (not yet isolated, nor under release procedure) |
| 34 | * |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 35 | * iv. isolation or dequeueing procedure must clear PagePrivate flag under |
| 36 | * page lock together with removing page from balloon device page list. |
| 37 | * |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 38 | * The functions provided by this interface are placed to help on coping with |
| 39 | * the aforementioned balloon page corner case, as well as to ensure the simple |
| 40 | * set of exposed rules are satisfied while we are dealing with balloon pages |
| 41 | * compaction / migration. |
| 42 | * |
| 43 | * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com> |
| 44 | */ |
| 45 | #ifndef _LINUX_BALLOON_COMPACTION_H |
| 46 | #define _LINUX_BALLOON_COMPACTION_H |
| 47 | #include <linux/pagemap.h> |
| 48 | #include <linux/page-flags.h> |
Minchan Kim | dd4123f | 2016-07-26 15:26:50 -0700 | [diff] [blame] | 49 | #include <linux/migrate.h> |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 50 | #include <linux/gfp.h> |
| 51 | #include <linux/err.h> |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 52 | #include <linux/fs.h> |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 53 | #include <linux/list.h> |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * Balloon device information descriptor. |
| 57 | * This struct is used to allow the common balloon compaction interface |
| 58 | * procedures to find the proper balloon device holding memory pages they'll |
| 59 | * have to cope for page compaction / migration, as well as it serves the |
| 60 | * balloon driver as a page book-keeper for its registered balloon devices. |
| 61 | */ |
| 62 | struct balloon_dev_info { |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 63 | unsigned long isolated_pages; /* # of isolated pages for migration */ |
| 64 | spinlock_t pages_lock; /* Protection to pages list */ |
| 65 | struct list_head pages; /* Pages enqueued & handled to Host */ |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 66 | int (*migratepage)(struct balloon_dev_info *, struct page *newpage, |
| 67 | struct page *page, enum migrate_mode mode); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 68 | struct inode *inode; |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 71 | extern struct page *balloon_page_alloc(void); |
| 72 | extern void balloon_page_enqueue(struct balloon_dev_info *b_dev_info, |
| 73 | struct page *page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 74 | extern struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 75 | |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 76 | static inline void balloon_devinfo_init(struct balloon_dev_info *balloon) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 77 | { |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 78 | balloon->isolated_pages = 0; |
| 79 | spin_lock_init(&balloon->pages_lock); |
| 80 | INIT_LIST_HEAD(&balloon->pages); |
| 81 | balloon->migratepage = NULL; |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 82 | balloon->inode = NULL; |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 85 | #ifdef CONFIG_BALLOON_COMPACTION |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 86 | extern const struct address_space_operations balloon_aops; |
| 87 | extern bool balloon_page_isolate(struct page *page, |
| 88 | isolate_mode_t mode); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 89 | extern void balloon_page_putback(struct page *page); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 90 | extern int balloon_page_migrate(struct address_space *mapping, |
| 91 | struct page *newpage, |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 92 | struct page *page, enum migrate_mode mode); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 93 | |
| 94 | /* |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 95 | * balloon_page_insert - insert a page into the balloon's page list and make |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 96 | * the page->private assignment accordingly. |
| 97 | * @balloon : pointer to balloon device |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 98 | * @page : page to be assigned as a 'balloon page' |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 99 | * |
| 100 | * Caller must ensure the page is locked and the spin_lock protecting balloon |
| 101 | * pages list is held before inserting a page into the balloon device. |
| 102 | */ |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 103 | static inline void balloon_page_insert(struct balloon_dev_info *balloon, |
| 104 | struct page *page) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 105 | { |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 106 | __SetPageBalloon(page); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 107 | __SetPageMovable(page, balloon->inode->i_mapping); |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 108 | set_page_private(page, (unsigned long)balloon); |
| 109 | list_add(&page->lru, &balloon->pages); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
| 113 | * balloon_page_delete - delete a page from balloon's page list and clear |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 114 | * the page->private assignement accordingly. |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 115 | * @page : page to be released from balloon's page list |
| 116 | * |
| 117 | * Caller must ensure the page is locked and the spin_lock protecting balloon |
| 118 | * pages list is held before deleting a page from the balloon device. |
| 119 | */ |
| 120 | static inline void balloon_page_delete(struct page *page) |
| 121 | { |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 122 | __ClearPageBalloon(page); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 123 | __ClearPageMovable(page); |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 124 | set_page_private(page, 0); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 125 | /* |
| 126 | * No touch page.lru field once @page has been isolated |
| 127 | * because VM is using the field. |
| 128 | */ |
| 129 | if (!PageIsolated(page)) |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 130 | list_del(&page->lru); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* |
| 134 | * balloon_page_device - get the b_dev_info descriptor for the balloon device |
| 135 | * that enqueues the given page. |
| 136 | */ |
| 137 | static inline struct balloon_dev_info *balloon_page_device(struct page *page) |
| 138 | { |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 139 | return (struct balloon_dev_info *)page_private(page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static inline gfp_t balloon_mapping_gfp_mask(void) |
| 143 | { |
| 144 | return GFP_HIGHUSER_MOVABLE; |
| 145 | } |
| 146 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 147 | #else /* !CONFIG_BALLOON_COMPACTION */ |
| 148 | |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 149 | static inline void balloon_page_insert(struct balloon_dev_info *balloon, |
| 150 | struct page *page) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 151 | { |
Konstantin Khlebnikov | 09316c0 | 2014-10-09 15:29:32 -0700 | [diff] [blame] | 152 | __SetPageBalloon(page); |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 153 | list_add(&page->lru, &balloon->pages); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static inline void balloon_page_delete(struct page *page) |
| 157 | { |
Konstantin Khlebnikov | 09316c0 | 2014-10-09 15:29:32 -0700 | [diff] [blame] | 158 | __ClearPageBalloon(page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 159 | list_del(&page->lru); |
| 160 | } |
| 161 | |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 162 | static inline bool __is_movable_balloon_page(struct page *page) |
| 163 | { |
| 164 | return false; |
| 165 | } |
| 166 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 167 | static inline bool balloon_page_movable(struct page *page) |
| 168 | { |
| 169 | return false; |
| 170 | } |
| 171 | |
Rafael Aquini | 117aad1 | 2013-09-30 13:45:16 -0700 | [diff] [blame] | 172 | static inline bool isolated_balloon_page(struct page *page) |
| 173 | { |
| 174 | return false; |
| 175 | } |
| 176 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 177 | static inline bool balloon_page_isolate(struct page *page) |
| 178 | { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | static inline void balloon_page_putback(struct page *page) |
| 183 | { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | static inline int balloon_page_migrate(struct page *newpage, |
| 188 | struct page *page, enum migrate_mode mode) |
| 189 | { |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static inline gfp_t balloon_mapping_gfp_mask(void) |
| 194 | { |
| 195 | return GFP_HIGHUSER; |
| 196 | } |
| 197 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 198 | #endif /* CONFIG_BALLOON_COMPACTION */ |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * balloon_page_push - insert a page into a page list. |
| 202 | * @head : pointer to list |
| 203 | * @page : page to be added |
| 204 | * |
| 205 | * Caller must ensure the page is private and protect the list. |
| 206 | */ |
| 207 | static inline void balloon_page_push(struct list_head *pages, struct page *page) |
| 208 | { |
| 209 | list_add(&page->lru, pages); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * balloon_page_pop - remove a page from a page list. |
| 214 | * @head : pointer to list |
| 215 | * @page : page to be added |
| 216 | * |
| 217 | * Caller must ensure the page is private and protect the list. |
| 218 | */ |
| 219 | static inline struct page *balloon_page_pop(struct list_head *pages) |
| 220 | { |
| 221 | struct page *page = list_first_entry_or_null(pages, struct page, lru); |
| 222 | |
| 223 | if (!page) |
| 224 | return NULL; |
| 225 | |
| 226 | list_del(&page->lru); |
| 227 | return page; |
| 228 | } |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 229 | #endif /* _LINUX_BALLOON_COMPACTION_H */ |