Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp946320rwn; Thu, 15 Sep 2022 08:23:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4/xPr2lWk051knEOdG9Ec8d4rjEhaHmxd7UFEvPrOvjf+6piDM8v0I60B5vbhBxdx3tmNt X-Received: by 2002:a17:902:8e84:b0:178:71f2:113c with SMTP id bg4-20020a1709028e8400b0017871f2113cmr80553plb.79.1663255416442; Thu, 15 Sep 2022 08:23:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663255416; cv=none; d=google.com; s=arc-20160816; b=tODNi8pwjYlGBO7Pahso6srBAOuleV4VY9ir8b8UzNiRMopUjDrFRFEM9xyGi9ht9J UVNlGuy+bz10fTQUGr6ge/V3AvkLpkCeQpLR/D17qK1tf3z9J2QFd1/6H/RppNx0a6W/ YjddPiyYIT/Yjtnk2aQI5IHXrtRNPiNfTNMGIGdNHxi0tZzOLsebct39JrO/+FZ+kkCU ympDKp/91/XnaQ04vmEPiyMmW9ptxysZEVp5ogKGI/kYLj3BOtVKQPBDVRBF52N8OrXD X7JzaTGLsydJj3Cy5CDGPvczPoxx2XRgVwY8HzuLw157DkqZJFyRtEQbDjfpM9/OZDx4 z74w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=X3whFcQy4DiYJehiz1E4F67fBFUtgX6WLcXgaPYTV90=; b=qd7hLproIXczFXhhznLgViNPO0Bpi/CfGZC1JBzf5CbOPjPp37mEy94QvOlk05ar0v Mzi+fjwySX9MsS+ur0++TE+2KPx+S6x5+PiHrN+dPp+LjZtfhnsWG2q5b7y1cQhMb+9x IwyT5iOmKiKTVQ+4jVLuiubo/bVloqpnRQDtqJnVs6QjbQme/fbkScORsKulbew+L7X+ DvNCxkdYobY7Pe3bkV9IYCPs1lUCvG7QGq5fINbTuh1p8lT/+hgiRTJK+g47OJOojWQS JB7mYqqDVncazfLx9HjfAteYoVkcU/jTCMbkgPkHn/LzihbHie/Fyh/lwuVH4L8MPIjS r+yQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Bc/vZMV0"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p12-20020a056a000b4c00b005436ab3283csi13449174pfo.43.2022.09.15.08.23.24; Thu, 15 Sep 2022 08:23:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Bc/vZMV0"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230314AbiIOOfl (ORCPT + 99 others); Thu, 15 Sep 2022 10:35:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230238AbiIOOeq (ORCPT ); Thu, 15 Sep 2022 10:34:46 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E388F10B4; Thu, 15 Sep 2022 07:34:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663252478; x=1694788478; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zh1uU9OfCds79z0Ds97NHO+IjadPr1HQahUg4D7PcaE=; b=Bc/vZMV0h3Re/Eissc1XY4I8Z4XODF67WxzFZ9r13FeGflucHUDctEwc 5AKXLHnuUEw7xEXS56Q7XXgtbh5Th0NEd1q7LwhfFoTtJDcJapFvN7Il1 INXltEeg5NIDIPC3bcvaqYJ/RTcmSYpRqRPxDBgeAymCWE43iSTicz6Nn y8dkGeVvjweeXSCvZEzPo5lH4yPAiO4VVeA+XSx9ebsptNkf+e2oYxbOA 617wHJPqpN/wXrNS3cHq/pFQ0c2HDbgtP0xcu3y9y17ihTTdSYlv364MX DprJ937yYsuk1Q/rV8WaNkS4dj44QcY2Xc/8CuDQ9GHNNxwCwC56Wy0rs w==; X-IronPort-AV: E=McAfee;i="6500,9779,10470"; a="278458436" X-IronPort-AV: E=Sophos;i="5.93,318,1654585200"; d="scan'208";a="278458436" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Sep 2022 07:34:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,318,1654585200"; d="scan'208";a="945977002" Received: from chaop.bj.intel.com ([10.240.193.75]) by fmsmga005.fm.intel.com with ESMTP; 15 Sep 2022 07:34:22 -0700 From: Chao Peng To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, qemu-devel@nongnu.org Cc: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H . Peter Anvin" , Hugh Dickins , Jeff Layton , "J . Bruce Fields" , Andrew Morton , Shuah Khan , Mike Rapoport , Steven Price , "Maciej S . Szmigiero" , Vlastimil Babka , Vishal Annapurve , Yu Zhang , Chao Peng , "Kirill A . Shutemov" , luto@kernel.org, jun.nakajima@intel.com, dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com, aarcange@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, Quentin Perret , Michael Roth , mhocko@suse.com, Muchun Song , wei.w.wang@intel.com Subject: [PATCH v8 3/8] KVM: Add KVM_EXIT_MEMORY_FAULT exit Date: Thu, 15 Sep 2022 22:29:08 +0800 Message-Id: <20220915142913.2213336-4-chao.p.peng@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220915142913.2213336-1-chao.p.peng@linux.intel.com> References: <20220915142913.2213336-1-chao.p.peng@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 do 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 --- Documentation/virt/kvm/api.rst | 23 +++++++++++++++++++++++ include/uapi/linux/kvm.h | 9 +++++++++ 2 files changed, 32 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index c1fac1e9f820..1a6c003b2a0b 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6638,6 +6638,29 @@ 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 (1 << 0) + __u32 flags; + __u32 padding; + __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 3ef462fb3b2a..0c8db7b7c138 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -300,6 +300,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. */ @@ -538,6 +539,14 @@ struct kvm_run { #define KVM_NOTIFY_CONTEXT_INVALID (1 << 0) __u32 flags; } notify; + /* KVM_EXIT_MEMORY_FAULT */ + struct { +#define KVM_MEMORY_EXIT_FLAG_PRIVATE (1 << 0) + __u32 flags; + __u32 padding; + __u64 gpa; + __u64 size; + } memory; /* Fix the size of the union. */ char padding[256]; }; -- 2.25.1