原文:
Follows an explanation of the patch that introduced unlocked_ioctl and compat_ioctl into 2.6.11.
The removal of the ioctl field happened a lot later, in 2.6.36.
Explanation: When ioctl was executed, it took the Big Kernel Lock (BKL), so nothing else could
execute at the same time. This is very bad on a multiprocessor machine, so there was a big effort
to get rid of the BKL. First, unlocked_ioctl was introduced. It lets each driver writer choose
what lock to use instead. This can be difficult, so there was a period of transition during which
old drivers still worked (using ioctl) but new drivers could use the improved interface (unlocked_ioctl).
Eventually all drivers were converted and ioctl could be removed.
compat_ioctl is actually unrelated, even though it was added at the same time.
Its purpose is to allow 32-bit userland programs to make ioctl calls on a 64-bit kernel.
The meaning of the last argument to ioctl depends on the driver, so there is no way to
do a driver-independent conversion
解释:
- ioctl : (在2.6.36后,去除了该字段),应用程序进入内核持有giant lock/big-lock/kernel-lock(巨型锁),同一时刻只有一个应用程序进入kernel space
- unlocked_ioctl : 64为应用程序调用的,非巨型锁
- compat_ioctl : 32为应用程序调用的,非巨型锁
参考:
Giant lock : 巨型锁—Big Kernel Lock (BKL)
Fine-grained locking : 非巨型锁—Fine-grained locking
what-is-the-difference-between-ioctl-unlocked-ioctl-and-compat-ioctl
compat_ptr_ioctl()函数的添加
其实就是做了一个32/64位的兼容,原文(原patch)如下:
Many drivers have ioctl() handlers that are completely compatible between
32-bit and 64-bit architectures, except for the argument that is passed
down from user space and may have to be passed through compat_ptr()
in order to become a valid 64-bit pointer.
参考 : https://patchwork.kernel.org/project/linux-fsdevel/patch/20191009191044.308087-9-arnd@arndb.de/
用法可参考optee的一段代码:
static const struct file_operations tee_fops = {
.owner = THIS_MODULE,
.open = tee_open,
.release = tee_release,
.unlocked_ioctl = tee_ioctl,
.compat_ioctl = compat_ptr_ioctl,
};
compat_ptr_ioctl最终,还是调用unlocked_ioctl函数
(linux-5.9/fs/ioctl.c)
long compat_ptr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
if (!file->f_op->unlocked_ioctl)
return -ENOIOCTLCMD;
return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
}
EXPORT_SYMBOL(compat_ptr_ioctl);
```c