Commit 09170a49 authored by Paolo Bonzini's avatar Paolo Bonzini

KVM: const-ify uses of struct kvm_userspace_memory_region

Architecture-specific helpers are not supposed to muck with
struct kvm_userspace_memory_region contents.  Add const to
enforce this.

In order to eliminate the only write in __kvm_set_memory_region,
the cleaning of deleted slots is pulled up from update_memslots
to __kvm_set_memory_region.
Reviewed-by: default avatarTakuya Yoshikawa <yoshikawa_takuya_b1@lab.ntt.co.jp>
Reviewed-by: default avatarRadim Krcmar <rkrcmar@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 9f6b8029
...@@ -1719,7 +1719,7 @@ int kvm_mmu_init(void) ...@@ -1719,7 +1719,7 @@ int kvm_mmu_init(void)
} }
void kvm_arch_commit_memory_region(struct kvm *kvm, void kvm_arch_commit_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old, const struct kvm_memory_slot *old,
enum kvm_mr_change change) enum kvm_mr_change change)
{ {
...@@ -1734,7 +1734,7 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, ...@@ -1734,7 +1734,7 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,
int kvm_arch_prepare_memory_region(struct kvm *kvm, int kvm_arch_prepare_memory_region(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
enum kvm_mr_change change) enum kvm_mr_change change)
{ {
hva_t hva = mem->userspace_addr; hva_t hva = mem->userspace_addr;
......
...@@ -198,14 +198,14 @@ int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, ...@@ -198,14 +198,14 @@ int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
int kvm_arch_prepare_memory_region(struct kvm *kvm, int kvm_arch_prepare_memory_region(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
enum kvm_mr_change change) enum kvm_mr_change change)
{ {
return 0; return 0;
} }
void kvm_arch_commit_memory_region(struct kvm *kvm, void kvm_arch_commit_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old, const struct kvm_memory_slot *old,
enum kvm_mr_change change) enum kvm_mr_change change)
{ {
......
...@@ -182,9 +182,9 @@ extern int kvmppc_core_create_memslot(struct kvm *kvm, ...@@ -182,9 +182,9 @@ extern int kvmppc_core_create_memslot(struct kvm *kvm,
unsigned long npages); unsigned long npages);
extern int kvmppc_core_prepare_memory_region(struct kvm *kvm, extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem); const struct kvm_userspace_memory_region *mem);
extern void kvmppc_core_commit_memory_region(struct kvm *kvm, extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old); const struct kvm_memory_slot *old);
extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
struct kvm_ppc_smmu_info *info); struct kvm_ppc_smmu_info *info);
...@@ -243,9 +243,9 @@ struct kvmppc_ops { ...@@ -243,9 +243,9 @@ struct kvmppc_ops {
void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot); void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot);
int (*prepare_memory_region)(struct kvm *kvm, int (*prepare_memory_region)(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem); const struct kvm_userspace_memory_region *mem);
void (*commit_memory_region)(struct kvm *kvm, void (*commit_memory_region)(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old); const struct kvm_memory_slot *old);
int (*unmap_hva)(struct kvm *kvm, unsigned long hva); int (*unmap_hva)(struct kvm *kvm, unsigned long hva);
int (*unmap_hva_range)(struct kvm *kvm, unsigned long start, int (*unmap_hva_range)(struct kvm *kvm, unsigned long start,
......
...@@ -757,13 +757,13 @@ void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot) ...@@ -757,13 +757,13 @@ void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
int kvmppc_core_prepare_memory_region(struct kvm *kvm, int kvmppc_core_prepare_memory_region(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem) const struct kvm_userspace_memory_region *mem)
{ {
return kvm->arch.kvm_ops->prepare_memory_region(kvm, memslot, mem); return kvm->arch.kvm_ops->prepare_memory_region(kvm, memslot, mem);
} }
void kvmppc_core_commit_memory_region(struct kvm *kvm, void kvmppc_core_commit_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old) const struct kvm_memory_slot *old)
{ {
kvm->arch.kvm_ops->commit_memory_region(kvm, mem, old); kvm->arch.kvm_ops->commit_memory_region(kvm, mem, old);
......
...@@ -2376,13 +2376,13 @@ static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot, ...@@ -2376,13 +2376,13 @@ static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm, static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem) const struct kvm_userspace_memory_region *mem)
{ {
return 0; return 0;
} }
static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm, static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old) const struct kvm_memory_slot *old)
{ {
unsigned long npages = mem->memory_size >> PAGE_SHIFT; unsigned long npages = mem->memory_size >> PAGE_SHIFT;
......
...@@ -1573,13 +1573,13 @@ static void kvmppc_core_flush_memslot_pr(struct kvm *kvm, ...@@ -1573,13 +1573,13 @@ static void kvmppc_core_flush_memslot_pr(struct kvm *kvm,
static int kvmppc_core_prepare_memory_region_pr(struct kvm *kvm, static int kvmppc_core_prepare_memory_region_pr(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem) const struct kvm_userspace_memory_region *mem)
{ {
return 0; return 0;
} }
static void kvmppc_core_commit_memory_region_pr(struct kvm *kvm, static void kvmppc_core_commit_memory_region_pr(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old) const struct kvm_memory_slot *old)
{ {
return; return;
......
...@@ -1784,13 +1784,13 @@ int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, ...@@ -1784,13 +1784,13 @@ int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
int kvmppc_core_prepare_memory_region(struct kvm *kvm, int kvmppc_core_prepare_memory_region(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem) const struct kvm_userspace_memory_region *mem)
{ {
return 0; return 0;
} }
void kvmppc_core_commit_memory_region(struct kvm *kvm, void kvmppc_core_commit_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old) const struct kvm_memory_slot *old)
{ {
} }
......
...@@ -595,14 +595,14 @@ int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, ...@@ -595,14 +595,14 @@ int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
int kvm_arch_prepare_memory_region(struct kvm *kvm, int kvm_arch_prepare_memory_region(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
enum kvm_mr_change change) enum kvm_mr_change change)
{ {
return kvmppc_core_prepare_memory_region(kvm, memslot, mem); return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
} }
void kvm_arch_commit_memory_region(struct kvm *kvm, void kvm_arch_commit_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old, const struct kvm_memory_slot *old,
enum kvm_mr_change change) enum kvm_mr_change change)
{ {
......
...@@ -2582,7 +2582,7 @@ int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, ...@@ -2582,7 +2582,7 @@ int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
/* Section: memory related */ /* Section: memory related */
int kvm_arch_prepare_memory_region(struct kvm *kvm, int kvm_arch_prepare_memory_region(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
enum kvm_mr_change change) enum kvm_mr_change change)
{ {
/* A few sanity checks. We can have memory slots which have to be /* A few sanity checks. We can have memory slots which have to be
...@@ -2600,7 +2600,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, ...@@ -2600,7 +2600,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
} }
void kvm_arch_commit_memory_region(struct kvm *kvm, void kvm_arch_commit_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old, const struct kvm_memory_slot *old,
enum kvm_mr_change change) enum kvm_mr_change change)
{ {
......
...@@ -7700,7 +7700,7 @@ void kvm_arch_memslots_updated(struct kvm *kvm) ...@@ -7700,7 +7700,7 @@ void kvm_arch_memslots_updated(struct kvm *kvm)
int kvm_arch_prepare_memory_region(struct kvm *kvm, int kvm_arch_prepare_memory_region(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
enum kvm_mr_change change) enum kvm_mr_change change)
{ {
/* /*
...@@ -7778,7 +7778,7 @@ static void kvm_mmu_slot_apply_flags(struct kvm *kvm, ...@@ -7778,7 +7778,7 @@ static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
} }
void kvm_arch_commit_memory_region(struct kvm *kvm, void kvm_arch_commit_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old, const struct kvm_memory_slot *old,
enum kvm_mr_change change) enum kvm_mr_change change)
{ {
......
...@@ -501,9 +501,9 @@ enum kvm_mr_change { ...@@ -501,9 +501,9 @@ enum kvm_mr_change {
}; };
int kvm_set_memory_region(struct kvm *kvm, int kvm_set_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem); const struct kvm_userspace_memory_region *mem);
int __kvm_set_memory_region(struct kvm *kvm, int __kvm_set_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem); const struct kvm_userspace_memory_region *mem);
void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
struct kvm_memory_slot *dont); struct kvm_memory_slot *dont);
int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
...@@ -511,10 +511,10 @@ int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, ...@@ -511,10 +511,10 @@ int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
void kvm_arch_memslots_updated(struct kvm *kvm); void kvm_arch_memslots_updated(struct kvm *kvm);
int kvm_arch_prepare_memory_region(struct kvm *kvm, int kvm_arch_prepare_memory_region(struct kvm *kvm,
struct kvm_memory_slot *memslot, struct kvm_memory_slot *memslot,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
enum kvm_mr_change change); enum kvm_mr_change change);
void kvm_arch_commit_memory_region(struct kvm *kvm, void kvm_arch_commit_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem, const struct kvm_userspace_memory_region *mem,
const struct kvm_memory_slot *old, const struct kvm_memory_slot *old,
enum kvm_mr_change change); enum kvm_mr_change change);
bool kvm_largepages_enabled(void); bool kvm_largepages_enabled(void);
......
...@@ -676,8 +676,6 @@ static void update_memslots(struct kvm_memslots *slots, ...@@ -676,8 +676,6 @@ static void update_memslots(struct kvm_memslots *slots,
WARN_ON(mslots[i].id != id); WARN_ON(mslots[i].id != id);
if (!new->npages) { if (!new->npages) {
WARN_ON(!mslots[i].npages); WARN_ON(!mslots[i].npages);
new->base_gfn = 0;
new->flags = 0;
if (mslots[i].npages) if (mslots[i].npages)
slots->used_slots--; slots->used_slots--;
} else { } else {
...@@ -717,7 +715,7 @@ static void update_memslots(struct kvm_memslots *slots, ...@@ -717,7 +715,7 @@ static void update_memslots(struct kvm_memslots *slots,
slots->id_to_index[mslots[i].id] = i; slots->id_to_index[mslots[i].id] = i;
} }
static int check_memory_region_flags(struct kvm_userspace_memory_region *mem) static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem)
{ {
u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
...@@ -767,7 +765,7 @@ static struct kvm_memslots *install_new_memslots(struct kvm *kvm, ...@@ -767,7 +765,7 @@ static struct kvm_memslots *install_new_memslots(struct kvm *kvm,
* Must be called holding kvm->slots_lock for write. * Must be called holding kvm->slots_lock for write.
*/ */
int __kvm_set_memory_region(struct kvm *kvm, int __kvm_set_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem) const struct kvm_userspace_memory_region *mem)
{ {
int r; int r;
gfn_t base_gfn; gfn_t base_gfn;
...@@ -806,9 +804,6 @@ int __kvm_set_memory_region(struct kvm *kvm, ...@@ -806,9 +804,6 @@ int __kvm_set_memory_region(struct kvm *kvm,
if (npages > KVM_MEM_MAX_NR_PAGES) if (npages > KVM_MEM_MAX_NR_PAGES)
goto out; goto out;
if (!npages)
mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
new = old = *slot; new = old = *slot;
new.id = mem->slot; new.id = mem->slot;
...@@ -834,11 +829,15 @@ int __kvm_set_memory_region(struct kvm *kvm, ...@@ -834,11 +829,15 @@ int __kvm_set_memory_region(struct kvm *kvm,
goto out; goto out;
} }
} }
} else if (old.npages) { } else {
change = KVM_MR_DELETE; if (!old.npages)
} else /* Modify a non-existent slot: disallowed. */
goto out; goto out;
change = KVM_MR_DELETE;
new.base_gfn = 0;
new.flags = 0;
}
if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) { if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
/* Check for overlaps */ /* Check for overlaps */
r = -EEXIST; r = -EEXIST;
...@@ -944,7 +943,7 @@ int __kvm_set_memory_region(struct kvm *kvm, ...@@ -944,7 +943,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
EXPORT_SYMBOL_GPL(__kvm_set_memory_region); EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
int kvm_set_memory_region(struct kvm *kvm, int kvm_set_memory_region(struct kvm *kvm,
struct kvm_userspace_memory_region *mem) const struct kvm_userspace_memory_region *mem)
{ {
int r; int r;
...@@ -960,6 +959,7 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, ...@@ -960,6 +959,7 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
{ {
if (mem->slot >= KVM_USER_MEM_SLOTS) if (mem->slot >= KVM_USER_MEM_SLOTS)
return -EINVAL; return -EINVAL;
return kvm_set_memory_region(kvm, mem); return kvm_set_memory_region(kvm, mem);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment