]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - mm/highmem.c
mtd: jedec_probe: fix NEC uPD29F064115 detection
[mirror_ubuntu-bionic-kernel.git] / mm / highmem.c
CommitLineData
1da177e4
LT
1/*
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>
29#include <asm/tlbflush.h>
30
1da177e4
LT
31/*
32 * Virtual_count is not a pure "count".
33 * 0 means that it is not mapped, and has not been mapped
34 * since a TLB flush - it is usable.
35 * 1 means that there are no users, but it has been mapped
36 * since the last TLB flush - so we can't use it.
37 * n means that there are (n-1) current users of it.
38 */
39#ifdef CONFIG_HIGHMEM
260b2367 40
c1f60a5a 41unsigned long totalhigh_pages __read_mostly;
db7a94d6 42EXPORT_SYMBOL(totalhigh_pages);
c1f60a5a
CL
43
44unsigned int nr_free_highpages (void)
45{
46 pg_data_t *pgdat;
47 unsigned int pages = 0;
48
2a1e274a 49 for_each_online_pgdat(pgdat) {
d23ad423
CL
50 pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
51 NR_FREE_PAGES);
2a1e274a
MG
52 if (zone_movable_is_highmem())
53 pages += zone_page_state(
54 &pgdat->node_zones[ZONE_MOVABLE],
55 NR_FREE_PAGES);
56 }
c1f60a5a
CL
57
58 return pages;
59}
60
1da177e4
LT
61static 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
3297e760
NP
69/*
70 * Most architectures have no use for kmap_high_get(), so let's abstract
71 * the disabling of IRQ out of the locking in that case to save on a
72 * potential useless overhead.
73 */
74#ifdef ARCH_NEEDS_KMAP_HIGH_GET
75#define lock_kmap() spin_lock_irq(&kmap_lock)
76#define unlock_kmap() spin_unlock_irq(&kmap_lock)
77#define lock_kmap_any(flags) spin_lock_irqsave(&kmap_lock, flags)
78#define unlock_kmap_any(flags) spin_unlock_irqrestore(&kmap_lock, flags)
79#else
80#define lock_kmap() spin_lock(&kmap_lock)
81#define unlock_kmap() spin_unlock(&kmap_lock)
82#define lock_kmap_any(flags) \
83 do { spin_lock(&kmap_lock); (void)(flags); } while (0)
84#define unlock_kmap_any(flags) \
85 do { spin_unlock(&kmap_lock); (void)(flags); } while (0)
86#endif
87
1da177e4
LT
88static void flush_all_zero_pkmaps(void)
89{
90 int i;
5843d9a4 91 int need_flush = 0;
1da177e4
LT
92
93 flush_cache_kmaps();
94
95 for (i = 0; i < LAST_PKMAP; i++) {
96 struct page *page;
97
98 /*
99 * zero means we don't have anything to do,
100 * >1 means that it is still in use. Only
101 * a count of 1 means that it is free but
102 * needs to be unmapped
103 */
104 if (pkmap_count[i] != 1)
105 continue;
106 pkmap_count[i] = 0;
107
108 /* sanity check */
75babcac 109 BUG_ON(pte_none(pkmap_page_table[i]));
1da177e4
LT
110
111 /*
112 * Don't need an atomic fetch-and-clear op here;
113 * no-one has the page mapped, and cannot get at
114 * its virtual address (and hence PTE) without first
115 * getting the kmap_lock (which is held here).
116 * So no dangers, even with speculative execution.
117 */
118 page = pte_page(pkmap_page_table[i]);
119 pte_clear(&init_mm, (unsigned long)page_address(page),
120 &pkmap_page_table[i]);
121
122 set_page_address(page, NULL);
5843d9a4 123 need_flush = 1;
1da177e4 124 }
5843d9a4
NP
125 if (need_flush)
126 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
1da177e4
LT
127}
128
77f6078a
RD
129/**
130 * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
131 */
ce6234b5
JF
132void kmap_flush_unused(void)
133{
3297e760 134 lock_kmap();
ce6234b5 135 flush_all_zero_pkmaps();
3297e760 136 unlock_kmap();
ce6234b5
JF
137}
138
1da177e4
LT
139static inline unsigned long map_new_virtual(struct page *page)
140{
141 unsigned long vaddr;
142 int count;
143
144start:
145 count = LAST_PKMAP;
146 /* Find an empty entry */
147 for (;;) {
148 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
149 if (!last_pkmap_nr) {
150 flush_all_zero_pkmaps();
151 count = LAST_PKMAP;
152 }
153 if (!pkmap_count[last_pkmap_nr])
154 break; /* Found a usable entry */
155 if (--count)
156 continue;
157
158 /*
159 * Sleep for somebody else to unmap their entries
160 */
161 {
162 DECLARE_WAITQUEUE(wait, current);
163
164 __set_current_state(TASK_UNINTERRUPTIBLE);
165 add_wait_queue(&pkmap_map_wait, &wait);
3297e760 166 unlock_kmap();
1da177e4
LT
167 schedule();
168 remove_wait_queue(&pkmap_map_wait, &wait);
3297e760 169 lock_kmap();
1da177e4
LT
170
171 /* Somebody else might have mapped it while we slept */
172 if (page_address(page))
173 return (unsigned long)page_address(page);
174
175 /* Re-start */
176 goto start;
177 }
178 }
179 vaddr = PKMAP_ADDR(last_pkmap_nr);
180 set_pte_at(&init_mm, vaddr,
181 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
182
183 pkmap_count[last_pkmap_nr] = 1;
184 set_page_address(page, (void *)vaddr);
185
186 return vaddr;
187}
188
77f6078a
RD
189/**
190 * kmap_high - map a highmem page into memory
191 * @page: &struct page to map
192 *
193 * Returns the page's virtual memory address.
194 *
195 * We cannot call this from interrupts, as it may block.
196 */
920c7a5d 197void *kmap_high(struct page *page)
1da177e4
LT
198{
199 unsigned long vaddr;
200
201 /*
202 * For highmem pages, we can't trust "virtual" until
203 * after we have the lock.
1da177e4 204 */
3297e760 205 lock_kmap();
1da177e4
LT
206 vaddr = (unsigned long)page_address(page);
207 if (!vaddr)
208 vaddr = map_new_virtual(page);
209 pkmap_count[PKMAP_NR(vaddr)]++;
75babcac 210 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
3297e760 211 unlock_kmap();
1da177e4
LT
212 return (void*) vaddr;
213}
214
215EXPORT_SYMBOL(kmap_high);
216
3297e760
NP
217#ifdef ARCH_NEEDS_KMAP_HIGH_GET
218/**
219 * kmap_high_get - pin a highmem page into memory
220 * @page: &struct page to pin
221 *
222 * Returns the page's current virtual memory address, or NULL if no mapping
223 * exists. When and only when a non null address is returned then a
224 * matching call to kunmap_high() is necessary.
225 *
226 * This can be called from any context.
227 */
228void *kmap_high_get(struct page *page)
229{
230 unsigned long vaddr, flags;
231
232 lock_kmap_any(flags);
233 vaddr = (unsigned long)page_address(page);
234 if (vaddr) {
235 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 1);
236 pkmap_count[PKMAP_NR(vaddr)]++;
237 }
238 unlock_kmap_any(flags);
239 return (void*) vaddr;
240}
241#endif
242
77f6078a
RD
243/**
244 * kunmap_high - map a highmem page into memory
245 * @page: &struct page to unmap
3297e760
NP
246 *
247 * If ARCH_NEEDS_KMAP_HIGH_GET is not defined then this may be called
248 * only from user context.
77f6078a 249 */
920c7a5d 250void kunmap_high(struct page *page)
1da177e4
LT
251{
252 unsigned long vaddr;
253 unsigned long nr;
3297e760 254 unsigned long flags;
1da177e4
LT
255 int need_wakeup;
256
3297e760 257 lock_kmap_any(flags);
1da177e4 258 vaddr = (unsigned long)page_address(page);
75babcac 259 BUG_ON(!vaddr);
1da177e4
LT
260 nr = PKMAP_NR(vaddr);
261
262 /*
263 * A count must never go down to zero
264 * without a TLB flush!
265 */
266 need_wakeup = 0;
267 switch (--pkmap_count[nr]) {
268 case 0:
269 BUG();
270 case 1:
271 /*
272 * Avoid an unnecessary wake_up() function call.
273 * The common case is pkmap_count[] == 1, but
274 * no waiters.
275 * The tasks queued in the wait-queue are guarded
276 * by both the lock in the wait-queue-head and by
277 * the kmap_lock. As the kmap_lock is held here,
278 * no need for the wait-queue-head's lock. Simply
279 * test if the queue is empty.
280 */
281 need_wakeup = waitqueue_active(&pkmap_map_wait);
282 }
3297e760 283 unlock_kmap_any(flags);
1da177e4
LT
284
285 /* do wake-up, if needed, race-free outside of the spin lock */
286 if (need_wakeup)
287 wake_up(&pkmap_map_wait);
288}
289
290EXPORT_SYMBOL(kunmap_high);
1da177e4
LT
291#endif
292
1da177e4
LT
293#if defined(HASHED_PAGE_VIRTUAL)
294
295#define PA_HASH_ORDER 7
296
297/*
298 * Describes one page->virtual association
299 */
300struct page_address_map {
301 struct page *page;
302 void *virtual;
303 struct list_head list;
304};
305
306/*
307 * page_address_map freelist, allocated from page_address_maps.
308 */
309static struct list_head page_address_pool; /* freelist */
310static spinlock_t pool_lock; /* protects page_address_pool */
311
312/*
313 * Hash table bucket
314 */
315static struct page_address_slot {
316 struct list_head lh; /* List of page_address_maps */
317 spinlock_t lock; /* Protect this bucket's list */
318} ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
319
320static struct page_address_slot *page_slot(struct page *page)
321{
322 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
323}
324
77f6078a
RD
325/**
326 * page_address - get the mapped virtual address of a page
327 * @page: &struct page to get the virtual address of
328 *
329 * Returns the page's virtual address.
330 */
1da177e4
LT
331void *page_address(struct page *page)
332{
333 unsigned long flags;
334 void *ret;
335 struct page_address_slot *pas;
336
337 if (!PageHighMem(page))
338 return lowmem_page_address(page);
339
340 pas = page_slot(page);
341 ret = NULL;
342 spin_lock_irqsave(&pas->lock, flags);
343 if (!list_empty(&pas->lh)) {
344 struct page_address_map *pam;
345
346 list_for_each_entry(pam, &pas->lh, list) {
347 if (pam->page == page) {
348 ret = pam->virtual;
349 goto done;
350 }
351 }
352 }
353done:
354 spin_unlock_irqrestore(&pas->lock, flags);
355 return ret;
356}
357
358EXPORT_SYMBOL(page_address);
359
77f6078a
RD
360/**
361 * set_page_address - set a page's virtual address
362 * @page: &struct page to set
363 * @virtual: virtual address to use
364 */
1da177e4
LT
365void set_page_address(struct page *page, void *virtual)
366{
367 unsigned long flags;
368 struct page_address_slot *pas;
369 struct page_address_map *pam;
370
371 BUG_ON(!PageHighMem(page));
372
373 pas = page_slot(page);
374 if (virtual) { /* Add */
375 BUG_ON(list_empty(&page_address_pool));
376
377 spin_lock_irqsave(&pool_lock, flags);
378 pam = list_entry(page_address_pool.next,
379 struct page_address_map, list);
380 list_del(&pam->list);
381 spin_unlock_irqrestore(&pool_lock, flags);
382
383 pam->page = page;
384 pam->virtual = virtual;
385
386 spin_lock_irqsave(&pas->lock, flags);
387 list_add_tail(&pam->list, &pas->lh);
388 spin_unlock_irqrestore(&pas->lock, flags);
389 } else { /* Remove */
390 spin_lock_irqsave(&pas->lock, flags);
391 list_for_each_entry(pam, &pas->lh, list) {
392 if (pam->page == page) {
393 list_del(&pam->list);
394 spin_unlock_irqrestore(&pas->lock, flags);
395 spin_lock_irqsave(&pool_lock, flags);
396 list_add_tail(&pam->list, &page_address_pool);
397 spin_unlock_irqrestore(&pool_lock, flags);
398 goto done;
399 }
400 }
401 spin_unlock_irqrestore(&pas->lock, flags);
402 }
403done:
404 return;
405}
406
407static struct page_address_map page_address_maps[LAST_PKMAP];
408
409void __init page_address_init(void)
410{
411 int i;
412
413 INIT_LIST_HEAD(&page_address_pool);
414 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
415 list_add(&page_address_maps[i].list, &page_address_pool);
416 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
417 INIT_LIST_HEAD(&page_address_htable[i].lh);
418 spin_lock_init(&page_address_htable[i].lock);
419 }
420 spin_lock_init(&pool_lock);
421}
422
423#endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */
f4112de6
AM
424
425#if defined(CONFIG_DEBUG_HIGHMEM) && defined(CONFIG_TRACE_IRQFLAGS_SUPPORT)
426
427void debug_kmap_atomic(enum km_type type)
428{
429 static unsigned warn_count = 10;
430
431 if (unlikely(warn_count == 0))
432 return;
433
434 if (unlikely(in_interrupt())) {
435 if (in_irq()) {
436 if (type != KM_IRQ0 && type != KM_IRQ1 &&
437 type != KM_BIO_SRC_IRQ && type != KM_BIO_DST_IRQ &&
438 type != KM_BOUNCE_READ) {
439 WARN_ON(1);
440 warn_count--;
441 }
442 } else if (!irqs_disabled()) { /* softirq */
443 if (type != KM_IRQ0 && type != KM_IRQ1 &&
444 type != KM_SOFTIRQ0 && type != KM_SOFTIRQ1 &&
445 type != KM_SKB_SUNRPC_DATA &&
446 type != KM_SKB_DATA_SOFTIRQ &&
447 type != KM_BOUNCE_READ) {
448 WARN_ON(1);
449 warn_count--;
450 }
451 }
452 }
453
454 if (type == KM_IRQ0 || type == KM_IRQ1 || type == KM_BOUNCE_READ ||
455 type == KM_BIO_SRC_IRQ || type == KM_BIO_DST_IRQ) {
456 if (!irqs_disabled()) {
457 WARN_ON(1);
458 warn_count--;
459 }
460 } else if (type == KM_SOFTIRQ0 || type == KM_SOFTIRQ1) {
461 if (irq_count() == 0 && !irqs_disabled()) {
462 WARN_ON(1);
463 warn_count--;
464 }
465 }
466}
467
468#endif