Revert "Update gitignore"
[libside.git] / src / tgif.c
CommitLineData
d04d4903
MD
1// SPDX-License-Identifier: MIT
2/*
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 */
5
6#include <tgif/trace.h>
7#include <string.h>
8
9#include "rcu.h"
10#include "list.h"
11
12/* Top 8 bits reserved for kernel tracer use. */
13#if TGIF_BITS_PER_LONG == 64
14# define TGIF_EVENT_ENABLED_KERNEL_MASK 0xFF00000000000000ULL
15# define TGIF_EVENT_ENABLED_KERNEL_USER_EVENT_MASK 0x8000000000000000ULL
16
17/* Allow 2^56 tracer references on an event. */
18# define TGIF_EVENT_ENABLED_USER_MASK 0x00FFFFFFFFFFFFFFULL
19#else
20# define TGIF_EVENT_ENABLED_KERNEL_MASK 0xFF000000UL
21# define TGIF_EVENT_ENABLED_KERNEL_USER_EVENT_MASK 0x80000000UL
22
23/* Allow 2^24 tracer references on an event. */
24# define TGIF_EVENT_ENABLED_USER_MASK 0x00FFFFFFUL
25#endif
26
27struct tgif_events_register_handle {
28 struct tgif_list_node node;
29 struct tgif_event_description **events;
30 uint32_t nr_events;
31};
32
33struct tgif_tracer_handle {
34 struct tgif_list_node node;
35 void (*cb)(enum tgif_tracer_notification notif,
36 struct tgif_event_description **events, uint32_t nr_events, void *priv);
37 void *priv;
38};
39
40static struct tgif_rcu_gp_state rcu_gp;
41
42/*
43 * Lazy initialization for early use within library constructors.
44 */
45static bool initialized;
46/*
47 * Do not register/unregister any more events after destructor.
48 */
49static bool finalized;
50
51/*
52 * Recursive mutex to allow tracer callbacks to use the tgif API.
53 */
54static pthread_mutex_t tgif_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
55
56static DEFINE_TGIF_LIST_HEAD(tgif_events_list);
57static DEFINE_TGIF_LIST_HEAD(tgif_tracer_list);
58
59/*
60 * The empty callback has a NULL function callback pointer, which stops
61 * iteration on the array of callbacks immediately.
62 */
63const struct tgif_callback tgif_empty_callback = { };
64
65void tgif_call(const struct tgif_event_description *desc, const struct tgif_arg_vec *tgif_arg_vec)
66{
67 struct tgif_rcu_read_state rcu_read_state;
68 const struct tgif_callback *tgif_cb;
69 uintptr_t enabled;
70
71 if (tgif_unlikely(finalized))
72 return;
73 if (tgif_unlikely(!initialized))
74 tgif_init();
75 if (tgif_unlikely(desc->flags & TGIF_EVENT_FLAG_VARIADIC)) {
76 printf("ERROR: unexpected variadic event description\n");
77 abort();
78 }
79 enabled = __atomic_load_n(desc->enabled, __ATOMIC_RELAXED);
80 if (tgif_unlikely(enabled & TGIF_EVENT_ENABLED_KERNEL_USER_EVENT_MASK)) {
81 // TODO: call kernel write.
82 }
83 tgif_rcu_read_begin(&rcu_gp, &rcu_read_state);
84 for (tgif_cb = tgif_rcu_dereference(desc->callbacks); tgif_cb->u.call != NULL; tgif_cb++)
85 tgif_cb->u.call(desc, tgif_arg_vec, tgif_cb->priv);
86 tgif_rcu_read_end(&rcu_gp, &rcu_read_state);
87}
88
89void tgif_call_variadic(const struct tgif_event_description *desc,
90 const struct tgif_arg_vec *tgif_arg_vec,
91 const struct tgif_arg_dynamic_struct *var_struct)
92{
93 struct tgif_rcu_read_state rcu_read_state;
94 const struct tgif_callback *tgif_cb;
95 uintptr_t enabled;
96
97 if (tgif_unlikely(finalized))
98 return;
99 if (tgif_unlikely(!initialized))
100 tgif_init();
101 if (tgif_unlikely(!(desc->flags & TGIF_EVENT_FLAG_VARIADIC))) {
102 printf("ERROR: unexpected non-variadic event description\n");
103 abort();
104 }
105 enabled = __atomic_load_n(desc->enabled, __ATOMIC_RELAXED);
106 if (tgif_unlikely(enabled & TGIF_EVENT_ENABLED_KERNEL_USER_EVENT_MASK)) {
107 // TODO: call kernel write.
108 }
109 tgif_rcu_read_begin(&rcu_gp, &rcu_read_state);
110 for (tgif_cb = tgif_rcu_dereference(desc->callbacks); tgif_cb->u.call_variadic != NULL; tgif_cb++)
111 tgif_cb->u.call_variadic(desc, tgif_arg_vec, var_struct, tgif_cb->priv);
112 tgif_rcu_read_end(&rcu_gp, &rcu_read_state);
113}
114
115static
116const struct tgif_callback *tgif_tracer_callback_lookup(
117 const struct tgif_event_description *desc,
118 void *call, void *priv)
119{
120 const struct tgif_callback *cb;
121
122 for (cb = desc->callbacks; cb->u.call != NULL; cb++) {
123 if ((void *) cb->u.call == call && cb->priv == priv)
124 return cb;
125 }
126 return NULL;
127}
128
129static
130int _tgif_tracer_callback_register(struct tgif_event_description *desc,
131 void *call, void *priv)
132{
133 struct tgif_callback *old_cb, *new_cb;
134 int ret = TGIF_ERROR_OK;
135 uint32_t old_nr_cb;
136
137 if (!call)
138 return TGIF_ERROR_INVAL;
139 if (finalized)
140 return TGIF_ERROR_EXITING;
141 if (!initialized)
142 tgif_init();
143 pthread_mutex_lock(&tgif_lock);
144 old_nr_cb = desc->nr_callbacks;
145 if (old_nr_cb == UINT32_MAX) {
146 ret = TGIF_ERROR_INVAL;
147 goto unlock;
148 }
149 /* Reject duplicate (call, priv) tuples. */
150 if (tgif_tracer_callback_lookup(desc, call, priv)) {
151 ret = TGIF_ERROR_EXIST;
152 goto unlock;
153 }
154 old_cb = (struct tgif_callback *) desc->callbacks;
155 /* old_nr_cb + 1 (new cb) + 1 (NULL) */
156 new_cb = (struct tgif_callback *) calloc(old_nr_cb + 2, sizeof(struct tgif_callback));
157 if (!new_cb) {
158 ret = TGIF_ERROR_NOMEM;
159 goto unlock;
160 }
161 memcpy(new_cb, old_cb, old_nr_cb);
162 if (desc->flags & TGIF_EVENT_FLAG_VARIADIC)
163 new_cb[old_nr_cb].u.call_variadic =
164 (tgif_tracer_callback_variadic_func) call;
165 else
166 new_cb[old_nr_cb].u.call =
167 (tgif_tracer_callback_func) call;
168 new_cb[old_nr_cb].priv = priv;
169 tgif_rcu_assign_pointer(desc->callbacks, new_cb);
170 tgif_rcu_wait_grace_period(&rcu_gp);
171 if (old_nr_cb)
172 free(old_cb);
173 desc->nr_callbacks++;
174 /* Increment concurrently with kernel setting the top bits. */
175 if (!old_nr_cb)
176 (void) __atomic_add_fetch(desc->enabled, 1, __ATOMIC_RELAXED);
177unlock:
178 pthread_mutex_unlock(&tgif_lock);
179 return ret;
180}
181
182int tgif_tracer_callback_register(struct tgif_event_description *desc,
183 tgif_tracer_callback_func call,
184 void *priv)
185{
186 if (desc->flags & TGIF_EVENT_FLAG_VARIADIC)
187 return TGIF_ERROR_INVAL;
188 return _tgif_tracer_callback_register(desc, (void *) call, priv);
189}
190
191int tgif_tracer_callback_variadic_register(struct tgif_event_description *desc,
192 tgif_tracer_callback_variadic_func call_variadic,
193 void *priv)
194{
195 if (!(desc->flags & TGIF_EVENT_FLAG_VARIADIC))
196 return TGIF_ERROR_INVAL;
197 return _tgif_tracer_callback_register(desc, (void *) call_variadic, priv);
198}
199
200static int _tgif_tracer_callback_unregister(struct tgif_event_description *desc,
201 void *call, void *priv)
202{
203 struct tgif_callback *old_cb, *new_cb;
204 const struct tgif_callback *cb_pos;
205 uint32_t pos_idx;
206 int ret = TGIF_ERROR_OK;
207 uint32_t old_nr_cb;
208
209 if (!call)
210 return TGIF_ERROR_INVAL;
211 if (finalized)
212 return TGIF_ERROR_EXITING;
213 if (!initialized)
214 tgif_init();
215 pthread_mutex_lock(&tgif_lock);
216 cb_pos = tgif_tracer_callback_lookup(desc, call, priv);
217 if (!cb_pos) {
218 ret = TGIF_ERROR_NOENT;
219 goto unlock;
220 }
221 old_nr_cb = desc->nr_callbacks;
222 old_cb = (struct tgif_callback *) desc->callbacks;
223 if (old_nr_cb == 1) {
224 new_cb = (struct tgif_callback *) &tgif_empty_callback;
225 } else {
226 pos_idx = cb_pos - desc->callbacks;
227 /* Remove entry at pos_idx. */
228 /* old_nr_cb - 1 (removed cb) + 1 (NULL) */
229 new_cb = (struct tgif_callback *) calloc(old_nr_cb, sizeof(struct tgif_callback));
230 if (!new_cb) {
231 ret = TGIF_ERROR_NOMEM;
232 goto unlock;
233 }
234 memcpy(new_cb, old_cb, pos_idx);
235 memcpy(&new_cb[pos_idx], &old_cb[pos_idx + 1], old_nr_cb - pos_idx - 1);
236 }
237 tgif_rcu_assign_pointer(desc->callbacks, new_cb);
238 tgif_rcu_wait_grace_period(&rcu_gp);
239 free(old_cb);
240 desc->nr_callbacks--;
241 /* Decrement concurrently with kernel setting the top bits. */
242 if (old_nr_cb == 1)
243 (void) __atomic_add_fetch(desc->enabled, -1, __ATOMIC_RELAXED);
244unlock:
245 pthread_mutex_unlock(&tgif_lock);
246 return ret;
247}
248
249int tgif_tracer_callback_unregister(struct tgif_event_description *desc,
250 tgif_tracer_callback_func call,
251 void *priv)
252{
253 if (desc->flags & TGIF_EVENT_FLAG_VARIADIC)
254 return TGIF_ERROR_INVAL;
255 return _tgif_tracer_callback_unregister(desc, (void *) call, priv);
256}
257
258int tgif_tracer_callback_variadic_unregister(struct tgif_event_description *desc,
259 tgif_tracer_callback_variadic_func call_variadic,
260 void *priv)
261{
262 if (!(desc->flags & TGIF_EVENT_FLAG_VARIADIC))
263 return TGIF_ERROR_INVAL;
264 return _tgif_tracer_callback_unregister(desc, (void *) call_variadic, priv);
265}
266
267struct tgif_events_register_handle *tgif_events_register(struct tgif_event_description **events, uint32_t nr_events)
268{
269 struct tgif_events_register_handle *events_handle = NULL;
270 struct tgif_tracer_handle *tracer_handle;
271
272 if (finalized)
273 return NULL;
274 if (!initialized)
275 tgif_init();
276 events_handle = (struct tgif_events_register_handle *)
277 calloc(1, sizeof(struct tgif_events_register_handle));
278 if (!events_handle)
279 return NULL;
280 events_handle->events = events;
281 events_handle->nr_events = nr_events;
282
283 pthread_mutex_lock(&tgif_lock);
284 tgif_list_insert_node_tail(&tgif_events_list, &events_handle->node);
285 tgif_list_for_each_entry(tracer_handle, &tgif_tracer_list, node) {
286 tracer_handle->cb(TGIF_TRACER_NOTIFICATION_INSERT_EVENTS,
287 events, nr_events, tracer_handle->priv);
288 }
289 pthread_mutex_unlock(&tgif_lock);
290 //TODO: call event batch register ioctl
291 return events_handle;
292}
293
294static
295void tgif_event_remove_callbacks(struct tgif_event_description *desc)
296{
297 uint32_t nr_cb = desc->nr_callbacks;
298 struct tgif_callback *old_cb;
299
300 if (!nr_cb)
301 return;
302 old_cb = (struct tgif_callback *) desc->callbacks;
303 (void) __atomic_add_fetch(desc->enabled, -1, __ATOMIC_RELAXED);
304 /*
305 * Setting the state back to 0 cb and empty callbacks out of
306 * caution. This should not matter because instrumentation is
307 * unreachable.
308 */
309 desc->nr_callbacks = 0;
310 tgif_rcu_assign_pointer(desc->callbacks, &tgif_empty_callback);
311 /*
312 * No need to wait for grace period because instrumentation is
313 * unreachable.
314 */
315 free(old_cb);
316}
317
318/*
319 * Unregister event handle. At this point, all tgif events in that
320 * handle should be unreachable.
321 */
322void tgif_events_unregister(struct tgif_events_register_handle *events_handle)
323{
324 struct tgif_tracer_handle *tracer_handle;
325 uint32_t i;
326
327 if (!events_handle)
328 return;
329 if (finalized)
330 return;
331 if (!initialized)
332 tgif_init();
333 pthread_mutex_lock(&tgif_lock);
334 tgif_list_remove_node(&events_handle->node);
335 tgif_list_for_each_entry(tracer_handle, &tgif_tracer_list, node) {
336 tracer_handle->cb(TGIF_TRACER_NOTIFICATION_REMOVE_EVENTS,
337 events_handle->events, events_handle->nr_events,
338 tracer_handle->priv);
339 }
340 for (i = 0; i < events_handle->nr_events; i++) {
341 struct tgif_event_description *event = events_handle->events[i];
342
343 /* Skip NULL pointers */
344 if (!event)
345 continue;
346 tgif_event_remove_callbacks(event);
347 }
348 pthread_mutex_unlock(&tgif_lock);
349 //TODO: call event batch unregister ioctl
350 free(events_handle);
351}
352
353struct tgif_tracer_handle *tgif_tracer_event_notification_register(
354 void (*cb)(enum tgif_tracer_notification notif,
355 struct tgif_event_description **events, uint32_t nr_events, void *priv),
356 void *priv)
357{
358 struct tgif_tracer_handle *tracer_handle;
359 struct tgif_events_register_handle *events_handle;
360
361 if (finalized)
362 return NULL;
363 if (!initialized)
364 tgif_init();
365 tracer_handle = (struct tgif_tracer_handle *)
366 calloc(1, sizeof(struct tgif_tracer_handle));
367 if (!tracer_handle)
368 return NULL;
369 pthread_mutex_lock(&tgif_lock);
370 tracer_handle->cb = cb;
371 tracer_handle->priv = priv;
372 tgif_list_insert_node_tail(&tgif_tracer_list, &tracer_handle->node);
373 tgif_list_for_each_entry(events_handle, &tgif_events_list, node) {
374 cb(TGIF_TRACER_NOTIFICATION_INSERT_EVENTS,
375 events_handle->events, events_handle->nr_events, priv);
376 }
377 pthread_mutex_unlock(&tgif_lock);
378 return tracer_handle;
379}
380
381void tgif_tracer_event_notification_unregister(struct tgif_tracer_handle *tracer_handle)
382{
383 struct tgif_events_register_handle *events_handle;
384
385 if (finalized)
386 return;
387 if (!initialized)
388 tgif_init();
389 pthread_mutex_lock(&tgif_lock);
390 tgif_list_for_each_entry(events_handle, &tgif_events_list, node) {
391 tracer_handle->cb(TGIF_TRACER_NOTIFICATION_REMOVE_EVENTS,
392 events_handle->events, events_handle->nr_events,
393 tracer_handle->priv);
394 }
395 tgif_list_remove_node(&tracer_handle->node);
396 pthread_mutex_unlock(&tgif_lock);
397}
398
399void tgif_init(void)
400{
401 if (initialized)
402 return;
403 tgif_rcu_gp_init(&rcu_gp);
404 initialized = true;
405}
406
407/*
408 * tgif_exit() is executed from a library destructor. It can be called
409 * explicitly at application exit as well. Concurrent tgif API use is
410 * not expected at that point.
411 */
412void tgif_exit(void)
413{
414 struct tgif_events_register_handle *handle, *tmp;
415
416 if (finalized)
417 return;
418 tgif_list_for_each_entry_safe(handle, tmp, &tgif_events_list, node)
419 tgif_events_unregister(handle);
420 tgif_rcu_gp_exit(&rcu_gp);
421 finalized = true;
422}
This page took 0.03695 seconds and 4 git commands to generate.