1 /*	$NetBSD: ttm_page_alloc.c,v 1.3 2021/12/18 23:45:44 riastradh Exp $	*/
2 
3 /*
4  * Copyright (c) Red Hat Inc.
5 
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sub license,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial portions
15  * of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors: Dave Airlie <airlied@redhat.com>
26  *          Jerome Glisse <jglisse@redhat.com>
27  *          Pauli Nieminen <suokkos@gmail.com>
28  */
29 
30 /* simple list based uncached page pool
31  * - Pool collects resently freed pages for reuse
32  * - Use page->lru to keep a free list
33  * - doesn't track currently in use pages
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ttm_page_alloc.c,v 1.3 2021/12/18 23:45:44 riastradh Exp $");
38 
39 #define pr_fmt(fmt) "[TTM] " fmt
40 
41 #include <linux/list.h>
42 #include <linux/spinlock.h>
43 #include <linux/highmem.h>
44 #include <linux/mm_types.h>
45 #include <linux/module.h>
46 #include <linux/mm.h>
47 #include <linux/seq_file.h> /* for seq_printf */
48 #include <linux/slab.h>
49 #include <linux/dma-mapping.h>
50 
51 #include <linux/atomic.h>
52 
53 #include <drm/ttm/ttm_bo_driver.h>
54 #include <drm/ttm/ttm_page_alloc.h>
55 #include <drm/ttm/ttm_set_memory.h>
56 
57 #define NUM_PAGES_TO_ALLOC		(PAGE_SIZE/sizeof(struct page *))
58 #define SMALL_ALLOCATION		16
59 #define FREE_ALL_PAGES			(~0U)
60 /* times are in msecs */
61 #define PAGE_FREE_INTERVAL		1000
62 
63 /**
64  * struct ttm_page_pool - Pool to reuse recently allocated uc/wc pages.
65  *
66  * @lock: Protects the shared pool from concurrnet access. Must be used with
67  * irqsave/irqrestore variants because pool allocator maybe called from
68  * delayed work.
69  * @fill_lock: Prevent concurrent calls to fill.
70  * @list: Pool of free uc/wc pages for fast reuse.
71  * @gfp_flags: Flags to pass for alloc_page.
72  * @npages: Number of pages in pool.
73  */
74 struct ttm_page_pool {
75 	spinlock_t		lock;
76 	bool			fill_lock;
77 	struct list_head	list;
78 	gfp_t			gfp_flags;
79 	unsigned		npages;
80 	char			*name;
81 	unsigned long		nfrees;
82 	unsigned long		nrefills;
83 	unsigned int		order;
84 };
85 
86 /**
87  * Limits for the pool. They are handled without locks because only place where
88  * they may change is in sysfs store. They won't have immediate effect anyway
89  * so forcing serialization to access them is pointless.
90  */
91 
92 struct ttm_pool_opts {
93 	unsigned	alloc_size;
94 	unsigned	max_size;
95 	unsigned	small;
96 };
97 
98 #define NUM_POOLS 6
99 
100 /**
101  * struct ttm_pool_manager - Holds memory pools for fst allocation
102  *
103  * Manager is read only object for pool code so it doesn't need locking.
104  *
105  * @free_interval: minimum number of jiffies between freeing pages from pool.
106  * @page_alloc_inited: reference counting for pool allocation.
107  * @work: Work that is used to shrink the pool. Work is only run when there is
108  * some pages to free.
109  * @small_allocation: Limit in number of pages what is small allocation.
110  *
111  * @pools: All pool objects in use.
112  **/
113 struct ttm_pool_manager {
114 	struct kobject		kobj;
115 	struct shrinker		mm_shrink;
116 	struct ttm_pool_opts	options;
117 
118 	union {
119 		struct ttm_page_pool	pools[NUM_POOLS];
120 		struct {
121 			struct ttm_page_pool	wc_pool;
122 			struct ttm_page_pool	uc_pool;
123 			struct ttm_page_pool	wc_pool_dma32;
124 			struct ttm_page_pool	uc_pool_dma32;
125 			struct ttm_page_pool	wc_pool_huge;
126 			struct ttm_page_pool	uc_pool_huge;
127 		} ;
128 	};
129 };
130 
131 static struct attribute ttm_page_pool_max = {
132 	.name = "pool_max_size",
133 	.mode = S_IRUGO | S_IWUSR
134 };
135 static struct attribute ttm_page_pool_small = {
136 	.name = "pool_small_allocation",
137 	.mode = S_IRUGO | S_IWUSR
138 };
139 static struct attribute ttm_page_pool_alloc_size = {
140 	.name = "pool_allocation_size",
141 	.mode = S_IRUGO | S_IWUSR
142 };
143 
144 static struct attribute *ttm_pool_attrs[] = {
145 	&ttm_page_pool_max,
146 	&ttm_page_pool_small,
147 	&ttm_page_pool_alloc_size,
148 	NULL
149 };
150 
ttm_pool_kobj_release(struct kobject * kobj)151 static void ttm_pool_kobj_release(struct kobject *kobj)
152 {
153 	struct ttm_pool_manager *m =
154 		container_of(kobj, struct ttm_pool_manager, kobj);
155 	kfree(m);
156 }
157 
ttm_pool_store(struct kobject * kobj,struct attribute * attr,const char * buffer,size_t size)158 static ssize_t ttm_pool_store(struct kobject *kobj,
159 		struct attribute *attr, const char *buffer, size_t size)
160 {
161 	struct ttm_pool_manager *m =
162 		container_of(kobj, struct ttm_pool_manager, kobj);
163 	int chars;
164 	unsigned val;
165 	chars = sscanf(buffer, "%u", &val);
166 	if (chars == 0)
167 		return size;
168 
169 	/* Convert kb to number of pages */
170 	val = val / (PAGE_SIZE >> 10);
171 
172 	if (attr == &ttm_page_pool_max)
173 		m->options.max_size = val;
174 	else if (attr == &ttm_page_pool_small)
175 		m->options.small = val;
176 	else if (attr == &ttm_page_pool_alloc_size) {
177 		if (val > NUM_PAGES_TO_ALLOC*8) {
178 			pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
179 			       NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
180 			       NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
181 			return size;
182 		} else if (val > NUM_PAGES_TO_ALLOC) {
183 			pr_warn("Setting allocation size to larger than %lu is not recommended\n",
184 				NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
185 		}
186 		m->options.alloc_size = val;
187 	}
188 
189 	return size;
190 }
191 
ttm_pool_show(struct kobject * kobj,struct attribute * attr,char * buffer)192 static ssize_t ttm_pool_show(struct kobject *kobj,
193 		struct attribute *attr, char *buffer)
194 {
195 	struct ttm_pool_manager *m =
196 		container_of(kobj, struct ttm_pool_manager, kobj);
197 	unsigned val = 0;
198 
199 	if (attr == &ttm_page_pool_max)
200 		val = m->options.max_size;
201 	else if (attr == &ttm_page_pool_small)
202 		val = m->options.small;
203 	else if (attr == &ttm_page_pool_alloc_size)
204 		val = m->options.alloc_size;
205 
206 	val = val * (PAGE_SIZE >> 10);
207 
208 	return snprintf(buffer, PAGE_SIZE, "%u\n", val);
209 }
210 
211 static const struct sysfs_ops ttm_pool_sysfs_ops = {
212 	.show = &ttm_pool_show,
213 	.store = &ttm_pool_store,
214 };
215 
216 static struct kobj_type ttm_pool_kobj_type = {
217 	.release = &ttm_pool_kobj_release,
218 	.sysfs_ops = &ttm_pool_sysfs_ops,
219 	.default_attrs = ttm_pool_attrs,
220 };
221 
222 static struct ttm_pool_manager *_manager;
223 
224 /**
225  * Select the right pool or requested caching state and ttm flags. */
ttm_get_pool(int flags,bool huge,enum ttm_caching_state cstate)226 static struct ttm_page_pool *ttm_get_pool(int flags, bool huge,
227 					  enum ttm_caching_state cstate)
228 {
229 	int pool_index;
230 
231 	if (cstate == tt_cached)
232 		return NULL;
233 
234 	if (cstate == tt_wc)
235 		pool_index = 0x0;
236 	else
237 		pool_index = 0x1;
238 
239 	if (flags & TTM_PAGE_FLAG_DMA32) {
240 		if (huge)
241 			return NULL;
242 		pool_index |= 0x2;
243 
244 	} else if (huge) {
245 		pool_index |= 0x4;
246 	}
247 
248 	return &_manager->pools[pool_index];
249 }
250 
251 /* set memory back to wb and free the pages. */
ttm_pages_put(struct page * pages[],unsigned npages,unsigned int order)252 static void ttm_pages_put(struct page *pages[], unsigned npages,
253 		unsigned int order)
254 {
255 	unsigned int i, pages_nr = (1 << order);
256 
257 	if (order == 0) {
258 		if (ttm_set_pages_array_wb(pages, npages))
259 			pr_err("Failed to set %d pages to wb!\n", npages);
260 	}
261 
262 	for (i = 0; i < npages; ++i) {
263 		if (order > 0) {
264 			if (ttm_set_pages_wb(pages[i], pages_nr))
265 				pr_err("Failed to set %d pages to wb!\n", pages_nr);
266 		}
267 		__free_pages(pages[i], order);
268 	}
269 }
270 
ttm_pool_update_free_locked(struct ttm_page_pool * pool,unsigned freed_pages)271 static void ttm_pool_update_free_locked(struct ttm_page_pool *pool,
272 		unsigned freed_pages)
273 {
274 	pool->npages -= freed_pages;
275 	pool->nfrees += freed_pages;
276 }
277 
278 /**
279  * Free pages from pool.
280  *
281  * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
282  * number of pages in one go.
283  *
284  * @pool: to free the pages from
285  * @free_all: If set to true will free all pages in pool
286  * @use_static: Safe to use static buffer
287  **/
ttm_page_pool_free(struct ttm_page_pool * pool,unsigned nr_free,bool use_static)288 static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free,
289 			      bool use_static)
290 {
291 	static struct page *static_buf[NUM_PAGES_TO_ALLOC];
292 	unsigned long irq_flags;
293 	struct page *p;
294 	struct page **pages_to_free;
295 	unsigned freed_pages = 0,
296 		 npages_to_free = nr_free;
297 
298 	if (NUM_PAGES_TO_ALLOC < nr_free)
299 		npages_to_free = NUM_PAGES_TO_ALLOC;
300 
301 	if (use_static)
302 		pages_to_free = static_buf;
303 	else
304 		pages_to_free = kmalloc_array(npages_to_free,
305 					      sizeof(struct page *),
306 					      GFP_KERNEL);
307 	if (!pages_to_free) {
308 		pr_debug("Failed to allocate memory for pool free operation\n");
309 		return 0;
310 	}
311 
312 restart:
313 	spin_lock_irqsave(&pool->lock, irq_flags);
314 
315 	list_for_each_entry_reverse(p, &pool->list, lru) {
316 		if (freed_pages >= npages_to_free)
317 			break;
318 
319 		pages_to_free[freed_pages++] = p;
320 		/* We can only remove NUM_PAGES_TO_ALLOC at a time. */
321 		if (freed_pages >= NUM_PAGES_TO_ALLOC) {
322 			/* remove range of pages from the pool */
323 			__list_del(p->lru.prev, &pool->list);
324 
325 			ttm_pool_update_free_locked(pool, freed_pages);
326 			/**
327 			 * Because changing page caching is costly
328 			 * we unlock the pool to prevent stalling.
329 			 */
330 			spin_unlock_irqrestore(&pool->lock, irq_flags);
331 
332 			ttm_pages_put(pages_to_free, freed_pages, pool->order);
333 			if (likely(nr_free != FREE_ALL_PAGES))
334 				nr_free -= freed_pages;
335 
336 			if (NUM_PAGES_TO_ALLOC >= nr_free)
337 				npages_to_free = nr_free;
338 			else
339 				npages_to_free = NUM_PAGES_TO_ALLOC;
340 
341 			freed_pages = 0;
342 
343 			/* free all so restart the processing */
344 			if (nr_free)
345 				goto restart;
346 
347 			/* Not allowed to fall through or break because
348 			 * following context is inside spinlock while we are
349 			 * outside here.
350 			 */
351 			goto out;
352 
353 		}
354 	}
355 
356 	/* remove range of pages from the pool */
357 	if (freed_pages) {
358 		__list_del(&p->lru, &pool->list);
359 
360 		ttm_pool_update_free_locked(pool, freed_pages);
361 		nr_free -= freed_pages;
362 	}
363 
364 	spin_unlock_irqrestore(&pool->lock, irq_flags);
365 
366 	if (freed_pages)
367 		ttm_pages_put(pages_to_free, freed_pages, pool->order);
368 out:
369 	if (pages_to_free != static_buf)
370 		kfree(pages_to_free);
371 	return nr_free;
372 }
373 
374 /**
375  * Callback for mm to request pool to reduce number of page held.
376  *
377  * XXX: (dchinner) Deadlock warning!
378  *
379  * This code is crying out for a shrinker per pool....
380  */
381 static unsigned long
ttm_pool_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)382 ttm_pool_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
383 {
384 	static DEFINE_MUTEX(lock);
385 	static unsigned start_pool;
386 	unsigned i;
387 	unsigned pool_offset;
388 	struct ttm_page_pool *pool;
389 	int shrink_pages = sc->nr_to_scan;
390 	unsigned long freed = 0;
391 	unsigned int nr_free_pool;
392 
393 	if (!mutex_trylock(&lock))
394 		return SHRINK_STOP;
395 	pool_offset = ++start_pool % NUM_POOLS;
396 	/* select start pool in round robin fashion */
397 	for (i = 0; i < NUM_POOLS; ++i) {
398 		unsigned nr_free = shrink_pages;
399 		unsigned page_nr;
400 
401 		if (shrink_pages == 0)
402 			break;
403 
404 		pool = &_manager->pools[(i + pool_offset)%NUM_POOLS];
405 		page_nr = (1 << pool->order);
406 		/* OK to use static buffer since global mutex is held. */
407 		nr_free_pool = roundup(nr_free, page_nr) >> pool->order;
408 		shrink_pages = ttm_page_pool_free(pool, nr_free_pool, true);
409 		freed += (nr_free_pool - shrink_pages) << pool->order;
410 		if (freed >= sc->nr_to_scan)
411 			break;
412 		shrink_pages <<= pool->order;
413 	}
414 	mutex_unlock(&lock);
415 	return freed;
416 }
417 
418 
419 static unsigned long
ttm_pool_shrink_count(struct shrinker * shrink,struct shrink_control * sc)420 ttm_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
421 {
422 	unsigned i;
423 	unsigned long count = 0;
424 	struct ttm_page_pool *pool;
425 
426 	for (i = 0; i < NUM_POOLS; ++i) {
427 		pool = &_manager->pools[i];
428 		count += (pool->npages << pool->order);
429 	}
430 
431 	return count;
432 }
433 
ttm_pool_mm_shrink_init(struct ttm_pool_manager * manager)434 static int ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
435 {
436 	manager->mm_shrink.count_objects = ttm_pool_shrink_count;
437 	manager->mm_shrink.scan_objects = ttm_pool_shrink_scan;
438 	manager->mm_shrink.seeks = 1;
439 	return register_shrinker(&manager->mm_shrink);
440 }
441 
ttm_pool_mm_shrink_fini(struct ttm_pool_manager * manager)442 static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
443 {
444 	unregister_shrinker(&manager->mm_shrink);
445 }
446 
ttm_set_pages_caching(struct page ** pages,enum ttm_caching_state cstate,unsigned cpages)447 static int ttm_set_pages_caching(struct page **pages,
448 		enum ttm_caching_state cstate, unsigned cpages)
449 {
450 	int r = 0;
451 	/* Set page caching */
452 	switch (cstate) {
453 	case tt_uncached:
454 		r = ttm_set_pages_array_uc(pages, cpages);
455 		if (r)
456 			pr_err("Failed to set %d pages to uc!\n", cpages);
457 		break;
458 	case tt_wc:
459 		r = ttm_set_pages_array_wc(pages, cpages);
460 		if (r)
461 			pr_err("Failed to set %d pages to wc!\n", cpages);
462 		break;
463 	default:
464 		break;
465 	}
466 	return r;
467 }
468 
469 /**
470  * Free pages the pages that failed to change the caching state. If there is
471  * any pages that have changed their caching state already put them to the
472  * pool.
473  */
ttm_handle_caching_state_failure(struct list_head * pages,int ttm_flags,enum ttm_caching_state cstate,struct page ** failed_pages,unsigned cpages)474 static void ttm_handle_caching_state_failure(struct list_head *pages,
475 		int ttm_flags, enum ttm_caching_state cstate,
476 		struct page **failed_pages, unsigned cpages)
477 {
478 	unsigned i;
479 	/* Failed pages have to be freed */
480 	for (i = 0; i < cpages; ++i) {
481 		list_del(&failed_pages[i]->lru);
482 		__free_page(failed_pages[i]);
483 	}
484 }
485 
486 /**
487  * Allocate new pages with correct caching.
488  *
489  * This function is reentrant if caller updates count depending on number of
490  * pages returned in pages array.
491  */
ttm_alloc_new_pages(struct list_head * pages,gfp_t gfp_flags,int ttm_flags,enum ttm_caching_state cstate,unsigned count,unsigned order)492 static int ttm_alloc_new_pages(struct list_head *pages, gfp_t gfp_flags,
493 			       int ttm_flags, enum ttm_caching_state cstate,
494 			       unsigned count, unsigned order)
495 {
496 	struct page **caching_array;
497 	struct page *p;
498 	int r = 0;
499 	unsigned i, j, cpages;
500 	unsigned npages = 1 << order;
501 	unsigned max_cpages = min(count << order, (unsigned)NUM_PAGES_TO_ALLOC);
502 
503 	/* allocate array for page caching change */
504 	caching_array = kmalloc_array(max_cpages, sizeof(struct page *),
505 				      GFP_KERNEL);
506 
507 	if (!caching_array) {
508 		pr_debug("Unable to allocate table for new pages\n");
509 		return -ENOMEM;
510 	}
511 
512 	for (i = 0, cpages = 0; i < count; ++i) {
513 		p = alloc_pages(gfp_flags, order);
514 
515 		if (!p) {
516 			pr_debug("Unable to get page %u\n", i);
517 
518 			/* store already allocated pages in the pool after
519 			 * setting the caching state */
520 			if (cpages) {
521 				r = ttm_set_pages_caching(caching_array,
522 							  cstate, cpages);
523 				if (r)
524 					ttm_handle_caching_state_failure(pages,
525 						ttm_flags, cstate,
526 						caching_array, cpages);
527 			}
528 			r = -ENOMEM;
529 			goto out;
530 		}
531 
532 		list_add(&p->lru, pages);
533 
534 #ifdef CONFIG_HIGHMEM
535 		/* gfp flags of highmem page should never be dma32 so we
536 		 * we should be fine in such case
537 		 */
538 		if (PageHighMem(p))
539 			continue;
540 
541 #endif
542 		for (j = 0; j < npages; ++j) {
543 			caching_array[cpages++] = p++;
544 			if (cpages == max_cpages) {
545 
546 				r = ttm_set_pages_caching(caching_array,
547 						cstate, cpages);
548 				if (r) {
549 					ttm_handle_caching_state_failure(pages,
550 						ttm_flags, cstate,
551 						caching_array, cpages);
552 					goto out;
553 				}
554 				cpages = 0;
555 			}
556 		}
557 	}
558 
559 	if (cpages) {
560 		r = ttm_set_pages_caching(caching_array, cstate, cpages);
561 		if (r)
562 			ttm_handle_caching_state_failure(pages,
563 					ttm_flags, cstate,
564 					caching_array, cpages);
565 	}
566 out:
567 	kfree(caching_array);
568 
569 	return r;
570 }
571 
572 /**
573  * Fill the given pool if there aren't enough pages and the requested number of
574  * pages is small.
575  */
ttm_page_pool_fill_locked(struct ttm_page_pool * pool,int ttm_flags,enum ttm_caching_state cstate,unsigned count,unsigned long * irq_flags)576 static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool, int ttm_flags,
577 				      enum ttm_caching_state cstate,
578 				      unsigned count, unsigned long *irq_flags)
579 {
580 	struct page *p;
581 	int r;
582 	unsigned cpages = 0;
583 	/**
584 	 * Only allow one pool fill operation at a time.
585 	 * If pool doesn't have enough pages for the allocation new pages are
586 	 * allocated from outside of pool.
587 	 */
588 	if (pool->fill_lock)
589 		return;
590 
591 	pool->fill_lock = true;
592 
593 	/* If allocation request is small and there are not enough
594 	 * pages in a pool we fill the pool up first. */
595 	if (count < _manager->options.small
596 		&& count > pool->npages) {
597 		struct list_head new_pages;
598 		unsigned alloc_size = _manager->options.alloc_size;
599 
600 		/**
601 		 * Can't change page caching if in irqsave context. We have to
602 		 * drop the pool->lock.
603 		 */
604 		spin_unlock_irqrestore(&pool->lock, *irq_flags);
605 
606 		INIT_LIST_HEAD(&new_pages);
607 		r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags,
608 					cstate, alloc_size, 0);
609 		spin_lock_irqsave(&pool->lock, *irq_flags);
610 
611 		if (!r) {
612 			list_splice(&new_pages, &pool->list);
613 			++pool->nrefills;
614 			pool->npages += alloc_size;
615 		} else {
616 			pr_debug("Failed to fill pool (%p)\n", pool);
617 			/* If we have any pages left put them to the pool. */
618 			list_for_each_entry(p, &new_pages, lru) {
619 				++cpages;
620 			}
621 			list_splice(&new_pages, &pool->list);
622 			pool->npages += cpages;
623 		}
624 
625 	}
626 	pool->fill_lock = false;
627 }
628 
629 /**
630  * Allocate pages from the pool and put them on the return list.
631  *
632  * @return zero for success or negative error code.
633  */
ttm_page_pool_get_pages(struct ttm_page_pool * pool,struct list_head * pages,int ttm_flags,enum ttm_caching_state cstate,unsigned count,unsigned order)634 static int ttm_page_pool_get_pages(struct ttm_page_pool *pool,
635 				   struct list_head *pages,
636 				   int ttm_flags,
637 				   enum ttm_caching_state cstate,
638 				   unsigned count, unsigned order)
639 {
640 	unsigned long irq_flags;
641 	struct list_head *p;
642 	unsigned i;
643 	int r = 0;
644 
645 	spin_lock_irqsave(&pool->lock, irq_flags);
646 	if (!order)
647 		ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count,
648 					  &irq_flags);
649 
650 	if (count >= pool->npages) {
651 		/* take all pages from the pool */
652 		list_splice_init(&pool->list, pages);
653 		count -= pool->npages;
654 		pool->npages = 0;
655 		goto out;
656 	}
657 	/* find the last pages to include for requested number of pages. Split
658 	 * pool to begin and halve it to reduce search space. */
659 	if (count <= pool->npages/2) {
660 		i = 0;
661 		list_for_each(p, &pool->list) {
662 			if (++i == count)
663 				break;
664 		}
665 	} else {
666 		i = pool->npages + 1;
667 		list_for_each_prev(p, &pool->list) {
668 			if (--i == count)
669 				break;
670 		}
671 	}
672 	/* Cut 'count' number of pages from the pool */
673 	list_cut_position(pages, &pool->list, p);
674 	pool->npages -= count;
675 	count = 0;
676 out:
677 	spin_unlock_irqrestore(&pool->lock, irq_flags);
678 
679 	/* clear the pages coming from the pool if requested */
680 	if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
681 		struct page *page;
682 
683 		list_for_each_entry(page, pages, lru) {
684 			if (PageHighMem(page))
685 				clear_highpage(page);
686 			else
687 				clear_page(page_address(page));
688 		}
689 	}
690 
691 	/* If pool didn't have enough pages allocate new one. */
692 	if (count) {
693 		gfp_t gfp_flags = pool->gfp_flags;
694 
695 		/* set zero flag for page allocation if required */
696 		if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
697 			gfp_flags |= __GFP_ZERO;
698 
699 		if (ttm_flags & TTM_PAGE_FLAG_NO_RETRY)
700 			gfp_flags |= __GFP_RETRY_MAYFAIL;
701 
702 		/* ttm_alloc_new_pages doesn't reference pool so we can run
703 		 * multiple requests in parallel.
704 		 **/
705 		r = ttm_alloc_new_pages(pages, gfp_flags, ttm_flags, cstate,
706 					count, order);
707 	}
708 
709 	return r;
710 }
711 
712 /* Put all pages in pages list to correct pool to wait for reuse */
ttm_put_pages(struct page ** pages,unsigned npages,int flags,enum ttm_caching_state cstate)713 static void ttm_put_pages(struct page **pages, unsigned npages, int flags,
714 			  enum ttm_caching_state cstate)
715 {
716 	struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
717 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
718 	struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
719 #endif
720 	unsigned long irq_flags;
721 	unsigned i;
722 
723 	if (pool == NULL) {
724 		/* No pool for this memory type so free the pages */
725 		i = 0;
726 		while (i < npages) {
727 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
728 			struct page *p = pages[i];
729 #endif
730 			unsigned order = 0, j;
731 
732 			if (!pages[i]) {
733 				++i;
734 				continue;
735 			}
736 
737 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
738 			if (!(flags & TTM_PAGE_FLAG_DMA32) &&
739 			    (npages - i) >= HPAGE_PMD_NR) {
740 				for (j = 1; j < HPAGE_PMD_NR; ++j)
741 					if (++p != pages[i + j])
742 					    break;
743 
744 				if (j == HPAGE_PMD_NR)
745 					order = HPAGE_PMD_ORDER;
746 			}
747 #endif
748 
749 			if (page_count(pages[i]) != 1)
750 				pr_err("Erroneous page count. Leaking pages.\n");
751 			__free_pages(pages[i], order);
752 
753 			j = 1 << order;
754 			while (j) {
755 				pages[i++] = NULL;
756 				--j;
757 			}
758 		}
759 		return;
760 	}
761 
762 	i = 0;
763 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
764 	if (huge) {
765 		unsigned max_size, n2free;
766 
767 		spin_lock_irqsave(&huge->lock, irq_flags);
768 		while ((npages - i) >= HPAGE_PMD_NR) {
769 			struct page *p = pages[i];
770 			unsigned j;
771 
772 			if (!p)
773 				break;
774 
775 			for (j = 1; j < HPAGE_PMD_NR; ++j)
776 				if (++p != pages[i + j])
777 				    break;
778 
779 			if (j != HPAGE_PMD_NR)
780 				break;
781 
782 			list_add_tail(&pages[i]->lru, &huge->list);
783 
784 			for (j = 0; j < HPAGE_PMD_NR; ++j)
785 				pages[i++] = NULL;
786 			huge->npages++;
787 		}
788 
789 		/* Check that we don't go over the pool limit */
790 		max_size = _manager->options.max_size;
791 		max_size /= HPAGE_PMD_NR;
792 		if (huge->npages > max_size)
793 			n2free = huge->npages - max_size;
794 		else
795 			n2free = 0;
796 		spin_unlock_irqrestore(&huge->lock, irq_flags);
797 		if (n2free)
798 			ttm_page_pool_free(huge, n2free, false);
799 	}
800 #endif
801 
802 	spin_lock_irqsave(&pool->lock, irq_flags);
803 	while (i < npages) {
804 		if (pages[i]) {
805 			if (page_count(pages[i]) != 1)
806 				pr_err("Erroneous page count. Leaking pages.\n");
807 			list_add_tail(&pages[i]->lru, &pool->list);
808 			pages[i] = NULL;
809 			pool->npages++;
810 		}
811 		++i;
812 	}
813 	/* Check that we don't go over the pool limit */
814 	npages = 0;
815 	if (pool->npages > _manager->options.max_size) {
816 		npages = pool->npages - _manager->options.max_size;
817 		/* free at least NUM_PAGES_TO_ALLOC number of pages
818 		 * to reduce calls to set_memory_wb */
819 		if (npages < NUM_PAGES_TO_ALLOC)
820 			npages = NUM_PAGES_TO_ALLOC;
821 	}
822 	spin_unlock_irqrestore(&pool->lock, irq_flags);
823 	if (npages)
824 		ttm_page_pool_free(pool, npages, false);
825 }
826 
827 /*
828  * On success pages list will hold count number of correctly
829  * cached pages.
830  */
ttm_get_pages(struct page ** pages,unsigned npages,int flags,enum ttm_caching_state cstate)831 static int ttm_get_pages(struct page **pages, unsigned npages, int flags,
832 			 enum ttm_caching_state cstate)
833 {
834 	struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
835 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
836 	struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
837 #endif
838 	struct list_head plist;
839 	struct page *p = NULL;
840 	unsigned count, first;
841 	int r;
842 
843 	/* No pool for cached pages */
844 	if (pool == NULL) {
845 		gfp_t gfp_flags = GFP_USER;
846 		unsigned i;
847 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
848 		unsigned j;
849 #endif
850 
851 		/* set zero flag for page allocation if required */
852 		if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
853 			gfp_flags |= __GFP_ZERO;
854 
855 		if (flags & TTM_PAGE_FLAG_NO_RETRY)
856 			gfp_flags |= __GFP_RETRY_MAYFAIL;
857 
858 		if (flags & TTM_PAGE_FLAG_DMA32)
859 			gfp_flags |= GFP_DMA32;
860 		else
861 			gfp_flags |= GFP_HIGHUSER;
862 
863 		i = 0;
864 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
865 		if (!(gfp_flags & GFP_DMA32)) {
866 			while (npages >= HPAGE_PMD_NR) {
867 				gfp_t huge_flags = gfp_flags;
868 
869 				huge_flags |= GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
870 					__GFP_KSWAPD_RECLAIM;
871 				huge_flags &= ~__GFP_MOVABLE;
872 				huge_flags &= ~__GFP_COMP;
873 				p = alloc_pages(huge_flags, HPAGE_PMD_ORDER);
874 				if (!p)
875 					break;
876 
877 				for (j = 0; j < HPAGE_PMD_NR; ++j)
878 					pages[i++] = p++;
879 
880 				npages -= HPAGE_PMD_NR;
881 			}
882 		}
883 #endif
884 
885 		first = i;
886 		while (npages) {
887 			p = alloc_page(gfp_flags);
888 			if (!p) {
889 				pr_debug("Unable to allocate page\n");
890 				return -ENOMEM;
891 			}
892 
893 			/* Swap the pages if we detect consecutive order */
894 			if (i > first && pages[i - 1] == p - 1)
895 				swap(p, pages[i - 1]);
896 
897 			pages[i++] = p;
898 			--npages;
899 		}
900 		return 0;
901 	}
902 
903 	count = 0;
904 
905 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
906 	if (huge && npages >= HPAGE_PMD_NR) {
907 		INIT_LIST_HEAD(&plist);
908 		ttm_page_pool_get_pages(huge, &plist, flags, cstate,
909 					npages / HPAGE_PMD_NR,
910 					HPAGE_PMD_ORDER);
911 
912 		list_for_each_entry(p, &plist, lru) {
913 			unsigned j;
914 
915 			for (j = 0; j < HPAGE_PMD_NR; ++j)
916 				pages[count++] = &p[j];
917 		}
918 	}
919 #endif
920 
921 	INIT_LIST_HEAD(&plist);
922 	r = ttm_page_pool_get_pages(pool, &plist, flags, cstate,
923 				    npages - count, 0);
924 
925 	first = count;
926 	list_for_each_entry(p, &plist, lru) {
927 		struct page *tmp = p;
928 
929 		/* Swap the pages if we detect consecutive order */
930 		if (count > first && pages[count - 1] == tmp - 1)
931 			swap(tmp, pages[count - 1]);
932 		pages[count++] = tmp;
933 	}
934 
935 	if (r) {
936 		/* If there is any pages in the list put them back to
937 		 * the pool.
938 		 */
939 		pr_debug("Failed to allocate extra pages for large request\n");
940 		ttm_put_pages(pages, count, flags, cstate);
941 		return r;
942 	}
943 
944 	return 0;
945 }
946 
ttm_page_pool_init_locked(struct ttm_page_pool * pool,gfp_t flags,char * name,unsigned int order)947 static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, gfp_t flags,
948 		char *name, unsigned int order)
949 {
950 	spin_lock_init(&pool->lock);
951 	pool->fill_lock = false;
952 	INIT_LIST_HEAD(&pool->list);
953 	pool->npages = pool->nfrees = 0;
954 	pool->gfp_flags = flags;
955 	pool->name = name;
956 	pool->order = order;
957 }
958 
ttm_page_alloc_init(struct ttm_mem_global * glob,unsigned max_pages)959 int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
960 {
961 	int ret;
962 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
963 	unsigned order = HPAGE_PMD_ORDER;
964 #else
965 	unsigned order = 0;
966 #endif
967 
968 	WARN_ON(_manager);
969 
970 	pr_info("Initializing pool allocator\n");
971 
972 	_manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
973 	if (!_manager)
974 		return -ENOMEM;
975 
976 	ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc", 0);
977 
978 	ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc", 0);
979 
980 	ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
981 				  GFP_USER | GFP_DMA32, "wc dma", 0);
982 
983 	ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
984 				  GFP_USER | GFP_DMA32, "uc dma", 0);
985 
986 	ttm_page_pool_init_locked(&_manager->wc_pool_huge,
987 				  (GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
988 				   __GFP_KSWAPD_RECLAIM) &
989 				  ~(__GFP_MOVABLE | __GFP_COMP),
990 				  "wc huge", order);
991 
992 	ttm_page_pool_init_locked(&_manager->uc_pool_huge,
993 				  (GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
994 				   __GFP_KSWAPD_RECLAIM) &
995 				  ~(__GFP_MOVABLE | __GFP_COMP)
996 				  , "uc huge", order);
997 
998 	_manager->options.max_size = max_pages;
999 	_manager->options.small = SMALL_ALLOCATION;
1000 	_manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
1001 
1002 	ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
1003 				   &glob->kobj, "pool");
1004 	if (unlikely(ret != 0))
1005 		goto error;
1006 
1007 	ret = ttm_pool_mm_shrink_init(_manager);
1008 	if (unlikely(ret != 0))
1009 		goto error;
1010 	return 0;
1011 
1012 error:
1013 	kobject_put(&_manager->kobj);
1014 	_manager = NULL;
1015 	return ret;
1016 }
1017 
ttm_page_alloc_fini(void)1018 void ttm_page_alloc_fini(void)
1019 {
1020 	int i;
1021 
1022 	pr_info("Finalizing pool allocator\n");
1023 	ttm_pool_mm_shrink_fini(_manager);
1024 
1025 	/* OK to use static buffer since global mutex is no longer used. */
1026 	for (i = 0; i < NUM_POOLS; ++i)
1027 		ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);
1028 
1029 	kobject_put(&_manager->kobj);
1030 	_manager = NULL;
1031 }
1032 
1033 static void
ttm_pool_unpopulate_helper(struct ttm_tt * ttm,unsigned mem_count_update)1034 ttm_pool_unpopulate_helper(struct ttm_tt *ttm, unsigned mem_count_update)
1035 {
1036 	struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1037 	unsigned i;
1038 
1039 	if (mem_count_update == 0)
1040 		goto put_pages;
1041 
1042 	for (i = 0; i < mem_count_update; ++i) {
1043 		if (!ttm->pages[i])
1044 			continue;
1045 
1046 		ttm_mem_global_free_page(mem_glob, ttm->pages[i], PAGE_SIZE);
1047 	}
1048 
1049 put_pages:
1050 	ttm_put_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
1051 		      ttm->caching_state);
1052 	ttm->state = tt_unpopulated;
1053 }
1054 
ttm_pool_populate(struct ttm_tt * ttm,struct ttm_operation_ctx * ctx)1055 int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
1056 {
1057 	struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1058 	unsigned i;
1059 	int ret;
1060 
1061 	if (ttm->state != tt_unpopulated)
1062 		return 0;
1063 
1064 	if (ttm_check_under_lowerlimit(mem_glob, ttm->num_pages, ctx))
1065 		return -ENOMEM;
1066 
1067 	ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
1068 			    ttm->caching_state);
1069 	if (unlikely(ret != 0)) {
1070 		ttm_pool_unpopulate_helper(ttm, 0);
1071 		return ret;
1072 	}
1073 
1074 	for (i = 0; i < ttm->num_pages; ++i) {
1075 		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
1076 						PAGE_SIZE, ctx);
1077 		if (unlikely(ret != 0)) {
1078 			ttm_pool_unpopulate_helper(ttm, i);
1079 			return -ENOMEM;
1080 		}
1081 	}
1082 
1083 	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
1084 		ret = ttm_tt_swapin(ttm);
1085 		if (unlikely(ret != 0)) {
1086 			ttm_pool_unpopulate(ttm);
1087 			return ret;
1088 		}
1089 	}
1090 
1091 	ttm->state = tt_unbound;
1092 	return 0;
1093 }
1094 EXPORT_SYMBOL(ttm_pool_populate);
1095 
ttm_pool_unpopulate(struct ttm_tt * ttm)1096 void ttm_pool_unpopulate(struct ttm_tt *ttm)
1097 {
1098 	ttm_pool_unpopulate_helper(ttm, ttm->num_pages);
1099 }
1100 EXPORT_SYMBOL(ttm_pool_unpopulate);
1101 
ttm_populate_and_map_pages(struct device * dev,struct ttm_dma_tt * tt,struct ttm_operation_ctx * ctx)1102 int ttm_populate_and_map_pages(struct device *dev, struct ttm_dma_tt *tt,
1103 					struct ttm_operation_ctx *ctx)
1104 {
1105 	unsigned i, j;
1106 	int r;
1107 
1108 	r = ttm_pool_populate(&tt->ttm, ctx);
1109 	if (r)
1110 		return r;
1111 
1112 	for (i = 0; i < tt->ttm.num_pages; ++i) {
1113 		struct page *p = tt->ttm.pages[i];
1114 		size_t num_pages = 1;
1115 
1116 		for (j = i + 1; j < tt->ttm.num_pages; ++j) {
1117 			if (++p != tt->ttm.pages[j])
1118 				break;
1119 
1120 			++num_pages;
1121 		}
1122 
1123 		tt->dma_address[i] = dma_map_page(dev, tt->ttm.pages[i],
1124 						  0, num_pages * PAGE_SIZE,
1125 						  DMA_BIDIRECTIONAL);
1126 		if (dma_mapping_error(dev, tt->dma_address[i])) {
1127 			while (i--) {
1128 				dma_unmap_page(dev, tt->dma_address[i],
1129 					       PAGE_SIZE, DMA_BIDIRECTIONAL);
1130 				tt->dma_address[i] = 0;
1131 			}
1132 			ttm_pool_unpopulate(&tt->ttm);
1133 			return -EFAULT;
1134 		}
1135 
1136 		for (j = 1; j < num_pages; ++j) {
1137 			tt->dma_address[i + 1] = tt->dma_address[i] + PAGE_SIZE;
1138 			++i;
1139 		}
1140 	}
1141 	return 0;
1142 }
1143 EXPORT_SYMBOL(ttm_populate_and_map_pages);
1144 
ttm_unmap_and_unpopulate_pages(struct device * dev,struct ttm_dma_tt * tt)1145 void ttm_unmap_and_unpopulate_pages(struct device *dev, struct ttm_dma_tt *tt)
1146 {
1147 	unsigned i, j;
1148 
1149 	for (i = 0; i < tt->ttm.num_pages;) {
1150 		struct page *p = tt->ttm.pages[i];
1151 		size_t num_pages = 1;
1152 
1153 		if (!tt->dma_address[i] || !tt->ttm.pages[i]) {
1154 			++i;
1155 			continue;
1156 		}
1157 
1158 		for (j = i + 1; j < tt->ttm.num_pages; ++j) {
1159 			if (++p != tt->ttm.pages[j])
1160 				break;
1161 
1162 			++num_pages;
1163 		}
1164 
1165 		dma_unmap_page(dev, tt->dma_address[i], num_pages * PAGE_SIZE,
1166 			       DMA_BIDIRECTIONAL);
1167 
1168 		i += num_pages;
1169 	}
1170 	ttm_pool_unpopulate(&tt->ttm);
1171 }
1172 EXPORT_SYMBOL(ttm_unmap_and_unpopulate_pages);
1173 
ttm_page_alloc_debugfs(struct seq_file * m,void * data)1174 int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
1175 {
1176 	struct ttm_page_pool *p;
1177 	unsigned i;
1178 	char *h[] = {"pool", "refills", "pages freed", "size"};
1179 	if (!_manager) {
1180 		seq_printf(m, "No pool allocator running.\n");
1181 		return 0;
1182 	}
1183 	seq_printf(m, "%7s %12s %13s %8s\n",
1184 			h[0], h[1], h[2], h[3]);
1185 	for (i = 0; i < NUM_POOLS; ++i) {
1186 		p = &_manager->pools[i];
1187 
1188 		seq_printf(m, "%7s %12ld %13ld %8d\n",
1189 				p->name, p->nrefills,
1190 				p->nfrees, p->npages);
1191 	}
1192 	return 0;
1193 }
1194 EXPORT_SYMBOL(ttm_page_alloc_debugfs);
1195