Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/util/thread-context.c
Line
Count
Source
1
/*
2
 * QEMU Thread Context
3
 *
4
 * Copyright Red Hat Inc., 2022
5
 *
6
 * Authors:
7
 *  David Hildenbrand <david@redhat.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10
 * See the COPYING file in the top-level directory.
11
 */
12
13
#include "qemu/osdep.h"
14
#include "qemu/thread-context.h"
15
#include "qapi/error.h"
16
#include "qapi/qapi-builtin-type-infos.h"
17
#include "qapi/qapi-builtin-visit.h"
18
#include "qapi/visitor.h"
19
#include "qemu/config-file.h"
20
#include "qapi/qapi-builtin-visit.h"
21
#include "qom/object_interfaces.h"
22
#include "qemu/module.h"
23
#include "qemu/bitmap.h"
24
25
#ifdef CONFIG_NUMA
26
#include <numa.h>
27
#endif
28
29
enum {
30
    TC_CMD_NONE = 0,
31
    TC_CMD_STOP,
32
    TC_CMD_NEW,
33
};
34
35
typedef struct ThreadContextCmdNew {
36
    QemuThread *thread;
37
    const char *name;
38
    void *(*start_routine)(void *);
39
    void *arg;
40
    int mode;
41
} ThreadContextCmdNew;
42
43
static void *thread_context_run(void *opaque)
44
0
{
45
0
    ThreadContext *tc = opaque;
46
47
0
    tc->thread_id = qemu_get_thread_id();
48
0
    qemu_sem_post(&tc->sem);
49
50
0
    while (true) {
51
        /*
52
         * Threads inherit the CPU affinity of the creating thread. For this
53
         * reason, we create new (especially short-lived) threads from our
54
         * persistent context thread.
55
         *
56
         * Especially when QEMU is not allowed to set the affinity itself,
57
         * management tools can simply set the affinity of the context thread
58
         * after creating the context, to have new threads created via
59
         * the context inherit the CPU affinity automatically.
60
         */
61
0
        switch (tc->thread_cmd) {
62
0
        case TC_CMD_NONE:
63
0
            break;
64
0
        case TC_CMD_STOP:
65
0
            tc->thread_cmd = TC_CMD_NONE;
66
0
            qemu_sem_post(&tc->sem);
67
0
            return NULL;
68
0
        case TC_CMD_NEW: {
69
0
            ThreadContextCmdNew *cmd_new = tc->thread_cmd_data;
70
71
0
            qemu_thread_create(cmd_new->thread, cmd_new->name,
72
0
                               cmd_new->start_routine, cmd_new->arg,
73
0
                               cmd_new->mode);
74
0
            tc->thread_cmd = TC_CMD_NONE;
75
0
            tc->thread_cmd_data = NULL;
76
0
            qemu_sem_post(&tc->sem);
77
0
            break;
78
0
        }
79
0
        default:
80
0
            g_assert_not_reached();
81
0
        }
82
0
        qemu_sem_wait(&tc->sem_thread);
83
0
    }
84
0
}
85
86
static void thread_context_set_cpu_affinity(Object *obj, Visitor *v,
87
                                            const char *name, void *opaque,
88
                                            Error **errp)
89
0
{
90
0
    ThreadContext *tc = THREAD_CONTEXT(obj);
91
0
    uint16List *l, *host_cpus = NULL;
92
0
    unsigned long *bitmap = NULL;
93
0
    int nbits = 0, ret;
94
95
0
    if (tc->init_cpu_bitmap) {
96
0
        error_setg(errp, "Mixing CPU and node affinity not supported");
97
0
        return;
98
0
    }
99
100
0
    if (!visit_type_uint16List(v, name, &host_cpus, errp)) {
101
0
        return;
102
0
    }
103
104
0
    if (!host_cpus) {
105
0
        error_setg(errp, "CPU list is empty");
106
0
        goto out;
107
0
    }
108
109
0
    for (l = host_cpus; l; l = l->next) {
110
0
        nbits = MAX(nbits, l->value + 1);
111
0
    }
112
0
    bitmap = bitmap_new(nbits);
113
0
    for (l = host_cpus; l; l = l->next) {
114
0
        set_bit(l->value, bitmap);
115
0
    }
116
117
0
    if (tc->thread_id != -1) {
118
        /*
119
         * Note: we won't be adjusting the affinity of any thread that is still
120
         * around, but only the affinity of the context thread.
121
         */
122
0
        ret = qemu_thread_set_affinity(&tc->thread, bitmap, nbits);
123
0
        if (ret) {
124
0
            error_setg(errp, "Setting CPU affinity failed: %s", strerror(ret));
125
0
        }
126
0
    } else {
127
0
        tc->init_cpu_bitmap = bitmap;
128
0
        bitmap = NULL;
129
0
        tc->init_cpu_nbits = nbits;
130
0
    }
131
0
out:
132
0
    g_free(bitmap);
133
0
    qapi_free_uint16List(host_cpus);
134
0
}
135
136
static void thread_context_get_cpu_affinity(Object *obj, Visitor *v,
137
                                            const char *name, void *opaque,
138
                                            Error **errp)
139
0
{
140
0
    unsigned long *bitmap, nbits, value;
141
0
    ThreadContext *tc = THREAD_CONTEXT(obj);
142
0
    uint16List *host_cpus = NULL;
143
0
    uint16List **tail = &host_cpus;
144
0
    int ret;
145
146
0
    if (tc->thread_id == -1) {
147
0
        error_setg(errp, "Object not initialized yet");
148
0
        return;
149
0
    }
150
151
0
    ret = qemu_thread_get_affinity(&tc->thread, &bitmap, &nbits);
152
0
    if (ret) {
153
0
        error_setg(errp, "Getting CPU affinity failed: %s", strerror(ret));
154
0
        return;
155
0
    }
156
157
0
    value = find_first_bit(bitmap, nbits);
158
0
    while (value < nbits) {
159
0
        QAPI_LIST_APPEND(tail, value);
160
161
0
        value = find_next_bit(bitmap, nbits, value + 1);
162
0
    }
163
0
    g_free(bitmap);
164
165
0
    visit_type_uint16List(v, name, &host_cpus, errp);
166
0
    qapi_free_uint16List(host_cpus);
167
0
}
168
169
static void thread_context_set_node_affinity(Object *obj, Visitor *v,
170
                                             const char *name, void *opaque,
171
                                             Error **errp)
172
0
{
173
#ifdef CONFIG_NUMA
174
    const int nbits = numa_num_possible_cpus();
175
    ThreadContext *tc = THREAD_CONTEXT(obj);
176
    uint16List *l, *host_nodes = NULL;
177
    unsigned long *bitmap = NULL;
178
    struct bitmask *tmp_cpus;
179
    int ret, i;
180
181
    if (tc->init_cpu_bitmap) {
182
        error_setg(errp, "Mixing CPU and node affinity not supported");
183
        return;
184
    }
185
186
    if (!visit_type_uint16List(v, name, &host_nodes, errp)) {
187
        return;
188
    }
189
190
    if (!host_nodes) {
191
        error_setg(errp, "Node list is empty");
192
        goto out;
193
    }
194
195
    bitmap = bitmap_new(nbits);
196
    tmp_cpus = numa_allocate_cpumask();
197
    for (l = host_nodes; l; l = l->next) {
198
        numa_bitmask_clearall(tmp_cpus);
199
        ret = numa_node_to_cpus(l->value, tmp_cpus);
200
        if (ret) {
201
            /* We ignore any errors, such as impossible nodes. */
202
            continue;
203
        }
204
        for (i = 0; i < nbits; i++) {
205
            if (numa_bitmask_isbitset(tmp_cpus, i)) {
206
                set_bit(i, bitmap);
207
            }
208
        }
209
    }
210
    numa_free_cpumask(tmp_cpus);
211
212
    if (bitmap_empty(bitmap, nbits)) {
213
        error_setg(errp, "The nodes select no CPUs");
214
        goto out;
215
    }
216
217
    if (tc->thread_id != -1) {
218
        /*
219
         * Note: we won't be adjusting the affinity of any thread that is still
220
         * around for now, but only the affinity of the context thread.
221
         */
222
        ret = qemu_thread_set_affinity(&tc->thread, bitmap, nbits);
223
        if (ret) {
224
            error_setg(errp, "Setting CPU affinity failed: %s", strerror(ret));
225
        }
226
    } else {
227
        tc->init_cpu_bitmap = bitmap;
228
        bitmap = NULL;
229
        tc->init_cpu_nbits = nbits;
230
    }
231
out:
232
    g_free(bitmap);
233
    qapi_free_uint16List(host_nodes);
234
#else
235
0
    error_setg(errp, "NUMA node affinity is not supported by this QEMU");
236
0
#endif
237
0
}
238
239
static void thread_context_get_thread_id(Object *obj, Visitor *v,
240
                                         const char *name, void *opaque,
241
                                         Error **errp)
242
0
{
243
0
    ThreadContext *tc = THREAD_CONTEXT(obj);
244
0
    uint64_t value = tc->thread_id;
245
246
0
    visit_type_uint64(v, name, &value, errp);
247
0
}
248
249
static void thread_context_instance_complete(UserCreatable *uc, Error **errp)
250
0
{
251
0
    ThreadContext *tc = THREAD_CONTEXT(uc);
252
0
    char *thread_name;
253
0
    int ret;
254
255
0
    thread_name = g_strdup_printf("TC %s",
256
0
                               object_get_canonical_path_component(OBJECT(uc)));
257
0
    qemu_thread_create(&tc->thread, thread_name, thread_context_run, tc,
258
0
                       QEMU_THREAD_JOINABLE);
259
0
    g_free(thread_name);
260
261
    /* Wait until initialization of the thread is done. */
262
0
    while (tc->thread_id == -1) {
263
0
        qemu_sem_wait(&tc->sem);
264
0
    }
265
266
0
    if (tc->init_cpu_bitmap) {
267
0
        ret = qemu_thread_set_affinity(&tc->thread, tc->init_cpu_bitmap,
268
0
                                       tc->init_cpu_nbits);
269
0
        if (ret) {
270
0
            error_setg(errp, "Setting CPU affinity failed: %s", strerror(ret));
271
0
        }
272
0
        g_free(tc->init_cpu_bitmap);
273
0
        tc->init_cpu_bitmap = NULL;
274
0
    }
275
0
}
276
277
static void thread_context_class_init(ObjectClass *oc, const void *data)
278
63
{
279
63
    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
280
281
63
    ucc->complete = thread_context_instance_complete;
282
63
    object_class_property_add(oc, "thread-id", "uint64",
283
63
                              &uint64_type_info,
284
63
                              thread_context_get_thread_id, NULL, NULL,
285
63
                              NULL);
286
63
    object_class_property_add(oc, "cpu-affinity", "[uint16]",
287
63
                              &uint16List_type_info,
288
63
                              thread_context_get_cpu_affinity,
289
63
                              thread_context_set_cpu_affinity, NULL, NULL);
290
63
    object_class_property_add(oc, "node-affinity", "[uint16]",
291
63
                              &uint16List_type_info, NULL,
292
63
                              thread_context_set_node_affinity, NULL, NULL);
293
63
}
294
295
static void thread_context_instance_init(Object *obj)
296
0
{
297
0
    ThreadContext *tc = THREAD_CONTEXT(obj);
298
299
0
    tc->thread_id = -1;
300
0
    qemu_sem_init(&tc->sem, 0);
301
0
    qemu_sem_init(&tc->sem_thread, 0);
302
0
    qemu_mutex_init(&tc->mutex);
303
0
}
304
305
static void thread_context_instance_finalize(Object *obj)
306
0
{
307
0
    ThreadContext *tc = THREAD_CONTEXT(obj);
308
309
0
    if (tc->thread_id != -1) {
310
0
        tc->thread_cmd = TC_CMD_STOP;
311
0
        qemu_sem_post(&tc->sem_thread);
312
0
        qemu_thread_join(&tc->thread);
313
0
    }
314
0
    qemu_sem_destroy(&tc->sem);
315
0
    qemu_sem_destroy(&tc->sem_thread);
316
0
    qemu_mutex_destroy(&tc->mutex);
317
0
}
318
319
static const TypeInfo thread_context_info = {
320
    .name = TYPE_THREAD_CONTEXT,
321
    .parent = TYPE_OBJECT,
322
    .class_init = thread_context_class_init,
323
    .instance_size = sizeof(ThreadContext),
324
    .instance_init = thread_context_instance_init,
325
    .instance_finalize = thread_context_instance_finalize,
326
    .interfaces = (const InterfaceInfo[]) {
327
        { TYPE_USER_CREATABLE },
328
        { }
329
    }
330
};
331
332
static void thread_context_register_types(void)
333
63
{
334
63
    type_register_static(&thread_context_info);
335
63
}
336
type_init(thread_context_register_types)
337
338
void thread_context_create_thread(ThreadContext *tc, QemuThread *thread,
339
                                  const char *name,
340
                                  void *(*start_routine)(void *), void *arg,
341
                                  int mode)
342
0
{
343
0
    ThreadContextCmdNew data = {
344
0
        .thread = thread,
345
0
        .name = name,
346
0
        .start_routine = start_routine,
347
0
        .arg = arg,
348
0
        .mode = mode,
349
0
    };
350
351
0
    qemu_mutex_lock(&tc->mutex);
352
0
    tc->thread_cmd = TC_CMD_NEW;
353
0
    tc->thread_cmd_data = &data;
354
0
    qemu_sem_post(&tc->sem_thread);
355
356
0
    while (tc->thread_cmd != TC_CMD_NONE) {
357
0
        qemu_sem_wait(&tc->sem);
358
0
    }
359
0
    qemu_mutex_unlock(&tc->mutex);
360
0
}