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.

lib: kunit_iov_iter: fix memory leaks

Use vfree() instead of vunmap() to free the buffer allocated by
iov_kunit_create_buffer() because vunmap() does not honour
VM_MAP_PUT_PAGES. In order for this to work the page array itself must
not be managed by kunit.

Remove the folio_put() when destroying a folioq. This is handled by
vfree(), now.

Pointed out by sashiko.dev on a previous iteration of this series.

Tested by running the kunit test 10000 times in a loop.

Link: https://lkml.kernel.org/r/20260326214905.818170-4-lk@c--e.de
Fixes: 2d71340ff1d4 ("iov_iter: Kunit tests for copying to/from an iterator")
Signed-off-by: Christian A. Ehrhardt <lk@c--e.de>
Cc: David Howells <dhowells@redhat.com>
Cc: David Gow <davidgow@google.com>
Cc: Kees Cook <kees@kernel.org>
Cc: Petr Mladek <pmladek@suse.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Christian A. Ehrhardt and committed by
Andrew Morton
0b49c7d0 118cf3f5

+8 -6
+8 -6
lib/tests/kunit_iov_iter.c
··· 42 42 43 43 static void iov_kunit_unmap(void *data) 44 44 { 45 - vunmap(data); 45 + vfree(data); 46 46 } 47 47 48 48 static void *__init iov_kunit_create_buffer(struct kunit *test, ··· 53 53 unsigned long got; 54 54 void *buffer; 55 55 56 - pages = kunit_kcalloc(test, npages, sizeof(struct page *), GFP_KERNEL); 57 - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pages); 56 + pages = kzalloc_objs(struct page *, npages, GFP_KERNEL); 57 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pages); 58 58 *ppages = pages; 59 59 60 60 got = alloc_pages_bulk(GFP_KERNEL, npages, pages); 61 61 if (got != npages) { 62 62 release_pages(pages, got); 63 + kvfree(pages); 63 64 KUNIT_ASSERT_EQ(test, got, npages); 64 65 } 65 66 66 67 buffer = vmap(pages, npages, VM_MAP | VM_MAP_PUT_PAGES, PAGE_KERNEL); 68 + if (buffer == NULL) { 69 + release_pages(pages, got); 70 + kvfree(pages); 71 + } 67 72 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buffer); 68 73 69 74 kunit_add_action_or_reset(test, iov_kunit_unmap, buffer); ··· 374 369 375 370 for (folioq = data; folioq; folioq = next) { 376 371 next = folioq->next; 377 - for (int i = 0; i < folioq_nr_slots(folioq); i++) 378 - if (folioq_folio(folioq, i)) 379 - folio_put(folioq_folio(folioq, i)); 380 372 kfree(folioq); 381 373 } 382 374 }