dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Óscar Megía López" <megia.oscar@gmail.com>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v4] Memory leak error in qxl unbind
Date: Sun, 09 Aug 2026 17:04:24 +0000	[thread overview]
Message-ID: <20260809170425.007951F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260809165326.164763-1-megia.oscar@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] The patch adds error cleanup to `ttm_pool_mgr_init()` (freeing the shrinker and destroying pools), but the caller (`ttm_global_init()`) ignores its return value. This leads to double-frees and NULL pointer dereferences during teardown.
- [High] `ttm_pool_init()` ignores the newly added return value of `ttm_pool_type_init()`, resulting in guaranteed NULL pointer dereferences during teardown if initialization fails.
- [High] Lockless TOCTOU data race on `shrinker_list` emptiness check in `ttm_pool_shrink()` leads to invalid pointer dereference and memory corruption.
--

--- Patch [1]: [PATCH v4] Memory leak error in qxl unbind ---
commit ed37ece8c0ad50e1a16deff6653abfe2c17eb1a0
Author: Óscar Megía López <megia.oscar@gmail.com>
Subject: Memory leak error in qxl unbind

This commit addresses an OOM leak and refcount corruption in qxl unbind.
It adds proper cleanup and error propagation for list_lru_init() failures
during TTM pool initialization to ensure pool types are not left orphaned.

Link: https://lore.kernel.org/lkml/20260727110212.64913-1-megia.oscar@gmail.com/
Link: https://lore.kernel.org/dri-devel/20260731053047.24503-1-megia.oscar@gmail.com/

> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
> index 1bf37023fed66..6821fadf1a8d0 100644
> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
> @@ -374,17 +374,23 @@ static struct page *ttm_pool_type_take(struct ttm_pool_type *pt, int nid)
>  }
>  
>  /* Initialize and add a pool type to the global shrinker list */
> -static void ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool,
> +static int ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool,
>  			       enum ttm_caching caching, unsigned int order)

[Severity: High]
Since this function now returns an error code instead of void, do all callers
check the new return value?

Looking at ttm_pool_init() in drivers/gpu/drm/ttm/ttm_pool.c:

    ttm_pool_type_init(pt, pool, i, j);

If ttm_pool_type_init() returns an error, pt->shrinker_list remains
zero-initialized. Later, when ttm_pool_fini() calls ttm_pool_type_fini(),
it unconditionally runs:

    list_del(&pt->shrinker_list);

Will this cause a NULL pointer dereference on the zero-initialized list head
during teardown?

[ ... ]
> @@ -458,6 +464,9 @@ static unsigned int ttm_pool_shrink(int nid, unsigned long num_to_free)
>  	struct ttm_pool_type *pt;
>  	unsigned int num_pages;
>  
> +	if ((shrinker_list.prev == &shrinker_list) && (shrinker_list.next == &shrinker_list))
> +		return 0;
> +
>  	down_read(&pool_shrink_rwsem);
>  	spin_lock(&shrinker_lock);
>  	pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);

[Severity: High]
Is it safe to check if shrinker_list is empty without holding shrinker_lock?

If the list becomes empty between this check and acquiring shrinker_lock
(for example, if a concurrent thread calls ttm_pool_type_fini() during
device teardown), list_first_entry() will execute on an empty list.

This would return a pointer to the list head itself cast to a struct
ttm_pool_type pointer, which could lead to memory corruption when
list_move_tail() and list_lru_walk_node() are invoked on this invalid
pointer.

Could this check be safely moved inside the critical section?

[ ... ]
> @@ -1506,15 +1576,53 @@ int ttm_pool_mgr_init(unsigned long num_pages)
>  	spin_lock_init(&shrinker_lock);
>  	INIT_LIST_HEAD(&shrinker_list);
>  
> +	mm_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE, "drm-ttm_pool");
> +	if (!mm_shrinker)
> +		return -ENOMEM;
> +
>  	for (i = 0; i < NR_PAGE_ORDERS; ++i) {
> -		ttm_pool_type_init(&global_write_combined[i], NULL,
> +		ret = ttm_pool_type_init(&global_write_combined[i], NULL,
>  				   ttm_write_combined, i);
> -		ttm_pool_type_init(&global_uncached[i], NULL, ttm_uncached, i);
> +		if (ret) {
> +			ttm_pool_type_fini_and_list_lru_destroy(i);
> +			shrinker_free(mm_shrinker);
> +			return ret;
> +		}

[Severity: Critical]
If ttm_pool_mgr_init() fails here, does the caller properly handle the
returned error?

If the caller (ttm_global_init()) ignores this error and continues execution,
mm_shrinker is left pointing to freed memory while execution proceeds.

Then during module unload, ttm_pool_mgr_fini() unconditionally calls:

    shrinker_free(mm_shrinker);
    ttm_pool_type_fini_and_list_lru_destroy(NR_PAGE_ORDERS);

Could this lead to a double-free of mm_shrinker and potential NULL pointer
dereferences on the partially destroyed list structures?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260809165326.164763-1-megia.oscar@gmail.com?part=1

  reply	other threads:[~2026-08-09 17:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-09 16:53 [PATCH v4] Memory leak error in qxl unbind Óscar Megía López
2026-08-09 17:04 ` sashiko-bot [this message]
2026-08-09 18:19 ` Greg KH
2026-08-21 14:48   ` Oscar Megia López

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=20260809170425.007951F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=megia.oscar@gmail.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox