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