public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-03-16 18:00 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-03-16 18:00 UTC (permalink / raw)
  To: glibc-cvs

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

commit bca96e18a53f58780c5753cc0a79086d5db79465
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-10-04 12:55 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-10-04 12:55 UTC (permalink / raw)
  To: glibc-cvs

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

commit 1b69625a0a0bde39e87f697a74cce90a026db78a
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 28 +++++++++++++++++++----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 ++--
 5 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..bea1e0e62d 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -12,7 +20,17 @@
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-06-09 21:15 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-06-09 21:15 UTC (permalink / raw)
  To: glibc-cvs

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

commit 590cb553a8d4c37f4739d96f8a346d03c2aee561
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-06-09 13:12 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-06-09 13:12 UTC (permalink / raw)
  To: glibc-cvs

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

commit 590cb553a8d4c37f4739d96f8a346d03c2aee561
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-06-03 14:01 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-06-03 14:01 UTC (permalink / raw)
  To: glibc-cvs

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

commit 7e1e9002672b3e197efb3991d4c813563052a85b
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-05-13 14:15 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-05-13 14:15 UTC (permalink / raw)
  To: glibc-cvs

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

commit 071a5b2a68423b79ca742115ca3cea878471b403
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-05-12 19:28 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-05-12 19:28 UTC (permalink / raw)
  To: glibc-cvs

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

commit ac1305d78451f47bbea25a6228c69171850e5216
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-05-10 18:19 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-05-10 18:19 UTC (permalink / raw)
  To: glibc-cvs

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

commit d48933f6eb5c196e4a538019c3db7c069c6ba188
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-04-29 13:59 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-04-29 13:59 UTC (permalink / raw)
  To: glibc-cvs

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

commit f61549c41083255977561392cab2ec721bf1b359
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-04-04 12:49 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-04-04 12:49 UTC (permalink / raw)
  To: glibc-cvs

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

commit 373ba7c185bfbf802c02915464d15bb3b0ca09ac
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-03-31 19:01 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-03-31 19:01 UTC (permalink / raw)
  To: glibc-cvs

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

commit 3e125f7df6df4a6e16274612e98c4c8e0ad9de48
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-03-29 20:25 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-03-29 20:25 UTC (permalink / raw)
  To: glibc-cvs

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

commit f7f08a29b833f2d54849f59e81a17fe06f7d93ee
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-03-15 18:38 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-03-15 18:38 UTC (permalink / raw)
  To: glibc-cvs

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

commit bca96e18a53f58780c5753cc0a79086d5db79465
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-03-11 17:22 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-03-11 17:22 UTC (permalink / raw)
  To: glibc-cvs

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

commit b19d7b655ca8b9cc7abdddb1c89855ed5de2aeb2
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface
@ 2022-03-10 19:21 Adhemerval Zanella
  0 siblings, 0 replies; 15+ messages in thread
From: Adhemerval Zanella @ 2022-03-10 19:21 UTC (permalink / raw)
  To: glibc-cvs

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

commit 4ec49bcf3e554c20aec0e2c6aea9dcdf31c40b00
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 9 17:12:03 2022 -0300

    linux: Avoid shifting a negative signed on POSIX timer interface
    
    The current macros uses pid as signed value, which triggers a compiler
    warning for process and thread timers.  Replace MAKE_PROCESS_CPUCLOCK
    with static inline function that expects the pid as unsigned.  These
    are similar to what Linux does internally.
    
    Checked on x86_64-linux-gnu.

Diff:
---
 nptl/pthread_getcpuclockid.c                      |  2 +-
 sysdeps/unix/sysv/linux/clock_getcpuclockid.c     |  2 +-
 sysdeps/unix/sysv/linux/clock_nanosleep.c         |  2 +-
 sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h | 30 ++++++++++++++++++-----
 sysdeps/unix/sysv/linux/timer_create.c            |  4 +--
 5 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 344bd6560e..b8bf09f550 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -35,7 +35,7 @@ __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 
   /* The clockid_t value is a simple computation from the TID.  */
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  const clockid_t tidclock = make_thread_cpuclock (pd->tid, CPUCLOCK_SCHED);
 
   *clockid = tidclock;
   return 0;
diff --git a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
index 5534127ed7..355d3c86af 100644
--- a/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
+++ b/sysdeps/unix/sysv/linux/clock_getcpuclockid.c
@@ -29,7 +29,7 @@ __clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
   /* The clockid_t value is a simple computation from the PID.
      But we do a clock_getres call to validate it.  */
 
-  const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
+  const clockid_t pidclock = make_process_cpuclock (pid, CPUCLOCK_SCHED);
 
 #ifndef __NR_clock_getres_time64
 # define __NR_clock_getres_time64 __NR_clock_getres
diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c
index befe6ecb8c..e610fd4e8d 100644
--- a/sysdeps/unix/sysv/linux/clock_nanosleep.c
+++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c
@@ -34,7 +34,7 @@ __clock_nanosleep_time64 (clockid_t clock_id, int flags,
   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
     return EINVAL;
   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
-    clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
+    clock_id = PROCESS_CLOCK;
 
   /* If the call is interrupted by a signal handler or encounters an error,
      it returns a positive value similar to errno.  */
diff --git a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
index 164a90ddeb..9c74bb1cf5 100644
--- a/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
+++ b/sysdeps/unix/sysv/linux/kernel-posix-cpu-timers.h
@@ -1,4 +1,12 @@
-/* Parameters for the Linux kernel ABI for CPU clocks.  */
+/*
+  Parameters for the Linux kernel ABI for CPU clocks, the bit fields within
+  a clockid:
+
+  - The most significant 29 bits hold either a pid or a file descriptor.
+  - Bit 2 indicates whether a cpu clock refers to a thread or a process.
+  - Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
+  - A clockid is invalid if bits 2, 1, and 0 are all set.
+ */
 
 #define CPUCLOCK_PID(clock)		((pid_t) ~((clock) >> 3))
 #define CPUCLOCK_PERTHREAD(clock) \
@@ -6,13 +14,23 @@
 #define CPUCLOCK_PID_MASK	7
 #define CPUCLOCK_PERTHREAD_MASK	4
 #define CPUCLOCK_WHICH(clock)	((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
-#define CPUCLOCK_CLOCK_MASK	3
+#define CPUCLOCK_CLOCK_MASK	3KE_PROCESS_CPUCLOCK
 #define CPUCLOCK_PROF		0
 #define CPUCLOCK_VIRT		1
 #define CPUCLOCK_SCHED		2
 #define CPUCLOCK_MAX		3
 
-#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
-	((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
-#define MAKE_THREAD_CPUCLOCK(tid, clock) \
-	MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
+static inline clockid_t
+make_process_cpuclock (unsigned int pid, clockid_t clock)
+{
+  return ((~pid) << 3) | clock;
+}
+
+static inline clockid_t
+make_thread_cpuclock (unsigned int tid, clockid_t clock)
+{
+  return make_process_cpuclock (tid, clock | CPUCLOCK_PERTHREAD_MASK);
+}
+
+#define PROCESS_CLOCK  make_process_cpuclock (0, CPUCLOCK_SCHED)
+#define THREAD_CLOCK   make_thread_cpuclock (0, CPUCLOCK_SCHED)
diff --git a/sysdeps/unix/sysv/linux/timer_create.c b/sysdeps/unix/sysv/linux/timer_create.c
index a8b2a41d9e..290324a7ea 100644
--- a/sysdeps/unix/sysv/linux/timer_create.c
+++ b/sysdeps/unix/sysv/linux/timer_create.c
@@ -33,9 +33,9 @@ ___timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
 {
   {
     clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
-				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? PROCESS_CLOCK
 				 : clock_id == CLOCK_THREAD_CPUTIME_ID
-				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
+				 ? THREAD_CLOCK
 				 : clock_id);
 
     /* If the user wants notification via a thread we need to handle


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2022-10-04 12:55 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-16 18:00 [glibc/azanella/clang] linux: Avoid shifting a negative signed on POSIX timer interface Adhemerval Zanella
  -- strict thread matches above, loose matches on Subject: below --
2022-10-04 12:55 Adhemerval Zanella
2022-06-09 21:15 Adhemerval Zanella
2022-06-09 13:12 Adhemerval Zanella
2022-06-03 14:01 Adhemerval Zanella
2022-05-13 14:15 Adhemerval Zanella
2022-05-12 19:28 Adhemerval Zanella
2022-05-10 18:19 Adhemerval Zanella
2022-04-29 13:59 Adhemerval Zanella
2022-04-04 12:49 Adhemerval Zanella
2022-03-31 19:01 Adhemerval Zanella
2022-03-29 20:25 Adhemerval Zanella
2022-03-15 18:38 Adhemerval Zanella
2022-03-11 17:22 Adhemerval Zanella
2022-03-10 19:21 Adhemerval Zanella

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).