]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/x86/mm/pageattr_32.c
x86: do not PSE on CONFIG_DEBUG_PAGEALLOC=y
[net-next-2.6.git] / arch / x86 / mm / pageattr_32.c
CommitLineData
9f4c815c
IM
1/*
2 * Copyright 2002 Andi Kleen, SuSE Labs.
1da177e4 3 * Thanks to Ben LaHaise for precious feedback.
9f4c815c 4 */
1da177e4 5
1da177e4
LT
6#include <linux/highmem.h>
7#include <linux/module.h>
9f4c815c 8#include <linux/sched.h>
1da177e4 9#include <linux/slab.h>
9f4c815c
IM
10#include <linux/mm.h>
11
1da177e4
LT
12#include <asm/processor.h>
13#include <asm/tlbflush.h>
f8af095d 14#include <asm/sections.h>
9f4c815c
IM
15#include <asm/uaccess.h>
16#include <asm/pgalloc.h>
1da177e4 17
f0646e43 18pte_t *lookup_address(unsigned long address, int *level)
9f4c815c 19{
1da177e4
LT
20 pgd_t *pgd = pgd_offset_k(address);
21 pud_t *pud;
22 pmd_t *pmd;
9f4c815c 23
1da177e4
LT
24 if (pgd_none(*pgd))
25 return NULL;
26 pud = pud_offset(pgd, address);
27 if (pud_none(*pud))
28 return NULL;
29 pmd = pmd_offset(pud, address);
30 if (pmd_none(*pmd))
31 return NULL;
f0646e43 32 *level = 2;
1da177e4
LT
33 if (pmd_large(*pmd))
34 return (pte_t *)pmd;
f0646e43 35 *level = 3;
1da177e4 36
9f4c815c
IM
37 return pte_offset_kernel(pmd, address);
38}
39
9a3dc780 40static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
9f4c815c 41{
9f4c815c 42 struct page *page;
1da177e4 43
9f4c815c
IM
44 /* change init_mm */
45 set_pte_atomic(kpte, pte);
5311ab62 46 if (SHARED_KERNEL_PMD)
1da177e4
LT
47 return;
48
1da177e4
LT
49 for (page = pgd_list; page; page = (struct page *)page->index) {
50 pgd_t *pgd;
51 pud_t *pud;
52 pmd_t *pmd;
9f4c815c 53
1da177e4
LT
54 pgd = (pgd_t *)page_address(page) + pgd_index(address);
55 pud = pud_offset(pgd, address);
56 pmd = pmd_offset(pud, address);
57 set_pte_atomic((pte_t *)pmd, pte);
58 }
1da177e4
LT
59}
60
7afe15b9 61static int split_large_page(pte_t *kpte, unsigned long address)
bb5c2dbd 62{
7afe15b9 63 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
12d6f21e 64 gfp_t gfp_flags = GFP_KERNEL;
9a3dc780 65 unsigned long flags;
bb5c2dbd
IM
66 unsigned long addr;
67 pte_t *pbase, *tmp;
68 struct page *base;
7afe15b9 69 int i, level;
bb5c2dbd 70
12d6f21e
IM
71#ifdef CONFIG_DEBUG_PAGEALLOC
72 gfp_flags = GFP_ATOMIC;
73#endif
74 base = alloc_pages(gfp_flags, 0);
bb5c2dbd
IM
75 if (!base)
76 return -ENOMEM;
77
9a3dc780 78 spin_lock_irqsave(&pgd_lock, flags);
bb5c2dbd
IM
79 /*
80 * Check for races, another CPU might have split this page
81 * up for us already:
82 */
83 tmp = lookup_address(address, &level);
5508a748
IM
84 if (tmp != kpte) {
85 WARN_ON_ONCE(1);
bb5c2dbd 86 goto out_unlock;
5508a748 87 }
bb5c2dbd
IM
88
89 address = __pa(address);
90 addr = address & LARGE_PAGE_MASK;
91 pbase = (pte_t *)page_address(base);
92 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
93
94 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
95 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
96
97 /*
98 * Install the new, split up pagetable:
99 */
9a3dc780 100 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
bb5c2dbd
IM
101 base = NULL;
102
103out_unlock:
9a3dc780 104 spin_unlock_irqrestore(&pgd_lock, flags);
bb5c2dbd
IM
105
106 if (base)
107 __free_pages(base, 0);
108
109 return 0;
110}
111
9f4c815c
IM
112static int __change_page_attr(struct page *page, pgprot_t prot)
113{
1da177e4 114 struct page *kpte_page;
9f4c815c 115 unsigned long address;
bb5c2dbd 116 int level, err = 0;
9f4c815c 117 pte_t *kpte;
1da177e4
LT
118
119 BUG_ON(PageHighMem(page));
120 address = (unsigned long)page_address(page);
121
97f99fed 122repeat:
f0646e43 123 kpte = lookup_address(address, &level);
1da177e4
LT
124 if (!kpte)
125 return -EINVAL;
9f4c815c 126
1da177e4 127 kpte_page = virt_to_page(kpte);
65d2f0bc
AK
128 BUG_ON(PageLRU(kpte_page));
129 BUG_ON(PageCompound(kpte_page));
130
1da177e4 131 /*
78c94aba
IM
132 * Better fail early if someone sets the kernel text to NX.
133 * Does not cover __inittext
1da177e4 134 */
78c94aba
IM
135 BUG_ON(address >= (unsigned long)&_text &&
136 address < (unsigned long)&_etext &&
137 (pgprot_val(prot) & _PAGE_NX));
65d2f0bc 138
78c94aba 139 if (level == 3) {
7afe15b9 140 set_pte_atomic(kpte, mk_pte(page, canon_pgprot(prot)));
78c94aba 141 } else {
7afe15b9 142 err = split_large_page(kpte, address);
bb5c2dbd
IM
143 if (!err)
144 goto repeat;
1da177e4 145 }
bb5c2dbd 146 return err;
9f4c815c 147}
1da177e4 148
1da177e4
LT
149/*
150 * Change the page attributes of an page in the linear mapping.
151 *
152 * This should be used when a page is mapped with a different caching policy
153 * than write-back somewhere - some CPUs do not like it when mappings with
154 * different caching policies exist. This changes the page attributes of the
155 * in kernel linear mapping too.
9f4c815c 156 *
1da177e4
LT
157 * The caller needs to ensure that there are no conflicting mappings elsewhere.
158 * This function only deals with the kernel linear map.
9f4c815c 159 *
1da177e4
LT
160 * Caller must call global_flush_tlb() after this.
161 */
162int change_page_attr(struct page *page, int numpages, pgprot_t prot)
163{
9f4c815c 164 int err = 0, i;
1da177e4 165
9f4c815c 166 for (i = 0; i < numpages; i++, page++) {
1da177e4 167 err = __change_page_attr(page, prot);
9f4c815c
IM
168 if (err)
169 break;
170 }
9f4c815c 171
1da177e4
LT
172 return err;
173}
9f4c815c 174EXPORT_SYMBOL(change_page_attr);
1da177e4 175
78c94aba 176int change_page_attr_addr(unsigned long addr, int numpages, pgprot_t prot)
626ab0e6 177{
78c94aba 178 int i;
5508a748 179 unsigned long pfn = (__pa(addr) >> PAGE_SHIFT);
1da177e4 180
78c94aba
IM
181 for (i = 0; i < numpages; i++) {
182 if (!pfn_valid(pfn + i)) {
5508a748 183 WARN_ON_ONCE(1);
78c94aba
IM
184 break;
185 } else {
186 int level;
187 pte_t *pte = lookup_address(addr + i*PAGE_SIZE, &level);
5508a748 188 BUG_ON(pte && pte_none(*pte));
78c94aba
IM
189 }
190 }
5508a748 191
78c94aba
IM
192 return change_page_attr(virt_to_page(addr), i, prot);
193}
194
195static void flush_kernel_map(void *arg)
196{
197 /*
198 * Flush all to work around Errata in early athlons regarding
199 * large page flushing.
200 */
201 __flush_tlb_all();
202
203 if (boot_cpu_data.x86_model >= 4)
204 wbinvd();
205}
206
207void global_flush_tlb(void)
208{
1da177e4
LT
209 BUG_ON(irqs_disabled());
210
78c94aba 211 on_each_cpu(flush_kernel_map, NULL, 1, 1);
626ab0e6 212}
9f4c815c 213EXPORT_SYMBOL(global_flush_tlb);
1da177e4
LT
214
215#ifdef CONFIG_DEBUG_PAGEALLOC
216void kernel_map_pages(struct page *page, int numpages, int enable)
217{
218 if (PageHighMem(page))
219 return;
9f4c815c 220 if (!enable) {
f9b8404c
IM
221 debug_check_no_locks_freed(page_address(page),
222 numpages * PAGE_SIZE);
9f4c815c 223 }
de5097c2 224
12d6f21e
IM
225 /*
226 * If page allocator is not up yet then do not call c_p_a():
227 */
228 if (!debug_pagealloc_enabled)
229 return;
230
9f4c815c
IM
231 /*
232 * the return value is ignored - the calls cannot fail,
1da177e4
LT
233 * large pages are disabled at boot time.
234 */
235 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
9f4c815c
IM
236
237 /*
238 * we should perform an IPI and flush all tlbs,
1da177e4
LT
239 * but that can deadlock->flush only current cpu.
240 */
241 __flush_tlb_all();
242}
243#endif