optimize slab quarantine: unify random+FIFO into single array#334
Open
peterlodri-sec wants to merge 1 commit into
Open
optimize slab quarantine: unify random+FIFO into single array#334peterlodri-sec wants to merge 1 commit into
peterlodri-sec wants to merge 1 commit into
Conversation
Member
|
There are conflicts with the current state of the code. The size of the quarantine should also not be increased. Fixing the u16 issue for large quarantine sizes should be fixed separately. I haven't looked at the code itself yet. |
Combine the two-stage slab quarantine (random array + FIFO queue) into a single random-replacement array. This reduces per-free operations from 2 pointer swaps to 1, improving performance while keeping the total quarantine slot count identical. - Replace CONFIG_SLAB_QUARANTINE_RANDOM_LENGTH and CONFIG_SLAB_QUARANTINE_QUEUE_LENGTH with unified CONFIG_SLAB_QUARANTINE_LENGTH - Default: 2 (same total slots as old 1+1) - Light: 0 (unchanged) - Remove stale FIFO references from README - Add extended-size-class quarantine tests Fixes GrapheneOS#179
938451c to
a4e45ad
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Implements #179: unify the two-stage
random + FIFOslab quarantine into a single random-replacement array. Steady-state per-free()work drops from 2 pointer swaps to 1.Background
The previous slab quarantine had two sequential stages:
SLAB_QUARANTINE_RANDOM_LENGTHslotsSLAB_QUARANTINE_QUEUE_LENGTHslotsIn steady state every
free()did 2 swaps: one against the random array, then one against the FIFO ring buffer.Change
Replace both arrays with a single random-replacement array of length
SLAB_QUARANTINE_LENGTH.Capacity is unchanged
The new default
SLAB_QUARANTINE_LENGTH = 2preserves the old default total capacity (RANDOM=1 + QUEUE=1).lightstays at0.Android.bpis updated to2to match.Tradeoff
The unified design loses the deterministic FIFO minimum delay before reuse. Eviction is now geometric: a pointer can in principle be evicted on the next
free(), with expected dwell time equal toLENGTHfrees. For the defaultLENGTH=2, the expected delay matches the oldRANDOM=1 + QUEUE=1. Double-free, invalid-free and write-after-free detection are untouched.Key changes
h_malloc.cquarantine[]array replacesquarantine_random[] + quarantine_queue[] + quarantine_queue_index.deallocate_small(): 1 random swap (was 2 swaps).h_malloc_trim(): one purge loop (was two).static_assertupper bound (<= 65536) andget_random_u16_uniformuse preserved unchanged.Configuration
CONFIG_SLAB_QUARANTINE_RANDOM_LENGTH+CONFIG_SLAB_QUARANTINE_QUEUE_LENGTHcollapsed intoCONFIG_SLAB_QUARANTINE_LENGTH.config/default.mk=2,config/light.mk=0,Android.bp=2.README.mdCONFIG_SLAB_QUARANTINE_LENGTHdescription (2scales to2048for 16-byte allocs at 16 KiB max,16384at 128 KiB max).Tests (5 new files)
quarantine_double_free_extendedquarantine_double_free_extended_delayeddouble freeordouble free (quarantine))quarantine_invalid_malloc_usable_size_extendedmalloc_usable_sizeon a quarantined extended-class pointerquarantine_invalid_malloc_object_size_extendedmalloc_object_sizeon a quarantined extended-class pointerquarantine_write_after_free_extended_reuseAll existing tests pass.
Migration
RANDOM=1, QUEUE=1LENGTH=2(default; same capacity)RANDOM=2, QUEUE=4LENGTH=6(same capacity)RANDOM=0, QUEUE=0LENGTH=0(disabled)There is no longer a deterministic minimum delay. Eviction follows a geometric distribution and higher
LENGTHincreases the expected delay.Notes for review
2; no increase.get_random_u16_uniformtruncation for very largeSLAB_QUARANTINE_LENGTHis not addressed here and is intended as a separate PR. With the defaultLENGTH=2the scaled length stays well underU16_MAX.main; GitHub reportsMERGEABLE/CLEAN.Closes #179