From: Izik Eidus <ieidus@redhat.com>
Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
linux-mm@kvack.org, avi@redhat.com, aarcange@redhat.com,
chrisw@redhat.com, riel@redhat.com, jeremy@goop.org,
mtosatti@redhat.com, hugh@veritas.com, corbet@lwn.net,
yaniv@redhat.com, dmonakhov@openvz.org,
Izik Eidus <ieidus@redhat.com>
Subject: [PATCH 3/4] add replace_page(): change the page pte is pointing to.
Date: Tue, 31 Mar 2009 02:59:19 +0300 [thread overview]
Message-ID: <1238457560-7613-4-git-send-email-ieidus@redhat.com> (raw)
In-Reply-To: <1238457560-7613-3-git-send-email-ieidus@redhat.com>
replace_page() allow changing the mapping of pte from one physical page
into diffrent physical page.
this function is working by removing oldpage from the rmap and calling
put_page on it, and by setting the pte to point into newpage and by
inserting it to the rmap using page_add_file_rmap().
note: newpage must be non anonymous page, the reason for this is:
replace_page() is built to allow mapping one page into more than one
virtual addresses, the mapping of this page can happen in diffrent
offsets inside each vma, and therefore we cannot trust the page->index
anymore.
the side effect of this issue is that newpage cannot be anything but
kernel allocated page that is not swappable.
Signed-off-by: Izik Eidus <ieidus@redhat.com>
---
include/linux/mm.h | 5 +++
mm/memory.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 85 insertions(+), 0 deletions(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 065cdf8..b19e4c2 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1237,6 +1237,11 @@ int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
unsigned long pfn);
+#if defined(CONFIG_KSM) || defined(CONFIG_KSM_MODULE)
+int replace_page(struct vm_area_struct *vma, struct page *oldpage,
+ struct page *newpage, pte_t orig_pte, pgprot_t prot);
+#endif
+
struct page *follow_page(struct vm_area_struct *, unsigned long address,
unsigned int foll_flags);
#define FOLL_WRITE 0x01 /* check pte is writable */
diff --git a/mm/memory.c b/mm/memory.c
index 0382a34..3946e79 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1562,6 +1562,86 @@ int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
}
EXPORT_SYMBOL(vm_insert_mixed);
+#if defined(CONFIG_KSM) || defined(CONFIG_KSM_MODULE)
+
+/**
+ * replace_page - replace page in vma with new page
+ * @vma: vma that hold the pte oldpage is pointed by.
+ * @oldpage: the page we are replacing with newpage
+ * @newpage: the page we replace oldpage with
+ * @orig_pte: the original value of the pte
+ * @prot: page protection bits
+ *
+ * Returns 0 on success, -EFAULT on failure.
+ *
+ * Note: @newpage must not be an anonymous page because replace_page() does
+ * not change the mapping of @newpage to have the same values as @oldpage.
+ * @newpage can be mapped in several vmas at different offsets (page->index).
+ */
+int replace_page(struct vm_area_struct *vma, struct page *oldpage,
+ struct page *newpage, pte_t orig_pte, pgprot_t prot)
+{
+ struct mm_struct *mm = vma->vm_mm;
+ pgd_t *pgd;
+ pud_t *pud;
+ pmd_t *pmd;
+ pte_t *ptep;
+ spinlock_t *ptl;
+ unsigned long addr;
+ int ret;
+
+ BUG_ON(PageAnon(newpage));
+
+ ret = -EFAULT;
+ addr = page_address_in_vma(oldpage, vma);
+ if (addr == -EFAULT)
+ goto out;
+
+ pgd = pgd_offset(mm, addr);
+ if (!pgd_present(*pgd))
+ goto out;
+
+ pud = pud_offset(pgd, addr);
+ if (!pud_present(*pud))
+ goto out;
+
+ pmd = pmd_offset(pud, addr);
+ if (!pmd_present(*pmd))
+ goto out;
+
+ ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
+ if (!ptep)
+ goto out;
+
+ if (!pte_same(*ptep, orig_pte)) {
+ pte_unmap_unlock(ptep, ptl);
+ goto out;
+ }
+
+ ret = 0;
+ get_page(newpage);
+ page_add_file_rmap(newpage);
+
+ flush_cache_page(vma, addr, pte_pfn(*ptep));
+ ptep_clear_flush(vma, addr, ptep);
+ set_pte_at_notify(mm, addr, ptep, mk_pte(newpage, prot));
+
+ page_remove_rmap(oldpage);
+ if (PageAnon(oldpage)) {
+ dec_mm_counter(mm, anon_rss);
+ inc_mm_counter(mm, file_rss);
+ }
+ put_page(oldpage);
+
+ pte_unmap_unlock(ptep, ptl);
+
+out:
+ return ret;
+}
+EXPORT_SYMBOL_GPL(replace_page);
+
+#endif
+
/*
* maps a range of physical memory into the requested pages. the old
* mappings are removed. any references to nonexistent pages results
--
1.5.6.5
--
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>
next prev parent reply other threads:[~2009-03-31 0:00 UTC|newest]
Thread overview: 58+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-03-30 23:59 [PATCH 0/4] ksm - dynamic page sharing driver for linux Izik Eidus
2009-03-30 23:59 ` [PATCH 1/4] MMU_NOTIFIERS: add set_pte_at_notify() Izik Eidus
2009-03-30 23:59 ` [PATCH 2/4] add page_wrprotect(): write protecting page Izik Eidus
2009-03-30 23:59 ` Izik Eidus [this message]
2009-03-30 23:59 ` [PATCH 4/4] add ksm kernel shared memory driver Izik Eidus
2009-03-31 2:12 ` Anthony Liguori
2009-03-31 12:24 ` Izik Eidus
2009-03-31 13:31 ` Anthony Liguori
2009-03-31 14:25 ` Andrea Arcangeli
2009-03-31 14:37 ` Anthony Liguori
2009-03-31 15:02 ` Andrea Arcangeli
2009-03-31 15:09 ` Anthony Liguori
2009-03-31 15:18 ` Andrea Arcangeli
2009-03-31 15:54 ` Anthony Liguori
2009-03-31 16:25 ` Andrea Arcangeli
2009-03-31 16:51 ` Anthony Liguori
2009-03-31 17:11 ` Andrea Arcangeli
2009-04-01 22:54 ` Izik Eidus
2009-04-02 0:31 ` Anthony Liguori
2009-04-02 0:48 ` Chris Wright
2009-04-02 1:22 ` Chris Wright
2009-04-02 2:36 ` Anthony Liguori
2009-04-02 5:31 ` [PATCH 5/4] update ksm userspace interfaces Chris Wright
2009-04-02 13:32 ` Izik Eidus
2009-04-02 15:20 ` Chris Wright
2009-04-02 15:56 ` Chris Wright
2009-04-02 15:55 ` Izik Eidus
2009-04-03 10:16 ` Gerd Hoffmann
2009-04-03 10:49 ` Izik Eidus
2009-04-03 11:08 ` Gerd Hoffmann
2009-04-03 16:22 ` Chris Wright
2009-04-02 14:41 ` Andrea Arcangeli
2009-04-02 15:12 ` Chris Wright
2009-04-02 15:25 ` Andrea Arcangeli
2009-04-02 5:48 ` [PATCH 4/4 alternative userspace] add ksm kernel shared memory driver Chris Wright
2009-04-02 5:57 ` Bert Wesarg
2009-04-02 5:59 ` Chris Wright
2009-04-02 6:00 ` Bert Wesarg
2009-04-02 7:09 ` Avi Kivity
2009-04-02 7:24 ` [PATCH 4/4] " Avi Kivity
2009-04-02 9:38 ` Andrea Arcangeli
2009-04-02 11:23 ` Izik Eidus
2009-03-31 2:15 ` KAMEZAWA Hiroyuki
2009-03-31 12:21 ` Izik Eidus
2009-03-31 23:57 ` KAMEZAWA Hiroyuki
2009-04-01 17:28 ` Izik Eidus
2009-03-31 20:52 ` Andrea Arcangeli
2009-03-31 1:42 ` [PATCH 0/4] ksm - dynamic page sharing driver for linux Anthony Liguori
2009-03-31 12:33 ` Izik Eidus
2009-04-02 19:22 ` Jesper Juhl
2009-04-02 19:38 ` Izik Eidus
2009-04-02 19:39 ` Chris Wright
2009-04-02 19:49 ` Jesper Juhl
-- strict thread matches above, loose matches on Subject: below --
2009-04-04 14:35 [PATCH 0/4] ksm - dynamic page sharing driver for linux v2 Izik Eidus
2009-04-04 14:35 ` [PATCH 1/4] MMU_NOTIFIERS: add set_pte_at_notify() Izik Eidus
2009-04-04 14:35 ` [PATCH 2/4] add page_wrprotect(): write protecting page Izik Eidus
2009-04-04 14:35 ` [PATCH 3/4] add replace_page(): change the page pte is pointing to Izik Eidus
2009-04-09 3:58 [PATCH 0/4] ksm - dynamic page sharing driver for linux v3 Izik Eidus
2009-04-09 3:58 ` [PATCH 1/4] MMU_NOTIFIERS: add set_pte_at_notify() Izik Eidus
2009-04-09 3:58 ` [PATCH 2/4] add page_wrprotect(): write protecting page Izik Eidus
2009-04-09 3:58 ` [PATCH 3/4] add replace_page(): change the page pte is pointing to Izik Eidus
2009-04-14 22:09 ` Andrew Morton
2009-04-15 11:25 ` Andrea Arcangeli
2009-04-15 22:48 ` Izik Eidus
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=1238457560-7613-4-git-send-email-ieidus@redhat.com \
--to=ieidus@redhat.com \
--cc=aarcange@redhat.com \
--cc=avi@redhat.com \
--cc=chrisw@redhat.com \
--cc=corbet@lwn.net \
--cc=dmonakhov@openvz.org \
--cc=hugh@veritas.com \
--cc=jeremy@goop.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mtosatti@redhat.com \
--cc=riel@redhat.com \
--cc=yaniv@redhat.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;
as well as URLs for NNTP newsgroup(s).