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 += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
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)
70 for (i = 0; i < LAST_PKMAP; i++) {
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)
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 static inline unsigned long map_new_virtual(struct page *page)
109 /* Find an empty entry */
111 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
112 if (!last_pkmap_nr) {
113 flush_all_zero_pkmaps();
116 if (!pkmap_count[last_pkmap_nr])
117 break; /* Found a usable entry */
122 * Sleep for somebody else to unmap their entries
125 DECLARE_WAITQUEUE(wait, current);
127 __set_current_state(TASK_UNINTERRUPTIBLE);
128 add_wait_queue(&pkmap_map_wait, &wait);
129 spin_unlock(&kmap_lock);
131 remove_wait_queue(&pkmap_map_wait, &wait);
132 spin_lock(&kmap_lock);
134 /* Somebody else might have mapped it while we slept */
135 if (page_address(page))
136 return (unsigned long)page_address(page);
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));
146 pkmap_count[last_pkmap_nr] = 1;
147 set_page_address(page, (void *)vaddr);
152 void fastcall *kmap_high(struct page *page)
157 * For highmem pages, we can't trust "virtual" until
158 * after we have the lock.
160 * We cannot call this from interrupts, as it may block
162 spin_lock(&kmap_lock);
163 vaddr = (unsigned long)page_address(page);
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;
172 EXPORT_SYMBOL(kmap_high);
174 void fastcall kunmap_high(struct page *page)
180 spin_lock(&kmap_lock);
181 vaddr = (unsigned long)page_address(page);
183 nr = PKMAP_NR(vaddr);
186 * A count must never go down to zero
187 * without a TLB flush!
190 switch (--pkmap_count[nr]) {
195 * Avoid an unnecessary wake_up() function call.
196 * The common case is pkmap_count[] == 1, but
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.
204 need_wakeup = waitqueue_active(&pkmap_map_wait);
206 spin_unlock(&kmap_lock);
208 /* do wake-up, if needed, race-free outside of the spin lock */
210 wake_up(&pkmap_map_wait);
213 EXPORT_SYMBOL(kunmap_high);
216 #if defined(HASHED_PAGE_VIRTUAL)
218 #define PA_HASH_ORDER 7
221 * Describes one page->virtual association
223 struct page_address_map {
226 struct list_head list;
230 * page_address_map freelist, allocated from page_address_maps.
232 static struct list_head page_address_pool; /* freelist */
233 static spinlock_t pool_lock; /* protects page_address_pool */
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];
243 static struct page_address_slot *page_slot(struct page *page)
245 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
248 void *page_address(struct page *page)
252 struct page_address_slot *pas;
254 if (!PageHighMem(page))
255 return lowmem_page_address(page);
257 pas = page_slot(page);
259 spin_lock_irqsave(&pas->lock, flags);
260 if (!list_empty(&pas->lh)) {
261 struct page_address_map *pam;
263 list_for_each_entry(pam, &pas->lh, list) {
264 if (pam->page == page) {
271 spin_unlock_irqrestore(&pas->lock, flags);
275 EXPORT_SYMBOL(page_address);
277 void set_page_address(struct page *page, void *virtual)
280 struct page_address_slot *pas;
281 struct page_address_map *pam;
283 BUG_ON(!PageHighMem(page));
285 pas = page_slot(page);
286 if (virtual) { /* Add */
287 BUG_ON(list_empty(&page_address_pool));
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);
296 pam->virtual = virtual;
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);
313 spin_unlock_irqrestore(&pas->lock, flags);
319 static struct page_address_map page_address_maps[LAST_PKMAP];
321 void __init page_address_init(void)
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);
332 spin_lock_init(&pool_lock);
335 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */