linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Hellstrom <thellstrom@vmware.com>
To: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Cc: linux-graphics-maintainer@vmware.com,
	Thomas Hellstrom <thellstrom@vmware.com>
Subject: [PATCH RFC 1/3] mm: Add pfn_mkwrite()
Date: Tue, 19 Nov 2013 12:06:14 -0800	[thread overview]
Message-ID: <1384891576-7851-2-git-send-email-thellstrom@vmware.com> (raw)
In-Reply-To: <1384891576-7851-1-git-send-email-thellstrom@vmware.com>

A callback similar to page_mkwrite except it will be called before making
ptes that don't point to normal pages writable.

Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com>
---
 include/linux/mm.h |    9 +++++++++
 mm/memory.c        |   52 +++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 8b6e55e..23d1791 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -212,6 +212,15 @@ struct vm_operations_struct {
 	 * writable, if an error is returned it will cause a SIGBUS */
 	int (*page_mkwrite)(struct vm_area_struct *vma, struct vm_fault *vmf);
 
+	/*
+	 * Notification that a previously read-only pfn map is about to become
+	 * writable, Returning VM_FAULT_NOPAGE will cause the fault to be
+	 * retried,
+	 * Returning a VM_FAULT_SIGBUS or VM_FAULT_OOM will propagate the
+	 * error. Returning 0 will make the pfn map writable.
+	 */
+	int (*pfn_mkwrite)(struct vm_area_struct *vma, struct vm_fault *vmf);
+
 	/* called by access_process_vm when get_user_pages() fails, typically
 	 * for use by special VMAs that can switch between memory and hardware
 	 */
diff --git a/mm/memory.c b/mm/memory.c
index d176154..8ae9a6e 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2584,6 +2584,45 @@ static inline void cow_user_page(struct page *dst, struct page *src, unsigned lo
 		copy_user_highpage(dst, src, va, vma);
 }
 
+static int prepare_call_pfn_mkwrite(struct vm_area_struct *vma,
+				    unsigned long address,
+				    pte_t *pte, pmd_t *pmd,
+				    spinlock_t *ptl, pte_t orig_pte)
+{
+	int ret = 0;
+	struct vm_fault vmf;
+	struct mm_struct *mm = vma->vm_mm;
+
+	if (!vma->vm_ops || !vma->vm_ops->pfn_mkwrite)
+		return 0;
+
+	/*
+	 * In general, we can't say anything about the mapping offset
+	 * here, so set it to 0.
+	 */
+	vmf.pgoff = 0;
+	vmf.virtual_address = (void __user *)(address & PAGE_MASK);
+	vmf.flags = FAULT_FLAG_WRITE | FAULT_FLAG_MKWRITE;
+	vmf.page = NULL;
+	pte_unmap_unlock(pte, ptl);
+	ret = vma->vm_ops->pfn_mkwrite(vma, &vmf);
+	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
+		return ret;
+
+	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
+
+	/*
+	 * Retry the fault if someone updated the pte while we
+	 * dropped the lock.
+	 */
+	if (!pte_same(*pte, orig_pte)) {
+		pte_unmap_unlock(pte, ptl);
+		return VM_FAULT_NOPAGE;
+	}
+
+	return 0;
+}
+
 /*
  * This routine handles present pages, when users try to write
  * to a shared page. It is done by copying the page to a new address
@@ -2621,12 +2660,19 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
 		 * VM_MIXEDMAP !pfn_valid() case
 		 *
 		 * We should not cow pages in a shared writeable mapping.
-		 * Just mark the pages writable as we can't do any dirty
-		 * accounting on raw pfn maps.
+		 * Optionally call pfn_mkwrite to notify the address
+		 * space that the pte is about to become writeable.
 		 */
 		if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
-				     (VM_WRITE|VM_SHARED))
+		    (VM_WRITE|VM_SHARED)) {
+			ret = prepare_call_pfn_mkwrite(vma, address,
+						       page_table, pmd, ptl,
+						       orig_pte);
+			if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))
+				return ret;
+
 			goto reuse;
+		}
 		goto gotten;
 	}
 
-- 
1.7.10.4

--
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>

  reply	other threads:[~2013-11-19 20:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-19 20:06 [PATCH RFC 0/3] Add dirty-tracking infrastructure for non-page-backed address spaces Thomas Hellstrom
2013-11-19 20:06 ` Thomas Hellstrom [this message]
2013-11-19 20:06 ` [PATCH RFC 2/3] mm: Add a non-populating version of apply_to_page_range() Thomas Hellstrom
2013-11-19 20:06 ` [PATCH RFC 3/3] mm: Add mkclean_mapping_range() Thomas Hellstrom
2013-11-19 22:51 ` [PATCH RFC 0/3] Add dirty-tracking infrastructure for non-page-backed address spaces Andy Lutomirski
2013-11-20  8:12   ` Thomas Hellstrom
2013-11-20 16:50     ` Andy Lutomirski
2013-11-20 20:16       ` Thomas Hellstrom
2013-11-20 20:29         ` Andy Lutomirski

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=1384891576-7851-2-git-send-email-thellstrom@vmware.com \
    --to=thellstrom@vmware.com \
    --cc=linux-graphics-maintainer@vmware.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    /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;
as well as URLs for NNTP newsgroup(s).