1  /* elision-conf.c: Lock elision tunable parameters.
       2     Copyright (C) 2013-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 <init-arch.h>
      22  #include <elision-conf.h>
      23  #include <unistd.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 we retry using elision if there is chance for the transaction
      42         to finish execution (e.g., it wasn't aborted due to the lock being
      43         already acquired.  */
      44      .retry_try_xbegin = 3,
      45      /* Same as SKIP_LOCK_INTERNAL_ABORT but for trylock.  */
      46      .skip_trylock_internal_abort = 3,
      47    };
      48  
      49  static __always_inline void
      50  do_set_elision_enable (int32_t elision_enable)
      51  {
      52    /* Enable elision if it's available in hardware. It's not necessary to check
      53       if __libc_enable_secure isn't enabled since elision_enable will be set
      54       according to the default, which is disabled.  */
      55    if (elision_enable == 1)
      56      __pthread_force_elision = CPU_FEATURE_USABLE (RTM) ? 1 : 0;
      57  }
      58  
      59  /* The pthread->elision_enable tunable is 0 or 1 indicating that elision
      60     should be disabled or enabled respectively.  The feature will only be used
      61     if it's supported by the hardware.  */
      62  
      63  void
      64  TUNABLE_CALLBACK (set_elision_enable) (tunable_val_t *valp)
      65  {
      66    int32_t elision_enable = (int32_t) valp->numval;
      67    do_set_elision_enable (elision_enable);
      68  }
      69  
      70  #define TUNABLE_CALLBACK_FNDECL(__name, __type)			\
      71  static __always_inline void					\
      72  do_set_elision_ ## __name (__type value)			\
      73  {								\
      74    __elision_aconf.__name = value;				\
      75  }								\
      76  void								\
      77  TUNABLE_CALLBACK (set_elision_ ## __name) (tunable_val_t *valp) \
      78  {								\
      79    __type value = (__type) (valp)->numval;			\
      80    do_set_elision_ ## __name (value);				\
      81  }
      82  
      83  TUNABLE_CALLBACK_FNDECL (skip_lock_busy, int32_t);
      84  TUNABLE_CALLBACK_FNDECL (skip_lock_internal_abort, int32_t);
      85  TUNABLE_CALLBACK_FNDECL (retry_try_xbegin, int32_t);
      86  TUNABLE_CALLBACK_FNDECL (skip_trylock_internal_abort, int32_t);
      87  
      88  /* Initialize elision.  */
      89  
      90  void
      91  __lll_elision_init (void)
      92  {
      93    /* Elision depends on tunables and must be explicitly turned on by setting
      94       the appropriate tunable on a supported platform.  */
      95  
      96    TUNABLE_GET (enable, int32_t,
      97  	       TUNABLE_CALLBACK (set_elision_enable));
      98    TUNABLE_GET (skip_lock_busy, int32_t,
      99  	       TUNABLE_CALLBACK (set_elision_skip_lock_busy));
     100    TUNABLE_GET (skip_lock_internal_abort, int32_t,
     101  	       TUNABLE_CALLBACK (set_elision_skip_lock_internal_abort));
     102    TUNABLE_GET (tries, int32_t,
     103  	       TUNABLE_CALLBACK (set_elision_retry_try_xbegin));
     104    TUNABLE_GET (skip_trylock_internal_abort, int32_t,
     105  	       TUNABLE_CALLBACK (set_elision_skip_trylock_internal_abort));
     106  
     107    if (!__pthread_force_elision)
     108      __elision_aconf.retry_try_xbegin = 0; /* Disable elision on rwlocks.  */
     109  }