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.
48 unsigned long totalhigh_pages __read_mostly
;
49 EXPORT_SYMBOL(totalhigh_pages
);
51 #ifndef CONFIG_PREEMPT_RT_FULL
52 EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx
);
55 unsigned int nr_free_highpages (void)
58 unsigned int pages
= 0;
60 for_each_online_pgdat(pgdat
) {
61 pages
+= zone_page_state(&pgdat
->node_zones
[ZONE_HIGHMEM
],
63 if (zone_movable_is_highmem())
64 pages
+= zone_page_state(
65 &pgdat
->node_zones
[ZONE_MOVABLE
],
72 static int pkmap_count
[LAST_PKMAP
];
73 static unsigned int last_pkmap_nr
;
74 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(kmap_lock
);
76 pte_t
* pkmap_page_table
;
78 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait
);
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.
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)
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)
99 struct page
*kmap_to_page(void *vaddr
)
101 unsigned long addr
= (unsigned long)vaddr
;
103 if (addr
>= PKMAP_ADDR(0) && addr
< PKMAP_ADDR(LAST_PKMAP
)) {
104 int i
= PKMAP_NR(addr
);
105 return pte_page(pkmap_page_table
[i
]);
108 return virt_to_page(addr
);
110 EXPORT_SYMBOL(kmap_to_page
);
112 static void flush_all_zero_pkmaps(void)
119 for (i
= 0; i
< LAST_PKMAP
; i
++) {
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
128 if (pkmap_count
[i
] != 1)
133 BUG_ON(pte_none(pkmap_page_table
[i
]));
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.
142 page
= pte_page(pkmap_page_table
[i
]);
143 pte_clear(&init_mm
, PKMAP_ADDR(i
), &pkmap_page_table
[i
]);
145 set_page_address(page
, NULL
);
149 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP
));
153 * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
155 void kmap_flush_unused(void)
158 flush_all_zero_pkmaps();
162 static inline unsigned long map_new_virtual(struct page
*page
)
169 /* Find an empty entry */
171 last_pkmap_nr
= (last_pkmap_nr
+ 1) & LAST_PKMAP_MASK
;
172 if (!last_pkmap_nr
) {
173 flush_all_zero_pkmaps();
176 if (!pkmap_count
[last_pkmap_nr
])
177 break; /* Found a usable entry */
182 * Sleep for somebody else to unmap their entries
185 DECLARE_WAITQUEUE(wait
, current
);
187 __set_current_state(TASK_UNINTERRUPTIBLE
);
188 add_wait_queue(&pkmap_map_wait
, &wait
);
191 remove_wait_queue(&pkmap_map_wait
, &wait
);
194 /* Somebody else might have mapped it while we slept */
195 if (page_address(page
))
196 return (unsigned long)page_address(page
);
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
));
206 pkmap_count
[last_pkmap_nr
] = 1;
207 set_page_address(page
, (void *)vaddr
);
213 * kmap_high - map a highmem page into memory
214 * @page: &struct page to map
216 * Returns the page's virtual memory address.
218 * We cannot call this from interrupts, as it may block.
220 void *kmap_high(struct page
*page
)
225 * For highmem pages, we can't trust "virtual" until
226 * after we have the lock.
229 vaddr
= (unsigned long)page_address(page
);
231 vaddr
= map_new_virtual(page
);
232 pkmap_count
[PKMAP_NR(vaddr
)]++;
233 BUG_ON(pkmap_count
[PKMAP_NR(vaddr
)] < 2);
235 return (void*) vaddr
;
238 EXPORT_SYMBOL(kmap_high
);
240 #ifdef ARCH_NEEDS_KMAP_HIGH_GET
242 * kmap_high_get - pin a highmem page into memory
243 * @page: &struct page to pin
245 * Returns the page's current virtual memory address, or NULL if no mapping
246 * exists. If and only if a non null address is returned then a
247 * matching call to kunmap_high() is necessary.
249 * This can be called from any context.
251 void *kmap_high_get(struct page
*page
)
253 unsigned long vaddr
, flags
;
255 lock_kmap_any(flags
);
256 vaddr
= (unsigned long)page_address(page
);
258 BUG_ON(pkmap_count
[PKMAP_NR(vaddr
)] < 1);
259 pkmap_count
[PKMAP_NR(vaddr
)]++;
261 unlock_kmap_any(flags
);
262 return (void*) vaddr
;
267 * kunmap_high - unmap a highmem page into memory
268 * @page: &struct page to unmap
270 * If ARCH_NEEDS_KMAP_HIGH_GET is not defined then this may be called
271 * only from user context.
273 void kunmap_high(struct page
*page
)
280 lock_kmap_any(flags
);
281 vaddr
= (unsigned long)page_address(page
);
283 nr
= PKMAP_NR(vaddr
);
286 * A count must never go down to zero
287 * without a TLB flush!
290 switch (--pkmap_count
[nr
]) {
295 * Avoid an unnecessary wake_up() function call.
296 * The common case is pkmap_count[] == 1, but
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.
304 need_wakeup
= waitqueue_active(&pkmap_map_wait
);
306 unlock_kmap_any(flags
);
308 /* do wake-up, if needed, race-free outside of the spin lock */
310 wake_up(&pkmap_map_wait
);
313 EXPORT_SYMBOL(kunmap_high
);
316 #if defined(HASHED_PAGE_VIRTUAL)
318 #define PA_HASH_ORDER 7
321 * Describes one page->virtual association
323 struct page_address_map
{
326 struct list_head list
;
329 static struct page_address_map page_address_maps
[LAST_PKMAP
];
334 static 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
];
339 static struct page_address_slot
*page_slot(const struct page
*page
)
341 return &page_address_htable
[hash_ptr(page
, PA_HASH_ORDER
)];
345 * page_address - get the mapped virtual address of a page
346 * @page: &struct page to get the virtual address of
348 * Returns the page's virtual address.
350 void *page_address(const struct page
*page
)
354 struct page_address_slot
*pas
;
356 if (!PageHighMem(page
))
357 return lowmem_page_address(page
);
359 pas
= page_slot(page
);
361 spin_lock_irqsave(&pas
->lock
, flags
);
362 if (!list_empty(&pas
->lh
)) {
363 struct page_address_map
*pam
;
365 list_for_each_entry(pam
, &pas
->lh
, list
) {
366 if (pam
->page
== page
) {
373 spin_unlock_irqrestore(&pas
->lock
, flags
);
377 EXPORT_SYMBOL(page_address
);
380 * set_page_address - set a page's virtual address
381 * @page: &struct page to set
382 * @virtual: virtual address to use
384 void set_page_address(struct page
*page
, void *virtual)
387 struct page_address_slot
*pas
;
388 struct page_address_map
*pam
;
390 BUG_ON(!PageHighMem(page
));
392 pas
= page_slot(page
);
393 if (virtual) { /* Add */
394 pam
= &page_address_maps
[PKMAP_NR((unsigned long)virtual)];
396 pam
->virtual = virtual;
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
);
410 spin_unlock_irqrestore(&pas
->lock
, flags
);
416 void __init
page_address_init(void)
420 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
);
426 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */