LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   problems with copy_to_user() (https://www.linuxquestions.org/questions/linux-kernel-70/problems-with-copy_to_user-623131/)

leoremoto 02-22-2008 02:53 PM

problems with copy_to_user()
 
Hi all,

I'm trying to use the function copy_to_user() to transfer an integer into user space but it transfers only 1 byte. For example, if I try to transfer the integer 300, it transfers only the first byte from 300:

300=12Ch

in this case only 2Ch will be transfered, so in the user space, with the function fread(), I get always 44 (44 = 2Ch). I have tried to change the parameters from fread(), to take more bytes, but it didn't work. I'm still getting only 1 byte per transfer.

Any Idea what could be this problem?

Thank you,
Leonardo

osor 02-22-2008 03:12 PM

Quote:

Originally Posted by leoremoto (Post 3066540)
Any Idea what could be this problem?

The function copy_to_user takes the number of bytes as its third parameter.

leoremoto 02-22-2008 03:18 PM

"The function copy_to_user takes the number of bytes as its third parameter."

I know, I have used also sizeof() in the third parameter but it does not work

osor 02-22-2008 03:29 PM

Would you show us some sample code?

leoremoto 02-22-2008 03:54 PM

ssize_t read(struct file *filp, int __user *buff,
size_t count, loff_t *offp)
{
int value;
copy_to_user(buf, &value,4);
return 1;
}


This is a simplfied version of this function's implementation. The rest is the same as in the "linux Device Drivers" book.
thank you for your help
Leonardo

osor 02-22-2008 04:18 PM

Quote:

Originally Posted by leoremoto (Post 3066589)
return 1;

You should return the number of bytes read (not 1, but 4). Other than that, it looks OK (except you never actually assign to value, and you have an extra ‘f’ in one of your variable names).

Btw, you if you enclose sourcecode like this: [code]Paste code here[/code], it is easier to read on the forum.

bay_sic 02-26-2008 04:29 AM

I seem to have stumbled on same kind of problem.

No matter how many bytes I try to send only one byte goes through. In kernel space I do like this:

Code:

copy_to_user(buf, &data_buf, 32);
So I want 32 bytes transferred which are in a buffer data_buf. In user space I have something like this:

Code:

bytes_read = fread(buf, size, count, MYDEVICE);
printf("\nFile size: %i", ftell(MYDEVICE));

fread should return how many data elements have been succesfully read. And that should go to same value as count, if I understood correctly. If I increase size from 1 that means bytes_read goes to zero. File size seems also to be 1B. Is this how it should be ??

As I said only first data byte gets through and I have hard time figuring that out...


Fixed it now. I had a couple of mishaps there. One of them was to somehow mix size and count in fread. Silly me!

dsw248 07-20-2012 07:19 AM

u may mistake the copy to user()
 
copy_to _user(__user *buf,__ker *buf,count) is transmit the address,and it has the bits limit (8 bit at most), the value count is to point out how many values that will transmit at the address

example :
int the test program

int distance[3];
...
read(fd, R_buf, 3);

int the read function of device driver


static ElemType dsw248_read(
struct file *file,
char __user *buf,
size_t count ,
loff_t *f_pos)
{
char ker_buf[3];
......
copy_to_user(buf, ker_buf, count);
return count;
}

that will trans 3 numbers at most and each number such as ker_buf[0] is 8 bit, that way 258 will return 2 (256+2) the 256 lost;


you can use __put_user() or __copy_to_user () to trans int numbers


使用 __put_user() 来实现数据复制。说明一下,put_user 和 copy_to_user 都是有内核地址检查的。 put_user() 是有 access_ok() 代码的 __put_user(), 其执行小数据(1、2、4、8字节)的复制,效率比 copy_to_user 稍高。

sundialsvcs 07-20-2012 01:28 PM

Hey... the Linux kernel is stuffed with "working" examples of this function. Simply grep one, and copy it exactly!

"Use the Source, Luke!!"


All times are GMT -5. The time now is 01:40 AM.