How to check if my kernel driver is leaking memory
Hi, I am developing a kernel driver. What should I test to make sure my kernel driver is not leaking memory? 1. under normal operation (when applications open and close my driver properly) 2. in error situation (when application open my driver and then it crashes without close my driver property) How can I find out the memory usage used by my driver? so that I can monitor it over time. Thank you.
On Sun, 09 Mar 2014 22:14:24 -0700, m silverstri said:
I am developing a kernel driver. What should I test to make sure my kernel driver is not leaking memory?
1) The brute force method - just add lots of printk's that have "allocating 25-byte frobozz struct" and "freeing 25-byte frobozz struct" and make sure they match up. 2) kmemleak.
1. under normal operation (when applications open and close my driver properly) 2. in error situation (when application open my driver and then it crashes without close my driver property)
Case (2) shouldn't happen, as even if a program crashes the kernel *should* be invoking the cleanup of open files at process termination. A more common cause of memory leaks is for an open() or read/write/ioctl() path to allocate N chunks of memory, hit an error, and return after having cleaned up only N-1 of the chunks. This is part of why most kernel code uses a 'goto error' structure with only one return; at the end of the function.
Hi, Are you writing driver on X86 or some other Architecture ? if it is X86 then use *KEDR framework(http://kedr.berlios.de/ <http://kedr.berlios.de/>).* *RegardsSanjeev Sharma* On Mon, Mar 10, 2014 at 7:28 PM, <Valdis.Kletnieks@vt.edu> wrote:
On Sun, 09 Mar 2014 22:14:24 -0700, m silverstri said:
I am developing a kernel driver. What should I test to make sure my kernel driver is not leaking memory?
1) The brute force method - just add lots of printk's that have "allocating 25-byte frobozz struct" and "freeing 25-byte frobozz struct" and make sure they match up.
2) kmemleak.
1. under normal operation (when applications open and close my driver properly) 2. in error situation (when application open my driver and then it crashes without close my driver property)
Case (2) shouldn't happen, as even if a program crashes the kernel *should* be invoking the cleanup of open files at process termination.
A more common cause of memory leaks is for an open() or read/write/ioctl() path to allocate N chunks of memory, hit an error, and return after having cleaned up only N-1 of the chunks. This is part of why most kernel code uses a 'goto error' structure with only one return; at the end of the function.
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
2014-03-10 10:44 GMT+05:30 m silverstri <michael.j.silverstri@gmail.com>:
Hi,
I am developing a kernel driver. What should I test to make sure my kernel driver is not leaking memory? 1. under normal operation (when applications open and close my driver properly) 2. in error situation (when application open my driver and then it crashes without close my driver property)
How can I find out the memory usage used by my driver? so that I can monitor it over time.
I have earlier come across a similar situation and kmemleak was very helpful. I documented my steps and you may find it useful: http://psankar.blogspot.in/2010/11/detecting-memory-leaks-in-kernel.html -- Sankar P http://psankar.blogspot.com
Thanks Sankar. If I just modify .config file to enable CONFIG_DEBUG_KMEMLEAK, how can I 'Increase the config option "Maximum kmemleak early log entires" value to a sufficiently large number like 1200. The default value of 400 may not work correctly in all configurations.'? On Thu, Mar 13, 2014 at 9:34 AM, Sankar P <sankar.curiosity@gmail.com> wrote:
2014-03-10 10:44 GMT+05:30 m silverstri <michael.j.silverstri@gmail.com>:
Hi,
I am developing a kernel driver. What should I test to make sure my kernel driver is not leaking memory? 1. under normal operation (when applications open and close my driver properly) 2. in error situation (when application open my driver and then it crashes without close my driver property)
How can I find out the memory usage used by my driver? so that I can monitor it over time.
I have earlier come across a similar situation and kmemleak was very helpful.
I documented my steps and you may find it useful:
http://psankar.blogspot.in/2010/11/detecting-memory-leaks-in-kernel.html
-- Sankar P http://psankar.blogspot.com
Please use make menuconfig and press / then search for DEBUG_KMEMLEAK_EARLY_LOG_SIZE and adjust value then save and exit. You will find it under Kernel Hacking -> Memory Debugging -|> Kernel memory leak detector Symbol: DEBUG_KMEMLEAK_EARLY_LOG_SIZE [=] │ Type : integer │ Prompt: Maximum kmemleak early log entries │ Location: │ -> Kernel hacking │ -> Memory Debugging │ (3) -> Kernel memory leak detector (DEBUG_KMEMLEAK [=n]) │ Defined at lib/Kconfig.debug:459 │ Depends on: DEBUG_KMEMLEAK [=n] On Thu, Mar 13, 2014 at 1:39 PM, m silverstri < michael.j.silverstri@gmail.com> wrote:
Thanks Sankar.
If I just modify .config file to enable CONFIG_DEBUG_KMEMLEAK, how can I 'Increase the config option "Maximum kmemleak early log entires" value to a sufficiently large number like 1200. The default value of 400 may not work correctly in all configurations.'?
On Thu, Mar 13, 2014 at 9:34 AM, Sankar P <sankar.curiosity@gmail.com> wrote:
2014-03-10 10:44 GMT+05:30 m silverstri <michael.j.silverstri@gmail.com :
Hi,
I am developing a kernel driver. What should I test to make sure my kernel driver is not leaking memory? 1. under normal operation (when applications open and close my driver properly) 2. in error situation (when application open my driver and then it crashes without close my driver property)
How can I find out the memory usage used by my driver? so that I can monitor it over time.
I have earlier come across a similar situation and kmemleak was very helpful.
I documented my steps and you may find it useful:
http://psankar.blogspot.in/2010/11/detecting-memory-leaks-in-kernel.html
-- Sankar P http://psankar.blogspot.com
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
participants (5)
-
Aruna Hewapathirane -
m silverstri -
sanjeev sharma -
Sankar P -
Valdis.Kletnieks@vt.edu