1  /* Lock elision tunable parameters.
       2     Copyright (C) 2014-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <config.h>
      20  #include <pthreadP.h>
      21  #include <elision-conf.h>
      22  #include <unistd.h>
      23  #include <dl-procinfo.h>
      24  
      25  #define TUNABLE_NAMESPACE elision
      26  #include <elf/dl-tunables.h>
      27  
      28  /* Reasonable initial tuning values, may be revised in the future.
      29     This is a conservative initial value.  */
      30  
      31  struct elision_config __elision_aconf =
      32    {
      33      /* How often to not attempt to use elision if a transaction aborted
      34         because the lock is already acquired.  Expressed in number of lock
      35         acquisition attempts.  */
      36      .skip_lock_busy = 3,
      37      /* How often to not attempt to use elision if a transaction aborted due
      38         to reasons other than other threads' memory accesses.  Expressed in
      39         number of lock acquisition attempts.  */
      40      .skip_lock_internal_abort = 3,
      41      /* How often to not attempt to use elision if a lock used up all retries
      42         without success.  Expressed in number of lock acquisition attempts.  */
      43      .skip_lock_out_of_tbegin_retries = 3,
      44      /* How often we try using elision if there is chance for the transaction
      45         to finish execution (e.g., it wasn't aborted due to the lock being
      46         already acquired.  */
      47      .try_tbegin = 3,
      48      /* Same as SKIP_LOCK_INTERNAL_ABORT but for trylock.  */
      49      .skip_trylock_internal_abort = 3,
      50    };
      51  
      52  static inline void
      53  __always_inline
      54  do_set_elision_enable (int32_t elision_enable)
      55  {
      56    /* Enable elision if it's available in hardware. It's not necessary to check
      57       if __libc_enable_secure isn't enabled since elision_enable will be set
      58       according to the default, which is disabled.  */
      59    if (elision_enable == 1)
      60      __pthread_force_elision = (GLRO (dl_hwcap) & HWCAP_S390_TE) ? 1 : 0;
      61  }
      62  
      63  /* The pthread->elision_enable tunable is 0 or 1 indicating that elision
      64     should be disabled or enabled respectively.  The feature will only be used
      65     if it's supported by the hardware.  */
      66  
      67  void
      68  TUNABLE_CALLBACK (set_elision_enable) (tunable_val_t *valp)
      69  {
      70    int32_t elision_enable = (int32_t) valp->numval;
      71    do_set_elision_enable (elision_enable);
      72  }
      73  
      74  #define TUNABLE_CALLBACK_FNDECL(__name, __type)			\
      75  static inline void						\
      76  __always_inline							\
      77  do_set_elision_ ## __name (__type value)			\
      78  {								\
      79    __elision_aconf.__name = value;				\
      80  }								\
      81  void								\
      82  TUNABLE_CALLBACK (set_elision_ ## __name) (tunable_val_t *valp) \
      83  {								\
      84    __type value = (__type) (valp)->numval;			\
      85    do_set_elision_ ## __name (value);				\
      86  }
      87  
      88  TUNABLE_CALLBACK_FNDECL (skip_lock_busy, int32_t);
      89  TUNABLE_CALLBACK_FNDECL (skip_lock_internal_abort, int32_t);
      90  TUNABLE_CALLBACK_FNDECL (skip_lock_out_of_tbegin_retries, int32_t);
      91  TUNABLE_CALLBACK_FNDECL (try_tbegin, int32_t);
      92  TUNABLE_CALLBACK_FNDECL (skip_trylock_internal_abort, int32_t);
      93  
      94  /* Initialize elison.  */
      95  
      96  void
      97  __lll_elision_init (void)
      98  {
      99    /* Elision depends on tunables and must be explicitly turned on by setting
     100       the appropriate tunable on a supported platform.  */
     101  
     102    TUNABLE_GET (enable, int32_t,
     103  	       TUNABLE_CALLBACK (set_elision_enable));
     104    TUNABLE_GET (skip_lock_busy, int32_t,
     105  	       TUNABLE_CALLBACK (set_elision_skip_lock_busy));
     106    TUNABLE_GET (skip_lock_internal_abort, int32_t,
     107  	       TUNABLE_CALLBACK (set_elision_skip_lock_internal_abort));
     108    TUNABLE_GET (skip_lock_after_retries, int32_t,
     109  	       TUNABLE_CALLBACK (set_elision_skip_lock_out_of_tbegin_retries));
     110    TUNABLE_GET (tries, int32_t,
     111  	       TUNABLE_CALLBACK (set_elision_try_tbegin));
     112    TUNABLE_GET (skip_trylock_internal_abort, int32_t,
     113  	       TUNABLE_CALLBACK (set_elision_skip_trylock_internal_abort));
     114  
     115    if (!__pthread_force_elision)
     116      __elision_aconf.try_tbegin = 0; /* Disable elision on rwlocks.  */
     117  }