mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andi Kleen <ak@suse.de>
To: linux-kernel@vger.kernel.org, jbeulich@novell.com, mingo@elte.hu,
	tglx@linutronix.de
Subject: [PATCH] [26/31] CPA: Fix reference counting when changing already changed pages
Date: Mon, 14 Jan 2008 23:16:59 +0100 (CET)	[thread overview]
Message-ID: <20080114221659.63C7514F83@wotan.suse.de> (raw)
In-Reply-To: <200801141116.534682000@suse.de>


When changing a page that has already been modified to non standard attributes
before don't change the reference count. And when changing back a page
only decrease the ref count if the old attributes were non standard.

Signed-off-by: Andi Kleen <ak@suse.de>

---
 arch/x86/mm/pageattr_32.c |   44 +++++++++++++++++++++++++-------------------
 arch/x86/mm/pageattr_64.c |   16 ++++++++++++----
 include/asm-x86/pgtable.h |    2 ++
 3 files changed, 39 insertions(+), 23 deletions(-)

Index: linux/arch/x86/mm/pageattr_64.c
===================================================================
--- linux.orig/arch/x86/mm/pageattr_64.c
+++ linux/arch/x86/mm/pageattr_64.c
@@ -206,20 +206,26 @@ __change_page_attr(unsigned long address
 { 
 	pte_t *kpte; 
 	struct page *kpte_page;
-	pgprot_t ref_prot2;
+	pgprot_t ref_prot2, oldprot;
 	int level;
 
 	kpte = lookup_address(address, &level);
 	if (!kpte) return 0;
 	kpte_page = virt_to_page(kpte);
+	oldprot = pte_pgprot(*kpte);
 	BUG_ON(PageCompound(kpte_page));
 	BUG_ON(PageLRU(kpte_page));
 
 	set_tlb_flush(address, cache_attr_changed(*kpte, prot, level),
 			level < 4);
 
+	ref_prot = canon_pgprot(ref_prot);
+	prot = canon_pgprot(prot);
+
 	if (pgprot_val(prot) != pgprot_val(ref_prot)) { 
 		if (level == 4) {
+			if (pgprot_val(oldprot) == pgprot_val(ref_prot))
+				page_private(kpte_page)++;
 			set_pte(kpte, pfn_pte(pfn, prot));
 		} else {
 			/*
@@ -234,12 +240,14 @@ __change_page_attr(unsigned long address
 			pgprot_val(ref_prot2) &= ~_PAGE_NX;
 			set_pte(kpte, mk_pte(split, ref_prot2));
 			kpte_page = split;
+			page_private(kpte_page)++;
 		}
-		page_private(kpte_page)++;
 	} else if (level == 4) {
+		if (pgprot_val(oldprot) != pgprot_val(ref_prot)) {
+			BUG_ON(page_private(kpte_page) <= 0);
+			page_private(kpte_page)--;
+		}
 		set_pte(kpte, pfn_pte(pfn, ref_prot));
-		BUG_ON(page_private(kpte_page) == 0);
-		page_private(kpte_page)--;
 	} else {
 		/*
 		 * When you're here you either set the same page to PAGE_KERNEL
Index: linux/arch/x86/mm/pageattr_32.c
===================================================================
--- linux.orig/arch/x86/mm/pageattr_32.c
+++ linux/arch/x86/mm/pageattr_32.c
@@ -151,20 +151,17 @@ static void set_pmd_pte(pte_t *kpte, uns
  * No more special protections in this 2/4MB area - revert to a
  * large page again. 
  */
-static inline void revert_page(struct page *kpte_page, unsigned long address)
+static void
+revert_page(struct page *kpte_page, unsigned long address, pgprot_t ref_prot)
 {
-	pgprot_t ref_prot;
 	pte_t *linear;
 
-	ref_prot =
-	((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
-		? PAGE_KERNEL_LARGE_EXEC : PAGE_KERNEL_LARGE;
-
 	linear = (pte_t *)
 		pmd_offset(pud_offset(pgd_offset_k(address), address), address);
 	set_pmd_pte(linear,  address,
-		    pfn_pte((__pa(address) & LARGE_PAGE_MASK) >> PAGE_SHIFT,
-			    ref_prot));
+		    pte_mkhuge(pfn_pte((__pa(address) & LARGE_PAGE_MASK)
+					>> PAGE_SHIFT,
+			    ref_prot)));
 }
 
 static inline void save_page(struct page *kpte_page)
@@ -223,6 +220,8 @@ __change_page_attr(struct page *page, pg
 	unsigned long address;
 	struct page *kpte_page;
 	int level;
+	pgprot_t oldprot;
+	pgprot_t ref_prot = PAGE_KERNEL;
 
 	BUG_ON(PageHighMem(page));
 	address = (unsigned long)page_address(page);
@@ -230,6 +229,8 @@ __change_page_attr(struct page *page, pg
 	kpte = lookup_address(address, &level);
 	if (!kpte)
 		return -EINVAL;
+
+	oldprot = pte_pgprot(*kpte);
 	kpte_page = virt_to_page(kpte);
 	BUG_ON(PageLRU(kpte_page));
 	BUG_ON(PageCompound(kpte_page));
@@ -237,27 +238,32 @@ __change_page_attr(struct page *page, pg
 	set_tlb_flush(address, cache_attr_changed(*kpte, prot, level),
 			level < 3);
 
-	if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) { 
+	if ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
+		ref_prot = PAGE_KERNEL_EXEC;
+
+	ref_prot = canon_pgprot(ref_prot);
+	prot = canon_pgprot(prot);
+
+	if (pgprot_val(prot) != pgprot_val(ref_prot)) {
 		if (level == 3) {
+			if (pgprot_val(oldprot) == pgprot_val(ref_prot))
+				page_private(kpte_page)++;
 			set_pte_atomic(kpte, mk_pte(page, prot)); 
 		} else {
-			pgprot_t ref_prot;
 			struct page *split;
-
-			ref_prot =
-			((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
-				? PAGE_KERNEL_EXEC : PAGE_KERNEL;
 			split = split_large_page(address, prot, ref_prot);
 			if (!split)
 				return -ENOMEM;
 			set_pmd_pte(kpte,address,mk_pte(split, ref_prot));
 			kpte_page = split;
+			page_private(kpte_page)++;
 		}
-		page_private(kpte_page)++;
 	} else if (level == 3) {
-		set_pte_atomic(kpte, mk_pte(page, PAGE_KERNEL));
-		BUG_ON(page_private(kpte_page) == 0);
-		page_private(kpte_page)--;
+		if (pgprot_val(oldprot) != pgprot_val(ref_prot)) {
+			BUG_ON(page_private(kpte_page) <= 0);
+			page_private(kpte_page)--;
+		}
+		set_pte_atomic(kpte, mk_pte(page, ref_prot));
 	} else {
 		/*
 		 * When you're here you either set the same page to PAGE_KERNEL
@@ -279,7 +285,7 @@ __change_page_attr(struct page *page, pg
 		if (cpu_has_pse && (page_private(kpte_page) == 0)) {
 			save_page(kpte_page);
 			paravirt_release_pt(page_to_pfn(kpte_page));
-			revert_page(kpte_page, address);
+			revert_page(kpte_page, address, ref_prot);
 		}
 	}
 	return 0;
Index: linux/include/asm-x86/pgtable.h
===================================================================
--- linux.orig/include/asm-x86/pgtable.h
+++ linux/include/asm-x86/pgtable.h
@@ -192,6 +192,8 @@ static inline pte_t pte_modify(pte_t pte
 	return __pte(val);
 }
 
+#define canon_pgprot(p) __pgprot(pgprot_val(p) & __supported_pte_mask)
+
 #ifdef CONFIG_PARAVIRT
 #include <asm/paravirt.h>
 #else  /* !CONFIG_PARAVIRT */

  parent reply	other threads:[~2008-01-14 22:26 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-14 22:16 [PATCH] [0/31] Great change_page_attr patch series v2 Andi Kleen
2008-01-14 22:16 ` [PATCH] [1/31] Shrink __PAGE_KERNEL/__PAGE_KERNEL_EXEC on non PAE kernels Andi Kleen
2008-01-14 22:16 ` [PATCH] [2/31] CPA: Do a simple self test at boot Andi Kleen
2008-01-15  8:47   ` Harvey Harrison
2008-01-15  9:59     ` Andi Kleen
2008-01-15 10:07       ` Harvey Harrison
2008-01-14 22:16 ` [PATCH] [3/31] Add pte accessors for the global bit Andi Kleen
2008-01-14 22:16 ` [PATCH] [4/31] Add pte_pgprot on i386 Andi Kleen
2008-01-15 13:00   ` Johannes Weiner
2008-01-14 22:16 ` [PATCH] [5/31] Don't drop NX bit in pte modifier functions for 32bit Andi Kleen
2008-01-14 22:16 ` [PATCH] [6/31] CPA: Undo white space changes Andi Kleen
2008-01-14 22:16 ` [PATCH] [7/31] Extract page table dumping code from i386 fault handler into dump_pagetable() Andi Kleen
2008-01-15  8:56   ` Harvey Harrison
2008-01-15 10:00     ` Andi Kleen
2008-01-15 10:05       ` Harvey Harrison
2008-01-14 22:16 ` [PATCH] [8/31] CPA: Return the page table level in lookup_address() Andi Kleen
2008-01-14 22:16 ` [PATCH] [9/31] CPA: Add simple self test at boot Andi Kleen
2008-01-15 10:37   ` Harvey Harrison
2008-01-14 22:16 ` [PATCH] [10/31] CPA: Change kernel_map_pages to not use c_p_a() Andi Kleen
2008-01-14 22:16 ` [PATCH] [11/31] CPA: Change 32bit back to init_mm semaphore locking Andi Kleen
2008-01-14 22:16 ` [PATCH] [12/31] CPA: CLFLUSH support in change_page_attr() Andi Kleen
2008-01-15  8:40   ` Jan Beulich
2008-01-15  9:57     ` Andi Kleen
2008-01-14 22:16 ` [PATCH] [13/31] CPA: Use macros to modify the PG_arch_1 page flags in change_page_attr Andi Kleen
2008-01-15  9:29   ` Harvey Harrison
2008-01-15 10:06     ` Andi Kleen
2008-01-15 10:15       ` Harvey Harrison
2008-01-15 10:25         ` Andi Kleen
2008-01-14 22:16 ` [PATCH] [14/31] CPA: Use page granuality TLB flushing " Andi Kleen
2008-01-14 22:16 ` [PATCH] [15/31] CPA: Don't flush the caches when the CPU supports self-snoop Andi Kleen
2008-01-14 22:16 ` [PATCH] [16/31] CPA: Use wbinvd() macro instead of inline assembly in 64bit c_p_a() Andi Kleen
2008-01-14 22:16 ` [PATCH] [17/31] CPA: Reorder TLB / cache flushes to follow Intel recommendation Andi Kleen
2008-01-14 22:16 ` [PATCH] [18/31] CPA: Make change_page_attr() more robust against use of PAT bits Andi Kleen
2008-01-14 22:16 ` [PATCH] [19/31] CPA: Limit cache flushing to pages that really change caching Andi Kleen
2008-01-15  8:46   ` Jan Beulich
2008-01-14 22:16 ` [PATCH] [20/31] CPA: Fix inaccurate comments in 64bit change_page_attr() Andi Kleen
2008-01-14 22:16 ` [PATCH] [21/31] CPA: Dump pagetable when inconsistency is detected Andi Kleen
2008-01-14 22:16 ` [PATCH] [22/31] CPA: Only queue actually unused page table pages for freeing Andi Kleen
2008-01-14 22:16 ` [PATCH] [23/31] CPA: Remove unnecessary masking of address Andi Kleen
2008-01-14 22:16 ` [PATCH] [24/31] CPA: Only unmap kernel init pages in text mapping when CONFIG_DEBUG_RODATA is set Andi Kleen
2008-01-14 22:16 ` [PATCH] [25/31] CPA: Always do full TLB flush when splitting large pages Andi Kleen
2008-01-14 22:16 ` Andi Kleen [this message]
2008-01-15  9:05   ` [PATCH] [26/31] CPA: Fix reference counting when changing already changed pages Jan Beulich
2008-01-15 10:04     ` Andi Kleen
2008-01-15 12:00       ` Jan Beulich
2008-01-14 22:17 ` [PATCH] [27/31] CPA: Change comments of external interfaces to kerneldoc format Andi Kleen
2008-01-14 22:50   ` Randy Dunlap
2008-01-15  0:49     ` Andi Kleen
2008-01-14 22:17 ` [PATCH] [28/31] CPA: Make kernel_text test match boot mapping initialization Andi Kleen
2008-01-14 22:17 ` [PATCH] [29/31] CPA: Add a BUG_ON checking for someone setting the kernel text NX Andi Kleen
2008-01-14 22:17 ` [PATCH] [30/31] Remove set_kernel_exec Andi Kleen
2008-01-14 22:17 ` [PATCH] [31/31] Clean up pte_exec Andi Kleen
2008-01-15  9:11 ` [PATCH] [0/31] Great change_page_attr patch series v2 Jan Beulich
2008-01-15 10:06   ` Andi Kleen
2008-01-15 11:55     ` Jan Beulich
2008-01-15 12:43       ` Andi Kleen

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=20080114221659.63C7514F83@wotan.suse.de \
    --to=ak@suse.de \
    --cc=jbeulich@novell.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=tglx@linutronix.de \
    /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

all inboxes | Powered by JetHome®