Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6110877ybi; Wed, 31 Jul 2019 08:30:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqyf0c2by4xvPWeI/JaAUBKUAsYJeV58Xi5VwWOTS8lynO5kzTTnrVBo73Dr7OKn0rxkVw+l X-Received: by 2002:a62:e417:: with SMTP id r23mr46365040pfh.160.1564587053608; Wed, 31 Jul 2019 08:30:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564587053; cv=none; d=google.com; s=arc-20160816; b=LXzTlOX2aiTW+0Z3V3C/iyWkU4rZbewpIRU0mHr9fT/meSzcdj+IZQhI/BxPgN8UDe nDvEw30Fv3Ot7mXpymrB2fsRrARspFZEFF19L3UqgDL1i6t5/dXO+o0OdUIQJkDFzlkX DCrB/nnzwUmVdd6Ej87xpcl0ZG0KVVYG/YL8wmxw9LI9M5d+a9o9GuiXurlRuoW8khij mQPWenOQH29pNArx/JBoqOsSEVHfQ2D+gY2paa+63AgWzxhyBZU3fYIE/g2c63AND9Jc 3NgyJOxk+SLUBSVJ6+4vcwKWGD0BZHbfRwQX00Qm0J30kASEhp+1jMcoOoNEuxr+7GK+ cumw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hC2uwgXxojO8zk/Rcrr6mceJUJW70ppjchwtq4ruRCg=; b=sKPBBA4ndig94RhLcZVfkxZUt1XxRy90wF3HEa9BCnLQCHrpqE5mwiUM/iXTzdjJ5h hH1O7LfqPibEYj24fssL5vmmfxv4vs+R5XtAzr27+3Af1esiM9SOr20wfEJr7eds1iU8 IiGgLNuFWkUviOFgbFtUzbtyQVNwxH8POtHssYrqe4a+TrdopNNUTv3mEVmaMyQkkQi9 i2HSAIbRTtTk9NiS7kvOoqeP0ZlVICZE12FzsYE1TDDPQ4VQ+5YCsbnIMi9kjj682cEr fVJC3Og03g528vmVM2PTSHBNBOFhsahPI0sFrxwLYY9+QMipkw1gqqQhLj7I5EJzEO7L /dWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b="0XV6P/gt"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d36si28960871pla.113.2019.07.31.08.30.38; Wed, 31 Jul 2019 08:30:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b="0XV6P/gt"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727485AbfGaPWP (ORCPT + 99 others); Wed, 31 Jul 2019 11:22:15 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:40457 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729664AbfGaPTm (ORCPT ); Wed, 31 Jul 2019 11:19:42 -0400 Received: by mail-ed1-f67.google.com with SMTP id k8so66032408eds.7 for ; Wed, 31 Jul 2019 08:19:40 -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=hC2uwgXxojO8zk/Rcrr6mceJUJW70ppjchwtq4ruRCg=; b=0XV6P/gt+Sg4f27WCDZfMiz3yUD8gmqmXbpUkqw2qeKte/64P4ld/yFj9TtHQsxMva Sbd6SFVGZn15wA2StFIL5OKo0viglmdPGCy3S9nsnoLjxmIaz4pZPHVNhzJMBFiEItzK GGFxIZHfCywnX+w1VFB8az1H4FFBZ0dEp86xAnm0F7Jfw3TX8bJ2UwJ4MOp6xtqistdS ci1LlbxLwNn2UPtS72pBvzNbUsUQSbVvUvjpSYYoWOORZ9uWxZOyAGcJabNrh3zrp7Ty xsBZdp44F7iwElj0Db5RusDWd82dGLl/6L2fkZUDkulyOvXwh+aBdW95+eM1LPwVFqZZ pCfQ== 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=hC2uwgXxojO8zk/Rcrr6mceJUJW70ppjchwtq4ruRCg=; b=NhiT1wW7jXoXzeZ0fs2F+94XI/GqaST0R07VxoBxglknnp8sSQF992A0SHZo8P4syX U/wFokK60gcvf3YjVtIPJWfgkdtWXgD+NlmPMSv8ORSkPQeHRYNCa0NqpcBMi4E6xvcQ y8D2D8gDuhJ/Whw0ZstTzRyx/kU60UYjCBTqpOJY7GLjtItIpKdqd416sTOnQ4CJ3lSn 1RXMNwbvzVO+TWieYmZYvI+ghYyZlE8tOAxZWieYTat5G9/H99L7E4b3Vl8Ilm7VP/5W mngicJnl6oNHG6x5Kw3GnHjLruPfcXV1tFQLgrALdv9JHBYuHM6CDI33CjmVmfHf7ZKO S2dA== X-Gm-Message-State: APjAAAUvRATnG7gWCuBKlWF6LcbQIGXGwOdizYjgM1MFelRVgjK0clLG A/Nbwx+TU59YC2WLR7xxL88= X-Received: by 2002:a17:906:9447:: with SMTP id z7mr29540487ejx.165.1564586039736; Wed, 31 Jul 2019 08:13:59 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id g11sm12443173ejm.86.2019.07.31.08.13.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 08:13:57 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id 488B9104606; Wed, 31 Jul 2019 18:08:17 +0300 (+03) To: Andrew Morton , x86@kernel.org, Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Borislav Petkov , Peter Zijlstra , Andy Lutomirski , David Howells Cc: Kees Cook , Dave Hansen , Kai Huang , Jacob Pan , Alison Schofield , linux-mm@kvack.org, kvm@vger.kernel.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A . Shutemov" Subject: [PATCHv2 48/59] iommu/vt-d: Support MKTME in DMA remapping Date: Wed, 31 Jul 2019 18:08:02 +0300 Message-Id: <20190731150813.26289-49-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> References: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jacob Pan When MKTME is enabled, keyid is stored in the high order bits of physical address. For DMA transactions targeting encrypted physical memory, keyid must be included in the IOVA to physical address translation. This patch appends page keyid when setting up the IOMMU PTEs. On the reverse direction, keyid bits are cleared in the physical address lookup. Mapping functions of both DMA ops and IOMMU ops are covered. Signed-off-by: Jacob Pan Signed-off-by: Kirill A. Shutemov --- drivers/iommu/intel-iommu.c | 29 +++++++++++++++++++++++++++-- include/linux/intel-iommu.h | 9 ++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c index ac4172c02244..32d22872656b 100644 --- a/drivers/iommu/intel-iommu.c +++ b/drivers/iommu/intel-iommu.c @@ -867,6 +867,28 @@ static void free_context_table(struct intel_iommu *iommu) spin_unlock_irqrestore(&iommu->lock, flags); } +static inline void set_pte_mktme_keyid(unsigned long phys_pfn, + phys_addr_t *pteval) +{ + unsigned long keyid; + + if (!pfn_valid(phys_pfn)) + return; + + keyid = page_keyid(pfn_to_page(phys_pfn)); + +#ifdef CONFIG_X86_INTEL_MKTME + /* + * When MKTME is enabled, set keyid in PTE such that DMA + * remapping will include keyid in the translation from IOVA + * to physical address. This applies to both user and kernel + * allocated DMA memory. + */ + *pteval &= ~mktme_keyid_mask(); + *pteval |= keyid << mktme_keyid_shift(); +#endif +} + static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain, unsigned long pfn, int *target_level) { @@ -893,7 +915,7 @@ static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain, break; if (!dma_pte_present(pte)) { - uint64_t pteval; + phys_addr_t pteval; tmp_page = alloc_pgtable_page(domain->nid); @@ -901,7 +923,8 @@ static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain, return NULL; domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE); - pteval = ((uint64_t)virt_to_dma_pfn(tmp_page) << VTD_PAGE_SHIFT) | DMA_PTE_READ | DMA_PTE_WRITE; + pteval = (virt_to_dma_pfn(tmp_page) << VTD_PAGE_SHIFT) | DMA_PTE_READ | DMA_PTE_WRITE; + set_pte_mktme_keyid(virt_to_dma_pfn(tmp_page), &pteval); if (cmpxchg64(&pte->val, 0ULL, pteval)) /* Someone else set it while we were thinking; use theirs. */ free_pgtable_page(tmp_page); @@ -2214,6 +2237,8 @@ static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, } } + set_pte_mktme_keyid(phys_pfn, &pteval); + /* We don't need lock here, nobody else * touches the iova range */ diff --git a/include/linux/intel-iommu.h b/include/linux/intel-iommu.h index f2ae8a006ff8..8fbb9353d5a6 100644 --- a/include/linux/intel-iommu.h +++ b/include/linux/intel-iommu.h @@ -22,6 +22,8 @@ #include #include +#include + /* * VT-d hardware uses 4KiB page size regardless of host page size. @@ -608,7 +610,12 @@ static inline void dma_clear_pte(struct dma_pte *pte) static inline u64 dma_pte_addr(struct dma_pte *pte) { #ifdef CONFIG_64BIT - return pte->val & VTD_PAGE_MASK; + u64 addr = pte->val; + addr &= VTD_PAGE_MASK; +#ifdef CONFIG_X86_INTEL_MKTME + addr &= ~mktme_keyid_mask(); +#endif + return addr; #else /* Must have a full atomic 64-bit read */ return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK; -- 2.21.0