linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.cz>
To: linux-mm@kvack.org
Cc: linux-fsdevel@vger.kernel.org, linux-nvdimm@lists.01.org,
	Dan Williams <dan.j.williams@intel.com>,
	Ross Zwisler <ross.zwisler@linux.intel.com>,
	Jan Kara <jack@suse.cz>
Subject: [PATCH 05/15] mm: Factor out functionality to finish page faults
Date: Fri, 22 Jul 2016 14:19:31 +0200	[thread overview]
Message-ID: <1469189981-19000-6-git-send-email-jack@suse.cz> (raw)
In-Reply-To: <1469189981-19000-1-git-send-email-jack@suse.cz>

Introduce function finish_fault() which handles locking of page tables
and insertion of PTE after page for the page fault is prepared. This
will be somewhat easier to use from page fault handlers than current
do_set_pte() which is unnecessarily low-level for most uses.

Signed-off-by: Jan Kara <jack@suse.cz>
---
 include/linux/mm.h |  1 +
 mm/memory.c        | 67 ++++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 48 insertions(+), 20 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 2442f972bdc8..21226cc2b1cd 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -606,6 +606,7 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
 
 void do_set_pte(struct vm_area_struct *vma, unsigned long address,
 		struct page *page, pte_t *pte, bool write, bool anon);
+int finish_fault(struct vm_area_struct *vma, struct vm_fault *vmf);
 #endif
 
 /*
diff --git a/mm/memory.c b/mm/memory.c
index aef88d634072..b785f823caa4 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2890,6 +2890,49 @@ void do_set_pte(struct vm_area_struct *vma, unsigned long address,
 	update_mmu_cache(vma, address, pte);
 }
 
+/**
+ * finish_fault - finish page fault once we have prepared the page to fault
+ *
+ * @vma: virtual memory area
+ * @vmf: structure describing the fault
+ *
+ * This function handles all that is needed to finish a page fault once the
+ * page to fault in is prepared. It handles locking of PTEs, inserts PTE for
+ * given page, adds reverse page mapping, handles memcg charges and LRU
+ * addition. The function returns 0 on success, error in case page could not
+ * be inserted into page tables.
+ *
+ * The function expects the page to be locked.
+ */
+int finish_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
+{
+	unsigned long address = (unsigned long)vmf->virtual_address;
+	struct page *page = vmf->page;
+	bool anon = false;
+	spinlock_t *ptl;
+	pte_t *pte;
+
+	if (vmf->cow_page) {
+		page = vmf->cow_page;
+		anon = true;
+	}
+
+	pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, address, &ptl);
+	if (unlikely(!pte_same(*pte, vmf->orig_pte))) {
+		pte_unmap_unlock(pte, ptl);
+		return -EBUSY;
+	}
+	do_set_pte(vma, address, page, pte, vmf->flags & FAULT_FLAG_WRITE,
+		   anon);
+	if (anon) {
+		mem_cgroup_commit_charge(page, vmf->memcg, false, false);
+		lru_cache_add_active_or_unevictable(page, vma);
+	}
+	pte_unmap_unlock(pte, ptl);
+
+	return 0;
+}
+
 static unsigned long fault_around_bytes __read_mostly =
 	rounddown_pow_of_two(65536);
 
@@ -3022,15 +3065,13 @@ static int do_read_fault(struct mm_struct *mm, struct vm_area_struct *vma,
 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
 		return ret;
 
-	pte = pte_offset_map_lock(mm, vmf->pmd, address, &ptl);
-	if (unlikely(!pte_same(*pte, vmf->orig_pte))) {
-		pte_unmap_unlock(pte, ptl);
+	if (unlikely(finish_fault(vma, vmf) < 0)) {
 		unlock_page(vmf->page);
 		put_page(vmf->page);
 		return ret;
 	}
-	do_set_pte(vma, address, vmf->page, pte, false, false);
 	unlock_page(vmf->page);
+	return ret;
 unlock_out:
 	pte_unmap_unlock(pte, ptl);
 	return ret;
@@ -3041,8 +3082,6 @@ static int do_cow_fault(struct mm_struct *mm, struct vm_area_struct *vma,
 {
 	struct page *new_page;
 	struct mem_cgroup *memcg;
-	spinlock_t *ptl;
-	pte_t *pte;
 	int ret;
 	unsigned long address = (unsigned long)vmf->virtual_address;
 
@@ -3070,9 +3109,7 @@ static int do_cow_fault(struct mm_struct *mm, struct vm_area_struct *vma,
 		copy_user_highpage(new_page, vmf->page, address, vma);
 	__SetPageUptodate(new_page);
 
-	pte = pte_offset_map_lock(mm, vmf->pmd, address, &ptl);
-	if (unlikely(!pte_same(*pte, vmf->orig_pte))) {
-		pte_unmap_unlock(pte, ptl);
+	if (unlikely(finish_fault(vma, vmf) < 0)) {
 		if (!(ret & VM_FAULT_DAX_LOCKED)) {
 			unlock_page(vmf->page);
 			put_page(vmf->page);
@@ -3082,10 +3119,6 @@ static int do_cow_fault(struct mm_struct *mm, struct vm_area_struct *vma,
 		}
 		goto uncharge_out;
 	}
-	do_set_pte(vma, address, new_page, pte, true, true);
-	mem_cgroup_commit_charge(new_page, memcg, false, false);
-	lru_cache_add_active_or_unevictable(new_page, vma);
-	pte_unmap_unlock(pte, ptl);
 	if (!(ret & VM_FAULT_DAX_LOCKED)) {
 		unlock_page(vmf->page);
 		put_page(vmf->page);
@@ -3104,8 +3137,6 @@ static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma,
 {
 	struct address_space *mapping;
 	unsigned long address = (unsigned long)vmf->virtual_address;
-	spinlock_t *ptl;
-	pte_t *pte;
 	int dirtied = 0;
 	int ret, tmp;
 
@@ -3128,15 +3159,11 @@ static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma,
 		}
 	}
 
-	pte = pte_offset_map_lock(mm, vmf->pmd, address, &ptl);
-	if (unlikely(!pte_same(*pte, vmf->orig_pte))) {
-		pte_unmap_unlock(pte, ptl);
+	if (unlikely(finish_fault(vma, vmf) < 0)) {
 		unlock_page(vmf->page);
 		put_page(vmf->page);
 		return ret;
 	}
-	do_set_pte(vma, address, vmf->page, pte, true, false);
-	pte_unmap_unlock(pte, ptl);
 
 	if (set_page_dirty(vmf->page))
 		dirtied = 1;
-- 
2.6.6

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

  parent reply	other threads:[~2016-07-22 12:20 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-22 12:19 [PATCH 0/15 v2] dax: Clear dirty bits after flushing caches Jan Kara
2016-07-22 12:19 ` [PATCH 01/15] mm: Create vm_fault structure earlier Jan Kara
2016-07-22 12:19 ` [PATCH 02/15] mm: Propagate original vm_fault into do_fault_around() Jan Kara
2016-07-22 12:19 ` [PATCH 03/15] mm: Add pmd and orig_pte fields to vm_fault Jan Kara
2016-07-22 12:19 ` [PATCH 04/15] mm: Allow full handling of COW faults in ->fault handlers Jan Kara
2016-07-22 12:19 ` Jan Kara [this message]
2016-07-22 12:19 ` [PATCH 06/15] mm: Move handling of COW faults into DAX code Jan Kara
2016-07-22 12:19 ` [PATCH 07/15] dax: Make cache flushing protected by entry lock Jan Kara
2016-07-22 12:19 ` [PATCH 08/15] mm: Export follow_pte() Jan Kara
2016-07-22 12:19 ` [PATCH 09/15] mm: Remove unnecessary vma->vm_ops check Jan Kara
2016-07-22 12:19 ` [PATCH 10/15] mm: Factor out common parts of write fault handling Jan Kara
2016-07-22 12:19 ` [PATCH 11/15] mm: Move part of wp_page_reuse() into the single call site Jan Kara
2016-07-22 12:19 ` [PATCH 12/15] mm: Lift vm_fault structure creation from do_page_mkwrite() Jan Kara
2016-07-22 12:19 ` [PATCH 13/15] mm: Provide helper for finishing mkwrite faults Jan Kara
2016-08-09 14:50   ` [lkp] [mm] 0c649028cd: vm-scalability.throughput 343.9% improvement kernel test robot
2016-07-22 12:19 ` [PATCH 14/15] dax: Protect PTE modification on WP fault by radix tree entry lock Jan Kara
2016-07-25 21:30   ` Ross Zwisler
2016-07-26 14:09     ` Jan Kara
2016-07-22 12:19 ` [PATCH 15/15] dax: Clear dirty entry tags on cache flush Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1469189981-19000-6-git-send-email-jack@suse.cz \
    --to=jack@suse.cz \
    --cc=dan.j.williams@intel.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-nvdimm@lists.01.org \
    --cc=ross.zwisler@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox