1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Common interface for implementing a memory balloon, including support
4 * for migration of pages inflated in a memory balloon.
5 *
6 * Balloon page migration makes use of the general "movable_ops page migration"
7 * feature.
8 *
9 * page->private is used to reference the responsible balloon device.
10 * That these pages have movable_ops, and which movable_ops apply,
11 * is derived from the page type (PageOffline()) combined with the
12 * PG_movable_ops flag (PageMovableOps()).
13 *
14 * Once the page type and the PG_movable_ops are set, migration code
15 * can initiate page isolation by invoking the
16 * movable_operations()->isolate_page() callback
17 *
18 * As long as page->private is set, the page is either on the balloon list
19 * or isolated for migration. If page->private is not set, the page is
20 * either still getting inflated, or was deflated to be freed by the balloon
21 * driver soon. Isolation is impossible in both cases.
22 *
23 * As the page isolation scanning step a compaction thread does is a lockless
24 * procedure (from a page standpoint), it might bring some racy situations while
25 * performing balloon page migration. In order to sort out these racy scenarios
26 * and safely perform balloon's page migration we must, always, ensure following
27 * these simple rules:
28 *
29 * i. Inflation/deflation must set/clear page->private under the
30 * balloon_pages_lock
31 *
32 * ii. isolation or dequeueing procedure must remove the page from balloon
33 * device page list under balloon_pages_lock
34 *
35 * Copyright (C) 2012, Red Hat, Inc. Rafael Aquini <aquini@redhat.com>
36 */
37 #ifndef _LINUX_BALLOON_H
38 #define _LINUX_BALLOON_H
39 #include <linux/pagemap.h>
40 #include <linux/page-flags.h>
41 #include <linux/migrate.h>
42 #include <linux/gfp.h>
43 #include <linux/err.h>
44 #include <linux/list.h>
45
46 /*
47 * Balloon device information descriptor.
48 * This struct is used to allow the common balloon page migration interface
49 * procedures to find the proper balloon device holding memory pages they'll
50 * have to cope for page migration, as well as it serves the balloon driver as
51 * 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 struct list_head pages; /* Pages enqueued & handled to Host */
56 int (*migratepage)(struct balloon_dev_info *, struct page *newpage,
57 struct page *page, enum migrate_mode mode);
58 bool adjust_managed_page_count;
59 };
60
61 struct page *balloon_page_alloc(void);
62 void balloon_page_enqueue(struct balloon_dev_info *b_dev_info,
63 struct page *page);
64 struct page *balloon_page_dequeue(struct balloon_dev_info *b_dev_info);
65 size_t balloon_page_list_enqueue(struct balloon_dev_info *b_dev_info,
66 struct list_head *pages);
67 size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,
68 struct list_head *pages, size_t n_req_pages);
69
balloon_devinfo_init(struct balloon_dev_info * balloon)70 static inline void balloon_devinfo_init(struct balloon_dev_info *balloon)
71 {
72 balloon->isolated_pages = 0;
73 INIT_LIST_HEAD(&balloon->pages);
74 balloon->migratepage = NULL;
75 balloon->adjust_managed_page_count = false;
76 }
77 #endif /* _LINUX_BALLOON_H */
78