Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp5311787pxv; Wed, 28 Jul 2021 07:58:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzV4ai20sc23isgW6wSuXT0AMIvgJwhrrZWSG/rojoxXceSWwhXshgIwHIMnp9Hl4KIO8va X-Received: by 2002:a05:6402:d2:: with SMTP id i18mr259220edu.17.1627484332450; Wed, 28 Jul 2021 07:58:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627484332; cv=none; d=google.com; s=arc-20160816; b=RascG7Wz4KDVb/jVUcc9pdWMT+q24NLiMpgC1+kzToM37YSe2vuk6DsJy4NFvUIdQL Hd2FiINrbTQl7dWF4dKZ1LTIuqgyj4EV+MNgK3eSidEBOlzpSiHUw1fnDUpBhpfckHDL ZAFJSMuxBNvY+VTyPecXdGRmUD3kBe4LXwfdYNGkbwlR7h3ymKpzB8ISFOCqNFVItALh PfNkClmM2i1qde1oDaq4oikgf9VRO8UDdPM05HtHla5n/iGs9KGQx3pujyVAiSqRELVP cuyQIhIJDI2nG7kI46x37d5hM0diKBBeD6uHt7mveZ8ksqOGKM1UtueK3mW5bV7cqdHK S0XQ== 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=fRYEBTeqaJZ2nuhpRxylpNXV0HYL+RYR+rak5A96Ekw=; b=Kpzzd2gewjyS3QpfFX0ma4kW6mHkP/mR7lePPSCxGG/ctIma3dHulzsffNDZ/Jzb1U WsoA/R+gkkiruGUvL0ZLB+Mmf98wvGlzeoEoWaHtAE6cAEUjK98h13loSXwlxjvOlrAj 5fH1FW2yJeFbYE+JrWd3Z1gKXp3unyIpHohc7a7acZDI7AoBoNbxL0yzjNh8yWmBfVVF xPLeEU7pdbY2o9DXYfqWTE5Zq+CmLxsShszLHgSg2GkF6H1gA1pl4oJVYp8APdGDcpJS wYsCeZD6Z5kEhASGPH5jaPzneuW+IP3vw7J/q2If2UMyGMRGWcTPp7TJoQOE4YRjfx+s AgKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=sig9nFz8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t15si46501ejr.112.2021.07.28.07.58.29; Wed, 28 Jul 2021 07:58:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=sig9nFz8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237108AbhG1OyT (ORCPT + 99 others); Wed, 28 Jul 2021 10:54:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237414AbhG1Oxt (ORCPT ); Wed, 28 Jul 2021 10:53:49 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 723ADC0617A1; Wed, 28 Jul 2021 07:53:30 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id f13so3025618plj.2; Wed, 28 Jul 2021 07:53:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fRYEBTeqaJZ2nuhpRxylpNXV0HYL+RYR+rak5A96Ekw=; b=sig9nFz8eF+R0264OvGZWIOLFWHbYjYEwR2pu5Gx18nTH1XR1cSX7qthexUthkJGnF pJ3gnLUuVZV82sVUwcehqWiFsrDdsp1tZqg/jE/39e470wf89bUSF5Q65P+BWFm5oA6l tdiu0+NkRbrdrY5sf+SlZYMVQQRnay/soDyBm9yR+3Sk7hPDtBJcXtSKp6bbcAlin5vP TxrwmdY7JJpG3nMN0Kpik8j7JBNr1h1zSYFRfmYey7tr/PqG3AfyBn5BZzutMLEWs9dk Q8BFOoHDdjXEgX/RmQsfZ/nngsOe/X2TpZTA23wyhqE+8CwnuHHe28EpNrXDO0BWSkNE 89qw== 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=fRYEBTeqaJZ2nuhpRxylpNXV0HYL+RYR+rak5A96Ekw=; b=U1cJCBwC+bsGohUrm+oT1Nd/pK5eoKDZdC+P8UEs7Kyuu1xAgyYk4IvnRZl+NDG9yZ I3wtFIcPObjXujkIkrqugj2IBq7bIIPleGE6lY2iIcpP8YD28TWEZ05fLe4AFo5bvo5/ 5QCvnbQZgDjDHbQxN/cvPq7uObRRUdrXOA8zryVe+ptYthnGcSvfdgjORP61FwZgwjOc WFHIL4nmsaFOH5UGKsqlsoa0Kl/e3mNPU8jrQM1LCSKWDNCo9zoX1CTfrcLwVGzOMqdn C4Iv0Z0eyIqTEZuvgWDDk4piMClKaBitt9DBWgk1VQhU4qzhWB7qCet8FOB/92+rraOk tW/Q== X-Gm-Message-State: AOAM533Ww4FcDnjVXLT1bddhE2DFVJSlZu0VCqpyw9KGG5/COayMxCH4 xO0hNdPsVKjR/0tC3VKxQn4= X-Received: by 2002:a17:90b:385:: with SMTP id ga5mr6922305pjb.183.1627484010063; Wed, 28 Jul 2021 07:53:30 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:29 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, will@kernel.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, anparri@microsoft.com Subject: [PATCH 09/13] DMA: Add dma_map_decrypted/dma_unmap_encrypted() function Date: Wed, 28 Jul 2021 10:52:24 -0400 Message-Id: <20210728145232.285861-10-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tianyu Lan In Hyper-V Isolation VM with AMD SEV, swiotlb boucne buffer needs to be mapped into address space above vTOM and so introduce dma_map_decrypted/dma_unmap_encrypted() to map/unmap bounce buffer memory. The platform can populate man/unmap callback in the dma memory decrypted ops. Signed-off-by: Tianyu Lan --- include/linux/dma-map-ops.h | 9 +++++++++ kernel/dma/mapping.c | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 0d53a96a3d64..01d60a024e45 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -71,6 +71,11 @@ struct dma_map_ops { unsigned long (*get_merge_boundary)(struct device *dev); }; +struct dma_memory_decrypted_ops { + void *(*map)(void *addr, unsigned long size); + void (*unmap)(void *addr); +}; + #ifdef CONFIG_DMA_OPS #include @@ -374,6 +379,10 @@ static inline void debug_dma_dump_mappings(struct device *dev) } #endif /* CONFIG_DMA_API_DEBUG */ +void *dma_map_decrypted(void *addr, unsigned long size); +int dma_unmap_decrypted(void *addr, unsigned long size); + extern const struct dma_map_ops dma_dummy_ops; +extern struct dma_memory_decrypted_ops dma_memory_generic_decrypted_ops; #endif /* _LINUX_DMA_MAP_OPS_H */ diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 2b06a809d0b9..6fb150dc1750 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -13,11 +13,13 @@ #include #include #include +#include #include "debug.h" #include "direct.h" bool dma_default_coherent; +struct dma_memory_decrypted_ops dma_memory_generic_decrypted_ops; /* * Managed DMA API */ @@ -736,3 +738,23 @@ unsigned long dma_get_merge_boundary(struct device *dev) return ops->get_merge_boundary(dev); } EXPORT_SYMBOL_GPL(dma_get_merge_boundary); + +void *dma_map_decrypted(void *addr, unsigned long size) +{ + if (set_memory_decrypted((unsigned long)addr, + size / PAGE_SIZE)) + return NULL; + + if (dma_memory_generic_decrypted_ops.map) + return dma_memory_generic_decrypted_ops.map(addr, size); + else + return addr; +} + +int dma_unmap_encrypted(void *addr, unsigned long size) +{ + if (dma_memory_generic_decrypted_ops.unmap) + dma_memory_generic_decrypted_ops.unmap(addr); + + return set_memory_encrypted((unsigned long)addr, size / PAGE_SIZE); +} -- 2.25.1