drm/ttm: Memory accounting rework.
Use inclusive zones to simplify accounting and its sysfs representation. Use DMA32 accounting where applicable. Add a sysfs interface to make the heuristically determined limits readable and configurable. Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com> Signed-off-by: Dave Airlie <airlied@linux.ie>
This commit is contained in:
committed by
Dave Airlie
parent
e9840be8c2
commit
5fd9cbad3a
@@ -70,7 +70,7 @@ static void ttm_bo_release_list(struct kref *list_kref)
|
|||||||
if (bo->destroy)
|
if (bo->destroy)
|
||||||
bo->destroy(bo);
|
bo->destroy(bo);
|
||||||
else {
|
else {
|
||||||
ttm_mem_global_free(bdev->mem_glob, bo->acc_size, false);
|
ttm_mem_global_free(bdev->mem_glob, bo->acc_size);
|
||||||
kfree(bo);
|
kfree(bo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1065,14 +1065,14 @@ int ttm_buffer_object_create(struct ttm_bo_device *bdev,
|
|||||||
|
|
||||||
size_t acc_size =
|
size_t acc_size =
|
||||||
ttm_bo_size(bdev, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
|
ttm_bo_size(bdev, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
|
||||||
ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false, false);
|
ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
|
||||||
if (unlikely(ret != 0))
|
if (unlikely(ret != 0))
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
bo = kzalloc(sizeof(*bo), GFP_KERNEL);
|
bo = kzalloc(sizeof(*bo), GFP_KERNEL);
|
||||||
|
|
||||||
if (unlikely(bo == NULL)) {
|
if (unlikely(bo == NULL)) {
|
||||||
ttm_mem_global_free(mem_glob, acc_size, false);
|
ttm_mem_global_free(mem_glob, acc_size);
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -71,7 +71,7 @@ int ttm_global_item_ref(struct ttm_global_reference *ref)
|
|||||||
|
|
||||||
mutex_lock(&item->mutex);
|
mutex_lock(&item->mutex);
|
||||||
if (item->refcount == 0) {
|
if (item->refcount == 0) {
|
||||||
item->object = kmalloc(ref->size, GFP_KERNEL);
|
item->object = kzalloc(ref->size, GFP_KERNEL);
|
||||||
if (unlikely(item->object == NULL)) {
|
if (unlikely(item->object == NULL)) {
|
||||||
ret = -ENOMEM;
|
ret = -ENOMEM;
|
||||||
goto out_err;
|
goto out_err;
|
||||||
@@ -89,7 +89,6 @@ int ttm_global_item_ref(struct ttm_global_reference *ref)
|
|||||||
mutex_unlock(&item->mutex);
|
mutex_unlock(&item->mutex);
|
||||||
return 0;
|
return 0;
|
||||||
out_err:
|
out_err:
|
||||||
kfree(item->object);
|
|
||||||
mutex_unlock(&item->mutex);
|
mutex_unlock(&item->mutex);
|
||||||
item->object = NULL;
|
item->object = NULL;
|
||||||
return ret;
|
return ret;
|
||||||
@@ -105,7 +104,6 @@ void ttm_global_item_unref(struct ttm_global_reference *ref)
|
|||||||
BUG_ON(ref->object != item->object);
|
BUG_ON(ref->object != item->object);
|
||||||
if (--item->refcount == 0) {
|
if (--item->refcount == 0) {
|
||||||
ref->release(ref);
|
ref->release(ref);
|
||||||
kfree(item->object);
|
|
||||||
item->object = NULL;
|
item->object = NULL;
|
||||||
}
|
}
|
||||||
mutex_unlock(&item->mutex);
|
mutex_unlock(&item->mutex);
|
||||||
|
@@ -26,15 +26,180 @@
|
|||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
#include "ttm/ttm_memory.h"
|
#include "ttm/ttm_memory.h"
|
||||||
|
#include "ttm/ttm_module.h"
|
||||||
#include <linux/spinlock.h>
|
#include <linux/spinlock.h>
|
||||||
#include <linux/sched.h>
|
#include <linux/sched.h>
|
||||||
#include <linux/wait.h>
|
#include <linux/wait.h>
|
||||||
#include <linux/mm.h>
|
#include <linux/mm.h>
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
|
|
||||||
#define TTM_PFX "[TTM] "
|
|
||||||
#define TTM_MEMORY_ALLOC_RETRIES 4
|
#define TTM_MEMORY_ALLOC_RETRIES 4
|
||||||
|
|
||||||
|
struct ttm_mem_zone {
|
||||||
|
struct kobject kobj;
|
||||||
|
struct ttm_mem_global *glob;
|
||||||
|
const char *name;
|
||||||
|
uint64_t zone_mem;
|
||||||
|
uint64_t emer_mem;
|
||||||
|
uint64_t max_mem;
|
||||||
|
uint64_t swap_limit;
|
||||||
|
uint64_t used_mem;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct attribute ttm_mem_sys = {
|
||||||
|
.name = "zone_memory",
|
||||||
|
.mode = S_IRUGO
|
||||||
|
};
|
||||||
|
static struct attribute ttm_mem_emer = {
|
||||||
|
.name = "emergency_memory",
|
||||||
|
.mode = S_IRUGO | S_IWUSR
|
||||||
|
};
|
||||||
|
static struct attribute ttm_mem_max = {
|
||||||
|
.name = "available_memory",
|
||||||
|
.mode = S_IRUGO | S_IWUSR
|
||||||
|
};
|
||||||
|
static struct attribute ttm_mem_swap = {
|
||||||
|
.name = "swap_limit",
|
||||||
|
.mode = S_IRUGO | S_IWUSR
|
||||||
|
};
|
||||||
|
static struct attribute ttm_mem_used = {
|
||||||
|
.name = "used_memory",
|
||||||
|
.mode = S_IRUGO
|
||||||
|
};
|
||||||
|
|
||||||
|
static void ttm_mem_zone_kobj_release(struct kobject *kobj)
|
||||||
|
{
|
||||||
|
struct ttm_mem_zone *zone =
|
||||||
|
container_of(kobj, struct ttm_mem_zone, kobj);
|
||||||
|
|
||||||
|
printk(KERN_INFO TTM_PFX
|
||||||
|
"Zone %7s: Used memory at exit: %llu kiB.\n",
|
||||||
|
zone->name, (unsigned long long) zone->used_mem >> 10);
|
||||||
|
kfree(zone);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t ttm_mem_zone_show(struct kobject *kobj,
|
||||||
|
struct attribute *attr,
|
||||||
|
char *buffer)
|
||||||
|
{
|
||||||
|
struct ttm_mem_zone *zone =
|
||||||
|
container_of(kobj, struct ttm_mem_zone, kobj);
|
||||||
|
uint64_t val = 0;
|
||||||
|
|
||||||
|
spin_lock(&zone->glob->lock);
|
||||||
|
if (attr == &ttm_mem_sys)
|
||||||
|
val = zone->zone_mem;
|
||||||
|
else if (attr == &ttm_mem_emer)
|
||||||
|
val = zone->emer_mem;
|
||||||
|
else if (attr == &ttm_mem_max)
|
||||||
|
val = zone->max_mem;
|
||||||
|
else if (attr == &ttm_mem_swap)
|
||||||
|
val = zone->swap_limit;
|
||||||
|
else if (attr == &ttm_mem_used)
|
||||||
|
val = zone->used_mem;
|
||||||
|
spin_unlock(&zone->glob->lock);
|
||||||
|
|
||||||
|
return snprintf(buffer, PAGE_SIZE, "%llu\n",
|
||||||
|
(unsigned long long) val >> 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ttm_check_swapping(struct ttm_mem_global *glob);
|
||||||
|
|
||||||
|
static ssize_t ttm_mem_zone_store(struct kobject *kobj,
|
||||||
|
struct attribute *attr,
|
||||||
|
const char *buffer,
|
||||||
|
size_t size)
|
||||||
|
{
|
||||||
|
struct ttm_mem_zone *zone =
|
||||||
|
container_of(kobj, struct ttm_mem_zone, kobj);
|
||||||
|
int chars;
|
||||||
|
unsigned long val;
|
||||||
|
uint64_t val64;
|
||||||
|
|
||||||
|
chars = sscanf(buffer, "%lu", &val);
|
||||||
|
if (chars == 0)
|
||||||
|
return size;
|
||||||
|
|
||||||
|
val64 = val;
|
||||||
|
val64 <<= 10;
|
||||||
|
|
||||||
|
spin_lock(&zone->glob->lock);
|
||||||
|
if (val64 > zone->zone_mem)
|
||||||
|
val64 = zone->zone_mem;
|
||||||
|
if (attr == &ttm_mem_emer) {
|
||||||
|
zone->emer_mem = val64;
|
||||||
|
if (zone->max_mem > val64)
|
||||||
|
zone->max_mem = val64;
|
||||||
|
} else if (attr == &ttm_mem_max) {
|
||||||
|
zone->max_mem = val64;
|
||||||
|
if (zone->emer_mem < val64)
|
||||||
|
zone->emer_mem = val64;
|
||||||
|
} else if (attr == &ttm_mem_swap)
|
||||||
|
zone->swap_limit = val64;
|
||||||
|
spin_unlock(&zone->glob->lock);
|
||||||
|
|
||||||
|
ttm_check_swapping(zone->glob);
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct attribute *ttm_mem_zone_attrs[] = {
|
||||||
|
&ttm_mem_sys,
|
||||||
|
&ttm_mem_emer,
|
||||||
|
&ttm_mem_max,
|
||||||
|
&ttm_mem_swap,
|
||||||
|
&ttm_mem_used,
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct sysfs_ops ttm_mem_zone_ops = {
|
||||||
|
.show = &ttm_mem_zone_show,
|
||||||
|
.store = &ttm_mem_zone_store
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct kobj_type ttm_mem_zone_kobj_type = {
|
||||||
|
.release = &ttm_mem_zone_kobj_release,
|
||||||
|
.sysfs_ops = &ttm_mem_zone_ops,
|
||||||
|
.default_attrs = ttm_mem_zone_attrs,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void ttm_mem_global_kobj_release(struct kobject *kobj)
|
||||||
|
{
|
||||||
|
struct ttm_mem_global *glob =
|
||||||
|
container_of(kobj, struct ttm_mem_global, kobj);
|
||||||
|
|
||||||
|
kfree(glob);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct kobj_type ttm_mem_glob_kobj_type = {
|
||||||
|
.release = &ttm_mem_global_kobj_release,
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
|
||||||
|
bool from_wq, uint64_t extra)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
struct ttm_mem_zone *zone;
|
||||||
|
uint64_t target;
|
||||||
|
|
||||||
|
for (i = 0; i < glob->num_zones; ++i) {
|
||||||
|
zone = glob->zones[i];
|
||||||
|
|
||||||
|
if (from_wq)
|
||||||
|
target = zone->swap_limit;
|
||||||
|
else if (capable(CAP_SYS_ADMIN))
|
||||||
|
target = zone->emer_mem;
|
||||||
|
else
|
||||||
|
target = zone->max_mem;
|
||||||
|
|
||||||
|
target = (extra > target) ? 0ULL : target;
|
||||||
|
|
||||||
|
if (zone->used_mem > target)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* At this point we only support a single shrink callback.
|
* At this point we only support a single shrink callback.
|
||||||
* Extend this if needed, perhaps using a linked list of callbacks.
|
* Extend this if needed, perhaps using a linked list of callbacks.
|
||||||
@@ -42,34 +207,17 @@
|
|||||||
* many threads may try to swap out at any given time.
|
* many threads may try to swap out at any given time.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void ttm_shrink(struct ttm_mem_global *glob, bool from_workqueue,
|
static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
|
||||||
uint64_t extra)
|
uint64_t extra)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
struct ttm_mem_shrink *shrink;
|
struct ttm_mem_shrink *shrink;
|
||||||
uint64_t target;
|
|
||||||
uint64_t total_target;
|
|
||||||
|
|
||||||
spin_lock(&glob->lock);
|
spin_lock(&glob->lock);
|
||||||
if (glob->shrink == NULL)
|
if (glob->shrink == NULL)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
if (from_workqueue) {
|
while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
|
||||||
target = glob->swap_limit;
|
|
||||||
total_target = glob->total_memory_swap_limit;
|
|
||||||
} else if (capable(CAP_SYS_ADMIN)) {
|
|
||||||
total_target = glob->emer_total_memory;
|
|
||||||
target = glob->emer_memory;
|
|
||||||
} else {
|
|
||||||
total_target = glob->max_total_memory;
|
|
||||||
target = glob->max_memory;
|
|
||||||
}
|
|
||||||
|
|
||||||
total_target = (extra >= total_target) ? 0 : total_target - extra;
|
|
||||||
target = (extra >= target) ? 0 : target - extra;
|
|
||||||
|
|
||||||
while (glob->used_memory > target ||
|
|
||||||
glob->used_total_memory > total_target) {
|
|
||||||
shrink = glob->shrink;
|
shrink = glob->shrink;
|
||||||
spin_unlock(&glob->lock);
|
spin_unlock(&glob->lock);
|
||||||
ret = shrink->do_shrink(shrink);
|
ret = shrink->do_shrink(shrink);
|
||||||
@@ -81,6 +229,8 @@ out:
|
|||||||
spin_unlock(&glob->lock);
|
spin_unlock(&glob->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void ttm_shrink_work(struct work_struct *work)
|
static void ttm_shrink_work(struct work_struct *work)
|
||||||
{
|
{
|
||||||
struct ttm_mem_global *glob =
|
struct ttm_mem_global *glob =
|
||||||
@@ -89,63 +239,178 @@ static void ttm_shrink_work(struct work_struct *work)
|
|||||||
ttm_shrink(glob, true, 0ULL);
|
ttm_shrink(glob, true, 0ULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
|
||||||
|
const struct sysinfo *si)
|
||||||
|
{
|
||||||
|
struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
|
||||||
|
uint64_t mem;
|
||||||
|
|
||||||
|
if (unlikely(!zone))
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
mem = si->totalram - si->totalhigh;
|
||||||
|
mem *= si->mem_unit;
|
||||||
|
|
||||||
|
zone->name = "kernel";
|
||||||
|
zone->zone_mem = mem;
|
||||||
|
zone->max_mem = mem >> 1;
|
||||||
|
zone->emer_mem = (mem >> 1) + (mem >> 2);
|
||||||
|
zone->swap_limit = zone->max_mem - (mem >> 3);
|
||||||
|
zone->used_mem = 0;
|
||||||
|
zone->glob = glob;
|
||||||
|
glob->zone_kernel = zone;
|
||||||
|
glob->zones[glob->num_zones++] = zone;
|
||||||
|
kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
|
||||||
|
return kobject_add(&zone->kobj, &glob->kobj, zone->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef CONFIG_HIGHMEM
|
||||||
|
static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
|
||||||
|
const struct sysinfo *si)
|
||||||
|
{
|
||||||
|
struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
|
||||||
|
uint64_t mem;
|
||||||
|
|
||||||
|
if (unlikely(!zone))
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
if (si->totalhigh == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
mem = si->totalram;
|
||||||
|
mem *= si->mem_unit;
|
||||||
|
|
||||||
|
zone->name = "highmem";
|
||||||
|
zone->zone_mem = mem;
|
||||||
|
zone->max_mem = mem >> 1;
|
||||||
|
zone->emer_mem = (mem >> 1) + (mem >> 2);
|
||||||
|
zone->swap_limit = zone->max_mem - (mem >> 3);
|
||||||
|
zone->used_mem = 0;
|
||||||
|
zone->glob = glob;
|
||||||
|
glob->zone_highmem = zone;
|
||||||
|
glob->zones[glob->num_zones++] = zone;
|
||||||
|
kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
|
||||||
|
return kobject_add(&zone->kobj, &glob->kobj, zone->name);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
|
||||||
|
const struct sysinfo *si)
|
||||||
|
{
|
||||||
|
struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
|
||||||
|
uint64_t mem;
|
||||||
|
|
||||||
|
if (unlikely(!zone))
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
mem = si->totalram;
|
||||||
|
mem *= si->mem_unit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* No special dma32 zone needed.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (mem <= ((uint64_t) 1ULL << 32))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Limit max dma32 memory to 4GB for now
|
||||||
|
* until we can figure out how big this
|
||||||
|
* zone really is.
|
||||||
|
*/
|
||||||
|
|
||||||
|
mem = ((uint64_t) 1ULL << 32);
|
||||||
|
zone->name = "dma32";
|
||||||
|
zone->zone_mem = mem;
|
||||||
|
zone->max_mem = mem >> 1;
|
||||||
|
zone->emer_mem = (mem >> 1) + (mem >> 2);
|
||||||
|
zone->swap_limit = zone->max_mem - (mem >> 3);
|
||||||
|
zone->used_mem = 0;
|
||||||
|
zone->glob = glob;
|
||||||
|
glob->zone_dma32 = zone;
|
||||||
|
glob->zones[glob->num_zones++] = zone;
|
||||||
|
kobject_init(&zone->kobj, &ttm_mem_zone_kobj_type);
|
||||||
|
return kobject_add(&zone->kobj, &glob->kobj, zone->name);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int ttm_mem_global_init(struct ttm_mem_global *glob)
|
int ttm_mem_global_init(struct ttm_mem_global *glob)
|
||||||
{
|
{
|
||||||
struct sysinfo si;
|
struct sysinfo si;
|
||||||
uint64_t mem;
|
int ret;
|
||||||
|
int i;
|
||||||
|
struct ttm_mem_zone *zone;
|
||||||
|
|
||||||
spin_lock_init(&glob->lock);
|
spin_lock_init(&glob->lock);
|
||||||
glob->swap_queue = create_singlethread_workqueue("ttm_swap");
|
glob->swap_queue = create_singlethread_workqueue("ttm_swap");
|
||||||
INIT_WORK(&glob->work, ttm_shrink_work);
|
INIT_WORK(&glob->work, ttm_shrink_work);
|
||||||
init_waitqueue_head(&glob->queue);
|
init_waitqueue_head(&glob->queue);
|
||||||
|
kobject_init(&glob->kobj, &ttm_mem_glob_kobj_type);
|
||||||
|
ret = kobject_add(&glob->kobj,
|
||||||
|
ttm_get_kobj(),
|
||||||
|
"memory_accounting");
|
||||||
|
if (unlikely(ret != 0))
|
||||||
|
goto out_no_zone;
|
||||||
|
|
||||||
si_meminfo(&si);
|
si_meminfo(&si);
|
||||||
|
|
||||||
mem = si.totalram - si.totalhigh;
|
ret = ttm_mem_init_kernel_zone(glob, &si);
|
||||||
mem *= si.mem_unit;
|
if (unlikely(ret != 0))
|
||||||
|
goto out_no_zone;
|
||||||
glob->max_memory = mem >> 1;
|
#ifdef CONFIG_HIGHMEM
|
||||||
glob->emer_memory = (mem >> 1) + (mem >> 2);
|
ret = ttm_mem_init_highmem_zone(glob, &si);
|
||||||
glob->swap_limit = glob->max_memory - (mem >> 3);
|
if (unlikely(ret != 0))
|
||||||
glob->used_memory = 0;
|
goto out_no_zone;
|
||||||
glob->used_total_memory = 0;
|
#else
|
||||||
glob->shrink = NULL;
|
ret = ttm_mem_init_dma32_zone(glob, &si);
|
||||||
|
if (unlikely(ret != 0))
|
||||||
mem = si.totalram;
|
goto out_no_zone;
|
||||||
mem *= si.mem_unit;
|
#endif
|
||||||
|
for (i = 0; i < glob->num_zones; ++i) {
|
||||||
glob->max_total_memory = mem >> 1;
|
zone = glob->zones[i];
|
||||||
glob->emer_total_memory = (mem >> 1) + (mem >> 2);
|
printk(KERN_INFO TTM_PFX
|
||||||
|
"Zone %7s: Available graphics memory: %llu kiB.\n",
|
||||||
glob->total_memory_swap_limit = glob->max_total_memory - (mem >> 3);
|
zone->name, (unsigned long long) zone->max_mem >> 10);
|
||||||
|
}
|
||||||
printk(KERN_INFO TTM_PFX "TTM available graphics memory: %llu MiB\n",
|
|
||||||
glob->max_total_memory >> 20);
|
|
||||||
printk(KERN_INFO TTM_PFX "TTM available object memory: %llu MiB\n",
|
|
||||||
glob->max_memory >> 20);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
out_no_zone:
|
||||||
|
ttm_mem_global_release(glob);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(ttm_mem_global_init);
|
EXPORT_SYMBOL(ttm_mem_global_init);
|
||||||
|
|
||||||
void ttm_mem_global_release(struct ttm_mem_global *glob)
|
void ttm_mem_global_release(struct ttm_mem_global *glob)
|
||||||
{
|
{
|
||||||
printk(KERN_INFO TTM_PFX "Used total memory is %llu bytes.\n",
|
unsigned int i;
|
||||||
(unsigned long long)glob->used_total_memory);
|
struct ttm_mem_zone *zone;
|
||||||
|
|
||||||
flush_workqueue(glob->swap_queue);
|
flush_workqueue(glob->swap_queue);
|
||||||
destroy_workqueue(glob->swap_queue);
|
destroy_workqueue(glob->swap_queue);
|
||||||
glob->swap_queue = NULL;
|
glob->swap_queue = NULL;
|
||||||
|
for (i = 0; i < glob->num_zones; ++i) {
|
||||||
|
zone = glob->zones[i];
|
||||||
|
kobject_del(&zone->kobj);
|
||||||
|
kobject_put(&zone->kobj);
|
||||||
|
}
|
||||||
|
kobject_del(&glob->kobj);
|
||||||
|
kobject_put(&glob->kobj);
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(ttm_mem_global_release);
|
EXPORT_SYMBOL(ttm_mem_global_release);
|
||||||
|
|
||||||
static inline void ttm_check_swapping(struct ttm_mem_global *glob)
|
static void ttm_check_swapping(struct ttm_mem_global *glob)
|
||||||
{
|
{
|
||||||
bool needs_swapping;
|
bool needs_swapping = false;
|
||||||
|
unsigned int i;
|
||||||
|
struct ttm_mem_zone *zone;
|
||||||
|
|
||||||
spin_lock(&glob->lock);
|
spin_lock(&glob->lock);
|
||||||
needs_swapping = (glob->used_memory > glob->swap_limit ||
|
for (i = 0; i < glob->num_zones; ++i) {
|
||||||
glob->used_total_memory >
|
zone = glob->zones[i];
|
||||||
glob->total_memory_swap_limit);
|
if (zone->used_mem > zone->swap_limit) {
|
||||||
|
needs_swapping = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
spin_unlock(&glob->lock);
|
spin_unlock(&glob->lock);
|
||||||
|
|
||||||
if (unlikely(needs_swapping))
|
if (unlikely(needs_swapping))
|
||||||
@@ -153,44 +418,60 @@ static inline void ttm_check_swapping(struct ttm_mem_global *glob)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ttm_mem_global_free(struct ttm_mem_global *glob,
|
static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
|
||||||
uint64_t amount, bool himem)
|
struct ttm_mem_zone *single_zone,
|
||||||
|
uint64_t amount)
|
||||||
{
|
{
|
||||||
|
unsigned int i;
|
||||||
|
struct ttm_mem_zone *zone;
|
||||||
|
|
||||||
spin_lock(&glob->lock);
|
spin_lock(&glob->lock);
|
||||||
glob->used_total_memory -= amount;
|
for (i = 0; i < glob->num_zones; ++i) {
|
||||||
if (!himem)
|
zone = glob->zones[i];
|
||||||
glob->used_memory -= amount;
|
if (single_zone && zone != single_zone)
|
||||||
wake_up_all(&glob->queue);
|
continue;
|
||||||
|
zone->used_mem -= amount;
|
||||||
|
}
|
||||||
spin_unlock(&glob->lock);
|
spin_unlock(&glob->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ttm_mem_global_free(struct ttm_mem_global *glob,
|
||||||
|
uint64_t amount)
|
||||||
|
{
|
||||||
|
return ttm_mem_global_free_zone(glob, NULL, amount);
|
||||||
|
}
|
||||||
|
|
||||||
static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
|
static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
|
||||||
uint64_t amount, bool himem, bool reserve)
|
struct ttm_mem_zone *single_zone,
|
||||||
|
uint64_t amount, bool reserve)
|
||||||
{
|
{
|
||||||
uint64_t limit;
|
uint64_t limit;
|
||||||
uint64_t lomem_limit;
|
|
||||||
int ret = -ENOMEM;
|
int ret = -ENOMEM;
|
||||||
|
unsigned int i;
|
||||||
|
struct ttm_mem_zone *zone;
|
||||||
|
|
||||||
spin_lock(&glob->lock);
|
spin_lock(&glob->lock);
|
||||||
|
for (i = 0; i < glob->num_zones; ++i) {
|
||||||
|
zone = glob->zones[i];
|
||||||
|
if (single_zone && zone != single_zone)
|
||||||
|
continue;
|
||||||
|
|
||||||
if (capable(CAP_SYS_ADMIN)) {
|
limit = (capable(CAP_SYS_ADMIN)) ?
|
||||||
limit = glob->emer_total_memory;
|
zone->emer_mem : zone->max_mem;
|
||||||
lomem_limit = glob->emer_memory;
|
|
||||||
} else {
|
if (zone->used_mem > limit)
|
||||||
limit = glob->max_total_memory;
|
goto out_unlock;
|
||||||
lomem_limit = glob->max_memory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlikely(glob->used_total_memory + amount > limit))
|
|
||||||
goto out_unlock;
|
|
||||||
if (unlikely(!himem && glob->used_memory + amount > lomem_limit))
|
|
||||||
goto out_unlock;
|
|
||||||
|
|
||||||
if (reserve) {
|
if (reserve) {
|
||||||
glob->used_total_memory += amount;
|
for (i = 0; i < glob->num_zones; ++i) {
|
||||||
if (!himem)
|
zone = glob->zones[i];
|
||||||
glob->used_memory += amount;
|
if (single_zone && zone != single_zone)
|
||||||
|
continue;
|
||||||
|
zone->used_mem += amount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
out_unlock:
|
out_unlock:
|
||||||
spin_unlock(&glob->lock);
|
spin_unlock(&glob->lock);
|
||||||
@@ -199,12 +480,17 @@ out_unlock:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
|
|
||||||
bool no_wait, bool interruptible, bool himem)
|
static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
|
||||||
|
struct ttm_mem_zone *single_zone,
|
||||||
|
uint64_t memory,
|
||||||
|
bool no_wait, bool interruptible)
|
||||||
{
|
{
|
||||||
int count = TTM_MEMORY_ALLOC_RETRIES;
|
int count = TTM_MEMORY_ALLOC_RETRIES;
|
||||||
|
|
||||||
while (unlikely(ttm_mem_global_reserve(glob, memory, himem, true)
|
while (unlikely(ttm_mem_global_reserve(glob,
|
||||||
|
single_zone,
|
||||||
|
memory, true)
|
||||||
!= 0)) {
|
!= 0)) {
|
||||||
if (no_wait)
|
if (no_wait)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
@@ -216,6 +502,56 @@ int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
|
||||||
|
bool no_wait, bool interruptible)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Normal allocations of kernel memory are registered in
|
||||||
|
* all zones.
|
||||||
|
*/
|
||||||
|
|
||||||
|
return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait,
|
||||||
|
interruptible);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
|
||||||
|
struct page *page,
|
||||||
|
bool no_wait, bool interruptible)
|
||||||
|
{
|
||||||
|
|
||||||
|
struct ttm_mem_zone *zone = NULL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Page allocations may be registed in a single zone
|
||||||
|
* only if highmem or !dma32.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef CONFIG_HIGHMEM
|
||||||
|
if (PageHighMem(page) && glob->zone_highmem != NULL)
|
||||||
|
zone = glob->zone_highmem;
|
||||||
|
#else
|
||||||
|
if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
|
||||||
|
zone = glob->zone_kernel;
|
||||||
|
#endif
|
||||||
|
return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait,
|
||||||
|
interruptible);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page)
|
||||||
|
{
|
||||||
|
struct ttm_mem_zone *zone = NULL;
|
||||||
|
|
||||||
|
#ifdef CONFIG_HIGHMEM
|
||||||
|
if (PageHighMem(page) && glob->zone_highmem != NULL)
|
||||||
|
zone = glob->zone_highmem;
|
||||||
|
#else
|
||||||
|
if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
|
||||||
|
zone = glob->zone_kernel;
|
||||||
|
#endif
|
||||||
|
ttm_mem_global_free_zone(glob, zone, PAGE_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
size_t ttm_round_pot(size_t size)
|
size_t ttm_round_pot(size_t size)
|
||||||
{
|
{
|
||||||
if ((size & (size - 1)) == 0)
|
if ((size & (size - 1)) == 0)
|
||||||
|
@@ -166,7 +166,7 @@ static void ttm_tt_free_user_pages(struct ttm_tt *ttm)
|
|||||||
set_page_dirty_lock(page);
|
set_page_dirty_lock(page);
|
||||||
|
|
||||||
ttm->pages[i] = NULL;
|
ttm->pages[i] = NULL;
|
||||||
ttm_mem_global_free(ttm->bdev->mem_glob, PAGE_SIZE, false);
|
ttm_mem_global_free(ttm->bdev->mem_glob, PAGE_SIZE);
|
||||||
put_page(page);
|
put_page(page);
|
||||||
}
|
}
|
||||||
ttm->state = tt_unpopulated;
|
ttm->state = tt_unpopulated;
|
||||||
@@ -187,21 +187,14 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
|
|||||||
if (!p)
|
if (!p)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (PageHighMem(p)) {
|
ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
|
||||||
ret =
|
if (unlikely(ret != 0))
|
||||||
ttm_mem_global_alloc(mem_glob, PAGE_SIZE,
|
goto out_err;
|
||||||
false, false, true);
|
|
||||||
if (unlikely(ret != 0))
|
if (PageHighMem(p))
|
||||||
goto out_err;
|
|
||||||
ttm->pages[--ttm->first_himem_page] = p;
|
ttm->pages[--ttm->first_himem_page] = p;
|
||||||
} else {
|
else
|
||||||
ret =
|
|
||||||
ttm_mem_global_alloc(mem_glob, PAGE_SIZE,
|
|
||||||
false, false, false);
|
|
||||||
if (unlikely(ret != 0))
|
|
||||||
goto out_err;
|
|
||||||
ttm->pages[++ttm->last_lomem_page] = p;
|
ttm->pages[++ttm->last_lomem_page] = p;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
out_err:
|
out_err:
|
||||||
@@ -355,8 +348,8 @@ static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
|
|||||||
printk(KERN_ERR TTM_PFX
|
printk(KERN_ERR TTM_PFX
|
||||||
"Erroneous page count. "
|
"Erroneous page count. "
|
||||||
"Leaking pages.\n");
|
"Leaking pages.\n");
|
||||||
ttm_mem_global_free(ttm->bdev->mem_glob, PAGE_SIZE,
|
ttm_mem_global_free_page(ttm->bdev->mem_glob,
|
||||||
PageHighMem(cur_page));
|
cur_page);
|
||||||
__free_page(cur_page);
|
__free_page(cur_page);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -411,7 +404,7 @@ int ttm_tt_set_user(struct ttm_tt *ttm,
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
ret = ttm_mem_global_alloc(mem_glob, num_pages * PAGE_SIZE,
|
ret = ttm_mem_global_alloc(mem_glob, num_pages * PAGE_SIZE,
|
||||||
false, false, false);
|
false, false);
|
||||||
if (unlikely(ret != 0))
|
if (unlikely(ret != 0))
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
@@ -422,7 +415,7 @@ int ttm_tt_set_user(struct ttm_tt *ttm,
|
|||||||
|
|
||||||
if (ret != num_pages && write) {
|
if (ret != num_pages && write) {
|
||||||
ttm_tt_free_user_pages(ttm);
|
ttm_tt_free_user_pages(ttm);
|
||||||
ttm_mem_global_free(mem_glob, num_pages * PAGE_SIZE, false);
|
ttm_mem_global_free(mem_glob, num_pages * PAGE_SIZE);
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -32,6 +32,7 @@
|
|||||||
#include <linux/spinlock.h>
|
#include <linux/spinlock.h>
|
||||||
#include <linux/wait.h>
|
#include <linux/wait.h>
|
||||||
#include <linux/errno.h>
|
#include <linux/errno.h>
|
||||||
|
#include <linux/kobject.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* struct ttm_mem_shrink - callback to shrink TTM memory usage.
|
* struct ttm_mem_shrink - callback to shrink TTM memory usage.
|
||||||
@@ -60,34 +61,33 @@ struct ttm_mem_shrink {
|
|||||||
* @queue: Wait queue for processes suspended waiting for memory.
|
* @queue: Wait queue for processes suspended waiting for memory.
|
||||||
* @lock: Lock to protect the @shrink - and the memory accounting members,
|
* @lock: Lock to protect the @shrink - and the memory accounting members,
|
||||||
* that is, essentially the whole structure with some exceptions.
|
* that is, essentially the whole structure with some exceptions.
|
||||||
* @emer_memory: Lowmem memory limit available for root.
|
* @zones: Array of pointers to accounting zones.
|
||||||
* @max_memory: Lowmem memory limit available for non-root.
|
* @num_zones: Number of populated entries in the @zones array.
|
||||||
* @swap_limit: Lowmem memory limit where the shrink workqueue kicks in.
|
* @zone_kernel: Pointer to the kernel zone.
|
||||||
* @used_memory: Currently used lowmem memory.
|
* @zone_highmem: Pointer to the highmem zone if there is one.
|
||||||
* @used_total_memory: Currently used total (lowmem + highmem) memory.
|
* @zone_dma32: Pointer to the dma32 zone if there is one.
|
||||||
* @total_memory_swap_limit: Total memory limit where the shrink workqueue
|
|
||||||
* kicks in.
|
|
||||||
* @max_total_memory: Total memory available to non-root processes.
|
|
||||||
* @emer_total_memory: Total memory available to root processes.
|
|
||||||
*
|
*
|
||||||
* Note that this structure is not per device. It should be global for all
|
* Note that this structure is not per device. It should be global for all
|
||||||
* graphics devices.
|
* graphics devices.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define TTM_MEM_MAX_ZONES 2
|
||||||
|
struct ttm_mem_zone;
|
||||||
struct ttm_mem_global {
|
struct ttm_mem_global {
|
||||||
|
struct kobject kobj;
|
||||||
struct ttm_mem_shrink *shrink;
|
struct ttm_mem_shrink *shrink;
|
||||||
struct workqueue_struct *swap_queue;
|
struct workqueue_struct *swap_queue;
|
||||||
struct work_struct work;
|
struct work_struct work;
|
||||||
wait_queue_head_t queue;
|
wait_queue_head_t queue;
|
||||||
spinlock_t lock;
|
spinlock_t lock;
|
||||||
uint64_t emer_memory;
|
struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
|
||||||
uint64_t max_memory;
|
unsigned int num_zones;
|
||||||
uint64_t swap_limit;
|
struct ttm_mem_zone *zone_kernel;
|
||||||
uint64_t used_memory;
|
#ifdef CONFIG_HIGHMEM
|
||||||
uint64_t used_total_memory;
|
struct ttm_mem_zone *zone_highmem;
|
||||||
uint64_t total_memory_swap_limit;
|
#else
|
||||||
uint64_t max_total_memory;
|
struct ttm_mem_zone *zone_dma32;
|
||||||
uint64_t emer_total_memory;
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -146,8 +146,13 @@ static inline void ttm_mem_unregister_shrink(struct ttm_mem_global *glob,
|
|||||||
extern int ttm_mem_global_init(struct ttm_mem_global *glob);
|
extern int ttm_mem_global_init(struct ttm_mem_global *glob);
|
||||||
extern void ttm_mem_global_release(struct ttm_mem_global *glob);
|
extern void ttm_mem_global_release(struct ttm_mem_global *glob);
|
||||||
extern int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
|
extern int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
|
||||||
bool no_wait, bool interruptible, bool himem);
|
bool no_wait, bool interruptible);
|
||||||
extern void ttm_mem_global_free(struct ttm_mem_global *glob,
|
extern void ttm_mem_global_free(struct ttm_mem_global *glob,
|
||||||
uint64_t amount, bool himem);
|
uint64_t amount);
|
||||||
|
extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
|
||||||
|
struct page *page,
|
||||||
|
bool no_wait, bool interruptible);
|
||||||
|
extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
|
||||||
|
struct page *page);
|
||||||
extern size_t ttm_round_pot(size_t size);
|
extern size_t ttm_round_pot(size_t size);
|
||||||
#endif
|
#endif
|
||||||
|
@@ -32,6 +32,7 @@
|
|||||||
#define _TTM_MODULE_H_
|
#define _TTM_MODULE_H_
|
||||||
|
|
||||||
#include <linux/kernel.h>
|
#include <linux/kernel.h>
|
||||||
|
struct kobject;
|
||||||
|
|
||||||
#define TTM_PFX "[TTM]"
|
#define TTM_PFX "[TTM]"
|
||||||
|
|
||||||
@@ -54,5 +55,6 @@ extern void ttm_global_init(void);
|
|||||||
extern void ttm_global_release(void);
|
extern void ttm_global_release(void);
|
||||||
extern int ttm_global_item_ref(struct ttm_global_reference *ref);
|
extern int ttm_global_item_ref(struct ttm_global_reference *ref);
|
||||||
extern void ttm_global_item_unref(struct ttm_global_reference *ref);
|
extern void ttm_global_item_unref(struct ttm_global_reference *ref);
|
||||||
|
extern struct kobject *ttm_get_kobj(void);
|
||||||
|
|
||||||
#endif /* _TTM_MODULE_H_ */
|
#endif /* _TTM_MODULE_H_ */
|
||||||
|
Reference in New Issue
Block a user