|
Message-ID: <CAK8P3a04TcLnhEE+MmQGOLU_AcVD5Rb+8vCKFSRF-zg33161ig@mail.gmail.com> Date: Tue, 14 Jun 2022 18:50:40 +0200 From: Arnd Bergmann <arnd@...nel.org> To: musl@...ts.openwall.com Cc: Rich Felker <dalias@...c.org> Subject: Re: Question about musl's time() implementation in time.c On Fri, Jun 10, 2022 at 10:53 AM Zev Levy Stevenson <zevlevys@...il.com> wrote: > > Thank you for the responses, those reasons make sense to me. We are using a very customized toolchain but the kernel itself is standard. > We looked into it a bit further and we were able to reproduce the issue with a clean musl-gcc toolchain for x86_64 (version 1.2.2) on a Linux kernel that we took from a standard Ubuntu distribution. > Specifically, tests in the libc-test suite (https://wiki.musl-libc.org/libc-test.html) using the time() function fail sometimes, e.g. src/functional/utime.c, which fails on about ~3-4 runs in every 1,000 runs. This can be reduced to this type of code failing: > > t = time(0); > if(futimens(fd, ((struct timespec[2]){{.tv_nsec=UTIME_NOW},{.tv_nsec=UTIME_OMIT}})) != 0) return 1; > if (fstat(fd, &st) != 0) return 1; > if (st.st_atim.tv_sec < t) printf("time inconsistency\n"); > > When replacing the call to time(0) with a raw call to the Linux time() syscall the issue seems to disappear. On the other hand, using the clock_gettime syscall results in the same issue. > Perhaps this is an issue with the Linux implementation of these syscalls / vdso functions, in which case further research may be required, or maybe such consistency when using different methods for measuring the system time doesn't have to be guaranteed, in which case the tests should probably be modified to allow for small inaccuracies such as the one described above. I think the problem here is likely the inconsistency between the COCK_REALTIME and CLOCK_REALTIME_COARSE domains. The time() syscall in the kernel is based on CLOCK_REALTIME_COARSE, while musl uses CLOCK_REALTIME. The futimens() syscall in turn uses CLOCK_REALTIME_COARSE. The coarse time can be up to one timer tick behind, so reading CLOCK_REALTIME first can give you the exact second with a small nanosecond value, while the utime will still set the previous value. Can you change the test case to check if the later time is less than clock_getres(CLOCK_REALTIME_COARSE, ...) behind? Arnd
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.