Re: Copy_to_user() and locks.

From: Andi Kleen (ak@muc.de)
Date: Wed Feb 09 2000 - 05:51:29 EST


root@chaos.analogic.com (Richard B. Johnson) writes:
>
> You must not copy_to_user() under a lock. This is because the
> user's pages may not be in memory and the kernel needs to page-
> fault.

Actually you can use locks that allow sleeping (semaphores, home built
locks using wait queues, reference counts etc.)

>
> /*
> * This is not going to change until after we return, so you
> * can check this with no lock.
> */
> if(!access_okay(VERIFY_WRITE, user_buf, request_length);
> return -EFAULT;

This is unnecessary, copy_to_user does this. It is also not
enough, because it only checks whether user_buf is a user space address.
The real checking whether all parts of user_buf are mapped is done
during the copy itself via exception handlers (see Documentation/exception.txt)

Thus you need:

> spin_unlock_irqrestore(&device_lock, flags);
> /*
> * Now copy to the user without a lock.
> */
> copy_to_user(user_buf, ptr, len);

You should check the return value here (> 0 for -EFAULT, 0 for success)

-Andi

-- 
This is like TV. I don't like TV.

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Tue Feb 15 2000 - 21:00:14 EST