drm/rockchip: vop2: Support 32x8 superblock afbc
[drm/drm-misc.git] / kernel / locking / ww_rt_mutex.c
blobc7196de838edcd614abbfcecad7c69e624495005
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * rtmutex API
4 */
5 #include <linux/spinlock.h>
6 #include <linux/export.h>
8 #define RT_MUTEX_BUILD_MUTEX
9 #define WW_RT
10 #include "rtmutex.c"
12 int ww_mutex_trylock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
14 struct rt_mutex *rtm = &lock->base;
16 if (!ww_ctx)
17 return rt_mutex_trylock(rtm);
20 * Reset the wounded flag after a kill. No other process can
21 * race and wound us here, since they can't have a valid owner
22 * pointer if we don't have any locks held.
24 if (ww_ctx->acquired == 0)
25 ww_ctx->wounded = 0;
27 if (__rt_mutex_trylock(&rtm->rtmutex)) {
28 ww_mutex_set_context_fastpath(lock, ww_ctx);
29 mutex_acquire_nest(&rtm->dep_map, 0, 1, &ww_ctx->dep_map, _RET_IP_);
30 return 1;
33 return 0;
35 EXPORT_SYMBOL(ww_mutex_trylock);
37 static int __sched
38 __ww_rt_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx,
39 unsigned int state, unsigned long ip)
41 struct lockdep_map __maybe_unused *nest_lock = NULL;
42 struct rt_mutex *rtm = &lock->base;
43 int ret;
45 might_sleep();
47 if (ww_ctx) {
48 if (unlikely(ww_ctx == READ_ONCE(lock->ctx)))
49 return -EALREADY;
52 * Reset the wounded flag after a kill. No other process can
53 * race and wound us here, since they can't have a valid owner
54 * pointer if we don't have any locks held.
56 if (ww_ctx->acquired == 0)
57 ww_ctx->wounded = 0;
59 #ifdef CONFIG_DEBUG_LOCK_ALLOC
60 nest_lock = &ww_ctx->dep_map;
61 #endif
63 mutex_acquire_nest(&rtm->dep_map, 0, 0, nest_lock, ip);
65 if (likely(rt_mutex_try_acquire(&rtm->rtmutex))) {
66 if (ww_ctx)
67 ww_mutex_set_context_fastpath(lock, ww_ctx);
68 return 0;
71 ret = rt_mutex_slowlock(&rtm->rtmutex, ww_ctx, state);
73 if (ret)
74 mutex_release(&rtm->dep_map, ip);
75 return ret;
78 int __sched
79 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
81 return __ww_rt_mutex_lock(lock, ctx, TASK_UNINTERRUPTIBLE, _RET_IP_);
83 EXPORT_SYMBOL(ww_mutex_lock);
85 int __sched
86 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
88 return __ww_rt_mutex_lock(lock, ctx, TASK_INTERRUPTIBLE, _RET_IP_);
90 EXPORT_SYMBOL(ww_mutex_lock_interruptible);
92 void __sched ww_mutex_unlock(struct ww_mutex *lock)
94 struct rt_mutex *rtm = &lock->base;
96 __ww_mutex_unlock(lock);
98 mutex_release(&rtm->dep_map, _RET_IP_);
99 __rt_mutex_unlock(&rtm->rtmutex);
101 EXPORT_SYMBOL(ww_mutex_unlock);