From mboxrd@z Thu Jan 1 00:00:00 1970 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 693E4606A32D for ; Thu, 3 Dec 2026 20:24:36 +0000 (UTC) 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=YztabPRP Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 07201112BCB; Thu, 3 Dec 2026 20:24:31 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6839B112BBC; Thu, 3 Dec 2026 20:24:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796329461; x=1827865461; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UUR3uzdo/9hEdYyqqNm0YeegMxAtX3TobxxM4Ugc/TE=; b=YztabPRPJUzjKJjYBeB1jxSRmBfh/L2ntN33QEJlQ8tzd3tZrUd0SFuD 7NX1CYl7tg153bDWgsPU2HkY9z+FzMYKLO9QpllOCoGJfNeBiSvYgpnIE ZXaH/4IluoUOmY9rZvvGuWL9+FejaMBi1iSUlL3KOMSTSPu9TO+JSid4t 3rD6pDhdLgvgbMyF2Cg0O4arTy2lRZm22dL13s1sneoxPaFfwJ8oNswiC 3NyXsfUv1+xlpaRaJ9hXvKKi2DhhItLHgFMEWG5JIDIjjZ3ja0CficNJG 6YGLe7Wd4kvaQWgzVgtN8rPp1+URrb/9msJpmNzsUkSpU1ffnq0y9Kk2Z Q==; X-CSE-ConnectionGUID: Qf4JMks4TuOvJUktTaLJFQ== X-CSE-MsgGUID: rYoHC6h6S0mslF1EI4Apcw== X-IronPort-AV: E=McAfee;i="6800,10657,11985"; a="5351158" X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="5351158" Received: from fmviesa013.fm.intel.com ([10.60.135.153]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 12:24:05 -0800 X-CSE-ConnectionGUID: Ui3YT4bnSdO3oTh9arZ/7g== X-CSE-MsgGUID: gUDVdLqpRtWuSUJG9Eu/Pw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="15862490" Received: from gsse-cloud1.jf.intel.com ([10.54.39.91]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 12:24:04 -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 v6 21/35] drm/xe: Defrag using out-of-lock page preallocation Date: Thu, 3 Dec 2026 12:23:38 -0800 Message-Id: <20261203202352.4153159-22-matthew.brost@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261203202352.4153159-1-matthew.brost@intel.com> References: <20261203202352.4153159-1-matthew.brost@intel.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: intel-xe@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Xe graphics driver List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Intel-xe" X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=unavailable 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 Convert xe_bo_defrag_one() to a three-phase scheme that moves the expensive beneficial-order allocations out of the BO dma-resv lock: 1. Under the lock, re-check eligibility, capture the tt cpu-caching and read the exact sub-optimal page count (ttm_tt_suboptimal_pages()), bounded by the run budget. 2. Drop the lock and preallocate that many beneficial-order chunks via ttm_pool_prealloc_fill(); reclaim/compaction stalls and the cpu cache mode change both happen here, outside the lock. 3. Re-take the lock, re-check, and validate consuming the prealloc bag. Any shortfall harvests the old backing instead of reclaiming, so the move never stalls under the lock; the BO re-queues for a later pass. With both allocation and caching hoisted out, the BO-lock held time drops to copy-bound (median ~0.27ms, p95 ~2ms); the remaining tail is lock-wait behind active rendering, as intended. 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 Signed-off-by: Matthew Brost --- drivers/gpu/drm/xe/xe_bo.c | 55 +++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c index 2ccf873cbcc0..99dc4ffc67b6 100644 --- a/drivers/gpu/drm/xe/xe_bo.c +++ b/drivers/gpu/drm/xe/xe_bo.c @@ -1521,16 +1521,65 @@ static int xe_bo_defrag_one(struct xe_device *xe, struct xe_bo *bo, .defrag_bytes_remaining = budget, }; struct ttm_buffer_object *ttm_bo = &bo->ttm; + struct ttm_pool *pool = &ttm_bo->bdev->pool; + struct ttm_pool_prealloc pp = {}; struct ttm_placement placement; struct ttm_place place; + enum ttm_caching tt_caching; + unsigned int order, want = 0; int ret = 0; *consumed = 0; *needs_more = false; + /* + * Phase 1: under the BO lock, re-check eligibility and estimate how + * many beneficial-order chunks the move will (re)allocate, bounded by + * the run's byte budget. Then drop the lock so the high-order, possibly + * reclaim/compaction stalling allocations happen unlocked. + */ + xe_bo_lock(bo, false); + if (!xe_bo_needs_defrag(bo)) { + xe_bo_defrag_remove(bo); + xe_bo_unlock(bo); + goto out; + } + tt_caching = bo->ttm.ttm->caching; + order = ttm_pool_prealloc_order(pool); + if (order && ttm_bo->ttm) { + u32 suboptimal = ttm_tt_suboptimal_pages(ttm_bo->ttm); + u64 cap = min_t(u64, budget, xe_bo_size(bo)) >> PAGE_SHIFT; + + /* Prealloc only the beneficial-order chunks the move replaces. */ + want = DIV_ROUND_UP_ULL(min_t(u64, suboptimal, cap), 1UL << order); + } + + /* + * Nothing the move could upgrade: TTM would fail a defrag move that + * allocates no chunk, so drop the BO rather than retry it forever. It + * is re-added if a later populate backs it sub-optimally again. + */ + if (!want) { + xe_bo_defrag_remove(bo); + xe_bo_unlock(bo); + goto out; + } + xe_bo_unlock(bo); + + /* Phase 2: preallocate outside the lock; */ + ret = ttm_pool_prealloc_fill(pool, tt_caching, &pp, want); + if (ret || !pp.count) { + ret = ret ?: -ENOMEM; + xe_gt_stats_incr(xe_root_mmio_gt(xe), + XE_GT_STATS_ID_DEFRAG_FAILED_COUNT, 1); + goto out_err; + } + + /* + * Phase 3: re-take the lock, re-check, and validate using the prealloc. + */ 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; @@ -1540,6 +1589,7 @@ static int xe_bo_defrag_one(struct xe_device *xe, struct xe_bo *bo, place.flags |= TTM_PL_FLAG_TEMPORARY; placement.num_placement = 1; placement.placement = &place; + ctx.prealloc = &pp; /* * On success the move reallocates the backing at beneficial order and @@ -1591,6 +1641,9 @@ static int xe_bo_defrag_one(struct xe_device *xe, struct xe_bo *bo, unlock: xe_bo_unlock(bo); +out_err: + ttm_pool_prealloc_fini(pool, &pp); +out: return ret; } -- 2.34.1