Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hskinnemoen...
[linux-2.6] / 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/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  * Virtual_count is not a pure "count".
34  *  0 means that it is not mapped, and has not been mapped
35  *    since a TLB flush - it is usable.
36  *  1 means that there are no users, but it has been mapped
37  *    since the last TLB flush - so we can't use it.
38  *  n means that there are (n-1) current users of it.
39  */
40 #ifdef CONFIG_HIGHMEM
41
42 unsigned long totalhigh_pages __read_mostly;
43
44 unsigned 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
61 static int pkmap_count[LAST_PKMAP];
62 static unsigned int last_pkmap_nr;
63 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
64
65 pte_t * pkmap_page_table;
66
67 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
68
69 static 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                  * zero means we don't have anything to do,
80                  * >1 means that it is still in use. Only
81                  * a count of 1 means that it is free but
82                  * needs to be unmapped
83                  */
84                 if (pkmap_count[i] != 1)
85                         continue;
86                 pkmap_count[i] = 0;
87
88                 /* sanity check */
89                 BUG_ON(pte_none(pkmap_page_table[i]));
90
91                 /*
92                  * Don't need an atomic fetch-and-clear op here;
93                  * no-one has the page mapped, and cannot get at
94                  * its virtual address (and hence PTE) without first
95                  * getting the kmap_lock (which is held here).
96                  * So no dangers, even with speculative execution.
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  * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
109  */
110 void kmap_flush_unused(void)
111 {
112         spin_lock(&kmap_lock);
113         flush_all_zero_pkmaps();
114         spin_unlock(&kmap_lock);
115 }
116
117 static inline unsigned long map_new_virtual(struct page *page)
118 {
119         unsigned long vaddr;
120         int count;
121
122 start:
123         count = LAST_PKMAP;
124         /* Find an empty entry */
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;  /* Found a usable entry */
133                 if (--count)
134                         continue;
135
136                 /*
137                  * Sleep for somebody else to unmap their entries
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                         /* Somebody else might have mapped it while we slept */
150                         if (page_address(page))
151                                 return (unsigned long)page_address(page);
152
153                         /* Re-start */
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  * kmap_high - map a highmem page into memory
169  * @page: &struct page to map
170  *
171  * Returns the page's virtual memory address.
172  *
173  * We cannot call this from interrupts, as it may block.
174  */
175 void *kmap_high(struct page *page)
176 {
177         unsigned long vaddr;
178
179         /*
180          * For highmem pages, we can't trust "virtual" until
181          * after we have the lock.
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
193 EXPORT_SYMBOL(kmap_high);
194
195 /**
196  * kunmap_high - map a highmem page into memory
197  * @page: &struct page to unmap
198  */
199 void 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          * A count must never go down to zero
212          * without a TLB flush!
213          */
214         need_wakeup = 0;
215         switch (--pkmap_count[nr]) {
216         case 0:
217                 BUG();
218         case 1:
219                 /*
220                  * Avoid an unnecessary wake_up() function call.
221                  * The common case is pkmap_count[] == 1, but
222                  * no waiters.
223                  * The tasks queued in the wait-queue are guarded
224                  * by both the lock in the wait-queue-head and by
225                  * the kmap_lock.  As the kmap_lock is held here,
226                  * no need for the wait-queue-head's lock.  Simply
227                  * test if the queue is empty.
228                  */
229                 need_wakeup = waitqueue_active(&pkmap_map_wait);
230         }
231         spin_unlock(&kmap_lock);
232
233         /* do wake-up, if needed, race-free outside of the spin lock */
234         if (need_wakeup)
235                 wake_up(&pkmap_map_wait);
236 }
237
238 EXPORT_SYMBOL(kunmap_high);
239 #endif
240
241 #if defined(HASHED_PAGE_VIRTUAL)
242
243 #define PA_HASH_ORDER   7
244
245 /*
246  * Describes one page->virtual association
247  */
248 struct page_address_map {
249         struct page *page;
250         void *virtual;
251         struct list_head list;
252 };
253
254 /*
255  * page_address_map freelist, allocated from page_address_maps.
256  */
257 static struct list_head page_address_pool;      /* freelist */
258 static spinlock_t pool_lock;                    /* protects page_address_pool */
259
260 /*
261  * Hash table bucket
262  */
263 static struct page_address_slot {
264         struct list_head lh;                    /* List of page_address_maps */
265         spinlock_t lock;                        /* Protect this bucket's list */
266 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
267
268 static 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  * page_address - get the mapped virtual address of a page
275  * @page: &struct page to get the virtual address of
276  *
277  * Returns the page's virtual address.
278  */
279 void *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         }
301 done:
302         spin_unlock_irqrestore(&pas->lock, flags);
303         return ret;
304 }
305
306 EXPORT_SYMBOL(page_address);
307
308 /**
309  * set_page_address - set a page's virtual address
310  * @page: &struct page to set
311  * @virtual: virtual address to use
312  */
313 void 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) {          /* Add */
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 {                /* Remove */
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         }
351 done:
352         return;
353 }
354
355 static struct page_address_map page_address_maps[LAST_PKMAP];
356
357 void __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  /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */