Skip to content

Commit 07c2a41

Browse files
committed
drm/v3d: alloc and init job in one shot
Move job memory allocation to v3d_job_init function. This aim to facilitate error handling in job initialization, since cleanup steps are similar for all (struct v3d_job)-based types of job involved in a command submission. To generalize v3d_job_init(), this change takes into account that all job structs have the first element a struct v3d_job (bin, render, tfu, csd) or it is a v3d_job itself (clean_job) for pointer casting. v3: - explicitly init job as NULL (Iago) - fix pm failure handling on v3_job_init (Iago) Suggested-by: Iago Toral Quiroga <itoral@igalia.com> Signed-off-by: Melissa Wen <mwen@igalia.com> Reviewed-by: Iago Toral Quiroga <itoral@igalia.com> Signed-off-by: Melissa Wen <melissa.srw@gmail.com> Link: https://patchwork.freedesktop.org/patch/msgid/4d12e07bd334d2cddb51cabd359e99edde595619.1633016479.git.mwen@igalia.com
1 parent 223583d commit 07c2a41

1 file changed

Lines changed: 53 additions & 82 deletions

File tree

‎drivers/gpu/drm/v3d/v3d_gem.c‎

Lines changed: 53 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ v3d_render_job_free(struct kref *ref)
392392

393393
void v3d_job_cleanup(struct v3d_job *job)
394394
{
395+
if (!job)
396+
return;
397+
395398
drm_sched_job_cleanup(&job->base);
396399
v3d_job_put(job);
397400
}
@@ -450,35 +453,48 @@ v3d_job_add_deps(struct drm_file *file_priv, struct v3d_job *job,
450453

451454
static int
452455
v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv,
453-
struct v3d_job *job, void (*free)(struct kref *ref),
456+
void **container, size_t size, void (*free)(struct kref *ref),
454457
u32 in_sync, enum v3d_queue queue)
455458
{
456459
struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
460+
struct v3d_job *job;
457461
int ret;
458462

463+
*container = kcalloc(1, size, GFP_KERNEL);
464+
if (!*container) {
465+
DRM_ERROR("Cannot allocate memory for v3d job.");
466+
return -ENOMEM;
467+
}
468+
469+
job = *container;
459470
job->v3d = v3d;
460471
job->free = free;
461472

462473
ret = pm_runtime_get_sync(v3d->drm.dev);
463474
if (ret < 0)
464-
return ret;
475+
goto fail;
465476

466477
ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue],
467478
v3d_priv);
468479
if (ret)
469-
goto fail;
480+
goto fail_job;
470481

471482
ret = v3d_job_add_deps(file_priv, job, in_sync, 0);
472483
if (ret)
473-
goto fail_job;
484+
goto fail_deps;
474485

475486
kref_init(&job->refcount);
476487

477488
return 0;
478-
fail_job:
489+
490+
fail_deps:
479491
drm_sched_job_cleanup(&job->base);
480-
fail:
492+
fail_job:
481493
pm_runtime_put_autosuspend(v3d->drm.dev);
494+
fail:
495+
kfree(*container);
496+
*container = NULL;
497+
482498
return ret;
483499
}
484500

@@ -541,7 +557,7 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
541557
struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
542558
struct drm_v3d_submit_cl *args = data;
543559
struct v3d_bin_job *bin = NULL;
544-
struct v3d_render_job *render;
560+
struct v3d_render_job *render = NULL;
545561
struct v3d_job *clean_job = NULL;
546562
struct v3d_job *last_job;
547563
struct ww_acquire_ctx acquire_ctx;
@@ -558,35 +574,20 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
558574
return -EINVAL;
559575
}
560576

561-
render = kcalloc(1, sizeof(*render), GFP_KERNEL);
562-
if (!render)
563-
return -ENOMEM;
577+
ret = v3d_job_init(v3d, file_priv, (void *)&render, sizeof(*render),
578+
v3d_render_job_free, args->in_sync_rcl, V3D_RENDER);
579+
if (ret)
580+
goto fail;
564581

565582
render->start = args->rcl_start;
566583
render->end = args->rcl_end;
567584
INIT_LIST_HEAD(&render->unref_list);
568585

569-
ret = v3d_job_init(v3d, file_priv, &render->base,
570-
v3d_render_job_free, args->in_sync_rcl, V3D_RENDER);
571-
if (ret) {
572-
kfree(render);
573-
return ret;
574-
}
575-
576586
if (args->bcl_start != args->bcl_end) {
577-
bin = kcalloc(1, sizeof(*bin), GFP_KERNEL);
578-
if (!bin) {
579-
v3d_job_cleanup(&render->base);
580-
return -ENOMEM;
581-
}
582-
583-
ret = v3d_job_init(v3d, file_priv, &bin->base,
587+
ret = v3d_job_init(v3d, file_priv, (void *)&bin, sizeof(*bin),
584588
v3d_job_free, args->in_sync_bcl, V3D_BIN);
585-
if (ret) {
586-
v3d_job_cleanup(&render->base);
587-
kfree(bin);
588-
return ret;
589-
}
589+
if (ret)
590+
goto fail;
590591

591592
bin->start = args->bcl_start;
592593
bin->end = args->bcl_end;
@@ -597,18 +598,10 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
597598
}
598599

599600
if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) {
600-
clean_job = kcalloc(1, sizeof(*clean_job), GFP_KERNEL);
601-
if (!clean_job) {
602-
ret = -ENOMEM;
603-
goto fail;
604-
}
605-
606-
ret = v3d_job_init(v3d, file_priv, clean_job, v3d_job_free, 0, V3D_CACHE_CLEAN);
607-
if (ret) {
608-
kfree(clean_job);
609-
clean_job = NULL;
601+
ret = v3d_job_init(v3d, file_priv, (void *)&clean_job, sizeof(*clean_job),
602+
v3d_job_free, 0, V3D_CACHE_CLEAN);
603+
if (ret)
610604
goto fail;
611-
}
612605

613606
last_job = clean_job;
614607
} else {
@@ -681,11 +674,9 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
681674
drm_gem_unlock_reservations(last_job->bo,
682675
last_job->bo_count, &acquire_ctx);
683676
fail:
684-
if (bin)
685-
v3d_job_cleanup(&bin->base);
686-
v3d_job_cleanup(&render->base);
687-
if (clean_job)
688-
v3d_job_cleanup(clean_job);
677+
v3d_job_cleanup((void *)bin);
678+
v3d_job_cleanup((void *)render);
679+
v3d_job_cleanup(clean_job);
689680

690681
return ret;
691682
}
@@ -705,28 +696,22 @@ v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
705696
{
706697
struct v3d_dev *v3d = to_v3d_dev(dev);
707698
struct drm_v3d_submit_tfu *args = data;
708-
struct v3d_tfu_job *job;
699+
struct v3d_tfu_job *job = NULL;
709700
struct ww_acquire_ctx acquire_ctx;
710701
int ret = 0;
711702

712703
trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
713704

714-
job = kcalloc(1, sizeof(*job), GFP_KERNEL);
715-
if (!job)
716-
return -ENOMEM;
717-
718-
ret = v3d_job_init(v3d, file_priv, &job->base,
705+
ret = v3d_job_init(v3d, file_priv, (void *)&job, sizeof(*job),
719706
v3d_job_free, args->in_sync, V3D_TFU);
720-
if (ret) {
721-
kfree(job);
722-
return ret;
723-
}
707+
if (ret)
708+
goto fail;
724709

725710
job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles),
726711
sizeof(*job->base.bo), GFP_KERNEL);
727712
if (!job->base.bo) {
728-
v3d_job_cleanup(&job->base);
729-
return -ENOMEM;
713+
ret = -ENOMEM;
714+
goto fail;
730715
}
731716

732717
job->args = *args;
@@ -773,7 +758,7 @@ v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
773758
return 0;
774759

775760
fail:
776-
v3d_job_cleanup(&job->base);
761+
v3d_job_cleanup((void *)job);
777762

778763
return ret;
779764
}
@@ -794,8 +779,8 @@ v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
794779
struct v3d_dev *v3d = to_v3d_dev(dev);
795780
struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
796781
struct drm_v3d_submit_csd *args = data;
797-
struct v3d_csd_job *job;
798-
struct v3d_job *clean_job;
782+
struct v3d_csd_job *job = NULL;
783+
struct v3d_job *clean_job = NULL;
799784
struct ww_acquire_ctx acquire_ctx;
800785
int ret;
801786

@@ -806,29 +791,15 @@ v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
806791
return -EINVAL;
807792
}
808793

809-
job = kcalloc(1, sizeof(*job), GFP_KERNEL);
810-
if (!job)
811-
return -ENOMEM;
812-
813-
ret = v3d_job_init(v3d, file_priv, &job->base,
794+
ret = v3d_job_init(v3d, file_priv, (void *)&job, sizeof(*job),
814795
v3d_job_free, args->in_sync, V3D_CSD);
815-
if (ret) {
816-
kfree(job);
817-
return ret;
818-
}
819-
820-
clean_job = kcalloc(1, sizeof(*clean_job), GFP_KERNEL);
821-
if (!clean_job) {
822-
v3d_job_cleanup(&job->base);
823-
return -ENOMEM;
824-
}
796+
if (ret)
797+
goto fail;
825798

826-
ret = v3d_job_init(v3d, file_priv, clean_job, v3d_job_free, 0, V3D_CACHE_CLEAN);
827-
if (ret) {
828-
v3d_job_cleanup(&job->base);
829-
kfree(clean_job);
830-
return ret;
831-
}
799+
ret = v3d_job_init(v3d, file_priv, (void *)&clean_job, sizeof(*clean_job),
800+
v3d_job_free, 0, V3D_CACHE_CLEAN);
801+
if (ret)
802+
goto fail;
832803

833804
job->args = *args;
834805

@@ -877,7 +848,7 @@ v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
877848
drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
878849
&acquire_ctx);
879850
fail:
880-
v3d_job_cleanup(&job->base);
851+
v3d_job_cleanup((void *)job);
881852
v3d_job_cleanup(clean_job);
882853

883854
return ret;

0 commit comments

Comments
 (0)