From mboxrd@z Thu Jan 1 00:00:00 1970 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 129D3CCD185 for ; Thu, 2 Oct 2025 05:16:13 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 7AA0110E775; Thu, 2 Oct 2025 05:16:12 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="Ux60sYz2"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) by gabe.freedesktop.org (Postfix) with ESMTPS id 46C2B10E76C; Thu, 2 Oct 2025 05:16:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1759382170; x=1790918170; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AdQlW8vVYuEJllnWM1/hGGj4UhS9rAE35xpGlP0CJQM=; b=Ux60sYz21jHbKrdLN8JwzP6HVhzOBAZk1BLomohKJIaFhHYXKys0a5Q9 9DZ/MjS/DXO9LgQKN+/UPgczTxPGxAEQDLyAzBe8cTafectf6CgqNoicg Kh1KXJNJWNFKiM3xPVLwpJZsNZxMZHjV6Wdr6leFEFJm/EKhmAwQc/3YS RAO/dqQ72VwGhGnZhSSM0qBAQ5ROL0uGx2ctrgRuE8ZpbZPPrwFKPnNjR RHnzFlZr4mlDwkesnhw56lF6+Lj4xp/nq/xh5MCvB3aO29q7/+bqFsOQr vcSDmsHYy1NajkavjO8Tp0onaoUBIR+U3dUqHhj9oYt6ZTUDiHOmGvRvG A==; X-CSE-ConnectionGUID: a5I+Z54qR9WVX/1gXDMWnw== X-CSE-MsgGUID: eQNXMNviT9i3FqrqYQz40w== X-IronPort-AV: E=McAfee;i="6800,10657,11569"; a="65303151" X-IronPort-AV: E=Sophos;i="6.18,308,1751266800"; d="scan'208";a="65303151" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Oct 2025 22:16:09 -0700 X-CSE-ConnectionGUID: Q4wMdfufQ9uH1/xfCfSNsA== X-CSE-MsgGUID: Z2AiwZN6RmCB9Rjx5gi0Jw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.18,308,1751266800"; d="scan'208";a="183264640" Received: from lstrano-desk.jf.intel.com ([10.54.39.91]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Oct 2025 22:16:09 -0700 From: Matthew Brost To: dri-devel@lists.freedesktop.org, intel-xe@lists.freedesktop.org Cc: alexdeucher@gmail.com, dakr@kernel.org, christian.koenig@amd.com, pstanner@redhat.com Subject: [RFC PATCH 2/4] drm/sched: Add several job helpers to avoid drivers touching scheduler state Date: Wed, 1 Oct 2025 22:16:02 -0700 Message-Id: <20251002051604.1865322-3-matthew.brost@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251002051604.1865322-1-matthew.brost@intel.com> References: <20251002051604.1865322-1-matthew.brost@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" Add helpers to find first pending job, pending job count, and a jobs signaled state. Expected to used driver side on recovery and debug flows. Signed-off-by: Matthew Brost --- include/drm/gpu_scheduler.h | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h index a2dcabab8284..9c25ab88b069 100644 --- a/include/drm/gpu_scheduler.h +++ b/include/drm/gpu_scheduler.h @@ -762,4 +762,56 @@ static inline void list_for_each_entry(__job, &(__sched)->pending_list, list) \ for_each_if(!__entitiy || (__job)->entity == (__entitiy)) +/** + * drm_sched_first_pending_job() - DRM scheduler first pending job + * @sched: DRM scheduler + * + * Find DRM scheduler first pending job, drivers should only call this function + * when scheduler is stopped or job can immediately disappear resulting in a + * UAF. + * + * Return: First pending job or NULL + */ +static inline struct drm_sched_job * +drm_sched_first_pending_job(struct drm_gpu_scheduler *sched) +{ + WARN_ON(!READ_ONCE(sched->pause_submit)); + guard(spinlock)(&sched->job_list_lock); + return list_first_entry_or_null(&sched->pending_list, + struct drm_sched_job, list); +} + +/** + * drm_sched_pending_job_count() - DRM scheduler pending job count + * @sched: DRM scheduler + * + * Determine DRM scheduler pending job count. If scheduler if not stopped, this + * value can immediately change, thus drivers must guard against using to count + * to anything memory unsafe. Use with caution. + * + * Return: Number of pending jobs + */ +static inline int drm_sched_pending_job_count(struct drm_gpu_scheduler *sched) +{ + guard(spinlock)(&sched->job_list_lock); + return list_count_nodes(&sched->pending_list); +} + +/** + * drm_sched_job_is_signaled() - DRM scheduler job is signaled + * @job: DRM scheduler job + * + * Determine if DRM scheduler job is signaled. DRM scheduler should be stopped + * to obtain a stable snapshot of state. + * + * Return: True if job is signaled, False otherwise + */ +static inline bool drm_sched_job_is_signaled(struct drm_sched_job *job) +{ + struct drm_sched_fence *s_fence = job->s_fence; + + WARN_ON(!READ_ONCE(job->sched->pause_submit)); + return s_fence->parent && dma_fence_is_signaled(s_fence->parent); +} + #endif -- 2.34.1