2018-05-02 21:46:21 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
|
2009-06-10 21:20:19 +08:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
* of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
**************************************************************************/
|
|
|
|
|
2012-03-17 12:43:50 +08:00
|
|
|
#define pr_fmt(fmt) "[TTM] " fmt
|
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/wait.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/module.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2018-02-05 17:57:07 +08:00
|
|
|
#include <linux/swap.h>
|
2009-06-10 21:20:19 +08:00
|
|
|
|
2020-11-17 20:52:28 +08:00
|
|
|
#include <drm/drm_device.h>
|
|
|
|
#include <drm/drm_file.h>
|
2020-10-06 19:35:32 +08:00
|
|
|
#include <drm/ttm/ttm_device.h>
|
2020-11-17 20:52:28 +08:00
|
|
|
|
|
|
|
#include "ttm_memory.h"
|
2020-11-17 22:44:07 +08:00
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
#define TTM_MEMORY_ALLOC_RETRIES 4
|
|
|
|
|
2018-10-19 19:49:05 +08:00
|
|
|
struct ttm_mem_global ttm_mem_glob;
|
|
|
|
EXPORT_SYMBOL(ttm_mem_glob);
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
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);
|
|
|
|
|
2019-03-01 00:48:34 +08:00
|
|
|
pr_info("Zone %7s: Used memory at exit: %llu KiB\n",
|
2012-03-17 12:43:50 +08:00
|
|
|
zone->name, (unsigned long long)zone->used_mem >> 10);
|
2009-08-17 22:28:39 +08:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2010-01-19 09:58:23 +08:00
|
|
|
static const struct sysfs_ops ttm_mem_zone_ops = {
|
2009-08-17 22:28:39 +08:00
|
|
|
.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,
|
|
|
|
};
|
|
|
|
|
2018-02-05 17:57:07 +08:00
|
|
|
static struct attribute ttm_mem_global_lower_mem_limit = {
|
|
|
|
.name = "lower_mem_limit",
|
|
|
|
.mode = S_IRUGO | S_IWUSR
|
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t ttm_mem_global_show(struct kobject *kobj,
|
|
|
|
struct attribute *attr,
|
|
|
|
char *buffer)
|
|
|
|
{
|
|
|
|
struct ttm_mem_global *glob =
|
|
|
|
container_of(kobj, struct ttm_mem_global, kobj);
|
|
|
|
uint64_t val = 0;
|
|
|
|
|
|
|
|
spin_lock(&glob->lock);
|
|
|
|
val = glob->lower_mem_limit;
|
|
|
|
spin_unlock(&glob->lock);
|
|
|
|
/* convert from number of pages to KB */
|
|
|
|
val <<= (PAGE_SHIFT - 10);
|
|
|
|
return snprintf(buffer, PAGE_SIZE, "%llu\n",
|
|
|
|
(unsigned long long) val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ttm_mem_global_store(struct kobject *kobj,
|
|
|
|
struct attribute *attr,
|
|
|
|
const char *buffer,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
int chars;
|
|
|
|
uint64_t val64;
|
|
|
|
unsigned long val;
|
|
|
|
struct ttm_mem_global *glob =
|
|
|
|
container_of(kobj, struct ttm_mem_global, kobj);
|
|
|
|
|
|
|
|
chars = sscanf(buffer, "%lu", &val);
|
|
|
|
if (chars == 0)
|
|
|
|
return size;
|
|
|
|
|
|
|
|
val64 = val;
|
|
|
|
/* convert from KB to number of pages */
|
|
|
|
val64 >>= (PAGE_SHIFT - 10);
|
|
|
|
|
|
|
|
spin_lock(&glob->lock);
|
|
|
|
glob->lower_mem_limit = val64;
|
|
|
|
spin_unlock(&glob->lock);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct attribute *ttm_mem_global_attrs[] = {
|
|
|
|
&ttm_mem_global_lower_mem_limit,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct sysfs_ops ttm_mem_global_ops = {
|
|
|
|
.show = &ttm_mem_global_show,
|
|
|
|
.store = &ttm_mem_global_store,
|
|
|
|
};
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
static struct kobj_type ttm_mem_glob_kobj_type = {
|
2018-02-05 17:57:07 +08:00
|
|
|
.sysfs_ops = &ttm_mem_global_ops,
|
|
|
|
.default_attrs = ttm_mem_global_attrs,
|
2009-08-17 22:28:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:52:58 +08:00
|
|
|
/*
|
2009-06-10 21:20:19 +08:00
|
|
|
* At this point we only support a single shrink callback.
|
|
|
|
* Extend this if needed, perhaps using a linked list of callbacks.
|
|
|
|
* Note that this function is reentrant:
|
|
|
|
* many threads may try to swap out at any given time.
|
|
|
|
*/
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
|
2017-12-21 17:42:53 +08:00
|
|
|
uint64_t extra, struct ttm_operation_ctx *ctx)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
spin_lock(&glob->lock);
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
|
2009-06-10 21:20:19 +08:00
|
|
|
spin_unlock(&glob->lock);
|
2020-10-06 19:35:32 +08:00
|
|
|
ret = ttm_global_swapout(ctx, GFP_KERNEL);
|
2009-06-10 21:20:19 +08:00
|
|
|
spin_lock(&glob->lock);
|
2021-04-22 16:37:49 +08:00
|
|
|
if (unlikely(ret <= 0))
|
2017-12-18 19:50:08 +08:00
|
|
|
break;
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
2017-12-18 19:50:08 +08:00
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
spin_unlock(&glob->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ttm_shrink_work(struct work_struct *work)
|
|
|
|
{
|
2017-12-21 17:42:53 +08:00
|
|
|
struct ttm_operation_ctx ctx = {
|
|
|
|
.interruptible = false,
|
|
|
|
.no_wait_gpu = false
|
|
|
|
};
|
2009-06-10 21:20:19 +08:00
|
|
|
struct ttm_mem_global *glob =
|
|
|
|
container_of(work, struct ttm_mem_global, work);
|
|
|
|
|
2017-12-21 17:42:53 +08:00
|
|
|
ttm_shrink(glob, true, 0ULL, &ctx);
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
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;
|
2009-08-20 16:29:09 +08:00
|
|
|
int ret;
|
2009-08-17 22:28:39 +08:00
|
|
|
|
|
|
|
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;
|
2010-03-13 18:36:32 +08:00
|
|
|
ret = kobject_init_and_add(
|
|
|
|
&zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
|
2009-08-20 16:29:09 +08:00
|
|
|
if (unlikely(ret != 0)) {
|
|
|
|
kobject_put(&zone->kobj);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
glob->zones[glob->num_zones++] = zone;
|
|
|
|
return 0;
|
2009-08-17 22:28:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_HIGHMEM
|
|
|
|
static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
|
|
|
|
const struct sysinfo *si)
|
|
|
|
{
|
2009-11-28 18:30:32 +08:00
|
|
|
struct ttm_mem_zone *zone;
|
2009-08-17 22:28:39 +08:00
|
|
|
uint64_t mem;
|
2009-08-20 16:29:09 +08:00
|
|
|
int ret;
|
2009-08-17 22:28:39 +08:00
|
|
|
|
|
|
|
if (si->totalhigh == 0)
|
|
|
|
return 0;
|
|
|
|
|
2009-11-28 18:30:32 +08:00
|
|
|
zone = kzalloc(sizeof(*zone), GFP_KERNEL);
|
|
|
|
if (unlikely(!zone))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
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;
|
2010-03-13 18:36:32 +08:00
|
|
|
ret = kobject_init_and_add(
|
2014-09-12 04:53:54 +08:00
|
|
|
&zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, "%s",
|
|
|
|
zone->name);
|
2009-08-20 16:29:09 +08:00
|
|
|
if (unlikely(ret != 0)) {
|
|
|
|
kobject_put(&zone->kobj);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
glob->zones[glob->num_zones++] = zone;
|
|
|
|
return 0;
|
2009-08-17 22:28:39 +08:00
|
|
|
}
|
|
|
|
#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;
|
2009-08-20 16:29:09 +08:00
|
|
|
int ret;
|
2009-08-17 22:28:39 +08:00
|
|
|
|
|
|
|
if (unlikely(!zone))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mem = si->totalram;
|
|
|
|
mem *= si->mem_unit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No special dma32 zone needed.
|
|
|
|
*/
|
|
|
|
|
2009-12-08 13:58:08 +08:00
|
|
|
if (mem <= ((uint64_t) 1ULL << 32)) {
|
|
|
|
kfree(zone);
|
2009-08-17 22:28:39 +08:00
|
|
|
return 0;
|
2009-12-08 13:58:08 +08:00
|
|
|
}
|
2009-08-17 22:28:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2010-03-13 18:36:32 +08:00
|
|
|
ret = kobject_init_and_add(
|
|
|
|
&zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
|
2009-08-20 16:29:09 +08:00
|
|
|
if (unlikely(ret != 0)) {
|
|
|
|
kobject_put(&zone->kobj);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
glob->zones[glob->num_zones++] = zone;
|
|
|
|
return 0;
|
2009-08-17 22:28:39 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-11-17 20:52:28 +08:00
|
|
|
int ttm_mem_global_init(struct ttm_mem_global *glob, struct device *dev)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
|
|
|
struct sysinfo si;
|
2009-08-17 22:28:39 +08:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
struct ttm_mem_zone *zone;
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
spin_lock_init(&glob->lock);
|
|
|
|
glob->swap_queue = create_singlethread_workqueue("ttm_swap");
|
|
|
|
INIT_WORK(&glob->work, ttm_shrink_work);
|
2020-11-17 20:52:28 +08:00
|
|
|
|
|
|
|
ret = kobject_init_and_add(&glob->kobj, &ttm_mem_glob_kobj_type,
|
|
|
|
&dev->kobj, "memory_accounting");
|
2009-08-20 16:29:09 +08:00
|
|
|
if (unlikely(ret != 0)) {
|
|
|
|
kobject_put(&glob->kobj);
|
|
|
|
return ret;
|
|
|
|
}
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
si_meminfo(&si);
|
|
|
|
|
2018-02-05 17:57:07 +08:00
|
|
|
/* set it as 0 by default to keep original behavior of OOM */
|
|
|
|
glob->lower_mem_limit = 0;
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
ret = ttm_mem_init_kernel_zone(glob, &si);
|
|
|
|
if (unlikely(ret != 0))
|
|
|
|
goto out_no_zone;
|
|
|
|
#ifdef CONFIG_HIGHMEM
|
|
|
|
ret = ttm_mem_init_highmem_zone(glob, &si);
|
|
|
|
if (unlikely(ret != 0))
|
|
|
|
goto out_no_zone;
|
|
|
|
#else
|
|
|
|
ret = ttm_mem_init_dma32_zone(glob, &si);
|
|
|
|
if (unlikely(ret != 0))
|
|
|
|
goto out_no_zone;
|
|
|
|
#endif
|
|
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
|
|
zone = glob->zones[i];
|
2019-03-01 00:48:34 +08:00
|
|
|
pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
|
2012-03-17 12:43:50 +08:00
|
|
|
zone->name, (unsigned long long)zone->max_mem >> 10);
|
2009-08-17 22:28:39 +08:00
|
|
|
}
|
2009-06-10 21:20:19 +08:00
|
|
|
return 0;
|
2009-08-17 22:28:39 +08:00
|
|
|
out_no_zone:
|
|
|
|
ttm_mem_global_release(glob);
|
|
|
|
return ret;
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ttm_mem_global_release(struct ttm_mem_global *glob)
|
|
|
|
{
|
2009-08-17 22:28:39 +08:00
|
|
|
struct ttm_mem_zone *zone;
|
2019-04-16 17:49:17 +08:00
|
|
|
unsigned int i;
|
2009-08-17 22:28:39 +08:00
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
flush_workqueue(glob->swap_queue);
|
|
|
|
destroy_workqueue(glob->swap_queue);
|
|
|
|
glob->swap_queue = NULL;
|
2009-08-17 22:28:39 +08:00
|
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
|
|
zone = glob->zones[i];
|
|
|
|
kobject_del(&zone->kobj);
|
|
|
|
kobject_put(&zone->kobj);
|
2019-04-16 17:49:17 +08:00
|
|
|
}
|
2009-08-17 22:28:39 +08:00
|
|
|
kobject_del(&glob->kobj);
|
|
|
|
kobject_put(&glob->kobj);
|
2019-04-16 17:49:17 +08:00
|
|
|
memset(glob, 0, sizeof(*glob));
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
static void ttm_check_swapping(struct ttm_mem_global *glob)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
2009-08-17 22:28:39 +08:00
|
|
|
bool needs_swapping = false;
|
|
|
|
unsigned int i;
|
|
|
|
struct ttm_mem_zone *zone;
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
spin_lock(&glob->lock);
|
2009-08-17 22:28:39 +08:00
|
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
|
|
zone = glob->zones[i];
|
|
|
|
if (zone->used_mem > zone->swap_limit) {
|
|
|
|
needs_swapping = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
spin_unlock(&glob->lock);
|
|
|
|
|
|
|
|
if (unlikely(needs_swapping))
|
|
|
|
(void)queue_work(glob->swap_queue, &glob->work);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
|
|
|
|
struct ttm_mem_zone *single_zone,
|
|
|
|
uint64_t amount)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
2009-08-17 22:28:39 +08:00
|
|
|
unsigned int i;
|
|
|
|
struct ttm_mem_zone *zone;
|
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
spin_lock(&glob->lock);
|
2009-08-17 22:28:39 +08:00
|
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
|
|
zone = glob->zones[i];
|
|
|
|
if (single_zone && zone != single_zone)
|
|
|
|
continue;
|
|
|
|
zone->used_mem -= amount;
|
|
|
|
}
|
2009-06-10 21:20:19 +08:00
|
|
|
spin_unlock(&glob->lock);
|
|
|
|
}
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
void ttm_mem_global_free(struct ttm_mem_global *glob,
|
|
|
|
uint64_t amount)
|
|
|
|
{
|
2019-02-23 03:44:41 +08:00
|
|
|
return ttm_mem_global_free_zone(glob, glob->zone_kernel, amount);
|
2009-08-17 22:28:39 +08:00
|
|
|
}
|
2009-12-07 04:46:27 +08:00
|
|
|
EXPORT_SYMBOL(ttm_mem_global_free);
|
2009-08-17 22:28:39 +08:00
|
|
|
|
2018-02-05 17:57:07 +08:00
|
|
|
/*
|
|
|
|
* check if the available mem is under lower memory limit
|
|
|
|
*
|
|
|
|
* a. if no swap disk at all or free swap space is under swap_mem_limit
|
|
|
|
* but available system mem is bigger than sys_mem_limit, allow TTM
|
|
|
|
* allocation;
|
|
|
|
*
|
|
|
|
* b. if the available system mem is less than sys_mem_limit but free
|
|
|
|
* swap disk is bigger than swap_mem_limit, allow TTM allocation.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
ttm_check_under_lowerlimit(struct ttm_mem_global *glob,
|
|
|
|
uint64_t num_pages,
|
|
|
|
struct ttm_operation_ctx *ctx)
|
|
|
|
{
|
|
|
|
int64_t available;
|
|
|
|
|
2020-11-02 20:16:13 +08:00
|
|
|
/* We allow over commit during suspend */
|
|
|
|
if (ctx->force_alloc)
|
2018-02-05 17:57:07 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
available = get_nr_swap_pages() + si_mem_available();
|
|
|
|
available -= num_pages;
|
|
|
|
if (available < glob->lower_mem_limit)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
|
2009-08-17 22:28:39 +08:00
|
|
|
struct ttm_mem_zone *single_zone,
|
|
|
|
uint64_t amount, bool reserve)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
|
|
|
uint64_t limit;
|
|
|
|
int ret = -ENOMEM;
|
2009-08-17 22:28:39 +08:00
|
|
|
unsigned int i;
|
|
|
|
struct ttm_mem_zone *zone;
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
spin_lock(&glob->lock);
|
2009-08-17 22:28:39 +08:00
|
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
|
|
zone = glob->zones[i];
|
|
|
|
if (single_zone && zone != single_zone)
|
|
|
|
continue;
|
2009-06-10 21:20:19 +08:00
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
limit = (capable(CAP_SYS_ADMIN)) ?
|
|
|
|
zone->emer_mem : zone->max_mem;
|
2009-06-10 21:20:19 +08:00
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
if (zone->used_mem > limit)
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
if (reserve) {
|
2009-08-17 22:28:39 +08:00
|
|
|
for (i = 0; i < glob->num_zones; ++i) {
|
|
|
|
zone = glob->zones[i];
|
|
|
|
if (single_zone && zone != single_zone)
|
|
|
|
continue;
|
|
|
|
zone->used_mem += amount;
|
|
|
|
}
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
2009-08-17 22:28:39 +08:00
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
ret = 0;
|
|
|
|
out_unlock:
|
|
|
|
spin_unlock(&glob->lock);
|
|
|
|
ttm_check_swapping(glob);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
|
|
|
|
static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
|
|
|
|
struct ttm_mem_zone *single_zone,
|
|
|
|
uint64_t memory,
|
2017-12-08 15:09:50 +08:00
|
|
|
struct ttm_operation_ctx *ctx)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
|
|
|
int count = TTM_MEMORY_ALLOC_RETRIES;
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
while (unlikely(ttm_mem_global_reserve(glob,
|
|
|
|
single_zone,
|
|
|
|
memory, true)
|
2009-06-10 21:20:19 +08:00
|
|
|
!= 0)) {
|
2017-12-08 15:09:50 +08:00
|
|
|
if (ctx->no_wait_gpu)
|
2009-06-10 21:20:19 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
if (unlikely(count-- == 0))
|
|
|
|
return -ENOMEM;
|
2017-12-21 17:42:53 +08:00
|
|
|
ttm_shrink(glob, false, memory + (memory >> 2) + 16, ctx);
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-17 22:28:39 +08:00
|
|
|
int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
|
2017-12-08 15:09:50 +08:00
|
|
|
struct ttm_operation_ctx *ctx)
|
2009-08-17 22:28:39 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Normal allocations of kernel memory are registered in
|
2019-02-23 03:44:41 +08:00
|
|
|
* the kernel zone.
|
2009-08-17 22:28:39 +08:00
|
|
|
*/
|
|
|
|
|
2019-02-23 03:44:41 +08:00
|
|
|
return ttm_mem_global_alloc_zone(glob, glob->zone_kernel, memory, ctx);
|
2009-08-17 22:28:39 +08:00
|
|
|
}
|
2009-12-07 04:46:27 +08:00
|
|
|
EXPORT_SYMBOL(ttm_mem_global_alloc);
|
2009-08-17 22:28:39 +08:00
|
|
|
|
|
|
|
int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
|
2017-12-08 15:21:18 +08:00
|
|
|
struct page *page, uint64_t size,
|
|
|
|
struct ttm_operation_ctx *ctx)
|
2009-08-17 22:28:39 +08:00
|
|
|
{
|
|
|
|
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
|
2017-12-08 15:21:18 +08:00
|
|
|
return ttm_mem_global_alloc_zone(glob, zone, size, ctx);
|
2009-08-17 22:28:39 +08:00
|
|
|
}
|
|
|
|
|
2017-07-04 22:56:24 +08:00
|
|
|
void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page,
|
|
|
|
uint64_t size)
|
2009-08-17 22:28:39 +08:00
|
|
|
{
|
|
|
|
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
|
2017-07-04 22:56:24 +08:00
|
|
|
ttm_mem_global_free_zone(glob, zone, size);
|
2009-08-17 22:28:39 +08:00
|
|
|
}
|
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
size_t ttm_round_pot(size_t size)
|
|
|
|
{
|
|
|
|
if ((size & (size - 1)) == 0)
|
|
|
|
return size;
|
|
|
|
else if (size > PAGE_SIZE)
|
|
|
|
return PAGE_ALIGN(size);
|
|
|
|
else {
|
|
|
|
size_t tmp_size = 4;
|
|
|
|
|
|
|
|
while (tmp_size < size)
|
|
|
|
tmp_size <<= 1;
|
|
|
|
|
|
|
|
return tmp_size;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-12-07 04:46:27 +08:00
|
|
|
EXPORT_SYMBOL(ttm_round_pot);
|