1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
// SPDX-License-Identifier: MIT
/*
* Copyright © 2024 Intel Corporation
*/
#include <linux/shrinker.h>
#include <drm/drm_managed.h>
#include <drm/ttm/ttm_backup.h>
#include <drm/ttm/ttm_bo.h>
#include <drm/ttm/ttm_tt.h>
#include "xe_bo.h"
#include "xe_pm.h"
#include "xe_shrinker.h"
/**
* struct xe_shrinker - per-device shrinker
* @xe: Back pointer to the device.
* @lock: Lock protecting accounting.
* @shrinkable_pages: Number of pages that are currently shrinkable.
* @purgeable_pages: Number of pages that are currently purgeable.
* @shrink: Pointer to the mm shrinker.
* @pm_worker: Worker to wake up the device if required.
*/
struct xe_shrinker {
struct xe_device *xe;
rwlock_t lock;
long shrinkable_pages;
long purgeable_pages;
struct shrinker *shrink;
struct work_struct pm_worker;
};
static struct xe_shrinker *to_xe_shrinker(struct shrinker *shrink)
{
return shrink->private_data;
}
/**
* xe_shrinker_mod_pages() - Modify shrinker page accounting
* @shrinker: Pointer to the struct xe_shrinker.
* @shrinkable: Shrinkable pages delta. May be negative.
* @purgeable: Purgeable page delta. May be negative.
*
* Modifies the shrinkable and purgeable pages accounting.
*/
void
xe_shrinker_mod_pages(struct xe_shrinker *shrinker, long shrinkable, long purgeable)
{
write_lock(&shrinker->lock);
shrinker->shrinkable_pages += shrinkable;
shrinker->purgeable_pages += purgeable;
write_unlock(&shrinker->lock);
}
static s64 __xe_shrinker_walk(struct xe_device *xe,
struct ttm_operation_ctx *ctx,
const struct xe_bo_shrink_flags flags,
unsigned long to_scan, unsigned long *scanned)
{
unsigned int mem_type;
s64 freed = 0, lret;
for (mem_type = XE_PL_SYSTEM; mem_type <= XE_PL_TT; ++mem_type) {
struct ttm_resource_manager *man = ttm_manager_type(&xe->ttm, mem_type);
struct ttm_bo_lru_cursor curs;
struct ttm_buffer_object *ttm_bo;
struct ttm_lru_walk_arg arg = {
.ctx = ctx,
.trylock_only = true,
};
if (!man || !man->use_tt)
continue;
ttm_bo_lru_for_each_reserved_guarded(&curs, man, &arg, ttm_bo) {
if (!ttm_bo_shrink_suitable(ttm_bo, ctx))
continue;
lret = xe_bo_shrink(ctx, ttm_bo, flags, scanned);
if (lret < 0)
return lret;
freed += lret;
if (*scanned >= to_scan)
break;
}
/* Trylocks should never error, just fail. */
xe_assert(xe, !IS_ERR(ttm_bo));
}
return freed;
}
/*
* Try shrinking idle objects without writeback first, then if not sufficient,
* try also non-idle objects and finally if that's not sufficient either,
* add writeback. This avoids stalls and explicit writebacks with light or
* moderate memory pressure.
*/
static s64 xe_shrinker_walk(struct xe_device *xe,
struct ttm_operation_ctx *ctx,
const struct xe_bo_shrink_flags flags,
unsigned long to_scan, unsigned long *scanned)
{
bool no_wait_gpu = true;
struct xe_bo_shrink_flags save_flags = flags;
s64 lret, freed;
swap(no_wait_gpu, ctx->no_wait_gpu);
save_flags.writeback = false;
lret = __xe_shrinker_walk(xe, ctx, save_flags, to_scan, scanned);
swap(no_wait_gpu, ctx->no_wait_gpu);
if (lret < 0 || *scanned >= to_scan)
return lret;
freed = lret;
if (!ctx->no_wait_gpu) {
lret = __xe_shrinker_walk(xe, ctx, save_flags, to_scan, scanned);
if (lret < 0)
return lret;
freed += lret;
if (*scanned >= to_scan)
return freed;
}
if (flags.writeback) {
lret = __xe_shrinker_walk(xe, ctx, flags, to_scan, scanned);
if (lret < 0)
return lret;
freed += lret;
}
return freed;
}
static unsigned long
xe_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
{
struct xe_shrinker *shrinker = to_xe_shrinker(shrink);
unsigned long num_pages;
bool can_backup = !!(sc->gfp_mask & __GFP_FS);
num_pages = ttm_backup_bytes_avail() >> PAGE_SHIFT;
read_lock(&shrinker->lock);
if (can_backup)
num_pages = min_t(unsigned long, num_pages, shrinker->shrinkable_pages);
else
num_pages = 0;
num_pages += shrinker->purgeable_pages;
read_unlock(&shrinker->lock);
return num_pages ? num_pages : SHRINK_EMPTY;
}
/*
* Check if we need runtime pm, and if so try to grab a reference if
* already active. If grabbing a reference fails, queue a worker that
* does it for us outside of reclaim, but don't wait for it to complete.
* If bo shrinking needs an rpm reference and we don't have it (yet),
* that bo will be skipped anyway.
*/
static bool xe_shrinker_runtime_pm_get(struct xe_shrinker *shrinker, bool force,
unsigned long nr_to_scan, bool can_backup)
{
struct xe_device *xe = shrinker->xe;
if (IS_DGFX(xe) || !xe_device_has_flat_ccs(xe) ||
!ttm_backup_bytes_avail())
return false;
if (!force) {
read_lock(&shrinker->lock);
force = (nr_to_scan > shrinker->purgeable_pages && can_backup);
read_unlock(&shrinker->lock);
if (!force)
return false;
}
if (!xe_pm_runtime_get_if_active(xe)) {
if (xe_rpm_reclaim_safe(xe) && !ttm_bo_shrink_avoid_wait()) {
xe_pm_runtime_get(xe);
return true;
}
queue_work(xe->unordered_wq, &shrinker->pm_worker);
return false;
}
return true;
}
static void xe_shrinker_runtime_pm_put(struct xe_shrinker *shrinker, bool runtime_pm)
{
if (runtime_pm)
xe_pm_runtime_put(shrinker->xe);
}
static unsigned long xe_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
{
struct xe_shrinker *shrinker = to_xe_shrinker(shrink);
struct ttm_operation_ctx ctx = {
.interruptible = false,
.no_wait_gpu = ttm_bo_shrink_avoid_wait(),
};
unsigned long nr_to_scan, nr_scanned = 0, freed = 0;
struct xe_bo_shrink_flags shrink_flags = {
.purge = true,
/* Don't request writeback without __GFP_IO. */
.writeback = !ctx.no_wait_gpu && (sc->gfp_mask & __GFP_IO),
};
bool runtime_pm;
bool purgeable;
bool can_backup = !!(sc->gfp_mask & __GFP_FS);
s64 lret;
nr_to_scan = sc->nr_to_scan;
read_lock(&shrinker->lock);
purgeable = !!shrinker->purgeable_pages;
read_unlock(&shrinker->lock);
/* Might need runtime PM. Try to wake early if it looks like it. */
runtime_pm = xe_shrinker_runtime_pm_get(shrinker, false, nr_to_scan, can_backup);
if (purgeable && nr_scanned < nr_to_scan) {
lret = xe_shrinker_walk(shrinker->xe, &ctx, shrink_flags,
nr_to_scan, &nr_scanned);
if (lret >= 0)
freed += lret;
}
sc->nr_scanned = nr_scanned;
if (nr_scanned >= nr_to_scan || !can_backup)
goto out;
/* If we didn't wake before, try to do it now if needed. */
if (!runtime_pm)
runtime_pm = xe_shrinker_runtime_pm_get(shrinker, true, 0, can_backup);
shrink_flags.purge = false;
lret = xe_shrinker_walk(shrinker->xe, &ctx, shrink_flags,
nr_to_scan, &nr_scanned);
if (lret >= 0)
freed += lret;
sc->nr_scanned = nr_scanned;
out:
xe_shrinker_runtime_pm_put(shrinker, runtime_pm);
return nr_scanned ? freed : SHRINK_STOP;
}
/* Wake up the device for shrinking. */
static void xe_shrinker_pm(struct work_struct *work)
{
struct xe_shrinker *shrinker =
container_of(work, typeof(*shrinker), pm_worker);
xe_pm_runtime_get(shrinker->xe);
xe_pm_runtime_put(shrinker->xe);
}
static void xe_shrinker_fini(struct drm_device *drm, void *arg)
{
struct xe_shrinker *shrinker = arg;
xe_assert(shrinker->xe, !shrinker->shrinkable_pages);
xe_assert(shrinker->xe, !shrinker->purgeable_pages);
shrinker_free(shrinker->shrink);
flush_work(&shrinker->pm_worker);
kfree(shrinker);
}
/**
* xe_shrinker_create() - Create an xe per-device shrinker
* @xe: Pointer to the xe device.
*
* Return: %0 on success. Negative error code on failure.
*/
int xe_shrinker_create(struct xe_device *xe)
{
struct xe_shrinker *shrinker = kzalloc(sizeof(*shrinker), GFP_KERNEL);
if (!shrinker)
return -ENOMEM;
shrinker->shrink = shrinker_alloc(0, "drm-xe_gem:%s", xe->drm.unique);
if (!shrinker->shrink) {
kfree(shrinker);
return -ENOMEM;
}
INIT_WORK(&shrinker->pm_worker, xe_shrinker_pm);
shrinker->xe = xe;
rwlock_init(&shrinker->lock);
shrinker->shrink->count_objects = xe_shrinker_count;
shrinker->shrink->scan_objects = xe_shrinker_scan;
shrinker->shrink->private_data = shrinker;
shrinker_register(shrinker->shrink);
xe->mem.shrinker = shrinker;
return drmm_add_action_or_reset(&xe->drm, xe_shrinker_fini, shrinker);
}
|