drm/nouveau: implement 1:1 scheduler - entity relationship
Subsystems (3)
PATCH 1/2 drm/nouveau: implement 1:1 scheduler - entity relationship
Recent patches to the DRM scheduler [1][2] allow for a variable number of run-queues and add support for (shared) workqueues rather than dedicated kthreads per scheduler. This allows us to create a 1:1 relationship between a GPU scheduler and a scheduler entity, in order to properly support firmware schedulers being able to handle an arbitrary amount of dynamically allocated command ring buffers. This perfectly matches Nouveau's needs, hence make use of it.
Topology wise we create one scheduler instance per client (handling VM_BIND jobs) and one scheduler instance per channel (handling EXEC jobs).
All channel scheduler instances share a workqueue, but every client scheduler instance has a dedicated workqueue. The latter is required to ensure that for VM_BIND job's free_job() work and run_job() work can always run concurrently and hence, free_job() work can never stall run_job() work. For EXEC jobs we don't have this requirement, since EXEC job's free_job() does not require to take any locks which indirectly or directly are held for allocations elsewhere.
[1] https://lore.kernel.org/all/[email protected]/T/
[2] https://lore.kernel.org/all/[email protected]/T/
--- drivers/gpu/drm/nouveau/nouveau_abi16.c | 18 +- drivers/gpu/drm/nouveau/nouveau_abi16.h | 2 +- drivers/gpu/drm/nouveau/nouveau_drm.c | 31 ++-- drivers/gpu/drm/nouveau/nouveau_drv.h | 9 +- drivers/gpu/drm/nouveau/nouveau_exec.c | 7 +- drivers/gpu/drm/nouveau/nouveau_exec.h | 2 +- drivers/gpu/drm/nouveau/nouveau_sched.c | 209 +++++++++++++----------- drivers/gpu/drm/nouveau/nouveau_sched.h | 35 ++-- drivers/gpu/drm/nouveau/nouveau_uvmm.c | 69 +++----- drivers/gpu/drm/nouveau/nouveau_uvmm.h | 4 +- 10 files changed, 188 insertions(+), 198 deletions(-)
drivers/gpu/drm/nouveau/nouveau_abi16.c +5 -13
@@ -126,21 +126,14 @@ nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16, {struct nouveau_abi16_ntfy *ntfy, *temp; -/* When a client exits without waiting for it's queued up jobs to -* finish it might happen that we fault the channel. This is due to -* drm_file_free() calling drm_gem_release() before the postclose() -* callback. Hence, we can't tear down this scheduler entity before -* uvmm mappings are unmapped. Currently, we can't detect this case. -* -* However, this should be rare and harmless, since the channel isn't -* needed anymore. -*/ -nouveau_sched_entity_fini (&chan->sched_entity); +/* Cancel all jobs from the entity's queue. */ +drm_sched_entity_fini (&chan->sched.entity); -/* wait for all activity to stop before cleaning up */ if (chan->chan)nouveau_channel_idle (chan->chan); +nouveau_sched_fini (&chan->sched); +/* cleanup notifier state */ list_for_each_entry_safe (ntfy, temp, &chan->notifiers, head) {nouveau_abi16_ntfy_fini (chan, ntfy);
@@ -323,8 +316,7 @@ nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)if (ret) goto done; - ret =nouveau_sched_entity_init (&chan->sched_entity, &drm->sched, - drm->sched_wq); + ret =nouveau_sched_init (&chan->sched, drm, drm->sched_wq);if (ret) goto done;
drivers/gpu/drm/nouveau/nouveau_abi16.h +1 -1
@@ -26,7 +26,7 @@ struct nouveau_abi16_chan {struct nouveau_bo *ntfy;struct nouveau_vma *ntfy_vma;struct nvkm_mm heap; -struct nouveau_sched_entity sched_entity; +struct nouveau_sched sched; };struct nouveau_abi16 {
drivers/gpu/drm/nouveau/nouveau_drm.c +21 -10
@@ -201,9 +201,9 @@ nouveau_cli_fini(struct nouveau_cli *cli)WARN_ON (!list_empty (&cli->worker));usif_client_fini (cli); +nouveau_sched_fini (&cli->sched);if (uvmm)nouveau_uvmm_fini (uvmm); -nouveau_sched_entity_fini (&cli->sched_entity);nouveau_vmm_fini (&cli->svm);nouveau_vmm_fini (&cli->vmm);nvif_mmu_dtor (&cli->mmu);
@@ -310,8 +310,17 @@ nouveau_cli_init(struct nouveau_drm *drm, const char *sname, cli->mem = &mems[ret]; - ret =nouveau_sched_entity_init (&cli->sched_entity, &drm->sched, - drm->sched_wq); +/* Don't pass in the (shared) sched_wq in order to let +* nouveau_sched_init() create a dedicated one for VM_BIND jobs. +* +* This is required to ensure that for VM_BIND jobs free_job() work and +* run_job() work can always run concurrently and hence, free_job() work +* can never stall run_job() work. For EXEC jobs we don't have this +* requirement, since EXEC job's free_job() does not require to take any +* locks which indirectly or directly are held for allocations +* elsewhere. +*/ + ret =nouveau_sched_init (&cli->sched, drm,NULL );if (ret) goto done;
@@ -582,13 +591,16 @@ nouveau_drm_device_init(struct drm_device *dev)nvif_parent_ctor (&nouveau_parent, &drm->parent); drm->master.base.object.parent = &drm->parent; - ret =nouveau_sched_init (drm); -if (ret) + drm->sched_wq =alloc_workqueue ("nouveau_sched_wq_shared" ,0 , +WQ_MAX_ACTIVE ); +if (!drm->sched_wq) { + ret = -ENOMEM ; goto fail_alloc; + } ret =nouveau_cli_init (drm,"DRM-master" , &drm->master);if (ret) - goto fail_sched; + goto fail_wq; ret =nouveau_cli_init (drm,"DRM" , &drm->client);if (ret)
@@ -658,8 +670,8 @@ nouveau_drm_device_init(struct drm_device *dev)nouveau_cli_fini (&drm->client); fail_master:nouveau_cli_fini (&drm->master); -fail_sched: -nouveau_sched_fini (drm); +fail_wq: +destroy_workqueue (drm->sched_wq); fail_alloc:nvif_parent_dtor (&drm->parent);kfree (drm);
@@ -711,10 +723,9 @@ nouveau_drm_device_fini(struct drm_device *dev) }mutex_unlock (&drm->clients_lock); -nouveau_sched_fini (drm); -nouveau_cli_fini (&drm->client);nouveau_cli_fini (&drm->master); +destroy_workqueue (drm->sched_wq);nvif_parent_dtor (&drm->parent);mutex_destroy (&drm->clients_lock);kfree (drm);
drivers/gpu/drm/nouveau/nouveau_drv.h +4 -5
@@ -98,7 +98,7 @@ struct nouveau_cli {bool disabled; } uvmm; -struct nouveau_sched_entity sched_entity; +struct nouveau_sched sched;const struct nvif_mclass *mem;
@@ -258,6 +258,9 @@ struct nouveau_drm {u64 context_base; } *runl; +/* Workqueue used for channel schedulers. */ +struct workqueue_struct *sched_wq; +/* context for accelerated drm-internal operations */ struct nouveau_channel *cechan;struct nouveau_channel *channel;
@@ -298,10 +301,6 @@ struct nouveau_drm {struct mutex lock;bool component_registered; } audio; - -struct drm_gpu_scheduler sched; -struct workqueue_struct *sched_wq; - }; static inline struct nouveau_drm *
drivers/gpu/drm/nouveau/nouveau_exec.c +3 -4
@@ -192,6 +192,7 @@ nouveau_exec_job_free(struct nouveau_job *job) {struct nouveau_exec_job *exec_job =to_nouveau_exec_job (job); +nouveau_job_done (job);nouveau_job_free (job);kfree (exec_job->fence);
@@ -211,8 +212,6 @@ nouveau_exec_job_timeout(struct nouveau_job *job)NV_PRINTK (warn, job->cli,"job timeout, channel %d killed!\n" , chan->chid); -nouveau_sched_entity_fini (job->entity); -return DRM_GPU_SCHED_STAT_ENODEV ; }
@@ -261,7 +260,7 @@ nouveau_exec_job_init(struct nouveau_exec_job **pjob, job->chan = __args->chan; - args.sched_entity = __args->sched_entity; + args.sched = __args->sched; args.file_priv = __args->file_priv; args.in_sync.count = __args->in_sync.count;
@@ -414,7 +413,7 @@ nouveau_exec_ioctl_exec(struct drm_device *dev,if (ret) goto out; - args.sched_entity = &chan16->sched_entity; + args.sched = &chan16->sched; args.file_priv = file_priv; args.chan = chan;
drivers/gpu/drm/nouveau/nouveau_exec.h +1 -1
@@ -10,7 +10,7 @@struct nouveau_exec_job_args {struct drm_file *file_priv; -struct nouveau_sched_entity *sched_entity; +struct nouveau_sched *sched;struct drm_exec exec;struct nouveau_channel *chan;
drivers/gpu/drm/nouveau/nouveau_sched.c +118 -91
312 lines not on this page · raw message
drivers/gpu/drm/nouveau/nouveau_sched.h +13 -22
@@ -26,7 +26,7 @@ enum nouveau_job_state {struct nouveau_job_args {struct drm_file *file_priv; -struct nouveau_sched_entity *sched_entity; +struct nouveau_sched *sched;enum dma_resv_usage resv_usage;bool sync;
@@ -49,7 +49,8 @@ struct nouveau_job {enum nouveau_job_state state; -struct nouveau_sched_entity *entity; +struct nouveau_sched *sched; +struct list_head entry;struct drm_file *file_priv;struct nouveau_cli *cli;
@@ -90,20 +91,17 @@ int nouveau_job_ucopy_syncs(struct nouveau_job_args *args,int nouveau_job_init (struct nouveau_job *job,struct nouveau_job_args *args); -void nouveau_job_free (struct nouveau_job *job); - -int nouveau_job_submit (struct nouveau_job *job);void nouveau_job_fini (struct nouveau_job *job); +int nouveau_job_submit (struct nouveau_job *job); +void nouveau_job_done (struct nouveau_job *job); +void nouveau_job_free (struct nouveau_job *job); -#define to_nouveau_sched_entity (entity) \ - container_of((entity), struct nouveau_sched_entity, base) - -struct nouveau_sched_entity { - struct drm_sched_entity base; +struct nouveau_sched { + struct drm_gpu_scheduler base; + struct drm_sched_entity entity; + struct workqueue_struct *wq; struct mutex mutex; - struct workqueue_struct *sched_wq; - struct { struct { struct list_head head;
@@ -113,15 +111,8 @@ struct nouveau_sched_entity { } job; }; -int nouveau_sched_entity_init (struct nouveau_sched_entity *entity, -struct drm_gpu_scheduler *sched, -struct workqueue_struct *sched_wq); -void nouveau_sched_entity_fini (struct nouveau_sched_entity *entity); - -bool nouveau_sched_entity_qwork (struct nouveau_sched_entity *entity, -struct work_struct *work); - -int nouveau_sched_init (struct nouveau_drm *drm); -void nouveau_sched_fini (struct nouveau_drm *drm); +int nouveau_sched_init (struct nouveau_sched *sched,struct nouveau_drm *drm, +struct workqueue_struct *wq); +void nouveau_sched_fini (struct nouveau_sched *sched);#endif
drivers/gpu/drm/nouveau/nouveau_uvmm.c +21 -48
159 lines not on this page · raw message
drivers/gpu/drm/nouveau/nouveau_uvmm.h +1 -3
@@ -44,8 +44,6 @@ struct nouveau_uvmm_bind_job {struct nouveau_job base;struct kref kref; -struct list_head entry; -struct work_struct work;struct completion complete;/* struct bind_job_op */
@@ -54,7 +52,7 @@ struct nouveau_uvmm_bind_job {struct nouveau_uvmm_bind_job_args {struct drm_file *file_priv; -struct nouveau_sched_entity *sched_entity; +struct nouveau_sched *sched;unsigned int flags;
base-commit: 50c1a36f594bb3dd33f3f9386c5d960cd12327d8
Signature · 1 line
2.41.0
PATCH 2/2 drm/nouveau: enable dynamic job-flow control
Make use of the scheduler's credit limit and scheduler job's credit count to account for the actual size of a job, such that we fill up the ring efficiently.
--- drivers/gpu/drm/nouveau/nouveau_abi16.c | 3 ++- drivers/gpu/drm/nouveau/nouveau_drm.c | 2 +- drivers/gpu/drm/nouveau/nouveau_exec.c | 4 +++- drivers/gpu/drm/nouveau/nouveau_sched.c | 9 ++++----- drivers/gpu/drm/nouveau/nouveau_sched.h | 3 ++- drivers/gpu/drm/nouveau/nouveau_uvmm.c | 4 +++- 6 files changed, 15 insertions(+), 10 deletions(-)
drivers/gpu/drm/nouveau/nouveau_abi16.c +2 -1
@@ -316,7 +316,8 @@ nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)if (ret) goto done; - ret =nouveau_sched_init (&chan->sched, drm, drm->sched_wq); + ret =nouveau_sched_init (&chan->sched, drm, drm->sched_wq, + chan->chan->dma.ib_max);if (ret) goto done;
drivers/gpu/drm/nouveau/nouveau_drm.c +1 -1
@@ -320,7 +320,7 @@ nouveau_cli_init(struct nouveau_drm *drm, const char *sname,* locks which indirectly or directly are held for allocations * elsewhere. */ - ret =nouveau_sched_init (&cli->sched, drm,NULL ); + ret =nouveau_sched_init (&cli->sched, drm,NULL ,1 );if (ret) goto done;
drivers/gpu/drm/nouveau/nouveau_exec.c +3 -1
@@ -258,10 +258,12 @@ nouveau_exec_job_init(struct nouveau_exec_job **pjob, } } + args.file_priv = __args->file_priv; job->chan = __args->chan; args.sched = __args->sched; - args.file_priv = __args->file_priv; +/* Plus one to account for the HW fence. */ + args.credits = job->push.count +1 ; args.in_sync.count = __args->in_sync.count; args.in_sync.s = __args->in_sync.s;
drivers/gpu/drm/nouveau/nouveau_sched.c +4 -5
@@ -12,7 +12,6 @@#include "nouveau_abi16.h" #include "nouveau_sched.h" -#define NOUVEAU_SCHED_HW_SUBMISSIONS 1#define NOUVEAU_SCHED_JOB_TIMEOUT_MS 10000/* Starts at 0, since the DRM scheduler interprets those parameters as (initial)
@@ -85,10 +84,10 @@ nouveau_job_init(struct nouveau_job *job, ret = -ENOMEM ; goto err_free_objs; } - } - ret =drm_sched_job_init (&job->base, &sched->entity,1 ,NULL ); + ret =drm_sched_job_init (&job->base, &sched->entity, + args->credits,NULL );if (ret) goto err_free_chains;
@@ -396,7 +395,7 @@ static const struct drm_sched_backend_ops nouveau_sched_ops = {int nouveau_sched_init (struct nouveau_sched *sched,struct nouveau_drm *drm, -struct workqueue_struct *wq) +struct workqueue_struct *wq,u32 credit_limit) {struct drm_gpu_scheduler *drm_sched = &sched->base;struct drm_sched_entity *entity = &sched->entity;
@@ -414,7 +413,7 @@ nouveau_sched_init(struct nouveau_sched *sched, struct nouveau_drm *drm, ret =drm_sched_init (drm_sched, &nouveau_sched_ops, wq,NOUVEAU_SCHED_PRIORITY_COUNT , -NOUVEAU_SCHED_HW_SUBMISSIONS ,0 , job_hang_limit, + credit_limit,0 , job_hang_limit,NULL ,NULL ,"nouveau_sched" , drm->dev->dev);if (ret) goto fail_wq;
drivers/gpu/drm/nouveau/nouveau_sched.h +2 -1
@@ -27,6 +27,7 @@ enum nouveau_job_state {struct nouveau_job_args {struct drm_file *file_priv;struct nouveau_sched *sched; +u32 credits;enum dma_resv_usage resv_usage;bool sync;
@@ -112,7 +113,7 @@ struct nouveau_sched { };int nouveau_sched_init (struct nouveau_sched *sched,struct nouveau_drm *drm, -struct workqueue_struct *wq); +struct workqueue_struct *wq,u32 credit_limit);void nouveau_sched_fini (struct nouveau_sched *sched);#endif
drivers/gpu/drm/nouveau/nouveau_uvmm.c +3 -1
@@ -1606,9 +1606,11 @@ nouveau_uvmm_bind_job_init(struct nouveau_uvmm_bind_job **pjob,init_completion (&job->complete); - args.sched = __args->sched; args.file_priv = __args->file_priv; + args.sched = __args->sched; + args.credits =1 ; + args.in_sync.count = __args->in_sync.count; args.in_sync.s = __args->in_sync.s;
Signature · 1 line
2.41.0
Re: [PATCH drm-misc-next 2/2] drm/nouveau: enable dynamic job-flow control
Quoted 4 lines · Danilo Krummrich
Make use of the scheduler's credit limit and scheduler job's credit count to account for the actual size of a job, such that we fill up the ring efficiently.
For the two:
Quoted 140 lines
Signed-off-by Danilo Krummrich <[email protected]>--- drivers/gpu/drm/nouveau/nouveau_abi16.c | 3 ++- drivers/gpu/drm/nouveau/nouveau_drm.c | 2 +- drivers/gpu/drm/nouveau/nouveau_exec.c | 4 +++- drivers/gpu/drm/nouveau/nouveau_sched.c | 9 ++++----- drivers/gpu/drm/nouveau/nouveau_sched.h | 3 ++- drivers/gpu/drm/nouveau/nouveau_uvmm.c | 4 +++- 6 files changed, 15 insertions(+), 10 deletions(-)drivers/gpu/drm/nouveau/nouveau_abi16.c +2
-1@@ -316,7 +316,8 @@ nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)if (ret) goto done; - ret =nouveau_sched_init (&chan->sched, drm, drm->sched_wq); + ret =nouveau_sched_init (&chan->sched, drm, drm->sched_wq, + chan->chan->dma.ib_max);if (ret) goto done;drivers/gpu/drm/nouveau/nouveau_drm.c +1
-1@@ -320,7 +320,7 @@ nouveau_cli_init(struct nouveau_drm *drm, const char *sname,* locks which indirectly or directly are held for allocations * elsewhere. */ - ret =nouveau_sched_init (&cli->sched, drm,NULL ); + ret =nouveau_sched_init (&cli->sched, drm,NULL ,1 );if (ret) goto done;drivers/gpu/drm/nouveau/nouveau_exec.c +3
-1@@ -258,10 +258,12 @@ nouveau_exec_job_init(struct nouveau_exec_job **pjob, } } + args.file_priv = __args->file_priv; job->chan = __args->chan; args.sched = __args->sched; - args.file_priv = __args->file_priv; +/* Plus one to account for the HW fence. */ + args.credits = job->push.count +1 ; args.in_sync.count = __args->in_sync.count; args.in_sync.s = __args->in_sync.s;drivers/gpu/drm/nouveau/nouveau_sched.c +4
-5@@ -12,7 +12,6 @@#include "nouveau_abi16.h" #include "nouveau_sched.h" -#define NOUVEAU_SCHED_HW_SUBMISSIONS 1#define NOUVEAU_SCHED_JOB_TIMEOUT_MS 10000/* Starts at 0, since the DRM scheduler interprets those parameters as (initial) @@ -85,10 +84,10 @@ nouveau_job_init(struct nouveau_job *job, ret = -ENOMEM ; goto err_free_objs; } - } - ret =drm_sched_job_init (&job->base, &sched->entity,1 ,NULL ); + ret =drm_sched_job_init (&job->base, &sched->entity, + args->credits,NULL );if (ret) goto err_free_chains;@@ -396,7 +395,7 @@ static const struct drm_sched_backend_ops nouveau_sched_ops = {int nouveau_sched_init (struct nouveau_sched *sched,struct nouveau_drm *drm, -struct workqueue_struct *wq) +struct workqueue_struct *wq,u32 credit_limit) {struct drm_gpu_scheduler *drm_sched = &sched->base;struct drm_sched_entity *entity = &sched->entity;@@ -414,7 +413,7 @@ nouveau_sched_init(struct nouveau_sched *sched, struct nouveau_drm *drm, ret =drm_sched_init (drm_sched, &nouveau_sched_ops, wq,NOUVEAU_SCHED_PRIORITY_COUNT , -NOUVEAU_SCHED_HW_SUBMISSIONS ,0 , job_hang_limit, + credit_limit,0 , job_hang_limit,NULL ,NULL ,"nouveau_sched" , drm->dev->dev);if (ret) goto fail_wq;drivers/gpu/drm/nouveau/nouveau_sched.h +2
-1@@ -27,6 +27,7 @@ enum nouveau_job_state {struct nouveau_job_args {struct drm_file *file_priv;struct nouveau_sched *sched; +u32 credits;enum dma_resv_usage resv_usage;bool sync;@@ -112,7 +113,7 @@ struct nouveau_sched { };int nouveau_sched_init (struct nouveau_sched *sched,struct nouveau_drm *drm, -struct workqueue_struct *wq); +struct workqueue_struct *wq,u32 credit_limit);void nouveau_sched_fini (struct nouveau_sched *sched);#endif drivers/gpu/drm/nouveau/nouveau_uvmm.c +3
-2@@ -1606,9 +1606,11 @@ nouveau_uvmm_bind_job_init(struct nouveau_uvmm_bind_job **pjob,init_completion (&job->complete); - args.sched = __args->sched; args.file_priv = __args->file_priv; + args.sched = __args->sched; + args.credits =1 ; + args.in_sync.count = __args->in_sync.count; args.in_sync.s = __args->in_sync.s; --2.41.0