From patchwork Tue Jul 18 23:44:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13317877 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 14369C001DC for ; Tue, 18 Jul 2023 23:48:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2649B8D0026; Tue, 18 Jul 2023 19:48:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 210FF8D0012; Tue, 18 Jul 2023 19:48:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 03AFE8D0026; Tue, 18 Jul 2023 19:48:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E979B8D0012 for ; Tue, 18 Jul 2023 19:48:47 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C85D01C8E51 for ; Tue, 18 Jul 2023 23:48:47 +0000 (UTC) X-FDA: 81026375094.25.CC82828 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf15.hostedemail.com (Postfix) with ESMTP id E68ADA0012 for ; Tue, 18 Jul 2023 23:48:45 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=fv5PAifQ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 33SS3ZAYKCDAeQMZVOSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=33SS3ZAYKCDAeQMZVOSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689724125; 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=YutdMElGbp9P5hSkuCpmC8VJ7V3PNb+s/AjU6M3aOE8=; b=gFlXHRE0qeBdDcCG3xyFMATFJ2VRi92ONg4ttcZIFrP1d8Z1Akzt0M/U4rvYMuWrmWudzd n6KmDJa2yYWJUFmO6Zyp1vcAzgAyrqUDOX05rEBJMI4er6t4B/U1wNM5n70dIscD/Rk06Q HfpliExnw926jlxjopQ0UE1BVcwXSG4= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=fv5PAifQ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 33SS3ZAYKCDAeQMZVOSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=33SS3ZAYKCDAeQMZVOSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689724126; a=rsa-sha256; cv=none; b=VAycRLyUtrZYdIcl9tccuj7y9ldzzYeQ6KN0bXgit7Le3rXbj3MMjeZeM7cUwlcTrt9Z27 0hrsJFnmSU+Vfrnnu/IV+VW/9WD1+47hhuJDEDfRYhvk4H7gGTVJf1Zn2AYqJvSmPRy7XW cvdanJ2bvMaC1iLijClDsxAXm0yS+/0= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-cac213f9264so6070885276.3 for ; Tue, 18 Jul 2023 16:48:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689724125; x=1692316125; 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=YutdMElGbp9P5hSkuCpmC8VJ7V3PNb+s/AjU6M3aOE8=; b=fv5PAifQ+IqLg5YCZqVv/8UfhbcgYKQcYF3VY0+jp+9AW0GEauEM9c/l2th/BfOtRm CQiXDrdFVD2W7gdnkrgGNuk1FWqnXZCui5KNHEJZNKFbYGE3o6y6cS/CRv3k2n8pR5iS sMPjf4zdwa9albkzvFhGKioR2FBoJS58rCVLaVO5jIXG18PShPMZQfFotlM0oiS44ak5 wH72KK42j+tXvl9XbOo8Q688HudYyAAS7uwccxs/g6IQguY5eI+gDO5TZMYBuh3X4bOG jik8MJDjJL4Dv3zLF48fb1grV3qG4y91bzqSazr3bySPVvnzIDpkEov2LrTDdj61DR8b pWpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689724125; x=1692316125; 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=YutdMElGbp9P5hSkuCpmC8VJ7V3PNb+s/AjU6M3aOE8=; b=k1OuEViSek2UtJmqN/qee1pyYygyB8eh/fYGTnMgA0In4Acz7qTd6Z/1CTR7rCVSLP Qrrj4sGpHsu+EGH9Sj/+ZbUNj+Bfmq6gjZjP03K0rrGBvv56gc99fgFMIttG//UKXkb/ v2D90JddSWAbI1pIXAQq+KVW4J7axDjScfptoCA06YolFgzd9qu7mi34aX3ryLtB5Vzd VOp/6gKEm29FHzuBpRBCNAothS+/gHeGzNhalW+jFyl9p80dWL9idcwMAef8oPjNyT5T UnuFUjvoHswGWs310nXKddpNJWGgWJAFBnexOeEaPRYUHpQJH3K4LXzdosWgpxa3O72w tnhA== X-Gm-Message-State: ABy/qLaK7G0JN1xOfBk3GChiAaBlxKlicM4Qj57sILH8oI7MVRNEf4we 9lrKfdnSrTiHOSJ20WCiZuzSo3q2REc= X-Google-Smtp-Source: APBJJlEBqRkJLxPhHZfq9dzn/bDkdLKPNX43lLfClkxec0b9fruWECEDkqvu4RpyrQH11Geekj2aMuiEVrw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:ab04:0:b0:bfe:ea69:91b4 with SMTP id u4-20020a25ab04000000b00bfeea6991b4mr11891ybi.4.1689724125035; Tue, 18 Jul 2023 16:48:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 18 Jul 2023 16:44:50 -0700 In-Reply-To: <20230718234512.1690985-1-seanjc@google.com> Mime-Version: 1.0 References: <20230718234512.1690985-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230718234512.1690985-8-seanjc@google.com> Subject: [RFC PATCH v11 07/29] KVM: Add KVM_EXIT_MEMORY_FAULT exit 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 , Yu Zhang , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , Vlastimil Babka , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: E68ADA0012 X-Rspam-User: X-Rspamd-Server: rspam02 X-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Q4hWYPLD6VDCikDco1US1B/FOFmx3nykE5KvrPcrMsLg36Pwykt7igWXMMMTINEROWFCzmLDzWezxsi0JGJw2HECtFGyj8g98iutkRo8yRJC34mu+uG3HvOnmqi/Sh5EzNz36STWEL6jd4GHz8vfZbc2HpZHHKgGKuWNYGqfRmq2jOi7UyXV8LIEzlF2YcF77NEVgLrwsEn64M9H2Zgdy1fEVqTcDDZJml47dqhGW7l2dq/9cF10/28F7HKQOgc/9LD4ByHOFkZZBL2ofWlNXMWW97iULtob5DbBahfNYSd9MHVjNYjjX/1DaPDXsaCmoBSVgs625yROZZyBc5AfWmBB3Av5W6vNrL2MIhn8fQAvXlpJuLe9zAsv5NlJc31SCPBBNaWbMufS5Z+xkBJ3d+2JkmAJe0SUOxDagVddaCzLPs8SKesS2JXZFeD+MU9gTEjn7L1PSCaZZFa+MFFib3roijKmOBaPmTY/t4TPYEFI6iLQcunxb84QCmi4DyYTtEYAbaOM6u1JpGEEN58/UdKsaIB/NBk2SK4Vq1yi7eIhmfyXYKqfQjDGXdvWgOnZqmWO8wEar/NEHVmf5pW2dMDHPV/dAkM9czuIyQIS5MVbYEzB+4Q5xuEhQFZst/74ikbFn3to00tWYUu8MEOHG1Oc3Sf67cLSr6uGsJG9HR0rfdQpECbD11/vWRAUBio5VwIXkxzFAhSniYNQXS3zHS58uTFD5ARLM8dkosrzQchFXGb5qlAgV3toOQv2iTHfD6sRrvrYWpuzYPoJvuwJ3bRvqy3laRcF6JRhxKRY9Zr3jG2/tIDqQHOQ/0rb9NCvWiQsKG47gbXRfZqZi/4= 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 This new KVM exit allows userspace to handle memory-related errors. It indicates an error happens in KVM at guest memory range [gpa, gpa+size). The flags includes additional information for userspace to handle the error. Currently bit 0 is defined as 'private memory' where '1' indicates error happens due to private memory access and '0' indicates error happens due to shared memory access. When private memory is enabled, this new exit will be used for KVM to exit to userspace for shared <-> private memory conversion in memory encryption usage. In such usage, typically there are two kind of memory conversions: - explicit conversion: happens when guest explicitly calls into KVM to map a range (as private or shared), KVM then exits to userspace to perform the map/unmap operations. - implicit conversion: happens in KVM page fault handler where KVM exits to userspace for an implicit conversion when the page is in a different state than requested (private or shared). Suggested-by: Sean Christopherson Co-developed-by: Yu Zhang Signed-off-by: Yu Zhang Signed-off-by: Chao Peng Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba Signed-off-by: Sean Christopherson --- Documentation/virt/kvm/api.rst | 22 ++++++++++++++++++++++ include/uapi/linux/kvm.h | 8 ++++++++ 2 files changed, 30 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index c0ddd3035462..34d4ce66e0c8 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6700,6 +6700,28 @@ 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; + +If exit reason is KVM_EXIT_MEMORY_FAULT then it indicates that the VCPU has +encountered a memory error which is not handled by KVM kernel module and +userspace may choose to handle it. The 'flags' field indicates the memory +properties of the exit. + + - KVM_MEMORY_EXIT_FLAG_PRIVATE - indicates the memory error is caused by + private memory access when the bit is set. Otherwise the memory error is + caused by shared memory access when the bit is clear. + +'gpa' and 'size' indicate the memory range the error occurs at. The userspace +may handle the error and return to KVM to retry the previous memory access. + :: /* KVM_EXIT_NOTIFY */ diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 4d4b3de8ac55..6c6ed214b6ac 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. */ @@ -520,6 +521,13 @@ struct kvm_run { #define KVM_NOTIFY_CONTEXT_INVALID (1 << 0) __u32 flags; } notify; + /* KVM_EXIT_MEMORY_FAULT */ + struct { +#define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) + __u64 flags; + __u64 gpa; + __u64 size; + } memory; /* Fix the size of the union. */ char padding[256]; };