-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
gh-144319: Fix huge page leak in datastack chunk allocator #147963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Fix a bug that could cause applications with specific allocation patterns to | ||
| leak memory via Huge Pages if compiled with Huge Page support. Patch by | ||
| Pablo Galindo |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1433,16 +1433,30 @@ tstate_is_alive(PyThreadState *tstate) | |||||
| // lifecycle | ||||||
| //---------- | ||||||
|
|
||||||
| /* When huge pages are enabled, _PyObject_VirtualAlloc may back small requests | ||||||
| * with a full 2MB huge page. We must pass the *mapped* size (not the | ||||||
| * requested size) to _PyObject_VirtualFree, otherwise munmap receives a | ||||||
| * sub-hugepage length and fails with EINVAL, leaking the huge page. */ | ||||||
| #if defined(PYMALLOC_USE_HUGEPAGES) && defined(__linux__) | ||||||
| # define HUGEPAGE_SIZE (2U << 20) /* 2 MB */ | ||||||
| # define ROUND_UP_HUGEPAGE(n) \ | ||||||
| (((n) + HUGEPAGE_SIZE - 1) & ~(HUGEPAGE_SIZE - 1)) | ||||||
| #else | ||||||
| # define ROUND_UP_HUGEPAGE(n) (n) | ||||||
| #endif | ||||||
|
|
||||||
|
|
||||||
| static _PyStackChunk* | ||||||
| allocate_chunk(int size_in_bytes, _PyStackChunk* previous) | ||||||
| { | ||||||
| assert(size_in_bytes % sizeof(PyObject **) == 0); | ||||||
| _PyStackChunk *res = _PyObject_VirtualAlloc(size_in_bytes); | ||||||
| size_t mapped_size = ROUND_UP_HUGEPAGE(size_in_bytes); | ||||||
| _PyStackChunk *res = _PyObject_VirtualAlloc(mapped_size); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, why using The comment even states: Line 33 in 4497cf3
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes but I want to preserve the original intent of the code for the minimum bugfix. I haven't think of the subtle consequences of just getting a malloc chunk yet....
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see! Does Line 1010 in 4497cf3
|
||||||
| if (res == NULL) { | ||||||
| return NULL; | ||||||
| } | ||||||
| res->previous = previous; | ||||||
| res->size = size_in_bytes; | ||||||
| res->size = mapped_size; | ||||||
| res->top = 0; | ||||||
| return res; | ||||||
| } | ||||||
|
|
@@ -3082,7 +3096,7 @@ push_chunk(PyThreadState *tstate, int size) | |||||
| &tstate->datastack_chunk->data[0]; | ||||||
| } | ||||||
| tstate->datastack_chunk = new; | ||||||
| tstate->datastack_limit = (PyObject **)(((char *)new) + allocate_size); | ||||||
| tstate->datastack_limit = (PyObject **)(((char *)new) + new->size); | ||||||
| // When new is the "root" chunk (i.e. new->previous == NULL), we can keep | ||||||
| // _PyThreadState_PopFrame from freeing it later by "skipping" over the | ||||||
| // first element: | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
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.
Maybe it could be a helper instead, leveraging
cpython/Objects/obmalloc.c
Line 546 in 4497cf3
_PyObject_VirtualAllocRounded, or similar