forked from facebook/CacheLib
-
Notifications
You must be signed in to change notification settings - Fork 4
Fix race in acquire #68
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inside this spin loop, it is a good practice to use pause instructions. Actually, folly already has platform-agnostic function
asm_volatile_pause(). Furthermore, there is afolly::detail::Sleeperabstraction, but the problem is that it is not a public interface.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this loop is actually guaranteed to have at most 2 iterations. I'll try to verify this experimentally but here's my thinking:
acquire() is always called under Access Container lock so there is only one scenario (1.a.) when the race can happen on the evicting 'item':
tryEvictToNextMemoryTierfailed, or we are evicting from the last tiera.
markForEvictionWhenMovingis called just after we gotincFailedMoving: if under the MoveLock we realize that item is no longer moving, it has has to be marked for eviction so we just return NULL (we loop once and fail with incFailedEviction). There's no use-after free on the item becauseacquireholds AC lock, so findEviction will block onunlinkItemForEviction.b. in all other cases we synchronize on AC lock or the item is already marked for eviction (and we just return NULL).
a.
unmarMovingis called just after we gotincFailedMoving? This cannot happen becauseunmarkMovingis called AFTERAC->replaceIf(which takes AC lock) which means we wouldn't enteracquirewith pointer to theitemthat is being evicted.We also need to consider
newItemHdl. IffindorinserOrReplacegetsnewItemthat is still markedmoving(replaceIfinmoveRegularItemWithSyncwas executed butunmarkMovingwas not) and thenewItemis no longer moving underMoveLockthen we just increment theitem(it will work since it's no longer moving).If someone will try to evict/move this new item before acquire gets a chance to
incRefsuccessfully,acquirewill surely succeed in creating a wait context (because acquire() is under AC lock so findEviction cannotreplaceIforremovefrom access container which is always done before unmarking).