drm/nouveau: nouveau_sched_fini() should check for init failure
Subsystems (3)
PATCH 1/1 drm/nouveau: nouveau_sched_fini() should check for init failure
If initialization fails, Nouveau can still call the _fini() function to clean up, with the expectation that the function can handle if its corresponding _init() function was never called or exited with error.
Such is not the case with nouveau_sched_fini(), which still attempts to wait for jobs to finish even if the underlying data structures were never initialized.
--- drivers/gpu/drm/nouveau/nouveau_sched.c | 5 +++++ drivers/gpu/drm/nouveau/nouveau_sched.h | 2 ++ 2 files changed, 7 insertions(+)
drivers/gpu/drm/nouveau/nouveau_sched.c +5 -0
@@ -443,6 +443,8 @@ nouveau_sched_init(struct nouveau_sched *sched, struct nouveau_drm *drm,INIT_LIST_HEAD (&sched->job.list.head);init_waitqueue_head (&sched->job.wq); + sched->initialized = true; +return 0 ; fail_sched:
@@ -459,6 +461,9 @@ nouveau_sched_fini(struct nouveau_sched *sched)struct drm_gpu_scheduler *drm_sched = &sched->base;struct drm_sched_entity *entity = &sched->entity; +if (!sched->initialized) +return ; +rmb ();/* for list_empty to work without lock */ wait_event (sched->job.wq,list_empty (&sched->job.list.head));
drivers/gpu/drm/nouveau/nouveau_sched.h +2 -0
@@ -109,6 +109,8 @@ struct nouveau_sched { } list;struct wait_queue_head wq; } job; + +bool initialized; }; int nouveau_sched_init(struct nouveau_sched *sched, struct nouveau_drm *drm,
Signature · 1 line
2.34.1
Quoted 7 lines · Timur Tabi
If initialization fails, Nouveau can still call the
_fini()function to clean up, with the expectation that the function can handle if its corresponding_init()function was never called or exited with error.Such is not the case with
nouveau_sched_fini(), which still attempts to wait for jobs to finish even if the underlying data structures were never initialized.
Good catch!
Quoted 13 lines
Fixes 5f03a507b29e ("drm/nouveau: implement 1:1 scheduler - entity relationship")Signed-off-by Timur Tabi <[email protected]>--- drivers/gpu/drm/nouveau/nouveau_sched.c | 5 +++++ drivers/gpu/drm/nouveau/nouveau_sched.h | 2 ++ 2 files changed, 7 insertions(+)drivers/gpu/drm/nouveau/nouveau_sched.c +0
-0
drivers/gpu/drm/nouveau/nouveau_sched.c nouveau_sched_init(struct nouveau_sched *sched, struct nouveau_drm *drm, · line 446
Quoted 4 lines
drivers/gpu/drm/nouveau/nouveau_sched.c +1
-0@@ -443,6 +443,8 @@ nouveau_sched_init(struct nouveau_sched *sched, struct nouveau_drm *drm,INIT_LIST_HEAD (&sched->job.list.head);init_waitqueue_head (&sched->job.wq); + sched->initialized = true;
I wonder if we should allocate struct nouveau_sched dynamically instead and just check for NULL in the corresponding *_fini() functions.
Actually, in nouveau_abi16_ioctl_channel_alloc() we can omit creating a scheduler instance entirely if !nouveau_cli_uvmm().
- Danilo
Quoted 27 lines
drivers/gpu/drm/nouveau/nouveau_sched.c +4
-0+return 0 ; fail_sched:@@ -459,6 +461,9 @@ nouveau_sched_fini(struct nouveau_sched *sched)struct drm_gpu_scheduler *drm_sched = &sched->base;struct drm_sched_entity *entity = &sched->entity; +if (!sched->initialized) +return ; +rmb ();/* for list_empty to work without lock */ wait_event (sched->job.wq,list_empty (&sched->job.list.head));drivers/gpu/drm/nouveau/nouveau_sched.h +2
-0@@ -109,6 +109,8 @@ struct nouveau_sched { } list;struct wait_queue_head wq; } job; + +bool initialized; }; int nouveau_sched_init(struct nouveau_sched *sched, struct nouveau_drm *drm,
Quoted 7 lines · Danilo Krummrich
I wonder if we should allocate struct
nouveau_scheddynamically instead and just
check for NULL in the corresponding*_fini()functions.Actually, in
nouveau_abi16_ioctl_channel_alloc()we can omit creating a scheduler
instance entirely if !nouveau_cli_uvmm().
I am all for a better solution if you can think of one. I don't really like the idea of "bool initialized", but I couldn't think of a more reliable test.
Quoted 11 lines · Timur Tabi
Quoted 7 lines · Danilo Krummrich
I wonder if we should allocate struct
nouveau_scheddynamically instead and just
check for NULL in the corresponding*_fini()functions.Actually, in
nouveau_abi16_ioctl_channel_alloc()we can omit creating a scheduler
instance entirely if !nouveau_cli_uvmm().I am all for a better solution if you can think of one. I don't really like the idea of "bool initialized", but I couldn't think of a more reliable test.
Yeah, it's not really ideal that there is a single error path calling the corresponding *_fini() in nouveau_abi16_ioctl_channel_alloc() and nouveau_cli_init() only. Maybe this can be re-worked to only reverse what has actually been done already.
However, I'd probably not do that for -fixes. Maybe we should go with the dynamic allocation and a NULL check for now.
Do you want to send another patch? Otherwise I can also come up with something and add your Reported-by.
- Danilo
Quoted 2 lines
On Thu, 2024-02-01 at 17:35 +0100, Danilo Krummrich wrote:
Do you want to send another patch? Otherwise I can also come up with something and add your Reported-by.
I think it's best if you do it, since I'm not at all familiar with the abi16 stuff.
I'm able to repro the problem easily by just having r535_gsp_wpr_meta_init() return an error code instead of 0.