Skip to contentSkip to conversation

mm: new function to forbid zeropage mappings for a process

PATCH In review Dominik Dingel 4 participants 13 messages Oct 22, 2014 – Oct 23, 2014

Subsystems (7)
Memory management KVM MM core s390 +3 more subsystems
lore
Share & tools
lore
#0 lore raw

v3 cover mm: new function to forbid zeropage mappings for a process

s390 has the special notion of storage keys which are some sort of page flags associated with physical pages and live outside of direct addressable memory. These storage keys can be queried and changed with a special set of instructions. The mentioned instructions behave quite nicely under virtualization, if there is:
- an invalid pte, then the instructions will work on memory in the host page table
- a valid pte, then the instructions will work with the real storage key

Thanks to Martin with his software reference and dirty bit tracking, the kernel does not issue any storage key instructions as now a software based approach will be taken, on the other hand distributions in the wild are currently using them.

However, for virtualized guests we still have a problem with guest pages mapped to zero pages and the kernel same page merging.
With each one multiple guest pages will point to the same physical page and share the same storage key.

Let's fix this by introducing a new function which s390 will define to forbid new zero page mappings. If the guest issues a storage key related instruction we flag the mm_struct, drop existing zero page mappings and unmerge the guest memory.

v2 -> v3:
- Clearing up patch description Patch 3/4
- removing unnecessary flag in mmu_context (Paolo)

v1 -> v2:
- Following Dave and Paolo suggestion removing the vma flag

Dominik Dingel (4):
s390/mm: recfactor global pgste updates
mm: introduce mm_forbids_zeropage function
s390/mm: prevent and break zero page mappings in case of storage keys s390/mm: disable KSM for storage key enabled pages

 arch/s390/include/asm/pgalloc.h |   2 -
 arch/s390/include/asm/pgtable.h |   8 +-
 arch/s390/kvm/kvm-s390.c        |   2 +-
 arch/s390/kvm/priv.c            |  17 ++--
 arch/s390/mm/pgtable.c          | 180 ++++++++++++++++++----------------------
 include/linux/mm.h              |   4 +
 mm/huge_memory.c                |   2 +-
 mm/memory.c                     |   2 +-
 8 files changed, 106 insertions(+), 111 deletions(-)
Signature · 7 lines
1.8.5.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw

PATCH v3 1/4 s390/mm: recfactor global pgste updates

Replace the s390 specific page table walker for the pgste updates with a call to the common code walk_page_range function. There are now two pte modification functions, one for the reset of the CMMA state and another one for the initialization of the storage keys.

---
 arch/s390/include/asm/pgalloc.h |   2 -
 arch/s390/include/asm/pgtable.h |   1 +
 arch/s390/kvm/kvm-s390.c        |   2 +-
 arch/s390/mm/pgtable.c          | 153 ++++++++++++++--------------------------
 4 files changed, 56 insertions(+), 102 deletions(-)
arch/s390/include/asm/pgalloc.h +0 -2
index 9e18a61..120e126 100644
@@ -22,8 +22,6 @@ unsigned long *page_table_alloc(struct mm_struct *, unsigned long);
 void page_table_free(struct mm_struct *, unsigned long *);
 void page_table_free_rcu(struct mmu_gather *, unsigned long *);
 
-void page_table_reset_pgste(struct mm_struct *, unsigned long, unsigned long,
-			    bool init_skey);
 int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
 			  unsigned long key, bool nq);
 
arch/s390/include/asm/pgtable.h +1 -0
index 5efb2fe..1e991f6a 100644
@@ -1750,6 +1750,7 @@ extern int vmem_add_mapping(unsigned long start, unsigned long size);
 extern int vmem_remove_mapping(unsigned long start, unsigned long size);
 extern int s390_enable_sie(void);
 extern void s390_enable_skey(void);
+extern void s390_reset_cmma(struct mm_struct *mm);
 
 /*
  * No page table caches to initialise
arch/s390/kvm/kvm-s390.c +1 -1
index 81b0e11..7a33c11 100644
@@ -281,7 +281,7 @@ static int kvm_s390_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
 	case KVM_S390_VM_MEM_CLR_CMMA:
 		mutex_lock(&kvm->lock);
 		idx = srcu_read_lock(&kvm->srcu);
-		page_table_reset_pgste(kvm->arch.gmap->mm, 0, TASK_SIZE, false);
+		s390_reset_cmma(kvm->arch.gmap->mm);
 		srcu_read_unlock(&kvm->srcu, idx);
 		mutex_unlock(&kvm->lock);
 		ret = 0;
arch/s390/mm/pgtable.c +54 -99

177 lines not on this page · raw message

Signature · 7 lines
1.8.5.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

PATCH v3 2/4 mm: introduce mm_forbids_zeropage function

In reply to Dominik Dingel

Add a new function stub to allow architectures to disable for an mm_structthe backing of non-present, anonymous pages with read-only empty zero pages.

---
 include/linux/mm.h | 4 ++++
 mm/huge_memory.c   | 2 +-
 mm/memory.c        | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)
include/linux/mm.h +4 -0
index cd33ae2..0a2022e 100644
@@ -56,6 +56,10 @@ extern int sysctl_legacy_va_layout;
 #define __pa_symbol(x)  __pa(RELOC_HIDE((unsigned long)(x), 0))
 #endif
 
+#ifndef mm_forbids_zeropage
+#define mm_forbids_zeropage(X)  (0)
+#endif
+
 extern unsigned long sysctl_user_reserve_kbytes;
 extern unsigned long sysctl_admin_reserve_kbytes;
 
mm/huge_memory.c +1 -1
index de98415..357a381 100644
@@ -805,7 +805,7 @@ int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
 		return VM_FAULT_OOM;
 	if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
 		return VM_FAULT_OOM;
-	if (!(flags & FAULT_FLAG_WRITE) &&
+	if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
 			transparent_hugepage_use_zero_page()) {
 		spinlock_t *ptl;
 		pgtable_t pgtable;
mm/memory.c +1 -1
index 64f82aa..f275a9d 100644
@@ -2640,7 +2640,7 @@ static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
 		return VM_FAULT_SIGBUS;
 
 	/* Use the zero-page for reads */
-	if (!(flags & FAULT_FLAG_WRITE)) {
+	if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm)) {
 		entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
 						vma->vm_page_prot));
 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
Signature · 7 lines
1.8.5.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#7 lore raw

PATCH v3 3/4 s390/mm: prevent and break zero page mappings in case of storage keys

In reply to Dominik Dingel

As soon as storage keys are enabled we need to stop working on zero page mappings to prevent inconsistencies between storage keys and pgste.

Otherwise following data corruption could happen:
1) guest enables storage key
2) guest sets storage key for not mapped page X
-> change goes to PGSTE
3) guest reads from page X
-> as X was not dirty before, the page will be zero page backed,
storage key from PGSTE for X will go to storage key for zero page
4) guest sets storage key for not mapped page Y (same logic as above
5) guest reads from page Y
-> as Y was not dirty before, the page will be zero page backed,
storage key from PGSTE for Y will got to storage key for zero page overwriting storage key for X

While holding the mmap sem, we are safe against changes on entries we already fixed, as every fault would need to take the mmap_sem (read).

Other vCPUs executing storage key instructions will get a one time interception and be serialized also with mmap_sem.

---
 arch/s390/include/asm/pgtable.h |  5 +++++
 arch/s390/mm/pgtable.c          | 13 ++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
arch/s390/include/asm/pgtable.h +5 -0
index 1e991f6a..0da98d6 100644
@@ -481,6 +481,11 @@ static inline int mm_has_pgste(struct mm_struct *mm)
 	return 0;
 }
 
+/*
+ * In the case that a guest uses storage keys
+ * faults should no longer be backed by zero pages
+ */
+#define mm_forbids_zeropage mm_use_skey
 static inline int mm_use_skey(struct mm_struct *mm)
 {
 #ifdef CONFIG_PGSTE
arch/s390/mm/pgtable.c +12 -1
index ab55ba8..58d7eb2 100644
@@ -1309,6 +1309,15 @@ static int __s390_enable_skey(pte_t *pte, unsigned long addr,
 	pgste_t pgste;
 
 	pgste = pgste_get_lock(pte);
+	/*
+	 * Remove all zero page mappings,
+	 * after establishing a policy to forbid zero page mappings
+	 * following faults for that page will get fresh anonymous pages
+	 */
+	if (is_zero_pfn(pte_pfn(*pte))) {
+		ptep_flush_direct(walk->mm, addr, pte);
+		pte_val(*pte) = _PAGE_INVALID;
+	}
 	/* Clear storage key */
 	pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT |
 			      PGSTE_GR_BIT | PGSTE_GC_BIT);
@@ -1327,9 +1336,11 @@ void s390_enable_skey(void)
 	down_write(&mm->mmap_sem);
 	if (mm_use_skey(mm))
 		goto out_up;
+
+	mm->context.use_skey = 1;
+
 	walk.mm = mm;
 	walk_page_range(0, TASK_SIZE, &walk);
-	mm->context.use_skey = 1;
 
 out_up:
 	up_write(&mm->mmap_sem);
Signature · 7 lines
1.8.5.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#9 lore raw

PATCH v3 4/4 s390/mm: disable KSM for storage key enabled pages

In reply to Dominik Dingel

When storage keys are enabled unmerge already merged pages and prevent new pages from being merged.

---
 arch/s390/include/asm/pgtable.h |  2 +-
 arch/s390/kvm/priv.c            | 17 ++++++++++++-----
 arch/s390/mm/pgtable.c          | 16 +++++++++++++++-
 3 files changed, 28 insertions(+), 7 deletions(-)
arch/s390/include/asm/pgtable.h +1 -1
index 0da98d6..dfb38af 100644
@@ -1754,7 +1754,7 @@ static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
 extern int vmem_add_mapping(unsigned long start, unsigned long size);
 extern int vmem_remove_mapping(unsigned long start, unsigned long size);
 extern int s390_enable_sie(void);
-extern void s390_enable_skey(void);
+extern int s390_enable_skey(void);
 extern void s390_reset_cmma(struct mm_struct *mm);
 
 /*
arch/s390/kvm/priv.c +12 -5
index f89c1cd..e0967fd 100644
@@ -156,21 +156,25 @@ static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
-static void __skey_check_enable(struct kvm_vcpu *vcpu)
+static int __skey_check_enable(struct kvm_vcpu *vcpu)
 {
+	int rc = 0;
 	if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
-		return;
+		return rc;
 
-	s390_enable_skey();
+	rc = s390_enable_skey();
 	trace_kvm_s390_skey_related_inst(vcpu);
 	vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
+	return rc;
 }
 
 
 static int handle_skey(struct kvm_vcpu *vcpu)
 {
-	__skey_check_enable(vcpu);
+	int rc = __skey_check_enable(vcpu);
 
+	if (rc)
+		return rc;
 	vcpu->stat.instruction_storage_key++;
 
 	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
@@ -692,7 +696,10 @@ static int handle_pfmf(struct kvm_vcpu *vcpu)
 		}
 
 		if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
-			__skey_check_enable(vcpu);
+			int rc = __skey_check_enable(vcpu);
+
+			if (rc)
+				return rc;
 			if (set_guest_storage_key(current->mm, useraddr,
 					vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
 					vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
arch/s390/mm/pgtable.c +15 -1
index 58d7eb2..82aa528 100644
@@ -18,6 +18,8 @@
 #include <linux/rcupdate.h>
 #include <linux/slab.h>
 #include <linux/swapops.h>
+#include <linux/ksm.h>
+#include <linux/mman.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -1328,22 +1330,34 @@ static int __s390_enable_skey(pte_t *pte, unsigned long addr,
 	return 0;
 }
 
-void s390_enable_skey(void)
+int s390_enable_skey(void)
 {
 	struct mm_walk walk = { .pte_entry = __s390_enable_skey };
 	struct mm_struct *mm = current->mm;
+	struct vm_area_struct *vma;
+	int rc = 0;
 
 	down_write(&mm->mmap_sem);
 	if (mm_use_skey(mm))
 		goto out_up;
 
 	mm->context.use_skey = 1;
+	for (vma = mm->mmap; vma; vma = vma->vm_next) {
+		if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
+				MADV_UNMERGEABLE, &vma->vm_flags)) {
+			mm->context.use_skey = 0;
+			rc = -ENOMEM;
+			goto out_up;
+		}
+	}
+	mm->def_flags &= ~VM_MERGEABLE;
 
 	walk.mm = mm;
 	walk_page_range(0, TASK_SIZE, &walk);
 
 out_up:
 	up_write(&mm->mmap_sem);
+	return rc;
 }
 EXPORT_SYMBOL_GPL(s390_enable_skey);
 
Signature · 7 lines
1.8.5.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#11 lore raw
In reply to Dominik Dingel
Quoted 17 lines · Dominik Dingel

s390 has the special notion of storage keys which are some sort of page flags associated with physical pages and live outside of direct addressable memory. These storage keys can be queried and changed with a special set of instructions. The mentioned instructions behave quite nicely under virtualization, if there is:
- an invalid pte, then the instructions will work on memory in the host page table
- a valid pte, then the instructions will work with the real storage key

Thanks to Martin with his software reference and dirty bit tracking, the kernel does not issue any storage key instructions as now a software based approach will be taken, on the other hand distributions in the wild are currently using them.

However, for virtualized guests we still have a problem with guest pages mapped to zero pages and the kernel same page merging.
With each one multiple guest pages will point to the same physical page and share the same storage key.

Quoted 8 lines

Let's fix this by introducing a new function which s390 will define to forbid new zero page mappings. If the guest issues a storage key related instruction we flag the mm_struct, drop existing zero page mappings and unmerge the guest memory.

v2 -> v3:
- Clearing up patch description Patch 3/4
- removing unnecessary flag in mmu_context (Paolo)

... and zero the mm_use_skey flag correctly, too. :)

Quoted 2 lines

v1 -> v2:
- Following Dave and Paolo suggestion removing the vma flag

Thanks, the patches look good. I expect that they will either go in through the s390 tree, or come in via Christian.

If the latter, Martin, please reply with your Acked-by.

Paolo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer Reviewed-by on v3 2/4
#3 lore raw

Re: [PATCH 2/4] mm: introduce mm_forbids_zeropage function

In reply to Dominik Dingel
Quoted 45 lines · Dominik Dingel

Add a new function stub to allow architectures to disable for an mm_structthe backing of non-present, anonymous pages with read-only empty zero pages.

---
 include/linux/mm.h | 4 ++++
 mm/huge_memory.c   | 2 +-
 mm/memory.c        | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)
include/linux/mm.h +4 -0
index cd33ae2..0a2022e 100644
@@ -56,6 +56,10 @@ extern int sysctl_legacy_va_layout;
 #define __pa_symbol(x)  __pa(RELOC_HIDE((unsigned long)(x), 0))
 #endif
 
+#ifndef mm_forbids_zeropage
+#define mm_forbids_zeropage(X)  (0)
+#endif
+
 extern unsigned long sysctl_user_reserve_kbytes;
 extern unsigned long sysctl_admin_reserve_kbytes;
 
mm/huge_memory.c +1 -1
index de98415..357a381 100644
@@ -805,7 +805,7 @@ int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
 		return VM_FAULT_OOM;
 	if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
 		return VM_FAULT_OOM;
-	if (!(flags & FAULT_FLAG_WRITE) &&
+	if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
 			transparent_hugepage_use_zero_page()) {
 		spinlock_t *ptl;
 		pgtable_t pgtable;
mm/memory.c +0 -0
index 64f82aa..f275a9d 100644

mm/memory.c static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, · line 2646

Quoted 8 lines
mm/memory.c +1 -1
@@ -2640,7 +2640,7 @@ static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
 		return VM_FAULT_SIGBUS;
 
 	/* Use the zero-page for reads */
-	if (!(flags & FAULT_FLAG_WRITE)) {
+	if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm)) {
 		entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
 						vma->vm_page_prot));
 		page_table = pte_offset_map_lock(mm, pmd, address, &ptl);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer Reviewed-by on v3 3/4
#8 lore raw

Re: [PATCH 3/4] s390/mm: prevent and break zero page mappings in case of storage keys

In reply to Dominik Dingel
Quoted 68 lines · Dominik Dingel

As soon as storage keys are enabled we need to stop working on zero page mappings to prevent inconsistencies between storage keys and pgste.

Otherwise following data corruption could happen:
1) guest enables storage key
2) guest sets storage key for not mapped page X
-> change goes to PGSTE
3) guest reads from page X
-> as X was not dirty before, the page will be zero page backed,
storage key from PGSTE for X will go to storage key for zero page
4) guest sets storage key for not mapped page Y (same logic as above
5) guest reads from page Y
-> as Y was not dirty before, the page will be zero page backed,
storage key from PGSTE for Y will got to storage key for zero page overwriting storage key for X

While holding the mmap sem, we are safe against changes on entries we already fixed, as every fault would need to take the mmap_sem (read).

Other vCPUs executing storage key instructions will get a one time interception and be serialized also with mmap_sem.

---
 arch/s390/include/asm/pgtable.h |  5 +++++
 arch/s390/mm/pgtable.c          | 13 ++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
arch/s390/include/asm/pgtable.h +5 -0
index 1e991f6a..0da98d6 100644
@@ -481,6 +481,11 @@ static inline int mm_has_pgste(struct mm_struct *mm)
 	return 0;
 }
 
+/*
+ * In the case that a guest uses storage keys
+ * faults should no longer be backed by zero pages
+ */
+#define mm_forbids_zeropage mm_use_skey
 static inline int mm_use_skey(struct mm_struct *mm)
 {
 #ifdef CONFIG_PGSTE
arch/s390/mm/pgtable.c +9 -0
index ab55ba8..58d7eb2 100644
@@ -1309,6 +1309,15 @@ static int __s390_enable_skey(pte_t *pte, unsigned long addr,
 	pgste_t pgste;
 
 	pgste = pgste_get_lock(pte);
+	/*
+	 * Remove all zero page mappings,
+	 * after establishing a policy to forbid zero page mappings
+	 * following faults for that page will get fresh anonymous pages
+	 */
+	if (is_zero_pfn(pte_pfn(*pte))) {
+		ptep_flush_direct(walk->mm, addr, pte);
+		pte_val(*pte) = _PAGE_INVALID;
+	}
 	/* Clear storage key */
 	pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT |
 			      PGSTE_GR_BIT | PGSTE_GC_BIT);
@@ -1327,9 +1336,11 @@ void s390_enable_skey(void)
 	down_write(&mm->mmap_sem);
 	if (mm_use_skey(mm))

arch/s390/mm/pgtable.c void s390_enable_skey(void) · line 1346

Quoted 10 lines
arch/s390/mm/pgtable.c +3 -1
 		goto out_up;
+
+	mm->context.use_skey = 1;
+
 	walk.mm = mm;
 	walk_page_range(0, TASK_SIZE, &walk);
-	mm->context.use_skey = 1;
 
 out_up:
 	up_write(&mm->mmap_sem);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer Reviewed-by on v3 4/4
#10 lore raw

Re: [PATCH 4/4] s390/mm: disable KSM for storage key enabled pages

In reply to Dominik Dingel

(missing R-b on patch 1 is _not_ a mistake :))

Paolo

Quoted 110 lines · Dominik Dingel

When storage keys are enabled unmerge already merged pages and prevent new pages from being merged.

---
 arch/s390/include/asm/pgtable.h |  2 +-
 arch/s390/kvm/priv.c            | 17 ++++++++++++-----
 arch/s390/mm/pgtable.c          | 16 +++++++++++++++-
 3 files changed, 28 insertions(+), 7 deletions(-)
arch/s390/include/asm/pgtable.h +1 -1
index 0da98d6..dfb38af 100644
@@ -1754,7 +1754,7 @@ static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
 extern int vmem_add_mapping(unsigned long start, unsigned long size);
 extern int vmem_remove_mapping(unsigned long start, unsigned long size);
 extern int s390_enable_sie(void);
-extern void s390_enable_skey(void);
+extern int s390_enable_skey(void);
 extern void s390_reset_cmma(struct mm_struct *mm);
 
 /*
arch/s390/kvm/priv.c +12 -5
index f89c1cd..e0967fd 100644
@@ -156,21 +156,25 @@ static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
-static void __skey_check_enable(struct kvm_vcpu *vcpu)
+static int __skey_check_enable(struct kvm_vcpu *vcpu)
 {
+	int rc = 0;
 	if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
-		return;
+		return rc;
 
-	s390_enable_skey();
+	rc = s390_enable_skey();
 	trace_kvm_s390_skey_related_inst(vcpu);
 	vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
+	return rc;
 }
 
 
 static int handle_skey(struct kvm_vcpu *vcpu)
 {
-	__skey_check_enable(vcpu);
+	int rc = __skey_check_enable(vcpu);
 
+	if (rc)
+		return rc;
 	vcpu->stat.instruction_storage_key++;
 
 	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
@@ -692,7 +696,10 @@ static int handle_pfmf(struct kvm_vcpu *vcpu)
 		}
 
 		if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
-			__skey_check_enable(vcpu);
+			int rc = __skey_check_enable(vcpu);
+
+			if (rc)
+				return rc;
 			if (set_guest_storage_key(current->mm, useraddr,
 					vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
 					vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
arch/s390/mm/pgtable.c +14 -1
index 58d7eb2..82aa528 100644
@@ -18,6 +18,8 @@
 #include <linux/rcupdate.h>
 #include <linux/slab.h>
 #include <linux/swapops.h>
+#include <linux/ksm.h>
+#include <linux/mman.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -1328,22 +1330,34 @@ static int __s390_enable_skey(pte_t *pte, unsigned long addr,
 	return 0;
 }
 
-void s390_enable_skey(void)
+int s390_enable_skey(void)
 {
 	struct mm_walk walk = { .pte_entry = __s390_enable_skey };
 	struct mm_struct *mm = current->mm;
+	struct vm_area_struct *vma;
+	int rc = 0;
 
 	down_write(&mm->mmap_sem);
 	if (mm_use_skey(mm))
 		goto out_up;
 
 	mm->context.use_skey = 1;
+	for (vma = mm->mmap; vma; vma = vma->vm_next) {
+		if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
+				MADV_UNMERGEABLE, &vma->vm_flags)) {
+			mm->context.use_skey = 0;
+			rc = -ENOMEM;
+			goto out_up;
+		}
+	}
+	mm->def_flags &= ~VM_MERGEABLE;

arch/s390/mm/pgtable.c static int __s390_enable_skey(pte_t *pte, unsigned long addr, · line 1363

Quoted 10 lines
arch/s390/mm/pgtable.c +1 -0
 
 	walk.mm = mm;
 	walk_page_range(0, TASK_SIZE, &walk);
 
 out_up:
 	up_write(&mm->mmap_sem);
+	return rc;
 }
 EXPORT_SYMBOL_GPL(s390_enable_skey);
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#4 lore raw

Re: [PATCH 2/4] mm: introduce mm_forbids_zeropage function

In reply to Dominik Dingel
Quoted 9 lines · On Wed, 22 Oct 2014 13:09:28 +0200 Dominik Dingel <[email protected]>…

Add a new function stub to allow architectures to disable for an mm_structthe backing of non-present, anonymous pages with read-only empty zero pages.

...

include/linux/mm.h extern int sysctl_legacy_va_layout; · line 61

Quoted 6 lines
include/linux/mm.h +3 -0
@@ -56,6 +56,10 @@ extern int sysctl_legacy_va_layout;
 #define __pa_symbol(x)  __pa(RELOC_HIDE((unsigned long)(x), 0))
 #endif
 
+#ifndef mm_forbids_zeropage
+#define mm_forbids_zeropage(X)  (0)
+#endif

Can we document this please? What it does, why it does it. We should also specify precisely which arch header file is responsible for defining mm_forbids_zeropage.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw

Re: [PATCH 2/4] mm: introduce mm_forbids_zeropage function

Quoted 14 lines · On Wed, 22 Oct 2014 12:22:23 -0700 Andrew Morton <[email protected]>…
Quoted 12 lines · On Wed, 22 Oct 2014 13:09:28 +0200 Dominik Dingel <[email protected]>…

Add a new function stub to allow architectures to disable for an mm_structthe backing of non-present, anonymous pages with read-only empty zero pages.

...

include/linux/mm.h +0 -0
@@ -56,6 +56,10 @@ extern int sysctl_legacy_va_layout;
 #define __pa_symbol(x)  __pa(RELOC_HIDE((unsigned long)(x), 0))
 #endif
 
Quoted 8 lines

include/linux/mm.h extern int sysctl_legacy_va_layout; · line 61

Quoted 3 lines
include/linux/mm.h +3 -0
+#ifndef mm_forbids_zeropage
+#define mm_forbids_zeropage(X)  (0)
+#endif

Can we document this please? What it does, why it does it. We should also specify precisely which arch header file is responsible for defining mm_forbids_zeropage.

I will add a comment like:

/*
* To prevent common memory management code establishing
* a zero page mapping on a read fault.
* This function should be implemented within <asm/pgtable.h>.
* s390 does this to prevent multiplexing of hardware bits
* related to the physical page in case of virtualization. */

Okay?

Quoted 6 lines

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#6 lore raw

Re: [PATCH 2/4] mm: introduce mm_forbids_zeropage function

Quoted 9 lines · On Wed, 22 Oct 2014 21:45:52 +0200 Dominik Dingel <[email protected]>…
Quoted 8 lines

include/linux/mm.h extern int sysctl_legacy_va_layout; · line 61

Quoted 3 lines
include/linux/mm.h +3 -0
+#ifndef mm_forbids_zeropage
+#define mm_forbids_zeropage(X)  (0)
+#endif

Can we document this please? What it does, why it does it. We should also specify precisely which arch header file is responsible for defining mm_forbids_zeropage.

Quoted 6 lines

I will add a comment like:

/*
* To prevent common memory management code establishing
* a zero page mapping on a read fault.
* This function should be implemented within <asm/pgtable.h>.

s/function should be implemented/macro should be defined/

Quoted 5 lines

* s390 does this to prevent multiplexing of hardware bits
* related to the physical page in case of virtualization. */

Okay?

Looks great, thanks.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#12 lore raw
In reply to Dominik Dingel
Quoted 35 lines · On Wed, 22 Oct 2014 13:09:26 +0200 Dominik Dingel <[email protected]>…

s390 has the special notion of storage keys which are some sort of page flags associated with physical pages and live outside of direct addressable memory. These storage keys can be queried and changed with a special set of instructions. The mentioned instructions behave quite nicely under virtualization, if there is:
- an invalid pte, then the instructions will work on memory in the host page table
- a valid pte, then the instructions will work with the real storage key

Thanks to Martin with his software reference and dirty bit tracking, the kernel does not issue any storage key instructions as now a software based approach will be taken, on the other hand distributions in the wild are currently using them.

However, for virtualized guests we still have a problem with guest pages mapped to zero pages and the kernel same page merging.
With each one multiple guest pages will point to the same physical page and share the same storage key.

Let's fix this by introducing a new function which s390 will define to forbid new zero page mappings. If the guest issues a storage key related instruction we flag the mm_struct, drop existing zero page mappings and unmerge the guest memory.

v2 -> v3:
- Clearing up patch description Patch 3/4
- removing unnecessary flag in mmu_context (Paolo)

v1 -> v2:
- Following Dave and Paolo suggestion removing the vma flag

Dominik Dingel (4):
s390/mm: recfactor global pgste updates
mm: introduce mm_forbids_zeropage function
s390/mm: prevent and break zero page mappings in case of storage keys s390/mm: disable KSM for storage key enabled pages

Quoted 9 lines
 arch/s390/include/asm/pgalloc.h |   2 -
 arch/s390/include/asm/pgtable.h |   8 +-
 arch/s390/kvm/kvm-s390.c        |   2 +-
 arch/s390/kvm/priv.c            |  17 ++--
 arch/s390/mm/pgtable.c          | 180 ++++++++++++++++++----------------------
 include/linux/mm.h              |   4 +
 mm/huge_memory.c                |   2 +-
 mm/memory.c                     |   2 +-
 8 files changed, 106 insertions(+), 111 deletions(-)

Patches look good to me and as nobody seems to disagree with the proposed solution I will add the code to the features branch of the s390 tree.

Signature · 10 lines
blue skies,
   Martin.

"Reality continues to ruin my life." - Calvin.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>