Hello, I have encountered an issue with a kernel application that I have written, the issue might be caused by a memory leak in the kernel. The application allocates and deallocates contiguous memory using contigmalloc() and contigfree(). The application will fail after a period of time because there is not enough free contiguous memory left. There could be an issue with the freeing of memory when using the contigfree() function.
I have attached a simplified version of the application. The resulting kernel module just allocates contiguous memory and then frees the memory using contigfree(); This allocation is done in a loop and the attached src code triggers the issue. After a period of time when running the application multiple times, the kernel reports that there is no available memory and fails with allocation. I can see that the amount of free memory is decreasing in correlation to how many times I run the application by using DDB and printing out freepages using "show freepages" I run the application in a loop using a shell script where I kldload then kldunload multiple times (script runs up to 1000000) The application can take 20 to over 60 minutes to trigger the issue and run out of memory but can take longer also. I am running the application on -> FreeBSD on 11.2 VM with 2 Gb of ram. Allocating one cpu core. Running on an Intel(R) Xeon(R) CPU E5-2658 v2 @ 2.40GH using Ubuntu 16.04 host. I have attached the test kernel application and a Makefile. Thanks, Ciunas. -------------------------------------------------------------- Intel Research and Development Ireland Limited Registered in Ireland Registered Office: Collinstown Industrial Park, Leixlip, County Kildare Registered Number: 308263 This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
#include <sys/param.h> #include <sys/conf.h> #include <sys/kernel.h> #include <sys/malloc.h> #include <sys/module.h> #include <sys/systm.h> #define DEV_MEM "test_mem" MALLOC_DECLARE(TEST_MEM); MALLOC_DEFINE(TEST_MEM, "test_mem", "test_mem driver"); /* Test application to allocate contiguous memory then free it */ static int test_application() { int i = 0; int array[10] = {2097152, 1024, 2101248, 1024, 2097152, 2101248, 2101248, 2097152, 2101248, 1024}; int array1[10] = {1024, 2101248, 2097152, 2101248, 2101248, 2097152, 1024, 2101248, 1024, 2097152}; void *mem_blocks[10]; for (i = 0; i < 10; i++) { mem_blocks[i] = contigmalloc(array[i], TEST_MEM, 0, 0, ~0, PAGE_SIZE, 0); if (!mem_blocks[i]) { printf("%s:%d Unable to allocate contiguous memory slab \n", __func__, __LINE__); return -1; } } for (i = 0; i < 10; i++) contigfree(mem_blocks[i], array[i], TEST_MEM); for (i = 0; i < 10; i++) { mem_blocks[i] = contigmalloc(array1[i], TEST_MEM, 0, 0, ~0, PAGE_SIZE, 0); if (!mem_blocks[i]) { printf("%s:%d Unable to allocate contiguous memory slab \n", __func__, __LINE__); return -1; } } for (i = 0; i < 10; i++) contigfree(mem_blocks[i], array1[i], TEST_MEM); return 0; } static int mem_modevent(module_t mod __unused, int type, void *data __unused) { switch (type) { case MOD_LOAD: test_application(); return (0); case MOD_UNLOAD: return (0); default: return (EOPNOTSUPP); } } DEV_MODULE(test_mem, mem_modevent, NULL); MODULE_VERSION(test_mem, 1);
Makefile
Description: Makefile
_______________________________________________ freebsd-stable@freebsd.org mailing list https://lists.freebsd.org/mailman/listinfo/freebsd-stable To unsubscribe, send any mail to "freebsd-stable-unsubscr...@freebsd.org"