ttm_memory.c revision 1.3 1 /* $NetBSD: ttm_memory.c,v 1.3 2018/08/27 04:58:37 riastradh Exp $ */
2
3 /**************************************************************************
4 *
5 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: ttm_memory.c,v 1.3 2018/08/27 04:58:37 riastradh Exp $");
32
33 #define pr_fmt(fmt) "[TTM] " fmt
34
35 #include <drm/drmP.h>
36 #include <drm/ttm/ttm_memory.h>
37 #include <drm/ttm/ttm_module.h>
38 #include <drm/ttm/ttm_page_alloc.h>
39 #include <linux/spinlock.h>
40 #include <linux/sched.h>
41 #include <linux/wait.h>
42 #include <linux/mm.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/printk.h>
46 #include <linux/export.h>
47
48 #define TTM_MEMORY_ALLOC_RETRIES 4
49
50 struct ttm_mem_zone {
51 #ifndef __NetBSD__
52 struct kobject kobj;
53 #endif
54 struct ttm_mem_global *glob;
55 const char *name;
56 uint64_t zone_mem;
57 uint64_t emer_mem;
58 uint64_t max_mem;
59 uint64_t swap_limit;
60 uint64_t used_mem;
61 };
62
63 #ifndef __NetBSD__
64 static struct attribute ttm_mem_sys = {
65 .name = "zone_memory",
66 .mode = S_IRUGO
67 };
68 static struct attribute ttm_mem_emer = {
69 .name = "emergency_memory",
70 .mode = S_IRUGO | S_IWUSR
71 };
72 static struct attribute ttm_mem_max = {
73 .name = "available_memory",
74 .mode = S_IRUGO | S_IWUSR
75 };
76 static struct attribute ttm_mem_swap = {
77 .name = "swap_limit",
78 .mode = S_IRUGO | S_IWUSR
79 };
80 static struct attribute ttm_mem_used = {
81 .name = "used_memory",
82 .mode = S_IRUGO
83 };
84
85 static void ttm_mem_zone_kobj_release(struct kobject *kobj)
86 {
87 struct ttm_mem_zone *zone =
88 container_of(kobj, struct ttm_mem_zone, kobj);
89
90 pr_info("Zone %7s: Used memory at exit: %llu kiB\n",
91 zone->name, (unsigned long long)zone->used_mem >> 10);
92 kfree(zone);
93 }
94
95 static ssize_t ttm_mem_zone_show(struct kobject *kobj,
96 struct attribute *attr,
97 char *buffer)
98 {
99 struct ttm_mem_zone *zone =
100 container_of(kobj, struct ttm_mem_zone, kobj);
101 uint64_t val = 0;
102
103 spin_lock(&zone->glob->lock);
104 if (attr == &ttm_mem_sys)
105 val = zone->zone_mem;
106 else if (attr == &ttm_mem_emer)
107 val = zone->emer_mem;
108 else if (attr == &ttm_mem_max)
109 val = zone->max_mem;
110 else if (attr == &ttm_mem_swap)
111 val = zone->swap_limit;
112 else if (attr == &ttm_mem_used)
113 val = zone->used_mem;
114 spin_unlock(&zone->glob->lock);
115
116 return snprintf(buffer, PAGE_SIZE, "%llu\n",
117 (unsigned long long) val >> 10);
118 }
119
120 static void ttm_check_swapping(struct ttm_mem_global *glob);
121
122 static ssize_t ttm_mem_zone_store(struct kobject *kobj,
123 struct attribute *attr,
124 const char *buffer,
125 size_t size)
126 {
127 struct ttm_mem_zone *zone =
128 container_of(kobj, struct ttm_mem_zone, kobj);
129 int chars;
130 unsigned long val;
131 uint64_t val64;
132
133 chars = sscanf(buffer, "%lu", &val);
134 if (chars == 0)
135 return size;
136
137 val64 = val;
138 val64 <<= 10;
139
140 spin_lock(&zone->glob->lock);
141 if (val64 > zone->zone_mem)
142 val64 = zone->zone_mem;
143 if (attr == &ttm_mem_emer) {
144 zone->emer_mem = val64;
145 if (zone->max_mem > val64)
146 zone->max_mem = val64;
147 } else if (attr == &ttm_mem_max) {
148 zone->max_mem = val64;
149 if (zone->emer_mem < val64)
150 zone->emer_mem = val64;
151 } else if (attr == &ttm_mem_swap)
152 zone->swap_limit = val64;
153 spin_unlock(&zone->glob->lock);
154
155 ttm_check_swapping(zone->glob);
156
157 return size;
158 }
159
160 static struct attribute *ttm_mem_zone_attrs[] = {
161 &ttm_mem_sys,
162 &ttm_mem_emer,
163 &ttm_mem_max,
164 &ttm_mem_swap,
165 &ttm_mem_used,
166 NULL
167 };
168
169 static const struct sysfs_ops ttm_mem_zone_ops = {
170 .show = &ttm_mem_zone_show,
171 .store = &ttm_mem_zone_store
172 };
173
174 static struct kobj_type ttm_mem_zone_kobj_type = {
175 .release = &ttm_mem_zone_kobj_release,
176 .sysfs_ops = &ttm_mem_zone_ops,
177 .default_attrs = ttm_mem_zone_attrs,
178 };
179
180 static void ttm_mem_global_kobj_release(struct kobject *kobj)
181 {
182 struct ttm_mem_global *glob =
183 container_of(kobj, struct ttm_mem_global, kobj);
184
185 kfree(glob);
186 }
187
188 static struct kobj_type ttm_mem_glob_kobj_type = {
189 .release = &ttm_mem_global_kobj_release,
190 };
191 #endif
192
193 static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
194 bool from_wq, uint64_t extra)
195 {
196 unsigned int i;
197 struct ttm_mem_zone *zone;
198 uint64_t target;
199
200 for (i = 0; i < glob->num_zones; ++i) {
201 zone = glob->zones[i];
202
203 if (from_wq)
204 target = zone->swap_limit;
205 #ifdef __NetBSD__
206 else if (DRM_SUSER())
207 #else
208 else if (capable(CAP_SYS_ADMIN))
209 #endif
210 target = zone->emer_mem;
211 else
212 target = zone->max_mem;
213
214 target = (extra > target) ? 0ULL : target;
215
216 if (zone->used_mem > target)
217 return true;
218 }
219 return false;
220 }
221
222 /**
223 * At this point we only support a single shrink callback.
224 * Extend this if needed, perhaps using a linked list of callbacks.
225 * Note that this function is reentrant:
226 * many threads may try to swap out at any given time.
227 */
228
229 static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
230 uint64_t extra)
231 {
232 int ret;
233 struct ttm_mem_shrink *shrink;
234
235 spin_lock(&glob->lock);
236 if (glob->shrink == NULL)
237 goto out;
238
239 while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
240 shrink = glob->shrink;
241 spin_unlock(&glob->lock);
242 ret = shrink->do_shrink(shrink);
243 spin_lock(&glob->lock);
244 if (unlikely(ret != 0))
245 goto out;
246 }
247 out:
248 spin_unlock(&glob->lock);
249 }
250
251
252
253 static void ttm_shrink_work(struct work_struct *work)
254 {
255 struct ttm_mem_global *glob =
256 container_of(work, struct ttm_mem_global, work);
257
258 ttm_shrink(glob, true, 0ULL);
259 }
260
261 static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
262 const struct sysinfo *si)
263 {
264 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
265 uint64_t mem;
266 #ifndef __NetBSD__
267 int ret;
268 #endif
269
270 if (unlikely(!zone))
271 return -ENOMEM;
272
273 mem = si->totalram - si->totalhigh;
274 mem *= si->mem_unit;
275
276 zone->name = "kernel";
277 zone->zone_mem = mem;
278 zone->max_mem = mem >> 1;
279 zone->emer_mem = (mem >> 1) + (mem >> 2);
280 zone->swap_limit = zone->max_mem - (mem >> 3);
281 zone->used_mem = 0;
282 zone->glob = glob;
283 glob->zone_kernel = zone;
284 #ifndef __NetBSD__
285 ret = kobject_init_and_add(
286 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
287 if (unlikely(ret != 0)) {
288 kobject_put(&zone->kobj);
289 return ret;
290 }
291 #endif
292 glob->zones[glob->num_zones++] = zone;
293 return 0;
294 }
295
296 #ifdef CONFIG_HIGHMEM
297 static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
298 const struct sysinfo *si)
299 {
300 struct ttm_mem_zone *zone;
301 uint64_t mem;
302 #ifndef __NetBSD__
303 int ret;
304 #endif
305
306 if (si->totalhigh == 0)
307 return 0;
308
309 zone = kzalloc(sizeof(*zone), GFP_KERNEL);
310 if (unlikely(!zone))
311 return -ENOMEM;
312
313 mem = si->totalram;
314 mem *= si->mem_unit;
315
316 zone->name = "highmem";
317 zone->zone_mem = mem;
318 zone->max_mem = mem >> 1;
319 zone->emer_mem = (mem >> 1) + (mem >> 2);
320 zone->swap_limit = zone->max_mem - (mem >> 3);
321 zone->used_mem = 0;
322 zone->glob = glob;
323 glob->zone_highmem = zone;
324 #ifndef __NetBSD__
325 ret = kobject_init_and_add(
326 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, "%s",
327 zone->name);
328 if (unlikely(ret != 0)) {
329 kobject_put(&zone->kobj);
330 return ret;
331 }
332 #endif
333 glob->zones[glob->num_zones++] = zone;
334 return 0;
335 }
336 #else
337 static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
338 const struct sysinfo *si)
339 {
340 struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
341 uint64_t mem;
342 #ifndef __NetBSD__
343 int ret;
344 #endif
345
346 if (unlikely(!zone))
347 return -ENOMEM;
348
349 mem = si->totalram;
350 mem *= si->mem_unit;
351
352 /**
353 * No special dma32 zone needed.
354 */
355
356 if (mem <= ((uint64_t) 1ULL << 32)) {
357 kfree(zone);
358 return 0;
359 }
360
361 /*
362 * Limit max dma32 memory to 4GB for now
363 * until we can figure out how big this
364 * zone really is.
365 */
366
367 mem = ((uint64_t) 1ULL << 32);
368 zone->name = "dma32";
369 zone->zone_mem = mem;
370 zone->max_mem = mem >> 1;
371 zone->emer_mem = (mem >> 1) + (mem >> 2);
372 zone->swap_limit = zone->max_mem - (mem >> 3);
373 zone->used_mem = 0;
374 zone->glob = glob;
375 glob->zone_dma32 = zone;
376 #ifndef __NetBSD__
377 ret = kobject_init_and_add(
378 &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
379 if (unlikely(ret != 0)) {
380 kobject_put(&zone->kobj);
381 return ret;
382 }
383 #endif
384 glob->zones[glob->num_zones++] = zone;
385 return 0;
386 }
387 #endif
388
389 int ttm_mem_global_init(struct ttm_mem_global *glob)
390 {
391 struct sysinfo si;
392 int ret;
393 int i;
394 struct ttm_mem_zone *zone;
395
396 spin_lock_init(&glob->lock);
397 glob->swap_queue = create_singlethread_workqueue("ttm_swap");
398 INIT_WORK(&glob->work, ttm_shrink_work);
399 #ifndef __NetBSD__
400 ret = kobject_init_and_add(
401 &glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
402 if (unlikely(ret != 0)) {
403 kobject_put(&glob->kobj);
404 return ret;
405 }
406 #endif
407
408 si_meminfo(&si);
409
410 ret = ttm_mem_init_kernel_zone(glob, &si);
411 if (unlikely(ret != 0))
412 goto out_no_zone;
413 #ifdef CONFIG_HIGHMEM
414 ret = ttm_mem_init_highmem_zone(glob, &si);
415 if (unlikely(ret != 0))
416 goto out_no_zone;
417 #else
418 ret = ttm_mem_init_dma32_zone(glob, &si);
419 if (unlikely(ret != 0))
420 goto out_no_zone;
421 #endif
422 for (i = 0; i < glob->num_zones; ++i) {
423 zone = glob->zones[i];
424 pr_info("Zone %7s: Available graphics memory: %llu kiB\n",
425 zone->name, (unsigned long long)zone->max_mem >> 10);
426 }
427 ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
428 ttm_dma_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
429 return 0;
430 out_no_zone:
431 ttm_mem_global_release(glob);
432 return ret;
433 }
434 EXPORT_SYMBOL(ttm_mem_global_init);
435
436 void ttm_mem_global_release(struct ttm_mem_global *glob)
437 {
438 unsigned int i;
439 struct ttm_mem_zone *zone;
440
441 /* let the page allocator first stop the shrink work. */
442 ttm_page_alloc_fini();
443 ttm_dma_page_alloc_fini();
444
445 flush_workqueue(glob->swap_queue);
446 destroy_workqueue(glob->swap_queue);
447 glob->swap_queue = NULL;
448 for (i = 0; i < glob->num_zones; ++i) {
449 zone = glob->zones[i];
450 #ifdef __NetBSD__
451 kfree(zone);
452 #else
453 kobject_del(&zone->kobj);
454 kobject_put(&zone->kobj);
455 #endif
456 }
457 #ifdef __NetBSD__
458 kfree(glob);
459 #else
460 kobject_del(&glob->kobj);
461 kobject_put(&glob->kobj);
462 #endif
463 }
464 EXPORT_SYMBOL(ttm_mem_global_release);
465
466 static void ttm_check_swapping(struct ttm_mem_global *glob)
467 {
468 bool needs_swapping = false;
469 unsigned int i;
470 struct ttm_mem_zone *zone;
471
472 spin_lock(&glob->lock);
473 for (i = 0; i < glob->num_zones; ++i) {
474 zone = glob->zones[i];
475 if (zone->used_mem > zone->swap_limit) {
476 needs_swapping = true;
477 break;
478 }
479 }
480
481 spin_unlock(&glob->lock);
482
483 if (unlikely(needs_swapping))
484 (void)queue_work(glob->swap_queue, &glob->work);
485
486 }
487
488 static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
489 struct ttm_mem_zone *single_zone,
490 uint64_t amount)
491 {
492 unsigned int i;
493 struct ttm_mem_zone *zone;
494
495 spin_lock(&glob->lock);
496 for (i = 0; i < glob->num_zones; ++i) {
497 zone = glob->zones[i];
498 if (single_zone && zone != single_zone)
499 continue;
500 zone->used_mem -= amount;
501 }
502 spin_unlock(&glob->lock);
503 }
504
505 void ttm_mem_global_free(struct ttm_mem_global *glob,
506 uint64_t amount)
507 {
508 return ttm_mem_global_free_zone(glob, NULL, amount);
509 }
510 EXPORT_SYMBOL(ttm_mem_global_free);
511
512 static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
513 struct ttm_mem_zone *single_zone,
514 uint64_t amount, bool reserve)
515 {
516 uint64_t limit;
517 int ret = -ENOMEM;
518 unsigned int i;
519 struct ttm_mem_zone *zone;
520
521 spin_lock(&glob->lock);
522 for (i = 0; i < glob->num_zones; ++i) {
523 zone = glob->zones[i];
524 if (single_zone && zone != single_zone)
525 continue;
526
527 #ifdef __NetBSD__
528 limit = DRM_SUSER() ?
529 zone->emer_mem : zone->max_mem;
530 #else
531 limit = (capable(CAP_SYS_ADMIN)) ?
532 zone->emer_mem : zone->max_mem;
533 #endif
534
535 if (zone->used_mem > limit)
536 goto out_unlock;
537 }
538
539 if (reserve) {
540 for (i = 0; i < glob->num_zones; ++i) {
541 zone = glob->zones[i];
542 if (single_zone && zone != single_zone)
543 continue;
544 zone->used_mem += amount;
545 }
546 }
547
548 ret = 0;
549 out_unlock:
550 spin_unlock(&glob->lock);
551 ttm_check_swapping(glob);
552
553 return ret;
554 }
555
556
557 static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
558 struct ttm_mem_zone *single_zone,
559 uint64_t memory,
560 bool no_wait, bool interruptible)
561 {
562 int count = TTM_MEMORY_ALLOC_RETRIES;
563
564 while (unlikely(ttm_mem_global_reserve(glob,
565 single_zone,
566 memory, true)
567 != 0)) {
568 if (no_wait)
569 return -ENOMEM;
570 if (unlikely(count-- == 0))
571 return -ENOMEM;
572 ttm_shrink(glob, false, memory + (memory >> 2) + 16);
573 }
574
575 return 0;
576 }
577
578 int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
579 bool no_wait, bool interruptible)
580 {
581 /**
582 * Normal allocations of kernel memory are registered in
583 * all zones.
584 */
585
586 return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait,
587 interruptible);
588 }
589 EXPORT_SYMBOL(ttm_mem_global_alloc);
590
591 int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
592 struct page *page,
593 bool no_wait, bool interruptible)
594 {
595
596 struct ttm_mem_zone *zone = NULL;
597
598 /**
599 * Page allocations may be registed in a single zone
600 * only if highmem or !dma32.
601 */
602
603 #ifdef CONFIG_HIGHMEM
604 if (PageHighMem(page) && glob->zone_highmem != NULL)
605 zone = glob->zone_highmem;
606 #else
607 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
608 zone = glob->zone_kernel;
609 #endif
610 return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait,
611 interruptible);
612 }
613
614 void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page)
615 {
616 struct ttm_mem_zone *zone = NULL;
617
618 #ifdef CONFIG_HIGHMEM
619 if (PageHighMem(page) && glob->zone_highmem != NULL)
620 zone = glob->zone_highmem;
621 #else
622 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
623 zone = glob->zone_kernel;
624 #endif
625 ttm_mem_global_free_zone(glob, zone, PAGE_SIZE);
626 }
627
628
629 size_t ttm_round_pot(size_t size)
630 {
631 if ((size & (size - 1)) == 0)
632 return size;
633 else if (size > PAGE_SIZE)
634 return PAGE_ALIGN(size);
635 else {
636 size_t tmp_size = 4;
637
638 while (tmp_size < size)
639 tmp_size <<= 1;
640
641 return tmp_size;
642 }
643 return 0;
644 }
645 EXPORT_SYMBOL(ttm_round_pot);
646