Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.12 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796168968; b=C7NfVYY9WJoIiK7WyfZM73bXzB/rbAWdGCHcs1WFwyJmRidPz5jRC+D29b5B3//doYgp Ff+vl5qfC3AgZbVTun8oDb/5qkFyD2Fuu63i1CjoXuZXWjkEIv1cWP1o1YBnx40C/zUdZ OYs/nmyuVaA7g/nCnGA6OuCICVtAFLtmMO8KvY+5Y2dVgpqcD7KMxYgT0q5cg2v+ZRYfL M11mVnPU0h2lrnwPTsq6ZZ46E/so2GJtbrjnmWEQt7lsT/TC36Sf9AFev1HtGpvSMdAa1 qd/+4HG8HeJH2J8X5GLsbIwrWznZrybPzDh4qQ4nCvkoRzQeTGuy9U1hEZ8TIyGyO5g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796168968; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:To:Cc:Subject: Date:Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=okXMFdvNAgWMpIaLSlJbD7UGL1NxKbkb394+YnFQcV4=; b=uzIKEwvmWeHYkL0y5hRnBXp//2jPiSqUkVi8GyxOJb4/xfhkYkr+HLczJuauRSoRcfVW LSvNsqyCzdrPyu14Yn/5R+kYX7WRpdYk62DtW2+zWnwded0cGIuO+6Kw0rFTkTVmKbzAm EfXe1vKrmZ4xl+z4NJRn+dgL6HjtKplnry5MXpwf8XtKbhx4f3HX1O/efdGhzSz9yQqOK BZyUqORh7d33EYPoYoSI7GCIKiT5LkjB53SqiUGFqQW72bIYyCfT2lCyYQm/nyliIA2/t p+AEXqsIIYou48qOQy4F6NZgchrilRVAhWoQhIZFPs45qmiSEHRrLlYo/jM3BUxxhrQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=j7GQEG3P; arc=none smtp.remote-ip=192.198.163.12 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 83E8F60A2631 for ; Tue, 1 Dec 2026 23:49:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 83E8F60A2631 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=j7GQEG3P DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796168968; x=1827704968; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=okXMFdvNAgWMpIaLSlJbD7UGL1NxKbkb394+YnFQcV4=; b=j7GQEG3PIDpn087MicISWH7B4BUHKBWa9aM28rnVSGAG4eIInxRyQzXR RQwQuJt3rhBRbV4j1rLVpJSqdPLLE3nl1v/xPYVfzbzcM7qavHGs4UjBZ s7DmKmCSgSAgC6o/BYBiUKeGM5bCXulCsgm4UCCCo+OMYuPtUezHBm+8R +Yh90PvL+X9VCEIEmHClUkO1efNGB3oOh01aSrxkPC92AX/PCVQMHPYui rO7Sz4FSvS/+7FTAnPegcgCDDp8EzKuL2myK2PNOa1OsEVx/IooHCw6gt 2cL5jXcWXqHWzD2BZkazhk/vdf7A8e+X/qD6A7GrmQK40KJ3HF0DIJ/jE g==; X-CSE-ConnectionGUID: kHiqCFfQSgWi+icBEu58CQ== X-CSE-MsgGUID: BmvMjFOyRgazSbrZMD6FUQ== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="4977472" X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="4977472" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 15:49:27 -0800 X-CSE-ConnectionGUID: OBo5c51zTuq3ky5skB8msw== X-CSE-MsgGUID: s8iyxfs1RZaUjVRDPnICkw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="13919959" Received: from gsse-cloud1.jf.intel.com ([10.54.39.91]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 15:49:26 -0800 From: Matthew Brost To: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org Cc: Carlos Santa , Ryan Neph , Natalie Vock , Arun Pravin , Matthew Auld , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , linux-kernel@vger.kernel.org, =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= Subject: [PATCH v4 18/33] drm/xe: Add a page defragmentation worker Date: Tue, 1 Dec 2026 15:49:00 -0800 Message-Id: <20261201234915.3305331-19-matthew.brost@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261201234915.3305331-1-matthew.brost@intel.com> References: <20261201234915.3305331-1-matthew.brost@intel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BOs whose backing TT pages were allocated below the device's beneficial order (tracked via the TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED tt page flag) are added to a per-device defrag list. Add a delayed worker that periodically walks this list and attempts to re-back each BO with beneficial-order pages, improving TLB efficiency for long-lived objects that were initially allocated under memory pressure. The worker is kicked when the list first transitions to non-empty and reschedules itself while work remains. On a failed pass it backs off exponentially (XE_BO_DEFRAG_INTERVAL_MS up to XE_BO_DEFRAG_INTERVAL_MAX_MS) and retries the whole list later; the interval is reset to the default on the next fresh enqueue. The worker stops scheduling once the list drains. A defrag move synchronously reallocates and re-copies a BO's backing store, which is not free. To avoid stalling concurrent active work when many BOs (or one very large BO) become eligible at once (e.g. after a burst of memory pressure, which would otherwise manifest as a large FPS drop while the worker churns through the whole list), cap the number of newly (re)allocated bytes processed per run to XE_BO_DEFRAG_SIZE_LIMIT and reschedule. Only the pages a move actually reallocates are charged - pages harvested (borrowed) from the old backing are free and do not count - so the budget tracks real defrag work and an object larger than the budget is upgraded in budget-sized slices across runs. This spreads the defrag effort over time, keeping it in the background and yielding to userspace progress. Defragmenting a BO forces a TTM move (ctx.defrag) that reallocates the backing at the beneficial order and relocates the contents on the GPU (see xe_bo_move()). The per-BO move is bounded by the run's remaining byte budget via ctx.defrag_bytes_remaining; the pool allocator reports how much it spent so the worker can charge it accurately. When beneficial-order pages cannot be obtained the new allocation is discharged during populate and the BO keeps its original backing, staying on the list for a later retry. v4: - Only enter reclaim from defrag worker - Disable worker with early cleanup handler too (Sashiko) Cc: Carlos Santa Cc: Ryan Neph Cc: Natalie Vock Cc: Arun Pravin Cc: Matthew Auld Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie Cc: Simona Vetter Cc: dri-devel@lists.freedesktop.org Cc: linux-kernel@vger.kernel.org Cc: Thomas Hellström Assisted-by: LLM Signed-off-by: Matthew Brost --- drivers/gpu/drm/xe/xe_bo.c | 327 ++++++++++++++++++++++++++- drivers/gpu/drm/xe/xe_bo.h | 4 +- drivers/gpu/drm/xe/xe_device.c | 13 +- drivers/gpu/drm/xe/xe_device_types.h | 11 + drivers/gpu/drm/xe/xe_pm.c | 8 +- 5 files changed, 355 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c index 689804916d6b..37d13c7a715c 100644 --- a/drivers/gpu/drm/xe/xe_bo.c +++ b/drivers/gpu/drm/xe/xe_bo.c @@ -34,6 +34,7 @@ #include "xe_pat.h" #include "xe_pm.h" #include "xe_preempt_fence.h" +#include "xe_printk.h" #include "xe_pxp.h" #include "xe_res_cursor.h" #include "xe_shrinker.h" @@ -45,6 +46,35 @@ #include "xe_vram_types.h" #include "xe_wedge.h" +/* + * Maximum number of bytes of newly (re)allocated backing the defrag worker will + * produce in a single run before yielding and rescheduling itself. + * + * A defrag move synchronously reallocates and re-copies a BO's backing store, + * which is not free. If a large number of BOs (or one very large BO) become + * eligible at once (e.g. after a burst of memory pressure), processing them all + * in one worker run would hold things up for a long, unbounded stretch and can + * visibly starve concurrent active work (e.g. a large FPS drop). Instead, cap + * the bytes actually upgraded per run and requeue, spreading the defrag effort + * out over time so it stays in the background and yields to userspace progress. + * Only pages a move truly reallocates are charged; pages harvested from the old + * backing are free, so an object larger than the budget is upgraded in + * budget-sized slices across runs. + */ +#define XE_BO_DEFRAG_SIZE_LIMIT SZ_32M + +/* Default delay before (re)running the defrag worker, in milliseconds. */ +#define XE_BO_DEFRAG_INTERVAL_MS 25 + +/* + * Upper bound for the (exponentially backed off) defrag worker interval, in + * milliseconds, so repeated failures don't push the retry arbitrarily far out. + */ +#define XE_BO_DEFRAG_INTERVAL_MAX_MS 15000 /* 15 seconds */ + +static void xe_bo_defrag_worker(struct work_struct *w); +static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place); + const char *const xe_mem_type_to_name[TTM_NUM_MEM_TYPES] = { [XE_PL_SYSTEM] = "system", [XE_PL_TT] = "gtt", @@ -722,9 +752,12 @@ static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo, return tt; } +static bool xe_bo_defrag_supported(struct xe_device *xe); + static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt, struct ttm_operation_ctx *ctx) { + struct xe_device *xe = ttm_to_xe_device(ttm_dev); struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); int err; @@ -736,6 +769,10 @@ static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt, !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE)) return 0; + /* Only defrag will enter reclaim at higher orders */ + ctx->beneficial_reclaim_backoff = !ctx->defrag && + xe_bo_defrag_supported(xe); + if (ttm_tt_is_backed_up(tt) && !xe_tt->purgeable) { err = ttm_tt_restore(ttm_dev, tt, ctx); } else { @@ -1201,17 +1238,85 @@ int xe_ttm_bo_purge(struct ttm_buffer_object *ttm_bo, struct ttm_operation_ctx * return 0; } +static void xe_bo_defrag_fini_early(void *arg) +{ + struct xe_device *xe = arg; + + disable_delayed_work_sync(&xe->mem.defrag.worker); +} + +static void xe_bo_defrag_fini(void *arg) +{ + struct xe_device *xe = arg; + + disable_delayed_work_sync(&xe->mem.defrag.worker); +} + /** * xe_bo_defrag_init_early() - Initialize the device defrag BO tracking * @xe: The xe device * - * Initialize the list, lock and count used to track BOs whose backing TT - * pages were allocated at a sub-optimal order. + * Initialize the list, lock, count and delayed worker used to track and + * defragment BOs whose backing TT pages were allocated at a sub-optimal order. + * + * Return: 0 on success, negative error code on failure. */ -void xe_bo_defrag_init_early(struct xe_device *xe) +int xe_bo_defrag_init_early(struct xe_device *xe) { spin_lock_init(&xe->mem.defrag.lock); INIT_LIST_HEAD(&xe->mem.defrag.list); + xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + INIT_DELAYED_WORK(&xe->mem.defrag.worker, xe_bo_defrag_worker); + + return devm_add_action_or_reset(xe->drm.dev, xe_bo_defrag_fini_early, xe); +} + +/** + * xe_bo_defrag_init() - Register defrag worker teardown + * @xe: The xe device + * + * Register the devm action that disables and drains the defrag worker on + * device teardown. This must be called after xe_migrate_init() so that, with + * devm's reverse-order cleanup, the worker is stopped before the migrate + * contexts it relies on (via xe_bo_defrag_one() -> ttm_bo_validate() -> + * xe_bo_move() -> xe_migrate_copy_defrag()) are torn down. + * + * Return: 0 on success, negative error code on failure. + */ +int xe_bo_defrag_init(struct xe_device *xe) +{ + return devm_add_action_or_reset(xe->drm.dev, xe_bo_defrag_fini, xe); +} + +static void xe_bo_defrag_schedule(struct xe_device *xe) +{ + schedule_delayed_work(&xe->mem.defrag.worker, + msecs_to_jiffies(xe->mem.defrag.interval_ms)); +} + +/** + * xe_bo_defrag_resume() - Re-arm the defragmentation worker after PM resume + * @xe: xe device instance + * + * The defrag worker bails without rescheduling when it cannot take a runtime + * PM reference (the device is runtime suspended/suspending). Call this from the + * PM resume paths so the worker is re-armed if BOs are still pending, rather + * than polling a wakeup timer while suspended. If the tracking list is empty + * this is a no-op; the next xe_bo_defrag_add() will kick the worker. + */ +void xe_bo_defrag_resume(struct xe_device *xe) +{ + bool kick = false; + + scoped_guard(spinlock, &xe->mem.defrag.lock) { + if (!list_empty(&xe->mem.defrag.list)) { + xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + kick = true; + } + } + + if (kick) + xe_bo_defrag_schedule(xe); } /* @@ -1248,14 +1353,23 @@ static bool xe_bo_needs_defrag(struct xe_bo *bo) static void xe_bo_defrag_add(struct xe_bo *bo) { struct xe_device *xe = xe_bo_device(bo); + bool kick = false; xe_bo_assert_held(bo); xe_assert(xe, xe_bo_needs_defrag(bo)); scoped_guard(spinlock, &xe->mem.defrag.lock) { - if (list_empty(&bo->defrag_link)) + if (list_empty(&bo->defrag_link)) { + /* Kick the worker when the list transitions to non-empty. */ + kick = list_empty(&xe->mem.defrag.list); + if (kick) + xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; list_add_tail(&bo->defrag_link, &xe->mem.defrag.list); + } } + + if (kick) + xe_bo_defrag_schedule(xe); } static void __xe_bo_defrag_remove(struct xe_bo *bo) @@ -1301,6 +1415,211 @@ static void xe_bo_defrag_update(struct xe_bo *bo) xe_bo_defrag_remove(bo); } +/* + * Attempt to defragment a single BO by forcing a move that reallocates its + * backing at the device's beneficial order. @budget bounds how many bytes of + * new pages the move may (re)allocate; the remainder of the object is completed + * by harvesting the old backing, and the BO stays flagged for a later pass. On + * return *@consumed holds the bytes actually (re)allocated (0 on failure or + * when nothing needed reallocating). *@needs_more is set to true when the move + * succeeded but the BO still has sub-beneficial backing (partial upgrade) and + * should be revisited; it is computed under the BO lock so the caller does not + * need to re-inspect bo->ttm.ttm after the lock is dropped. Returns 0 if the BO + * no longer needs to be tracked (either defragmented or no longer eligible), or + * a negative error code if the attempt should be retried later. + */ +static int xe_bo_defrag_one(struct xe_device *xe, struct xe_bo *bo, + u64 budget, u64 *consumed, bool *needs_more) +{ + struct ttm_operation_ctx ctx = { + .gfp_retry_mayfail = true, + .defrag = true, + .defrag_bytes_remaining = budget, + }; + struct ttm_buffer_object *ttm_bo = &bo->ttm; + struct ttm_placement placement; + struct ttm_place place; + int ret = 0; + + *consumed = 0; + *needs_more = false; + + xe_bo_lock(bo, false); + + /* Re-check eligibility under the BO lock. */ + if (!xe_bo_needs_defrag(bo)) { + xe_bo_defrag_remove(bo); + goto unlock; + } + + xe_place_from_ttm_type(ttm_bo->resource->mem_type, &place); + place.flags |= TTM_PL_FLAG_TEMPORARY; + placement.num_placement = 1; + placement.placement = &place; + + /* + * On success the move reallocates the backing at beneficial order and + * drops the BO from the defrag list. On failure the BO keeps its + * original backing and stays on the list for a later retry. + */ + ret = ttm_bo_validate(ttm_bo, &placement, &ctx); + + /* + * The pool allocator decremented defrag_bytes_remaining as it + * (re)allocated pages, so the unused budget tells us how much was spent. + * Only meaningful when the move succeeded. A successful move may spend + * nothing - e.g. when the object was already fully upgraded and every + * chunk is harvested (borrowed) from the old backing for free - so a + * zero spend is legitimate and must be charged as zero, not the whole + * budget. + */ + if (!ret) { + s64 spent = (s64)budget - ctx.defrag_bytes_remaining; + + *consumed = spent > 0 ? spent : 0; + } + + xe_dbg(xe, "Defrag attempt on BO size=%zu: ret=%pe consumed=%llu\n", + xe_bo_size(bo), ERR_PTR(ret), *consumed); + + if (!ret && ttm_tt_is_beneficial_order_failed(bo->ttm.ttm)) + *needs_more = true; + +unlock: + xe_bo_unlock(bo); + return ret; +} + +static void xe_bo_defrag_worker(struct work_struct *w) +{ + struct delayed_work *dwork = to_delayed_work(w); + struct xe_device *xe = + container_of(dwork, struct xe_device, mem.defrag.worker); + u64 defrag_bytes = 0; + bool requeue = false; + int idx; + + if (!drm_dev_enter(&xe->drm, &idx)) + return; + + if (!xe_pm_runtime_get_if_in_use(xe)) { + /* + * The device is (runtime) suspending/suspended. Do not spin a + * wakeup timer here: the worker is re-armed from the PM resume + * path (xe_bo_defrag_resume()) once the device is active again, + * so tracked BOs are picked up without polling while suspended. + */ + drm_dev_exit(idx); + return; + } + + /* + * Process at most XE_BO_DEFRAG_SIZE_LIMIT bytes of newly (re)allocated + * backing per run rather than draining the whole list in one go. Only + * the pages a move actually reallocates are charged - pages harvested + * (borrowed) from the old backing are free and do not count - so the + * budget tracks real defrag work. A single object larger than the + * budget is upgraded in slices across successive runs. Each defrag is + * synchronous and relatively expensive, so bounding the work per run + * keeps the worker from monopolising resources and lets concurrent + * active work make progress; any remaining work is handled by a + * follow-up run scheduled below. + */ + while (defrag_bytes < XE_BO_DEFRAG_SIZE_LIMIT) { + struct xe_bo *first, *bo; + u64 consumed = 0; + bool needs_more = false; + int ret; + + scoped_guard(spinlock, &xe->mem.defrag.lock) { + bool again; + + /* Progress, reset interval */ + if (defrag_bytes) + xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + + do { + first = list_first_entry_or_null(&xe->mem.defrag.list, + struct xe_bo, + defrag_link); + bo = first ? xe_bo_get_unless_zero(first) : NULL; + again = first && !bo; + if (again) + list_del_init(&first->defrag_link); + } while (again); + } + + if (!bo) + break; + + /* + * Bound the per-BO move to the run's remaining byte budget. A + * large object is upgraded in budget-sized slices across runs; + * the slices already upgraded come back as free already- + * beneficial harvests, so each run only spends budget on new + * forward progress. + */ + ret = xe_bo_defrag_one(xe, bo, XE_BO_DEFRAG_SIZE_LIMIT - defrag_bytes, + &consumed, &needs_more); + defrag_bytes += consumed; + + if (ret || needs_more) { + scoped_guard(spinlock, &xe->mem.defrag.lock) { + if (ret) + /* + * Abort the pass and retry the whole + * list later, backing off exponentially + * on every failure. + */ + xe->mem.defrag.interval_ms = + min(xe->mem.defrag.interval_ms * 2, + (unsigned int)XE_BO_DEFRAG_INTERVAL_MAX_MS); + else + /* Progress, reset interval */ + xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + } + + xe_bo_put(bo); + requeue = true; + break; + } + + xe_bo_put(bo); + } + + /* + * Decide whether to reschedule: + * + * - The loop hit its per-run limit (defrag_bytes >= + * XE_BO_DEFRAG_SIZE_LIMIT): we stopped early to spread the work out, + * so requeue at the default interval (reset here since this run made + * progress) whenever the list still has entries. + * + * - The loop aborted on a failed defrag (requeue): retry later at the + * backed-off interval computed above. + * + * - The loop drained the list (broke on the empty list): nothing left to + * do, so stop scheduling. A fresh enqueue will kick the worker again. + */ + if (defrag_bytes >= XE_BO_DEFRAG_SIZE_LIMIT) { + struct xe_bo *bo; + + scoped_guard(spinlock, &xe->mem.defrag.lock) { + bo = list_first_entry_or_null(&xe->mem.defrag.list, + struct xe_bo, defrag_link); + xe->mem.defrag.interval_ms = XE_BO_DEFRAG_INTERVAL_MS; + } + + if (bo) + xe_bo_defrag_schedule(xe); + } else if (requeue) { + xe_bo_defrag_schedule(xe); + } + + xe_pm_runtime_put(xe); + drm_dev_exit(idx); +} + static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict, struct ttm_operation_ctx *ctx, struct ttm_resource *new_mem, diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h index f6b8b71228c6..2bb291fd06c5 100644 --- a/drivers/gpu/drm/xe/xe_bo.h +++ b/drivers/gpu/drm/xe/xe_bo.h @@ -232,7 +232,9 @@ void xe_bo_unpin_external(struct xe_bo *bo); void xe_bo_unpin(struct xe_bo *bo); void xe_bo_pin_account(struct xe_bo *bo); void xe_bo_unpin_account(struct xe_bo *bo); -void xe_bo_defrag_init_early(struct xe_device *xe); +int xe_bo_defrag_init_early(struct xe_device *xe); +int xe_bo_defrag_init(struct xe_device *xe); +void xe_bo_defrag_resume(struct xe_device *xe); int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict, struct drm_exec *exec); diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c index 67d648833030..bc89f0becade 100644 --- a/drivers/gpu/drm/xe/xe_device.c +++ b/drivers/gpu/drm/xe/xe_device.c @@ -549,7 +549,9 @@ int xe_device_init_early(struct xe_device *xe) if (err) return err; - xe_bo_defrag_init_early(xe); + err = xe_bo_defrag_init_early(xe); + if (err) + return err; xe->preempt_fence_wq = alloc_ordered_workqueue("xe-preempt-fence-wq", WQ_MEM_RECLAIM); @@ -1062,6 +1064,15 @@ int xe_device_probe(struct xe_device *xe) if (err) return err; + /* + * Register the defrag worker teardown now that the migrate contexts it + * depends on are initialized, so devm's reverse-order cleanup stops the + * worker before those contexts are torn down. + */ + err = xe_bo_defrag_init(xe); + if (err) + return err; + err = xe_pagefault_init(xe); if (err) return err; diff --git a/drivers/gpu/drm/xe/xe_device_types.h b/drivers/gpu/drm/xe/xe_device_types.h index 0d09cb5bd2d5..f9fd40197a96 100644 --- a/drivers/gpu/drm/xe/xe_device_types.h +++ b/drivers/gpu/drm/xe/xe_device_types.h @@ -337,6 +337,17 @@ struct xe_device { spinlock_t lock; /** @mem.defrag.list: List of struct xe_bo defrag_link. */ struct list_head list; + /** + * @mem.defrag.worker: Delayed worker that walks + * @mem.defrag.list trying to reallocate BO backing + * store at the device's beneficial order. + */ + struct delayed_work worker; + /** + * @mem.defrag.interval_ms: Reschedule interval for + * @mem.defrag.worker, in milliseconds. + */ + unsigned int interval_ms; } defrag; } mem; diff --git a/drivers/gpu/drm/xe/xe_pm.c b/drivers/gpu/drm/xe/xe_pm.c index f73f68bd449b..f76ab97ca49e 100644 --- a/drivers/gpu/drm/xe/xe_pm.c +++ b/drivers/gpu/drm/xe/xe_pm.c @@ -218,10 +218,12 @@ int xe_pm_resume(struct xe_device *xe) trace_xe_pm_resume(xe, __builtin_return_address(0)); err = xe_device_resume(xe, false); - if (err) + if (err) { drm_dbg(&xe->drm, "Device resume failed %d\n", err); - else + } else { + xe_bo_defrag_resume(xe); drm_dbg(&xe->drm, "Device resumed\n"); + } xe_pm_block_end_signalling(); return err; @@ -689,6 +691,8 @@ int xe_pm_runtime_resume(struct xe_device *xe) if (xe->d3cold.allowed) xe_late_bind_pm_resume(&xe->late_bind); + xe_bo_defrag_resume(xe); + out: xe_rpm_lockmap_release(xe); xe_pm_write_callback_task(xe, NULL); -- 2.34.1