I'm working on a Linux kernel module which shares a piece of memory with user applications through the syscall mmap. The module works alright with the help of the mmap callback defined in the struct file_operations, which informs the module when the syscall is invoked.
However, the problem comes out when user applications want to stop the sharing through syscall munmap. There is not an munmap callback or something that does similar work in the struct file_operations. Therefore, I have to do another ioctl to inform the kernel module that the sharing has been revoked, which is both inconvenient and insecure.
During my search for the solution, I found that there was once an munmap callback defined. But it was removed when the kernel version was about 2.4 or some times after.
Can someone tell me why the munmap callback is removed or is there any alternative approach to inform the kernel module when an munmap syscall is called?
After some searching, I have finally come to the answers.
The function that I need lies in the vm_operations_struct. The close callback will be invoked if the munmap() syscall is executed successfully and I can use this function pointer to inform my kernel module a memory unmap has just happened.
The override of the default close action, that is the assignment of the close function pointer, is done in the mmap handler in my kernel module, where a vm_area struct is provided as a parameter and you can do all the things you want to it.
Actually, all the detail is described in the Book Linux Device Drivers 3, Chapter 15.
Thanks all your guys for providing really useful suggestions.
munmap()
is not a file operation, it a system call that operates on the mapped memory itself.
It is perfectly valid to mmap()
a file descriptor, and then close the file descriptor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With