mmu.c (f5756029eef501bcd39ecd844968e4fb3055c1bd) mmu.c (a54d806688fe1e482350ce759a8a0fc9ebf814b0)
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * This module enables machines with Intel VT-x extensions to run virtual
6 * machines without emulation or binary translation.
7 *
8 * MMU support

--- 3395 unchanged lines hidden (view full) ---

3404 write_unlock(&vcpu->kvm->mmu_lock);
3405 return r;
3406}
3407
3408static int mmu_first_shadow_root_alloc(struct kvm *kvm)
3409{
3410 struct kvm_memslots *slots;
3411 struct kvm_memory_slot *slot;
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * This module enables machines with Intel VT-x extensions to run virtual
6 * machines without emulation or binary translation.
7 *
8 * MMU support

--- 3395 unchanged lines hidden (view full) ---

3404 write_unlock(&vcpu->kvm->mmu_lock);
3405 return r;
3406}
3407
3408static int mmu_first_shadow_root_alloc(struct kvm *kvm)
3409{
3410 struct kvm_memslots *slots;
3411 struct kvm_memory_slot *slot;
3412 int r = 0, i;
3412 int r = 0, i, bkt;
3413
3414 /*
3415 * Check if this is the first shadow root being allocated before
3416 * taking the lock.
3417 */
3418 if (kvm_shadow_root_allocated(kvm))
3419 return 0;
3420

--- 8 unchanged lines hidden (view full) ---

3429 * will be allocated upfront if TDP is disabled.
3430 */
3431 if (kvm_memslots_have_rmaps(kvm) &&
3432 kvm_page_track_write_tracking_enabled(kvm))
3433 goto out_success;
3434
3435 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
3436 slots = __kvm_memslots(kvm, i);
3413
3414 /*
3415 * Check if this is the first shadow root being allocated before
3416 * taking the lock.
3417 */
3418 if (kvm_shadow_root_allocated(kvm))
3419 return 0;
3420

--- 8 unchanged lines hidden (view full) ---

3429 * will be allocated upfront if TDP is disabled.
3430 */
3431 if (kvm_memslots_have_rmaps(kvm) &&
3432 kvm_page_track_write_tracking_enabled(kvm))
3433 goto out_success;
3434
3435 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
3436 slots = __kvm_memslots(kvm, i);
3437 kvm_for_each_memslot(slot, slots) {
3437 kvm_for_each_memslot(slot, bkt, slots) {
3438 /*
3439 * Both of these functions are no-ops if the target is
3440 * already allocated, so unconditionally calling both
3441 * is safe. Intentionally do NOT free allocations on
3442 * failure to avoid having to track which allocations
3443 * were made now versus when the memslot was created.
3444 * The metadata is guaranteed to be freed when the slot
3445 * is freed, and will be kept/used if userspace retries

--- 2279 unchanged lines hidden (view full) ---

5725}
5726
5727static bool __kvm_zap_rmaps(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
5728{
5729 const struct kvm_memory_slot *memslot;
5730 struct kvm_memslots *slots;
5731 bool flush = false;
5732 gfn_t start, end;
3438 /*
3439 * Both of these functions are no-ops if the target is
3440 * already allocated, so unconditionally calling both
3441 * is safe. Intentionally do NOT free allocations on
3442 * failure to avoid having to track which allocations
3443 * were made now versus when the memslot was created.
3444 * The metadata is guaranteed to be freed when the slot
3445 * is freed, and will be kept/used if userspace retries

--- 2279 unchanged lines hidden (view full) ---

5725}
5726
5727static bool __kvm_zap_rmaps(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
5728{
5729 const struct kvm_memory_slot *memslot;
5730 struct kvm_memslots *slots;
5731 bool flush = false;
5732 gfn_t start, end;
5733 int i;
5733 int i, bkt;
5734
5735 if (!kvm_memslots_have_rmaps(kvm))
5736 return flush;
5737
5738 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
5739 slots = __kvm_memslots(kvm, i);
5734
5735 if (!kvm_memslots_have_rmaps(kvm))
5736 return flush;
5737
5738 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
5739 slots = __kvm_memslots(kvm, i);
5740 kvm_for_each_memslot(memslot, slots) {
5740 kvm_for_each_memslot(memslot, bkt, slots) {
5741 start = max(gfn_start, memslot->base_gfn);
5742 end = min(gfn_end, memslot->base_gfn + memslot->npages);
5743 if (start >= end)
5744 continue;
5745
5746 flush = slot_handle_level_range(kvm, memslot, kvm_zap_rmapp,
5747 PG_LEVEL_4K, KVM_MAX_HUGEPAGE_LEVEL,
5748 start, end - 1, true, flush);

--- 575 unchanged lines hidden ---
5741 start = max(gfn_start, memslot->base_gfn);
5742 end = min(gfn_end, memslot->base_gfn + memslot->npages);
5743 if (start >= end)
5744 continue;
5745
5746 flush = slot_handle_level_range(kvm, memslot, kvm_zap_rmapp,
5747 PG_LEVEL_4K, KVM_MAX_HUGEPAGE_LEVEL,
5748 start, end - 1, true, flush);

--- 575 unchanged lines hidden ---