5

Will Linux Kernel free kmalloc'ed and not kfree'd in kernel module memory after module release just like it's work with user space apps?

2 Answers 2

9

The kernel will not do any garbage collection for a module. If the module kmallocs a chunk of memory and doesn't kfree it before the module is unloaded, that chunk will stay allocated and inaccessible until the next reboot.

Sign up to request clarification or add additional context in comments.

2 Comments

The kernel will not do any garbage collection, and also will not free memory when the module exits. The latter is the key difference to a userspace process, which has all it's memory returned to the system when it exits.
Note that exactly the same thing applies in userspace, if you write a module that is loaded using dlopen().
1

As others said, the kernel will not do any garbage collection for a module but device drivers can use devm_* types of resource allocations (called managed resource allocation functions) and the kernel will do all the required cleanups after there is no more reference to the device.

See here for the commented source code in the kernel source for devm_kmalloc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.