[PATCH] don't include swap.h in mm.h
[linux-flexiantxendom0-3.2.10.git] / mm / highmem.c
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  */
18
19 #include <linux/mm.h>
20 #include <linux/swap.h>
21 #include <linux/bio.h>
22 #include <linux/pagemap.h>
23 #include <linux/mempool.h>
24 #include <linux/blkdev.h>
25 #include <linux/init.h>
26 #include <linux/hash.h>
27 #include <asm/pgalloc.h>
28 #include <asm/tlbflush.h>
29
30 static mempool_t *page_pool, *isa_page_pool;
31
32 static void *page_pool_alloc(int gfp_mask, void *data)
33 {
34         int gfp = gfp_mask | (int) (long) data;
35
36         return alloc_page(gfp);
37 }
38
39 static void page_pool_free(void *page, void *data)
40 {
41         __free_page(page);
42 }
43
44 /*
45  * Virtual_count is not a pure "count".
46  *  0 means that it is not mapped, and has not been mapped
47  *    since a TLB flush - it is usable.
48  *  1 means that there are no users, but it has been mapped
49  *    since the last TLB flush - so we can't use it.
50  *  n means that there are (n-1) current users of it.
51  */
52 #ifdef CONFIG_HIGHMEM
53 static int pkmap_count[LAST_PKMAP];
54 static unsigned int last_pkmap_nr;
55 static spinlock_t kmap_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
56
57 pte_t * pkmap_page_table;
58
59 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
60
61 static void flush_all_zero_pkmaps(void)
62 {
63         int i;
64
65         flush_cache_all();
66
67         for (i = 0; i < LAST_PKMAP; i++) {
68                 struct page *page;
69
70                 /*
71                  * zero means we don't have anything to do,
72                  * >1 means that it is still in use. Only
73                  * a count of 1 means that it is free but
74                  * needs to be unmapped
75                  */
76                 if (pkmap_count[i] != 1)
77                         continue;
78                 pkmap_count[i] = 0;
79
80                 /* sanity check */
81                 if (pte_none(pkmap_page_table[i]))
82                         BUG();
83
84                 /*
85                  * Don't need an atomic fetch-and-clear op here;
86                  * no-one has the page mapped, and cannot get at
87                  * its virtual address (and hence PTE) without first
88                  * getting the kmap_lock (which is held here).
89                  * So no dangers, even with speculative execution.
90                  */
91                 page = pte_page(pkmap_page_table[i]);
92                 pte_clear(&pkmap_page_table[i]);
93
94                 set_page_address(page, NULL);
95         }
96         flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
97 }
98
99 static inline unsigned long map_new_virtual(struct page *page)
100 {
101         unsigned long vaddr;
102         int count;
103
104 start:
105         count = LAST_PKMAP;
106         /* Find an empty entry */
107         for (;;) {
108                 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
109                 if (!last_pkmap_nr) {
110                         flush_all_zero_pkmaps();
111                         count = LAST_PKMAP;
112                 }
113                 if (!pkmap_count[last_pkmap_nr])
114                         break;  /* Found a usable entry */
115                 if (--count)
116                         continue;
117
118                 /*
119                  * Sleep for somebody else to unmap their entries
120                  */
121                 {
122                         DECLARE_WAITQUEUE(wait, current);
123
124                         __set_current_state(TASK_UNINTERRUPTIBLE);
125                         add_wait_queue(&pkmap_map_wait, &wait);
126                         spin_unlock(&kmap_lock);
127                         schedule();
128                         remove_wait_queue(&pkmap_map_wait, &wait);
129                         spin_lock(&kmap_lock);
130
131                         /* Somebody else might have mapped it while we slept */
132                         if (page_address(page))
133                                 return (unsigned long)page_address(page);
134
135                         /* Re-start */
136                         goto start;
137                 }
138         }
139         vaddr = PKMAP_ADDR(last_pkmap_nr);
140         set_pte(&(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
141
142         pkmap_count[last_pkmap_nr] = 1;
143         set_page_address(page, (void *)vaddr);
144
145         return vaddr;
146 }
147
148 void *kmap_high(struct page *page)
149 {
150         unsigned long vaddr;
151
152         /*
153          * For highmem pages, we can't trust "virtual" until
154          * after we have the lock.
155          *
156          * We cannot call this from interrupts, as it may block
157          */
158         spin_lock(&kmap_lock);
159         vaddr = (unsigned long)page_address(page);
160         if (!vaddr)
161                 vaddr = map_new_virtual(page);
162         pkmap_count[PKMAP_NR(vaddr)]++;
163         if (pkmap_count[PKMAP_NR(vaddr)] < 2)
164                 BUG();
165         spin_unlock(&kmap_lock);
166         return (void*) vaddr;
167 }
168
169 void kunmap_high(struct page *page)
170 {
171         unsigned long vaddr;
172         unsigned long nr;
173         int need_wakeup;
174
175         spin_lock(&kmap_lock);
176         vaddr = (unsigned long)page_address(page);
177         if (!vaddr)
178                 BUG();
179         nr = PKMAP_NR(vaddr);
180
181         /*
182          * A count must never go down to zero
183          * without a TLB flush!
184          */
185         need_wakeup = 0;
186         switch (--pkmap_count[nr]) {
187         case 0:
188                 BUG();
189         case 1:
190                 /*
191                  * Avoid an unnecessary wake_up() function call.
192                  * The common case is pkmap_count[] == 1, but
193                  * no waiters.
194                  * The tasks queued in the wait-queue are guarded
195                  * by both the lock in the wait-queue-head and by
196                  * the kmap_lock.  As the kmap_lock is held here,
197                  * no need for the wait-queue-head's lock.  Simply
198                  * test if the queue is empty.
199                  */
200                 need_wakeup = waitqueue_active(&pkmap_map_wait);
201         }
202         spin_unlock(&kmap_lock);
203
204         /* do wake-up, if needed, race-free outside of the spin lock */
205         if (need_wakeup)
206                 wake_up(&pkmap_map_wait);
207 }
208
209 #define POOL_SIZE       64
210
211 static __init int init_emergency_pool(void)
212 {
213         struct sysinfo i;
214         si_meminfo(&i);
215         si_swapinfo(&i);
216         
217         if (!i.totalhigh)
218                 return 0;
219
220         page_pool = mempool_create(POOL_SIZE, page_pool_alloc, page_pool_free, NULL);
221         if (!page_pool)
222                 BUG();
223         printk("highmem bounce pool size: %d pages\n", POOL_SIZE);
224
225         return 0;
226 }
227
228 __initcall(init_emergency_pool);
229
230 /*
231  * highmem version, map in to vec
232  */
233 static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
234 {
235         unsigned long flags;
236         unsigned char *vto;
237
238         local_irq_save(flags);
239         vto = kmap_atomic(to->bv_page, KM_BOUNCE_READ);
240         memcpy(vto + to->bv_offset, vfrom, to->bv_len);
241         kunmap_atomic(vto, KM_BOUNCE_READ);
242         local_irq_restore(flags);
243 }
244
245 #else /* CONFIG_HIGHMEM */
246
247 #define bounce_copy_vec(to, vfrom)      \
248         memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
249
250 #endif
251
252 #define ISA_POOL_SIZE   16
253
254 /*
255  * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
256  * as the max address, so check if the pool has already been created.
257  */
258 int init_emergency_isa_pool(void)
259 {
260         if (isa_page_pool)
261                 return 0;
262
263         isa_page_pool = mempool_create(ISA_POOL_SIZE, page_pool_alloc, page_pool_free, (void *) __GFP_DMA);
264         if (!isa_page_pool)
265                 BUG();
266
267         printk("isa bounce pool size: %d pages\n", ISA_POOL_SIZE);
268         return 0;
269 }
270
271 /*
272  * Simple bounce buffer support for highmem pages. Depending on the
273  * queue gfp mask set, *to may or may not be a highmem page. kmap it
274  * always, it will do the Right Thing
275  */
276 static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
277 {
278         unsigned char *vfrom;
279         struct bio_vec *tovec, *fromvec;
280         int i;
281
282         __bio_for_each_segment(tovec, to, i, 0) {
283                 fromvec = from->bi_io_vec + i;
284
285                 /*
286                  * not bounced
287                  */
288                 if (tovec->bv_page == fromvec->bv_page)
289                         continue;
290
291                 vfrom = page_address(fromvec->bv_page) + fromvec->bv_offset;
292
293                 bounce_copy_vec(tovec, vfrom);
294         }
295 }
296
297 static void bounce_end_io(struct bio *bio, mempool_t *pool)
298 {
299         struct bio *bio_orig = bio->bi_private;
300         struct bio_vec *bvec, *org_vec;
301         int i;
302
303         if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
304                 goto out_eio;
305
306         set_bit(BIO_UPTODATE, &bio_orig->bi_flags);
307
308         /*
309          * free up bounce indirect pages used
310          */
311         __bio_for_each_segment(bvec, bio, i, 0) {
312                 org_vec = bio_orig->bi_io_vec + i;
313                 if (bvec->bv_page == org_vec->bv_page)
314                         continue;
315
316                 mempool_free(bvec->bv_page, pool);      
317         }
318
319 out_eio:
320         bio_endio(bio_orig, bio_orig->bi_size, 0);
321         bio_put(bio);
322 }
323
324 static int bounce_end_io_write(struct bio *bio, unsigned int bytes_done,int err)
325 {
326         if (bio->bi_size)
327                 return 1;
328
329         bounce_end_io(bio, page_pool);
330         return 0;
331 }
332
333 static int bounce_end_io_write_isa(struct bio *bio, unsigned int bytes_done, int err)
334 {
335         if (bio->bi_size)
336                 return 1;
337
338         bounce_end_io(bio, isa_page_pool);
339         return 0;
340 }
341
342 static void __bounce_end_io_read(struct bio *bio, mempool_t *pool)
343 {
344         struct bio *bio_orig = bio->bi_private;
345
346         if (test_bit(BIO_UPTODATE, &bio->bi_flags))
347                 copy_to_high_bio_irq(bio_orig, bio);
348
349         bounce_end_io(bio, pool);
350 }
351
352 static int bounce_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
353 {
354         if (bio->bi_size)
355                 return 1;
356
357         __bounce_end_io_read(bio, page_pool);
358         return 0;
359 }
360
361 static int bounce_end_io_read_isa(struct bio *bio, unsigned int bytes_done, int err)
362 {
363         if (bio->bi_size)
364                 return 1;
365
366         __bounce_end_io_read(bio, isa_page_pool);
367         return 0;
368 }
369
370 static void __blk_queue_bounce(request_queue_t *q, struct bio **bio_orig,
371                         mempool_t *pool)
372 {
373         struct page *page;
374         struct bio *bio = NULL;
375         int i, rw = bio_data_dir(*bio_orig);
376         struct bio_vec *to, *from;
377
378         bio_for_each_segment(from, *bio_orig, i) {
379                 page = from->bv_page;
380
381                 /*
382                  * is destination page below bounce pfn?
383                  */
384                 if ((page - page_zone(page)->zone_mem_map) + (page_zone(page)->zone_start_pfn) < q->bounce_pfn)
385                         continue;
386
387                 /*
388                  * irk, bounce it
389                  */
390                 if (!bio)
391                         bio = bio_alloc(GFP_NOIO, (*bio_orig)->bi_vcnt);
392
393                 to = bio->bi_io_vec + i;
394
395                 to->bv_page = mempool_alloc(pool, q->bounce_gfp);
396                 to->bv_len = from->bv_len;
397                 to->bv_offset = from->bv_offset;
398
399                 if (rw == WRITE) {
400                         char *vto, *vfrom;
401
402                         vto = page_address(to->bv_page) + to->bv_offset;
403                         vfrom = kmap(from->bv_page) + from->bv_offset;
404                         memcpy(vto, vfrom, to->bv_len);
405                         kunmap(from->bv_page);
406                 }
407         }
408
409         /*
410          * no pages bounced
411          */
412         if (!bio)
413                 return;
414
415         /*
416          * at least one page was bounced, fill in possible non-highmem
417          * pages
418          */
419         bio_for_each_segment(from, *bio_orig, i) {
420                 to = bio_iovec_idx(bio, i);
421                 if (!to->bv_page) {
422                         to->bv_page = from->bv_page;
423                         to->bv_len = from->bv_len;
424                         to->bv_offset = from->bv_offset;
425                 }
426         }
427
428         bio->bi_bdev = (*bio_orig)->bi_bdev;
429         bio->bi_flags |= (1 << BIO_BOUNCED);
430         bio->bi_sector = (*bio_orig)->bi_sector;
431         bio->bi_rw = (*bio_orig)->bi_rw;
432
433         bio->bi_vcnt = (*bio_orig)->bi_vcnt;
434         bio->bi_idx = 0;
435         bio->bi_size = (*bio_orig)->bi_size;
436
437         if (pool == page_pool) {
438                 bio->bi_end_io = bounce_end_io_write;
439                 if (rw == READ)
440                         bio->bi_end_io = bounce_end_io_read;
441         } else {
442                 bio->bi_end_io = bounce_end_io_write_isa;
443                 if (rw == READ)
444                         bio->bi_end_io = bounce_end_io_read_isa;
445         }
446
447         bio->bi_private = *bio_orig;
448         *bio_orig = bio;
449 }
450
451 void blk_queue_bounce(request_queue_t *q, struct bio **bio_orig)
452 {
453         mempool_t *pool;
454
455         /*
456          * for non-isa bounce case, just check if the bounce pfn is equal
457          * to or bigger than the highest pfn in the system -- in that case,
458          * don't waste time iterating over bio segments
459          */
460         if (!(q->bounce_gfp & GFP_DMA)) {
461                 if (q->bounce_pfn >= blk_max_pfn)
462                         return;
463                 pool = page_pool;
464         } else {
465                 BUG_ON(!isa_page_pool);
466                 pool = isa_page_pool;
467         }
468
469         /*
470          * slow path
471          */
472         __blk_queue_bounce(q, bio_orig, pool);
473 }
474
475 #if defined(CONFIG_DEBUG_HIGHMEM) && defined(CONFIG_HIGHMEM)
476 void check_highmem_ptes(void)
477 {
478         int idx, type;
479
480         preempt_disable();
481         for (type = 0; type < KM_TYPE_NR; type++) {
482                 idx = type + KM_TYPE_NR*smp_processor_id();
483                 if (!pte_none(*(kmap_pte-idx))) {
484                         printk("scheduling with KM_TYPE %d held!\n", type);
485                         BUG();
486                 }
487         }
488         preempt_enable();
489 }
490 #endif
491
492 #if defined(HASHED_PAGE_VIRTUAL)
493
494 #define PA_HASH_ORDER   7
495
496 /*
497  * Describes one page->virtual association
498  */
499 struct page_address_map {
500         struct page *page;
501         void *virtual;
502         struct list_head list;
503 };
504
505 /*
506  * page_address_map freelist, allocated from page_address_maps.
507  */
508 static struct list_head page_address_pool;      /* freelist */
509 static spinlock_t pool_lock;                    /* protects page_address_pool */
510
511 /*
512  * Hash table bucket
513  */
514 static struct page_address_slot {
515         struct list_head lh;                    /* List of page_address_maps */
516         spinlock_t lock;                        /* Protect this bucket's list */
517 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
518
519 static struct page_address_slot *page_slot(struct page *page)
520 {
521         return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
522 }
523
524 void *page_address(struct page *page)
525 {
526         unsigned long flags;
527         void *ret;
528         struct page_address_slot *pas;
529
530         if (!PageHighMem(page))
531                 return lowmem_page_address(page);
532
533         pas = page_slot(page);
534         ret = NULL;
535         spin_lock_irqsave(&pas->lock, flags);
536         if (!list_empty(&pas->lh)) {
537                 struct page_address_map *pam;
538
539                 list_for_each_entry(pam, &pas->lh, list) {
540                         if (pam->page == page) {
541                                 ret = pam->virtual;
542                                 goto done;
543                         }
544                 }
545         }
546 done:
547         spin_unlock_irqrestore(&pas->lock, flags);
548         return ret;
549 }
550
551 void set_page_address(struct page *page, void *virtual)
552 {
553         unsigned long flags;
554         struct page_address_slot *pas;
555         struct page_address_map *pam;
556
557         BUG_ON(!PageHighMem(page));
558
559         pas = page_slot(page);
560         if (virtual) {          /* Add */
561                 BUG_ON(list_empty(&page_address_pool));
562
563                 spin_lock_irqsave(&pool_lock, flags);
564                 pam = list_entry(page_address_pool.next,
565                                 struct page_address_map, list);
566                 list_del(&pam->list);
567                 spin_unlock_irqrestore(&pool_lock, flags);
568
569                 pam->page = page;
570                 pam->virtual = virtual;
571
572                 spin_lock_irqsave(&pas->lock, flags);
573                 list_add_tail(&pam->list, &pas->lh);
574                 spin_unlock_irqrestore(&pas->lock, flags);
575         } else {                /* Remove */
576                 spin_lock_irqsave(&pas->lock, flags);
577                 list_for_each_entry(pam, &pas->lh, list) {
578                         if (pam->page == page) {
579                                 list_del(&pam->list);
580                                 spin_unlock_irqrestore(&pas->lock, flags);
581                                 spin_lock_irqsave(&pool_lock, flags);
582                                 list_add_tail(&pam->list, &page_address_pool);
583                                 spin_unlock_irqrestore(&pool_lock, flags);
584                                 goto done;
585                         }
586                 }
587                 spin_unlock_irqrestore(&pas->lock, flags);
588         }
589 done:
590         return;
591 }
592
593 static struct page_address_map page_address_maps[LAST_PKMAP];
594
595 void __init page_address_init(void)
596 {
597         int i;
598
599         INIT_LIST_HEAD(&page_address_pool);
600         for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
601                 list_add(&page_address_maps[i].list, &page_address_pool);
602         for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
603                 INIT_LIST_HEAD(&page_address_htable[i].lh);
604                 spin_lock_init(&page_address_htable[i].lock);
605         }
606         spin_lock_init(&pool_lock);
607 }
608
609 #endif  /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */