Skip to content

ext/opcache/zend_shared_alloc: use memfd for locking if available #10589

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion ext/opcache/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ if test "$PHP_OPCACHE" != "no"; then

fi

AC_CHECK_FUNCS([mprotect])
AC_CHECK_FUNCS([mprotect memfd_create])

AC_MSG_CHECKING(for sysvipc shared memory support)
AC_RUN_IFELSE([AC_LANG_SOURCE([[
Expand Down
19 changes: 19 additions & 0 deletions ext/opcache/zend_shared_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
+----------------------------------------------------------------------+
*/

#if defined(__linux__) && defined(HAVE_MEMFD_CREATE)
# ifndef _GNU_SOURCE
# define _GNU_SOURCE
# endif
# include <sys/mman.h>
#endif

#include <errno.h>
#include "ZendAccelerator.h"
#include "zend_shared_alloc.h"
Expand Down Expand Up @@ -81,6 +88,18 @@ void zend_shared_alloc_create_lock(char *lockfile_path)
zts_lock = tsrm_mutex_alloc();
#endif

#if defined(__linux__) && defined(HAVE_MEMFD_CREATE)
/* on Linux, we can use a memfd instead of a "real" file, so
* we can do this without a writable filesystem and without
* needing to clean up */
/* note: FreeBSD has memfd_create(), too, but fcntl(F_SETLKW)
* on it fails with EBADF, therefore we use this only on
* Linux */
lock_file = memfd_create("opcache_lock", MFD_CLOEXEC);
if (lock_file >= 0)
return;
#endif

snprintf(lockfile_name, sizeof(lockfile_name), "%s/%sXXXXXX", lockfile_path, SEM_FILENAME_PREFIX);
lock_file = mkstemp(lockfile_name);
if (lock_file == -1) {
Expand Down