xref: /openbmc/qemu/hw/vfio/migration.c (revision eda7362af9595a9c6b1f1fefdd94b5ef711c250c)
1 /*
2  * Migration support for VFIO devices
3  *
4  * Copyright NVIDIA, Inc. 2020
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2. See
7  * the COPYING file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "qemu/main-loop.h"
12 #include "qemu/cutils.h"
13 #include "qemu/units.h"
14 #include "qemu/error-report.h"
15 #include <linux/vfio.h>
16 #include <sys/ioctl.h>
17 
18 #include "sysemu/runstate.h"
19 #include "hw/vfio/vfio-common.h"
20 #include "migration/migration.h"
21 #include "migration/vmstate.h"
22 #include "migration/qemu-file.h"
23 #include "migration/register.h"
24 #include "migration/blocker.h"
25 #include "migration/misc.h"
26 #include "qapi/error.h"
27 #include "exec/ramlist.h"
28 #include "exec/ram_addr.h"
29 #include "pci.h"
30 #include "trace.h"
31 #include "hw/hw.h"
32 
33 /*
34  * Flags to be used as unique delimiters for VFIO devices in the migration
35  * stream. These flags are composed as:
36  * 0xffffffff => MSB 32-bit all 1s
37  * 0xef10     => Magic ID, represents emulated (virtual) function IO
38  * 0x0000     => 16-bits reserved for flags
39  *
40  * The beginning of state information is marked by _DEV_CONFIG_STATE,
41  * _DEV_SETUP_STATE, or _DEV_DATA_STATE, respectively. The end of a
42  * certain state information is marked by _END_OF_STATE.
43  */
44 #define VFIO_MIG_FLAG_END_OF_STATE      (0xffffffffef100001ULL)
45 #define VFIO_MIG_FLAG_DEV_CONFIG_STATE  (0xffffffffef100002ULL)
46 #define VFIO_MIG_FLAG_DEV_SETUP_STATE   (0xffffffffef100003ULL)
47 #define VFIO_MIG_FLAG_DEV_DATA_STATE    (0xffffffffef100004ULL)
48 
49 /*
50  * This is an arbitrary size based on migration of mlx5 devices, where typically
51  * total device migration size is on the order of 100s of MB. Testing with
52  * larger values, e.g. 128MB and 1GB, did not show a performance improvement.
53  */
54 #define VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE (1 * MiB)
55 
56 static int64_t bytes_transferred;
57 
58 static const char *mig_state_to_str(enum vfio_device_mig_state state)
59 {
60     switch (state) {
61     case VFIO_DEVICE_STATE_ERROR:
62         return "ERROR";
63     case VFIO_DEVICE_STATE_STOP:
64         return "STOP";
65     case VFIO_DEVICE_STATE_RUNNING:
66         return "RUNNING";
67     case VFIO_DEVICE_STATE_STOP_COPY:
68         return "STOP_COPY";
69     case VFIO_DEVICE_STATE_RESUMING:
70         return "RESUMING";
71     case VFIO_DEVICE_STATE_PRE_COPY:
72         return "PRE_COPY";
73     default:
74         return "UNKNOWN STATE";
75     }
76 }
77 
78 static int vfio_migration_set_state(VFIODevice *vbasedev,
79                                     enum vfio_device_mig_state new_state,
80                                     enum vfio_device_mig_state recover_state)
81 {
82     VFIOMigration *migration = vbasedev->migration;
83     uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +
84                               sizeof(struct vfio_device_feature_mig_state),
85                               sizeof(uint64_t))] = {};
86     struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
87     struct vfio_device_feature_mig_state *mig_state =
88         (struct vfio_device_feature_mig_state *)feature->data;
89     int ret;
90 
91     feature->argsz = sizeof(buf);
92     feature->flags =
93         VFIO_DEVICE_FEATURE_SET | VFIO_DEVICE_FEATURE_MIG_DEVICE_STATE;
94     mig_state->device_state = new_state;
95     if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
96         /* Try to set the device in some good state */
97         ret = -errno;
98 
99         if (recover_state == VFIO_DEVICE_STATE_ERROR) {
100             error_report("%s: Failed setting device state to %s, err: %s. "
101                          "Recover state is ERROR. Resetting device",
102                          vbasedev->name, mig_state_to_str(new_state),
103                          strerror(errno));
104 
105             goto reset_device;
106         }
107 
108         error_report(
109             "%s: Failed setting device state to %s, err: %s. Setting device in recover state %s",
110                      vbasedev->name, mig_state_to_str(new_state),
111                      strerror(errno), mig_state_to_str(recover_state));
112 
113         mig_state->device_state = recover_state;
114         if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
115             ret = -errno;
116             error_report(
117                 "%s: Failed setting device in recover state, err: %s. Resetting device",
118                          vbasedev->name, strerror(errno));
119 
120             goto reset_device;
121         }
122 
123         migration->device_state = recover_state;
124 
125         return ret;
126     }
127 
128     migration->device_state = new_state;
129     if (mig_state->data_fd != -1) {
130         if (migration->data_fd != -1) {
131             /*
132              * This can happen if the device is asynchronously reset and
133              * terminates a data transfer.
134              */
135             error_report("%s: data_fd out of sync", vbasedev->name);
136             close(mig_state->data_fd);
137 
138             return -EBADF;
139         }
140 
141         migration->data_fd = mig_state->data_fd;
142     }
143 
144     trace_vfio_migration_set_state(vbasedev->name, mig_state_to_str(new_state));
145 
146     return 0;
147 
148 reset_device:
149     if (ioctl(vbasedev->fd, VFIO_DEVICE_RESET)) {
150         hw_error("%s: Failed resetting device, err: %s", vbasedev->name,
151                  strerror(errno));
152     }
153 
154     migration->device_state = VFIO_DEVICE_STATE_RUNNING;
155 
156     return ret;
157 }
158 
159 static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev,
160                             uint64_t data_size)
161 {
162     VFIOMigration *migration = vbasedev->migration;
163     int ret;
164 
165     ret = qemu_file_get_to_fd(f, migration->data_fd, data_size);
166     trace_vfio_load_state_device_data(vbasedev->name, data_size, ret);
167 
168     return ret;
169 }
170 
171 static int vfio_save_device_config_state(QEMUFile *f, void *opaque)
172 {
173     VFIODevice *vbasedev = opaque;
174 
175     qemu_put_be64(f, VFIO_MIG_FLAG_DEV_CONFIG_STATE);
176 
177     if (vbasedev->ops && vbasedev->ops->vfio_save_config) {
178         vbasedev->ops->vfio_save_config(vbasedev, f);
179     }
180 
181     qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE);
182 
183     trace_vfio_save_device_config_state(vbasedev->name);
184 
185     return qemu_file_get_error(f);
186 }
187 
188 static int vfio_load_device_config_state(QEMUFile *f, void *opaque)
189 {
190     VFIODevice *vbasedev = opaque;
191     uint64_t data;
192 
193     if (vbasedev->ops && vbasedev->ops->vfio_load_config) {
194         int ret;
195 
196         ret = vbasedev->ops->vfio_load_config(vbasedev, f);
197         if (ret) {
198             error_report("%s: Failed to load device config space",
199                          vbasedev->name);
200             return ret;
201         }
202     }
203 
204     data = qemu_get_be64(f);
205     if (data != VFIO_MIG_FLAG_END_OF_STATE) {
206         error_report("%s: Failed loading device config space, "
207                      "end flag incorrect 0x%"PRIx64, vbasedev->name, data);
208         return -EINVAL;
209     }
210 
211     trace_vfio_load_device_config_state(vbasedev->name);
212     return qemu_file_get_error(f);
213 }
214 
215 static void vfio_migration_cleanup(VFIODevice *vbasedev)
216 {
217     VFIOMigration *migration = vbasedev->migration;
218 
219     close(migration->data_fd);
220     migration->data_fd = -1;
221 }
222 
223 static int vfio_query_stop_copy_size(VFIODevice *vbasedev,
224                                      uint64_t *stop_copy_size)
225 {
226     uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +
227                               sizeof(struct vfio_device_feature_mig_data_size),
228                               sizeof(uint64_t))] = {};
229     struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
230     struct vfio_device_feature_mig_data_size *mig_data_size =
231         (struct vfio_device_feature_mig_data_size *)feature->data;
232 
233     feature->argsz = sizeof(buf);
234     feature->flags =
235         VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_MIG_DATA_SIZE;
236 
237     if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
238         return -errno;
239     }
240 
241     *stop_copy_size = mig_data_size->stop_copy_length;
242 
243     return 0;
244 }
245 
246 static int vfio_query_precopy_size(VFIOMigration *migration)
247 {
248     struct vfio_precopy_info precopy = {
249         .argsz = sizeof(precopy),
250     };
251 
252     migration->precopy_init_size = 0;
253     migration->precopy_dirty_size = 0;
254 
255     if (ioctl(migration->data_fd, VFIO_MIG_GET_PRECOPY_INFO, &precopy)) {
256         return -errno;
257     }
258 
259     migration->precopy_init_size = precopy.initial_bytes;
260     migration->precopy_dirty_size = precopy.dirty_bytes;
261 
262     return 0;
263 }
264 
265 /* Returns the size of saved data on success and -errno on error */
266 static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration)
267 {
268     ssize_t data_size;
269 
270     data_size = read(migration->data_fd, migration->data_buffer,
271                      migration->data_buffer_size);
272     if (data_size < 0) {
273         /*
274          * Pre-copy emptied all the device state for now. For more information,
275          * please refer to the Linux kernel VFIO uAPI.
276          */
277         if (errno == ENOMSG) {
278             return 0;
279         }
280 
281         return -errno;
282     }
283     if (data_size == 0) {
284         return 0;
285     }
286 
287     qemu_put_be64(f, VFIO_MIG_FLAG_DEV_DATA_STATE);
288     qemu_put_be64(f, data_size);
289     qemu_put_buffer(f, migration->data_buffer, data_size);
290     bytes_transferred += data_size;
291 
292     trace_vfio_save_block(migration->vbasedev->name, data_size);
293 
294     return qemu_file_get_error(f) ?: data_size;
295 }
296 
297 static void vfio_update_estimated_pending_data(VFIOMigration *migration,
298                                                uint64_t data_size)
299 {
300     if (!data_size) {
301         /*
302          * Pre-copy emptied all the device state for now, update estimated sizes
303          * accordingly.
304          */
305         migration->precopy_init_size = 0;
306         migration->precopy_dirty_size = 0;
307 
308         return;
309     }
310 
311     if (migration->precopy_init_size) {
312         uint64_t init_size = MIN(migration->precopy_init_size, data_size);
313 
314         migration->precopy_init_size -= init_size;
315         data_size -= init_size;
316     }
317 
318     migration->precopy_dirty_size -= MIN(migration->precopy_dirty_size,
319                                          data_size);
320 }
321 
322 static bool vfio_precopy_supported(VFIODevice *vbasedev)
323 {
324     VFIOMigration *migration = vbasedev->migration;
325 
326     return migration->mig_flags & VFIO_MIGRATION_PRE_COPY;
327 }
328 
329 /* ---------------------------------------------------------------------- */
330 
331 static int vfio_save_setup(QEMUFile *f, void *opaque)
332 {
333     VFIODevice *vbasedev = opaque;
334     VFIOMigration *migration = vbasedev->migration;
335     uint64_t stop_copy_size = VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE;
336 
337     qemu_put_be64(f, VFIO_MIG_FLAG_DEV_SETUP_STATE);
338 
339     vfio_query_stop_copy_size(vbasedev, &stop_copy_size);
340     migration->data_buffer_size = MIN(VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE,
341                                       stop_copy_size);
342     migration->data_buffer = g_try_malloc0(migration->data_buffer_size);
343     if (!migration->data_buffer) {
344         error_report("%s: Failed to allocate migration data buffer",
345                      vbasedev->name);
346         return -ENOMEM;
347     }
348 
349     if (vfio_precopy_supported(vbasedev)) {
350         int ret;
351 
352         switch (migration->device_state) {
353         case VFIO_DEVICE_STATE_RUNNING:
354             ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_PRE_COPY,
355                                            VFIO_DEVICE_STATE_RUNNING);
356             if (ret) {
357                 return ret;
358             }
359 
360             vfio_query_precopy_size(migration);
361 
362             break;
363         case VFIO_DEVICE_STATE_STOP:
364             /* vfio_save_complete_precopy() will go to STOP_COPY */
365             break;
366         default:
367             return -EINVAL;
368         }
369     }
370 
371     trace_vfio_save_setup(vbasedev->name, migration->data_buffer_size);
372 
373     qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE);
374 
375     return qemu_file_get_error(f);
376 }
377 
378 static void vfio_save_cleanup(void *opaque)
379 {
380     VFIODevice *vbasedev = opaque;
381     VFIOMigration *migration = vbasedev->migration;
382 
383     g_free(migration->data_buffer);
384     migration->data_buffer = NULL;
385     migration->precopy_init_size = 0;
386     migration->precopy_dirty_size = 0;
387     vfio_migration_cleanup(vbasedev);
388     trace_vfio_save_cleanup(vbasedev->name);
389 }
390 
391 static void vfio_state_pending_estimate(void *opaque, uint64_t *must_precopy,
392                                         uint64_t *can_postcopy)
393 {
394     VFIODevice *vbasedev = opaque;
395     VFIOMigration *migration = vbasedev->migration;
396 
397     if (migration->device_state != VFIO_DEVICE_STATE_PRE_COPY) {
398         return;
399     }
400 
401     *must_precopy +=
402         migration->precopy_init_size + migration->precopy_dirty_size;
403 
404     trace_vfio_state_pending_estimate(vbasedev->name, *must_precopy,
405                                       *can_postcopy,
406                                       migration->precopy_init_size,
407                                       migration->precopy_dirty_size);
408 }
409 
410 /*
411  * Migration size of VFIO devices can be as little as a few KBs or as big as
412  * many GBs. This value should be big enough to cover the worst case.
413  */
414 #define VFIO_MIG_STOP_COPY_SIZE (100 * GiB)
415 
416 static void vfio_state_pending_exact(void *opaque, uint64_t *must_precopy,
417                                      uint64_t *can_postcopy)
418 {
419     VFIODevice *vbasedev = opaque;
420     VFIOMigration *migration = vbasedev->migration;
421     uint64_t stop_copy_size = VFIO_MIG_STOP_COPY_SIZE;
422 
423     /*
424      * If getting pending migration size fails, VFIO_MIG_STOP_COPY_SIZE is
425      * reported so downtime limit won't be violated.
426      */
427     vfio_query_stop_copy_size(vbasedev, &stop_copy_size);
428     *must_precopy += stop_copy_size;
429 
430     if (migration->device_state == VFIO_DEVICE_STATE_PRE_COPY) {
431         vfio_query_precopy_size(migration);
432 
433         *must_precopy +=
434             migration->precopy_init_size + migration->precopy_dirty_size;
435     }
436 
437     trace_vfio_state_pending_exact(vbasedev->name, *must_precopy, *can_postcopy,
438                                    stop_copy_size, migration->precopy_init_size,
439                                    migration->precopy_dirty_size);
440 }
441 
442 static bool vfio_is_active_iterate(void *opaque)
443 {
444     VFIODevice *vbasedev = opaque;
445     VFIOMigration *migration = vbasedev->migration;
446 
447     return migration->device_state == VFIO_DEVICE_STATE_PRE_COPY;
448 }
449 
450 static int vfio_save_iterate(QEMUFile *f, void *opaque)
451 {
452     VFIODevice *vbasedev = opaque;
453     VFIOMigration *migration = vbasedev->migration;
454     ssize_t data_size;
455 
456     data_size = vfio_save_block(f, migration);
457     if (data_size < 0) {
458         return data_size;
459     }
460     qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE);
461 
462     vfio_update_estimated_pending_data(migration, data_size);
463 
464     trace_vfio_save_iterate(vbasedev->name, migration->precopy_init_size,
465                             migration->precopy_dirty_size);
466 
467     /*
468      * A VFIO device's pre-copy dirty_bytes is not guaranteed to reach zero.
469      * Return 1 so following handlers will not be potentially blocked.
470      */
471     return 1;
472 }
473 
474 static int vfio_save_complete_precopy(QEMUFile *f, void *opaque)
475 {
476     VFIODevice *vbasedev = opaque;
477     ssize_t data_size;
478     int ret;
479 
480     /* We reach here with device state STOP or STOP_COPY only */
481     ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP_COPY,
482                                    VFIO_DEVICE_STATE_STOP);
483     if (ret) {
484         return ret;
485     }
486 
487     do {
488         data_size = vfio_save_block(f, vbasedev->migration);
489         if (data_size < 0) {
490             return data_size;
491         }
492     } while (data_size);
493 
494     qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE);
495     ret = qemu_file_get_error(f);
496     if (ret) {
497         return ret;
498     }
499 
500     /*
501      * If setting the device in STOP state fails, the device should be reset.
502      * To do so, use ERROR state as a recover state.
503      */
504     ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP,
505                                    VFIO_DEVICE_STATE_ERROR);
506     trace_vfio_save_complete_precopy(vbasedev->name, ret);
507 
508     return ret;
509 }
510 
511 static void vfio_save_state(QEMUFile *f, void *opaque)
512 {
513     VFIODevice *vbasedev = opaque;
514     int ret;
515 
516     ret = vfio_save_device_config_state(f, opaque);
517     if (ret) {
518         error_report("%s: Failed to save device config space",
519                      vbasedev->name);
520         qemu_file_set_error(f, ret);
521     }
522 }
523 
524 static int vfio_load_setup(QEMUFile *f, void *opaque)
525 {
526     VFIODevice *vbasedev = opaque;
527 
528     return vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RESUMING,
529                                    vbasedev->migration->device_state);
530 }
531 
532 static int vfio_load_cleanup(void *opaque)
533 {
534     VFIODevice *vbasedev = opaque;
535 
536     vfio_migration_cleanup(vbasedev);
537     trace_vfio_load_cleanup(vbasedev->name);
538 
539     return 0;
540 }
541 
542 static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)
543 {
544     VFIODevice *vbasedev = opaque;
545     int ret = 0;
546     uint64_t data;
547 
548     data = qemu_get_be64(f);
549     while (data != VFIO_MIG_FLAG_END_OF_STATE) {
550 
551         trace_vfio_load_state(vbasedev->name, data);
552 
553         switch (data) {
554         case VFIO_MIG_FLAG_DEV_CONFIG_STATE:
555         {
556             return vfio_load_device_config_state(f, opaque);
557         }
558         case VFIO_MIG_FLAG_DEV_SETUP_STATE:
559         {
560             data = qemu_get_be64(f);
561             if (data == VFIO_MIG_FLAG_END_OF_STATE) {
562                 return ret;
563             } else {
564                 error_report("%s: SETUP STATE: EOS not found 0x%"PRIx64,
565                              vbasedev->name, data);
566                 return -EINVAL;
567             }
568             break;
569         }
570         case VFIO_MIG_FLAG_DEV_DATA_STATE:
571         {
572             uint64_t data_size = qemu_get_be64(f);
573 
574             if (data_size) {
575                 ret = vfio_load_buffer(f, vbasedev, data_size);
576                 if (ret < 0) {
577                     return ret;
578                 }
579             }
580             break;
581         }
582         default:
583             error_report("%s: Unknown tag 0x%"PRIx64, vbasedev->name, data);
584             return -EINVAL;
585         }
586 
587         data = qemu_get_be64(f);
588         ret = qemu_file_get_error(f);
589         if (ret) {
590             return ret;
591         }
592     }
593     return ret;
594 }
595 
596 static const SaveVMHandlers savevm_vfio_handlers = {
597     .save_setup = vfio_save_setup,
598     .save_cleanup = vfio_save_cleanup,
599     .state_pending_estimate = vfio_state_pending_estimate,
600     .state_pending_exact = vfio_state_pending_exact,
601     .is_active_iterate = vfio_is_active_iterate,
602     .save_live_iterate = vfio_save_iterate,
603     .save_live_complete_precopy = vfio_save_complete_precopy,
604     .save_state = vfio_save_state,
605     .load_setup = vfio_load_setup,
606     .load_cleanup = vfio_load_cleanup,
607     .load_state = vfio_load_state,
608 };
609 
610 /* ---------------------------------------------------------------------- */
611 
612 static void vfio_vmstate_change(void *opaque, bool running, RunState state)
613 {
614     VFIODevice *vbasedev = opaque;
615     VFIOMigration *migration = vbasedev->migration;
616     enum vfio_device_mig_state new_state;
617     int ret;
618 
619     if (running) {
620         new_state = VFIO_DEVICE_STATE_RUNNING;
621     } else {
622         new_state =
623             (migration->device_state == VFIO_DEVICE_STATE_PRE_COPY &&
624              (state == RUN_STATE_FINISH_MIGRATE || state == RUN_STATE_PAUSED)) ?
625                 VFIO_DEVICE_STATE_STOP_COPY :
626                 VFIO_DEVICE_STATE_STOP;
627     }
628 
629     /*
630      * If setting the device in new_state fails, the device should be reset.
631      * To do so, use ERROR state as a recover state.
632      */
633     ret = vfio_migration_set_state(vbasedev, new_state,
634                                    VFIO_DEVICE_STATE_ERROR);
635     if (ret) {
636         /*
637          * Migration should be aborted in this case, but vm_state_notify()
638          * currently does not support reporting failures.
639          */
640         if (migrate_get_current()->to_dst_file) {
641             qemu_file_set_error(migrate_get_current()->to_dst_file, ret);
642         }
643     }
644 
645     trace_vfio_vmstate_change(vbasedev->name, running, RunState_str(state),
646                               mig_state_to_str(new_state));
647 }
648 
649 static void vfio_migration_state_notifier(Notifier *notifier, void *data)
650 {
651     MigrationState *s = data;
652     VFIOMigration *migration = container_of(notifier, VFIOMigration,
653                                             migration_state);
654     VFIODevice *vbasedev = migration->vbasedev;
655 
656     trace_vfio_migration_state_notifier(vbasedev->name,
657                                         MigrationStatus_str(s->state));
658 
659     switch (s->state) {
660     case MIGRATION_STATUS_CANCELLING:
661     case MIGRATION_STATUS_CANCELLED:
662     case MIGRATION_STATUS_FAILED:
663         bytes_transferred = 0;
664         /*
665          * If setting the device in RUNNING state fails, the device should
666          * be reset. To do so, use ERROR state as a recover state.
667          */
668         vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RUNNING,
669                                  VFIO_DEVICE_STATE_ERROR);
670     }
671 }
672 
673 static void vfio_migration_free(VFIODevice *vbasedev)
674 {
675     g_free(vbasedev->migration);
676     vbasedev->migration = NULL;
677 }
678 
679 static int vfio_migration_query_flags(VFIODevice *vbasedev, uint64_t *mig_flags)
680 {
681     uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +
682                                   sizeof(struct vfio_device_feature_migration),
683                               sizeof(uint64_t))] = {};
684     struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
685     struct vfio_device_feature_migration *mig =
686         (struct vfio_device_feature_migration *)feature->data;
687 
688     feature->argsz = sizeof(buf);
689     feature->flags = VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_MIGRATION;
690     if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
691         if (errno == ENOTTY) {
692             error_report("%s: VFIO migration is not supported in kernel",
693                          vbasedev->name);
694         } else {
695             error_report("%s: Failed to query VFIO migration support, err: %s",
696                          vbasedev->name, strerror(errno));
697         }
698 
699         return -errno;
700     }
701 
702     *mig_flags = mig->flags;
703 
704     return 0;
705 }
706 
707 static bool vfio_dma_logging_supported(VFIODevice *vbasedev)
708 {
709     uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature),
710                               sizeof(uint64_t))] = {};
711     struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
712 
713     feature->argsz = sizeof(buf);
714     feature->flags = VFIO_DEVICE_FEATURE_PROBE |
715                      VFIO_DEVICE_FEATURE_DMA_LOGGING_START;
716 
717     return !ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature);
718 }
719 
720 static int vfio_migration_init(VFIODevice *vbasedev)
721 {
722     int ret;
723     Object *obj;
724     VFIOMigration *migration;
725     char id[256] = "";
726     g_autofree char *path = NULL, *oid = NULL;
727     uint64_t mig_flags = 0;
728 
729     if (!vbasedev->ops->vfio_get_object) {
730         return -EINVAL;
731     }
732 
733     obj = vbasedev->ops->vfio_get_object(vbasedev);
734     if (!obj) {
735         return -EINVAL;
736     }
737 
738     ret = vfio_migration_query_flags(vbasedev, &mig_flags);
739     if (ret) {
740         return ret;
741     }
742 
743     /* Basic migration functionality must be supported */
744     if (!(mig_flags & VFIO_MIGRATION_STOP_COPY)) {
745         return -EOPNOTSUPP;
746     }
747 
748     vbasedev->migration = g_new0(VFIOMigration, 1);
749     migration = vbasedev->migration;
750     migration->vbasedev = vbasedev;
751     migration->device_state = VFIO_DEVICE_STATE_RUNNING;
752     migration->data_fd = -1;
753     migration->mig_flags = mig_flags;
754 
755     vbasedev->dirty_pages_supported = vfio_dma_logging_supported(vbasedev);
756 
757     oid = vmstate_if_get_id(VMSTATE_IF(DEVICE(obj)));
758     if (oid) {
759         path = g_strdup_printf("%s/vfio", oid);
760     } else {
761         path = g_strdup("vfio");
762     }
763     strpadcpy(id, sizeof(id), path, '\0');
764 
765     register_savevm_live(id, VMSTATE_INSTANCE_ID_ANY, 1, &savevm_vfio_handlers,
766                          vbasedev);
767 
768     migration->vm_state = qdev_add_vm_change_state_handler(vbasedev->dev,
769                                                            vfio_vmstate_change,
770                                                            vbasedev);
771     migration->migration_state.notify = vfio_migration_state_notifier;
772     add_migration_state_change_notifier(&migration->migration_state);
773 
774     return 0;
775 }
776 
777 /* ---------------------------------------------------------------------- */
778 
779 int64_t vfio_mig_bytes_transferred(void)
780 {
781     return bytes_transferred;
782 }
783 
784 int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
785 {
786     int ret = -ENOTSUP;
787 
788     if (!vbasedev->enable_migration) {
789         goto add_blocker;
790     }
791 
792     ret = vfio_migration_init(vbasedev);
793     if (ret) {
794         goto add_blocker;
795     }
796 
797     ret = vfio_block_multiple_devices_migration(errp);
798     if (ret) {
799         return ret;
800     }
801 
802     ret = vfio_block_giommu_migration(errp);
803     if (ret) {
804         return ret;
805     }
806 
807     trace_vfio_migration_probe(vbasedev->name);
808     return 0;
809 
810 add_blocker:
811     error_setg(&vbasedev->migration_blocker,
812                "VFIO device doesn't support migration");
813 
814     ret = migrate_add_blocker(vbasedev->migration_blocker, errp);
815     if (ret < 0) {
816         error_free(vbasedev->migration_blocker);
817         vbasedev->migration_blocker = NULL;
818     }
819     return ret;
820 }
821 
822 void vfio_migration_exit(VFIODevice *vbasedev)
823 {
824     if (vbasedev->migration) {
825         VFIOMigration *migration = vbasedev->migration;
826 
827         remove_migration_state_change_notifier(&migration->migration_state);
828         qemu_del_vm_change_state_handler(migration->vm_state);
829         unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
830         vfio_migration_free(vbasedev);
831         vfio_unblock_multiple_devices_migration();
832     }
833 
834     if (vbasedev->migration_blocker) {
835         migrate_del_blocker(vbasedev->migration_blocker);
836         error_free(vbasedev->migration_blocker);
837         vbasedev->migration_blocker = NULL;
838     }
839 }
840