ia64/xen-unstable

view linux-2.4.29-xen-sparse/mm/highmem.c @ 3602:9a9c5a491401

bitkeeper revision 1.1159.235.1 (42000d3dwcPyT8aY4VIPYGCfCAJuQQ)

More x86/64. Status: traps.c now included in the build, but actual building
of IDT doesn't happen, and we need some sort of entry.S. More page-table
building required so that arch_init_memory() can work. And there is something
odd with MP-table parsing; I currently suspect that __init sections are
causing problems.
Signed-off-by: keir.fraser@cl.cam.ac.uk
author kaf24@viper.(none)
date Tue Feb 01 23:14:05 2005 +0000 (2005-02-01)
parents 610068179f96
children 0a4b76b6b5a0
line source
1 /*
2 * High memory handling common code and variables.
3 *
4 * (C) 1999 Andrea Arcangeli, SuSE GmbH, andrea@suse.de
5 * Gerhard Wichert, Siemens AG, Gerhard.Wichert@pdb.siemens.de
6 *
7 *
8 * Redesigned the x86 32-bit VM architecture to deal with
9 * 64-bit physical space. With current x86 CPUs this
10 * means up to 64 Gigabytes physical RAM.
11 *
12 * Rewrote high memory support to move the page cache into
13 * high memory. Implemented permanent (schedulable) kmaps
14 * based on Linus' idea.
15 *
16 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
17 */
19 #include <linux/mm.h>
20 #include <linux/pagemap.h>
21 #include <linux/highmem.h>
22 #include <linux/swap.h>
23 #include <linux/slab.h>
25 /*
26 * Virtual_count is not a pure "count".
27 * 0 means that it is not mapped, and has not been mapped
28 * since a TLB flush - it is usable.
29 * 1 means that there are no users, but it has been mapped
30 * since the last TLB flush - so we can't use it.
31 * n means that there are (n-1) current users of it.
32 */
33 static int pkmap_count[LAST_PKMAP];
34 static unsigned int last_pkmap_nr;
35 static spinlock_cacheline_t kmap_lock_cacheline = {SPIN_LOCK_UNLOCKED};
36 #define kmap_lock kmap_lock_cacheline.lock
38 pte_t * pkmap_page_table;
40 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
42 static void flush_all_zero_pkmaps(void)
43 {
44 int i;
46 flush_cache_all();
48 for (i = 0; i < LAST_PKMAP; i++) {
49 struct page *page;
51 /*
52 * zero means we don't have anything to do,
53 * >1 means that it is still in use. Only
54 * a count of 1 means that it is free but
55 * needs to be unmapped
56 */
57 if (pkmap_count[i] != 1)
58 continue;
59 pkmap_count[i] = 0;
61 /* sanity check */
62 if (pte_none(pkmap_page_table[i]))
63 BUG();
65 /*
66 * Don't need an atomic fetch-and-clear op here;
67 * no-one has the page mapped, and cannot get at
68 * its virtual address (and hence PTE) without first
69 * getting the kmap_lock (which is held here).
70 * So no dangers, even with speculative execution.
71 */
72 page = pte_page(pkmap_page_table[i]);
73 pte_clear(&pkmap_page_table[i]);
75 page->virtual = NULL;
76 }
77 flush_tlb_all();
78 }
80 static inline unsigned long map_new_virtual(struct page *page, int nonblocking)
81 {
82 unsigned long vaddr;
83 int count;
85 start:
86 count = LAST_PKMAP;
87 /* Find an empty entry */
88 for (;;) {
89 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
90 if (!last_pkmap_nr) {
91 flush_all_zero_pkmaps();
92 count = LAST_PKMAP;
93 }
94 if (!pkmap_count[last_pkmap_nr])
95 break; /* Found a usable entry */
96 if (--count)
97 continue;
99 if (nonblocking)
100 return 0;
102 /*
103 * Sleep for somebody else to unmap their entries
104 */
105 {
106 DECLARE_WAITQUEUE(wait, current);
108 current->state = TASK_UNINTERRUPTIBLE;
109 add_wait_queue(&pkmap_map_wait, &wait);
110 spin_unlock(&kmap_lock);
111 schedule();
112 remove_wait_queue(&pkmap_map_wait, &wait);
113 spin_lock(&kmap_lock);
115 /* Somebody else might have mapped it while we slept */
116 if (page->virtual)
117 return (unsigned long) page->virtual;
119 /* Re-start */
120 goto start;
121 }
122 }
123 vaddr = PKMAP_ADDR(last_pkmap_nr);
124 set_pte(&(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
125 XEN_flush_page_update_queue();
127 pkmap_count[last_pkmap_nr] = 1;
128 page->virtual = (void *) vaddr;
130 return vaddr;
131 }
133 void fastcall *kmap_high(struct page *page, int nonblocking)
134 {
135 unsigned long vaddr;
137 /*
138 * For highmem pages, we can't trust "virtual" until
139 * after we have the lock.
140 *
141 * We cannot call this from interrupts, as it may block
142 */
143 spin_lock(&kmap_lock);
144 vaddr = (unsigned long) page->virtual;
145 if (!vaddr) {
146 vaddr = map_new_virtual(page, nonblocking);
147 if (!vaddr)
148 goto out;
149 }
150 pkmap_count[PKMAP_NR(vaddr)]++;
151 if (pkmap_count[PKMAP_NR(vaddr)] < 2)
152 BUG();
153 out:
154 spin_unlock(&kmap_lock);
155 return (void*) vaddr;
156 }
158 void fastcall kunmap_high(struct page *page)
159 {
160 unsigned long vaddr;
161 unsigned long nr;
162 int need_wakeup;
164 spin_lock(&kmap_lock);
165 vaddr = (unsigned long) page->virtual;
166 if (!vaddr)
167 BUG();
168 nr = PKMAP_NR(vaddr);
170 /*
171 * A count must never go down to zero
172 * without a TLB flush!
173 */
174 need_wakeup = 0;
175 switch (--pkmap_count[nr]) {
176 case 0:
177 BUG();
178 case 1:
179 /*
180 * Avoid an unnecessary wake_up() function call.
181 * The common case is pkmap_count[] == 1, but
182 * no waiters.
183 * The tasks queued in the wait-queue are guarded
184 * by both the lock in the wait-queue-head and by
185 * the kmap_lock. As the kmap_lock is held here,
186 * no need for the wait-queue-head's lock. Simply
187 * test if the queue is empty.
188 */
189 need_wakeup = waitqueue_active(&pkmap_map_wait);
190 }
191 spin_unlock(&kmap_lock);
193 /* do wake-up, if needed, race-free outside of the spin lock */
194 if (need_wakeup)
195 wake_up(&pkmap_map_wait);
196 }
198 #define POOL_SIZE 32
200 /*
201 * This lock gets no contention at all, normally.
202 */
203 static spinlock_t emergency_lock = SPIN_LOCK_UNLOCKED;
205 int nr_emergency_pages;
206 static LIST_HEAD(emergency_pages);
208 int nr_emergency_bhs;
209 static LIST_HEAD(emergency_bhs);
211 /*
212 * Simple bounce buffer support for highmem pages.
213 * This will be moved to the block layer in 2.5.
214 */
216 static inline void copy_from_high_bh (struct buffer_head *to,
217 struct buffer_head *from)
218 {
219 struct page *p_from;
220 char *vfrom;
222 p_from = from->b_page;
224 vfrom = kmap_atomic(p_from, KM_USER0);
225 memcpy(to->b_data, vfrom + bh_offset(from), to->b_size);
226 kunmap_atomic(vfrom, KM_USER0);
227 }
229 static inline void copy_to_high_bh_irq (struct buffer_head *to,
230 struct buffer_head *from)
231 {
232 struct page *p_to;
233 char *vto;
234 unsigned long flags;
236 p_to = to->b_page;
237 __save_flags(flags);
238 __cli();
239 vto = kmap_atomic(p_to, KM_BOUNCE_READ);
240 memcpy(vto + bh_offset(to), from->b_data, to->b_size);
241 kunmap_atomic(vto, KM_BOUNCE_READ);
242 __restore_flags(flags);
243 }
245 static inline void bounce_end_io (struct buffer_head *bh, int uptodate)
246 {
247 struct page *page;
248 struct buffer_head *bh_orig = (struct buffer_head *)(bh->b_private);
249 unsigned long flags;
251 bh_orig->b_end_io(bh_orig, uptodate);
253 page = bh->b_page;
255 spin_lock_irqsave(&emergency_lock, flags);
256 if (nr_emergency_pages >= POOL_SIZE)
257 __free_page(page);
258 else {
259 /*
260 * We are abusing page->list to manage
261 * the highmem emergency pool:
262 */
263 list_add(&page->list, &emergency_pages);
264 nr_emergency_pages++;
265 }
267 if (nr_emergency_bhs >= POOL_SIZE) {
268 #ifdef HIGHMEM_DEBUG
269 /* Don't clobber the constructed slab cache */
270 init_waitqueue_head(&bh->b_wait);
271 #endif
272 kmem_cache_free(bh_cachep, bh);
273 } else {
274 /*
275 * Ditto in the bh case, here we abuse b_inode_buffers:
276 */
277 list_add(&bh->b_inode_buffers, &emergency_bhs);
278 nr_emergency_bhs++;
279 }
280 spin_unlock_irqrestore(&emergency_lock, flags);
281 }
283 static __init int init_emergency_pool(void)
284 {
285 struct sysinfo i;
286 si_meminfo(&i);
287 si_swapinfo(&i);
289 if (!i.totalhigh)
290 return 0;
292 spin_lock_irq(&emergency_lock);
293 while (nr_emergency_pages < POOL_SIZE) {
294 struct page * page = alloc_page(GFP_ATOMIC);
295 if (!page) {
296 printk("couldn't refill highmem emergency pages");
297 break;
298 }
299 list_add(&page->list, &emergency_pages);
300 nr_emergency_pages++;
301 }
302 while (nr_emergency_bhs < POOL_SIZE) {
303 struct buffer_head * bh = kmem_cache_alloc(bh_cachep, SLAB_ATOMIC);
304 if (!bh) {
305 printk("couldn't refill highmem emergency bhs");
306 break;
307 }
308 list_add(&bh->b_inode_buffers, &emergency_bhs);
309 nr_emergency_bhs++;
310 }
311 spin_unlock_irq(&emergency_lock);
312 printk("allocated %d pages and %d bhs reserved for the highmem bounces\n",
313 nr_emergency_pages, nr_emergency_bhs);
315 return 0;
316 }
318 __initcall(init_emergency_pool);
320 static void bounce_end_io_write (struct buffer_head *bh, int uptodate)
321 {
322 bounce_end_io(bh, uptodate);
323 }
325 static void bounce_end_io_read (struct buffer_head *bh, int uptodate)
326 {
327 struct buffer_head *bh_orig = (struct buffer_head *)(bh->b_private);
329 if (uptodate)
330 copy_to_high_bh_irq(bh_orig, bh);
331 bounce_end_io(bh, uptodate);
332 }
334 struct page *alloc_bounce_page (void)
335 {
336 struct list_head *tmp;
337 struct page *page;
339 page = alloc_page(GFP_NOHIGHIO);
340 if (page)
341 return page;
342 /*
343 * No luck. First, kick the VM so it doesn't idle around while
344 * we are using up our emergency rations.
345 */
346 wakeup_bdflush();
348 repeat_alloc:
349 /*
350 * Try to allocate from the emergency pool.
351 */
352 tmp = &emergency_pages;
353 spin_lock_irq(&emergency_lock);
354 if (!list_empty(tmp)) {
355 page = list_entry(tmp->next, struct page, list);
356 list_del(tmp->next);
357 nr_emergency_pages--;
358 }
359 spin_unlock_irq(&emergency_lock);
360 if (page)
361 return page;
363 /* we need to wait I/O completion */
364 run_task_queue(&tq_disk);
366 yield();
367 goto repeat_alloc;
368 }
370 struct buffer_head *alloc_bounce_bh (void)
371 {
372 struct list_head *tmp;
373 struct buffer_head *bh;
375 bh = kmem_cache_alloc(bh_cachep, SLAB_NOHIGHIO);
376 if (bh)
377 return bh;
378 /*
379 * No luck. First, kick the VM so it doesn't idle around while
380 * we are using up our emergency rations.
381 */
382 wakeup_bdflush();
384 repeat_alloc:
385 /*
386 * Try to allocate from the emergency pool.
387 */
388 tmp = &emergency_bhs;
389 spin_lock_irq(&emergency_lock);
390 if (!list_empty(tmp)) {
391 bh = list_entry(tmp->next, struct buffer_head, b_inode_buffers);
392 list_del(tmp->next);
393 nr_emergency_bhs--;
394 }
395 spin_unlock_irq(&emergency_lock);
396 if (bh)
397 return bh;
399 /* we need to wait I/O completion */
400 run_task_queue(&tq_disk);
402 yield();
403 goto repeat_alloc;
404 }
406 struct buffer_head * create_bounce(int rw, struct buffer_head * bh_orig)
407 {
408 struct page *page;
409 struct buffer_head *bh;
411 if (!PageHighMem(bh_orig->b_page))
412 return bh_orig;
414 bh = alloc_bounce_bh();
415 /*
416 * This is wasteful for 1k buffers, but this is a stopgap measure
417 * and we are being ineffective anyway. This approach simplifies
418 * things immensly. On boxes with more than 4GB RAM this should
419 * not be an issue anyway.
420 */
421 page = alloc_bounce_page();
423 set_bh_page(bh, page, 0);
425 bh->b_next = NULL;
426 bh->b_blocknr = bh_orig->b_blocknr;
427 bh->b_size = bh_orig->b_size;
428 bh->b_list = -1;
429 bh->b_dev = bh_orig->b_dev;
430 bh->b_count = bh_orig->b_count;
431 bh->b_rdev = bh_orig->b_rdev;
432 bh->b_state = bh_orig->b_state;
433 #ifdef HIGHMEM_DEBUG
434 bh->b_flushtime = jiffies;
435 bh->b_next_free = NULL;
436 bh->b_prev_free = NULL;
437 /* bh->b_this_page */
438 bh->b_reqnext = NULL;
439 bh->b_pprev = NULL;
440 #endif
441 /* bh->b_page */
442 if (rw == WRITE) {
443 bh->b_end_io = bounce_end_io_write;
444 copy_from_high_bh(bh, bh_orig);
445 } else
446 bh->b_end_io = bounce_end_io_read;
447 bh->b_private = (void *)bh_orig;
448 bh->b_rsector = bh_orig->b_rsector;
449 #ifdef HIGHMEM_DEBUG
450 memset(&bh->b_wait, -1, sizeof(bh->b_wait));
451 #endif
453 return bh;
454 }