1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "ttm/ttm_memory.h"
29 #include "ttm/ttm_module.h"
30 #include <linux/spinlock.h>
31 #include <linux/sched.h>
32 #include <linux/wait.h>
34 #include <linux/module.h>
36 #define TTM_MEMORY_ALLOC_RETRIES 4
40 struct ttm_mem_global
*glob
;
49 static struct attribute ttm_mem_sys
= {
50 .name
= "zone_memory",
53 static struct attribute ttm_mem_emer
= {
54 .name
= "emergency_memory",
55 .mode
= S_IRUGO
| S_IWUSR
57 static struct attribute ttm_mem_max
= {
58 .name
= "available_memory",
59 .mode
= S_IRUGO
| S_IWUSR
61 static struct attribute ttm_mem_swap
= {
63 .mode
= S_IRUGO
| S_IWUSR
65 static struct attribute ttm_mem_used
= {
66 .name
= "used_memory",
70 static void ttm_mem_zone_kobj_release(struct kobject
*kobj
)
72 struct ttm_mem_zone
*zone
=
73 container_of(kobj
, struct ttm_mem_zone
, kobj
);
75 printk(KERN_INFO TTM_PFX
76 "Zone %7s: Used memory at exit: %llu kiB.\n",
77 zone
->name
, (unsigned long long) zone
->used_mem
>> 10);
81 static ssize_t
ttm_mem_zone_show(struct kobject
*kobj
,
82 struct attribute
*attr
,
85 struct ttm_mem_zone
*zone
=
86 container_of(kobj
, struct ttm_mem_zone
, kobj
);
89 spin_lock(&zone
->glob
->lock
);
90 if (attr
== &ttm_mem_sys
)
92 else if (attr
== &ttm_mem_emer
)
94 else if (attr
== &ttm_mem_max
)
96 else if (attr
== &ttm_mem_swap
)
97 val
= zone
->swap_limit
;
98 else if (attr
== &ttm_mem_used
)
100 spin_unlock(&zone
->glob
->lock
);
102 return snprintf(buffer
, PAGE_SIZE
, "%llu\n",
103 (unsigned long long) val
>> 10);
106 static void ttm_check_swapping(struct ttm_mem_global
*glob
);
108 static ssize_t
ttm_mem_zone_store(struct kobject
*kobj
,
109 struct attribute
*attr
,
113 struct ttm_mem_zone
*zone
=
114 container_of(kobj
, struct ttm_mem_zone
, kobj
);
119 chars
= sscanf(buffer
, "%lu", &val
);
126 spin_lock(&zone
->glob
->lock
);
127 if (val64
> zone
->zone_mem
)
128 val64
= zone
->zone_mem
;
129 if (attr
== &ttm_mem_emer
) {
130 zone
->emer_mem
= val64
;
131 if (zone
->max_mem
> val64
)
132 zone
->max_mem
= val64
;
133 } else if (attr
== &ttm_mem_max
) {
134 zone
->max_mem
= val64
;
135 if (zone
->emer_mem
< val64
)
136 zone
->emer_mem
= val64
;
137 } else if (attr
== &ttm_mem_swap
)
138 zone
->swap_limit
= val64
;
139 spin_unlock(&zone
->glob
->lock
);
141 ttm_check_swapping(zone
->glob
);
146 static struct attribute
*ttm_mem_zone_attrs
[] = {
155 static struct sysfs_ops ttm_mem_zone_ops
= {
156 .show
= &ttm_mem_zone_show
,
157 .store
= &ttm_mem_zone_store
160 static struct kobj_type ttm_mem_zone_kobj_type
= {
161 .release
= &ttm_mem_zone_kobj_release
,
162 .sysfs_ops
= &ttm_mem_zone_ops
,
163 .default_attrs
= ttm_mem_zone_attrs
,
166 static void ttm_mem_global_kobj_release(struct kobject
*kobj
)
168 struct ttm_mem_global
*glob
=
169 container_of(kobj
, struct ttm_mem_global
, kobj
);
174 static struct kobj_type ttm_mem_glob_kobj_type
= {
175 .release
= &ttm_mem_global_kobj_release
,
178 static bool ttm_zones_above_swap_target(struct ttm_mem_global
*glob
,
179 bool from_wq
, uint64_t extra
)
182 struct ttm_mem_zone
*zone
;
185 for (i
= 0; i
< glob
->num_zones
; ++i
) {
186 zone
= glob
->zones
[i
];
189 target
= zone
->swap_limit
;
190 else if (capable(CAP_SYS_ADMIN
))
191 target
= zone
->emer_mem
;
193 target
= zone
->max_mem
;
195 target
= (extra
> target
) ? 0ULL : target
;
197 if (zone
->used_mem
> target
)
204 * At this point we only support a single shrink callback.
205 * Extend this if needed, perhaps using a linked list of callbacks.
206 * Note that this function is reentrant:
207 * many threads may try to swap out at any given time.
210 static void ttm_shrink(struct ttm_mem_global
*glob
, bool from_wq
,
214 struct ttm_mem_shrink
*shrink
;
216 spin_lock(&glob
->lock
);
217 if (glob
->shrink
== NULL
)
220 while (ttm_zones_above_swap_target(glob
, from_wq
, extra
)) {
221 shrink
= glob
->shrink
;
222 spin_unlock(&glob
->lock
);
223 ret
= shrink
->do_shrink(shrink
);
224 spin_lock(&glob
->lock
);
225 if (unlikely(ret
!= 0))
229 spin_unlock(&glob
->lock
);
234 static void ttm_shrink_work(struct work_struct
*work
)
236 struct ttm_mem_global
*glob
=
237 container_of(work
, struct ttm_mem_global
, work
);
239 ttm_shrink(glob
, true, 0ULL);
242 static int ttm_mem_init_kernel_zone(struct ttm_mem_global
*glob
,
243 const struct sysinfo
*si
)
245 struct ttm_mem_zone
*zone
= kzalloc(sizeof(*zone
), GFP_KERNEL
);
252 mem
= si
->totalram
- si
->totalhigh
;
255 zone
->name
= "kernel";
256 zone
->zone_mem
= mem
;
257 zone
->max_mem
= mem
>> 1;
258 zone
->emer_mem
= (mem
>> 1) + (mem
>> 2);
259 zone
->swap_limit
= zone
->max_mem
- (mem
>> 3);
262 glob
->zone_kernel
= zone
;
263 kobject_init(&zone
->kobj
, &ttm_mem_zone_kobj_type
);
264 ret
= kobject_add(&zone
->kobj
, &glob
->kobj
, zone
->name
);
265 if (unlikely(ret
!= 0)) {
266 kobject_put(&zone
->kobj
);
269 glob
->zones
[glob
->num_zones
++] = zone
;
273 #ifdef CONFIG_HIGHMEM
274 static int ttm_mem_init_highmem_zone(struct ttm_mem_global
*glob
,
275 const struct sysinfo
*si
)
277 struct ttm_mem_zone
*zone
;
281 if (si
->totalhigh
== 0)
284 zone
= kzalloc(sizeof(*zone
), GFP_KERNEL
);
291 zone
->name
= "highmem";
292 zone
->zone_mem
= mem
;
293 zone
->max_mem
= mem
>> 1;
294 zone
->emer_mem
= (mem
>> 1) + (mem
>> 2);
295 zone
->swap_limit
= zone
->max_mem
- (mem
>> 3);
298 glob
->zone_highmem
= zone
;
299 kobject_init(&zone
->kobj
, &ttm_mem_zone_kobj_type
);
300 ret
= kobject_add(&zone
->kobj
, &glob
->kobj
, zone
->name
);
301 if (unlikely(ret
!= 0)) {
302 kobject_put(&zone
->kobj
);
305 glob
->zones
[glob
->num_zones
++] = zone
;
309 static int ttm_mem_init_dma32_zone(struct ttm_mem_global
*glob
,
310 const struct sysinfo
*si
)
312 struct ttm_mem_zone
*zone
= kzalloc(sizeof(*zone
), GFP_KERNEL
);
323 * No special dma32 zone needed.
326 if (mem
<= ((uint64_t) 1ULL << 32)) {
332 * Limit max dma32 memory to 4GB for now
333 * until we can figure out how big this
337 mem
= ((uint64_t) 1ULL << 32);
338 zone
->name
= "dma32";
339 zone
->zone_mem
= mem
;
340 zone
->max_mem
= mem
>> 1;
341 zone
->emer_mem
= (mem
>> 1) + (mem
>> 2);
342 zone
->swap_limit
= zone
->max_mem
- (mem
>> 3);
345 glob
->zone_dma32
= zone
;
346 kobject_init(&zone
->kobj
, &ttm_mem_zone_kobj_type
);
347 ret
= kobject_add(&zone
->kobj
, &glob
->kobj
, zone
->name
);
348 if (unlikely(ret
!= 0)) {
349 kobject_put(&zone
->kobj
);
352 glob
->zones
[glob
->num_zones
++] = zone
;
357 int ttm_mem_global_init(struct ttm_mem_global
*glob
)
362 struct ttm_mem_zone
*zone
;
364 spin_lock_init(&glob
->lock
);
365 glob
->swap_queue
= create_singlethread_workqueue("ttm_swap");
366 INIT_WORK(&glob
->work
, ttm_shrink_work
);
367 init_waitqueue_head(&glob
->queue
);
368 kobject_init(&glob
->kobj
, &ttm_mem_glob_kobj_type
);
369 ret
= kobject_add(&glob
->kobj
,
371 "memory_accounting");
372 if (unlikely(ret
!= 0)) {
373 kobject_put(&glob
->kobj
);
379 ret
= ttm_mem_init_kernel_zone(glob
, &si
);
380 if (unlikely(ret
!= 0))
382 #ifdef CONFIG_HIGHMEM
383 ret
= ttm_mem_init_highmem_zone(glob
, &si
);
384 if (unlikely(ret
!= 0))
387 ret
= ttm_mem_init_dma32_zone(glob
, &si
);
388 if (unlikely(ret
!= 0))
391 for (i
= 0; i
< glob
->num_zones
; ++i
) {
392 zone
= glob
->zones
[i
];
393 printk(KERN_INFO TTM_PFX
394 "Zone %7s: Available graphics memory: %llu kiB.\n",
395 zone
->name
, (unsigned long long) zone
->max_mem
>> 10);
399 ttm_mem_global_release(glob
);
402 EXPORT_SYMBOL(ttm_mem_global_init
);
404 void ttm_mem_global_release(struct ttm_mem_global
*glob
)
407 struct ttm_mem_zone
*zone
;
409 flush_workqueue(glob
->swap_queue
);
410 destroy_workqueue(glob
->swap_queue
);
411 glob
->swap_queue
= NULL
;
412 for (i
= 0; i
< glob
->num_zones
; ++i
) {
413 zone
= glob
->zones
[i
];
414 kobject_del(&zone
->kobj
);
415 kobject_put(&zone
->kobj
);
417 kobject_del(&glob
->kobj
);
418 kobject_put(&glob
->kobj
);
420 EXPORT_SYMBOL(ttm_mem_global_release
);
422 static void ttm_check_swapping(struct ttm_mem_global
*glob
)
424 bool needs_swapping
= false;
426 struct ttm_mem_zone
*zone
;
428 spin_lock(&glob
->lock
);
429 for (i
= 0; i
< glob
->num_zones
; ++i
) {
430 zone
= glob
->zones
[i
];
431 if (zone
->used_mem
> zone
->swap_limit
) {
432 needs_swapping
= true;
437 spin_unlock(&glob
->lock
);
439 if (unlikely(needs_swapping
))
440 (void)queue_work(glob
->swap_queue
, &glob
->work
);
444 static void ttm_mem_global_free_zone(struct ttm_mem_global
*glob
,
445 struct ttm_mem_zone
*single_zone
,
449 struct ttm_mem_zone
*zone
;
451 spin_lock(&glob
->lock
);
452 for (i
= 0; i
< glob
->num_zones
; ++i
) {
453 zone
= glob
->zones
[i
];
454 if (single_zone
&& zone
!= single_zone
)
456 zone
->used_mem
-= amount
;
458 spin_unlock(&glob
->lock
);
461 void ttm_mem_global_free(struct ttm_mem_global
*glob
,
464 return ttm_mem_global_free_zone(glob
, NULL
, amount
);
466 EXPORT_SYMBOL(ttm_mem_global_free
);
468 static int ttm_mem_global_reserve(struct ttm_mem_global
*glob
,
469 struct ttm_mem_zone
*single_zone
,
470 uint64_t amount
, bool reserve
)
475 struct ttm_mem_zone
*zone
;
477 spin_lock(&glob
->lock
);
478 for (i
= 0; i
< glob
->num_zones
; ++i
) {
479 zone
= glob
->zones
[i
];
480 if (single_zone
&& zone
!= single_zone
)
483 limit
= (capable(CAP_SYS_ADMIN
)) ?
484 zone
->emer_mem
: zone
->max_mem
;
486 if (zone
->used_mem
> limit
)
491 for (i
= 0; i
< glob
->num_zones
; ++i
) {
492 zone
= glob
->zones
[i
];
493 if (single_zone
&& zone
!= single_zone
)
495 zone
->used_mem
+= amount
;
501 spin_unlock(&glob
->lock
);
502 ttm_check_swapping(glob
);
508 static int ttm_mem_global_alloc_zone(struct ttm_mem_global
*glob
,
509 struct ttm_mem_zone
*single_zone
,
511 bool no_wait
, bool interruptible
)
513 int count
= TTM_MEMORY_ALLOC_RETRIES
;
515 while (unlikely(ttm_mem_global_reserve(glob
,
521 if (unlikely(count
-- == 0))
523 ttm_shrink(glob
, false, memory
+ (memory
>> 2) + 16);
529 int ttm_mem_global_alloc(struct ttm_mem_global
*glob
, uint64_t memory
,
530 bool no_wait
, bool interruptible
)
533 * Normal allocations of kernel memory are registered in
537 return ttm_mem_global_alloc_zone(glob
, NULL
, memory
, no_wait
,
540 EXPORT_SYMBOL(ttm_mem_global_alloc
);
542 int ttm_mem_global_alloc_page(struct ttm_mem_global
*glob
,
544 bool no_wait
, bool interruptible
)
547 struct ttm_mem_zone
*zone
= NULL
;
550 * Page allocations may be registed in a single zone
551 * only if highmem or !dma32.
554 #ifdef CONFIG_HIGHMEM
555 if (PageHighMem(page
) && glob
->zone_highmem
!= NULL
)
556 zone
= glob
->zone_highmem
;
558 if (glob
->zone_dma32
&& page_to_pfn(page
) > 0x00100000UL
)
559 zone
= glob
->zone_kernel
;
561 return ttm_mem_global_alloc_zone(glob
, zone
, PAGE_SIZE
, no_wait
,
565 void ttm_mem_global_free_page(struct ttm_mem_global
*glob
, struct page
*page
)
567 struct ttm_mem_zone
*zone
= NULL
;
569 #ifdef CONFIG_HIGHMEM
570 if (PageHighMem(page
) && glob
->zone_highmem
!= NULL
)
571 zone
= glob
->zone_highmem
;
573 if (glob
->zone_dma32
&& page_to_pfn(page
) > 0x00100000UL
)
574 zone
= glob
->zone_kernel
;
576 ttm_mem_global_free_zone(glob
, zone
, PAGE_SIZE
);
580 size_t ttm_round_pot(size_t size
)
582 if ((size
& (size
- 1)) == 0)
584 else if (size
> PAGE_SIZE
)
585 return PAGE_ALIGN(size
);
589 while (tmp_size
< size
)
596 EXPORT_SYMBOL(ttm_round_pot
);