All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marco Pagani <marco.pagani@linux.dev>
To: Tvrtko Ursulin <tursulin@ursulin.net>
Cc: "Matthew Brost" <matthew.brost@intel.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Philipp Stanner" <phasta@kernel.org>,
	"Christian König" <ckoenig.leichtzumerken@gmail.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH] drm/sched/tests: Let the DRM scheduler manage job lifetimes
Date: Tue, 21 Jul 2026 13:55:22 +0200	[thread overview]
Message-ID: <c3a56e7e-1599-416e-a76d-2143656b5cf4@linux.dev> (raw)
In-Reply-To: <4dd5a686-f935-41a3-89d2-0f651061545d@ursulin.net>



On 16/07/2026 14:14, Tvrtko Ursulin wrote:
> 
> 
> On 15/07/2026 17:02, Marco Pagani wrote:
> 
> 8<>
> 
>>> I first want to understand exactly what the issue is. The current design
>>> exactly cannot free the memory under the scheduler nose unless there is
>>> a test bug where test exited and has left the scheduler with unprocessed
>>> jobs. I am not saying we cannot have reference counted jobs but, again,
>>> at the moment I don't understand the problem.
>>
>> I think that the current design of the mock scheduler can indeed free
>> test objects' memory under the scheduler backend's nose. Consider the
>> example discussed above: the mock scheduler is instantiated and released
>> in the .init and .exit functions respectively, while jobs are allocated
>> in the test body using drm_mock_sched_job_new(). In this rather basic
>> use case, jobs' memory is freed before the entities and the scheduler
>> are released in the .exit function.
> 
> So at first I thought that as long as entity is freed at each test exit, 
> which includes idling, and is done by all current test cases (apart from 
> the parallel one where you did it via actions) it is all fine. The 
> entity->last_scheduled you mention is not a concern then since it is 
> cleared etc.
> 
> However, now I think that even if entity is idled and freed before test 
> exit that does nothing for the jobs not yet processed by the 
> drm_sched_free_job_work()-er.
> 
> Even though I was testing with KASAN I possibly did not manage to hit 
> that race. Strange but I guess possible.

It was also a surprise for me when Philipp reported a suspected UAF.
Definitely a subtle bug to catch. I managed to reproduce it by building
the kernel with KASAN and kunit.autorun enabled, and then simply booting
it with no rootfs using qemu with this slightly odd configuration:

qemu-system-x86_64 -enable-kvm -m 48G -smp cpus=18 -kernel [...]

> Unless I am missing something with the above analysis, one options is 
> along the lines of what you propose - forgo using kunit managed 
> allocations for anything mock scheduler owned. Second would be to stop 
> using test suite init/exit for the scheduler management and just make 
> each test create own scheduler and free it as it exits.

I think the second approach is still fragile. If we move
drm_mock_sched_fini() at the end of each test body, any KUNIT_ASSERT_*()
failure will cause the test to abort immediately, skipping the teardown
and leaving the scheduler in an inconsistent state that could cause any
sort of problem.

Moreover, initializing/tearing down the scheduler in the test's
init/exit functions is the most intuitive and possibly the best approach
(DRY) from a test code perspective. Asking test writers to not use the
standard init/exit functions and enforcing this limitation is a burden
that I think no one deserves to take.

> Regardless of the option, as long as it is safe against UAFs we are I 
> think good. Memory leaks on test failures are not that interesting that 
> it would warrant coming up with anything more complicated.

> Does that make sense? If so, question is which option is simpler? 
> Probably just making tests create and destroy schedulers since then 
> there is no need to deal with any memory freeing. It would be just two 
> lines added to a subset of tests, the ones which rely on .init/.exit.

I thought so too when developing the drm_gem_shmem_test some time ago,
but then I changed my mind after dealing with downstream CI. A single
test leaking some memory is not optimal, but not even a big deal per se.
However, consider hundreds of KUnit tests running periodically on a CI.
If all failing tests did not care about leaking memory, they could
seriously bog down the system. I think it's responsible to have tests
that do not leak memory when they fail.

I'll see if I can come up with a cleaner version of the kref approach,
taking into account your recommendations and Sashiko's bug report.

Thank you for your suggestions,
Marco
 
> Regards,
> 
> Tvrtko
> 


      reply	other threads:[~2026-07-21 11:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07 11:48 [RFC PATCH] drm/sched/tests: Let the DRM scheduler manage job lifetimes Marco Pagani
2026-07-07 12:05 ` sashiko-bot
2026-07-08  9:20 ` Tvrtko Ursulin
2026-07-09 21:53   ` Marco Pagani
2026-07-10 13:20     ` Tvrtko Ursulin
2026-07-15 16:02       ` Marco Pagani
2026-07-16 12:14         ` Tvrtko Ursulin
2026-07-21 11:55           ` Marco Pagani [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=c3a56e7e-1599-416e-a76d-2143656b5cf4@linux.dev \
    --to=marco.pagani@linux.dev \
    --cc=airlied@gmail.com \
    --cc=ckoenig.leichtzumerken@gmail.com \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=matthew.brost@intel.com \
    --cc=mripard@kernel.org \
    --cc=phasta@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tursulin@ursulin.net \
    --cc=tzimmermann@suse.de \
    /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.