Fix: hash table cleanup call_rcu deadlock
[lttng-tools.git] / src / bin / lttng-sessiond / ht-cleanup.c
CommitLineData
0b2dc8df
MD
1/*
2 * Copyright (C) 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18#define _GNU_SOURCE
19#include <assert.h>
20
21#include <common/hashtable/hashtable.h>
22#include <common/common.h>
23#include <common/utils.h>
24
25#include "lttng-sessiond.h"
26#include "health.h"
27
28void *thread_ht_cleanup(void *data)
29{
30 int ret, i, pollfd, err = -1;
31 uint32_t revents, nb_fd;
32 struct lttng_poll_event events;
33
34 DBG("[ht-thread] startup.");
35
36 rcu_register_thread();
37 rcu_thread_online();
38
39 health_register(HEALTH_TYPE_HT_CLEANUP);
40
41 health_code_update();
42
43 ret = sessiond_set_thread_pollset(&events, 2);
44 if (ret < 0) {
45 goto error_poll_create;
46 }
47
48 /* Add pipe to the pollset. */
49 ret = lttng_poll_add(&events, ht_cleanup_pipe[0], LPOLLIN | LPOLLERR);
50 if (ret < 0) {
51 goto error;
52 }
53
54 health_code_update();
55
56 while (1) {
57 DBG3("[ht-thread] Polling on %d fds.",
58 LTTNG_POLL_GETNB(&events));
59
60 /* Inifinite blocking call, waiting for transmission */
61restart:
62 health_poll_entry();
63 ret = lttng_poll_wait(&events, -1);
64 health_poll_exit();
65 if (ret < 0) {
66 /*
67 * Restart interrupted system call.
68 */
69 if (errno == EINTR) {
70 goto restart;
71 }
72 goto error;
73 }
74
75 nb_fd = ret;
76
77 for (i = 0; i < nb_fd; i++) {
78 struct lttng_ht *ht;
79
80 health_code_update();
81
82 /* Fetch once the poll data */
83 revents = LTTNG_POLL_GETEV(&events, i);
84 pollfd = LTTNG_POLL_GETFD(&events, i);
85
86 /* Thread quit pipe has been closed. Killing thread. */
87 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
88 if (ret) {
89 err = 0;
90 goto exit;
91 }
92 assert(pollfd == ht_cleanup_pipe[0]);
93
94 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
95 ERR("ht cleanup pipe error");
96 goto error;
97 } else if (!(revents & LPOLLIN)) {
98 /* No POLLIN and not a catched error, stop the thread. */
99 ERR("ht cleanup failed. revent: %u", revents);
100 goto error;
101 }
102
103 do {
104 /* Get socket from dispatch thread. */
105 ret = read(ht_cleanup_pipe[0], &ht, sizeof(ht));
106 } while (ret < 0 && errno == EINTR);
107 if (ret < 0 || ret < sizeof(ht)) {
108 PERROR("ht cleanup notify pipe");
109 goto error;
110 }
111 health_code_update();
112 /*
113 * The whole point of this thread is to call
114 * lttng_ht_destroy from a context that is NOT:
115 * 1) a read-side RCU lock,
116 * 2) a call_rcu thread.
117 */
118 lttng_ht_destroy(ht);
119
120 health_code_update();
121 }
122 }
123
124exit:
125error:
126 lttng_poll_clean(&events);
127error_poll_create:
128 utils_close_pipe(ht_cleanup_pipe);
129 ht_cleanup_pipe[0] = ht_cleanup_pipe[1] = -1;
130 DBG("[ust-thread] cleanup complete.");
131 if (err) {
132 health_error();
133 ERR("Health error occurred in %s", __func__);
134 }
135 health_unregister();
136 rcu_thread_offline();
137 rcu_unregister_thread();
138 return NULL;
139}
This page took 0.027237 seconds and 4 git commands to generate.