xref: /openbmc/qemu/migration/postcopy-ram.h (revision 32e70aad)
1 /*
2  * Postcopy migration for RAM
3  *
4  * Copyright 2013 Red Hat, Inc. and/or its affiliates
5  *
6  * Authors:
7  *  Dave Gilbert  <dgilbert@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  *
12  */
13 #ifndef QEMU_POSTCOPY_RAM_H
14 #define QEMU_POSTCOPY_RAM_H
15 
16 /* Return true if the host supports everything we need to do postcopy-ram */
17 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis);
18 
19 /*
20  * Make all of RAM sensitive to accesses to areas that haven't yet been written
21  * and wire up anything necessary to deal with it.
22  */
23 int postcopy_ram_enable_notify(MigrationIncomingState *mis);
24 
25 /*
26  * Initialise postcopy-ram, setting the RAM to a state where we can go into
27  * postcopy later; must be called prior to any precopy.
28  * called from ram.c's similarly named ram_postcopy_incoming_init
29  */
30 int postcopy_ram_incoming_init(MigrationIncomingState *mis);
31 
32 /*
33  * At the end of a migration where postcopy_ram_incoming_init was called.
34  */
35 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis);
36 
37 /*
38  * Userfault requires us to mark RAM as NOHUGEPAGE prior to discard
39  * however leaving it until after precopy means that most of the precopy
40  * data is still THPd
41  */
42 int postcopy_ram_prepare_discard(MigrationIncomingState *mis);
43 
44 /*
45  * Called at the start of each RAMBlock by the bitmap code.
46  */
47 void postcopy_discard_send_init(MigrationState *ms, const char *name);
48 
49 /*
50  * Called by the bitmap code for each chunk to discard.
51  * May send a discard message, may just leave it queued to
52  * be sent later.
53  * @start,@length: a range of pages in the migration bitmap in the
54  *  RAM block passed to postcopy_discard_send_init() (length=1 is one page)
55  */
56 void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
57                                  unsigned long length);
58 
59 /*
60  * Called at the end of each RAMBlock by the bitmap code.
61  * Sends any outstanding discard messages.
62  */
63 void postcopy_discard_send_finish(MigrationState *ms);
64 
65 /*
66  * Place a page (from) at (host) efficiently
67  *    There are restrictions on how 'from' must be mapped, in general best
68  *    to use other postcopy_ routines to allocate.
69  * returns 0 on success
70  */
71 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
72                         RAMBlock *rb);
73 
74 /*
75  * Place a zero page at (host) atomically
76  * returns 0 on success
77  */
78 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
79                              RAMBlock *rb);
80 
81 /* The current postcopy state is read/set by postcopy_state_get/set
82  * which update it atomically.
83  * The state is updated as postcopy messages are received, and
84  * in general only one thread should be writing to the state at any one
85  * time, initially the main thread and then the listen thread;
86  * Corner cases are where either thread finishes early and/or errors.
87  * The state is checked as messages are received to ensure that
88  * the source is sending us messages in the correct order.
89  * The state is also used by the RAM reception code to know if it
90  * has to place pages atomically, and the cleanup code at the end of
91  * the main thread to know if it has to delay cleanup until the end
92  * of postcopy.
93  */
94 typedef enum {
95     POSTCOPY_INCOMING_NONE = 0,  /* Initial state - no postcopy */
96     POSTCOPY_INCOMING_ADVISE,
97     POSTCOPY_INCOMING_DISCARD,
98     POSTCOPY_INCOMING_LISTENING,
99     POSTCOPY_INCOMING_RUNNING,
100     POSTCOPY_INCOMING_END
101 } PostcopyState;
102 
103 /*
104  * Allocate a page of memory that can be mapped at a later point in time
105  * using postcopy_place_page
106  * Returns: Pointer to allocated page
107  */
108 void *postcopy_get_tmp_page(MigrationIncomingState *mis);
109 
110 PostcopyState postcopy_state_get(void);
111 /* Set the state and return the old state */
112 PostcopyState postcopy_state_set(PostcopyState new_state);
113 
114 void postcopy_fault_thread_notify(MigrationIncomingState *mis);
115 
116 /*
117  * To be called once at the start before any device initialisation
118  */
119 void postcopy_infrastructure_init(void);
120 
121 /* Add a notifier to a list to be called when checking whether the devices
122  * can support postcopy.
123  * It's data is a *PostcopyNotifyData
124  * It should return 0 if OK, or a negative value on failure.
125  * On failure it must set the data->errp to an error.
126  *
127  */
128 enum PostcopyNotifyReason {
129     POSTCOPY_NOTIFY_PROBE = 0,
130     POSTCOPY_NOTIFY_INBOUND_ADVISE,
131     POSTCOPY_NOTIFY_INBOUND_LISTEN,
132     POSTCOPY_NOTIFY_INBOUND_END,
133 };
134 
135 struct PostcopyNotifyData {
136     enum PostcopyNotifyReason reason;
137     Error **errp;
138 };
139 
140 void postcopy_add_notifier(NotifierWithReturn *nn);
141 void postcopy_remove_notifier(NotifierWithReturn *n);
142 /* Call the notifier list set by postcopy_add_start_notifier */
143 int postcopy_notify(enum PostcopyNotifyReason reason, Error **errp);
144 
145 struct PostCopyFD;
146 
147 /* ufd is a pointer to the struct uffd_msg *TODO: more Portable! */
148 typedef int (*pcfdhandler)(struct PostCopyFD *pcfd, void *ufd);
149 /* Notification to wake, either on place or on reception of
150  * a fault on something that's already arrived (race)
151  */
152 typedef int (*pcfdwake)(struct PostCopyFD *pcfd, RAMBlock *rb, uint64_t offset);
153 
154 struct PostCopyFD {
155     int fd;
156     /* Data to pass to handler */
157     void *data;
158     /* Handler to be called whenever we get a poll event */
159     pcfdhandler handler;
160     /* Notification to wake shared client */
161     pcfdwake waker;
162     /* A string to use in error messages */
163     const char *idstr;
164 };
165 
166 /* Register a userfaultfd owned by an external process for
167  * shared memory.
168  */
169 void postcopy_register_shared_ufd(struct PostCopyFD *pcfd);
170 void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd);
171 /* Call each of the shared 'waker's registerd telling them of
172  * availability of a block.
173  */
174 int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset);
175 /* postcopy_wake_shared: Notify a client ufd that a page is available
176  *
177  * Returns 0 on success
178  *
179  * @pcfd: Structure with fd, handler and name as above
180  * @client_addr: Address in the client program, not QEMU
181  * @rb: The RAMBlock the page is in
182  */
183 int postcopy_wake_shared(struct PostCopyFD *pcfd, uint64_t client_addr,
184                          RAMBlock *rb);
185 /* Callback from shared fault handlers to ask for a page */
186 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
187                                  uint64_t client_addr, uint64_t offset);
188 
189 #endif
190