From fc05e5e3d7bcd59a64eabb8b94ee7af4d0bcc4d5 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 17 Feb 2026 22:39:56 +0900 Subject: [PATCH 1/3] gh-141510: Update mp_length of frozendict to use non atomic operation (gh-144913) --- Objects/dictobject.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 46b0148cf59ab5..510a0fab468cc6 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -3482,6 +3482,12 @@ dict_length(PyObject *self) return GET_USED(_PyAnyDict_CAST(self)); } +static Py_ssize_t +frozendict_length(PyObject *self) +{ + return _PyAnyDict_CAST(self)->ma_used; +} + static PyObject * dict_subscript(PyObject *self, PyObject *key) { @@ -7833,7 +7839,7 @@ static PyNumberMethods frozendict_as_number = { }; static PyMappingMethods frozendict_as_mapping = { - .mp_length = dict_length, + .mp_length = frozendict_length, .mp_subscript = dict_subscript, }; From 1bd8cf9ed26977ac8c5cf3ec0eccdb6e7a3798e4 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 17 Feb 2026 23:52:50 +0900 Subject: [PATCH 2/3] gh-141510: Remove unncessary lock holding for frozendict repr (gh-144920) --- Objects/dictobject.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 510a0fab468cc6..62abb793d002e0 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -3381,19 +3381,18 @@ dict_dealloc(PyObject *self) static PyObject * -dict_repr_lock_held(PyObject *self) +anydict_repr_impl(PyObject *self) { PyDictObject *mp = (PyDictObject *)self; PyObject *key = NULL, *value = NULL; - ASSERT_DICT_LOCKED(mp); - int res = Py_ReprEnter((PyObject *)mp); + int res = Py_ReprEnter(self); if (res != 0) { return (res > 0 ? PyUnicode_FromString("{...}") : NULL); } if (mp->ma_used == 0) { - Py_ReprLeave((PyObject *)mp); + Py_ReprLeave(self); return PyUnicode_FromString("{}"); } @@ -3412,7 +3411,7 @@ dict_repr_lock_held(PyObject *self) Note that repr may mutate the dict. */ Py_ssize_t i = 0; int first = 1; - while (_PyDict_Next((PyObject *)mp, &i, &key, &value, NULL)) { + while (_PyDict_Next(self, &i, &key, &value, NULL)) { // Prevent repr from deleting key or value during key format. Py_INCREF(key); Py_INCREF(value); @@ -3454,18 +3453,25 @@ dict_repr_lock_held(PyObject *self) goto error; } - Py_ReprLeave((PyObject *)mp); + Py_ReprLeave(self); return PyUnicodeWriter_Finish(writer); error: - Py_ReprLeave((PyObject *)mp); + Py_ReprLeave(self); PyUnicodeWriter_Discard(writer); Py_XDECREF(key); Py_XDECREF(value); return NULL; } +static PyObject * +dict_repr_lock_held(PyObject *self) +{ + ASSERT_DICT_LOCKED((PyDictObject *)self); + return anydict_repr_impl(self); +} + static PyObject * dict_repr(PyObject *self) { @@ -7862,7 +7868,7 @@ static PyMethodDef frozendict_methods[] = { static PyObject * frozendict_repr(PyObject *self) { - PyObject *repr = dict_repr(self); + PyObject *repr = anydict_repr_impl(self); if (repr == NULL) { return NULL; } From 6577d870b0cb82baf540f4bcf49c01d68204e468 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Tue, 17 Feb 2026 09:12:25 -0700 Subject: [PATCH 3/3] gh-144438: Fix false sharing between QSBR and tlbc_index (gh-144554) Align the QSBR thread state array to a 64-byte cache line boundary and add padding at the end of _PyThreadStateImpl. Depending on heap layout, the QSBR array could end up sharing a cache line with a thread's tlbc_index, causing QSBR quiescent state updates to contend with reads of tlbc_index in RESUME_CHECK. This is sensitive to earlier allocations during interpreter init and can appear or disappear with seemingly unrelated changes. Either change alone is sufficient to fix the specific issue, but both are worthwhile to avoid similar problems in the future. --- Include/internal/pycore_qsbr.h | 3 ++- Include/internal/pycore_tstate.h | 6 ++++++ ...2-06-21-45-52.gh-issue-144438.GI_uB1LR.rst | 2 ++ Python/qsbr.c | 20 +++++++++++++------ 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-02-06-21-45-52.gh-issue-144438.GI_uB1LR.rst diff --git a/Include/internal/pycore_qsbr.h b/Include/internal/pycore_qsbr.h index 1f9b3fcf777493..eeca6fc472be37 100644 --- a/Include/internal/pycore_qsbr.h +++ b/Include/internal/pycore_qsbr.h @@ -83,8 +83,9 @@ struct _qsbr_shared { // Minimum observed read sequence of all QSBR thread states uint64_t rd_seq; - // Array of QSBR thread states. + // Array of QSBR thread states (aligned to 64 bytes). struct _qsbr_pad *array; + void *array_raw; // raw allocation pointer (for free) Py_ssize_t size; // Freelist of unused _qsbr_thread_states (protected by mutex) diff --git a/Include/internal/pycore_tstate.h b/Include/internal/pycore_tstate.h index 64b90710b8e664..eb2b0c84acdc7c 100644 --- a/Include/internal/pycore_tstate.h +++ b/Include/internal/pycore_tstate.h @@ -102,6 +102,12 @@ typedef struct _PyThreadStateImpl { #if _Py_TIER2 struct _PyJitTracerState *jit_tracer_state; #endif + +#ifdef Py_GIL_DISABLED + // gh-144438: Add padding to ensure that the fields above don't share a + // cache line with other allocations. + char __padding[64]; +#endif } _PyThreadStateImpl; #ifdef __cplusplus diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-06-21-45-52.gh-issue-144438.GI_uB1LR.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-06-21-45-52.gh-issue-144438.GI_uB1LR.rst new file mode 100644 index 00000000000000..3e33e461ae8b5a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-06-21-45-52.gh-issue-144438.GI_uB1LR.rst @@ -0,0 +1,2 @@ +Align the QSBR thread state array to a 64-byte cache line boundary to +avoid false sharing in the :term:`free-threaded build`. diff --git a/Python/qsbr.c b/Python/qsbr.c index 6bf5b75f346690..e9d935bfb40d84 100644 --- a/Python/qsbr.c +++ b/Python/qsbr.c @@ -85,22 +85,29 @@ grow_thread_array(struct _qsbr_shared *shared) new_size = MIN_ARRAY_SIZE; } - struct _qsbr_pad *array = PyMem_RawCalloc(new_size, sizeof(*array)); - if (array == NULL) { + // Overallocate by 63 bytes so we can align to a 64-byte boundary. + // This avoids potential false sharing between the first entry and other + // allocations. + size_t alignment = 64; + size_t alloc_size = (size_t)new_size * sizeof(struct _qsbr_pad) + alignment - 1; + void *raw = PyMem_RawCalloc(1, alloc_size); + if (raw == NULL) { return -1; } + struct _qsbr_pad *array = _Py_ALIGN_UP(raw, alignment); - struct _qsbr_pad *old = shared->array; - if (old != NULL) { + void *old_raw = shared->array_raw; + if (shared->array != NULL) { memcpy(array, shared->array, shared->size * sizeof(*array)); } shared->array = array; + shared->array_raw = raw; shared->size = new_size; shared->freelist = NULL; initialize_new_array(shared); - PyMem_RawFree(old); + PyMem_RawFree(old_raw); return 0; } @@ -257,8 +264,9 @@ void _Py_qsbr_fini(PyInterpreterState *interp) { struct _qsbr_shared *shared = &interp->qsbr; - PyMem_RawFree(shared->array); + PyMem_RawFree(shared->array_raw); shared->array = NULL; + shared->array_raw = NULL; shared->size = 0; shared->freelist = NULL; }