[PATCH 08/12] tools/nolibc: gettimeofday(): avoid libgcc 64-bit divisions

Thomas Weißschuh posted 12 patches 3 months, 1 week ago
[PATCH 08/12] tools/nolibc: gettimeofday(): avoid libgcc 64-bit divisions
Posted by Thomas Weißschuh 3 months, 1 week ago
timespec::tv_nsec is going to be 64-bit wide even on 32-bit
architectures. As not all architectures support 64-bit division
instructions, calls to libgcc (__divdi3()) may be emitted by the
compiler which are not provided by nolibc.

As tv_nsec is guaranteed to always fit into an uint32_t, perform a
32-bit division instead.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 tools/include/nolibc/sys/time.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/include/nolibc/sys/time.h b/tools/include/nolibc/sys/time.h
index 33782a19aae9..6dd3705c6c9d 100644
--- a/tools/include/nolibc/sys/time.h
+++ b/tools/include/nolibc/sys/time.h
@@ -33,7 +33,7 @@ int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
 	ret = sys_clock_gettime(CLOCK_REALTIME, &tp);
 	if (!ret && tv) {
 		tv->tv_sec = tp.tv_sec;
-		tv->tv_usec = tp.tv_nsec / 1000;
+		tv->tv_usec = (uint32_t)tp.tv_nsec / 1000;
 	}
 
 	return ret;

-- 
2.51.1.dirty

Re: [PATCH 08/12] tools/nolibc: gettimeofday(): avoid libgcc 64-bit divisions
Posted by Willy Tarreau 3 months, 1 week ago
On Wed, Oct 29, 2025 at 05:02:58PM +0100, Thomas Weißschuh wrote:
> timespec::tv_nsec is going to be 64-bit wide even on 32-bit
> architectures. As not all architectures support 64-bit division
> instructions, calls to libgcc (__divdi3()) may be emitted by the
> compiler which are not provided by nolibc.
> 
> As tv_nsec is guaranteed to always fit into an uint32_t, perform a
> 32-bit division instead.
>
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  tools/include/nolibc/sys/time.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/include/nolibc/sys/time.h b/tools/include/nolibc/sys/time.h
> index 33782a19aae9..6dd3705c6c9d 100644
> --- a/tools/include/nolibc/sys/time.h
> +++ b/tools/include/nolibc/sys/time.h
> @@ -33,7 +33,7 @@ int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
>  	ret = sys_clock_gettime(CLOCK_REALTIME, &tp);
>  	if (!ret && tv) {
>  		tv->tv_sec = tp.tv_sec;
> -		tv->tv_usec = tp.tv_nsec / 1000;
> +		tv->tv_usec = (uint32_t)tp.tv_nsec / 1000;
>  	}

Good catch! I'm wondering if this one shouldn't be marked as a build
fix for 5e7392dc82ed ("tools/nolibc: fall back to sys_clock_gettime()
in gettimeofday()") so that it can be backported.

Acked-by: Willy Tarreau <w@1wt.eu>

Willy
Re: [PATCH 08/12] tools/nolibc: gettimeofday(): avoid libgcc 64-bit divisions
Posted by Thomas Weißschuh 3 months, 1 week ago
On 2025-11-02 09:31:56+0100, Willy Tarreau wrote:
> On Wed, Oct 29, 2025 at 05:02:58PM +0100, Thomas Weißschuh wrote:
> > timespec::tv_nsec is going to be 64-bit wide even on 32-bit
> > architectures. As not all architectures support 64-bit division
> > instructions, calls to libgcc (__divdi3()) may be emitted by the
> > compiler which are not provided by nolibc.
> > 
> > As tv_nsec is guaranteed to always fit into an uint32_t, perform a
> > 32-bit division instead.
> >
> > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > ---
> >  tools/include/nolibc/sys/time.h | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/tools/include/nolibc/sys/time.h b/tools/include/nolibc/sys/time.h
> > index 33782a19aae9..6dd3705c6c9d 100644
> > --- a/tools/include/nolibc/sys/time.h
> > +++ b/tools/include/nolibc/sys/time.h
> > @@ -33,7 +33,7 @@ int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
> >  	ret = sys_clock_gettime(CLOCK_REALTIME, &tp);
> >  	if (!ret && tv) {
> >  		tv->tv_sec = tp.tv_sec;
> > -		tv->tv_usec = tp.tv_nsec / 1000;
> > +		tv->tv_usec = (uint32_t)tp.tv_nsec / 1000;
> >  	}
> 
> Good catch! I'm wondering if this one shouldn't be marked as a build
> fix for 5e7392dc82ed ("tools/nolibc: fall back to sys_clock_gettime()
> in gettimeofday()") so that it can be backported.

Right now timespec::tv_nsec is of type 'long', so it should only be
64-bits on architectures which have native 64-bit division instructions. 
But marking it as fix shouldn't hurt either.


Thomas
Re: [PATCH 08/12] tools/nolibc: gettimeofday(): avoid libgcc 64-bit divisions
Posted by Willy Tarreau 3 months, 1 week ago
On Sun, Nov 02, 2025 at 10:27:18AM +0100, Thomas Weißschuh wrote:
> On 2025-11-02 09:31:56+0100, Willy Tarreau wrote:
> > On Wed, Oct 29, 2025 at 05:02:58PM +0100, Thomas Weißschuh wrote:
> > > timespec::tv_nsec is going to be 64-bit wide even on 32-bit
> > > architectures. As not all architectures support 64-bit division
> > > instructions, calls to libgcc (__divdi3()) may be emitted by the
> > > compiler which are not provided by nolibc.
> > > 
> > > As tv_nsec is guaranteed to always fit into an uint32_t, perform a
> > > 32-bit division instead.
> > >
> > > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > > ---
> > >  tools/include/nolibc/sys/time.h | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/tools/include/nolibc/sys/time.h b/tools/include/nolibc/sys/time.h
> > > index 33782a19aae9..6dd3705c6c9d 100644
> > > --- a/tools/include/nolibc/sys/time.h
> > > +++ b/tools/include/nolibc/sys/time.h
> > > @@ -33,7 +33,7 @@ int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
> > >  	ret = sys_clock_gettime(CLOCK_REALTIME, &tp);
> > >  	if (!ret && tv) {
> > >  		tv->tv_sec = tp.tv_sec;
> > > -		tv->tv_usec = tp.tv_nsec / 1000;
> > > +		tv->tv_usec = (uint32_t)tp.tv_nsec / 1000;
> > >  	}
> > 
> > Good catch! I'm wondering if this one shouldn't be marked as a build
> > fix for 5e7392dc82ed ("tools/nolibc: fall back to sys_clock_gettime()
> > in gettimeofday()") so that it can be backported.
> 
> Right now timespec::tv_nsec is of type 'long', so it should only be
> 64-bits on architectures which have native 64-bit division instructions. 
> But marking it as fix shouldn't hurt either.

Ah yeah you're right. Don't bother with that then.

Thanks!
Willy
Re: [PATCH 08/12] tools/nolibc: gettimeofday(): avoid libgcc 64-bit divisions
Posted by Arnd Bergmann 3 months, 1 week ago
On Wed, Oct 29, 2025, at 17:02, Thomas Weißschuh wrote:
> timespec::tv_nsec is going to be 64-bit wide even on 32-bit
> architectures. As not all architectures support 64-bit division
> instructions, calls to libgcc (__divdi3()) may be emitted by the
> compiler which are not provided by nolibc.
>
> As tv_nsec is guaranteed to always fit into an uint32_t, perform a
> 32-bit division instead.
>
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Reviewed-by: Arnd Bergmann <arnd@arndb.de>