* debugfs question ...
@ 2019-04-30 11:45 rdq
2019-04-30 12:07 ` Greg KH
2019-04-30 12:11 ` Martin Christian
0 siblings, 2 replies; 5+ messages in thread
From: rdq @ 2019-04-30 11:45 UTC (permalink / raw)
To: kernelnewbies
Greetings,
My I2C sensor driver has a debugfs entry for development purposes.
Everything works fine with the exception of the read operation. When 'cat'
is used, the read operation is called repeatedly and indefinitely. If the
read() is changed to return 0 then, as expected, nothing is displayed.
The pattern for the implementation is (AFAICT) right out of the book (shown
below).
What am I missing? Any thoughts much appreciated.
TAIA.
RDQ
static ssize_t sc031gs_reg_read_file(struct file *file, char __user
*user_buf,
size_t count, loff_t *ppos)
{
char *buf = 0;
ssize_t total = 0;
struct sc031gs_dev *sensor = file->private_data;
if (!sensor)
return -EINVAL;
if (*ppos < 0 || !count)
return -EINVAL;
buf = kmalloc(count, GFP_KERNEL);
if (!buf)
return -ENOMEM;
total = snprintf(buf,count,"Hello world\n");
if (total >= 0) {
if (copy_to_user(user_buf, buf, total)) {
kfree(buf);
return -EFAULT;
}
*ppos += total;
}
kfree(buf);
return total;
}
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: debugfs question ...
2019-04-30 11:45 debugfs question rdq
@ 2019-04-30 12:07 ` Greg KH
[not found] ` <014a01d50028$ce098580$6a1c9080$@metamail.co>
2019-04-30 12:11 ` Martin Christian
1 sibling, 1 reply; 5+ messages in thread
From: Greg KH @ 2019-04-30 12:07 UTC (permalink / raw)
To: rdq; +Cc: kernelnewbies
On Tue, Apr 30, 2019 at 12:45:08PM +0100, rdq@metamail.co wrote:
> Greetings,
>
> My I2C sensor driver has a debugfs entry for development purposes.
> Everything works fine with the exception of the read operation. When 'cat'
> is used, the read operation is called repeatedly and indefinitely. If the
> read() is changed to return 0 then, as expected, nothing is displayed.
>
> The pattern for the implementation is (AFAICT) right out of the book (shown
> below).
>
> What am I missing? Any thoughts much appreciated.
>
> TAIA.
>
> RDQ
>
> static ssize_t sc031gs_reg_read_file(struct file *file, char __user
> *user_buf,
> size_t count, loff_t *ppos)
> {
> char *buf = 0;
> ssize_t total = 0;
> struct sc031gs_dev *sensor = file->private_data;
> if (!sensor)
> return -EINVAL;
> if (*ppos < 0 || !count)
> return -EINVAL;
> buf = kmalloc(count, GFP_KERNEL);
> if (!buf)
> return -ENOMEM;
> total = snprintf(buf,count,"Hello world\n");
> if (total >= 0) {
> if (copy_to_user(user_buf, buf, total)) {
> kfree(buf);
> return -EFAULT;
> }
> *ppos += total;
> }
> kfree(buf);
> return total;
> }
You are returning a "short" read, and then disallowing ppos to be set to
a non-zero value? That's a recipie for disaster :(
Also, you allow userspace to allocate as much memory as it asks for?
Not good :(
Why not just use the simple_read_from_buffer() call? That handles all
of the "housekeeping" for you, and your function can be _much_ simpler.
good luck!
greg k-h
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: debugfs question ...
2019-04-30 11:45 debugfs question rdq
2019-04-30 12:07 ` Greg KH
@ 2019-04-30 12:11 ` Martin Christian
2019-05-01 12:06 ` rdq
1 sibling, 1 reply; 5+ messages in thread
From: Martin Christian @ 2019-04-30 12:11 UTC (permalink / raw)
To: kernelnewbies
[-- Attachment #1.1.1: Type: text/plain, Size: 1897 bytes --]
Hi,
my guess is your are not returning EOF (= 0). User space expects a
return value of 0 (EOF) to terminate reading. But your code will always
return sizeof("Hello world\n"). A fix would be to not only increment
ppos, but also check if it's already behind your data.
Regards,
Martin
Am 30.04.19 um 13:45 schrieb rdq@metamail.co:
> Greetings,
>
> My I2C sensor driver has a debugfs entry for development purposes.
> Everything works fine with the exception of the read operation. When 'cat'
> is used, the read operation is called repeatedly and indefinitely. If the
> read() is changed to return 0 then, as expected, nothing is displayed.
>
> The pattern for the implementation is (AFAICT) right out of the book (shown
> below).
>
> What am I missing? Any thoughts much appreciated.
>
> TAIA.
>
> RDQ
>
> static ssize_t sc031gs_reg_read_file(struct file *file, char __user
> *user_buf,
> size_t count, loff_t *ppos)
> {
> char *buf = 0;
> ssize_t total = 0;
> struct sc031gs_dev *sensor = file->private_data;
> if (!sensor)
> return -EINVAL;
> if (*ppos < 0 || !count)
> return -EINVAL;
> buf = kmalloc(count, GFP_KERNEL);
> if (!buf)
> return -ENOMEM;
> total = snprintf(buf,count,"Hello world\n");
> if (total >= 0) {
> if (copy_to_user(user_buf, buf, total)) {
> kfree(buf);
> return -EFAULT;
> }
> *ppos += total;
> }
> kfree(buf);
> return total;
> }
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies@kernelnewbies.org
> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
--
Dipl.-Inf. Martin Christian
Senior Berater Entwicklung Hardware
secunet Security Networks AG
Tel.: +49 201 5454-3612, Fax +49 201 5454-1323
E-Mail: martin.christian@secunet.com
Ammonstraße 74, 01067 Dresden
www.secunet.com
[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
[-- Attachment #2: Type: text/plain, Size: 170 bytes --]
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2019-05-01 14:35 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-30 11:45 debugfs question rdq
2019-04-30 12:07 ` Greg KH
[not found] ` <014a01d50028$ce098580$6a1c9080$@metamail.co>
2019-05-01 14:35 ` 'Greg KH'
2019-04-30 12:11 ` Martin Christian
2019-05-01 12:06 ` rdq
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).