public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Adhemerval Zanella <azanella@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc/azanella/futex-refactor] y2038: Convert aio_suspend to support 64 bit time
Date: Mon, 23 Nov 2020 20:49:36 +0000 (GMT)	[thread overview]
Message-ID: <20201123204936.07AA7388A412@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=f92702293d48478348df1bf5c9196f2af9cc63b4

commit f92702293d48478348df1bf5c9196f2af9cc63b4
Author: Lukasz Majewski <lukma@denx.de>
Date:   Mon Nov 23 17:47:20 2020 -0300

    y2038: Convert aio_suspend to support 64 bit time
    
    The aio_suspend function has been converted to support 64 bit time.
    In the __aio_suspend_common64() function not all occurrences of struct
    timespec, clock_gettime() and pthread_cond_timedwait() have been replaced
    with their 64 bit counterparts, as i686-gnu port (HURD) don't #define
    DONT_NEED_AIO_MISC_COND and requires them (as it is not yet supporting 64
    bit time for being Y2038 safe).
    
    This change uses:
    - New __futex_reltimed_wait64 (instead of futex_reltimed_wait)
    - New __futex_reltimed_wait_cancellable64
            (instead of futex_reltimed_wait_cancellable)
        from ./sysdeps/nptl/futex-helpers.h
    
    The aio_suspend() accepts relative timeout.
    
    The __aio_suspend() is supposed to be run on ports with __TIMESIZE !=64 and
    __WORDSIZE==32. It internally utilizes __aio_suspend_time64() and hence the
    conversion from 32 bit struct timespec to 64 bit one is required.
    
    For ports supporting 64 bit time the __aio_suspend_time64() will be used
    either via alias (to __aio_suspend when __TIMESIZE==64) or redirection
    (when -D_TIME_BITS=64 is passed).
    
    Build tests:
    ./src/scripts/build-many-glibcs.py glibcs
    
    ---
    Changes for v2:
    - Add missing -EOVERFLOW error handling for __futex_reltimed_wait64 and
      _futex_reltimed_wait_cancelable64
    
    Changes for v3:
    - Remove "__" prefix from futex_reltimed_wait64 and
      futex_reltimed_wait_cancellable64
    
    - Remove some code, as HURD is not defining DONT_NEED_AIO_MISC_COND
      (i.e. Linux ports are defining it) and add in-code explanation why
      the code is NOT converted to support 64 bit time.
    
    - Rewrite the commit message
    
    Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Diff:
---
 include/aio.h                 |  8 +++++++
 nptl/Versions                 |  1 +
 sysdeps/nptl/aio_misc.h       |  9 ++++----
 sysdeps/nptl/futex-internal.c |  2 ++
 sysdeps/nptl/futex-internal.h |  6 +++--
 sysdeps/pthread/aio_suspend.c | 53 +++++++++++++++++++++++++++++++++++++++----
 6 files changed, 69 insertions(+), 10 deletions(-)

diff --git a/include/aio.h b/include/aio.h
index 90c74f9951..c7f4233310 100644
--- a/include/aio.h
+++ b/include/aio.h
@@ -9,6 +9,14 @@ extern void __aio_init (const struct aioinit *__init);
    lio_listio and we do not issue events for each individual list
    element.  */
 #define LIO_NO_INDIVIDUAL_EVENT	128
+
+# if __TIMESIZE == 64
+#  define __aio_suspend_time64 __aio_suspend
+# else
+extern int __aio_suspend_time64 (const struct aiocb *const list[], int nent,
+                                 const struct __timespec64 *timeout);
+librt_hidden_proto (__aio_suspend_time64)
+# endif
 #endif
 
 #endif
diff --git a/nptl/Versions b/nptl/Versions
index aed118e717..02650fe91c 100644
--- a/nptl/Versions
+++ b/nptl/Versions
@@ -302,6 +302,7 @@ libpthread {
     __pthread_clock_gettime; __pthread_clock_settime;
     __pthread_unwind; __pthread_get_minstack;
     __pthread_barrier_init; __pthread_barrier_wait;
+    __futex_abstimed_wait64; __futex_abstimed_wait_cancelable64;
     __shm_directory;
     __libpthread_freeres;
   }
diff --git a/sysdeps/nptl/aio_misc.h b/sysdeps/nptl/aio_misc.h
index 3f195f4794..dd8d99e5d6 100644
--- a/sysdeps/nptl/aio_misc.h
+++ b/sysdeps/nptl/aio_misc.h
@@ -45,11 +45,12 @@
 	do								      \
 	  {								      \
 	    if (cancel)							      \
-	      status = futex_reltimed_wait_cancelable (			      \
-		(unsigned int *) futexaddr, oldval, timeout, FUTEX_PRIVATE);  \
+	      status = __futex_abstimed_wait_cancelable64 (		      \
+		(unsigned int *) futexaddr, oldval, CLOCK_MONOTONIC, timeout, \
+		FUTEX_PRIVATE);						      \
 	    else							      \
-	      status = futex_reltimed_wait ((unsigned int *) futexaddr,	      \
-		oldval, timeout, FUTEX_PRIVATE);	      		      \
+	      status = __futex_abstimed_wait64 ((unsigned int *) futexaddr,   \
+		oldval, CLOCK_REALTIME, timeout, FUTEX_PRIVATE); 	      \
 	    if (status != EAGAIN)					      \
 	      break;							      \
 									      \
diff --git a/sysdeps/nptl/futex-internal.c b/sysdeps/nptl/futex-internal.c
index 11031cc46a..22ef312c2a 100644
--- a/sysdeps/nptl/futex-internal.c
+++ b/sysdeps/nptl/futex-internal.c
@@ -112,6 +112,7 @@ __futex_abstimed_wait64 (unsigned int* futex_word, unsigned int expected,
   return __futex_abstimed_wait_common64 (futex_word, expected, clockid,
                                          abstime, private, false);
 }
+libpthread_hidden_def (__futex_abstimed_wait64)
 
 int
 __futex_abstimed_wait_cancelable64 (unsigned int* futex_word,
@@ -122,3 +123,4 @@ __futex_abstimed_wait_cancelable64 (unsigned int* futex_word,
   return __futex_abstimed_wait_common64 (futex_word, expected, clockid,
                                          abstime, private, true);
 }
+libpthread_hidden_def (__futex_abstimed_wait_cancelable64)
diff --git a/sysdeps/nptl/futex-internal.h b/sysdeps/nptl/futex-internal.h
index 632051278b..2619f59f07 100644
--- a/sysdeps/nptl/futex-internal.h
+++ b/sysdeps/nptl/futex-internal.h
@@ -409,13 +409,15 @@ int
 __futex_abstimed_wait_cancelable64 (unsigned int* futex_word,
                                     unsigned int expected, clockid_t clockid,
                                     const struct __timespec64* abstime,
-                                    int private) attribute_hidden;
+                                    int private);
+libpthread_hidden_proto (__futex_abstimed_wait_cancelable64);
 
 int
 __futex_abstimed_wait64 (unsigned int* futex_word, unsigned int expected,
                          clockid_t clockid,
                          const struct __timespec64* abstime,
-                         int private) attribute_hidden;
+                         int private);
+libpthread_hidden_proto (__futex_abstimed_wait64);
 
 
 static __always_inline int
diff --git a/sysdeps/pthread/aio_suspend.c b/sysdeps/pthread/aio_suspend.c
index ad03f13558..52b1720569 100644
--- a/sysdeps/pthread/aio_suspend.c
+++ b/sysdeps/pthread/aio_suspend.c
@@ -94,7 +94,7 @@ cleanup (void *arg)
 #ifdef DONT_NEED_AIO_MISC_COND
 static int
 __attribute__ ((noinline))
-do_aio_misc_wait (unsigned int *cntr, const struct timespec *timeout)
+do_aio_misc_wait (unsigned int *cntr, const struct __timespec64 *timeout)
 {
   int result = 0;
 
@@ -105,8 +105,8 @@ do_aio_misc_wait (unsigned int *cntr, const struct timespec *timeout)
 #endif
 
 int
-aio_suspend (const struct aiocb *const list[], int nent,
-	     const struct timespec *timeout)
+__aio_suspend_common64 (const struct aiocb *const list[], int nent,
+                        const struct __timespec64 *timeout)
 {
   if (__glibc_unlikely (nent < 0))
     {
@@ -157,6 +157,20 @@ aio_suspend (const struct aiocb *const list[], int nent,
 	  break;
       }
 
+#ifdef DONT_NEED_AIO_MISC_COND
+  struct __timespec64 ts;
+  if (timeout != NULL)
+    {
+      clock_gettime (CLOCK_MONOTONIC, &ts);
+      ts.tv_sec += timeout->tv_sec;
+      ts.tv_nsec += timeout->tv_nsec;
+      if (ts.tv_nsec >= 1000000000)
+	{
+	  ts.tv_nsec -= 1000000000;
+	  ts.tv_sec++;
+	}
+    }
+#endif
 
   /* Only if none of the entries is NULL or finished to be wait.  */
   if (cnt == nent && any)
@@ -175,7 +189,7 @@ aio_suspend (const struct aiocb *const list[], int nent,
       pthread_cleanup_push (cleanup, &clparam);
 
 #ifdef DONT_NEED_AIO_MISC_COND
-      result = do_aio_misc_wait (&cntr, timeout);
+      result = do_aio_misc_wait (&cntr, timeout == NULL ? NULL : &ts);
 #else
       if (timeout == NULL)
 	result = pthread_cond_wait (&cond, &__aio_requests_mutex);
@@ -183,6 +197,14 @@ aio_suspend (const struct aiocb *const list[], int nent,
 	{
 	  /* We have to convert the relative timeout value into an
 	     absolute time value with pthread_cond_timedwait expects.  */
+	  /* Y2038: The below code is used by ports which do NOT
+	     #define DONT_NEED_AIO_MISC_COND flag (e.g. i686-gnu)
+	     Ports which define it use futex syscalls supporting 64 bit
+	     time. As a result below code don't need to be adjusted to
+	     support 64 bit time as it is advised to either add support
+	     for DONT_NEED_AIO_MISC_COND flag or provide
+	     __pthread_cond_timedwait64() implementation and use struct
+	     __timespec64 and __clock_gettime64().  */
 	  struct timespec now;
 	  struct timespec abstime;
 
@@ -250,4 +272,27 @@ aio_suspend (const struct aiocb *const list[], int nent,
   return result;
 }
 
+int
+__aio_suspend_time64 (const struct aiocb *const list[], int nent,
+		      const struct __timespec64 *timeout)
+{
+  return __aio_suspend_common64 (list, nent, timeout);
+}
+
+#if __TIMESIZE != 64
+librt_hidden_def (__aio_suspend_time64)
+
+int
+__aio_suspend (const struct aiocb *const list[], int nent,
+               const struct timespec *timeout)
+{
+  struct __timespec64 ts64;
+
+  if (timeout != NULL)
+    ts64 = valid_timespec_to_timespec64 (*timeout);
+
+  return __aio_suspend_time64 (list, nent, timeout != NULL ? &ts64 : NULL);
+}
+#endif
+weak_alias (__aio_suspend, aio_suspend)
 weak_alias (aio_suspend, aio_suspend64)


             reply	other threads:[~2020-11-23 20:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-23 20:49 Adhemerval Zanella [this message]
2020-11-23 21:08 Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201123204936.07AA7388A412@sourceware.org \
    --to=azanella@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).