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/export.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/kgdb.h>
30 #include <asm/tlbflush.h>
32 #ifndef CONFIG_PREEMPT_RT_FULL
33 #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
34 DEFINE_PER_CPU(int, __kmap_atomic_idx);
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.
49 * Architecture with aliasing data cache may define the following family of
50 * helper functions in its asm/highmem.h to control cache color of virtual
51 * addresses where physical memory pages are mapped by kmap.
53 #ifndef get_pkmap_color
56 * Determine color of virtual address where the page should be mapped.
58 static inline unsigned int get_pkmap_color(struct page *page)
62 #define get_pkmap_color get_pkmap_color
65 * Get next index for mapping inside PKMAP region for page with given color.
67 static inline unsigned int get_next_pkmap_nr(unsigned int color)
69 static unsigned int last_pkmap_nr;
71 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
76 * Determine if page index inside PKMAP region (pkmap_nr) of given color
77 * has wrapped around PKMAP region end. When this happens an attempt to
78 * flush all unused PKMAP slots is made.
80 static inline int no_more_pkmaps(unsigned int pkmap_nr, unsigned int color)
86 * Get the number of PKMAP entries of the given color. If no free slot is
87 * found after checking that many entries, kmap will sleep waiting for
88 * someone to call kunmap and free PKMAP slot.
90 static inline int get_pkmap_entries_count(unsigned int color)
96 * Get head of a wait queue for PKMAP entries of the given color.
97 * Wait queues for different mapping colors should be independent to avoid
98 * unnecessary wakeups caused by freeing of slots of other colors.
100 static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
102 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
104 return &pkmap_map_wait;
108 unsigned long totalhigh_pages __read_mostly;
109 EXPORT_SYMBOL(totalhigh_pages);
111 #ifndef CONFIG_PREEMPT_RT_FULL
112 EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
115 unsigned int nr_free_highpages (void)
118 unsigned int pages = 0;
120 for_each_online_pgdat(pgdat) {
121 pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
123 if (zone_movable_is_highmem())
124 pages += zone_page_state(
125 &pgdat->node_zones[ZONE_MOVABLE],
132 static int pkmap_count[LAST_PKMAP];
133 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
135 pte_t * pkmap_page_table;
138 * Most architectures have no use for kmap_high_get(), so let's abstract
139 * the disabling of IRQ out of the locking in that case to save on a
140 * potential useless overhead.
142 #ifdef ARCH_NEEDS_KMAP_HIGH_GET
143 #define lock_kmap() spin_lock_irq(&kmap_lock)
144 #define unlock_kmap() spin_unlock_irq(&kmap_lock)
145 #define lock_kmap_any(flags) spin_lock_irqsave(&kmap_lock, flags)
146 #define unlock_kmap_any(flags) spin_unlock_irqrestore(&kmap_lock, flags)
148 #define lock_kmap() spin_lock(&kmap_lock)
149 #define unlock_kmap() spin_unlock(&kmap_lock)
150 #define lock_kmap_any(flags) \
151 do { spin_lock(&kmap_lock); (void)(flags); } while (0)
152 #define unlock_kmap_any(flags) \
153 do { spin_unlock(&kmap_lock); (void)(flags); } while (0)
156 struct page *kmap_to_page(void *vaddr)
158 unsigned long addr = (unsigned long)vaddr;
160 if (addr >= PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP)) {
161 int i = PKMAP_NR(addr);
162 return pte_page(pkmap_page_table[i]);
165 return virt_to_page(addr);
167 EXPORT_SYMBOL(kmap_to_page);
169 static void flush_all_zero_pkmaps(void)
176 for (i = 0; i < LAST_PKMAP; i++) {
180 * zero means we don't have anything to do,
181 * >1 means that it is still in use. Only
182 * a count of 1 means that it is free but
183 * needs to be unmapped
185 if (pkmap_count[i] != 1)
190 BUG_ON(pte_none(pkmap_page_table[i]));
193 * Don't need an atomic fetch-and-clear op here;
194 * no-one has the page mapped, and cannot get at
195 * its virtual address (and hence PTE) without first
196 * getting the kmap_lock (which is held here).
197 * So no dangers, even with speculative execution.
199 page = pte_page(pkmap_page_table[i]);
200 pte_clear(&init_mm, PKMAP_ADDR(i), &pkmap_page_table[i]);
202 set_page_address(page, NULL);
206 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
210 * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
212 void kmap_flush_unused(void)
215 flush_all_zero_pkmaps();
219 static inline unsigned long map_new_virtual(struct page *page)
223 unsigned int last_pkmap_nr;
224 unsigned int color = get_pkmap_color(page);
227 count = get_pkmap_entries_count(color);
228 /* Find an empty entry */
230 last_pkmap_nr = get_next_pkmap_nr(color);
231 if (no_more_pkmaps(last_pkmap_nr, color)) {
232 flush_all_zero_pkmaps();
233 count = get_pkmap_entries_count(color);
235 if (!pkmap_count[last_pkmap_nr])
236 break; /* Found a usable entry */
241 * Sleep for somebody else to unmap their entries
244 DECLARE_WAITQUEUE(wait, current);
245 wait_queue_head_t *pkmap_map_wait =
246 get_pkmap_wait_queue_head(color);
248 __set_current_state(TASK_UNINTERRUPTIBLE);
249 add_wait_queue(pkmap_map_wait, &wait);
252 remove_wait_queue(pkmap_map_wait, &wait);
255 /* Somebody else might have mapped it while we slept */
256 if (page_address(page))
257 return (unsigned long)page_address(page);
263 vaddr = PKMAP_ADDR(last_pkmap_nr);
264 set_pte_at(&init_mm, vaddr,
265 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
267 pkmap_count[last_pkmap_nr] = 1;
268 set_page_address(page, (void *)vaddr);
274 * kmap_high - map a highmem page into memory
275 * @page: &struct page to map
277 * Returns the page's virtual memory address.
279 * We cannot call this from interrupts, as it may block.
281 void *kmap_high(struct page *page)
286 * For highmem pages, we can't trust "virtual" until
287 * after we have the lock.
290 vaddr = (unsigned long)page_address(page);
292 vaddr = map_new_virtual(page);
293 pkmap_count[PKMAP_NR(vaddr)]++;
294 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
296 return (void*) vaddr;
299 EXPORT_SYMBOL(kmap_high);
301 #ifdef ARCH_NEEDS_KMAP_HIGH_GET
303 * kmap_high_get - pin a highmem page into memory
304 * @page: &struct page to pin
306 * Returns the page's current virtual memory address, or NULL if no mapping
307 * exists. If and only if a non null address is returned then a
308 * matching call to kunmap_high() is necessary.
310 * This can be called from any context.
312 void *kmap_high_get(struct page *page)
314 unsigned long vaddr, flags;
316 lock_kmap_any(flags);
317 vaddr = (unsigned long)page_address(page);
319 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 1);
320 pkmap_count[PKMAP_NR(vaddr)]++;
322 unlock_kmap_any(flags);
323 return (void*) vaddr;
328 * kunmap_high - unmap a highmem page into memory
329 * @page: &struct page to unmap
331 * If ARCH_NEEDS_KMAP_HIGH_GET is not defined then this may be called
332 * only from user context.
334 void kunmap_high(struct page *page)
340 unsigned int color = get_pkmap_color(page);
341 wait_queue_head_t *pkmap_map_wait;
343 lock_kmap_any(flags);
344 vaddr = (unsigned long)page_address(page);
346 nr = PKMAP_NR(vaddr);
349 * A count must never go down to zero
350 * without a TLB flush!
353 switch (--pkmap_count[nr]) {
358 * Avoid an unnecessary wake_up() function call.
359 * The common case is pkmap_count[] == 1, but
361 * The tasks queued in the wait-queue are guarded
362 * by both the lock in the wait-queue-head and by
363 * the kmap_lock. As the kmap_lock is held here,
364 * no need for the wait-queue-head's lock. Simply
365 * test if the queue is empty.
367 pkmap_map_wait = get_pkmap_wait_queue_head(color);
368 need_wakeup = waitqueue_active(pkmap_map_wait);
370 unlock_kmap_any(flags);
372 /* do wake-up, if needed, race-free outside of the spin lock */
374 wake_up(pkmap_map_wait);
377 EXPORT_SYMBOL(kunmap_high);
380 #if defined(HASHED_PAGE_VIRTUAL)
382 #define PA_HASH_ORDER 7
385 * Describes one page->virtual association
387 struct page_address_map {
390 struct list_head list;
393 static struct page_address_map page_address_maps[LAST_PKMAP];
398 static struct page_address_slot {
399 struct list_head lh; /* List of page_address_maps */
400 spinlock_t lock; /* Protect this bucket's list */
401 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
403 static struct page_address_slot *page_slot(const struct page *page)
405 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
409 * page_address - get the mapped virtual address of a page
410 * @page: &struct page to get the virtual address of
412 * Returns the page's virtual address.
414 void *page_address(const struct page *page)
418 struct page_address_slot *pas;
420 if (!PageHighMem(page))
421 return lowmem_page_address(page);
423 pas = page_slot(page);
425 spin_lock_irqsave(&pas->lock, flags);
426 if (!list_empty(&pas->lh)) {
427 struct page_address_map *pam;
429 list_for_each_entry(pam, &pas->lh, list) {
430 if (pam->page == page) {
437 spin_unlock_irqrestore(&pas->lock, flags);
441 EXPORT_SYMBOL(page_address);
444 * set_page_address - set a page's virtual address
445 * @page: &struct page to set
446 * @virtual: virtual address to use
448 void set_page_address(struct page *page, void *virtual)
451 struct page_address_slot *pas;
452 struct page_address_map *pam;
454 BUG_ON(!PageHighMem(page));
456 pas = page_slot(page);
457 if (virtual) { /* Add */
458 pam = &page_address_maps[PKMAP_NR((unsigned long)virtual)];
460 pam->virtual = virtual;
462 spin_lock_irqsave(&pas->lock, flags);
463 list_add_tail(&pam->list, &pas->lh);
464 spin_unlock_irqrestore(&pas->lock, flags);
465 } else { /* Remove */
466 spin_lock_irqsave(&pas->lock, flags);
467 list_for_each_entry(pam, &pas->lh, list) {
468 if (pam->page == page) {
469 list_del(&pam->list);
470 spin_unlock_irqrestore(&pas->lock, flags);
474 spin_unlock_irqrestore(&pas->lock, flags);
480 void __init page_address_init(void)
484 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
485 INIT_LIST_HEAD(&page_address_htable[i].lh);
486 spin_lock_init(&page_address_htable[i].lock);
490 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */