1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/mm.h>
20#include <linux/module.h>
21#include <linux/swap.h>
22#include <linux/bio.h>
23#include <linux/pagemap.h>
24#include <linux/mempool.h>
25#include <linux/blkdev.h>
26#include <linux/init.h>
27#include <linux/hash.h>
28#include <linux/highmem.h>
29#include <linux/blktrace_api.h>
30#include <asm/tlbflush.h>
31
32
33
34
35
36
37
38
39
40#ifdef CONFIG_HIGHMEM
41
42unsigned long totalhigh_pages __read_mostly;
43
44unsigned int nr_free_highpages (void)
45{
46 pg_data_t *pgdat;
47 unsigned int pages = 0;
48
49 for_each_online_pgdat(pgdat)
50 pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
51 NR_FREE_PAGES);
52
53 return pages;
54}
55
56static int pkmap_count[LAST_PKMAP];
57static unsigned int last_pkmap_nr;
58static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
59
60pte_t * pkmap_page_table;
61
62static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
63
64static void flush_all_zero_pkmaps(void)
65{
66 int i;
67
68 flush_cache_kmaps();
69
70 for (i = 0; i < LAST_PKMAP; i++) {
71 struct page *page;
72
73
74
75
76
77
78
79 if (pkmap_count[i] != 1)
80 continue;
81 pkmap_count[i] = 0;
82
83
84 BUG_ON(pte_none(pkmap_page_table[i]));
85
86
87
88
89
90
91
92
93 page = pte_page(pkmap_page_table[i]);
94 pte_clear(&init_mm, (unsigned long)page_address(page),
95 &pkmap_page_table[i]);
96
97 set_page_address(page, NULL);
98 }
99 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
100}
101
102
103
104void kmap_flush_unused(void)
105{
106 spin_lock(&kmap_lock);
107 flush_all_zero_pkmaps();
108 spin_unlock(&kmap_lock);
109}
110
111static inline unsigned long map_new_virtual(struct page *page)
112{
113 unsigned long vaddr;
114 int count;
115
116start:
117 count = LAST_PKMAP;
118
119 for (;;) {
120 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
121 if (!last_pkmap_nr) {
122 flush_all_zero_pkmaps();
123 count = LAST_PKMAP;
124 }
125 if (!pkmap_count[last_pkmap_nr])
126 break;
127 if (--count)
128 continue;
129
130
131
132
133 {
134 DECLARE_WAITQUEUE(wait, current);
135
136 __set_current_state(TASK_UNINTERRUPTIBLE);
137 add_wait_queue(&pkmap_map_wait, &wait);
138 spin_unlock(&kmap_lock);
139 schedule();
140 remove_wait_queue(&pkmap_map_wait, &wait);
141 spin_lock(&kmap_lock);
142
143
144 if (page_address(page))
145 return (unsigned long)page_address(page);
146
147
148 goto start;
149 }
150 }
151 vaddr = PKMAP_ADDR(last_pkmap_nr);
152 set_pte_at(&init_mm, vaddr,
153 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
154
155 pkmap_count[last_pkmap_nr] = 1;
156 set_page_address(page, (void *)vaddr);
157
158 return vaddr;
159}
160
161void fastcall *kmap_high(struct page *page)
162{
163 unsigned long vaddr;
164
165
166
167
168
169
170
171 spin_lock(&kmap_lock);
172 vaddr = (unsigned long)page_address(page);
173 if (!vaddr)
174 vaddr = map_new_virtual(page);
175 pkmap_count[PKMAP_NR(vaddr)]++;
176 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
177 spin_unlock(&kmap_lock);
178 return (void*) vaddr;
179}
180
181EXPORT_SYMBOL(kmap_high);
182
183void fastcall kunmap_high(struct page *page)
184{
185 unsigned long vaddr;
186 unsigned long nr;
187 int need_wakeup;
188
189 spin_lock(&kmap_lock);
190 vaddr = (unsigned long)page_address(page);
191 BUG_ON(!vaddr);
192 nr = PKMAP_NR(vaddr);
193
194
195
196
197
198 need_wakeup = 0;
199 switch (--pkmap_count[nr]) {
200 case 0:
201 BUG();
202 case 1:
203
204
205
206
207
208
209
210
211
212
213 need_wakeup = waitqueue_active(&pkmap_map_wait);
214 }
215 spin_unlock(&kmap_lock);
216
217
218 if (need_wakeup)
219 wake_up(&pkmap_map_wait);
220}
221
222EXPORT_SYMBOL(kunmap_high);
223#endif
224
225#if defined(HASHED_PAGE_VIRTUAL)
226
227#define PA_HASH_ORDER 7
228
229
230
231
232struct page_address_map {
233 struct page *page;
234 void *virtual;
235 struct list_head list;
236};
237
238
239
240
241static struct list_head page_address_pool;
242static spinlock_t pool_lock;
243
244
245
246
247static struct page_address_slot {
248 struct list_head lh;
249 spinlock_t lock;
250} ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
251
252static struct page_address_slot *page_slot(struct page *page)
253{
254 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
255}
256
257void *page_address(struct page *page)
258{
259 unsigned long flags;
260 void *ret;
261 struct page_address_slot *pas;
262
263 if (!PageHighMem(page))
264 return lowmem_page_address(page);
265
266 pas = page_slot(page);
267 ret = NULL;
268 spin_lock_irqsave(&pas->lock, flags);
269 if (!list_empty(&pas->lh)) {
270 struct page_address_map *pam;
271
272 list_for_each_entry(pam, &pas->lh, list) {
273 if (pam->page == page) {
274 ret = pam->virtual;
275 goto done;
276 }
277 }
278 }
279done:
280 spin_unlock_irqrestore(&pas->lock, flags);
281 return ret;
282}
283
284EXPORT_SYMBOL(page_address);
285
286void set_page_address(struct page *page, void *virtual)
287{
288 unsigned long flags;
289 struct page_address_slot *pas;
290 struct page_address_map *pam;
291
292 BUG_ON(!PageHighMem(page));
293
294 pas = page_slot(page);
295 if (virtual) {
296 BUG_ON(list_empty(&page_address_pool));
297
298 spin_lock_irqsave(&pool_lock, flags);
299 pam = list_entry(page_address_pool.next,
300 struct page_address_map, list);
301 list_del(&pam->list);
302 spin_unlock_irqrestore(&pool_lock, flags);
303
304 pam->page = page;
305 pam->virtual = virtual;
306
307 spin_lock_irqsave(&pas->lock, flags);
308 list_add_tail(&pam->list, &pas->lh);
309 spin_unlock_irqrestore(&pas->lock, flags);
310 } else {
311 spin_lock_irqsave(&pas->lock, flags);
312 list_for_each_entry(pam, &pas->lh, list) {
313 if (pam->page == page) {
314 list_del(&pam->list);
315 spin_unlock_irqrestore(&pas->lock, flags);
316 spin_lock_irqsave(&pool_lock, flags);
317 list_add_tail(&pam->list, &page_address_pool);
318 spin_unlock_irqrestore(&pool_lock, flags);
319 goto done;
320 }
321 }
322 spin_unlock_irqrestore(&pas->lock, flags);
323 }
324done:
325 return;
326}
327
328static struct page_address_map page_address_maps[LAST_PKMAP];
329
330void __init page_address_init(void)
331{
332 int i;
333
334 INIT_LIST_HEAD(&page_address_pool);
335 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
336 list_add(&page_address_maps[i].list, &page_address_pool);
337 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
338 INIT_LIST_HEAD(&page_address_htable[i].lh);
339 spin_lock_init(&page_address_htable[i].lock);
340 }
341 spin_lock_init(&pool_lock);
342}
343
344#endif
345