Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/hmm/test: fix error handling in dmirror_device_init

dmirror_device_init() calls device_initialize() which sets the device
reference count to 1, but fails to call put_device() when error occurs
after dev_set_name() or cdev_device_add() failures. This results in
memory leaks of struct device objects. Additionally,
dmirror_device_remove() lacks the final put_device() call to properly
release the device reference.

Found by code review.

Link: https://lkml.kernel.org/r/20251108115346.6368-1-make24@iscas.ac.cn
Fixes: 6a760f58c792 ("mm/hmm/test: use char dev with struct device to get device node")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
Cc: Haoxiang Li <make24@iscas.ac.cn>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: Mika Penttilä <mpenttil@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Ma Ke and committed by
Andrew Morton
fe9d31fd 50d0598c

+7 -2
+7 -2
lib/test_hmm.c
··· 1740 1740 1741 1741 ret = dev_set_name(&mdevice->device, "hmm_dmirror%u", id); 1742 1742 if (ret) 1743 - return ret; 1743 + goto put_device; 1744 1744 1745 1745 ret = cdev_device_add(&mdevice->cdevice, &mdevice->device); 1746 1746 if (ret) 1747 - return ret; 1747 + goto put_device; 1748 1748 1749 1749 /* Build a list of free ZONE_DEVICE struct pages */ 1750 1750 return dmirror_allocate_chunk(mdevice, NULL, false); 1751 + 1752 + put_device: 1753 + put_device(&mdevice->device); 1754 + return ret; 1751 1755 } 1752 1756 1753 1757 static void dmirror_device_remove(struct dmirror_device *mdevice) 1754 1758 { 1755 1759 dmirror_device_remove_chunks(mdevice); 1756 1760 cdev_device_del(&mdevice->cdevice, &mdevice->device); 1761 + put_device(&mdevice->device); 1757 1762 } 1758 1763 1759 1764 static int __init hmm_dmirror_init(void)