Skip to content
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

Remove cast_ref_to_mut related to SFT #851

Closed
wks opened this issue Jun 8, 2023 · 3 comments
Closed

Remove cast_ref_to_mut related to SFT #851

wks opened this issue Jun 8, 2023 · 3 comments
Assignees

Comments

@wks
Copy link
Collaborator

wks commented Jun 8, 2023

Parent: #850

SFTMap implementations cast &self to &mut self for modifying its underlying sft: Vec<&'a (dyn SFT + Sync + 'static)>. This table is accessed by multiple threads, but it needs to be updated occasionally. It is problematic in two ways:

  1. Each element can be read and updated at the same time. This is a data race which has undefined behaviour.
  2. After one thread grows the space, other threads can access the SFT for the extended part while the space that grew the space is still updating the SFT entries. See:
    // We need this lock: Othrewise, it is possible that one thread acquires pages in a new chunk, but not yet

One way to solve the first problem is changing the elements of the Vec to AtomicPtr. However, & dyn Trait is 16 bytes in size.

The "double-checked locking" algorithm can be used for data structures that are lazily initialised. I think the use pattern of SFT matches lazy initialisation in certain ways, and may be applicable.

@playXE
Copy link
Contributor

playXE commented Aug 2, 2023

&dyn Trait can be replaced with AtomicU128. The problem is most likely in HashMap, it has to be replaced with DashMap to allow safe concurrent access. But also, the problem is that Vec can be pushed into which is not really safe as well in multiple threads scenario

@k-sareen
Copy link
Collaborator

k-sareen commented Aug 6, 2023

A fix was merged in #879, but I'm keeping this issue open since removing unsafe would be ideal.

@qinsoon qinsoon moved this to 🔖 Ready in v0.20 (20230929) Aug 30, 2023
@qinsoon qinsoon moved this from 🔖 Ready to 🏗 In progress in v0.20 (20230929) Aug 30, 2023
@qinsoon qinsoon self-assigned this Aug 30, 2023
@qinsoon qinsoon moved this from 🏗 In progress to 👀 In review in v0.20 (20230929) Sep 4, 2023
@qinsoon qinsoon moved this from 👀 In review to ✅ Done in v0.20 (20230929) Sep 6, 2023
@qinsoon
Copy link
Member

qinsoon commented Sep 6, 2023

Done in #931.

@qinsoon qinsoon closed this as completed Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

No branches or pull requests

4 participants