From patchwork Thu Sep 14 01:55:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384014 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 48B68EE0203 for ; Thu, 14 Sep 2023 01:55:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 951046B028F; Wed, 13 Sep 2023 21:55:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FDF96B0297; Wed, 13 Sep 2023 21:55:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7779E6B0299; Wed, 13 Sep 2023 21:55:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 601F56B028F for ; Wed, 13 Sep 2023 21:55:53 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3697DB4209 for ; Thu, 14 Sep 2023 01:55:53 +0000 (UTC) X-FDA: 81233536986.22.4106033 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf26.hostedemail.com (Postfix) with ESMTP id 544F814000A for ; Thu, 14 Sep 2023 01:55:51 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=F+eEFcb9; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3JmgCZQYKCC8dPLYUNRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--seanjc.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3JmgCZQYKCC8dPLYUNRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656551; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=o3ncWp87Xrx57ugLKytKkEWMncW9euHhZSqjHZ5fZmU=; b=uTo1XvlKLCaqmdDsJCmLfVAFNMe1ppG9VEdIgPBzVUwVZh2oHTtV72h/i2upN6vWtI7fr/ N8bHHvUoIEZNQS16rAm9QQLfREfDW3sxBWzDBfzlPg60KCIeBoFaBUGAzLL1yh/t0D4Tst LbsVtUXv+4c5OdAdPu8mvmGxYw2JxfQ= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=F+eEFcb9; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3JmgCZQYKCC8dPLYUNRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--seanjc.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3JmgCZQYKCC8dPLYUNRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656551; a=rsa-sha256; cv=none; b=kOL3S9ctLW0efn05/tsBV5CqbKNhHna5qe76qqGLD0olRuNwMbi5LMmIyy/waE0uUM7v4b QVr0as92RuTu7eQseehLSbsjHTbMQqvvsoGJHmGOt8cJc0JV6jy1icnaSt93WHBZJpm+Yw GApdNhUlLP91RmP0QdvRbXJI8P20LCc= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-26f6ed09f59so387953a91.1 for ; Wed, 13 Sep 2023 18:55:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656550; x=1695261350; darn=kvack.org; h=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=o3ncWp87Xrx57ugLKytKkEWMncW9euHhZSqjHZ5fZmU=; b=F+eEFcb9PIqfNv6ff2L48CzHszpRS+uaHc9s/hlNGutx1BjJN/rbT/T8JWdkT8B0W8 TzpKZAI5AVz5m7aVTJaJuFdyJMB5zrXKdwgOGWU98ZrDQfOFwLZhuVuF3KLcTtabomdq TOwSge/SonhJ4rNGQQVSRiDkVGVHeC6sOx3oUtpfmXTJyf3fKoYH+az5hFUond8NuZI+ fWIo8wp6LUuS2UOJHEpsu2nNisp12SL4aNOHOvYjFTmW5oLjFNXHkC12xcvCR+w67Dxa v9hhZwm2B0sFSTxbqBakBJfzgk9oxKXWpP628vXcwE/DwGN6acBWNNhbpufA4NzYlzm3 QasA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656550; x=1695261350; h=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=o3ncWp87Xrx57ugLKytKkEWMncW9euHhZSqjHZ5fZmU=; b=p1B06CV7+hbMfkMnuHPjCsT0HplcaoIJuvlvba/rjTRWyC6ilX1fEEVgeN3jtjwVrM eld0kibhRPIX1pHnEqfU2Rh5LWXfUHek+VQGhBpQxjZcd0QzCLOCw/2oOOxQBYpE8mlR ZCwAE8I30LkAXUEpl8fXKACwg6R55qHJngMmTw3PvgLyPZR4lNfpg1zePs7h/a6yLOMp 6Q3nR+Q2nsfHzpLQSrnu1eeN9wNAfJ5zBW5GznAFu5uJVdYghjGpukZ5VNxRWwueuEha fzQJUOjoVr6VAsAmjVmQXBh2AWb13a6acUovmjDvy/mWA1Z1NKZvO1Tvrh7Z2XlTExL9 u3+g== X-Gm-Message-State: AOJu0YxrM8UuvqVC/a4mCxIacWQQ1FoqStBHSjzM/6Zn3cIHxghLZQ/5 OkVd5+iHnyJB0miDBKz0RlDGPQzX9vA= X-Google-Smtp-Source: AGHT+IGMCwYjc3KV33cqzPB4diNP6tSuJVwUoEl1ZPejd5zU17fdiva4810hnuR1hhtSq7kLalb8uulnWEI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:6bca:b0:1b8:80c9:a98e with SMTP id m10-20020a1709026bca00b001b880c9a98emr149786plt.13.1694656550053; Wed, 13 Sep 2023 18:55:50 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:05 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-8-seanjc@google.com> Subject: [RFC PATCH v12 07/33] KVM: Add KVM_EXIT_MEMORY_FAULT exit to report faults to userspace From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" 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-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 544F814000A X-Stat-Signature: mzamneiz6kckziagdwnde85uppob7cix X-Rspam-User: X-HE-Tag: 1694656551-348228 X-HE-Meta: U2FsdGVkX1+JV1uxPzid0iuE682Xvxgb36DTy98qnqSHqdszcCLw93q7tRZV8eGjUkUnPcw0+Lrif7a5KVFpLKKPGITg5OpipFVauXjWSIxdA+Wp/BDgLOEqCocl6Rtqw9NzSGeQl/QURgyZz/odBLO76aFQqGHA7oLM5I7pg1yhhveu+e0/wAOdX5yaEbWu6zqDneXufRw78jA505rPsFoyMUCcHfR263emJaYhQIe/SGd6MmWy+dbllIUh6NHonjOu2gLF24GbKrrSx9GHPUKO9R+di9YPOjhTVugiWDMutHEMD7sehXRMKg8+4VJTBjo4J74yWu13d7jjqdW+OzBAD+BtCz3eBYE14EhiWsB7nAEP4hsr1UplOW2BU/9FRD31i6pnmlroUfx9yGcPSeKnCbNr9HwDYtihvA/mcAb1uUoW7/ONOy7OtvwxmaRX0LoOwb+e79BEgsBJXMfExKYd0Bc2a9cBSrWE8Hvwh6VQAM54ZCSXkQLQ188LjsSVHv4rdBBLjyyZEVfeMdcEbEVnk4XwEJHGgA2QIX4habgzjmtg9QrRU2n0p7cYQnp0bzlhPFJ0qU6ZaTAOX8lGAN5v6wOjJZMrUy6TQtlul9pi+7MPn0fKst+6MD+BeOkS/HIkhcfgIHZj0MeGASHrk3OVAhM8i7WlKwGtUIlkASw0/9WzCqbBI6NmIR4Ydq1gI3cYrbD6sLJREjyCw/Yhgin9UH8NIuzDgL0jN9Oaw+jR1TTCP4hbVb8DcjEYyYOq9yBbvHtLRRMICC15Gi2sK9JFYS8n4TxTYeGEF3XiKx+pMl/S9OC6Di1QXgb2xqp/pi1fXgJaLnLhVhGYIVXiP28iGsOak+I+BKcIDaE+M/XKfFwxdQ20NZUj85WjyhF38o2C9JLwRlS1aVyxGwp+JzhWjC4ObR6p305HHIsUHggV7XfxEW6xO+8CWjyoVmTaPoq+rWy0p0d6s+FloOK 1skF2TJm hvUSTvTdkJh39m2ghnCqTb/S76XSFSFBxKxS2sw6eCyf8+vaPEImUo8VMVxHWZTFL+b3Nc42DruVT5KK24/GGWXVheuf6vaVRqRVHJjIGM2HOtxhwLn33a6vNegmyR2vDp65jUaDCjTMBWgslpWAjjvlsRWv5AQMGpbh+ThTb/IOjMXoYuvW6LGBXEqyoBhntu6/63bQRTxm3c74E4N5HjNIH5/6v4tpP1IdQj/lngkJU7DMRFc/QE0Kge+yDz/PkWyYDuZ05w+r3ykj9101zFpcizL1SAiNOT9OFprKEpP7DqW64oEviBcM+axmkGNvmdk35t0EArZlmgR/CLFtWxdjBcP2XnjA9LwKdCnkN8ThII4OevEowAmNY10vmiv8a46Cy+eGdxxSaSJEzupf7np39zscc/8/UUKLd04o/ncrO96OKGEfKssdhJUYXw8CG8hmL0OrJ7kbcyvAGkBu8lffosUZCpq/0YsKjQsKBtcGoB/Z2JC3GJ5aP8ijkUp5HYe0AWIUc49UXWmv75sBZ8S4e848ZBQD/xLozavxxQue1Wb6Yd31T92O73tVkEo11o8NMJs0n2DbySYEjNkanICgR/3g5Avkn3r7npQXd3pdmuq5z0Cq9IElQJHcdQL9BTrNu5cqVGhBOy80eC9HsTryZaqNpVo+zgB3VKhaVijcJPqxY51KY3xHStYB4J33OE5aLXHv2g5CY+GwtRM3WLrArJ2IYSG+pi2+cZ2YR0fP7EvyNNXdxUGn2ZypxZSVokXeT39IdpcyUakG2z4HvZ6S888KfpNNA7JJnzS+Tbu4W2EelcoDAj9NT+qLWtydmg9ZmWeMEDhqWzz6NoZU2H4Lj2bEJmgzierzbwb5GslKqU77ugFfXPbKXjJM3PUQ6ZJTEb1H/owjWTzuwfgJcEul9KoDMic9GcjeG 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: From: Chao Peng Add a new KVM exit type to allow userspace to handle memory faults that KVM cannot resolve, but that userspace *may* be able to handle (without terminating the guest). KVM will initially use KVM_EXIT_MEMORY_FAULT to report implicit conversions between private and shared memory. With guest private memory, there will be two kind of memory conversions: - explicit conversion: happens when the guest explicitly calls into KVM to map a range (as private or shared) - implicit conversion: happens when the guest attempts to access a gfn that is configured in the "wrong" state (private vs. shared) On x86 (first architecture to support guest private memory), explicit conversions will be reported via KVM_EXIT_HYPERCALL+KVM_HC_MAP_GPA_RANGE, but reporting KVM_EXIT_HYPERCALL for implicit conversions is undesriable as there is (obviously) no hypercall, and there is no guarantee that the guest actually intends to convert between private and shared, i.e. what KVM thinks is an implicit conversion "request" could actually be the result of a guest code bug. KVM_EXIT_MEMORY_FAULT will be used to report memory faults that appear to be implicit conversions. Place "struct memory_fault" in a second anonymous union so that filling memory_fault doesn't clobber state from other yet-to-be-fulfilled exits, and to provide additional information if KVM does NOT ultimately exit to userspace with KVM_EXIT_MEMORY_FAULT, e.g. if KVM suppresses (or worse, loses) the exit, as KVM often suppresses exits for memory failures that occur when accessing paravirt data structures. The initial usage for private memory will be all-or-nothing, but other features such as the proposed "userfault on missing mappings" support will use KVM_EXIT_MEMORY_FAULT for potentially _all_ guest memory accesses, i.e. will run afoul of KVM's various quirks. Use bit 3 for flagging private memory so that KVM can use bits 0-2 for capturing RWX behavior if/when userspace needs such information. Note! To allow for future possibilities where KVM reports KVM_EXIT_MEMORY_FAULT and fills run->memory_fault on _any_ unresolved fault, KVM returns "-EFAULT" (-1 with errno == EFAULT from userspace's perspective), not '0'! Due to historical baggage within KVM, exiting to userspace with '0' from deep callstacks, e.g. in emulation paths, is infeasible as doing so would require a near-complete overhaul of KVM, whereas KVM already propagates -errno return codes to userspace even when the -errno originated in a low level helper. Link: https://lore.kernel.org/all/20230908222905.1321305-5-amoorthy@google.com Cc: Anish Moorthy Suggested-by: Sean Christopherson Co-developed-by: Yu Zhang Signed-off-by: Yu Zhang Signed-off-by: Chao Peng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- Documentation/virt/kvm/api.rst | 24 ++++++++++++++++++++++++ include/linux/kvm_host.h | 15 +++++++++++++++ include/uapi/linux/kvm.h | 24 ++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 21a7578142a1..e28a13439a95 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6702,6 +6702,30 @@ array field represents return values. The userspace should update the return values of SBI call before resuming the VCPU. For more details on RISC-V SBI spec refer, https://github.com/riscv/riscv-sbi-doc. +:: + + /* KVM_EXIT_MEMORY_FAULT */ + struct { + #define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) + __u64 flags; + __u64 gpa; + __u64 size; + } memory; + +KVM_EXIT_MEMORY_FAULT indicates the vCPU has encountered a memory fault that +could not be resolved by KVM. The 'gpa' and 'size' (in bytes) describe the +guest physical address range [gpa, gpa + size) of the fault. The 'flags' field +describes properties of the faulting access that are likely pertinent: + + - KVM_MEMORY_EXIT_FLAG_PRIVATE - When set, indicates the memory fault occurred + on a private memory access. When clear, indicates the fault occurred on a + shared access. + +Note! KVM_EXIT_MEMORY_FAULT is unique among all KVM exit reasons in that it +accompanies a return code of '-1', not '0'! errno will always be set to EFAULT +or EHWPOISON when KVM exits with KVM_EXIT_MEMORY_FAULT, userspace should assume +kvm_run.exit_reason is stale/undefined for all other error numbers. + :: /* KVM_EXIT_NOTIFY */ diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4e741ff27af3..d8c6ce6c8211 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2327,4 +2327,19 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr) /* Max number of entries allowed for each kvm dirty ring */ #define KVM_DIRTY_RING_MAX_ENTRIES 65536 +static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, + gpa_t gpa, gpa_t size, + bool is_write, bool is_exec, + bool is_private) +{ + vcpu->run->exit_reason = KVM_EXIT_MEMORY_FAULT; + vcpu->run->memory_fault.gpa = gpa; + vcpu->run->memory_fault.size = size; + + /* RWX flags are not (yet) defined or communicated to userspace. */ + vcpu->run->memory_fault.flags = 0; + if (is_private) + vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_PRIVATE; +} + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index bd1abe067f28..d2d913acf0df 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -274,6 +274,7 @@ struct kvm_xen_exit { #define KVM_EXIT_RISCV_SBI 35 #define KVM_EXIT_RISCV_CSR 36 #define KVM_EXIT_NOTIFY 37 +#define KVM_EXIT_MEMORY_FAULT 38 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -541,6 +542,29 @@ struct kvm_run { struct kvm_sync_regs regs; char padding[SYNC_REGS_SIZE_BYTES]; } s; + + /* + * This second exit union holds structs for exit types which may be + * triggered after KVM has already initiated a different exit, or which + * may be ultimately dropped by KVM. + * + * For example, because of limitations in KVM's uAPI, KVM x86 can + * generate a memory fault exit an MMIO exit is initiated (exit_reason + * and kvm_run.mmio are filled). And conversely, KVM often disables + * paravirt features if a memory fault occurs when accessing paravirt + * data instead of reporting the error to userspace. + */ + union { + /* KVM_EXIT_MEMORY_FAULT */ + struct { +#define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) + __u64 flags; + __u64 gpa; + __u64 size; + } memory_fault; + /* Fix the size of the union. */ + char padding2[256]; + }; }; /* for KVM_REGISTER_COALESCED_MMIO / KVM_UNREGISTER_COALESCED_MMIO */