| 1 | /* |
| 2 | * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include <assert.h> |
| 11 | #include <inttypes.h> |
| 12 | #include <signal.h> |
| 13 | |
| 14 | #include "timer.h" |
| 15 | #include "health-sessiond.h" |
| 16 | #include "rotation-thread.h" |
| 17 | #include "thread.h" |
| 18 | |
| 19 | #define LTTNG_SESSIOND_SIG_QS SIGRTMIN + 10 |
| 20 | #define LTTNG_SESSIOND_SIG_EXIT SIGRTMIN + 11 |
| 21 | #define LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK SIGRTMIN + 12 |
| 22 | #define LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION SIGRTMIN + 13 |
| 23 | |
| 24 | #define UINT_TO_PTR(value) \ |
| 25 | ({ \ |
| 26 | assert(value <= UINTPTR_MAX); \ |
| 27 | (void *) (uintptr_t) value; \ |
| 28 | }) |
| 29 | #define PTR_TO_UINT(ptr) ((uintptr_t) ptr) |
| 30 | |
| 31 | /* |
| 32 | * Handle timer teardown race wrt memory free of private data by sessiond |
| 33 | * signals are handled by a single thread, which permits a synchronization |
| 34 | * point between handling of each signal. Internal lock ensures mutual |
| 35 | * exclusion. |
| 36 | */ |
| 37 | static |
| 38 | struct timer_signal_data { |
| 39 | /* Thread managing signals. */ |
| 40 | pthread_t tid; |
| 41 | int qs_done; |
| 42 | pthread_mutex_t lock; |
| 43 | } timer_signal = { |
| 44 | .tid = 0, |
| 45 | .qs_done = 0, |
| 46 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * Set custom signal mask to current thread. |
| 51 | */ |
| 52 | static |
| 53 | void setmask(sigset_t *mask) |
| 54 | { |
| 55 | int ret; |
| 56 | |
| 57 | ret = sigemptyset(mask); |
| 58 | if (ret) { |
| 59 | PERROR("sigemptyset"); |
| 60 | } |
| 61 | ret = sigaddset(mask, LTTNG_SESSIOND_SIG_QS); |
| 62 | if (ret) { |
| 63 | PERROR("sigaddset teardown"); |
| 64 | } |
| 65 | ret = sigaddset(mask, LTTNG_SESSIOND_SIG_EXIT); |
| 66 | if (ret) { |
| 67 | PERROR("sigaddset exit"); |
| 68 | } |
| 69 | ret = sigaddset(mask, LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK); |
| 70 | if (ret) { |
| 71 | PERROR("sigaddset pending rotation check"); |
| 72 | } |
| 73 | ret = sigaddset(mask, LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION); |
| 74 | if (ret) { |
| 75 | PERROR("sigaddset scheduled rotation"); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * This is the same function as timer_signal_thread_qs, when it |
| 81 | * returns, it means that no timer signr is currently pending or being handled |
| 82 | * by the timer thread. This cannot be called from the timer thread. |
| 83 | */ |
| 84 | static |
| 85 | void timer_signal_thread_qs(unsigned int signr) |
| 86 | { |
| 87 | sigset_t pending_set; |
| 88 | int ret; |
| 89 | |
| 90 | /* |
| 91 | * We need to be the only thread interacting with the thread |
| 92 | * that manages signals for teardown synchronization. |
| 93 | */ |
| 94 | pthread_mutex_lock(&timer_signal.lock); |
| 95 | |
| 96 | /* Ensure we don't have any signal queued for this session. */ |
| 97 | for (;;) { |
| 98 | ret = sigemptyset(&pending_set); |
| 99 | if (ret == -1) { |
| 100 | PERROR("sigemptyset"); |
| 101 | } |
| 102 | ret = sigpending(&pending_set); |
| 103 | if (ret == -1) { |
| 104 | PERROR("sigpending"); |
| 105 | } |
| 106 | if (!sigismember(&pending_set, signr)) { |
| 107 | break; |
| 108 | } |
| 109 | caa_cpu_relax(); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * From this point, no new signal handler will be fired that would try to |
| 114 | * access "session". However, we still need to wait for any currently |
| 115 | * executing handler to complete. |
| 116 | */ |
| 117 | cmm_smp_mb(); |
| 118 | CMM_STORE_SHARED(timer_signal.qs_done, 0); |
| 119 | cmm_smp_mb(); |
| 120 | |
| 121 | /* |
| 122 | * Kill with LTTNG_SESSIOND_SIG_QS, so signal management thread |
| 123 | * wakes up. |
| 124 | */ |
| 125 | kill(getpid(), LTTNG_SESSIOND_SIG_QS); |
| 126 | |
| 127 | while (!CMM_LOAD_SHARED(timer_signal.qs_done)) { |
| 128 | caa_cpu_relax(); |
| 129 | } |
| 130 | cmm_smp_mb(); |
| 131 | |
| 132 | pthread_mutex_unlock(&timer_signal.lock); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Start a timer on a session that will fire at a given interval |
| 137 | * (timer_interval_us) and fire a given signal (signal). |
| 138 | * |
| 139 | * Returns a negative value on error, 0 if a timer was created, and |
| 140 | * a positive value if no timer was created (not an error). |
| 141 | */ |
| 142 | static |
| 143 | int timer_start(timer_t *timer_id, struct ltt_session *session, |
| 144 | unsigned int timer_interval_us, int signal, bool one_shot) |
| 145 | { |
| 146 | int ret = 0, delete_ret; |
| 147 | struct sigevent sev = {}; |
| 148 | struct itimerspec its; |
| 149 | |
| 150 | sev.sigev_notify = SIGEV_SIGNAL; |
| 151 | sev.sigev_signo = signal; |
| 152 | sev.sigev_value.sival_ptr = session; |
| 153 | ret = timer_create(CLOCK_MONOTONIC, &sev, timer_id); |
| 154 | if (ret == -1) { |
| 155 | PERROR("timer_create"); |
| 156 | goto end; |
| 157 | } |
| 158 | |
| 159 | its.it_value.tv_sec = timer_interval_us / 1000000; |
| 160 | its.it_value.tv_nsec = (timer_interval_us % 1000000) * 1000; |
| 161 | if (one_shot) { |
| 162 | its.it_interval.tv_sec = 0; |
| 163 | its.it_interval.tv_nsec = 0; |
| 164 | } else { |
| 165 | its.it_interval.tv_sec = its.it_value.tv_sec; |
| 166 | its.it_interval.tv_nsec = its.it_value.tv_nsec; |
| 167 | } |
| 168 | |
| 169 | ret = timer_settime(*timer_id, 0, &its, NULL); |
| 170 | if (ret == -1) { |
| 171 | PERROR("timer_settime"); |
| 172 | goto error_destroy_timer; |
| 173 | } |
| 174 | goto end; |
| 175 | |
| 176 | error_destroy_timer: |
| 177 | delete_ret = timer_delete(*timer_id); |
| 178 | if (delete_ret == -1) { |
| 179 | PERROR("timer_delete"); |
| 180 | } |
| 181 | |
| 182 | end: |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | static |
| 187 | int timer_stop(timer_t *timer_id, int signal) |
| 188 | { |
| 189 | int ret = 0; |
| 190 | |
| 191 | ret = timer_delete(*timer_id); |
| 192 | if (ret == -1) { |
| 193 | PERROR("timer_delete"); |
| 194 | goto end; |
| 195 | } |
| 196 | |
| 197 | timer_signal_thread_qs(signal); |
| 198 | *timer_id = 0; |
| 199 | end: |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | int timer_session_rotation_pending_check_start(struct ltt_session *session, |
| 204 | unsigned int interval_us) |
| 205 | { |
| 206 | int ret; |
| 207 | |
| 208 | if (!session_get(session)) { |
| 209 | ret = -1; |
| 210 | goto end; |
| 211 | } |
| 212 | DBG("Enabling session rotation pending check timer on session %" PRIu64, |
| 213 | session->id); |
| 214 | /* |
| 215 | * We arm this timer in a one-shot mode so we don't have to disable it |
| 216 | * explicitly (which could deadlock if the timer thread is blocked |
| 217 | * writing in the rotation_timer_pipe). |
| 218 | * |
| 219 | * Instead, we re-arm it if needed after the rotation_pending check as |
| 220 | * returned. Also, this timer is usually only needed once, so there is |
| 221 | * no need to go through the whole signal teardown scheme everytime. |
| 222 | */ |
| 223 | ret = timer_start(&session->rotation_pending_check_timer, |
| 224 | session, interval_us, |
| 225 | LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK, |
| 226 | /* one-shot */ true); |
| 227 | if (ret == 0) { |
| 228 | session->rotation_pending_check_timer_enabled = true; |
| 229 | } |
| 230 | end: |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Call with session and session_list locks held. |
| 236 | */ |
| 237 | int timer_session_rotation_pending_check_stop(struct ltt_session *session) |
| 238 | { |
| 239 | int ret; |
| 240 | |
| 241 | assert(session); |
| 242 | assert(session->rotation_pending_check_timer_enabled); |
| 243 | |
| 244 | DBG("Disabling session rotation pending check timer on session %" PRIu64, |
| 245 | session->id); |
| 246 | ret = timer_stop(&session->rotation_pending_check_timer, |
| 247 | LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK); |
| 248 | if (ret == -1) { |
| 249 | ERR("Failed to stop rotate_pending_check timer"); |
| 250 | } else { |
| 251 | session->rotation_pending_check_timer_enabled = false; |
| 252 | /* |
| 253 | * The timer's reference to the session can be released safely. |
| 254 | */ |
| 255 | session_put(session); |
| 256 | } |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Call with session and session_list locks held. |
| 262 | */ |
| 263 | int timer_session_rotation_schedule_timer_start(struct ltt_session *session, |
| 264 | unsigned int interval_us) |
| 265 | { |
| 266 | int ret; |
| 267 | |
| 268 | if (!session_get(session)) { |
| 269 | ret = -1; |
| 270 | goto end; |
| 271 | } |
| 272 | DBG("Enabling scheduled rotation timer on session \"%s\" (%ui %s)", session->name, |
| 273 | interval_us, USEC_UNIT); |
| 274 | ret = timer_start(&session->rotation_schedule_timer, session, |
| 275 | interval_us, LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION, |
| 276 | /* one-shot */ false); |
| 277 | if (ret < 0) { |
| 278 | goto end; |
| 279 | } |
| 280 | session->rotation_schedule_timer_enabled = true; |
| 281 | end: |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Call with session and session_list locks held. |
| 287 | */ |
| 288 | int timer_session_rotation_schedule_timer_stop(struct ltt_session *session) |
| 289 | { |
| 290 | int ret = 0; |
| 291 | |
| 292 | assert(session); |
| 293 | |
| 294 | if (!session->rotation_schedule_timer_enabled) { |
| 295 | goto end; |
| 296 | } |
| 297 | |
| 298 | DBG("Disabling scheduled rotation timer on session %s", session->name); |
| 299 | ret = timer_stop(&session->rotation_schedule_timer, |
| 300 | LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION); |
| 301 | if (ret < 0) { |
| 302 | ERR("Failed to stop scheduled rotation timer of session \"%s\"", |
| 303 | session->name); |
| 304 | goto end; |
| 305 | } |
| 306 | |
| 307 | session->rotation_schedule_timer_enabled = false; |
| 308 | /* The timer's reference to the session can be released safely. */ |
| 309 | session_put(session); |
| 310 | ret = 0; |
| 311 | end: |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Block the RT signals for the entire process. It must be called from the |
| 317 | * sessiond main before creating the threads |
| 318 | */ |
| 319 | int timer_signal_init(void) |
| 320 | { |
| 321 | int ret; |
| 322 | sigset_t mask; |
| 323 | |
| 324 | /* Block signal for entire process, so only our thread processes it. */ |
| 325 | setmask(&mask); |
| 326 | ret = pthread_sigmask(SIG_BLOCK, &mask, NULL); |
| 327 | if (ret) { |
| 328 | errno = ret; |
| 329 | PERROR("pthread_sigmask"); |
| 330 | return -1; |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * This thread is the sighandler for the timer signals. |
| 337 | */ |
| 338 | static |
| 339 | void *thread_timer(void *data) |
| 340 | { |
| 341 | int signr; |
| 342 | sigset_t mask; |
| 343 | siginfo_t info; |
| 344 | struct timer_thread_parameters *ctx = data; |
| 345 | |
| 346 | rcu_register_thread(); |
| 347 | rcu_thread_online(); |
| 348 | |
| 349 | health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_TIMER); |
| 350 | health_code_update(); |
| 351 | |
| 352 | /* Only self thread will receive signal mask. */ |
| 353 | setmask(&mask); |
| 354 | CMM_STORE_SHARED(timer_signal.tid, pthread_self()); |
| 355 | |
| 356 | while (1) { |
| 357 | health_code_update(); |
| 358 | |
| 359 | health_poll_entry(); |
| 360 | signr = sigwaitinfo(&mask, &info); |
| 361 | health_poll_exit(); |
| 362 | |
| 363 | /* |
| 364 | * NOTE: cascading conditions are used instead of a switch case |
| 365 | * since the use of SIGRTMIN in the definition of the signals' |
| 366 | * values prevents the reduction to an integer constant. |
| 367 | */ |
| 368 | if (signr == -1) { |
| 369 | if (errno != EINTR) { |
| 370 | PERROR("sigwaitinfo"); |
| 371 | } |
| 372 | continue; |
| 373 | } else if (signr == LTTNG_SESSIOND_SIG_QS) { |
| 374 | cmm_smp_mb(); |
| 375 | CMM_STORE_SHARED(timer_signal.qs_done, 1); |
| 376 | cmm_smp_mb(); |
| 377 | } else if (signr == LTTNG_SESSIOND_SIG_EXIT) { |
| 378 | goto end; |
| 379 | } else if (signr == LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK) { |
| 380 | struct ltt_session *session = |
| 381 | (struct ltt_session *) info.si_value.sival_ptr; |
| 382 | |
| 383 | rotation_thread_enqueue_job(ctx->rotation_thread_job_queue, |
| 384 | ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION, |
| 385 | session); |
| 386 | } else if (signr == LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION) { |
| 387 | rotation_thread_enqueue_job(ctx->rotation_thread_job_queue, |
| 388 | ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION, |
| 389 | (struct ltt_session *) info.si_value.sival_ptr); |
| 390 | /* |
| 391 | * The scheduled periodic rotation timer is not in |
| 392 | * "one-shot" mode. The reference to the session is not |
| 393 | * released since the timer is still enabled and can |
| 394 | * still fire. |
| 395 | */ |
| 396 | } else { |
| 397 | ERR("Unexpected signal %d", info.si_signo); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | end: |
| 402 | DBG("Thread exit"); |
| 403 | health_unregister(the_health_sessiond); |
| 404 | rcu_thread_offline(); |
| 405 | rcu_unregister_thread(); |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | static |
| 410 | bool shutdown_timer_thread(void *data) |
| 411 | { |
| 412 | return kill(getpid(), LTTNG_SESSIOND_SIG_EXIT) == 0; |
| 413 | } |
| 414 | |
| 415 | bool launch_timer_thread( |
| 416 | struct timer_thread_parameters *timer_thread_parameters) |
| 417 | { |
| 418 | struct lttng_thread *thread; |
| 419 | |
| 420 | thread = lttng_thread_create("Timer", |
| 421 | thread_timer, |
| 422 | shutdown_timer_thread, |
| 423 | NULL, |
| 424 | timer_thread_parameters); |
| 425 | if (!thread) { |
| 426 | goto error; |
| 427 | } |
| 428 | lttng_thread_put(thread); |
| 429 | return true; |
| 430 | error: |
| 431 | return false; |
| 432 | } |