Doubt on show() function of sysfs attribute
Hello everyone, I am going through Documentation/filesystems/sysfs.rst. It says that sysfs allocates a buffer of size PAGE_SIZE and passes it to the show/store functions of the attribute. On read(), the show() method should fill the entire buffer[0]. The document also says that sysfs_emit() and sysfs_emit_at() are the preferred function calls in show() function when formatting the value to be returned to the user space [1]. I checked the sysfs_emit() function definition. sysfs_emit() -> vscnprintf() -> vsnprintf() is the function series. (-> = calls) In the vsnprintf() function, i found that only in the case of (start + size > end) the buffer is completely filled, in the other case, that's not the case [2]. Isn't this contradictory to what the sysfs.rst says? (that entire buffer should be filled) Can you please help me understand what i am missing here? Thank you for your time. Cheers, ymdatta. [0]: https://elixir.bootlin.com/linux/latest/source/Documentation/filesystems/sys... [1]: https://elixir.bootlin.com/linux/latest/source/Documentation/filesystems/sys... [2]: https://elixir.bootlin.com/linux/latest/source/lib/vsprintf.c#L2689
On Tue, 26 Jan 2021 23:07:24 +0530, Mohana Datta Yelugoti said:
Hello everyone,
I am going through Documentation/filesystems/sysfs.rst.
It says that sysfs allocates a buffer of size PAGE_SIZE and passes it to the show/store functions of the attribute. On read(), the show() method should fill the entire buffer[0].
'git blame' indicates that except for a commit that converted it to RST format, that file is essentially the same as it was when Linus originally imported the kernel tree into git. So it's describing the situation as it was in 2005, and probably way out of date.
On Tue, Jan 26, 2021 at 11:07:24PM +0530, Mohana Datta Yelugoti wrote:
Hello everyone,
I am going through Documentation/filesystems/sysfs.rst.
It says that sysfs allocates a buffer of size PAGE_SIZE and passes it to the show/store functions of the attribute. On read(), the show() method should fill the entire buffer[0].
No, that's not the case. Please only fill in what you need sent back to userspace and return the number of bytes you used. A simple call to sprintf() or better yet, sysfs_emit(), as a return statement is all you need to do. See the thousands of examples in the kernel of this if you need proof :) thanks, greg k-h
On Wed, Jan 27, 2021 at 9:10 AM Greg KH <greg@kroah.com> wrote:
On Tue, Jan 26, 2021 at 11:07:24PM +0530, Mohana Datta Yelugoti wrote:
Hello everyone,
I am going through Documentation/filesystems/sysfs.rst.
It says that sysfs allocates a buffer of size PAGE_SIZE and passes it to the show/store functions of the attribute. On read(), the show() method should fill the entire buffer[0].
No, that's not the case. Please only fill in what you need sent back to userspace and return the number of bytes you used. A simple call to sprintf() or better yet, sysfs_emit(), as a return statement is all you need to do.
See the thousands of examples in the kernel of this if you need proof :)
And Mohana, try it out and once you understand how the API really works, feel free to provide a patch to Documentation/filesystems/sysfs.rst to describe the actual behavior. Lukas
See the thousands of examples in the kernel of this if you need proof :)
True. I was actually confused because many kernel examples use sysfs_emit() and here in sysfs_emit(), the buffer is not completely filled. But, i got it now. I will experiment more and will see if i can send a patch updating sysfs.rst accordingly. Thank you. Cheers, ymdatta.
participants (4)
-
Greg KH -
Lukas Bulwahn -
Mohana Datta Yelugoti -
Valdis Klētnieks