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 if (zone_movable_is_highmem())
53 pages += zone_page_state(
54 &pgdat->node_zones[ZONE_MOVABLE],
55 NR_FREE_PAGES);
56 }
57
58 return pages;
59}
60
61static int pkmap_count[LAST_PKMAP];
62static unsigned int last_pkmap_nr;
63static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
64
65pte_t * pkmap_page_table;
66
67static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
68
69static void flush_all_zero_pkmaps(void)
70{
71 int i;
72
73 flush_cache_kmaps();
74
75 for (i = 0; i < LAST_PKMAP; i++) {
76 struct page *page;
77
78
79
80
81
82
83
84 if (pkmap_count[i] != 1)
85 continue;
86 pkmap_count[i] = 0;
87
88
89 BUG_ON(pte_none(pkmap_page_table[i]));
90
91
92
93
94
95
96
97
98 page = pte_page(pkmap_page_table[i]);
99 pte_clear(&init_mm, (unsigned long)page_address(page),
100 &pkmap_page_table[i]);
101
102 set_page_address(page, NULL);
103 }
104 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
105}
106
107
108
109
110void kmap_flush_unused(void)
111{
112 spin_lock(&kmap_lock);
113 flush_all_zero_pkmaps();
114 spin_unlock(&kmap_lock);
115}
116
117static inline unsigned long map_new_virtual(struct page *page)
118{
119 unsigned long vaddr;
120 int count;
121
122start:
123 count = LAST_PKMAP;
124
125 for (;;) {
126 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
127 if (!last_pkmap_nr) {
128 flush_all_zero_pkmaps();
129 count = LAST_PKMAP;
130 }
131 if (!pkmap_count[last_pkmap_nr])
132 break;
133 if (--count)
134 continue;
135
136
137
138
139 {
140 DECLARE_WAITQUEUE(wait, current);
141
142 __set_current_state(TASK_UNINTERRUPTIBLE);
143 add_wait_queue(&pkmap_map_wait, &wait);
144 spin_unlock(&kmap_lock);
145 schedule();
146 remove_wait_queue(&pkmap_map_wait, &wait);
147 spin_lock(&kmap_lock);
148
149
150 if (page_address(page))
151 return (unsigned long)page_address(page);
152
153
154 goto start;
155 }
156 }
157 vaddr = PKMAP_ADDR(last_pkmap_nr);
158 set_pte_at(&init_mm, vaddr,
159 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
160
161 pkmap_count[last_pkmap_nr] = 1;
162 set_page_address(page, (void *)vaddr);
163
164 return vaddr;
165}
166
167
168
169
170
171
172
173
174
175void *kmap_high(struct page *page)
176{
177 unsigned long vaddr;
178
179
180
181
182
183 spin_lock(&kmap_lock);
184 vaddr = (unsigned long)page_address(page);
185 if (!vaddr)
186 vaddr = map_new_virtual(page);
187 pkmap_count[PKMAP_NR(vaddr)]++;
188 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
189 spin_unlock(&kmap_lock);
190 return (void*) vaddr;
191}
192
193EXPORT_SYMBOL(kmap_high);
194
195
196
197
198
199void kunmap_high(struct page *page)
200{
201 unsigned long vaddr;
202 unsigned long nr;
203 int need_wakeup;
204
205 spin_lock(&kmap_lock);
206 vaddr = (unsigned long)page_address(page);
207 BUG_ON(!vaddr);
208 nr = PKMAP_NR(vaddr);
209
210
211
212
213
214 need_wakeup = 0;
215 switch (--pkmap_count[nr]) {
216 case 0:
217 BUG();
218 case 1:
219
220
221
222
223
224
225
226
227
228
229 need_wakeup = waitqueue_active(&pkmap_map_wait);
230 }
231 spin_unlock(&kmap_lock);
232
233
234 if (need_wakeup)
235 wake_up(&pkmap_map_wait);
236}
237
238EXPORT_SYMBOL(kunmap_high);
239#endif
240
241#if defined(HASHED_PAGE_VIRTUAL)
242
243#define PA_HASH_ORDER 7
244
245
246
247
248struct page_address_map {
249 struct page *page;
250 void *virtual;
251 struct list_head list;
252};
253
254
255
256
257static struct list_head page_address_pool;
258static spinlock_t pool_lock;
259
260
261
262
263static struct page_address_slot {
264 struct list_head lh;
265 spinlock_t lock;
266} ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
267
268static struct page_address_slot *page_slot(struct page *page)
269{
270 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
271}
272
273
274
275
276
277
278
279void *page_address(struct page *page)
280{
281 unsigned long flags;
282 void *ret;
283 struct page_address_slot *pas;
284
285 if (!PageHighMem(page))
286 return lowmem_page_address(page);
287
288 pas = page_slot(page);
289 ret = NULL;
290 spin_lock_irqsave(&pas->lock, flags);
291 if (!list_empty(&pas->lh)) {
292 struct page_address_map *pam;
293
294 list_for_each_entry(pam, &pas->lh, list) {
295 if (pam->page == page) {
296 ret = pam->virtual;
297 goto done;
298 }
299 }
300 }
301done:
302 spin_unlock_irqrestore(&pas->lock, flags);
303 return ret;
304}
305
306EXPORT_SYMBOL(page_address);
307
308
309
310
311
312
313void set_page_address(struct page *page, void *virtual)
314{
315 unsigned long flags;
316 struct page_address_slot *pas;
317 struct page_address_map *pam;
318
319 BUG_ON(!PageHighMem(page));
320
321 pas = page_slot(page);
322 if (virtual) {
323 BUG_ON(list_empty(&page_address_pool));
324
325 spin_lock_irqsave(&pool_lock, flags);
326 pam = list_entry(page_address_pool.next,
327 struct page_address_map, list);
328 list_del(&pam->list);
329 spin_unlock_irqrestore(&pool_lock, flags);
330
331 pam->page = page;
332 pam->virtual = virtual;
333
334 spin_lock_irqsave(&pas->lock, flags);
335 list_add_tail(&pam->list, &pas->lh);
336 spin_unlock_irqrestore(&pas->lock, flags);
337 } else {
338 spin_lock_irqsave(&pas->lock, flags);
339 list_for_each_entry(pam, &pas->lh, list) {
340 if (pam->page == page) {
341 list_del(&pam->list);
342 spin_unlock_irqrestore(&pas->lock, flags);
343 spin_lock_irqsave(&pool_lock, flags);
344 list_add_tail(&pam->list, &page_address_pool);
345 spin_unlock_irqrestore(&pool_lock, flags);
346 goto done;
347 }
348 }
349 spin_unlock_irqrestore(&pas->lock, flags);
350 }
351done:
352 return;
353}
354
355static struct page_address_map page_address_maps[LAST_PKMAP];
356
357void __init page_address_init(void)
358{
359 int i;
360
361 INIT_LIST_HEAD(&page_address_pool);
362 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
363 list_add(&page_address_maps[i].list, &page_address_pool);
364 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
365 INIT_LIST_HEAD(&page_address_htable[i].lh);
366 spin_lock_init(&page_address_htable[i].lock);
367 }
368 spin_lock_init(&pool_lock);
369}
370
371#endif
372