MKTME enabling
Subsystems (16)
v5 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 8 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
v5:
- Do not merge VMAs with different KeyID (for real).
- Do not use zero page in encrypted VMAs.
- Avoid division in __pa(). The division is replaced with masking which makes it near-free. I was not able to measure difference comparing to base line. direct_mapping_size now has to be power-of-2 if MKTME enabled. Only in this case we can use masking there.
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 (19):
mm: Do no merge VMAs with different encryption KeyIDs
mm: Do not use zero page in encrypted pages
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/s390/include/asm/pgtable.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 | 4 +- 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 | 4 + arch/x86/kernel/setup.c | 3 + arch/x86/mm/Makefile | 2 + arch/x86/mm/init_64.c | 68 ++++ arch/x86/mm/kaslr.c | 11 +- arch/x86/mm/mktme.c | 546 +++++++++++++++++++++++++++ fs/userfaultfd.c | 7 +- include/linux/gfp.h | 54 ++- include/linux/migrate.h | 12 +- include/linux/mm.h | 20 +- include/linux/page_ext.h | 11 +- mm/compaction.c | 1 + mm/huge_memory.c | 3 +- mm/khugepaged.c | 10 + mm/ksm.c | 3 + mm/madvise.c | 2 +- mm/memory.c | 3 +- mm/mempolicy.c | 31 +- mm/migrate.c | 4 +- mm/mlock.c | 2 +- mm/mmap.c | 31 +- mm/mprotect.c | 2 +- mm/page_alloc.c | 47 +++ mm/page_ext.c | 3 + 34 files changed, 954 insertions(+), 60 deletions(-) create mode 100644 arch/x86/include/asm/mktme.h create mode 100644 arch/x86/mm/mktme.c
Signature · 1 line
2.18.0
PATCH v5 1/19 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.
--- fs/userfaultfd.c | 7 ++++--- include/linux/mm.h | 9 ++++++++- mm/madvise.c | 2 +- mm/mempolicy.c | 3 ++- mm/mlock.c | 2 +- mm/mmap.c | 31 +++++++++++++++++++------------ mm/mprotect.c | 2 +- 7 files changed, 36 insertions(+), 20 deletions(-)
fs/userfaultfd.c +4 -3
@@ -890,7 +890,7 @@ static int userfaultfd_release(struct inode *inode, struct file *file) new_flags, vma->anon_vma, vma->vm_file, vma->vm_pgoff,vma_policy (vma), - NULL_VM_UFFD_CTX); + NULL_VM_UFFD_CTX, vma_keyid(vma));if (prev) vma = prev;else
@@ -1423,7 +1423,8 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, prev =vma_merge (mm, prev, start, vma_end, new_flags, vma->anon_vma, vma->vm_file, vma->vm_pgoff,vma_policy (vma), - ((struct vm_userfaultfd_ctx ){ ctx })); + ((struct vm_userfaultfd_ctx ){ ctx }), +vma_keyid (vma));if (prev) { vma = prev; goto next;
@@ -1581,7 +1582,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, prev =vma_merge (mm, prev, start, vma_end, new_flags, vma->anon_vma, vma->vm_file, vma->vm_pgoff,vma_policy (vma), -NULL_VM_UFFD_CTX ); +NULL_VM_UFFD_CTX ,vma_keyid (vma));if (prev) { vma = prev; goto next;
include/linux/mm.h +8 -1
@@ -1544,6 +1544,13 @@ static inline bool vma_is_anonymous(struct vm_area_struct *vma)return vma->vm_ops == &anon_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
@@ -2219,7 +2226,7 @@ static inline int vma_adjust(struct vm_area_struct *vma, unsigned long start,extern struct vm_area_struct *vma_merge (struct mm_struct *,struct vm_area_struct *prev,unsigned long addr,unsigned long end,unsigned long vm_flags,struct anon_vma *,struct file *,pgoff_t , -struct mempolicy *,struct vm_userfaultfd_ctx ); +struct mempolicy *,struct vm_userfaultfd_ctx ,int keyid);extern struct anon_vma *find_mergeable_anon_vma (struct vm_area_struct *);extern int __split_vma (struct mm_struct *,struct vm_area_struct *,unsigned long addr,int new_below);
mm/madvise.c +1 -1
@@ -138,7 +138,7 @@ static long madvise_behavior(struct vm_area_struct *vma, pgoff = vma->vm_pgoff + ((start - vma->vm_start) >>PAGE_SHIFT ); *prev =vma_merge (mm, *prev, start, end, new_flags, vma->anon_vma, vma->vm_file, pgoff,vma_policy (vma), - vma->vm_userfaultfd_ctx); + vma->vm_userfaultfd_ctx,vma_keyid (vma));if (*prev) { vma = *prev; goto success;
mm/mempolicy.c +2 -1
@@ -705,7 +705,8 @@ static int mbind_range(struct mm_struct *mm, unsigned long start, ((vmstart - vma->vm_start) >>PAGE_SHIFT ); prev =vma_merge (mm, prev, vmstart, vmend, vma->vm_flags, vma->anon_vma, vma->vm_file, pgoff, - new_pol, vma->vm_userfaultfd_ctx); + new_pol, vma->vm_userfaultfd_ctx, +vma_keyid (vma));if (prev) { vma = prev; next = vma->vm_next;
mm/mlock.c +1 -1
@@ -534,7 +534,7 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev, pgoff = vma->vm_pgoff + ((start - vma->vm_start) >>PAGE_SHIFT ); *prev =vma_merge (mm, *prev, start, end, newflags, vma->anon_vma, vma->vm_file, pgoff,vma_policy (vma), - vma->vm_userfaultfd_ctx); + vma->vm_userfaultfd_ctx,vma_keyid (vma));if (*prev) { vma = *prev; goto success;
mm/mmap.c +19 -12
@@ -984,7 +984,8 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,*/ static inline int is_mergeable_vma (struct vm_area_struct *vma,struct file *file,unsigned long vm_flags, -struct vm_userfaultfd_ctx vm_userfaultfd_ctx) +struct vm_userfaultfd_ctx vm_userfaultfd_ctx, +int keyid) {/* * VM_SOFTDIRTY should not prevent from VMA merging, if we
@@ -998,6 +999,8 @@ static inline int is_mergeable_vma(struct vm_area_struct *vma,return 0 ;if (vma->vm_file != file)return 0 ; +if (vma_keyid (vma) != keyid) +return 0 ;if (vma->vm_ops->close)return 0 ;if (!is_mergeable_vm_userfaultfd_ctx (vma, vm_userfaultfd_ctx))
@@ -1034,9 +1037,10 @@ static int can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff, - struct vm_userfaultfd_ctx vm_userfaultfd_ctx) + struct vm_userfaultfd_ctx vm_userfaultfd_ctx, + int keyid) { - if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) && + if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, keyid) && is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { if (vma->vm_pgoff == vm_pgoff) return 1;
@@ -1055,9 +1059,10 @@ static int can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff, - struct vm_userfaultfd_ctx vm_userfaultfd_ctx) + struct vm_userfaultfd_ctx vm_userfaultfd_ctx, + int keyid) { - if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) && + if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, keyid) && is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { pgoff_t vm_pglen; vm_pglen =vma_pages (vma);
@@ -1112,7 +1117,8 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm, unsigned long end, unsigned long vm_flags, struct anon_vma *anon_vma, struct file *file, pgoff_t pgoff, struct mempolicy *policy, - struct vm_userfaultfd_ctx vm_userfaultfd_ctx) + struct vm_userfaultfd_ctx vm_userfaultfd_ctx, + int keyid) { pgoff_t pglen = (end - addr) >> PAGE_SHIFT;struct vm_area_struct *area, *next;
@@ -1145,7 +1151,7 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,mpol_equal (vma_policy (prev), policy) &&can_vma_merge_after (prev, vm_flags, anon_vma, file, pgoff, - vm_userfaultfd_ctx)) { + vm_userfaultfd_ctx, keyid)) {/* * OK, it can. Can we now merge in the successor as well? */
@@ -1154,7 +1160,8 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,can_vma_merge_before (next, vm_flags, anon_vma, file, pgoff+pglen, - vm_userfaultfd_ctx) && + vm_userfaultfd_ctx, + keyid) &&is_mergeable_anon_vma (prev->anon_vma, next->anon_vma, NULL)) {/* cases 1, 6 */
@@ -1177,7 +1184,7 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,mpol_equal (policy,vma_policy (next)) &&can_vma_merge_before (next, vm_flags, anon_vma, file, pgoff+pglen, - vm_userfaultfd_ctx)) { + vm_userfaultfd_ctx, keyid)) { if (prev && addr < prev->vm_end) /* case 4 */ err = __vma_adjust(prev, prev->vm_start, addr, prev->vm_pgoff,NULL , next);
@@ -1722,7 +1729,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,* Can we just expand an old mapping? */ vma =vma_merge (mm, prev, addr, addr + len, vm_flags, -NULL , file, pgoff,NULL ,NULL_VM_UFFD_CTX ); +NULL , file, pgoff,NULL ,NULL_VM_UFFD_CTX ,0 );if (vma) goto out;
@@ -2987,7 +2994,7 @@ static int do_brk_flags(unsigned long addr, unsigned long len, unsigned long fla/* Can we just expand an old private anonymous mapping? */ vma =vma_merge (mm, prev, addr, addr + len, flags, -NULL ,NULL , pgoff,NULL ,NULL_VM_UFFD_CTX ); +NULL ,NULL , pgoff,NULL ,NULL_VM_UFFD_CTX ,0 );if (vma) goto out;
@@ -3189,7 +3196,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,return NULL ;/* should never get here */ new_vma =vma_merge (mm, prev, addr, addr + len, vma->vm_flags, vma->anon_vma, vma->vm_file, pgoff,vma_policy (vma), - vma->vm_userfaultfd_ctx); + vma->vm_userfaultfd_ctx,vma_keyid (vma));if (new_vma) {/* * Source vma may have been merged into new_vma
mm/mprotect.c +1 -1
@@ -349,7 +349,7 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev, pgoff = vma->vm_pgoff + ((start - vma->vm_start) >>PAGE_SHIFT ); *pprev =vma_merge (mm, *pprev, start, end, newflags, vma->anon_vma, vma->vm_file, pgoff,vma_policy (vma), - vma->vm_userfaultfd_ctx); + vma->vm_userfaultfd_ctx,vma_keyid (vma));if (*pprev) { vma = *pprev;VM_WARN_ON ((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY );
Signature · 1 line
2.18.0
PATCH v5 2/19 mm: Do not use zero page in encrypted pages
In reply to Kirill A. ShutemovZero page is not encrypted and putting it into encrypted VMA produces garbage.
We can map zero page with KeyID-0 into an encrypted VMA, but this would be violation security boundary between encryption domains.
Forbid zero pages in encrypted VMAs.
--- arch/s390/include/asm/pgtable.h | 2 +- include/linux/mm.h | 4 ++-- mm/huge_memory.c | 3 +-- mm/memory.c | 3 +-- 4 files changed, 5 insertions(+), 7 deletions(-)
arch/s390/include/asm/pgtable.h +1 -1
@@ -505,7 +505,7 @@ static inline int mm_alloc_pgste(struct mm_struct *mm)* In the case that a guest uses storage keys * faults should no longer be backed by zero pages */ -#define mm_forbids_zeropage mm_has_pgste +#define vma_forbids_zeropage (vma) mm_has_pgste(vma->vm_mm)static inline int mm_uses_skeys (struct mm_struct *mm) {#ifdef CONFIG_PGSTE
include/linux/mm.h +2 -2
@@ -92,8 +92,8 @@ extern int mmap_rnd_compat_bits __read_mostly;* s390 does this to prevent multiplexing of hardware bits * related to the physical page in case of virtualization. */ -#ifndef mm_forbids_zeropage -#define mm_forbids_zeropage (X ) (0) +#ifndef vma_forbids_zeropage +#define vma_forbids_zeropage (vma) vma_keyid(vma)#endif /*
mm/huge_memory.c +1 -2
@@ -676,8 +676,7 @@ int do_huge_pmd_anonymous_page(struct vm_fault *vmf)return VM_FAULT_OOM ;if (unlikely (khugepaged_enter (vma, vma->vm_flags)))return VM_FAULT_OOM ; -if (!(vmf->flags &FAULT_FLAG_WRITE ) && - !mm_forbids_zeropage (vma->vm_mm) && +if (!(vmf->flags &FAULT_FLAG_WRITE ) && !vma_forbids_zeropage (vma) &&transparent_hugepage_use_zero_page ()) {pgtable_t pgtable;struct page *zero_page;
mm/memory.c +1 -2
@@ -3139,8 +3139,7 @@ static int do_anonymous_page(struct vm_fault *vmf)return 0 ;/* Use the zero-page for reads */ -if (!(vmf->flags &FAULT_FLAG_WRITE ) && - !mm_forbids_zeropage (vma->vm_mm)) { +if (!(vmf->flags &FAULT_FLAG_WRITE ) && !vma_forbids_zeropage (vma)) { entry =pte_mkspecial (pfn_pte (my_zero_pfn (vmf->address), vma->vm_page_prot)); vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
Signature · 1 line
2.18.0
Re: [PATCHv5 02/19] mm: Do not use zero page in encrypted pages
Quoted 5 lines · Kirill A. Shutemov
Zero page is not encrypted and putting it into encrypted VMA produces garbage.
We can map zero page with KeyID-0 into an encrypted VMA, but this would be violation security boundary between encryption domains.
Why? How is it a violation?
It only matters if they write secrets. They can't write secrets to the zero page.
Is this only because you accidentally inherited ->vm_page_prot on the zero page PTE?
Re: [PATCHv5 02/19] mm: Do not use zero page in encrypted pages
Quoted 11 lines · Dave Hansen
Quoted 5 lines · Kirill A. Shutemov
Zero page is not encrypted and putting it into encrypted VMA produces garbage.
We can map zero page with KeyID-0 into an encrypted VMA, but this would be violation security boundary between encryption domains.
Why? How is it a violation?
It only matters if they write secrets. They can't write secrets to the zero page.
I believe usage of zero page is wrong here. It would indirectly reveal content of supposedly encrypted memory region.
I can see argument why it should be okay and I don't have very strong opinion on this.
If folks see it's okay to use zero page in encrypted VMAs I can certainly make it work.
Quoted 2 lines
Is this only because you accidentally inherited ->vm_page_prot on the zero page PTE?
Yes, in previous patchset I mapped zero page with wrong KeyID. This is one of possible fixes for this.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 02/19] mm: Do not use zero page in encrypted pages
Quoted 9 lines · Kirill A. Shutemov
Quoted 8 lines · Dave Hansen
Quoted 5 lines · Kirill A. Shutemov
Zero page is not encrypted and putting it into encrypted VMA produces garbage.
We can map zero page with KeyID-0 into an encrypted VMA, but this would be violation security boundary between encryption domains.
Why? How is it a violation?
Quoted 7 lines
Quoted 2 lines
It only matters if they write secrets. They can't write secrets to the zero page.
I believe usage of zero page is wrong here. It would indirectly reveal content of supposedly encrypted memory region.
I can see argument why it should be okay and I don't have very strong opinion on this.
I think we should make the zero page work. If folks are security-sensitive, they need to write to guarantee it isn't being shared. That's a pretty low bar.
I'm struggling to think of a case where an attacker has access to the encrypted data, the virt->phys mapping, *and* can glean something valuable from the presence of the zero page.
Please spend some time and focus on your patch descriptions. Use facts that are backed up and are *precise* or tell the story of how your patch was developed. In this case, citing the "security boundary" is not precise enough without explaining what the boundary is and how it is violated.
Re: [PATCHv5 02/19] mm: Do not use zero page in encrypted pages
Quoted 18 lines · Dave Hansen
Quoted 9 lines · Kirill A. Shutemov
Quoted 8 lines · Dave Hansen
Quoted 5 lines · Kirill A. Shutemov
Zero page is not encrypted and putting it into encrypted VMA produces garbage.
We can map zero page with KeyID-0 into an encrypted VMA, but this would be violation security boundary between encryption domains.
Why? How is it a violation?
Quoted 7 lines
Quoted 2 lines
It only matters if they write secrets. They can't write secrets to the zero page.
I believe usage of zero page is wrong here. It would indirectly reveal content of supposedly encrypted memory region.
I can see argument why it should be okay and I don't have very strong opinion on this.
Quoted 7 lines
I think we should make the zero page work. If folks are security-sensitive, they need to write to guarantee it isn't being shared. That's a pretty low bar.
I'm struggling to think of a case where an attacker has access to the encrypted data, the
virt->physmapping, *and* can glean something valuable from the presence of the zero page.
Okay.
Quoted 5 lines
Please spend some time and focus on your patch descriptions. Use facts that are backed up and are *precise* or tell the story of how your patch was developed. In this case, citing the "security boundary" is not precise enough without explaining what the boundary is and how it is violated.
Fair enough. I'll go though all commit messages once again. Sorry.
Signature · 1 line
Kirill A. Shutemov
PATCH v5 3/19 mm/ksm: Do not merge pages with different KeyIDs
In reply to Kirill A. ShutemovPages encrypted with different encryption keys are not allowed to be merged by KSM. 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
@@ -1551,6 +1551,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
@@ -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
Re: [PATCHv5 03/19] mm/ksm: Do not merge pages with different KeyIDs
Quoted 2 lines · Kirill A. Shutemov
Pages encrypted with different encryption keys are not allowed to be merged by KSM. Otherwise it would cross security boundary.
Let's say I'm using plain AES (not AES-XTS). I use the same key in two keyid slots. I map a page with the first keyid and another with the other keyid.
Won't they have the same cipertext? Why shouldn't we KSM them?
Re: [PATCHv5 03/19] mm/ksm: Do not merge pages with different KeyIDs
Quoted 9 lines · Dave Hansen
Quoted 2 lines · Kirill A. Shutemov
Pages encrypted with different encryption keys are not allowed to be merged by KSM. Otherwise it would cross security boundary.
Let's say I'm using plain AES (not AES-XTS). I use the same key in two keyid slots. I map a page with the first keyid and another with the other keyid.
Won't they have the same cipertext? Why shouldn't we KSM them?
We compare plain text, not ciphertext. And for good reason.
Comparing ciphertext would only make KSM successful for AES-ECB that doesn't dependent on physical address of the page.
MKTME only supports AES-XTS (no plans to support AES-ECB). It effectively disables KSM if we go with comparing ciphertext.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 03/19] mm/ksm: Do not merge pages with different KeyIDs
Quoted 10 lines · Kirill A. Shutemov
Quoted 8 lines · Dave Hansen
Quoted 2 lines · Kirill A. Shutemov
Pages encrypted with different encryption keys are not allowed to be merged by KSM. Otherwise it would cross security boundary.
Let's say I'm using plain AES (not AES-XTS). I use the same key in two keyid slots. I map a page with the first keyid and another with the other keyid.
Won't they have the same cipertext? Why shouldn't we KSM them?
We compare plain text, not ciphertext. And for good reason.
What's the reason? Probably good to talk about it for those playing along at home.
Quoted 5 lines
Comparing ciphertext would only make KSM successful for AES-ECB that doesn't dependent on physical address of the page.
MKTME only supports AES-XTS (no plans to support AES-ECB). It effectively disables KSM if we go with comparing ciphertext.
But what's the security boundary that is violated? You are talking about some practical concerns (KSM scanning inefficiency) which is a far cry from being any kind of security issue.
Re: [PATCHv5 03/19] mm/ksm: Do not merge pages with different KeyIDs
Quoted 9 lines · Dave Hansen
Quoted 8 lines · Kirill A. Shutemov
Quoted 7 lines · Dave Hansen
Quoted 2 lines · Kirill A. Shutemov
Pages encrypted with different encryption keys are not allowed to be merged by KSM. Otherwise it would cross security boundary.
Let's say I'm using plain AES (not AES-XTS). I use the same key in two keyid slots. I map a page with the first keyid and another with the other keyid.
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
Won't they have the same cipertext? Why shouldn't we KSM them?
We compare plain text, not ciphertext. And for good reason.
What's the reason? Probably good to talk about it for those playing along at home.
I'll update commit message.
Quoted 9 lines
Quoted 5 lines
Comparing ciphertext would only make KSM successful for AES-ECB that doesn't dependent on physical address of the page.
MKTME only supports AES-XTS (no plans to support AES-ECB). It effectively disables KSM if we go with comparing ciphertext.
But what's the security boundary that is violated? You are talking about some practical concerns (KSM scanning inefficiency) which is a far cry from being any kind of security issue.
As with zero page, my initial reasoning was that mixing pages from different secrutiy domains is bad idea.
Signature · 1 line
Kirill A. Shutemov
PATCH v5 4/19 mm/page_alloc: Unify alloc_hugepage_vma()
In reply to Kirill A. ShutemovWe 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
@@ -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_HIGHPAGEextern void copy_page (void * _to,void * _from);
include/linux/gfp.h +2 -4
@@ -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
Re: [PATCHv5 04/19] mm/page_alloc: Unify alloc_hugepage_vma()
A grammar error or two is probably OK in these descriptions, but these are just riddled with them in a way that makes them hard to read. Suggestions below.
Quoted 3 lines · Kirill A. Shutemov
We don't need to have separate implementations of
alloc_hugepage_vma()for NUMA and non-NUMA. Using variant based onalloc_pages_vma()we would cover both cases.
"Using the"
Quoted 1 line
This is preparation patch for allocation encrypted pages.
"a preparation"
"allocation encrypted pages" -> "allocation of encrypted pages" or "allocation encrypted pages" -> "allocating encrypted pages" or
Quoted 2 lines
alloc_pages_vma()will handle allocation of encrypted pages. With this change we don' t need to coveralloc_hugepage_vma()separately.
"don' t" -> "don't"
Quoted 1 line
The change makes typo in Alpha's implementation of
"a typo"
PATCH v5 5/19 mm/page_alloc: Handle allocation for encrypted memory
In reply to Kirill A. ShutemovFor 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
@@ -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
@@ -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
@@ -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
@@ -921,22 +921,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 ); + } }/*
@@ -2013,9 +2019,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 ) {
@@ -2058,6 +2071,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
@@ -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
Re: [PATCHv5 05/19] mm/page_alloc: Handle allocation for encrypted memory
I asked about this before and it still isn't covered in the description:
You were specifically asked (maybe in person at LSF/MM?) not to modify allocator to pass the keyid around. Please specifically mention how this design addresses that feedback in the patch description.
You were told, "don't change the core allocator", so I think you just added new functions that wrap the core allocator and called them from the majority of sites that call into the core allocator. Personally, I think that misses the point of the original request.
Do I have a better way? Nope, not really.
Quoted 5 lines
include/linux/gfp.h +5
-0+/* +* 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) +{
include/linux/gfp.h static inline void arch_free_page(struct page *page, int order) { } · line 470
Quoted 10 lines
include/linux/gfp.h +10
-0+if (!keyid) +return false; + +if (*gfp_mask & __GFP_ZERO) { + *gfp_mask &= ~__GFP_ZERO; +return true; + } + +return false; +}
Shouldn't this be zero_page_at_alloc()?
Otherwise, it gets confusing about whether the page needs zeroing at *all*, vs at alloc vs. free.
include/linux/gfp.h static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask, · line 523
Quoted 8 lines
include/linux/gfp.h +8
-0+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); +}
We have an innumerable number of (__)?alloc_pages* functions. This adds two more. I'm not a big fan of making this worse.
Do I have a better idea? Not really. The best I have is to start being more careful about all of the arguments and actually formalize the list of things that we need to succeed in an allocation in a struct alloc_args or something.
Quoted 10 lines
#define
alloc_page(gfp_mask)alloc_pages(gfp_mask, 0)
#definealloc_page_vma(gfp_mask, vma, addr)\include/linux/migrate.h +0
-0@@ -38,9 +38,15 @@ static inline struct page *new_page_nodemask(struct page *page,unsigned int order =0 ;struct page *new_page =NULL ;
include/linux/migrate.h static inline struct page *new_page_nodemask(struct page *page, · line 49
Quoted 10 lines
include/linux/migrate.h +7
-1-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); + }
Shouldn't we be returning NULL? Seems like failing the allocation is much less likely to result in bad things happening.
include/linux/migrate.h static inline struct page *new_page_nodemask(struct page *page, · line 60
Quoted 10 lines
include/linux/migrate.h +2
-2if (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));
Needs a comment please. It's totally non-obvious that this is the migration case from the context, new_page_nodemask()'s name, or the name of 'page'.
/* Allocate a page with the same KeyID as the source page */
mm/compaction.c static struct page *compaction_alloc(struct page *migratepage, · line 1192
Quoted 11 lines
mm/compaction.c +1
-0@@ -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; }
Comments, please.
Why is this here? What other code might need prep_encrypted_page()?
Quoted 43 lines
mm/mempolicy.c +12
-6@@ -921,22 +921,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 ); + } }/* @@ -2013,9 +2019,16 @@ alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma, {struct mempolicy *pol;struct page *page;
mm/mempolicy.c alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma, · line 2030
Quoted 10 lines
mm/mempolicy.c +7
-1-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; + }
Comments, please. 'zero' should be 'deferred_zero', at least.
Also, can't we hide this a _bit_ better?
if (deferred_page_zero(vma)) gfp &= ~__GFP_ZERO;
Then, later:
deferred_page_prep(vma, page, order);
and hide everything in deferred_page_zero() and deferred_page_prep().
Quoted 12 lines
mm/page_alloc.c +6
-0@@ -3697,6 +3697,39 @@ should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_fla }#endif /* CONFIG_COMPACTION */ +#ifndef CONFIG_NUMA +struct page *alloc_pages_vma (gfp_t gfp_mask,int order, +struct vm_area_struct *vma,unsigned long addr, +int node,bool hugepage) +{ +struct page *page;
mm/page_alloc.c should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_fla · line 3715
Quoted 10 lines
mm/page_alloc.c +10
-0+bool need_zero; +int keyid =vma_keyid (vma); + + need_zero =encrypted_page_needs_zero (keyid, &gfp_mask); + page =alloc_pages (gfp_mask, order); +prep_encrypted_page (page, order, keyid, need_zero); + + return page; +} +#endif
Is there *ever* a VMA-based allocation that doesn't need zeroing?
Quoted 26 lines
mm/page_alloc.c +19
-0+struct page *__alloc_pages_node_keyid (int nid,int keyid, +gfp_t gfp_mask,unsigned int order) +{ +struct page *page; +bool need_zero; + +VM_BUG_ON (nid <0 || nid >=MAX_NUMNODES ); +VM_WARN_ON (!node_online (nid)); + + need_zero =encrypted_page_needs_zero (keyid, &gfp_mask); + page =__alloc_pages (gfp_mask, order, nid); +prep_encrypted_page (page, order, keyid, need_zero); + +return page; +} +#ifdef CONFIG_LOCKDEP static struct lockdep_map __fs_reclaim_map =STATIC_LOCKDEP_MAP_INIT ("fs_reclaim" , &__fs_reclaim_map);@@ -4401,6 +4434,20 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid, }EXPORT_SYMBOL (__alloc_pages_nodemask); +struct page * +__alloc_pages_nodemask_keyid (gfp_t gfp_mask,unsigned int order, +int preferred_nid,nodemask_t *nodemask,int keyid)
mm/page_alloc.c __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid, · line 4449
Quoted 10 lines
mm/page_alloc.c +10
-0+{ +struct page *page; +bool need_zero; + + need_zero =encrypted_page_needs_zero (keyid, &gfp_mask); + page =__alloc_pages_nodemask (gfp_mask, order, preferred_nid, nodemask); +prep_encrypted_page (page, order, keyid, need_zero); +return page; +} +EXPORT_SYMBOL (__alloc_pages_nodemask_keyid);
That looks like three duplicates of the same code, wrapping three more allocator variants. Do we really have no other alternatives? Can you please go ask the folks that gave you the feedback about the allocator modifications and ask them if this is OK explicitly?
Re: [PATCHv5 05/19] mm/page_alloc: Handle allocation for encrypted memory
Quoted 11 lines · Dave Hansen
I asked about this before and it still isn't covered in the description:
You were specifically asked (maybe in person at LSF/MM?) not to modify allocator to pass the keyid around. Please specifically mention how this design addresses that feedback in the patch description.You were told, "don't change the core allocator", so I think you just added new functions that wrap the core allocator and called them from the majority of sites that call into the core allocator. Personally, I think that misses the point of the original request.
Do I have a better way? Nope, not really.
+Michal.
IIRC, Michal was not happy that I propagate the KeyID to very core allcoator and we've talked about wrappers around existing APIs as a better solution.
Michal, is it correct?
Quoted 18 lines
Quoted 5 lines
include/linux/gfp.h +5
-0+/* +* 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) +{include/linux/gfp.h static inline void arch_free_page(struct page *page, int order) { } · line 470
Quoted 10 lines
include/linux/gfp.h +10
-0+if (!keyid) +return false; + +if (*gfp_mask & __GFP_ZERO) { + *gfp_mask &= ~__GFP_ZERO; +return true; + } + +return false; +}Shouldn't this be
zero_page_at_alloc()?
Quoted 2 lines
Otherwise, it gets confusing about whether the page needs zeroing at *all*, vs at alloc vs. free.
I like your idea with deferred_page_zero() below. I'll go with it.
Quoted 12 lines
include/linux/gfp.h static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask, · line 523
Quoted 8 lines
include/linux/gfp.h +8
-0+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); +}We have an innumerable number of (__)?alloc_pages* functions. This adds two more. I'm not a big fan of making this worse.
Quoted 4 lines
Do I have a better idea? Not really. The best I have is to start being more careful about all of the arguments and actually formalize the list of things that we need to succeed in an allocation in a struct
alloc_argsor something.
Sounds like a separate project to me :)
Quoted 10 lines
Quoted 10 lines
#define
alloc_page(gfp_mask)alloc_pages(gfp_mask, 0)
#definealloc_page_vma(gfp_mask, vma, addr)\include/linux/migrate.h +0
-0@@ -38,9 +38,15 @@ static inline struct page *new_page_nodemask(struct page *page,unsigned int order =0 ;struct page *new_page =NULL ;
Quoted 13 lines
include/linux/migrate.h static inline struct page *new_page_nodemask(struct page *page, · line 49
Quoted 10 lines
include/linux/migrate.h +7
-1-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); + }Shouldn't we be returning NULL? Seems like failing the allocation is much less likely to result in bad things happening.
Okay.
Quoted 15 lines
include/linux/migrate.h static inline struct page *new_page_nodemask(struct page *page, · line 60
Quoted 10 lines
include/linux/migrate.h +2
-2if (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));Needs a comment please. It's totally non-obvious that this is the migration case from the context, new_page_nodemask()'s name, or the name of 'page'.
Quoted 1 line
/* Allocate a page with the same KeyID as the source page */
Sure.
Quoted 9 lines
Quoted 9 lines
mm/compaction.c +0
-0@@ -1187,6 +1187,7 @@ static struct page *compaction_alloc(struct page *migratepage,list_del (&freepage->lru); cc->nr_freepages--;
Quoted 7 lines
mm/compaction.c static struct page *compaction_alloc(struct page *migratepage, · line 1192
Quoted 3 lines
mm/compaction.c +1
-0+prep_encrypted_page (freepage,0 ,page_keyid (migratepage), false);return freepage; }Comments, please.
Why is this here?
/* Prepare the page using the same KeyID as the source page */
Quoted 1 line
What other code might need
prep_encrypted_page()?
Custom pages allocators if these pages can end up in encrypted VMAs.
It this case compaction creates own pool of pages to be used for allocation during page migration.
Quoted 84 lines
Quoted 43 lines
mm/mempolicy.c +12
-6@@ -921,22 +921,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 ); + } }/* @@ -2013,9 +2019,16 @@ alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma, {struct mempolicy *pol;struct page *page;mm/mempolicy.c alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma, · line 2030
Quoted 10 lines
mm/mempolicy.c +7
-1-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; + }Comments, please. 'zero' should be '
deferred_zero', at least.Also, can't we hide this a _bit_ better?
if (deferred_page_zero(vma)) gfp &= ~__GFP_ZERO;Then, later:
deferred_page_prep(vma, page, order);and hide everything in
deferred_page_zero()anddeferred_page_prep().Quoted 16 lines
mm/page_alloc.c +10
-0@@ -3697,6 +3697,39 @@ should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_fla }#endif /* CONFIG_COMPACTION */ +#ifndef CONFIG_NUMA +struct page *alloc_pages_vma (gfp_t gfp_mask,int order, +struct vm_area_struct *vma,unsigned long addr, +int node,bool hugepage) +{ +struct page *page; +bool need_zero; +int keyid =vma_keyid (vma); + + need_zero =encrypted_page_needs_zero (keyid, &gfp_mask);
Quoted 8 lines
mm/page_alloc.c should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_fla · line 3715
Quoted 6 lines
mm/page_alloc.c +6
-0+ page =alloc_pages (gfp_mask, order); +prep_encrypted_page (page, order, keyid, need_zero); + + return page; +} +#endif Is there *ever* a VMA-based allocation that doesn't need zeroing?
Sure. Any allocations for CoW.
Quoted 33 lines
Quoted 33 lines
mm/page_alloc.c +26
-0+struct page *__alloc_pages_node_keyid (int nid,int keyid, +gfp_t gfp_mask,unsigned int order) +{ +struct page *page; +bool need_zero; + +VM_BUG_ON (nid <0 || nid >=MAX_NUMNODES ); +VM_WARN_ON (!node_online (nid)); + + need_zero =encrypted_page_needs_zero (keyid, &gfp_mask); + page =__alloc_pages (gfp_mask, order, nid); +prep_encrypted_page (page, order, keyid, need_zero); + +return page; +} +#ifdef CONFIG_LOCKDEP static struct lockdep_map __fs_reclaim_map =STATIC_LOCKDEP_MAP_INIT ("fs_reclaim" , &__fs_reclaim_map);@@ -4401,6 +4434,20 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid, }EXPORT_SYMBOL (__alloc_pages_nodemask); +struct page * +__alloc_pages_nodemask_keyid (gfp_t gfp_mask,unsigned int order, +int preferred_nid,nodemask_t *nodemask,int keyid) +{ +struct page *page; +bool need_zero; + + need_zero =encrypted_page_needs_zero (keyid, &gfp_mask); + page =__alloc_pages_nodemask (gfp_mask, order, preferred_nid, nodemask); +prep_encrypted_page (page, order, keyid, need_zero);
Quoted 8 lines
mm/page_alloc.c __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid, · line 4449
Quoted 3 lines
mm/page_alloc.c +3
-0+return page; +} +EXPORT_SYMBOL (__alloc_pages_nodemask_keyid);That looks like three duplicates of the same code, wrapping three more allocator variants. Do we really have no other alternatives? Can you please go ask the folks that gave you the feedback about the allocator modifications and ask them if this is OK explicitly?
Michal, any feedback for the patch?
Signature · 1 line
G Kirill A. Shutemov
Re: [PATCHv5 05/19] mm/page_alloc: Handle allocation for encrypted memory
Quoted 6 lines · Kirill A. Shutemov
Quoted 1 line
What other code might need
prep_encrypted_page()?Custom pages allocators if these pages can end up in encrypted VMAs.
It this case compaction creates own pool of pages to be used for allocation during page migration.
OK, that makes sense. It also sounds like some great information to add near prep_encrypted_page().
Do we have any ability to catch cases like this if we get them wrong, or will we just silently corrupt data?
Re: [PATCHv5 05/19] mm/page_alloc: Handle allocation for encrypted memory
Quoted 10 lines · Dave Hansen
Quoted 6 lines · Kirill A. Shutemov
Quoted 1 line
What other code might need
prep_encrypted_page()?Custom pages allocators if these pages can end up in encrypted VMAs.
It this case compaction creates own pool of pages to be used for allocation during page migration.
OK, that makes sense. It also sounds like some great information to add near
prep_encrypted_page().
Okay.
Quoted 2 lines
Do we have any ability to catch cases like this if we get them wrong, or will we just silently corrupt data?
I cannot come up with any reasonable way to detect this immediately. I'll think about this more.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 05/19] mm/page_alloc: Handle allocation for encrypted memory
In reply to Kirill A. ShutemovQuoted 13 lines · Kirill A. Shutemov
Quoted 11 lines · Dave Hansen
I asked about this before and it still isn't covered in the description:
You were specifically asked (maybe in person at LSF/MM?) not to modify allocator to pass the keyid around. Please specifically mention how this design addresses that feedback in the patch description.You were told, "don't change the core allocator", so I think you just added new functions that wrap the core allocator and called them from the majority of sites that call into the core allocator. Personally, I think that misses the point of the original request.
Do I have a better way? Nope, not really.
Quoted 7 lines
+Michal.
IIRC, Michal was not happy that I propagate the KeyID to very core allcoator and we've talked about wrappers around existing APIs as a better solution.
Michal, is it correct?
Yes that is the case. I haven't seen this series and unlikely will get to it in upcoming days though so I cannot comment much more unfortunately.
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH v5 6/19 mm/khugepaged: Handle encrypted pages
In reply to Kirill A. Shutemovkhugepaged 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
@@ -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
Re: [PATCHv5 06/19] mm/khugepaged: Handle encrypted pages
Quoted 2 lines · 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.
That's not really true. We have the VMA and the address in the caller (khugepaged_scan_pmd()), but we drop the lock and have to revalidate the VMA.
Quoted 7 lines
mm/khugepaged.c +0
-0@@ -1056,6 +1056,16 @@ static void collapse_huge_page(struct mm_struct *mm,*/ anon_vma_unlock_write (vma->anon_vma);
mm/khugepaged.c static void collapse_huge_page(struct mm_struct *mm, · line 1067
Quoted 10 lines
mm/khugepaged.c +9
-0+/* +* 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); + }
I guess this isn't horribly problematic now, but if we ever keep pools of preassigned-keyids, this won't work any more.
Re: [PATCHv5 06/19] mm/khugepaged: Handle encrypted pages
Quoted 7 lines · Dave Hansen
Quoted 2 lines · 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.
That's not really true. We have the VMA and the address in the caller (
khugepaged_scan_pmd()), but we drop the lock and have to revalidate the VMA.
For !NUMA we allocate the page in khugepaged_do_scan(), well before we know VMA.
Quoted 10 lines
Quoted 10 lines
mm/khugepaged.c +0
-0@@ -1056,6 +1056,16 @@ static void collapse_huge_page(struct mm_struct *mm,*/ anon_vma_unlock_write (vma->anon_vma);
Quoted 12 lines
mm/khugepaged.c static void collapse_huge_page(struct mm_struct *mm, · line 1067
Quoted 9 lines
mm/khugepaged.c +9
-0+/* +* 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); + }I guess this isn't horribly problematic now, but if we ever keep pools of preassigned-keyids, this won't work any more.
I don't get this. What pools of preassigned-keyids are you talking about?
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 06/19] mm/khugepaged: Handle encrypted pages
Quoted 11 lines · Kirill A. Shutemov
Quoted 7 lines · Dave Hansen
Quoted 2 lines · 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.
That's not really true. We have the VMA and the address in the caller (
khugepaged_scan_pmd()), but we drop the lock and have to revalidate the VMA.For !NUMA we allocate the page in
khugepaged_do_scan(), well before we know VMA.
Ahh, thanks for clarifying. That's some more very good information about the design and progression of your patch that belongs in the changelog.
Quoted 21 lines
Quoted 8 lines
Quoted 8 lines
mm/khugepaged.c +0
-0@@ -1056,6 +1056,16 @@ static void collapse_huge_page(struct mm_struct *mm,*/ anon_vma_unlock_write (vma->anon_vma);Quoted 12 lines
mm/khugepaged.c static void collapse_huge_page(struct mm_struct *mm, · line 1067
Quoted 9 lines
mm/khugepaged.c +9
-0+/* +* 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); + }I guess this isn't horribly problematic now, but if we ever keep pools of preassigned-keyids, this won't work any more.
Quoted 1 line
I don't get this. What pools of preassigned-keyids are you talking about?
My point was that if we ever teach the allocator or something _near_ the allocator to keep pools of pre-zeroed and/or pre-cache-cleared pages, this approach will need to get changed otherwise we will double-prep pages.
My overall concern with prep_encrypted_page() in this patch set is that it's inserted pretty ad-hoc. It seems easy to miss spots where it should be. I'm also unsure of the failure mode and anything we've done to ensure that if we get this wrong, we scream clearly and loudly about what happened. Do we do something like that?
Re: [PATCHv5 06/19] mm/khugepaged: Handle encrypted pages
Quoted 10 lines · Dave Hansen
Quoted 9 lines · Kirill A. Shutemov
Quoted 7 lines · Dave Hansen
Quoted 2 lines · 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.
That's not really true. We have the VMA and the address in the caller (
khugepaged_scan_pmd()), but we drop the lock and have to revalidate the VMA.
Quoted 6 lines
Quoted 2 lines
For !NUMA we allocate the page in
khugepaged_do_scan(), well before we know VMA.Ahh, thanks for clarifying. That's some more very good information about the design and progression of your patch that belongs in the changelog.
Okay.
Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 7 lines
mm/khugepaged.c +0
-0@@ -1056,6 +1056,16 @@ static void collapse_huge_page(struct mm_struct *mm,*/ anon_vma_unlock_write (vma->anon_vma);mm/khugepaged.c static void collapse_huge_page(struct mm_struct *mm, · line 1067
Quoted 10 lines
mm/khugepaged.c +9
-0+/* +* 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); + }I guess this isn't horribly problematic now, but if we ever keep pools of preassigned-keyids, this won't work any more.
Quoted 5 lines
Quoted 1 line
I don't get this. What pools of preassigned-keyids are you talking about?
My point was that if we ever teach the allocator or something _near_ the allocator to keep pools of pre-zeroed and/or pre-cache-cleared pages, this approach will need to get changed otherwise we will double-prep pages.
It shouldn't be a problem here. It's pretty slow path. We often wait memory to be compacted before page for khugepaged gets allocated. Double-prep shouldn't have visible impact.
Quoted 5 lines
My overall concern with
prep_encrypted_page()in this patch set is that it's inserted pretty ad-hoc. It seems easy to miss spots where it should be. I'm also unsure of the failure mode and anything we've done to ensure that if we get this wrong, we scream clearly and loudly about what happened. Do we do something like that?
I have debugging patch that puts BUG_ONs around set_pte_at() to check if the page's keyid matches VMA's keyid. But that's not very systematic. We would need something better than this.
Signature · 1 line
Kirill A. Shutemov
PATCH v5 7/19 x86/mm: Mask out KeyID bits from page table entry pfn
In reply to Kirill A. ShutemovMKTME 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
@@ -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
Re: [PATCHv5 07/19] x86/mm: Mask out KeyID bits from page table entry pfn
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 594
Quoted 8 lines · Kirill A. Shutemov
arch/x86/kernel/cpu/intel.c +8
-0+ }else { +/* +* Reset __PHYSICAL_MASK. +* Maybe needed if there's inconsistent configuation +* between CPUs. +*/ + physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) -1 ; + }
This seems like an appropriate place for a WARN_ON(). Either that, or axe this code.
Re: [PATCHv5 07/19] x86/mm: Mask out KeyID bits from page table entry pfn
Quoted 12 lines · Dave Hansen
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 594
Quoted 8 lines · Kirill A. Shutemov
arch/x86/kernel/cpu/intel.c +8
-0+ }else { +/* +* Reset __PHYSICAL_MASK. +* Maybe needed if there's inconsistent configuation +* between CPUs. +*/ + physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) -1 ; + }This seems like an appropriate place for a
WARN_ON(). Either that, or axe this code.
There's pr_err_once() above in the function.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 07/19] x86/mm: Mask out KeyID bits from page table entry pfn
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Dave Hansen
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 594
Quoted 8 lines · Kirill A. Shutemov
arch/x86/kernel/cpu/intel.c +8
-0+ }else { +/* +* Reset __PHYSICAL_MASK. +* Maybe needed if there's inconsistent configuation +* between CPUs. +*/ + physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) -1 ; + }This seems like an appropriate place for a
WARN_ON(). Either that, or axe this code.
Quoted 1 line
There's
pr_err_once()above in the function.
Do you mean for the (tme_activate != tme_activate_cpu0) check?
But that's about double-activating this feature. This check is about an inconsistent configuration between two CPUs which seems totally different.
Could you explain?
Re: [PATCHv5 07/19] x86/mm: Mask out KeyID bits from page table entry pfn
Quoted 13 lines · Dave Hansen
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Dave Hansen
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 594
Quoted 8 lines · Kirill A. Shutemov
arch/x86/kernel/cpu/intel.c +8
-0+ }else { +/* +* Reset __PHYSICAL_MASK. +* Maybe needed if there's inconsistent configuation +* between CPUs. +*/ + physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) -1 ; + }This seems like an appropriate place for a
WARN_ON(). Either that, or axe this code.
Quoted 8 lines
Quoted 1 line
There's
pr_err_once()above in the function.Do you mean for the (
tme_activate!=tme_activate_cpu0) check?But that's about double-activating this feature. This check is about an inconsistent configuration between two CPUs which seems totally different.
Could you explain?
(tme_activate != tme_activate_cpu0) check is about inconsistent configuration. It checks if MSR's content on the given CPU matches MSR on CPU0.
Signature · 1 line
Kirill A. Shutemov
PATCH v5 8/19 x86/mm: Introduce variables to store number, shift and mask of KeyIDs
In reply to Kirill A. Shutemovmktme_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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
Quoted 4 lines · Kirill A. Shutemov
mktme_nr_keyidsholds number of KeyIDs available for MKTME, excluding KeyID zero which used by TME. MKTME KeyIDs start from 1.
mktme_keyid_shiftholds shift of KeyID within physical address.
I know what all these words mean, but the combination of them makes no sense to me. I still don't know what the variable does after reading this.
Is this the lowest bit in the physical address which is used for the KeyID? How many bits you must shift up a KeyID to get to the location at which it can be masked into the physical address?
Quoted 1 line
mktme_keyid_maskholds mask to extract KeyID from physical address.
Good descriptions, wrong place. Please put these in the code.
Also, the grammar constantly needs some work. "holds mask" needs to be "holds the mask".
Quoted 47 lines
Signed-off-by Kirill A. Shutemov <[email protected]>--- 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.carch/x86/include/asm/mktme.h new file +16
-0@@ -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 +3
-0@@ -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. *
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 587
Quoted 9 lines
arch/x86/kernel/cpu/intel.c +2
-4@@ -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;
Seems a bit silly that we introduce keyid_mask only to make it global a few patches later.
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 598
Quoted 11 lines
arch/x86/kernel/cpu/intel.c +3
-0}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 ; }
Should be unnecessary. These are zeroed by the compiler.
Quoted 16 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.oarch/x86/mm/mktme.c new file +0
-0
arch/x86/mm/mktme.c line 5
Quoted 5 lines
arch/x86/mm/mktme.c new file +5
-0@@ -0,0 +1,5 @@ +#include <asm/mktme.h> + +phys_addr_t mktme_keyid_mask; +int mktme_nr_keyids; +int mktme_keyid_shift;
Descriptions should be here, please, not in the changelog.
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
Quoted 4 lines · Dave Hansen
Quoted 3 lines · Kirill A. Shutemov
mktme_nr_keyidsholds number of KeyIDs available for MKTME, excluding KeyID zero which used by TME. MKTME KeyIDs start from 1.
Quoted 8 lines
Quoted 1 line
mktme_keyid_shiftholds shift of KeyID within physical address.I know what all these words mean, but the combination of them makes no sense to me. I still don't know what the variable does after reading this.
Is this the lowest bit in the physical address which is used for the KeyID? How many bits you must shift up a KeyID to get to the location at which it can be masked into the physical address?
Right.
I'm not sure what is not clear from the description. It look fine to me.
Quoted 3 lines
Quoted 1 line
mktme_keyid_maskholds mask to extract KeyID from physical address.Good descriptions, wrong place. Please put these in the code.
Okay.
Quoted 2 lines
Also, the grammar constantly needs some work. "holds mask" needs to be "holds the mask".
Right. Thanks
Quoted 51 lines
Quoted 51 lines
Signed-off-by Kirill A. Shutemov <[email protected]>--- 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.carch/x86/include/asm/mktme.h new file +16
-0@@ -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 +3
-1@@ -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;
Quoted 8 lines
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 587
Quoted 5 lines
arch/x86/kernel/cpu/intel.c +2
-3- - 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;Seems a bit silly that we introduce
keyid_maskonly to make it global a few patches later.
Is it a big deal?
I found it easier to split changes into logical pieces this way.
Quoted 5 lines
Quoted 5 lines
arch/x86/kernel/cpu/intel.c +0
-0}else {/* * Reset __PHYSICAL_MASK. @@ -591,6 +592,9 @@ static void detect_tme(struct cpuinfo_x86 *c)* between CPUs.
Quoted 8 lines
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 598
Quoted 6 lines
arch/x86/kernel/cpu/intel.c +3
-0*/ physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) -1 ; + mktme_keyid_mask =0 ; + mktme_keyid_shift =0 ; + mktme_nr_keyids =0 ; }Should be unnecessary. These are zeroed by the compiler.
No. detect_tme() called for each CPU in the system.
Quoted 15 lines
Quoted 15 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.oarch/x86/mm/mktme.c new file +0
-0
Quoted 8 lines
arch/x86/mm/mktme.c line 5
Quoted 6 lines
arch/x86/mm/mktme.c new file +5
-0@@ -0,0 +1,5 @@ +#include <asm/mktme.h> + +phys_addr_t mktme_keyid_mask; +int mktme_nr_keyids; +int mktme_keyid_shift;Descriptions should be here, please, not in the changelog.
Okay.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
Quoted 15 lines · Kirill A. Shutemov
Quoted 5 lines · Dave Hansen
Quoted 5 lines
arch/x86/kernel/cpu/intel.c +0
-0}else {/* * Reset __PHYSICAL_MASK. @@ -591,6 +592,9 @@ static void detect_tme(struct cpuinfo_x86 *c)* between CPUs. Quoted 8 lines
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 598
Quoted 6 lines
arch/x86/kernel/cpu/intel.c +3
-0*/ physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) -1 ; + mktme_keyid_mask =0 ; + mktme_keyid_shift =0 ; + mktme_nr_keyids =0 ; }Should be unnecessary. These are zeroed by the compiler.
Quoted 1 line
No.
detect_tme()called for each CPU in the system.
And then the variables are cleared out while other CPUs can access them? How is that supposed to work?
Thanks,
tglx
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
Quoted 16 lines · Thomas Gleixner
Quoted 15 lines · Kirill A. Shutemov
Quoted 13 lines · Dave Hansen
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 598
Quoted 11 lines
arch/x86/kernel/cpu/intel.c +3
-0}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 ; }Should be unnecessary. These are zeroed by the compiler.
Quoted 4 lines
Quoted 1 line
No.
detect_tme()called for each CPU in the system.And then the variables are cleared out while other CPUs can access them? How is that supposed to work?
This code path only matter in patalogical case: when MKTME configuation is inconsitent between CPUs. Basically if BIOS screwed things up we disable MKTME.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
Quoted 17 lines · Kirill A. Shutemov
Quoted 16 lines · Thomas Gleixner
Quoted 15 lines · Kirill A. Shutemov
Quoted 13 lines · Dave Hansen
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 598
Quoted 11 lines
arch/x86/kernel/cpu/intel.c +3
-0}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 ; }Should be unnecessary. These are zeroed by the compiler.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
No.
detect_tme()called for each CPU in the system.And then the variables are cleared out while other CPUs can access them? How is that supposed to work?
This code path only matter in patalogical case: when MKTME configuation is inconsitent between CPUs. Basically if BIOS screwed things up we disable MKTME.
I still don't see how that's supposed to work.
When the inconsistent CPU is brought up _AFTER_ MKTME is enabled, then how does clearing the variables help? It does not magically make all the other stuff go away.
Thanks,
tglx
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
Quoted 27 lines · Thomas Gleixner
Quoted 17 lines · Kirill A. Shutemov
Quoted 16 lines · Thomas Gleixner
Quoted 15 lines · Kirill A. Shutemov
Quoted 13 lines · Dave Hansen
arch/x86/kernel/cpu/intel.c static void detect_tme(struct cpuinfo_x86 *c) · line 598
Quoted 11 lines
arch/x86/kernel/cpu/intel.c +3
-0}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 ; }Should be unnecessary. These are zeroed by the compiler.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
No.
detect_tme()called for each CPU in the system.And then the variables are cleared out while other CPUs can access them? How is that supposed to work?
This code path only matter in patalogical case: when MKTME configuation is inconsitent between CPUs. Basically if BIOS screwed things up we disable MKTME.
Quoted 5 lines
I still don't see how that's supposed to work.
When the inconsistent CPU is brought up _AFTER_ MKTME is enabled, then how does clearing the variables help? It does not magically make all the other stuff go away.
We don't actually enable MKTME in kernel. BIOS does. Kernel makes choose to use it or not. Current design targeted to be used by userspace. So until init we don't have any other stuff to go away. We can just pretend that MKTME was never there.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
Quoted 30 lines · Kirill A. Shutemov
Quoted 29 lines · Thomas Gleixner
Quoted 17 lines · Kirill A. Shutemov
Quoted 16 lines · Thomas Gleixner
Quoted 15 lines · Kirill A. Shutemov
Quoted 13 lines · Dave Hansen
Review static void detect_tme(struct cpuinfo_x86 *c) · line 598
Quoted 11 lines
(quoted diff) +3
-0} 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; }Should be unnecessary. These are zeroed by the compiler.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
No.
detect_tme()called for each CPU in the system.And then the variables are cleared out while other CPUs can access them? How is that supposed to work?
This code path only matter in patalogical case: when MKTME configuation is inconsitent between CPUs. Basically if BIOS screwed things up we disable MKTME.
I still don't see how that's supposed to work.
Quoted 8 lines
Quoted 3 lines
When the inconsistent CPU is brought up _AFTER_ MKTME is enabled, then how does clearing the variables help? It does not magically make all the other stuff go away.
We don't actually enable MKTME in kernel. BIOS does. Kernel makes choose to use it or not. Current design targeted to be used by userspace. So until init we don't have any other stuff to go away. We can just pretend that MKTME was never there.
Hotplug is not guaranteed to happen _BEFORE_ init. Think about physical hotplug.
Thanks,
tglx
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
Quoted 35 lines · Thomas Gleixner
Quoted 34 lines · Kirill A. Shutemov
Quoted 32 lines · Thomas Gleixner
Quoted 17 lines · Kirill A. Shutemov
Quoted 16 lines · Thomas Gleixner
Quoted 15 lines · Kirill A. Shutemov
Quoted 13 lines · Dave Hansen
Review static void detect_tme(struct cpuinfo_x86 *c) · line 598
Quoted 11 lines
(quoted diff) +3
-0} 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; }Should be unnecessary. These are zeroed by the compiler.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
No.
detect_tme()called for each CPU in the system.And then the variables are cleared out while other CPUs can access them? How is that supposed to work?
This code path only matter in patalogical case: when MKTME configuation is inconsitent between CPUs. Basically if BIOS screwed things up we disable MKTME.
I still don't see how that's supposed to work.
When the inconsistent CPU is brought up _AFTER_ MKTME is enabled, then how does clearing the variables help? It does not magically make all the other stuff go away.
Quoted 7 lines
Quoted 4 lines
We don't actually enable MKTME in kernel. BIOS does. Kernel makes choose to use it or not. Current design targeted to be used by userspace. So until init we don't have any other stuff to go away. We can just pretend that MKTME was never there.
Hotplug is not guaranteed to happen _BEFORE_ init. Think about physical hotplug.
Ouch. I didn't think about this. :/
In this case I don't see how to handle the situation properly. Is it okay to WARN() && pray()?
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Thomas Gleixner
Quoted 10 lines
Quoted 5 lines
I still don't see how that's supposed to work.
When the inconsistent CPU is brought up _AFTER_ MKTME is enabled, then how does clearing the variables help? It does not magically make all the other stuff go away.
We don't actually enable MKTME in kernel. BIOS does. Kernel makes choose to use it or not. Current design targeted to be used by userspace. So until init we don't have any other stuff to go away. We can just pretend that MKTME was never there.
Quoted 7 lines
Quoted 2 lines
Hotplug is not guaranteed to happen _BEFORE_ init. Think about physical hotplug.
Ouch. I didn't think about this. :/
In this case I don't see how to handle the situation properly. Is it okay to
WARN()&&pray()?
Not really. First of all, you want to do the initial checking on the boot CPU and then when secondary CPUs are brought up, verify that they have matching parameters. If they do not, then we should just shut them down right away before they can touch anything which is TME related and mark them as 'don't online again'. That needs some extra logic in the hotplug code, but I already have played with that for different reasons. Stick a fat comment into that 'not matching' code path for now and I'll give you the magic for preventing full bringup after polishing it a bit.
Thanks,
tglx
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
Quoted 21 lines · Thomas Gleixner
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Thomas Gleixner
Quoted 10 lines
Quoted 5 lines
I still don't see how that's supposed to work.
When the inconsistent CPU is brought up _AFTER_ MKTME is enabled, then how does clearing the variables help? It does not magically make all the other stuff go away.
We don't actually enable MKTME in kernel. BIOS does. Kernel makes choose to use it or not. Current design targeted to be used by userspace. So until init we don't have any other stuff to go away. We can just pretend that MKTME was never there.
Quoted 7 lines
Quoted 2 lines
Hotplug is not guaranteed to happen _BEFORE_ init. Think about physical hotplug.
Ouch. I didn't think about this. :/
In this case I don't see how to handle the situation properly. Is it okay to
WARN()&&pray()?
Quoted 8 lines
Not really. First of all, you want to do the initial checking on the boot CPU and then when secondary CPUs are brought up, verify that they have matching parameters. If they do not, then we should just shut them down right away before they can touch anything which is TME related and mark them as 'don't online again'. That needs some extra logic in the hotplug code, but I already have played with that for different reasons. Stick a fat comment into that 'not matching' code path for now and I'll give you the magic for preventing full bringup after polishing it a bit.
Got it. Thanks!
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
In reply to Kirill A. ShutemovQuoted 9 lines · Kirill A. Shutemov
Quoted 8 lines · Dave Hansen
Quoted 4 lines · Kirill A. Shutemov
mktme_nr_keyidsholds number of KeyIDs available for MKTME, excluding KeyID zero which used by TME. MKTME KeyIDs start from 1.
mktme_keyid_shiftholds shift of KeyID within physical address.I know what all these words mean, but the combination of them makes no sense to me. I still don't know what the variable does after reading this.
Quoted 6 lines
Quoted 3 lines
Is this the lowest bit in the physical address which is used for the KeyID? How many bits you must shift up a KeyID to get to the location at which it can be masked into the physical address?
Right.
I'm not sure what is not clear from the description. It look fine to me.
Well, OK, I guess I can write a better one for you.
"Position in the PTE of the lowest bit of the KeyID"
It's also a name that could use some love (now that I know what it does). mktme_keyid_pte_shift would be much better. Or mktme_keyid_low_pte_bit.
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
Quoted 17 lines · Dave Hansen
Quoted 9 lines · Kirill A. Shutemov
Quoted 8 lines · Dave Hansen
Quoted 4 lines · Kirill A. Shutemov
mktme_nr_keyidsholds number of KeyIDs available for MKTME, excluding KeyID zero which used by TME. MKTME KeyIDs start from 1.
mktme_keyid_shiftholds shift of KeyID within physical address.I know what all these words mean, but the combination of them makes no sense to me. I still don't know what the variable does after reading this.
Quoted 6 lines
Quoted 3 lines
Is this the lowest bit in the physical address which is used for the KeyID? How many bits you must shift up a KeyID to get to the location at which it can be masked into the physical address?
Right.
I'm not sure what is not clear from the description. It look fine to me.
Quoted 7 lines
Well, OK, I guess I can write a better one for you.
"Position in the PTE of the lowest bit of the KeyID"
It's also a name that could use some love (now that I know what it does).
mktme_keyid_pte_shiftwould be much better. Ormktme_keyid_low_pte_bit.
Okay, thanks.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 08/19] x86/mm: Introduce variables to store number, shift and mask of KeyIDs
In reply to Kirill A. ShutemovQuoted 6 lines · Kirill A. Shutemov
mktme_nr_keyidsholds number of KeyIDs available for MKTME, excluding KeyID zero which used by TME. MKTME KeyIDs start from 1.
mktme_keyid_shiftholds shift of KeyID within physical address.
mktme_keyid_maskholds mask to extract KeyID from physical address.
Sorry to bring this up, but AMD SME already introduced sme_me_mask, and __sme_{set/clr} in linux/mem_encrypt.h, should we try to merge MKTME and SME to have common variables, and reuse mem_encrypt.h? IMHO sme_me_mask is sort of equivalent to'keyID=1'. And w/ different names for SME and MKTME, in other components which want to use memory encryption (ex, DMA API), we have to have explict code to distinguish MKTME and SME IMO, which is not good.
Thanks,
-Kai
Quoted 98 lines
Signed-off-by Kirill A. Shutemov <[email protected]>--- 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.carch/x86/include/asm/mktme.h new file +16
-0@@ -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 +3
-0@@ -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.
arch/x86/kernel/cpu/intel.c +0
-0* @@ -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
arch/x86/kernel/cpu/intel.c +0
-3* physical address is unusable. */ -phys_addr_t keyid_mask; - - keyid_mask = GENMASK_ULL(c->x86_phys_bits - 1, c-Quoted 1 line
x86_phys_bits-keyid_bits);arch/x86/kernel/cpu/intel.c +1
-1- physical_mask &= ~keyid_mask; + mktme_keyid_mask = GENMASK_ULL(c->x86_phys_bits - 1,
mktme_keyid_shift);arch/x86/kernel/cpu/intel.c +4
-0+ 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 +0
-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.oarch/x86/mm/Makefile +2
-0+ +obj-$(CONFIG_X86_INTEL_MKTME) += mktme.oarch/x86/mm/mktme.c new file +5
-0@@ -0,0 +1,5 @@ +#include <asm/mktme.h> + +phys_addr_t mktme_keyid_mask; +int mktme_nr_keyids; +int mktme_keyid_shift;
PATCH v5 9/19 x86/mm: Preserve KeyID on pte_modify() and pgprot_modify()
In reply to Kirill A. ShutemovAn 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
@@ -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
Re: [PATCHv5 09/19] x86/mm: Preserve KeyID on pte_modify() and pgprot_modify()
Quoted 3 lines · 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
We don't do anything special for protection keys, either. They just work too.
Quoted 10 lines
arch/x86/include/asm/pgtable_types.h +2
-0@@ -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.
arch/x86/include/asm/pgtable_types.h line 134
Quoted 10 lines
arch/x86/include/asm/pgtable_types.h +9
-0+* 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). */
Could you please make the comment block consistent? You're a lot wider than the comment above.
arch/x86/include/asm/pgtable_types.h line 139
Quoted 6 lines
arch/x86/include/asm/pgtable_types.h +3
-1-#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)
Man, I'm not a fan of this. This saves us from consuming 6 VM_HIGH bits (which we are not short on). But, at the cost of complexity.
Protection keys eat up PTE space and have an interface called pkey_mprotect(). MKTME KeyIDs take up PTE space and will probably have an interface called something_mprotect(). Yet, the implementations are going to be _very_ different with pkeys being excluded from _PAGE_CHG_MASK and KeyIDs being included.
I think you're saved here because we don't _actually_ do pte_modify() on an existing PTE: we blow the old one away upon encrypted_mprotect() and replace the PTE with a new one.
But, this is incompatible with any case where we want to change the KeyID and keep the old PTE target. With AES-XTS, I guess this is a safe assumption, but it's worrying.
Are there scenarios where we want to keep PTE contents, but change the KeyID?
Re: [PATCHv5 09/19] x86/mm: Preserve KeyID on pte_modify() and pgprot_modify()
Quoted 23 lines · Dave Hansen
Quoted 3 lines · 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 entriesWe don't do anything special for protection keys, either. They just work too.
Quoted 15 lines
arch/x86/include/asm/pgtable_types.h +7
-0@@ -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.
Quoted 8 lines
arch/x86/include/asm/pgtable_types.h line 134
Quoted 5 lines
arch/x86/include/asm/pgtable_types.h +4
-0+* +* 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). */ Could you please make the comment block consistent? You're a lot wider than the comment above.
Okay.
Quoted 9 lines
arch/x86/include/asm/pgtable_types.h line 139
Quoted 6 lines
arch/x86/include/asm/pgtable_types.h +3
-1-#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)Man, I'm not a fan of this. This saves us from consuming 6
VM_HIGHbits (which we are not short on). But, at the cost of complexity.
15, not 6. We have up-to 15 KeyID bits architecturally.
We can just have a separate field in vm_area_struct if we must. But vm_page_prot work fine so far. I don't see a big reasone to change them.
Quoted 10 lines
Protection keys eat up PTE space and have an interface called
pkey_mprotect(). MKTME KeyIDs take up PTE space and will probably have an interface calledsomething_mprotect(). Yet, the implementations are going to be _very_ different with pkeys being excluded from_PAGE_CHG_MASKand KeyIDs being included.I think you're saved here because we don't _actually_ do
pte_modify()on an existing PTE: we blow the old one away uponencrypted_mprotect()and replace the PTE with a new one.
Quoted 6 lines
But, this is incompatible with any case where we want to change the KeyID and keep the old PTE target. With AES-XTS, I guess this is a safe assumption, but it's worrying.
Are there scenarios where we want to keep PTE contents, but change the KeyID?
I don't see such scenario.
If for some reason we would need to map the same memory with different KeyID it can be done from scratch. Without modifing existing mapping.
Signature · 1 line
Kirill A. Shutemov
PATCH v5 10/19 x86/mm: Implement page_keyid() using page_ext
In reply to Kirill A. ShutemovStore 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCHv5 10/19] x86/mm: Implement page_keyid() using page_ext
Quoted 1 line · Kirill A. Shutemov
Store KeyID in bits 31:16 of extended page flags. These bits are unused.
I'd love a two sentence remind of what page_ext is and why you chose to use it. Yes, you need this. No, not everybody that you want to review this patch set knows what it is or why you chose it.
Quoted 1 line
page_keyid()returns zero untilpage_extis ready.
Is there any implication of this? Or does it not matter because we don't run userspace until after page_ext initialization is done?
Quoted 1 line
page_extinitializer enables static branch to indicate that
"enables a static branch"
Quoted 2 lines
page_keyid()can usepage_ext. The same static branch will gate MKTME readiness in general.
Can you elaborate on this a bit? It would also be a nice place to hint to the folks working hard on the APIs to ensure she checks this.
Quoted 3 lines
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.
It also wouldn't hurt to mention why you don't use an X86_FEATURE_* for this rather than an explicit static branch. I'm sure the x86 maintainers will be curious.
Re: [PATCHv5 10/19] x86/mm: Implement page_keyid() using page_ext
Quoted 6 lines · Dave Hansen
Quoted 1 line · Kirill A. Shutemov
Store KeyID in bits 31:16 of extended page flags. These bits are unused.
I'd love a two sentence remind of what
page_extis and why you chose to use it. Yes, you need this. No, not everybody that you want to review this patch set knows what it is or why you chose it.
Okay.
Quoted 4 lines
Quoted 1 line
page_keyid()returns zero untilpage_extis ready.Is there any implication of this? Or does it not matter because we don't run userspace until after
page_extinitialization is done?
It matters in sense that we shouldn't reference page_ext before it's initialized otherwise we will get garbage and crash.
Quoted 9 lines
Quoted 1 line
page_extinitializer enables static branch to indicate that"enables a static branch"
Quoted 2 lines
page_keyid()can usepage_ext. The same static branch will gate MKTME readiness in general.Can you elaborate on this a bit? It would also be a nice place to hint to the folks working hard on the APIs to ensure she checks this.
Okay.
Quoted 7 lines
Quoted 3 lines
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.It also wouldn't hurt to mention why you don't use an X86_FEATURE_* for this rather than an explicit static branch. I'm sure the x86 maintainers will be curious.
Sure.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 10/19] x86/mm: Implement page_keyid() using page_ext
Quoted 22 lines · Kirill A. Shutemov
Quoted 6 lines · Dave Hansen
Quoted 1 line · Kirill A. Shutemov
Store KeyID in bits 31:16 of extended page flags. These bits are unused.
I'd love a two sentence remind of what
page_extis and why you chose to use it. Yes, you need this. No, not everybody that you want to review this patch set knows what it is or why you chose it.Okay.
Quoted 4 lines
Quoted 1 line
page_keyid()returns zero untilpage_extis ready.Is there any implication of this? Or does it not matter because we don't run userspace until after
page_extinitialization is done?It matters in sense that we shouldn't reference
page_extbefore it's initialized otherwise we will get garbage and crash.Quoted 4 lines
Quoted 1 line
page_extinitializer enables static branch to indicate that"enables a static branch"
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
page_keyid()can usepage_ext. The same static branch will gate MKTME readiness in general.Can you elaborate on this a bit? It would also be a nice place to hint to the folks working hard on the APIs to ensure she checks this.
Okay.
At API init time we can check if (MKTME_ENABLED && mktme_nr_keyids > 0) Sounds like this is another dependency we need to check and 'wait' on? It happens after MKTME_ENABLED is set? Let me know.
Quoted 13 lines
Quoted 8 lines
Quoted 4 lines
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.It also wouldn't hurt to mention why you don't use an X86_FEATURE_* for this rather than an explicit static branch. I'm sure the x86 maintainers will be curious.
Sure.
Signature · 1 line
Kirill A. Shutemov
PATCH v5 11/19 x86/mm: Implement vma_keyid()
In reply to Kirill A. ShutemovWe 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
@@ -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_keyidint 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
@@ -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
Re: [PATCHv5 11/19] x86/mm: Implement vma_keyid()
Quoted 12 lines
arch/x86/mm/mktme.c +2
-0@@ -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, }; +
arch/x86/mm/mktme.c struct page_ext_operations page_mktme_ops = { · line 51
Quoted 10 lines
arch/x86/mm/mktme.c +10
-0+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; +}
I'm a bit surprised this isn't inlined. Not that function calls are expensive, but we *could* entirely avoid them using the normal pattern of:
// In the header:
static inline vma_keyid(...)
{
if (!mktme_enabled())
return 0;
return __vma_keyid(...); // <- the .c file version
}Re: [PATCHv5 11/19] x86/mm: Implement vma_keyid()
Quoted 26 lines · Dave Hansen
Quoted 12 lines
arch/x86/mm/mktme.c +2
-0@@ -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, }; +arch/x86/mm/mktme.c struct page_ext_operations page_mktme_ops = { · line 51
Quoted 10 lines
arch/x86/mm/mktme.c +10
-0+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; +}I'm a bit surprised this isn't inlined. Not that function calls are expensive, but we *could* entirely avoid them using the normal pattern of:
Quoted 8 lines
// In the header:
static inlinevma_keyid(...){ if (!mktme_enabled()) return 0; return __vma_keyid(...); // <- the .c file version }
Okay. I'll do this. But it would be a macros. <asm/mktme.h> gets included very early. We cannot really use jump label code there directly.
Signature · 1 line
Kirill A. Shutemov
PATCH v5 12/19 x86/mm: Implement prep_encrypted_page() and arch_free_page()
In reply to Kirill A. ShutemovThe 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
@@ -19,6 +19,12 @@ int page_keyid(const struct page *page);#define vma_keyid vma_keyidint 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
Re: [PATCHv5 12/19] x86/mm: Implement prep_encrypted_page() and arch_free_page()
The description doesn't mention the potential performance implications of this patch. That's criminal at this point.
Quoted 10 lines
arch/x86/mm/mktme.c +1
-0@@ -1,4 +1,5 @@#include <linux/mm.h> +#include <linux/highmem.h> #include <asm/mktme.h> phys_addr_t mktme_keyid_mask;@@ -49,3 +50,51 @@ int vma_keyid(struct vm_area_struct *vma) prot =pgprot_val (vma->vm_page_prot);
arch/x86/mm/mktme.c int vma_keyid(struct vm_area_struct *vma) · line 60
Quoted 10 lines
arch/x86/mm/mktme.c +8
-0return (prot & mktme_keyid_mask) >> mktme_keyid_shift; } + +void prep_encrypted_page (struct page *page,int order,int keyid,bool zero) +{ +int i; + +/* It's not encrypted page: nothing to do */ +if (!keyid) +return ;
prep_encrypted_page() is called in the fast path in the page allocator. This out-of-line copy costs a function call for all users and this is also out of the reach of the compiler to understand that keyid!=0 is unlikely.
I think this needs to be treated to the inline-in-the-header treatment.
arch/x86/mm/mktme.c int vma_keyid(struct vm_area_struct *vma) · line 69
Quoted 8 lines
arch/x86/mm/mktme.c +8
-0+/* +* 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. +* +* We flush cache before allocating encrypted page +*/ +clflush_cache_range (page_address (page),PAGE_SIZE << order);
It's also worth pointing out that this must be done on the keyid alias direct map, not the normal one.
Wait a sec... How do we know which direct map to use?
Quoted 10 lines
arch/x86/mm/mktme.c +10
-0+for (i =0 ; i < (1 << order); i++) { +/* All pages coming out of the allocator should have KeyID 0 */ +WARN_ON_ONCE (lookup_page_ext (page)->keyid); +lookup_page_ext (page)->keyid = keyid; + +/* Clear the page after the KeyID is set. */ +if (zero) +clear_highpage (page); + + page++;
arch/x86/mm/mktme.c int vma_keyid(struct vm_area_struct *vma) · line 90
Quoted 10 lines
arch/x86/mm/mktme.c +10
-0+ } +} + +void arch_free_page (struct page *page,int order) +{ +int i; + +/* It's not encrypted page: nothing to do */ +if (!page_keyid (page)) +return ;
Ditto on pushing this to a header.
arch/x86/mm/mktme.c int vma_keyid(struct vm_area_struct *vma) · line 89
Quoted 1 line
arch/x86/mm/mktme.c +1
-0+clflush_cache_range (page_address (page),PAGE_SIZE << order);
OK, how do we know which copy of the direct map to use, here?
Quoted 8 lines
arch/x86/mm/mktme.c +7
-0+for (i =0 ; i < (1 << order); i++) { +/* Check if the page has reasonable KeyID */ +WARN_ON_ONCE (lookup_page_ext (page)->keyid > mktme_nr_keyids); +lookup_page_ext (page)->keyid =0 ; + page++; + } +}
Re: [PATCHv5 12/19] x86/mm: Implement prep_encrypted_page() and arch_free_page()
Quoted 29 lines · Dave Hansen
The description doesn't mention the potential performance implications of this patch. That's criminal at this point.
Quoted 10 lines
arch/x86/mm/mktme.c +1
-0@@ -1,4 +1,5 @@#include <linux/mm.h> +#include <linux/highmem.h> #include <asm/mktme.h> phys_addr_t mktme_keyid_mask;@@ -49,3 +50,51 @@ int vma_keyid(struct vm_area_struct *vma) prot =pgprot_val (vma->vm_page_prot);arch/x86/mm/mktme.c int vma_keyid(struct vm_area_struct *vma) · line 60
Quoted 10 lines
arch/x86/mm/mktme.c +8
-0return (prot & mktme_keyid_mask) >> mktme_keyid_shift; } + +void prep_encrypted_page (struct page *page,int order,int keyid,bool zero) +{ +int i; + +/* It's not encrypted page: nothing to do */ +if (!keyid) +return ;
prep_encrypted_page()is called in the fast path in the page allocator. This out-of-line copy costs a function call for all users and this is also out of the reach of the compiler to understand that keyid!=0 is unlikely.
Quoted 1 line
I think this needs to be treated to the inline-in-the-header treatment.
Okay. Again as a macros.
Quoted 12 lines
arch/x86/mm/mktme.c int vma_keyid(struct vm_area_struct *vma) · line 69
Quoted 8 lines
arch/x86/mm/mktme.c +8
-0+/* +* 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. +* +* We flush cache before allocating encrypted page +*/ +clflush_cache_range (page_address (page),PAGE_SIZE << order);It's also worth pointing out that this must be done on the keyid alias direct map, not the normal one.
Quoted 1 line
Wait a sec... How do we know which direct map to use?
page_address() -> lowmem_page_address() -> page_to_virt()
page_to_virt() returns virtual address from the right direct mapping.
Quoted 23 lines
Quoted 10 lines
arch/x86/mm/mktme.c +10
-0+for (i =0 ; i < (1 << order); i++) { +/* All pages coming out of the allocator should have KeyID 0 */ +WARN_ON_ONCE (lookup_page_ext (page)->keyid); +lookup_page_ext (page)->keyid = keyid; + +/* Clear the page after the KeyID is set. */ +if (zero) +clear_highpage (page); + + page++;arch/x86/mm/mktme.c int vma_keyid(struct vm_area_struct *vma) · line 90
Quoted 10 lines
arch/x86/mm/mktme.c +10
-0+ } +} + +void arch_free_page (struct page *page,int order) +{ +int i; + +/* It's not encrypted page: nothing to do */ +if (!page_keyid (page)) +return ;Ditto on pushing this to a header.
Quoted 3 lines
arch/x86/mm/mktme.c int vma_keyid(struct vm_area_struct *vma) · line 89
Quoted 1 line
arch/x86/mm/mktme.c +1
-0+clflush_cache_range (page_address (page),PAGE_SIZE << order);OK, how do we know which copy of the direct map to use, here?
The same way as above.
Quoted 9 lines
Quoted 8 lines
arch/x86/mm/mktme.c +7
-0+for (i =0 ; i < (1 << order); i++) { +/* Check if the page has reasonable KeyID */ +WARN_ON_ONCE (lookup_page_ext (page)->keyid > mktme_nr_keyids); +lookup_page_ext (page)->keyid =0 ; + page++; + } +}
Signature · 1 line
Kirill A. Shutemov
PATCH v5 13/19 x86/mm: Rename CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING
In reply to Kirill A. ShutemovRename 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
@@ -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
@@ -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
PATCH v5 14/19 x86/mm: Allow to disable MKTME after enumeration
In reply to Kirill A. ShutemovThe 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
@@ -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
@@ -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
@@ -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
PATCH v5 15/19 x86/mm: Detect MKTME early
In reply to Kirill A. ShutemovWe 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
@@ -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
PATCH v5 16/19 x86/mm: Calculate direct mapping size
In reply to Kirill A. ShutemovThe 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.
With MKTME size of direct mapping has to be power-of-2. It makes implementation of __pa() efficient.
--- Documentation/x86/x86_64/mm.txt | 4 +++ arch/x86/include/asm/page_64.h | 2 ++ arch/x86/include/asm/setup.h | 6 ++++ arch/x86/kernel/head64.c | 4 +++ arch/x86/kernel/setup.c | 3 ++ arch/x86/mm/init_64.c | 58 +++++++++++++++++++++++++++++++++ arch/x86/mm/kaslr.c | 11 +++++-- 7 files changed, 85 insertions(+), 3 deletions(-)
Documentation/x86/x86_64/mm.txt +4 -0
@@ -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 +2 -0
@@ -14,6 +14,8 @@ 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; +extern unsigned long direct_mapping_mask;static inline unsigned long __phys_addr_nodebug (unsigned long x) {
arch/x86/include/asm/setup.h +6 -0
@@ -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 +4 -0
@@ -59,6 +59,10 @@ 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); +unsigned long direct_mapping_mask__ro_after_init =-1UL ; +EXPORT_SYMBOL (direct_mapping_mask);#define __head __section(.head.text)
arch/x86/kernel/setup.c +3 -0
@@ -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 +58 -0
64 lines not on this page · raw message
arch/x86/mm/kaslr.c +8 -3
@@ -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
PATCH v5 17/19 x86/mm: Implement sync_direct_mapping()
In reply to Kirill A. ShutemovFor 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
@@ -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
@@ -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
Re: [PATCHv5 17/19] x86/mm: Implement sync_direct_mapping()
Quoted 4 lines · Kirill A. Shutemov
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(+)
I'm not the maintainer. But, NAK from me on this on the diffstat alone.
There is simply too much technical debt here. There is no way this code is not riddled with bugs and I would bet lots of beer on the fact that this has received little to know testing with all the combinations that matter, like memory hotplug. I'd love to be proven wrong, so I eagerly await to be dazzled with the test results that have so far escaped mention in the changelog.
Please make an effort to refactor this to reuse the code that we already have to manage the direct mapping. We can't afford 455 new lines of page table manipulation that nobody tests or runs.
How _was_ this tested?
Re: [PATCHv5 17/19] x86/mm: Implement sync_direct_mapping()
Quoted 15 lines · Dave Hansen
Quoted 4 lines · Kirill A. Shutemov
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(+)I'm not the maintainer. But, NAK from me on this on the diffstat alone.
There is simply too much technical debt here. There is no way this code is not riddled with bugs and I would bet lots of beer on the fact that this has received little to know testing with all the combinations that matter, like memory hotplug. I'd love to be proven wrong, so I eagerly await to be dazzled with the test results that have so far escaped mention in the changelog.
Quoted 3 lines
Please make an effort to refactor this to reuse the code that we already have to manage the direct mapping. We can't afford 455 new lines of page table manipulation that nobody tests or runs.
I'll look in this once again. But I'm not sure that there's any better solution.
The problem boils down to page allocation issue. We are not be able to allocate enough page tables in early boot for all direct mappings. At that stage we have very limited pool of pages that can be used for page tables. The pool is allocated at compile-time and it's not enough to handle MKTME.
Syncing approach appeared to be the simplest to me.
Other possibility I see is to write down a journal of operations on direct mappings to be replayed once we have proper page allocator around.
Quoted 1 line
How _was_ this tested?
Besides normal boot with MTKME enabled and access pages via new direct mappings, I also test memory hotplug and hotremove with QEMU.
Ideally we wound need some self-test for this. But I don't see a way to simulate hotplug and hotremove. Soft offlining doesn't cut it. We actually need to see the ACPI event to trigger the code.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 17/19] x86/mm: Implement sync_direct_mapping()
Quoted 7 lines · Kirill A. Shutemov
On Wed, Jul 18, 2018 at 05:01:37PM -0700, Dave Hansen wrote:>> Please make an effort to refactor this to reuse the code that we already
Quoted 2 lines
have to manage the direct mapping. We can't afford 455 new lines of page table manipulation that nobody tests or runs.
I'll look in this once again. But I'm not sure that there's any better solution.
Quoted 6 lines
The problem boils down to page allocation issue. We are not be able to allocate enough page tables in early boot for all direct mappings. At that stage we have very limited pool of pages that can be used for page tables. The pool is allocated at compile-time and it's not enough to handle MKTME.
Syncing approach appeared to be the simplest to me.
If that is, indeed, the primary motivation for this design, then please call that out in the changelog. It's exceedingly difficult to review without this information.
We also need data and facts, please.
Which pool are we talking about? How large is it now? How large would it need to be to accommodate MKTME? How much memory do we need to map before we run into issues?
Quoted 4 lines
Quoted 1 line
How _was_ this tested?
Besides normal boot with MTKME enabled and access pages via new direct mappings, I also test memory hotplug and hotremove with QEMU.
... also great changelog fodder.
Quoted 3 lines
Ideally we wound need some self-test for this. But I don't see a way to simulate hotplug and hotremove. Soft offlining doesn't cut it. We actually need to see the ACPI event to trigger the code.
That's something that we have to go fix. For the online side, we always have the "probe" file. I guess nobody ever bothered to make an equivalent for the remove side. But, that doesn't seem like an insurmountable problem to me.
PATCH v5 18/19 x86/mm: Handle encrypted memory in page_to_virt() and __pa()
In reply to Kirill A. ShutemovPer-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
@@ -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
@@ -24,7 +24,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_mask; }#ifdef CONFIG_DEBUG_VIRTUAL
Signature · 1 line
2.18.0
Re: [PATCHv5 18/19] x86/mm: Handle encrypted memory in page_to_virt() and __pa()
Quoted 18 lines · 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.Signed-off-by Kirill A. Shutemov <[email protected]>--- 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 +0
-0
arch/x86/include/asm/mktme.h void arch_free_page(struct page *page, int order); · line 33
Quoted 5 lines
arch/x86/include/asm/mktme.h +2
-0@@ -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)
This really does not belong into the mktme header.
Please make this the unconditional x86 page_to_virt() implementation in asm/page.h, which is the canonical and obvious place for it.
The page_keyid() name is quite generic as well. Can this please have some kind of reference to the underlying mechanism, i.e. mktme?
Please hide the multiplication with direct_mapping_size in the mktme header as well. It's non interesting for the !MKTME case. Something like:
#define page_to_virt(x) \ (__va(PFN_PHYS(page_to_pfn(x))) + mktme_page_to_virt_offset(x))
makes it immediately clear where to look and also makes it clear that the offset will be 0 for a !MKTME enabled kernel and (hopefully) for all !MKTME enabled processors as well.
And then have a proper implementation of mktme_page_to_virt_offset() with a proper comment what on earth this is doing. It might be all obvious to you now, but it's completely non obvious for the casual reader and you will have to twist your brain around it 6 month from now as well.
arch/x86/include/asm/page_64.h static inline unsigned long __phys_addr_nodebug(unsigned long x) · line 27
Quoted 13 lines
arch/x86/include/asm/mktme.h +0
-0#else #define mktme_keyid_mask ((phys_addr_t)0)#define mktme_nr_keyids 0arch/x86/include/asm/page_64.h +1
-1@@ -24,7 +24,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_mask;
This hunk also lacks any explanation both in the changelog and in form of a comment.
Quoted 2 lines
Per-KeyID direct mappings require changes into how we find the right virtual address for a page and virt-to-phys address translations.
That's pretty useless as it does just tell about 'changes', but not at all about what kind of changes and why these changes are required. It's really not helpful to assume that everyone stumbling over this will know the whole story especially not 6 month after this has been merged and then someone ends up with a bisect on that change.
While at it, please get rid of the 'we'. We are neither CPUs nor code.
Thanks,
tglx
Re: [PATCHv5 18/19] x86/mm: Handle encrypted memory in page_to_virt() and __pa()
Quoted 23 lines · Thomas Gleixner
Quoted 21 lines · 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.Signed-off-by Kirill A. Shutemov <[email protected]>--- 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 +0
-0@@ -29,6 +29,9 @@ void arch_free_page(struct page *page, int order);int sync_direct_mapping (void );
Quoted 7 lines
arch/x86/include/asm/mktme.h void arch_free_page(struct page *page, int order); · line 33
Quoted 2 lines
arch/x86/include/asm/mktme.h +2
-0+#define page_to_virt (x) \ + (__va(PFN_PHYS(page_to_pfn(x))) + page_keyid(x) * direct_mapping_size)This really does not belong into the mktme header.
Please make this the unconditional x86
page_to_virt()implementation inasm/page.h, which is the canonical and obvious place for it.
Okay. (and I owe Dave a beer on this :P)
Quoted 2 lines
The
page_keyid()name is quite generic as well. Can this please have some kind of reference to the underlying mechanism, i.e. mktme?
Hm. I intentially get the name generic. It used outside arch/x86.
We can have an alias (mktme_page_keyid() ?) to be used in arch/x86 to indicate undelying mechanism.
Is it what you want to see?
Quoted 6 lines
Please hide the multiplication with
direct_mapping_sizein the mktme header as well. It's non interesting for the !MKTME case. Something like:#define page_to_virt(x) \ (__va(PFN_PHYS(page_to_pfn(x))) + mktme_page_to_virt_offset(x))
Quoted 8 lines
makes it immediately clear where to look and also makes it clear that the offset will be 0 for a !MKTME enabled kernel and (hopefully) for all !MKTME enabled processors as well.
And then have a proper implementation of
mktme_page_to_virt_offset()with a proper comment what on earth this is doing. It might be all obvious to you now, but it's completely non obvious for the casual reader and you will have to twist your brain around it 6 month from now as well.
Sure.
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 0arch/x86/include/asm/page_64.h +0
-0@@ -24,7 +24,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 5 lines
arch/x86/include/asm/page_64.h static inline unsigned long __phys_addr_nodebug(unsigned long x) · line 27
Quoted 2 lines
arch/x86/include/asm/page_64.h +1
-1-return x; +return x & direct_mapping_mask;This hunk also lacks any explanation both in the changelog and in form of a comment.
I'll fix it.
Quoted 10 lines
Quoted 2 lines
Per-KeyID direct mappings require changes into how we find the right virtual address for a page and virt-to-phys address translations.
That's pretty useless as it does just tell about 'changes', but not at all about what kind of changes and why these changes are required. It's really not helpful to assume that everyone stumbling over this will know the whole story especially not 6 month after this has been merged and then someone ends up with a bisect on that change.
While at it, please get rid of the 'we'. We are neither CPUs nor code.
Okay. I'll rewrite this.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv5 18/19] x86/mm: Handle encrypted memory in page_to_virt() and __pa()
Quoted 3 lines · Kirill A. Shutemov
page_to_virt()definition overwrites default macros provided by <linux/mm.h>. We only overwrite the macros if MTKME is enabled compile-time.
Can you remind me why we need this in page_to_virt() as opposed to in the kmap() code? Is it because we have lots of 64-bit code that doesn't use kmap() or something?
Re: [PATCHv5 18/19] x86/mm: Handle encrypted memory in page_to_virt() and __pa()
Quoted 8 lines · Dave Hansen
Quoted 3 lines · Kirill A. Shutemov
page_to_virt()definition overwrites default macros provided by <linux/mm.h>. We only overwrite the macros if MTKME is enabled compile-time.Can you remind me why we need this in
page_to_virt()as opposed to in thekmap()code? Is it because we have lots of 64-bit code that doesn't usekmap()or something?
I just found it most suitable. It should cover all cases, even if kmap() is not used.
Signature · 1 line
Kirill A. Shutemov
PATCH v5 19/19 x86: Introduce CONFIG_X86_INTEL_MKTME
In reply to Kirill A. ShutemovAdd 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
@@ -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 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
Re: [PATCHv5 19/19] x86: Introduce CONFIG_X86_INTEL_MKTME
Hi!
Quoted 29 lines
Add new config option to enabled/disable Multi-Key Total Memory Encryption support.
MKTME uses
MEMORY_PHYSICAL_PADDINGto reserve enough space in per-KeyID direct mappings for memory hotplug.Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/Kconfig | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)arch/x86/Kconfig +10
-0@@ -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
arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1545
Quoted 10 lines
arch/x86/Kconfig +7
-0+ transparent memory encryption in 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"
Would it be good to provide documentation, or link to documentation, explaining what security guarantees this is supposed to provide, and what disadvantages (if any) it has? I guess it costs a bit of performance...
I see that TME helps with cold boot attacks.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCHv5 19/19] x86: Introduce CONFIG_X86_INTEL_MKTME
Quoted 37 lines · Pavel Machek
Hi!
Quoted 35 lines
Add new config option to enabled/disable Multi-Key Total Memory Encryption support.
MKTME uses
MEMORY_PHYSICAL_PADDINGto reserve enough space in per-KeyID direct mappings for memory hotplug.Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/Kconfig | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)arch/x86/Kconfig +16
-0@@ -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 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.
Quoted 8 lines
arch/x86/Kconfig config ARCH_USE_MEMREMAP_PROT · line 1545
Quoted 4 lines
arch/x86/Kconfig +1
-0+ # Common NUMA Features config NUMA bool "Numa Memory Allocation and Scheduler Support"Would it be good to provide documentation, or link to documentation, explaining what security guarantees this is supposed to provide, and what disadvantages (if any) it has?
The main goal is to add additional level of isolation between different tenants of a machine. It mostly targeted to VMs and protect against leaking information between guests.
In the design kernel (or hypervisor) is trusted and have a mean to access encrypted memory as long as key is programmed into the CPU.
Worth noting that encryption happens in memory controller so all data in caches of all levels are plain-text.
The spec can be found here:
Quoted 1 line
I guess it costs a bit of performance...
The most overhead is paid on allocation and freeing of encrypted pages:
switching between keyids for a page requires cache flushing.
Access time to encrypted memory *shouldn't* be measurably slower. Encryption overhead is hidden within other latencies in memory pipeline.
Quoted 1 line
I see that TME helps with cold boot attacks.
Right.
Signature · 1 line
Kirill A. Shutemov