Skip to content

Commit 25a35c1

Browse files
committed
Merge branch kvm-arm64/smccc-filter-cleanups into kvmarm/next
* kvm-arm64/smccc-filter-cleanups: : Cleanup the management of KVM's SMCCC maple tree : : Avoid the cost of maintaining the SMCCC filter maple tree if userspace : hasn't writen a rule to the filter. While at it, rip out the now : unnecessary VM flag to indicate whether or not the SMCCC filter was : configured. KVM: arm64: Use mtree_empty() to determine if SMCCC filter configured KVM: arm64: Only insert reserved ranges when SMCCC filter is used KVM: arm64: Add a predicate for testing if SMCCC filter is configured Signed-off-by: Oliver Upton <[email protected]>
2 parents 7ff7dfe + 4202bca commit 25a35c1

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

arch/arm64/include/asm/kvm_host.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,8 @@ struct kvm_arch {
239239
#define KVM_ARCH_FLAG_VM_COUNTER_OFFSET 5
240240
/* Timer PPIs made immutable */
241241
#define KVM_ARCH_FLAG_TIMER_PPIS_IMMUTABLE 6
242-
/* SMCCC filter initialized for the VM */
243-
#define KVM_ARCH_FLAG_SMCCC_FILTER_CONFIGURED 7
244242
/* Initial ID reg values loaded */
245-
#define KVM_ARCH_FLAG_ID_REGS_INITIALIZED 8
243+
#define KVM_ARCH_FLAG_ID_REGS_INITIALIZED 7
246244
unsigned long flags;
247245

248246
/* VM-wide vCPU feature set */

arch/arm64/kvm/hypercalls.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,10 @@ static bool kvm_smccc_test_fw_bmap(struct kvm_vcpu *vcpu, u32 func_id)
133133
ARM_SMCCC_SMC_64, \
134134
0, ARM_SMCCC_FUNC_MASK)
135135

136-
static void init_smccc_filter(struct kvm *kvm)
136+
static int kvm_smccc_filter_insert_reserved(struct kvm *kvm)
137137
{
138138
int r;
139139

140-
mt_init(&kvm->arch.smccc_filter);
141-
142140
/*
143141
* Prevent userspace from handling any SMCCC calls in the architecture
144142
* range, avoiding the risk of misrepresenting Spectre mitigation status
@@ -148,14 +146,25 @@ static void init_smccc_filter(struct kvm *kvm)
148146
SMC32_ARCH_RANGE_BEGIN, SMC32_ARCH_RANGE_END,
149147
xa_mk_value(KVM_SMCCC_FILTER_HANDLE),
150148
GFP_KERNEL_ACCOUNT);
151-
WARN_ON_ONCE(r);
149+
if (r)
150+
goto out_destroy;
152151

153152
r = mtree_insert_range(&kvm->arch.smccc_filter,
154153
SMC64_ARCH_RANGE_BEGIN, SMC64_ARCH_RANGE_END,
155154
xa_mk_value(KVM_SMCCC_FILTER_HANDLE),
156155
GFP_KERNEL_ACCOUNT);
157-
WARN_ON_ONCE(r);
156+
if (r)
157+
goto out_destroy;
158158

159+
return 0;
160+
out_destroy:
161+
mtree_destroy(&kvm->arch.smccc_filter);
162+
return r;
163+
}
164+
165+
static bool kvm_smccc_filter_configured(struct kvm *kvm)
166+
{
167+
return !mtree_empty(&kvm->arch.smccc_filter);
159168
}
160169

161170
static int kvm_smccc_set_filter(struct kvm *kvm, struct kvm_smccc_filter __user *uaddr)
@@ -184,13 +193,14 @@ static int kvm_smccc_set_filter(struct kvm *kvm, struct kvm_smccc_filter __user
184193
goto out_unlock;
185194
}
186195

196+
if (!kvm_smccc_filter_configured(kvm)) {
197+
r = kvm_smccc_filter_insert_reserved(kvm);
198+
if (WARN_ON_ONCE(r))
199+
goto out_unlock;
200+
}
201+
187202
r = mtree_insert_range(&kvm->arch.smccc_filter, start, end,
188203
xa_mk_value(filter.action), GFP_KERNEL_ACCOUNT);
189-
if (r)
190-
goto out_unlock;
191-
192-
set_bit(KVM_ARCH_FLAG_SMCCC_FILTER_CONFIGURED, &kvm->arch.flags);
193-
194204
out_unlock:
195205
mutex_unlock(&kvm->arch.config_lock);
196206
return r;
@@ -201,7 +211,7 @@ static u8 kvm_smccc_filter_get_action(struct kvm *kvm, u32 func_id)
201211
unsigned long idx = func_id;
202212
void *val;
203213

204-
if (!test_bit(KVM_ARCH_FLAG_SMCCC_FILTER_CONFIGURED, &kvm->arch.flags))
214+
if (!kvm_smccc_filter_configured(kvm))
205215
return KVM_SMCCC_FILTER_HANDLE;
206216

207217
/*
@@ -387,7 +397,7 @@ void kvm_arm_init_hypercalls(struct kvm *kvm)
387397
smccc_feat->std_hyp_bmap = KVM_ARM_SMCCC_STD_HYP_FEATURES;
388398
smccc_feat->vendor_hyp_bmap = KVM_ARM_SMCCC_VENDOR_HYP_FEATURES;
389399

390-
init_smccc_filter(kvm);
400+
mt_init(&kvm->arch.smccc_filter);
391401
}
392402

393403
void kvm_arm_teardown_hypercalls(struct kvm *kvm)

0 commit comments

Comments
 (0)