From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F898C27C47 for ; Fri, 27 Oct 2023 18:23:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4CCC80015; Fri, 27 Oct 2023 14:22:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD3C78000C; Fri, 27 Oct 2023 14:22:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A173780015; Fri, 27 Oct 2023 14:22:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 875FD8000C for ; Fri, 27 Oct 2023 14:22:54 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6036B1CB290 for ; Fri, 27 Oct 2023 18:22:54 +0000 (UTC) X-FDA: 81392062668.08.FA2A288 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf20.hostedemail.com (Postfix) with ESMTP id 5DCC71C0011 for ; Fri, 27 Oct 2023 18:22:52 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=kXp5Atak; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3-_87ZQYKCBwK62FB48GG8D6.4GEDAFMP-EECN24C.GJ8@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3-_87ZQYKCBwK62FB48GG8D6.4GEDAFMP-EECN24C.GJ8@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430972; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PHuKmbJK++WVHRTr2ixGhZnz3PjglwNcwDhJuJ6xsm0=; b=b3td16VUvnJ2hI3CHuGtl8x3SsTGqeedzC6CKMlzHQKn4UqXa29FitGBdvMkzlWhAZvwL3 t1badf4AidkmWPORH91mJgZg/L/RYUEND560T3jAoJqcSylRycw7QYo8nqMo8oP1BQ+yuf et6kXT9rqKiNtVbN8YSD5UxG1307B4U= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=kXp5Atak; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3-_87ZQYKCBwK62FB48GG8D6.4GEDAFMP-EECN24C.GJ8@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3-_87ZQYKCBwK62FB48GG8D6.4GEDAFMP-EECN24C.GJ8@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430972; a=rsa-sha256; cv=none; b=yuOI51R1Ul5aQcyDYVmQg3aB1IXDZnxIQxjInzGLtWSPOpTlqsPZQxh4ZCJ5c28BPmG7Z9 j4GseIW3DDwwRXcsnjJZXaD8ExJ/eaXsPLdHrGKNHLnrTLP3anCgL84LIw1cqzKPDOAn8c iGNyEXgNKgCcEcXZ30uuhXiRLJ7LGQ8= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5a7bbe0a453so21184367b3.0 for ; Fri, 27 Oct 2023 11:22:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430971; x=1699035771; darn=kvack.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:from:to:cc:subject:date :message-id:reply-to; bh=PHuKmbJK++WVHRTr2ixGhZnz3PjglwNcwDhJuJ6xsm0=; b=kXp5AtakBMDdqWMJqlkWXn2FbwujSbkAo2BT5jjt/vpaciOTrvbtJjDeRF7Tz9Kwg1 gByVoW4OzxyDi1DjqnrLukmo+u3A1tRRzBtnCHDWEgN3Z4ZDhqcEm2AURYgDhA5RVnJX sQLawZXL86of97TeIR3z4znfjvgGH2z1Ywmqg/5s6P08Z5vyvN7ZQCJAIvhlD1WV/gON QifPRmnazGV5f5AmhCXLhmJLmXo97fypjukSoTXgLv7ftbRrIOCRsMvheiVkaXJtlU0U BAsvYblHNnE341sPJHOE9TmLOK8D3my07yjyGCTAD/k5tJu3W0Ed6W9IvMry4y+ug0sB Tvyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430971; x=1699035771; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=PHuKmbJK++WVHRTr2ixGhZnz3PjglwNcwDhJuJ6xsm0=; b=S1URrOQ/fVunb86fPK3LvRsGCm+1z5Jo2qyJnyMEuPxOQCRZMMVTMlxDc0kZkb5t5K /D/wG1t34zi2onBfBe0OUzMYxu1eY8Ww5PByf60aQtl/+isK8FhkL20PjQtzhpZ46OAf OUJ0OWrixZug3wjb9Bu87esXrhrTFHZyllIqICr1paw6S30hEq21RbB4PfVEUT4vUz+q Hz7+klwtr3yF3GQU9lqT0V927b2PM1TJhcJFGCXOxUhoszsMtQ/1ugfWJUZmao8rMhoX y4nNXtNBnHEi1p1JQef7BaZ1yt4PqQAx6bREVP/djBg8btqkRvs+WpzuPVRpJdTiMXWJ nmng== X-Gm-Message-State: AOJu0YzTvzEjc98CTwcjcNIKUpFN3YrVqvEijDbh+y2miOgeTR9au1AU tlzaxSSwhzr20iZ/lb5yvdSocFPBrAE= X-Google-Smtp-Source: AGHT+IHeAi8c5J1JHmkq3P4PpsdNdjX6Wq3XK5NOfAhf9NwJS3eapFdTXpLG2igv2I31VgMM7Gn14gWqO18= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:ca97:0:b0:5a1:d4a5:7dff with SMTP id m145-20020a0dca97000000b005a1d4a57dffmr73743ywd.6.1698430971510; Fri, 27 Oct 2023 11:22:51 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:55 -0700 In-Reply-To: <20231027182217.3615211-1-seanjc@google.com> Mime-Version: 1.0 References: <20231027182217.3615211-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.820.g83a721a137-goog Message-ID: <20231027182217.3615211-14-seanjc@google.com> Subject: [PATCH v13 13/35] KVM: Introduce per-page memory attributes From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , "=?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?=" , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 5DCC71C0011 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: f9g8m9m6gss9s9c4o7jzejgmpbtkumqu X-HE-Tag: 1698430972-508768 X-HE-Meta: U2FsdGVkX1/44VEGCrB28XVMrQ/XprIUt1kDSXYOVlGQMLediJ+oDZrTTxBnJtEFKsg6f1IB8l3iPtCangJVZ+jRrNKcf0RwdsfYp6wLLGDre2DAh3sOJzrC0nhmRc90y50GONppCbGw5ddhewiC69MswPHumwdnbcn35zi+nk/aagrkrAl7oETrPXK5th177Cj4wFcydGvy4laZp8zMwWf/5NvEuBnegX27po3y0IJ66h/6Po9bjbNL6iK96CEM+Dw8GRraTGNbvSTeuqG/az1vaHHIR2h70ZAYyQzcf0tYCUzBhPXIkD5a4VcNx5fRkDVMKWrply0PThSDmtw/dkM3hxiz46T3DB/AXmVRVNowbkaptBXmLPkBZHcOBJ41JVdy8ZKzPRQABiWQ2TDBy6TxaRRW1xD9dEAKtgvqZVI1qHrm6R2E+eGUoJiIH/ToYi+pwg61EAxg9gFzOt/heSIQfNofUo6vAleBd7LRap69kMcmZz1g+2Yvo+Dgv/+0uA5eE2ZaF4dkTefOYRSwKdoNlLJD9clWOorr4mhj258qO+kCyvhiRlDlUYib9LYhQZUAhnS+62Pds7GhQZmcHq6YPra3z9wjq5T7n9CGvvNfwllP1bOEfMkT8AOZw/yQw2eRfSd3dJxAV4teDsjcvC8j0Wj4uSisHVXdO0c5DoP7daQC+rwqS6N4y8tVwnnlPmE1yAcZ1sD3cD7xM+EzlbwEPcm4AgdPSSdmGFewQqRbH1fUMc6aqs2fZMBIgrttkgQuYHmu2pnYR2NfWn+5htqM5/bmADHN+lRvRmR53+RroiL7xwNoI0PwU8FnSNZ+uyYqbF5rqT9ibiAFeUyG8YWY9WHNxpKfpnqhY8lFIW/b5sxJnVmFzEW3FKbQBXVjtPnMku8x+Ox2ZIujbK2+eXQCcyd+08zObM4/c6WUlWlGbcdY8jfDfzXqWhDoVsYdZlPSpJDZek2xeDSkpfO 2u0/ZA0m SvswblHExLR1pURj0aIBaEPo5wIm2/JVI405Xd+QVq3mdgy94alx32zFfufBiKHI6GeoiSQkTtgyk6ja0kNhTYY+gFoGRMF63SkGmCRNqbJ6e3jLPi8Aj1ySa6FwlFc10aehornvQkhcEfPSX62Kr5B6c/Rn4nvxe5EaOIbutCueDR+89OiKGVPUQa68MlZWjGszmJuR9BCWW3V9i4Q0vCeNJDdMmSShNFBLBHrMf4ZSwzJRNKJigsaQio7a5VxvMJUoM1e7/ch39o2nhA2BhID/adsJ3/UCF4mnQVVLgtlPT5o9GsFbk2VMlQK8DU7rxd/FGw4h2b5nZGWZ6OurpjtdLTuC7nl7x2Dkgmr+EpiH84Tay8Sbfp0G4xk60uHlGMr8t8UJtxHI7Xs/iODK/HiNCRhDp1jhSAGVYQoViXO3/kpxw4HBQ1BTYo7Cwsor0mQksWaaPmYWbN8rxR4hb5PcJyO7lF4OEgvBFItDa+kiUP+IP3ElmRKL21Vz8Q1fber3KOS/kyyrw7y1/xBszRypjkLOg3CRN77fbYswNH3qxdNuuPHzCuDbkSIQ6GD294L9og06+ZDFbbdqHtEE9F0c8L+L+kGnlrjdHrJ2DBMeKL0QmVgBVnzn66rI05OW9bAhHdQOae5f0mT+TwtyeV2sTG6+2HpLO2vwu7bcfeS/OqntpachfQAJAIO/tYpcY7EXvG75JrBw9rY5x3tdg+uKqy8IHi+MG1CJ9WT07piNYlOcnGu0RVhnFDuMXGgdxbiyVGd4mHxiYd9HzBEmTBQLPelXQoZDQzC0xbdzSSUW9FEfjBfZrwbp1o8agvWx8t/YxmiJVaRF7o7X10Sr30DXX57mYLWa2q1s+f9zxYp3J4mAheCZGhpkmNbh8w5r7wU01q5iTU/CUQJbZr0V7izbbJGNAipPrfKiF X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chao Peng 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 two ioctls (advertised by KVM_CAP_MEMORY_ATTRIBUTES) to allow userspace to operate on the per-page memory attributes. - KVM_SET_MEMORY_ATTRIBUTES to set the per-page memory attributes to a guest memory range. - KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES to return the KVM supported memory attributes. 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. Suggested-by: Sean Christopherson Link: https://lore.kernel.org/all/Y2WB48kD0J4VGynX@google.com Cc: Fuad Tabba Cc: Xu Yilun Cc: Micka=C3=ABl Sala=C3=BCn Signed-off-by: Chao Peng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- Documentation/virt/kvm/api.rst | 36 +++++ include/linux/kvm_host.h | 18 +++ include/uapi/linux/kvm.h | 13 ++ virt/kvm/Kconfig | 4 + virt/kvm/kvm_main.c | 233 +++++++++++++++++++++++++++++++++ 5 files changed, 304 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rs= t index 860216536810..e2252c748fd6 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6091,6 +6091,42 @@ applied. =20 See KVM_SET_USER_MEMORY_REGION. =20 +4.140 KVM_SET_MEMORY_ATTRIBUTES +------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_memory_attributes(in) +:Returns: 0 on success, <0 on error + +KVM_SET_MEMORY_ATTRIBUTES allows userspace to set memory attributes for a = range +of guest physical memory. + +:: + + struct kvm_memory_attributes { + __u64 address; + __u64 size; + __u64 attributes; + __u64 flags; + }; + + #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) + +The address and size must be page aligned. The supported attributes can b= e +retrieved via ioctl(KVM_CHECK_EXTENSION) on KVM_CAP_MEMORY_ATTRIBUTES. If +executed on a VM, KVM_CAP_MEMORY_ATTRIBUTES precisely returns the attribut= es +supported by that VM. If executed at system scope, KVM_CAP_MEMORY_ATTRIBU= TES +returns all attributes supported by KVM. The only attribute defined at th= is +time is KVM_MEMORY_ATTRIBUTE_PRIVATE, which marks the associated gfn as be= ing +guest private memory. + +Note, there is no "get" API. Userspace is responsible for explicitly trac= king +the state of a gfn/page as needed. + +The "flags" field is reserved for future extensions and must be '0'. + 5. The kvm_run structure =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 89c1a991a3b8..df573229651b 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -256,6 +256,7 @@ int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu); #ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER union kvm_mmu_notifier_arg { pte_t pte; + unsigned long attributes; }; =20 struct kvm_gfn_range { @@ -808,6 +809,9 @@ struct kvm { =20 #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier; +#endif +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + struct xarray mem_attr_array; #endif char stats_id[KVM_STATS_NAME_SIZE]; }; @@ -2340,4 +2344,18 @@ static inline void kvm_prepare_memory_fault_exit(str= uct kvm_vcpu *vcpu, vcpu->run->memory_fault.flags =3D 0; } =20 +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn= _t gfn) +{ + return xa_to_value(xa_load(&kvm->mem_attr_array, gfn)); +} + +bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t e= nd, + unsigned long attrs); +bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range); +bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range); +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 7ae9987b48dd..547837feaa28 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1211,6 +1211,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229 #define KVM_CAP_USER_MEMORY2 230 #define KVM_CAP_MEMORY_FAULT_INFO 231 +#define KVM_CAP_MEMORY_ATTRIBUTES 232 =20 #ifdef KVM_CAP_IRQ_ROUTING =20 @@ -2277,4 +2278,16 @@ struct kvm_s390_zpci_op { /* flags for kvm_s390_zpci_op->u.reg_aen.flags */ #define KVM_S390_ZPCIOP_REGAEN_HOST (1 << 0) =20 +/* Available with KVM_CAP_MEMORY_ATTRIBUTES */ +#define KVM_SET_MEMORY_ATTRIBUTES _IOW(KVMIO, 0xd2, struct k= vm_memory_attributes) + +struct kvm_memory_attributes { + __u64 address; + __u64 size; + __u64 attributes; + __u64 flags; +}; + +#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index ecae2914c97e..5bd7fcaf9089 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -96,3 +96,7 @@ config KVM_GENERIC_HARDWARE_ENABLING config KVM_GENERIC_MMU_NOTIFIER select MMU_NOTIFIER bool + +config KVM_GENERIC_MEMORY_ATTRIBUTES + select KVM_GENERIC_MMU_NOTIFIER + bool diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 302ccb87b4c1..78a0b09ef2a5 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1218,6 +1218,9 @@ static struct kvm *kvm_create_vm(unsigned long type, = const char *fdname) spin_lock_init(&kvm->mn_invalidate_lock); rcuwait_init(&kvm->mn_memslots_update_rcuwait); xa_init(&kvm->vcpu_array); +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + xa_init(&kvm->mem_attr_array); +#endif =20 INIT_LIST_HEAD(&kvm->gpc_list); spin_lock_init(&kvm->gpc_lock); @@ -1398,6 +1401,9 @@ static void kvm_destroy_vm(struct kvm *kvm) } cleanup_srcu_struct(&kvm->irq_srcu); cleanup_srcu_struct(&kvm->srcu); +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + xa_destroy(&kvm->mem_attr_array); +#endif kvm_arch_free_vm(kvm); preempt_notifier_dec(); hardware_disable_all(); @@ -2396,6 +2402,210 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm = *kvm, } #endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ =20 +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +/* + * Returns true if _all_ gfns in the range [@start, @end) have attributes + * matching @attrs. + */ +bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t e= nd, + unsigned long attrs) +{ + XA_STATE(xas, &kvm->mem_attr_array, start); + unsigned long index; + bool has_attrs; + void *entry; + + rcu_read_lock(); + + if (!attrs) { + has_attrs =3D !xas_find(&xas, end - 1); + goto out; + } + + has_attrs =3D true; + for (index =3D start; index < end; index++) { + do { + entry =3D xas_next(&xas); + } while (xas_retry(&xas, entry)); + + if (xas.xa_index !=3D index || xa_to_value(entry) !=3D attrs) { + has_attrs =3D false; + break; + } + } + +out: + rcu_read_unlock(); + return has_attrs; +} + +static u64 kvm_supported_mem_attributes(struct kvm *kvm) +{ + if (!kvm) + return KVM_MEMORY_ATTRIBUTE_PRIVATE; + + return 0; +} + +static __always_inline void kvm_handle_gfn_range(struct kvm *kvm, + struct kvm_mmu_notifier_range *range) +{ + struct kvm_gfn_range gfn_range; + struct kvm_memory_slot *slot; + struct kvm_memslots *slots; + struct kvm_memslot_iter iter; + bool found_memslot =3D false; + bool ret =3D false; + int i; + + gfn_range.arg =3D range->arg; + gfn_range.may_block =3D range->may_block; + + /* + * If/when KVM supports more attributes beyond private .vs shared, this + * _could_ set only_{private,shared} appropriately if the entire target + * range already has the desired private vs. shared state (it's unclear + * if that is a net win). For now, KVM reaches this point if and only + * if the private flag is being toggled, i.e. all mappings are in play. + */ + gfn_range.only_private =3D false; + gfn_range.only_shared =3D false; + + for (i =3D 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + slots =3D __kvm_memslots(kvm, i); + + kvm_for_each_memslot_in_gfn_range(&iter, slots, range->start, range->end= ) { + slot =3D iter.slot; + gfn_range.slot =3D slot; + + gfn_range.start =3D max(range->start, slot->base_gfn); + gfn_range.end =3D min(range->end, slot->base_gfn + slot->npages); + if (gfn_range.start >=3D gfn_range.end) + continue; + + if (!found_memslot) { + found_memslot =3D true; + KVM_MMU_LOCK(kvm); + if (!IS_KVM_NULL_FN(range->on_lock)) + range->on_lock(kvm); + } + + ret |=3D range->handler(kvm, &gfn_range); + } + } + + if (range->flush_on_ret && ret) + kvm_flush_remote_tlbs(kvm); + + if (found_memslot) + KVM_MMU_UNLOCK(kvm); +} + +static bool kvm_pre_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range) +{ + /* + * Unconditionally add the range to the invalidation set, regardless of + * whether or not the arch callback actually needs to zap SPTEs. E.g. + * if KVM supports RWX attributes in the future and the attributes are + * going from R=3D>RW, zapping isn't strictly necessary. Unconditionally + * adding the range allows KVM to require that MMU invalidations add at + * least one range between begin() and end(), e.g. allows KVM to detect + * bugs where the add() is missed. Rexlaing the rule *might* be safe, + * but it's not obvious that allowing new mappings while the attributes + * are in flux is desirable or worth the complexity. + */ + kvm_mmu_invalidate_range_add(kvm, range->start, range->end); + + return kvm_arch_pre_set_memory_attributes(kvm, range); +} + +/* Set @attributes for the gfn range [@start, @end). */ +static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t e= nd, + unsigned long attributes) +{ + struct kvm_mmu_notifier_range pre_set_range =3D { + .start =3D start, + .end =3D end, + .handler =3D kvm_pre_set_memory_attributes, + .on_lock =3D kvm_mmu_invalidate_begin, + .flush_on_ret =3D true, + .may_block =3D true, + }; + struct kvm_mmu_notifier_range post_set_range =3D { + .start =3D start, + .end =3D end, + .arg.attributes =3D attributes, + .handler =3D kvm_arch_post_set_memory_attributes, + .on_lock =3D kvm_mmu_invalidate_end, + .may_block =3D true, + }; + unsigned long i; + void *entry; + int r =3D 0; + + entry =3D attributes ? xa_mk_value(attributes) : NULL; + + mutex_lock(&kvm->slots_lock); + + /* Nothing to do if the entire range as the desired attributes. */ + if (kvm_range_has_memory_attributes(kvm, start, end, attributes)) + goto out_unlock; + + /* + * Reserve memory ahead of time to avoid having to deal with failures + * partway through setting the new attributes. + */ + for (i =3D start; i < end; i++) { + r =3D xa_reserve(&kvm->mem_attr_array, i, GFP_KERNEL_ACCOUNT); + if (r) + goto out_unlock; + } + + kvm_handle_gfn_range(kvm, &pre_set_range); + + for (i =3D start; i < end; i++) { + r =3D xa_err(xa_store(&kvm->mem_attr_array, i, entry, + GFP_KERNEL_ACCOUNT)); + KVM_BUG_ON(r, kvm); + } + + kvm_handle_gfn_range(kvm, &post_set_range); + +out_unlock: + mutex_unlock(&kvm->slots_lock); + + return r; +} +static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, + struct kvm_memory_attributes *attrs) +{ + gfn_t start, end; + + /* flags is currently not used. */ + if (attrs->flags) + return -EINVAL; + if (attrs->attributes & ~kvm_supported_mem_attributes(kvm)) + return -EINVAL; + if (attrs->size =3D=3D 0 || attrs->address + attrs->size < attrs->address= ) + return -EINVAL; + if (!PAGE_ALIGNED(attrs->address) || !PAGE_ALIGNED(attrs->size)) + return -EINVAL; + + start =3D attrs->address >> PAGE_SHIFT; + end =3D (attrs->address + attrs->size) >> PAGE_SHIFT; + + /* + * xarray tracks data using "unsigned long", and as a result so does + * KVM. For simplicity, supports generic attributes only on 64-bit + * architectures. + */ + BUILD_BUG_ON(sizeof(attrs->attributes) !=3D sizeof(unsigned long)); + + return kvm_vm_set_mem_attributes(kvm, start, end, attrs->attributes); +} +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ + struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) { return __gfn_to_memslot(kvm_memslots(kvm), gfn); @@ -4640,6 +4850,17 @@ static int kvm_vm_ioctl_check_extension_generic(stru= ct kvm *kvm, long arg) case KVM_CAP_BINARY_STATS_FD: case KVM_CAP_SYSTEM_EVENT_DATA: return 1; +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + case KVM_CAP_MEMORY_ATTRIBUTES: + u64 attrs =3D kvm_supported_mem_attributes(kvm); + + r =3D -EFAULT; + if (copy_to_user(argp, &attrs, sizeof(attrs))) + goto out; + r =3D 0; + break; + } +#endif default: break; } @@ -5022,6 +5243,18 @@ static long kvm_vm_ioctl(struct file *filp, break; } #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */ +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + case KVM_SET_MEMORY_ATTRIBUTES: { + struct kvm_memory_attributes attrs; + + r =3D -EFAULT; + if (copy_from_user(&attrs, argp, sizeof(attrs))) + goto out; + + r =3D kvm_vm_ioctl_set_mem_attributes(kvm, &attrs); + break; + } +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ case KVM_CREATE_DEVICE: { struct kvm_create_device cd; =20 --=20 2.42.0.820.g83a721a137-goog