简体   繁体   English

如果需要调用 copy_to_user,如何使用自旋锁?

[英]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):想象一下这个场景(在单处理器机器上):

  1. Process A mmap() ed a file处理一个mmap()编辑的文件
  2. The process calls your driver providing an address into that mmap() ed area该过程调用您的驱动程序提供一个地址到该mmap() ed 区域
  3. Your code runs, locks, then 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导致该地址上的页面错误 - 内存不存在,因此进程进入睡眠状态,直到数据来自磁盘。
  4. The kernel schedules to process B, it calls your the driver the same way.内核调度处理 B,它以相同的方式调用您的驱动程序。
  5. Deadlock - Process A is waiting for the IO to return inside a lock but won't be scheduled because B is holding the CPU waiting for the same lock to be unlocked.死锁- 进程 A 正在等待 IO 在锁内返回,但不会被调度,因为 B 持有 CPU 等待相同的锁被解锁。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM