From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796541436; b=gKwIYCKyAg+EmOD9XkrvM8bpEaGRsYP8khi07z5t7S2B57nC01nWQJhu1foyxCV3sEl1 L3NkFerhT0IPYMyUuHxPgY/4Xqu5LUDQEJiETOzs4jGSPxXOppHztzw6Ir9Pt7kUhthW+ njTC4W+jUD5lzMcYeeru9yNpTuemc3BxV50YZxb27wpPd49gUUD8g8FFGWTRocR2H7brl V5B3JcmuHMJnh4P1PTazvBuuuMoFQZ57T62YHwv1X9v72FBaiImcexzxc8PDkTsSGRk0E irWgnOlTI8kpvxT5JIletA5nrcPvGwlXSXG5dBJyRzGI1XN3e94vS9mNRgfDhWJ3/yQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796541436; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=OdabrZOzCYy0QVjh7xUBGkjINL38dk29Nm+JuNiCmdo=; b=EBVDOEX9gkiLnRvIlCkXxT06fOmCXzY7/gqp9l7S4wPM1FtX/SatvQyMyJXmALKOxIUO 5Bv3WeTXlpWXq5w1GBRv1iTFPBDnjlpqsMrTaSQ2W4RyDnE80NMKRHP70W+e8+EVBqqxF 4HXi0qjeE7gHvYUOPEg8t7mvzvVr0ZCoWDMNtjwRuN5vl+H4ZPb7633SbQh6/7CVDfon7 vZMKRaZgyX2oY99O8b25O1x2/MwkJWFVI7DAOrEBVhtobK8V1MsrgxldJmc3N/rct0Jlf glMPYKbwSWH5Q+B84eY90JcRgTd3l9XhILJjviNat0QuXeFKNLjKaAXeolbBANsBVyg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=T7PK0wP3; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4DB03606A6B2; Sun, 6 Dec 2026 07:17:16 +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=T7PK0wP3 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796541436; b=fS7ppms6hZAiD27rnRIRvWATsmFRzChGieT+jPsdSFG3nJwAXoaWsovi65xxvVhqRj3U XCx/jM0WNIg4Cgp980myRG44/Wl3oKx6t1cSIBw6Jx4UkgsmGDn8u+26EIcUJTk38AgkZ 13Qwv8+8TGThUQ/7FDIVEvXIJXdrSkLn09Kid8G3/5CKU5/sdh2LM6t+c0tEPwkSf9DXW AxlK860vekKzlia84CpMEhS9MdxOfh+HoX+d6WQj4r2rZDNwFJRFy4hjHahLnAEbb8noq JJQYnHttNtZg0AW9RC6n0s7e4+aRQiuYh8qIw2pzKBMWTmS3sCCMYtTop26ZZxuxusQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796541436; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=OdabrZOzCYy0QVjh7xUBGkjINL38dk29Nm+JuNiCmdo=; b=vf53nPK5woKwYM8mNfSrOtmaTrnOzYIa2EcZCbsudoD4TiqT6lHb/zANFuCNFTrRGE4n dTdseFbYsHf8Keu2qFCx+jVaifbaQcVzRt5fzpvXG1CtutwtZf+GE30j8ICQPnEAQt49U MH22jvEJVLpnEYMXAuM7CMk6yH3GFH6/lS2wm1WbpP0iSGuL20iOwqgoC+g+Lwcrx3cra UhvSAFiZPxDQ9Q8rPvPEEyixEzp3ChhxR8E+aQsF0yP3A75w6eVQMct8CYAQtMP9uhWUA twAVfmVqfXRkndYxFE37psazNi/EwAC/B7yB3vVggIcNi54i++UAyOcwzMqIcXk2WlA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (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.kernel.org (Postfix) with ESMTPS id C22B41F0093B; Sun, 6 Dec 2026 07:17:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796541435; bh=OdabrZOzCYy0QVjh7xUBGkjINL38dk29Nm+JuNiCmdo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=T7PK0wP3BXRYo+WKzy8IFBklPWjYY/yhvKFLW08dgern3fdIJ0/kYL+515GiKFN9A FIt4j1ioyZY3a9TVhlAeOqGbQD7jR9tZM4xrR62bvuugv8qVlU+nrXcjq8TogiYlp6 IX2Ebd2F5tZ01KjLwG5RSlXBSXNz7cwoCmmQpCHzyBSH+HzICHP5CKGH6oFdhzM2XJ eYYj/xQajTIyip+2X+dw8e8Tglrp6I/h57eWOLvcUYrbkO5bcwgA1j8X4HdRRXRxLq ig6lARfZNC6phXuQdRUWQ4zL2wA+gBRogH7AHsOuwGOlkosDAGBSm33P3CeSzLNkbV 0hVCqtbahpjNA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id BCF795068D; Sun, 6 Dec 2026 07:17:13 +0000 (UTC) From: Arun Iyengar via B4 Relay Date: Sun, 06 Dec 2026 07:17:18 +0000 Subject: [PATCH v4 7/7] accel/rocket: Move open files off a core at unbind X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261206-rocket-fixes-v4-7-75a6279bce98@gmail.com> References: <20261206-rocket-fixes-v4-0-75a6279bce98@gmail.com> In-Reply-To: <20261206-rocket-fixes-v4-0-75a6279bce98@gmail.com> To: Tomeu Vizoso , Oded Gabbay Cc: Heiko Stuebner , Jeff Hugo , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Philipp Stanner , Danilo Krummrich , Matthew Brost , Igor Paunovic , Midgy BALON , Jiaxing Hu , dri-devel@lists.freedesktop.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, stable@vger.kernel.org, Arun Iyengar X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796541432; l=11596; i=aiyengar1990@gmail.com; s=20261110; h=from:subject:message-id; bh=rrG+BpKPTCfdeNP39xfgrHoRYCzupq/btY+MF1J5aiM=; b=7HQRTxh3pQLO9D2m2o3NetfKsjXIFw9qDoowlMfnWwT2gT5Mov8GTU3AbJzEp85V6oskd8WMG QMv2QyK9dGfDM7LjNWFdF9XzLvm0iUuDPey6HNfzymU/XyWmqPWPw+E X-Developer-Key: i=aiyengar1990@gmail.com; a=ed25519; pk=zXpRgUTrhaFe6jLyPqbBy3ccfkj04w0g2690Wv3mg+0= X-Endpoint-Received: by B4 Relay for aiyengar1990@gmail.com/20261110 with auth_id=1480 X-Original-From: Arun Iyengar Reply-To: aiyengar1990@gmail.com Content-Transfer-Encoding: 7bit From: Arun Iyengar A file's scheduler entity can stay on the run queue of a core that is unbound. drm_sched_fini() frees that run queue, and the next submit or the file's close then uses it. Jobs queued on that core never run. At unbind, destroy each entity on the unbound core, which waits until its queued jobs have started (they fail with -ENODEV), and build it again on the cores that are up. Wait for the file's last job before the rebuild, or the new entity could run another job next to it. The other entities, and all of them at bind, only get the new core list. Jobs submitted on a file left with no core are dropped (-ENODEV inside; rocket_ioctl_submit() returns it only with commit 3185975d75f8 ("accel/rocket: Propagate job submission errors")). Keep sched_lock and the core array until the DRM device is released, as files closed after the last core is unbound still use them. Close waits until the file's jobs are freed: killed jobs are freed later, from a work item that uses their core. Fixes: 0810d5ad88a1 ("accel/rocket: Add job submission IOCTL") Cc: stable@vger.kernel.org Signed-off-by: Arun Iyengar --- drivers/accel/rocket/rocket_core.c | 2 + drivers/accel/rocket/rocket_device.c | 7 +- drivers/accel/rocket/rocket_device.h | 2 + drivers/accel/rocket/rocket_drv.h | 8 ++ drivers/accel/rocket/rocket_job.c | 139 ++++++++++++++++++++++++++++++----- drivers/accel/rocket/rocket_job.h | 2 +- 6 files changed, 140 insertions(+), 20 deletions(-) diff --git a/drivers/accel/rocket/rocket_core.c b/drivers/accel/rocket/rocket_core.c index e51b64845a8b..f49efc18a66e 100644 --- a/drivers/accel/rocket/rocket_core.c +++ b/drivers/accel/rocket/rocket_core.c @@ -95,6 +95,8 @@ int rocket_core_init(struct rocket_core *core) dev_info(dev, "Rockchip NPU core %d version: %d\n", core->index, version); + rocket_job_reinit_entities(core->rdev); + return 0; } diff --git a/drivers/accel/rocket/rocket_device.c b/drivers/accel/rocket/rocket_device.c index efd004194c1a..26a65e1b8a24 100644 --- a/drivers/accel/rocket/rocket_device.c +++ b/drivers/accel/rocket/rocket_device.c @@ -2,6 +2,7 @@ /* Copyright 2024-2025 Tomeu Vizoso */ #include +#include #include #include #include @@ -33,7 +34,7 @@ struct rocket_device *rocket_device_init(struct platform_device *pdev, rdev->max_cores = num_cores; - rdev->cores = devm_kcalloc(dev, num_cores, sizeof(*rdev->cores), GFP_KERNEL); + rdev->cores = drmm_kcalloc(ddev, num_cores, sizeof(*rdev->cores), GFP_KERNEL); if (!rdev->cores) return ERR_PTR(-ENOMEM); @@ -43,10 +44,12 @@ struct rocket_device *rocket_device_init(struct platform_device *pdev, if (err) return ERR_PTR(err); - err = devm_mutex_init(dev, &rdev->sched_lock); + err = drmm_mutex_init(ddev, &rdev->sched_lock); if (err) return ERR_PTR(-ENOMEM); + INIT_LIST_HEAD(&rdev->files); + err = drm_dev_register(ddev, 0); if (err) return ERR_PTR(err); diff --git a/drivers/accel/rocket/rocket_device.h b/drivers/accel/rocket/rocket_device.h index c62d56701069..4ee8935def26 100644 --- a/drivers/accel/rocket/rocket_device.h +++ b/drivers/accel/rocket/rocket_device.h @@ -16,6 +16,8 @@ struct rocket_device { struct drm_device ddev; struct mutex sched_lock; + /* Open files, under @sched_lock */ + struct list_head files; struct rocket_core *cores; unsigned int num_cores; diff --git a/drivers/accel/rocket/rocket_drv.h b/drivers/accel/rocket/rocket_drv.h index f7a77ea98467..c8e9cad31625 100644 --- a/drivers/accel/rocket/rocket_drv.h +++ b/drivers/accel/rocket/rocket_drv.h @@ -24,8 +24,16 @@ struct rocket_file_priv { struct rocket_vm *vm; + struct list_head node; + struct drm_gpu_scheduler **scheds; + unsigned int num_scheds; struct drm_sched_entity sched_entity; + /* Serialises @sched_entity changes at close, core unbind and bind */ + struct mutex entity_lock; + bool closed; + /* Jobs not freed yet */ + atomic_t jobs; struct dma_fence *last_job_fence; }; diff --git a/drivers/accel/rocket/rocket_job.c b/drivers/accel/rocket/rocket_job.c index 53f0751d7a1e..c57bbbfe55d2 100644 --- a/drivers/accel/rocket/rocket_job.c +++ b/drivers/accel/rocket/rocket_job.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "rocket_core.h" #include "rocket_device.h" @@ -232,6 +233,11 @@ static int rocket_job_push(struct rocket_job *job) goto err; scoped_guard(mutex, &rdev->sched_lock) { + if (!job->file_priv->num_scheds) { + ret = -ENODEV; + goto err_unlock; + } + drm_sched_job_arm(&job->base); job->inference_done_fence = dma_fence_get(&job->base.s_fence->finished); @@ -266,6 +272,7 @@ static void rocket_job_cleanup(struct kref *ref) { struct rocket_job *job = container_of(ref, struct rocket_job, refcount); + struct rocket_file_priv *file_priv = job->file_priv; unsigned int i; rocket_vm_put(job->vm); @@ -290,6 +297,9 @@ static void rocket_job_cleanup(struct kref *ref) kvfree(job->tasks); kfree(job); + + if (atomic_dec_and_test(&file_priv->jobs)) + wake_up_var(&file_priv->jobs); } static void rocket_job_put(struct rocket_job *job) @@ -520,6 +530,86 @@ static irqreturn_t rocket_job_irq_handler(int irq, void *data) return IRQ_WAKE_THREAD; } +/* List the cores that are up. Called with sched_lock held. */ +static unsigned int rocket_job_up_scheds(struct rocket_file_priv *rocket_priv) +{ + struct rocket_device *rdev = rocket_priv->rdev; + unsigned int core, n = 0; + + for (core = 0; core < rdev->max_cores; core++) { + struct rocket_core *c = &rdev->cores[core]; + + if (!READ_ONCE(c->removing) && drm_sched_wqueue_ready(&c->sched)) + rocket_priv->scheds[n++] = &c->sched; + } + + return n; +} + +/* Build the entity on the cores that are up. Called with sched_lock held. */ +static int rocket_job_entity_init(struct rocket_file_priv *rocket_priv) +{ + unsigned int n = rocket_job_up_scheds(rocket_priv); + int ret; + + if (!n) + return -ENODEV; + + ret = drm_sched_entity_init(&rocket_priv->sched_entity, + DRM_SCHED_PRIORITY_NORMAL, + rocket_priv->scheds, n, NULL); + if (!ret) + rocket_priv->num_scheds = n; + + return ret; +} + +/* Called with sched_lock held: jobs are armed under it, so entity->rq is stable */ +static bool rocket_job_on_removing_core(struct drm_sched_entity *entity) +{ + return READ_ONCE(container_of(entity->rq->sched, struct rocket_core, + sched)->removing); +} + +/* + * Move every open file's entity onto the cores that are up. An entity on a + * removing core is rebuilt, as drm_sched_fini() frees its run queue: the + * flush in drm_sched_entity_destroy() runs its queued jobs, which fail. + */ +void rocket_job_reinit_entities(struct rocket_device *rdev) +{ + struct rocket_file_priv *rocket_priv; + + guard(mutex)(&rdev->sched_lock); + + list_for_each_entry(rocket_priv, &rdev->files, node) { + struct drm_sched_entity *entity = &rocket_priv->sched_entity; + + guard(mutex)(&rocket_priv->entity_lock); + + if (rocket_priv->closed) + continue; + + if (rocket_priv->num_scheds && !rocket_job_on_removing_core(entity)) { + rocket_priv->num_scheds = rocket_job_up_scheds(rocket_priv); + drm_sched_entity_modify_sched(entity, rocket_priv->scheds, + rocket_priv->num_scheds); + continue; + } + + if (rocket_priv->num_scheds) { + drm_sched_entity_destroy(entity); + rocket_priv->num_scheds = 0; + + /* No second job of the file on the NPU */ + if (rocket_priv->last_job_fence) + dma_fence_wait(rocket_priv->last_job_fence, false); + } + + rocket_job_entity_init(rocket_priv); + } +} + int rocket_job_init(struct rocket_core *core) { struct drm_sched_init_args args = { @@ -581,6 +671,9 @@ void rocket_job_fini(struct rocket_core *core) queue_work(core->reset.wq, &core->reset.work); flush_work(&core->reset.work); + /* drm_sched_fini() frees the run queues the entities are on */ + rocket_job_reinit_entities(core->rdev); + drm_sched_fini(&core->sched); cancel_work_sync(&core->reset.work); @@ -590,40 +683,51 @@ void rocket_job_fini(struct rocket_core *core) int rocket_job_open(struct rocket_file_priv *rocket_priv) { struct rocket_device *rdev = rocket_priv->rdev; - struct drm_gpu_scheduler **scheds = kmalloc_objs(*scheds, - rdev->num_cores); - unsigned int core; int ret; - if (!scheds) + rocket_priv->scheds = kmalloc_objs(*rocket_priv->scheds, rdev->max_cores); + if (!rocket_priv->scheds) return -ENOMEM; - for (core = 0; core < rdev->num_cores; core++) - scheds[core] = &rdev->cores[core].sched; + mutex_init(&rocket_priv->entity_lock); - ret = drm_sched_entity_init(&rocket_priv->sched_entity, - DRM_SCHED_PRIORITY_NORMAL, - scheds, - rdev->num_cores, NULL); - if (ret) - kfree(scheds); - else - rocket_priv->scheds = scheds; + guard(mutex)(&rdev->sched_lock); - return ret; + ret = rocket_job_entity_init(rocket_priv); + if (ret) { + mutex_destroy(&rocket_priv->entity_lock); + kfree(rocket_priv->scheds); + return ret; + } + + list_add(&rocket_priv->node, &rdev->files); + + return 0; } void rocket_job_close(struct rocket_file_priv *rocket_priv) { - struct drm_sched_entity *entity = &rocket_priv->sched_entity; + struct rocket_device *rdev = rocket_priv->rdev; - drm_sched_entity_destroy(entity); + scoped_guard(mutex, &rocket_priv->entity_lock) { + if (rocket_priv->num_scheds) + drm_sched_entity_destroy(&rocket_priv->sched_entity); + rocket_priv->num_scheds = 0; + rocket_priv->closed = true; + } + + scoped_guard(mutex, &rdev->sched_lock) + list_del(&rocket_priv->node); if (rocket_priv->last_job_fence) { dma_fence_wait(rocket_priv->last_job_fence, false); dma_fence_put(rocket_priv->last_job_fence); } + /* Killed jobs are freed later, from a work item that uses the core */ + wait_var_event(&rocket_priv->jobs, !atomic_read(&rocket_priv->jobs)); + + mutex_destroy(&rocket_priv->entity_lock); kfree(rocket_priv->scheds); } @@ -655,6 +759,7 @@ static int rocket_ioctl_submit_job(struct drm_device *dev, struct drm_file *file rjob->rdev = rdev; rjob->file_priv = file_priv; + atomic_inc(&file_priv->jobs); rjob->vm = rocket_vm_get(file_priv); ret = drm_sched_job_init(&rjob->base, diff --git a/drivers/accel/rocket/rocket_job.h b/drivers/accel/rocket/rocket_job.h index 27db6f861250..f018920c8f3f 100644 --- a/drivers/accel/rocket/rocket_job.h +++ b/drivers/accel/rocket/rocket_job.h @@ -20,7 +20,6 @@ struct rocket_job { struct rocket_device *rdev; - /* Not valid after rocket_job_close() */ struct rocket_file_priv *file_priv; struct drm_gem_object **in_bos; @@ -48,6 +47,7 @@ int rocket_ioctl_submit(struct drm_device *dev, void *data, struct drm_file *fil int rocket_job_init(struct rocket_core *core); void rocket_job_fini(struct rocket_core *core); +void rocket_job_reinit_entities(struct rocket_device *rdev); int rocket_job_open(struct rocket_file_priv *rocket_priv); void rocket_job_close(struct rocket_file_priv *rocket_priv); int rocket_job_is_idle(struct rocket_core *core); -- 2.55.0