From cedcaa2949c795e71a47e78ae413c8a41b17d7d0 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:26:01 +0800 Subject: [PATCH] fix(memory): hold imported pages through a memory entry Core Animation unmaps an image's Shmem once its no-copy buffer is released, which can come before the runtime frees the import. The render thread of backboardd then stayed in vm_deallocate while the import still had the pages wired by its UPL, and the desktop froze. An import of user memory now asks for kIOMemoryPersistent, so the descriptor keeps a memory entry and with it a reference on the VM object; the owner's unmap leaves the object to the import until it completes. A range no memory entry can be made of is imported as before. Two counters tell the cases apart. --- NativeAdrenoMemory.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/NativeAdrenoMemory.cpp b/NativeAdrenoMemory.cpp index cec75a7..4b402ca 100644 --- a/NativeAdrenoMemory.cpp +++ b/NativeAdrenoMemory.cpp @@ -159,6 +159,20 @@ failed: * as success. No object has been exposed to a GPU or userspace yet. */ (void)release();return status; } +/* An import holds the pages through a memory entry of its own (kIOMemoryPersistent), which references the VM object: the owner may unmap + * its range while the GPU still uses it (Core Animation frees an image's Shmem once its no-copy buffer is released, before the runtime frees + * the import), and the object then lives on until the import completes instead of being torn down under a wired UPL (int-233..235: the render + * thread stuck in vm_deallocate). A range no memory entry can be made of is imported as before; [0] counts the persistent imports, [1] those. */ +extern "C" {volatile uint32_t native_adreno_import_persistent[2];} +static IOMemoryDescriptor *importRange(uint64_t address,size_t bytes,IODirection direction,void *task) +{ + const IOOptionBits options=static_cast(direction)|static_cast(kIOMemoryMapperNone); + IOMemoryDescriptor *range=IOMemoryDescriptor::withAddressRange(address,bytes,options|kIOMemoryPersistent,static_cast(task)); + if(range) {native_adreno_import_persistent[0]=native_adreno_import_persistent[0]+1;return range;} + range=IOMemoryDescriptor::withAddressRange(address,bytes,options,static_cast(task)); + if(range)native_adreno_import_persistent[1]=native_adreno_import_persistent[1]+1; + return range; +} IOReturn NativeAdrenoMemory::importTask(void *task,uint64_t address,size_t bytes,unsigned bits,size_t budget) { native_adreno_import_stage=10; @@ -172,8 +186,7 @@ IOReturn NativeAdrenoMemory::importTask(void *task,uint64_t address,size_t bytes native_adreno_import_stage=13; IOReturn status=NativeAdrenoQueryCpu4K(&cpu);if(status!=kIOReturnSuccess)return status; const uint64_t limit=UINT64_C(1)<(kIODirectionInOut)|static_cast(kIOMemoryMapperNone),static_cast(task)); + imported_=importRange(address,bytes,kIODirectionInOut,task); native_adreno_import_stage=14; if(!imported_)return kIOReturnNoMemory; native_adreno_import_stage=15; @@ -182,8 +195,7 @@ IOReturn NativeAdrenoMemory::importTask(void *task,uint64_t address,size_t bytes /* Read-only memory of the owner (a mapped file, constant data): cannot be pinned for writing. Pin it for reading only; the GPU then maps it * read-only. Metal's no-copy buffers over such memory are only ever read by shaders. */ imported_->release();imported_=nullptr; - imported_=IOMemoryDescriptor::withAddressRange(address,bytes, - static_cast(kIODirectionOut)|static_cast(kIOMemoryMapperNone),static_cast(task)); + imported_=importRange(address,bytes,kIODirectionOut,task); if(!imported_)return kIOReturnNoMemory; status=imported_->prepare(); if(status==kIOReturnSuccess)read_only_=true;