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
parent 7dcdafea00
commit d4cde60066
4 changed files with 11 additions and 35 deletions

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