Problem with reading (in userspace) from a kernel module
Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Problem with reading (in userspace) from a kernel module
Hello everyone,
I have an issue with a kernel module I'm trying to write.
The only purpose of the module is to be a buffer between 2 userspace programs (each reading and writing to the buffer).
The function I currently use to read from userspace:
Code:
/** This funtion is called when the /proc file is read */
static ssize_t module_read(struct file *file, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset)
{
...code...
...code...
...code...
return length; /* Return the number of bytes "read" */
}
has a the following problem:
every time it runs, it always receives 1024 in the 'length' var.
No matter how much bytes I really try to read in userspace.
Where 'sizeToRead' is an integer representing the amount of bytes I want to read, 'fd' is the file descriptor of the kernel module, and 'buffer' is the string I try to write.
Moreover, and even more strangely:
When the kernel module receives the request to get 1024 bytes, it sends to userspace 1024 bytes of data, and returns '1024' as number of bytes read (length variable).
But in userspace, the number I get in 'bytesRead' (how much bytes I read) is identical to 'sizeToRead' and not the real amount of data that was sent from the kernel module.
btw, I don't have this problem when writing to the kernel module:
/** This funtion is called when the /proc file is read */
static ssize_t module_read(struct file *file, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset)
{
...code...
...code...
...code...
return length; /* Return the number of bytes "read" */
}
Shouldn't this return your "bytesRead" variable?
Quote:
When the kernel module receives the request to get 1024 bytes, it sends to userspace 1024 bytes of data, and returns '1024' as number of bytes read (length variable).
But in userspace, the number I get in 'bytesRead' (how much bytes I read) is identical to 'sizeToRead' and not the real amount of data that was sent from the kernel module.
Did you verify this with a debugger or du you just assume so because of the results of your program?
BTW, if this is not just for the sake of practicing kernel module programming, don't write a module for this. There are several IPC mechanisms readily available (pipes, named pipes, shared memory areas) and one of them will probably suit your problem well.
I actually manipulate the 'Length' variable I get from the userspace, and in the end of the module_read function it holds the amount of bytes actually read (it may be smaller than the amount requested).
Quote:
Did you verify this with a debugger or du you just assume so because of the results of your program?
I verified with a debugger.
Quote:
BTW, if this is not just for the sake of practicing kernel module programming, don't write a module for this. There are several IPC mechanisms readily available (pipes, named pipes, shared memory areas) and one of them will probably suit your problem well.
Unfortunately it is
It's for a HW assignment in an OS course I'm taking.
Ok, maybe your implementation of the proc interface does not return the correct values... it seems to be a quite strange interface. The following is a working proc-read implementation i had to do for a university course:
Code:
static int proc_idedisk_read_breakeven
(char *page, char **start, off_t off, int count, int *eof, void *data)
{
ide_drive_t *drive = (ide_drive_t *) data;
int len = sprintf(page, "%u\n",
jiffies_to_msecs(drive->break_even_period));
PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
}
I just did some copy&paste development back then, so didn't bother much about the macro used:
every time it runs, it always receives 1024 in the 'length' var.
No matter how much bytes I really try to read in userspace.
If you want to read more than 1024 what happens ? The read function is call several time ? I have seen that when I wanted to write a large amount of data. I have not tried with less than 1024.
Ok, maybe your implementation of the proc interface does not return the correct values... it seems to be a quite strange interface. The following is a working proc-read implementation i had to do for a university course:
Code:
static int proc_idedisk_read_breakeven
(char *page, char **start, off_t off, int count, int *eof, void *data)
{
ide_drive_t *drive = (ide_drive_t *) data;
int len = sprintf(page, "%u\n",
jiffies_to_msecs(drive->break_even_period));
PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
}
I just did some copy&paste development back then, so didn't bother much about the macro used:
but it looks quite a little complicated. maybe you got something wrong in your code
I'm not sure I understand your code.
How do you send data to the userspace? using spritnf to page?
The way I do it is:
Code:
copy_to_user(buffer, procfs_buffer, length)
In either case, when I get length parameter in the function it's already 1024, so it doesn't change anything for me...
Quote:
Originally Posted by Odulo
If you want to read more than 1024 what happens ? The read function is call several time ? I have seen that when I wanted to write a large amount of data. I have not tried with less than 1024.
I haven't tried it yet, because I'm having a problem with smaller ones.
If I try to read 5 bytes out of a 10-byte buffer, the read_module function receives a command to read 1024 bytes, reads 10 bytes (the entire buffer content) and returns it to userspace (that only receives 5 first bytes of it).
So that's my problem...
Edit:
I did try to read more than 1024 bytes, and it seems the reads I get are in 1024 bytes blocks.
i.e. If I try to read 1500 bytes, I get 1024 in 'length'.
If I try to read 5000 bytes, I get 4096 in 'length'.
It's possible I implemented a block device instead of a char device.
Is it possible I need to create a char device using the mknod command, and then read/write from/to it?
(instead of directly from /proc/devicename)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.