Skip to content

perf: avoid charging mmap aliases twice - #947

Closed
joshuaswarren wants to merge 3 commits into
integration/aurorafrom
perf/mmap-alias-accounting-pr
Closed

joshuaswarren wants to merge 3 commits into
integration/aurorafrom
perf/mmap-alias-accounting-pr

Conversation

@joshuaswarren

Copy link
Copy Markdown
Contributor

Problem

Repeated normal-ring and AUX mmap aliases share existing pages but are charged again; only the final close removes the original charge. A failed alias mapping can leave an extra charge as well.

Change

Charge each normal ring or AUX allocation once, retaining the existing alias validation and refcount paths. A separate source-level host regression exercises duplicate and failed mappings, metadata-only rings, and partial/full limit spills.

Verification

  • Independent review confirmed the reported leak and reviewed the fix.
  • Corrected host harness: 26/26 single-user, single-mm cases pass; the unmodified target fails the alias cases.
  • Focused x86_64 defconfig kernel/events/core.o build passed.

Limits

The host harness stubs kernel plumbing and does not establish in-kernel runtime, concurrency, cross-user or cross-mm behavior. Independent review identified separate pre-existing cross-user AUX and cross-mm final-close accounting concerns; those are not addressed here. No hardware test.

Draft pending commit DCO sign-off and maintainer review.

Charge ring and AUX pages only on first allocation; aliases share the existing charge and final close releases it. Verified with source-level lifecycle regressions and a focused core.o build.
Exercise duplicate and failed mappings, metadata-only rings and real limit splitting using extracted kernel source. Host regression passes on the fix and fails on the unfixed base.

@chriskearney chriskearney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No correctness blocker found in the two accounting changes at 4b2b95f. Removing the normal-alias addition and moving AUX charging into the first-allocation branch matches the existing once-per-buffer final-close uncharge. Size/offset validation, allocation-limit checks, refcount updates, and map_range failure cleanup remain intact. The accounting call also remains under the same AUX mutex scope.

I independently reproduced the supplied source-level harness: 26/26 pass at this head; the exact base 1d76233 fails 16/26 cases, including duplicate and failed aliases. A local test-only variant also passes 26/26 when the original mapping closes before its aliases and when the normal ring VMA closes while AUX remains mapped. This is extracted-code host validation with stubbed plumbing, not a kernel runtime/concurrency test; I did not reproduce the reported core.o build.

Non-blocking harness follow-ups:

  • Commit the alternate close-order cases so that ownership of the first mapping is not mistaken for ownership of the allocation lifetime.
  • Check perf_mlock and locked_vm separately in user_leaked/check_split/invariants_broken: summing them could hide equal and opposite corruption between the counters.
  • Before adding redirected-event list cases, make the perf_event.refcount stub match atomic_long_t. It is currently refcount_t containing an int, while atomic_long_inc_not_zero casts its address to long *, which can overwrite the adjacent mmap_count on a 64-bit host if that path executes.

The pre-existing owner mismatches remain separate: AUX first allocation can charge current_user while close subtracts rb->mmap_user; pinned_vm is subtracted from the final closer's mm rather than a retained charging mm. Neither is introduced or fixed by these two hunks, and the PR correctly excludes cross-user/cross-mm qualification. This review does not clear the draft's outstanding DCO requirement.

Check both user counters separately, exercise alternate ring and AUX close orders, and match the kernel event reference type. The 26-case fixed harness passes and the unfixed base retains 16 expected failures.

Assisted-by: LLM
@joshuaswarren

joshuaswarren commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

[Joshua's agent] Thanks, Chris. Added your harness changes in new commit e2bf3e2 (no history rewrite): both original-first ring and ring-close-with-AUX-live orders are exercised; perf_mlock and locked_vm are checked independently, including failed paths; and the event refcount stub now uses atomic_long_t.

Re-ran the source-level harness: this head passes 26/26 (exit 0), while the exact base 1d76233 fails the expected 16 alias cases (exit 1). Scope remains single-user, single-mm and single-threaded with mocked kernel plumbing; the inherited cross-user/cross-mm issues are separate. Draft remains open for review and human DCO sign-off.

@chriskearney chriskearney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes at e2bf3e2 address all three harness points from my review. The diff from 4b2b95f changes only the test: normal-ring original-first close and ring-close-with-AUX-live are exercised, the two user counters are checked separately (including failed paths), and perf_event.refcount now has the kernel's atomic_long_t shape.

Independently reran the updated harness: this head passes 26/26 scenarios (exit 0); exact base 1d76233 fails the same 16 alias scenarios (exit 1). The successful run executes nine original-first ring lifecycles and three ring-close-with-AUX-live lifecycles in addition to the existing close orders.

I am happy with this head for the stated source-level, single-user/single-mm fix; no remaining correctness objection from this review. The kernel changes are unchanged from the reviewed 4b2b95f. The runtime/concurrency and inherited cross-user/cross-mm limitations still apply. This is a COMMENT review, with no kernel boot/build by me and no DCO sign-off supplied on anyone else's behalf; the squashed replacement still needs Joshua's own sign-off and the normal maintainer process.

@joshuaswarren

joshuaswarren commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

[Joshua's agent] Chris, thank you for reviewing e2bf3e2. The unsigned replacement is #948. Its initial squash commit 85b4a97 has tree 375a6a354713d4fb957965e797e986365b888b4b, identical to this reviewed head. Follow-up commit 5355437 registers the host test, so the current #948 head has a different tree and needs its own review. The replacement stays draft pending Joshua's personal DCO sign-off and the normal maintainer process. This thread remains open.

@joshuaswarren

Copy link
Copy Markdown
Contributor Author

[Joshua's agent] Closing in favor of #948: its first commit has the tree of the head reviewed here (e2bf3e2), and it adds the host-test registration. Review continues on #948.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants