Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37A87606A681 for ; Wed, 2 Dec 2026 00:23:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=XzM0z1xL Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 954CA10E4B3; Wed, 2 Dec 2026 00:23:13 +0000 (UTC) Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9EF9D10E4B3; Wed, 2 Dec 2026 00:23:07 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id BB22461154; Wed, 2 Dec 2026 00:23:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B1A41F008A0; Wed, 2 Dec 2026 00:23:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796170986; bh=fODLkiEddhcY/HikHKiwRdovp/KHqr/aVWf+uSuhkZo=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=XzM0z1xLl/GInSUUXR9LFWbVcUJt1EhWGVvB+0qBWJh7HNKuzuoVX2sBXqZ8UKefK G+QDO7eca9/pOoP5r95kR/Fg6KZbRHpQX/xoiICY6jPWKhHOdBtJM6JT6aF73JLDG3 gfWdkXz+9Wq17HtcVxN5KRyiogZC56JVObXo6jeSI+/fe3KM+nn/nSwilKCHti4Q7n mVXJMzyEu+WF8mV7pNS1h10Hg/Aysb+GQ987RpXpLYvovho/lWlQQ/j2CPVDIK6Cep xFLBWvpfRY789XSkuGxoByuCjzNFRq9JfdO65WK2nIMp4njJ8NaV+1tWpCbBTrUads 0tjEbB3m+prwA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4 18/33] drm/xe: Add a page defragmentation worker To: "Matthew Brost" , intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org In-Reply-To: <20261201234915.3305331-19-matthew.brost@intel.com> References: <20261201234915.3305331-1-matthew.brost@intel.com> <20261201234915.3305331-19-matthew.brost@intel.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 00:23:05 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sashiko-reviews@lists.linux.dev Sender: "dri-devel" X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] drm/xe: missing suspend and FLR synchronization in xe_bo_defrag_worker() - [High] drm/xe: TTM_PL_FLAG_TEMPORARY left on resource in xe_bo_defrag_one() - [High] drm/xe: defrag worker stalls on active device in xe_bo_defrag_worker() - [Medium] drm/xe: BO lost from defrag list on move failure in xe_bo_move() - [Medium] drm/xe: defrag worker not re-armed on xe_pm_runtime_suspend() failure - [Low] drm/xe: unlocked interval_ms read and stale timer in xe_bo_defrag_schedule() -- commit 1c62d5735db470f5d28289297de300836bbb6ef4 Author: Matthew Brost drm/xe: Add a page defragmentation worker This commit adds a per-device delayed worker that walks the list of buffer objects backed by sub-optimal TT page orders and forces a TTM move to reallocate their backing pages at the device's beneficial order, bounded by a per-run byte budget. > diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c > index 689804916d6ba..37d13c7a715c7 100644 > --- a/drivers/gpu/drm/xe/xe_bo.c > +++ b/drivers/gpu/drm/xe/xe_bo.c [ ... ] > @@ -1201,17 +1238,85 @@ int xe_ttm_bo_purge(struct ttm_buffer_object *ttm= _bo, struct ttm_operation_ctx * > +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)); > +} [Severity: Low] Should xe_bo_defrag_schedule() read xe->mem.defrag.interval_ms under xe->mem.defrag.lock (or pass the interval in) and use mod_delayed_work() instead of schedule_delayed_work()? All writes to interval_ms hold xe->mem.defrag.lock, whereas xe_bo_defrag_schedule() reads it after dropping the lock. In addition, if a failed pass backs off interval_ms (up to 15 seconds) and schedules the delayed work, and all BOs are then removed before a new BO is added via xe_bo_defrag_add() (or xe_bo_defrag_resume() runs), interval_ms is reset to 25 ms under the lock, but schedule_delayed_work() is a no-op while the backed-off timer is still pending. [ ... ] > @@ -1301,6 +1415,211 @@ static void xe_bo_defrag_update(struct xe_bo *bo) > +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 =3D { > + .gfp_retry_mayfail =3D true, > + .defrag =3D true, > + .defrag_bytes_remaining =3D budget, > + }; > + struct ttm_buffer_object *ttm_bo =3D &bo->ttm; > + struct ttm_placement placement; > + struct ttm_place place; > + int ret =3D 0; > + > + *consumed =3D 0; > + *needs_more =3D 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 |=3D TTM_PL_FLAG_TEMPORARY; > + placement.num_placement =3D 1; > + placement.placement =3D &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 =3D ttm_bo_validate(ttm_bo, &placement, &ctx); [Severity: High] Does passing TTM_PL_FLAG_TEMPORARY in place.flags leave TTM_PL_FLAG_TEMPORARY permanently set on ttm_bo->resource->placement after ttm_bo_validate() succeeds? When xe_ttm_sys_mgr_new() allocates the replacement XE_PL_TT resource, ttm_resource_init() copies place->flags into res->placement. Because the defrag move calls xe_bo_move_notify() -> xe_bo_trigger_rebind(), the BO is re-validated on the next submission or in preempt_rebind_work_func(). ttm_resource_compatible() unconditionally returns false whenever res->placement & TTM_PL_FLAG_TEMPORARY is set, forcing ttm_bo_validate() to reject the freshly defragmented resource, tear down CPU mappings a second time, and temporarily double-charge the BO's size against xe->mem.sys_mgr while the old resource is still allocated (which can fail with -ENOSPC/-ENOMEM when usage exceeds man->size and kill an LR VM in preempt_rebind_work_func()). [Severity: Medium] Can a BO be lost from xe->mem.defrag.list if ttm_bo_populate() succeeds at the beneficial order during a defrag move, but xe_bo_move() subsequently fails? In ttm_bo_handle_move_mem(), TTM stashes the old sub-optimal ttm_tt in bo->defrag_old_tt, populates the new bo->ttm (with TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED clear), and calls xe_bo_move(). If xe_bo_move() fails (for example in xe_tt_map_sg(), xe_bo_move_notify(), or xe_migrate_copy_defrag()), its error exit runs xe_bo_defrag_update(bo) while bo->ttm.ttm still points to the new ttm_tt: drivers/gpu/drm/ttm/ttm_bo.c:ttm_bo_handle_move_mem() { ... ret =3D bdev->funcs->move(bo, evict, ctx, mem, hop); if (ret) { if (ret =3D=3D -EMULTIHOP) return ret; goto out_err; } ... out_err: if (bo->defrag_old_tt) { ttm_tt_defrag_disown_borrowed(bo->ttm, bo->defrag_old_tt); ttm_bo_tt_destroy(bo); bo->ttm =3D bo->defrag_old_tt; bo->defrag_old_tt =3D NULL; } } Because xe_bo_defrag_update(bo) sees the temporary new ttm_tt before out_err restores bo->ttm =3D bo->defrag_old_tt, it removes the BO from xe->mem.defrag.list, and xe_bo_defrag_one() does not re-run xe_bo_defrag_update(bo) after ttm_bo_validate() returns the error. > + > + /* > + * The pool allocator decremented defrag_bytes_remaining as it > + * (re)allocated pages, so the unused budget tells us how much was spen= t. > + * 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 =3D (s64)budget - ctx.defrag_bytes_remaining; > + > + *consumed =3D spent > 0 ? spent : 0; > + } > + > + xe_dbg(xe, "Defrag attempt on BO size=3D%zu: ret=3D%pe consumed=3D%llu\= n", > + xe_bo_size(bo), ERR_PTR(ret), *consumed); > + > + if (!ret && ttm_tt_is_beneficial_order_failed(bo->ttm.ttm)) > + *needs_more =3D true; > + > +unlock: > + xe_bo_unlock(bo); > + return ret; > +} > + > +static void xe_bo_defrag_worker(struct work_struct *w) > +{ > + struct delayed_work *dwork =3D to_delayed_work(w); > + struct xe_device *xe =3D > + container_of(dwork, struct xe_device, mem.defrag.worker); > + u64 defrag_bytes =3D 0; > + bool requeue =3D 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; > + } [Severity: High] Can xe_bo_defrag_worker() run concurrently with or after xe_device_suspend() during system suspend, hibernation, or PCI FLR? xe_bo_defrag_schedule() queues xe->mem.defrag.worker on system_percpu_wq (not WQ_FREEZABLE), and neither xe_pm_notifier_callback(), xe_pm_suspend(), nor xe_device_suspend() cancels or disables the worker, nor does xe_bo_defrag_worker() check try_wait_for_completion(&xe->pm_block). During system suspend/hibernation, device_prepare() and xe_pm_notifier_callback(PM_SUSPEND_PREPARE) hold runtime PM usage references while runtime_status remains RPM_ACTIVE until device_suspend_late(), and evicting user VRAM BOs to XE_PL_TT in xe_bo_evict_all_user() / xe_bo_evict_all() can call xe_bo_defrag_add() and arm the 25 ms timer: CPU0 (system suspend / FLR) CPU1 (system_percpu_wq) xe_pm_notifier_callback() xe_pm_runtime_get(xe) xe_bo_evict_all_user(xe) ... xe_bo_defrag_add() xe_bo_defrag_schedule() xe_pm_suspend() xe_device_suspend() xe_bo_evict_all(xe) xe_gt_suspend(gt) xe_irq_suspend(xe) xe_bo_defrag_worker() xe_pm_runtime_get_if_in_use(xe) -> true xe_bo_defrag_one() ... xe_migrate_copy_defrag() <-- submits GPU job while GT is suspended / kernel BOs are evicted In addition, xe_pci_reset_done() calls xe_device_resume(xe, true) directly rather than xe_pm_resume(), so xe_bo_defrag_resume(xe) is not called after an FLR. [Severity: High] Does using xe_pm_runtime_get_if_in_use(xe) here stall background defragmentation while the device is active in its autosuspend window? Normal DRM ioctls hold a runtime PM usage reference only for the duration of the syscall, leaving dev->power.runtime_status =3D=3D RPM_ACTIVE with dev->power.usage_count =3D=3D 0 during the 1000 ms autosuspend delay. When xe_bo_defrag_worker() fires 25 ms after an ioctl adds a BO, xe_pm_runtime_get_if_in_use(xe) returns false whenever usage_count is 0 (unlike xe_pm_runtime_get_if_active(xe)), and the worker exits without rescheduling. Because the device is already RPM_ACTIVE, subsequent GPU activity before autosuspend does not trigger xe_pm_runtime_resume() -> xe_bo_defrag_resume(), and because xe->mem.defrag.list is already non-empty, subsequent calls to xe_bo_defrag_add() see list_empty(&xe->mem.defrag.list) =3D=3D false and never re-kick the worker. > diff --git a/drivers/gpu/drm/xe/xe_pm.c b/drivers/gpu/drm/xe/xe_pm.c > index f73f68bd449b8..f76ab97ca49e2 100644 > --- a/drivers/gpu/drm/xe/xe_pm.c > +++ b/drivers/gpu/drm/xe/xe_pm.c [ ... ] > @@ -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); > =20 > + xe_bo_defrag_resume(xe); > + > out: > xe_rpm_lockmap_release(xe); > xe_pm_write_callback_task(xe, NULL); [Severity: Medium] If xe_bo_defrag_worker() fires while runtime_status is RPM_SUSPENDING and exits early, what re-arms it if xe_pm_runtime_suspend() subsequently fails? When xe_pm_runtime_suspend() fails and unwinds through out_resume or out, rpm_suspend() reverts dev->power.runtime_status back to RPM_ACTIVE without calling xe_pm_runtime_resume(), leaving a non-empty xe->mem.defrag.list without a scheduled worker until the next full runtime suspend/resume cycle. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201234915.3305= 331-1-matthew.brost@intel.com?part=3D18