Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Thread lockup detection |
| 3 | * |
| 4 | * Copyright 2000-2019 Willy Tarreau <willy@haproxy.org>. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <signal.h> |
| 13 | #include <time.h> |
| 14 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 15 | #include <haproxy/api.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 16 | #include <haproxy/clock.h> |
Willy Tarreau | 2a83d60 | 2020-05-27 16:58:08 +0200 | [diff] [blame] | 17 | #include <haproxy/debug.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 18 | #include <haproxy/errors.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 19 | #include <haproxy/global.h> |
Willy Tarreau | 7f673c2 | 2021-05-08 12:27:42 +0200 | [diff] [blame] | 20 | #include <haproxy/signal-t.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 21 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 22 | #include <haproxy/tools.h> |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 23 | |
| 24 | |
| 25 | /* |
| 26 | * It relies on timer_create() and timer_settime() which are only available in |
| 27 | * this case. |
| 28 | */ |
Willy Tarreau | b474f43 | 2021-10-08 15:23:26 +0200 | [diff] [blame] | 29 | #if defined(USE_RT) && defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) |
| 30 | |
| 31 | /* define a dummy value to designate "no timer". Use only 32 bits. */ |
| 32 | #ifndef TIMER_INVALID |
| 33 | #define TIMER_INVALID ((timer_t)(unsigned long)(0xfffffffful)) |
| 34 | #endif |
| 35 | |
| 36 | static timer_t per_thread_wd_timer[MAX_THREADS]; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 38 | /* Setup (or ping) the watchdog timer for thread <thr>. Returns non-zero on |
| 39 | * success, zero on failure. It interrupts once per second of CPU time. It |
| 40 | * happens that timers based on the CPU time are not automatically re-armed |
| 41 | * so we only use the value and leave the interval unset. |
| 42 | */ |
| 43 | int wdt_ping(int thr) |
| 44 | { |
| 45 | struct itimerspec its; |
| 46 | |
| 47 | its.it_value.tv_sec = 1; its.it_value.tv_nsec = 0; |
| 48 | its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; |
Willy Tarreau | b474f43 | 2021-10-08 15:23:26 +0200 | [diff] [blame] | 49 | return timer_settime(per_thread_wd_timer[thr], 0, &its, NULL) == 0; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* This is the WDTSIG signal handler */ |
| 53 | void wdt_handler(int sig, siginfo_t *si, void *arg) |
| 54 | { |
| 55 | unsigned long long n, p; |
Willy Tarreau | adc1f52 | 2022-06-27 16:20:13 +0200 | [diff] [blame^] | 56 | ulong thr_bit; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 57 | int thr; |
| 58 | |
| 59 | switch (si->si_code) { |
| 60 | case SI_TIMER: |
| 61 | /* A thread's timer fired, the thread ID is in si_int. We have |
| 62 | * no guarantee that the thread handling this signal is in any |
| 63 | * way related to the one triggering it, so we need to retrieve |
| 64 | * the thread number from there. Note: this thread might |
| 65 | * continue to execute in parallel. |
| 66 | */ |
Willy Tarreau | 02255b2 | 2019-05-23 08:36:29 +0200 | [diff] [blame] | 67 | thr = si->si_value.sival_int; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 68 | |
| 69 | /* cannot happen unless an unknown timer tries to play with our |
| 70 | * nerves. Let's die for now if this happens. |
| 71 | */ |
| 72 | if (thr < 0 || thr >= global.nbthread) |
| 73 | break; |
| 74 | |
Willy Tarreau | adc1f52 | 2022-06-27 16:20:13 +0200 | [diff] [blame^] | 75 | thr_bit = ha_thread_info[thr].ltid_bit; |
Willy Tarreau | 45c38e2 | 2021-09-30 18:28:49 +0200 | [diff] [blame] | 76 | p = ha_thread_ctx[thr].prev_cpu_time; |
Willy Tarreau | 2169498 | 2021-10-08 15:09:17 +0200 | [diff] [blame] | 77 | n = now_cpu_time_thread(thr); |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 78 | |
William Lallemand | ae053b3 | 2022-05-13 11:03:39 +0200 | [diff] [blame] | 79 | /* not yet reached the deadline of 1 sec, |
| 80 | * or p wasn't initialized yet |
| 81 | */ |
| 82 | if (!p || n - p < 1000000000UL) |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 83 | goto update_and_leave; |
| 84 | |
Willy Tarreau | e7475c8 | 2022-06-20 09:23:24 +0200 | [diff] [blame] | 85 | if ((_HA_ATOMIC_LOAD(&th_ctx->flags) & TH_FL_SLEEPING) && |
Willy Tarreau | adc1f52 | 2022-06-27 16:20:13 +0200 | [diff] [blame^] | 86 | ((threads_harmless_mask|threads_to_dump) & thr_bit)) { |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 87 | /* This thread is currently doing exactly nothing |
| 88 | * waiting in the poll loop (unlikely but possible), |
| 89 | * waiting for all other threads to join the rendez-vous |
| 90 | * point (common), or waiting for another thread to |
| 91 | * finish an isolated operation (unlikely but possible). |
| 92 | */ |
| 93 | goto update_and_leave; |
| 94 | } |
| 95 | |
| 96 | /* So the thread indeed appears locked up. In order to be |
| 97 | * certain that we're not witnessing an exceptional spike of |
| 98 | * CPU usage due to a configuration issue (like running tens |
| 99 | * of thousands of tasks in a single loop), we'll check if the |
Willy Tarreau | a0b9953 | 2021-09-30 18:48:37 +0200 | [diff] [blame] | 100 | * scheduler is still alive by setting the TH_FL_STUCK flag |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 101 | * that the scheduler clears when switching to the next task. |
| 102 | * If it's already set, then it's our second call with no |
| 103 | * progress and the thread is dead. |
| 104 | */ |
Willy Tarreau | bdcd325 | 2022-06-22 09:19:46 +0200 | [diff] [blame] | 105 | if (!(_HA_ATOMIC_LOAD(&ha_thread_ctx[thr].flags) & TH_FL_STUCK)) { |
Willy Tarreau | a0b9953 | 2021-09-30 18:48:37 +0200 | [diff] [blame] | 106 | _HA_ATOMIC_OR(&ha_thread_ctx[thr].flags, TH_FL_STUCK); |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 107 | goto update_and_leave; |
| 108 | } |
| 109 | |
| 110 | /* No doubt now, there's no hop to recover, die loudly! */ |
| 111 | break; |
Willy Tarreau | 6414e44 | 2021-10-08 15:31:04 +0200 | [diff] [blame] | 112 | |
| 113 | #if defined(USE_THREAD) && defined(SI_TKILL) /* Linux uses this */ |
| 114 | |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 115 | case SI_TKILL: |
| 116 | /* we got a pthread_kill, stop on it */ |
| 117 | thr = tid; |
| 118 | break; |
Willy Tarreau | 6414e44 | 2021-10-08 15:31:04 +0200 | [diff] [blame] | 119 | |
| 120 | #elif defined(USE_THREAD) && defined(SI_LWP) /* FreeBSD uses this */ |
| 121 | |
| 122 | case SI_LWP: |
| 123 | /* we got a pthread_kill, stop on it */ |
| 124 | thr = tid; |
| 125 | break; |
| 126 | |
Willy Tarreau | 0627815 | 2020-03-10 09:26:17 +0100 | [diff] [blame] | 127 | #endif |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 128 | default: |
| 129 | /* unhandled other conditions */ |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | /* By default we terminate. If we're not on the victim thread, better |
| 134 | * bounce the signal there so that we produce a cleaner stack trace |
| 135 | * with the other thread interrupted exactly where it was running and |
| 136 | * the current one not involved in this. |
| 137 | */ |
Willy Tarreau | e58114e | 2020-03-04 10:53:07 +0100 | [diff] [blame] | 138 | #ifdef USE_THREAD |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 139 | if (thr != tid) |
Willy Tarreau | 19b18ad | 2021-10-06 21:43:38 +0200 | [diff] [blame] | 140 | ha_tkill(thr, sig); |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 141 | else |
Willy Tarreau | e58114e | 2020-03-04 10:53:07 +0100 | [diff] [blame] | 142 | #endif |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 143 | ha_panic(); |
| 144 | return; |
| 145 | |
| 146 | update_and_leave: |
| 147 | wdt_ping(thr); |
| 148 | } |
| 149 | |
| 150 | int init_wdt_per_thread() |
| 151 | { |
Willy Tarreau | b474f43 | 2021-10-08 15:23:26 +0200 | [diff] [blame] | 152 | if (!clock_setup_signal_timer(&per_thread_wd_timer[tid], WDTSIG, tid)) |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 153 | goto fail1; |
| 154 | |
| 155 | if (!wdt_ping(tid)) |
| 156 | goto fail2; |
| 157 | |
| 158 | return 1; |
| 159 | |
| 160 | fail2: |
Willy Tarreau | b474f43 | 2021-10-08 15:23:26 +0200 | [diff] [blame] | 161 | timer_delete(per_thread_wd_timer[tid]); |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 162 | fail1: |
Willy Tarreau | b474f43 | 2021-10-08 15:23:26 +0200 | [diff] [blame] | 163 | per_thread_wd_timer[tid] = TIMER_INVALID; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 164 | ha_warning("Failed to setup watchdog timer for thread %u, disabling lockup detection.\n", tid); |
Willy Tarreau | 7259fa2 | 2020-03-04 10:46:13 +0100 | [diff] [blame] | 165 | return 1; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void deinit_wdt_per_thread() |
| 169 | { |
Willy Tarreau | b474f43 | 2021-10-08 15:23:26 +0200 | [diff] [blame] | 170 | if (per_thread_wd_timer[tid] != TIMER_INVALID) |
| 171 | timer_delete(per_thread_wd_timer[tid]); |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* registers the watchdog signal handler and returns 0. This sets up the signal |
| 175 | * handler for WDTSIG, so it must be called once per process. |
| 176 | */ |
| 177 | int init_wdt() |
| 178 | { |
| 179 | struct sigaction sa; |
| 180 | |
| 181 | sa.sa_handler = NULL; |
| 182 | sa.sa_sigaction = wdt_handler; |
| 183 | sigemptyset(&sa.sa_mask); |
| 184 | sa.sa_flags = SA_SIGINFO; |
| 185 | sigaction(WDTSIG, &sa, NULL); |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 186 | return ERR_NONE; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | REGISTER_POST_CHECK(init_wdt); |
| 190 | REGISTER_PER_THREAD_INIT(init_wdt_per_thread); |
| 191 | REGISTER_PER_THREAD_DEINIT(deinit_wdt_per_thread); |
| 192 | #endif |