Skip to contentSkip to conversation

MKTME enabling

PATCH Superseded Kirill A. Shutemov 5 participants 36 messages Jun 26, 2018 – Jul 10, 2018

Subsystems (12)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover MKTME enabling

Multikey Total Memory Encryption (MKTME)[1] is a technology that allows transparent memory encryption in upcoming Intel platforms. See overview below.

Here's updated version of my patchset that brings support of MKTME. Please review and consider applying.

The patchset provides in-kernel infrastructure for MKTME, but doesn't yet have userspace interface.

First 5 patches are for core-mm. The rest is x86-specific.

The patchset is on top of tip- tree plus page_ext cleanups I've posted earlier[2]. page_ext cleanups are in -mm tree now.

Below is performance numbers for kernel build. Enabling MKTME doesn't affect performance of non-encrypted memory allocation.

For encrypted memory allocation requires cache flush on allocation and freeing encrypted memory. For kernel build it results in ~20% performance degradation if we allocate all anonymous memory as encrypted.

We would need to maintain per-KeyID pool of free pages to minimize cache flushing. I'm going to work on the optimization on top of this patchset.

The patchset also can be found here:

git://git.kernel.org/pub/scm/linux/kernel/git/kas/linux.git mktme/wip

v4:
- Address Dave's feedback.

- Add performance numbers.

v3:
- Kernel now can access encrypted pages via per-KeyID direct mapping.

- Rework page allocation for encrypted memory to minimize overhead on non-encrypted pages. It comes with cost for allocation of encrypted
pages: we have to flush cache on every time we allocate *and* free encrypted page. We will need to optimize it later.

v2:
- Store KeyID of page in page_ext->flags rather than in anon_vma. anon_vma approach turned out to be problematic. The main problem is that anon_vma of the page is no longer stable after last mapcount has gone. We would like to preserve last used KeyID even for freed pages as it allows to avoid unnecessary cache flushing on allocation of an encrypted page. page_ext serves this well enough.

- KeyID is now propagated through page allocator. No need in GFP_ENCRYPT anymore.

- Patch "Decouple dynamic __PHYSICAL_MASK from AMD SME" has been fix to work with AMD SEV (need to be confirmed by AMD folks).

------------------------------------------------------------------------------

MKTME is built on top of TME. TME allows encryption of the entirety of system memory using a single key. MKTME allows to have multiple encryption domains, each having own key -- different memory pages can be encrypted with different keys.

Key design points of Intel MKTME:

- Initial HW implementation would support upto 63 keys (plus one default TME key). But the number of keys may be as low as 3, depending to SKU and BIOS settings

- To access encrypted memory you need to use mapping with proper KeyID int the page table entry. KeyID is encoded in upper bits of PFN in page table entry.

- CPU does not enforce coherency between mappings of the same physical page with different KeyIDs or encryption keys. We wound need to take care about flushing cache on allocation of encrypted page and on returning it back to free pool.

- For managing keys, there's MKTME_KEY_PROGRAM leaf of the new PCONFIG (platform configuration) instruction. It allows load and clear keys associated with a KeyID. You can also ask CPU to generate a key for you or disable memory encryption when a KeyID is used.

Performance numbers for kernel build:

Base (tip- tree):

Performance counter stats for 'sh -c make -j100 -B -k >/dev/null' (5 runs):

    5664711.936917      task-clock (msec)         #   34.815 CPUs utilized            ( +-  0.02% )
         1,033,886      context-switches          #    0.183 K/sec                    ( +-  0.37% )
           189,308      cpu-migrations            #    0.033 K/sec                    ( +-  0.39% )
       104,951,554      page-faults               #    0.019 M/sec                    ( +-  0.01% )
16,907,670,543,945      cycles                    #    2.985 GHz                      ( +-  0.01% )
12,662,345,427,578      stalled-cycles-frontend   #   74.89% frontend cycles idle     ( +-  0.02% )
 9,936,469,878,830      instructions              #    0.59  insn per cycle
                                                  #    1.27  stalled cycles per insn  ( +-  0.00% )
 2,179,100,082,611      branches                  #  384.680 M/sec                    ( +-  0.00% )
    91,235,200,652      branch-misses             #    4.19% of all branches          ( +-  0.01% )

162.706797586 seconds time elapsed ( +- 0.04% )

CONFIG_X86_INTEL_MKTME=y, no encrypted memory:

Performance counter stats for 'sh -c make -j100 -B -k >/dev/null' (5 runs):

    5668508.245004      task-clock (msec)         #   34.872 CPUs utilized            ( +-  0.02% )
         1,032,034      context-switches          #    0.182 K/sec                    ( +-  0.90% )
           188,098      cpu-migrations            #    0.033 K/sec                    ( +-  1.15% )
       104,964,084      page-faults               #    0.019 M/sec                    ( +-  0.01% )
16,919,270,913,026      cycles                    #    2.985 GHz                      ( +-  0.02% )
12,672,067,815,805      stalled-cycles-frontend   #   74.90% frontend cycles idle     ( +-  0.02% )
 9,942,560,135,477      instructions              #    0.59  insn per cycle
                                                  #    1.27  stalled cycles per insn  ( +-  0.00% )
 2,180,800,745,687      branches                  #  384.722 M/sec                    ( +-  0.00% )
    91,167,857,700      branch-misses             #    4.18% of all branches          ( +-  0.02% )

162.552503629 seconds time elapsed ( +- 0.10% )

CONFIG_X86_INTEL_MKTME=y, all anonymous memory encrypted with KeyID-1, pay cache flush overhead on allocation and free:

Performance counter stats for 'sh -c make -j100 -B -k >/dev/null' (5 runs):

    7041851.999259      task-clock (msec)         #   35.915 CPUs utilized            ( +-  0.01% )
         1,118,938      context-switches          #    0.159 K/sec                    ( +-  0.49% )
           197,039      cpu-migrations            #    0.028 K/sec                    ( +-  0.80% )
       104,970,021      page-faults               #    0.015 M/sec                    ( +-  0.00% )
21,025,639,251,627      cycles                    #    2.986 GHz                      ( +-  0.01% )
16,729,451,765,492      stalled-cycles-frontend   #   79.57% frontend cycles idle     ( +-  0.02% )
10,010,727,735,588      instructions              #    0.48  insn per cycle
                                                  #    1.67  stalled cycles per insn  ( +-  0.00% )
 2,197,110,181,421      branches                  #  312.007 M/sec                    ( +-  0.00% )
    91,119,463,513      branch-misses             #    4.15% of all branches          ( +-  0.01% )

196.072361087 seconds time elapsed ( +- 0.14% )

[1] https://software.intel.com/sites/default/files/managed/a5/16/Multi-Key-Total-Memory-Encryption-Spec.pdf
[2] https://lkml.kernel.org/r/[email protected]

Kirill A. Shutemov (18):
mm: Do no merge VMAs with different encryption KeyIDs mm/ksm: Do not merge pages with different KeyIDs mm/page_alloc: Unify alloc_hugepage_vma()
mm/page_alloc: Handle allocation for encrypted memory mm/khugepaged: Handle encrypted pages
x86/mm: Mask out KeyID bits from page table entry pfn x86/mm: Introduce variables to store number, shift and mask of KeyIDs x86/mm: Preserve KeyID on pte_modify() and pgprot_modify() x86/mm: Implement page_keyid() using page_ext
x86/mm: Implement vma_keyid()
x86/mm: Implement prep_encrypted_page() and arch_free_page() x86/mm: Rename CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING x86/mm: Allow to disable MKTME after enumeration
x86/mm: Detect MKTME early
x86/mm: Calculate direct mapping size
x86/mm: Implement sync_direct_mapping()
x86/mm: Handle encrypted memory in page_to_virt() and __pa()
x86: Introduce CONFIG_X86_INTEL_MKTME

 Documentation/x86/x86_64/mm.txt      |   4 +
 arch/alpha/include/asm/page.h        |   2 +-
 arch/x86/Kconfig                     |  21 +-
 arch/x86/include/asm/mktme.h         |  47 +++
 arch/x86/include/asm/page.h          |   1 +
 arch/x86/include/asm/page_64.h       |   3 +-
 arch/x86/include/asm/pgtable_types.h |  15 +-
 arch/x86/include/asm/setup.h         |   6 +
 arch/x86/kernel/cpu/intel.c          |  32 +-
 arch/x86/kernel/head64.c             |   2 +
 arch/x86/kernel/setup.c              |   3 +
 arch/x86/mm/Makefile                 |   2 +
 arch/x86/mm/init_64.c                |  50 +++
 arch/x86/mm/kaslr.c                  |  11 +-
 arch/x86/mm/mktme.c                  | 546 +++++++++++++++++++++++++++
 include/linux/gfp.h                  |  54 ++-
 include/linux/migrate.h              |  12 +-
 include/linux/mm.h                   |  14 +
 include/linux/page_ext.h             |  11 +-
 mm/compaction.c                      |   1 +
 mm/khugepaged.c                      |  10 +
 mm/ksm.c                             |   3 +
 mm/mempolicy.c                       |  28 +-
 mm/migrate.c                         |   4 +-
 mm/mmap.c                            |   3 +-
 mm/page_alloc.c                      |  47 +++
 mm/page_ext.c                        |   3 +
 27 files changed, 901 insertions(+), 34 deletions(-)
 create mode 100644 arch/x86/include/asm/mktme.h
 create mode 100644 arch/x86/mm/mktme.c
Signature · 1 line
2.18.0
#1 lore raw

PATCH v4 1/18 mm: Do no merge VMAs with different encryption KeyIDs

VMAs with different KeyID do not mix together. Only VMAs with the same KeyID are compatible.

---
 include/linux/mm.h | 7 +++++++
 mm/mmap.c          | 3 ++-
 2 files changed, 9 insertions(+), 1 deletion(-)
include/linux/mm.h +7 -0
index a0fbb9ffe380..ebf4bd8bd0bf 100644
@@ -1541,6 +1541,13 @@ static inline bool vma_is_anonymous(struct vm_area_struct *vma)
 	return !vma->vm_ops;
 }
 
+#ifndef vma_keyid
+static inline int vma_keyid(struct vm_area_struct *vma)
+{
+	return 0;
+}
+#endif
+
 #ifdef CONFIG_SHMEM
 /*
  * The vma_is_shmem is not inline because it is used only by slow
mm/mmap.c +2 -1
index d1eb87ef4b1a..7823eb264cc0 100644
@@ -1217,7 +1217,8 @@ static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *
 		mpol_equal(vma_policy(a), vma_policy(b)) &&
 		a->vm_file == b->vm_file &&
 		!((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC|VM_SOFTDIRTY)) &&
-		b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
+		b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT) &&
+		vma_keyid(a) == vma_keyid(b);
 }
 
 /*
Signature · 1 line
2.18.0
#2 lore raw

PATCH v4 2/18 mm/ksm: Do not merge pages with different KeyIDs

In reply to Kirill A. Shutemov

Pages encrypted with different encryption keys are not subject to KSM merge. Otherwise it would cross security boundary.

---
 include/linux/mm.h | 7 +++++++
 mm/ksm.c           | 3 +++
 2 files changed, 10 insertions(+)
include/linux/mm.h +7 -0
index ebf4bd8bd0bf..406a28cadfcf 100644
@@ -1548,6 +1548,13 @@ static inline int vma_keyid(struct vm_area_struct *vma)
 }
 #endif
 
+#ifndef page_keyid
+static inline int page_keyid(struct page *page)
+{
+	return 0;
+}
+#endif
+
 #ifdef CONFIG_SHMEM
 /*
  * The vma_is_shmem is not inline because it is used only by slow
mm/ksm.c +3 -0
index a6d43cf9a982..1bd7b9710e29 100644
@@ -1214,6 +1214,9 @@ static int try_to_merge_one_page(struct vm_area_struct *vma,
 	if (!PageAnon(page))
 		goto out;
 
+	if (page_keyid(page) != page_keyid(kpage))
+		goto out;
+
 	/*
 	 * We need the page lock to read a stable PageSwapCache in
 	 * write_protect_page().  We use trylock_page() instead of
Signature · 1 line
2.18.0
#3 lore raw

Re: [PATCHv4 02/18] mm/ksm: Do not merge pages with different KeyIDs

Quoted 1 line · Kirill A. Shutemov

Pages encrypted with different encryption keys are not subject to KSM

Perhaps not allowed instead of subject?

Quoted 43 lines

merge. Otherwise it would cross security boundary.

---
 include/linux/mm.h | 7 +++++++
 mm/ksm.c           | 3 +++
 2 files changed, 10 insertions(+)
include/linux/mm.h +7 -0
index ebf4bd8bd0bf..406a28cadfcf 100644
@@ -1548,6 +1548,13 @@ static inline int vma_keyid(struct vm_area_struct *vma)
 }
 #endif
 
+#ifndef page_keyid
+static inline int page_keyid(struct page *page)
+{
+	return 0;
+}
+#endif
+
 #ifdef CONFIG_SHMEM
 /*
  * The vma_is_shmem is not inline because it is used only by slow
mm/ksm.c +3 -0
index a6d43cf9a982..1bd7b9710e29 100644
@@ -1214,6 +1214,9 @@ static int try_to_merge_one_page(struct vm_area_struct *vma,
 	if (!PageAnon(page))
 		goto out;
 
+	if (page_keyid(page) != page_keyid(kpage))
+		goto out;
+
 	/*
 	 * We need the page lock to read a stable PageSwapCache in
 	 * write_protect_page().  We use trylock_page() instead of
Signature · 1 line
2.18.0
#4 lore raw

PATCH v4 3/18 mm/page_alloc: Unify alloc_hugepage_vma()

In reply to Kirill A. Shutemov

We don't need to have separate implementations of alloc_hugepage_vma() for NUMA and non-NUMA. Using variant based on alloc_pages_vma() we would cover both cases.

This is preparation patch for allocation encrypted pages.

alloc_pages_vma() will handle allocation of encrypted pages. With this change we don' t need to cover alloc_hugepage_vma() separately.

The change makes typo in Alpha's implementation of __alloc_zeroed_user_highpage() visible. Fix it too.

---
 arch/alpha/include/asm/page.h | 2 +-
 include/linux/gfp.h           | 6 ++----
 2 files changed, 3 insertions(+), 5 deletions(-)
arch/alpha/include/asm/page.h +1 -1
index f3fb2848470a..9a6fbb5269f3 100644
@@ -18,7 +18,7 @@ extern void clear_page(void *page);
 #define clear_user_page(page, vaddr, pg)	clear_page(page)
 
 #define __alloc_zeroed_user_highpage(movableflags, vma, vaddr) \
-	alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma, vmaddr)
+	alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma, vaddr)
 #define __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE
 
 extern void copy_page(void * _to, void * _from);
include/linux/gfp.h +2 -4
index a6afcec53795..66f395737990 100644
@@ -494,21 +494,19 @@ alloc_pages(gfp_t gfp_mask, unsigned int order)
 extern struct page *alloc_pages_vma(gfp_t gfp_mask, int order,
 			struct vm_area_struct *vma, unsigned long addr,
 			int node, bool hugepage);
-#define alloc_hugepage_vma(gfp_mask, vma, addr, order)	\
-	alloc_pages_vma(gfp_mask, order, vma, addr, numa_node_id(), true)
 #else
 #define alloc_pages(gfp_mask, order) \
 		alloc_pages_node(numa_node_id(), gfp_mask, order)
 #define alloc_pages_vma(gfp_mask, order, vma, addr, node, false)\
 	alloc_pages(gfp_mask, order)
-#define alloc_hugepage_vma(gfp_mask, vma, addr, order)	\
-	alloc_pages(gfp_mask, order)
 #endif
 #define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0)
 #define alloc_page_vma(gfp_mask, vma, addr)			\
 	alloc_pages_vma(gfp_mask, 0, vma, addr, numa_node_id(), false)
 #define alloc_page_vma_node(gfp_mask, vma, addr, node)		\
 	alloc_pages_vma(gfp_mask, 0, vma, addr, node, false)
+#define alloc_hugepage_vma(gfp_mask, vma, addr, order) \
+	alloc_pages_vma(gfp_mask, order, vma, addr, numa_node_id(), true)
 
 extern unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order);
 extern unsigned long get_zeroed_page(gfp_t gfp_mask);
Signature · 1 line
2.18.0
#5 lore raw

PATCH v4 4/18 mm/page_alloc: Handle allocation for encrypted memory

In reply to Kirill A. Shutemov

For encrypted memory, we need to allocate pages for a specific encryption KeyID.

There are two cases when we need to allocate a page for encryption:

- Allocation for an encrypted VMA;

- Allocation for migration of encrypted page;

The first case can be covered within alloc_page_vma(). We know KeyID from the VMA.

The second case requires few new page allocation routines that would allocate the page for a specific KeyID.

An encrypted page has to be cleared after KeyID set. This is handled in prep_encrypted_page() that will be provided by arch-specific code.

---
 include/linux/gfp.h     | 48 ++++++++++++++++++++++++++++++++++++-----
 include/linux/migrate.h | 12 ++++++++---
 mm/compaction.c         |  1 +
 mm/mempolicy.c          | 28 ++++++++++++++++++------
 mm/migrate.c            |  4 ++--
 mm/page_alloc.c         | 47 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 123 insertions(+), 17 deletions(-)
include/linux/gfp.h +43 -5
index 66f395737990..347a40558cfc 100644
@@ -446,16 +446,46 @@ static inline void arch_free_page(struct page *page, int order) { }
 static inline void arch_alloc_page(struct page *page, int order) { }
 #endif
 
+#ifndef prep_encrypted_page
+static inline void prep_encrypted_page(struct page *page, int order,
+		int keyid, bool zero)
+{
+}
+#endif
+
+/*
+ * Encrypted page has to be cleared once keyid is set, not on allocation.
+ */
+static inline bool encrypted_page_needs_zero(int keyid, gfp_t *gfp_mask)
+{
+	if (!keyid)
+		return false;
+
+	if (*gfp_mask & __GFP_ZERO) {
+		*gfp_mask &= ~__GFP_ZERO;
+		return true;
+	}
+
+	return false;
+}
+
 struct page *
 __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid,
 							nodemask_t *nodemask);
 
+struct page *
+__alloc_pages_nodemask_keyid(gfp_t gfp_mask, unsigned int order,
+		int preferred_nid, nodemask_t *nodemask, int keyid);
+
 static inline struct page *
 __alloc_pages(gfp_t gfp_mask, unsigned int order, int preferred_nid)
 {
 	return __alloc_pages_nodemask(gfp_mask, order, preferred_nid, NULL);
 }
 
+struct page *__alloc_pages_node_keyid(int nid, int keyid,
+		gfp_t gfp_mask, unsigned int order);
+
 /*
  * Allocate pages, preferring the node given as nid. The node must be valid and
  * online. For more general interface, see alloc_pages_node().
@@ -483,6 +513,19 @@ static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask,
 	return __alloc_pages_node(nid, gfp_mask, order);
 }
 
+static inline struct page *alloc_pages_node_keyid(int nid, int keyid,
+		gfp_t gfp_mask, unsigned int order)
+{
+	if (nid == NUMA_NO_NODE)
+		nid = numa_mem_id();
+
+	return __alloc_pages_node_keyid(nid, keyid, gfp_mask, order);
+}
+
+extern struct page *alloc_pages_vma(gfp_t gfp_mask, int order,
+			struct vm_area_struct *vma, unsigned long addr,
+			int node, bool hugepage);
+
 #ifdef CONFIG_NUMA
 extern struct page *alloc_pages_current(gfp_t gfp_mask, unsigned order);
 
@@ -491,14 +534,9 @@ alloc_pages(gfp_t gfp_mask, unsigned int order)
 {
 	return alloc_pages_current(gfp_mask, order);
 }
-extern struct page *alloc_pages_vma(gfp_t gfp_mask, int order,
-			struct vm_area_struct *vma, unsigned long addr,
-			int node, bool hugepage);
 #else
 #define alloc_pages(gfp_mask, order) \
 		alloc_pages_node(numa_node_id(), gfp_mask, order)
-#define alloc_pages_vma(gfp_mask, order, vma, addr, node, false)\
-	alloc_pages(gfp_mask, order)
 #endif
 #define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0)
 #define alloc_page_vma(gfp_mask, vma, addr)			\
include/linux/migrate.h +9 -3
index f2b4abbca55e..fede9bfa89d9 100644
@@ -38,9 +38,15 @@ static inline struct page *new_page_nodemask(struct page *page,
 	unsigned int order = 0;
 	struct page *new_page = NULL;
 
-	if (PageHuge(page))
+	if (PageHuge(page)) {
+		/*
+		 * HugeTLB doesn't support encryption. We shouldn't see
+		 * such pages.
+		 */
+		WARN_ON(page_keyid(page));
 		return alloc_huge_page_nodemask(page_hstate(compound_head(page)),
 				preferred_nid, nodemask);
+	}
 
 	if (PageTransHuge(page)) {
 		gfp_mask |= GFP_TRANSHUGE;
@@ -50,8 +56,8 @@ static inline struct page *new_page_nodemask(struct page *page,
 	if (PageHighMem(page) || (zone_idx(page_zone(page)) == ZONE_MOVABLE))
 		gfp_mask |= __GFP_HIGHMEM;
 
-	new_page = __alloc_pages_nodemask(gfp_mask, order,
-				preferred_nid, nodemask);
+	new_page = __alloc_pages_nodemask_keyid(gfp_mask, order,
+				preferred_nid, nodemask, page_keyid(page));
 
 	if (new_page && PageTransHuge(new_page))
 		prep_transhuge_page(new_page);
mm/compaction.c +1 -0
index faca45ebe62d..fd51aa32ad96 100644
@@ -1187,6 +1187,7 @@ static struct page *compaction_alloc(struct page *migratepage,
 	list_del(&freepage->lru);
 	cc->nr_freepages--;
 
+	prep_encrypted_page(freepage, 0, page_keyid(migratepage), false);
 	return freepage;
 }
 
mm/mempolicy.c +21 -7
index 9ac49ef17b4e..b0fc42642f8f 100644
@@ -920,22 +920,28 @@ static void migrate_page_add(struct page *page, struct list_head *pagelist,
 /* page allocation callback for NUMA node migration */
 struct page *alloc_new_node_page(struct page *page, unsigned long node)
 {
-	if (PageHuge(page))
+	if (PageHuge(page)) {
+		/*
+		 * HugeTLB doesn't support encryption. We shouldn't see
+		 * such pages.
+		 */
+		WARN_ON(page_keyid(page));
 		return alloc_huge_page_node(page_hstate(compound_head(page)),
 					node);
-	else if (PageTransHuge(page)) {
+	} else if (PageTransHuge(page)) {
 		struct page *thp;
 
-		thp = alloc_pages_node(node,
+		thp = alloc_pages_node_keyid(node, page_keyid(page),
 			(GFP_TRANSHUGE | __GFP_THISNODE),
 			HPAGE_PMD_ORDER);
 		if (!thp)
 			return NULL;
 		prep_transhuge_page(thp);
 		return thp;
-	} else
-		return __alloc_pages_node(node, GFP_HIGHUSER_MOVABLE |
-						    __GFP_THISNODE, 0);
+	} else {
+		return __alloc_pages_node_keyid(node, page_keyid(page),
+				GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 0);
+	}
 }
 
 /*
@@ -2012,9 +2018,16 @@ alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
 {
 	struct mempolicy *pol;
 	struct page *page;
-	int preferred_nid;
+	bool zero = false;
+	int keyid, preferred_nid;
 	nodemask_t *nmask;
 
+	keyid = vma_keyid(vma);
+	if (keyid && (gfp & __GFP_ZERO)) {
+		zero = true;
+		gfp &= ~__GFP_ZERO;
+	}
+
 	pol = get_vma_policy(vma, addr);
 
 	if (pol->mode == MPOL_INTERLEAVE) {
@@ -2057,6 +2070,7 @@ alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
 	page = __alloc_pages_nodemask(gfp, order, preferred_nid, nmask);
 	mpol_cond_put(pol);
 out:
+	prep_encrypted_page(page, order, keyid, zero);
 	return page;
 }
 
mm/migrate.c +2 -2
index 8c0af0f7cab1..eb8dea219dcb 100644
@@ -1847,7 +1847,7 @@ static struct page *alloc_misplaced_dst_page(struct page *page,
 	int nid = (int) data;
 	struct page *newpage;
 
-	newpage = __alloc_pages_node(nid,
+	newpage = __alloc_pages_node_keyid(nid, page_keyid(page),
 					 (GFP_HIGHUSER_MOVABLE |
 					  __GFP_THISNODE | __GFP_NOMEMALLOC |
 					  __GFP_NORETRY | __GFP_NOWARN) &
@@ -2030,7 +2030,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,
 	if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
 		goto out_dropref;
 
-	new_page = alloc_pages_node(node,
+	new_page = alloc_pages_node_keyid(node, page_keyid(page),
 		(GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
 		HPAGE_PMD_ORDER);
 	if (!new_page)
mm/page_alloc.c +47 -0

59 lines not on this page · raw message

Signature · 1 line
2.18.0
#6 lore raw

PATCH v4 5/18 mm/khugepaged: Handle encrypted pages

In reply to Kirill A. Shutemov

khugepaged allocates page in advance, before we found a VMA for collapse. We don't yet know which KeyID to use for the allocation.

The page is allocated with KeyID-0. Once we know that the VMA is suitable for collapsing, we prepare the page for KeyID we need, based on vma_keyid().

---
 mm/khugepaged.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
mm/khugepaged.c +10 -0
index d7b2a4bf8671..4dff3c114501 100644
@@ -1056,6 +1056,16 @@ static void collapse_huge_page(struct mm_struct *mm,
 	 */
 	anon_vma_unlock_write(vma->anon_vma);
 
+	/*
+	 * At this point new_page is allocated as non-encrypted.
+	 * If VMA's KeyID is non-zero, we need to prepare it to be encrypted
+	 * before coping data.
+	 */
+	if (vma_keyid(vma)) {
+		prep_encrypted_page(new_page, HPAGE_PMD_ORDER,
+				vma_keyid(vma), false);
+	}
+
 	__collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
 	pte_unmap(pte);
 	__SetPageUptodate(new_page);
Signature · 1 line
2.18.0
#7 lore raw

PATCH v4 6/18 x86/mm: Mask out KeyID bits from page table entry pfn

In reply to Kirill A. Shutemov

MKTME claims several upper bits of the physical address in a page table entry to encode KeyID. It effectively shrinks number of bits for physical address. We should exclude KeyID bits from physical addresses.

For instance, if CPU enumerates 52 physical address bits and number of bits claimed for KeyID is 6, bits 51:46 must not be threated as part physical address.

This patch adjusts __PHYSICAL_MASK during MKTME enumeration.

---
 arch/x86/kernel/cpu/intel.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/x86/kernel/cpu/intel.c +23 -0
index eb75564f2d25..bf2caf9d52dd 100644
@@ -571,6 +571,29 @@ static void detect_tme(struct cpuinfo_x86 *c)
 		mktme_status = MKTME_ENABLED;
 	}
 
+#ifdef CONFIG_X86_INTEL_MKTME
+	if (mktme_status == MKTME_ENABLED && nr_keyids) {
+		/*
+		 * Mask out bits claimed from KeyID from physical address mask.
+		 *
+		 * For instance, if a CPU enumerates 52 physical address bits
+		 * and number of bits claimed for KeyID is 6, bits 51:46 of
+		 * physical address is unusable.
+		 */
+		phys_addr_t keyid_mask;
+
+		keyid_mask = GENMASK_ULL(c->x86_phys_bits - 1, c->x86_phys_bits - keyid_bits);
+		physical_mask &= ~keyid_mask;
+	} else {
+		/*
+		 * Reset __PHYSICAL_MASK.
+		 * Maybe needed if there's inconsistent configuation
+		 * between CPUs.
+		 */
+		physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
+	}
+#endif
+
 	/*
 	 * KeyID bits effectively lower the number of physical address
 	 * bits.  Update cpuinfo_x86::x86_phys_bits accordingly.
Signature · 1 line
2.18.0
#8 lore raw

PATCH v4 7/18 x86/mm: Introduce variables to store number, shift and mask of KeyIDs

In reply to Kirill A. Shutemov

mktme_nr_keyids holds number of KeyIDs available for MKTME, excluding KeyID zero which used by TME. MKTME KeyIDs start from 1.

mktme_keyid_shift holds shift of KeyID within physical address.

mktme_keyid_mask holds mask to extract KeyID from physical address.

---
 arch/x86/include/asm/mktme.h | 16 ++++++++++++++++
 arch/x86/kernel/cpu/intel.c  | 12 ++++++++----
 arch/x86/mm/Makefile         |  2 ++
 arch/x86/mm/mktme.c          |  5 +++++
 4 files changed, 31 insertions(+), 4 deletions(-)
 create mode 100644 arch/x86/include/asm/mktme.h
 create mode 100644 arch/x86/mm/mktme.c
arch/x86/include/asm/mktme.h new file +16 -0
new file mode 100644
index 000000000000..df31876ec48c
@@ -0,0 +1,16 @@
+#ifndef	_ASM_X86_MKTME_H
+#define	_ASM_X86_MKTME_H
+
+#include <linux/types.h>
+
+#ifdef CONFIG_X86_INTEL_MKTME
+extern phys_addr_t mktme_keyid_mask;
+extern int mktme_nr_keyids;
+extern int mktme_keyid_shift;
+#else
+#define mktme_keyid_mask	((phys_addr_t)0)
+#define mktme_nr_keyids		0
+#define mktme_keyid_shift	0
+#endif
+
+#endif
arch/x86/kernel/cpu/intel.c +8 -4
index bf2caf9d52dd..efc9e9fc47d4 100644
@@ -573,6 +573,9 @@ static void detect_tme(struct cpuinfo_x86 *c)
 
 #ifdef CONFIG_X86_INTEL_MKTME
 	if (mktme_status == MKTME_ENABLED && nr_keyids) {
+		mktme_nr_keyids = nr_keyids;
+		mktme_keyid_shift = c->x86_phys_bits - keyid_bits;
+
 		/*
 		 * Mask out bits claimed from KeyID from physical address mask.
 		 *
@@ -580,10 +583,8 @@ static void detect_tme(struct cpuinfo_x86 *c)
 		 * and number of bits claimed for KeyID is 6, bits 51:46 of
 		 * physical address is unusable.
 		 */
-		phys_addr_t keyid_mask;
-
-		keyid_mask = GENMASK_ULL(c->x86_phys_bits - 1, c->x86_phys_bits - keyid_bits);
-		physical_mask &= ~keyid_mask;
+		mktme_keyid_mask = GENMASK_ULL(c->x86_phys_bits - 1, mktme_keyid_shift);
+		physical_mask &= ~mktme_keyid_mask;
 	} else {
 		/*
 		 * Reset __PHYSICAL_MASK.
@@ -591,6 +592,9 @@ static void detect_tme(struct cpuinfo_x86 *c)
 		 * between CPUs.
 		 */
 		physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
+		mktme_keyid_mask = 0;
+		mktme_keyid_shift = 0;
+		mktme_nr_keyids = 0;
 	}
 #endif
 
arch/x86/mm/Makefile +2 -0
index 4b101dd6e52f..4ebee899c363 100644
@@ -53,3 +53,5 @@ obj-$(CONFIG_PAGE_TABLE_ISOLATION)		+= pti.o
 obj-$(CONFIG_AMD_MEM_ENCRYPT)	+= mem_encrypt.o
 obj-$(CONFIG_AMD_MEM_ENCRYPT)	+= mem_encrypt_identity.o
 obj-$(CONFIG_AMD_MEM_ENCRYPT)	+= mem_encrypt_boot.o
+
+obj-$(CONFIG_X86_INTEL_MKTME)	+= mktme.o
arch/x86/mm/mktme.c new file +5 -0
new file mode 100644
index 000000000000..467f1b26c737
@@ -0,0 +1,5 @@
+#include <asm/mktme.h>
+
+phys_addr_t mktme_keyid_mask;
+int mktme_nr_keyids;
+int mktme_keyid_shift;
Signature · 1 line
2.18.0
#9 lore raw

Re: [PATCHv4 07/18] x86/mm: Introduce variables to store number, shift and mask of KeyIDs

arch/x86/mm/Makefile obj-$(CONFIG_PAGE_TABLE_ISOLATION) += pti.o · line 57

Quoted 10 lines
arch/x86/mm/Makefile +2 -0
index 4b101dd6e52f..4ebee899c363 100644
@@ -53,3 +53,5 @@ obj-$(CONFIG_PAGE_TABLE_ISOLATION)		+= pti.o
 obj-$(CONFIG_AMD_MEM_ENCRYPT)	+= mem_encrypt.o
 obj-$(CONFIG_AMD_MEM_ENCRYPT)	+= mem_encrypt_identity.o
 obj-$(CONFIG_AMD_MEM_ENCRYPT)	+= mem_encrypt_boot.o
+
+obj-$(CONFIG_X86_INTEL_MKTME)	+= mktme.o

Any particular reason to have x86 in the CONFIG?

Quoted 14 lines
arch/x86/mm/mktme.c new file +5 -0
new file mode 100644
index 000000000000..467f1b26c737
@@ -0,0 +1,5 @@
+#include <asm/mktme.h>
+
+phys_addr_t mktme_keyid_mask;
+int mktme_nr_keyids;
+int mktme_keyid_shift;
Signature · 1 line
2.18.0
#10 lore raw

Re: [PATCHv4 07/18] x86/mm: Introduce variables to store number, shift and mask of KeyIDs

Quoted 4 lines · Konrad Rzeszutek Wilk
Quoted 4 lines
arch/x86/mm/Makefile +0 -0
index 4b101dd6e52f..4ebee899c363 100644
Quoted 8 lines

arch/x86/mm/Makefile obj-$(CONFIG_PAGE_TABLE_ISOLATION) += pti.o · line 57

Quoted 6 lines
arch/x86/mm/Makefile +2 -0
@@ -53,3 +53,5 @@ obj-$(CONFIG_PAGE_TABLE_ISOLATION)		+= pti.o
 obj-$(CONFIG_AMD_MEM_ENCRYPT)	+= mem_encrypt.o
 obj-$(CONFIG_AMD_MEM_ENCRYPT)	+= mem_encrypt_identity.o
 obj-$(CONFIG_AMD_MEM_ENCRYPT)	+= mem_encrypt_boot.o
+
+obj-$(CONFIG_X86_INTEL_MKTME)	+= mktme.o

Any particular reason to have x86 in the CONFIG?

It is consistent with MPX and protection keys.

Signature · 1 line
 Kirill A. Shutemov
#11 lore raw

PATCH v4 8/18 x86/mm: Preserve KeyID on pte_modify() and pgprot_modify()

In reply to Kirill A. Shutemov

An encrypted VMA will have KeyID stored in vma->vm_page_prot. This way we don't need to do anything special to setup encrypted page table entries and don't need to reserve space for KeyID in a VMA.

This patch changes _PAGE_CHG_MASK to include KeyID bits. Otherwise they are going to be stripped from vm_page_prot on the first pgprot_modify().

Define PTE_PFN_MASK_MAX similar to PTE_PFN_MASK but based on __PHYSICAL_MASK_SHIFT. This way we include whole range of bits architecturally available for PFN without referencing physical_mask and mktme_keyid_mask variables.

---
 arch/x86/include/asm/pgtable_types.h | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
arch/x86/include/asm/pgtable_types.h +14 -1
index 99fff853c944..3731f7e08757 100644
@@ -120,8 +120,21 @@
  * protection key is treated like _PAGE_RW, for
  * instance, and is *not* included in this mask since
  * pte_modify() does modify it.
+ *
+ * They include the physical address and the memory encryption keyID.
+ * The paddr and the keyID never occupy the same bits at the same time.
+ * But, a given bit might be used for the keyID on one system and used for
+ * the physical address on another. As an optimization, we manage them in
+ * one unit here since their combination always occupies the same hardware
+ * bits. PTE_PFN_MASK_MAX stores combined mask.
+ *
+ * Cast PAGE_MASK to a signed type so that it is sign-extended if
+ * virtual addresses are 32-bits but physical addresses are larger
+ * (ie, 32-bit PAE).
  */
-#define _PAGE_CHG_MASK	(PTE_PFN_MASK | _PAGE_PCD | _PAGE_PWT |		\
+#define PTE_PFN_MASK_MAX \
+	(((signed long)PAGE_MASK) & ((1ULL << __PHYSICAL_MASK_SHIFT) - 1))
+#define _PAGE_CHG_MASK	(PTE_PFN_MASK_MAX | _PAGE_PCD | _PAGE_PWT |		\
 			 _PAGE_SPECIAL | _PAGE_ACCESSED | _PAGE_DIRTY |	\
 			 _PAGE_SOFT_DIRTY)
 #define _HPAGE_CHG_MASK (_PAGE_CHG_MASK | _PAGE_PSE)
Signature · 1 line
2.18.0
#12 lore raw

PATCH v4 9/18 x86/mm: Implement page_keyid() using page_ext

In reply to Kirill A. Shutemov

Store KeyID in bits 31:16 of extended page flags. These bits are unused.

page_keyid() returns zero until page_ext is ready. page_ext initializer enables static branch to indicate that page_keyid() can use page_ext. The same static branch will gate MKTME readiness in general.

We don't yet set KeyID for the page. It will come in the following patch that implements prep_encrypted_page(). All pages have KeyID-0 for now.

---
 arch/x86/include/asm/mktme.h |  7 +++++++
 arch/x86/include/asm/page.h  |  1 +
 arch/x86/mm/mktme.c          | 34 ++++++++++++++++++++++++++++++++++
 include/linux/page_ext.h     | 11 ++++++++++-
 mm/page_ext.c                |  3 +++
 5 files changed, 55 insertions(+), 1 deletion(-)
arch/x86/include/asm/mktme.h +7 -0
index df31876ec48c..7266494b4f0a 100644
@@ -2,11 +2,18 @@
 #define	_ASM_X86_MKTME_H
 
 #include <linux/types.h>
+#include <linux/page_ext.h>
 
 #ifdef CONFIG_X86_INTEL_MKTME
 extern phys_addr_t mktme_keyid_mask;
 extern int mktme_nr_keyids;
 extern int mktme_keyid_shift;
+
+extern struct page_ext_operations page_mktme_ops;
+
+#define page_keyid page_keyid
+int page_keyid(const struct page *page);
+
 #else
 #define mktme_keyid_mask	((phys_addr_t)0)
 #define mktme_nr_keyids		0
arch/x86/include/asm/page.h +1 -0
index 7555b48803a8..39af59487d5f 100644
@@ -19,6 +19,7 @@
 struct page;
 
 #include <linux/range.h>
+#include <asm/mktme.h>
 extern struct range pfn_mapped[];
 extern int nr_pfn_mapped;
 
arch/x86/mm/mktme.c +34 -0
index 467f1b26c737..09cbff678b9f 100644
@@ -3,3 +3,37 @@
 phys_addr_t mktme_keyid_mask;
 int mktme_nr_keyids;
 int mktme_keyid_shift;
+
+static DEFINE_STATIC_KEY_FALSE(mktme_enabled_key);
+
+static inline bool mktme_enabled(void)
+{
+	return static_branch_unlikely(&mktme_enabled_key);
+}
+
+int page_keyid(const struct page *page)
+{
+	if (!mktme_enabled())
+		return 0;
+
+	return lookup_page_ext(page)->keyid;
+}
+EXPORT_SYMBOL(page_keyid);
+
+static bool need_page_mktme(void)
+{
+	/* Make sure keyid doesn't collide with extended page flags */
+	BUILD_BUG_ON(__NR_PAGE_EXT_FLAGS > 16);
+
+	return !!mktme_nr_keyids;
+}
+
+static void init_page_mktme(void)
+{
+	static_branch_enable(&mktme_enabled_key);
+}
+
+struct page_ext_operations page_mktme_ops = {
+	.need = need_page_mktme,
+	.init = init_page_mktme,
+};
include/linux/page_ext.h +10 -1
index f84f167ec04c..d9c5aae9523f 100644
@@ -23,6 +23,7 @@ enum page_ext_flags {
 	PAGE_EXT_YOUNG,
 	PAGE_EXT_IDLE,
 #endif
+	__NR_PAGE_EXT_FLAGS
 };
 
 /*
@@ -33,7 +34,15 @@ enum page_ext_flags {
  * then the page_ext for pfn always exists.
  */
 struct page_ext {
-	unsigned long flags;
+	union {
+		unsigned long flags;
+#ifdef CONFIG_X86_INTEL_MKTME
+		struct {
+			unsigned short __pad;
+			unsigned short keyid;
+		};
+#endif
+	};
 };
 
 extern void pgdat_page_ext_init(struct pglist_data *pgdat);
mm/page_ext.c +3 -0
index a9826da84ccb..036658229842 100644
@@ -68,6 +68,9 @@ static struct page_ext_operations *page_ext_ops[] = {
 #if defined(CONFIG_IDLE_PAGE_TRACKING) && !defined(CONFIG_64BIT)
 	&page_idle_ops,
 #endif
+#ifdef CONFIG_X86_INTEL_MKTME
+	&page_mktme_ops,
+#endif
 };
 
 static unsigned long total_usage;
Signature · 1 line
2.18.0
#13 lore raw

PATCH v4 10/18 x86/mm: Implement vma_keyid()

In reply to Kirill A. Shutemov

We store KeyID in upper bits for vm_page_prot that match position of KeyID in PTE. vma_keyid() extracts KeyID from vm_page_prot.

With KeyID in vm_page_prot we don't need to modify any page table helper to propagate the KeyID to page table entires.

---
 arch/x86/include/asm/mktme.h |  5 +++++
 arch/x86/mm/mktme.c          | 12 ++++++++++++
 2 files changed, 17 insertions(+)
arch/x86/include/asm/mktme.h +5 -0
index 7266494b4f0a..f0b7844e36a4 100644
@@ -4,6 +4,8 @@
 #include <linux/types.h>
 #include <linux/page_ext.h>
 
+struct vm_area_struct;
+
 #ifdef CONFIG_X86_INTEL_MKTME
 extern phys_addr_t mktme_keyid_mask;
 extern int mktme_nr_keyids;
@@ -14,6 +16,9 @@ extern struct page_ext_operations page_mktme_ops;
 #define page_keyid page_keyid
 int page_keyid(const struct page *page);
 
+#define vma_keyid vma_keyid
+int vma_keyid(struct vm_area_struct *vma);
+
 #else
 #define mktme_keyid_mask	((phys_addr_t)0)
 #define mktme_nr_keyids		0
arch/x86/mm/mktme.c +12 -0
index 09cbff678b9f..a1f40ee61b25 100644
@@ -1,3 +1,4 @@
+#include <linux/mm.h>
 #include <asm/mktme.h>
 
 phys_addr_t mktme_keyid_mask;
@@ -37,3 +38,14 @@ struct page_ext_operations page_mktme_ops = {
 	.need = need_page_mktme,
 	.init = init_page_mktme,
 };
+
+int vma_keyid(struct vm_area_struct *vma)
+{
+	pgprotval_t prot;
+
+	if (!mktme_enabled())
+		return 0;
+
+	prot = pgprot_val(vma->vm_page_prot);
+	return (prot & mktme_keyid_mask) >> mktme_keyid_shift;
+}
Signature · 1 line
2.18.0
#14 lore raw

PATCH v4 11/18 x86/mm: Implement prep_encrypted_page() and arch_free_page()

In reply to Kirill A. Shutemov

The hardware/CPU does not enforce coherency between mappings of the same physical page with different KeyIDs or encryption keys.
We are responsible for cache management.

Flush cache on allocating encrypted page and on returning the page to the free pool.

prep_encrypted_page() also takes care about zeroing the page. We have to do this after KeyID is set for the page.

---
 arch/x86/include/asm/mktme.h |  6 +++++
 arch/x86/mm/mktme.c          | 49 ++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
arch/x86/include/asm/mktme.h +6 -0
index f0b7844e36a4..44409b8bbaca 100644
@@ -19,6 +19,12 @@ int page_keyid(const struct page *page);
 #define vma_keyid vma_keyid
 int vma_keyid(struct vm_area_struct *vma);
 
+#define prep_encrypted_page prep_encrypted_page
+void prep_encrypted_page(struct page *page, int order, int keyid, bool zero);
+
+#define HAVE_ARCH_FREE_PAGE
+void arch_free_page(struct page *page, int order);
+
 #else
 #define mktme_keyid_mask	((phys_addr_t)0)
 #define mktme_nr_keyids		0
arch/x86/mm/mktme.c +49 -0

56 lines not on this page · raw message

Signature · 1 line
2.18.0
#15 lore raw

PATCH v4 12/18 x86/mm: Rename CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING

In reply to Kirill A. Shutemov

Rename the option to CONFIG_MEMORY_PHYSICAL_PADDING. It will be used not only for KASLR.

---
 arch/x86/Kconfig    | 2 +-
 arch/x86/mm/kaslr.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/Kconfig +1 -1
index c4d64b19acff..fa5e1ec09247 100644
@@ -2197,7 +2197,7 @@ config RANDOMIZE_MEMORY
 
 	   If unsure, say Y.
 
-config RANDOMIZE_MEMORY_PHYSICAL_PADDING
+config MEMORY_PHYSICAL_PADDING
 	hex "Physical memory mapping padding" if EXPERT
 	depends on RANDOMIZE_MEMORY
 	default "0xa" if MEMORY_HOTPLUG
arch/x86/mm/kaslr.c +1 -1
index 61db77b0eda9..4408cd9a3bef 100644
@@ -102,7 +102,7 @@ void __init kernel_randomize_memory(void)
 	 */
 	BUG_ON(kaslr_regions[0].base != &page_offset_base);
 	memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
-		CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
+		CONFIG_MEMORY_PHYSICAL_PADDING;
 
 	/* Adapt phyiscal memory region size based on available memory */
 	if (memory_tb < kaslr_regions[0].size_tb)
Signature · 1 line
2.18.0
#16 lore raw

PATCH v4 13/18 x86/mm: Allow to disable MKTME after enumeration

In reply to Kirill A. Shutemov

The new helper mktme_disable() allows to disable MKTME even if it's enumerated successfully. MKTME initialization may fail and this functionality allows system to boot regardless of the failure.

MKTME needs per-KeyID direct mapping. It requires a lot more virtual address space which may be a problem in 4-level paging mode. If the system has more physical memory than we can handle with MKTME. The feature allows to fail MKTME, but boot the system successfully.

---
 arch/x86/include/asm/mktme.h | 2 ++
 arch/x86/kernel/cpu/intel.c  | 5 +----
 arch/x86/mm/mktme.c          | 9 +++++++++
 3 files changed, 12 insertions(+), 4 deletions(-)
arch/x86/include/asm/mktme.h +2 -0
index 44409b8bbaca..ebbee6a0c495 100644
@@ -6,6 +6,8 @@
 
 struct vm_area_struct;
 
+void mktme_disable(void);
+
 #ifdef CONFIG_X86_INTEL_MKTME
 extern phys_addr_t mktme_keyid_mask;
 extern int mktme_nr_keyids;
arch/x86/kernel/cpu/intel.c +1 -4
index efc9e9fc47d4..75e3b2602b4a 100644
@@ -591,10 +591,7 @@ static void detect_tme(struct cpuinfo_x86 *c)
 		 * Maybe needed if there's inconsistent configuation
 		 * between CPUs.
 		 */
-		physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
-		mktme_keyid_mask = 0;
-		mktme_keyid_shift = 0;
-		mktme_nr_keyids = 0;
+		mktme_disable();
 	}
 #endif
 
arch/x86/mm/mktme.c +9 -0
index 1194496633ce..bb6210dbcf0e 100644
@@ -13,6 +13,15 @@ static inline bool mktme_enabled(void)
 	return static_branch_unlikely(&mktme_enabled_key);
 }
 
+void mktme_disable(void)
+{
+	physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
+	mktme_keyid_mask = 0;
+	mktme_keyid_shift = 0;
+	mktme_nr_keyids = 0;
+	static_branch_disable(&mktme_enabled_key);
+}
+
 int page_keyid(const struct page *page)
 {
 	if (!mktme_enabled())
Signature · 1 line
2.18.0
#17 lore raw

Re: [PATCHv4 13/18] x86/mm: Allow to disable MKTME after enumeration

Quoted 7 lines · Kirill A. Shutemov

The new helper mktme_disable() allows to disable MKTME even if it's enumerated successfully. MKTME initialization may fail and this functionality allows system to boot regardless of the failure.

MKTME needs per-KeyID direct mapping. It requires a lot more virtual address space which may be a problem in 4-level paging mode. If the system has more physical memory than we can handle with MKTME.

.. then what should happen?

Quoted 61 lines

The feature allows to fail MKTME, but boot the system successfully.

---
 arch/x86/include/asm/mktme.h | 2 ++
 arch/x86/kernel/cpu/intel.c  | 5 +----
 arch/x86/mm/mktme.c          | 9 +++++++++
 3 files changed, 12 insertions(+), 4 deletions(-)
arch/x86/include/asm/mktme.h +2 -0
index 44409b8bbaca..ebbee6a0c495 100644
@@ -6,6 +6,8 @@
 
 struct vm_area_struct;
 
+void mktme_disable(void);
+
 #ifdef CONFIG_X86_INTEL_MKTME
 extern phys_addr_t mktme_keyid_mask;
 extern int mktme_nr_keyids;
arch/x86/kernel/cpu/intel.c +1 -4
index efc9e9fc47d4..75e3b2602b4a 100644
@@ -591,10 +591,7 @@ static void detect_tme(struct cpuinfo_x86 *c)
 		 * Maybe needed if there's inconsistent configuation
 		 * between CPUs.
 		 */
-		physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
-		mktme_keyid_mask = 0;
-		mktme_keyid_shift = 0;
-		mktme_nr_keyids = 0;
+		mktme_disable();
 	}
 #endif
 
arch/x86/mm/mktme.c +9 -0
index 1194496633ce..bb6210dbcf0e 100644
@@ -13,6 +13,15 @@ static inline bool mktme_enabled(void)
 	return static_branch_unlikely(&mktme_enabled_key);
 }
 
+void mktme_disable(void)
+{
+	physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
+	mktme_keyid_mask = 0;
+	mktme_keyid_shift = 0;
+	mktme_nr_keyids = 0;
+	static_branch_disable(&mktme_enabled_key);
+}
+
 int page_keyid(const struct page *page)
 {
 	if (!mktme_enabled())
Signature · 1 line
2.18.0
#18 lore raw

Re: [PATCHv4 13/18] x86/mm: Allow to disable MKTME after enumeration

Quoted 10 lines · Konrad Rzeszutek Wilk
Quoted 7 lines · Kirill A. Shutemov

The new helper mktme_disable() allows to disable MKTME even if it's enumerated successfully. MKTME initialization may fail and this functionality allows system to boot regardless of the failure.

MKTME needs per-KeyID direct mapping. It requires a lot more virtual address space which may be a problem in 4-level paging mode. If the system has more physical memory than we can handle with MKTME.

.. then what should happen?

We fail MKTME initialization and boot the system. See next sentence.

Quoted 62 lines
Quoted 61 lines

The feature allows to fail MKTME, but boot the system successfully.

---
 arch/x86/include/asm/mktme.h | 2 ++
 arch/x86/kernel/cpu/intel.c  | 5 +----
 arch/x86/mm/mktme.c          | 9 +++++++++
 3 files changed, 12 insertions(+), 4 deletions(-)
arch/x86/include/asm/mktme.h +2 -0
index 44409b8bbaca..ebbee6a0c495 100644
@@ -6,6 +6,8 @@
 
 struct vm_area_struct;
 
+void mktme_disable(void);
+
 #ifdef CONFIG_X86_INTEL_MKTME
 extern phys_addr_t mktme_keyid_mask;
 extern int mktme_nr_keyids;
arch/x86/kernel/cpu/intel.c +1 -4
index efc9e9fc47d4..75e3b2602b4a 100644
@@ -591,10 +591,7 @@ static void detect_tme(struct cpuinfo_x86 *c)
 		 * Maybe needed if there's inconsistent configuation
 		 * between CPUs.
 		 */
-		physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
-		mktme_keyid_mask = 0;
-		mktme_keyid_shift = 0;
-		mktme_nr_keyids = 0;
+		mktme_disable();
 	}
 #endif
 
arch/x86/mm/mktme.c +9 -0
index 1194496633ce..bb6210dbcf0e 100644
@@ -13,6 +13,15 @@ static inline bool mktme_enabled(void)
 	return static_branch_unlikely(&mktme_enabled_key);
 }
 
+void mktme_disable(void)
+{
+	physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
+	mktme_keyid_mask = 0;
+	mktme_keyid_shift = 0;
+	mktme_nr_keyids = 0;
+	static_branch_disable(&mktme_enabled_key);
+}
+
 int page_keyid(const struct page *page)
 {
 	if (!mktme_enabled())
Signature · 1 line
2.18.0
Signature · 1 line
 Kirill A. Shutemov
#19 lore raw

Re: [PATCHv4 13/18] x86/mm: Allow to disable MKTME after enumeration

Quoted 6 lines · Kirill A. Shutemov
Quoted 5 lines · Konrad Rzeszutek Wilk
Quoted 4 lines · Kirill A. Shutemov

The new helper mktme_disable() allows to disable MKTME even if it's enumerated successfully. MKTME initialization may fail and this functionality allows system to boot regardless of the failure.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

MKTME needs per-KeyID direct mapping. It requires a lot more

virtual

Quoted 4 lines
Quoted 2 lines

address space which may be a problem in 4-level paging mode. If the system has more physical memory than we can handle with MKTME.

.. then what should happen?

We fail MKTME initialization and boot the system. See next sentence.

Perhaps you can then remove the "." and join the sentences

Quoted 65 lines
Quoted 11 lines

Review

Quoted 11 lines

The feature allows to fail MKTME, but boot the system successfully.

---
 arch/x86/include/asm/mktme.h | 2 ++
 arch/x86/kernel/cpu/intel.c  | 5 +----
 arch/x86/mm/mktme.c          | 9 +++++++++
 3 files changed, 12 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/arch/x86/include/asm/mktme.h

Quoted 5 lines
Quoted 5 lines

index 44409b8bbaca..ebbee6a0c495 100644

arch/x86/include/asm/mktme.h +0 -0
@@ -6,6 +6,8 @@
 
Quoted 8 lines

Review

Quoted 8 lines
arch/x86/include/asm/mktme.h +2 -0
 struct vm_area_struct;
 
+void mktme_disable(void);
+
 #ifdef CONFIG_X86_INTEL_MKTME
 extern phys_addr_t mktme_keyid_mask;
 extern int mktme_nr_keyids;
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/intel.c

Quoted 39 lines
Quoted 38 lines

index efc9e9fc47d4..75e3b2602b4a 100644

arch/x86/kernel/cpu/intel.c +1 -4
@@ -591,10 +591,7 @@ static void detect_tme(struct cpuinfo_x86 *c)
 		 * Maybe needed if there's inconsistent configuation
 		 * between CPUs.
 		 */
-		physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
-		mktme_keyid_mask = 0;
-		mktme_keyid_shift = 0;
-		mktme_nr_keyids = 0;
+		mktme_disable();
 	}
 #endif
 
arch/x86/mm/mktme.c +9 -0
index 1194496633ce..bb6210dbcf0e 100644
@@ -13,6 +13,15 @@ static inline bool mktme_enabled(void)
 	return static_branch_unlikely(&mktme_enabled_key);
 }
 
+void mktme_disable(void)
+{
+	physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
+	mktme_keyid_mask = 0;
+	mktme_keyid_shift = 0;
+	mktme_nr_keyids = 0;
+	static_branch_disable(&mktme_enabled_key);
+}
+
 int page_keyid(const struct page *page)
 {
 	if (!mktme_enabled())
Signature · 1 line
2.18.0
#20 lore raw

PATCH v4 14/18 x86/mm: Detect MKTME early

In reply to Kirill A. Shutemov

We need to know number of KeyIDs before KALSR is initialized. Number of KeyIDs would determinate how much address space would be needed for per-KeyID direct mapping.

KALSR initialization happens before full CPU initizliation is complete. Move detect_tme() call to early_init_intel().

---
 arch/x86/kernel/cpu/intel.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/intel.c +5 -3
index 75e3b2602b4a..39830806dd42 100644
@@ -158,6 +158,8 @@ static bool bad_spectre_microcode(struct cpuinfo_x86 *c)
 	return false;
 }
 
+static void detect_tme(struct cpuinfo_x86 *c);
+
 static void early_init_intel(struct cpuinfo_x86 *c)
 {
 	u64 misc_enable;
@@ -301,6 +303,9 @@ static void early_init_intel(struct cpuinfo_x86 *c)
 	}
 
 	check_mpx_erratum(c);
+
+	if (cpu_has(c, X86_FEATURE_TME))
+		detect_tme(c);
 }
 
 #ifdef CONFIG_X86_32
@@ -766,9 +771,6 @@ static void init_intel(struct cpuinfo_x86 *c)
 	if (cpu_has(c, X86_FEATURE_VMX))
 		detect_vmx_virtcap(c);
 
-	if (cpu_has(c, X86_FEATURE_TME))
-		detect_tme(c);
-
 	init_intel_energy_perf(c);
 
 	init_intel_misc_features(c);
Signature · 1 line
2.18.0
#21 lore raw

PATCH v4 15/18 x86/mm: Calculate direct mapping size

In reply to Kirill A. Shutemov

The kernel needs to have a way to access encrypted memory. We have two option on how approach it:

- Create temporary mappings every time kernel needs access to encrypted memory. That's basically brings highmem and its overhead back.

- Create multiple direct mappings, one per-KeyID. In this setup we don't need to create temporary mappings on the fly -- encrypted memory is permanently available in kernel address space.

We take the second approach as it has lower overhead.

It's worth noting that with per-KeyID direct mappings compromised kernel would give access to decrypted data right away without additional tricks to get memory mapped with the correct KeyID.

Per-KeyID mappings require a lot more virtual address space. On 4-level machine with 64 KeyIDs we max out 46-bit virtual address space dedicated for direct mapping with 1TiB of RAM. Given that we round up any calculation on direct mapping size to 1TiB, we effectively claim all 46-bit address space for direct mapping on such machine regardless of RAM size.

Increased usage of virtual address space has implications for KASLR:
we have less space for randomization. With 64 TiB claimed for direct mapping with 4-level we left with 27 TiB of entropy to place page_offset_base, vmalloc_base and vmemmap_base.

5-level paging provides much wider virtual address space and KASLR doesn't suffer significantly from per-KeyID direct mappings.

It's preferred to run MKTME with 5-level paging.

A direct mapping for each KeyID will be put next to each other in the virtual address space. We need to have a way to find boundaries of direct mapping for particular KeyID.

The new variable direct_mapping_size specifies the size of direct mapping. With the value, it's trivial to find direct mapping for
KeyID-N: PAGE_OFFSET + N * direct_mapping_size.

Size of direct mapping is calculated during KASLR setup. If KALSR is disabled it happens during MKTME initialization.

---
 Documentation/x86/x86_64/mm.txt |  4 ++++
 arch/x86/include/asm/page_64.h  |  1 +
 arch/x86/include/asm/setup.h    |  6 +++++
 arch/x86/kernel/head64.c        |  2 ++
 arch/x86/kernel/setup.c         |  3 +++
 arch/x86/mm/init_64.c           | 40 +++++++++++++++++++++++++++++++++
 arch/x86/mm/kaslr.c             | 11 ++++++---
 7 files changed, 64 insertions(+), 3 deletions(-)
Documentation/x86/x86_64/mm.txt +4 -0
index 5432a96d31ff..c5b92904090f 100644
@@ -61,6 +61,10 @@ The direct mapping covers all memory in the system up to the highest
 memory address (this means in some cases it can also include PCI memory
 holes).
 
+With MKTME, we have multiple direct mappings. One per-KeyID. They are put
+next to each other. PAGE_OFFSET + N * direct_mapping_size can be used to
+find direct mapping for KeyID-N.
+
 vmalloc space is lazily synchronized into the different PML4/PML5 pages of
 the processes using the page fault handler, with init_top_pgt as
 reference.
arch/x86/include/asm/page_64.h +1 -0
index 939b1cff4a7b..53c32af895ab 100644
@@ -14,6 +14,7 @@ extern unsigned long phys_base;
 extern unsigned long page_offset_base;
 extern unsigned long vmalloc_base;
 extern unsigned long vmemmap_base;
+extern unsigned long direct_mapping_size;
 
 static inline unsigned long __phys_addr_nodebug(unsigned long x)
 {
arch/x86/include/asm/setup.h +6 -0
index ae13bc974416..bcac5080cca5 100644
@@ -59,6 +59,12 @@ extern void x86_ce4100_early_setup(void);
 static inline void x86_ce4100_early_setup(void) { }
 #endif
 
+#ifdef CONFIG_MEMORY_PHYSICAL_PADDING
+void calculate_direct_mapping_size(void);
+#else
+static inline void calculate_direct_mapping_size(void) { }
+#endif
+
 #ifndef _SETUP
 
 #include <asm/espfix.h>
arch/x86/kernel/head64.c +2 -0
index 8047379e575a..854e8665aba0 100644
@@ -59,6 +59,8 @@ EXPORT_SYMBOL(vmalloc_base);
 unsigned long vmemmap_base __ro_after_init = __VMEMMAP_BASE_L4;
 EXPORT_SYMBOL(vmemmap_base);
 #endif
+unsigned long direct_mapping_size __ro_after_init = -1UL;
+EXPORT_SYMBOL(direct_mapping_size);
 
 #define __head	__section(.head.text)
 
arch/x86/kernel/setup.c +3 -0
index 2f86d883dd95..09ddbd142e3c 100644
@@ -1053,6 +1053,9 @@ void __init setup_arch(char **cmdline_p)
 	 */
 	init_cache_modes();
 
+	 /* direct_mapping_size has to be initialized before KASLR and MKTME */
+	calculate_direct_mapping_size();
+
 	/*
 	 * Define random base addresses for memory sections after max_pfn is
 	 * defined and before each memory section base is used.
arch/x86/mm/init_64.c +40 -0
index a688617c727e..6fc506f33e58 100644
@@ -1399,6 +1399,46 @@ unsigned long memory_block_size_bytes(void)
 	return memory_block_size_probed;
 }
 
+#ifdef CONFIG_MEMORY_PHYSICAL_PADDING
+void __init calculate_direct_mapping_size(void)
+{
+	unsigned long available_va;
+
+	/* 1/4 of virtual address space is didicated for direct mapping */
+	available_va = 1UL << (__VIRTUAL_MASK_SHIFT - 1);
+
+	/* How much memory the systrem has? */
+	direct_mapping_size = max_pfn << PAGE_SHIFT;
+	direct_mapping_size = round_up(direct_mapping_size, 1UL << 40);
+
+	if (!IS_ENABLED(CONFIG_X86_INTEL_MKTME) || !mktme_nr_keyids)
+		goto out;
+
+	/*
+	 * Not enough virtual address space to address all physical memory with
+	 * MKTME enabled. Even without padding.
+	 *
+	 * Disable MKTME instead.
+	 */
+	if (direct_mapping_size > available_va / (mktme_nr_keyids + 1)) {
+		pr_err("x86/mktme: Disabled. Not enough virtual address space\n");
+		pr_err("x86/mktme: Consider switching to 5-level paging\n");
+		mktme_disable();
+		goto out;
+	}
+
+	/*
+	 * Virtual address space is divided between per-KeyID direct mappings.
+	 */
+	available_va /= mktme_nr_keyids + 1;
+out:
+	/* Add padding, if there's enough virtual address space */
+	direct_mapping_size += (1UL << 40) * CONFIG_MEMORY_PHYSICAL_PADDING;
+	if (direct_mapping_size > available_va)
+		direct_mapping_size = available_va;
+}
+#endif
+
 #ifdef CONFIG_SPARSEMEM_VMEMMAP
 /*
  * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
arch/x86/mm/kaslr.c +8 -3
index 4408cd9a3bef..bf044ff50ec0 100644
@@ -101,10 +101,15 @@ void __init kernel_randomize_memory(void)
 	 * add padding if needed (especially for memory hotplug support).
 	 */
 	BUG_ON(kaslr_regions[0].base != &page_offset_base);
-	memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
-		CONFIG_MEMORY_PHYSICAL_PADDING;
 
-	/* Adapt phyiscal memory region size based on available memory */
+	/*
+	 * Calculate space required to map all physical memory.
+	 * In case of MKTME, we map physical memory multiple times, one for
+	 * each KeyID. If MKTME is disabled mktme_nr_keyids is 0.
+	 */
+	memory_tb = (direct_mapping_size * (mktme_nr_keyids + 1)) >> TB_SHIFT;
+
+	/* Adapt physical memory region size based on available memory */
 	if (memory_tb < kaslr_regions[0].size_tb)
 		kaslr_regions[0].size_tb = memory_tb;
 
Signature · 1 line
2.18.0
#22 lore raw

Re: [PATCHv4 15/18] x86/mm: Calculate direct mapping size

Quoted 28 lines · Kirill A. Shutemov

The kernel needs to have a way to access encrypted memory. We have two option on how approach it:

- Create temporary mappings every time kernel needs access to encrypted memory. That's basically brings highmem and its overhead back.

- Create multiple direct mappings, one per-KeyID. In this setup we don't need to create temporary mappings on the fly -- encrypted memory is permanently available in kernel address space.

We take the second approach as it has lower overhead.

It's worth noting that with per-KeyID direct mappings compromised kernel would give access to decrypted data right away without additional tricks to get memory mapped with the correct KeyID.

Per-KeyID mappings require a lot more virtual address space. On 4-level machine with 64 KeyIDs we max out 46-bit virtual address space dedicated for direct mapping with 1TiB of RAM. Given that we round up any calculation on direct mapping size to 1TiB, we effectively claim all 46-bit address space for direct mapping on such machine regardless of RAM size.

Increased usage of virtual address space has implications for KASLR:
we have less space for randomization. With 64 TiB claimed for direct mapping with 4-level we left with 27 TiB of entropy to place page_offset_base, vmalloc_base and vmemmap_base.

Quoted 4 lines

5-level paging provides much wider virtual address space and KASLR doesn't suffer significantly from per-KeyID direct mappings.

It's preferred to run MKTME with 5-level paging.

Why not make this a config dependency then?

#23 lore raw

PATCH v4 16/18 x86/mm: Implement sync_direct_mapping()

In reply to Kirill A. Shutemov

For MKTME we use per-KeyID direct mappings. This allows kernel to have access to encrypted memory.

sync_direct_mapping() sync per-KeyID direct mappings with a canonical one -- KeyID-0.

The function tracks changes in the canonical mapping:
- creating or removing chunks of the translation tree;
- changes in mapping flags (i.e. protection bits);
- splitting huge page mapping into a page table;
- replacing page table with a huge page mapping;

The function need to be called on every change to the direct mapping:
hotplug, hotremove, changes in permissions bits, etc.

The function is nop until MKTME is enabled.

---
 arch/x86/include/asm/mktme.h |   8 +
 arch/x86/mm/init_64.c        |  10 +
 arch/x86/mm/mktme.c          | 437 +++++++++++++++++++++++++++++++++++
 3 files changed, 455 insertions(+)
arch/x86/include/asm/mktme.h +8 -0
index ebbee6a0c495..ba83fba4f9b3 100644
@@ -27,10 +27,18 @@ void prep_encrypted_page(struct page *page, int order, int keyid, bool zero);
 #define HAVE_ARCH_FREE_PAGE
 void arch_free_page(struct page *page, int order);
 
+int sync_direct_mapping(void);
+
 #else
 #define mktme_keyid_mask	((phys_addr_t)0)
 #define mktme_nr_keyids		0
 #define mktme_keyid_shift	0
+
+static inline int sync_direct_mapping(void)
+{
+	return 0;
+}
+
 #endif
 
 #endif
arch/x86/mm/init_64.c +10 -0
index 6fc506f33e58..5a20fe465947 100644
@@ -698,6 +698,7 @@ kernel_physical_mapping_init(unsigned long paddr_start,
 {
 	bool pgd_changed = false;
 	unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
+	int ret;
 
 	paddr_last = paddr_end;
 	vaddr = (unsigned long)__va(paddr_start);
@@ -731,6 +732,9 @@ kernel_physical_mapping_init(unsigned long paddr_start,
 		pgd_changed = true;
 	}
 
+	ret = sync_direct_mapping();
+	WARN_ON(ret);
+
 	if (pgd_changed)
 		sync_global_pgds(vaddr_start, vaddr_end - 1);
 
@@ -1142,10 +1146,13 @@ void __ref vmemmap_free(unsigned long start, unsigned long end,
 static void __meminit
 kernel_physical_mapping_remove(unsigned long start, unsigned long end)
 {
+	int ret;
 	start = (unsigned long)__va(start);
 	end = (unsigned long)__va(end);
 
 	remove_pagetable(start, end, true, NULL);
+	ret = sync_direct_mapping();
+	WARN_ON(ret);
 }
 
 int __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
@@ -1253,6 +1260,7 @@ void mark_rodata_ro(void)
 	unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
 	unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
 	unsigned long all_end;
+	int ret;
 
 	printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
 	       (end - start) >> 10);
@@ -1290,6 +1298,8 @@ void mark_rodata_ro(void)
 			(unsigned long) __va(__pa_symbol(rodata_end)),
 			(unsigned long) __va(__pa_symbol(_sdata)));
 
+	ret = sync_direct_mapping();
+	WARN_ON(ret);
 	debug_checkwx();
 
 	/*
arch/x86/mm/mktme.c +437 -0

452 lines not on this page · raw message

Signature · 1 line
2.18.0
#24 lore raw

PATCH v4 17/18 x86/mm: Handle encrypted memory in page_to_virt() and __pa()

In reply to Kirill A. Shutemov

Per-KeyID direct mappings require changes into how we find the right virtual address for a page and virt-to-phys address translations.

page_to_virt() definition overwrites default macros provided by <linux/mm.h>. We only overwrite the macros if MTKME is enabled compile-time.

---
 arch/x86/include/asm/mktme.h   | 3 +++
 arch/x86/include/asm/page_64.h | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/include/asm/mktme.h +3 -0
index ba83fba4f9b3..dbfbd955da98 100644
@@ -29,6 +29,9 @@ void arch_free_page(struct page *page, int order);
 
 int sync_direct_mapping(void);
 
+#define page_to_virt(x) \
+	(__va(PFN_PHYS(page_to_pfn(x))) + page_keyid(x) * direct_mapping_size)
+
 #else
 #define mktme_keyid_mask	((phys_addr_t)0)
 #define mktme_nr_keyids		0
arch/x86/include/asm/page_64.h +1 -1
index 53c32af895ab..ffad496aadad 100644
@@ -23,7 +23,7 @@ static inline unsigned long __phys_addr_nodebug(unsigned long x)
 	/* use the carry flag to determine if x was < __START_KERNEL_map */
 	x = y + ((x > y) ? phys_base : (__START_KERNEL_map - PAGE_OFFSET));
 
-	return x;
+	return x % direct_mapping_size;
 }
 
 #ifdef CONFIG_DEBUG_VIRTUAL
Signature · 1 line
2.18.0
#25 lore raw

Re: [PATCHv4 17/18] x86/mm: Handle encrypted memory in page_to_virt() and __pa()

arch/x86/include/asm/mktme.h void arch_free_page(struct page *page, int order); · line 33

Quoted 10 lines
arch/x86/include/asm/mktme.h +2 -0
index ba83fba4f9b3..dbfbd955da98 100644
@@ -29,6 +29,9 @@ void arch_free_page(struct page *page, int order);
 
 int sync_direct_mapping(void);
 
+#define page_to_virt(x) \
+	(__va(PFN_PHYS(page_to_pfn(x))) + page_keyid(x) * direct_mapping_size)

Please put this in a generic header so that this hunk represents the *default* x86 implementation that is used universally on x86. Then, please do

#ifndef CONFIG_MKTME_WHATEVER
#define page_keyid(x) (0)
#endif

arch/x86/include/asm/page_64.h static inline unsigned long __phys_addr_nodebug(unsigned long x) · line 26

Quoted 13 lines
arch/x86/include/asm/mktme.h +0 -0
 #else
 #define mktme_keyid_mask	((phys_addr_t)0)
 #define mktme_nr_keyids		0
arch/x86/include/asm/page_64.h +1 -1
index 53c32af895ab..ffad496aadad 100644
@@ -23,7 +23,7 @@ static inline unsigned long __phys_addr_nodebug(unsigned long x)
 	/* use the carry flag to determine if x was < __START_KERNEL_map */
 	x = y + ((x > y) ? phys_base : (__START_KERNEL_map - PAGE_OFFSET));
 
-	return x;
+	return x % direct_mapping_size;

There are almost *surely* performance implications from this that affect anyone with this compile option turned on. There's now a 64-bit integer division operation which is used in places like kfree().

That's a show-stopper for me until we've done some pretty comprehensive performance analysis of this, which means much more than one kernel compile test on one system.

#26 lore raw

Re: [PATCHv4 17/18] x86/mm: Handle encrypted memory in page_to_virt() and __pa()

Quoted 6 lines · Dave Hansen
Quoted 6 lines
arch/x86/include/asm/mktme.h +0 -0
index ba83fba4f9b3..dbfbd955da98 100644
@@ -29,6 +29,9 @@ void arch_free_page(struct page *page, int order);
 
Quoted 7 lines

arch/x86/include/asm/mktme.h void arch_free_page(struct page *page, int order); · line 33

Quoted 4 lines
arch/x86/include/asm/mktme.h +2 -0
 int sync_direct_mapping(void);
 
+#define page_to_virt(x) \
+	(__va(PFN_PHYS(page_to_pfn(x))) + page_keyid(x) * direct_mapping_size)

Please put this in a generic header so that this hunk represents the *default* x86 implementation that is used universally on x86.

As I said, I disagree with you on the style preference.

If a maintainer prefers it to be done in your way, I'll move the macros.

Quoted 5 lines

Then, please do

#ifndef CONFIG_MKTME_WHATEVER
#define page_keyid(x) (0)
#endif

Default page_keyid() implementation returns 0.

Quoted 11 lines
Quoted 11 lines
arch/x86/include/asm/mktme.h +0 -0
 #else
 #define mktme_keyid_mask	((phys_addr_t)0)
 #define mktme_nr_keyids		0
arch/x86/include/asm/page_64.h +0 -0
index 53c32af895ab..ffad496aadad 100644
@@ -23,7 +23,7 @@ static inline unsigned long __phys_addr_nodebug(unsigned long x)
 	/* use the carry flag to determine if x was < __START_KERNEL_map */
 	x = y + ((x > y) ? phys_base : (__START_KERNEL_map - PAGE_OFFSET));
 
Quoted 6 lines

arch/x86/include/asm/page_64.h static inline unsigned long __phys_addr_nodebug(unsigned long x) · line 26

Quoted 2 lines
arch/x86/include/asm/page_64.h +1 -1
-	return x;
+	return x % direct_mapping_size;

There are almost *surely* performance implications from this that affect anyone with this compile option turned on. There's now a 64-bit integer division operation which is used in places like kfree().

Fair point. Apparently, modern CPU is good enough to hide the overhead. I'll look into how to avoid division.

After quick look the only way to get it cheap (near free on my CPU) is to have power-of-2 direct_mapping_size and mask address before returning it.

If direct_mapping_size is not power-of-2, the best variant I've come up with so far costs a branch for non-encrypted memory.

For encrypted it is branch, 32-bit division and some bit shifting and masking.

I'll look into this more.

Signature · 1 line
 Kirill A. Shutemov
#27 lore raw

PATCH v4 18/18 x86: Introduce CONFIG_X86_INTEL_MKTME

In reply to Kirill A. Shutemov

Add new config option to enabled/disable Multi-Key Total Memory Encryption support.

MKTME uses MEMORY_PHYSICAL_PADDING to reserve enough space in per-KeyID direct mappings for memory hotplug.

---
 arch/x86/Kconfig | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
arch/x86/Kconfig +18 -1
index fa5e1ec09247..9a843bd63108 100644
@@ -1523,6 +1523,23 @@ config ARCH_USE_MEMREMAP_PROT
 	def_bool y
 	depends on AMD_MEM_ENCRYPT
 
+config X86_INTEL_MKTME
+	bool "Intel Multi-Key Total Memory Encryption"
+	select DYNAMIC_PHYSICAL_MASK
+	select PAGE_EXTENSION
+	depends on X86_64 && CPU_SUP_INTEL
+	---help---
+	  Say yes to enable support for Multi-Key Total Memory Encryption.
+	  This requires an Intel processor that has support of the feature.
+
+	  Multikey Total Memory Encryption (MKTME) is a technology that allows
+	  transparent memory encryption in and upcoming Intel platforms.
+
+	  MKTME is built on top of TME. TME allows encryption of the entirety
+	  of system memory using a single key. MKTME allows having multiple
+	  encryption domains, each having own key -- different memory pages can
+	  be encrypted with different keys.
+
 # Common NUMA Features
 config NUMA
 	bool "Numa Memory Allocation and Scheduler Support"
@@ -2199,7 +2216,7 @@ config RANDOMIZE_MEMORY
 
 config MEMORY_PHYSICAL_PADDING
 	hex "Physical memory mapping padding" if EXPERT
-	depends on RANDOMIZE_MEMORY
+	depends on RANDOMIZE_MEMORY || X86_INTEL_MKTME
 	default "0xa" if MEMORY_HOTPLUG
 	default "0x0"
 	range 0x1 0x40 if MEMORY_HOTPLUG
Signature · 1 line
2.18.0
reviewer
#28 lore raw

Re: [PATCHv4 18/18] x86: Introduce CONFIG_X86_INTEL_MKTME

Quoted 20 lines · Kirill A. Shutemov

Add new config option to enabled/disable Multi-Key Total Memory Encryption support.

MKTME uses MEMORY_PHYSICAL_PADDING to reserve enough space in per-KeyID direct mappings for memory hotplug.

---
 arch/x86/Kconfig | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
arch/x86/Kconfig +1 -0
index fa5e1ec09247..9a843bd63108 100644
@@ -1523,6 +1523,23 @@ config ARCH_USE_MEMREMAP_PROT
 	def_bool y
 	depends on AMD_MEM_ENCRYPT
 
+config X86_INTEL_MKTME

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1536

Quoted 10 lines
arch/x86/Kconfig +10 -0
+	bool "Intel Multi-Key Total Memory Encryption"
+	select DYNAMIC_PHYSICAL_MASK
+	select PAGE_EXTENSION
+	depends on X86_64 && CPU_SUP_INTEL
+	---help---
+	  Say yes to enable support for Multi-Key Total Memory Encryption.
+	  This requires an Intel processor that has support of the feature.
+
+	  Multikey Total Memory Encryption (MKTME) is a technology that allows
+	  transparent memory encryption in and upcoming Intel platforms.

huh? Maybe drop the "and"?

Quoted 19 lines
arch/x86/Kconfig +7 -1
+
+	  MKTME is built on top of TME. TME allows encryption of the entirety
+	  of system memory using a single key. MKTME allows having multiple
+	  encryption domains, each having own key -- different memory pages can
+	  be encrypted with different keys.
+
 # Common NUMA Features
 config NUMA
 	bool "Numa Memory Allocation and Scheduler Support"
@@ -2199,7 +2216,7 @@ config RANDOMIZE_MEMORY
 
 config MEMORY_PHYSICAL_PADDING
 	hex "Physical memory mapping padding" if EXPERT
-	depends on RANDOMIZE_MEMORY
+	depends on RANDOMIZE_MEMORY || X86_INTEL_MKTME
 	default "0xa" if MEMORY_HOTPLUG
 	default "0x0"
 	range 0x1 0x40 if MEMORY_HOTPLUG
Signature · 1 line
~Randy
#29 lore raw

Re: [PATCHv4 18/18] x86: Introduce CONFIG_X86_INTEL_MKTME

Quoted 20 lines · Randy Dunlap
Quoted 19 lines · Kirill A. Shutemov

Add new config option to enabled/disable Multi-Key Total Memory Encryption support.

MKTME uses MEMORY_PHYSICAL_PADDING to reserve enough space in per-KeyID direct mappings for memory hotplug.

---
 arch/x86/Kconfig | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
arch/x86/Kconfig +0 -0
index fa5e1ec09247..9a843bd63108 100644
@@ -1523,6 +1523,23 @@ config ARCH_USE_MEMREMAP_PROT
 	def_bool y
 	depends on AMD_MEM_ENCRYPT
 
Quoted 13 lines

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1536

Quoted 11 lines
arch/x86/Kconfig +11 -0
+config X86_INTEL_MKTME
+	bool "Intel Multi-Key Total Memory Encryption"
+	select DYNAMIC_PHYSICAL_MASK
+	select PAGE_EXTENSION
+	depends on X86_64 && CPU_SUP_INTEL
+	---help---
+	  Say yes to enable support for Multi-Key Total Memory Encryption.
+	  This requires an Intel processor that has support of the feature.
+
+	  Multikey Total Memory Encryption (MKTME) is a technology that allows
+	  transparent memory encryption in and upcoming Intel platforms.

huh? Maybe drop the "and"?

Ugh.. It has to be "an".

Signature · 1 line
 Kirill A. Shutemov
reviewer
#30 lore raw

Re: [PATCHv4 18/18] x86: Introduce CONFIG_X86_INTEL_MKTME

Quoted 35 lines · Kirill A. Shutemov
Quoted 20 lines · Randy Dunlap
Quoted 19 lines · Kirill A. Shutemov

Add new config option to enabled/disable Multi-Key Total Memory Encryption support.

MKTME uses MEMORY_PHYSICAL_PADDING to reserve enough space in per-KeyID direct mappings for memory hotplug.

---
 arch/x86/Kconfig | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
arch/x86/Kconfig +0 -0
index fa5e1ec09247..9a843bd63108 100644
@@ -1523,6 +1523,23 @@ config ARCH_USE_MEMREMAP_PROT
 	def_bool y
 	depends on AMD_MEM_ENCRYPT
 
Quoted 13 lines

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1536

Quoted 11 lines
arch/x86/Kconfig +11 -0
+config X86_INTEL_MKTME
+	bool "Intel Multi-Key Total Memory Encryption"
+	select DYNAMIC_PHYSICAL_MASK
+	select PAGE_EXTENSION
+	depends on X86_64 && CPU_SUP_INTEL
+	---help---
+	  Say yes to enable support for Multi-Key Total Memory Encryption.
+	  This requires an Intel processor that has support of the feature.
+
+	  Multikey Total Memory Encryption (MKTME) is a technology that allows
+	  transparent memory encryption in and upcoming Intel platforms.

huh? Maybe drop the "and"?

Quoted 1 line

Ugh.. It has to be "an".

an ... platform.
or
in upcoming Intel platforms.

Signature · 1 line
~Randy
#31 lore raw

Re: [PATCHv4 18/18] x86: Introduce CONFIG_X86_INTEL_MKTME

In reply to Kirill A. Shutemov
Quoted 16 lines · Kirill A. Shutemov

Add new config option to enabled/disable Multi-Key Total Memory Encryption support.

MKTME uses MEMORY_PHYSICAL_PADDING to reserve enough space in per-KeyID direct mappings for memory hotplug.

---
 arch/x86/Kconfig | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
arch/x86/Kconfig +0 -0
index fa5e1ec09247..9a843bd63108 100644

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1526

Quoted 4 lines
arch/x86/Kconfig +1 -0
@@ -1523,6 +1523,23 @@ config ARCH_USE_MEMREMAP_PROT
 	def_bool y
 	depends on AMD_MEM_ENCRYPT
 
+config X86_INTEL_MKTME

Rip out the X86?

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1529

Quoted 3 lines
arch/x86/Kconfig +3 -0
+	bool "Intel Multi-Key Total Memory Encryption"
+	select DYNAMIC_PHYSICAL_MASK
+	select PAGE_EXTENSION

And maybe select 5-page?

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1536

Quoted 7 lines
arch/x86/Kconfig +7 -0
+	depends on X86_64 && CPU_SUP_INTEL
+	---help---
+	  Say yes to enable support for Multi-Key Total Memory Encryption.
+	  This requires an Intel processor that has support of the feature.
+
+	  Multikey Total Memory Encryption (MKTME) is a technology that allows
+	  transparent memory encryption in and upcoming Intel platforms.

How about saying which CPUs? Or just dropping this?

Quoted 21 lines
arch/x86/Kconfig +7 -1
+
+	  MKTME is built on top of TME. TME allows encryption of the entirety
+	  of system memory using a single key. MKTME allows having multiple
+	  encryption domains, each having own key -- different memory pages can
+	  be encrypted with different keys.
+
 # Common NUMA Features
 config NUMA
 	bool "Numa Memory Allocation and Scheduler Support"
@@ -2199,7 +2216,7 @@ config RANDOMIZE_MEMORY
 
 config MEMORY_PHYSICAL_PADDING
 	hex "Physical memory mapping padding" if EXPERT
-	depends on RANDOMIZE_MEMORY
+	depends on RANDOMIZE_MEMORY || X86_INTEL_MKTME
 	default "0xa" if MEMORY_HOTPLUG
 	default "0x0"
 	range 0x1 0x40 if MEMORY_HOTPLUG
Signature · 1 line
2.18.0
#32 lore raw

Re: [PATCHv4 18/18] x86: Introduce CONFIG_X86_INTEL_MKTME

Quoted 7 lines · Konrad Rzeszutek Wilk

On Tue, Jun 26, 2018 at 05:22:45PM +0300, Kirill A. Shutemov wrote:
Rip out the X86?

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1529

Quoted 3 lines
arch/x86/Kconfig +3 -0
+	bool "Intel Multi-Key Total Memory Encryption"
+	select DYNAMIC_PHYSICAL_MASK
+	select PAGE_EXTENSION

And maybe select 5-page?

Why? It's not a strict dependency. You *can* build a 4-level kernel and run it on smaller systems.

Quoted 9 lines

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1536

Quoted 7 lines
arch/x86/Kconfig +7 -0
+	depends on X86_64 && CPU_SUP_INTEL
+	---help---
+	  Say yes to enable support for Multi-Key Total Memory Encryption.
+	  This requires an Intel processor that has support of the feature.
+
+	  Multikey Total Memory Encryption (MKTME) is a technology that allows
+	  transparent memory encryption in and upcoming Intel platforms.

How about saying which CPUs? Or just dropping this?

We don't have any information about specifically which processors with have this feature to share. But, this config text does tell someone that they can't use this feature on today's platforms.

We _did_ say this for previous features (protection keys stands out where we said it was for "Skylake Servers" IIRC), but we are not yet able to do the same for this feature.

#33 lore raw

Re: [PATCHv4 18/18] x86: Introduce CONFIG_X86_INTEL_MKTME

Quoted 11 lines · Dave Hansen
Quoted 7 lines · Konrad Rzeszutek Wilk

On Tue, Jun 26, 2018 at 05:22:45PM +0300, Kirill A. Shutemov wrote:
Rip out the X86?

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1529

Quoted 3 lines
arch/x86/Kconfig +3 -0
+	bool "Intel Multi-Key Total Memory Encryption"
+	select DYNAMIC_PHYSICAL_MASK
+	select PAGE_EXTENSION

And maybe select 5-page?

Why? It's not a strict dependency. You *can* build a 4-level kernel and run it on smaller systems.

Sure, but in one of his commits he mentions that we may run in overlapping physical memory if we use 4-level paging. Hence why not just move to 5-level paging and simplify this.

Quoted 18 lines
Quoted 10 lines

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1536

Quoted 8 lines
arch/x86/Kconfig +7 -0
+	depends on X86_64 && CPU_SUP_INTEL
+	---help---
+	  Say yes to enable support for Multi-Key Total Memory Encryption.
+	  This requires an Intel processor that has support of the feature.
+
+	  Multikey Total Memory Encryption (MKTME) is a technology that allows
+	  transparent memory encryption in and upcoming Intel platforms.

How about saying which CPUs? Or just dropping this?

We don't have any information about specifically which processors with have this feature to share. But, this config text does tell someone that they can't use this feature on today's platforms.

We _did_ say this for previous features (protection keys stands out where we said it was for "Skylake Servers" IIRC), but we are not yet able to do the same for this feature.

#34 lore raw

Re: [PATCHv4 18/18] x86: Introduce CONFIG_X86_INTEL_MKTME

Quoted 10 lines · Konrad Rzeszutek Wilk
Quoted 9 lines · Dave Hansen
Quoted 7 lines · Konrad Rzeszutek Wilk

On Tue, Jun 26, 2018 at 05:22:45PM +0300, Kirill A. Shutemov wrote:
Rip out the X86?

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1529

Quoted 3 lines
arch/x86/Kconfig +3 -0
+	bool "Intel Multi-Key Total Memory Encryption"
+	select DYNAMIC_PHYSICAL_MASK
+	select PAGE_EXTENSION

And maybe select 5-page?

Quoted 6 lines
Quoted 2 lines

Why? It's not a strict dependency. You *can* build a 4-level kernel and run it on smaller systems.

Sure, but in one of his commits he mentions that we may run in overlapping physical memory if we use 4-level paging. Hence why not just move to 5-level paging and simplify this.

I'm not sure it _actually_ simplifies anything. We still need code to handle the cases where we bump into the limits because even 5-level paging systems can hit the *architectural* limits. We just don't think we'll bump into those limits any time soon in practice since they're 512x larger on 5-level systems.

But, a future system that needs physical address space or has a bunch more KeyID bits might bump into the limits.

It's also _possible_ that a processor could come out that supports MKTME but not 5-level paging, or a hypervisor would expose such a configuration to a guest. We've asked our colleagues very nicely that Intel not make a processor that does this, but it's still possible one shows up.

#35 lore raw

Re: [PATCHv4 18/18] x86: Introduce CONFIG_X86_INTEL_MKTME

Quoted 18 lines · Dave Hansen
Quoted 10 lines · Konrad Rzeszutek Wilk
Quoted 9 lines · Dave Hansen
Quoted 7 lines · Konrad Rzeszutek Wilk

On Tue, Jun 26, 2018 at 05:22:45PM +0300, Kirill A. Shutemov wrote:
Rip out the X86?

arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1529

Quoted 3 lines
arch/x86/Kconfig +3 -0
+	bool "Intel Multi-Key Total Memory Encryption"
+	select DYNAMIC_PHYSICAL_MASK
+	select PAGE_EXTENSION

And maybe select 5-page?

Quoted 6 lines
Quoted 2 lines

Why? It's not a strict dependency. You *can* build a 4-level kernel and run it on smaller systems.

Sure, but in one of his commits he mentions that we may run in overlapping physical memory if we use 4-level paging. Hence why not just move to 5-level paging and simplify this.

Quoted 8 lines

I'm not sure it _actually_ simplifies anything. We still need code to handle the cases where we bump into the limits because even 5-level paging systems can hit the *architectural* limits. We just don't think we'll bump into those limits any time soon in practice since they're 512x larger on 5-level systems.

But, a future system that needs physical address space or has a bunch more KeyID bits might bump into the limits.

Yikes. So when will we expand to 128-bit page fields?

Quoted 6 lines

It's also _possible_ that a processor could come out that supports MKTME but not 5-level paging, or a hypervisor would expose such a configuration to a guest. We've asked our colleagues very nicely that Intel not make a processor that does this, but it's still possible one shows up.