blob: b1c7d434b24c8945c935dc71be804a93be0f91bc [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>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040020#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Jason Wesseleac790052010-08-05 09:22:24 -050029#include <linux/kgdb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/tlbflush.h>
31
Peter Zijlstra5f88ad92011-07-28 10:43:51 +020032#ifndef CONFIG_PREEMPT_RT_FULL
Peter Zijlstraa8e23a22010-10-27 15:32:57 -070033#if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
34DEFINE_PER_CPU(int, __kmap_atomic_idx);
35#endif
Peter Zijlstra5f88ad92011-07-28 10:43:51 +020036#endif
Peter Zijlstraa8e23a22010-10-27 15:32:57 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * Virtual_count is not a pure "count".
40 * 0 means that it is not mapped, and has not been mapped
41 * since a TLB flush - it is usable.
42 * 1 means that there are no users, but it has been mapped
43 * since the last TLB flush - so we can't use it.
44 * n means that there are (n-1) current users of it.
45 */
46#ifdef CONFIG_HIGHMEM
Al Viro260b2362005-10-21 03:22:44 -040047
Christoph Lameterc1f60a52006-09-25 23:31:11 -070048unsigned long totalhigh_pages __read_mostly;
David S. Millerdb7a94d2008-07-19 22:39:46 -070049EXPORT_SYMBOL(totalhigh_pages);
Christoph Lameterc1f60a52006-09-25 23:31:11 -070050
Peter Zijlstra5f88ad92011-07-28 10:43:51 +020051#ifndef CONFIG_PREEMPT_RT_FULL
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070052EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
Peter Zijlstra5f88ad92011-07-28 10:43:51 +020053#endif
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070054
Christoph Lameterc1f60a52006-09-25 23:31:11 -070055unsigned int nr_free_highpages (void)
56{
57 pg_data_t *pgdat;
58 unsigned int pages = 0;
59
Mel Gorman2a1e2742007-07-17 04:03:12 -070060 for_each_online_pgdat(pgdat) {
Christoph Lameterd23ad422007-02-10 01:43:02 -080061 pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
62 NR_FREE_PAGES);
Mel Gorman2a1e2742007-07-17 04:03:12 -070063 if (zone_movable_is_highmem())
64 pages += zone_page_state(
65 &pgdat->node_zones[ZONE_MOVABLE],
66 NR_FREE_PAGES);
67 }
Christoph Lameterc1f60a52006-09-25 23:31:11 -070068
69 return pages;
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int pkmap_count[LAST_PKMAP];
73static unsigned int last_pkmap_nr;
74static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
75
76pte_t * pkmap_page_table;
77
78static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
79
Nicolas Pitre3297e762009-03-04 22:49:41 -050080/*
81 * Most architectures have no use for kmap_high_get(), so let's abstract
82 * the disabling of IRQ out of the locking in that case to save on a
83 * potential useless overhead.
84 */
85#ifdef ARCH_NEEDS_KMAP_HIGH_GET
86#define lock_kmap() spin_lock_irq(&kmap_lock)
87#define unlock_kmap() spin_unlock_irq(&kmap_lock)
88#define lock_kmap_any(flags) spin_lock_irqsave(&kmap_lock, flags)
89#define unlock_kmap_any(flags) spin_unlock_irqrestore(&kmap_lock, flags)
90#else
91#define lock_kmap() spin_lock(&kmap_lock)
92#define unlock_kmap() spin_unlock(&kmap_lock)
93#define lock_kmap_any(flags) \
94 do { spin_lock(&kmap_lock); (void)(flags); } while (0)
95#define unlock_kmap_any(flags) \
96 do { spin_unlock(&kmap_lock); (void)(flags); } while (0)
97#endif
98
Mel Gorman5a178112012-07-31 16:45:02 -070099struct page *kmap_to_page(void *vaddr)
100{
101 unsigned long addr = (unsigned long)vaddr;
102
Will Deacon498c2282012-11-16 14:15:00 -0800103 if (addr >= PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP)) {
Joonsoo Kim4de22c02012-12-11 16:01:17 -0800104 int i = PKMAP_NR(addr);
Mel Gorman5a178112012-07-31 16:45:02 -0700105 return pte_page(pkmap_page_table[i]);
106 }
107
108 return virt_to_page(addr);
109}
Will Deaconf0263d22012-10-19 14:03:31 +0100110EXPORT_SYMBOL(kmap_to_page);
Mel Gorman5a178112012-07-31 16:45:02 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static void flush_all_zero_pkmaps(void)
113{
114 int i;
Nick Piggin5843d9a2008-08-01 03:15:21 +0200115 int need_flush = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 flush_cache_kmaps();
118
119 for (i = 0; i < LAST_PKMAP; i++) {
120 struct page *page;
121
122 /*
123 * zero means we don't have anything to do,
124 * >1 means that it is still in use. Only
125 * a count of 1 means that it is free but
126 * needs to be unmapped
127 */
128 if (pkmap_count[i] != 1)
129 continue;
130 pkmap_count[i] = 0;
131
132 /* sanity check */
Eric Sesterhenn75babca2006-04-02 13:47:35 +0200133 BUG_ON(pte_none(pkmap_page_table[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 /*
136 * Don't need an atomic fetch-and-clear op here;
137 * no-one has the page mapped, and cannot get at
138 * its virtual address (and hence PTE) without first
139 * getting the kmap_lock (which is held here).
140 * So no dangers, even with speculative execution.
141 */
142 page = pte_page(pkmap_page_table[i]);
Joonsoo Kimeb2db432012-12-11 16:01:24 -0800143 pte_clear(&init_mm, PKMAP_ADDR(i), &pkmap_page_table[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 set_page_address(page, NULL);
Nick Piggin5843d9a2008-08-01 03:15:21 +0200146 need_flush = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Nick Piggin5843d9a2008-08-01 03:15:21 +0200148 if (need_flush)
149 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Randy Dunlap77f60782008-03-19 17:00:42 -0700152/**
153 * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
154 */
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +0200155void kmap_flush_unused(void)
156{
Nicolas Pitre3297e762009-03-04 22:49:41 -0500157 lock_kmap();
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +0200158 flush_all_zero_pkmaps();
Nicolas Pitre3297e762009-03-04 22:49:41 -0500159 unlock_kmap();
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +0200160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static inline unsigned long map_new_virtual(struct page *page)
163{
164 unsigned long vaddr;
165 int count;
166
167start:
168 count = LAST_PKMAP;
169 /* Find an empty entry */
170 for (;;) {
171 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
172 if (!last_pkmap_nr) {
173 flush_all_zero_pkmaps();
174 count = LAST_PKMAP;
175 }
176 if (!pkmap_count[last_pkmap_nr])
177 break; /* Found a usable entry */
178 if (--count)
179 continue;
180
181 /*
182 * Sleep for somebody else to unmap their entries
183 */
184 {
185 DECLARE_WAITQUEUE(wait, current);
186
187 __set_current_state(TASK_UNINTERRUPTIBLE);
188 add_wait_queue(&pkmap_map_wait, &wait);
Nicolas Pitre3297e762009-03-04 22:49:41 -0500189 unlock_kmap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 schedule();
191 remove_wait_queue(&pkmap_map_wait, &wait);
Nicolas Pitre3297e762009-03-04 22:49:41 -0500192 lock_kmap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /* Somebody else might have mapped it while we slept */
195 if (page_address(page))
196 return (unsigned long)page_address(page);
197
198 /* Re-start */
199 goto start;
200 }
201 }
202 vaddr = PKMAP_ADDR(last_pkmap_nr);
203 set_pte_at(&init_mm, vaddr,
204 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
205
206 pkmap_count[last_pkmap_nr] = 1;
207 set_page_address(page, (void *)vaddr);
208
209 return vaddr;
210}
211
Randy Dunlap77f60782008-03-19 17:00:42 -0700212/**
213 * kmap_high - map a highmem page into memory
214 * @page: &struct page to map
215 *
216 * Returns the page's virtual memory address.
217 *
218 * We cannot call this from interrupts, as it may block.
219 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800220void *kmap_high(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 unsigned long vaddr;
223
224 /*
225 * For highmem pages, we can't trust "virtual" until
226 * after we have the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
Nicolas Pitre3297e762009-03-04 22:49:41 -0500228 lock_kmap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 vaddr = (unsigned long)page_address(page);
230 if (!vaddr)
231 vaddr = map_new_virtual(page);
232 pkmap_count[PKMAP_NR(vaddr)]++;
Eric Sesterhenn75babca2006-04-02 13:47:35 +0200233 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
Nicolas Pitre3297e762009-03-04 22:49:41 -0500234 unlock_kmap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return (void*) vaddr;
236}
237
238EXPORT_SYMBOL(kmap_high);
239
Nicolas Pitre3297e762009-03-04 22:49:41 -0500240#ifdef ARCH_NEEDS_KMAP_HIGH_GET
241/**
242 * kmap_high_get - pin a highmem page into memory
243 * @page: &struct page to pin
244 *
245 * Returns the page's current virtual memory address, or NULL if no mapping
Uwe Kleine-König5e39df52010-01-25 21:38:09 +0100246 * exists. If and only if a non null address is returned then a
Nicolas Pitre3297e762009-03-04 22:49:41 -0500247 * matching call to kunmap_high() is necessary.
248 *
249 * This can be called from any context.
250 */
251void *kmap_high_get(struct page *page)
252{
253 unsigned long vaddr, flags;
254
255 lock_kmap_any(flags);
256 vaddr = (unsigned long)page_address(page);
257 if (vaddr) {
258 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 1);
259 pkmap_count[PKMAP_NR(vaddr)]++;
260 }
261 unlock_kmap_any(flags);
262 return (void*) vaddr;
263}
264#endif
265
Randy Dunlap77f60782008-03-19 17:00:42 -0700266/**
Li Haifeng4e9dc5d2011-10-31 17:09:09 -0700267 * kunmap_high - unmap a highmem page into memory
Randy Dunlap77f60782008-03-19 17:00:42 -0700268 * @page: &struct page to unmap
Nicolas Pitre3297e762009-03-04 22:49:41 -0500269 *
270 * If ARCH_NEEDS_KMAP_HIGH_GET is not defined then this may be called
271 * only from user context.
Randy Dunlap77f60782008-03-19 17:00:42 -0700272 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800273void kunmap_high(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 unsigned long vaddr;
276 unsigned long nr;
Nicolas Pitre3297e762009-03-04 22:49:41 -0500277 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int need_wakeup;
279
Nicolas Pitre3297e762009-03-04 22:49:41 -0500280 lock_kmap_any(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 vaddr = (unsigned long)page_address(page);
Eric Sesterhenn75babca2006-04-02 13:47:35 +0200282 BUG_ON(!vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 nr = PKMAP_NR(vaddr);
284
285 /*
286 * A count must never go down to zero
287 * without a TLB flush!
288 */
289 need_wakeup = 0;
290 switch (--pkmap_count[nr]) {
291 case 0:
292 BUG();
293 case 1:
294 /*
295 * Avoid an unnecessary wake_up() function call.
296 * The common case is pkmap_count[] == 1, but
297 * no waiters.
298 * The tasks queued in the wait-queue are guarded
299 * by both the lock in the wait-queue-head and by
300 * the kmap_lock. As the kmap_lock is held here,
301 * no need for the wait-queue-head's lock. Simply
302 * test if the queue is empty.
303 */
304 need_wakeup = waitqueue_active(&pkmap_map_wait);
305 }
Nicolas Pitre3297e762009-03-04 22:49:41 -0500306 unlock_kmap_any(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* do wake-up, if needed, race-free outside of the spin lock */
309 if (need_wakeup)
310 wake_up(&pkmap_map_wait);
311}
312
313EXPORT_SYMBOL(kunmap_high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#endif
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#if defined(HASHED_PAGE_VIRTUAL)
317
318#define PA_HASH_ORDER 7
319
320/*
321 * Describes one page->virtual association
322 */
323struct page_address_map {
324 struct page *page;
325 void *virtual;
326 struct list_head list;
327};
328
Joonsoo Kima354e2c2012-12-11 16:01:23 -0800329static struct page_address_map page_address_maps[LAST_PKMAP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331/*
332 * Hash table bucket
333 */
334static struct page_address_slot {
335 struct list_head lh; /* List of page_address_maps */
336 spinlock_t lock; /* Protect this bucket's list */
337} ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
338
Ian Campbellf9918792011-08-17 13:45:09 +0100339static struct page_address_slot *page_slot(const struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
342}
343
Randy Dunlap77f60782008-03-19 17:00:42 -0700344/**
345 * page_address - get the mapped virtual address of a page
346 * @page: &struct page to get the virtual address of
347 *
348 * Returns the page's virtual address.
349 */
Ian Campbellf9918792011-08-17 13:45:09 +0100350void *page_address(const struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 unsigned long flags;
353 void *ret;
354 struct page_address_slot *pas;
355
356 if (!PageHighMem(page))
357 return lowmem_page_address(page);
358
359 pas = page_slot(page);
360 ret = NULL;
361 spin_lock_irqsave(&pas->lock, flags);
362 if (!list_empty(&pas->lh)) {
363 struct page_address_map *pam;
364
365 list_for_each_entry(pam, &pas->lh, list) {
366 if (pam->page == page) {
367 ret = pam->virtual;
368 goto done;
369 }
370 }
371 }
372done:
373 spin_unlock_irqrestore(&pas->lock, flags);
374 return ret;
375}
376
377EXPORT_SYMBOL(page_address);
378
Randy Dunlap77f60782008-03-19 17:00:42 -0700379/**
380 * set_page_address - set a page's virtual address
381 * @page: &struct page to set
382 * @virtual: virtual address to use
383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384void set_page_address(struct page *page, void *virtual)
385{
386 unsigned long flags;
387 struct page_address_slot *pas;
388 struct page_address_map *pam;
389
390 BUG_ON(!PageHighMem(page));
391
392 pas = page_slot(page);
393 if (virtual) { /* Add */
Joonsoo Kima354e2c2012-12-11 16:01:23 -0800394 pam = &page_address_maps[PKMAP_NR((unsigned long)virtual)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 pam->page = page;
396 pam->virtual = virtual;
397
398 spin_lock_irqsave(&pas->lock, flags);
399 list_add_tail(&pam->list, &pas->lh);
400 spin_unlock_irqrestore(&pas->lock, flags);
401 } else { /* Remove */
402 spin_lock_irqsave(&pas->lock, flags);
403 list_for_each_entry(pam, &pas->lh, list) {
404 if (pam->page == page) {
405 list_del(&pam->list);
406 spin_unlock_irqrestore(&pas->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 goto done;
408 }
409 }
410 spin_unlock_irqrestore(&pas->lock, flags);
411 }
412done:
413 return;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416void __init page_address_init(void)
417{
418 int i;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
421 INIT_LIST_HEAD(&page_address_htable[i].lh);
422 spin_lock_init(&page_address_htable[i].lock);
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426#endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */