unlocked_ioctl vs normal ioctl

时间:2009-06-30 13:03:50

标签: c linux synchronization kernel ioctl

在我的驱动程序的file_operations结构中,我有:

struct file_operations Fops = {
  read:    device_read,
  write:   device_write,
  unlocked_ioctl:   device_ioctl,
  ...
};

即。没有使用ioctl字段。这是否足以避免Big Kernel Lock并在没有任何同步的情况下进入device_ioctl()?或者我是否也必须在代码的用户空间部分更改ioctl()调用?

3 个答案:

答案 0 :(得分:10)

阅读这篇LWN文章: http://lwn.net/Articles/119652/

在2.6.33和2.6.35 rc之间的某个时间(使用git-diff来找出哪个提交)内核现在只在定义.ioctl时进行WARN。

这是向更明确和细粒度锁定迈进的一步。另请注意,只有更改函数签名和指针才会编译,但会引入竞争条件的可能性(两个用户空间应用程序同时进行ioctl调用)。

答案 1 :(得分:8)

嗯,我解决了这个问题。还需要更改device_ioctl函数的签名。没有inode参数,函数也应返回long。就像下面的补丁一样:

-static int st_ioctl(struct inode *inode, struct file *file,
- unsigned int cmd_in, unsigned long arg)
+static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
{

(来自:http://linux.derkeiler.com/Mailing-Lists/Kernel/2008-01/msg06799.html

答案 2 :(得分:8)

Andi Kleem在Linux内核邮件列表上发布了使用ioctlunlocked_ioctl快速转换代码的方法:

[JANITOR PROPOSAL] Switch ioctl functions to ->unlocked_ioctl

该配方解释了如何调整功能的参数并插入锁定和解锁呼叫。