40ad858
commit 15afd6b8d8263010ed41bca09e62fefec1b7b3f8
40ad858
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
40ad858
Date:   Fri Feb 5 13:18:58 2021 +0530
40ad858
40ad858
    tunables: Simplify TUNABLE_SET interface
40ad858
    
40ad858
    The TUNABLE_SET interface took a primitive C type argument, which
40ad858
    resulted in inconsistent type conversions internally due to incorrect
40ad858
    dereferencing of types, especialy on 32-bit architectures.  This
40ad858
    change simplifies the TUNABLE setting logic along with the interfaces.
40ad858
    
40ad858
    Now all numeric tunable values are stored as signed numbers in
40ad858
    tunable_num_t, which is intmax_t.  All calls to set tunables cast the
40ad858
    input value to its primitive type and then to tunable_num_t for
40ad858
    storage.  This relies on gcc-specific (although I suspect other
40ad858
    compilers woul also do the same) unsigned to signed integer conversion
40ad858
    semantics, i.e. the bit pattern is conserved.  The reverse conversion
40ad858
    is guaranteed by the standard.
40ad858
    
40ad858
    (cherry picked from commit 61117bfa1b08ca048e6512c0652c568300fedf6a)
40ad858
40ad858
diff --git a/elf/dl-tunable-types.h b/elf/dl-tunable-types.h
40ad858
index 3fcc0806f5f2ec04..626ca334be105e69 100644
40ad858
--- a/elf/dl-tunable-types.h
40ad858
+++ b/elf/dl-tunable-types.h
40ad858
@@ -38,8 +38,8 @@ typedef enum
40ad858
 typedef struct
40ad858
 {
40ad858
   tunable_type_code_t type_code;
40ad858
-  int64_t min;
40ad858
-  int64_t max;
40ad858
+  tunable_num_t min;
40ad858
+  tunable_num_t max;
40ad858
 } tunable_type_t;
40ad858
 
40ad858
 /* Security level for tunables.  This decides what to do with individual
40ad858
diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
40ad858
index b1a50b84693d15de..a2be9cde2f7333ea 100644
40ad858
--- a/elf/dl-tunables.c
40ad858
+++ b/elf/dl-tunables.c
40ad858
@@ -93,87 +93,45 @@ get_next_env (char **envp, char **name, size_t *namelen, char **val,
40ad858
   return NULL;
40ad858
 }
40ad858
 
40ad858
-#define TUNABLE_SET_VAL_IF_VALID_RANGE(__cur, __val, __type)		      \
40ad858
-({									      \
40ad858
-  __type min = (__cur)->type.min;					      \
40ad858
-  __type max = (__cur)->type.max;					      \
40ad858
-									      \
40ad858
-  if ((__type) (__val) >= min && (__type) (__val) <= max)		      \
40ad858
-    {									      \
40ad858
-      (__cur)->val.numval = (__val);					      \
40ad858
-      (__cur)->initialized = true;					      \
40ad858
-    }									      \
40ad858
-})
40ad858
-
40ad858
-#define TUNABLE_SET_BOUNDS_IF_VALID(__cur, __minp, __maxp, __type)	      \
40ad858
-({									      \
40ad858
-  if (__minp != NULL)							      \
40ad858
-    {									      \
40ad858
-      /* MIN is specified.  */						      \
40ad858
-      __type min = *((__type *) __minp);				      \
40ad858
-      if (__maxp != NULL)						      \
40ad858
-	{								      \
40ad858
-	   /* Both MIN and MAX are specified.  */			      \
40ad858
-	    __type max = *((__type *) __maxp);				      \
40ad858
-	  if (max >= min						      \
40ad858
-	      && max <= (__cur)->type.max				      \
40ad858
-	      && min >= (__cur)->type.min)				      \
40ad858
-	    {								      \
40ad858
-	      (__cur)->type.min = min;					      \
40ad858
-	      (__cur)->type.max = max;					      \
40ad858
-	    }								      \
40ad858
-	}								      \
40ad858
-      else if (min > (__cur)->type.min && min <= (__cur)->type.max)	      \
40ad858
-	{								      \
40ad858
-	  /* Only MIN is specified.  */					      \
40ad858
-	  (__cur)->type.min = min;					      \
40ad858
-	}								      \
40ad858
-    }									      \
40ad858
-  else if (__maxp != NULL)						      \
40ad858
-    {									      \
40ad858
-      /* Only MAX is specified.  */					      \
40ad858
-      __type max = *((__type *) __maxp);				      \
40ad858
-      if (max < (__cur)->type.max && max >= (__cur)->type.min)		      \
40ad858
-	(__cur)->type.max = max;					      \
40ad858
-    }									      \
40ad858
-})
40ad858
-
40ad858
 static void
40ad858
-do_tunable_update_val (tunable_t *cur, const void *valp,
40ad858
-		       const void *minp, const void *maxp)
40ad858
+do_tunable_update_val (tunable_t *cur, const tunable_val_t *valp,
40ad858
+		       const tunable_num_t *minp,
40ad858
+		       const tunable_num_t *maxp)
40ad858
 {
40ad858
-  uint64_t val;
40ad858
+  tunable_num_t val, min, max;
40ad858
 
40ad858
-  if (cur->type.type_code != TUNABLE_TYPE_STRING)
40ad858
-    val = *((int64_t *) valp);
40ad858
+  if (cur->type.type_code == TUNABLE_TYPE_STRING)
40ad858
+    {
40ad858
+      cur->val.strval = valp->strval;
40ad858
+      cur->initialized = true;
40ad858
+      return;
40ad858
+    }
40ad858
 
40ad858
-  switch (cur->type.type_code)
40ad858
+  val = valp->numval;
40ad858
+  min = minp != NULL ? *minp : cur->type.min;
40ad858
+  max = maxp != NULL ? *maxp : cur->type.max;
40ad858
+
40ad858
+  /* We allow only increasingly restrictive bounds.  */
40ad858
+  if (min < cur->type.min)
40ad858
+    min = cur->type.min;
40ad858
+
40ad858
+  if (max > cur->type.max)
40ad858
+    max = cur->type.max;
40ad858
+
40ad858
+  /* Skip both bounds if they're inconsistent.  */
40ad858
+  if (min > max)
40ad858
     {
40ad858
-    case TUNABLE_TYPE_INT_32:
40ad858
-	{
40ad858
-	  TUNABLE_SET_BOUNDS_IF_VALID (cur, minp, maxp, int64_t);
40ad858
-	  TUNABLE_SET_VAL_IF_VALID_RANGE (cur, val, int64_t);
40ad858
-	  break;
40ad858
-	}
40ad858
-    case TUNABLE_TYPE_UINT_64:
40ad858
-	{
40ad858
-	  TUNABLE_SET_BOUNDS_IF_VALID (cur, minp, maxp, uint64_t);
40ad858
-	  TUNABLE_SET_VAL_IF_VALID_RANGE (cur, val, uint64_t);
40ad858
-	  break;
40ad858
-	}
40ad858
-    case TUNABLE_TYPE_SIZE_T:
40ad858
-	{
40ad858
-	  TUNABLE_SET_BOUNDS_IF_VALID (cur, minp, maxp, uint64_t);
40ad858
-	  TUNABLE_SET_VAL_IF_VALID_RANGE (cur, val, uint64_t);
40ad858
-	  break;
40ad858
-	}
40ad858
-    case TUNABLE_TYPE_STRING:
40ad858
-	{
40ad858
-	  cur->val.strval = valp;
40ad858
-	  break;
40ad858
-	}
40ad858
-    default:
40ad858
-      __builtin_unreachable ();
40ad858
+      min = cur->type.min;
40ad858
+      max = cur->type.max;
40ad858
+    }
40ad858
+
40ad858
+  /* Write everything out if the value and the bounds are valid.  */
40ad858
+  if (min <= val && val <= max)
40ad858
+    {
40ad858
+      cur->val.numval = val;
40ad858
+      cur->type.min = min;
40ad858
+      cur->type.max = max;
40ad858
+      cur->initialized = true;
40ad858
     }
40ad858
 }
40ad858
 
40ad858
@@ -182,24 +140,18 @@ do_tunable_update_val (tunable_t *cur, const void *valp,
40ad858
 static void
40ad858
 tunable_initialize (tunable_t *cur, const char *strval)
40ad858
 {
40ad858
-  uint64_t val;
40ad858
-  const void *valp;
40ad858
+  tunable_val_t val;
40ad858
 
40ad858
   if (cur->type.type_code != TUNABLE_TYPE_STRING)
40ad858
-    {
40ad858
-      val = _dl_strtoul (strval, NULL);
40ad858
-      valp = &val;
40ad858
-    }
40ad858
+    val.numval = (tunable_num_t) _dl_strtoul (strval, NULL);
40ad858
   else
40ad858
-    {
40ad858
-      cur->initialized = true;
40ad858
-      valp = strval;
40ad858
-    }
40ad858
-  do_tunable_update_val (cur, valp, NULL, NULL);
40ad858
+    val.strval = strval;
40ad858
+  do_tunable_update_val (cur, &val, NULL, NULL);
40ad858
 }
40ad858
 
40ad858
 void
40ad858
-__tunable_set_val (tunable_id_t id, void *valp, void *minp, void *maxp)
40ad858
+__tunable_set_val (tunable_id_t id, tunable_val_t *valp, tunable_num_t *minp,
40ad858
+		   tunable_num_t *maxp)
40ad858
 {
40ad858
   tunable_t *cur = &tunable_list[id];
40ad858
 
40ad858
diff --git a/elf/dl-tunables.h b/elf/dl-tunables.h
40ad858
index 971376ba8d3bad1c..ba7ae6b52ecea7a3 100644
40ad858
--- a/elf/dl-tunables.h
40ad858
+++ b/elf/dl-tunables.h
40ad858
@@ -33,9 +33,11 @@ __tunables_init (char **unused __attribute__ ((unused)))
40ad858
 # include <stddef.h>
40ad858
 # include <stdint.h>
40ad858
 
40ad858
+typedef intmax_t tunable_num_t;
40ad858
+
40ad858
 typedef union
40ad858
 {
40ad858
-  int64_t numval;
40ad858
+  tunable_num_t numval;
40ad858
   const char *strval;
40ad858
 } tunable_val_t;
40ad858
 
40ad858
@@ -52,7 +54,8 @@ typedef void (*tunable_callback_t) (tunable_val_t *);
40ad858
 extern void __tunables_init (char **);
40ad858
 extern void __tunables_print (void);
40ad858
 extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t);
40ad858
-extern void __tunable_set_val (tunable_id_t, void *, void *, void *);
40ad858
+extern void __tunable_set_val (tunable_id_t, tunable_val_t *, tunable_num_t *,
40ad858
+			       tunable_num_t *);
40ad858
 rtld_hidden_proto (__tunables_init)
40ad858
 rtld_hidden_proto (__tunables_print)
40ad858
 rtld_hidden_proto (__tunable_get_val)
40ad858
@@ -64,20 +67,18 @@ rtld_hidden_proto (__tunable_set_val)
40ad858
 #if defined TOP_NAMESPACE && defined TUNABLE_NAMESPACE
40ad858
 # define TUNABLE_GET(__id, __type, __cb) \
40ad858
   TUNABLE_GET_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, __type, __cb)
40ad858
-# define TUNABLE_SET(__id, __type, __val) \
40ad858
-  TUNABLE_SET_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, __type, __val)
40ad858
-# define TUNABLE_SET_WITH_BOUNDS(__id, __type, __val, __min, __max) \
40ad858
+# define TUNABLE_SET(__id, __val) \
40ad858
+  TUNABLE_SET_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, __val)
40ad858
+# define TUNABLE_SET_WITH_BOUNDS(__id, __val, __min, __max) \
40ad858
   TUNABLE_SET_WITH_BOUNDS_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, \
40ad858
-				__type, __val, __min, __max)
40ad858
+				__val, __min, __max)
40ad858
 #else
40ad858
 # define TUNABLE_GET(__top, __ns, __id, __type, __cb) \
40ad858
   TUNABLE_GET_FULL (__top, __ns, __id, __type, __cb)
40ad858
-# define TUNABLE_SET(__top, __ns, __id, __type, __val) \
40ad858
-  TUNABLE_SET_FULL (__top, __ns, __id, __type, __val)
40ad858
-# define TUNABLE_SET_WITH_BOUNDS(__top, __ns, __id, __type, __val, \
40ad858
-				 __min, __max) \
40ad858
-  TUNABLE_SET_WITH_BOUNDS_FULL (__top, __ns, __id, __type, __val, \
40ad858
-				__min, __max)
40ad858
+# define TUNABLE_SET(__top, __ns, __id, __val) \
40ad858
+  TUNABLE_SET_FULL (__top, __ns, __id, __val)
40ad858
+# define TUNABLE_SET_WITH_BOUNDS(__top, __ns, __id, __val, __min, __max) \
40ad858
+  TUNABLE_SET_WITH_BOUNDS_FULL (__top, __ns, __id, __val, __min, __max)
40ad858
 #endif
40ad858
 
40ad858
 /* Get and return a tunable value.  If the tunable was set externally and __CB
40ad858
@@ -91,19 +92,19 @@ rtld_hidden_proto (__tunable_set_val)
40ad858
 })
40ad858
 
40ad858
 /* Set a tunable value.  */
40ad858
-# define TUNABLE_SET_FULL(__top, __ns, __id, __type, __val) \
40ad858
+# define TUNABLE_SET_FULL(__top, __ns, __id, __val) \
40ad858
 ({									      \
40ad858
   __tunable_set_val (TUNABLE_ENUM_NAME (__top, __ns, __id),		      \
40ad858
-		     & (__type) {__val}, NULL, NULL);			      \
40ad858
+		     & (tunable_val_t) {.numval = __val}, NULL, NULL);	      \
40ad858
 })
40ad858
 
40ad858
 /* Set a tunable value together with min/max values.  */
40ad858
-# define TUNABLE_SET_WITH_BOUNDS_FULL(__top, __ns, __id, __type, __val,	      \
40ad858
-				      __min, __max)			      \
40ad858
+# define TUNABLE_SET_WITH_BOUNDS_FULL(__top, __ns, __id,__val, __min, __max)  \
40ad858
 ({									      \
40ad858
   __tunable_set_val (TUNABLE_ENUM_NAME (__top, __ns, __id),		      \
40ad858
-		     & (__type) {__val},  & (__type) {__min},		      \
40ad858
-		     & (__type) {__max});				      \
40ad858
+		     & (tunable_val_t) {.numval = __val},		      \
40ad858
+		     & (tunable_num_t) {__min},				      \
40ad858
+		     & (tunable_num_t) {__max});			      \
40ad858
 })
40ad858
 
40ad858
 /* Namespace sanity for callback functions.  Use this macro to keep the
40ad858
diff --git a/manual/README.tunables b/manual/README.tunables
40ad858
index d8c768abcc70b5a4..605ddd78cd52b5ef 100644
40ad858
--- a/manual/README.tunables
40ad858
+++ b/manual/README.tunables
40ad858
@@ -98,17 +98,16 @@ where it can expect the tunable value to be passed in VALP.
40ad858
 
40ad858
 Tunables in the module can be updated using:
40ad858
 
40ad858
-  TUNABLE_SET (check, int32_t, val)
40ad858
+  TUNABLE_SET (check, val)
40ad858
 
40ad858
-where 'check' is the tunable name, 'int32_t' is the C type of the tunable and
40ad858
-'val' is a value of same type.
40ad858
+where 'check' is the tunable name and 'val' is a value of same type.
40ad858
 
40ad858
 To get and set tunables in a different namespace from that module, use the full
40ad858
 form of the macros as follows:
40ad858
 
40ad858
   val = TUNABLE_GET_FULL (glibc, cpu, hwcap_mask, uint64_t, NULL)
40ad858
 
40ad858
-  TUNABLE_SET_FULL (glibc, cpu, hwcap_mask, uint64_t, val)
40ad858
+  TUNABLE_SET_FULL (glibc, cpu, hwcap_mask, val)
40ad858
 
40ad858
 where 'glibc' is the top namespace, 'cpu' is the tunable namespace and the
40ad858
 remaining arguments are the same as the short form macros.
40ad858
@@ -116,18 +115,17 @@ remaining arguments are the same as the short form macros.
40ad858
 The minimum and maximum values can updated together with the tunable value
40ad858
 using:
40ad858
 
40ad858
-  TUNABLE_SET_WITH_BOUNDS (check, int32_t, val, min, max)
40ad858
+  TUNABLE_SET_WITH_BOUNDS (check, val, min, max)
40ad858
 
40ad858
-where 'check' is the tunable name, 'int32_t' is the C type of the tunable,
40ad858
-'val' is a value of same type, 'min' and 'max' are the minimum and maximum
40ad858
-values of the tunable.
40ad858
+where 'check' is the tunable name, 'val' is a value of same type, 'min' and
40ad858
+'max' are the minimum and maximum values of the tunable.
40ad858
 
40ad858
 To set the minimum and maximum values of tunables in a different namespace
40ad858
 from that module, use the full form of the macros as follows:
40ad858
 
40ad858
   val = TUNABLE_GET_FULL (glibc, cpu, hwcap_mask, uint64_t, NULL)
40ad858
 
40ad858
-  TUNABLE_SET_WITH_BOUNDS_FULL (glibc, cpu, hwcap_mask, uint64_t, val, min, max)
40ad858
+  TUNABLE_SET_WITH_BOUNDS_FULL (glibc, cpu, hwcap_mask, val, min, max)
40ad858
 
40ad858
 where 'glibc' is the top namespace, 'cpu' is the tunable namespace and the
40ad858
 remaining arguments are the same as the short form macros.
40ad858
diff --git a/sysdeps/unix/sysv/linux/aarch64/cpu-features.c b/sysdeps/unix/sysv/linux/aarch64/cpu-features.c
40ad858
index fe52b6308ee0ff72..db6aa3516c1b5cb1 100644
40ad858
--- a/sysdeps/unix/sysv/linux/aarch64/cpu-features.c
40ad858
+++ b/sysdeps/unix/sysv/linux/aarch64/cpu-features.c
40ad858
@@ -104,7 +104,7 @@ init_cpu_features (struct cpu_features *cpu_features)
40ad858
   cpu_features->mte_state = (GLRO (dl_hwcap2) & HWCAP2_MTE) ? mte_state : 0;
40ad858
   /* If we lack the MTE feature, disable the tunable, since it will
40ad858
      otherwise cause instructions that won't run on this CPU to be used.  */
40ad858
-  TUNABLE_SET (glibc, mem, tagging, unsigned, cpu_features->mte_state);
40ad858
+  TUNABLE_SET (glibc, mem, tagging, cpu_features->mte_state);
40ad858
 # endif
40ad858
 
40ad858
   if (cpu_features->mte_state & 2)
40ad858
diff --git a/sysdeps/x86/dl-cacheinfo.h b/sysdeps/x86/dl-cacheinfo.h
40ad858
index a31fa0783a23a9d8..e0a72568d8174db7 100644
40ad858
--- a/sysdeps/x86/dl-cacheinfo.h
40ad858
+++ b/sysdeps/x86/dl-cacheinfo.h
40ad858
@@ -917,17 +917,14 @@ dl_init_cacheinfo (struct cpu_features *cpu_features)
40ad858
   rep_stosb_threshold = TUNABLE_GET (x86_rep_stosb_threshold,
40ad858
 				     long int, NULL);
40ad858
 
40ad858
-  TUNABLE_SET_WITH_BOUNDS (x86_data_cache_size, long int, data,
40ad858
+  TUNABLE_SET_WITH_BOUNDS (x86_data_cache_size, data, 0, (long int) -1);
40ad858
+  TUNABLE_SET_WITH_BOUNDS (x86_shared_cache_size, shared, 0, (long int) -1);
40ad858
+  TUNABLE_SET_WITH_BOUNDS (x86_non_temporal_threshold, non_temporal_threshold,
40ad858
 			   0, (long int) -1);
40ad858
-  TUNABLE_SET_WITH_BOUNDS (x86_shared_cache_size, long int, shared,
40ad858
-			   0, (long int) -1);
40ad858
-  TUNABLE_SET_WITH_BOUNDS (x86_non_temporal_threshold, long int,
40ad858
-			   non_temporal_threshold, 0, (long int) -1);
40ad858
-  TUNABLE_SET_WITH_BOUNDS (x86_rep_movsb_threshold, long int,
40ad858
-			   rep_movsb_threshold,
40ad858
+  TUNABLE_SET_WITH_BOUNDS (x86_rep_movsb_threshold, rep_movsb_threshold,
40ad858
 			   minimum_rep_movsb_threshold, (long int) -1);
40ad858
-  TUNABLE_SET_WITH_BOUNDS (x86_rep_stosb_threshold, long int,
40ad858
-			   rep_stosb_threshold, 1, (long int) -1);
40ad858
+  TUNABLE_SET_WITH_BOUNDS (x86_rep_stosb_threshold, rep_stosb_threshold, 1,
40ad858
+			   (long int) -1);
40ad858
 #endif
40ad858
 
40ad858
   cpu_features->data_cache_size = data;