[PATCH] x86_64: pageattr use single list
[deliverable/linux.git] / arch / x86_64 / mm / pageattr.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright 2002 Andi Kleen, SuSE Labs.
3 * Thanks to Ben LaHaise for precious feedback.
4 */
5
6#include <linux/config.h>
7#include <linux/mm.h>
8#include <linux/sched.h>
9#include <linux/highmem.h>
10#include <linux/module.h>
11#include <linux/slab.h>
12#include <asm/uaccess.h>
13#include <asm/processor.h>
14#include <asm/tlbflush.h>
15#include <asm/io.h>
16
17static inline pte_t *lookup_address(unsigned long address)
18{
19 pgd_t *pgd = pgd_offset_k(address);
20 pud_t *pud;
21 pmd_t *pmd;
22 pte_t *pte;
23 if (pgd_none(*pgd))
24 return NULL;
25 pud = pud_offset(pgd, address);
26 if (!pud_present(*pud))
27 return NULL;
28 pmd = pmd_offset(pud, address);
29 if (!pmd_present(*pmd))
30 return NULL;
31 if (pmd_large(*pmd))
32 return (pte_t *)pmd;
33 pte = pte_offset_kernel(pmd, address);
34 if (pte && !pte_present(*pte))
35 pte = NULL;
36 return pte;
37}
38
39static struct page *split_large_page(unsigned long address, pgprot_t prot,
40 pgprot_t ref_prot)
41{
42 int i;
43 unsigned long addr;
44 struct page *base = alloc_pages(GFP_KERNEL, 0);
45 pte_t *pbase;
46 if (!base)
47 return NULL;
48 address = __pa(address);
49 addr = address & LARGE_PAGE_MASK;
50 pbase = (pte_t *)page_address(base);
51 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) {
52 pbase[i] = pfn_pte(addr >> PAGE_SHIFT,
53 addr == address ? prot : ref_prot);
54 }
55 return base;
56}
57
58
59static void flush_kernel_map(void *address)
60{
61 if (0 && address && cpu_has_clflush) {
62 /* is this worth it? */
63 int i;
64 for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
65 asm volatile("clflush (%0)" :: "r" (address + i));
66 } else
67 asm volatile("wbinvd":::"memory");
68 if (address)
69 __flush_tlb_one(address);
70 else
71 __flush_tlb_all();
72}
73
74
75static inline void flush_map(unsigned long address)
76{
77 on_each_cpu(flush_kernel_map, (void *)address, 1, 1);
78}
79
20aaffd6
NP
80static struct page *deferred_pages; /* protected by init_mm.mmap_sem */
81
82static inline void save_page(struct page *fpage)
1da177e4 83{
20aaffd6
NP
84 fpage->lru.next = (struct list_head *)deferred_pages;
85 deferred_pages = fpage;
1da177e4
LT
86}
87
88/*
89 * No more special protections in this 2/4MB area - revert to a
90 * large page again.
91 */
92static void revert_page(unsigned long address, pgprot_t ref_prot)
93{
94 pgd_t *pgd;
95 pud_t *pud;
96 pmd_t *pmd;
97 pte_t large_pte;
98
99 pgd = pgd_offset_k(address);
100 BUG_ON(pgd_none(*pgd));
101 pud = pud_offset(pgd,address);
102 BUG_ON(pud_none(*pud));
103 pmd = pmd_offset(pud, address);
104 BUG_ON(pmd_val(*pmd) & _PAGE_PSE);
105 pgprot_val(ref_prot) |= _PAGE_PSE;
106 large_pte = mk_pte_phys(__pa(address) & LARGE_PAGE_MASK, ref_prot);
107 set_pte((pte_t *)pmd, large_pte);
108}
109
110static int
111__change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot,
112 pgprot_t ref_prot)
113{
114 pte_t *kpte;
115 struct page *kpte_page;
116 unsigned kpte_flags;
c728252c 117 pgprot_t ref_prot2;
1da177e4
LT
118 kpte = lookup_address(address);
119 if (!kpte) return 0;
120 kpte_page = virt_to_page(((unsigned long)kpte) & PAGE_MASK);
121 kpte_flags = pte_val(*kpte);
122 if (pgprot_val(prot) != pgprot_val(ref_prot)) {
123 if ((kpte_flags & _PAGE_PSE) == 0) {
124 set_pte(kpte, pfn_pte(pfn, prot));
125 } else {
126 /*
127 * split_large_page will take the reference for this change_page_attr
128 * on the split page.
129 */
c728252c
AV
130
131 struct page *split;
132 ref_prot2 = __pgprot(pgprot_val(pte_pgprot(*lookup_address(address))) & ~(1<<_PAGE_BIT_PSE));
133
134 split = split_large_page(address, prot, ref_prot2);
1da177e4
LT
135 if (!split)
136 return -ENOMEM;
c728252c 137 set_pte(kpte,mk_pte(split, ref_prot2));
1da177e4
LT
138 kpte_page = split;
139 }
140 get_page(kpte_page);
141 } else if ((kpte_flags & _PAGE_PSE) == 0) {
142 set_pte(kpte, pfn_pte(pfn, ref_prot));
143 __put_page(kpte_page);
144 } else
145 BUG();
146
147 /* on x86-64 the direct mapping set at boot is not using 4k pages */
148 BUG_ON(PageReserved(kpte_page));
149
150 switch (page_count(kpte_page)) {
151 case 1:
20aaffd6 152 save_page(kpte_page);
1da177e4
LT
153 revert_page(address, ref_prot);
154 break;
155 case 0:
156 BUG(); /* memleak and failed 2M page regeneration */
157 }
158 return 0;
159}
160
161/*
162 * Change the page attributes of an page in the linear mapping.
163 *
164 * This should be used when a page is mapped with a different caching policy
165 * than write-back somewhere - some CPUs do not like it when mappings with
166 * different caching policies exist. This changes the page attributes of the
167 * in kernel linear mapping too.
168 *
169 * The caller needs to ensure that there are no conflicting mappings elsewhere.
170 * This function only deals with the kernel linear map.
171 *
172 * Caller must call global_flush_tlb() after this.
173 */
174int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
175{
176 int err = 0;
177 int i;
178
179 down_write(&init_mm.mmap_sem);
180 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
181 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
182
183 err = __change_page_attr(address, pfn, prot, PAGE_KERNEL);
184 if (err)
185 break;
186 /* Handle kernel mapping too which aliases part of the
187 * lowmem */
188 if (__pa(address) < KERNEL_TEXT_SIZE) {
189 unsigned long addr2;
190 pgprot_t prot2 = prot;
191 addr2 = __START_KERNEL_map + __pa(address);
192 pgprot_val(prot2) &= ~_PAGE_NX;
193 err = __change_page_attr(addr2, pfn, prot2, PAGE_KERNEL_EXEC);
194 }
195 }
196 up_write(&init_mm.mmap_sem);
197 return err;
198}
199
200/* Don't call this for MMIO areas that may not have a mem_map entry */
201int change_page_attr(struct page *page, int numpages, pgprot_t prot)
202{
203 unsigned long addr = (unsigned long)page_address(page);
204 return change_page_attr_addr(addr, numpages, prot);
205}
206
207void global_flush_tlb(void)
208{
20aaffd6 209 struct page *dpage;
1da177e4
LT
210
211 down_read(&init_mm.mmap_sem);
20aaffd6 212 dpage = xchg(&deferred_pages, NULL);
1da177e4 213 up_read(&init_mm.mmap_sem);
20aaffd6
NP
214
215 flush_map((dpage && !dpage->lru.next) ? (unsigned long)page_address(dpage) : 0);
216 while (dpage) {
217 struct page *tmp = dpage;
218 dpage = (struct page *)dpage->lru.next;
219 __free_page(tmp);
1da177e4
LT
220 }
221}
222
223EXPORT_SYMBOL(change_page_attr);
224EXPORT_SYMBOL(global_flush_tlb);
This page took 0.095634 seconds and 5 git commands to generate.