mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Philipp Stanner <phasta@mailbox.org>
To: "Tvrtko Ursulin" <tursulin@ursulin.net>,
	"Philipp Stanner" <phasta@kernel.org>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Christian König" <ckoenig.leichtzumerken@gmail.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] drm/sched: Document racy behavior of drm_sched_entity_push_job()
Date: Wed, 12 Nov 2025 13:15:57 +0100	[thread overview]
Message-ID: <38bce31a7cdea31738c161bb06af272d5f68af1a.camel@mailbox.org> (raw)
In-Reply-To: <e9c02871-fa80-46c7-8b96-bad3a6a2c5b9@ursulin.net>

On Wed, 2025-11-12 at 09:42 +0000, Tvrtko Ursulin wrote:
> 
> On 12/11/2025 07:31, Philipp Stanner wrote:
> > drm_sched_entity_push_job() uses the unlocked spsc_queue. It takes a
> > reference to that queue's tip at the start, and some time later removes
> > that entry from that list, without locking or protection against
> > preemption.
> 
> I couldn't figure out what you refer to by tip reference at the start, 
> and later removes it. Are you talking about the top level view from 
> drm_sched_entity_push_job() or where exactly?
> > This is by design, since the spsc_queue demands single producer and
> > single consumer. It was, however, never documented.
> > 
> > Document that you must not call drm_sched_entity_push_job() in parallel
> > for the same entity.
> > 
> > Signed-off-by: Philipp Stanner <phasta@kernel.org>
> > ---
> >   drivers/gpu/drm/scheduler/sched_entity.c | 3 +++
> >   1 file changed, 3 insertions(+)
> > 
> > diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c
> > index 5a4697f636f2..b31e8d14aa20 100644
> > --- a/drivers/gpu/drm/scheduler/sched_entity.c
> > +++ b/drivers/gpu/drm/scheduler/sched_entity.c
> > @@ -562,6 +562,9 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
> >    * drm_sched_entity_push_job - Submit a job to the entity's job queue
> >    * @sched_job: job to submit
> >    *
> > + * It is illegal to call this function in parallel, at least for jobs belonging
> > + * to the same entity. Doing so leads to undefined behavior.
> 
> One thing that is documented in the very next paragraph is that the 
> design implies a lock held between arm and push. At least to ensure 
> seqno order matches the queue order.
> 
> I did not get what other breakage you found, but I also previously did 
> find something other than that. Hm.. if I could only remember what it
> was. Probably mine was something involving drm_sched_entity_select_rq(), 
> drm_sched_entity_modify_sched() and (theoretical) multi-threaded 
> userspace submit on the same entity. Luckily it seems no one does that.
> 
> The issue you found is separate and not theoretically fixed by this 
> hypothetical common lock held over arm and push?

Well, if 2 CPUs should ever run in parallel in
drm_sched_entity_push_job() the spsc_queue will just explode. Most
notably, one CPU could get the job at the tip (the oldest job), then be
preempted, and then another CPU takes the same job and pops it.

The API contract should be that the user doesn't have to know whether
there's a linked list or the magic spsc_queue.

The entire entity submission pattern is basically designed around
"single producer [per entity]", and that's not very well documented.

I don't think the common lock comment fully addresses that, because
it's merely about the sequence numbers. I think the common lock should
likely prevent any issues, but I feel more comfortable if the user gets
informed explicitly about the potential racyness of this function.

Maybe we can combine the two paragraphs.

P.

  reply	other threads:[~2025-11-12 12:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-12  7:31 Philipp Stanner
2025-11-12  9:42 ` Tvrtko Ursulin
2025-11-12 12:15   ` Philipp Stanner [this message]
2025-11-12 13:13     ` Tvrtko Ursulin
2025-11-12 13:31       ` Philipp Stanner
2025-11-12 13:38         ` Tvrtko Ursulin

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=38bce31a7cdea31738c161bb06af272d5f68af1a.camel@mailbox.org \
    --to=phasta@mailbox.org \
    --cc=ckoenig.leichtzumerken@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=matthew.brost@intel.com \
    --cc=phasta@kernel.org \
    --cc=tursulin@ursulin.net \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®