From patchwork Fri Apr 2 15:26:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 12181189 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3909C43462 for ; Fri, 2 Apr 2021 15:27:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 401D361166 for ; Fri, 2 Apr 2021 15:27:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 401D361166 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A14F06B007E; Fri, 2 Apr 2021 11:27:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C53D8D0002; Fri, 2 Apr 2021 11:27:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C7816B0083; Fri, 2 Apr 2021 11:27:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0034.hostedemail.com [216.40.44.34]) by kanga.kvack.org (Postfix) with ESMTP id 54E256B007E for ; Fri, 2 Apr 2021 11:27:01 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 139C3A2AA for ; Fri, 2 Apr 2021 15:27:01 +0000 (UTC) X-FDA: 77987805042.28.8DC1371 Received: from mail-lj1-f170.google.com (mail-lj1-f170.google.com [209.85.208.170]) by imf12.hostedemail.com (Postfix) with ESMTP id 65E6D132 for ; Fri, 2 Apr 2021 15:26:59 +0000 (UTC) Received: by mail-lj1-f170.google.com with SMTP id r20so5961280ljk.4 for ; Fri, 02 Apr 2021 08:27:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aho3YzbAYOK3xFgDwaA/KoZASR7zBJ1wJNmUpntSsWc=; b=2QzvDuEvCR3I2ZDGC2q/r4bNtqnH2e/nkaqSMvgaY8t1XXblCZNLVwizVwriqp4ctq CuEbDoBOax44+oLmnKITnvspc5PpazSknGo20E7xxQUgEL0BMY+U4SOPirIT02WkkJ/w wrAY+AmwUvUFXoqRvjBzonIuQt3gNbeytFCVwJuDxuQrO9BxYGmZTXg0Nt7gEsMJptNO Ivjl0iZK4X/qBiwWdxuaKlUU5GzHNIbUoaBRC7viRsnEds4bK5Selt6pbN7neSbHwSap g32WgF2ulA5zJiMifrWUkKbeNzEZUyS9ygY05adoG52zdthFiBJxjcrkFR5TiLVRXDKW 6UpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aho3YzbAYOK3xFgDwaA/KoZASR7zBJ1wJNmUpntSsWc=; b=nf1oydCzLSq9opH8gSDOC9u9kF5D8YNp8iLyfD3Ke13EmlWe5AMQexvb23a/+zJNfb kWunXRZRi95cA/RzfrxgJbdlySI22XmFWYlsZRjjEnFnGmxjqFbCUGV2HuzRejXu+8tB mRrf7u4dx0B+hK2l9QkQn/WmF4BGuZzURiXuQn/RT1OacTfAwMgTxLptb+NLu7AQ2W38 auBYJdN9rMZ9VEnj8Q5lKy1/+8+JXbsEB14eShWzRI1M78RcOK+Py4Jx74gJwSfIrCo8 nkDtlOzj3rtgmjrGyvynoZJ52xy/MsX2VJmRhfpsOPZYdA5f97ADXtQ4WJStWvzGjek5 z1vw== X-Gm-Message-State: AOAM530EE9d1iqcbtha2l6xOnAH0oW/37FpJivbF9VOlyTfKx3nLyLIb vuf6eWJhVZ73aauB/FMkrM6C9A== X-Google-Smtp-Source: ABdhPJxPXdliVU29L50xs/trh5bjPIkQyC1OfYRcYmB3fubXDmftI2jmDxrfsCutGi24mgL0K0++ww== X-Received: by 2002:a2e:9157:: with SMTP id q23mr8978027ljg.298.1617377219398; Fri, 02 Apr 2021 08:26:59 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id v11sm947533ljp.63.2021.04.02.08.26.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Apr 2021 08:26:57 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id 3598F102674; Fri, 2 Apr 2021 18:26:59 +0300 (+03) To: Dave Hansen , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , Jim Mattson Cc: David Rientjes , "Edgecombe, Rick P" , "Kleen, Andi" , "Yamahata, Isaku" , x86@kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [RFCv1 3/7] x86/kvm: Make DMA pages shared Date: Fri, 2 Apr 2021 18:26:41 +0300 Message-Id: <20210402152645.26680-4-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20210402152645.26680-1-kirill.shutemov@linux.intel.com> References: <20210402152645.26680-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 65E6D132 X-Stat-Signature: miym7g3pnfghegwcww5fpwdz8awpzg5z Received-SPF: none (shutemov.name>: No applicable sender policy available) receiver=imf12; identity=mailfrom; envelope-from=""; helo=mail-lj1-f170.google.com; client-ip=209.85.208.170 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1617377219-166858 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: Make force_dma_unencrypted() return true for KVM to get DMA pages mapped as shared. __set_memory_enc_dec() now informs the host via hypercall if the state of the page has changed from shared to private or back. Signed-off-by: Kirill A. Shutemov --- arch/x86/Kconfig | 1 + arch/x86/mm/mem_encrypt_common.c | 5 +++-- arch/x86/mm/pat/set_memory.c | 10 ++++++++++ include/uapi/linux/kvm_para.h | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 2b4ce1722dbd..d197b3beb904 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -811,6 +811,7 @@ config KVM_GUEST select PARAVIRT_CLOCK select ARCH_CPUIDLE_HALTPOLL select X86_HV_CALLBACK_VECTOR + select X86_MEM_ENCRYPT_COMMON default y help This option enables various optimizations for running under the KVM diff --git a/arch/x86/mm/mem_encrypt_common.c b/arch/x86/mm/mem_encrypt_common.c index dd791352f73f..6bf0718bb72a 100644 --- a/arch/x86/mm/mem_encrypt_common.c +++ b/arch/x86/mm/mem_encrypt_common.c @@ -10,14 +10,15 @@ #include #include #include +#include /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ bool force_dma_unencrypted(struct device *dev) { /* - * For SEV, all DMA must be to unencrypted/shared addresses. + * For SEV and KVM, all DMA must be to unencrypted/shared addresses. */ - if (sev_active()) + if (sev_active() || kvm_mem_protected()) return true; /* diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 16f878c26667..4b312d80033d 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -1977,6 +1978,15 @@ static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) struct cpa_data cpa; int ret; + if (kvm_mem_protected()) { + /* TODO: Unsharing memory back */ + if (WARN_ON_ONCE(enc)) + return -ENOSYS; + + return kvm_hypercall2(KVM_HC_MEM_SHARE, + __pa(addr) >> PAGE_SHIFT, numpages); + } + /* Nothing to do if memory encryption is not active */ if (!mem_encrypt_active()) return 0; diff --git a/include/uapi/linux/kvm_para.h b/include/uapi/linux/kvm_para.h index 1a216f32e572..09d36683ee0a 100644 --- a/include/uapi/linux/kvm_para.h +++ b/include/uapi/linux/kvm_para.h @@ -30,6 +30,7 @@ #define KVM_HC_SEND_IPI 10 #define KVM_HC_SCHED_YIELD 11 #define KVM_HC_ENABLE_MEM_PROTECTED 12 +#define KVM_HC_MEM_SHARE 13 /* * hypercalls use architecture specific