postcopy-ram.h 6.1 KB

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