Fix #112351: Sculpt implicit sharing thread safety crash #112690

Merged
Hans Goudey merged 7 commits from HooglyBoogly/blender:sculpt-implicit-sharing-crash into main 2023-09-26 14:21:15 +02:00
Member

Currently the iteration over a PBVH node's vertices retrieves mutable
access to the mask custom data layer. This isn't threadsafe, but it is
done in the multithreaded loops over all nodes.

In general, we need to be more careful and conservative about storage
of non-const pointers to mesh data. Ideally we would only have one
mutable reference to a resource at a time. And we should avoid doing
work like looking up custom data layers more than we need to.

To that end, make the pointer to the custom data layer used everywhere
const, and retrieve mutable access before parallel node iteration with
a specific function, and write to the mask data with that in mind.

This pushes us in the direction of sharing less code per PBVH type.
In my opinion that's a good thing, because we can actually optimize for
each type. For example, SCULPT_mask_write_array gives a picture of
how each of these hot loops could become much simpler.

Currently the iteration over a PBVH node's vertices retrieves mutable access to the mask custom data layer. This isn't threadsafe, but it is done in the multithreaded loops over all nodes. In general, we need to be more careful and conservative about storage of non-const pointers to mesh data. Ideally we would only have one mutable reference to a resource at a time. And we should avoid doing work like looking up custom data layers more than we need to. To that end, make the pointer to the custom data layer used everywhere const, and retrieve mutable access before parallel node iteration with a specific function, and write to the mask data with that in mind. This pushes us in the direction of sharing less code per PBVH type. In my opinion that's a good thing, because we can actually optimize for each type. For example, `SCULPT_mask_write_array` gives a picture of how each of these hot loops could become much simpler.
Hans Goudey added 1 commit 2023-09-21 21:37:51 +02:00
7a4c73fb8e Fix #112351: Sculpt implicit sharing thread safety crash
Currently the iteration over a PBVH node's vertices retrieves mutable
access to the mask custom data layer. This isn't threadsafe, but it is
done in the multithreaded loops over all nodes.

In general, we need to be more careful and conservative about storage
of non-const pointers to mesh data. Ideally we would only have one
mutable reference to a resource at a time. And we should avoid doing
work like looking up custom data layers more than we need to.

To that end, make the pointer to the custom data layer used everywhere
const, and retrieve mutable access before parallel node iteration with
a specific function, and write to the mask data with that in mind.

This pushes us in the direction of sharing less code per PBVH type.
In my opinion that's a good thing, because we can actually optimize for
each type. For example, `SCULPT_mask_write_array` gives a picture of
how each of these hot loops could become much simpler.
Hans Goudey added this to the Sculpt, Paint & Texture project 2023-09-21 21:38:13 +02:00
Hans Goudey requested review from Sergey Sharybin 2023-09-21 21:38:19 +02:00
Sergey Sharybin requested changes 2023-09-22 09:33:39 +02:00
Sergey Sharybin left a comment
Owner

On a bigger scale the change sounds good, and did not spot obvious mistakes. There are some inlined comments though which would be nice to iterate over.

On a bigger scale the change sounds good, and did not spot obvious mistakes. There are some inlined comments though which would be nice to iterate over.
@ -502,3 +504,3 @@
int totvert;
const int *vert_indices;
float *vmask;
const float *vmask;

Intuitively this should no longer be a pointer, but a single float. It used to be a pointer to allow an easy write access which is now done by SCULPT_mask_vert_set.

Intuitively this should no longer be a pointer, but a single float. It used to be a pointer to allow an easy write access which is now done by `SCULPT_mask_vert_set`.
Author
Member

Totally agree! But do you mind if I do that in a separate commit after this? It would make the diff much bigger I think.

Totally agree! But do you mind if I do that in a separate commit after this? It would make the diff much bigger I think.

Separate commit sounds perfectly fine.

Separate commit sounds perfectly fine.
@ -2718,3 +2718,3 @@
for (int i = 0; i < data->totlayer; i++) {
if (data->layers[i].type == type) {
BLI_assert(uint(n) < uint(layer_num));
BLI_assert(n < layer_num);

This does not seem to be related to the problem which the patch description is solving.

This does not seem to be related to the problem which the patch description is solving.
Author
Member

Right, sorry! It was required to make the file from the bug report load without an assert. But I'll remove it now.

Right, sorry! It was required to make the file from the bug report load without an assert. But I'll remove it now.
HooglyBoogly marked this conversation as resolved
@ -74,3 +74,3 @@
{0}};
static void mask_flood_fill_set_elem(float *elem, PaintMaskFloodMode mode, float value)
static float mask_flood_fill_set_elem(const float elem, PaintMaskFloodMode mode, float value)

I do not think it is valid to call it "set_elem" anymore. It is more of "get_new_value_for_elem" type of operation.

I do not think it is valid to call it "set_elem" anymore. It is more of "get_new_value_for_elem" type of operation.
HooglyBoogly marked this conversation as resolved
@ -122,1 +121,3 @@
if (prevmask != *vi.mask) {
const float new_mask = mask_flood_fill_set_elem(prevmask, mode, value);
if (prevmask != new_mask) {
SCULPT_mask_vert_set(BKE_pbvh_type(ob->sculpt->pbvh), mask_write, new_mask, vi);

The first BKE_pbvh_type(ob->sculpt->pbvh) argument seems to be repeated for every function. Did you consider passing the session/pbvh instead to shorten the invocation line?

The first `BKE_pbvh_type(ob->sculpt->pbvh)` argument seems to be repeated for every function. Did you consider passing the session/pbvh instead to shorten the invocation line?
Author
Member

I don't have a strong opinion, but I chose this way so it was clear that the function didn't depend on any other part of the PBVH besides the type. Otherwise it felt a bit more like a black box.

Initially I wanted to not have the SCULPT_mask_vert_set function at all for the same reason, but that made a bit too much code duplication.

I don't have a strong opinion, but I chose this way so it was clear that the function didn't depend on any other part of the `PBVH` besides the type. Otherwise it felt a bit more like a black box. Initially I wanted to not have the `SCULPT_mask_vert_set` function at all for the same reason, but that made a bit too much code duplication.

Hrm, you do have a good point. Well, lets leave it as-is then :)

Hrm, you do have a good point. Well, lets leave it as-is then :)
HooglyBoogly marked this conversation as resolved
@ -1205,3 +1200,1 @@
BKE_pbvh_vertex_iter_end;
BKE_pbvh_node_mark_redraw(node);
}
SCULPT_mask_write_array(ss, nodes, {expand_cache->original_mask, SCULPT_vertex_count_get(ss)});

To me this can/should be split into a separate change, for the ease of bisect. Unless there is some dependency with the rest of the changes in this patch.

To me this can/should be split into a separate change, for the ease of bisect. Unless there is some dependency with the rest of the changes in this patch.
HooglyBoogly marked this conversation as resolved
Hans Goudey added 5 commits 2023-09-26 05:01:48 +02:00
Sergey Sharybin approved these changes 2023-09-26 09:25:02 +02:00
Sergey Sharybin left a comment
Owner

Thanks for the updates.

Only did code review, which seems fine. Please make sure you've tested code on a functional level before landing :)

Thanks for the updates. Only did code review, which seems fine. Please make sure you've tested code on a functional level before landing :)
Hans Goudey added 1 commit 2023-09-26 14:19:42 +02:00
Hans Goudey merged commit 97f2b01ea9 into main 2023-09-26 14:21:15 +02:00
Hans Goudey deleted branch sculpt-implicit-sharing-crash 2023-09-26 14:21:16 +02:00
Sign in to join this conversation.
No reviewers
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
2 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#112690
No description provided.