The release system call is called when 释放系统调用被调用时。

huangapple go评论89阅读模式
英文:

When is the release system call called?

问题

我正在编写一个设备驱动程序,其中我希望mmap系统调用引用我的驱动程序的一个函数,该函数已注册为文件操作。然后,我希望release系统调用引用另一个已注册为vm_operations_struct的函数。当使用这些系统调用的用户级程序退出或在运行时被终止时,我注意到释放函数会自动调用,而我没有编写相应的代码。我的问题是,释放系统调用是否实际上会在程序返回或崩溃时自动调用,以及是否有一种方法可以在代码中自己调用释放系统调用?

英文:

I am writing a device driver in which I have the mmap system call refer to a function of my driver, which is registered as a file operation. Then I have the release system call refer to another function, registered as vm_operations_struct. When my user level program which uses those system calls exits or is killed while running I see that the release function is called automatically, with no code written. My question is whether the release system call is called actually automatically when the program returns or crashes and is there a way to call the release system call myself in the code?

答案1

得分: 2

是的,每当一个进程终止时,无论是正常终止(通过_exit系统调用)还是被信号终止,内核会自动释放该进程持有的所有资源。在Linux中,我相信您可以查看do_exit函数的代码,了解这是如何实现的。

没有"release"系统调用,但是假设您已经正确连接了所有内容,我期望当程序使用munmap来撤销由您的驱动支持的内存映射,和/或者当它使用close关闭由您的驱动支持的文件描述符时,您的驱动程序的释放函数也会被调用。

英文:

Yes, whenever a process terminates, either normally (via the _exit system call) or by the action of a signal, the kernel automatically releases all resources held by that process. For Linux I believe you can look at the code of do_exit to see how this is done.

There is no "release" system call, but assuming you have wired everything up correctly, I would expect that your driver's release function will also be called when the program uses munmap to tear down memory mappings backed by your driver, and/or when it uses close to close file descriptors backed by your driver.

答案2

得分: 1

以下是翻译好的内容:

确实,在内核中,每当最后一个具有对由mmap返回的内存区域的引用的用户级进程或线程退出或使用munmap系统调用明确取消映射内存区域时,vm_operations_struct中的释放函数会自动被调用。

mmap调用期间分配的资源,如物理内存或设备寄存器,必须使用释放函数来释放。在取消映射内存区域之前,内核会调用此函数,以便驱动程序有时间对其资源进行碎片整理。

可以使用作为释放函数参数传递的vm_area_struct结构来明确从驱动程序代码内部调用该函数。释放函数设计为只能在内核确定可以安全释放与内存区域相关的资源时才被调用,因此通常不建议这样做。自行释放可能导致意外行为甚至系统崩溃。

如果需要在用户级程序崩溃或退出时执行清理任务,请考虑安装SIGINTSIGTERM信号处理程序,或使用atexit函数注册退出处理程序。这些机制使您能够在用户终止或终止程序之前执行清理操作。

英文:

It is true that the release function in the vm_operations_struct is automatically called by the kernel whenever the last user-level process or thread that has a reference to the memory region returned by mmap exits or the memory region is specifically unmapped using the munmap system call.

A resource that was allocated during an mmap call, such as physical memory or device registers, must be released using the release function. Before the memory region is unmapped, the kernel calls this function to give the driver time to defragment its resources.

The vm_area_struct structure, which is passed as an argument to the release function, can be used to explicitly call the function from within your driver code. The release function is designed to only be called by the kernel when it is safe to release resources related to the memory region, so doing so is typically not advised. Self-releasing could result in unexpected behavior and even a system crash.

Consider installing a SIGINT or SIGTERM signal handler or registering an exit handler using the atexit function if you need to perform cleanup tasks when your user-level program crashes or exits. These mechanisms enable you to carry out cleanup actions prior to the user killing or terminating your program.

huangapple
  • 本文由 发表于 2023年4月6日 23:53:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定