xref: /OK3568_Linux_fs/kernel/tools/lib/lockdep/preload.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #define _GNU_SOURCE
3*4882a593Smuzhiyun #include <pthread.h>
4*4882a593Smuzhiyun #include <stdio.h>
5*4882a593Smuzhiyun #include <dlfcn.h>
6*4882a593Smuzhiyun #include <stdlib.h>
7*4882a593Smuzhiyun #include <sysexits.h>
8*4882a593Smuzhiyun #include <unistd.h>
9*4882a593Smuzhiyun #include "include/liblockdep/mutex.h"
10*4882a593Smuzhiyun #include "../../include/linux/rbtree.h"
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun /**
13*4882a593Smuzhiyun  * struct lock_lookup - liblockdep's view of a single unique lock
14*4882a593Smuzhiyun  * @orig: pointer to the original pthread lock, used for lookups
15*4882a593Smuzhiyun  * @dep_map: lockdep's dep_map structure
16*4882a593Smuzhiyun  * @key: lockdep's key structure
17*4882a593Smuzhiyun  * @node: rb-tree node used to store the lock in a global tree
18*4882a593Smuzhiyun  * @name: a unique name for the lock
19*4882a593Smuzhiyun  */
20*4882a593Smuzhiyun struct lock_lookup {
21*4882a593Smuzhiyun 	void *orig; /* Original pthread lock, used for lookups */
22*4882a593Smuzhiyun 	struct lockdep_map dep_map; /* Since all locks are dynamic, we need
23*4882a593Smuzhiyun 				     * a dep_map and a key for each lock */
24*4882a593Smuzhiyun 	/*
25*4882a593Smuzhiyun 	 * Wait, there's no support for key classes? Yup :(
26*4882a593Smuzhiyun 	 * Most big projects wrap the pthread api with their own calls to
27*4882a593Smuzhiyun 	 * be compatible with different locking methods. This means that
28*4882a593Smuzhiyun 	 * "classes" will be brokes since the function that creates all
29*4882a593Smuzhiyun 	 * locks will point to a generic locking function instead of the
30*4882a593Smuzhiyun 	 * actual code that wants to do the locking.
31*4882a593Smuzhiyun 	 */
32*4882a593Smuzhiyun 	struct lock_class_key key;
33*4882a593Smuzhiyun 	struct rb_node node;
34*4882a593Smuzhiyun #define LIBLOCKDEP_MAX_LOCK_NAME 22
35*4882a593Smuzhiyun 	char name[LIBLOCKDEP_MAX_LOCK_NAME];
36*4882a593Smuzhiyun };
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /* This is where we store our locks */
39*4882a593Smuzhiyun static struct rb_root locks = RB_ROOT;
40*4882a593Smuzhiyun static pthread_rwlock_t locks_rwlock = PTHREAD_RWLOCK_INITIALIZER;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /* pthread mutex API */
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #ifdef __GLIBC__
45*4882a593Smuzhiyun extern int __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
46*4882a593Smuzhiyun extern int __pthread_mutex_lock(pthread_mutex_t *mutex);
47*4882a593Smuzhiyun extern int __pthread_mutex_trylock(pthread_mutex_t *mutex);
48*4882a593Smuzhiyun extern int __pthread_mutex_unlock(pthread_mutex_t *mutex);
49*4882a593Smuzhiyun extern int __pthread_mutex_destroy(pthread_mutex_t *mutex);
50*4882a593Smuzhiyun #else
51*4882a593Smuzhiyun #define __pthread_mutex_init	NULL
52*4882a593Smuzhiyun #define __pthread_mutex_lock	NULL
53*4882a593Smuzhiyun #define __pthread_mutex_trylock	NULL
54*4882a593Smuzhiyun #define __pthread_mutex_unlock	NULL
55*4882a593Smuzhiyun #define __pthread_mutex_destroy	NULL
56*4882a593Smuzhiyun #endif
57*4882a593Smuzhiyun static int (*ll_pthread_mutex_init)(pthread_mutex_t *mutex,
58*4882a593Smuzhiyun 			const pthread_mutexattr_t *attr)	= __pthread_mutex_init;
59*4882a593Smuzhiyun static int (*ll_pthread_mutex_lock)(pthread_mutex_t *mutex)	= __pthread_mutex_lock;
60*4882a593Smuzhiyun static int (*ll_pthread_mutex_trylock)(pthread_mutex_t *mutex)	= __pthread_mutex_trylock;
61*4882a593Smuzhiyun static int (*ll_pthread_mutex_unlock)(pthread_mutex_t *mutex)	= __pthread_mutex_unlock;
62*4882a593Smuzhiyun static int (*ll_pthread_mutex_destroy)(pthread_mutex_t *mutex)	= __pthread_mutex_destroy;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /* pthread rwlock API */
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun #ifdef __GLIBC__
67*4882a593Smuzhiyun extern int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
68*4882a593Smuzhiyun extern int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
69*4882a593Smuzhiyun extern int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
70*4882a593Smuzhiyun extern int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
71*4882a593Smuzhiyun extern int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
72*4882a593Smuzhiyun extern int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
73*4882a593Smuzhiyun extern int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
74*4882a593Smuzhiyun #else
75*4882a593Smuzhiyun #define __pthread_rwlock_init		NULL
76*4882a593Smuzhiyun #define __pthread_rwlock_destroy	NULL
77*4882a593Smuzhiyun #define __pthread_rwlock_wrlock		NULL
78*4882a593Smuzhiyun #define __pthread_rwlock_trywrlock	NULL
79*4882a593Smuzhiyun #define __pthread_rwlock_rdlock		NULL
80*4882a593Smuzhiyun #define __pthread_rwlock_tryrdlock	NULL
81*4882a593Smuzhiyun #define __pthread_rwlock_unlock		NULL
82*4882a593Smuzhiyun #endif
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun static int (*ll_pthread_rwlock_init)(pthread_rwlock_t *rwlock,
85*4882a593Smuzhiyun 			const pthread_rwlockattr_t *attr)		= __pthread_rwlock_init;
86*4882a593Smuzhiyun static int (*ll_pthread_rwlock_destroy)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_destroy;
87*4882a593Smuzhiyun static int (*ll_pthread_rwlock_rdlock)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_rdlock;
88*4882a593Smuzhiyun static int (*ll_pthread_rwlock_tryrdlock)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_tryrdlock;
89*4882a593Smuzhiyun static int (*ll_pthread_rwlock_trywrlock)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_trywrlock;
90*4882a593Smuzhiyun static int (*ll_pthread_rwlock_wrlock)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_wrlock;
91*4882a593Smuzhiyun static int (*ll_pthread_rwlock_unlock)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_unlock;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun enum { none, prepare, done, } __init_state;
94*4882a593Smuzhiyun static void init_preload(void);
try_init_preload(void)95*4882a593Smuzhiyun static void try_init_preload(void)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	if (__init_state != done)
98*4882a593Smuzhiyun 		init_preload();
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
__get_lock_node(void * lock,struct rb_node ** parent)101*4882a593Smuzhiyun static struct rb_node **__get_lock_node(void *lock, struct rb_node **parent)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	struct rb_node **node = &locks.rb_node;
104*4882a593Smuzhiyun 	struct lock_lookup *l;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	*parent = NULL;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	while (*node) {
109*4882a593Smuzhiyun 		l = rb_entry(*node, struct lock_lookup, node);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 		*parent = *node;
112*4882a593Smuzhiyun 		if (lock < l->orig)
113*4882a593Smuzhiyun 			node = &l->node.rb_left;
114*4882a593Smuzhiyun 		else if (lock > l->orig)
115*4882a593Smuzhiyun 			node = &l->node.rb_right;
116*4882a593Smuzhiyun 		else
117*4882a593Smuzhiyun 			return node;
118*4882a593Smuzhiyun 	}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	return node;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun #ifndef LIBLOCKDEP_STATIC_ENTRIES
124*4882a593Smuzhiyun #define LIBLOCKDEP_STATIC_ENTRIES	1024
125*4882a593Smuzhiyun #endif
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun static struct lock_lookup __locks[LIBLOCKDEP_STATIC_ENTRIES];
128*4882a593Smuzhiyun static int __locks_nr;
129*4882a593Smuzhiyun 
is_static_lock(struct lock_lookup * lock)130*4882a593Smuzhiyun static inline bool is_static_lock(struct lock_lookup *lock)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	return lock >= __locks && lock < __locks + ARRAY_SIZE(__locks);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
alloc_lock(void)135*4882a593Smuzhiyun static struct lock_lookup *alloc_lock(void)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	if (__init_state != done) {
138*4882a593Smuzhiyun 		/*
139*4882a593Smuzhiyun 		 * Some programs attempt to initialize and use locks in their
140*4882a593Smuzhiyun 		 * allocation path. This means that a call to malloc() would
141*4882a593Smuzhiyun 		 * result in locks being initialized and locked.
142*4882a593Smuzhiyun 		 *
143*4882a593Smuzhiyun 		 * Why is it an issue for us? dlsym() below will try allocating
144*4882a593Smuzhiyun 		 * to give us the original function. Since this allocation will
145*4882a593Smuzhiyun 		 * result in a locking operations, we have to let pthread deal
146*4882a593Smuzhiyun 		 * with it, but we can't! we don't have the pointer to the
147*4882a593Smuzhiyun 		 * original API since we're inside dlsym() trying to get it
148*4882a593Smuzhiyun 		 */
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 		int idx = __locks_nr++;
151*4882a593Smuzhiyun 		if (idx >= ARRAY_SIZE(__locks)) {
152*4882a593Smuzhiyun 			dprintf(STDERR_FILENO,
153*4882a593Smuzhiyun 		"LOCKDEP error: insufficient LIBLOCKDEP_STATIC_ENTRIES\n");
154*4882a593Smuzhiyun 			exit(EX_UNAVAILABLE);
155*4882a593Smuzhiyun 		}
156*4882a593Smuzhiyun 		return __locks + idx;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	return malloc(sizeof(struct lock_lookup));
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
free_lock(struct lock_lookup * lock)162*4882a593Smuzhiyun static inline void free_lock(struct lock_lookup *lock)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	if (likely(!is_static_lock(lock)))
165*4882a593Smuzhiyun 		free(lock);
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun /**
169*4882a593Smuzhiyun  * __get_lock - find or create a lock instance
170*4882a593Smuzhiyun  * @lock: pointer to a pthread lock function
171*4882a593Smuzhiyun  *
172*4882a593Smuzhiyun  * Try to find an existing lock in the rbtree using the provided pointer. If
173*4882a593Smuzhiyun  * one wasn't found - create it.
174*4882a593Smuzhiyun  */
__get_lock(void * lock)175*4882a593Smuzhiyun static struct lock_lookup *__get_lock(void *lock)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	struct rb_node **node, *parent;
178*4882a593Smuzhiyun 	struct lock_lookup *l;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	ll_pthread_rwlock_rdlock(&locks_rwlock);
181*4882a593Smuzhiyun 	node = __get_lock_node(lock, &parent);
182*4882a593Smuzhiyun 	ll_pthread_rwlock_unlock(&locks_rwlock);
183*4882a593Smuzhiyun 	if (*node) {
184*4882a593Smuzhiyun 		return rb_entry(*node, struct lock_lookup, node);
185*4882a593Smuzhiyun 	}
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	/* We didn't find the lock, let's create it */
188*4882a593Smuzhiyun 	l = alloc_lock();
189*4882a593Smuzhiyun 	if (l == NULL)
190*4882a593Smuzhiyun 		return NULL;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	l->orig = lock;
193*4882a593Smuzhiyun 	/*
194*4882a593Smuzhiyun 	 * Currently the name of the lock is the ptr value of the pthread lock,
195*4882a593Smuzhiyun 	 * while not optimal, it makes debugging a bit easier.
196*4882a593Smuzhiyun 	 *
197*4882a593Smuzhiyun 	 * TODO: Get the real name of the lock using libdwarf
198*4882a593Smuzhiyun 	 */
199*4882a593Smuzhiyun 	sprintf(l->name, "%p", lock);
200*4882a593Smuzhiyun 	lockdep_init_map(&l->dep_map, l->name, &l->key, 0);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	ll_pthread_rwlock_wrlock(&locks_rwlock);
203*4882a593Smuzhiyun 	/* This might have changed since the last time we fetched it */
204*4882a593Smuzhiyun 	node = __get_lock_node(lock, &parent);
205*4882a593Smuzhiyun 	rb_link_node(&l->node, parent, node);
206*4882a593Smuzhiyun 	rb_insert_color(&l->node, &locks);
207*4882a593Smuzhiyun 	ll_pthread_rwlock_unlock(&locks_rwlock);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	return l;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun 
__del_lock(struct lock_lookup * lock)212*4882a593Smuzhiyun static void __del_lock(struct lock_lookup *lock)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	ll_pthread_rwlock_wrlock(&locks_rwlock);
215*4882a593Smuzhiyun 	rb_erase(&lock->node, &locks);
216*4882a593Smuzhiyun 	ll_pthread_rwlock_unlock(&locks_rwlock);
217*4882a593Smuzhiyun 	free_lock(lock);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
pthread_mutex_init(pthread_mutex_t * mutex,const pthread_mutexattr_t * attr)220*4882a593Smuzhiyun int pthread_mutex_init(pthread_mutex_t *mutex,
221*4882a593Smuzhiyun 			const pthread_mutexattr_t *attr)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	int r;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/*
226*4882a593Smuzhiyun 	 * We keep trying to init our preload module because there might be
227*4882a593Smuzhiyun 	 * code in init sections that tries to touch locks before we are
228*4882a593Smuzhiyun 	 * initialized, in that case we'll need to manually call preload
229*4882a593Smuzhiyun 	 * to get us going.
230*4882a593Smuzhiyun 	 *
231*4882a593Smuzhiyun 	 * Funny enough, kernel's lockdep had the same issue, and used
232*4882a593Smuzhiyun 	 * (almost) the same solution. See look_up_lock_class() in
233*4882a593Smuzhiyun 	 * kernel/locking/lockdep.c for details.
234*4882a593Smuzhiyun 	 */
235*4882a593Smuzhiyun 	try_init_preload();
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	r = ll_pthread_mutex_init(mutex, attr);
238*4882a593Smuzhiyun 	if (r == 0)
239*4882a593Smuzhiyun 		/*
240*4882a593Smuzhiyun 		 * We do a dummy initialization here so that lockdep could
241*4882a593Smuzhiyun 		 * warn us if something fishy is going on - such as
242*4882a593Smuzhiyun 		 * initializing a held lock.
243*4882a593Smuzhiyun 		 */
244*4882a593Smuzhiyun 		__get_lock(mutex);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	return r;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
pthread_mutex_lock(pthread_mutex_t * mutex)249*4882a593Smuzhiyun int pthread_mutex_lock(pthread_mutex_t *mutex)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	int r;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	try_init_preload();
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	lock_acquire(&__get_lock(mutex)->dep_map, 0, 0, 0, 1, NULL,
256*4882a593Smuzhiyun 			(unsigned long)_RET_IP_);
257*4882a593Smuzhiyun 	/*
258*4882a593Smuzhiyun 	 * Here's the thing with pthread mutexes: unlike the kernel variant,
259*4882a593Smuzhiyun 	 * they can fail.
260*4882a593Smuzhiyun 	 *
261*4882a593Smuzhiyun 	 * This means that the behaviour here is a bit different from what's
262*4882a593Smuzhiyun 	 * going on in the kernel: there we just tell lockdep that we took the
263*4882a593Smuzhiyun 	 * lock before actually taking it, but here we must deal with the case
264*4882a593Smuzhiyun 	 * that locking failed.
265*4882a593Smuzhiyun 	 *
266*4882a593Smuzhiyun 	 * To do that we'll "release" the lock if locking failed - this way
267*4882a593Smuzhiyun 	 * we'll get lockdep doing the correct checks when we try to take
268*4882a593Smuzhiyun 	 * the lock, and if that fails - we'll be back to the correct
269*4882a593Smuzhiyun 	 * state by releasing it.
270*4882a593Smuzhiyun 	 */
271*4882a593Smuzhiyun 	r = ll_pthread_mutex_lock(mutex);
272*4882a593Smuzhiyun 	if (r)
273*4882a593Smuzhiyun 		lock_release(&__get_lock(mutex)->dep_map, (unsigned long)_RET_IP_);
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	return r;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun 
pthread_mutex_trylock(pthread_mutex_t * mutex)278*4882a593Smuzhiyun int pthread_mutex_trylock(pthread_mutex_t *mutex)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	int r;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	try_init_preload();
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	lock_acquire(&__get_lock(mutex)->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_);
285*4882a593Smuzhiyun 	r = ll_pthread_mutex_trylock(mutex);
286*4882a593Smuzhiyun 	if (r)
287*4882a593Smuzhiyun 		lock_release(&__get_lock(mutex)->dep_map, (unsigned long)_RET_IP_);
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	return r;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun 
pthread_mutex_unlock(pthread_mutex_t * mutex)292*4882a593Smuzhiyun int pthread_mutex_unlock(pthread_mutex_t *mutex)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun 	int r;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	try_init_preload();
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	lock_release(&__get_lock(mutex)->dep_map, (unsigned long)_RET_IP_);
299*4882a593Smuzhiyun 	/*
300*4882a593Smuzhiyun 	 * Just like taking a lock, only in reverse!
301*4882a593Smuzhiyun 	 *
302*4882a593Smuzhiyun 	 * If we fail releasing the lock, tell lockdep we're holding it again.
303*4882a593Smuzhiyun 	 */
304*4882a593Smuzhiyun 	r = ll_pthread_mutex_unlock(mutex);
305*4882a593Smuzhiyun 	if (r)
306*4882a593Smuzhiyun 		lock_acquire(&__get_lock(mutex)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_);
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	return r;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
pthread_mutex_destroy(pthread_mutex_t * mutex)311*4882a593Smuzhiyun int pthread_mutex_destroy(pthread_mutex_t *mutex)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	try_init_preload();
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	/*
316*4882a593Smuzhiyun 	 * Let's see if we're releasing a lock that's held.
317*4882a593Smuzhiyun 	 *
318*4882a593Smuzhiyun 	 * TODO: Hook into free() and add that check there as well.
319*4882a593Smuzhiyun 	 */
320*4882a593Smuzhiyun 	debug_check_no_locks_freed(mutex, sizeof(*mutex));
321*4882a593Smuzhiyun 	__del_lock(__get_lock(mutex));
322*4882a593Smuzhiyun 	return ll_pthread_mutex_destroy(mutex);
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun /* This is the rwlock part, very similar to what happened with mutex above */
pthread_rwlock_init(pthread_rwlock_t * rwlock,const pthread_rwlockattr_t * attr)326*4882a593Smuzhiyun int pthread_rwlock_init(pthread_rwlock_t *rwlock,
327*4882a593Smuzhiyun 			const pthread_rwlockattr_t *attr)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun 	int r;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	try_init_preload();
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	r = ll_pthread_rwlock_init(rwlock, attr);
334*4882a593Smuzhiyun 	if (r == 0)
335*4882a593Smuzhiyun 		__get_lock(rwlock);
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	return r;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
pthread_rwlock_destroy(pthread_rwlock_t * rwlock)340*4882a593Smuzhiyun int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun 	try_init_preload();
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	debug_check_no_locks_freed(rwlock, sizeof(*rwlock));
345*4882a593Smuzhiyun 	__del_lock(__get_lock(rwlock));
346*4882a593Smuzhiyun 	return ll_pthread_rwlock_destroy(rwlock);
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
pthread_rwlock_rdlock(pthread_rwlock_t * rwlock)349*4882a593Smuzhiyun int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun 	int r;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun         init_preload();
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 2, 1, NULL, (unsigned long)_RET_IP_);
356*4882a593Smuzhiyun 	r = ll_pthread_rwlock_rdlock(rwlock);
357*4882a593Smuzhiyun 	if (r)
358*4882a593Smuzhiyun 		lock_release(&__get_lock(rwlock)->dep_map, (unsigned long)_RET_IP_);
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	return r;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
pthread_rwlock_tryrdlock(pthread_rwlock_t * rwlock)363*4882a593Smuzhiyun int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun 	int r;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun         init_preload();
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	lock_acquire(&__get_lock(rwlock)->dep_map, 0, 1, 2, 1, NULL, (unsigned long)_RET_IP_);
370*4882a593Smuzhiyun 	r = ll_pthread_rwlock_tryrdlock(rwlock);
371*4882a593Smuzhiyun 	if (r)
372*4882a593Smuzhiyun 		lock_release(&__get_lock(rwlock)->dep_map, (unsigned long)_RET_IP_);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	return r;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun 
pthread_rwlock_trywrlock(pthread_rwlock_t * rwlock)377*4882a593Smuzhiyun int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun 	int r;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun         init_preload();
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	lock_acquire(&__get_lock(rwlock)->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_);
384*4882a593Smuzhiyun 	r = ll_pthread_rwlock_trywrlock(rwlock);
385*4882a593Smuzhiyun 	if (r)
386*4882a593Smuzhiyun 		lock_release(&__get_lock(rwlock)->dep_map, (unsigned long)_RET_IP_);
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	return r;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
pthread_rwlock_wrlock(pthread_rwlock_t * rwlock)391*4882a593Smuzhiyun int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun 	int r;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun         init_preload();
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_);
398*4882a593Smuzhiyun 	r = ll_pthread_rwlock_wrlock(rwlock);
399*4882a593Smuzhiyun 	if (r)
400*4882a593Smuzhiyun 		lock_release(&__get_lock(rwlock)->dep_map, (unsigned long)_RET_IP_);
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	return r;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
pthread_rwlock_unlock(pthread_rwlock_t * rwlock)405*4882a593Smuzhiyun int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	int r;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun         init_preload();
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	lock_release(&__get_lock(rwlock)->dep_map, (unsigned long)_RET_IP_);
412*4882a593Smuzhiyun 	r = ll_pthread_rwlock_unlock(rwlock);
413*4882a593Smuzhiyun 	if (r)
414*4882a593Smuzhiyun 		lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	return r;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun 
init_preload(void)419*4882a593Smuzhiyun __attribute__((constructor)) static void init_preload(void)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	if (__init_state == done)
422*4882a593Smuzhiyun 		return;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun #ifndef __GLIBC__
425*4882a593Smuzhiyun 	__init_state = prepare;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	ll_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
428*4882a593Smuzhiyun 	ll_pthread_mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock");
429*4882a593Smuzhiyun 	ll_pthread_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
430*4882a593Smuzhiyun 	ll_pthread_mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
431*4882a593Smuzhiyun 	ll_pthread_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy");
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	ll_pthread_rwlock_init = dlsym(RTLD_NEXT, "pthread_rwlock_init");
434*4882a593Smuzhiyun 	ll_pthread_rwlock_destroy = dlsym(RTLD_NEXT, "pthread_rwlock_destroy");
435*4882a593Smuzhiyun 	ll_pthread_rwlock_rdlock = dlsym(RTLD_NEXT, "pthread_rwlock_rdlock");
436*4882a593Smuzhiyun 	ll_pthread_rwlock_tryrdlock = dlsym(RTLD_NEXT, "pthread_rwlock_tryrdlock");
437*4882a593Smuzhiyun 	ll_pthread_rwlock_wrlock = dlsym(RTLD_NEXT, "pthread_rwlock_wrlock");
438*4882a593Smuzhiyun 	ll_pthread_rwlock_trywrlock = dlsym(RTLD_NEXT, "pthread_rwlock_trywrlock");
439*4882a593Smuzhiyun 	ll_pthread_rwlock_unlock = dlsym(RTLD_NEXT, "pthread_rwlock_unlock");
440*4882a593Smuzhiyun #endif
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	__init_state = done;
443*4882a593Smuzhiyun }
444