From patchwork Tue Oct 20 06:18:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 11845749 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C396D16C0 for ; Tue, 20 Oct 2020 06:19:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5CB412242E for ; Tue, 20 Oct 2020 06:19:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=shutemov-name.20150623.gappssmtp.com header.i=@shutemov-name.20150623.gappssmtp.com header.b="xayXCOMN" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5CB412242E 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 60E0E6B0062; Tue, 20 Oct 2020 02:19:08 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 55C9D6B006C; Tue, 20 Oct 2020 02:19:08 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A20B6B006E; Tue, 20 Oct 2020 02:19:08 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0118.hostedemail.com [216.40.44.118]) by kanga.kvack.org (Postfix) with ESMTP id 0AEF36B0062 for ; Tue, 20 Oct 2020 02:19:07 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id A1222180AD811 for ; Tue, 20 Oct 2020 06:19:07 +0000 (UTC) X-FDA: 77391301134.14.bath50_21149892723d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id 829EC18229818 for ; Tue, 20 Oct 2020 06:19:07 +0000 (UTC) X-Spam-Summary: 1,0,0,405bedd663eabc8f,d41d8cd98f00b204,kirill@shutemov.name,,RULES_HIT:41:355:379:541:960:973:988:989:1260:1311:1314:1345:1359:1431:1437:1515:1535:1542:1711:1730:1747:1777:1792:2393:2559:2562:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3871:4117:4250:4321:4605:5007:6119:6261:6653:6742:7875:7903:8660:9036:10004:11026:11473:11657:11658:11914:12043:12048:12296:12297:12438:12517:12519:12555:12895:12986:13148:13230:13894:14096:14181:14721:21080:21220:21444:21451:21627:21939:21990:30045:30054:30070,0,RBL:209.85.167.68:@shutemov.name:.lbl8.mailshell.net-62.8.0.100 66.201.201.201;04ygkiuc974catg13auix46tzjjwfock11fjs6affsabt7oqbf7gjrf9eufmytg.hwcg4upyuebnzse851i3b6q13unoqjf5ymtufb79cpbfwncp7a1as8ioadibnsh.k-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:28,LUA_SUMMARY:none X-HE-Tag: bath50_21149892723d X-Filterd-Recvd-Size: 6753 Received: from mail-lf1-f68.google.com (mail-lf1-f68.google.com [209.85.167.68]) by imf45.hostedemail.com (Postfix) with ESMTP for ; Tue, 20 Oct 2020 06:19:06 +0000 (UTC) Received: by mail-lf1-f68.google.com with SMTP id a7so653596lfk.9 for ; Mon, 19 Oct 2020 23:19:06 -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=hD8fdBTcCRJJbNA8v3NsYdfNpsT0GOXjZBHChPVSAwQ=; b=xayXCOMNNZwbmuUkVVezQLWXxIF8Miy8enw02DRg4WMA4X4m4slW+NMA5P0c3qhO5D +kGcktM4P+HXe2yBeAnG/2+Xclqp2OgxTip6/Bb3CVCIZ2bBJdJuUilLIHzwrkBYOE4B TsX/QxjT2Zm+Xmi1DZEf4e3GHpzUBG52A5spJ1L+jd8GahD1wE/T70vxhKe2veBu6B1U on4Jm3FJeU84oDq/lvJr3ij5Om1hKycOuf2yF3ueejQYeW554+O4+OSO9VQsHq+jp040 3geG9EoBXHbv6MxUNE5qtCkA4baTyshro1n8PLvrT+t13ES/igx8D186TbBJCvSPy9yt zZ+Q== 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=hD8fdBTcCRJJbNA8v3NsYdfNpsT0GOXjZBHChPVSAwQ=; b=j3RekthepK2bhhSYOFtW0QBn1XbfFCL9fwTZnO1RHv1BolesIv3HMrStg+vkKJ8KIZ eCCH/SmxWKU7Pmedi/Ly9E6NyvJ0AkMg+2gI03wjuZrbF6YazCQVd6mZ763bhpUq6Pz5 r9oo42QVluEdQQCCdPRBZWny+xMV8M+KlShQInKW2KoN+lL/QMDle4TxqTnSbHozawmc W9F6p8IOKqlGBKRhdtZEZh2xEXVqgExA3+GJ2Xxw//u+7s8wEorhnLGMDOCLGs3bQ8I2 BBb+RBwsxk+secCcaOcYAyhUHzddiHZ/nz4LL3xevt4h6asHwgWvBl1b4JNlBUR16/3T cWQg== X-Gm-Message-State: AOAM533EfunwyvaFPsxs5AtfkNAgnb6b32gGsFRi1QUJsjxGhz2AS6KI hbbADhSsRprnhc/pOwPLPCUKCw== X-Google-Smtp-Source: ABdhPJxCPKzmW7NJTv3qxlWKTiOn+Grv8DEV/W8CxNVAx3qx4Sk8sxbe6hNbgDfkCVtKyrc+SsUrYg== X-Received: by 2002:a19:824f:: with SMTP id e76mr380839lfd.572.1603174745613; Mon, 19 Oct 2020 23:19:05 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id y19sm135513lfj.288.2020.10.19.23.19.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Oct 2020 23:19:04 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id C76D7102F62; Tue, 20 Oct 2020 09:19:01 +0300 (+03) To: Dave Hansen , Andy Lutomirski , Peter Zijlstra , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Cc: David Rientjes , Andrea Arcangeli , Kees Cook , Will Drewry , "Edgecombe, Rick P" , "Kleen, Andi" , Liran Alon , Mike Rapoport , x86@kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [RFCv2 03/16] x86/kvm: Make DMA pages shared Date: Tue, 20 Oct 2020 09:18:46 +0300 Message-Id: <20201020061859.18385-4-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201020061859.18385-1-kirill.shutemov@linux.intel.com> References: <20201020061859.18385-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 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 | 7 +++++++ include/uapi/linux/kvm_para.h | 2 ++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 619ebf40e457..cd272e3babbc 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -805,6 +805,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 964e04152417..a878e7f246d5 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 d1b2a889f035..4c49303126c9 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,12 @@ static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) struct cpa_data cpa; int ret; + if (kvm_mem_protected()) { + unsigned long gfn = __pa(addr) >> PAGE_SHIFT; + int call = enc ? KVM_HC_MEM_UNSHARE : KVM_HC_MEM_SHARE; + return kvm_hypercall2(call, gfn, 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..c6d8c988e330 100644 --- a/include/uapi/linux/kvm_para.h +++ b/include/uapi/linux/kvm_para.h @@ -30,6 +30,8 @@ #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 +#define KVM_HC_MEM_UNSHARE 14 /* * hypercalls use architecture specific