[scudo] Only lock active TSDs in TSDRegistrySharedT::disable() (#233617)

TSDRegistrySharedT::disable() and enable() run around fork() via
pthread_atfork and lock and unlock all TSDsArraySize TSDs. Only the
first NumberOfTSDs TSDs can ever be handed to a thread, so touching the
rest only dirties their pages.

On Android (TSDsArraySize 8, NumberOfTSDs 2 by default), unlocking the
six inactive TSDs in the child writes to their mutex words and causes
copy-on-write faults on five otherwise untouched .bss pages in every
forked process. On an idle device this is 20 KiB of private dirty
memory per Zygote-forked process, or about 2.3 MiB across the ~120 such
processes.

Bound both loops by NumberOfTSDs, matching drainCaches() and getStats().
setNumberOfTSDs() only grows the count and only while holding Mutex,
which disable() holds until enable(), so both loops see the same value.

Also call disable()/enable() in TSDRegistryTSDsCount before and after
raising MaxTSDsCount, as a smoke test that the lock and unlock loops
stay balanced.

Assisted-by: Gemini
GitOrigin-RevId: 470fbd4326de41f94d6d2f6a786c61c0210e706c
2 files changed