108 Commits

Author SHA1 Message Date
ChiaHungDuan
4ea0ef2e94
[scudo] Move out the definitions of member functions in primary allocators (#147601)
This greatly improves the readability so that we are able to tell the
design by the concise class definition.
2025-07-09 13:42:02 -07:00
ChiaHungDuan
8b65c9d1ed
[scudo] Make block storage in TransferBatch trailing objects (#144204)
This allows us to change the number of blocks stored according to the
size of BatchClass.

Also change the name `TransferBatch` to `Batch` given that it's never
the unit of transferring blocks.
2025-07-08 11:07:05 -07:00
Christopher Ferris
1756fcb8b0
[scudo] Add primary option to enable/disable cache blocks. (#129794)
When configured this way, no primary blocks will be cached except the
batch class. Nothing else changes, no change in the page releasing
strategy.
2025-04-17 14:23:42 -07:00
ChiaHungDuan
b71c44b9be
[scudo] Add the record of number of attempted page release (#120497)
This also removes the `RangesReleased` which doesn't give much insight
to whether we should adjust the heuristic of doing page release.
2024-12-19 10:47:44 -08:00
ChiaHungDuan
0347c11226
[scudo] Remove unused field in BatchGroup (#109322)
Also fix the logic while determining the size of BatchClass and update
some legacy comments.
2024-09-19 13:58:47 -07:00
ChiaHungDuan
d9a9960203
[scudo] Add fragmentation info for each memory group (#107475)
This information helps with tuning the heuristic of selecting memory
groups to release the unused pages.
2024-09-09 13:59:03 -07:00
Christopher Ferris
1ff8657b26
[scudo] Use variable instead of recomputing. (#106647)
In the get fragmentation functions, there is already a variable that
computes the
in use bytes, so use that instead of recomputing it.
2024-09-04 14:12:25 -07:00
ChiaHungDuan
11f4f458d9
[scudo] Support setting default value of ReleaseToOsIntervalMs in config (#90256) 2024-04-29 08:41:46 -07:00
ChiaHungDuan
2dc9ec47fb
[scudo] Refactor allocator config to support optional flags (#81805)
Instead of explicitly disabling a feature by declaring the variable and
set it to false, this change supports the optional flags. I.e., you can
skip certain flags if you are not using it.

This optional feature supports both forms,
  1. Value: A parameter for a feature. E.g., EnableRandomOffset
  2. Type: A C++ type implementing a feature. E.g., ConditionVariableT

On the other hand, to access the flags will be through one of the
wrappers, BaseConfig/PrimaryConfig/SecondaryConfig/CacheConfig
(CacheConfig is embedded in SecondaryConfig). These wrappers have the
getters to access the value and the type. When adding a new feature, we
need to add it to `allocator_config.def` and mark the new variable with
either *_REQUIRED_* or *_OPTIONAL_* macro so that the accessor will be
generated properly.

In addition, also remove the need of `UseConditionVariable` to flip
on/off of condition variable. Now we only need to define the type of
condition variable.
2024-03-13 16:05:24 -07:00
ChiaHungDuan
1a7776abe6
Reland "[scudo] Store more blocks in each TransferBatch" (#83078) (#83081)
This reverts commit 056d62be38c5db3d8332ac300c4ff29214126697.

Fixed the number of bytes copied in moveNToArray()
2024-02-28 12:09:49 -08:00
ChiaHungDuan
056d62be38
Revert "[scudo] Store more blocks in each TransferBatch" (#83078)
Reverts llvm/llvm-project#70390

There's a bug caught by
`ScudoCombinedTestReallocateInPlaceStress_DefaultConfig.ReallocateInPlaceStress`
with gwp asan. It's an easy fix but given that this is a major change, I
would like to revert it first
2024-02-26 15:21:32 -08:00
ChiaHungDuan
1865c7ea85
[scudo] Store more blocks in each TransferBatch (#70390)
Instead of always storing the same number of blocks as cached, we prefer
increasing the utilization by saving more blocks in a single
TransferBatch. This may slightly impact the performance, but it will
save a lot of memory used by BatchClassId (especially for larger
blocks).
2024-02-26 11:04:08 -08:00
Christopher Ferris
a8ef9c0969
[scudo] Add utilization percentages for stats. (#75101)
Refactor the percentage display in the secondary code. Re-use that to
display a utilization percentage when displaying fragmentation data.
2023-12-11 16:42:44 -08:00
ChiaHungDuan
4e8d6c4f82
[scudo] Pass the max number of blocks to popBlocks (#70243)
Make the cache have the fully control on how many blocks to be popped
(At before, it depended the number of blocks stored in the
TransferBatch)
2023-10-26 15:16:57 -07:00
ChiaHungDuan
24b0c43c99
Reapply "[scudo] Make local cache be agnostic to the type of node in … (#68633)
…f… (#68626)

This reverts commit 8dd9615dfbd148993964ea6f2de6c6a94f49660c.

1. Fixed the ambiguous aliasing
2. Fixed the type conversion warning
2023-10-09 15:17:42 -07:00
ChiaHungDuan
8dd9615dfb
Revert "[scudo] Make local cache be agnostic to the type of node in f… (#68626)
…reelist (#67379)"

This reverts commit b9c6737ba7307308ecb6ec4cecc4c07e48e7c141.
2023-10-09 13:54:27 -07:00
ChiaHungDuan
b9c6737ba7
[scudo] Make local cache be agnostic to the type of node in freelist (#67379)
This change moves the `TransferBatch` and `BatchGroup` out of
SizeClassAllocatorLocalCache. It allows us that the node in freelist can
store more blocks instead of depending on the number of blocks cached.

That means we will be able to store more blocks in each node of freelist
and therefore reduce the memory used by BatchClass (with little
performance overhead). Note that we haven't enabled that in this patch.
This is the first step of this transition.
2023-10-09 13:11:55 -07:00
Chia-hung Duan
ba7cb620ac [scudo] Support dumping fragmentation data in SizeClassAllocator32
This is the same fragmentation data as in SizeClassAllocator64.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D158454
2023-08-24 16:17:09 +00:00
Chia-hung Duan
fe0cb7b1ee [scudo] Move the blocks marking logic into markFreeBlocks()
This change is only in SizeClassAllocator32. SizeClassAllocator64 has
it implemented.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D158455
2023-08-24 16:17:09 +00:00
Chia-hung Duan
56241b65ce [scudo] Move the decision making of page release to hasChanceToReleasePages()
This change is only in SizeClassAllocator32. SizeClassAllocator64 has
it implemented.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D158456
2023-08-24 16:17:08 +00:00
Chia-hung Duan
5f771c9936 [scudo] Support dumping fragmentation data in SizeClassAllocator64
This tells the number of pages that still have blocks in-used, i.e.,
those pages can't do releaseToOSMaybe(). Along with the information of
getStats() and RSS usage from the system (like smaps), we can tell if
the heuristic in releaseToOSMaybe() works well in certain scenarios.

Here's the sample output:
```
    Fragmentation Stats: SizeClassAllocator64: page size = 4096
      01 (    32): inuse/total blocks:    275/   416 inuse/total pages:      4/     4 inuse bytes:     16K
      02 (    48): inuse/total blocks:    182/   312 inuse/total pages:      4/     4 inuse bytes:     16K
      03 (    64): inuse/total blocks:    169/   312 inuse/total pages:      5/     5 inuse bytes:     20K
      ...
      32 (  1040): inuse/total blocks:     90/   152 inuse/total pages:     37/    39 inuse bytes:    148K
      33 (  1168): inuse/total blocks:    136/   232 inuse/total pages:     64/    67 inuse bytes:    256K
      34 (  1296): inuse/total blocks:    138/   226 inuse/total pages:     68/    72 inuse bytes:    272K
      35 (  1424): inuse/total blocks:    140/   232 inuse/total pages:     78/    81 inuse bytes:    312K
      36 (  1552): inuse/total blocks:    157/   248 inuse/total pages:     89/    94 inuse bytes:    356K
      ...
```

SizeClassAllocator32 requires further refactoring to support this.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D158225
2023-08-21 17:26:42 +00:00
Chia-hung Duan
e0105374f7 [scudo] Skip visiting all free blocks if grouping is disabled
This is only applied to SizeClassAllocator64 which has single region.
In SizeClassAllocator32, the region size has to be equal to the group
size.

Differential Revision: https://reviews.llvm.org/D156740
2023-07-31 22:20:18 +00:00
Chia-hung Duan
316ca42a65 [scudo] Print PushedBytesDelta in getStats()
This gives a hint of potential bytes to release. Also remove the RSS
which is not supported yet. Will add it back when it's available.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D154551
2023-07-10 21:29:18 +00:00
Chia-hung Duan
399eb5632f [scudo] Explicit casting for u16 arithmetic operation
This fixes the werror from https://lab.llvm.org/buildbot/#/builders/165/builds/38829

Reviewed By: enh

Differential Revision: https://reviews.llvm.org/D154733
2023-07-07 19:58:02 +00:00
Chia-hung Duan
1ff3a5d9bb [scudo] Allow pushing single block to the freelist of BatchClass
This CL removes the restriction that pushing blocks into BatchClassId
can only be done when freelist is not empty. Without this constraint,
BatchClassId is also available for gathering blocks into groups.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D153492
2023-07-07 17:27:48 +00:00
Chia-hung Duan
bce8c9e3d7 [scudo] Try to release pages after unlocking the TSDs
This increases the parallelism and the usage of TSDs

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D152988
2023-07-06 17:40:26 +00:00
Chia-hung Duan
531f90acc5 [scudo] Verify the size of free blocks in primary allocator
When all the blocks (local caches are included) are freed, the size of
free blocks should be equal to `AllocatedUser`.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D152769
2023-07-06 17:40:25 +00:00
Chia-hung Duan
18207dbc3a [scudo] update Pushedblocks/PoppedBlocks in Impl functions
Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D152420
2023-06-23 18:53:22 +00:00
Chia-hung Duan
5beaa7339f [scudo] Group poppedBlocks/pushedBlocks into BlocksInfo (NFC)
Create a new BlocksInfo to contain a list of blocks, poppedBlocks and
pushedBlocks. This is the preparation of adding new lock for operations
on freelist.

Differential Revision: https://reviews.llvm.org/D149143
2023-06-15 22:38:48 +00:00
Chia-hung Duan
2f08a08f61 [scudo] Support importing custom configuration
To define custom allocation, you only need to put the configuration in
custom_scudo_config.h and define two required aliases, then you will be
switched to the customized config and the tests will also run with your
configuration.

In this CL, we also have a minor refactor the structure of
configuration. Now the essential fields are put under the associated
hierarchy and which will make the defining new configuration easier.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D150481
2023-06-02 16:28:00 +00:00
Chia-hung Duan
5172beb4fa [scudo] Release pages of larger block more frequently
Release pages for large block (size greater than a page) is faster than
the small blocks. Besides, larger blocks are supposed not to be used
so often like smaller blocks which means we may hold several pages used
by large block and rarely get chance to release them if there's no
explicit M_PURGE call. Therefore, relax the release-interval condition
for large block.

This also fixes the assumption that FORCE_ALL should always try page
release.

Differential Revision: https://reviews.llvm.org/D151290
2023-05-31 20:14:04 +00:00
Chia-hung Duan
11ea40cff5 [scudo] releaseToOSMaybe can fail if it can't allocate PageMap
PageMap is allocated with MAP_ALLOWNOMEM if there's no static buffer
left. So it can be failed and return nullptr without any assertion
triggered. Instead of crashing in the releaseToOSMaybe in the middle,
just return and let the program handles the page failure.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D151379
2023-05-25 18:07:55 +00:00
Chia-hung Duan
0d45bbcb60 [scudo] Skip releaseToOSMaybe if there's no byte in freelist
In primary32, the unused region will have max/min region index with 0
value and it's an invalid index. Skip releaseToOSMaybe in both primary32
and primary64 even it's M_PURGE_ALL.

Differential Revision: https://reviews.llvm.org/D150243
2023-05-10 20:20:17 +00:00
Chia-hung Duan
e74834f9bb [scudo] Use bytes-in-freelist as a hint of page release
Tracking the pushed bytes between to releaseToOSMaybe calls may lead to
a overestimated case that if we do malloc 2KB -> free 2KB -> malloc 2KB
-> free 2KB, we may think we have released 4KB but it only releases 2KB
actually. Switch to use bytes-in-freelist excludes more cases that can't
release the pages

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D146400
2023-03-24 17:10:49 +00:00
Christopher Ferris
8aaefa9243 [scudo] Add a method to force release everything.
The force flag to releaseToOSMaybe does not release everything
since it is an expensive operation. Modify the release flag to
have three states: normal, force, forceall. Force behaves the same
as setting Force to true from before this change. Forceall will
release everything regardless of how much time it takes, or
how much there is to release.

In addition, add a new mallopt that will call the release function
with the forceall flag set.

Reviewed By: Chia-hungDuan

Differential Revision: https://reviews.llvm.org/D146106
2023-03-15 17:29:19 -07:00
Christopher Ferris
32be3405f5 [scudo] Add a fast get time version.
On Android, the _COARSE version of clock_gettime is about twice as fast.
Therefore, add a getMonotonicTimeFast function that is used in the
releaseToOSMaybe functions.

Reviewed By: Chia-hungDuan

Differential Revision: https://reviews.llvm.org/D145636
2023-03-09 14:12:53 -08:00
Chia-hung Duan
e4efa88538 [scudo] Slightly improve the handling of last block in a region
Instead of going through all those trailing blocks, just count the
number and increase the counter at once.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D145419
2023-03-09 16:18:00 +00:00
Chia-hung Duan
657d297a92 [scudo] Simplify markFreeBlocks
With memory group, we always mark the free blocks from the same region.
Therefore, we don't need to calculate the offset from base and determine
the region index. Also improve the way we deal with the last block in
the region so that the loop body is simpler.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D143303
2023-03-07 21:55:04 +00:00
Chia-hung Duan
a6e3bb9bfb [scudo] Make the boundary of memory group aligned with region begin
This alignment guarantee enables simpler group range check while page
releasing and a potential optimization which is, now all the pointers
from the same group are also inth same region, that means the complexity
in markFreeBlocks() can be reduced as well.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D142931
2023-03-06 19:26:40 +00:00
Chia-hung Duan
5b9d6097e7 Reland D144920 "[scudo] Only prepare PageMap entry for partial region
This reverts commit daaef4c49954cb04ea1831615e0876865a29a08a.

Differential Revision: https://reviews.llvm.org/D144920
2023-03-06 16:38:18 +00:00
Chia-hung Duan
daaef4c499 Revert "Revert "Revert "[scudo] Only prepare PageMap entry for partial region"""
ScudoReleaseTest.ReleaseFreeMemoryToOSAndroid failed on Fuchsia

This reverts commit c6ef6bbd8d964028ee6c2f03441604d7a7ba5375.
2023-03-02 20:37:55 +00:00
Chia-hung Duan
c6ef6bbd8d Revert "Revert "[scudo] Only prepare PageMap entry for partial region""
Fixed the bug in merging BatchGroups back to the FreeList. Added DCHECKs
to ensure the order of BatchGroups

This reverts commit 387452ec591c81def6d8869b23c2ab2f1c56f999.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D144920
2023-02-28 16:55:48 +00:00
Chia-hung Duan
387452ec59 Revert "[scudo] Only prepare PageMap entry for partial region"
This reverts commit 0a0b6fa4fbdf3bdeb300ddd58859f66b714b8bdf.
2023-02-27 21:12:28 +00:00
Chia-hung Duan
0a0b6fa4fb [scudo] Only prepare PageMap entry for partial region
This reduces the size of PageMap and we are more likely to use the
static local buffer. Note that now this is only supported for single
region case, i.e. on SizeClassAllocator64. For SizeClassAllocator32,
it needs a different way to save the PageMap.

Differential Revision: https://reviews.llvm.org/D142659
2023-02-27 18:49:10 +00:00
Chia-hung Duan
6860d1c6e4 [scudo] Accessing PossibleRegions requires lock
It is preferable to use `std::shared_mutex` style mutex. Will switch to
using it when it's available.

Differential Revision: https://reviews.llvm.org/D144691
2023-02-24 04:15:33 +00:00
Chia-hung Duan
a78805f3cb [scudo] Mark all blocks in a range without visiting each of them
When all the blocks in the group are known to be used, we should just
mark the pages in the range as all counted instead of visiting each of
them. This will reduce the time of marking free blocks especially for
smaller size class.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D141958
2023-02-23 23:26:03 +00:00
Chia-hung Duan
16bffb7e2f [scudo] Ensure all blocks are put in the correct group
While populating new blocks, we didn't always put them into their own
groups because that needs additional sort for an almost-sorted new
blocks array. However, ensuring all blocks are placed in the right group
enables the fast identifying of unused pages in a group by simply
accouting the number of free blocks are there. Therefore, this commit is
used to set up the invariant for future optimizations.

Differential Revision: https://reviews.llvm.org/D141957
2023-02-22 22:10:48 +00:00
Chia-hung Duan
dbf047454f [scudo] Fix the logic of pushing blocks of BatchClass
BatchClass is used to manage the free blocks for each size class. It's a
little bit tricky when it has to manage the free blocks of BatchClass.
In general, BatchClass block records the addresses of free blocks. In
order not to waste additional block to record the blocks in BatchClass,
it's self contained, i.e., it'll record its own address. The safety is
maintained by 2 preconditions,

  1. If a block is used to record other BatchClass blocks, it'll also
     record the address itself
  2. While allocating free blocks, all the recorded blcoks will be
     allocated together, which means there's no partial allocation

This CL fixes the violation of 1. and then we can push the free blocks
without having to push them in batches.

Differential Revision: https://reviews.llvm.org/D141956
2023-02-22 22:10:48 +00:00
Chia-hung Duan
a9269773eb [scudo] Improve the uses of roundUpTo/roundDownTo/isAligned
The implementations of those functions require the rounding target to be
power-of-two. It's better to add a debugging check to avoid misuse.
Besides, add a general verion of those three to accommadate non
power-of-two cases.

Also change the name to roundUp/roundDown/isAligned

Reviewed By: cferris, cryptoad

Differential Revision: https://reviews.llvm.org/D142658
2023-02-15 23:44:44 +00:00
Chia-hung Duan
94a391b949 [scudo] Calling iterateOverChunks requires holding lock
Ensure the allocator is disabled before visiting all chunks.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D142157
2023-02-15 23:44:44 +00:00