Skip to content

KVM: arm64: Userspace SMCCC call filtering

Oliver Upton kvm kvmarm 3 versions – Applied

Status from this message: Applied to next, thanks!

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest13 patches 11 repliesApplied
  2. v2 posting 2/213 patches 7 repliesSuperseded
  3. v2 RFC posting 1/26 patches 9 repliesSuperseded

Changes in v3 from the cover letter

- Collect Sean's Ack (thanks!)
- s/ALLOW/HANDLE/g (Marc)
- Test that the padding is zero (Marc)
- Doc fixes, typos, etc. (Marc)
- Test that the filter range has not overflowed

Patches in v3 Applied

#Subject and filesSizeRepliesApplied
1/13KVM: x86: Redefine 'longmode' as a flag for KVM_EXIT_HYPERCALLDocumentation/virt/kvm/api.rst arch/x86/include/asm/kvm_host.h arch/x86/include/uapi/asm/kvm.h +2 more+23 -50 replies
2/13KVM: arm64: Add a helper to check if a VM has ran oncearch/arm64/include/asm/kvm_host.h arch/arm64/kvm/hypercalls.c arch/arm64/kvm/pmu-emul.c+6 -40 replies
3/13KVM: arm64: Add vm fd device attribute accessorsarch/arm64/kvm/arm.c+29 -00 replies
4/13KVM: arm64: Rename SMC/HVC call handler to reflect realityarch/arm64/kvm/handle_exit.c arch/arm64/kvm/hypercalls.c include/kvm/arm_hypercalls.h+4 -40 replies
5/13KVM: arm64: Start handling SMCs from EL1arch/arm64/kvm/handle_exit.c+7 -70 replies
6/13KVM: arm64: Refactor hvc filtering to support different actionsarch/arm64/include/uapi/asm/kvm.h arch/arm64/kvm/hypercalls.c+31 -40 replies
7/13KVM: arm64: Use a maple tree to represent the SMCCC filterarch/arm64/include/asm/kvm_host.h arch/arm64/kvm/arm.c arch/arm64/kvm/hypercalls.c +1 more+64 -10 replies
8/13KVM: arm64: Add support for KVM_EXIT_HYPERCALLDocumentation/virt/kvm/api.rst arch/arm64/include/uapi/asm/kvm.h arch/arm64/kvm/handle_exit.c +1 more+39 -310 replies
9/13KVM: arm64: Introduce support for userspace SMCCC filteringDocumentation/virt/kvm/api.rst Documentation/virt/kvm/devices/vm.rst arch/arm64/include/uapi/asm/kvm.h +3 more+161 -00 replies
10/13KVM: arm64: Return NOT_SUPPORTED to guest for unknown PSCI versionarch/arm64/kvm/psci.c+5 -20 replies
11/13KVM: arm64: Let errors from SMCCC emulation to reach userspacearch/arm64/kvm/handle_exit.c+2 -160 replies
12/13KVM: selftests: Add a helper for SMCCC calls with SMC instructiontools/testing/selftests/kvm/include/aarch64/processor.h tools/testing/selftests/kvm/lib/aarch64/processor.c+46 -190 replies
13/13KVM: selftests: Add test for SMCCC filtertools/testing/selftests/kvm/Makefile tools/testing/selftests/kvm/aarch64/smccc_filter.c+261 -00 replies

Reviews trailers per patch

6 of 13 patches have a Reviewed-by or Acked-by.

PatchSean ChristophersonSuzuki K PouloseOliver Upton
1/13A-b
2/13
3/13R-b
4/13R-b
5/13R-b
6/13R-b
7/13
8/13R-b
9/13
10/13
11/13
12/13
13/13

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 3 reviewers.

Read the v3 thread

Cover letter [PATCH v3 00/13] KVM: arm64: Userspace SMCCC call filtering

The Arm SMCCC is rather prescriptive in regards to the allocation of SMCCC function ID ranges. Many of the hypercall ranges have an associated specification from Arm (FF-A, PSCI, SDEI, etc.) with some room for vendor-specific implementations.

The ever-expanding SMCCC surface leaves a lot of work within KVM for providing new features. Furthermore, KVM implements its own vendor-specific ABI, with little room for other implementations (like Hyper-V, for example). Rather than cramming it all into the kernel we should provide a way for userspace to handle hypercalls.

It would appear that vCPU hotplug [*] has a legitimate use case for something like this, sending PSCI calls to userspace (where they should have gone in the first place).

[*] https://lore.kernel.org/kvmarm/[email protected]/

=> We have these new hypercall bitmap registers, why not use that?

The hypercall bitmap registers aren't necessarily aimed at the same problem. The bitmap registers allow a VMM to preserve the ABI the guest gets from KVM by default when migrating between hosts. By default KVM exposes the entire feature set to the guest, whereas user SMCCC calls need explicit opt-in from userspace.

Applies to 6.3-rc3. Note that the kvm->lock is used in this series to guard the SMCCC filter and should be converted to use kvm->arch.config_lock when that is available. Marc, you can find my conflict resolution below between these two series, and I've also pushed the result to:

git://git.kernel.org/pub/scm/linux/kernel/git/oupton/linux.git kvm-arm64/for-6.4

Presumptive, I know! :)

v1: https://lore.kernel.org/kvmarm/[email protected]/
v2: https://lore.kernel.org/kvmarm/[email protected]/

v2 -> v3:
- Collect Sean's Ack (thanks!)
- s/ALLOW/HANDLE/g (Marc)
- Test that the padding is zero (Marc)
- Doc fixes, typos, etc. (Marc)
- Test that the filter range has not overflowed

v1 -> v2:
- Only set bit 0 for longmode, requiring the remaining lower 31 bits be 0 (Sean)
- Let errors from kvm_smccc_call_handler() reach userspace (Suzuki)
- Use the kvm_vm_has_ran_once() helper in the hypercall bitmap register handlers
- Collect Suzuki's R-bs (thanks!)

Oliver Upton (13):
KVM: x86: Redefine 'longmode' as a flag for KVM_EXIT_HYPERCALL
KVM: arm64: Add a helper to check if a VM has ran once
KVM: arm64: Add vm fd device attribute accessors
KVM: arm64: Rename SMC/HVC call handler to reflect reality
KVM: arm64: Start handling SMCs from EL1
KVM: arm64: Refactor hvc filtering to support different actions
KVM: arm64: Use a maple tree to represent the SMCCC filter
KVM: arm64: Add support for KVM_EXIT_HYPERCALL
KVM: arm64: Introduce support for userspace SMCCC filtering
KVM: arm64: Return NOT_SUPPORTED to guest for unknown PSCI version
KVM: arm64: Let errors from SMCCC emulation to reach userspace
KVM: selftests: Add a helper for SMCCC calls with SMC instruction
KVM: selftests: Add test for SMCCC filter

 Documentation/virt/kvm/api.rst                |  25 +-
 Documentation/virt/kvm/devices/vm.rst         |  79 ++++++
 arch/arm64/include/asm/kvm_host.h             |   8 +-
 arch/arm64/include/uapi/asm/kvm.h             |  24 ++
 arch/arm64/kvm/arm.c                          |  35 +++
 arch/arm64/kvm/handle_exit.c                  |  36 +--
 arch/arm64/kvm/hypercalls.c                   | 164 ++++++++++-
 arch/arm64/kvm/pmu-emul.c                     |   4 +-
 arch/arm64/kvm/psci.c                         |   7 +-
 arch/x86/include/asm/kvm_host.h               |   7 +
 arch/x86/include/uapi/asm/kvm.h               |   3 +
 arch/x86/kvm/x86.c                            |   6 +-
 include/kvm/arm_hypercalls.h                  |   6 +-
 include/uapi/linux/kvm.h                      |   9 +-
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/aarch64/smccc_filter.c      | 260 ++++++++++++++++++
 .../selftests/kvm/include/aarch64/processor.h |  13 +
 .../selftests/kvm/lib/aarch64/processor.c     |  52 ++--
 18 files changed, 676 insertions(+), 63 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/aarch64/smccc_filter.c

base-commit: e8d018dd0257f744ca50a729e3d042cf2ec9da65

arch/arm64/include/asm/kvm_host.h +0 -0
diff --cc arch/arm64/include/asm/kvm_host.h
index a8e2c52b44aa,2682b3fd0881..000000000000
@@@ -224,11 -222,8 +225,12 @@@ struct kvm_arch 
  #define KVM_ARCH_FLAG_EL1_32BIT				4
  	/* PSCI SYSTEM_SUSPEND enabled for the guest */
  #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED		5
 +	/* VM counter offset */
 +#define KVM_ARCH_FLAG_VM_COUNTER_OFFSET			6
 +	/* Timer PPIs made immutable */
 +#define KVM_ARCH_FLAG_TIMER_PPIS_IMMUTABLE		7
- 
+ 	/* SMCCC filter initialized for the VM */
 -#define KVM_ARCH_FLAG_SMCCC_FILTER_CONFIGURED		6
++#define KVM_ARCH_FLAG_SMCCC_FILTER_CONFIGURED		8
  	unsigned long flags;
  
  	/*
arch/arm64/kvm/arm.c +0 -0
diff --cc arch/arm64/kvm/arm.c
index 0e5a3ff8cc5a,efee032c9560..000000000000
@@@ -1513,13 -1502,18 +1536,25 @@@ long kvm_arch_vm_ioctl(struct file *fil
  			return -EFAULT;
  		return kvm_vm_ioctl_mte_copy_tags(kvm, &copy_tags);
  	}
 +	case KVM_ARM_SET_COUNTER_OFFSET: {
 +		struct kvm_arm_counter_offset offset;
 +
 +		if (copy_from_user(&offset, argp, sizeof(offset)))
 +			return -EFAULT;
 +		return kvm_vm_ioctl_set_counter_offset(kvm, &offset);
 +	}
+ 	case KVM_HAS_DEVICE_ATTR: {
+ 		if (copy_from_user(&attr, argp, sizeof(attr)))
+ 			return -EFAULT;
+ 
+ 		return kvm_vm_has_attr(kvm, &attr);
+ 	}
+ 	case KVM_SET_DEVICE_ATTR: {
+ 		if (copy_from_user(&attr, argp, sizeof(attr)))
+ 			return -EFAULT;
+ 
+ 		return kvm_vm_set_attr(kvm, &attr);
+ 	}
  	default:
  		return -EINVAL;
  	}
arch/arm64/kvm/pmu-emul.c +0 -0
diff --cc arch/arm64/kvm/pmu-emul.c
index 240168416838,a0fc569fdbca..000000000000
@@@ -958,8 -961,12 +958,8 @@@ int kvm_arm_pmu_v3_set_attr(struct kvm_
  		     filter.action != KVM_PMU_EVENT_DENY))
  			return -EINVAL;
  
- 		if (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags))
 -		mutex_lock(&kvm->lock);
 -
 -		if (kvm_vm_has_ran_once(kvm)) {
 -			mutex_unlock(&kvm->lock);
++		if (kvm_vm_has_ran_once(kvm))
  			return -EBUSY;
 -		}
  
  		if (!kvm->arch.pmu_filter) {
  			kvm->arch.pmu_filter = bitmap_alloc(nr_events, GFP_KERNEL_ACCOUNT);
arch/arm64/kvm/hypercalls.c +2 -2
index ba1adc68d695..22938e4d947c 100644
@@ -164,7 +164,7 @@ static int kvm_smccc_set_filter(struct kvm *kvm, struct kvm_smccc_filter __user
 	if (end < start || filter.action >= NR_SMCCC_FILTER_ACTIONS)
 		return -EINVAL;
 
-	mutex_lock(&kvm->lock);
+	mutex_lock(&kvm->arch.config_lock);
 
 	if (kvm_vm_has_ran_once(kvm)) {
 		r = -EBUSY;
@@ -179,7 +179,7 @@ static int kvm_smccc_set_filter(struct kvm *kvm, struct kvm_smccc_filter __user
 	set_bit(KVM_ARCH_FLAG_SMCCC_FILTER_CONFIGURED, &kvm->arch.flags);
 
 out_unlock:
-	mutex_unlock(&kvm->lock);
+	mutex_unlock(&kvm->arch.config_lock);
 	return r;
 }
 
Signature · 1 line
2.40.0.348.gf938b09366-goog