Skip to content

Commit 4bd774e

Browse files
Dave MartinMarc Zyngier
authored andcommitted
KVM: arm64/sve: Simplify KVM_REG_ARM64_SVE_VLS array sizing
A complicated DIV_ROUND_UP() expression is currently written out explicitly in multiple places in order to specify the size of the bitmap exchanged with userspace to represent the value of the KVM_REG_ARM64_SVE_VLS pseudo-register. Userspace currently has no direct way to work this out either: for documentation purposes, the size is just quoted as 8 u64s. To make this more intuitive, this patch replaces these with a single define, which is also exported to userspace as KVM_ARM64_SVE_VLS_WORDS. Since the number of words in a bitmap is just the index of the last word used + 1, this patch expresses the bound that way instead. This should make it clearer what is being expressed. For userspace convenience, the minimum and maximum possible vector lengths relevant to the KVM ABI are exposed to UAPI as KVM_ARM64_SVE_VQ_MIN, KVM_ARM64_SVE_VQ_MAX. Since the only direct use for these at present is manipulation of KVM_REG_ARM64_SVE_VLS, no corresponding _VL_ macros are defined. They could be added later if a need arises. Since use of DIV_ROUND_UP() was the only reason for including <linux/kernel.h> in guest.c, this patch also removes that #include. Suggested-by: Andrew Jones <drjones@redhat.com> Signed-off-by: Dave Martin <Dave.Martin@arm.com> Reviewed-by: Andrew Jones <drjones@redhat.com> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 55ffad3 commit 4bd774e

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,13 +2171,15 @@ and KVM_ARM_VCPU_FINALIZE for more information about this procedure.
21712171
KVM_REG_ARM64_SVE_VLS is a pseudo-register that allows the set of vector
21722172
lengths supported by the vcpu to be discovered and configured by
21732173
userspace. When transferred to or from user memory via KVM_GET_ONE_REG
2174-
or KVM_SET_ONE_REG, the value of this register is of type __u64[8], and
2175-
encodes the set of vector lengths as follows:
2174+
or KVM_SET_ONE_REG, the value of this register is of type
2175+
__u64[KVM_ARM64_SVE_VLS_WORDS], and encodes the set of vector lengths as
2176+
follows:
21762177

2177-
__u64 vector_lengths[8];
2178+
__u64 vector_lengths[KVM_ARM64_SVE_VLS_WORDS];
21782179

21792180
if (vq >= SVE_VQ_MIN && vq <= SVE_VQ_MAX &&
2180-
((vector_lengths[(vq - 1) / 64] >> ((vq - 1) % 64)) & 1))
2181+
((vector_lengths[(vq - KVM_ARM64_SVE_VQ_MIN) / 64] >>
2182+
((vq - KVM_ARM64_SVE_VQ_MIN) % 64)) & 1))
21812183
/* Vector length vq * 16 bytes supported */
21822184
else
21832185
/* Vector length vq * 16 bytes not supported */

‎arch/arm64/include/uapi/asm/kvm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,14 @@ struct kvm_vcpu_events {
258258
KVM_REG_SIZE_U256 | \
259259
((i) & (KVM_ARM64_SVE_MAX_SLICES - 1)))
260260

261+
#define KVM_ARM64_SVE_VQ_MIN __SVE_VQ_MIN
262+
#define KVM_ARM64_SVE_VQ_MAX __SVE_VQ_MAX
263+
261264
/* Vector lengths pseudo-register: */
262265
#define KVM_REG_ARM64_SVE_VLS (KVM_REG_ARM64 | KVM_REG_ARM64_SVE | \
263266
KVM_REG_SIZE_U512 | 0xffff)
267+
#define KVM_ARM64_SVE_VLS_WORDS \
268+
((KVM_ARM64_SVE_VQ_MAX - KVM_ARM64_SVE_VQ_MIN) / 64 + 1)
264269

265270
/* Device Control API: ARM VGIC */
266271
#define KVM_DEV_ARM_VGIC_GRP_ADDR 0

‎arch/arm64/kvm/guest.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <linux/errno.h>
2424
#include <linux/err.h>
2525
#include <linux/nospec.h>
26-
#include <linux/kernel.h>
2726
#include <linux/kvm_host.h>
2827
#include <linux/module.h>
2928
#include <linux/stddef.h>
@@ -210,7 +209,7 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
210209
#define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
211210

212211
static bool vq_present(
213-
const u64 (*const vqs)[DIV_ROUND_UP(SVE_VQ_MAX - SVE_VQ_MIN + 1, 64)],
212+
const u64 (*const vqs)[KVM_ARM64_SVE_VLS_WORDS],
214213
unsigned int vq)
215214
{
216215
return (*vqs)[vq_word(vq)] & vq_mask(vq);
@@ -219,7 +218,7 @@ static bool vq_present(
219218
static int get_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
220219
{
221220
unsigned int max_vq, vq;
222-
u64 vqs[DIV_ROUND_UP(SVE_VQ_MAX - SVE_VQ_MIN + 1, 64)];
221+
u64 vqs[KVM_ARM64_SVE_VLS_WORDS];
223222

224223
if (!vcpu_has_sve(vcpu))
225224
return -ENOENT;
@@ -243,7 +242,7 @@ static int get_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
243242
static int set_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
244243
{
245244
unsigned int max_vq, vq;
246-
u64 vqs[DIV_ROUND_UP(SVE_VQ_MAX - SVE_VQ_MIN + 1, 64)];
245+
u64 vqs[KVM_ARM64_SVE_VLS_WORDS];
247246

248247
if (!vcpu_has_sve(vcpu))
249248
return -ENOENT;

0 commit comments

Comments
 (0)