1/* elision-conf.c: Lock elision tunable parameters.
2 Copyright (C) 2013-2018 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 <http://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#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
33struct 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 we retry using elision if there is chance for the transaction
44 to finish execution (e.g., it wasn't aborted due to the lock being
45 already acquired. */
46 .retry_try_xbegin = 3,
47 /* Same as SKIP_LOCK_INTERNAL_ABORT but for trylock. */
48 .skip_trylock_internal_abort = 3,
49 };
50
51/* Force elision for all new locks. This is used to decide whether existing
52 DEFAULT locks should be automatically upgraded to elision in
53 pthread_mutex_lock(). Disabled for suid programs. Only used when elision
54 is available. */
55
56int __pthread_force_elision attribute_hidden = 0;
57
58#if HAVE_TUNABLES
59static inline void
60__always_inline
61do_set_elision_enable (int32_t elision_enable)
62{
63 /* Enable elision if it's avaliable in hardware. It's not necessary to check
64 if __libc_enable_secure isn't enabled since elision_enable will be set
65 according to the default, which is disabled. */
66 if (elision_enable == 1)
67 __pthread_force_elision = HAS_CPU_FEATURE (RTM) ? 1 : 0;
68}
69
70/* The pthread->elision_enable tunable is 0 or 1 indicating that elision
71 should be disabled or enabled respectively. The feature will only be used
72 if it's supported by the hardware. */
73
74void
75TUNABLE_CALLBACK (set_elision_enable) (tunable_val_t *valp)
76{
77 int32_t elision_enable = (int32_t) valp->numval;
78 do_set_elision_enable (elision_enable);
79}
80
81#define TUNABLE_CALLBACK_FNDECL(__name, __type) \
82static inline void \
83__always_inline \
84do_set_elision_ ## __name (__type value) \
85{ \
86 __elision_aconf.__name = value; \
87} \
88void \
89TUNABLE_CALLBACK (set_elision_ ## __name) (tunable_val_t *valp) \
90{ \
91 __type value = (__type) (valp)->numval; \
92 do_set_elision_ ## __name (value); \
93}
94
95TUNABLE_CALLBACK_FNDECL (skip_lock_busy, int32_t);
96TUNABLE_CALLBACK_FNDECL (skip_lock_internal_abort, int32_t);
97TUNABLE_CALLBACK_FNDECL (retry_try_xbegin, int32_t);
98TUNABLE_CALLBACK_FNDECL (skip_trylock_internal_abort, int32_t);
99#endif
100
101/* Initialize elision. */
102
103static void
104elision_init (int argc __attribute__ ((unused)),
105 char **argv __attribute__ ((unused)),
106 char **environ)
107{
108#if HAVE_TUNABLES
109 /* Elision depends on tunables and must be explicitly turned on by setting
110 the appropriate tunable on a supported platform. */
111
112 TUNABLE_GET (enable, int32_t,
113 TUNABLE_CALLBACK (set_elision_enable));
114 TUNABLE_GET (skip_lock_busy, int32_t,
115 TUNABLE_CALLBACK (set_elision_skip_lock_busy));
116 TUNABLE_GET (skip_lock_internal_abort, int32_t,
117 TUNABLE_CALLBACK (set_elision_skip_lock_internal_abort));
118 TUNABLE_GET (tries, int32_t,
119 TUNABLE_CALLBACK (set_elision_retry_try_xbegin));
120 TUNABLE_GET (skip_trylock_internal_abort, int32_t,
121 TUNABLE_CALLBACK (set_elision_skip_trylock_internal_abort));
122#endif
123
124 if (!__pthread_force_elision)
125 __elision_aconf.retry_try_xbegin = 0; /* Disable elision on rwlocks. */
126}
127
128#ifdef SHARED
129# define INIT_SECTION ".init_array"
130#else
131# define INIT_SECTION ".preinit_array"
132#endif
133
134void (*const __pthread_init_array []) (int, char **, char **)
135 __attribute__ ((section (INIT_SECTION), aligned (sizeof (void *)))) =
136{
137 &elision_init
138};
139