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>
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.
42 unsigned long totalhigh_pages __read_mostly;
44 unsigned int nr_free_highpages (void)
47 unsigned int pages = 0;
49 for_each_online_pgdat(pgdat)
50 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
55 static int pkmap_count[LAST_PKMAP];
56 static unsigned int last_pkmap_nr;
57 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
59 pte_t * pkmap_page_table;
61 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
63 static void flush_all_zero_pkmaps(void)
69 for (i = 0; i < LAST_PKMAP; i++) {
73 * zero means we don't have anything to do,
74 * >1 means that it is still in use. Only
75 * a count of 1 means that it is free but
76 * needs to be unmapped
78 if (pkmap_count[i] != 1)
83 BUG_ON(pte_none(pkmap_page_table[i]));
86 * Don't need an atomic fetch-and-clear op here;
87 * no-one has the page mapped, and cannot get at
88 * its virtual address (and hence PTE) without first
89 * getting the kmap_lock (which is held here).
90 * So no dangers, even with speculative execution.
92 page = pte_page(pkmap_page_table[i]);
93 pte_clear(&init_mm, (unsigned long)page_address(page),
94 &pkmap_page_table[i]);
96 set_page_address(page, NULL);
98 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
101 static inline unsigned long map_new_virtual(struct page *page)
108 /* Find an empty entry */
110 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
111 if (!last_pkmap_nr) {
112 flush_all_zero_pkmaps();
115 if (!pkmap_count[last_pkmap_nr])
116 break; /* Found a usable entry */
121 * Sleep for somebody else to unmap their entries
124 DECLARE_WAITQUEUE(wait, current);
126 __set_current_state(TASK_UNINTERRUPTIBLE);
127 add_wait_queue(&pkmap_map_wait, &wait);
128 spin_unlock(&kmap_lock);
130 remove_wait_queue(&pkmap_map_wait, &wait);
131 spin_lock(&kmap_lock);
133 /* Somebody else might have mapped it while we slept */
134 if (page_address(page))
135 return (unsigned long)page_address(page);
141 vaddr = PKMAP_ADDR(last_pkmap_nr);
142 set_pte_at(&init_mm, vaddr,
143 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
145 pkmap_count[last_pkmap_nr] = 1;
146 set_page_address(page, (void *)vaddr);
152 void kmap_flush_unused(void)
154 spin_lock(&kmap_lock);
155 flush_all_zero_pkmaps();
156 spin_unlock(&kmap_lock);
159 EXPORT_SYMBOL(kmap_flush_unused);
162 void fastcall *kmap_high(struct page *page)
167 * For highmem pages, we can't trust "virtual" until
168 * after we have the lock.
170 * We cannot call this from interrupts, as it may block
172 spin_lock(&kmap_lock);
173 vaddr = (unsigned long)page_address(page);
175 vaddr = map_new_virtual(page);
176 pkmap_count[PKMAP_NR(vaddr)]++;
177 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
178 spin_unlock(&kmap_lock);
179 return (void*) vaddr;
182 EXPORT_SYMBOL(kmap_high);
184 void fastcall kunmap_high(struct page *page)
190 spin_lock(&kmap_lock);
191 vaddr = (unsigned long)page_address(page);
193 nr = PKMAP_NR(vaddr);
196 * A count must never go down to zero
197 * without a TLB flush!
200 switch (--pkmap_count[nr]) {
205 * Avoid an unnecessary wake_up() function call.
206 * The common case is pkmap_count[] == 1, but
208 * The tasks queued in the wait-queue are guarded
209 * by both the lock in the wait-queue-head and by
210 * the kmap_lock. As the kmap_lock is held here,
211 * no need for the wait-queue-head's lock. Simply
212 * test if the queue is empty.
214 need_wakeup = waitqueue_active(&pkmap_map_wait);
216 spin_unlock(&kmap_lock);
218 /* do wake-up, if needed, race-free outside of the spin lock */
220 wake_up(&pkmap_map_wait);
223 EXPORT_SYMBOL(kunmap_high);
226 #if defined(HASHED_PAGE_VIRTUAL)
228 #define PA_HASH_ORDER 7
231 * Describes one page->virtual association
233 struct page_address_map {
236 struct list_head list;
240 * page_address_map freelist, allocated from page_address_maps.
242 static struct list_head page_address_pool; /* freelist */
243 static spinlock_t pool_lock; /* protects page_address_pool */
248 static struct page_address_slot {
249 struct list_head lh; /* List of page_address_maps */
250 spinlock_t lock; /* Protect this bucket's list */
251 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
253 static struct page_address_slot *page_slot(struct page *page)
255 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
258 void *page_address(struct page *page)
262 struct page_address_slot *pas;
264 if (!PageHighMem(page))
265 return lowmem_page_address(page);
267 pas = page_slot(page);
269 spin_lock_irqsave(&pas->lock, flags);
270 if (!list_empty(&pas->lh)) {
271 struct page_address_map *pam;
273 list_for_each_entry(pam, &pas->lh, list) {
274 if (pam->page == page) {
281 spin_unlock_irqrestore(&pas->lock, flags);
285 EXPORT_SYMBOL(page_address);
287 void set_page_address(struct page *page, void *virtual)
290 struct page_address_slot *pas;
291 struct page_address_map *pam;
293 BUG_ON(!PageHighMem(page));
295 pas = page_slot(page);
296 if (virtual) { /* Add */
297 BUG_ON(list_empty(&page_address_pool));
299 spin_lock_irqsave(&pool_lock, flags);
300 pam = list_entry(page_address_pool.next,
301 struct page_address_map, list);
302 list_del(&pam->list);
303 spin_unlock_irqrestore(&pool_lock, flags);
306 pam->virtual = virtual;
308 spin_lock_irqsave(&pas->lock, flags);
309 list_add_tail(&pam->list, &pas->lh);
310 spin_unlock_irqrestore(&pas->lock, flags);
311 } else { /* Remove */
312 spin_lock_irqsave(&pas->lock, flags);
313 list_for_each_entry(pam, &pas->lh, list) {
314 if (pam->page == page) {
315 list_del(&pam->list);
316 spin_unlock_irqrestore(&pas->lock, flags);
317 spin_lock_irqsave(&pool_lock, flags);
318 list_add_tail(&pam->list, &page_address_pool);
319 spin_unlock_irqrestore(&pool_lock, flags);
323 spin_unlock_irqrestore(&pas->lock, flags);
329 static struct page_address_map page_address_maps[LAST_PKMAP];
331 void __init page_address_init(void)
335 INIT_LIST_HEAD(&page_address_pool);
336 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
337 list_add(&page_address_maps[i].list, &page_address_pool);
338 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
339 INIT_LIST_HEAD(&page_address_htable[i].lh);
340 spin_lock_init(&page_address_htable[i].lock);
342 spin_lock_init(&pool_lock);
345 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */