All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Thomas Hellström" <thomas.hellstrom@linux.intel.com>
Cc: tj@kernel.org, hannes@cmpxchg.org,
	dri-devel@lists.freedesktop.org, mkoutny@suse.com
Subject: Re: [PATCH v8 6/6] drm/amdgpu: Wire up dmem cgroup reclaim for VRAM manager
Date: Thu, 23 Jul 2026 10:50:12 +0000	[thread overview]
Message-ID: <20260723105013.441ED1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260723100350.16895-7-thomas.hellstrom@linux.intel.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Memory Leak and Use-After-Free of cgroup region and gpu_buddy on probe failure
- [High] Permanent memory charge leak on VRAM eviction failure
- [Medium] Commit Message and Implementation Mismatch regarding cgroup region registration
--

--- Patch [6]: [PATCH v8 6/6] drm/amdgpu: Wire up dmem cgroup reclaim for VRAM manager ---
commit 9102e928a842cf958ce390fd437331e21fe61836
Author: Thomas Hellström <thomas.hellstrom@linux.intel.com>

drm/amdgpu: Wire up dmem cgroup reclaim for VRAM manager

Register the VRAM manager with the dmem cgroup reclaim infrastructure
so that lowering dmem.max below current VRAM usage triggers TTM
eviction rather than failing with -EBUSY.

> Use drmm_cgroup_register_region() so that the region is automatically
> unregistered at DRM device release, after drm_dev_unplug() has already
> made drm_dev_enter() return false.

[Severity: Medium]
Is this description still accurate? The commit message claims that
drmm_cgroup_register_region() is used to automatically tie the cgroup
region's lifecycle to the DRM device release, but the code implements
manual registration using dmem_cgroup_register_region().

The v8 changelog at the bottom accurately describes why this manual approach
was chosen, but the main body of the commit message appears to still have
the older description.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c

[ ... ]

> @@ -917,6 +939,7 @@ int amdgpu_vram_mgr_init(struct amdgpu_device *adev)
>  {
>  	struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
>  	struct ttm_resource_manager *man = &mgr->manager;
> +	struct dmem_cgroup_region *cg;
>  	int err;
>  
>  	ttm_resource_manager_init(man, &adev->mman.bdev,
> @@ -933,12 +956,17 @@ int amdgpu_vram_mgr_init(struct amdgpu_device *adev)
>  	if (err)
>  		return err;
>  
> -	man->cg = drmm_cgroup_register_region(adev_to_drm(adev), "vram",
> -					      &(struct dmem_cgroup_init){
> +	cg = dmem_cgroup_register_region(&(struct dmem_cgroup_init){
>  						.size = adev->gmc.real_vram_size,
> -					      });
> -	if (IS_ERR(man->cg))
> -		return PTR_ERR(man->cg);
> +						.ops = &amdgpu_vram_mgr_dmem_ops,
> +						.reclaim_priv = man,
> +					 },
> +					 "drm/%s/vram", adev_to_drm(adev)->unique);
> +	if (IS_ERR(cg))
> +		return PTR_ERR(cg);

[Severity: High]
Does this early return leak the gpu_buddy allocator? It looks like
gpu_buddy_init() succeeds just before this, but gpu_buddy_fini() is not
called if dmem_cgroup_register_region() fails.

Also, if amdgpu_vram_mgr_init() succeeds but a subsequent step in the GMC
block's sw_init fails:

amdgpu_device_ip_init() {
    r = adev->ip_blocks[i].version->funcs->sw_init(&adev->ip_blocks[i]);
    if (r) {
        goto init_failed;
    }
    adev->ip_blocks[i].status.sw = true;
}

The status.sw flag is not set, causing amdgpu_device_ip_fini() to skip
sw_fini and bypass all cleanup. This would permanently leak the gpu_buddy
structures and the registered cgroup region.

Because the cgroup region remains active and its reclaim_priv points to the
manager inside the freed amdgpu_device structure, could a subsequent cgroup
memory pressure event trigger a use-after-free on the dangling pointer?

> +
> +	mgr->cg_region = cg;
> +	ttm_resource_manager_set_dmem_region(man, cg);
>  
>  	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, &mgr->manager);
>  	ttm_resource_manager_set_used(man, true);

[ ... ]

> @@ -963,6 +991,19 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev)
>  	ttm_resource_manager_set_used(man, false);
>  
>  	ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
> +
> +	/*
> +	 * Unregister the dmem cgroup region regardless of the evict_all()
> +	 * result and before any further teardown.  This drains in-flight
> +	 * reclaim callbacks and blocks new ones, so no reclaim can reference
> +	 * the manager once we start freeing it.  It must run after evict_all()
> +	 * so that ttm_resource_free() can still uncharge via man->cg during
> +	 * eviction.  Clear man->cg afterwards.
> +	 */
> +	dmem_cgroup_unregister_region(mgr->cg_region);
> +	mgr->cg_region = NULL;
> +	ttm_resource_manager_set_dmem_region(man, NULL);
> +
>  	if (ret)
>  		return;

[Severity: High]
Does this cause a permanent memory charge leak if eviction fails?

The unregistration happens and man->cg is cleared before returning early
on eviction failure. If eviction fails, buffer objects are left alive in
VRAM.

When these buffer objects are eventually freed, ttm_resource_free() relies
on man->cg being non-NULL to know whether it should call
dmem_cgroup_uncharge():

ttm_resource_free() {
    if (man->cg)
        dmem_cgroup_uncharge(pool, bo->base.size);
}

Since man->cg was already cleared, the uncharge is skipped, which appears
to leak the cgroup charge and its css reference permanently.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260723100350.16895-1-thomas.hellstrom@linux.intel.com?part=6

  reply	other threads:[~2026-07-23 10:50 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-23 10:03 [PATCH v8 0/6] Add reclaim to the dmem cgroup controller Thomas Hellström
2026-07-23 10:03 ` [PATCH v8 1/6] drm/amdgpu: Fix init ordering in amdgpu_vram_mgr_init() Thomas Hellström
2026-07-23 10:23   ` sashiko-bot
2026-07-23 10:03 ` [PATCH v8 2/6] cgroup/dmem: Introduce struct dmem_cgroup_init for region initialization Thomas Hellström
2026-07-23 10:03 ` [PATCH v8 3/6] cgroup/dmem: Add reclaim callback for lowering max below current usage Thomas Hellström
2026-07-23 10:21   ` sashiko-bot
2026-07-23 10:03 ` [PATCH v8 4/6] drm/ttm: Hook up a cgroup-aware reclaim callback for the dmem controller Thomas Hellström
2026-07-23 10:31   ` sashiko-bot
2026-07-23 12:02   ` Maarten Lankhorst
2026-07-23 15:55     ` Thomas Hellström
2026-07-23 10:03 ` [PATCH v8 5/6] drm/xe: Wire up dmem cgroup reclaim for VRAM manager Thomas Hellström
2026-07-23 10:03 ` [PATCH v8 6/6] drm/amdgpu: " Thomas Hellström
2026-07-23 10:50   ` sashiko-bot [this message]
2026-07-23 11:47 ` ✗ CI.checkpatch: warning for Add reclaim to the dmem cgroup controller (rev8) Patchwork
2026-07-23 11:48 ` ✓ CI.KUnit: success " Patchwork
2026-07-23 12:29 ` ✓ Xe.CI.BAT: " Patchwork

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=20260723105013.441ED1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hannes@cmpxchg.org \
    --cc=mkoutny@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tj@kernel.org \
    /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.