From a310b283e357b54533c8168a92e8f1068efdc4d1 Mon Sep 17 00:00:00 2001 From: Dominik Dingel Date: Thu, 5 Mar 2015 16:56:21 +0100 Subject: s390x/kvm: passing max memory size to accelerator With "KVM: s390: Allow userspace to limit guest memory size" KVM is able to do some optimizations based on the guest memory limit. The guest memory limit is computed by the initial definition and with the notion of hotplugged memory. Reviewed-by: Thomas Huth Reviewed-by: Guenther Hutzl Reviewed-by: David Hildenbrand Signed-off-by: Dominik Dingel Signed-off-by: Jens Freimann Message-Id: <1425570981-40609-3-git-send-email-jfrei@linux.vnet.ibm.com> Signed-off-by: Christian Borntraeger --- target-s390x/cpu.h | 14 ++++++++++++++ target-s390x/kvm.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) (limited to 'target-s390x') diff --git a/target-s390x/cpu.h b/target-s390x/cpu.h index 56c0293..143b631 100644 --- a/target-s390x/cpu.h +++ b/target-s390x/cpu.h @@ -1029,6 +1029,7 @@ int kvm_s390_get_memslot_count(KVMState *s); void kvm_s390_clear_cmma_callback(void *opaque); int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state); void kvm_s390_reset_vcpu(S390CPU *cpu); +int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit); #else static inline void kvm_s390_io_interrupt(uint16_t subchannel_id, uint16_t subchannel_nr, @@ -1066,8 +1067,21 @@ static inline int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state) static inline void kvm_s390_reset_vcpu(S390CPU *cpu) { } +static inline int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, + uint64_t *hw_limit) +{ + return 0; +} #endif +static inline int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit) +{ + if (kvm_enabled()) { + return kvm_s390_set_mem_limit(kvm_state, new_limit, hw_limit); + } + return 0; +} + static inline void cmma_reset(S390CPU *cpu) { if (kvm_enabled()) { diff --git a/target-s390x/kvm.c b/target-s390x/kvm.c index 6c4360b..a585fe3 100644 --- a/target-s390x/kvm.c +++ b/target-s390x/kvm.c @@ -122,6 +122,51 @@ static int cap_async_pf; static void *legacy_s390_alloc(size_t size, uint64_t *align); +static int kvm_s390_supports_mem_limit(KVMState *s) +{ + struct kvm_device_attr attr = { + .group = KVM_S390_VM_MEM_CTRL, + .attr = KVM_S390_VM_MEM_LIMIT_SIZE, + }; + + return (kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr) == 0); +} + +static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit) +{ + struct kvm_device_attr attr = { + .group = KVM_S390_VM_MEM_CTRL, + .attr = KVM_S390_VM_MEM_LIMIT_SIZE, + .addr = (uint64_t) memory_limit, + }; + + return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr); +} + +int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit) +{ + int rc; + + struct kvm_device_attr attr = { + .group = KVM_S390_VM_MEM_CTRL, + .attr = KVM_S390_VM_MEM_LIMIT_SIZE, + .addr = (uint64_t) &new_limit, + }; + + if (!kvm_s390_supports_mem_limit(s)) { + return 0; + } + + rc = kvm_s390_query_mem_limit(s, hw_limit); + if (rc) { + return rc; + } else if (*hw_limit < new_limit) { + return -E2BIG; + } + + return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr); +} + static int kvm_s390_check_clear_cmma(KVMState *s) { struct kvm_device_attr attr = { -- cgit v1.1