xref: /PHP-8.0/ext/opcache/shared_alloc_mmap.c (revision ae23852e)
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    | http://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_MMAP
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/mman.h>
31 
32 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
33 # define MAP_ANONYMOUS MAP_ANON
34 #endif
35 #if defined(MAP_ALIGNED_SUPER)
36 # define MAP_HUGETLB MAP_ALIGNED_SUPER
37 #endif
38 
create_segments(size_t requested_size,zend_shared_segment *** shared_segments_p,int * shared_segments_count,char ** error_in)39 static int create_segments(size_t requested_size, zend_shared_segment ***shared_segments_p, int *shared_segments_count, char **error_in)
40 {
41 	zend_shared_segment *shared_segment;
42   int flags = PROT_READ | PROT_WRITE;
43 	void *p;
44 #ifdef PROT_MPROTECT
45   flags |= PROT_MPROTECT(PROT_EXEC);
46 #endif
47 #ifdef PROT_MAX
48   flags |= PROT_MAX(PROT_READ | PROT_WRITE | PROT_EXEC);
49 #endif
50 #ifdef MAP_HUGETLB
51 	size_t huge_page_size = 2 * 1024 * 1024;
52 
53 	/* Try to allocate huge pages first to reduce dTLB misses.
54 	 * OSes has to be configured properly
55 	 * on Linux
56 	 * (e.g. https://wiki.debian.org/Hugepages#Enabling_HugeTlbPage)
57 	 * You may verify huge page usage with the following command:
58 	 * `grep "Huge" /proc/meminfo`
59 	 * on FreeBSD
60 	 * sysctl vm.pmap.pg_ps_enabled entry
61 	 * (boot time config only, but enabled by default on most arches).
62 	 */
63 	if (requested_size >= huge_page_size && requested_size % huge_page_size == 0) {
64 # if defined(__x86_64__) && defined(MAP_32BIT)
65 		/* to got HUGE PAGES in low 32-bit address we have to reserve address
66 		   space and then remap it using MAP_HUGETLB */
67 
68 		p = mmap(NULL, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS|MAP_32BIT, -1, 0);
69 		if (p != MAP_FAILED) {
70 			munmap(p, requested_size);
71 			p = (void*)(ZEND_MM_ALIGNED_SIZE_EX((ptrdiff_t)p, huge_page_size));
72 			p = mmap(p, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS|MAP_32BIT|MAP_HUGETLB|MAP_FIXED, -1, 0);
73 			if (p != MAP_FAILED) {
74 				goto success;
75 			} else {
76 				p = mmap(NULL, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS|MAP_32BIT, -1, 0);
77 				if (p != MAP_FAILED) {
78 					goto success;
79 				}
80 			}
81 		}
82 # endif
83 		p = mmap(0, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS|MAP_HUGETLB, -1, 0);
84 		if (p != MAP_FAILED) {
85 			goto success;
86 		}
87 	}
88 #elif defined(PREFER_MAP_32BIT) && defined(__x86_64__) && defined(MAP_32BIT)
89 	p = mmap(NULL, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS|MAP_32BIT, -1, 0);
90 	if (p != MAP_FAILED) {
91 		goto success;
92 	}
93 #endif
94 
95 	p = mmap(0, requested_size, flags, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
96 	if (p == MAP_FAILED) {
97 		*error_in = "mmap";
98 		return ALLOC_FAILURE;
99 	}
100 
101 success: ZEND_ATTRIBUTE_UNUSED;
102 	*shared_segments_count = 1;
103 	*shared_segments_p = (zend_shared_segment **) calloc(1, sizeof(zend_shared_segment) + sizeof(void *));
104 	if (!*shared_segments_p) {
105 		munmap(p, requested_size);
106 		*error_in = "calloc";
107 		return ALLOC_FAILURE;
108 	}
109 	shared_segment = (zend_shared_segment *)((char *)(*shared_segments_p) + sizeof(void *));
110 	(*shared_segments_p)[0] = shared_segment;
111 
112 	shared_segment->p = p;
113 	shared_segment->pos = 0;
114 	shared_segment->size = requested_size;
115 
116 	return ALLOC_SUCCESS;
117 }
118 
detach_segment(zend_shared_segment * shared_segment)119 static int detach_segment(zend_shared_segment *shared_segment)
120 {
121 	munmap(shared_segment->p, shared_segment->size);
122 	return 0;
123 }
124 
segment_type_size(void)125 static size_t segment_type_size(void)
126 {
127 	return sizeof(zend_shared_segment);
128 }
129 
130 zend_shared_memory_handlers zend_alloc_mmap_handlers = {
131 	create_segments,
132 	detach_segment,
133 	segment_type_size
134 };
135 
136 #endif /* USE_MMAP */
137