xref: /PHP-8.4/ext/opcache/shared_alloc_posix.c (revision a4674654)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | https://www.php.net/license/3_01.txt                                 |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Stanislav Malyshev <stas@zend.com>                          |
18    |          Dmitry Stogov <dmitry@php.net>                              |
19    +----------------------------------------------------------------------+
20 */
21 
22 #include "zend_shared_alloc.h"
23 
24 #ifdef USE_SHM_OPEN
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <sys/mman.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 
34 typedef struct  {
35     zend_shared_segment common;
36     int shm_fd;
37 } zend_shared_segment_posix;
38 
create_segments(size_t requested_size,zend_shared_segment_posix *** shared_segments_p,int * shared_segments_count,const char ** error_in)39 static int create_segments(size_t requested_size, zend_shared_segment_posix ***shared_segments_p, int *shared_segments_count, const char **error_in)
40 {
41 	zend_shared_segment_posix *shared_segment;
42 	char shared_segment_name[sizeof("/ZendAccelerator.") + 20];
43 	int shared_segment_flags = O_RDWR|O_CREAT|O_TRUNC;
44 	mode_t shared_segment_mode = 0600;
45 
46 #if defined(HAVE_SHM_CREATE_LARGEPAGE)
47 	/**
48 	 * architectures have 3 entries max and we are interested
49 	 * from the second offset minimum to be worthy creating
50 	 * a special shared segment tagged as 'large'.
51 	 * only then amd64/i386/arm64 and perharps risc64*
52 	 * archs are on interest here.
53 	 */
54 	size_t i, shared_segment_sizes = 0, shared_segment_lg_index = 0;
55 	size_t shared_segment_sindexes[3] = {0};
56 	const size_t entries = sizeof(shared_segment_sindexes) / sizeof(shared_segment_sindexes[0]);
57 
58 	shared_segment_sizes = getpagesizes(shared_segment_sindexes, entries);
59 
60 	if (shared_segment_sizes > 0) {
61 		for (i = shared_segment_sizes - 1; i >= 0; i --) {
62 			if (shared_segment_sindexes[i] != 0 &&
63 			    !(requested_size % shared_segment_sindexes[i])) {
64 				shared_segment_lg_index = i;
65 				break;
66 			}
67 		}
68 	}
69 #endif
70 
71 	*shared_segments_count = 1;
72 	*shared_segments_p = (zend_shared_segment_posix **) calloc(1, sizeof(zend_shared_segment_posix) + sizeof(void *));
73 	if (!*shared_segments_p) {
74 		*error_in = "calloc";
75 		return ALLOC_FAILURE;
76 	}
77 	shared_segment = (zend_shared_segment_posix *)((char *)(*shared_segments_p) + sizeof(void *));
78 	(*shared_segments_p)[0] = shared_segment;
79 
80 	snprintf(shared_segment_name, sizeof(shared_segment_name), "/ZendAccelerator.%d", getpid());
81 #if defined(HAVE_SHM_CREATE_LARGEPAGE)
82 	if (shared_segment_lg_index > 0) {
83 		shared_segment->shm_fd =  shm_create_largepage(shared_segment_name, shared_segment_flags, shared_segment_lg_index, SHM_LARGEPAGE_ALLOC_DEFAULT, shared_segment_mode);
84 		if (shared_segment->shm_fd != -1) {
85 			goto truncate_segment;
86 		}
87 	}
88 #endif
89 
90 	shared_segment->shm_fd = shm_open(shared_segment_name, shared_segment_flags, shared_segment_mode);
91 	if (shared_segment->shm_fd == -1) {
92 		*error_in = "shm_open";
93 		return ALLOC_FAILURE;
94 	}
95 
96 #if defined(HAVE_SHM_CREATE_LARGEPAGE)
97 truncate_segment:
98 #endif
99 	if (ftruncate(shared_segment->shm_fd, requested_size) != 0) {
100 		*error_in = "ftruncate";
101 		shm_unlink(shared_segment_name);
102 		return ALLOC_FAILURE;
103 	}
104 
105 	shared_segment->common.p = mmap(0, requested_size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_segment->shm_fd, 0);
106 	if (shared_segment->common.p == MAP_FAILED) {
107 		*error_in = "mmap";
108 		shm_unlink(shared_segment_name);
109 		return ALLOC_FAILURE;
110 	}
111 	shm_unlink(shared_segment_name);
112 
113 	shared_segment->common.pos = 0;
114 	shared_segment->common.size = requested_size;
115 
116 	return ALLOC_SUCCESS;
117 }
118 
detach_segment(zend_shared_segment_posix * shared_segment)119 static int detach_segment(zend_shared_segment_posix *shared_segment)
120 {
121 	munmap(shared_segment->common.p, shared_segment->common.size);
122 	close(shared_segment->shm_fd);
123 	return 0;
124 }
125 
segment_type_size(void)126 static size_t segment_type_size(void)
127 {
128 	return sizeof(zend_shared_segment_posix);
129 }
130 
131 const zend_shared_memory_handlers zend_alloc_posix_handlers = {
132 	(create_segments_t)create_segments,
133 	(detach_segment_t)detach_segment,
134 	segment_type_size
135 };
136 
137 #endif /* USE_SHM_OPEN */
138