| 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 "movable_ops page migration" | 
|---|
| 8 | * feature. | 
|---|
| 9 | * | 
|---|
| 10 | * page->private is used to reference the responsible balloon device. | 
|---|
| 11 | * That these pages have movable_ops, and which movable_ops apply, | 
|---|
| 12 | * is derived from the page type (PageOffline()) combined with the | 
|---|
| 13 | * PG_movable_ops flag (PageMovableOps()). | 
|---|
| 14 | * | 
|---|
| 15 | * As the page isolation scanning step a compaction thread does is a lockless | 
|---|
| 16 | * procedure (from a page standpoint), it might bring some racy situations while | 
|---|
| 17 | * performing balloon page compaction. In order to sort out these racy scenarios | 
|---|
| 18 | * and safely perform balloon's page compaction and migration we must, always, | 
|---|
| 19 | * ensure following these simple rules: | 
|---|
| 20 | * | 
|---|
| 21 | *   i. Setting the PG_movable_ops flag and page->private with the following | 
|---|
| 22 | *	lock order | 
|---|
| 23 | *	    +-page_lock(page); | 
|---|
| 24 | *	      +--spin_lock_irq(&b_dev_info->pages_lock); | 
|---|
| 25 | * | 
|---|
| 26 | *  ii. isolation or dequeueing procedure must remove the page from balloon | 
|---|
| 27 | *      device page list under b_dev_info->pages_lock. | 
|---|
| 28 | * | 
|---|
| 29 | * The functions provided by this interface are placed to help on coping with | 
|---|
| 30 | * the aforementioned balloon page corner case, as well as to ensure the simple | 
|---|
| 31 | * set of exposed rules are satisfied while we are dealing with balloon pages | 
|---|
| 32 | * compaction / migration. | 
|---|
| 33 | * | 
|---|
| 34 | * Copyright (C) 2012, Red Hat, Inc.  Rafael Aquini <aquini@redhat.com> | 
|---|
| 35 | */ | 
|---|
| 36 | #ifndef _LINUX_BALLOON_COMPACTION_H | 
|---|
| 37 | #define _LINUX_BALLOON_COMPACTION_H | 
|---|
| 38 | #include <linux/pagemap.h> | 
|---|
| 39 | #include <linux/page-flags.h> | 
|---|
| 40 | #include <linux/migrate.h> | 
|---|
| 41 | #include <linux/gfp.h> | 
|---|
| 42 | #include <linux/err.h> | 
|---|
| 43 | #include <linux/fs.h> | 
|---|
| 44 | #include <linux/list.h> | 
|---|
| 45 |  | 
|---|
| 46 | /* | 
|---|
| 47 | * Balloon device information descriptor. | 
|---|
| 48 | * This struct is used to allow the common balloon compaction interface | 
|---|
| 49 | * procedures to find the proper balloon device holding memory pages they'll | 
|---|
| 50 | * have to cope for page compaction / migration, as well as it serves the | 
|---|
| 51 | * balloon driver as a page book-keeper for its registered balloon devices. | 
|---|
| 52 | */ | 
|---|
| 53 | struct balloon_dev_info { | 
|---|
| 54 | unsigned long isolated_pages;	/* # of isolated pages for migration */ | 
|---|
| 55 | spinlock_t pages_lock;		/* Protection to pages list */ | 
|---|
| 56 | struct list_head pages;		/* Pages enqueued & handled to Host */ | 
|---|
| 57 | int (*migratepage)(struct balloon_dev_info *, struct page *newpage, | 
|---|
| 58 | struct page *page, enum migrate_mode mode); | 
|---|
| 59 | }; | 
|---|
| 60 |  | 
|---|
| 61 | extern struct page *balloon_page_alloc(void); | 
|---|
| 62 | extern void balloon_page_enqueue(struct balloon_dev_info *b_dev_info, | 
|---|
| 63 | struct page *page); | 
|---|
| 64 | extern struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info); | 
|---|
| 65 | extern size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info, | 
|---|
| 66 | struct list_head *pages); | 
|---|
| 67 | extern size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info, | 
|---|
| 68 | struct list_head *pages, size_t n_req_pages); | 
|---|
| 69 |  | 
|---|
| 70 | static inline void balloon_devinfo_init(struct balloon_dev_info *balloon) | 
|---|
| 71 | { | 
|---|
| 72 | balloon->isolated_pages = 0; | 
|---|
| 73 | spin_lock_init(&balloon->pages_lock); | 
|---|
| 74 | INIT_LIST_HEAD(list: &balloon->pages); | 
|---|
| 75 | balloon->migratepage = NULL; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | #ifdef CONFIG_BALLOON_COMPACTION | 
|---|
| 79 | extern const struct movable_operations balloon_mops; | 
|---|
| 80 | /* | 
|---|
| 81 | * balloon_page_device - get the b_dev_info descriptor for the balloon device | 
|---|
| 82 | *			 that enqueues the given page. | 
|---|
| 83 | */ | 
|---|
| 84 | static inline struct balloon_dev_info *balloon_page_device(struct page *page) | 
|---|
| 85 | { | 
|---|
| 86 | return (struct balloon_dev_info *)page_private(page); | 
|---|
| 87 | } | 
|---|
| 88 | #endif /* CONFIG_BALLOON_COMPACTION */ | 
|---|
| 89 |  | 
|---|
| 90 | /* | 
|---|
| 91 | * balloon_page_insert - insert a page into the balloon's page list and make | 
|---|
| 92 | *			 the page->private assignment accordingly. | 
|---|
| 93 | * @balloon : pointer to balloon device | 
|---|
| 94 | * @page    : page to be assigned as a 'balloon page' | 
|---|
| 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 | */ | 
|---|
| 99 | static inline void balloon_page_insert(struct balloon_dev_info *balloon, | 
|---|
| 100 | struct page *page) | 
|---|
| 101 | { | 
|---|
| 102 | __SetPageOffline(page); | 
|---|
| 103 | if (IS_ENABLED(CONFIG_BALLOON_COMPACTION)) { | 
|---|
| 104 | SetPageMovableOps(page); | 
|---|
| 105 | set_page_private(page, private: (unsigned long)balloon); | 
|---|
| 106 | } | 
|---|
| 107 | list_add(new: &page->lru, head: &balloon->pages); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | static inline gfp_t balloon_mapping_gfp_mask(void) | 
|---|
| 111 | { | 
|---|
| 112 | if (IS_ENABLED(CONFIG_BALLOON_COMPACTION)) | 
|---|
| 113 | return GFP_HIGHUSER_MOVABLE; | 
|---|
| 114 | return GFP_HIGHUSER; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | /* | 
|---|
| 118 | * balloon_page_finalize - prepare a balloon page that was removed from the | 
|---|
| 119 | *			   balloon list for release to the page allocator | 
|---|
| 120 | * @page: page to be released to the page allocator | 
|---|
| 121 | * | 
|---|
| 122 | * Caller must ensure that the page is locked. | 
|---|
| 123 | */ | 
|---|
| 124 | static inline void balloon_page_finalize(struct page *page) | 
|---|
| 125 | { | 
|---|
| 126 | if (IS_ENABLED(CONFIG_BALLOON_COMPACTION)) | 
|---|
| 127 | set_page_private(page, private: 0); | 
|---|
| 128 | /* PageOffline is sticky until the page is freed to the buddy. */ | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | /* | 
|---|
| 132 | * balloon_page_push - insert a page into a page list. | 
|---|
| 133 | * @head : pointer to list | 
|---|
| 134 | * @page : page to be added | 
|---|
| 135 | * | 
|---|
| 136 | * Caller must ensure the page is private and protect the list. | 
|---|
| 137 | */ | 
|---|
| 138 | static inline void balloon_page_push(struct list_head *pages, struct page *page) | 
|---|
| 139 | { | 
|---|
| 140 | list_add(new: &page->lru, head: pages); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | /* | 
|---|
| 144 | * balloon_page_pop - remove a page from a page list. | 
|---|
| 145 | * @head : pointer to list | 
|---|
| 146 | * @page : page to be added | 
|---|
| 147 | * | 
|---|
| 148 | * Caller must ensure the page is private and protect the list. | 
|---|
| 149 | */ | 
|---|
| 150 | static inline struct page *balloon_page_pop(struct list_head *pages) | 
|---|
| 151 | { | 
|---|
| 152 | struct page *page = list_first_entry_or_null(pages, struct page, lru); | 
|---|
| 153 |  | 
|---|
| 154 | if (!page) | 
|---|
| 155 | return NULL; | 
|---|
| 156 |  | 
|---|
| 157 | list_del(entry: &page->lru); | 
|---|
| 158 | return page; | 
|---|
| 159 | } | 
|---|
| 160 | #endif /* _LINUX_BALLOON_COMPACTION_H */ | 
|---|
| 161 |  | 
|---|