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 <linux/spinlock.h>
30 #include <linux/sched.h>
31 #include <linux/wait.h>
33 #include <linux/module.h>
35 #define TTM_PFX "[TTM] "
36 #define TTM_MEMORY_ALLOC_RETRIES 4
39 * At this point we only support a single shrink callback.
40 * Extend this if needed, perhaps using a linked list of callbacks.
41 * Note that this function is reentrant:
42 * many threads may try to swap out at any given time.
45 static void ttm_shrink(struct ttm_mem_global *glob, bool from_workqueue,
49 struct ttm_mem_shrink *shrink;
51 uint64_t total_target;
53 spin_lock(&glob->lock);
54 if (glob->shrink == NULL)
58 target = glob->swap_limit;
59 total_target = glob->total_memory_swap_limit;
60 } else if (capable(CAP_SYS_ADMIN)) {
61 total_target = glob->emer_total_memory;
62 target = glob->emer_memory;
64 total_target = glob->max_total_memory;
65 target = glob->max_memory;
68 total_target = (extra >= total_target) ? 0 : total_target - extra;
69 target = (extra >= target) ? 0 : target - extra;
71 while (glob->used_memory > target ||
72 glob->used_total_memory > total_target) {
73 shrink = glob->shrink;
74 spin_unlock(&glob->lock);
75 ret = shrink->do_shrink(shrink);
76 spin_lock(&glob->lock);
77 if (unlikely(ret != 0))
81 spin_unlock(&glob->lock);
84 static void ttm_shrink_work(struct work_struct *work)
86 struct ttm_mem_global *glob =
87 container_of(work, struct ttm_mem_global, work);
89 ttm_shrink(glob, true, 0ULL);
92 int ttm_mem_global_init(struct ttm_mem_global *glob)
97 spin_lock_init(&glob->lock);
98 glob->swap_queue = create_singlethread_workqueue("ttm_swap");
99 INIT_WORK(&glob->work, ttm_shrink_work);
100 init_waitqueue_head(&glob->queue);
104 mem = si.totalram - si.totalhigh;
107 glob->max_memory = mem >> 1;
108 glob->emer_memory = (mem >> 1) + (mem >> 2);
109 glob->swap_limit = glob->max_memory - (mem >> 3);
110 glob->used_memory = 0;
111 glob->used_total_memory = 0;
117 glob->max_total_memory = mem >> 1;
118 glob->emer_total_memory = (mem >> 1) + (mem >> 2);
120 glob->total_memory_swap_limit = glob->max_total_memory - (mem >> 3);
122 printk(KERN_INFO TTM_PFX "TTM available graphics memory: %llu MiB\n",
123 glob->max_total_memory >> 20);
124 printk(KERN_INFO TTM_PFX "TTM available object memory: %llu MiB\n",
125 glob->max_memory >> 20);
129 EXPORT_SYMBOL(ttm_mem_global_init);
131 void ttm_mem_global_release(struct ttm_mem_global *glob)
133 printk(KERN_INFO TTM_PFX "Used total memory is %llu bytes.\n",
134 (unsigned long long)glob->used_total_memory);
135 flush_workqueue(glob->swap_queue);
136 destroy_workqueue(glob->swap_queue);
137 glob->swap_queue = NULL;
139 EXPORT_SYMBOL(ttm_mem_global_release);
141 static inline void ttm_check_swapping(struct ttm_mem_global *glob)
145 spin_lock(&glob->lock);
146 needs_swapping = (glob->used_memory > glob->swap_limit ||
147 glob->used_total_memory >
148 glob->total_memory_swap_limit);
149 spin_unlock(&glob->lock);
151 if (unlikely(needs_swapping))
152 (void)queue_work(glob->swap_queue, &glob->work);
156 void ttm_mem_global_free(struct ttm_mem_global *glob,
157 uint64_t amount, bool himem)
159 spin_lock(&glob->lock);
160 glob->used_total_memory -= amount;
162 glob->used_memory -= amount;
163 wake_up_all(&glob->queue);
164 spin_unlock(&glob->lock);
167 static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
168 uint64_t amount, bool himem, bool reserve)
171 uint64_t lomem_limit;
174 spin_lock(&glob->lock);
176 if (capable(CAP_SYS_ADMIN)) {
177 limit = glob->emer_total_memory;
178 lomem_limit = glob->emer_memory;
180 limit = glob->max_total_memory;
181 lomem_limit = glob->max_memory;
184 if (unlikely(glob->used_total_memory + amount > limit))
186 if (unlikely(!himem && glob->used_memory + amount > lomem_limit))
190 glob->used_total_memory += amount;
192 glob->used_memory += amount;
196 spin_unlock(&glob->lock);
197 ttm_check_swapping(glob);
202 int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
203 bool no_wait, bool interruptible, bool himem)
205 int count = TTM_MEMORY_ALLOC_RETRIES;
207 while (unlikely(ttm_mem_global_reserve(glob, memory, himem, true)
211 if (unlikely(count-- == 0))
213 ttm_shrink(glob, false, memory + (memory >> 2) + 16);
219 size_t ttm_round_pot(size_t size)
221 if ((size & (size - 1)) == 0)
223 else if (size > PAGE_SIZE)
224 return PAGE_ALIGN(size);
228 while (tmp_size < size)