Remove pidfd_open call handling and rely on PIDFD_SELF

This commit is contained in:
Slobodan Predolac 2025-08-26 15:15:08 -07:00 committed by Guangli Dai
parent 9442300cc3
commit 5d5f76ee01
4 changed files with 11 additions and 35 deletions

View file

@ -2638,10 +2638,11 @@ if test "x${je_cv_madvise}" = "xyes" ; then
dnl Check for process_madvise
JE_COMPILABLE([process_madvise(2)], [
#include <sys/pidfd.h>
#include <sys/syscall.h>
#include <unistd.h>
], [
syscall(SYS_process_madvise, 0, (void *)0, 0, 0, 0);
syscall(SYS_process_madvise, PIDFD_SELF, (void *)0, 0, 0, 0);
], [je_cv_process_madvise])
if test "x${je_cv_process_madvise}" = "xyes" ; then
AC_DEFINE([JEMALLOC_HAVE_PROCESS_MADVISE], [ ], [ ])

View file

@ -127,6 +127,5 @@ bool pages_boot(void);
void pages_set_thp_state(void *ptr, size_t size);
void pages_mark_guards(void *head, void *tail);
void pages_unmark_guards(void *head, void *tail);
void pages_postfork_child(void);
#endif /* JEMALLOC_INTERNAL_PAGES_EXTERNS_H */

View file

@ -4535,7 +4535,6 @@ jemalloc_postfork_child(void) {
malloc_mutex_postfork_child(tsd_tsdn(tsd), &arenas_lock);
tcache_postfork_child(tsd_tsdn(tsd));
ctl_postfork_child(tsd_tsdn(tsd));
pages_postfork_child();
}
/******************************************************************************/

View file

@ -621,7 +621,11 @@ pages_dodump(void *addr, size_t size) {
#ifdef JEMALLOC_HAVE_PROCESS_MADVISE
# include <sys/mman.h>
# include <sys/syscall.h>
static atomic_i_t process_madvise_pidfd = ATOMIC_INIT(-1);
#ifndef PIDFD_SELF
#define PIDFD_SELF -10000
#endif
static atomic_b_t process_madvise_gate = ATOMIC_INIT(true);
static bool
@ -650,33 +654,17 @@ pages_purge_process_madvise_impl(
if (!atomic_load_b(&process_madvise_gate, ATOMIC_RELAXED)) {
return true;
}
int pid_fd = atomic_load_i(&process_madvise_pidfd, ATOMIC_RELAXED);
while (pid_fd == -1) {
int newfd = (int) syscall(SYS_pidfd_open, getpid(), 0);
if (newfd == -1) {
return true;
}
if (!atomic_compare_exchange_strong_i(&process_madvise_pidfd,
&pid_fd, newfd,
ATOMIC_RELAXED,
ATOMIC_RELAXED)) {
/* Someone else set the fd, so we close ours */
assert(pid_fd != -1);
close(newfd);
} else {
pid_fd = newfd;
}
}
/*
* TODO: remove this save/restore of errno after supporting errno
* preservation for free() call properly.
*/
int saved_errno = get_errno();
size_t purged_bytes = (size_t)syscall(JE_SYS_PROCESS_MADVISE_NR, pid_fd,
(struct iovec *)vec, vec_len, MADV_DONTNEED, 0);
size_t purged_bytes = (size_t)syscall(JE_SYS_PROCESS_MADVISE_NR,
PIDFD_SELF, (struct iovec *)vec, vec_len, MADV_DONTNEED, 0);
if (purged_bytes == (size_t) -1) {
if (errno == EPERM || errno == EINVAL || errno == ENOSYS) {
if (errno == EPERM || errno == EINVAL || errno == ENOSYS
|| errno == EBADF) {
/* Process madvise not supported the way we need it. */
atomic_store_b(&process_madvise_gate, false,
ATOMIC_RELAXED);
@ -687,15 +675,6 @@ pages_purge_process_madvise_impl(
return purged_bytes != total_bytes;
}
void pages_postfork_child(void) {
/* Reset the file descriptor we inherited from parent process */
int pid_fd = atomic_load_i(&process_madvise_pidfd, ATOMIC_RELAXED);
if (pid_fd != -1) {
atomic_store_i(&process_madvise_pidfd, -1, ATOMIC_RELAXED);
close(pid_fd);
}
}
#else
static bool
@ -710,8 +689,6 @@ pages_purge_process_madvise_impl(
return true;
}
void pages_postfork_child(void) {}
#endif
bool