balloon_compaction.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * 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
  31. * isolation, re-test the PageBalloon mark and the PagePrivate flag
  32. * under the proper page lock, to ensure isolating a valid balloon page
  33. * (not yet isolated, nor under release procedure)
  34. *
  35. * iv. isolation or dequeueing procedure must clear PagePrivate flag under
  36. * page lock together with removing page from balloon device page list.
  37. *
  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>
  49. #include <linux/migrate.h>
  50. #include <linux/gfp.h>
  51. #include <linux/err.h>
  52. #include <linux/fs.h>
  53. #include <linux/list.h>
  54. /*
  55. * Balloon device information descriptor.
  56. * This struct is used to allow the common balloon compaction interface
  57. * procedures to find the proper balloon device holding memory pages they'll
  58. * have to cope for page compaction / migration, as well as it serves the
  59. * balloon driver as a page book-keeper for its registered balloon devices.
  60. */
  61. struct balloon_dev_info {
  62. unsigned long isolated_pages; /* # of isolated pages for migration */
  63. spinlock_t pages_lock; /* Protection to pages list */
  64. struct list_head pages; /* Pages enqueued & handled to Host */
  65. int (*migratepage)(struct balloon_dev_info *, struct page *newpage,
  66. struct page *page, enum migrate_mode mode);
  67. struct inode *inode;
  68. };
  69. extern struct page *balloon_page_alloc(void);
  70. extern void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
  71. struct page *page);
  72. extern struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info);
  73. static inline void balloon_devinfo_init(struct balloon_dev_info *balloon)
  74. {
  75. balloon->isolated_pages = 0;
  76. spin_lock_init(&balloon->pages_lock);
  77. INIT_LIST_HEAD(&balloon->pages);
  78. balloon->migratepage = NULL;
  79. balloon->inode = NULL;
  80. }
  81. #ifdef CONFIG_BALLOON_COMPACTION
  82. extern const struct address_space_operations balloon_aops;
  83. extern bool balloon_page_isolate(struct page *page,
  84. isolate_mode_t mode);
  85. extern void balloon_page_putback(struct page *page);
  86. extern int balloon_page_migrate(struct address_space *mapping,
  87. struct page *newpage,
  88. struct page *page, enum migrate_mode mode);
  89. /*
  90. * balloon_page_insert - insert a page into the balloon's page list and make
  91. * the page->private assignment accordingly.
  92. * @balloon : pointer to balloon device
  93. * @page : page to be assigned as a 'balloon page'
  94. *
  95. * Caller must ensure the page is locked and the spin_lock protecting balloon
  96. * pages list is held before inserting a page into the balloon device.
  97. */
  98. static inline void balloon_page_insert(struct balloon_dev_info *balloon,
  99. struct page *page)
  100. {
  101. __SetPageBalloon(page);
  102. __SetPageMovable(page, balloon->inode->i_mapping);
  103. set_page_private(page, (unsigned long)balloon);
  104. list_add(&page->lru, &balloon->pages);
  105. }
  106. /*
  107. * balloon_page_delete - delete a page from balloon's page list and clear
  108. * the page->private assignement accordingly.
  109. * @page : page to be released from balloon's page list
  110. *
  111. * Caller must ensure the page is locked and the spin_lock protecting balloon
  112. * pages list is held before deleting a page from the balloon device.
  113. */
  114. static inline void balloon_page_delete(struct page *page)
  115. {
  116. __ClearPageBalloon(page);
  117. __ClearPageMovable(page);
  118. set_page_private(page, 0);
  119. /*
  120. * No touch page.lru field once @page has been isolated
  121. * because VM is using the field.
  122. */
  123. if (!PageIsolated(page))
  124. list_del(&page->lru);
  125. }
  126. /*
  127. * balloon_page_device - get the b_dev_info descriptor for the balloon device
  128. * that enqueues the given page.
  129. */
  130. static inline struct balloon_dev_info *balloon_page_device(struct page *page)
  131. {
  132. return (struct balloon_dev_info *)page_private(page);
  133. }
  134. static inline gfp_t balloon_mapping_gfp_mask(void)
  135. {
  136. return GFP_HIGHUSER_MOVABLE;
  137. }
  138. #else /* !CONFIG_BALLOON_COMPACTION */
  139. static inline void balloon_page_insert(struct balloon_dev_info *balloon,
  140. struct page *page)
  141. {
  142. __SetPageBalloon(page);
  143. list_add(&page->lru, &balloon->pages);
  144. }
  145. static inline void balloon_page_delete(struct page *page)
  146. {
  147. __ClearPageBalloon(page);
  148. list_del(&page->lru);
  149. }
  150. static inline bool __is_movable_balloon_page(struct page *page)
  151. {
  152. return false;
  153. }
  154. static inline bool balloon_page_movable(struct page *page)
  155. {
  156. return false;
  157. }
  158. static inline bool isolated_balloon_page(struct page *page)
  159. {
  160. return false;
  161. }
  162. static inline bool balloon_page_isolate(struct page *page)
  163. {
  164. return false;
  165. }
  166. static inline void balloon_page_putback(struct page *page)
  167. {
  168. return;
  169. }
  170. static inline int balloon_page_migrate(struct page *newpage,
  171. struct page *page, enum migrate_mode mode)
  172. {
  173. return 0;
  174. }
  175. static inline gfp_t balloon_mapping_gfp_mask(void)
  176. {
  177. return GFP_HIGHUSER;
  178. }
  179. #endif /* CONFIG_BALLOON_COMPACTION */
  180. /*
  181. * balloon_page_push - insert a page into a page list.
  182. * @head : pointer to list
  183. * @page : page to be added
  184. *
  185. * Caller must ensure the page is private and protect the list.
  186. */
  187. static inline void balloon_page_push(struct list_head *pages, struct page *page)
  188. {
  189. list_add(&page->lru, pages);
  190. }
  191. /*
  192. * balloon_page_pop - remove a page from a page list.
  193. * @head : pointer to list
  194. * @page : page to be added
  195. *
  196. * Caller must ensure the page is private and protect the list.
  197. */
  198. static inline struct page *balloon_page_pop(struct list_head *pages)
  199. {
  200. struct page *page = list_first_entry_or_null(pages, struct page, lru);
  201. if (!page)
  202. return NULL;
  203. list_del(&page->lru);
  204. return page;
  205. }
  206. #endif /* _LINUX_BALLOON_COMPACTION_H */