[NetLabel]: consolidate the struct socket/sock handling to just struct sock
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / highmem.c
blobbe8f8d36a8b9b6919f4d1c36e1b3b3e973b242f1
1 /*
2 * High memory handling common code and variables.
4 * (C) 1999 Andrea Arcangeli, SuSE GmbH, andrea@suse.de
5 * Gerhard Wichert, Siemens AG, Gerhard.Wichert@pdb.siemens.de
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.
12 * Rewrote high memory support to move the page cache into
13 * high memory. Implemented permanent (schedulable) kmaps
14 * based on Linus' idea.
16 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
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>
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.
40 #ifdef CONFIG_HIGHMEM
42 unsigned long totalhigh_pages __read_mostly;
44 unsigned int nr_free_highpages (void)
46 pg_data_t *pgdat;
47 unsigned int pages = 0;
49 for_each_online_pgdat(pgdat)
50 pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
51 NR_FREE_PAGES);
53 return pages;
56 static int pkmap_count[LAST_PKMAP];
57 static unsigned int last_pkmap_nr;
58 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
60 pte_t * pkmap_page_table;
62 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
64 static void flush_all_zero_pkmaps(void)
66 int i;
68 flush_cache_kmaps();
70 for (i = 0; i < LAST_PKMAP; i++) {
71 struct page *page;
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
79 if (pkmap_count[i] != 1)
80 continue;
81 pkmap_count[i] = 0;
83 /* sanity check */
84 BUG_ON(pte_none(pkmap_page_table[i]));
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.
93 page = pte_page(pkmap_page_table[i]);
94 pte_clear(&init_mm, (unsigned long)page_address(page),
95 &pkmap_page_table[i]);
97 set_page_address(page, NULL);
99 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
102 /* Flush all unused kmap mappings in order to remove stray
103 mappings. */
104 void kmap_flush_unused(void)
106 spin_lock(&kmap_lock);
107 flush_all_zero_pkmaps();
108 spin_unlock(&kmap_lock);
111 static inline unsigned long map_new_virtual(struct page *page)
113 unsigned long vaddr;
114 int count;
116 start:
117 count = LAST_PKMAP;
118 /* Find an empty entry */
119 for (;;) {
120 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
121 if (!last_pkmap_nr) {
122 flush_all_zero_pkmaps();
123 count = LAST_PKMAP;
125 if (!pkmap_count[last_pkmap_nr])
126 break; /* Found a usable entry */
127 if (--count)
128 continue;
131 * Sleep for somebody else to unmap their entries
134 DECLARE_WAITQUEUE(wait, current);
136 __set_current_state(TASK_UNINTERRUPTIBLE);
137 add_wait_queue(&pkmap_map_wait, &wait);
138 spin_unlock(&kmap_lock);
139 schedule();
140 remove_wait_queue(&pkmap_map_wait, &wait);
141 spin_lock(&kmap_lock);
143 /* Somebody else might have mapped it while we slept */
144 if (page_address(page))
145 return (unsigned long)page_address(page);
147 /* Re-start */
148 goto start;
151 vaddr = PKMAP_ADDR(last_pkmap_nr);
152 set_pte_at(&init_mm, vaddr,
153 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
155 pkmap_count[last_pkmap_nr] = 1;
156 set_page_address(page, (void *)vaddr);
158 return vaddr;
161 void fastcall *kmap_high(struct page *page)
163 unsigned long vaddr;
166 * For highmem pages, we can't trust "virtual" until
167 * after we have the lock.
169 * We cannot call this from interrupts, as it may block
171 spin_lock(&kmap_lock);
172 vaddr = (unsigned long)page_address(page);
173 if (!vaddr)
174 vaddr = map_new_virtual(page);
175 pkmap_count[PKMAP_NR(vaddr)]++;
176 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
177 spin_unlock(&kmap_lock);
178 return (void*) vaddr;
181 EXPORT_SYMBOL(kmap_high);
183 void fastcall kunmap_high(struct page *page)
185 unsigned long vaddr;
186 unsigned long nr;
187 int need_wakeup;
189 spin_lock(&kmap_lock);
190 vaddr = (unsigned long)page_address(page);
191 BUG_ON(!vaddr);
192 nr = PKMAP_NR(vaddr);
195 * A count must never go down to zero
196 * without a TLB flush!
198 need_wakeup = 0;
199 switch (--pkmap_count[nr]) {
200 case 0:
201 BUG();
202 case 1:
204 * Avoid an unnecessary wake_up() function call.
205 * The common case is pkmap_count[] == 1, but
206 * no waiters.
207 * The tasks queued in the wait-queue are guarded
208 * by both the lock in the wait-queue-head and by
209 * the kmap_lock. As the kmap_lock is held here,
210 * no need for the wait-queue-head's lock. Simply
211 * test if the queue is empty.
213 need_wakeup = waitqueue_active(&pkmap_map_wait);
215 spin_unlock(&kmap_lock);
217 /* do wake-up, if needed, race-free outside of the spin lock */
218 if (need_wakeup)
219 wake_up(&pkmap_map_wait);
222 EXPORT_SYMBOL(kunmap_high);
223 #endif
225 #if defined(HASHED_PAGE_VIRTUAL)
227 #define PA_HASH_ORDER 7
230 * Describes one page->virtual association
232 struct page_address_map {
233 struct page *page;
234 void *virtual;
235 struct list_head list;
239 * page_address_map freelist, allocated from page_address_maps.
241 static struct list_head page_address_pool; /* freelist */
242 static spinlock_t pool_lock; /* protects page_address_pool */
245 * Hash table bucket
247 static struct page_address_slot {
248 struct list_head lh; /* List of page_address_maps */
249 spinlock_t lock; /* Protect this bucket's list */
250 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
252 static struct page_address_slot *page_slot(struct page *page)
254 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
257 void *page_address(struct page *page)
259 unsigned long flags;
260 void *ret;
261 struct page_address_slot *pas;
263 if (!PageHighMem(page))
264 return lowmem_page_address(page);
266 pas = page_slot(page);
267 ret = NULL;
268 spin_lock_irqsave(&pas->lock, flags);
269 if (!list_empty(&pas->lh)) {
270 struct page_address_map *pam;
272 list_for_each_entry(pam, &pas->lh, list) {
273 if (pam->page == page) {
274 ret = pam->virtual;
275 goto done;
279 done:
280 spin_unlock_irqrestore(&pas->lock, flags);
281 return ret;
284 EXPORT_SYMBOL(page_address);
286 void set_page_address(struct page *page, void *virtual)
288 unsigned long flags;
289 struct page_address_slot *pas;
290 struct page_address_map *pam;
292 BUG_ON(!PageHighMem(page));
294 pas = page_slot(page);
295 if (virtual) { /* Add */
296 BUG_ON(list_empty(&page_address_pool));
298 spin_lock_irqsave(&pool_lock, flags);
299 pam = list_entry(page_address_pool.next,
300 struct page_address_map, list);
301 list_del(&pam->list);
302 spin_unlock_irqrestore(&pool_lock, flags);
304 pam->page = page;
305 pam->virtual = virtual;
307 spin_lock_irqsave(&pas->lock, flags);
308 list_add_tail(&pam->list, &pas->lh);
309 spin_unlock_irqrestore(&pas->lock, flags);
310 } else { /* Remove */
311 spin_lock_irqsave(&pas->lock, flags);
312 list_for_each_entry(pam, &pas->lh, list) {
313 if (pam->page == page) {
314 list_del(&pam->list);
315 spin_unlock_irqrestore(&pas->lock, flags);
316 spin_lock_irqsave(&pool_lock, flags);
317 list_add_tail(&pam->list, &page_address_pool);
318 spin_unlock_irqrestore(&pool_lock, flags);
319 goto done;
322 spin_unlock_irqrestore(&pas->lock, flags);
324 done:
325 return;
328 static struct page_address_map page_address_maps[LAST_PKMAP];
330 void __init page_address_init(void)
332 int i;
334 INIT_LIST_HEAD(&page_address_pool);
335 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
336 list_add(&page_address_maps[i].list, &page_address_pool);
337 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
338 INIT_LIST_HEAD(&page_address_htable[i].lh);
339 spin_lock_init(&page_address_htable[i].lock);
341 spin_lock_init(&pool_lock);
344 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */