highmem: Get rid of kmap_types.h
[linux-2.6-block.git] / mm / highmem.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * High memory handling common code and variables.
4 *
5 * (C) 1999 Andrea Arcangeli, SuSE GmbH, andrea@suse.de
6 * Gerhard Wichert, Siemens AG, Gerhard.Wichert@pdb.siemens.de
7 *
8 *
9 * Redesigned the x86 32-bit VM architecture to deal with
10 * 64-bit physical space. With current x86 CPUs this
11 * means up to 64 Gigabytes physical RAM.
12 *
13 * Rewrote high memory support to move the page cache into
14 * high memory. Implemented permanent (schedulable) kmaps
15 * based on Linus' idea.
16 *
17 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
18 */
19
20#include <linux/mm.h>
b95f1b31 21#include <linux/export.h>
1da177e4
LT
22#include <linux/swap.h>
23#include <linux/bio.h>
24#include <linux/pagemap.h>
25#include <linux/mempool.h>
26#include <linux/blkdev.h>
27#include <linux/init.h>
28#include <linux/hash.h>
29#include <linux/highmem.h>
eac79005 30#include <linux/kgdb.h>
1da177e4 31#include <asm/tlbflush.h>
186525bd 32#include <linux/vmalloc.h>
a8e23a29 33
298fa1ad 34#ifndef CONFIG_KMAP_LOCAL
157e118b 35#ifdef CONFIG_HIGHMEM
a8e23a29
PZ
36DEFINE_PER_CPU(int, __kmap_atomic_idx);
37#endif
298fa1ad 38#endif
a8e23a29 39
1da177e4
LT
40/*
41 * Virtual_count is not a pure "count".
42 * 0 means that it is not mapped, and has not been mapped
43 * since a TLB flush - it is usable.
44 * 1 means that there are no users, but it has been mapped
45 * since the last TLB flush - so we can't use it.
46 * n means that there are (n-1) current users of it.
47 */
48#ifdef CONFIG_HIGHMEM
260b2367 49
15de36a4
MF
50/*
51 * Architecture with aliasing data cache may define the following family of
52 * helper functions in its asm/highmem.h to control cache color of virtual
53 * addresses where physical memory pages are mapped by kmap.
54 */
55#ifndef get_pkmap_color
56
57/*
58 * Determine color of virtual address where the page should be mapped.
59 */
60static inline unsigned int get_pkmap_color(struct page *page)
61{
62 return 0;
63}
64#define get_pkmap_color get_pkmap_color
65
66/*
67 * Get next index for mapping inside PKMAP region for page with given color.
68 */
69static inline unsigned int get_next_pkmap_nr(unsigned int color)
70{
71 static unsigned int last_pkmap_nr;
72
73 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
74 return last_pkmap_nr;
75}
76
77/*
78 * Determine if page index inside PKMAP region (pkmap_nr) of given color
79 * has wrapped around PKMAP region end. When this happens an attempt to
80 * flush all unused PKMAP slots is made.
81 */
82static inline int no_more_pkmaps(unsigned int pkmap_nr, unsigned int color)
83{
84 return pkmap_nr == 0;
85}
86
87/*
88 * Get the number of PKMAP entries of the given color. If no free slot is
89 * found after checking that many entries, kmap will sleep waiting for
90 * someone to call kunmap and free PKMAP slot.
91 */
92static inline int get_pkmap_entries_count(unsigned int color)
93{
94 return LAST_PKMAP;
95}
96
97/*
98 * Get head of a wait queue for PKMAP entries of the given color.
99 * Wait queues for different mapping colors should be independent to avoid
100 * unnecessary wakeups caused by freeing of slots of other colors.
101 */
102static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
103{
104 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
105
106 return &pkmap_map_wait;
107}
108#endif
109
ca79b0c2
AK
110atomic_long_t _totalhigh_pages __read_mostly;
111EXPORT_SYMBOL(_totalhigh_pages);
3e4d3af5 112
c1f60a5a
CL
113unsigned int nr_free_highpages (void)
114{
33499bfe 115 struct zone *zone;
c1f60a5a
CL
116 unsigned int pages = 0;
117
33499bfe
JK
118 for_each_populated_zone(zone) {
119 if (is_highmem(zone))
120 pages += zone_page_state(zone, NR_FREE_PAGES);
2a1e274a 121 }
c1f60a5a
CL
122
123 return pages;
124}
125
1da177e4 126static int pkmap_count[LAST_PKMAP];
1da177e4
LT
127static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
128
129pte_t * pkmap_page_table;
130
3297e760
NP
131/*
132 * Most architectures have no use for kmap_high_get(), so let's abstract
133 * the disabling of IRQ out of the locking in that case to save on a
134 * potential useless overhead.
135 */
136#ifdef ARCH_NEEDS_KMAP_HIGH_GET
137#define lock_kmap() spin_lock_irq(&kmap_lock)
138#define unlock_kmap() spin_unlock_irq(&kmap_lock)
139#define lock_kmap_any(flags) spin_lock_irqsave(&kmap_lock, flags)
140#define unlock_kmap_any(flags) spin_unlock_irqrestore(&kmap_lock, flags)
141#else
142#define lock_kmap() spin_lock(&kmap_lock)
143#define unlock_kmap() spin_unlock(&kmap_lock)
144#define lock_kmap_any(flags) \
145 do { spin_lock(&kmap_lock); (void)(flags); } while (0)
146#define unlock_kmap_any(flags) \
147 do { spin_unlock(&kmap_lock); (void)(flags); } while (0)
148#endif
149
5a178119
MG
150struct page *kmap_to_page(void *vaddr)
151{
152 unsigned long addr = (unsigned long)vaddr;
153
498c2280 154 if (addr >= PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP)) {
4de22c05 155 int i = PKMAP_NR(addr);
5a178119
MG
156 return pte_page(pkmap_page_table[i]);
157 }
158
159 return virt_to_page(addr);
160}
f0263d2d 161EXPORT_SYMBOL(kmap_to_page);
5a178119 162
1da177e4
LT
163static void flush_all_zero_pkmaps(void)
164{
165 int i;
5843d9a4 166 int need_flush = 0;
1da177e4
LT
167
168 flush_cache_kmaps();
169
170 for (i = 0; i < LAST_PKMAP; i++) {
171 struct page *page;
172
173 /*
174 * zero means we don't have anything to do,
175 * >1 means that it is still in use. Only
176 * a count of 1 means that it is free but
177 * needs to be unmapped
178 */
179 if (pkmap_count[i] != 1)
180 continue;
181 pkmap_count[i] = 0;
182
183 /* sanity check */
75babcac 184 BUG_ON(pte_none(pkmap_page_table[i]));
1da177e4
LT
185
186 /*
187 * Don't need an atomic fetch-and-clear op here;
188 * no-one has the page mapped, and cannot get at
189 * its virtual address (and hence PTE) without first
190 * getting the kmap_lock (which is held here).
191 * So no dangers, even with speculative execution.
192 */
193 page = pte_page(pkmap_page_table[i]);
eb2db439 194 pte_clear(&init_mm, PKMAP_ADDR(i), &pkmap_page_table[i]);
1da177e4
LT
195
196 set_page_address(page, NULL);
5843d9a4 197 need_flush = 1;
1da177e4 198 }
5843d9a4
NP
199 if (need_flush)
200 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
1da177e4
LT
201}
202
77f6078a
RD
203/**
204 * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
205 */
ce6234b5
JF
206void kmap_flush_unused(void)
207{
3297e760 208 lock_kmap();
ce6234b5 209 flush_all_zero_pkmaps();
3297e760 210 unlock_kmap();
ce6234b5
JF
211}
212
1da177e4
LT
213static inline unsigned long map_new_virtual(struct page *page)
214{
215 unsigned long vaddr;
216 int count;
15de36a4
MF
217 unsigned int last_pkmap_nr;
218 unsigned int color = get_pkmap_color(page);
1da177e4
LT
219
220start:
15de36a4 221 count = get_pkmap_entries_count(color);
1da177e4
LT
222 /* Find an empty entry */
223 for (;;) {
15de36a4
MF
224 last_pkmap_nr = get_next_pkmap_nr(color);
225 if (no_more_pkmaps(last_pkmap_nr, color)) {
1da177e4 226 flush_all_zero_pkmaps();
15de36a4 227 count = get_pkmap_entries_count(color);
1da177e4
LT
228 }
229 if (!pkmap_count[last_pkmap_nr])
230 break; /* Found a usable entry */
231 if (--count)
232 continue;
233
234 /*
235 * Sleep for somebody else to unmap their entries
236 */
237 {
238 DECLARE_WAITQUEUE(wait, current);
15de36a4
MF
239 wait_queue_head_t *pkmap_map_wait =
240 get_pkmap_wait_queue_head(color);
1da177e4
LT
241
242 __set_current_state(TASK_UNINTERRUPTIBLE);
15de36a4 243 add_wait_queue(pkmap_map_wait, &wait);
3297e760 244 unlock_kmap();
1da177e4 245 schedule();
15de36a4 246 remove_wait_queue(pkmap_map_wait, &wait);
3297e760 247 lock_kmap();
1da177e4
LT
248
249 /* Somebody else might have mapped it while we slept */
250 if (page_address(page))
251 return (unsigned long)page_address(page);
252
253 /* Re-start */
254 goto start;
255 }
256 }
257 vaddr = PKMAP_ADDR(last_pkmap_nr);
258 set_pte_at(&init_mm, vaddr,
259 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
260
261 pkmap_count[last_pkmap_nr] = 1;
262 set_page_address(page, (void *)vaddr);
263
264 return vaddr;
265}
266
77f6078a
RD
267/**
268 * kmap_high - map a highmem page into memory
269 * @page: &struct page to map
270 *
271 * Returns the page's virtual memory address.
272 *
273 * We cannot call this from interrupts, as it may block.
274 */
920c7a5d 275void *kmap_high(struct page *page)
1da177e4
LT
276{
277 unsigned long vaddr;
278
279 /*
280 * For highmem pages, we can't trust "virtual" until
281 * after we have the lock.
1da177e4 282 */
3297e760 283 lock_kmap();
1da177e4
LT
284 vaddr = (unsigned long)page_address(page);
285 if (!vaddr)
286 vaddr = map_new_virtual(page);
287 pkmap_count[PKMAP_NR(vaddr)]++;
75babcac 288 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
3297e760 289 unlock_kmap();
1da177e4
LT
290 return (void*) vaddr;
291}
292
293EXPORT_SYMBOL(kmap_high);
294
3297e760
NP
295#ifdef ARCH_NEEDS_KMAP_HIGH_GET
296/**
297 * kmap_high_get - pin a highmem page into memory
298 * @page: &struct page to pin
299 *
300 * Returns the page's current virtual memory address, or NULL if no mapping
5e39df56 301 * exists. If and only if a non null address is returned then a
3297e760
NP
302 * matching call to kunmap_high() is necessary.
303 *
304 * This can be called from any context.
305 */
306void *kmap_high_get(struct page *page)
307{
308 unsigned long vaddr, flags;
309
310 lock_kmap_any(flags);
311 vaddr = (unsigned long)page_address(page);
312 if (vaddr) {
313 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 1);
314 pkmap_count[PKMAP_NR(vaddr)]++;
315 }
316 unlock_kmap_any(flags);
317 return (void*) vaddr;
318}
319#endif
320
77f6078a 321/**
4e9dc5df 322 * kunmap_high - unmap a highmem page into memory
77f6078a 323 * @page: &struct page to unmap
3297e760
NP
324 *
325 * If ARCH_NEEDS_KMAP_HIGH_GET is not defined then this may be called
326 * only from user context.
77f6078a 327 */
920c7a5d 328void kunmap_high(struct page *page)
1da177e4
LT
329{
330 unsigned long vaddr;
331 unsigned long nr;
3297e760 332 unsigned long flags;
1da177e4 333 int need_wakeup;
15de36a4
MF
334 unsigned int color = get_pkmap_color(page);
335 wait_queue_head_t *pkmap_map_wait;
1da177e4 336
3297e760 337 lock_kmap_any(flags);
1da177e4 338 vaddr = (unsigned long)page_address(page);
75babcac 339 BUG_ON(!vaddr);
1da177e4
LT
340 nr = PKMAP_NR(vaddr);
341
342 /*
343 * A count must never go down to zero
344 * without a TLB flush!
345 */
346 need_wakeup = 0;
347 switch (--pkmap_count[nr]) {
348 case 0:
349 BUG();
350 case 1:
351 /*
352 * Avoid an unnecessary wake_up() function call.
353 * The common case is pkmap_count[] == 1, but
354 * no waiters.
355 * The tasks queued in the wait-queue are guarded
356 * by both the lock in the wait-queue-head and by
357 * the kmap_lock. As the kmap_lock is held here,
358 * no need for the wait-queue-head's lock. Simply
359 * test if the queue is empty.
360 */
15de36a4
MF
361 pkmap_map_wait = get_pkmap_wait_queue_head(color);
362 need_wakeup = waitqueue_active(pkmap_map_wait);
1da177e4 363 }
3297e760 364 unlock_kmap_any(flags);
1da177e4
LT
365
366 /* do wake-up, if needed, race-free outside of the spin lock */
367 if (need_wakeup)
15de36a4 368 wake_up(pkmap_map_wait);
1da177e4 369}
1da177e4 370EXPORT_SYMBOL(kunmap_high);
298fa1ad
TG
371#endif /* CONFIG_HIGHMEM */
372
373#ifdef CONFIG_KMAP_LOCAL
374
375#include <asm/kmap_size.h>
376
377static DEFINE_PER_CPU(int, __kmap_local_idx);
378
389755c2
TG
379/*
380 * With DEBUG_HIGHMEM the stack depth is doubled and every second
381 * slot is unused which acts as a guard page
382 */
383#ifdef CONFIG_DEBUG_HIGHMEM
384# define KM_INCR 2
385#else
386# define KM_INCR 1
387#endif
388
298fa1ad
TG
389static inline int kmap_local_idx_push(void)
390{
389755c2 391 int idx = __this_cpu_add_return(__kmap_local_idx, KM_INCR) - 1;
298fa1ad
TG
392
393 WARN_ON_ONCE(in_irq() && !irqs_disabled());
394 BUG_ON(idx >= KM_MAX_IDX);
395 return idx;
396}
397
398static inline int kmap_local_idx(void)
399{
400 return __this_cpu_read(__kmap_local_idx) - 1;
401}
402
403static inline void kmap_local_idx_pop(void)
404{
389755c2 405 int idx = __this_cpu_sub_return(__kmap_local_idx, KM_INCR);
298fa1ad
TG
406
407 BUG_ON(idx < 0);
408}
409
410#ifndef arch_kmap_local_post_map
411# define arch_kmap_local_post_map(vaddr, pteval) do { } while (0)
412#endif
413#ifndef arch_kmap_local_pre_unmap
414# define arch_kmap_local_pre_unmap(vaddr) do { } while (0)
415#endif
416
417#ifndef arch_kmap_local_post_unmap
418# define arch_kmap_local_post_unmap(vaddr) do { } while (0)
419#endif
420
421#ifndef arch_kmap_local_map_idx
422#define arch_kmap_local_map_idx(idx, pfn) kmap_local_calc_idx(idx)
423#endif
424
425#ifndef arch_kmap_local_unmap_idx
426#define arch_kmap_local_unmap_idx(idx, vaddr) kmap_local_calc_idx(idx)
427#endif
428
429#ifndef arch_kmap_local_high_get
430static inline void *arch_kmap_local_high_get(struct page *page)
431{
432 return NULL;
433}
434#endif
435
436/* Unmap a local mapping which was obtained by kmap_high_get() */
437static inline void kmap_high_unmap_local(unsigned long vaddr)
438{
439#ifdef ARCH_NEEDS_KMAP_HIGH_GET
440 if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP))
441 kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
442#endif
443}
444
445static inline int kmap_local_calc_idx(int idx)
446{
447 return idx + KM_MAX_IDX * smp_processor_id();
448}
449
450static pte_t *__kmap_pte;
451
452static pte_t *kmap_get_pte(void)
453{
454 if (!__kmap_pte)
455 __kmap_pte = virt_to_kpte(__fix_to_virt(FIX_KMAP_BEGIN));
456 return __kmap_pte;
457}
458
459void *__kmap_local_pfn_prot(unsigned long pfn, pgprot_t prot)
460{
461 pte_t pteval, *kmap_pte = kmap_get_pte();
462 unsigned long vaddr;
463 int idx;
464
465 preempt_disable();
466 idx = arch_kmap_local_map_idx(kmap_local_idx_push(), pfn);
467 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
468 BUG_ON(!pte_none(*(kmap_pte - idx)));
469 pteval = pfn_pte(pfn, prot);
470 set_pte_at(&init_mm, vaddr, kmap_pte - idx, pteval);
471 arch_kmap_local_post_map(vaddr, pteval);
472 preempt_enable();
473
474 return (void *)vaddr;
475}
476EXPORT_SYMBOL_GPL(__kmap_local_pfn_prot);
477
478void *__kmap_local_page_prot(struct page *page, pgprot_t prot)
479{
480 void *kmap;
481
482 if (!PageHighMem(page))
483 return page_address(page);
484
485 /* Try kmap_high_get() if architecture has it enabled */
486 kmap = arch_kmap_local_high_get(page);
487 if (kmap)
488 return kmap;
489
490 return __kmap_local_pfn_prot(page_to_pfn(page), prot);
491}
492EXPORT_SYMBOL(__kmap_local_page_prot);
493
494void kunmap_local_indexed(void *vaddr)
495{
496 unsigned long addr = (unsigned long) vaddr & PAGE_MASK;
497 pte_t *kmap_pte = kmap_get_pte();
498 int idx;
499
500 if (addr < __fix_to_virt(FIX_KMAP_END) ||
501 addr > __fix_to_virt(FIX_KMAP_BEGIN)) {
502 WARN_ON_ONCE(addr < PAGE_OFFSET);
503
504 /* Handle mappings which were obtained by kmap_high_get() */
505 kmap_high_unmap_local(addr);
506 return;
507 }
508
509 preempt_disable();
510 idx = arch_kmap_local_unmap_idx(kmap_local_idx(), addr);
511 WARN_ON_ONCE(addr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
512
513 arch_kmap_local_pre_unmap(addr);
514 pte_clear(&init_mm, addr, kmap_pte - idx);
515 arch_kmap_local_post_unmap(addr);
516 kmap_local_idx_pop();
517 preempt_enable();
518}
519EXPORT_SYMBOL(kunmap_local_indexed);
520#endif
1da177e4 521
1da177e4
LT
522#if defined(HASHED_PAGE_VIRTUAL)
523
524#define PA_HASH_ORDER 7
525
526/*
527 * Describes one page->virtual association
528 */
529struct page_address_map {
530 struct page *page;
531 void *virtual;
532 struct list_head list;
533};
534
a354e2c8 535static struct page_address_map page_address_maps[LAST_PKMAP];
1da177e4
LT
536
537/*
538 * Hash table bucket
539 */
540static struct page_address_slot {
541 struct list_head lh; /* List of page_address_maps */
542 spinlock_t lock; /* Protect this bucket's list */
543} ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
544
f9918794 545static struct page_address_slot *page_slot(const struct page *page)
1da177e4
LT
546{
547 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
548}
549
77f6078a
RD
550/**
551 * page_address - get the mapped virtual address of a page
552 * @page: &struct page to get the virtual address of
553 *
554 * Returns the page's virtual address.
555 */
f9918794 556void *page_address(const struct page *page)
1da177e4
LT
557{
558 unsigned long flags;
559 void *ret;
560 struct page_address_slot *pas;
561
562 if (!PageHighMem(page))
563 return lowmem_page_address(page);
564
565 pas = page_slot(page);
566 ret = NULL;
567 spin_lock_irqsave(&pas->lock, flags);
568 if (!list_empty(&pas->lh)) {
569 struct page_address_map *pam;
570
571 list_for_each_entry(pam, &pas->lh, list) {
572 if (pam->page == page) {
573 ret = pam->virtual;
574 goto done;
575 }
576 }
577 }
578done:
579 spin_unlock_irqrestore(&pas->lock, flags);
580 return ret;
581}
582
583EXPORT_SYMBOL(page_address);
584
77f6078a
RD
585/**
586 * set_page_address - set a page's virtual address
587 * @page: &struct page to set
588 * @virtual: virtual address to use
589 */
1da177e4
LT
590void set_page_address(struct page *page, void *virtual)
591{
592 unsigned long flags;
593 struct page_address_slot *pas;
594 struct page_address_map *pam;
595
596 BUG_ON(!PageHighMem(page));
597
598 pas = page_slot(page);
599 if (virtual) { /* Add */
a354e2c8 600 pam = &page_address_maps[PKMAP_NR((unsigned long)virtual)];
1da177e4
LT
601 pam->page = page;
602 pam->virtual = virtual;
603
604 spin_lock_irqsave(&pas->lock, flags);
605 list_add_tail(&pam->list, &pas->lh);
606 spin_unlock_irqrestore(&pas->lock, flags);
607 } else { /* Remove */
608 spin_lock_irqsave(&pas->lock, flags);
609 list_for_each_entry(pam, &pas->lh, list) {
610 if (pam->page == page) {
611 list_del(&pam->list);
612 spin_unlock_irqrestore(&pas->lock, flags);
1da177e4
LT
613 goto done;
614 }
615 }
616 spin_unlock_irqrestore(&pas->lock, flags);
617 }
618done:
619 return;
620}
621
1da177e4
LT
622void __init page_address_init(void)
623{
624 int i;
625
1da177e4
LT
626 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
627 INIT_LIST_HEAD(&page_address_htable[i].lh);
628 spin_lock_init(&page_address_htable[i].lock);
629 }
1da177e4
LT
630}
631
955cc774 632#endif /* defined(HASHED_PAGE_VIRTUAL) */