Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-117657: Fix data races when writing / reading ob_gc_bits #118292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use new locking discipline
  • Loading branch information
mpage committed May 6, 2024
commit 3f354ebabb8507ad9f179dc288e012947cd62c39
15 changes: 0 additions & 15 deletions 15 Include/cpython/pyatomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,6 @@ static inline uintptr_t
_Py_atomic_and_uintptr(uintptr_t *obj, uintptr_t value);


// --- _Py_atomic_and_relaxed ------------------------------------------------
// Performs `*obj &= value` atomically and returns the previous value of `*obj`
// (relaxed consistency).

static inline uint8_t
_Py_atomic_and_uint8_relaxed(uint8_t *obj, uint8_t value);


// --- _Py_atomic_or ---------------------------------------------------------
// Performs `*obj |= value` atomically and returns the previous value of `*obj`.

Expand All @@ -277,13 +269,6 @@ static inline uintptr_t
_Py_atomic_or_uintptr(uintptr_t *obj, uintptr_t value);


// --- _Py_atomic_or_relaxed -------------------------------------------------
// Performs `*obj |= value` atomically and returns the previous value of `*obj`
// (relaxed consistency).

static inline uint8_t
_Py_atomic_or_uint8_relaxed(uint8_t *obj, uint8_t value);

// --- _Py_atomic_load -------------------------------------------------------
// Atomically loads `*obj` (sequential consistency)

Expand Down
16 changes: 0 additions & 16 deletions 16 Include/cpython/pyatomic_gcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,6 @@ _Py_atomic_and_uintptr(uintptr_t *obj, uintptr_t value)
{ return __atomic_fetch_and(obj, value, __ATOMIC_SEQ_CST); }


// --- _Py_atomic_and_relaxed ------------------------------------------------

static inline uint8_t
_Py_atomic_and_uint8_relaxed(uint8_t *obj, uint8_t value)
{ return __atomic_fetch_and(obj, value, __ATOMIC_RELAXED); }


// --- _Py_atomic_or ---------------------------------------------------------

static inline uint8_t
Expand All @@ -248,15 +241,6 @@ _Py_atomic_or_uintptr(uintptr_t *obj, uintptr_t value)
{ return __atomic_fetch_or(obj, value, __ATOMIC_SEQ_CST); }


// --- _Py_atomic_or_relaxed -------------------------------------------------

static inline uint8_t
_Py_atomic_or_uint8_relaxed(uint8_t *obj, uint8_t value)
{
return __atomic_fetch_or(obj, value, __ATOMIC_RELAXED);
}


// --- _Py_atomic_load -------------------------------------------------------

static inline int
Expand Down
21 changes: 1 addition & 20 deletions 21 Include/cpython/pyatomic_msc.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,6 @@ _Py_atomic_and_uintptr(uintptr_t *obj, uintptr_t value)
}


// --- _Py_atomic_and_relaxed ------------------------------------------------

static inline uint8_t
_Py_atomic_and_uint8_relaxed(uint8_t *obj, uint8_t value)
{
_Py_atomic_ASSERT_ARG_TYPE(char);
return (uint8_t)_InterlockedAnd8NoFence((volatile char *)obj, (char)value);
}


// --- _Py_atomic_or ---------------------------------------------------------

static inline uint8_t
Expand Down Expand Up @@ -500,6 +490,7 @@ _Py_atomic_or_uint64(uint64_t *obj, uint64_t value)
#endif
}


static inline uintptr_t
_Py_atomic_or_uintptr(uintptr_t *obj, uintptr_t value)
{
Expand All @@ -515,16 +506,6 @@ _Py_atomic_or_uintptr(uintptr_t *obj, uintptr_t value)
}


// --- _Py_atomic_or_relaxed -------------------------------------------------

static inline uint8_t
_Py_atomic_or_uint8_relaxed(uint8_t *obj, uint8_t value)
{
_Py_atomic_ASSERT_ARG_TYPE(char);
return (uint8_t)_InterlockedOr8NoFence((volatile char *)obj, (char)value);
}


// --- _Py_atomic_load -------------------------------------------------------

static inline uint8_t
Expand Down
20 changes: 0 additions & 20 deletions 20 Include/cpython/pyatomic_std.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,6 @@ _Py_atomic_and_uintptr(uintptr_t *obj, uintptr_t value)
}


// --- _Py_atomic_and_relaxed ------------------------------------------------

static inline uint8_t
_Py_atomic_and_uint8_relaxed(uint8_t *obj, uint8_t value)
{
_Py_USING_STD;
return atomic_fetch_and_explicit((_Atomic(uint8_t)*)obj, value, memory_order_relaxed);
}


// --- _Py_atomic_or ---------------------------------------------------------

static inline uint8_t
Expand Down Expand Up @@ -414,16 +404,6 @@ _Py_atomic_or_uintptr(uintptr_t *obj, uintptr_t value)
}


// --- _Py_atomic_or_relaxed -------------------------------------------------

static inline uint8_t
_Py_atomic_or_uint8_relaxed(uint8_t *obj, uint8_t value)
{
_Py_USING_STD;
return atomic_fetch_or_explicit((_Atomic(uint8_t)*)obj, value, memory_order_relaxed);
}


// --- _Py_atomic_load -------------------------------------------------------

static inline int
Expand Down
20 changes: 15 additions & 5 deletions 20 Include/internal/pycore_gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ static inline PyObject* _Py_FROM_GC(PyGC_Head *gc) {
}


/* Bit flags for ob_gc_bits (in Py_GIL_DISABLED builds) */
/* Bit flags for ob_gc_bits (in Py_GIL_DISABLED builds)
*
* Setting the bits requires a relaxed store. The per-object lock must also be
* held, except when the object is only visible to a single thread (e.g. during
* object initialization or destruction).
*
* Reading the bits requires using a relaxed load, but does not require holding
* the per-object lock.
*/
#ifdef Py_GIL_DISABLED
# define _PyGC_BITS_TRACKED (1) // Tracked by the GC
# define _PyGC_BITS_FINALIZED (2) // tp_finalize was called
Expand All @@ -51,9 +59,10 @@ static inline PyObject* _Py_FROM_GC(PyGC_Head *gc) {
#ifdef Py_GIL_DISABLED

static inline void
_PyObject_SET_GC_BITS(PyObject *op, uint8_t bits)
_PyObject_SET_GC_BITS(PyObject *op, uint8_t new_bits)
{
_Py_atomic_or_uint8_relaxed(&op->ob_gc_bits, bits);
uint8_t bits = _Py_atomic_load_uint8_relaxed(&op->ob_gc_bits);
_Py_atomic_store_uint8_relaxed(&op->ob_gc_bits, bits | new_bits);
}

static inline int
Expand All @@ -63,9 +72,10 @@ _PyObject_HAS_GC_BITS(PyObject *op, uint8_t bits)
}

static inline void
_PyObject_CLEAR_GC_BITS(PyObject *op, uint8_t bits)
_PyObject_CLEAR_GC_BITS(PyObject *op, uint8_t bits_to_clear)
{
_Py_atomic_and_uint8_relaxed(&op->ob_gc_bits, ~bits);
uint8_t bits = _Py_atomic_load_uint8_relaxed(&op->ob_gc_bits);
_Py_atomic_store_uint8_relaxed(&op->ob_gc_bits, bits & ~bits_to_clear);
}

#endif
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.