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