Lines Matching refs:vm

283 static void virtio_mem_retry(struct virtio_mem *vm);
284 static int virtio_mem_create_resource(struct virtio_mem *vm);
285 static void virtio_mem_delete_resource(struct virtio_mem *vm);
291 static int register_virtio_mem_device(struct virtio_mem *vm) in register_virtio_mem_device() argument
300 list_add_rcu(&vm->next, &virtio_mem_devices); in register_virtio_mem_device()
310 static void unregister_virtio_mem_device(struct virtio_mem *vm) in unregister_virtio_mem_device() argument
314 list_del_rcu(&vm->next); in unregister_virtio_mem_device()
341 static unsigned long virtio_mem_phys_to_bb_id(struct virtio_mem *vm, in virtio_mem_phys_to_bb_id() argument
344 return addr / vm->bbm.bb_size; in virtio_mem_phys_to_bb_id()
350 static uint64_t virtio_mem_bb_id_to_phys(struct virtio_mem *vm, in virtio_mem_bb_id_to_phys() argument
353 return bb_id * vm->bbm.bb_size; in virtio_mem_bb_id_to_phys()
359 static unsigned long virtio_mem_phys_to_sb_id(struct virtio_mem *vm, in virtio_mem_phys_to_sb_id() argument
365 return (addr - mb_addr) / vm->sbm.sb_size; in virtio_mem_phys_to_sb_id()
371 static void virtio_mem_bbm_set_bb_state(struct virtio_mem *vm, in virtio_mem_bbm_set_bb_state() argument
375 const unsigned long idx = bb_id - vm->bbm.first_bb_id; in virtio_mem_bbm_set_bb_state()
378 old_state = vm->bbm.bb_states[idx]; in virtio_mem_bbm_set_bb_state()
379 vm->bbm.bb_states[idx] = state; in virtio_mem_bbm_set_bb_state()
381 BUG_ON(vm->bbm.bb_count[old_state] == 0); in virtio_mem_bbm_set_bb_state()
382 vm->bbm.bb_count[old_state]--; in virtio_mem_bbm_set_bb_state()
383 vm->bbm.bb_count[state]++; in virtio_mem_bbm_set_bb_state()
389 static enum virtio_mem_bbm_bb_state virtio_mem_bbm_get_bb_state(struct virtio_mem *vm, in virtio_mem_bbm_get_bb_state() argument
392 return vm->bbm.bb_states[bb_id - vm->bbm.first_bb_id]; in virtio_mem_bbm_get_bb_state()
398 static int virtio_mem_bbm_bb_states_prepare_next_bb(struct virtio_mem *vm) in virtio_mem_bbm_bb_states_prepare_next_bb() argument
400 unsigned long old_bytes = vm->bbm.next_bb_id - vm->bbm.first_bb_id; in virtio_mem_bbm_bb_states_prepare_next_bb()
406 if (vm->bbm.bb_states && old_pages == new_pages) in virtio_mem_bbm_bb_states_prepare_next_bb()
413 mutex_lock(&vm->hotplug_mutex); in virtio_mem_bbm_bb_states_prepare_next_bb()
414 if (vm->bbm.bb_states) in virtio_mem_bbm_bb_states_prepare_next_bb()
415 memcpy(new_array, vm->bbm.bb_states, old_pages * PAGE_SIZE); in virtio_mem_bbm_bb_states_prepare_next_bb()
416 vfree(vm->bbm.bb_states); in virtio_mem_bbm_bb_states_prepare_next_bb()
417 vm->bbm.bb_states = new_array; in virtio_mem_bbm_bb_states_prepare_next_bb()
418 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_bbm_bb_states_prepare_next_bb()
424 for (_bb_id = vm->bbm.first_bb_id; \
425 _bb_id < vm->bbm.next_bb_id && _vm->bbm.bb_count[_state]; \
430 for (_bb_id = vm->bbm.next_bb_id - 1; \
431 _bb_id >= vm->bbm.first_bb_id && _vm->bbm.bb_count[_state]; \
438 static void virtio_mem_sbm_set_mb_state(struct virtio_mem *vm, in virtio_mem_sbm_set_mb_state() argument
441 const unsigned long idx = mb_id - vm->sbm.first_mb_id; in virtio_mem_sbm_set_mb_state()
444 old_state = vm->sbm.mb_states[idx]; in virtio_mem_sbm_set_mb_state()
445 vm->sbm.mb_states[idx] = state; in virtio_mem_sbm_set_mb_state()
447 BUG_ON(vm->sbm.mb_count[old_state] == 0); in virtio_mem_sbm_set_mb_state()
448 vm->sbm.mb_count[old_state]--; in virtio_mem_sbm_set_mb_state()
449 vm->sbm.mb_count[state]++; in virtio_mem_sbm_set_mb_state()
455 static uint8_t virtio_mem_sbm_get_mb_state(struct virtio_mem *vm, in virtio_mem_sbm_get_mb_state() argument
458 const unsigned long idx = mb_id - vm->sbm.first_mb_id; in virtio_mem_sbm_get_mb_state()
460 return vm->sbm.mb_states[idx]; in virtio_mem_sbm_get_mb_state()
466 static int virtio_mem_sbm_mb_states_prepare_next_mb(struct virtio_mem *vm) in virtio_mem_sbm_mb_states_prepare_next_mb() argument
468 int old_pages = PFN_UP(vm->sbm.next_mb_id - vm->sbm.first_mb_id); in virtio_mem_sbm_mb_states_prepare_next_mb()
469 int new_pages = PFN_UP(vm->sbm.next_mb_id - vm->sbm.first_mb_id + 1); in virtio_mem_sbm_mb_states_prepare_next_mb()
472 if (vm->sbm.mb_states && old_pages == new_pages) in virtio_mem_sbm_mb_states_prepare_next_mb()
479 mutex_lock(&vm->hotplug_mutex); in virtio_mem_sbm_mb_states_prepare_next_mb()
480 if (vm->sbm.mb_states) in virtio_mem_sbm_mb_states_prepare_next_mb()
481 memcpy(new_array, vm->sbm.mb_states, old_pages * PAGE_SIZE); in virtio_mem_sbm_mb_states_prepare_next_mb()
482 vfree(vm->sbm.mb_states); in virtio_mem_sbm_mb_states_prepare_next_mb()
483 vm->sbm.mb_states = new_array; in virtio_mem_sbm_mb_states_prepare_next_mb()
484 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_sbm_mb_states_prepare_next_mb()
505 static int virtio_mem_sbm_sb_state_bit_nr(struct virtio_mem *vm, in virtio_mem_sbm_sb_state_bit_nr() argument
508 return (mb_id - vm->sbm.first_mb_id) * vm->sbm.sbs_per_mb + sb_id; in virtio_mem_sbm_sb_state_bit_nr()
516 static void virtio_mem_sbm_set_sb_plugged(struct virtio_mem *vm, in virtio_mem_sbm_set_sb_plugged() argument
520 const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id); in virtio_mem_sbm_set_sb_plugged()
522 __bitmap_set(vm->sbm.sb_states, bit, count); in virtio_mem_sbm_set_sb_plugged()
530 static void virtio_mem_sbm_set_sb_unplugged(struct virtio_mem *vm, in virtio_mem_sbm_set_sb_unplugged() argument
534 const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id); in virtio_mem_sbm_set_sb_unplugged()
536 __bitmap_clear(vm->sbm.sb_states, bit, count); in virtio_mem_sbm_set_sb_unplugged()
542 static bool virtio_mem_sbm_test_sb_plugged(struct virtio_mem *vm, in virtio_mem_sbm_test_sb_plugged() argument
546 const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id); in virtio_mem_sbm_test_sb_plugged()
549 return test_bit(bit, vm->sbm.sb_states); in virtio_mem_sbm_test_sb_plugged()
552 return find_next_zero_bit(vm->sbm.sb_states, bit + count, bit) >= in virtio_mem_sbm_test_sb_plugged()
559 static bool virtio_mem_sbm_test_sb_unplugged(struct virtio_mem *vm, in virtio_mem_sbm_test_sb_unplugged() argument
563 const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id); in virtio_mem_sbm_test_sb_unplugged()
566 return find_next_bit(vm->sbm.sb_states, bit + count, bit) >= in virtio_mem_sbm_test_sb_unplugged()
574 static int virtio_mem_sbm_first_unplugged_sb(struct virtio_mem *vm, in virtio_mem_sbm_first_unplugged_sb() argument
577 const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, 0); in virtio_mem_sbm_first_unplugged_sb()
579 return find_next_zero_bit(vm->sbm.sb_states, in virtio_mem_sbm_first_unplugged_sb()
580 bit + vm->sbm.sbs_per_mb, bit) - bit; in virtio_mem_sbm_first_unplugged_sb()
586 static int virtio_mem_sbm_sb_states_prepare_next_mb(struct virtio_mem *vm) in virtio_mem_sbm_sb_states_prepare_next_mb() argument
588 const unsigned long old_nb_mb = vm->sbm.next_mb_id - vm->sbm.first_mb_id; in virtio_mem_sbm_sb_states_prepare_next_mb()
589 const unsigned long old_nb_bits = old_nb_mb * vm->sbm.sbs_per_mb; in virtio_mem_sbm_sb_states_prepare_next_mb()
590 const unsigned long new_nb_bits = (old_nb_mb + 1) * vm->sbm.sbs_per_mb; in virtio_mem_sbm_sb_states_prepare_next_mb()
595 if (vm->sbm.sb_states && old_pages == new_pages) in virtio_mem_sbm_sb_states_prepare_next_mb()
602 mutex_lock(&vm->hotplug_mutex); in virtio_mem_sbm_sb_states_prepare_next_mb()
603 if (vm->sbm.sb_states) in virtio_mem_sbm_sb_states_prepare_next_mb()
604 memcpy(new_bitmap, vm->sbm.sb_states, old_pages * PAGE_SIZE); in virtio_mem_sbm_sb_states_prepare_next_mb()
606 old_bitmap = vm->sbm.sb_states; in virtio_mem_sbm_sb_states_prepare_next_mb()
607 vm->sbm.sb_states = new_bitmap; in virtio_mem_sbm_sb_states_prepare_next_mb()
608 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_sbm_sb_states_prepare_next_mb()
618 static bool virtio_mem_could_add_memory(struct virtio_mem *vm, uint64_t size) in virtio_mem_could_add_memory() argument
620 if (WARN_ON_ONCE(size > vm->offline_threshold)) in virtio_mem_could_add_memory()
623 return atomic64_read(&vm->offline_size) + size <= vm->offline_threshold; in virtio_mem_could_add_memory()
634 static int virtio_mem_add_memory(struct virtio_mem *vm, uint64_t addr, in virtio_mem_add_memory() argument
643 if (!vm->resource_name) { in virtio_mem_add_memory()
644 vm->resource_name = kstrdup_const("System RAM (virtio_mem)", in virtio_mem_add_memory()
646 if (!vm->resource_name) in virtio_mem_add_memory()
650 dev_dbg(&vm->vdev->dev, "adding memory: 0x%llx - 0x%llx\n", addr, in virtio_mem_add_memory()
653 atomic64_add(size, &vm->offline_size); in virtio_mem_add_memory()
654 rc = add_memory_driver_managed(vm->mgid, addr, size, vm->resource_name, in virtio_mem_add_memory()
657 atomic64_sub(size, &vm->offline_size); in virtio_mem_add_memory()
658 dev_warn(&vm->vdev->dev, "adding memory failed: %d\n", rc); in virtio_mem_add_memory()
670 static int virtio_mem_sbm_add_mb(struct virtio_mem *vm, unsigned long mb_id) in virtio_mem_sbm_add_mb() argument
675 return virtio_mem_add_memory(vm, addr, size); in virtio_mem_sbm_add_mb()
681 static int virtio_mem_bbm_add_bb(struct virtio_mem *vm, unsigned long bb_id) in virtio_mem_bbm_add_bb() argument
683 const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id); in virtio_mem_bbm_add_bb()
684 const uint64_t size = vm->bbm.bb_size; in virtio_mem_bbm_add_bb()
686 return virtio_mem_add_memory(vm, addr, size); in virtio_mem_bbm_add_bb()
698 static int virtio_mem_remove_memory(struct virtio_mem *vm, uint64_t addr, in virtio_mem_remove_memory() argument
703 dev_dbg(&vm->vdev->dev, "removing memory: 0x%llx - 0x%llx\n", addr, in virtio_mem_remove_memory()
707 atomic64_sub(size, &vm->offline_size); in virtio_mem_remove_memory()
712 virtio_mem_retry(vm); in virtio_mem_remove_memory()
714 dev_dbg(&vm->vdev->dev, "removing memory failed: %d\n", rc); in virtio_mem_remove_memory()
722 static int virtio_mem_sbm_remove_mb(struct virtio_mem *vm, unsigned long mb_id) in virtio_mem_sbm_remove_mb() argument
727 return virtio_mem_remove_memory(vm, addr, size); in virtio_mem_sbm_remove_mb()
738 static int virtio_mem_offline_and_remove_memory(struct virtio_mem *vm, in virtio_mem_offline_and_remove_memory() argument
744 dev_dbg(&vm->vdev->dev, in virtio_mem_offline_and_remove_memory()
750 atomic64_sub(size, &vm->offline_size); in virtio_mem_offline_and_remove_memory()
755 virtio_mem_retry(vm); in virtio_mem_offline_and_remove_memory()
758 dev_dbg(&vm->vdev->dev, "offlining and removing memory failed: %d\n", rc); in virtio_mem_offline_and_remove_memory()
771 static int virtio_mem_sbm_offline_and_remove_mb(struct virtio_mem *vm, in virtio_mem_sbm_offline_and_remove_mb() argument
777 return virtio_mem_offline_and_remove_memory(vm, addr, size); in virtio_mem_sbm_offline_and_remove_mb()
786 static int virtio_mem_sbm_try_remove_unplugged_mb(struct virtio_mem *vm, in virtio_mem_sbm_try_remove_unplugged_mb() argument
795 if (!virtio_mem_sbm_test_sb_unplugged(vm, mb_id, 0, vm->sbm.sbs_per_mb)) in virtio_mem_sbm_try_remove_unplugged_mb()
799 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_sbm_try_remove_unplugged_mb()
800 rc = virtio_mem_sbm_offline_and_remove_mb(vm, mb_id); in virtio_mem_sbm_try_remove_unplugged_mb()
801 mutex_lock(&vm->hotplug_mutex); in virtio_mem_sbm_try_remove_unplugged_mb()
803 virtio_mem_sbm_set_mb_state(vm, mb_id, in virtio_mem_sbm_try_remove_unplugged_mb()
812 static int virtio_mem_bbm_offline_and_remove_bb(struct virtio_mem *vm, in virtio_mem_bbm_offline_and_remove_bb() argument
815 const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id); in virtio_mem_bbm_offline_and_remove_bb()
816 const uint64_t size = vm->bbm.bb_size; in virtio_mem_bbm_offline_and_remove_bb()
818 return virtio_mem_offline_and_remove_memory(vm, addr, size); in virtio_mem_bbm_offline_and_remove_bb()
824 static void virtio_mem_retry(struct virtio_mem *vm) in virtio_mem_retry() argument
828 spin_lock_irqsave(&vm->removal_lock, flags); in virtio_mem_retry()
829 if (!vm->removing) in virtio_mem_retry()
830 queue_work(system_freezable_wq, &vm->wq); in virtio_mem_retry()
831 spin_unlock_irqrestore(&vm->removal_lock, flags); in virtio_mem_retry()
834 static int virtio_mem_translate_node_id(struct virtio_mem *vm, uint16_t node_id) in virtio_mem_translate_node_id() argument
839 if (virtio_has_feature(vm->vdev, VIRTIO_MEM_F_ACPI_PXM)) in virtio_mem_translate_node_id()
849 static bool virtio_mem_overlaps_range(struct virtio_mem *vm, uint64_t start, in virtio_mem_overlaps_range() argument
852 return start < vm->addr + vm->region_size && vm->addr < start + size; in virtio_mem_overlaps_range()
859 static bool virtio_mem_contains_range(struct virtio_mem *vm, uint64_t start, in virtio_mem_contains_range() argument
862 return start >= vm->addr && start + size <= vm->addr + vm->region_size; in virtio_mem_contains_range()
865 static int virtio_mem_sbm_notify_going_online(struct virtio_mem *vm, in virtio_mem_sbm_notify_going_online() argument
868 switch (virtio_mem_sbm_get_mb_state(vm, mb_id)) { in virtio_mem_sbm_notify_going_online()
875 dev_warn_ratelimited(&vm->vdev->dev, in virtio_mem_sbm_notify_going_online()
880 static void virtio_mem_sbm_notify_offline(struct virtio_mem *vm, in virtio_mem_sbm_notify_offline() argument
883 switch (virtio_mem_sbm_get_mb_state(vm, mb_id)) { in virtio_mem_sbm_notify_offline()
886 virtio_mem_sbm_set_mb_state(vm, mb_id, in virtio_mem_sbm_notify_offline()
891 virtio_mem_sbm_set_mb_state(vm, mb_id, in virtio_mem_sbm_notify_offline()
900 static void virtio_mem_sbm_notify_online(struct virtio_mem *vm, in virtio_mem_sbm_notify_online() argument
907 switch (virtio_mem_sbm_get_mb_state(vm, mb_id)) { in virtio_mem_sbm_notify_online()
922 virtio_mem_sbm_set_mb_state(vm, mb_id, new_state); in virtio_mem_sbm_notify_online()
925 static void virtio_mem_sbm_notify_going_offline(struct virtio_mem *vm, in virtio_mem_sbm_notify_going_offline() argument
928 const unsigned long nr_pages = PFN_DOWN(vm->sbm.sb_size); in virtio_mem_sbm_notify_going_offline()
932 for (sb_id = 0; sb_id < vm->sbm.sbs_per_mb; sb_id++) { in virtio_mem_sbm_notify_going_offline()
933 if (virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id, 1)) in virtio_mem_sbm_notify_going_offline()
936 sb_id * vm->sbm.sb_size); in virtio_mem_sbm_notify_going_offline()
941 static void virtio_mem_sbm_notify_cancel_offline(struct virtio_mem *vm, in virtio_mem_sbm_notify_cancel_offline() argument
944 const unsigned long nr_pages = PFN_DOWN(vm->sbm.sb_size); in virtio_mem_sbm_notify_cancel_offline()
948 for (sb_id = 0; sb_id < vm->sbm.sbs_per_mb; sb_id++) { in virtio_mem_sbm_notify_cancel_offline()
949 if (virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id, 1)) in virtio_mem_sbm_notify_cancel_offline()
952 sb_id * vm->sbm.sb_size); in virtio_mem_sbm_notify_cancel_offline()
957 static void virtio_mem_bbm_notify_going_offline(struct virtio_mem *vm, in virtio_mem_bbm_notify_going_offline() argument
966 if (virtio_mem_bbm_get_bb_state(vm, bb_id) != in virtio_mem_bbm_notify_going_offline()
972 static void virtio_mem_bbm_notify_cancel_offline(struct virtio_mem *vm, in virtio_mem_bbm_notify_cancel_offline() argument
977 if (virtio_mem_bbm_get_bb_state(vm, bb_id) != in virtio_mem_bbm_notify_cancel_offline()
991 struct virtio_mem *vm = container_of(nb, struct virtio_mem, in virtio_mem_memory_notifier_cb() local
999 if (!virtio_mem_overlaps_range(vm, start, size)) in virtio_mem_memory_notifier_cb()
1002 if (vm->in_sbm) { in virtio_mem_memory_notifier_cb()
1013 id = virtio_mem_phys_to_bb_id(vm, start); in virtio_mem_memory_notifier_cb()
1020 if (WARN_ON_ONCE(id != virtio_mem_phys_to_bb_id(vm, start + size - 1))) in virtio_mem_memory_notifier_cb()
1034 mutex_lock(&vm->hotplug_mutex); in virtio_mem_memory_notifier_cb()
1035 if (vm->removing) { in virtio_mem_memory_notifier_cb()
1037 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_memory_notifier_cb()
1040 vm->hotplug_active = true; in virtio_mem_memory_notifier_cb()
1041 if (vm->in_sbm) in virtio_mem_memory_notifier_cb()
1042 virtio_mem_sbm_notify_going_offline(vm, id); in virtio_mem_memory_notifier_cb()
1044 virtio_mem_bbm_notify_going_offline(vm, id, in virtio_mem_memory_notifier_cb()
1049 mutex_lock(&vm->hotplug_mutex); in virtio_mem_memory_notifier_cb()
1050 if (vm->removing) { in virtio_mem_memory_notifier_cb()
1052 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_memory_notifier_cb()
1055 vm->hotplug_active = true; in virtio_mem_memory_notifier_cb()
1056 if (vm->in_sbm) in virtio_mem_memory_notifier_cb()
1057 rc = virtio_mem_sbm_notify_going_online(vm, id); in virtio_mem_memory_notifier_cb()
1060 if (vm->in_sbm) in virtio_mem_memory_notifier_cb()
1061 virtio_mem_sbm_notify_offline(vm, id); in virtio_mem_memory_notifier_cb()
1063 atomic64_add(size, &vm->offline_size); in virtio_mem_memory_notifier_cb()
1069 virtio_mem_retry(vm); in virtio_mem_memory_notifier_cb()
1071 vm->hotplug_active = false; in virtio_mem_memory_notifier_cb()
1072 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_memory_notifier_cb()
1075 if (vm->in_sbm) in virtio_mem_memory_notifier_cb()
1076 virtio_mem_sbm_notify_online(vm, id, mhp->start_pfn); in virtio_mem_memory_notifier_cb()
1078 atomic64_sub(size, &vm->offline_size); in virtio_mem_memory_notifier_cb()
1085 if (!atomic_read(&vm->wq_active) && in virtio_mem_memory_notifier_cb()
1086 virtio_mem_could_add_memory(vm, vm->offline_threshold / 2)) in virtio_mem_memory_notifier_cb()
1087 virtio_mem_retry(vm); in virtio_mem_memory_notifier_cb()
1089 vm->hotplug_active = false; in virtio_mem_memory_notifier_cb()
1090 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_memory_notifier_cb()
1093 if (!vm->hotplug_active) in virtio_mem_memory_notifier_cb()
1095 if (vm->in_sbm) in virtio_mem_memory_notifier_cb()
1096 virtio_mem_sbm_notify_cancel_offline(vm, id); in virtio_mem_memory_notifier_cb()
1098 virtio_mem_bbm_notify_cancel_offline(vm, id, in virtio_mem_memory_notifier_cb()
1101 vm->hotplug_active = false; in virtio_mem_memory_notifier_cb()
1102 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_memory_notifier_cb()
1105 if (!vm->hotplug_active) in virtio_mem_memory_notifier_cb()
1107 vm->hotplug_active = false; in virtio_mem_memory_notifier_cb()
1108 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_memory_notifier_cb()
1122 struct virtio_mem *vm = container_of(nb, struct virtio_mem, in virtio_mem_pm_notifier_cb() local
1131 dev_err(&vm->vdev->dev, "hibernation is not supported.\n"); in virtio_mem_pm_notifier_cb()
1222 static int virtio_mem_fake_offline(struct virtio_mem *vm, unsigned long pfn, in virtio_mem_fake_offline() argument
1241 if (atomic_read(&vm->config_changed)) in virtio_mem_fake_offline()
1297 static void virtio_mem_online_page(struct virtio_mem *vm, in virtio_mem_online_page() argument
1315 if (vm->in_sbm) { in virtio_mem_online_page()
1317 sb_id = virtio_mem_phys_to_sb_id(vm, addr); in virtio_mem_online_page()
1318 count = virtio_mem_phys_to_sb_id(vm, next - 1) - sb_id + 1; in virtio_mem_online_page()
1320 if (virtio_mem_sbm_test_sb_plugged(vm, id, sb_id, count)) { in virtio_mem_online_page()
1324 virtio_mem_sbm_test_sb_unplugged(vm, id, sb_id, count)) { in virtio_mem_online_page()
1333 order = ilog2(vm->sbm.sb_size) - PAGE_SHIFT; in virtio_mem_online_page()
1334 do_online = virtio_mem_sbm_test_sb_plugged(vm, id, sb_id, 1); in virtio_mem_online_page()
1342 id = virtio_mem_phys_to_bb_id(vm, addr); in virtio_mem_online_page()
1343 do_online = virtio_mem_bbm_get_bb_state(vm, id) != in virtio_mem_online_page()
1359 struct virtio_mem *vm; in virtio_mem_online_page_cb() local
1362 list_for_each_entry_rcu(vm, &virtio_mem_devices, next) { in virtio_mem_online_page_cb()
1367 if (!virtio_mem_contains_range(vm, addr, PFN_PHYS(1 << order))) in virtio_mem_online_page_cb()
1378 virtio_mem_online_page(vm, page, order); in virtio_mem_online_page_cb()
1387 static uint64_t virtio_mem_send_request(struct virtio_mem *vm, in virtio_mem_send_request() argument
1395 vm->req = *req; in virtio_mem_send_request()
1398 sg_init_one(&sg_req, &vm->req, sizeof(vm->req)); in virtio_mem_send_request()
1402 sg_init_one(&sg_resp, &vm->resp, sizeof(vm->resp)); in virtio_mem_send_request()
1405 rc = virtqueue_add_sgs(vm->vq, sgs, 1, 1, vm, GFP_KERNEL); in virtio_mem_send_request()
1409 virtqueue_kick(vm->vq); in virtio_mem_send_request()
1412 wait_event(vm->host_resp, virtqueue_get_buf(vm->vq, &len)); in virtio_mem_send_request()
1414 return virtio16_to_cpu(vm->vdev, vm->resp.type); in virtio_mem_send_request()
1417 static int virtio_mem_send_plug_request(struct virtio_mem *vm, uint64_t addr, in virtio_mem_send_plug_request() argument
1420 const uint64_t nb_vm_blocks = size / vm->device_block_size; in virtio_mem_send_plug_request()
1422 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_PLUG), in virtio_mem_send_plug_request()
1423 .u.plug.addr = cpu_to_virtio64(vm->vdev, addr), in virtio_mem_send_plug_request()
1424 .u.plug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks), in virtio_mem_send_plug_request()
1428 if (atomic_read(&vm->config_changed)) in virtio_mem_send_plug_request()
1431 dev_dbg(&vm->vdev->dev, "plugging memory: 0x%llx - 0x%llx\n", addr, in virtio_mem_send_plug_request()
1434 switch (virtio_mem_send_request(vm, &req)) { in virtio_mem_send_plug_request()
1436 vm->plugged_size += size; in virtio_mem_send_plug_request()
1451 dev_dbg(&vm->vdev->dev, "plugging memory failed: %d\n", rc); in virtio_mem_send_plug_request()
1455 static int virtio_mem_send_unplug_request(struct virtio_mem *vm, uint64_t addr, in virtio_mem_send_unplug_request() argument
1458 const uint64_t nb_vm_blocks = size / vm->device_block_size; in virtio_mem_send_unplug_request()
1460 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG), in virtio_mem_send_unplug_request()
1461 .u.unplug.addr = cpu_to_virtio64(vm->vdev, addr), in virtio_mem_send_unplug_request()
1462 .u.unplug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks), in virtio_mem_send_unplug_request()
1466 if (atomic_read(&vm->config_changed)) in virtio_mem_send_unplug_request()
1469 dev_dbg(&vm->vdev->dev, "unplugging memory: 0x%llx - 0x%llx\n", addr, in virtio_mem_send_unplug_request()
1472 switch (virtio_mem_send_request(vm, &req)) { in virtio_mem_send_unplug_request()
1474 vm->plugged_size -= size; in virtio_mem_send_unplug_request()
1486 dev_dbg(&vm->vdev->dev, "unplugging memory failed: %d\n", rc); in virtio_mem_send_unplug_request()
1490 static int virtio_mem_send_unplug_all_request(struct virtio_mem *vm) in virtio_mem_send_unplug_all_request() argument
1493 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG_ALL), in virtio_mem_send_unplug_all_request()
1497 dev_dbg(&vm->vdev->dev, "unplugging all memory"); in virtio_mem_send_unplug_all_request()
1499 switch (virtio_mem_send_request(vm, &req)) { in virtio_mem_send_unplug_all_request()
1501 vm->unplug_all_required = false; in virtio_mem_send_unplug_all_request()
1502 vm->plugged_size = 0; in virtio_mem_send_unplug_all_request()
1504 atomic_set(&vm->config_changed, 1); in virtio_mem_send_unplug_all_request()
1513 dev_dbg(&vm->vdev->dev, "unplugging all memory failed: %d\n", rc); in virtio_mem_send_unplug_all_request()
1521 static int virtio_mem_sbm_plug_sb(struct virtio_mem *vm, unsigned long mb_id, in virtio_mem_sbm_plug_sb() argument
1525 sb_id * vm->sbm.sb_size; in virtio_mem_sbm_plug_sb()
1526 const uint64_t size = count * vm->sbm.sb_size; in virtio_mem_sbm_plug_sb()
1529 rc = virtio_mem_send_plug_request(vm, addr, size); in virtio_mem_sbm_plug_sb()
1531 virtio_mem_sbm_set_sb_plugged(vm, mb_id, sb_id, count); in virtio_mem_sbm_plug_sb()
1539 static int virtio_mem_sbm_unplug_sb(struct virtio_mem *vm, unsigned long mb_id, in virtio_mem_sbm_unplug_sb() argument
1543 sb_id * vm->sbm.sb_size; in virtio_mem_sbm_unplug_sb()
1544 const uint64_t size = count * vm->sbm.sb_size; in virtio_mem_sbm_unplug_sb()
1547 rc = virtio_mem_send_unplug_request(vm, addr, size); in virtio_mem_sbm_unplug_sb()
1549 virtio_mem_sbm_set_sb_unplugged(vm, mb_id, sb_id, count); in virtio_mem_sbm_unplug_sb()
1558 static int virtio_mem_bbm_unplug_bb(struct virtio_mem *vm, unsigned long bb_id) in virtio_mem_bbm_unplug_bb() argument
1560 const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id); in virtio_mem_bbm_unplug_bb()
1561 const uint64_t size = vm->bbm.bb_size; in virtio_mem_bbm_unplug_bb()
1563 return virtio_mem_send_unplug_request(vm, addr, size); in virtio_mem_bbm_unplug_bb()
1571 static int virtio_mem_bbm_plug_bb(struct virtio_mem *vm, unsigned long bb_id) in virtio_mem_bbm_plug_bb() argument
1573 const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id); in virtio_mem_bbm_plug_bb()
1574 const uint64_t size = vm->bbm.bb_size; in virtio_mem_bbm_plug_bb()
1576 return virtio_mem_send_plug_request(vm, addr, size); in virtio_mem_bbm_plug_bb()
1588 static int virtio_mem_sbm_unplug_any_sb_raw(struct virtio_mem *vm, in virtio_mem_sbm_unplug_any_sb_raw() argument
1594 sb_id = vm->sbm.sbs_per_mb - 1; in virtio_mem_sbm_unplug_any_sb_raw()
1598 virtio_mem_sbm_test_sb_unplugged(vm, mb_id, sb_id, 1)) in virtio_mem_sbm_unplug_any_sb_raw()
1605 virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id - 1, 1)) { in virtio_mem_sbm_unplug_any_sb_raw()
1610 rc = virtio_mem_sbm_unplug_sb(vm, mb_id, sb_id, count); in virtio_mem_sbm_unplug_any_sb_raw()
1627 static int virtio_mem_sbm_unplug_mb(struct virtio_mem *vm, unsigned long mb_id) in virtio_mem_sbm_unplug_mb() argument
1629 uint64_t nb_sb = vm->sbm.sbs_per_mb; in virtio_mem_sbm_unplug_mb()
1631 return virtio_mem_sbm_unplug_any_sb_raw(vm, mb_id, &nb_sb); in virtio_mem_sbm_unplug_mb()
1637 static int virtio_mem_sbm_prepare_next_mb(struct virtio_mem *vm, in virtio_mem_sbm_prepare_next_mb() argument
1642 if (vm->sbm.next_mb_id > vm->sbm.last_usable_mb_id) in virtio_mem_sbm_prepare_next_mb()
1646 rc = virtio_mem_sbm_mb_states_prepare_next_mb(vm); in virtio_mem_sbm_prepare_next_mb()
1651 rc = virtio_mem_sbm_sb_states_prepare_next_mb(vm); in virtio_mem_sbm_prepare_next_mb()
1655 vm->sbm.mb_count[VIRTIO_MEM_SBM_MB_UNUSED]++; in virtio_mem_sbm_prepare_next_mb()
1656 *mb_id = vm->sbm.next_mb_id++; in virtio_mem_sbm_prepare_next_mb()
1666 static int virtio_mem_sbm_plug_and_add_mb(struct virtio_mem *vm, in virtio_mem_sbm_plug_and_add_mb() argument
1669 const int count = min_t(int, *nb_sb, vm->sbm.sbs_per_mb); in virtio_mem_sbm_plug_and_add_mb()
1679 rc = virtio_mem_sbm_plug_sb(vm, mb_id, 0, count); in virtio_mem_sbm_plug_and_add_mb()
1687 if (count == vm->sbm.sbs_per_mb) in virtio_mem_sbm_plug_and_add_mb()
1688 virtio_mem_sbm_set_mb_state(vm, mb_id, in virtio_mem_sbm_plug_and_add_mb()
1691 virtio_mem_sbm_set_mb_state(vm, mb_id, in virtio_mem_sbm_plug_and_add_mb()
1695 rc = virtio_mem_sbm_add_mb(vm, mb_id); in virtio_mem_sbm_plug_and_add_mb()
1699 if (virtio_mem_sbm_unplug_sb(vm, mb_id, 0, count)) in virtio_mem_sbm_plug_and_add_mb()
1701 virtio_mem_sbm_set_mb_state(vm, mb_id, new_state); in virtio_mem_sbm_plug_and_add_mb()
1717 static int virtio_mem_sbm_plug_any_sb(struct virtio_mem *vm, in virtio_mem_sbm_plug_any_sb() argument
1720 const int old_state = virtio_mem_sbm_get_mb_state(vm, mb_id); in virtio_mem_sbm_plug_any_sb()
1729 sb_id = virtio_mem_sbm_first_unplugged_sb(vm, mb_id); in virtio_mem_sbm_plug_any_sb()
1730 if (sb_id >= vm->sbm.sbs_per_mb) in virtio_mem_sbm_plug_any_sb()
1734 sb_id + count < vm->sbm.sbs_per_mb && in virtio_mem_sbm_plug_any_sb()
1735 !virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id + count, 1)) in virtio_mem_sbm_plug_any_sb()
1738 rc = virtio_mem_sbm_plug_sb(vm, mb_id, sb_id, count); in virtio_mem_sbm_plug_any_sb()
1747 sb_id * vm->sbm.sb_size); in virtio_mem_sbm_plug_any_sb()
1748 nr_pages = PFN_DOWN(count * vm->sbm.sb_size); in virtio_mem_sbm_plug_any_sb()
1752 if (virtio_mem_sbm_test_sb_plugged(vm, mb_id, 0, vm->sbm.sbs_per_mb)) in virtio_mem_sbm_plug_any_sb()
1753 virtio_mem_sbm_set_mb_state(vm, mb_id, old_state - 1); in virtio_mem_sbm_plug_any_sb()
1758 static int virtio_mem_sbm_plug_request(struct virtio_mem *vm, uint64_t diff) in virtio_mem_sbm_plug_request() argument
1765 uint64_t nb_sb = diff / vm->sbm.sb_size; in virtio_mem_sbm_plug_request()
1773 mutex_lock(&vm->hotplug_mutex); in virtio_mem_sbm_plug_request()
1776 virtio_mem_sbm_for_each_mb(vm, mb_id, mb_states[i]) { in virtio_mem_sbm_plug_request()
1777 rc = virtio_mem_sbm_plug_any_sb(vm, mb_id, &nb_sb); in virtio_mem_sbm_plug_request()
1788 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_sbm_plug_request()
1791 virtio_mem_sbm_for_each_mb(vm, mb_id, VIRTIO_MEM_SBM_MB_UNUSED) { in virtio_mem_sbm_plug_request()
1792 if (!virtio_mem_could_add_memory(vm, memory_block_size_bytes())) in virtio_mem_sbm_plug_request()
1795 rc = virtio_mem_sbm_plug_and_add_mb(vm, mb_id, &nb_sb); in virtio_mem_sbm_plug_request()
1803 if (!virtio_mem_could_add_memory(vm, memory_block_size_bytes())) in virtio_mem_sbm_plug_request()
1806 rc = virtio_mem_sbm_prepare_next_mb(vm, &mb_id); in virtio_mem_sbm_plug_request()
1809 rc = virtio_mem_sbm_plug_and_add_mb(vm, mb_id, &nb_sb); in virtio_mem_sbm_plug_request()
1817 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_sbm_plug_request()
1826 static int virtio_mem_bbm_plug_and_add_bb(struct virtio_mem *vm, in virtio_mem_bbm_plug_and_add_bb() argument
1831 if (WARN_ON_ONCE(virtio_mem_bbm_get_bb_state(vm, bb_id) != in virtio_mem_bbm_plug_and_add_bb()
1835 rc = virtio_mem_bbm_plug_bb(vm, bb_id); in virtio_mem_bbm_plug_and_add_bb()
1838 virtio_mem_bbm_set_bb_state(vm, bb_id, VIRTIO_MEM_BBM_BB_ADDED); in virtio_mem_bbm_plug_and_add_bb()
1840 rc = virtio_mem_bbm_add_bb(vm, bb_id); in virtio_mem_bbm_plug_and_add_bb()
1842 if (!virtio_mem_bbm_unplug_bb(vm, bb_id)) in virtio_mem_bbm_plug_and_add_bb()
1843 virtio_mem_bbm_set_bb_state(vm, bb_id, in virtio_mem_bbm_plug_and_add_bb()
1847 virtio_mem_bbm_set_bb_state(vm, bb_id, in virtio_mem_bbm_plug_and_add_bb()
1857 static int virtio_mem_bbm_prepare_next_bb(struct virtio_mem *vm, in virtio_mem_bbm_prepare_next_bb() argument
1862 if (vm->bbm.next_bb_id > vm->bbm.last_usable_bb_id) in virtio_mem_bbm_prepare_next_bb()
1866 rc = virtio_mem_bbm_bb_states_prepare_next_bb(vm); in virtio_mem_bbm_prepare_next_bb()
1870 vm->bbm.bb_count[VIRTIO_MEM_BBM_BB_UNUSED]++; in virtio_mem_bbm_prepare_next_bb()
1871 *bb_id = vm->bbm.next_bb_id; in virtio_mem_bbm_prepare_next_bb()
1872 vm->bbm.next_bb_id++; in virtio_mem_bbm_prepare_next_bb()
1876 static int virtio_mem_bbm_plug_request(struct virtio_mem *vm, uint64_t diff) in virtio_mem_bbm_plug_request() argument
1878 uint64_t nb_bb = diff / vm->bbm.bb_size; in virtio_mem_bbm_plug_request()
1886 virtio_mem_bbm_for_each_bb(vm, bb_id, VIRTIO_MEM_BBM_BB_UNUSED) { in virtio_mem_bbm_plug_request()
1887 if (!virtio_mem_could_add_memory(vm, vm->bbm.bb_size)) in virtio_mem_bbm_plug_request()
1890 rc = virtio_mem_bbm_plug_and_add_bb(vm, bb_id); in virtio_mem_bbm_plug_request()
1900 if (!virtio_mem_could_add_memory(vm, vm->bbm.bb_size)) in virtio_mem_bbm_plug_request()
1903 rc = virtio_mem_bbm_prepare_next_bb(vm, &bb_id); in virtio_mem_bbm_plug_request()
1906 rc = virtio_mem_bbm_plug_and_add_bb(vm, bb_id); in virtio_mem_bbm_plug_request()
1920 static int virtio_mem_plug_request(struct virtio_mem *vm, uint64_t diff) in virtio_mem_plug_request() argument
1922 if (vm->in_sbm) in virtio_mem_plug_request()
1923 return virtio_mem_sbm_plug_request(vm, diff); in virtio_mem_plug_request()
1924 return virtio_mem_bbm_plug_request(vm, diff); in virtio_mem_plug_request()
1936 static int virtio_mem_sbm_unplug_any_sb_offline(struct virtio_mem *vm, in virtio_mem_sbm_unplug_any_sb_offline() argument
1942 rc = virtio_mem_sbm_unplug_any_sb_raw(vm, mb_id, nb_sb); in virtio_mem_sbm_unplug_any_sb_offline()
1945 if (!virtio_mem_sbm_test_sb_plugged(vm, mb_id, 0, vm->sbm.sbs_per_mb)) in virtio_mem_sbm_unplug_any_sb_offline()
1946 virtio_mem_sbm_set_mb_state(vm, mb_id, in virtio_mem_sbm_unplug_any_sb_offline()
1951 if (virtio_mem_sbm_test_sb_unplugged(vm, mb_id, 0, vm->sbm.sbs_per_mb)) { in virtio_mem_sbm_unplug_any_sb_offline()
1958 virtio_mem_sbm_set_mb_state(vm, mb_id, in virtio_mem_sbm_unplug_any_sb_offline()
1961 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_sbm_unplug_any_sb_offline()
1962 rc = virtio_mem_sbm_remove_mb(vm, mb_id); in virtio_mem_sbm_unplug_any_sb_offline()
1964 mutex_lock(&vm->hotplug_mutex); in virtio_mem_sbm_unplug_any_sb_offline()
1974 static int virtio_mem_sbm_unplug_sb_online(struct virtio_mem *vm, in virtio_mem_sbm_unplug_sb_online() argument
1978 const unsigned long nr_pages = PFN_DOWN(vm->sbm.sb_size) * count; in virtio_mem_sbm_unplug_sb_online()
1979 const int old_state = virtio_mem_sbm_get_mb_state(vm, mb_id); in virtio_mem_sbm_unplug_sb_online()
1984 sb_id * vm->sbm.sb_size); in virtio_mem_sbm_unplug_sb_online()
1986 rc = virtio_mem_fake_offline(vm, start_pfn, nr_pages); in virtio_mem_sbm_unplug_sb_online()
1991 rc = virtio_mem_sbm_unplug_sb(vm, mb_id, sb_id, count); in virtio_mem_sbm_unplug_sb_online()
2000 virtio_mem_sbm_set_mb_state(vm, mb_id, in virtio_mem_sbm_unplug_sb_online()
2004 virtio_mem_sbm_set_mb_state(vm, mb_id, in virtio_mem_sbm_unplug_sb_online()
2022 static int virtio_mem_sbm_unplug_any_sb_online(struct virtio_mem *vm, in virtio_mem_sbm_unplug_any_sb_online() argument
2029 if (*nb_sb >= vm->sbm.sbs_per_mb && in virtio_mem_sbm_unplug_any_sb_online()
2030 virtio_mem_sbm_test_sb_plugged(vm, mb_id, 0, vm->sbm.sbs_per_mb)) { in virtio_mem_sbm_unplug_any_sb_online()
2031 rc = virtio_mem_sbm_unplug_sb_online(vm, mb_id, 0, in virtio_mem_sbm_unplug_any_sb_online()
2032 vm->sbm.sbs_per_mb); in virtio_mem_sbm_unplug_any_sb_online()
2034 *nb_sb -= vm->sbm.sbs_per_mb; in virtio_mem_sbm_unplug_any_sb_online()
2041 for (sb_id = vm->sbm.sbs_per_mb - 1; sb_id >= 0 && *nb_sb; sb_id--) { in virtio_mem_sbm_unplug_any_sb_online()
2044 !virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id, 1)) in virtio_mem_sbm_unplug_any_sb_online()
2049 rc = virtio_mem_sbm_unplug_sb_online(vm, mb_id, sb_id, 1); in virtio_mem_sbm_unplug_any_sb_online()
2058 rc = virtio_mem_sbm_try_remove_unplugged_mb(vm, mb_id); in virtio_mem_sbm_unplug_any_sb_online()
2060 vm->sbm.have_unplugged_mb = 1; in virtio_mem_sbm_unplug_any_sb_online()
2077 static int virtio_mem_sbm_unplug_any_sb(struct virtio_mem *vm, in virtio_mem_sbm_unplug_any_sb() argument
2081 const int old_state = virtio_mem_sbm_get_mb_state(vm, mb_id); in virtio_mem_sbm_unplug_any_sb()
2088 return virtio_mem_sbm_unplug_any_sb_online(vm, mb_id, nb_sb); in virtio_mem_sbm_unplug_any_sb()
2091 return virtio_mem_sbm_unplug_any_sb_offline(vm, mb_id, nb_sb); in virtio_mem_sbm_unplug_any_sb()
2096 static int virtio_mem_sbm_unplug_request(struct virtio_mem *vm, uint64_t diff) in virtio_mem_sbm_unplug_request() argument
2106 uint64_t nb_sb = diff / vm->sbm.sb_size; in virtio_mem_sbm_unplug_request()
2118 mutex_lock(&vm->hotplug_mutex); in virtio_mem_sbm_unplug_request()
2128 virtio_mem_sbm_for_each_mb_rev(vm, mb_id, mb_states[i]) { in virtio_mem_sbm_unplug_request()
2129 rc = virtio_mem_sbm_unplug_any_sb(vm, mb_id, &nb_sb); in virtio_mem_sbm_unplug_request()
2132 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_sbm_unplug_request()
2134 mutex_lock(&vm->hotplug_mutex); in virtio_mem_sbm_unplug_request()
2137 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_sbm_unplug_request()
2142 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_sbm_unplug_request()
2145 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_sbm_unplug_request()
2156 static int virtio_mem_bbm_offline_remove_and_unplug_bb(struct virtio_mem *vm, in virtio_mem_bbm_offline_remove_and_unplug_bb() argument
2159 const unsigned long start_pfn = PFN_DOWN(virtio_mem_bb_id_to_phys(vm, bb_id)); in virtio_mem_bbm_offline_remove_and_unplug_bb()
2160 const unsigned long nr_pages = PFN_DOWN(vm->bbm.bb_size); in virtio_mem_bbm_offline_remove_and_unplug_bb()
2166 if (WARN_ON_ONCE(virtio_mem_bbm_get_bb_state(vm, bb_id) != in virtio_mem_bbm_offline_remove_and_unplug_bb()
2176 mutex_lock(&vm->hotplug_mutex); in virtio_mem_bbm_offline_remove_and_unplug_bb()
2177 virtio_mem_bbm_set_bb_state(vm, bb_id, VIRTIO_MEM_BBM_BB_FAKE_OFFLINE); in virtio_mem_bbm_offline_remove_and_unplug_bb()
2184 rc = virtio_mem_fake_offline(vm, pfn, PAGES_PER_SECTION); in virtio_mem_bbm_offline_remove_and_unplug_bb()
2190 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_bbm_offline_remove_and_unplug_bb()
2192 rc = virtio_mem_bbm_offline_and_remove_bb(vm, bb_id); in virtio_mem_bbm_offline_remove_and_unplug_bb()
2194 mutex_lock(&vm->hotplug_mutex); in virtio_mem_bbm_offline_remove_and_unplug_bb()
2198 rc = virtio_mem_bbm_unplug_bb(vm, bb_id); in virtio_mem_bbm_offline_remove_and_unplug_bb()
2200 virtio_mem_bbm_set_bb_state(vm, bb_id, in virtio_mem_bbm_offline_remove_and_unplug_bb()
2203 virtio_mem_bbm_set_bb_state(vm, bb_id, in virtio_mem_bbm_offline_remove_and_unplug_bb()
2214 virtio_mem_bbm_set_bb_state(vm, bb_id, VIRTIO_MEM_BBM_BB_ADDED); in virtio_mem_bbm_offline_remove_and_unplug_bb()
2215 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_bbm_offline_remove_and_unplug_bb()
2222 static bool virtio_mem_bbm_bb_is_offline(struct virtio_mem *vm, in virtio_mem_bbm_bb_is_offline() argument
2225 const unsigned long start_pfn = PFN_DOWN(virtio_mem_bb_id_to_phys(vm, bb_id)); in virtio_mem_bbm_bb_is_offline()
2226 const unsigned long nr_pages = PFN_DOWN(vm->bbm.bb_size); in virtio_mem_bbm_bb_is_offline()
2241 static bool virtio_mem_bbm_bb_is_movable(struct virtio_mem *vm, in virtio_mem_bbm_bb_is_movable() argument
2244 const unsigned long start_pfn = PFN_DOWN(virtio_mem_bb_id_to_phys(vm, bb_id)); in virtio_mem_bbm_bb_is_movable()
2245 const unsigned long nr_pages = PFN_DOWN(vm->bbm.bb_size); in virtio_mem_bbm_bb_is_movable()
2261 static int virtio_mem_bbm_unplug_request(struct virtio_mem *vm, uint64_t diff) in virtio_mem_bbm_unplug_request() argument
2263 uint64_t nb_bb = diff / vm->bbm.bb_size; in virtio_mem_bbm_unplug_request()
2275 virtio_mem_bbm_for_each_bb_rev(vm, bb_id, VIRTIO_MEM_BBM_BB_ADDED) { in virtio_mem_bbm_unplug_request()
2282 if (i == 0 && !virtio_mem_bbm_bb_is_offline(vm, bb_id)) in virtio_mem_bbm_unplug_request()
2284 if (i == 1 && !virtio_mem_bbm_bb_is_movable(vm, bb_id)) in virtio_mem_bbm_unplug_request()
2286 rc = virtio_mem_bbm_offline_remove_and_unplug_bb(vm, bb_id); in virtio_mem_bbm_unplug_request()
2304 static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t diff) in virtio_mem_unplug_request() argument
2306 if (vm->in_sbm) in virtio_mem_unplug_request()
2307 return virtio_mem_sbm_unplug_request(vm, diff); in virtio_mem_unplug_request()
2308 return virtio_mem_bbm_unplug_request(vm, diff); in virtio_mem_unplug_request()
2316 static int virtio_mem_cleanup_pending_mb(struct virtio_mem *vm) in virtio_mem_cleanup_pending_mb() argument
2321 if (!vm->in_sbm) { in virtio_mem_cleanup_pending_mb()
2322 virtio_mem_bbm_for_each_bb(vm, id, in virtio_mem_cleanup_pending_mb()
2324 rc = virtio_mem_bbm_unplug_bb(vm, id); in virtio_mem_cleanup_pending_mb()
2327 virtio_mem_bbm_set_bb_state(vm, id, in virtio_mem_cleanup_pending_mb()
2333 virtio_mem_sbm_for_each_mb(vm, id, VIRTIO_MEM_SBM_MB_PLUGGED) { in virtio_mem_cleanup_pending_mb()
2334 rc = virtio_mem_sbm_unplug_mb(vm, id); in virtio_mem_cleanup_pending_mb()
2337 virtio_mem_sbm_set_mb_state(vm, id, in virtio_mem_cleanup_pending_mb()
2341 if (!vm->sbm.have_unplugged_mb) in virtio_mem_cleanup_pending_mb()
2348 vm->sbm.have_unplugged_mb = false; in virtio_mem_cleanup_pending_mb()
2350 mutex_lock(&vm->hotplug_mutex); in virtio_mem_cleanup_pending_mb()
2351 virtio_mem_sbm_for_each_mb(vm, id, VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL) in virtio_mem_cleanup_pending_mb()
2352 rc |= virtio_mem_sbm_try_remove_unplugged_mb(vm, id); in virtio_mem_cleanup_pending_mb()
2353 virtio_mem_sbm_for_each_mb(vm, id, VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL) in virtio_mem_cleanup_pending_mb()
2354 rc |= virtio_mem_sbm_try_remove_unplugged_mb(vm, id); in virtio_mem_cleanup_pending_mb()
2355 virtio_mem_sbm_for_each_mb(vm, id, VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL) in virtio_mem_cleanup_pending_mb()
2356 rc |= virtio_mem_sbm_try_remove_unplugged_mb(vm, id); in virtio_mem_cleanup_pending_mb()
2357 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_cleanup_pending_mb()
2360 vm->sbm.have_unplugged_mb = true; in virtio_mem_cleanup_pending_mb()
2368 static void virtio_mem_refresh_config(struct virtio_mem *vm) in virtio_mem_refresh_config() argument
2374 virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size, in virtio_mem_refresh_config()
2376 if (WARN_ON_ONCE(new_plugged_size != vm->plugged_size)) in virtio_mem_refresh_config()
2377 vm->plugged_size = new_plugged_size; in virtio_mem_refresh_config()
2380 virtio_cread_le(vm->vdev, struct virtio_mem_config, in virtio_mem_refresh_config()
2382 end_addr = min(vm->addr + usable_region_size - 1, in virtio_mem_refresh_config()
2385 if (vm->in_sbm) { in virtio_mem_refresh_config()
2386 vm->sbm.last_usable_mb_id = virtio_mem_phys_to_mb_id(end_addr); in virtio_mem_refresh_config()
2388 vm->sbm.last_usable_mb_id--; in virtio_mem_refresh_config()
2390 vm->bbm.last_usable_bb_id = virtio_mem_phys_to_bb_id(vm, in virtio_mem_refresh_config()
2392 if (!IS_ALIGNED(end_addr + 1, vm->bbm.bb_size)) in virtio_mem_refresh_config()
2393 vm->bbm.last_usable_bb_id--; in virtio_mem_refresh_config()
2403 virtio_cread_le(vm->vdev, struct virtio_mem_config, requested_size, in virtio_mem_refresh_config()
2404 &vm->requested_size); in virtio_mem_refresh_config()
2406 dev_info(&vm->vdev->dev, "plugged size: 0x%llx", vm->plugged_size); in virtio_mem_refresh_config()
2407 dev_info(&vm->vdev->dev, "requested size: 0x%llx", vm->requested_size); in virtio_mem_refresh_config()
2415 struct virtio_mem *vm = container_of(work, struct virtio_mem, wq); in virtio_mem_run_wq() local
2419 if (unlikely(vm->in_kdump)) { in virtio_mem_run_wq()
2420 dev_warn_once(&vm->vdev->dev, in virtio_mem_run_wq()
2425 hrtimer_cancel(&vm->retry_timer); in virtio_mem_run_wq()
2427 if (vm->broken) in virtio_mem_run_wq()
2430 atomic_set(&vm->wq_active, 1); in virtio_mem_run_wq()
2435 if (unlikely(vm->unplug_all_required)) in virtio_mem_run_wq()
2436 rc = virtio_mem_send_unplug_all_request(vm); in virtio_mem_run_wq()
2438 if (atomic_read(&vm->config_changed)) { in virtio_mem_run_wq()
2439 atomic_set(&vm->config_changed, 0); in virtio_mem_run_wq()
2440 virtio_mem_refresh_config(vm); in virtio_mem_run_wq()
2445 rc = virtio_mem_cleanup_pending_mb(vm); in virtio_mem_run_wq()
2447 if (!rc && vm->requested_size != vm->plugged_size) { in virtio_mem_run_wq()
2448 if (vm->requested_size > vm->plugged_size) { in virtio_mem_run_wq()
2449 diff = vm->requested_size - vm->plugged_size; in virtio_mem_run_wq()
2450 rc = virtio_mem_plug_request(vm, diff); in virtio_mem_run_wq()
2452 diff = vm->plugged_size - vm->requested_size; in virtio_mem_run_wq()
2453 rc = virtio_mem_unplug_request(vm, diff); in virtio_mem_run_wq()
2461 if (!rc && vm->in_sbm && vm->sbm.have_unplugged_mb) in virtio_mem_run_wq()
2466 vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS; in virtio_mem_run_wq()
2486 hrtimer_start(&vm->retry_timer, ms_to_ktime(vm->retry_timer_ms), in virtio_mem_run_wq()
2494 dev_err(&vm->vdev->dev, in virtio_mem_run_wq()
2496 vm->broken = true; in virtio_mem_run_wq()
2499 atomic_set(&vm->wq_active, 0); in virtio_mem_run_wq()
2504 struct virtio_mem *vm = container_of(timer, struct virtio_mem, in virtio_mem_timer_expired() local
2507 virtio_mem_retry(vm); in virtio_mem_timer_expired()
2508 vm->retry_timer_ms = min_t(unsigned int, vm->retry_timer_ms * 2, in virtio_mem_timer_expired()
2515 struct virtio_mem *vm = vq->vdev->priv; in virtio_mem_handle_response() local
2517 wake_up(&vm->host_resp); in virtio_mem_handle_response()
2520 static int virtio_mem_init_vq(struct virtio_mem *vm) in virtio_mem_init_vq() argument
2524 vq = virtio_find_single_vq(vm->vdev, virtio_mem_handle_response, in virtio_mem_init_vq()
2528 vm->vq = vq; in virtio_mem_init_vq()
2533 static int virtio_mem_init_hotplug(struct virtio_mem *vm) in virtio_mem_init_hotplug() argument
2540 if (!IS_ALIGNED(vm->addr, memory_block_size_bytes())) in virtio_mem_init_hotplug()
2541 dev_warn(&vm->vdev->dev, in virtio_mem_init_hotplug()
2543 if (!IS_ALIGNED(vm->addr + vm->region_size, memory_block_size_bytes())) in virtio_mem_init_hotplug()
2544 dev_warn(&vm->vdev->dev, in virtio_mem_init_hotplug()
2546 if (vm->addr < pluggable_range.start || in virtio_mem_init_hotplug()
2547 vm->addr + vm->region_size - 1 > pluggable_range.end) in virtio_mem_init_hotplug()
2548 dev_warn(&vm->vdev->dev, in virtio_mem_init_hotplug()
2552 vm->offline_threshold = max_t(uint64_t, 2 * memory_block_size_bytes(), in virtio_mem_init_hotplug()
2560 sb_size = max_t(uint64_t, vm->device_block_size, sb_size); in virtio_mem_init_hotplug()
2564 vm->in_sbm = true; in virtio_mem_init_hotplug()
2565 vm->sbm.sb_size = sb_size; in virtio_mem_init_hotplug()
2566 vm->sbm.sbs_per_mb = memory_block_size_bytes() / in virtio_mem_init_hotplug()
2567 vm->sbm.sb_size; in virtio_mem_init_hotplug()
2570 addr = max_t(uint64_t, vm->addr, pluggable_range.start) + in virtio_mem_init_hotplug()
2572 vm->sbm.first_mb_id = virtio_mem_phys_to_mb_id(addr); in virtio_mem_init_hotplug()
2573 vm->sbm.next_mb_id = vm->sbm.first_mb_id; in virtio_mem_init_hotplug()
2576 vm->bbm.bb_size = max_t(uint64_t, vm->device_block_size, in virtio_mem_init_hotplug()
2581 dev_warn(&vm->vdev->dev, in virtio_mem_init_hotplug()
2583 } else if (bbm_block_size < vm->bbm.bb_size) { in virtio_mem_init_hotplug()
2584 dev_warn(&vm->vdev->dev, in virtio_mem_init_hotplug()
2587 vm->bbm.bb_size = bbm_block_size; in virtio_mem_init_hotplug()
2592 addr = max_t(uint64_t, vm->addr, pluggable_range.start) + in virtio_mem_init_hotplug()
2593 vm->bbm.bb_size - 1; in virtio_mem_init_hotplug()
2594 vm->bbm.first_bb_id = virtio_mem_phys_to_bb_id(vm, addr); in virtio_mem_init_hotplug()
2595 vm->bbm.next_bb_id = vm->bbm.first_bb_id; in virtio_mem_init_hotplug()
2598 vm->offline_threshold = max_t(uint64_t, 2 * vm->bbm.bb_size, in virtio_mem_init_hotplug()
2599 vm->offline_threshold); in virtio_mem_init_hotplug()
2602 dev_info(&vm->vdev->dev, "memory block size: 0x%lx", in virtio_mem_init_hotplug()
2604 if (vm->in_sbm) in virtio_mem_init_hotplug()
2605 dev_info(&vm->vdev->dev, "subblock size: 0x%llx", in virtio_mem_init_hotplug()
2606 (unsigned long long)vm->sbm.sb_size); in virtio_mem_init_hotplug()
2608 dev_info(&vm->vdev->dev, "big block size: 0x%llx", in virtio_mem_init_hotplug()
2609 (unsigned long long)vm->bbm.bb_size); in virtio_mem_init_hotplug()
2612 rc = virtio_mem_create_resource(vm); in virtio_mem_init_hotplug()
2617 if (vm->in_sbm) in virtio_mem_init_hotplug()
2620 unit_pages = PHYS_PFN(vm->bbm.bb_size); in virtio_mem_init_hotplug()
2621 rc = memory_group_register_dynamic(vm->nid, unit_pages); in virtio_mem_init_hotplug()
2624 vm->mgid = rc; in virtio_mem_init_hotplug()
2631 if (vm->plugged_size) { in virtio_mem_init_hotplug()
2632 vm->unplug_all_required = true; in virtio_mem_init_hotplug()
2633 dev_info(&vm->vdev->dev, "unplugging all memory is required\n"); in virtio_mem_init_hotplug()
2637 vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb; in virtio_mem_init_hotplug()
2638 rc = register_memory_notifier(&vm->memory_notifier); in virtio_mem_init_hotplug()
2642 vm->pm_notifier.priority = INT_MAX; in virtio_mem_init_hotplug()
2643 vm->pm_notifier.notifier_call = virtio_mem_pm_notifier_cb; in virtio_mem_init_hotplug()
2644 rc = register_pm_notifier(&vm->pm_notifier); in virtio_mem_init_hotplug()
2647 rc = register_virtio_mem_device(vm); in virtio_mem_init_hotplug()
2653 unregister_pm_notifier(&vm->pm_notifier); in virtio_mem_init_hotplug()
2655 unregister_memory_notifier(&vm->memory_notifier); in virtio_mem_init_hotplug()
2657 memory_group_unregister(vm->mgid); in virtio_mem_init_hotplug()
2659 virtio_mem_delete_resource(vm); in virtio_mem_init_hotplug()
2664 static int virtio_mem_send_state_request(struct virtio_mem *vm, uint64_t addr, in virtio_mem_send_state_request() argument
2667 const uint64_t nb_vm_blocks = size / vm->device_block_size; in virtio_mem_send_state_request()
2669 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_STATE), in virtio_mem_send_state_request()
2670 .u.state.addr = cpu_to_virtio64(vm->vdev, addr), in virtio_mem_send_state_request()
2671 .u.state.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks), in virtio_mem_send_state_request()
2675 dev_dbg(&vm->vdev->dev, "requesting state: 0x%llx - 0x%llx\n", addr, in virtio_mem_send_state_request()
2678 switch (virtio_mem_send_request(vm, &req)) { in virtio_mem_send_state_request()
2680 return virtio16_to_cpu(vm->vdev, vm->resp.u.state.state); in virtio_mem_send_state_request()
2688 dev_dbg(&vm->vdev->dev, "requesting state failed: %d\n", rc); in virtio_mem_send_state_request()
2695 struct virtio_mem *vm = container_of(cb, struct virtio_mem, in virtio_mem_vmcore_pfn_is_ram() local
2701 if (!virtio_mem_contains_range(vm, addr, PAGE_SIZE)) in virtio_mem_vmcore_pfn_is_ram()
2703 if (!vm->plugged_size) in virtio_mem_vmcore_pfn_is_ram()
2710 mutex_lock(&vm->hotplug_mutex); in virtio_mem_vmcore_pfn_is_ram()
2712 addr = ALIGN_DOWN(addr, vm->device_block_size); in virtio_mem_vmcore_pfn_is_ram()
2713 if (addr != vm->last_block_addr) { in virtio_mem_vmcore_pfn_is_ram()
2714 rc = virtio_mem_send_state_request(vm, addr, in virtio_mem_vmcore_pfn_is_ram()
2715 vm->device_block_size); in virtio_mem_vmcore_pfn_is_ram()
2718 vm->last_block_plugged = true; in virtio_mem_vmcore_pfn_is_ram()
2720 vm->last_block_plugged = false; in virtio_mem_vmcore_pfn_is_ram()
2721 vm->last_block_addr = addr; in virtio_mem_vmcore_pfn_is_ram()
2724 is_ram = vm->last_block_plugged; in virtio_mem_vmcore_pfn_is_ram()
2725 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_vmcore_pfn_is_ram()
2730 static int virtio_mem_init_kdump(struct virtio_mem *vm) in virtio_mem_init_kdump() argument
2733 dev_info(&vm->vdev->dev, "memory hot(un)plug disabled in kdump kernel\n"); in virtio_mem_init_kdump()
2734 vm->vmcore_cb.pfn_is_ram = virtio_mem_vmcore_pfn_is_ram; in virtio_mem_init_kdump()
2735 register_vmcore_cb(&vm->vmcore_cb); in virtio_mem_init_kdump()
2738 dev_warn(&vm->vdev->dev, "disabled in kdump kernel without vmcore\n"); in virtio_mem_init_kdump()
2743 static int virtio_mem_init(struct virtio_mem *vm) in virtio_mem_init() argument
2747 if (!vm->vdev->config->get) { in virtio_mem_init()
2748 dev_err(&vm->vdev->dev, "config access disabled\n"); in virtio_mem_init()
2753 virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size, in virtio_mem_init()
2754 &vm->plugged_size); in virtio_mem_init()
2755 virtio_cread_le(vm->vdev, struct virtio_mem_config, block_size, in virtio_mem_init()
2756 &vm->device_block_size); in virtio_mem_init()
2757 virtio_cread_le(vm->vdev, struct virtio_mem_config, node_id, in virtio_mem_init()
2759 vm->nid = virtio_mem_translate_node_id(vm, node_id); in virtio_mem_init()
2760 virtio_cread_le(vm->vdev, struct virtio_mem_config, addr, &vm->addr); in virtio_mem_init()
2761 virtio_cread_le(vm->vdev, struct virtio_mem_config, region_size, in virtio_mem_init()
2762 &vm->region_size); in virtio_mem_init()
2765 if (vm->nid == NUMA_NO_NODE) in virtio_mem_init()
2766 vm->nid = memory_add_physaddr_to_nid(vm->addr); in virtio_mem_init()
2768 dev_info(&vm->vdev->dev, "start address: 0x%llx", vm->addr); in virtio_mem_init()
2769 dev_info(&vm->vdev->dev, "region size: 0x%llx", vm->region_size); in virtio_mem_init()
2770 dev_info(&vm->vdev->dev, "device block size: 0x%llx", in virtio_mem_init()
2771 (unsigned long long)vm->device_block_size); in virtio_mem_init()
2772 if (vm->nid != NUMA_NO_NODE && IS_ENABLED(CONFIG_NUMA)) in virtio_mem_init()
2773 dev_info(&vm->vdev->dev, "nid: %d", vm->nid); in virtio_mem_init()
2779 if (vm->in_kdump) in virtio_mem_init()
2780 return virtio_mem_init_kdump(vm); in virtio_mem_init()
2781 return virtio_mem_init_hotplug(vm); in virtio_mem_init()
2784 static int virtio_mem_create_resource(struct virtio_mem *vm) in virtio_mem_create_resource() argument
2790 const char *name = kstrdup(dev_name(&vm->vdev->dev), GFP_KERNEL); in virtio_mem_create_resource()
2796 vm->parent_resource = __request_mem_region(vm->addr, vm->region_size, in virtio_mem_create_resource()
2799 if (!vm->parent_resource) { in virtio_mem_create_resource()
2801 dev_warn(&vm->vdev->dev, "could not reserve device region\n"); in virtio_mem_create_resource()
2802 dev_info(&vm->vdev->dev, in virtio_mem_create_resource()
2808 vm->parent_resource->flags &= ~IORESOURCE_BUSY; in virtio_mem_create_resource()
2812 static void virtio_mem_delete_resource(struct virtio_mem *vm) in virtio_mem_delete_resource() argument
2816 if (!vm->parent_resource) in virtio_mem_delete_resource()
2819 name = vm->parent_resource->name; in virtio_mem_delete_resource()
2820 release_resource(vm->parent_resource); in virtio_mem_delete_resource()
2821 kfree(vm->parent_resource); in virtio_mem_delete_resource()
2823 vm->parent_resource = NULL; in virtio_mem_delete_resource()
2831 static bool virtio_mem_has_memory_added(struct virtio_mem *vm) in virtio_mem_has_memory_added() argument
2835 return walk_iomem_res_desc(IORES_DESC_NONE, flags, vm->addr, in virtio_mem_has_memory_added()
2836 vm->addr + vm->region_size, NULL, in virtio_mem_has_memory_added()
2842 struct virtio_mem *vm; in virtio_mem_probe() local
2848 vdev->priv = vm = kzalloc(sizeof(*vm), GFP_KERNEL); in virtio_mem_probe()
2849 if (!vm) in virtio_mem_probe()
2852 init_waitqueue_head(&vm->host_resp); in virtio_mem_probe()
2853 vm->vdev = vdev; in virtio_mem_probe()
2854 INIT_WORK(&vm->wq, virtio_mem_run_wq); in virtio_mem_probe()
2855 mutex_init(&vm->hotplug_mutex); in virtio_mem_probe()
2856 INIT_LIST_HEAD(&vm->next); in virtio_mem_probe()
2857 spin_lock_init(&vm->removal_lock); in virtio_mem_probe()
2858 hrtimer_init(&vm->retry_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in virtio_mem_probe()
2859 vm->retry_timer.function = virtio_mem_timer_expired; in virtio_mem_probe()
2860 vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS; in virtio_mem_probe()
2861 vm->in_kdump = is_kdump_kernel(); in virtio_mem_probe()
2864 rc = virtio_mem_init_vq(vm); in virtio_mem_probe()
2869 rc = virtio_mem_init(vm); in virtio_mem_probe()
2876 if (!vm->in_kdump) { in virtio_mem_probe()
2877 atomic_set(&vm->config_changed, 1); in virtio_mem_probe()
2878 queue_work(system_freezable_wq, &vm->wq); in virtio_mem_probe()
2885 kfree(vm); in virtio_mem_probe()
2891 static void virtio_mem_deinit_hotplug(struct virtio_mem *vm) in virtio_mem_deinit_hotplug() argument
2900 mutex_lock(&vm->hotplug_mutex); in virtio_mem_deinit_hotplug()
2901 spin_lock_irq(&vm->removal_lock); in virtio_mem_deinit_hotplug()
2902 vm->removing = true; in virtio_mem_deinit_hotplug()
2903 spin_unlock_irq(&vm->removal_lock); in virtio_mem_deinit_hotplug()
2904 mutex_unlock(&vm->hotplug_mutex); in virtio_mem_deinit_hotplug()
2907 cancel_work_sync(&vm->wq); in virtio_mem_deinit_hotplug()
2908 hrtimer_cancel(&vm->retry_timer); in virtio_mem_deinit_hotplug()
2910 if (vm->in_sbm) { in virtio_mem_deinit_hotplug()
2915 virtio_mem_sbm_for_each_mb(vm, mb_id, in virtio_mem_deinit_hotplug()
2917 rc = virtio_mem_sbm_remove_mb(vm, mb_id); in virtio_mem_deinit_hotplug()
2919 virtio_mem_sbm_set_mb_state(vm, mb_id, in virtio_mem_deinit_hotplug()
2930 unregister_virtio_mem_device(vm); in virtio_mem_deinit_hotplug()
2931 unregister_pm_notifier(&vm->pm_notifier); in virtio_mem_deinit_hotplug()
2932 unregister_memory_notifier(&vm->memory_notifier); in virtio_mem_deinit_hotplug()
2939 if (virtio_mem_has_memory_added(vm)) { in virtio_mem_deinit_hotplug()
2940 dev_warn(&vm->vdev->dev, in virtio_mem_deinit_hotplug()
2943 virtio_mem_delete_resource(vm); in virtio_mem_deinit_hotplug()
2944 kfree_const(vm->resource_name); in virtio_mem_deinit_hotplug()
2945 memory_group_unregister(vm->mgid); in virtio_mem_deinit_hotplug()
2949 if (vm->in_sbm) { in virtio_mem_deinit_hotplug()
2950 vfree(vm->sbm.mb_states); in virtio_mem_deinit_hotplug()
2951 vfree(vm->sbm.sb_states); in virtio_mem_deinit_hotplug()
2953 vfree(vm->bbm.bb_states); in virtio_mem_deinit_hotplug()
2957 static void virtio_mem_deinit_kdump(struct virtio_mem *vm) in virtio_mem_deinit_kdump() argument
2960 unregister_vmcore_cb(&vm->vmcore_cb); in virtio_mem_deinit_kdump()
2966 struct virtio_mem *vm = vdev->priv; in virtio_mem_remove() local
2968 if (vm->in_kdump) in virtio_mem_remove()
2969 virtio_mem_deinit_kdump(vm); in virtio_mem_remove()
2971 virtio_mem_deinit_hotplug(vm); in virtio_mem_remove()
2977 kfree(vm); in virtio_mem_remove()
2983 struct virtio_mem *vm = vdev->priv; in virtio_mem_config_changed() local
2985 if (unlikely(vm->in_kdump)) in virtio_mem_config_changed()
2988 atomic_set(&vm->config_changed, 1); in virtio_mem_config_changed()
2989 virtio_mem_retry(vm); in virtio_mem_config_changed()
2995 struct virtio_mem *vm = vdev->priv; in virtio_mem_freeze() local
3003 vm->plugged_size && in virtio_mem_freeze()
3004 !virtio_has_feature(vm->vdev, VIRTIO_MEM_F_PERSISTENT_SUSPEND)) { in virtio_mem_freeze()
3005 dev_err(&vm->vdev->dev, in virtio_mem_freeze()
3012 vm->vq = NULL; in virtio_mem_freeze()
3018 struct virtio_mem *vm = vdev->priv; in virtio_mem_restore() local
3021 ret = virtio_mem_init_vq(vm); in virtio_mem_restore()