blob: e16e1523b688680572513b16ef0e49d5ac0314f0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Jens Axboe2056a782006-03-23 20:00:26 +010029#include <linux/blktrace_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/tlbflush.h>
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
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
Al Viro260b2362005-10-21 03:22:44 -040041
Christoph Lameterc1f60a52006-09-25 23:31:11 -070042unsigned long totalhigh_pages __read_mostly;
David S. Millerdb7a94d2008-07-19 22:39:46 -070043EXPORT_SYMBOL(totalhigh_pages);
Christoph Lameterc1f60a52006-09-25 23:31:11 -070044
45unsigned int nr_free_highpages (void)
46{
47 pg_data_t *pgdat;
48 unsigned int pages = 0;
49
Mel Gorman2a1e2742007-07-17 04:03:12 -070050 for_each_online_pgdat(pgdat) {
Christoph Lameterd23ad422007-02-10 01:43:02 -080051 pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
52 NR_FREE_PAGES);
Mel Gorman2a1e2742007-07-17 04:03:12 -070053 if (zone_movable_is_highmem())
54 pages += zone_page_state(
55 &pgdat->node_zones[ZONE_MOVABLE],
56 NR_FREE_PAGES);
57 }
Christoph Lameterc1f60a52006-09-25 23:31:11 -070058
59 return pages;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int pkmap_count[LAST_PKMAP];
63static unsigned int last_pkmap_nr;
64static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
65
66pte_t * pkmap_page_table;
67
68static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
69
70static void flush_all_zero_pkmaps(void)
71{
72 int i;
73
74 flush_cache_kmaps();
75
76 for (i = 0; i < LAST_PKMAP; i++) {
77 struct page *page;
78
79 /*
80 * zero means we don't have anything to do,
81 * >1 means that it is still in use. Only
82 * a count of 1 means that it is free but
83 * needs to be unmapped
84 */
85 if (pkmap_count[i] != 1)
86 continue;
87 pkmap_count[i] = 0;
88
89 /* sanity check */
Eric Sesterhenn75babca2006-04-02 13:47:35 +020090 BUG_ON(pte_none(pkmap_page_table[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /*
93 * Don't need an atomic fetch-and-clear op here;
94 * no-one has the page mapped, and cannot get at
95 * its virtual address (and hence PTE) without first
96 * getting the kmap_lock (which is held here).
97 * So no dangers, even with speculative execution.
98 */
99 page = pte_page(pkmap_page_table[i]);
100 pte_clear(&init_mm, (unsigned long)page_address(page),
101 &pkmap_page_table[i]);
102
103 set_page_address(page, NULL);
104 }
105 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
106}
107
Randy Dunlap77f60782008-03-19 17:00:42 -0700108/**
109 * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
110 */
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +0200111void kmap_flush_unused(void)
112{
113 spin_lock(&kmap_lock);
114 flush_all_zero_pkmaps();
115 spin_unlock(&kmap_lock);
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static inline unsigned long map_new_virtual(struct page *page)
119{
120 unsigned long vaddr;
121 int count;
122
123start:
124 count = LAST_PKMAP;
125 /* Find an empty entry */
126 for (;;) {
127 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
128 if (!last_pkmap_nr) {
129 flush_all_zero_pkmaps();
130 count = LAST_PKMAP;
131 }
132 if (!pkmap_count[last_pkmap_nr])
133 break; /* Found a usable entry */
134 if (--count)
135 continue;
136
137 /*
138 * Sleep for somebody else to unmap their entries
139 */
140 {
141 DECLARE_WAITQUEUE(wait, current);
142
143 __set_current_state(TASK_UNINTERRUPTIBLE);
144 add_wait_queue(&pkmap_map_wait, &wait);
145 spin_unlock(&kmap_lock);
146 schedule();
147 remove_wait_queue(&pkmap_map_wait, &wait);
148 spin_lock(&kmap_lock);
149
150 /* Somebody else might have mapped it while we slept */
151 if (page_address(page))
152 return (unsigned long)page_address(page);
153
154 /* Re-start */
155 goto start;
156 }
157 }
158 vaddr = PKMAP_ADDR(last_pkmap_nr);
159 set_pte_at(&init_mm, vaddr,
160 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
161
162 pkmap_count[last_pkmap_nr] = 1;
163 set_page_address(page, (void *)vaddr);
164
165 return vaddr;
166}
167
Randy Dunlap77f60782008-03-19 17:00:42 -0700168/**
169 * kmap_high - map a highmem page into memory
170 * @page: &struct page to map
171 *
172 * Returns the page's virtual memory address.
173 *
174 * We cannot call this from interrupts, as it may block.
175 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800176void *kmap_high(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 unsigned long vaddr;
179
180 /*
181 * For highmem pages, we can't trust "virtual" until
182 * after we have the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
184 spin_lock(&kmap_lock);
185 vaddr = (unsigned long)page_address(page);
186 if (!vaddr)
187 vaddr = map_new_virtual(page);
188 pkmap_count[PKMAP_NR(vaddr)]++;
Eric Sesterhenn75babca2006-04-02 13:47:35 +0200189 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 spin_unlock(&kmap_lock);
191 return (void*) vaddr;
192}
193
194EXPORT_SYMBOL(kmap_high);
195
Randy Dunlap77f60782008-03-19 17:00:42 -0700196/**
197 * kunmap_high - map a highmem page into memory
198 * @page: &struct page to unmap
199 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800200void kunmap_high(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 unsigned long vaddr;
203 unsigned long nr;
204 int need_wakeup;
205
206 spin_lock(&kmap_lock);
207 vaddr = (unsigned long)page_address(page);
Eric Sesterhenn75babca2006-04-02 13:47:35 +0200208 BUG_ON(!vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 nr = PKMAP_NR(vaddr);
210
211 /*
212 * A count must never go down to zero
213 * without a TLB flush!
214 */
215 need_wakeup = 0;
216 switch (--pkmap_count[nr]) {
217 case 0:
218 BUG();
219 case 1:
220 /*
221 * Avoid an unnecessary wake_up() function call.
222 * The common case is pkmap_count[] == 1, but
223 * no waiters.
224 * The tasks queued in the wait-queue are guarded
225 * by both the lock in the wait-queue-head and by
226 * the kmap_lock. As the kmap_lock is held here,
227 * no need for the wait-queue-head's lock. Simply
228 * test if the queue is empty.
229 */
230 need_wakeup = waitqueue_active(&pkmap_map_wait);
231 }
232 spin_unlock(&kmap_lock);
233
234 /* do wake-up, if needed, race-free outside of the spin lock */
235 if (need_wakeup)
236 wake_up(&pkmap_map_wait);
237}
238
239EXPORT_SYMBOL(kunmap_high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#endif
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#if defined(HASHED_PAGE_VIRTUAL)
243
244#define PA_HASH_ORDER 7
245
246/*
247 * Describes one page->virtual association
248 */
249struct page_address_map {
250 struct page *page;
251 void *virtual;
252 struct list_head list;
253};
254
255/*
256 * page_address_map freelist, allocated from page_address_maps.
257 */
258static struct list_head page_address_pool; /* freelist */
259static spinlock_t pool_lock; /* protects page_address_pool */
260
261/*
262 * Hash table bucket
263 */
264static struct page_address_slot {
265 struct list_head lh; /* List of page_address_maps */
266 spinlock_t lock; /* Protect this bucket's list */
267} ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
268
269static struct page_address_slot *page_slot(struct page *page)
270{
271 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
272}
273
Randy Dunlap77f60782008-03-19 17:00:42 -0700274/**
275 * page_address - get the mapped virtual address of a page
276 * @page: &struct page to get the virtual address of
277 *
278 * Returns the page's virtual address.
279 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280void *page_address(struct page *page)
281{
282 unsigned long flags;
283 void *ret;
284 struct page_address_slot *pas;
285
286 if (!PageHighMem(page))
287 return lowmem_page_address(page);
288
289 pas = page_slot(page);
290 ret = NULL;
291 spin_lock_irqsave(&pas->lock, flags);
292 if (!list_empty(&pas->lh)) {
293 struct page_address_map *pam;
294
295 list_for_each_entry(pam, &pas->lh, list) {
296 if (pam->page == page) {
297 ret = pam->virtual;
298 goto done;
299 }
300 }
301 }
302done:
303 spin_unlock_irqrestore(&pas->lock, flags);
304 return ret;
305}
306
307EXPORT_SYMBOL(page_address);
308
Randy Dunlap77f60782008-03-19 17:00:42 -0700309/**
310 * set_page_address - set a page's virtual address
311 * @page: &struct page to set
312 * @virtual: virtual address to use
313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314void set_page_address(struct page *page, void *virtual)
315{
316 unsigned long flags;
317 struct page_address_slot *pas;
318 struct page_address_map *pam;
319
320 BUG_ON(!PageHighMem(page));
321
322 pas = page_slot(page);
323 if (virtual) { /* Add */
324 BUG_ON(list_empty(&page_address_pool));
325
326 spin_lock_irqsave(&pool_lock, flags);
327 pam = list_entry(page_address_pool.next,
328 struct page_address_map, list);
329 list_del(&pam->list);
330 spin_unlock_irqrestore(&pool_lock, flags);
331
332 pam->page = page;
333 pam->virtual = virtual;
334
335 spin_lock_irqsave(&pas->lock, flags);
336 list_add_tail(&pam->list, &pas->lh);
337 spin_unlock_irqrestore(&pas->lock, flags);
338 } else { /* Remove */
339 spin_lock_irqsave(&pas->lock, flags);
340 list_for_each_entry(pam, &pas->lh, list) {
341 if (pam->page == page) {
342 list_del(&pam->list);
343 spin_unlock_irqrestore(&pas->lock, flags);
344 spin_lock_irqsave(&pool_lock, flags);
345 list_add_tail(&pam->list, &page_address_pool);
346 spin_unlock_irqrestore(&pool_lock, flags);
347 goto done;
348 }
349 }
350 spin_unlock_irqrestore(&pas->lock, flags);
351 }
352done:
353 return;
354}
355
356static struct page_address_map page_address_maps[LAST_PKMAP];
357
358void __init page_address_init(void)
359{
360 int i;
361
362 INIT_LIST_HEAD(&page_address_pool);
363 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
364 list_add(&page_address_maps[i].list, &page_address_pool);
365 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
366 INIT_LIST_HEAD(&page_address_htable[i].lh);
367 spin_lock_init(&page_address_htable[i].lock);
368 }
369 spin_lock_init(&pool_lock);
370}
371
372#endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */