[英]How to use a spin lock if copy_to_user needs to be called?
I have written a small driver to read some data and give it to the user.我编写了一个小驱动程序来读取一些数据并将其提供给用户。 My driver can be used by more than one application, ie it's a reentrant driver, hence the use of a spin lock.
我的驱动程序可以被多个应用程序使用,即它是一个可重入的驱动程序,因此使用了自旋锁。 But I discovered that
copy_to_user
should not be called with a spin lock held.但我发现
copy_to_user
不应该在持有自旋锁的情况下调用。 char_device_buf
in the following code is shared data;下面代码中的
char_device_buf
为共享数据; I have to protect it.我必须保护它。 Is there any mechanism apart from mutex to use spin lock and use
copy_to_user
?除了互斥锁之外还有其他机制可以使用自旋锁和使用
copy_to_user
吗?
static ssize_t char_dev_read(struct file *file,
char *buf,
size_t lbuf,
loff_t *ppos)
{
int maxbytes; /* number of bytes from ppos to MAX_LENGTH */
int bytes_to_do; /* number of bytes to read */
int nbytes; /* number of bytes actually read */
maxbytes = MAX_LENGTH - *ppos;
if( maxbytes > lbuf ) bytes_to_do = lbuf;
else bytes_to_do = maxbytes;
if( bytes_to_do == 0 ) {
printk("Reached end of device\n");
return -ENOSPC; /* Causes read() to return EOF */
}
/* Tesing for accidental release */
// accidental_release();
printk(KERN_DEBUG "READER: trying for critical region lock \n");
spin_lock(&myspin);/*begin of critical region */
printk(KERN_DEBUG "READER : acquired lock: executing critical code\n");
nbytes = bytes_to_do -
copy_to_user( buf, /* to */
char_device_buf + *ppos, /* from */
bytes_to_do ); /* how many bytes */
spin_unlock(&myspin); /* end of critical region */
*ppos += nbytes;
return nbytes;
}
The reason why copy_{to,from}_user
should not be used inside a spin lock, is that these functions can sleep.不应该在自旋锁中使用
copy_{to,from}_user
的原因是这些函数可以休眠。 Imagine this scenario (on a uni-processor machine):想象一下这个场景(在单处理器机器上):
mmap()
ed a filemmap()
编辑的文件mmap()
ed areammap()
ed 区域copy_to_user
causes a page fault on that address - the memory is not present so that process goes to sleep until the data comes from the disk.copy_to_user
导致该地址上的页面错误 - 内存不存在,因此进程进入睡眠状态,直到数据来自磁盘。 Unless there is a 100% guarantee that copy_{to,from}_user
won't cause a segfault, you cannot use spin locks, but must use a sleep lock instead, such as 'mutex_lock'.除非 100% 保证
copy_{to,from}_user
不会导致段错误,否则不能使用自旋锁,而必须使用睡眠锁,例如“mutex_lock”。 Sleep locks yield control to the scheduler while spin locks do not.睡眠锁将控制权交给调度程序,而自旋锁则不会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.