From 64a87eb641fb158e16698e488242d844e2a7811b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Tue, 30 Jul 2024 20:20:09 +0000 Subject: [PATCH] sessiond: dispatch.cpp: iterate on list using list_iteration_adapter MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: Ie8a45753922b0a5dd476be06ce15a1f7d6883c08 Signed-off-by: Jérémie Galarneau --- src/bin/lttng-sessiond/dispatch.cpp | 48 ++++++++++++++++------------- src/common/urcu.hpp | 6 ++-- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/bin/lttng-sessiond/dispatch.cpp b/src/bin/lttng-sessiond/dispatch.cpp index 32d50a25e..058582ec3 100644 --- a/src/bin/lttng-sessiond/dispatch.cpp +++ b/src/bin/lttng-sessiond/dispatch.cpp @@ -38,7 +38,6 @@ struct thread_notifiers { */ static void update_ust_app(int app_sock) { - struct ltt_session *sess, *stmp; const struct ltt_session_list *session_list = session_get_list(); struct ust_app *app; @@ -65,7 +64,8 @@ static void update_ust_app(int app_sock) ust_app_global_update_event_notifier_rules(app); /* For all tracing session(s) */ - cds_list_for_each_entry_safe (sess, stmp, &session_list->head, list) { + for (auto *sess : lttng::urcu::list_iteration_adapter( + session_list->head)) { if (!session_get(sess)) { continue; } @@ -91,7 +91,6 @@ static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue) int ret, nb_fd = 0, i; unsigned int fd_added = 0; struct lttng_poll_event events; - struct ust_reg_wait_node *wait_node = nullptr, *tmp_wait_node; LTTNG_ASSERT(wait_queue); @@ -107,7 +106,9 @@ static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue) goto error_create; } - cds_list_for_each_entry_safe (wait_node, tmp_wait_node, &wait_queue->head, head) { + for (auto *wait_node : + lttng::urcu::list_iteration_adapter( + wait_queue->head)) { LTTNG_ASSERT(wait_node->app); ret = lttng_poll_add(&events, wait_node->app->sock, LPOLLIN); if (ret < 0) { @@ -136,18 +137,16 @@ static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue) const uint32_t revents = LTTNG_POLL_GETEV(&events, i); const int pollfd = LTTNG_POLL_GETFD(&events, i); - cds_list_for_each_entry_safe (wait_node, tmp_wait_node, &wait_queue->head, head) { + for (auto *wait_node : + lttng::urcu::list_iteration_adapter( + wait_queue->head)) { if (pollfd == wait_node->app->sock && (revents & (LPOLLHUP | LPOLLERR))) { cds_list_del(&wait_node->head); wait_queue->count--; ust_app_put(wait_node->app); free(wait_node); - /* - * Silence warning of use-after-free in - * cds_list_for_each_entry_safe which uses - * __typeof__(*wait_node). - */ wait_node = nullptr; break; } else { @@ -224,7 +223,7 @@ static void *thread_dispatch_ust_registration(void *data) int ret, err = -1; struct cds_wfcq_node *node; struct ust_command *ust_cmd = nullptr; - struct ust_reg_wait_node *wait_node = nullptr, *tmp_wait_node; + struct ust_reg_wait_node *wait_node = nullptr; struct ust_reg_wait_queue wait_queue = { .count = 0, .head = {}, @@ -338,16 +337,19 @@ static void *thread_dispatch_ust_registration(void *data) * Look for the application in the local wait queue and set the * notify socket if found. */ - cds_list_for_each_entry_safe ( - wait_node, tmp_wait_node, &wait_queue.head, head) { + for (auto *wait_node_in_queue : + lttng::urcu::list_iteration_adapter( + wait_queue.head)) { health_code_update(); - if (wait_node->app->pid == ust_cmd->reg_msg.pid) { - wait_node->app->notify_sock = ust_cmd->sock; - cds_list_del(&wait_node->head); + if (wait_node_in_queue->app->pid == ust_cmd->reg_msg.pid) { + wait_node_in_queue->app->notify_sock = + ust_cmd->sock; + cds_list_del(&wait_node_in_queue->head); wait_queue.count--; - app = wait_node->app; - free(wait_node); - wait_node = nullptr; + app = wait_node_in_queue->app; + free(wait_node_in_queue); + DBG3("UST app notify socket %d is set", ust_cmd->sock); break; @@ -447,10 +449,12 @@ static void *thread_dispatch_ust_registration(void *data) error: /* Clean up wait queue. */ - cds_list_for_each_entry_safe (wait_node, tmp_wait_node, &wait_queue.head, head) { - cds_list_del(&wait_node->head); + for (auto *wait_node_in_queue : + lttng::urcu::list_iteration_adapter( + wait_queue.head)) { + cds_list_del(&wait_node_in_queue->head); wait_queue.count--; - free(wait_node); + free(wait_node_in_queue); } /* Empty command queue. */ diff --git a/src/common/urcu.hpp b/src/common/urcu.hpp index 6466fac43..c5b4c13c0 100644 --- a/src/common/urcu.hpp +++ b/src/common/urcu.hpp @@ -345,7 +345,7 @@ public: cds_list_head _node_contents; }; - explicit list_iteration_adapter(cds_list_head& list) : _list(list) + explicit list_iteration_adapter(const cds_list_head& list) : _list(list) { } @@ -363,7 +363,7 @@ public: protected: /* Reference to the list being iterated over. */ - cds_list_head& _list; + const cds_list_head& _list; }; template @@ -396,7 +396,7 @@ public: } }; - explicit rcu_list_iteration_adapter(cds_list_head& list) : + explicit rcu_list_iteration_adapter(const cds_list_head& list) : list_iteration_adapter(list) { } -- 2.34.1