Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / mm / highmem.c
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/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>
31
32 #ifndef CONFIG_PREEMPT_RT_FULL
33 #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
34 DEFINE_PER_CPU(int, __kmap_atomic_idx);
35 #endif
36 #endif
37
38 /*
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
47
48 /*
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.
52  */
53 #ifndef get_pkmap_color
54
55 /*
56  * Determine color of virtual address where the page should be mapped.
57  */
58 static inline unsigned int get_pkmap_color(struct page *page)
59 {
60         return 0;
61 }
62 #define get_pkmap_color get_pkmap_color
63
64 /*
65  * Get next index for mapping inside PKMAP region for page with given color.
66  */
67 static inline unsigned int get_next_pkmap_nr(unsigned int color)
68 {
69         static unsigned int last_pkmap_nr;
70
71         last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
72         return last_pkmap_nr;
73 }
74
75 /*
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.
79  */
80 static inline int no_more_pkmaps(unsigned int pkmap_nr, unsigned int color)
81 {
82         return pkmap_nr == 0;
83 }
84
85 /*
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.
89  */
90 static inline int get_pkmap_entries_count(unsigned int color)
91 {
92         return LAST_PKMAP;
93 }
94
95 /*
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.
99  */
100 static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
101 {
102         static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
103
104         return &pkmap_map_wait;
105 }
106 #endif
107
108 unsigned long totalhigh_pages __read_mostly;
109 EXPORT_SYMBOL(totalhigh_pages);
110
111 #ifndef CONFIG_PREEMPT_RT_FULL
112 EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
113 #endif
114
115 unsigned int nr_free_highpages (void)
116 {
117         pg_data_t *pgdat;
118         unsigned int pages = 0;
119
120         for_each_online_pgdat(pgdat) {
121                 pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
122                         NR_FREE_PAGES);
123                 if (zone_movable_is_highmem())
124                         pages += zone_page_state(
125                                         &pgdat->node_zones[ZONE_MOVABLE],
126                                         NR_FREE_PAGES);
127         }
128
129         return pages;
130 }
131
132 static int pkmap_count[LAST_PKMAP];
133 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
134
135 pte_t * pkmap_page_table;
136
137 /*
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.
141  */
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)
147 #else
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)
154 #endif
155
156 struct page *kmap_to_page(void *vaddr)
157 {
158         unsigned long addr = (unsigned long)vaddr;
159
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]);
163         }
164
165         return virt_to_page(addr);
166 }
167 EXPORT_SYMBOL(kmap_to_page);
168
169 static void flush_all_zero_pkmaps(void)
170 {
171         int i;
172         int need_flush = 0;
173
174         flush_cache_kmaps();
175
176         for (i = 0; i < LAST_PKMAP; i++) {
177                 struct page *page;
178
179                 /*
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
184                  */
185                 if (pkmap_count[i] != 1)
186                         continue;
187                 pkmap_count[i] = 0;
188
189                 /* sanity check */
190                 BUG_ON(pte_none(pkmap_page_table[i]));
191
192                 /*
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.
198                  */
199                 page = pte_page(pkmap_page_table[i]);
200                 pte_clear(&init_mm, PKMAP_ADDR(i), &pkmap_page_table[i]);
201
202                 set_page_address(page, NULL);
203                 need_flush = 1;
204         }
205         if (need_flush)
206                 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
207 }
208
209 /**
210  * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
211  */
212 void kmap_flush_unused(void)
213 {
214         lock_kmap();
215         flush_all_zero_pkmaps();
216         unlock_kmap();
217 }
218
219 static inline unsigned long map_new_virtual(struct page *page)
220 {
221         unsigned long vaddr;
222         int count;
223         unsigned int last_pkmap_nr;
224         unsigned int color = get_pkmap_color(page);
225
226 start:
227         count = get_pkmap_entries_count(color);
228         /* Find an empty entry */
229         for (;;) {
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);
234                 }
235                 if (!pkmap_count[last_pkmap_nr])
236                         break;  /* Found a usable entry */
237                 if (--count)
238                         continue;
239
240                 /*
241                  * Sleep for somebody else to unmap their entries
242                  */
243                 {
244                         DECLARE_WAITQUEUE(wait, current);
245                         wait_queue_head_t *pkmap_map_wait =
246                                 get_pkmap_wait_queue_head(color);
247
248                         __set_current_state(TASK_UNINTERRUPTIBLE);
249                         add_wait_queue(pkmap_map_wait, &wait);
250                         unlock_kmap();
251                         schedule();
252                         remove_wait_queue(pkmap_map_wait, &wait);
253                         lock_kmap();
254
255                         /* Somebody else might have mapped it while we slept */
256                         if (page_address(page))
257                                 return (unsigned long)page_address(page);
258
259                         /* Re-start */
260                         goto start;
261                 }
262         }
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));
266
267         pkmap_count[last_pkmap_nr] = 1;
268         set_page_address(page, (void *)vaddr);
269
270         return vaddr;
271 }
272
273 /**
274  * kmap_high - map a highmem page into memory
275  * @page: &struct page to map
276  *
277  * Returns the page's virtual memory address.
278  *
279  * We cannot call this from interrupts, as it may block.
280  */
281 void *kmap_high(struct page *page)
282 {
283         unsigned long vaddr;
284
285         /*
286          * For highmem pages, we can't trust "virtual" until
287          * after we have the lock.
288          */
289         lock_kmap();
290         vaddr = (unsigned long)page_address(page);
291         if (!vaddr)
292                 vaddr = map_new_virtual(page);
293         pkmap_count[PKMAP_NR(vaddr)]++;
294         BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
295         unlock_kmap();
296         return (void*) vaddr;
297 }
298
299 EXPORT_SYMBOL(kmap_high);
300
301 #ifdef ARCH_NEEDS_KMAP_HIGH_GET
302 /**
303  * kmap_high_get - pin a highmem page into memory
304  * @page: &struct page to pin
305  *
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.
309  *
310  * This can be called from any context.
311  */
312 void *kmap_high_get(struct page *page)
313 {
314         unsigned long vaddr, flags;
315
316         lock_kmap_any(flags);
317         vaddr = (unsigned long)page_address(page);
318         if (vaddr) {
319                 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 1);
320                 pkmap_count[PKMAP_NR(vaddr)]++;
321         }
322         unlock_kmap_any(flags);
323         return (void*) vaddr;
324 }
325 #endif
326
327 /**
328  * kunmap_high - unmap a highmem page into memory
329  * @page: &struct page to unmap
330  *
331  * If ARCH_NEEDS_KMAP_HIGH_GET is not defined then this may be called
332  * only from user context.
333  */
334 void kunmap_high(struct page *page)
335 {
336         unsigned long vaddr;
337         unsigned long nr;
338         unsigned long flags;
339         int need_wakeup;
340         unsigned int color = get_pkmap_color(page);
341         wait_queue_head_t *pkmap_map_wait;
342
343         lock_kmap_any(flags);
344         vaddr = (unsigned long)page_address(page);
345         BUG_ON(!vaddr);
346         nr = PKMAP_NR(vaddr);
347
348         /*
349          * A count must never go down to zero
350          * without a TLB flush!
351          */
352         need_wakeup = 0;
353         switch (--pkmap_count[nr]) {
354         case 0:
355                 BUG();
356         case 1:
357                 /*
358                  * Avoid an unnecessary wake_up() function call.
359                  * The common case is pkmap_count[] == 1, but
360                  * no waiters.
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.
366                  */
367                 pkmap_map_wait = get_pkmap_wait_queue_head(color);
368                 need_wakeup = waitqueue_active(pkmap_map_wait);
369         }
370         unlock_kmap_any(flags);
371
372         /* do wake-up, if needed, race-free outside of the spin lock */
373         if (need_wakeup)
374                 wake_up(pkmap_map_wait);
375 }
376
377 EXPORT_SYMBOL(kunmap_high);
378 #endif
379
380 #if defined(HASHED_PAGE_VIRTUAL)
381
382 #define PA_HASH_ORDER   7
383
384 /*
385  * Describes one page->virtual association
386  */
387 struct page_address_map {
388         struct page *page;
389         void *virtual;
390         struct list_head list;
391 };
392
393 static struct page_address_map page_address_maps[LAST_PKMAP];
394
395 /*
396  * Hash table bucket
397  */
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];
402
403 static struct page_address_slot *page_slot(const struct page *page)
404 {
405         return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
406 }
407
408 /**
409  * page_address - get the mapped virtual address of a page
410  * @page: &struct page to get the virtual address of
411  *
412  * Returns the page's virtual address.
413  */
414 void *page_address(const struct page *page)
415 {
416         unsigned long flags;
417         void *ret;
418         struct page_address_slot *pas;
419
420         if (!PageHighMem(page))
421                 return lowmem_page_address(page);
422
423         pas = page_slot(page);
424         ret = NULL;
425         spin_lock_irqsave(&pas->lock, flags);
426         if (!list_empty(&pas->lh)) {
427                 struct page_address_map *pam;
428
429                 list_for_each_entry(pam, &pas->lh, list) {
430                         if (pam->page == page) {
431                                 ret = pam->virtual;
432                                 goto done;
433                         }
434                 }
435         }
436 done:
437         spin_unlock_irqrestore(&pas->lock, flags);
438         return ret;
439 }
440
441 EXPORT_SYMBOL(page_address);
442
443 /**
444  * set_page_address - set a page's virtual address
445  * @page: &struct page to set
446  * @virtual: virtual address to use
447  */
448 void set_page_address(struct page *page, void *virtual)
449 {
450         unsigned long flags;
451         struct page_address_slot *pas;
452         struct page_address_map *pam;
453
454         BUG_ON(!PageHighMem(page));
455
456         pas = page_slot(page);
457         if (virtual) {          /* Add */
458                 pam = &page_address_maps[PKMAP_NR((unsigned long)virtual)];
459                 pam->page = page;
460                 pam->virtual = virtual;
461
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);
471                                 goto done;
472                         }
473                 }
474                 spin_unlock_irqrestore(&pas->lock, flags);
475         }
476 done:
477         return;
478 }
479
480 void __init page_address_init(void)
481 {
482         int i;
483
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);
487         }
488 }
489
490 #endif  /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */