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 | * |
David Hildenbrand | 4d3467e | 2019-03-05 15:42:18 -0800 | [diff] [blame] | 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. |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 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, |
David Hildenbrand | 4d3467e | 2019-03-05 15:42:18 -0800 | [diff] [blame] | 18 | * ensure following these simple rules: |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 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 | * |
David Hildenbrand | 4d3467e | 2019-03-05 15:42:18 -0800 | [diff] [blame] | 27 | * ii. isolation or dequeueing procedure must remove the page from balloon |
| 28 | * device page list under b_dev_info->pages_lock. |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 29 | * |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 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> |
Minchan Kim | dd4123f | 2016-07-26 15:26:50 -0700 | [diff] [blame] | 41 | #include <linux/migrate.h> |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 42 | #include <linux/gfp.h> |
| 43 | #include <linux/err.h> |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 44 | #include <linux/fs.h> |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 45 | #include <linux/list.h> |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 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 { |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 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 */ |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 58 | int (*migratepage)(struct balloon_dev_info *, struct page *newpage, |
| 59 | struct page *page, enum migrate_mode mode); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 60 | struct inode *inode; |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 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); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 66 | 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] | 67 | |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 68 | static inline void balloon_devinfo_init(struct balloon_dev_info *balloon) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 69 | { |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 70 | balloon->isolated_pages = 0; |
| 71 | spin_lock_init(&balloon->pages_lock); |
| 72 | INIT_LIST_HEAD(&balloon->pages); |
| 73 | balloon->migratepage = NULL; |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 74 | balloon->inode = NULL; |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 77 | #ifdef CONFIG_BALLOON_COMPACTION |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 78 | extern const struct address_space_operations balloon_aops; |
| 79 | extern bool balloon_page_isolate(struct page *page, |
| 80 | isolate_mode_t mode); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 81 | extern void balloon_page_putback(struct page *page); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 82 | extern int balloon_page_migrate(struct address_space *mapping, |
| 83 | struct page *newpage, |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 84 | struct page *page, enum migrate_mode mode); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 85 | |
| 86 | /* |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 87 | * 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] | 88 | * the page->private assignment accordingly. |
| 89 | * @balloon : pointer to balloon device |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 90 | * @page : page to be assigned as a 'balloon page' |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 91 | * |
| 92 | * Caller must ensure the page is locked and the spin_lock protecting balloon |
| 93 | * pages list is held before inserting a page into the balloon device. |
| 94 | */ |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 95 | static inline void balloon_page_insert(struct balloon_dev_info *balloon, |
| 96 | struct page *page) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 97 | { |
David Hildenbrand | ca21508 | 2019-03-05 15:42:23 -0800 | [diff] [blame] | 98 | __SetPageOffline(page); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 99 | __SetPageMovable(page, balloon->inode->i_mapping); |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 100 | set_page_private(page, (unsigned long)balloon); |
| 101 | list_add(&page->lru, &balloon->pages); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* |
| 105 | * 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] | 106 | * the page->private assignement accordingly. |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 107 | * @page : page to be released from balloon's page list |
| 108 | * |
| 109 | * Caller must ensure the page is locked and the spin_lock protecting balloon |
| 110 | * pages list is held before deleting a page from the balloon device. |
| 111 | */ |
| 112 | static inline void balloon_page_delete(struct page *page) |
| 113 | { |
David Hildenbrand | ca21508 | 2019-03-05 15:42:23 -0800 | [diff] [blame] | 114 | __ClearPageOffline(page); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 115 | __ClearPageMovable(page); |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 116 | set_page_private(page, 0); |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 117 | /* |
| 118 | * No touch page.lru field once @page has been isolated |
| 119 | * because VM is using the field. |
| 120 | */ |
| 121 | if (!PageIsolated(page)) |
Konstantin Khlebnikov | d6d86c0 | 2014-10-09 15:29:27 -0700 | [diff] [blame] | 122 | list_del(&page->lru); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* |
| 126 | * balloon_page_device - get the b_dev_info descriptor for the balloon device |
| 127 | * that enqueues the given page. |
| 128 | */ |
| 129 | static inline struct balloon_dev_info *balloon_page_device(struct page *page) |
| 130 | { |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 131 | return (struct balloon_dev_info *)page_private(page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static inline gfp_t balloon_mapping_gfp_mask(void) |
| 135 | { |
| 136 | return GFP_HIGHUSER_MOVABLE; |
| 137 | } |
| 138 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 139 | #else /* !CONFIG_BALLOON_COMPACTION */ |
| 140 | |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 141 | static inline void balloon_page_insert(struct balloon_dev_info *balloon, |
| 142 | struct page *page) |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 143 | { |
David Hildenbrand | ca21508 | 2019-03-05 15:42:23 -0800 | [diff] [blame] | 144 | __SetPageOffline(page); |
Konstantin Khlebnikov | 9d1ba80 | 2014-10-09 15:29:29 -0700 | [diff] [blame] | 145 | list_add(&page->lru, &balloon->pages); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static inline void balloon_page_delete(struct page *page) |
| 149 | { |
David Hildenbrand | ca21508 | 2019-03-05 15:42:23 -0800 | [diff] [blame] | 150 | __ClearPageOffline(page); |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 151 | list_del(&page->lru); |
| 152 | } |
| 153 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 154 | static inline bool balloon_page_isolate(struct page *page) |
| 155 | { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | static inline void balloon_page_putback(struct page *page) |
| 160 | { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | static inline int balloon_page_migrate(struct page *newpage, |
| 165 | struct page *page, enum migrate_mode mode) |
| 166 | { |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static inline gfp_t balloon_mapping_gfp_mask(void) |
| 171 | { |
| 172 | return GFP_HIGHUSER; |
| 173 | } |
| 174 | |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 175 | #endif /* CONFIG_BALLOON_COMPACTION */ |
Michael S. Tsirkin | c7cdff0 | 2017-10-13 16:11:48 +0300 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * balloon_page_push - insert a page into a page list. |
| 179 | * @head : pointer to list |
| 180 | * @page : page to be added |
| 181 | * |
| 182 | * Caller must ensure the page is private and protect the list. |
| 183 | */ |
| 184 | static inline void balloon_page_push(struct list_head *pages, struct page *page) |
| 185 | { |
| 186 | list_add(&page->lru, pages); |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * balloon_page_pop - remove a page from a page list. |
| 191 | * @head : pointer to list |
| 192 | * @page : page to be added |
| 193 | * |
| 194 | * Caller must ensure the page is private and protect the list. |
| 195 | */ |
| 196 | static inline struct page *balloon_page_pop(struct list_head *pages) |
| 197 | { |
| 198 | struct page *page = list_first_entry_or_null(pages, struct page, lru); |
| 199 | |
| 200 | if (!page) |
| 201 | return NULL; |
| 202 | |
| 203 | list_del(&page->lru); |
| 204 | return page; |
| 205 | } |
Rafael Aquini | 18468d9 | 2012-12-11 16:02:38 -0800 | [diff] [blame] | 206 | #endif /* _LINUX_BALLOON_COMPACTION_H */ |