Kernel Newbies Archive on lore.kernel.org
 help / color / Atom feed
* Doubt on show() function of sysfs attribute
@ 2021-01-26 17:37 Mohana Datta Yelugoti
  2021-01-27  3:51 ` Valdis Klētnieks
  2021-01-27  8:10 ` Greg KH
  0 siblings, 2 replies; 5+ messages in thread
From: Mohana Datta Yelugoti @ 2021-01-26 17:37 UTC (permalink / raw)
  To: kernelnewbies

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/sysfs.rst#L214

[1]:
https://elixir.bootlin.com/linux/latest/source/Documentation/filesystems/sysfs.rst#L246

[2]: https://elixir.bootlin.com/linux/latest/source/lib/vsprintf.c#L2689

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Doubt on show() function of sysfs attribute
  2021-01-26 17:37 Doubt on show() function of sysfs attribute Mohana Datta Yelugoti
@ 2021-01-27  3:51 ` Valdis Klētnieks
  2021-01-27  8:10 ` Greg KH
  1 sibling, 0 replies; 5+ messages in thread
From: Valdis Klētnieks @ 2021-01-27  3:51 UTC (permalink / raw)
  To: Mohana Datta Yelugoti; +Cc: kernelnewbies

[-- Attachment #1.1: Type: text/plain, Size: 594 bytes --]

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.

[-- Attachment #1.2: Type: application/pgp-signature, Size: 832 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

* Re: Doubt on show() function of sysfs attribute
  2021-01-26 17:37 Doubt on show() function of sysfs attribute Mohana Datta Yelugoti
  2021-01-27  3:51 ` Valdis Klētnieks
@ 2021-01-27  8:10 ` Greg KH
  2021-01-27  9:39   ` Lukas Bulwahn
  2021-01-27 16:21   ` Mohana Datta Yelugoti
  1 sibling, 2 replies; 5+ messages in thread
From: Greg KH @ 2021-01-27  8:10 UTC (permalink / raw)
  To: Mohana Datta Yelugoti; +Cc: kernelnewbies

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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Doubt on show() function of sysfs attribute
  2021-01-27  8:10 ` Greg KH
@ 2021-01-27  9:39   ` Lukas Bulwahn
  2021-01-27 16:21   ` Mohana Datta Yelugoti
  1 sibling, 0 replies; 5+ messages in thread
From: Lukas Bulwahn @ 2021-01-27  9:39 UTC (permalink / raw)
  To: Greg KH; +Cc: Mohana Datta Yelugoti, kernelnewbies

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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Doubt on show() function of sysfs attribute
  2021-01-27  8:10 ` Greg KH
  2021-01-27  9:39   ` Lukas Bulwahn
@ 2021-01-27 16:21   ` Mohana Datta Yelugoti
  1 sibling, 0 replies; 5+ messages in thread
From: Mohana Datta Yelugoti @ 2021-01-27 16:21 UTC (permalink / raw)
  To: Greg KH; +Cc: lukas.bulwahn, kernelnewbies


> 
> 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.

_______________________________________________
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, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-26 17:37 Doubt on show() function of sysfs attribute Mohana Datta Yelugoti
2021-01-27  3:51 ` Valdis Klētnieks
2021-01-27  8:10 ` Greg KH
2021-01-27  9:39   ` Lukas Bulwahn
2021-01-27 16:21   ` Mohana Datta Yelugoti

Kernel Newbies Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kernelnewbies/0 kernelnewbies/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kernelnewbies kernelnewbies/ https://lore.kernel.org/kernelnewbies \
		kernelnewbies@kernelnewbies.org
	public-inbox-index kernelnewbies

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernelnewbies.kernelnewbies


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git