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 10A6B606A322 for ; Thu, 3 Dec 2026 14:01:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=PK1726rx Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C19A310E757; Thu, 3 Dec 2026 14:01:46 +0000 (UTC) Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) by gabe.freedesktop.org (Postfix) with ESMTPS id ED9631128BE; Thu, 3 Dec 2026 14:01:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID: Date:Subject:Cc:To:From:From:Reply-To; bh=zUa1hMsZ9DBkevGknfkknmJi6jmjbECDSM/bpEYET40=; t=1796306500; x=2111666500; b=PK1726rxrWgEssJWfiGRT38aud1/hIQLhDkKpcch1o6MMuTU71FHI0KC1iL/eK3CnYAg9CR2wzU pxaiCMJLABCNF8G8/bI35+0jROqombGMJ93eX6woqa7f71Jm+ICce3VOZh6n61zyobOfqJYszFfSs 8B7nxqrd5h76q1bnN8A261pG3ExDkmXHA92g3MpPwOyHtF+oKOVChbq/xLaBsptQDR51ozZHJISRT 9+PTYvcRvUGL79d5glJV9YicP5lWjQUwhxsBkAh3fMs1KJYAN+yeqN1JPYJg2+GtvgyWCa04qDvbl 4XzcR8JTj0Q61QQ/PU04vSMiZ6HohbGXLOUA==; Received: from [81.79.79.1] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) (envelope-from ) id 1xZ7NS-00000001fYR-1TFE; Thu, 03 Dec 2026 15:01:38 +0100 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: kernel-dev@igalia.com, Tvrtko Ursulin , =?UTF-8?q?Christian=20K=C3=B6nig?= , Alex Deucher , Luben Tuikov , Matthew Brost , Simona Vetter , Danilo Krummrich , Philipp Stanner , "Cc: Tomeu Vizoso" , Sidong Yang , amd-gfx@lists.freedesktop.org Subject: [PATCH v2 09/10] drm/sched: Remove the single scheduler special case Date: Thu, 3 Dec 2026 14:01:27 +0000 Message-ID: <20261203140128.6813-10-tvrtko.ursulin@igalia.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261203140128.6813-1-tvrtko.ursulin@igalia.com> References: <20261203140128.6813-1-tvrtko.ursulin@igalia.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "amd-gfx" X-Spam-Status: No, score=-17.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,HK_RANDOM_FROM, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT 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 Long time ago in commit b3ac17667f11 ("drm/scheduler: rework entity creation") a change was made which prevented priority changes for entities with only one assigned scheduler. That commit reduced drm_sched_entity_set_priority() to simply update the entities priority, but the run queue selection logic in drm_sched_entity_select_rq() was never able to actually change the originally assigned run queue. In practice that only affected amdgpu, being the only driver which can do dynamic priority changes, and it appears it was attempted to rectify this in commit 2316a86bde49 ("drm/amdgpu: change hw sched list on ctx priority override") . A few unresolved problems however remained: The above only fixed drm_sched_entity_set_priority() if drm_sched_entity_modify_sched() was called first which was not documented anywhere. Secondly, that only worked if drm_sched_entity_modify_sched() is actually called, which in amdgpu's case is true only for gfx and compute. Priority changes for other engines, which have only one scheduler assigned such as jpeg and video decode still did not work. (Note that this was also noticed in 981b04d96856 ("drm/sched: improve docs around drm_sched_entity").) Completely different set of non-obvious confusion was that, whereas drm_sched_entity_init() was not keeping the passed in list of schedulers (courtesy of commit 8c23056bdc7a ("drm/scheduler: do not keep a copy of sched list") ), drm_sched_entity_modify_sched() was disagreeing with that and would simply assign the single item list. That incosistency appears to have been semi-silently fixed in commit ac4eb83ab255 ("drm/sched: select new rq even if there is only one v3") . What was also not documented is why it was important to not keep the list of schedulers when there is only one scheduler given. This was in fact due many of the callers passing in a stack container for this case. For the multiple schedulers on the other hand amdgpu and rocket are the only such callers and there the container is not on the stack but correctly follows the lifetime requirement. Keeping a stack backed list in the entity would obviously be undefined behaviour if the list was kept. Having said that, amdgpu however did only stop passing in stack backed container for the more than one scheduler case in commit 977f7e1068be ("drm/amdgpu: allocate entities on demand") . Until then dereferencing freed stack from drm_sched_entity_select_rq() was a possibility. In order to untangle all this lets make this area behave consistently and store the single scheduler case directly in the entity. We can do that easily by creating an union between the current pointer to a list of schedulers, and a new pointer to the single scheduler. This allows removal of the special cases which artificially cleared entity->sched_list for a single scheduler case and fixes the amdgpu dynamic priority changes for the single IP instances. Signed-off-by: Tvrtko Ursulin Fixes: b3ac17667f11 ("drm/scheduler: rework entity creation") References: 8c23056bdc7a ("drm/scheduler: do not keep a copy of sched list") References: 977f7e1068be ("drm/amdgpu: allocate entities on demand") References: 2316a86bde49 ("drm/amdgpu: change hw sched list on ctx priority override") References: ac4eb83ab255 ("drm/sched: select new rq even if there is only one v3") References: 981b04d96856 ("drm/sched: improve docs around drm_sched_entity") References: 77a6809f1dc3 ("drm/sched: Remove FIFO and RR and simplify to a single run queue") Cc: Christian König Cc: Alex Deucher Cc: Luben Tuikov Cc: Matthew Brost Cc: Simona Vetter Cc: Danilo Krummrich Cc: Philipp Stanner Cc: Cc: Tomeu Vizoso Cc: Sidong Yang Cc: amd-gfx@lists.freedesktop.org --- drivers/gpu/drm/scheduler/sched_entity.c | 30 ++++++++++------- include/drm/gpu_scheduler.h | 42 ++++++++++++++---------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c index cdc55906c413..599dd4ff589b 100644 --- a/drivers/gpu/drm/scheduler/sched_entity.c +++ b/drivers/gpu/drm/scheduler/sched_entity.c @@ -115,7 +115,9 @@ static bool drm_sched_list_is_initialized(struct drm_gpu_scheduler **sched_list, * @guilty: atomic_t set to 1 when a job on this queue * is found to be guilty causing a timeout * - * Note that the &sched_list must have at least one element to schedule the entity. + * Note that the &sched_list must have exactly one element to schedule the + * entity. Callers which pass more than one scheduler on the @sched_list must + * ensure the list remains valid for the lifetime of the entity. * * For changing @priority later on at runtime see * drm_sched_entity_set_priority(). For changing the set of schedulers @@ -150,7 +152,10 @@ int drm_sched_entity_init(struct drm_sched_entity *entity, entity->guilty = guilty; entity->last_user = current->group_leader; entity->num_sched_list = num_sched_list; - entity->sched_list = num_sched_list > 1 ? sched_list : NULL; + if (num_sched_list == 1) + entity->sched = sched_list[0]; + else + entity->sched_list = sched_list; RB_CLEAR_NODE(&entity->rb_tree_node); for (i = 0; i < num_sched_list; i++) { @@ -197,6 +202,9 @@ EXPORT_SYMBOL(drm_sched_entity_init); * existing entity->sched_list * @num_sched_list: number of drm sched in sched_list * + * Callers which pass more than one scheduler on the @sched_list must + * ensure the list remains valid for the lifetime of the entity. + * * Note that this must be called under the same common lock for @entity as * drm_sched_job_arm() and drm_sched_entity_push_job(), or the driver needs to * guarantee through some other means that this is never called while new jobs @@ -214,7 +222,10 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity, return; spin_lock(&entity->lock); - entity->sched_list = sched_list; + if (num_sched_list == 1) + entity->sched = sched_list[0]; + else + entity->sched_list = sched_list; entity->num_sched_list = num_sched_list; spin_unlock(&entity->lock); } @@ -596,10 +607,6 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity) struct drm_gpu_scheduler *sched; struct drm_sched_rq *rq; - /* single possible engine and already selected */ - if (!entity->sched_list) - return; - /* queue non-empty, stay on the same engine */ if (spsc_queue_count(&entity->job_queue)) return; @@ -616,16 +623,17 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity) if (fence && !dma_fence_is_signaled(fence)) goto out; - sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list); + if (entity->num_sched_list == 1) + sched = entity->sched; + else + sched = drm_sched_pick_best(entity->sched_list, + entity->num_sched_list); rq = sched ? sched->sched_rq[entity->rq_priority] : NULL; if (rq != entity->rq) { drm_sched_rq_remove_entity(entity->rq, entity); entity->rq = rq; } - if (entity->num_sched_list == 1) - entity->sched_list = NULL; - out: spin_unlock(&entity->lock); } diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h index 23ec446595ae..3978ccecbd15 100644 --- a/include/drm/gpu_scheduler.h +++ b/include/drm/gpu_scheduler.h @@ -121,23 +121,31 @@ struct drm_sched_entity { */ struct drm_sched_entity_stats *stats; - /** - * @sched_list: - * - * A list of schedulers (struct drm_gpu_scheduler). Jobs from this entity can - * be scheduled on any scheduler on this list. - * - * This can be modified by calling drm_sched_entity_modify_sched(). - * Locking is entirely up to the driver, see the above function for more - * details. - * - * This will be set to NULL if &num_sched_list equals 1 and @rq has been - * set already. - * - * FIXME: This means priority changes through - * drm_sched_entity_set_priority() will be lost henceforth in this case. - */ - struct drm_gpu_scheduler **sched_list; + union { + /** + * @sched_list: + * + * A list of schedulers (struct drm_gpu_scheduler). Jobs from + * this entity can be scheduled on any scheduler on this list. + * + * This can be modified by calling + * drm_sched_entity_modify_sched(). Locking is entirely up to + * the driver, see the above function for more details. + */ + struct drm_gpu_scheduler **sched_list; + + /** + * @sched: + * + * A single struct drm_gpu_scheduler jobs from this entity will + * be scheduled on. + * + * This can be modified by calling + * drm_sched_entity_modify_sched(). Locking is entirely up to + * the driver, see the above function for more details. + */ + struct drm_gpu_scheduler *sched; + }; /** * @num_sched_list: -- 2.56.0