1 /* Lock elision tunable parameters.
2    Copyright (C) 2014-2021 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 #if HAVE_TUNABLES
26 # define TUNABLE_NAMESPACE elision
27 #endif
28 #include <elf/dl-tunables.h>
29 
30 /* Reasonable initial tuning values, may be revised in the future.
31    This is a conservative initial value.  */
32 
33 struct elision_config __elision_aconf =
34   {
35     /* How often to not attempt to use elision if a transaction aborted
36        because the lock is already acquired.  Expressed in number of lock
37        acquisition attempts.  */
38     .skip_lock_busy = 3,
39     /* How often to not attempt to use elision if a transaction aborted due
40        to reasons other than other threads' memory accesses.  Expressed in
41        number of lock acquisition attempts.  */
42     .skip_lock_internal_abort = 3,
43     /* How often to not attempt to use elision if a lock used up all retries
44        without success.  Expressed in number of lock acquisition attempts.  */
45     .skip_lock_out_of_tbegin_retries = 3,
46     /* How often we try using elision if there is chance for the transaction
47        to finish execution (e.g., it wasn't aborted due to the lock being
48        already acquired.  */
49     .try_tbegin = 3,
50     /* Same as SKIP_LOCK_INTERNAL_ABORT but for trylock.  */
51     .skip_trylock_internal_abort = 3,
52   };
53 
54 #if HAVE_TUNABLES
55 static inline void
56 __always_inline
do_set_elision_enable(int32_t elision_enable)57 do_set_elision_enable (int32_t elision_enable)
58 {
59   /* Enable elision if it's avaliable in hardware. It's not necessary to check
60      if __libc_enable_secure isn't enabled since elision_enable will be set
61      according to the default, which is disabled.  */
62   if (elision_enable == 1)
63     __pthread_force_elision = (GLRO (dl_hwcap) & HWCAP_S390_TE) ? 1 : 0;
64 }
65 
66 /* The pthread->elision_enable tunable is 0 or 1 indicating that elision
67    should be disabled or enabled respectively.  The feature will only be used
68    if it's supported by the hardware.  */
69 
70 void
TUNABLE_CALLBACK(set_elision_enable)71 TUNABLE_CALLBACK (set_elision_enable) (tunable_val_t *valp)
72 {
73   int32_t elision_enable = (int32_t) valp->numval;
74   do_set_elision_enable (elision_enable);
75 }
76 
77 #define TUNABLE_CALLBACK_FNDECL(__name, __type)			\
78 static inline void						\
79 __always_inline							\
80 do_set_elision_ ## __name (__type value)			\
81 {								\
82   __elision_aconf.__name = value;				\
83 }								\
84 void								\
85 TUNABLE_CALLBACK (set_elision_ ## __name) (tunable_val_t *valp) \
86 {								\
87   __type value = (__type) (valp)->numval;			\
88   do_set_elision_ ## __name (value);				\
89 }
90 
91 TUNABLE_CALLBACK_FNDECL (skip_lock_busy, int32_t);
92 TUNABLE_CALLBACK_FNDECL (skip_lock_internal_abort, int32_t);
93 TUNABLE_CALLBACK_FNDECL (skip_lock_out_of_tbegin_retries, int32_t);
94 TUNABLE_CALLBACK_FNDECL (try_tbegin, int32_t);
95 TUNABLE_CALLBACK_FNDECL (skip_trylock_internal_abort, int32_t);
96 #endif
97 
98 /* Initialize elison.  */
99 
100 void
__lll_elision_init(void)101 __lll_elision_init (void)
102 {
103 #if HAVE_TUNABLES
104   /* Elision depends on tunables and must be explicitly turned on by setting
105      the appropriate tunable on a supported platform.  */
106 
107   TUNABLE_GET (enable, int32_t,
108 	       TUNABLE_CALLBACK (set_elision_enable));
109   TUNABLE_GET (skip_lock_busy, int32_t,
110 	       TUNABLE_CALLBACK (set_elision_skip_lock_busy));
111   TUNABLE_GET (skip_lock_internal_abort, int32_t,
112 	       TUNABLE_CALLBACK (set_elision_skip_lock_internal_abort));
113   TUNABLE_GET (skip_lock_after_retries, int32_t,
114 	       TUNABLE_CALLBACK (set_elision_skip_lock_out_of_tbegin_retries));
115   TUNABLE_GET (tries, int32_t,
116 	       TUNABLE_CALLBACK (set_elision_try_tbegin));
117   TUNABLE_GET (skip_trylock_internal_abort, int32_t,
118 	       TUNABLE_CALLBACK (set_elision_skip_trylock_internal_abort));
119 #endif
120 
121   if (!__pthread_force_elision)
122     __elision_aconf.try_tbegin = 0; /* Disable elision on rwlocks.  */
123 }
124