Skip to contentSkip to conversation

Memory leak error in qxl unbind

PATCH Superseded Óscar Megía López 3 participants 4 messages Jul 27 – Aug 3

Subsystems (4)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Memory leak error in qxl unbind

I discovered OOM after run script below around two hours in my computer:

while :; do
echo [pci qxl id] > /sys/bus/pci/drivers/qxl/unbind echo [pci qxl id] > /sys/bus/pci/drivers/qxl/bind
done

If you run 10000 times above script kmemleak does not report any memory leak, but if you run above script about two hours several OOM ocurs and at the end kernel panic.

The OOM isn't just a simple leak; it's a refcount corruption that renders the list_lru fix dead code after the first mid-init failure.

Here's the chain:

Bug 1: ttm_global_init ignores ttm_pool_mgr_init() return
If shrinker_alloc() fails under memory pressure, ttm_pool_mgr_init returns -ENOMEM with pool types already initialized (64 list_lru_init calls done). ttm_global_init ignored this and returned 0, leaving orphaned pool types with a NULL mm_shrinker.

Fix: Check ret from ttm_pool_mgr_init; if non-zero, goto out cleans up refcount + debugfs.

Bug 2: ttm_pool_mgr_init leaks pool types on shrinker_alloc failure If shrinker_alloc fails after all 64 pool types were list_lru_init'd, the function returned -ENOMEM without undoing them. With Bug 1 now triggering proper error handling, this undo is necessary.

Fix: err_shrinker: label that finalizes + destroys all 64 pool types before returning.

You must apply the patch from the link "[PATCH v3] drm/qxl: fix use-after-free in qxl_irq_handler on PCI" before testing. If you don't apply this patch, you will get a UAF error when running the script above.

Fix: "Unchecked list_lru_init() return value in ttm_pool_type_init() causes a deterministic NULL pointer dereference in the newly added error path."
Now check list_lru_init return value in ttm_pool_type_take, check if pt->pages.node is NULL in ttm_pool_type_fini and ttm_pool_type_init return value in ttm_pool_mgr_init.

Solved new issues:
- [High] The patch introduces a use-after-free race condition between ttm_pool_type_fini() and the active memory shrinker ttm_pool_shrink() by calling list_lru_destroy() prematurely.

Solved pre-existing issues:
- [High] ttm_pool_type_init() ignores the return value of list_lru_init(), leading to a NULL pointer dereference if allocation fails.
- [High] ttm_pool_shrink() assumes shrinker_list is never empty, causing memory corruption and crashes during module unload if triggered.

Assisted-by OpenCode:1.17.18-Big Pickle/DeepSeek V4 Flash
---
 drivers/gpu/drm/drm_gem.c        |  1 +
 drivers/gpu/drm/ttm/ttm_device.c |  5 ++-
 drivers/gpu/drm/ttm/ttm_pool.c   | 71 +++++++++++++++++++++++++-------
 3 files changed, 60 insertions(+), 17 deletions(-)
drivers/gpu/drm/drm_gem.c +1 -0
index e3ed684ddcf2..b3f608619cfa 100644
@@ -143,6 +143,7 @@ static void
 drm_gem_init_release(struct drm_device *dev, void *ptr)
 {
 	drm_vma_offset_manager_destroy(dev->vma_offset_manager);
+	idr_destroy(&dev->object_name_idr);
 }
 
 /**
drivers/gpu/drm/ttm/ttm_device.c +4 -1
index d3bfb9a696a7..c880a0430363 100644
@@ -96,7 +96,10 @@ static int ttm_global_init(void)
 		>> PAGE_SHIFT;
 	num_dma32 = min(num_dma32, 2UL << (30 - PAGE_SHIFT));
 
-	ttm_pool_mgr_init(num_pages);
+	ret = ttm_pool_mgr_init(num_pages);
+	if (ret)
+		goto out;
+
 	ttm_tt_mgr_init(num_pages, num_dma32);
 
 	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32 |
drivers/gpu/drm/ttm/ttm_pool.c +55 -16

126 lines not on this page · raw message

Signature · 1 line
2.55.0
#1 lore raw

…

Quoted 2 lines

Fix: Check ret from ttm_pool_mgr_init; if non-zero, goto out cleans up refcount + debugfs.

…

Quoted 15 lines

Fix: err_shrinker: label that finalizes + destroys all 64 pool types before returning.

You must apply the patch from the link "[PATCH v3] drm/qxl: fix use-after-free in qxl_irq_handler on PCI" before testing. If you don't apply this patch, you will get a UAF error when running the script above.

Fix: "Unchecked list_lru_init() return value in ttm_pool_type_init() causes a deterministic NULL pointer dereference in the newly added error path."
Now check list_lru_init return value in ttm_pool_type_take, check if pt->pages.node is NULL in ttm_pool_type_fini and ttm_pool_type_init return value in ttm_pool_mgr_init.

Quoted 2 lines

Solved new issues:
- [High] The patch introduces a use-after-free race condition between ttm_pool_type_fini() and the active memory shrinker ttm_pool_shrink() by calling list_lru_destroy() prematurely.

How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?

See also:
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.2-rc5#n145
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v7.2-rc5#n34

Quoted 3 lines

Solved pre-existing issues:
- [High] ttm_pool_type_init() ignores the return value of list_lru_init(), leading to a NULL pointer dereference if allocation fails.
- [High] ttm_pool_shrink() assumes shrinker_list is never empty, causing memory corruption and crashes during module unload if triggered.

Would another change description variant become better?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.2-rc5#n81

…

Quoted 3 lines
---
 drivers/gpu/drm/drm_gem.c        |  1 +

…

Some contributors would appreciate patch version descriptions.
https://lore.kernel.org/all/?q=%22This+looks+like+a+new+version+of+a+previously+submitted+patch%22
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.2-rc5#n310

Regards,
Markus

#2 lore raw
In reply to Óscar Megía López
kernel test robot noticed the following build errors:

Hi Óscar,

kernel test robot noticed the following build errors:

[auto build test ERROR on drm-misc/drm-misc-next]
[also build test ERROR on linus/master v7.2-rc5 next-20260731] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/scar-Meg-a-L-pez/Memory-leak-error-in-qxl-unbind/20260803-140059
base:   https://gitlab.freedesktop.org/drm/misc/kernel.git drm-misc-next
patch link:    https://lore.kernel.org/r/20260727114155.69204-1-megia.oscar%40gmail.com
patch subject: [PATCH v3] Memory leak error in qxl unbind
config: parisc-defconfig (https://download.01.org/0day-ci/archive/20260803/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 16.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260803/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/gpu/drm/ttm/ttm_pool.c: In function 'ttm_pool_mgr_fini':

>> drivers/gpu/drm/ttm/ttm_pool.c:1510:9: error: 'backup_fault_inject' undeclared (first use in this function)
    1510 |     if (backup_fault_inject.dname) {
         |         ^~~~~~~~~~~~~~~~~~~

drivers/gpu/drm/ttm/ttm_pool.c:1510:9: note: each undeclared identifier is reported only once for each function it appears in

vim +/backup_fault_inject +1510 drivers/gpu/drm/ttm/ttm_pool.c

1502
1503 /**
1504 * ttm_pool_mgr_fini - Finalize globals
1505 *
1506 * Cleanup the global pools and unregister the MM shrinker.
1507 */
1508 void ttm_pool_mgr_fini(void)
1509 {

> 1510	    if (backup_fault_inject.dname) {

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#3 lore raw
In reply to Óscar Megía López
kernel test robot noticed the following build errors:

Hi Óscar,

kernel test robot noticed the following build errors:

[auto build test ERROR on drm-misc/drm-misc-next]
[also build test ERROR on linus/master v7.2-rc5 next-20260731] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/scar-Meg-a-L-pez/Memory-leak-error-in-qxl-unbind/20260803-140059
base:   https://gitlab.freedesktop.org/drm/misc/kernel.git drm-misc-next
patch link:    https://lore.kernel.org/r/20260727114155.69204-1-megia.oscar%40gmail.com
patch subject: [PATCH v3] Memory leak error in qxl unbind
config: loongarch-defconfig (https://download.01.org/0day-ci/archive/20260803/[email protected]/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project bacfe2950f8218268fcc0a8765644ea0c15f0360)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260803/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/gpu/drm/ttm/ttm_pool.c:1510:9: error: use of undeclared identifier 'backup_fault_inject'
    1510 |     if (backup_fault_inject.dname) {
         |         ^~~~~~~~~~~~~~~~~~~
   drivers/gpu/drm/ttm/ttm_pool.c:1511:14: error: use of undeclared identifier 'backup_fault_inject'
    1511 |         dput(backup_fault_inject.dname);
         |              ^~~~~~~~~~~~~~~~~~~
   drivers/gpu/drm/ttm/ttm_pool.c:1512:9: error: use of undeclared identifier 'backup_fault_inject'
    1512 |         backup_fault_inject.dname = NULL;
         |         ^~~~~~~~~~~~~~~~~~~

3 errors generated.

vim +/backup_fault_inject +1510 drivers/gpu/drm/ttm/ttm_pool.c

1502
1503 /**
1504 * ttm_pool_mgr_fini - Finalize globals
1505 *
1506 * Cleanup the global pools and unregister the MM shrinker.
1507 */
1508 void ttm_pool_mgr_fini(void)
1509 {

> 1510	    if (backup_fault_inject.dname) {

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki