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