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. -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: k_memory_contig.c URL: <http://lists.freebsd.org/pipermail/freebsd-stable/attachments/20181024/f8b8155b/attachment.c> -------------- next part -------------- A non-text attachment was scrubbed... Name: Makefile Type: application/octet-stream Size: 67 bytes Desc: Makefile URL: <http://lists.freebsd.org/pipermail/freebsd-stable/attachments/20181024/f8b8155b/attachment.obj>
Konstantin Belousov
2018-Oct-26 20:12 UTC
Possible memory leak in the kernel (contigmalloc)
On Wed, Oct 24, 2018 at 04:27:52PM +0000, Bennett, Ciunas wrote:> 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. >It is unlikely that there is an issue with a leak, but I would be not surprised if your allocation/free pattern would cause fragmentation on free lists that results in contigmalloc(9) failures after. Look at the vmstat -z/vmstat -m output to see uma and malloc stats. More interesting for your case can be the output from sysctl vm.phys_free which provides information about the free queues and order of free pages on them.