Skip to content

Zend, ext/opcache: use PR_SET_VMA_ANON_NAME (Linux 5.17) #8234

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

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions Zend/zend_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "zend_operators.h"
#include "zend_multiply.h"
#include "zend_bitset.h"
#include "zend_mmap.h"
#include <signal.h>

#ifdef HAVE_UNISTD_H
Expand Down Expand Up @@ -475,6 +476,7 @@ static void *zend_mm_mmap(size_t size)
#endif
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, mflags, fd, 0);
if (ptr != MAP_FAILED) {
zend_mmap_set_name(ptr, size, "zend_alloc");
return ptr;
}
}
Expand All @@ -488,6 +490,7 @@ static void *zend_mm_mmap(size_t size)
#endif
return NULL;
}
zend_mmap_set_name(ptr, size, "zend_alloc");
return ptr;
#endif
}
Expand Down
3 changes: 3 additions & 0 deletions Zend/zend_fibers.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "zend_exceptions.h"
#include "zend_builtin_functions.h"
#include "zend_observer.h"
#include "zend_mmap.h"

#include "zend_fibers.h"
#include "zend_fibers_arginfo.h"
Expand Down Expand Up @@ -211,6 +212,8 @@ static zend_fiber_stack *zend_fiber_stack_allocate(size_t size)
return NULL;
}

zend_mmap_set_name(pointer, alloc_size, "zend_fiber_stack");

# if ZEND_FIBER_GUARD_PAGES
if (mprotect(pointer, ZEND_FIBER_GUARD_PAGES * page_size, PROT_NONE) < 0) {
zend_throw_exception_ex(NULL, 0, "Fiber stack protect failed: mprotect failed: %s (%d)", strerror(errno), errno);
Expand Down
44 changes: 44 additions & 0 deletions Zend/zend_mmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
+----------------------------------------------------------------------+
| This source file is subject to version 2.00 of the Zend license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.zend.com/license/2_00.txt. |
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@zend.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Max Kellermann <max.kellermann@ionos.com> |
+----------------------------------------------------------------------+
*/

#ifndef ZEND_MMAP_H
#define ZEND_MMAP_H

#include "zend_portability.h"

#ifdef __linux__
# include <sys/prctl.h>

/* fallback definitions if our libc is older than the kernel */
# ifndef PR_SET_VMA
# define PR_SET_VMA 0x53564d41
# endif
# ifndef PR_SET_VMA_ANON_NAME
# define PR_SET_VMA_ANON_NAME 0
# endif
#endif // __linux__

/**
* Set a name for the specified memory area.
*
* This feature requires Linux 5.17.
*/
static zend_always_inline void zend_mmap_set_name(const void *start, size_t len, const char *name)
{
#ifdef __linux__
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)start, len, (unsigned long)name);
Copy link
Member

Choose a reason for hiding this comment

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

Is it safe to call prctl with options unsupported by the kernel?

https://github.com/torvalds/linux/blob/7403e6d8263937dea206dd201fed1ceed190ca18/kernel/sys.c#L2615

Looks like the answer is yes and it'll just return -1 but the docs aren't very clear so I thought I'd ask.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it safe to call prctl with options unsupported by the kernel?

yes,, it will just return -1;

Copy link
Member

Choose a reason for hiding this comment

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

Not sure it warrants a new file for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made this a new file to reduce header dependencies, to avoid adding a dependency to sys/prctl.h to almost all sources.
The PHP code base is currently pretty clumsy about header dependencies; you have catch-all headers like zend_portability.h. I don't like that.
My coding style is to have many small and domain-specific headers with only the bare minimum of header-including-other-header. This speeds up compile times and improves code correctness.
But ... since this is not my project, I'll accept any style you desire, and will amend this PR.

Copy link
Member

@devnexen devnexen Jun 18, 2022

Choose a reason for hiding this comment

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

I feel if it had more it maybe it would worth it but that s personal opinion tough, other members might think differently. But what do you make about the other points I made below ?

Copy link
Member

Choose a reason for hiding this comment

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

I would generally agree that many of our files are too big and mixing too much code of different purposes can make it hard to understand the original intention of the file. But I don't know how the other maintainers feel.

Copy link
Member

Choose a reason for hiding this comment

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

Same sentiment here

#endif
}

#endif /* ZEND_MMAP_H */
3 changes: 3 additions & 0 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "zend_vm.h"
#include "zend_inheritance.h"
#include "zend_exceptions.h"
#include "zend_mmap.h"
#include "main/php_main.h"
#include "main/SAPI.h"
#include "main/php_streams.h"
Expand Down Expand Up @@ -2987,6 +2988,8 @@ static int accel_remap_huge_pages(void *start, size_t size, size_t real_size, co
# endif
}

zend_mmap_set_name(start, size, "zend_huge_code_pages");

if (ret == start) {
memcpy(start, mem, real_size);
mprotect(start, size, PROT_READ | PROT_EXEC);
Expand Down
6 changes: 5 additions & 1 deletion ext/opcache/jit/zend_jit_perf_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extern unsigned int thr_self(void);
#endif

#include "zend_elf.h"
#include "zend_mmap.h"

/*
* 1) Profile using perf-<pid>.map
Expand Down Expand Up @@ -171,8 +172,9 @@ static void zend_jit_perf_jitdump_open(void)
return;
}

const size_t page_size = sysconf(_SC_PAGESIZE);
jitdump_mem = mmap(NULL,
sysconf(_SC_PAGESIZE),
page_size,
PROT_READ|PROT_EXEC,
MAP_PRIVATE, jitdump_fd, 0);

Expand All @@ -182,6 +184,8 @@ static void zend_jit_perf_jitdump_open(void)
return;
}

zend_mmap_set_name(jitdump_mem, page_size, "zend_jitdump");

memset(&jit_hdr, 0, sizeof(jit_hdr));
jit_hdr.magic = ZEND_PERF_JITDUMP_HEADER_MAGIC;
jit_hdr.version = ZEND_PERF_JITDUMP_HEADER_VERSION;
Expand Down