xref: /linux/tools/perf/util/mutex.c (revision 4456c404c6bb2e978769f5f27090deee3457f95c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "mutex.h"
3 
4 #include "debug.h"
5 #include <linux/string.h>
6 #include <errno.h>
7 
8 static void check_err(const char *fn, int err)
9 {
10 	char sbuf[STRERR_BUFSIZE];
11 
12 	if (err == 0)
13 		return;
14 
15 	pr_err("%s error: '%s'\n", fn, str_error_r(err, sbuf, sizeof(sbuf)));
16 }
17 
18 #define CHECK_ERR(err) check_err(__func__, err)
19 
20 #ifdef HAVE_PTHREAD_MUTEX_ROBUST
21 /*
22  * A robust mutex whose owner died while holding it comes back as EOWNERDEAD,
23  * with the caller holding it: make it consistent, which takes it over; failing
24  * that, a give_up caller unlocks it, leaving it ENOTRECOVERABLE, not leaked.
25  */
26 static int mutex_take_over(struct mutex *mtx, int err, bool give_up)
27 {
28 	if (err != EOWNERDEAD)
29 		return err;
30 
31 	pr_debug("mutex: taking over one left by a dead owner\n");
32 	err = pthread_mutex_consistent(&mtx->lock);
33 	if (err && give_up)
34 		CHECK_ERR(pthread_mutex_unlock(&mtx->lock));
35 
36 	return err;
37 }
38 #else
39 #define mutex_take_over(mtx, err, give_up) ((void)(mtx), (void)(give_up), (err))
40 #endif
41 
42 static bool __mutex_init(struct mutex *mtx, bool pshared, bool recursive, bool robust)
43 {
44 	pthread_mutexattr_t attr;
45 	bool ok = false, robust_unavailable = false;
46 	int err;
47 
48 	err = pthread_mutexattr_init(&attr);
49 	if (err)
50 		goto out;
51 
52 #ifndef NDEBUG
53 	/* In normal builds enable error checking, such as recursive usage. */
54 	err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
55 	if (err)
56 		goto out_destroy;
57 #endif
58 	if (recursive) {
59 		err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
60 		if (err)
61 			goto out_destroy;
62 	}
63 	if (pshared) {
64 		err = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
65 		if (err)
66 			goto out_destroy;
67 	}
68 	if (robust) {
69 #ifdef HAVE_PTHREAD_MUTEX_ROBUST
70 		err = pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
71 		if (err)
72 			robust_unavailable = true;
73 #else
74 		robust_unavailable = true;
75 #endif
76 		if (robust_unavailable)
77 			goto out_destroy;
78 	}
79 
80 	err = pthread_mutex_init(&mtx->lock, &attr);
81 	if (!err)
82 		ok = true;
83 	else if (robust && pshared && err == ENOTSUP)
84 		/*
85 		 * glibc refuses a robust process shared mutex with ENOTSUP when
86 		 * set_robust_list(2) isn't usable, i.e. when the machine provides
87 		 * no robust mutexes for the process shared case: an unavailable
88 		 * API, not a bad request.
89 		 */
90 		robust_unavailable = true;
91 out_destroy:
92 	CHECK_ERR(pthread_mutexattr_destroy(&attr));
93 out:
94 	if (robust_unavailable)
95 		pr_debug("mutex: robust mutexes aren't available\n");
96 	else if (err)
97 		CHECK_ERR(err);
98 
99 	return ok;
100 }
101 
102 void mutex_init(struct mutex *mtx)
103 {
104 	__mutex_init(mtx, /*pshared=*/false, /*recursive=*/false, /*robust=*/false);
105 }
106 
107 void mutex_init_pshared(struct mutex *mtx)
108 {
109 	__mutex_init(mtx, /*pshared=*/true, /*recursive=*/false, /*robust=*/false);
110 }
111 
112 void mutex_init_recursive(struct mutex *mtx)
113 {
114 	__mutex_init(mtx, /*pshared=*/false, /*recursive=*/true, /*robust=*/false);
115 }
116 
117 bool mutex_init_pshared_robust(struct mutex *mtx)
118 {
119 	return __mutex_init(mtx, /*pshared=*/true, /*recursive=*/false, /*robust=*/true);
120 }
121 
122 void mutex_destroy(struct mutex *mtx)
123 {
124 	CHECK_ERR(pthread_mutex_destroy(&mtx->lock));
125 }
126 
127 void mutex_lock(struct mutex *mtx)
128 	NO_THREAD_SAFETY_ANALYSIS
129 {
130 	CHECK_ERR(mutex_take_over(mtx, pthread_mutex_lock(&mtx->lock),
131 				  /*give_up=*/false));
132 }
133 
134 void mutex_unlock(struct mutex *mtx)
135 	NO_THREAD_SAFETY_ANALYSIS
136 {
137 	CHECK_ERR(pthread_mutex_unlock(&mtx->lock));
138 }
139 
140 bool mutex_trylock(struct mutex *mtx)
141 {
142 	int ret = mutex_take_over(mtx, pthread_mutex_trylock(&mtx->lock),
143 				  /*give_up=*/true);
144 
145 	if (ret == 0)
146 		return true; /* Lock acquired. */
147 
148 	if (ret == EBUSY)
149 		return false; /* Lock busy. */
150 
151 	/* Print error. */
152 	CHECK_ERR(ret);
153 	return false;
154 }
155 
156 static void __cond_init(struct cond *cnd, bool pshared)
157 {
158 	pthread_condattr_t attr;
159 
160 	CHECK_ERR(pthread_condattr_init(&attr));
161 	if (pshared)
162 		CHECK_ERR(pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
163 
164 	CHECK_ERR(pthread_cond_init(&cnd->cond, &attr));
165 	CHECK_ERR(pthread_condattr_destroy(&attr));
166 }
167 
168 void cond_init(struct cond *cnd)
169 {
170 	__cond_init(cnd, /*pshared=*/false);
171 }
172 
173 void cond_init_pshared(struct cond *cnd)
174 {
175 	__cond_init(cnd, /*pshared=*/true);
176 }
177 
178 void cond_destroy(struct cond *cnd)
179 {
180 	CHECK_ERR(pthread_cond_destroy(&cnd->cond));
181 }
182 
183 void cond_wait(struct cond *cnd, struct mutex *mtx)
184 {
185 	CHECK_ERR(mutex_take_over(mtx,
186 				  pthread_cond_wait(&cnd->cond, &mtx->lock),
187 				  /*give_up=*/false));
188 }
189 
190 void cond_signal(struct cond *cnd)
191 {
192 	CHECK_ERR(pthread_cond_signal(&cnd->cond));
193 }
194 
195 void cond_broadcast(struct cond *cnd)
196 {
197 	CHECK_ERR(pthread_cond_broadcast(&cnd->cond));
198 }
199