Fix #108792: Ensure Metal buffers correctly freed on exit #108940

Merged
Jeroen Bakker merged 5 commits from Jason-Fielder/blender:Fix_108792 into blender-v3.6-release 2023-06-19 20:28:58 +02:00
Member

Replaces vector of allocations with dynamic linked list.
Bug caused by previously freed buffers still having been in the
list. Linked list enables fast removal of already-released buffers.

Also ensured that the memory manager classes are included in
memory tracking.

Authored by Apple: Michael Parkin-White

Replaces vector of allocations with dynamic linked list. Bug caused by previously freed buffers still having been in the list. Linked list enables fast removal of already-released buffers. Also ensured that the memory manager classes are included in memory tracking. Authored by Apple: Michael Parkin-White
Jason Fielder added 1 commit 2023-06-13 16:29:59 +02:00
a0ce3d7141 Fix #108792: Ensure Metal buffers correctly freed on exit
Replaces vector of allocations with dynamic linked list.
Bug caused by previously freed buffers still having been in the
list. Linked list enables fast removal of already-released buffers.

Also ensured that the memory manager classes are included in
memory tracking.

Authored by Apple: Michael Parkin-White
Jason Fielder requested review from Sergey Sharybin 2023-06-13 16:30:07 +02:00
Jason Fielder requested review from Jeroen Bakker 2023-06-13 16:30:17 +02:00
Sergey Sharybin requested changes 2023-06-13 17:28:18 +02:00
Sergey Sharybin left a comment
Owner

Bug caused by previously freed buffers still having been in the list.

I am a bit confused. There was no lists before.

Do you mean that previously it was possible to have situation when some elements of vectors are freed, but not null-ed in the vector or something (leading to attempt to free them again)?

> Bug caused by previously freed buffers still having been in the list. I am a bit confused. There was no lists before. Do you mean that previously it was possible to have situation when some elements of vectors are freed, but not null-ed in the vector or something (leading to attempt to free them again)?
@ -456,0 +516,4 @@
BLI_assert(prev == nullptr);
}
allocations_list_size_--;
BLI_assert(allocations_list_size_ >= 0);

allocations_list_size_ is unsigned, so the check is always true. You can do something like

BLI_assert(allocations_list_size_ >= 1);
allocations_list_size_--;
`allocations_list_size_` is unsigned, so the check is always true. You can do something like ``` BLI_assert(allocations_list_size_ >= 1); allocations_list_size_--; ```
First-time contributor

Bug caused by previously freed buffers still having been in the list.

I am a bit confused. There was no lists before.

Do you mean that previously it was possible to have situation when some elements of vectors are freed, but not null-ed in the vector or something (leading to attempt to free them again)?

Yes, by "list" I was referring to the original vector of elements. There was an erroneous situation after a recent change introducing memory cleanup which did not nullify the stored element in vector leading to a double free.

Replaced the vector with a linked list to prioritize fast insertion and removal, as the list is only ever iterated upon cleanup during shutdown.

(Will also update or just remove the allocations list size check as it is redundant. A number of the assertions are perhaps a little over-defensive given the usage of the code is rather minimal.)

> > Bug caused by previously freed buffers still having been in the list. > > I am a bit confused. There was no lists before. > > Do you mean that previously it was possible to have situation when some elements of vectors are freed, but not null-ed in the vector or something (leading to attempt to free them again)? Yes, by "list" I was referring to the original vector of elements. There was an erroneous situation after a recent change introducing memory cleanup which did not nullify the stored element in vector leading to a double free. Replaced the vector with a linked list to prioritize fast insertion and removal, as the list is only ever iterated upon cleanup during shutdown. (Will also update or just remove the allocations list size check as it is redundant. A number of the assertions are perhaps a little over-defensive given the usage of the code is rather minimal.)
Jeroen Bakker requested changes 2023-06-14 08:41:59 +02:00
@ -108,2 +108,4 @@
class MTLBuffer {
public:
gpu::MTLBuffer *next, *prev;
Member

use ListBase, ListBaseWrapperTemplate and the functions in BLI_listbase.h. Some casts might be needed, but it reduces code-duplication.

use `ListBase`, `ListBaseWrapperTemplate` and the functions in `BLI_listbase.h`. Some casts might be needed, but it reduces code-duplication.
First-time contributor

Thanks, have modified the implementation. Admittedly not 100% sure if there are any catches with ListBase.
I have attempted to utilise both BLI_freelinkN for deletion and BLI_freelistN for complete freeing of the list, but a little unsure how the builtin MEM_freeN functions work alongside C++ new/delete, and with constructors.

i.e. is it valid for gpu::MTLBuffer to be created using new gpu::MTLBuffer(params...), and freed using MEM_freeN(buffer)?

It appears to be releasing the resources correctly, as it looked like it was sufficient to use MEM_freeN based functions, if you have MEM_CXX_CLASS_ALLOC_FUNCS specified, however, it would be very helpful if someone could confirm whether the new implementation in the latest commit is correct.

Cheers!

Thanks, have modified the implementation. Admittedly not 100% sure if there are any catches with ListBase. I have attempted to utilise both `BLI_freelinkN` for deletion and `BLI_freelistN` for complete freeing of the list, but a little unsure how the builtin `MEM_freeN` functions work alongside C++ new/delete, and with constructors. i.e. is it valid for gpu::MTLBuffer to be created using `new gpu::MTLBuffer(params...)`, and freed using `MEM_freeN(buffer)`? It appears to be releasing the resources correctly, as it looked like it was sufficient to use `MEM_freeN` based functions, if you have `MEM_CXX_CLASS_ALLOC_FUNCS` specified, however, it would be very helpful if someone could confirm whether the new implementation in the latest commit is correct. Cheers!
First-time contributor

@Jason-Fielder, @Sergey, @Jeroen-Bakker, could we please have this fix merged as is, just to avoid those nasty random crashes and let @Michael-Parkin-White-Apple sort out the refactoring later on? 🙏

Update: I ended up building Blender from the main repo and applying the diff patch and as far as I am concerned (#108836) @Michael-Parkin-White-Apple 's fix does solve the crash on exit after a long sculpting session. 👍

@Jason-Fielder, @Sergey, @Jeroen-Bakker, could we please have this fix merged as is, just to avoid those nasty random crashes and let @Michael-Parkin-White-Apple sort out the refactoring later on? 🙏 Update: I ended up building Blender from the main repo and applying the diff patch and as far as I am concerned (#108836) @Michael-Parkin-White-Apple 's fix does solve the crash on exit after a long sculpting session. 👍

Out of an abundance of caution, I have the PR applied and am reporting the following warning on exit:

Blender(81644) MallocStackLogging: attempting to over-release a stack. Potential double free bug.
Saved session recovery to "/var/folders/5j/z5m41t610_s4hwj4vrcx4ttr0000gn/T/quit.blend"

Blender quit
2023-06-19 13:59:04.470923+1200 Blender[81644:8353398] [client] No error handler for XPC error: Connection invalid
Program ended with exit code: 0
Out of an abundance of caution, I have the PR applied and am reporting the following warning on exit: ``` Blender(81644) MallocStackLogging: attempting to over-release a stack. Potential double free bug. Saved session recovery to "/var/folders/5j/z5m41t610_s4hwj4vrcx4ttr0000gn/T/quit.blend" Blender quit 2023-06-19 13:59:04.470923+1200 Blender[81644:8353398] [client] No error handler for XPC error: Connection invalid Program ended with exit code: 0 ```
First-time contributor

Out of an abundance of caution, I have the PR applied and am reporting the following warning on exit:

Blender(81644) MallocStackLogging: attempting to over-release a stack. Potential double free bug.
Saved session recovery to "/var/folders/5j/z5m41t610_s4hwj4vrcx4ttr0000gn/T/quit.blend"

Blender quit
2023-06-19 13:59:04.470923+1200 Blender[81644:8353398] [client] No error handler for XPC error: Connection invalid
Program ended with exit code: 0

Thanks for raising this, I'll have a deeper look into this issue and see if there are any problems.
I'm unsure if this over-release is related. Was this also happening without the patch?

> Out of an abundance of caution, I have the PR applied and am reporting the following warning on exit: > ``` > Blender(81644) MallocStackLogging: attempting to over-release a stack. Potential double free bug. > Saved session recovery to "/var/folders/5j/z5m41t610_s4hwj4vrcx4ttr0000gn/T/quit.blend" > > Blender quit > 2023-06-19 13:59:04.470923+1200 Blender[81644:8353398] [client] No error handler for XPC error: Connection invalid > Program ended with exit code: 0 > ``` Thanks for raising this, I'll have a deeper look into this issue and see if there are any problems. I'm unsure if this over-release is related. Was this also happening without the patch?
Michael Parkin-White added 1 commit 2023-06-19 11:37:13 +02:00

Out of an abundance of caution, I have the PR applied and am reporting the following warning on exit:

Blender(81644) MallocStackLogging: attempting to over-release a stack. Potential double free bug.
Saved session recovery to "/var/folders/5j/z5m41t610_s4hwj4vrcx4ttr0000gn/T/quit.blend"

Blender quit
2023-06-19 13:59:04.470923+1200 Blender[81644:8353398] [client] No error handler for XPC error: Connection invalid
Program ended with exit code: 0

Thanks for raising this, I'll have a deeper look into this issue and see if there are any problems.
I'm unsure if this over-release is related. Was this also happening without the patch?

I have a suspicion the "Potential double free bug" is actually related to a different bug I was chasing today ( #107500 ), but I never previously saw the warning because of the metal-buffers issue.

... most likely nothing to see here ...

> > Out of an abundance of caution, I have the PR applied and am reporting the following warning on exit: > > ``` > > Blender(81644) MallocStackLogging: attempting to over-release a stack. Potential double free bug. > > Saved session recovery to "/var/folders/5j/z5m41t610_s4hwj4vrcx4ttr0000gn/T/quit.blend" > > > > Blender quit > > 2023-06-19 13:59:04.470923+1200 Blender[81644:8353398] [client] No error handler for XPC error: Connection invalid > > Program ended with exit code: 0 > > ``` > > Thanks for raising this, I'll have a deeper look into this issue and see if there are any problems. > I'm unsure if this over-release is related. Was this also happening without the patch? I have a suspicion the "Potential double free bug" is actually related to a different bug I was chasing today ( #107500 ), but I never previously saw the warning because of the metal-buffers issue. ... most likely nothing to see here ...
Jason Fielder added 1 commit 2023-06-19 15:23:44 +02:00
Member

@Michael-Parkin-White-Apple I think you're right and we should revert the change of listbase. Sorry for that. ListBase isn't aware that it needs to call the destructor and could lead to other memory issues.

@Michael-Parkin-White-Apple I think you're right and we should revert the change of listbase. Sorry for that. ListBase isn't aware that it needs to call the destructor and could lead to other memory issues.
First-time contributor

@Michael-Parkin-White-Apple I think you're right and we should revert the change of listbase. Sorry for that. ListBase isn't aware that it needs to call the destructor and could lead to other memory issues.

Ahh right, so this may just free the root ptr but not the destructor?

I think we can keep the list base wrapper for the core list, but perhaps just revert to the manual iteration for release? It also looks like we can use BLI_remlink instead of BLI_freelinkN to just disconnect a link in the node, then release the memory separately.

Apologies, still familiarising with this API.

> @Michael-Parkin-White-Apple I think you're right and we should revert the change of listbase. Sorry for that. ListBase isn't aware that it needs to call the destructor and could lead to other memory issues. Ahh right, so this may just free the root ptr but not the destructor? I think we can keep the list base wrapper for the core list, but perhaps just revert to the manual iteration for release? It also looks like we can use `BLI_remlink` instead of `BLI_freelinkN` to just disconnect a link in the node, then release the memory separately. Apologies, still familiarising with this API.
Member

Best not use the list base api at all. That would only confuse other developers. Just add a note why listbase isn’t used.

Best not use the list base api at all. That would only confuse other developers. Just add a note why listbase isn’t used.
Jason Fielder added 2 commits 2023-06-19 16:34:19 +02:00
Sergey Sharybin approved these changes 2023-06-19 17:12:07 +02:00
Sergey Sharybin left a comment
Owner

To me it looks good now. Thanks for the updates!
Would be cool to have Jeroen's final pass of review.

To me it looks good now. Thanks for the updates! Would be cool to have Jeroen's final pass of review.
Jeroen Bakker approved these changes 2023-06-19 20:28:14 +02:00
Jeroen Bakker merged commit 53cb09357e into blender-v3.6-release 2023-06-19 20:28:58 +02:00
Jeroen Bakker added the
Interest
Metal
label 2023-06-19 20:29:08 +02:00
Jeroen Bakker added this to the 3.6 LTS milestone 2023-06-19 20:29:13 +02:00
Jeroen Bakker added this to the EEVEE & Viewport project 2023-06-19 20:29:17 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No Assignees
6 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#108940
No description provided.