GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-144319: Fix huge page leak in datastack chunk allocator by pablogsal · Pull Request #147963 · python/cpython

Conversation

@pablogsal

@pablogsal pablogsal commented Apr 1, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

The datastack chunk allocator (allocate_chunk in pystate.c) and other
small virtual-memory users such as the JIT tracer allocate through
_PyObject_VirtualAlloc() using the logical requested size. When huge pages
are enabled, the arena allocator may round those mappings up to the platform
huge page size, so freeing with the smaller requested size can make
munmap() fail with EINVAL and leak the mapping.

This change moves the rounding logic into _PyObject_VirtualAlloc() /
_PyObject_VirtualFree() so allocation and free always agree on the full
mapped size. Keeping it in the allocator layer also avoids a hardcoded 2 MB
assumption in pystate.c and fixes the JIT tracer allocation path at the
same time.

  • Issue: Add huge pages support for pymalloc #144319

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@pablogsal
pablogsal requested review from FFY00, ZeroIntensity and ericsnowcurrently as code owners April 1, 2026 11:50
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 1, 2026
@pablogsal
pablogsal requested a review from nascheme April 1, 2026 11:50
@pablogsal pablogsal changed the title Fix huge page leak in datastack chunk allocator gh-144319: Fix huge page leak in datastack chunk allocator Apr 1, 2026
@pablogsal pablogsal added the skip news label Apr 1, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Apr 1, 2026
Add huge pages support for pymalloc #144319
Open
@pablogsal pablogsal removed the skip news label Apr 1, 2026
@pablogsal
pablogsal force-pushed the fixes branch from b1a5ad8 to 9d62c27 Compare April 1, 2026 11:52
@pablogsal

pablogsal commented Apr 1, 2026

Copy link
Copy Markdown
Member Author

CC: @Fidget-Spinner

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

maurycy
maurycy reviewed Apr 1, 2026
View reviewed changes
Comment thread Python/pystate.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

maurycy
maurycy reviewed Apr 1, 2026
View reviewed changes
Comment thread Python/pystate.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/pystate.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner
Fidget-Spinner approved these changes Apr 3, 2026
View reviewed changes

@Fidget-Spinner Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Other than the stylistic stuff @maurycy pointed out, I think this looks fine.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Apr 3, 2026
@pablogsal
Fix huge page leak in datastack chunk allocator
b122fec
The original fix rounded datastack chunk allocations in pystate.c so that
_PyObject_VirtualFree() would receive the full huge page mapping size.

Change direction and move that logic into _PyObject_VirtualAlloc() and
_PyObject_VirtualFree() instead. The key invariant is that munmap() must see
the full mapped size, so alloc and free now apply the same platform-specific
rounding in the allocator layer.

This keeps _PyStackChunk bookkeeping in requested-size units, avoids a
hardcoded 2 MB assumption, and also covers other small virtual-memory users
such as the JIT tracer state allocation in optimizer.c.
@pablogsal ChatGPT Codex Connector

pablogsal commented Apr 4, 2026

Copy link
Copy Markdown
Member Author

I changed direction from the pystate.c-local roundup to an allocator-level fix. The important invariant here is that munmap() must always see the full mapped size, so I moved the rounding into _PyObject_VirtualAlloc() / _PyObject_VirtualFree() to keep alloc/free symmetric. That also covers the JIT tracer path instead of fixing only the datastack caller.

maurycy reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal
pablogsal force-pushed the fixes branch from 9d62c27 to b122fec Compare April 4, 2026 21:07
Fidget-Spinner
Fidget-Spinner approved these changes Apr 5, 2026
View reviewed changes
@pablogsal
pablogsal merged commit 77fc2f5 into python:main Apr 5, 2026
54 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal
pablogsal deleted the fixes branch April 5, 2026 15:29
@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 5, 2026
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@pablogsal @ljfp
pythongh-144319: Fix huge page leak in datastack chunk allocator (pyt…
5946e83
…hon#147963)

Fix huge page leak in datastack chunk allocator

The original fix rounded datastack chunk allocations in pystate.c so that
_PyObject_VirtualFree() would receive the full huge page mapping size.

Change direction and move that logic into _PyObject_VirtualAlloc() and
_PyObject_VirtualFree() instead. The key invariant is that munmap() must see
the full mapped size, so alloc and free now apply the same platform-specific
rounding in the allocator layer.

This keeps _PyStackChunk bookkeeping in requested-size units, avoids a
hardcoded 2 MB assumption, and also covers other small virtual-memory users
such as the JIT tracer state allocation in optimizer.c.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Fidget-Spinner Fidget-Spinner Fidget-Spinner approved these changes
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity
@FFY00 FFY00 Awaiting requested review from FFY00
@nascheme nascheme Awaiting requested review from nascheme
+1 more reviewer
@maurycy maurycy maurycy left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@pablogsal @maurycy @Fidget-Spinner
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.