xref: /openbmc/linux/mm/highmem.c (revision db7a94d6)
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 <linux/blktrace_api.h>
30 #include <asm/tlbflush.h>
31 
32 /*
33  * Virtual_count is not a pure "count".
34  *  0 means that it is not mapped, and has not been mapped
35  *    since a TLB flush - it is usable.
36  *  1 means that there are no users, but it has been mapped
37  *    since the last TLB flush - so we can't use it.
38  *  n means that there are (n-1) current users of it.
39  */
40 #ifdef CONFIG_HIGHMEM
41 
42 unsigned long totalhigh_pages __read_mostly;
43 EXPORT_SYMBOL(totalhigh_pages);
44 
45 unsigned int nr_free_highpages (void)
46 {
47 	pg_data_t *pgdat;
48 	unsigned int pages = 0;
49 
50 	for_each_online_pgdat(pgdat) {
51 		pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
52 			NR_FREE_PAGES);
53 		if (zone_movable_is_highmem())
54 			pages += zone_page_state(
55 					&pgdat->node_zones[ZONE_MOVABLE],
56 					NR_FREE_PAGES);
57 	}
58 
59 	return pages;
60 }
61 
62 static int pkmap_count[LAST_PKMAP];
63 static unsigned int last_pkmap_nr;
64 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
65 
66 pte_t * pkmap_page_table;
67 
68 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
69 
70 static void flush_all_zero_pkmaps(void)
71 {
72 	int i;
73 
74 	flush_cache_kmaps();
75 
76 	for (i = 0; i < LAST_PKMAP; i++) {
77 		struct page *page;
78 
79 		/*
80 		 * zero means we don't have anything to do,
81 		 * >1 means that it is still in use. Only
82 		 * a count of 1 means that it is free but
83 		 * needs to be unmapped
84 		 */
85 		if (pkmap_count[i] != 1)
86 			continue;
87 		pkmap_count[i] = 0;
88 
89 		/* sanity check */
90 		BUG_ON(pte_none(pkmap_page_table[i]));
91 
92 		/*
93 		 * Don't need an atomic fetch-and-clear op here;
94 		 * no-one has the page mapped, and cannot get at
95 		 * its virtual address (and hence PTE) without first
96 		 * getting the kmap_lock (which is held here).
97 		 * So no dangers, even with speculative execution.
98 		 */
99 		page = pte_page(pkmap_page_table[i]);
100 		pte_clear(&init_mm, (unsigned long)page_address(page),
101 			  &pkmap_page_table[i]);
102 
103 		set_page_address(page, NULL);
104 	}
105 	flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
106 }
107 
108 /**
109  * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
110  */
111 void kmap_flush_unused(void)
112 {
113 	spin_lock(&kmap_lock);
114 	flush_all_zero_pkmaps();
115 	spin_unlock(&kmap_lock);
116 }
117 
118 static inline unsigned long map_new_virtual(struct page *page)
119 {
120 	unsigned long vaddr;
121 	int count;
122 
123 start:
124 	count = LAST_PKMAP;
125 	/* Find an empty entry */
126 	for (;;) {
127 		last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
128 		if (!last_pkmap_nr) {
129 			flush_all_zero_pkmaps();
130 			count = LAST_PKMAP;
131 		}
132 		if (!pkmap_count[last_pkmap_nr])
133 			break;	/* Found a usable entry */
134 		if (--count)
135 			continue;
136 
137 		/*
138 		 * Sleep for somebody else to unmap their entries
139 		 */
140 		{
141 			DECLARE_WAITQUEUE(wait, current);
142 
143 			__set_current_state(TASK_UNINTERRUPTIBLE);
144 			add_wait_queue(&pkmap_map_wait, &wait);
145 			spin_unlock(&kmap_lock);
146 			schedule();
147 			remove_wait_queue(&pkmap_map_wait, &wait);
148 			spin_lock(&kmap_lock);
149 
150 			/* Somebody else might have mapped it while we slept */
151 			if (page_address(page))
152 				return (unsigned long)page_address(page);
153 
154 			/* Re-start */
155 			goto start;
156 		}
157 	}
158 	vaddr = PKMAP_ADDR(last_pkmap_nr);
159 	set_pte_at(&init_mm, vaddr,
160 		   &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
161 
162 	pkmap_count[last_pkmap_nr] = 1;
163 	set_page_address(page, (void *)vaddr);
164 
165 	return vaddr;
166 }
167 
168 /**
169  * kmap_high - map a highmem page into memory
170  * @page: &struct page to map
171  *
172  * Returns the page's virtual memory address.
173  *
174  * We cannot call this from interrupts, as it may block.
175  */
176 void *kmap_high(struct page *page)
177 {
178 	unsigned long vaddr;
179 
180 	/*
181 	 * For highmem pages, we can't trust "virtual" until
182 	 * after we have the lock.
183 	 */
184 	spin_lock(&kmap_lock);
185 	vaddr = (unsigned long)page_address(page);
186 	if (!vaddr)
187 		vaddr = map_new_virtual(page);
188 	pkmap_count[PKMAP_NR(vaddr)]++;
189 	BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
190 	spin_unlock(&kmap_lock);
191 	return (void*) vaddr;
192 }
193 
194 EXPORT_SYMBOL(kmap_high);
195 
196 /**
197  * kunmap_high - map a highmem page into memory
198  * @page: &struct page to unmap
199  */
200 void kunmap_high(struct page *page)
201 {
202 	unsigned long vaddr;
203 	unsigned long nr;
204 	int need_wakeup;
205 
206 	spin_lock(&kmap_lock);
207 	vaddr = (unsigned long)page_address(page);
208 	BUG_ON(!vaddr);
209 	nr = PKMAP_NR(vaddr);
210 
211 	/*
212 	 * A count must never go down to zero
213 	 * without a TLB flush!
214 	 */
215 	need_wakeup = 0;
216 	switch (--pkmap_count[nr]) {
217 	case 0:
218 		BUG();
219 	case 1:
220 		/*
221 		 * Avoid an unnecessary wake_up() function call.
222 		 * The common case is pkmap_count[] == 1, but
223 		 * no waiters.
224 		 * The tasks queued in the wait-queue are guarded
225 		 * by both the lock in the wait-queue-head and by
226 		 * the kmap_lock.  As the kmap_lock is held here,
227 		 * no need for the wait-queue-head's lock.  Simply
228 		 * test if the queue is empty.
229 		 */
230 		need_wakeup = waitqueue_active(&pkmap_map_wait);
231 	}
232 	spin_unlock(&kmap_lock);
233 
234 	/* do wake-up, if needed, race-free outside of the spin lock */
235 	if (need_wakeup)
236 		wake_up(&pkmap_map_wait);
237 }
238 
239 EXPORT_SYMBOL(kunmap_high);
240 #endif
241 
242 #if defined(HASHED_PAGE_VIRTUAL)
243 
244 #define PA_HASH_ORDER	7
245 
246 /*
247  * Describes one page->virtual association
248  */
249 struct page_address_map {
250 	struct page *page;
251 	void *virtual;
252 	struct list_head list;
253 };
254 
255 /*
256  * page_address_map freelist, allocated from page_address_maps.
257  */
258 static struct list_head page_address_pool;	/* freelist */
259 static spinlock_t pool_lock;			/* protects page_address_pool */
260 
261 /*
262  * Hash table bucket
263  */
264 static struct page_address_slot {
265 	struct list_head lh;			/* List of page_address_maps */
266 	spinlock_t lock;			/* Protect this bucket's list */
267 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
268 
269 static struct page_address_slot *page_slot(struct page *page)
270 {
271 	return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
272 }
273 
274 /**
275  * page_address - get the mapped virtual address of a page
276  * @page: &struct page to get the virtual address of
277  *
278  * Returns the page's virtual address.
279  */
280 void *page_address(struct page *page)
281 {
282 	unsigned long flags;
283 	void *ret;
284 	struct page_address_slot *pas;
285 
286 	if (!PageHighMem(page))
287 		return lowmem_page_address(page);
288 
289 	pas = page_slot(page);
290 	ret = NULL;
291 	spin_lock_irqsave(&pas->lock, flags);
292 	if (!list_empty(&pas->lh)) {
293 		struct page_address_map *pam;
294 
295 		list_for_each_entry(pam, &pas->lh, list) {
296 			if (pam->page == page) {
297 				ret = pam->virtual;
298 				goto done;
299 			}
300 		}
301 	}
302 done:
303 	spin_unlock_irqrestore(&pas->lock, flags);
304 	return ret;
305 }
306 
307 EXPORT_SYMBOL(page_address);
308 
309 /**
310  * set_page_address - set a page's virtual address
311  * @page: &struct page to set
312  * @virtual: virtual address to use
313  */
314 void set_page_address(struct page *page, void *virtual)
315 {
316 	unsigned long flags;
317 	struct page_address_slot *pas;
318 	struct page_address_map *pam;
319 
320 	BUG_ON(!PageHighMem(page));
321 
322 	pas = page_slot(page);
323 	if (virtual) {		/* Add */
324 		BUG_ON(list_empty(&page_address_pool));
325 
326 		spin_lock_irqsave(&pool_lock, flags);
327 		pam = list_entry(page_address_pool.next,
328 				struct page_address_map, list);
329 		list_del(&pam->list);
330 		spin_unlock_irqrestore(&pool_lock, flags);
331 
332 		pam->page = page;
333 		pam->virtual = virtual;
334 
335 		spin_lock_irqsave(&pas->lock, flags);
336 		list_add_tail(&pam->list, &pas->lh);
337 		spin_unlock_irqrestore(&pas->lock, flags);
338 	} else {		/* Remove */
339 		spin_lock_irqsave(&pas->lock, flags);
340 		list_for_each_entry(pam, &pas->lh, list) {
341 			if (pam->page == page) {
342 				list_del(&pam->list);
343 				spin_unlock_irqrestore(&pas->lock, flags);
344 				spin_lock_irqsave(&pool_lock, flags);
345 				list_add_tail(&pam->list, &page_address_pool);
346 				spin_unlock_irqrestore(&pool_lock, flags);
347 				goto done;
348 			}
349 		}
350 		spin_unlock_irqrestore(&pas->lock, flags);
351 	}
352 done:
353 	return;
354 }
355 
356 static struct page_address_map page_address_maps[LAST_PKMAP];
357 
358 void __init page_address_init(void)
359 {
360 	int i;
361 
362 	INIT_LIST_HEAD(&page_address_pool);
363 	for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
364 		list_add(&page_address_maps[i].list, &page_address_pool);
365 	for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
366 		INIT_LIST_HEAD(&page_address_htable[i].lh);
367 		spin_lock_init(&page_address_htable[i].lock);
368 	}
369 	spin_lock_init(&pool_lock);
370 }
371 
372 #endif	/* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */
373