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