Skip to content

Link the clickhouse binary with 2 MiB segment alignment on Linux x86-64 and AArch64 - #125949

Closed
groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:release-2mib-segment-alignment
Closed

groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:release-2mib-segment-alignment

Conversation

@groeneai

@groeneai groeneai commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #125278

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The clickhouse binary on Linux x86-64 and AArch64 is linked with 2 MiB segment alignment. Its load address is still random, but on Linux 5.10 and newer (6.11 for the static musl build) the position of the code relative to 2 MiB boundaries is now the same at every server start, so the speed of some queries no longer depends on the restart: on AArch64 some of them ran about 1.5 times slower after some restarts.

Description

Since the release binary became position independent (#117449, #119284), the kernel picks its load address in steps of the largest segment alignment: 64 KiB on AArch64 and 4 KiB on x86-64. So the address of the code modulo 2 MiB changes with every start. On AArch64 CI the slow starts follow it: for build 010807a35d71, json_to_string #1 was slow in 22 of 22 runs whose base fell in [0x140000, 0x190000) modulo 2 MiB (median 1.51x) and at most 1.05x in the other 99 (findings, decision).

Change: the clickhouse target is linked with -z max-page-size=0x200000, so every PT_LOAD is 2 MiB-aligned and the kernel (Linux 5.10+, 6.11+ for static musl) aligns the random base to 2 MiB. The binary stays ET_DYN with DF_1_PIE; other executables keep the default.

Cost: the base stays random over the same range, but in 2^19 positions instead of 2^24 (AArch64) and 2^28 (x86-64) on the CI kernels, i.e. 5 and 9 bits less entropy for the executable. Libraries, heap, stacks and mmap are unchanged. The x86-64 file grows by 2.6 MiB of unmapped padding (11 KB after zstd).

Tested on a local x86-64 release build: 20 of 20 starts at a 2 MiB-aligned base with 20 distinct bases (before: 0 of 20); remap_executable still puts all 470 MiB of code on huge pages; the stack trace and file offset tests pass. On AArch64 with the same-flags reference of #124676, PR Performance Comparison jobs whose two servers had the start of the code within 64 KiB of the same position modulo 2 MiB had 2.4 queries per job more than 10% apart (20 jobs), the others 5.2 (257 jobs, 2026-10-20); with this PR the position is the same in every job.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/125949]

groeneai and others added 2 commits October 15, 2026 04:27
Since the release binary became position independent (ClickHouse#117449, ClickHouse#119284), the
kernel draws its load address at every start in steps of the largest PT_LOAD
alignment: 64 KiB on AArch64 (lld default) and 4 KiB on x86-64. The address of
the code modulo 2 MiB therefore changed with every start, and on AArch64 CI
the per-start slow mode of some performance tests is a function of the build
and of that residue (e.g. json_to_string ClickHouse#1: 22 of 22 runs of one build with
the base in [0x140000, 0x190000) mod 2 MiB were 1.5x slower, the other 99 were
within 5%).

Link the `clickhouse` target with `-z max-page-size=0x200000` on Linux x86-64
and AArch64, so every PT_LOAD is 2 MiB aligned and the kernel aligns the random
base to 2 MiB: the layout modulo 2 MiB is fixed per build again while the
binary stays PIE with a random base (2^19 positions on the CI kernels instead
of 2^24 on AArch64 and 2^28 on x86-64). Other executables keep the default.
The file grows by about 2.6 MiB of unmapped padding.

Add a Compatibility check row that checks the largest PT_LOAD alignment of the
packaged binary, since nothing in the build fails when the linker silently
uses a smaller one.

Decided in ClickHouse#125278 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A larger PT_LOAD alignment makes the kernel align the random base to that
value, which would silently cost more ASLR entropy than intended, so the row
now fails on anything other than exactly 2 MiB. The CMake comment now says
that the base alignment applies on Linux 5.10 and newer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)

Two rounds, each with one cold review plus one independent model review.

Finding Verdict
⚠️ Round 1 (e854d9fa915d): the Compatibility check row accepted any largest PT_LOAD alignment of at least 2 MiB, so a larger one (which costs more ASLR entropy than stated) would pass Agreed, fixed: the row now requires exactly 2 MiB
💡 Round 1: base alignment to p_align holds only on Linux 5.10 and newer Agreed, fixed: the comment and the changelog entry say so
Round 2 (115a08c9f920): no findings -

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes, for the property this PR changes. Local x86-64 release build, 20 starts per binary under the same command: before, 0 of 20 starts had a 2 MiB-aligned base (19 distinct residues modulo 2 MiB); after, 20 of 20 (1 residue), with 20 distinct bases. The AArch64 slow mode itself needs an AArch64 host; its relation to the base modulo 2 MiB was measured on CI data.
b Root cause explained? A position independent executable is loaded at random & ~(largest p_align - 1). With 64 KiB (AArch64) or 4 KiB (x86-64) alignment, the address of the code modulo 2 MiB is random per start. On AArch64 some builds have hot loops that are slow at some of those positions, so some server starts are slow.
c Fix matches root cause? Yes. The 2 MiB alignment makes the base modulo 2 MiB fixed per build, as it was before PIE. The base stays random. Nothing in the perf harness changes.
d Test intent preserved / new tests added? The load address cannot be observed from SQL. Instead, a new Compatibility check row checks the largest PT_LOAD alignment of the packaged binary on amd64 and aarch64 (same shape as the PIE row). The existing stack trace and file offset tests pass on the new layout.
e Both directions demonstrated? The new row fails on the unchanged binary (['0x1000', '0x1000', '0x1000', '0x1000']) and passes on the fixed one. The runtime base alignment goes from 0/20 to 20/20.
f Fix is general across code paths? It covers x86-64 and AArch64, glibc -pie and musl -static-pie. Stripping for the packages keeps the alignment, and the self-extracting stub execvs the aligned binary. Other executables keep the default.
g Fix generalizes across inputs (params/datatypes/wrappers)? Kernels older than 5.10 ignore the alignment and load the binary as before. 2 MiB is a multiple of the 16 KiB and 64 KiB page sizes. The row's parser was checked on no LOAD line, mixed alignments, 64 KiB and 4 MiB; anything other than exactly 2 MiB fails the row.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, format or protocol change. The binary is still ET_DYN with DF_1_PIE. The file grows by 2.6 MiB of padding (11 KB after zstd), and that padding is not mapped into memory.
i Invariants and contracts preserved? Stack-trace file offsets stay correct (system.stack_trace, system.error_log, addressToLine, query profiler tests pass). remap_executable remaps all 470 MiB of code and puts it on huge pages. The PIE check still passes.

Session id: cron:clickhouse-impl-slot-4:20261015-034800

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2efed57]

Summary: ✅


AI Review

Summary

The PR links Linux x86-64 and AArch64 clickhouse PIE builds with 2 MiB segment alignment to stabilize code placement across restarts. No actionable findings remain in the current diff. The earlier musl kernel-version concern is corrected, and the subsequent AArch64 measurements address the earlier validation concern.

Missing context / blind spots
  • ⚠️ Current-head release, compatibility, and performance jobs have not yet published results. Runtime assessment relies on the validation recorded in the PR discussion, including the aligned AArch64 runs and matching-build comparison; no local build or query tests were run. Completed current-head CI would close this gap.
Final Verdict

Status: ✅ Approve. No review changes requested; normal CI completion remains pending.

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label Oct 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Algunenano, could you review this? It keeps the clickhouse binary PIE with a random base, but links it with -z max-page-size=0x200000 so the base moves in 2 MiB steps (5 bits less executable ASLR entropy on AArch64, 9 on x86-64), the option chosen on #125278.

@clickhouse-gh clickhouse-gh Bot added the comp-build-system Build system (CMake, compilation, platform builds). label Oct 15, 2026
@Algunenano Algunenano self-assigned this Oct 15, 2026
@Algunenano Algunenano added can be tested Allows running workflows for external contributors pr-performance Pull request with some performance improvements labels Oct 15, 2026
@Algunenano

Copy link
Copy Markdown
Member

@groeneai You keep linking all performance regression/changes to PIE without a real investigation. We had the same issues way before we enabled PIE, and in most of the cases it was related to things like tests using random, using LIMIT (which gives you a non deterministic subset), etc. Please investigate each case separately instead of saying it's PIE without any research.

Comment thread programs/CMakeLists.txt Resolved
Comment thread programs/CMakeLists.txt Resolved
@groeneai

Copy link
Copy Markdown
Collaborator Author

No, there is nothing more the linker can do: Linux has no ELF flag that requests huge pages (fs/binfmt_elf.c only reads p_align), so the 2 MiB alignment of this PR is the whole linker part. Whether the kernel then puts the code on huge pages depends on its version. I measured it with a probe that has 64 MiB of 2 MiB aligned text, on ext4 and XFS:

  • Linux 7.2 (in QEMU) removed CONFIG_READ_ONLY_THP_FOR_FS (torvalds/linux@e4df0f37bd95), so khugepaged and MADV_COLLAPSE handle executable files. With THP always, khugepaged moved all 64 MiB to huge pages within 2 minutes in an idle VM without any call; with madvise, one madvise(MADV_COLLAPSE) did it immediately, whatever was in the page cache.
  • Ubuntu's 6.17 on my machine and 6.8.0-1066-aws on the performance test runners (arm and amd, from the server logs of this PR) are built without CONFIG_READ_ONLY_THP_FOR_FS, so MADV_COLLAPSE returns EINVAL and khugepaged skips files. The code gets huge pages only where the page cache holds the file in 2 MiB folios, and that depends on how the file was written or first read: 0 of 64 MiB after 64 KiB or 1 MiB writes or a first run without madvise, 36 to 60 MiB on a cold page cache with MADV_HUGEPAGE, all 64 MiB after 4 MiB writes. On ext4 this needs 6.16 or newer.

So remap_executable can be replaced by one madvise(MADV_COLLAPSE) once the runners have Linux 7.2 or newer. Until then nothing else puts the code on huge pages there: remap_executable copies it into anonymous memory, which gets all of it every time (235 of 235 windows on my local build of this PR). It is off by default and the performance tests are its main user, so I would keep it until the runners are upgraded.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Result for the logical_functions_medium #7 row (arm red on #125401 at 15bc6e43e177): it follows the load address, but this PR alone would not have prevented it.

  • The PR build's server was slower in 7 of 7 runs (0.0778-0.0806 s against 0.0661-0.0684 s) and again after the dashboard restart (+13%), with equal rows, function calls and allocations and 16% more user CPU. The test's other zeros() queries were slower on that server too (doc: fix typo #4 +12.8%, Comparison with ElasticSearch #14 +9.7%), its Memory-table queries were not. FunctionExecute has been 9174 in every run since August, so no optimizer change removed the work.
  • Within one build, two starts at the same base modulo 2 MiB were more than 10% apart in 7 of 295 pairs, at different bases in 799 of 9279 (Sep 25 to Oct 16, bases from the jobs' *-smaps.txt). In master runs one side was more than 10% slower in 2 of 960 runs before Sep 9 and in 26 of 661 since Sep 15.
  • But both servers of the red job started at the same base modulo 2 MiB (0x180000). The PR build has its executable segment at file offset 0xab20000 and the official build at 0xac10000, so their code started at 0x0a0000 and 0x190000 modulo 2 MiB, and across builds that position is what decides this query: a PR build and an official build measured within 12 hours of each other were more than 10% apart in 92 of 1268 pairs at the same base residue, and in 23 of 1284 at the same code-start residue.

With this PR every start of a build lands at the same base residue, so a PR build and its reference keep their speed across restarts, but their code still starts at different positions. Two builds of the same flavour at the same base residue were more than 10% apart in 26 of 967 pairs, about as often as two starts of one build, so for this query the reference also has to be built like the PR build (#124676, or option 4 there).

@groeneai

Copy link
Copy Markdown
Collaborator Author

Result for the new_json_type #0-#2 row (arm slower verdicts since Oct 14 on 14 PRs and on master, e.g. the red on #126492 at bb36b12bf932): it follows the load address. This PR removes most of the start-to-start spread, and the reference also has to be built like the PR build.

  • In the red job the PR build's server was slower in 7 of 7 runs of Repair subtree2 #1 (0.402-0.434 s against 0.311-0.325 s) and again after the harness restart. #0 and Repair subtree2 #2 were slower on that server too (2.3x and +12%). Rows, bytes and memory usage were equal; only user CPU grew (+40%). amd had no such run in 10 days.
  • Within one build the speed follows the base modulo 2 MiB. Master 0a30525620, 92 starts: 14 of the 18 starts at 0x010000-0x050000 were 1.07-1.22x, and the 45 starts at 0x090000-0x170000 were at most 1.02x. Master 663364fe5e: the 4 starts at 0x040000-0x080000 were 1.22-1.31x, the other 21 at most 1.06x. Over 16 master builds (552 starts), two starts were more than 10% apart in 21 of 450 pairs at the same base residue and in 1906 of 12957 at different ones.
  • Which positions are slow changes from build to build (371b23a4b6, Oct 12: none in 44 starts). One side was more than 12% slower in 15 of 8890 arm runs from Aug 1 until PIE, 0 of 755 while it was reverted, 186 of 10556 from Sep 15 to Oct 14, and 172 of 1130 since Oct 14 18:00. The work did not change: fast starts used 226 ms of user CPU on Oct 11-12 and 229 ms on Oct 17-18, with the same counters.
  • In this PR's two arm runs the code started at a 2 MiB boundary and both PR servers were fast (0.320 and 0.325 s). But measured from the code start, the slow starts of PR builds sit at a median of 0x080000 and those of official builds at 0x050000 (Oct 14-18), so PR and official builds place this code differently. As for logical_functions_medium Error fetching data from materialized view #7, the reference has to be built like the PR build as well (Perf CI: compare PR builds with a master build compiled with the PR flags #124676, or option 4 there).

Bases come from the jobs' *-smaps.txt, timings from query_metric_runs_v1 (server_time, Oct 9-18).

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, let's answer the main question - does it make performance tests more stable?

@groeneai

Copy link
Copy Markdown
Collaborator Author

On AArch64 it does when both servers run the same kind of build, which is the case in master runs. In PR checks it does not by itself.

I took 6947 arm Performance Comparison jobs from Sep 26 to Oct 16. The load addresses of both servers come from the jobs' *-smaps.txt, and the per-query medians of the main run (before the confirmation restart) come from query_metric_runs_v1. For each job I counted the queries, out of about 630, whose two servers differed by more than 10% or 15%:

Code of the two servers, modulo 2 MiB Jobs Queries > 10% apart, per job (95% CI) > 15%
PR checks, at different positions (almost all of them today) 6084 5.65 2.67
PR checks, at the same position 167 4.23 (3.74-4.78) 2.02
PR checks, at the same base (what this PR gives them) 200 6.15 (5.59-6.71) 2.83
master runs, at different positions 676 5.12 2.31
master runs, at the same position 20 3.55 (2.75-4.40) 1.70

So when the code of both servers sits at the same position modulo 2 MiB, about a quarter fewer queries differ.

  • In master runs both servers are official builds, and their code starts at the same file offset modulo 2 MiB (653 of 696 jobs), so this PR puts them at the same position at every start.
  • In PR checks the reference is an official build, and the PR build's code starts at a different offset in 6250 of 6251 jobs (1.0 to 1.25 MiB apart in 6025). At the same base the code is still that far apart, and the count does not drop. With this PR alone the distance would also be the same after every restart, so the confirmation rerun could no longer clear a red that it causes. For PR checks the reference also has to be built like the PR build (Perf CI: compare PR builds with a master build compiled with the PR flags #124676).

The other ~4 queries per job that differ by more than 10% remain when the placement is equal, so placement does not explain them. On x86-64 I have not found a query whose speed follows the load address, so I don't expect a change there.

@Algunenano

Copy link
Copy Markdown
Member

@groeneai We keep circling around and it seems to me that the core issue we need to fix is in the PR you linked: #124676

We need to make sure that the binaries created by the PR and the binary it's compared against are equivalent. They need to use the same build flags (include debug and PGO). The question about what link segment we need to use seems secondary to that and I feel that it's not solving the general problem, just reducing it. There is no guarantee that both binaries will keep the same distance between 2 functions, since by definition they will be different. OTOH, it will undoubtfully reduce noise in the case where it's the same binary/code and when the code didn't change significantly.

@Algunenano

Copy link
Copy Markdown
Member

Marking as draft to compare after #124676

@Algunenano
Algunenano marked this pull request as draft October 19, 2026 12:00
@groeneai

Copy link
Copy Markdown
Collaborator Author

Agreed, the same-flags reference comes first. I'll rework #124676 as you described there: a separate release_performance_reference build whose flags and CMake options come from the same shared place as the PR build's, used as the reference by PR checks, while master runs keep comparing official vs official builds. Once it is merged, I'll compare again with this PR on top of it and post the result here.

@Algunenano
Algunenano marked this pull request as ready for review October 20, 2026 11:39
@Algunenano

Copy link
Copy Markdown
Member

@groeneai The perf reference binary changes have been merged. Can you verify if this PR is still necessary please? Also, we 100% do not need a check like process_segment_alignment_check. If we still need this PR please drop it, if not let's close this.

Not needed, as asked in review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Yes, it is still needed on AArch64. I measured the 277 AArch64 Performance Comparison jobs of PRs that used the new reference today:

  • The PR build and the reference now have the same code offset in 244 of 277 jobs (0 of the 180 jobs that still used the official reference), so the random load address is what still separates the two servers.
  • Jobs where the code of the two servers started at most 64 KiB apart modulo 2 MiB had 2.4 queries per job more than 10% apart (20 jobs, 16 PRs). The other jobs had 5.2 (257 jobs), or 4.6 compared within the same shards (stratified permutation p < 0.001).

With this PR the code starts at the same position in every job, once the reference is built with it too. On x86-64 I have no measurement showing a gain (40 jobs since the merge, none with matching positions, and at least 99% of the remapped code is on huge pages either way). If you prefer, I can limit the flag to AArch64 and keep the x86-64 ASLR entropy.

I dropped process_segment_alignment_check in 2efed57 and updated the description.

@Algunenano
Algunenano marked this pull request as draft October 20, 2026 12:55
@Algunenano

Copy link
Copy Markdown
Member

I have serious concerns about the security implications of this PR and the weaking of ASLR. We currently have some address oracles (working on fixing them, see #126748, #126760 and others) and this makes them way more practical to exploit.

I plan to work on addressing those issues first and then re-evaluate, as right now the impact of the 2 together seems too high.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Understood, I'll keep it as a draft and not push to it until you re-evaluate.

If you'd rather not touch the release binaries at all, the perf tests can get the same effect from the harness alone: compare.sh could start both servers under setarch -R (the job already runs with seccomp=unconfined), so their code starts at the same position as with this PR, and ASLR stays as it is everywhere else. I can send that as a separate PR if you want it.

@Algunenano

Copy link
Copy Markdown
Member

@groeneai Yes, that looks much more preferable and worth doing / testing.

We'd need to apply it to both performance_tests.py and compare.sh, and verify that the executable base of the 2 running servers is the same (and fail the run if not to alert and address it in the scripts).

@groeneai

Copy link
Copy Markdown
Collaborator Author

Acknowledged. I'm preparing a separate PR that starts both servers under setarch -R in compare.sh and performance_tests.py, checks that the executable base of the two running servers is the same, and fails the run when it is not. I'll post the link here.

@groeneai

Copy link
Copy Markdown
Collaborator Author

The separate PR is #126977: both launchers start the server under setarch <arch> -R, and the run fails when the two running servers have different executable bases.

@Algunenano

Copy link
Copy Markdown
Member

Closing in favor of #126977 for now. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-build-system Build system (CMake, compilation, platform builds). manual approve Manual approve required to run CI pr-build Pull request with build/testing/packaging improvement pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants