什么时候应该编写Linux内核模块? [英] When should I write a Linux kernel module?

查看:94
本文介绍了什么时候应该编写Linux内核模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,有些人希望将代码从用户空间移至Linux的内核空间.很多时候,原因似乎是代码应该具有特别高的优先级,或者仅仅是内核空间更快".

Some people want to move code from user space to kernel space in Linux for some reason. A lot of times the reason seems to be that the code should have particularly high priority or simply "kernel space is faster".

这对我来说似乎很奇怪.我什么时候应该考虑编写内核模块?有一套标准吗?

This seems strange to me. When should I consider writing a kernel module? Are there a set of criterias?

我该如何激励将代码保存在(我认为)属于该用户空间的用户空间中?

How can I motivate keeping code in user space that (I believe) belong there?

推荐答案

经验法则:尝试绝对​​的最好的将代码保留在用户空间中.如果您认为不能,请花与编写代码相同的时间(即很长的时间)来研究内核代码的替代方法,然后重试以在用户空间中实现它. .如果您 still 无法,请进行更多研究以确保您做出正确的选择,然后非常谨慎进入内核.正如其他人所说的那样,在少数情况下,要求编写内核模块和调试内核代码是一件非常令人毛骨悚然的事情,因此请不惜一切代价避开.

Rule of thumb: try your absolute best to keep your code in user-space. If you don't think you can, spend as much time researching alternatives to kernel code as you would writing the code (ie: a long time), and then try again to implement it in user-space. If you still can't, research more to ensure you're making the right choice, then very cautiously move into the kernel. As others have said, there are very few circumstances that dictate writing kernel modules and debugging kernel code can be quite hellish, so steer clear at all costs.

关于在考虑编写内核模式代码时应检查的具体条件,请注意以下几点:它是否需要访问极低级的资源(例如中断)?您的代码是否为无法在当前导出的功能上构建 的硬件定义了新的接口/驱动程序?您的代码是否要求访问未导出到内核空间之外的数据结构或原语?您是否正在编写主要由 other 内核子系统使用的内容,例如调度程序或VM系统(即使在此,子系统不一定完全处于内核模式也是如此:Mach对以下内容有很强的支持: user-mode 虚拟内存传呼机,所以可以肯定地做到这一点)?

As far as concrete conditions you should check for when considering writing kernel-mode code, here are a few: Does it need access to extremely low-level resources, such as interrupts? Is your code defining a new interface/driver for hardware that cannot be built on top of currently exported functionality? Does your code require access to data structures or primitives that are not exported out of kernel space? Are you writing something that will be primarily used by other kernel subsystems, such as a scheduler or VM system (even here it isn't entirely necessary that the subsystem be kernel-mode: Mach has strong support for user-mode virtual memory pagers, so it can definitely be done)?

这篇关于什么时候应该编写Linux内核模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆