Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there isn't a munmap callback in struct file_operation?

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?

like image 379
liangpig1 Avatar asked Aug 02 '14 19:08

liangpig1


2 Answers

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.

like image 110
liangpig1 Avatar answered Sep 19 '22 16:09

liangpig1


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.

like image 39
jxh Avatar answered Sep 16 '22 16:09

jxh