Skip to content

Commit aa069a9

Browse files
committed
KVM: PPC: Book3S HV: Add a VM capability to enable nested virtualization
With this, userspace can enable a KVM-HV guest to run nested guests under it. The administrator can control whether any nested guests can be run; setting the "nested" module parameter to false prevents any guests becoming nested hypervisors (that is, any attempt to enable the nested capability on a guest will fail). Guests which are already nested hypervisors will continue to be so. Reviewed-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
1 parent 9d67121 commit aa069a9

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

Documentation/virtual/kvm/api.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4532,6 +4532,20 @@ With this capability, a guest may read the MSR_PLATFORM_INFO MSR. Otherwise,
45324532
a #GP would be raised when the guest tries to access. Currently, this
45334533
capability does not enable write permissions of this MSR for the guest.
45344534

4535+
7.16 KVM_CAP_PPC_NESTED_HV
4536+
4537+
Architectures: ppc
4538+
Parameters: none
4539+
Returns: 0 on success, -EINVAL when the implementation doesn't support
4540+
nested-HV virtualization.
4541+
4542+
HV-KVM on POWER9 and later systems allows for "nested-HV"
4543+
virtualization, which provides a way for a guest VM to run guests that
4544+
can run using the CPU's supervisor mode (privileged non-hypervisor
4545+
state). Enabling this capability on a VM depends on the CPU having
4546+
the necessary functionality and on the facility being enabled with a
4547+
kvm-hv module parameter.
4548+
45354549
8. Other capabilities.
45364550
----------------------
45374551

arch/powerpc/include/asm/kvm_ppc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ struct kvmppc_ops {
325325
int (*set_smt_mode)(struct kvm *kvm, unsigned long mode,
326326
unsigned long flags);
327327
void (*giveup_ext)(struct kvm_vcpu *vcpu, ulong msr);
328+
int (*enable_nested)(struct kvm *kvm);
328329
};
329330

330331
extern struct kvmppc_ops *kvmppc_hv_ops;

arch/powerpc/kvm/book3s_hv.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644);
122122
MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
123123
#endif
124124

125+
/* If set, guests are allowed to create and control nested guests */
126+
static bool nested = true;
127+
module_param(nested, bool, S_IRUGO | S_IWUSR);
128+
MODULE_PARM_DESC(nested, "Enable nested virtualization (only on POWER9)");
129+
130+
static inline bool nesting_enabled(struct kvm *kvm)
131+
{
132+
return kvm->arch.nested_enable && kvm_is_radix(kvm);
133+
}
134+
125135
/* If set, the threads on each CPU core have to be in the same MMU mode */
126136
static bool no_mixing_hpt_and_radix;
127137

@@ -963,12 +973,12 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
963973

964974
case H_SET_PARTITION_TABLE:
965975
ret = H_FUNCTION;
966-
if (vcpu->kvm->arch.nested_enable)
976+
if (nesting_enabled(vcpu->kvm))
967977
ret = kvmhv_set_partition_table(vcpu);
968978
break;
969979
case H_ENTER_NESTED:
970980
ret = H_FUNCTION;
971-
if (!vcpu->kvm->arch.nested_enable)
981+
if (!nesting_enabled(vcpu->kvm))
972982
break;
973983
ret = kvmhv_enter_nested_guest(vcpu);
974984
if (ret == H_INTERRUPT) {
@@ -978,9 +988,8 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
978988
break;
979989
case H_TLB_INVALIDATE:
980990
ret = H_FUNCTION;
981-
if (!vcpu->kvm->arch.nested_enable)
982-
break;
983-
ret = kvmhv_do_nested_tlbie(vcpu);
991+
if (nesting_enabled(vcpu->kvm))
992+
ret = kvmhv_do_nested_tlbie(vcpu);
984993
break;
985994

986995
default:
@@ -4508,10 +4517,8 @@ static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
45084517
/* Must be called with kvm->lock held and mmu_ready = 0 and no vcpus running */
45094518
int kvmppc_switch_mmu_to_hpt(struct kvm *kvm)
45104519
{
4511-
if (kvm->arch.nested_enable) {
4512-
kvm->arch.nested_enable = false;
4520+
if (nesting_enabled(kvm))
45134521
kvmhv_release_all_nested(kvm);
4514-
}
45154522
kvmppc_free_radix(kvm);
45164523
kvmppc_update_lpcr(kvm, LPCR_VPM1,
45174524
LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
@@ -4788,7 +4795,7 @@ static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
47884795

47894796
/* Perform global invalidation and return lpid to the pool */
47904797
if (cpu_has_feature(CPU_FTR_ARCH_300)) {
4791-
if (kvm->arch.nested_enable)
4798+
if (nesting_enabled(kvm))
47924799
kvmhv_release_all_nested(kvm);
47934800
kvm->arch.process_table = 0;
47944801
kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0);
@@ -5181,6 +5188,19 @@ static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
51815188
return err;
51825189
}
51835190

5191+
static int kvmhv_enable_nested(struct kvm *kvm)
5192+
{
5193+
if (!nested)
5194+
return -EPERM;
5195+
if (!cpu_has_feature(CPU_FTR_ARCH_300))
5196+
return -ENODEV;
5197+
5198+
/* kvm == NULL means the caller is testing if the capability exists */
5199+
if (kvm)
5200+
kvm->arch.nested_enable = true;
5201+
return 0;
5202+
}
5203+
51845204
static struct kvmppc_ops kvm_ops_hv = {
51855205
.get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
51865206
.set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
@@ -5220,6 +5240,7 @@ static struct kvmppc_ops kvm_ops_hv = {
52205240
.configure_mmu = kvmhv_configure_mmu,
52215241
.get_rmmu_info = kvmhv_get_rmmu_info,
52225242
.set_smt_mode = kvmhv_set_smt_mode,
5243+
.enable_nested = kvmhv_enable_nested,
52235244
};
52245245

52255246
static int kvm_init_subcore_bitmap(void)

arch/powerpc/kvm/powerpc.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
597597
r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300) &&
598598
cpu_has_feature(CPU_FTR_HVMODE));
599599
break;
600+
case KVM_CAP_PPC_NESTED_HV:
601+
r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&
602+
!kvmppc_hv_ops->enable_nested(NULL));
603+
break;
600604
#endif
601605
case KVM_CAP_SYNC_MMU:
602606
#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
@@ -2115,6 +2119,14 @@ static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
21152119
r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
21162120
break;
21172121
}
2122+
2123+
case KVM_CAP_PPC_NESTED_HV:
2124+
r = -EINVAL;
2125+
if (!is_kvmppc_hv_enabled(kvm) ||
2126+
!kvm->arch.kvm_ops->enable_nested)
2127+
break;
2128+
r = kvm->arch.kvm_ops->enable_nested(kvm);
2129+
break;
21182130
#endif
21192131
default:
21202132
r = -EINVAL;

include/uapi/linux/kvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,7 @@ struct kvm_ppc_resize_hpt {
953953
#define KVM_CAP_NESTED_STATE 157
954954
#define KVM_CAP_ARM_INJECT_SERROR_ESR 158
955955
#define KVM_CAP_MSR_PLATFORM_INFO 159
956+
#define KVM_CAP_PPC_NESTED_HV 160
956957

957958
#ifdef KVM_CAP_IRQ_ROUTING
958959

0 commit comments

Comments
 (0)