History

KVM: Introduce per-page memory attributes

In confidential computing usages, whether a page is private or shared is necessary information for KVM to perform operations like page fault handling, page zapping etc. There are other potential use cases for per-page memory attributes, e.g. to make memory read-only (or no-exec, or exec-only, etc.) without having to modify memslots. Introduce the KVM_SET_MEMORY_ATTRIBUTES ioctl, advertised by KVM_CAP_MEMORY_ATTRIBUTES, to allow userspace to set the per-page memory attributes to a guest memory range. Use an xarray to store the per-page attributes internally, with a naive, not fully optimized implementation, i.e. prioritize correctness over performance for the initial implementation. Use bit 3 for the PRIVATE attribute so that KVM can use bits 0-2 for RWX attributes/protections in the future, e.g. to give userspace fine-grained control over read, write, and execute protections for guest memory. Provide arch hooks for handling attribute changes before and after common code sets the new attributes, e.g. x86 will use the "pre" hook to zap all relevant mappings, and the "post" hook to track whether or not hugepages can be used to map the range. To simplify the implementation wrap the entire sequence with kvm_mmu_invalidate_{begin,end}() even though the operation isn't strictly guaranteed to be an invalidation. For the initial use case, x86 *will* always invalidate memory, and preventing arch code from creating new mappings while the attributes are in flux makes it much easier to reason about the correctness of consuming attributes. It's possible that future usages may not require an invalidation, e.g. if KVM ends up supporting RWX protections and userspace grants _more_ protections, but again opt for simplicity and punt optimizations to if/when they are needed.

Chao Peng committed 3 years ago

Commit 5a475551 parent 193bbfa5 files · +288 −0Browse files

Showing the changes to include/uapi (1 of 5 files). Show all

Unified · Split

include/uapi/linux/kvm.h+13−0
@@ -1220,6 +1220,7 @@ struct kvm_ppc_resize_hpt { · 1219 unmodified lines
1220#define KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES 2301220#define KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES 230
1221#define KVM_CAP_USER_MEMORY2 2311221#define KVM_CAP_USER_MEMORY2 231
1222#define KVM_CAP_MEMORY_FAULT_INFO 2321222#define KVM_CAP_MEMORY_FAULT_INFO 232
1223#define KVM_CAP_MEMORY_ATTRIBUTES 233
1223 1224
1224#ifdef KVM_CAP_IRQ_ROUTING1225#ifdef KVM_CAP_IRQ_ROUTING
1225 1226
@@ -2288,4 +2289,16 @@ struct kvm_s390_zpci_op { · 1062 unmodified lines
2288/* flags for kvm_s390_zpci_op->u.reg_aen.flags */2289/* flags for kvm_s390_zpci_op->u.reg_aen.flags */
2289#define KVM_S390_ZPCIOP_REGAEN_HOST (1 << 0)2290#define KVM_S390_ZPCIOP_REGAEN_HOST (1 << 0)
2290 2291
2292/* Available with KVM_CAP_MEMORY_ATTRIBUTES */
2293#define KVM_SET_MEMORY_ATTRIBUTES _IOW(KVMIO, 0xd2, struct kvm_memory_attributes)
2294
2295struct kvm_memory_attributes {
2296 __u64 address;
2297 __u64 size;
2298 __u64 attributes;
2299 __u64 flags;
2300};
2301
2302#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3)
2303
2291#endif /* __LINUX_KVM_H */2304#endif /* __LINUX_KVM_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t