diff --git a/cuda_core/cuda/core/_memory/_ipc.pyi b/cuda_core/cuda/core/_memory/_ipc.pyi index ebeb839b058..06599a3e656 100644 --- a/cuda_core/cuda/core/_memory/_ipc.pyi +++ b/cuda_core/cuda/core/_memory/_ipc.pyi @@ -215,5 +215,6 @@ class VirtualMemoryFabricBufferDescriptor(_VirtualMemoryDescriptor): def __eq__(self, other) -> bool: ... def __hash__(self) -> int: ... +def _set_register_reentry_hook_for_testing(hook): ... def _reduce_allocation_handle(alloc_handle: IPCAllocationHandle) -> tuple[object, ...]: ... def _reconstruct_allocation_handle(cls: type, df: object, uuid: uuid.UUID | None) -> IPCAllocationHandle: ... diff --git a/cuda_core/cuda/core/_memory/_ipc.pyx b/cuda_core/cuda/core/_memory/_ipc.pyx index 199af8da6cb..f73f6e198a3 100644 --- a/cuda_core/cuda/core/_memory/_ipc.pyx +++ b/cuda_core/cuda/core/_memory/_ipc.pyx @@ -46,6 +46,20 @@ cdef object registry = weakref.WeakValueDictionary() # weak reference, which can run a garbage-collection callback or finalizer that # registers another resource on the same thread. cdef object _registry_lock = threading.RLock() +# The resource each in-progress MP_register call is inserting, by UUID. Accessed +# only under _registry_lock, so only the thread inserting it sees an entry. +cdef dict _registering = {} + +# Test-only hook. When set, MP_register calls it right before it inserts into the +# registry, so a test can register the same UUID re-entrantly at the point where +# a garbage-collection finalizer could. See +# test_reentrant_registration_of_one_uuid_picks_one_winner. +cdef object _register_reentry_hook_for_testing = None + + +def _set_register_reentry_hook_for_testing(hook): + global _register_reentry_hook_for_testing + _register_reentry_hook_for_testing = hook cdef cydriver.CUmemAllocationHandleType IPC_HANDLE_TYPE = \ @@ -623,6 +637,10 @@ cdef _MemPool MP_register(_MemPool self, uuid): # threads can both miss and register different resources for one UUID. with _registry_lock: existing = registry.get(uuid) + if existing is None: + # A garbage-collection callback re-entered MP_register while this + # thread is inserting another resource under this UUID. + existing = _registering.get(uuid) if existing is not None: MP_check_open(<_MemPool>existing) return existing @@ -633,7 +651,23 @@ cdef _MemPool MP_register(_MemPool self, uuid): f"Cannot register memory resource with UUID {uuid}: " f"the resource already has UUID {self.uuid}" ) - registry[uuid] = self + # The registry and uuid.UUID run Python code, where a collection can + # start and its callbacks can register this UUID on this thread. Once + # this resource is marked, such a registration returns it instead of + # inserting its own, which the insertion below would overwrite. One + # that completed before the mark is found by the second lookup. + _registering[uuid] = self + try: + existing = registry.get(uuid) + if existing is None: + if _register_reentry_hook_for_testing is not None: + _register_reentry_hook_for_testing(uuid) + registry[uuid] = self + finally: + del _registering[uuid] + if existing is not None: + MP_check_open(<_MemPool>existing) + return existing self._ipc_data._alloc_handle._uuid = uuid return self diff --git a/cuda_core/tests/helpers/memory.py b/cuda_core/tests/helpers/memory.py index 506e54c3e94..1a479808cf3 100644 --- a/cuda_core/tests/helpers/memory.py +++ b/cuda_core/tests/helpers/memory.py @@ -63,6 +63,19 @@ def import_pool_resource(cls, device, alloc_handle): return DeviceMemoryResource.from_allocation_handle(device, alloc_handle) +def pool_supported_devices(mr_type, device): + """Devices on which an imported pool of ``mr_type`` grants access in this process. + + A pinned (host-NUMA) pool grants device access to every device that supports + host NUMA memory pools; a device pool grants access to its own device. Keep + this in sync with ``_PMR_grant_device_access``. + """ + if mr_type is PinnedMemoryResource: + return [d for d in Device.get_all_devices() if d.properties.host_numa_memory_pools_supported] + assert mr_type is DeviceMemoryResource, mr_type + return [device] + + def pool_access_flags(mr, device): """The ``CUmemAccess_flags`` that ``device`` has on the memory pool of ``mr`` in this process.""" location = driver.CUmemLocation() diff --git a/cuda_core/tests/memory_ipc/test_ipc_pool_duplicate_import.py b/cuda_core/tests/memory_ipc/test_ipc_pool_duplicate_import.py index 2c5769106b7..dc4aa16c792 100644 --- a/cuda_core/tests/memory_ipc/test_ipc_pool_duplicate_import.py +++ b/cuda_core/tests/memory_ipc/test_ipc_pool_duplicate_import.py @@ -22,11 +22,12 @@ import pytest from helpers.child_processes import run_child from helpers.contexts import assert_no_cuda_warning -from helpers.memory import import_pool_resource, is_pool_resource, pool_access_flags +from helpers.memory import import_pool_resource, is_pool_resource, pool_access_flags, pool_supported_devices from cuda.bindings import driver from cuda.core import Buffer, Device, DeviceMemoryResource from cuda.core._memory._buffer import _live_owned_mempool_count +from cuda.core._memory._ipc import _set_register_reentry_hook_for_testing from cuda.core._rt import _set_ipc_pool_release_delay_for_testing NBYTES = 64 @@ -144,8 +145,11 @@ def reimport(): raise errors[0] # The release has finished: its cleanup must have left the new import alone. + # The final release revokes access one GPU at a time, so check every GPU the + # pool grants access to, not only the default one. ((mr, buffer),) = reimported - assert pool_access_flags(mr, device) == driver.CUmemAccess_flags.CU_MEM_ACCESS_FLAGS_PROT_READWRITE + for access_device in pool_supported_devices(mr_type, device): + assert pool_access_flags(mr, access_device) == driver.CUmemAccess_flags.CU_MEM_ACCESS_FLAGS_PROT_READWRITE buffer.fill(7, stream=stream) stream.sync() buffer.close() @@ -205,6 +209,47 @@ def register_during_collection(phase, info): assert _live_owned_mempool_count() == baseline +def child_register_reentrant_same_uuid(mr_type, alloc_handle): + device = Device() + device.set_current() + baseline = _live_owned_mempool_count() + + # Two resources that both claim one UUID: the outer call and a re-entrant + # call that the hook injects right before the outer call's insertion, where + # a garbage-collection finalizer could. They share one pool handle, so both + # are valid owners; registration must pick exactly one of them, and both + # calls must return it. + first = import_pool_resource(mr_type, device, int(alloc_handle)) + second = import_pool_resource(mr_type, device, int(alloc_handle)) + + target_uuid = uuid.uuid4() + reentrant = [] + + def inject_reentrant_registration(hooked_uuid): + # Fire once: disarm before re-entering so the nested registration does + # not recurse through the hook. + _set_register_reentry_hook_for_testing(None) + reentrant.append(second.register(hooked_uuid)) + + _set_register_reentry_hook_for_testing(inject_reentrant_registration) + try: + outer = first.register(target_uuid) + finally: + _set_register_reentry_hook_for_testing(None) + + assert reentrant, "the reentry hook did not fire" + winner = reentrant[0] + assert winner is first or winner is second + # Both calls agree on the winner, and the registry holds it, so no second + # owner of the pool survives under this UUID. + assert outer is winner + assert mr_type.from_registry(target_uuid) is winner + + first.close() + second.close() + assert _live_owned_mempool_count() == baseline + + def _import_concurrently(mr_type, device, alloc_handle): barrier = threading.Barrier(THREADS) results = [None] * THREADS @@ -303,6 +348,11 @@ def test_registration_reentered_from_garbage_collection(self, ipc_device, pool_r ipc_device.set_current() run_child(child_register_reentered_by_gc, type(pool_resource), pool_resource.allocation_handle) + @pytest.mark.agent_authored(model="claude-opus-5-5") + def test_reentrant_registration_of_one_uuid_picks_one_winner(self, ipc_device, pool_resource): + ipc_device.set_current() + run_child(child_register_reentrant_same_uuid, type(pool_resource), pool_resource.allocation_handle) + @pytest.mark.agent_authored(model="claude-opus-5-5") def test_concurrent_imports_of_one_uuid_share_a_resource(self, ipc_device, pool_resource): ipc_device.set_current()