From 657033b3db93b4f793d8d837346e07d1a6805f53 Mon Sep 17 00:00:00 2001 From: wuminghui03 Date: Tue, 28 Apr 2026 14:46:20 +0800 Subject: [PATCH] Fix TLS variable access issue under PGO optimization In task_group, there is the following call chain where ready_to_run_in_worker is indirectly called: `task_runner -> ending_sched -> sched_to -*-> ready_to_run_in_worker` With PGO optimization is enabled, these functions may be inlined into `task_runner`, which will cause TLS access error. --- src/bthread/task_group.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bthread/task_group.cpp b/src/bthread/task_group.cpp index 579bb23120..58b6646b7b 100644 --- a/src/bthread/task_group.cpp +++ b/src/bthread/task_group.cpp @@ -341,7 +341,7 @@ void TaskGroup::asan_task_runner(intptr_t) { void TaskGroup::task_runner(intptr_t skip_remained) { // NOTE: tls_task_group is volatile since tasks are moved around // different groups. - TaskGroup* g = tls_task_group; + TaskGroup* g = BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group); #ifdef BRPC_BTHREAD_TRACER TaskTracer::set_running_status(g->tid(), g->_cur_meta); #endif // BRPC_BTHREAD_TRACER @@ -732,7 +732,7 @@ void TaskGroup::sched_to(TaskGroup** pg, TaskMeta* next_meta) { #endif // Save errno so that errno is bthread-specific. int saved_errno = errno; - void* saved_unique_user_ptr = tls_unique_user_ptr; + void* saved_unique_user_ptr = BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_unique_user_ptr); TaskMeta* const cur_meta = g->_cur_meta; int64_t now = butil::cpuwide_time_ns(); @@ -910,7 +910,7 @@ void TaskGroup::flush_nosignal_tasks_general() { void TaskGroup::ready_to_run_in_worker(void* args_in) { ReadyToRunArgs* args = static_cast(args_in); - return tls_task_group->ready_to_run(args->meta, args->nosignal); + return BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group)->ready_to_run(args->meta, args->nosignal); } void TaskGroup::ready_to_run_in_worker_ignoresignal(void* args_in) { @@ -919,7 +919,7 @@ void TaskGroup::ready_to_run_in_worker_ignoresignal(void* args_in) { tls_task_group->_control->_task_tracer.set_status( TASK_STATUS_READY, args->meta); #endif // BRPC_BTHREAD_TRACER - return tls_task_group->push_rq(args->meta->tid); + return BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group)->push_rq(args->meta->tid); } void TaskGroup::priority_to_run(void* args_in) {