Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Thu, 1 Dec 2022 10:11:36 -0500
From: Rich Felker <dalias@...c.org>
To: Gregor Jasny <gjasny@...glemail.com>
Cc: musl@...ts.openwall.com
Subject: Re: Prefer monotonic clock for DNS lookup timeouts

On Thu, Dec 01, 2022 at 12:24:47PM +0100, Gregor Jasny wrote:
> Hello,
> 
> while looking for a reason for a failed DNS resolve I noticed that
> the mtime function which is used to calculate and decide on the
> timeout uses the wall clock instead of a monotonic clock:
> 
> static unsigned long mtime()
> {
> 	struct timespec ts;
> 	clock_gettime(CLOCK_REALTIME, &ts);
> 	return (unsigned long)ts.tv_sec * 1000
> 		+ ts.tv_nsec / 1000000;
> }
> 
> http://git.musl-libc.org/cgit/musl/tree/src/network/res_msend.c#n28
> 
> Is this a bug or intentional?

It was intentional, based on a belief that the monotonic clock might
not be present on all kernels. That seems to be incorrect for the
range of versions we "support" (>=2.6.0) but some archs unofficially
work back to mid 2.4.x or earlier with limited functionality (no
threads). Note for example that clock_gettime has fallback to the
gettimeofday syscall despite all kernels >=2.6.0 having clock_gettime
(though was it perhaps gated under some CONFIG_ for "realtime
features" at some point? this probably calls for some research...)

Switching to monotonic here has been on my radar for a while. I see
two decent ways to do it without any possibility of regression:

1. Have the above mtime() function fall back to CLOCK_REALTIME on
   ENOSYS, or

2. Go through with integrating a fallback for CLOCK_MONOTONIC I've had
   in draft for a long time that works on ancient kernels. It works by
   combining the seconds-resolution time from SYS_sysinfo uptime with
   the finer-grained-but-wrapping jiffy count from SYS_times too get a
   monotonic jiffies-resolution uptime.

The latter is cute/fun but a little bit of work to get right and I'm
not sure it's sufficiently useful to justify doing it. Option 1 seems
very reasonable.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.