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 <[email protected]>
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/[email protected]/
Link: 
https://lore.kernel.org/dri-devel/[email protected]/

> 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/[email protected]?part=1

Reply via email to