1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#ifndef _LINUX_RESERVATION_H
40#define _LINUX_RESERVATION_H
41
42#include <linux/ww_mutex.h>
43#include <linux/fence.h>
44#include <linux/slab.h>
45#include <linux/seqlock.h>
46#include <linux/rcupdate.h>
47
48extern struct ww_class reservation_ww_class;
49extern struct lock_class_key reservation_seqcount_class;
50extern const char reservation_seqcount_string[];
51
52
53
54
55
56
57
58
59struct reservation_object_list {
60 struct rcu_head rcu;
61 u32 shared_count, shared_max;
62 struct fence __rcu *shared[];
63};
64
65
66
67
68
69
70
71
72
73struct reservation_object {
74 struct ww_mutex lock;
75 seqcount_t seq;
76
77 struct fence __rcu *fence_excl;
78 struct reservation_object_list __rcu *fence;
79 struct reservation_object_list *staged;
80};
81
82#define reservation_object_held(obj) lockdep_is_held(&(obj)->lock.base)
83#define reservation_object_assert_held(obj) \
84 lockdep_assert_held(&(obj)->lock.base)
85
86
87
88
89
90static inline void
91reservation_object_init(struct reservation_object *obj)
92{
93 ww_mutex_init(&obj->lock, &reservation_ww_class);
94
95 __seqcount_init(&obj->seq, reservation_seqcount_string, &reservation_seqcount_class);
96 RCU_INIT_POINTER(obj->fence, NULL);
97 RCU_INIT_POINTER(obj->fence_excl, NULL);
98 obj->staged = NULL;
99}
100
101
102
103
104
105static inline void
106reservation_object_fini(struct reservation_object *obj)
107{
108 int i;
109 struct reservation_object_list *fobj;
110 struct fence *excl;
111
112
113
114
115
116 excl = rcu_dereference_protected(obj->fence_excl, 1);
117 if (excl)
118 fence_put(excl);
119
120 fobj = rcu_dereference_protected(obj->fence, 1);
121 if (fobj) {
122 for (i = 0; i < fobj->shared_count; ++i)
123 fence_put(rcu_dereference_protected(fobj->shared[i], 1));
124
125 kfree(fobj);
126 }
127 kfree(obj->staged);
128
129 ww_mutex_destroy(&obj->lock);
130}
131
132
133
134
135
136
137
138
139
140static inline struct reservation_object_list *
141reservation_object_get_list(struct reservation_object *obj)
142{
143 return rcu_dereference_protected(obj->fence,
144 reservation_object_held(obj));
145}
146
147
148
149
150
151
152
153
154
155
156
157
158static inline struct fence *
159reservation_object_get_excl(struct reservation_object *obj)
160{
161 return rcu_dereference_protected(obj->fence_excl,
162 reservation_object_held(obj));
163}
164
165
166
167
168
169
170
171
172
173
174
175
176static inline struct fence *
177reservation_object_get_excl_rcu(struct reservation_object *obj)
178{
179 struct fence *fence;
180 unsigned seq;
181retry:
182 seq = read_seqcount_begin(&obj->seq);
183 rcu_read_lock();
184 fence = rcu_dereference(obj->fence_excl);
185 if (read_seqcount_retry(&obj->seq, seq)) {
186 rcu_read_unlock();
187 goto retry;
188 }
189 fence = fence_get(fence);
190 rcu_read_unlock();
191 return fence;
192}
193
194int reservation_object_reserve_shared(struct reservation_object *obj);
195void reservation_object_add_shared_fence(struct reservation_object *obj,
196 struct fence *fence);
197
198void reservation_object_add_excl_fence(struct reservation_object *obj,
199 struct fence *fence);
200
201int reservation_object_get_fences_rcu(struct reservation_object *obj,
202 struct fence **pfence_excl,
203 unsigned *pshared_count,
204 struct fence ***pshared);
205
206long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
207 bool wait_all, bool intr,
208 unsigned long timeout);
209
210bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
211 bool test_all);
212
213#endif
214