Skip to content

Commit f0376ed

Browse files
stevenprice-armMarc Zyngier
authored andcommitted
KVM: arm64: Add ioctl to fetch/store tags in a guest
The VMM may not wish to have it's own mapping of guest memory mapped with PROT_MTE because this causes problems if the VMM has tag checking enabled (the guest controls the tags in physical RAM and it's unlikely the tags are correct for the VMM). Instead add a new ioctl which allows the VMM to easily read/write the tags from guest memory, allowing the VMM's mapping to be non-PROT_MTE while the VMM can still read/write the tags for the purpose of migration. Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Signed-off-by: Steven Price <steven.price@arm.com> Signed-off-by: Marc Zyngier <maz@kernel.org> Link: https://lore.kernel.org/r/20210621111716.37157-6-steven.price@arm.com
1 parent 673638f commit f0376ed

6 files changed

Lines changed: 105 additions & 0 deletions

File tree

arch/arm64/include/asm/kvm_host.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,9 @@ int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
730730
int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
731731
struct kvm_device_attr *attr);
732732

733+
long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
734+
struct kvm_arm_copy_mte_tags *copy_tags);
735+
733736
/* Guest/host FPSIMD coordination helpers */
734737
int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
735738
void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);

arch/arm64/include/asm/mte-def.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#define MTE_GRANULE_SIZE UL(16)
99
#define MTE_GRANULE_MASK (~(MTE_GRANULE_SIZE - 1))
10+
#define MTE_GRANULES_PER_PAGE (PAGE_SIZE / MTE_GRANULE_SIZE)
1011
#define MTE_TAG_SHIFT 56
1112
#define MTE_TAG_SIZE 4
1213
#define MTE_TAG_MASK GENMASK((MTE_TAG_SHIFT + (MTE_TAG_SIZE - 1)), MTE_TAG_SHIFT)

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ struct kvm_vcpu_events {
184184
__u32 reserved[12];
185185
};
186186

187+
struct kvm_arm_copy_mte_tags {
188+
__u64 guest_ipa;
189+
__u64 length;
190+
void __user *addr;
191+
__u64 flags;
192+
__u64 reserved[2];
193+
};
194+
195+
#define KVM_ARM_TAGS_TO_GUEST 0
196+
#define KVM_ARM_TAGS_FROM_GUEST 1
197+
187198
/* If you need to interpret the index values, here is the key: */
188199
#define KVM_REG_ARM_COPROC_MASK 0x000000000FFF0000
189200
#define KVM_REG_ARM_COPROC_SHIFT 16

arch/arm64/kvm/arm.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,13 @@ long kvm_arch_vm_ioctl(struct file *filp,
13591359

13601360
return 0;
13611361
}
1362+
case KVM_ARM_MTE_COPY_TAGS: {
1363+
struct kvm_arm_copy_mte_tags copy_tags;
1364+
1365+
if (copy_from_user(&copy_tags, argp, sizeof(copy_tags)))
1366+
return -EFAULT;
1367+
return kvm_vm_ioctl_mte_copy_tags(kvm, &copy_tags);
1368+
}
13621369
default:
13631370
return -EINVAL;
13641371
}

arch/arm64/kvm/guest.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,3 +995,85 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
995995

996996
return ret;
997997
}
998+
999+
long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
1000+
struct kvm_arm_copy_mte_tags *copy_tags)
1001+
{
1002+
gpa_t guest_ipa = copy_tags->guest_ipa;
1003+
size_t length = copy_tags->length;
1004+
void __user *tags = copy_tags->addr;
1005+
gpa_t gfn;
1006+
bool write = !(copy_tags->flags & KVM_ARM_TAGS_FROM_GUEST);
1007+
int ret = 0;
1008+
1009+
if (!kvm_has_mte(kvm))
1010+
return -EINVAL;
1011+
1012+
if (copy_tags->reserved[0] || copy_tags->reserved[1])
1013+
return -EINVAL;
1014+
1015+
if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST)
1016+
return -EINVAL;
1017+
1018+
if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK)
1019+
return -EINVAL;
1020+
1021+
gfn = gpa_to_gfn(guest_ipa);
1022+
1023+
mutex_lock(&kvm->slots_lock);
1024+
1025+
while (length > 0) {
1026+
kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL);
1027+
void *maddr;
1028+
unsigned long num_tags;
1029+
struct page *page;
1030+
1031+
if (is_error_noslot_pfn(pfn)) {
1032+
ret = -EFAULT;
1033+
goto out;
1034+
}
1035+
1036+
page = pfn_to_online_page(pfn);
1037+
if (!page) {
1038+
/* Reject ZONE_DEVICE memory */
1039+
ret = -EFAULT;
1040+
goto out;
1041+
}
1042+
maddr = page_address(page);
1043+
1044+
if (!write) {
1045+
if (test_bit(PG_mte_tagged, &page->flags))
1046+
num_tags = mte_copy_tags_to_user(tags, maddr,
1047+
MTE_GRANULES_PER_PAGE);
1048+
else
1049+
/* No tags in memory, so write zeros */
1050+
num_tags = MTE_GRANULES_PER_PAGE -
1051+
clear_user(tags, MTE_GRANULES_PER_PAGE);
1052+
kvm_release_pfn_clean(pfn);
1053+
} else {
1054+
num_tags = mte_copy_tags_from_user(maddr, tags,
1055+
MTE_GRANULES_PER_PAGE);
1056+
kvm_release_pfn_dirty(pfn);
1057+
}
1058+
1059+
if (num_tags != MTE_GRANULES_PER_PAGE) {
1060+
ret = -EFAULT;
1061+
goto out;
1062+
}
1063+
1064+
/* Set the flag after checking the write completed fully */
1065+
if (write)
1066+
set_bit(PG_mte_tagged, &page->flags);
1067+
1068+
gfn++;
1069+
tags += num_tags;
1070+
length -= PAGE_SIZE;
1071+
}
1072+
1073+
out:
1074+
mutex_unlock(&kvm->slots_lock);
1075+
/* If some data has been copied report the number of bytes copied */
1076+
if (length != copy_tags->length)
1077+
return copy_tags->length - length;
1078+
return ret;
1079+
}

include/uapi/linux/kvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,7 @@ struct kvm_s390_ucas_mapping {
14291429
/* Available with KVM_CAP_PMU_EVENT_FILTER */
14301430
#define KVM_SET_PMU_EVENT_FILTER _IOW(KVMIO, 0xb2, struct kvm_pmu_event_filter)
14311431
#define KVM_PPC_SVM_OFF _IO(KVMIO, 0xb3)
1432+
#define KVM_ARM_MTE_COPY_TAGS _IOR(KVMIO, 0xb4, struct kvm_arm_copy_mte_tags)
14321433

14331434
/* ioctl for vm fd */
14341435
#define KVM_CREATE_DEVICE _IOWR(KVMIO, 0xe0, struct kvm_create_device)

0 commit comments

Comments
 (0)