Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
0e3933d
gh-141770: Annotate anonymous mmap only for debug build
corona10 Nov 28, 2025
8b7023e
Apply annotation feature to modules
corona10 Nov 29, 2025
816d962
Add NEWS.d
corona10 Nov 29, 2025
979ce02
nit
corona10 Nov 29, 2025
b3d8547
Update Include/internal/pycore_pymem.h
corona10 Nov 29, 2025
0c5a495
Address code review
corona10 Nov 29, 2025
e757dd9
Address code review
corona10 Nov 29, 2025
cb9509b
Change annotation prefix
corona10 Nov 30, 2025
855742b
nit
corona10 Nov 30, 2025
32cc007
Address code review
corona10 Dec 1, 2025
49b4f95
Apply suggestions from code review
corona10 Dec 2, 2025
1b88b75
Address code review
corona10 Dec 2, 2025
e2eedbc
Address code review
corona10 Dec 2, 2025
a680677
test
corona10 Dec 3, 2025
8ec4d73
Add Include/internal/pycore_mmap.h
corona10 Dec 3, 2025
c0e08b6
fix
corona10 Dec 3, 2025
e66e650
Update PC
corona10 Dec 3, 2025
b0ca751
Update
corona10 Dec 3, 2025
c9138de
Update Whats News
corona10 Dec 3, 2025
8f77e0d
Merge remote-tracking branch 'upstream/main' into gh-141770
corona10 Dec 4, 2025
8706fce
Address code review
corona10 Dec 4, 2025
8ce8281
lint
corona10 Dec 4, 2025
d71a744
Update
corona10 Dec 4, 2025
4dd1ee4
Address code review
corona10 Dec 4, 2025
fee757e
Apply suggestions from code review
corona10 Dec 5, 2025
a240b3c
Update Objects/obmalloc.c
corona10 Dec 5, 2025
805c539
Address code review
corona10 Dec 5, 2025
795d31c
Update Misc/NEWS.d/next/Core_and_Builtins/2025-11-29-18-14-28.gh-issu…
corona10 Dec 5, 2025
be4d550
Address code review
corona10 Dec 5, 2025
290864a
Address code review
corona10 Dec 5, 2025
b287e0c
nit
corona10 Dec 5, 2025
1619f95
Update Misc/NEWS.d/next/Core_and_Builtins/2025-11-29-18-14-28.gh-issu…
corona10 Dec 6, 2025
a1ad592
Update Include/internal/pycore_mmap.h
corona10 Dec 8, 2025
e4e4835
Update Modules/_ctypes/malloc_closure.c
corona10 Dec 8, 2025
0429a0a
Update Include/internal/pycore_mmap.h
corona10 Dec 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Include/internal/pycore_pymem.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#if defined(Py_DEBUG) && defined(HAVE_PR_SET_VMA_ANON_NAME) && defined(__linux__)
#include <linux/prctl.h>
#include <sys/prctl.h>
#endif

// Try to get the allocators name set by _PyMem_SetupAllocators().
// Return NULL if unknown.
// Export for '_testinternalcapi' shared extension.
Expand Down Expand Up @@ -91,6 +96,18 @@ static inline int _PyMem_IsULongFreed(unsigned long value)
#endif
}

static inline int
_PyAnnotateMemoryMap(void *addr, size_t size, const char *name)
{
#if defined(Py_DEBUG) && defined(HAVE_PR_SET_VMA_ANON_NAME) && defined(__linux__)
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)addr, size, name);
// Ignore errno from prctl
// See: https://bugzilla.redhat.com/show_bug.cgi?id=2302746
errno = 0;
#endif
return 0;
}

extern int _PyMem_GetAllocatorName(
const char *name,
PyMemAllocatorName *allocator);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Annotate anonymous mmap usage in debug builds only when supported by the
Linux kernel. Patch by Donghee Na.
5 changes: 4 additions & 1 deletion Modules/_ctypes/malloc_closure.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# endif
#endif
#include "ctypes.h"
#include "pycore_obmalloc.h" // _PyAnnotateMemoryMap()

/* BLOCKSIZE can be adjusted. Larger blocksize will take a larger memory
overhead, but allocate less blocks from the system. It may be that some
Expand Down Expand Up @@ -74,14 +75,16 @@ static void more_core(void)
if (item == NULL)
return;
#else
size_t mem_size = count * sizeof(ITEM);
item = (ITEM *)mmap(NULL,
count * sizeof(ITEM),
mem_size,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS,
-1,
0);
if (item == (void *)MAP_FAILED)
return;
_PyAnnotateMemoryMap(item, mem_size, "cpython:more_core");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
_PyAnnotateMemoryMap(item, mem_size, "cpython:more_core");
_PyAnnotateMemoryMap(item, mem_size, "cpython:ctypes:malloc_closure");

#endif

#ifdef MALLOC_CLOSURE_DEBUG
Expand Down
2 changes: 2 additions & 0 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "pycore_abstract.h" // _Py_convert_optional_to_ssize_t()
#include "pycore_bytesobject.h" // _PyBytes_Find()
#include "pycore_fileutils.h" // _Py_stat_struct
#include "pycore_obmalloc.h" // _PyAnnotateMemoryMap()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include <stddef.h> // offsetof()
Expand Down Expand Up @@ -1951,6 +1952,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
_PyAnnotateMemoryMap(m_obj->data, map_size, "cpython:new_mmap_object");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
_PyAnnotateMemoryMap(m_obj->data, map_size, "cpython:new_mmap_object");
_PyAnnotateMemoryMap(m_obj->data, map_size, "cpython:mmap");

(If we allow users to set an annotation: this could be cpython:mmap:xxx?)

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.

I don't think _PyAnnotateMemoryMap should be used in this case at all:

  1. This function creates a memory mapping on behalf of the caller, not Python itself. So it's not really correct to attribute the memory region to Python.
  2. You can only name anonymous memory areas, but this region may not be anonymous.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's better to annotate this as allocated with the Python mmap module, rather than by something else (within CPython, or another library).

If the mmap is not anonymous, the annotation fails. Good.
If in the future, the kernel somehow allows annotating non-anonymous areas, this annotation would also work.

m_obj->access = (access_mode)access;
return (PyObject *)m_obj;
}
Expand Down
1 change: 1 addition & 0 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ _PyMem_ArenaAlloc(void *Py_UNUSED(ctx), size_t size)
if (ptr == MAP_FAILED)
return NULL;
assert(ptr != NULL);
_PyAnnotateMemoryMap(ptr, size, "cpython:PyMem_ArenaAlloc");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

obmalloc only uses mmap for arenas

Suggested change
_PyAnnotateMemoryMap(ptr, size, "cpython:PyMem_ArenaAlloc");
_PyAnnotateMemoryMap(ptr, size, "cpython:obmalloc");

return ptr;
#else
return malloc(size);
Expand Down
3 changes: 3 additions & 0 deletions Python/jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ jit_alloc(size_t size)
int prot = PROT_READ | PROT_WRITE;
unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0);
int failed = memory == MAP_FAILED;
if (!failed) {
_PyAnnotateMemoryMap(memory, size, "cpython:jit_alloc");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
_PyAnnotateMemoryMap(memory, size, "cpython:jit_alloc");
_PyAnnotateMemoryMap(memory, size, "cpython:jit");

}
#endif
if (failed) {
jit_error("unable to allocate memory");
Expand Down
2 changes: 2 additions & 0 deletions Python/perf_jit_trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "pycore_ceval.h" // _PyPerf_Callbacks
#include "pycore_frame.h"
#include "pycore_interp.h"
#include "pycore_obmalloc.h" // _PyAnnotateMemoryMap()
#include "pycore_runtime.h" // _PyRuntime

#ifdef PY_HAVE_PERF_TRAMPOLINE
Expand Down Expand Up @@ -1085,6 +1086,7 @@ static void* perf_map_jit_init(void) {
close(fd);
return NULL; // Memory mapping failed
}
_PyAnnotateMemoryMap(perf_jit_map_state.mapped_buffer, page_size, "cpython:perf_map_jit_init");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
_PyAnnotateMemoryMap(perf_jit_map_state.mapped_buffer, page_size, "cpython:perf_map_jit_init");
_PyAnnotateMemoryMap(perf_jit_map_state.mapped_buffer, page_size, "cpython:jit:perf_map");

#endif

perf_jit_map_state.mapped_size = page_size;
Expand Down
2 changes: 2 additions & 0 deletions Python/perf_trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ any DWARF information available for them).
#include "Python.h"
#include "pycore_ceval.h" // _PyPerf_Callbacks
#include "pycore_interpframe.h" // _PyFrame_GetCode()
#include "pycore_obmalloc.h" // _PyAnnotateMemoryMap()
#include "pycore_runtime.h" // _PyRuntime


Expand Down Expand Up @@ -290,6 +291,7 @@ new_code_arena(void)
perf_status = PERF_STATUS_FAILED;
return -1;
}
_PyAnnotateMemoryMap(memory, mem_size, "cpython:new_code_arena");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
_PyAnnotateMemoryMap(memory, mem_size, "cpython:new_code_arena");
_PyAnnotateMemoryMap(memory, mem_size, "cpython:perf_trampoline");

void *start = &_Py_trampoline_func_start;
void *end = &_Py_trampoline_func_end;
size_t code_size = end - start;
Expand Down
19 changes: 19 additions & 0 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -5578,6 +5578,13 @@ AC_CHECK_DECLS([UT_NAMESIZE],
[],
[@%:@include <utmp.h>])

AC_CHECK_DECLS([PR_SET_VMA_ANON_NAME],
[AC_DEFINE([HAVE_PR_SET_VMA_ANON_NAME], [1],
[Define if you have the 'PR_SET_VMA_ANON_NAME' constant.])],
[],
[@%:@include <linux/prctl.h>
@%:@include <sys/prctl.h>])

# check for openpty, login_tty, and forkpty

AC_CHECK_FUNCS([openpty], [],
Expand Down
7 changes: 7 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@
/* Define to 1 if you have the <db.h> header file. */
#undef HAVE_DB_H

/* Define to 1 if you have the declaration of 'PR_SET_VMA_ANON_NAME', and to 0
if you don't. */
#undef HAVE_DECL_PR_SET_VMA_ANON_NAME

/* Define to 1 if you have the declaration of 'RTLD_DEEPBIND', and to 0 if you
don't. */
#undef HAVE_DECL_RTLD_DEEPBIND
Expand Down Expand Up @@ -996,6 +1000,9 @@
/* Define if your compiler supports function prototype */
#undef HAVE_PROTOTYPES

/* Define if you have the 'PR_SET_VMA_ANON_NAME' constant. */
#undef HAVE_PR_SET_VMA_ANON_NAME

/* Define to 1 if you have the 'pthread_condattr_setclock' function. */
#undef HAVE_PTHREAD_CONDATTR_SETCLOCK

Expand Down
Loading