Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[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
53         return pages;
54 }
55
56 static int pkmap_count[LAST_PKMAP];
57 static unsigned int last_pkmap_nr;
58 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
59
60 pte_t * pkmap_page_table;
61
62 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
63
64 static void flush_all_zero_pkmaps(void)
65 {
66         int i;
67
68         flush_cache_kmaps();
69
70         for (i = 0; i < LAST_PKMAP; i++) {
71                 struct page *page;
72
73                 /*
74                  * zero means we don't have anything to do,
75                  * >1 means that it is still in use. Only
76                  * a count of 1 means that it is free but
77                  * needs to be unmapped
78                  */
79                 if (pkmap_count[i] != 1)
80                         continue;
81                 pkmap_count[i] = 0;
82
83                 /* sanity check */
84                 BUG_ON(pte_none(pkmap_page_table[i]));
85
86                 /*
87                  * Don't need an atomic fetch-and-clear op here;
88                  * no-one has the page mapped, and cannot get at
89                  * its virtual address (and hence PTE) without first
90                  * getting the kmap_lock (which is held here).
91                  * So no dangers, even with speculative execution.
92                  */
93                 page = pte_page(pkmap_page_table[i]);
94                 pte_clear(&init_mm, (unsigned long)page_address(page),
95                           &pkmap_page_table[i]);
96
97                 set_page_address(page, NULL);
98         }
99         flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
100 }
101
102 static inline unsigned long map_new_virtual(struct page *page)
103 {
104         unsigned long vaddr;
105         int count;
106
107 start:
108         count = LAST_PKMAP;
109         /* Find an empty entry */
110         for (;;) {
111                 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
112                 if (!last_pkmap_nr) {
113                         flush_all_zero_pkmaps();
114                         count = LAST_PKMAP;
115                 }
116                 if (!pkmap_count[last_pkmap_nr])
117                         break;  /* Found a usable entry */
118                 if (--count)
119                         continue;
120
121                 /*
122                  * Sleep for somebody else to unmap their entries
123                  */
124                 {
125                         DECLARE_WAITQUEUE(wait, current);
126
127                         __set_current_state(TASK_UNINTERRUPTIBLE);
128                         add_wait_queue(&pkmap_map_wait, &wait);
129                         spin_unlock(&kmap_lock);
130                         schedule();
131                         remove_wait_queue(&pkmap_map_wait, &wait);
132                         spin_lock(&kmap_lock);
133
134                         /* Somebody else might have mapped it while we slept */
135                         if (page_address(page))
136                                 return (unsigned long)page_address(page);
137
138                         /* Re-start */
139                         goto start;
140                 }
141         }
142         vaddr = PKMAP_ADDR(last_pkmap_nr);
143         set_pte_at(&init_mm, vaddr,
144                    &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
145
146         pkmap_count[last_pkmap_nr] = 1;
147         set_page_address(page, (void *)vaddr);
148
149         return vaddr;
150 }
151
152 void fastcall *kmap_high(struct page *page)
153 {
154         unsigned long vaddr;
155
156         /*
157          * For highmem pages, we can't trust "virtual" until
158          * after we have the lock.
159          *
160          * We cannot call this from interrupts, as it may block
161          */
162         spin_lock(&kmap_lock);
163         vaddr = (unsigned long)page_address(page);
164         if (!vaddr)
165                 vaddr = map_new_virtual(page);
166         pkmap_count[PKMAP_NR(vaddr)]++;
167         BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
168         spin_unlock(&kmap_lock);
169         return (void*) vaddr;
170 }
171
172 EXPORT_SYMBOL(kmap_high);
173
174 void fastcall kunmap_high(struct page *page)
175 {
176         unsigned long vaddr;
177         unsigned long nr;
178         int need_wakeup;
179
180         spin_lock(&kmap_lock);
181         vaddr = (unsigned long)page_address(page);
182         BUG_ON(!vaddr);
183         nr = PKMAP_NR(vaddr);
184
185         /*
186          * A count must never go down to zero
187          * without a TLB flush!
188          */
189         need_wakeup = 0;
190         switch (--pkmap_count[nr]) {
191         case 0:
192                 BUG();
193         case 1:
194                 /*
195                  * Avoid an unnecessary wake_up() function call.
196                  * The common case is pkmap_count[] == 1, but
197                  * no waiters.
198                  * The tasks queued in the wait-queue are guarded
199                  * by both the lock in the wait-queue-head and by
200                  * the kmap_lock.  As the kmap_lock is held here,
201                  * no need for the wait-queue-head's lock.  Simply
202                  * test if the queue is empty.
203                  */
204                 need_wakeup = waitqueue_active(&pkmap_map_wait);
205         }
206         spin_unlock(&kmap_lock);
207
208         /* do wake-up, if needed, race-free outside of the spin lock */
209         if (need_wakeup)
210                 wake_up(&pkmap_map_wait);
211 }
212
213 EXPORT_SYMBOL(kunmap_high);
214 #endif
215
216 #if defined(HASHED_PAGE_VIRTUAL)
217
218 #define PA_HASH_ORDER   7
219
220 /*
221  * Describes one page->virtual association
222  */
223 struct page_address_map {
224         struct page *page;
225         void *virtual;
226         struct list_head list;
227 };
228
229 /*
230  * page_address_map freelist, allocated from page_address_maps.
231  */
232 static struct list_head page_address_pool;      /* freelist */
233 static spinlock_t pool_lock;                    /* protects page_address_pool */
234
235 /*
236  * Hash table bucket
237  */
238 static struct page_address_slot {
239         struct list_head lh;                    /* List of page_address_maps */
240         spinlock_t lock;                        /* Protect this bucket's list */
241 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
242
243 static struct page_address_slot *page_slot(struct page *page)
244 {
245         return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
246 }
247
248 void *page_address(struct page *page)
249 {
250         unsigned long flags;
251         void *ret;
252         struct page_address_slot *pas;
253
254         if (!PageHighMem(page))
255                 return lowmem_page_address(page);
256
257         pas = page_slot(page);
258         ret = NULL;
259         spin_lock_irqsave(&pas->lock, flags);
260         if (!list_empty(&pas->lh)) {
261                 struct page_address_map *pam;
262
263                 list_for_each_entry(pam, &pas->lh, list) {
264                         if (pam->page == page) {
265                                 ret = pam->virtual;
266                                 goto done;
267                         }
268                 }
269         }
270 done:
271         spin_unlock_irqrestore(&pas->lock, flags);
272         return ret;
273 }
274
275 EXPORT_SYMBOL(page_address);
276
277 void set_page_address(struct page *page, void *virtual)
278 {
279         unsigned long flags;
280         struct page_address_slot *pas;
281         struct page_address_map *pam;
282
283         BUG_ON(!PageHighMem(page));
284
285         pas = page_slot(page);
286         if (virtual) {          /* Add */
287                 BUG_ON(list_empty(&page_address_pool));
288
289                 spin_lock_irqsave(&pool_lock, flags);
290                 pam = list_entry(page_address_pool.next,
291                                 struct page_address_map, list);
292                 list_del(&pam->list);
293                 spin_unlock_irqrestore(&pool_lock, flags);
294
295                 pam->page = page;
296                 pam->virtual = virtual;
297
298                 spin_lock_irqsave(&pas->lock, flags);
299                 list_add_tail(&pam->list, &pas->lh);
300                 spin_unlock_irqrestore(&pas->lock, flags);
301         } else {                /* Remove */
302                 spin_lock_irqsave(&pas->lock, flags);
303                 list_for_each_entry(pam, &pas->lh, list) {
304                         if (pam->page == page) {
305                                 list_del(&pam->list);
306                                 spin_unlock_irqrestore(&pas->lock, flags);
307                                 spin_lock_irqsave(&pool_lock, flags);
308                                 list_add_tail(&pam->list, &page_address_pool);
309                                 spin_unlock_irqrestore(&pool_lock, flags);
310                                 goto done;
311                         }
312                 }
313                 spin_unlock_irqrestore(&pas->lock, flags);
314         }
315 done:
316         return;
317 }
318
319 static struct page_address_map page_address_maps[LAST_PKMAP];
320
321 void __init page_address_init(void)
322 {
323         int i;
324
325         INIT_LIST_HEAD(&page_address_pool);
326         for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
327                 list_add(&page_address_maps[i].list, &page_address_pool);
328         for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
329                 INIT_LIST_HEAD(&page_address_htable[i].lh);
330                 spin_lock_init(&page_address_htable[i].lock);
331         }
332         spin_lock_init(&pool_lock);
333 }
334
335 #endif  /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */