From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796205970; b=miNr12z6JXZNwg1PMMaPfYOScVvn8F+Dsndugh31acOVH26NLOerT3t8QebWFVo10FIB nZWYNZQQmJs1HLs5NIB9Llt0AORoiYgW5tBvvFFBmsEufTRHbLLNCA5uYw0Q+zTuLPmvP Oxgd4kmU+UPC8hsk5cciy//+xyyjR5M1wSe9w7SMFzlIcs7Hyj4m3wmxpnTj/THBpOafw BhGkPlHxhIqyfcw5TA1Ihpnt3yQdCgWhoqaXB8YZYUZ2yxAljxlxiUQ3GFyV+AFYrp4xD gMRPq8hLCyreF3JEupMUln5oko4XrEeinlaaJbdtj679Hz+Cqh1GvygdDXJ0Lmin8xA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796205970; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=EGaUGIQWfSxdb/iq2gDczo2OdcKoUY5B8pF9AVAoKjs=; b=r/euJk0hk7AgQ3cna/jQTnvr33/cxqIxVx+2KrnCtXXcLZS6TMX3QbftgN9imBhZvVhe PwKv52n7pYkdZ4eBmo+NNzu8W5peQm3OOaRJcY7FyIIor/YCuocCsreapLXkG1M6uNSlA /KEvYFX/7dfgbQ0Qb/yTLnCqkNOSVSfU8rY65U+kEfr5ddDaQd73w8ioTBeqFsxAgnW+K 6Gmx33Szy5ujaWlZZvZ1rdXQ0H8F61z2L3WObOuCYuHeQcT2/gaPM1AHtUvPPFnQTcYHn HOQoC4Db0/wFcmPLW+6NwVbA4Un/9aN91jUqm0HEc/zZA2wCWa8rbmDlkeiu/AZy3kw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="GOu/R0gQ"; arc=none smtp.remote-ip=209.85.216.41 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6E57606A333 for ; Wed, 2 Dec 2026 10:06:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C6E57606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=GOu/R0gQ Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-3b3a42a4bcdso5245845a91.0 for ; Wed, 02 Dec 2026 02:06:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796205969; x=1796810769; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=EGaUGIQWfSxdb/iq2gDczo2OdcKoUY5B8pF9AVAoKjs=; b=GOu/R0gQeEXbTD1zO+7J3TXxgg85fxfno/6IQDzii+0knzPqRnhQPN1g2yLtXvDLGt zWkwVonHo687Y3mlfGVHJn1PjsqZ7CmKPXG/z+9/LkjE3Fd09ZgJVIuLktSPqme97lzZ epeucTTtDswGJK3gcq70GTmXMz7PSpTtBWopUM+RVBmsk2tg0mdaN0T2q77v0Ly1NWiJ nb/T1ABXQeyMSBvxdiPVCTEjx5g7BK3ZIeOc2jAeHmWKoXEIfA5HH8DRQRPEOTVedEmQ 2RM86u+w+Qxc/U5s62C5NX9kw5SCilg7h2FMn6y96QrLwjaddyFCIESCtLuWllqIkvSY 1o/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796205969; x=1796810769; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=EGaUGIQWfSxdb/iq2gDczo2OdcKoUY5B8pF9AVAoKjs=; b=EfC6uMnIg9EhZkJhb62FNRw5Olqk+bfwxJUKKx8UlbMXw6yG+qmJqHAVNRj98Hsoui AWyb+IlY1ba+bekxWd3m+1cr0lUZ01qWbT58uaaXg/5m0jOORMwgW64w6POyGgOWRYEN M1bdqob5sfHJZe/GP6YB9eKdNl0zoqpj00B6/R+Y3K0v5ZkscmGwV8D3d1d3b/cvDlDV GUgqbTnMWX/XnOiX4S3zB72mgC/JpdnfEwAufmA63Yz+nlxFeChqiRwKsBbSU0ytsUlq lW0BCgOkJVYOFa25QJfX1p3N+yijkQGFgmhKpjb30mBXAEDxIKDyunffLR/6zcrlj3VF QeLA== X-Forwarded-Encrypted: i=1; AL+hErHrI5sf9Vlg3KBoB+qBPPOYi5ZjKTNH6q3WdeOsruXIDI51eXCW2VjVsncVK9quece+LzVArZXzmv51oUM=@vger.kernel.org X-Gm-Message-State: APuU4Z/MHq0DbLYtkj51CDbea6DdIS6bw1VNwOEHdp4dFnhBwOdAOT4j BTsXrGf+Ccm7kJuYRFnwYgZiNYIQrhEPLxryXWHhsmLBRDZpMspGqG3r X-Gm-Gg: AZFdTZeCe3BSiDQKEKMDjvjiA3qDe712E2lVUTPdj7gqEE2pa0Z6Jvw0QwP0CjIpOg5 I2Rn58quHzYKA7My78DvH8DLJLsw+pUkWrKm1GishiI11jvNV44E8c0bEMdDu9ORsPeb31l+jbZ mky5/xNnOvK0jwlaJB2YKR5Iqq+zmi6pueDkrYwn3w3PHtmsuuFVJtf1GQ318lZBcSj552OBFid 1U5RBx+auttru3IcfQ7tDDj2J93wC+p6HF6ckEGBIaUYJCzNB9/0Q87xbzxMOdcwXXecBjLtfWq UJ5nKUzoiXvsKmCckMWrNiR8kPgG/ScVrx4ZICAiLXGHPXD6zUdVSWi/QIBQDPUJjOT85k56KI8 QJZBNpoDILNbEiFHeI31xrtMAzgKvlFIaem+ji2ye6+WcEjBPE+Qw0LTLH0a8QgbqhIDrFGsaet hLJCF1Ewa/cFn7VHVR2wlzGPMlMd92gNVyNxv2aOrQd5FiKkWHGYzZHMnruyR/g/mn17eu8AR6Z nkJjD1ibICaMexzpx0v7HImcs25m3IVCY0rRcvfVOI9ImUV/rg6VRSzDVQeDj/KZZ1ctwcktLR4 EkfbfW2/ItdtqjD6Bi6WvCUxURDZq40LPjYZG1zeQevtXv6v4MVZbttESCeEXdwnaw== X-Received: by 2002:a17:90b:578b:b0:3b5:a3:4433 with SMTP id 98e67ed59e1d1-3b500a3463bmr24679543a91.34.1796205968909; Wed, 02 Dec 2026 02:06:08 -0800 (PST) Received: from gye-System-Product-Name.tail6a86d6.ts.net ([115.88.114.131]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b41ad48786sm61529106a91.9.2026.12.02.02.06.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 02:06:08 -0800 (PST) From: Gyeyoung Baek To: Tomeu Vizoso , Oded Gabbay Cc: Heiko Stuebner , David Airlie , Simona Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , dri-devel@lists.freedesktop.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org, Gyeyoung Baek Subject: [PATCH] accel/rocket: Parallelize independent jobs across cores Date: Wed, 2 Dec 2026 19:05:55 +0900 Message-ID: <20261202100555.3122236-1-gye976@gmail.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The jobs of models with independent branches, such as inception, could run concurrently, but they currently run one after the other on a single core. For example, if jobs B1, B2 and B3 each read the output of job P, and none of them reads the output of another: .--> B1 P ----+--> B2 '--> B3 now: with this patch: core0 [P ][B1][B2][B3] core0 [P ][B1] core1 core1 [B2] core2 core2 [B3] ----------------> t --------> t This is because all the jobs of a submission go to a single entity, and an entity runs on one core at a time. To fix this: - Give each file one entity per core, and pick the core when queueing a job. Jobs are then only ordered by their BO dependencies. Document this in the uAPI. - Pick, in this order: a core whose last job the job depends on, the least recently used idle core, the least recently used core. - Picking the entity now needs the fences in the dma_resv of the BOs, which is shared between submissions and can only be read with its lock held. So move drm_sched_job_init(), which takes the entity, after the BOs are locked and the core is picked. On RK3588 with Mesa main, comparing the inference time before and after this patch (mean of 8 runs of the median of 300 inferences), models with independent branches got faster. Models without branches also got slightly faster, as consecutive inferences now take turns on the idle cores. with branches inception_v1 -11.3% ssd_mobilenet_v2 -6.1% ssdlite_mobiledet -5.9% without branches mobilenet_v2 -3.5% mobilenet_v1 -1.6% Signed-off-by: Gyeyoung Baek --- Depends on "accel/rocket: publish read fences for input buffers" (4d8cb7be620e in drm-misc-fixes). drivers/accel/rocket/rocket_core.h | 5 + drivers/accel/rocket/rocket_device.h | 3 + drivers/accel/rocket/rocket_drv.h | 5 +- drivers/accel/rocket/rocket_job.c | 174 ++++++++++++++++++++------- include/uapi/drm/rocket_accel.h | 3 + 5 files changed, 146 insertions(+), 44 deletions(-) diff --git a/drivers/accel/rocket/rocket_core.h b/drivers/accel/rocket/rocket_core.h index f6d7382854ca9..aaaa844539054 100644 --- a/drivers/accel/rocket/rocket_core.h +++ b/drivers/accel/rocket/rocket_core.h @@ -55,6 +55,11 @@ struct rocket_core { struct drm_gpu_scheduler sched; u64 fence_context; u64 emit_seqno; + + /* Finished fence of the last job queued on this core, protected by sched_lock. */ + struct dma_fence *last_fence; + /* Value of rocket_device.queued_jobs when @last_fence was queued. */ + u64 last_queued; }; int rocket_core_init(struct rocket_core *core); diff --git a/drivers/accel/rocket/rocket_device.h b/drivers/accel/rocket/rocket_device.h index c62d567010696..319ce62f601b1 100644 --- a/drivers/accel/rocket/rocket_device.h +++ b/drivers/accel/rocket/rocket_device.h @@ -17,6 +17,9 @@ struct rocket_device { struct mutex sched_lock; + /* Number of jobs queued so far, protected by sched_lock. */ + u64 queued_jobs; + struct rocket_core *cores; unsigned int num_cores; unsigned int max_cores; diff --git a/drivers/accel/rocket/rocket_drv.h b/drivers/accel/rocket/rocket_drv.h index 9421e48ec5d84..3bd14542353ad 100644 --- a/drivers/accel/rocket/rocket_drv.h +++ b/drivers/accel/rocket/rocket_drv.h @@ -23,8 +23,9 @@ struct rocket_file_priv { struct drm_mm mm; struct mutex mm_lock; - struct drm_gpu_scheduler **scheds; - struct drm_sched_entity sched_entity; + /* One entity per core, see rocket_pick_core(). */ + struct drm_sched_entity *sched_entities; + unsigned int num_sched_entities; }; struct rocket_iommu_domain *rocket_iommu_domain_get(struct rocket_file_priv *rocket_priv); diff --git a/drivers/accel/rocket/rocket_job.c b/drivers/accel/rocket/rocket_job.c index d28167208dc4a..7542c148ac2d0 100644 --- a/drivers/accel/rocket/rocket_job.c +++ b/drivers/accel/rocket/rocket_job.c @@ -210,11 +210,91 @@ static void rocket_attach_object_fences(struct drm_gem_object **bos, dma_resv_add_fence(bos[i]->resv, fence, usage); } -static int rocket_job_push(struct rocket_job *job) +/* Returns true if @job has to wait for @fence because of its BOs. */ +static bool rocket_job_depends_on(struct rocket_job *job, struct dma_fence *fence) { + struct dma_resv_iter cursor; + struct dma_fence *dep; + u32 i; + + for (i = 0; i < job->in_bo_count; i++) { + dma_resv_for_each_fence(&cursor, job->in_bos[i]->resv, + dma_resv_usage_rw(false), dep) { + if (dep == fence) + return true; + } + } + + for (i = 0; i < job->out_bo_count; i++) { + dma_resv_for_each_fence(&cursor, job->out_bos[i]->resv, + dma_resv_usage_rw(true), dep) { + if (dep == fence) + return true; + } + } + + return false; +} + +/* + * Returns the core to queue @job on, the first of: + * 1. a core whose last queued job @job depends on, + * 2. the least recently used idle core, + * 3. the least recently used core. + * + * Called with the reservations of the BOs of @job and rdev->sched_lock held. + */ +static unsigned int rocket_pick_core(struct rocket_file_priv *file_priv, + struct rocket_job *job) +{ + struct rocket_device *rdev = file_priv->rdev; + unsigned int i, best = 0; + bool found = false; + + /* 1. */ + for (i = 0; i < file_priv->num_sched_entities; i++) { + struct rocket_core *core = &rdev->cores[i]; + + if (!core->last_fence || dma_fence_is_signaled(core->last_fence)) + continue; + + if (rocket_job_depends_on(job, core->last_fence)) + return i; + } + + /* 2. */ + for (i = 0; i < file_priv->num_sched_entities; i++) { + struct rocket_core *core = &rdev->cores[i]; + + if (core->last_fence && !dma_fence_is_signaled(core->last_fence)) + continue; + + if (!found || core->last_queued < rdev->cores[best].last_queued) { + best = i; + found = true; + } + } + + if (found) + return best; + + /* 3. */ + for (i = 1; i < file_priv->num_sched_entities; i++) { + if (rdev->cores[i].last_queued < rdev->cores[best].last_queued) + best = i; + } + + return best; +} + +static int rocket_job_push(struct rocket_job *job, struct drm_file *file) +{ + struct rocket_file_priv *file_priv = file->driver_priv; struct rocket_device *rdev = job->rdev; + struct drm_sched_entity *entity; struct drm_gem_object **bos; struct ww_acquire_ctx acquire_ctx; + struct rocket_core *core; int bo_count; int ret = 0; @@ -231,24 +311,35 @@ static int rocket_job_push(struct rocket_job *job) if (ret) goto err; - ret = rocket_acquire_object_fences(job->in_bos, job->in_bo_count, - &job->base, false); - if (ret) - goto err_unlock; - - ret = rocket_acquire_object_fences(job->out_bos, job->out_bo_count, - &job->base, true); - if (ret) - goto err_unlock; - scoped_guard(mutex, &rdev->sched_lock) { + entity = &file_priv->sched_entities[rocket_pick_core(file_priv, job)]; + ret = drm_sched_job_init(&job->base, entity, 1, NULL, file->client_id); + if (ret) + goto err_unlock; + + ret = rocket_acquire_object_fences(job->in_bos, job->in_bo_count, + &job->base, false); + if (!ret) + ret = rocket_acquire_object_fences(job->out_bos, job->out_bo_count, + &job->base, true); + if (ret) { + drm_sched_job_cleanup(&job->base); + goto err_unlock; + } + drm_sched_job_arm(&job->base); job->inference_done_fence = dma_fence_get(&job->base.s_fence->finished); + core = container_of(job->base.sched, struct rocket_core, sched); kref_get(&job->refcount); /* put by scheduler job completion */ drm_sched_entity_push_job(&job->base); + + /* Record the job as the last one of its core, for rocket_pick_core(). */ + dma_fence_put(core->last_fence); + core->last_fence = dma_fence_get(job->inference_done_fence); + core->last_queued = ++rdev->queued_jobs; } rocket_attach_object_fences(job->in_bos, job->in_bo_count, @@ -530,6 +621,9 @@ void rocket_job_fini(struct rocket_core *core) { drm_sched_fini(&core->sched); + dma_fence_put(core->last_fence); + core->last_fence = NULL; + cancel_work_sync(&core->reset.work); destroy_workqueue(core->reset.wq); } @@ -537,35 +631,42 @@ 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->sched_entities = kzalloc_objs(*rocket_priv->sched_entities, + rdev->num_cores); + if (!rocket_priv->sched_entities) return -ENOMEM; - for (core = 0; core < rdev->num_cores; core++) - scheds[core] = &rdev->cores[core].sched; + for (core = 0; core < rdev->num_cores; core++) { + struct drm_gpu_scheduler *sched = &rdev->cores[core].sched; - 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; + ret = drm_sched_entity_init(&rocket_priv->sched_entities[core], + DRM_SCHED_PRIORITY_NORMAL, + &sched, 1, NULL); + if (ret) + goto err_destroy_entities; + } + + rocket_priv->num_sched_entities = rdev->num_cores; + + return 0; +err_destroy_entities: + while (core--) + drm_sched_entity_destroy(&rocket_priv->sched_entities[core]); + kfree(rocket_priv->sched_entities); return ret; } void rocket_job_close(struct rocket_file_priv *rocket_priv) { - struct drm_sched_entity *entity = &rocket_priv->sched_entity; + unsigned int core; - drm_sched_entity_destroy(entity); - kfree(rocket_priv->scheds); + for (core = 0; core < rocket_priv->num_sched_entities; core++) + drm_sched_entity_destroy(&rocket_priv->sched_entities[core]); + kfree(rocket_priv->sched_entities); } int rocket_job_is_idle(struct rocket_core *core) @@ -602,37 +703,26 @@ static int rocket_ioctl_submit_job(struct drm_device *dev, struct drm_file *file rjob->rdev = rdev; rjob->domain = rocket_iommu_domain_get(file_priv); - ret = drm_sched_job_init(&rjob->base, - &file_priv->sched_entity, - 1, NULL, file->client_id); - if (ret) - goto out_put_job; - ret = rocket_copy_tasks(dev, file, job, rjob); if (ret) - goto out_cleanup_job; + goto out_put_job; ret = drm_gem_objects_lookup(file, u64_to_user_ptr(job->in_bo_handles), job->in_bo_handle_count, &rjob->in_bos); if (ret) - goto out_cleanup_job; + goto out_put_job; rjob->in_bo_count = job->in_bo_handle_count; ret = drm_gem_objects_lookup(file, u64_to_user_ptr(job->out_bo_handles), job->out_bo_handle_count, &rjob->out_bos); if (ret) - goto out_cleanup_job; + goto out_put_job; rjob->out_bo_count = job->out_bo_handle_count; - ret = rocket_job_push(rjob); - if (ret) - goto out_cleanup_job; + ret = rocket_job_push(rjob, file); -out_cleanup_job: - if (ret) - drm_sched_job_cleanup(&rjob->base); out_put_job: rocket_job_put(rjob); diff --git a/include/uapi/drm/rocket_accel.h b/include/uapi/drm/rocket_accel.h index d0685e372b792..6c852e3d00aa3 100644 --- a/include/uapi/drm/rocket_accel.h +++ b/include/uapi/drm/rocket_accel.h @@ -161,6 +161,9 @@ struct drm_rocket_job { * struct drm_rocket_submit - ioctl argument for submitting commands to the NPU. * * The kernel will schedule the execution of these jobs in dependency order. + * Dependencies, including those on jobs of earlier submissions, are only + * given by the BOs listed in each struct drm_rocket_job. Jobs that don't + * depend on each other may run concurrently on different cores. */ struct drm_rocket_submit { /** base-commit: 28f286b0dfa9610d4ee2447415e964e8bc603056 prerequisite-patch-id: 5f2509e3f6505d397b609881d54814a3d7a47bd8 -- 2.43.0