copy_to_user() returns the no of bytes that it couldn't actually copy to the user space and sizeof(unsigned int)=4...rite? That's where "4" came from.
I think, you are trying to write to a location which is not mapped to the user memory. The function access_ok() which is called from within copy_to_user() is returning error and so copy_to_user() cannot succeed.
My take is that "data" variable is on the kernel stack(as it an arguement...copy by value). So its wrong to write to this location using copy_to_user() as its not in user space.
You can confirm if I am right by calling
access_ok(VERIFY_WRITE, (unsigned int *)data, sizeof(unsigned int));
If this function returns 1, then you are allowed to write.
So my solution is that try to write to a valid user-address and you are done.
Correct me if I am wrong.
Namaste,
Sameer
On 12/20/05, John Que <qwejohn@xxxxxxxxx> wrote:
Hello,
1)I don't have a clue from where this 4 is returned.
2)The udage is ususally thus is the kernel:
if the return value of copy_to_user() is not zero, than
the method who calls it returns -EFAULT.
Any ideas how to solve this problem?
John
On 12/20/05, Bernd Petrovitsch <bernd@xxxxxxxxx> wrote:
> On Tue, 2005-12-20 at 14:32 +0200, John Que wrote:
> [....]
> > res of copy_to_user = 4
> > Any idea?
>
> Yes.
> Where might the 4 come from?
> And how do others use the return value?
>
> [...]
> > On 12/20/05, Bernd Petrovitsch < bernd@xxxxxxxxx> wrote:
> > > On Tue, 2005-12-20 at 14:02 +0200, John Que wrote:
> > > [...]
> > > > I had written a very simple ioctl() method in a module;
> > > > this ioctl calls copy_to_user();
> > > >
> > > > I am getting EFAULT when trying to call this ioctl from
> > > > use space.
> > > >
> > > > Any idea what can be the reason ?
> > >
> > > Yes.
> > >
> > > > Here is the code:
> > > >
> > > > static unsigned long my_ioctl(struct inode* inode,
> > > > struct file* file,
> > > > unsigned int cmd,
> > > > unsigned long data)
> > > >
> > > > {
> > > > ...
> > > > ...
> > > > case MY_IOCTL:
> > > > {
> > > > unsigned int result = 99;
> > > >
> > > > if (copy_to_user((unsigned int *)data,
> > > > &result,
> > > > sizeof(unsigned int)))
> > > > {
> > > > printk("copy_to_user failed\n");
> > > > return -EFAULT;
> > > > }
> > > >
> > > > }
> > >
> > > What value is returned by copy_to_user()?
>
> Bernd
> --
> Firmix Software GmbH http://www.firmix.at/
> mobil: +43 664 4416156 fax: +43 1 7890849-55
> Embedded Linux Development and Services
>
>
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive: http://mail.nl.linux.org/kernelnewbies/
FAQ: http://kernelnewbies.org/faq/