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 | 2a83d60 | 2020-05-27 16:58:08 +0200 | [diff] [blame] | 16 | #include <haproxy/debug.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 17 | #include <haproxy/errors.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 18 | #include <haproxy/global.h> |
Willy Tarreau | 7f673c2 | 2021-05-08 12:27:42 +0200 | [diff] [blame] | 19 | #include <haproxy/signal-t.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 20 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 21 | #include <haproxy/tools.h> |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 22 | |
| 23 | |
| 24 | /* |
| 25 | * It relies on timer_create() and timer_settime() which are only available in |
| 26 | * this case. |
| 27 | */ |
Willy Tarreau | e58114e | 2020-03-04 10:53:07 +0100 | [diff] [blame] | 28 | #if defined(USE_RT) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 30 | /* Setup (or ping) the watchdog timer for thread <thr>. Returns non-zero on |
| 31 | * success, zero on failure. It interrupts once per second of CPU time. It |
| 32 | * happens that timers based on the CPU time are not automatically re-armed |
| 33 | * so we only use the value and leave the interval unset. |
| 34 | */ |
| 35 | int wdt_ping(int thr) |
| 36 | { |
| 37 | struct itimerspec its; |
| 38 | |
| 39 | its.it_value.tv_sec = 1; its.it_value.tv_nsec = 0; |
| 40 | its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 41 | return timer_settime(ha_thread_info[thr].wd_timer, 0, &its, NULL) == 0; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /* This is the WDTSIG signal handler */ |
| 45 | void wdt_handler(int sig, siginfo_t *si, void *arg) |
| 46 | { |
| 47 | unsigned long long n, p; |
| 48 | int thr; |
| 49 | |
| 50 | switch (si->si_code) { |
| 51 | case SI_TIMER: |
| 52 | /* A thread's timer fired, the thread ID is in si_int. We have |
| 53 | * no guarantee that the thread handling this signal is in any |
| 54 | * way related to the one triggering it, so we need to retrieve |
| 55 | * the thread number from there. Note: this thread might |
| 56 | * continue to execute in parallel. |
| 57 | */ |
Willy Tarreau | 02255b2 | 2019-05-23 08:36:29 +0200 | [diff] [blame] | 58 | thr = si->si_value.sival_int; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 59 | |
| 60 | /* cannot happen unless an unknown timer tries to play with our |
| 61 | * nerves. Let's die for now if this happens. |
| 62 | */ |
| 63 | if (thr < 0 || thr >= global.nbthread) |
| 64 | break; |
| 65 | |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 66 | p = ha_thread_info[thr].prev_cpu_time; |
| 67 | n = now_cpu_time_thread(&ha_thread_info[thr]); |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 68 | |
William Lallemand | fd91346 | 2022-05-13 11:03:39 +0200 | [diff] [blame] | 69 | /* not yet reached the deadline of 1 sec, |
| 70 | * or p wasn't initialized yet |
| 71 | */ |
| 72 | if (!p || n - p < 1000000000UL) |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 73 | goto update_and_leave; |
| 74 | |
Willy Tarreau | a37cb18 | 2019-07-31 19:20:39 +0200 | [diff] [blame] | 75 | if ((threads_harmless_mask|sleeping_thread_mask|threads_to_dump) & (1UL << thr)) { |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 76 | /* This thread is currently doing exactly nothing |
| 77 | * waiting in the poll loop (unlikely but possible), |
| 78 | * waiting for all other threads to join the rendez-vous |
| 79 | * point (common), or waiting for another thread to |
| 80 | * finish an isolated operation (unlikely but possible). |
| 81 | */ |
| 82 | goto update_and_leave; |
| 83 | } |
| 84 | |
| 85 | /* So the thread indeed appears locked up. In order to be |
| 86 | * certain that we're not witnessing an exceptional spike of |
| 87 | * CPU usage due to a configuration issue (like running tens |
| 88 | * of thousands of tasks in a single loop), we'll check if the |
| 89 | * scheduler is still alive by setting the TI_FL_STUCK flag |
| 90 | * that the scheduler clears when switching to the next task. |
| 91 | * If it's already set, then it's our second call with no |
| 92 | * progress and the thread is dead. |
| 93 | */ |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 94 | if (!(ha_thread_info[thr].flags & TI_FL_STUCK)) { |
| 95 | _HA_ATOMIC_OR(&ha_thread_info[thr].flags, TI_FL_STUCK); |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 96 | goto update_and_leave; |
| 97 | } |
| 98 | |
| 99 | /* No doubt now, there's no hop to recover, die loudly! */ |
| 100 | break; |
Willy Tarreau | 0627815 | 2020-03-10 09:26:17 +0100 | [diff] [blame] | 101 | #ifdef USE_THREAD |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 102 | case SI_TKILL: |
| 103 | /* we got a pthread_kill, stop on it */ |
| 104 | thr = tid; |
| 105 | break; |
Willy Tarreau | 0627815 | 2020-03-10 09:26:17 +0100 | [diff] [blame] | 106 | #endif |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 107 | default: |
| 108 | /* unhandled other conditions */ |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | /* By default we terminate. If we're not on the victim thread, better |
| 113 | * bounce the signal there so that we produce a cleaner stack trace |
| 114 | * with the other thread interrupted exactly where it was running and |
| 115 | * the current one not involved in this. |
| 116 | */ |
Willy Tarreau | e58114e | 2020-03-04 10:53:07 +0100 | [diff] [blame] | 117 | #ifdef USE_THREAD |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 118 | if (thr != tid) |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 119 | pthread_kill(ha_thread_info[thr].pthread, sig); |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 120 | else |
Willy Tarreau | e58114e | 2020-03-04 10:53:07 +0100 | [diff] [blame] | 121 | #endif |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 122 | ha_panic(); |
| 123 | return; |
| 124 | |
| 125 | update_and_leave: |
| 126 | wdt_ping(thr); |
| 127 | } |
| 128 | |
| 129 | int init_wdt_per_thread() |
| 130 | { |
Willy Tarreau | c1563e5 | 2020-02-26 14:03:05 +0100 | [diff] [blame] | 131 | struct sigevent sev = { }; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 132 | sigset_t set; |
| 133 | |
| 134 | /* unblock the WDTSIG signal we intend to use */ |
| 135 | sigemptyset(&set); |
| 136 | sigaddset(&set, WDTSIG); |
| 137 | ha_sigmask(SIG_UNBLOCK, &set, NULL); |
| 138 | |
| 139 | /* this timer will signal WDTSIG when it fires, with tid in the si_int |
| 140 | * field (important since any thread will receive the signal). |
| 141 | */ |
| 142 | sev.sigev_notify = SIGEV_SIGNAL; |
| 143 | sev.sigev_signo = WDTSIG; |
| 144 | sev.sigev_value.sival_int = tid; |
Willy Tarreau | d6f1966 | 2020-03-04 10:48:18 +0100 | [diff] [blame] | 145 | if (timer_create(ti->clock_id, &sev, &ti->wd_timer) == -1 && |
| 146 | timer_create(CLOCK_REALTIME, &sev, &ti->wd_timer) == -1) |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 147 | goto fail1; |
| 148 | |
| 149 | if (!wdt_ping(tid)) |
| 150 | goto fail2; |
| 151 | |
| 152 | return 1; |
| 153 | |
| 154 | fail2: |
| 155 | timer_delete(ti->wd_timer); |
| 156 | fail1: |
| 157 | ti->wd_timer = TIMER_INVALID; |
| 158 | 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] | 159 | return 1; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void deinit_wdt_per_thread() |
| 163 | { |
| 164 | if (ti->wd_timer != TIMER_INVALID) |
| 165 | timer_delete(ti->wd_timer); |
| 166 | } |
| 167 | |
| 168 | /* registers the watchdog signal handler and returns 0. This sets up the signal |
| 169 | * handler for WDTSIG, so it must be called once per process. |
| 170 | */ |
| 171 | int init_wdt() |
| 172 | { |
| 173 | struct sigaction sa; |
| 174 | |
| 175 | sa.sa_handler = NULL; |
| 176 | sa.sa_sigaction = wdt_handler; |
| 177 | sigemptyset(&sa.sa_mask); |
| 178 | sa.sa_flags = SA_SIGINFO; |
| 179 | sigaction(WDTSIG, &sa, NULL); |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 180 | return ERR_NONE; |
Willy Tarreau | 2bfefdb | 2019-05-03 13:52:18 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | REGISTER_POST_CHECK(init_wdt); |
| 184 | REGISTER_PER_THREAD_INIT(init_wdt_per_thread); |
| 185 | REGISTER_PER_THREAD_DEINIT(deinit_wdt_per_thread); |
| 186 | #endif |