All of lore.kernel.org
 help / color / mirror / Atom feed
* [Xenomai] [PATCH 1/2] ipipe: Rework and simplify __ipipe_pin_vma
@ 2013-01-07 18:13 Jan Kiszka
  2013-01-07 18:14 ` [Xenomai] [PATCH 2/2] ipipe: Fault in locked vmas after changing the protection flags Jan Kiszka
  0 siblings, 1 reply; 2+ messages in thread
From: Jan Kiszka @ 2013-01-07 18:13 UTC (permalink / raw)
  To: Xenomai

Implements __ipipe_pin_vma after mlock_vma_pages_range, just properly
returning errors so that __ipipe_disable_ondemand_mappings can evaluate
them. This not only simplifies the code, it also ensures that we fault
in pages under not yet existing page directory nodes. That is important
when performing access-enabling mprotect on a locked memory region of a
real-time process.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 mm/memory.c |   79 -----------------------------------------------------------
 mm/mlock.c  |   18 +++++++++++++
 2 files changed, 18 insertions(+), 79 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index ab93d65..4230192 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4075,85 +4075,6 @@ void copy_user_huge_page(struct page *dst, struct page *src,
 
 #ifdef CONFIG_IPIPE
 
-static inline int ipipe_pin_pte_range(struct mm_struct *mm, pmd_t *pmd,
-				      struct vm_area_struct *vma,
-				      unsigned long addr, unsigned long end)
-{
-	spinlock_t *ptl;
-	pte_t *pte;
-
-	do {
-		pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
-		if (!pte)
-			continue;
-
-		if (!pte_present(*pte) || pte_write(*pte)) {
-			pte_unmap_unlock(pte, ptl);
-			continue;
-		}
-
-		if (do_wp_page(mm, vma, addr, pte, pmd, ptl, *pte) == VM_FAULT_OOM)
-			return -ENOMEM;
-	} while (addr += PAGE_SIZE, addr != end);
-	return 0;
-}
-
-static inline int ipipe_pin_pmd_range(struct mm_struct *mm, pud_t *pud,
-				      struct vm_area_struct *vma,
-				      unsigned long addr, unsigned long end)
-{
-	unsigned long next;
-	pmd_t *pmd;
-
-	pmd = pmd_offset(pud, addr);
-	do {
-		next = pmd_addr_end(addr, end);
-		if (pmd_none_or_clear_bad(pmd))
-			continue;
-		if (ipipe_pin_pte_range(mm, pmd, vma, addr, next))
-			return -ENOMEM;
-	} while (pmd++, addr = next, addr != end);
-	return 0;
-}
-
-static inline int ipipe_pin_pud_range(struct mm_struct *mm, pgd_t *pgd,
-				      struct vm_area_struct *vma,
-				      unsigned long addr, unsigned long end)
-{
-	unsigned long next;
-	pud_t *pud;
-
-	pud = pud_offset(pgd, addr);
-	do {
-		next = pud_addr_end(addr, end);
-		if (pud_none_or_clear_bad(pud))
-			continue;
-		if (ipipe_pin_pmd_range(mm, pud, vma, addr, next))
-			return -ENOMEM;
-	} while (pud++, addr = next, addr != end);
-	return 0;
-}
-
-int __ipipe_pin_vma(struct mm_struct *mm, struct vm_area_struct *vma)
-{
-	unsigned long addr, next, end;
-	pgd_t *pgd;
-
-	addr = vma->vm_start;
-	end = vma->vm_end;
-
-	pgd = pgd_offset(mm, addr);
-	do {
-		next = pgd_addr_end(addr, end);
-		if (pgd_none_or_clear_bad(pgd))
-			continue;
-		if (ipipe_pin_pud_range(mm, pgd, vma, addr, next))
-			return -ENOMEM;
-	} while (pgd++, addr = next, addr != end);
-
-	return 0;
-}
-
 int __ipipe_disable_ondemand_mappings(struct task_struct *tsk)
 {
 	struct vm_area_struct *vma;
diff --git a/mm/mlock.c b/mm/mlock.c
index ef726e8..158828b 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -624,3 +624,21 @@ void user_shm_unlock(size_t size, struct user_struct *user)
 	spin_unlock(&shmlock_user_lock);
 	free_uid(user);
 }
+
+#ifdef CONFIG_IPIPE
+int __ipipe_pin_vma(struct mm_struct *mm, struct vm_area_struct *vma)
+{
+	int ret;
+
+	if (vma->vm_flags & (VM_IO | VM_PFNMAP))
+		return 0;
+
+	if (!((vma->vm_flags & (VM_DONTEXPAND | VM_RESERVED)) ||
+	    is_vm_hugetlb_page(vma) || vma == get_gate_vma(mm))) {
+		ret = __mlock_vma_pages_range(vma, vma->vm_start, vma->vm_end,
+					      NULL);
+		return (ret < 0) ? ret : 0;
+	} else
+		return make_pages_present(vma->vm_start, vma->vm_end);
+}
+#endif
-- 
1.7.3.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [Xenomai] [PATCH 2/2] ipipe: Fault in locked vmas after changing the protection flags
  2013-01-07 18:13 [Xenomai] [PATCH 1/2] ipipe: Rework and simplify __ipipe_pin_vma Jan Kiszka
@ 2013-01-07 18:14 ` Jan Kiszka
  0 siblings, 0 replies; 2+ messages in thread
From: Jan Kiszka @ 2013-01-07 18:14 UTC (permalink / raw)
  To: Xenomai

__ipipe_pin_vma now practically removes the risk that vmas using COW are
not fully populated before we patch the protection flags, thus can take
faults later on and use that patched value on the zero page. Still, it
turned out to be safer and simpler to fault in the pages after the
protection change without touching the protection flags.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 mm/mprotect.c |   36 +++++++-----------------------------
 1 files changed, 7 insertions(+), 29 deletions(-)

diff --git a/mm/mprotect.c b/mm/mprotect.c
index 195b91e..057b1d8 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -147,7 +147,6 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
 {
 	struct mm_struct *mm = vma->vm_mm;
 	unsigned long oldflags = vma->vm_flags;
-	unsigned long protflags;
 	long nrpages = (end - start) >> PAGE_SHIFT;
 	unsigned long charged = 0;
 	pgoff_t pgoff;
@@ -206,17 +205,8 @@ success:
 	 * held in write mode.
 	 */
 	vma->vm_flags = newflags;
-	protflags = newflags;
-#ifdef CONFIG_IPIPE
-	/*
-	 * Enforce non-COW vm_page_prot by faking VM_SHARED on locked regions.
-	 */
-	if (test_bit(MMF_VM_PINNED, &mm->flags) &&
-	    ((vma->vm_flags | mm->def_flags) & VM_LOCKED))
-		protflags |= VM_SHARED;
-#endif
 	vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
-					  vm_get_page_prot(protflags));
+					  vm_get_page_prot(newflags));
 
 	if (vma_wants_writenotify(vma)) {
 		vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
@@ -224,28 +214,16 @@ success:
 	}
 
 	mmu_notifier_invalidate_range_start(mm, start, end);
-#ifdef CONFIG_IPIPE
-	/*
-	 * Privatize potential COW pages
-	 */
-	if (test_bit(MMF_VM_PINNED, &mm->flags) &&
-	    (((vma->vm_flags | mm->def_flags) & (VM_LOCKED | VM_WRITE)) ==
-	     (VM_LOCKED | VM_WRITE))) {
-		error = __ipipe_pin_vma(mm, vma);
-		if (error)
-			/*
-			 * OOM. Just revert the fake VM_SHARED so that the
-			 * zero page cannot be overwritten.
-			 */
-			vma->vm_page_prot =
-				pgprot_modify(vma->vm_page_prot,
-					      vm_get_page_prot(newflags));
-	}
-#endif
 	if (is_vm_hugetlb_page(vma))
 		hugetlb_change_protection(vma, start, end, vma->vm_page_prot);
 	else
 		change_protection(vma, start, end, vma->vm_page_prot, dirty_accountable);
+#ifdef CONFIG_IPIPE
+	if (test_bit(MMF_VM_PINNED, &mm->flags) &&
+	    ((vma->vm_flags | mm->def_flags) & VM_LOCKED) &&
+	    (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
+		__ipipe_pin_vma(mm, vma);
+#endif
 	mmu_notifier_invalidate_range_end(mm, start, end);
 	vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
 	vm_stat_account(mm, newflags, vma->vm_file, nrpages);
-- 
1.7.3.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-01-07 18:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-07 18:13 [Xenomai] [PATCH 1/2] ipipe: Rework and simplify __ipipe_pin_vma Jan Kiszka
2013-01-07 18:14 ` [Xenomai] [PATCH 2/2] ipipe: Fault in locked vmas after changing the protection flags Jan Kiszka

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.