2019-06-03 07:44:50 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-05-17 16:19:32 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2016 Red Hat
|
|
|
|
|
* Author: Rob Clark <robdclark@gmail.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-14 16:51:34 +08:00
|
|
|
#include <linux/vmalloc.h>
|
2021-12-02 12:34:00 -08:00
|
|
|
#include <linux/sched/mm.h>
|
2021-10-14 16:51:34 +08:00
|
|
|
|
2016-05-17 16:19:32 -04:00
|
|
|
#include "msm_drv.h"
|
|
|
|
|
#include "msm_gem.h"
|
2020-11-16 09:48:50 -08:00
|
|
|
#include "msm_gpu.h"
|
2020-09-01 08:41:55 -07:00
|
|
|
#include "msm_gpu_trace.h"
|
2016-05-17 16:19:32 -04:00
|
|
|
|
2021-04-05 10:45:31 -07:00
|
|
|
/* Default disabled for now until it has some more testing on the different
|
|
|
|
|
* iommu combinations that can be paired with the driver:
|
|
|
|
|
*/
|
2022-06-25 15:54:37 -07:00
|
|
|
static bool enable_eviction = false;
|
2021-04-05 10:45:31 -07:00
|
|
|
MODULE_PARM_DESC(enable_eviction, "Enable swappable GEM buffers");
|
|
|
|
|
module_param(enable_eviction, bool, 0600);
|
|
|
|
|
|
|
|
|
|
static bool can_swap(void)
|
|
|
|
|
{
|
|
|
|
|
return enable_eviction && get_nr_swap_pages() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-17 16:19:32 -04:00
|
|
|
static unsigned long
|
|
|
|
|
msm_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
|
|
|
|
|
{
|
|
|
|
|
struct msm_drm_private *priv =
|
|
|
|
|
container_of(shrinker, struct msm_drm_private, shrinker);
|
2022-08-02 08:51:43 -07:00
|
|
|
unsigned count = priv->lru.dontneed.count;
|
2021-04-05 10:45:31 -07:00
|
|
|
|
|
|
|
|
if (can_swap())
|
2022-08-02 08:51:43 -07:00
|
|
|
count += priv->lru.willneed.count;
|
2021-04-05 10:45:31 -07:00
|
|
|
|
|
|
|
|
return count;
|
2016-05-17 16:19:32 -04:00
|
|
|
}
|
|
|
|
|
|
2021-04-05 10:45:25 -07:00
|
|
|
static bool
|
2022-08-02 08:51:43 -07:00
|
|
|
purge(struct drm_gem_object *obj)
|
2021-04-05 10:45:25 -07:00
|
|
|
{
|
2022-08-02 08:51:43 -07:00
|
|
|
if (!is_purgeable(to_msm_bo(obj)))
|
2021-04-05 10:45:25 -07:00
|
|
|
return false;
|
|
|
|
|
|
2022-08-02 08:51:43 -07:00
|
|
|
if (msm_gem_active(obj))
|
2022-08-02 08:51:37 -07:00
|
|
|
return false;
|
|
|
|
|
|
2022-08-02 08:51:43 -07:00
|
|
|
msm_gem_purge(obj);
|
2021-04-05 10:45:25 -07:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 10:45:31 -07:00
|
|
|
static bool
|
2022-08-02 08:51:43 -07:00
|
|
|
evict(struct drm_gem_object *obj)
|
2021-04-05 10:45:31 -07:00
|
|
|
{
|
2022-08-02 08:51:43 -07:00
|
|
|
if (is_unevictable(to_msm_bo(obj)))
|
2021-04-05 10:45:31 -07:00
|
|
|
return false;
|
|
|
|
|
|
2022-08-02 08:51:43 -07:00
|
|
|
if (msm_gem_active(obj))
|
2022-08-02 08:51:37 -07:00
|
|
|
return false;
|
|
|
|
|
|
2022-08-02 08:51:43 -07:00
|
|
|
msm_gem_evict(obj);
|
2021-04-05 10:45:31 -07:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 10:45:25 -07:00
|
|
|
static unsigned long
|
|
|
|
|
msm_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
|
|
|
|
|
{
|
|
|
|
|
struct msm_drm_private *priv =
|
|
|
|
|
container_of(shrinker, struct msm_drm_private, shrinker);
|
2022-08-02 08:51:43 -07:00
|
|
|
long nr = sc->nr_to_scan;
|
2021-04-05 10:45:25 -07:00
|
|
|
unsigned long freed;
|
|
|
|
|
|
2022-08-02 08:51:43 -07:00
|
|
|
freed = drm_gem_lru_scan(&priv->lru.dontneed, nr, purge);
|
|
|
|
|
nr -= freed;
|
2021-04-05 10:45:25 -07:00
|
|
|
|
|
|
|
|
if (freed > 0)
|
2020-09-01 08:41:55 -07:00
|
|
|
trace_msm_gem_purge(freed << PAGE_SHIFT);
|
2016-05-17 16:19:32 -04:00
|
|
|
|
2022-08-02 08:51:43 -07:00
|
|
|
if (can_swap() && nr > 0) {
|
|
|
|
|
unsigned long evicted;
|
|
|
|
|
|
|
|
|
|
evicted = drm_gem_lru_scan(&priv->lru.willneed, nr, evict);
|
|
|
|
|
nr -= evicted;
|
2021-04-05 10:45:31 -07:00
|
|
|
|
|
|
|
|
if (evicted > 0)
|
|
|
|
|
trace_msm_gem_evict(evicted << PAGE_SHIFT);
|
|
|
|
|
|
|
|
|
|
freed += evicted;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 10:45:25 -07:00
|
|
|
return (freed > 0) ? freed : SHRINK_STOP;
|
2016-05-17 16:19:32 -04:00
|
|
|
}
|
|
|
|
|
|
2021-06-14 08:06:18 -07:00
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
|
unsigned long
|
|
|
|
|
msm_gem_shrinker_shrink(struct drm_device *dev, unsigned long nr_to_scan)
|
|
|
|
|
{
|
|
|
|
|
struct msm_drm_private *priv = dev->dev_private;
|
|
|
|
|
struct shrink_control sc = {
|
|
|
|
|
.nr_to_scan = nr_to_scan,
|
|
|
|
|
};
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
fs_reclaim_acquire(GFP_KERNEL);
|
|
|
|
|
ret = msm_gem_shrinker_scan(&priv->shrinker, &sc);
|
|
|
|
|
fs_reclaim_release(GFP_KERNEL);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-11-16 09:48:50 -08:00
|
|
|
/* since we don't know any better, lets bail after a few
|
|
|
|
|
* and if necessary the shrinker will be invoked again.
|
|
|
|
|
* Seems better than unmapping *everything*
|
|
|
|
|
*/
|
|
|
|
|
static const int vmap_shrink_limit = 15;
|
|
|
|
|
|
2021-04-05 10:45:25 -07:00
|
|
|
static bool
|
2022-08-02 08:51:43 -07:00
|
|
|
vmap_shrink(struct drm_gem_object *obj)
|
2016-05-27 11:16:28 -04:00
|
|
|
{
|
2022-08-02 08:51:43 -07:00
|
|
|
if (!is_vunmapable(to_msm_bo(obj)))
|
2021-04-05 10:45:25 -07:00
|
|
|
return false;
|
2016-05-27 11:16:28 -04:00
|
|
|
|
2022-08-02 08:51:43 -07:00
|
|
|
msm_gem_vunmap(obj);
|
2020-10-23 09:51:07 -07:00
|
|
|
|
2021-04-05 10:45:25 -07:00
|
|
|
return true;
|
2020-11-16 09:48:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
msm_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr)
|
|
|
|
|
{
|
|
|
|
|
struct msm_drm_private *priv =
|
|
|
|
|
container_of(nb, struct msm_drm_private, vmap_notifier);
|
2022-08-02 08:51:43 -07:00
|
|
|
struct drm_gem_lru *lrus[] = {
|
|
|
|
|
&priv->lru.dontneed,
|
|
|
|
|
&priv->lru.willneed,
|
|
|
|
|
&priv->lru.pinned,
|
2020-11-16 09:48:50 -08:00
|
|
|
NULL,
|
|
|
|
|
};
|
|
|
|
|
unsigned idx, unmapped = 0;
|
|
|
|
|
|
2022-08-02 08:51:43 -07:00
|
|
|
for (idx = 0; lrus[idx] && unmapped < vmap_shrink_limit; idx++) {
|
|
|
|
|
unmapped += drm_gem_lru_scan(lrus[idx],
|
|
|
|
|
vmap_shrink_limit - unmapped,
|
|
|
|
|
vmap_shrink);
|
2016-05-27 11:16:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*(unsigned long *)ptr += unmapped;
|
|
|
|
|
|
|
|
|
|
if (unmapped > 0)
|
2020-09-01 08:41:55 -07:00
|
|
|
trace_msm_gem_purge_vmaps(unmapped);
|
2016-05-27 11:16:28 -04:00
|
|
|
|
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-17 16:19:32 -04:00
|
|
|
/**
|
|
|
|
|
* msm_gem_shrinker_init - Initialize msm shrinker
|
2020-11-24 19:37:45 +00:00
|
|
|
* @dev: drm device
|
2016-05-17 16:19:32 -04:00
|
|
|
*
|
|
|
|
|
* This function registers and sets up the msm shrinker.
|
|
|
|
|
*/
|
|
|
|
|
void msm_gem_shrinker_init(struct drm_device *dev)
|
|
|
|
|
{
|
|
|
|
|
struct msm_drm_private *priv = dev->dev_private;
|
|
|
|
|
priv->shrinker.count_objects = msm_gem_shrinker_count;
|
|
|
|
|
priv->shrinker.scan_objects = msm_gem_shrinker_scan;
|
|
|
|
|
priv->shrinker.seeks = DEFAULT_SEEKS;
|
2022-05-31 20:22:24 -07:00
|
|
|
WARN_ON(register_shrinker(&priv->shrinker, "drm-msm_gem"));
|
2016-05-27 11:16:28 -04:00
|
|
|
|
|
|
|
|
priv->vmap_notifier.notifier_call = msm_gem_shrinker_vmap;
|
|
|
|
|
WARN_ON(register_vmap_purge_notifier(&priv->vmap_notifier));
|
2016-05-17 16:19:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* msm_gem_shrinker_cleanup - Clean up msm shrinker
|
2020-11-24 19:37:45 +00:00
|
|
|
* @dev: drm device
|
2016-05-17 16:19:32 -04:00
|
|
|
*
|
|
|
|
|
* This function unregisters the msm shrinker.
|
|
|
|
|
*/
|
|
|
|
|
void msm_gem_shrinker_cleanup(struct drm_device *dev)
|
|
|
|
|
{
|
|
|
|
|
struct msm_drm_private *priv = dev->dev_private;
|
2016-11-03 17:36:18 +05:30
|
|
|
|
|
|
|
|
if (priv->shrinker.nr_deferred) {
|
|
|
|
|
WARN_ON(unregister_vmap_purge_notifier(&priv->vmap_notifier));
|
|
|
|
|
unregister_shrinker(&priv->shrinker);
|
|
|
|
|
}
|
2016-05-17 16:19:32 -04:00
|
|
|
}
|