-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdispatcher.cpp
310 lines (244 loc) · 9.6 KB
/
dispatcher.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2016 The Fuchsia Authors
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT
#include <object/dispatcher.h>
#include <inttypes.h>
#include <arch/ops.h>
#include <lib/ktrace.h>
#include <lib/counters.h>
#include <fbl/atomic.h>
#include <fbl/mutex.h>
#include <object/tls_slots.h>
// kernel counters. The following counters never decrease.
// counts the number of times a dispatcher has been created and destroyed.
KCOUNTER(dispatcher_create_count, "kernel.dispatcher.create");
KCOUNTER(dispatcher_destroy_count, "kernel.dispatcher.destroy");
// counts the number of times observers have been added to a kernel object.
KCOUNTER(dispatcher_observe_count, "kernel.dispatcher.observer.add");
// counts the number of times observers have been canceled.
KCOUNTER(dispatcher_cancel_bh_count, "kernel.dispatcher.observer.cancel.byhandle");
KCOUNTER(dispatcher_cancel_bk_count, "kernel.dispatcher.observer.cancel.bykey");
// counts the number of cookies set or changed (reset).
KCOUNTER(dispatcher_cookie_set_count, "kernel.dispatcher.cookie.set");
KCOUNTER(dispatcher_cookie_reset_count, "kernel.dispatcher.cookie.reset");
namespace {
// The first 1K koids are reserved.
fbl::atomic<zx_koid_t> global_koid(1024ULL);
zx_koid_t GenerateKernelObjectId() {
return global_koid.fetch_add(1ULL, fbl::memory_order_relaxed);
}
// Helper class that safely allows deleting Dispatchers without
// risk of blowing up the kernel stack. It uses one TLS slot to
// unwind the recursion.
class SafeDeleter {
public:
static void Delete(Dispatcher* kobj) {
auto deleter = reinterpret_cast<SafeDeleter*>(tls_get(TLS_ENTRY_KOBJ_DELETER));
if (deleter) {
// Delete() was called recursively.
deleter->pending_.push_front(kobj);
} else {
SafeDeleter deleter;
tls_set(TLS_ENTRY_KOBJ_DELETER, &deleter);
do {
// This delete call can cause recursive calls to
// Dispatcher::fbl_recycle() and hence to Delete().
delete kobj;
kobj = deleter.pending_.pop_front();
} while (kobj);
tls_set(TLS_ENTRY_KOBJ_DELETER, nullptr);
}
}
private:
fbl::SinglyLinkedList<Dispatcher*, Dispatcher::DeleterListTraits> pending_;
};
} // namespace
Dispatcher::Dispatcher(zx_signals_t signals)
: koid_(GenerateKernelObjectId()),
handle_count_(0u),
signals_(signals) {
kcounter_add(dispatcher_create_count, 1);
}
Dispatcher::~Dispatcher() {
ktrace(TAG_OBJECT_DELETE, (uint32_t)koid_, 0, 0, 0);
kcounter_add(dispatcher_destroy_count, 1);
}
// The refcount of this object has reached zero: delete self
// using the SafeDeleter to avoid potential recursion hazards.
// TODO(cpu): Not all object need the SafeDeleter. Only objects
// that can control the lifetime of dispatchers that in turn
// can control the lifetime of others. For example events do
// not fall in this category.
void Dispatcher::fbl_recycle() {
SafeDeleter::Delete(this);
}
zx_status_t Dispatcher::add_observer(StateObserver* observer) {
if (!is_waitable())
return ZX_ERR_NOT_SUPPORTED;
AddObserver(observer, nullptr);
return ZX_OK;
}
namespace {
template <typename Func, typename LockType>
StateObserver::Flags CancelWithFunc(Dispatcher::ObserverList* observers,
Lock<LockType>* observer_lock, Func f) {
StateObserver::Flags flags = 0;
Dispatcher::ObserverList obs_to_remove;
{
Guard<LockType> guard{observer_lock};
for (auto it = observers->begin(); it != observers->end();) {
StateObserver::Flags it_flags = f(it.CopyPointer());
flags |= it_flags;
if (it_flags & StateObserver::kNeedRemoval) {
auto to_remove = it;
++it;
obs_to_remove.push_back(observers->erase(to_remove));
} else {
++it;
}
}
}
while (!obs_to_remove.is_empty()) {
obs_to_remove.pop_front()->OnRemoved();
}
// We've processed the removal flag, so strip it
return flags & (~StateObserver::kNeedRemoval);
}
} // namespace
// Since this conditionally takes the dispatcher's |lock_|, based on
// the type of Mutex (either fbl::Mutex or fbl::NullLock), the thread
// safety analysis is unable to prove that the accesses to |signals_|
// and to |observers_| are always protected.
template <typename LockType>
void Dispatcher::AddObserverHelper(StateObserver* observer,
const StateObserver::CountInfo* cinfo,
Lock<LockType>* lock) TA_NO_THREAD_SAFETY_ANALYSIS {
ZX_DEBUG_ASSERT(is_waitable());
DEBUG_ASSERT(observer != nullptr);
StateObserver::Flags flags;
{
Guard<LockType> guard{lock};
flags = observer->OnInitialize(signals_, cinfo);
if (!(flags & StateObserver::kNeedRemoval))
observers_.push_front(observer);
}
if (flags & StateObserver::kNeedRemoval)
observer->OnRemoved();
kcounter_add(dispatcher_observe_count, 1);
}
void Dispatcher::AddObserver(StateObserver* observer, const StateObserver::CountInfo* cinfo) {
AddObserverHelper(observer, cinfo, get_lock());
}
void Dispatcher::AddObserverLocked(StateObserver* observer, const StateObserver::CountInfo* cinfo) {
// Type tag and local NullLock to make lockdep happy.
struct DispatcherAddObserverLocked {};
DECLARE_LOCK(DispatcherAddObserverLocked, fbl::NullLock) lock;
AddObserverHelper(observer, cinfo, &lock);
}
void Dispatcher::RemoveObserver(StateObserver* observer) {
ZX_DEBUG_ASSERT(is_waitable());
Guard<fbl::Mutex> guard{get_lock()};
DEBUG_ASSERT(observer != nullptr);
observers_.erase(*observer);
}
void Dispatcher::Cancel(const Handle* handle) {
ZX_DEBUG_ASSERT(is_waitable());
CancelWithFunc(&observers_, get_lock(), [handle](StateObserver* obs) {
return obs->OnCancel(handle);
});
kcounter_add(dispatcher_cancel_bh_count, 1);
}
bool Dispatcher::CancelByKey(const Handle* handle, const void* port, uint64_t key) {
ZX_DEBUG_ASSERT(is_waitable());
StateObserver::Flags flags = CancelWithFunc(&observers_, get_lock(),
[handle, port, key](StateObserver* obs) {
return obs->OnCancelByKey(handle, port, key);
});
kcounter_add(dispatcher_cancel_bk_count, 1);
return flags & StateObserver::kHandled;
}
// Since this conditionally takes the dispatcher's |lock_|, based on
// the type of Mutex (either fbl::Mutex or fbl::NullLock), the thread
// safety analysis is unable to prove that the accesses to |signals_|
// are always protected.
template <typename LockType>
void Dispatcher::UpdateStateHelper(zx_signals_t clear_mask,
zx_signals_t set_mask,
Lock<LockType>* lock) TA_NO_THREAD_SAFETY_ANALYSIS {
Dispatcher::ObserverList obs_to_remove;
{
Guard<LockType> guard{lock};
auto previous_signals = signals_;
signals_ &= ~clear_mask;
signals_ |= set_mask;
if (previous_signals == signals_)
return;
UpdateInternalLocked(&obs_to_remove, signals_);
}
while (!obs_to_remove.is_empty()) {
obs_to_remove.pop_front()->OnRemoved();
}
}
void Dispatcher::UpdateState(zx_signals_t clear_mask,
zx_signals_t set_mask) {
UpdateStateHelper(clear_mask, set_mask, get_lock());
}
void Dispatcher::UpdateStateLocked(zx_signals_t clear_mask,
zx_signals_t set_mask) {
// Type tag and local NullLock to make lockdep happy.
struct DispatcherUpdateStateLocked {};
DECLARE_LOCK(DispatcherUpdateStateLocked, fbl::NullLock) lock;
UpdateStateHelper(clear_mask, set_mask, &lock);
}
void Dispatcher::UpdateInternalLocked(ObserverList* obs_to_remove, zx_signals_t signals) {
ZX_DEBUG_ASSERT(is_waitable());
for (auto it = observers_.begin(); it != observers_.end();) {
StateObserver::Flags it_flags = it->OnStateChange(signals);
if (it_flags & StateObserver::kNeedRemoval) {
auto to_remove = it;
++it;
obs_to_remove->push_back(observers_.erase(to_remove));
} else {
++it;
}
}
}
zx_status_t Dispatcher::SetCookie(CookieJar* cookiejar, zx_koid_t scope, uint64_t cookie) {
if (cookiejar == nullptr)
return ZX_ERR_NOT_SUPPORTED;
Guard<fbl::Mutex> guard{get_lock()};
if (cookiejar->scope_ == ZX_KOID_INVALID) {
cookiejar->scope_ = scope;
cookiejar->cookie_ = cookie;
kcounter_add(dispatcher_cookie_set_count, 1);
return ZX_OK;
}
if (cookiejar->scope_ == scope) {
cookiejar->cookie_ = cookie;
kcounter_add(dispatcher_cookie_reset_count, 1);
return ZX_OK;
}
return ZX_ERR_ACCESS_DENIED;
}
zx_status_t Dispatcher::GetCookie(CookieJar* cookiejar, zx_koid_t scope, uint64_t* cookie) {
if (cookiejar == nullptr)
return ZX_ERR_NOT_SUPPORTED;
Guard<fbl::Mutex> guard{get_lock()};
if (cookiejar->scope_ == scope) {
*cookie = cookiejar->cookie_;
return ZX_OK;
}
return ZX_ERR_ACCESS_DENIED;
}
zx_status_t Dispatcher::InvalidateCookieLocked(CookieJar* cookiejar) {
if (cookiejar == nullptr)
return ZX_ERR_NOT_SUPPORTED;
cookiejar->scope_ = ZX_KOID_KERNEL;
return ZX_OK;
}
zx_status_t Dispatcher::InvalidateCookie(CookieJar* cookiejar) {
Guard<fbl::Mutex> guard{get_lock()};
return InvalidateCookieLocked(cookiejar);
}