Coverage Report

Created: 2025-11-24 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/qsbr.c
Line
Count
Source
1
/*
2
 * Implementation of safe memory reclamation scheme using
3
 * quiescent states.  See InternalDocs/qsbr.md.
4
 *
5
 * This is derived from the "GUS" safe memory reclamation technique
6
 * in FreeBSD written by Jeffrey Roberson. It is heavily modified. Any bugs
7
 * in this code are likely due to the modifications.
8
 *
9
 * The original copyright is preserved below.
10
 *
11
 * Copyright (c) 2019,2020 Jeffrey Roberson <jeff@FreeBSD.org>
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions
15
 * are met:
16
 * 1. Redistributions of source code must retain the above copyright
17
 *    notice unmodified, this list of conditions, and the following
18
 *    disclaimer.
19
 * 2. Redistributions in binary form must reproduce the above copyright
20
 *    notice, this list of conditions and the following disclaimer in the
21
 *    documentation and/or other materials provided with the distribution.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
 */
34
#include "Python.h"
35
#include "pycore_interp.h"          // PyInterpreterState
36
#include "pycore_pystate.h"         // _PyThreadState_GET()
37
#include "pycore_qsbr.h"
38
#include "pycore_tstate.h"          // _PyThreadStateImpl
39
#include "pycore_stats.h"           // FT_STAT_QSBR_POLL_INC()
40
41
42
// Starting size of the array of qsbr thread states
43
0
#define MIN_ARRAY_SIZE 8
44
45
// Allocate a QSBR thread state from the freelist
46
static struct _qsbr_thread_state *
47
qsbr_allocate(struct _qsbr_shared *shared)
48
0
{
49
0
    struct _qsbr_thread_state *qsbr = shared->freelist;
50
0
    if (qsbr == NULL) {
51
0
        return NULL;
52
0
    }
53
0
    shared->freelist = qsbr->freelist_next;
54
0
    qsbr->freelist_next = NULL;
55
0
    qsbr->shared = shared;
56
0
    qsbr->allocated = true;
57
0
    return qsbr;
58
0
}
59
60
// Initialize (or reintialize) the freelist of QSBR thread states
61
static void
62
initialize_new_array(struct _qsbr_shared *shared)
63
0
{
64
0
    for (Py_ssize_t i = 0; i != shared->size; i++) {
65
0
        struct _qsbr_thread_state *qsbr = &shared->array[i].qsbr;
66
0
        if (qsbr->tstate != NULL) {
67
            // Update the thread state pointer to its QSBR state
68
0
            _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)qsbr->tstate;
69
0
            tstate->qsbr = qsbr;
70
0
        }
71
0
        if (!qsbr->allocated) {
72
            // Push to freelist
73
0
            qsbr->freelist_next = shared->freelist;
74
0
            shared->freelist = qsbr;
75
0
        }
76
0
    }
77
0
}
78
79
// Grow the array of QSBR thread states. Returns 0 on success, -1 on failure.
80
static int
81
grow_thread_array(struct _qsbr_shared *shared)
82
0
{
83
0
    Py_ssize_t new_size = shared->size * 2;
84
0
    if (new_size < MIN_ARRAY_SIZE) {
85
0
        new_size = MIN_ARRAY_SIZE;
86
0
    }
87
88
0
    struct _qsbr_pad *array = PyMem_RawCalloc(new_size, sizeof(*array));
89
0
    if (array == NULL) {
90
0
        return -1;
91
0
    }
92
93
0
    struct _qsbr_pad *old = shared->array;
94
0
    if (old != NULL) {
95
0
        memcpy(array, shared->array, shared->size * sizeof(*array));
96
0
    }
97
98
0
    shared->array = array;
99
0
    shared->size = new_size;
100
0
    shared->freelist = NULL;
101
0
    initialize_new_array(shared);
102
103
0
    PyMem_RawFree(old);
104
0
    return 0;
105
0
}
106
107
uint64_t
108
_Py_qsbr_advance(struct _qsbr_shared *shared)
109
0
{
110
    // NOTE: with 64-bit sequence numbers, we don't have to worry too much
111
    // about the wr_seq getting too far ahead of rd_seq, but if we ever use
112
    // 32-bit sequence numbers, we'll need to be more careful.
113
0
    return _Py_atomic_add_uint64(&shared->wr_seq, QSBR_INCR) + QSBR_INCR;
114
0
}
115
116
uint64_t
117
_Py_qsbr_shared_next(struct _qsbr_shared *shared)
118
0
{
119
0
    return _Py_qsbr_shared_current(shared) + QSBR_INCR;
120
0
}
121
122
static uint64_t
123
qsbr_poll_scan(struct _qsbr_shared *shared)
124
0
{
125
    // Synchronize with store in _Py_qsbr_attach(). We need to ensure that
126
    // the reads from each thread's sequence number are not reordered to see
127
    // earlier "offline" states.
128
0
    _Py_atomic_fence_seq_cst();
129
130
    // Compute the minimum sequence number of all attached threads
131
0
    uint64_t min_seq = _Py_atomic_load_uint64(&shared->wr_seq);
132
0
    struct _qsbr_pad *array = shared->array;
133
0
    for (Py_ssize_t i = 0, size = shared->size; i != size; i++) {
134
0
        struct _qsbr_thread_state *qsbr = &array[i].qsbr;
135
136
0
        uint64_t seq = _Py_atomic_load_uint64(&qsbr->seq);
137
0
        if (seq != QSBR_OFFLINE && QSBR_LT(seq, min_seq)) {
138
0
            min_seq = seq;
139
0
        }
140
0
    }
141
142
    // Update the shared read sequence
143
0
    uint64_t rd_seq = _Py_atomic_load_uint64(&shared->rd_seq);
144
0
    if (QSBR_LT(rd_seq, min_seq)) {
145
        // It's okay if the compare-exchange failed: another thread updated it
146
0
        (void)_Py_atomic_compare_exchange_uint64(&shared->rd_seq, &rd_seq, min_seq);
147
0
        rd_seq = min_seq;
148
0
    }
149
150
0
    return rd_seq;
151
0
}
152
153
bool
154
_Py_qsbr_poll(struct _qsbr_thread_state *qsbr, uint64_t goal)
155
0
{
156
0
    assert(_Py_atomic_load_int_relaxed(&_PyThreadState_GET()->state) == _Py_THREAD_ATTACHED);
157
0
    assert(((_PyThreadStateImpl *)_PyThreadState_GET())->qsbr == qsbr);
158
159
0
    if (_Py_qbsr_goal_reached(qsbr, goal)) {
160
0
        return true;
161
0
    }
162
0
    FT_STAT_QSBR_POLL_INC();
163
0
    uint64_t rd_seq = qsbr_poll_scan(qsbr->shared);
164
0
    return QSBR_LEQ(goal, rd_seq);
165
0
}
166
167
void
168
_Py_qsbr_attach(struct _qsbr_thread_state *qsbr)
169
0
{
170
0
    assert(qsbr->seq == 0 && "already attached");
171
172
0
    uint64_t seq = _Py_qsbr_shared_current(qsbr->shared);
173
0
    _Py_atomic_store_uint64(&qsbr->seq, seq);  // needs seq_cst
174
0
}
175
176
void
177
_Py_qsbr_detach(struct _qsbr_thread_state *qsbr)
178
0
{
179
0
    assert(qsbr->seq != 0 && "already detached");
180
181
0
    _Py_atomic_store_uint64_release(&qsbr->seq, QSBR_OFFLINE);
182
0
}
183
184
Py_ssize_t
185
_Py_qsbr_reserve(PyInterpreterState *interp)
186
0
{
187
0
    struct _qsbr_shared *shared = &interp->qsbr;
188
189
0
    PyMutex_Lock(&shared->mutex);
190
    // Try allocating from our internal freelist
191
0
    struct _qsbr_thread_state *qsbr = qsbr_allocate(shared);
192
193
    // If there are no free entries, we pause all threads, grow the array,
194
    // and update the pointers in PyThreadState to entries in the new array.
195
0
    if (qsbr == NULL) {
196
0
        _PyEval_StopTheWorld(interp);
197
0
        if (grow_thread_array(shared) == 0) {
198
0
            qsbr = qsbr_allocate(shared);
199
0
        }
200
0
        _PyEval_StartTheWorld(interp);
201
0
    }
202
203
    // Return an index rather than the pointer because the array may be
204
    // resized and the pointer invalidated.
205
0
    Py_ssize_t index = -1;
206
0
    if (qsbr != NULL) {
207
0
        index = (struct _qsbr_pad *)qsbr - shared->array;
208
0
    }
209
0
    PyMutex_Unlock(&shared->mutex);
210
0
    return index;
211
0
}
212
213
void
214
_Py_qsbr_register(_PyThreadStateImpl *tstate, PyInterpreterState *interp,
215
                  Py_ssize_t index)
216
0
{
217
    // Associate the QSBR state with the thread state
218
0
    struct _qsbr_shared *shared = &interp->qsbr;
219
220
0
    PyMutex_Lock(&shared->mutex);
221
0
    struct _qsbr_thread_state *qsbr = &interp->qsbr.array[index].qsbr;
222
0
    assert(qsbr->allocated && qsbr->tstate == NULL);
223
0
    qsbr->tstate = (PyThreadState *)tstate;
224
0
    tstate->qsbr = qsbr;
225
0
    PyMutex_Unlock(&shared->mutex);
226
0
}
227
228
void
229
_Py_qsbr_unregister(PyThreadState *tstate)
230
0
{
231
0
    struct _qsbr_shared *shared = &tstate->interp->qsbr;
232
0
    struct _PyThreadStateImpl *tstate_imp = (_PyThreadStateImpl*) tstate;
233
234
    // gh-119369: GIL must be released (if held) to prevent deadlocks, because
235
    // we might not have an active tstate, which means that blocking on PyMutex
236
    // locks will not implicitly release the GIL.
237
0
    assert(!tstate->holds_gil);
238
239
0
    PyMutex_Lock(&shared->mutex);
240
    // NOTE: we must load (or reload) the thread state's qbsr inside the mutex
241
    // because the array may have been resized (changing tstate->qsbr) while
242
    // we waited to acquire the mutex.
243
0
    struct _qsbr_thread_state *qsbr = tstate_imp->qsbr;
244
245
0
    assert(qsbr->seq == 0 && "thread state must be detached");
246
0
    assert(qsbr->allocated && qsbr->tstate == tstate);
247
248
0
    tstate_imp->qsbr = NULL;
249
0
    qsbr->tstate = NULL;
250
0
    qsbr->allocated = false;
251
0
    qsbr->freelist_next = shared->freelist;
252
0
    shared->freelist = qsbr;
253
0
    PyMutex_Unlock(&shared->mutex);
254
0
}
255
256
void
257
_Py_qsbr_fini(PyInterpreterState *interp)
258
0
{
259
0
    struct _qsbr_shared *shared = &interp->qsbr;
260
0
    PyMem_RawFree(shared->array);
261
0
    shared->array = NULL;
262
0
    shared->size = 0;
263
0
    shared->freelist = NULL;
264
0
}
265
266
void
267
_Py_qsbr_after_fork(_PyThreadStateImpl *tstate)
268
0
{
269
0
    struct _qsbr_thread_state *this_qsbr = tstate->qsbr;
270
0
    struct _qsbr_shared *shared = this_qsbr->shared;
271
272
0
    _PyMutex_at_fork_reinit(&shared->mutex);
273
274
0
    for (Py_ssize_t i = 0; i != shared->size; i++) {
275
0
        struct _qsbr_thread_state *qsbr = &shared->array[i].qsbr;
276
0
        if (qsbr != this_qsbr && qsbr->allocated) {
277
0
            qsbr->tstate = NULL;
278
            qsbr->allocated = false;
279
0
            qsbr->freelist_next = shared->freelist;
280
0
            shared->freelist = qsbr;
281
0
        }
282
0
    }
283
0
}