Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 46975C54FD2 for ; Thu, 30 Jul 2026 05:24:06 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E927C10E646; Thu, 30 Jul 2026 05:24:05 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="TVbF8OGf"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) by gabe.freedesktop.org (Postfix) with ESMTPS id CB40010E645 for ; Thu, 30 Jul 2026 05:24:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1785389043; x=1816925043; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sebxGSbI38Ye2n0lftfeZkEVXi66s6RRPIVVF9WhBzg=; b=TVbF8OGf8XiyfdEt8jxUDnuJCuULTh0nz/OX0TfKS9+m+zvSytd15cRt dNDXN9DQVPQuPdjas3saP1+AV7IzUn1Ekl6e8a9dpYWP610bRkALynrPA riLShkfQNJLi/5LSPFZKgP9SEd/pOVpfMjljxvs7yWAe+O0Y6bOfxHUTs lw0ZzJDHddQMvpQAw6FLPncogqTteKqzN0erRggLBxNR1KfpaMhztSDhA 10Pw3mg5H7Y0ELs3OOeJ8/l6p/0XJTl/8E0xHlmx/A+Ifs1ZBigg7QTF9 aTKEFE3Jljed2+8G340ukVmFa+7+XnAj//Gj9s4lHE/OS8U8qFg17DZoL w==; X-CSE-ConnectionGUID: ngBpxGdhRxy/u1uvBCDGqA== X-CSE-MsgGUID: TwLL4TI6RaCxBM7nakIfRg== X-IronPort-AV: E=McAfee;i="6800,10657,11859"; a="86078532" X-IronPort-AV: E=Sophos;i="6.25,193,1779174000"; d="scan'208";a="86078532" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Jul 2026 22:24:02 -0700 X-CSE-ConnectionGUID: ek6cUT13RemtYVpKqG33mw== X-CSE-MsgGUID: RCSrXBDuRbS7m3GWTKJl0Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,193,1779174000"; d="scan'208";a="260256344" Received: from soc-5cg5233szh.clients.intel.com ([10.66.65.194]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Jul 2026 22:24:00 -0700 From: Gajendra Uttamchand To: umesh.nerlige.ramappa@intel.com, matthew.brost@intel.com, kamil.konieczny@intel.com, rodrigo.vivi@intel.com, intel-xe@lists.freedesktop.org Cc: Gajendra Uttamchand Subject: [PATCH v8 3/3] drm/xe/lrc: Fix ABA race on engine migration in context timestamp read Date: Thu, 30 Jul 2026 05:23:47 +0000 Message-ID: <20260730052343.1205472-8-gajendra.uttamchand@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260730052343.1205472-5-gajendra.uttamchand@intel.com> References: <20260730052343.1205472-5-gajendra.uttamchand@intel.com> MIME-Version: 1.0 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-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-xe-bounces@lists.freedesktop.org Sender: "Intel-xe" xe_lrc_context_timestamp() reads the engine id once via xe_lrc_engine_id(lrc) and uses it to fetch the live CTX_TIMESTAMP MMIO register, then re-checks the LRC-stored value to detect whether the context switched out while the MMIO read was in flight. That check only confirms the context is (still/again) active - it does not confirm it is active on the *same* engine the MMIO read targeted. If the context is saved and restored onto a different engine between the initial engine id read and the final activity check, the CONTEXT_ACTIVE sentinel will be observed again (now for the new engine), and the stale MMIO value read from the old, now-unrelated engine is returned as if it were valid. Pin the engine id used for the MMIO read and re-validate it against the current engine id after the final activity check. If the engine changed, retry the whole read (bounded by a small retry count) instead of trusting a timestamp sampled from an unrelated context, falling back to the last cached value if the context keeps migrating. Assisted-by: GitHub-Copilot:claude-sonnet-5 Signed-off-by: Gajendra Uttamchand --- drivers/gpu/drm/xe/xe_lrc.c | 45 ++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/drivers/gpu/drm/xe/xe_lrc.c b/drivers/gpu/drm/xe/xe_lrc.c index b9dde26611e3..fedbbb88f6c7 100644 --- a/drivers/gpu/drm/xe/xe_lrc.c +++ b/drivers/gpu/drm/xe/xe_lrc.c @@ -2738,39 +2738,44 @@ static u64 xe_lrc_update_multi_queue_timestamp(struct xe_lrc *lrc, u64 *old_ts) return lrc->queue_timestamp; } +/* + * Bound the number of times we retry the full read sequence when a + * context migration between engines is detected. A small bound (3) + * prevents long loops; if we exhaust retries we fall back to the + * last cached `lrc->ctx_timestamp`. + */ +#define CTX_TIMESTAMP_MAX_RETRIES 3 + static u64 xe_lrc_context_timestamp(struct xe_lrc *lrc) { u64 reg_ts, new_ts = lrc->ctx_timestamp; u64 stored; + u32 engine_id; + int retries = CTX_TIMESTAMP_MAX_RETRIES; /* CTX_TIMESTAMP mmio read is invalid on VF, so return the LRC value */ if (IS_SRIOV_VF(lrc_to_xe(lrc))) return xe_lrc_ctx_timestamp(lrc); - /* - * Safely read CTX_TIMESTAMP: check the LRC-stored value before and - * after the MMIO read to avoid a TOCTOU where a context switch makes the - * MMIO value stale. If the LRC value is not `CONTEXT_ACTIVE` return it; - * otherwise accept the MMIO value only if the context remained active. - */ + do { + /* Read LRC-stored timestamp before/after MMIO to avoid TOCTOU. */ + stored = xe_lrc_ctx_timestamp(lrc); + if (stored != CONTEXT_ACTIVE) + return stored; - stored = xe_lrc_ctx_timestamp(lrc); - if (stored != CONTEXT_ACTIVE) - return stored; + engine_id = xe_lrc_engine_id(lrc); + if (!get_ctx_timestamp(lrc, engine_id, ®_ts)) + new_ts = reg_ts; - /* Context is active: read the live timestamp from the engine's MMIO register. */ - if (!get_ctx_timestamp(lrc, xe_lrc_engine_id(lrc), ®_ts)) - new_ts = reg_ts; + stored = xe_lrc_ctx_timestamp(lrc); + if (stored != CONTEXT_ACTIVE) + return stored; - /* Re-check the LRC-stored timestamp: if the context switched out while - * reading MMIO the hardware saved the canonical timestamp into the LRC - * during context-save, so return that value instead of the MMIO read. - */ - stored = xe_lrc_ctx_timestamp(lrc); - if (stored != CONTEXT_ACTIVE) - return stored; + if (xe_lrc_engine_id(lrc) == engine_id) + return new_ts; + } while (--retries); - return new_ts; + return lrc->ctx_timestamp; } static u64 xe_lrc_update_context_timestamp(struct xe_lrc *lrc, u64 *old_ts) -- 2.43.0