Re: [PATCH] sysfs: make sysfs_emit() return ssize_t

From: Alexey Dobriyan
Date: Fri Mar 08 2024 - 01:26:13 EST


On Thu, Mar 07, 2024 at 10:04:41PM +0000, Greg Kroah-Hartman wrote:
> On Mon, Feb 05, 2024 at 01:11:36PM +0300, Alexey Dobriyan wrote:
> > sysfs_emit() is most often found in functions returning ssize_t
> > not int:
> >
> > static ssize_t oops_count_show(...)
> > {
> > return sysfs_emit(page, ...);
> > }
> >
> > This pattern results in sign-extension instruction between
> > sysfs_emit() return value (int) and caller return value (which is
> > ssize_t).
>
> Is that a problem?

Small problem, but, yes.

If sysfs_emit() returns "int", then every user compiles to

call sysfs_emit
movsx rax, eax
ret

Given than sysfs_emit() is the official way to print in sysfs,
there are lots of users and there will be more users in the future
as it grows.

This trailing "movsx" instruction is duplicated every time.

If sysfs_emit() returns ssize_t then it is folded into sysfs_emit() and
appears in the code _once_ saving bytes.

Ultimately, all this confusion and mismatches come from snprintf()
accepting "size_t" but returning "int" (despite working on very large
strings!) which sysfs_emit() copied.

> > But it is better to do sign-extension once inside sysfs_emit()
> > then duplicate it at nearly every call site on 64-bit.
>
> Why is that better? Does this affect code generation? If so, how much?
> And to what affect?
>
> And the function itself really is dealing with an int, it's up to the
> caller to want to do something with that, not the sysfs_emit() call
> itself.