All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jiwoo Kim" <arpe1618@gmail.com>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [PATCH 2/2] drm/panfrost: Stop a job queued after its JM context was destroyed
Date: Sat, 14 Nov 2026 06:41:00 +0000	[thread overview]
Message-ID: <sashiko-outbox-214229@kernel.org> (raw)
In-Reply-To: <20261114062637.2447-2-arpe1618@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/panfrost: unreachable panfrost_stop_job() in
  panfrost_jm_ctx_destroy()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] drm/sched: null pointer dereference in
  drm_sched_entity_flush() [1]
--

commit 1e5a8eff6ae859bd583772ff4513448b2fcf6c55
Author: Jiwoo Kim <arpe1618@gmail.com>

drm/panfrost: Stop a job queued after its JM context was destroyed

This commit extracts a panfrost_stop_job() helper from
panfrost_jm_ctx_destroy() and calls it in panfrost_job_hw_submit() under
job_lock if the context was destroyed while the job was being submitted.

> diff --git a/drivers/gpu/drm/panfrost/panfrost_job.c b/drivers/gpu/drm/panfrost/panfrost_job.c
> index 7022b1b2e0206..fbfdee42a4ae6 100644
> --- a/drivers/gpu/drm/panfrost/panfrost_job.c
> +++ b/drivers/gpu/drm/panfrost/panfrost_job.c

[ ... ]

> @@ -272,6 +303,16 @@ static int panfrost_job_hw_submit(struct panfrost_job *job, int js)
>  			"JS: Submitting atom %p to js[%d][%d] with head=0x%llx AS %d",
>  			job, js, subslot, jc_head, cfg & 0xf);
>  	}
> +
> +	/*
> +	 * panfrost_jm_ctx_destroy() only stops the jobs it finds in
> +	 * pfdev->jobs[], so stop this one here if its context was destroyed
> +	 * while we were getting here, e.g. while waiting for the GPU to
> +	 * resume. Pairs with the WRITE_ONCE() in panfrost_jm_ctx_destroy().
> +	 */
> +	if (READ_ONCE(job->ctx->destroyed))
> +		panfrost_stop_job(pfdev, js, subslot, job);
> +
>  	spin_unlock(&pfdev->js->job_lock);
>  
>  	return 0;

[ ... ]

> @@ -1100,7 +1141,7 @@ int panfrost_jm_ctx_destroy(struct drm_file *file, u32 handle)
>  	if (!jm_ctx)
>  		return -EINVAL;
>  
> -	jm_ctx->destroyed = true;
> +	WRITE_ONCE(jm_ctx->destroyed, true);
>  
>  	/* Kill in-flight jobs */
>  	spin_lock(&pfdev->js->job_lock);
> @@ -1109,32 +1150,11 @@ int panfrost_jm_ctx_destroy(struct drm_file *file, u32 handle)
>  
>  		for (int j = ARRAY_SIZE(pfdev->jobs[0]) - 1; j >= 0; j--) {
>  			struct panfrost_job *job = pfdev->jobs[i][j];
> -			u32 cmd;
>  
>  			if (!job || job->base.entity != entity)
>  				continue;
>  
> -			if (j == 1) {
> -				/* Try to cancel the job before it starts */
> -				job_write(pfdev, JS_COMMAND_NEXT(i), JS_COMMAND_NOP);
> -				/* Reset the job head so it doesn't get restarted if
> -				 * the job in the first slot failed.
> -				 */
> -				job->jc = 0;
> -			}
> -
> -			if (panfrost_has_hw_feature(pfdev, HW_FEATURE_JOBCHAIN_DISAMBIGUATION)) {
> -				cmd = panfrost_get_job_chain_flag(job) ?
> -				      JS_COMMAND_HARD_STOP_1 :
> -				      JS_COMMAND_HARD_STOP_0;
> -			} else {
> -				cmd = JS_COMMAND_HARD_STOP;
> -			}
> -
> -			job_write(pfdev, JS_COMMAND(i), cmd);
> -
> -			/* Jobs can outlive their file context */
> -			job->engine_usage = NULL;
> +			panfrost_stop_job(pfdev, i, j, job);
>  		}
>  	}
>  	spin_unlock(&pfdev->js->job_lock);

[Severity: High]
Can panfrost_jm_ctx_destroy() ever reach panfrost_stop_job() when checking
job->base.entity != entity?

Before calling sched->ops->run_job() (panfrost_job_run()),
drm_sched_run_job_work() pops the job via drm_sched_entity_pop_job(), which
clears sched_job->entity:

drivers/gpu/drm/scheduler/sched_entity.c:drm_sched_entity_pop_job() {
    ...
	sched_job->entity = NULL;
	return sched_job;
}

Every job in pfdev->jobs[i][j] therefore has job->base.entity == NULL, so
job->base.entity != entity is always true and panfrost_jm_ctx_destroy()
never stops any already-queued job or clears its job->engine_usage, leaving
panfrost_dequeue_job() to write to freed panfrost_file_priv memory after
panfrost_postclose() when profiling is enabled.  Matching jobs by
job->ctx != jm_ctx instead of job->base.entity != entity would allow
panfrost_jm_ctx_destroy() to find the context's in-flight jobs.

[Severity: Critical]
This problem wasn't introduced by this patch, but can
panfrost_jm_ctx_release() trigger a NULL pointer dereference in
drm_sched_entity_flush() [1] when context creation fails?

In panfrost_jm_ctx_create(), if jm_ctx_prio_to_drm_sched_prio() or
drm_sched_entity_init() fails, err_put_jm_ctx calls panfrost_jm_ctx_put()
-> panfrost_jm_ctx_release(), which runs drm_sched_entity_destroy() on
every element of jm_ctx->slot_entity[]:

drivers/gpu/drm/panfrost/panfrost_job.c:panfrost_jm_ctx_release() {
    ...
	for (u32 i = 0; i < ARRAY_SIZE(jm_ctx->slot_entity); i++)
		drm_sched_entity_destroy(&jm_ctx->slot_entity[i]);
}

For any slot_entity where drm_sched_entity_init() failed or was not yet
reached, entity->rq is NULL, and drm_sched_entity_destroy() ->
drm_sched_entity_flush() unconditionally dereferences entity->rq->sched.

[1] https://sashiko.dev/#/bug/linux-1f36c24e-a878-442e-991c-1b387017bee6

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261114062637.2447-1-arpe1618@gmail.com?part=2

      reply	other threads:[~2026-11-14  6:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-14  6:26 [PATCH 1/2] drm/panfrost: Hold a reference to the MMU context of a job Jiwoo Kim
2026-11-14  6:26 ` [PATCH 2/2] drm/panfrost: Stop a job queued after its JM context was destroyed Jiwoo Kim
2026-11-14  6:41   ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-214229@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=arpe1618@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.