Search code examples
linux-kernel

What happen if I alloc memory using vzalloc() in kernel but free using kfree()?


If I alloc one memory use vzalloc() interface (kernel v4.9), but free it use kfree(), what will happen in kernel? Can I free this memory?

I watch my code, and see this trouble, but I do not what will happen if touch this problem, so ask others for help.


Solution

  • The documentation for kfree() says:

    Don’t free memory not originally allocated by kmalloc() or you will run into trouble.

    If you violate the restriction the behavior is undefined. If you're lucky the kernel will crash; if you're unlucky you might silently corrupt something and cause unspecified future weirdness.

    To free memory allocated by vzalloc(), use vfree() or kvfree().