blob: eb4312b27258eb584be9f0c056ffa8e0e8b1eda6 [file] [log] [blame]
Damjan Marion01914ce2017-09-14 19:04:50 +02001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#define _GNU_SOURCE
17#include <stdlib.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <unistd.h>
21#include <sys/mount.h>
22#include <sys/mman.h>
23#include <fcntl.h>
24#include <linux/mempolicy.h>
25#include <linux/memfd.h>
26
27#include <vppinfra/clib.h>
28#include <vppinfra/mem.h>
Florin Corasd3e83a92018-01-16 02:40:18 -080029#include <vppinfra/time.h>
Damjan Marion01914ce2017-09-14 19:04:50 +020030#include <vppinfra/format.h>
31#include <vppinfra/clib_error.h>
32#include <vppinfra/linux/syscall.h>
33#include <vppinfra/linux/sysfs.h>
34
35#ifndef F_LINUX_SPECIFIC_BASE
36#define F_LINUX_SPECIFIC_BASE 1024
37#endif
38
39#ifndef F_ADD_SEALS
40#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
41#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
42
43#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
44#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
45#define F_SEAL_GROW 0x0004 /* prevent file from growing */
46#define F_SEAL_WRITE 0x0008 /* prevent writes */
47#endif
48
Dave Barach9466c452018-08-24 17:21:14 -040049u64
Florin Corasd3e83a92018-01-16 02:40:18 -080050clib_mem_vm_get_page_size (int fd)
Damjan Marion01914ce2017-09-14 19:04:50 +020051{
52 struct stat st = { 0 };
Chris Lukeb2bcad62017-09-18 08:51:22 -040053 if (fstat (fd, &st) == -1)
Damjan Marion01914ce2017-09-14 19:04:50 +020054 return 0;
Florin Corasd3e83a92018-01-16 02:40:18 -080055 return st.st_blksize;
56}
57
58int
59clib_mem_vm_get_log2_page_size (int fd)
60{
61 return min_log2 (clib_mem_vm_get_page_size (fd));
62}
63
Florin Corasb384b542018-01-15 01:08:33 -080064void
Florin Corasd3e83a92018-01-16 02:40:18 -080065clib_mem_vm_randomize_va (uword * requested_va, u32 log2_page_size)
66{
67 u8 bit_mask = 15;
68
69 if (log2_page_size <= 12)
70 bit_mask = 15;
71 else if (log2_page_size > 12 && log2_page_size <= 16)
72 bit_mask = 3;
73 else
74 bit_mask = 0;
75
Haiyang Tana5ab5032018-10-15 06:17:55 -070076 *requested_va +=
77 (clib_cpu_time_now () & bit_mask) * (1ull << log2_page_size);
Damjan Marion01914ce2017-09-14 19:04:50 +020078}
79
Damjan Marion1636b162018-10-19 12:54:42 +020080#ifndef MFD_HUGETLB
81#define MFD_HUGETLB 0x0004U
82#endif
83
84clib_error_t *
85clib_mem_create_hugetlb_fd (char *name, int *fdp)
86{
87 clib_error_t *err = 0;
88 int fd = -1;
89 static int memfd_hugetlb_supported = 1;
90 char *mount_dir;
91 char template[] = "/tmp/hugepage_mount.XXXXXX";
92 u8 *filename;
93
94 if (memfd_hugetlb_supported)
95 {
96 if ((fd = memfd_create (name, MFD_HUGETLB)) != -1)
97 goto done;
98
99 /* avoid further tries if memfd MFD_HUGETLB is not supported */
100 if (errno == EINVAL && strnlen (name, 256) <= 249)
101 memfd_hugetlb_supported = 0;
102 }
103
104 mount_dir = mkdtemp (template);
105 if (mount_dir == 0)
106 return clib_error_return_unix (0, "mkdtemp \'%s\'", template);
107
108 if (mount ("none", (char *) mount_dir, "hugetlbfs", 0, NULL))
109 {
110 rmdir ((char *) mount_dir);
111 err = clib_error_return_unix (0, "mount hugetlb directory '%s'",
112 mount_dir);
113 }
114
115 filename = format (0, "%s/%s%c", mount_dir, name, 0);
116 fd = open ((char *) filename, O_CREAT | O_RDWR, 0755);
117 umount2 ((char *) mount_dir, MNT_DETACH);
118 rmdir ((char *) mount_dir);
119
120 if (fd == -1)
121 err = clib_error_return_unix (0, "open");
122
123done:
124 if (fd != -1)
125 fdp[0] = fd;
126 return err;
127}
128
Damjan Marion01914ce2017-09-14 19:04:50 +0200129clib_error_t *
130clib_mem_vm_ext_alloc (clib_mem_vm_alloc_t * a)
131{
132 int fd = -1;
133 clib_error_t *err = 0;
134 void *addr = 0;
135 u8 *filename = 0;
Damjan Marion7b185362018-03-04 16:41:35 +0100136 int mmap_flags = 0;
Damjan Marion01914ce2017-09-14 19:04:50 +0200137 int log2_page_size;
138 int n_pages;
139 int old_mpol = -1;
Dave Barach9466c452018-08-24 17:21:14 -0400140 long unsigned int old_mask[16] = { 0 };
Damjan Marion01914ce2017-09-14 19:04:50 +0200141
142 /* save old numa mem policy if needed */
143 if (a->flags & (CLIB_MEM_VM_F_NUMA_PREFER | CLIB_MEM_VM_F_NUMA_FORCE))
144 {
145 int rv;
Damjan Marion2e172ea2018-01-03 15:48:34 +0000146 rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1,
147 0, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200148
149 if (rv == -1)
150 {
Damjan Marion2e172ea2018-01-03 15:48:34 +0000151 if (a->numa_node != 0 && (a->flags & CLIB_MEM_VM_F_NUMA_FORCE) != 0)
Damjan Marion01914ce2017-09-14 19:04:50 +0200152 {
153 err = clib_error_return_unix (0, "get_mempolicy");
154 goto error;
155 }
156 else
157 old_mpol = -1;
158 }
159 }
160
Damjan Marion7b185362018-03-04 16:41:35 +0100161 if (a->flags & CLIB_MEM_VM_F_LOCKED)
162 mmap_flags |= MAP_LOCKED;
163
Damjan Marion01914ce2017-09-14 19:04:50 +0200164 /* if we are creating shared segment, we need file descriptor */
165 if (a->flags & CLIB_MEM_VM_F_SHARED)
166 {
Damjan Marion7b185362018-03-04 16:41:35 +0100167 mmap_flags |= MAP_SHARED;
Damjan Marion01914ce2017-09-14 19:04:50 +0200168 /* if hugepages are needed we need to create mount point */
169 if (a->flags & CLIB_MEM_VM_F_HUGETLB)
170 {
Damjan Marion1636b162018-10-19 12:54:42 +0200171 if ((err = clib_mem_create_hugetlb_fd (a->name, &fd)))
172 goto error;
Damjan Marion01914ce2017-09-14 19:04:50 +0200173
Damjan Marion01914ce2017-09-14 19:04:50 +0200174 mmap_flags |= MAP_LOCKED;
175 }
176 else
177 {
178 if ((fd = memfd_create (a->name, MFD_ALLOW_SEALING)) == -1)
179 {
180 err = clib_error_return_unix (0, "memfd_create");
181 goto error;
182 }
183
184 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
185 {
186 err = clib_error_return_unix (0, "fcntl (F_ADD_SEALS)");
187 goto error;
188 }
189 }
Chris Luke879ace32017-09-26 13:15:16 -0400190
Florin Corasd3e83a92018-01-16 02:40:18 -0800191 log2_page_size = clib_mem_vm_get_log2_page_size (fd);
Chris Luke879ace32017-09-26 13:15:16 -0400192 if (log2_page_size == 0)
193 {
194 err = clib_error_return_unix (0, "cannot determine page size");
195 goto error;
196 }
Florin Corasd3e83a92018-01-16 02:40:18 -0800197
198 if (a->requested_va)
199 {
200 clib_mem_vm_randomize_va (&a->requested_va, log2_page_size);
201 mmap_flags |= MAP_FIXED;
202 }
Damjan Marion01914ce2017-09-14 19:04:50 +0200203 }
204 else /* not CLIB_MEM_VM_F_SHARED */
205 {
Damjan Marion7b185362018-03-04 16:41:35 +0100206 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marion01914ce2017-09-14 19:04:50 +0200207 if (a->flags & CLIB_MEM_VM_F_HUGETLB)
208 {
Damjan Marion7b185362018-03-04 16:41:35 +0100209 mmap_flags |= MAP_HUGETLB;
Damjan Marion01914ce2017-09-14 19:04:50 +0200210 log2_page_size = 21;
211 }
212 else
213 {
Damjan Marion01914ce2017-09-14 19:04:50 +0200214 log2_page_size = min_log2 (sysconf (_SC_PAGESIZE));
215 }
216 }
217
218 n_pages = ((a->size - 1) >> log2_page_size) + 1;
219
Damjan Marion01914ce2017-09-14 19:04:50 +0200220 if (a->flags & CLIB_MEM_VM_F_HUGETLB_PREALLOC)
221 {
222 err = clib_sysfs_prealloc_hugepages (a->numa_node,
223 1 << (log2_page_size - 10),
224 n_pages);
225 if (err)
226 goto error;
227
228 }
229
230 if (fd != -1)
Lee Roberts45a09462018-03-07 19:47:00 -0700231 if ((ftruncate (fd, (u64) n_pages * (1 << log2_page_size))) == -1)
Damjan Marion01914ce2017-09-14 19:04:50 +0200232 {
233 err = clib_error_return_unix (0, "ftruncate");
234 goto error;
235 }
236
237 if (old_mpol != -1)
238 {
239 int rv;
Dave Barach9466c452018-08-24 17:21:14 -0400240 long unsigned int mask[16] = { 0 };
Damjan Marion01914ce2017-09-14 19:04:50 +0200241 mask[0] = 1 << a->numa_node;
242 rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1);
Damjan Marion915e3f12018-04-18 09:21:24 +0200243 if (rv == -1 && a->numa_node != 0 &&
244 (a->flags & CLIB_MEM_VM_F_NUMA_FORCE) != 0)
Damjan Marion01914ce2017-09-14 19:04:50 +0200245 {
246 err = clib_error_return_unix (0, "set_mempolicy");
247 goto error;
248 }
249 }
250
Florin Corasd3e83a92018-01-16 02:40:18 -0800251 addr = mmap (uword_to_pointer (a->requested_va, void *), a->size,
252 (PROT_READ | PROT_WRITE), mmap_flags, fd, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200253 if (addr == MAP_FAILED)
254 {
255 err = clib_error_return_unix (0, "mmap");
256 goto error;
257 }
258
Damjan Marion2e172ea2018-01-03 15:48:34 +0000259 /* re-apply old numa memory policy */
Damjan Marion01914ce2017-09-14 19:04:50 +0200260 if (old_mpol != -1 &&
261 set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1) == -1)
262 {
263 err = clib_error_return_unix (0, "set_mempolicy");
264 goto error;
265 }
266
267 a->log2_page_size = log2_page_size;
268 a->n_pages = n_pages;
269 a->addr = addr;
270 a->fd = fd;
271 goto done;
272
273error:
274 if (fd != -1)
275 close (fd);
276
277done:
278 vec_free (filename);
279 return err;
280}
281
Haiyang Tan642829d2018-10-09 19:09:45 -0700282void
283clib_mem_vm_ext_free (clib_mem_vm_alloc_t * a)
284{
285 if (a != 0)
286 {
Haiyang Tana5ab5032018-10-15 06:17:55 -0700287 clib_mem_vm_free (a->addr, 1ull << a->log2_page_size);
Haiyang Tan642829d2018-10-09 19:09:45 -0700288 if (a->fd != -1)
289 close (a->fd);
290 }
291}
292
Damjan Marion01914ce2017-09-14 19:04:50 +0200293u64 *
294clib_mem_vm_get_paddr (void *mem, int log2_page_size, int n_pages)
295{
296 int pagesize = sysconf (_SC_PAGESIZE);
297 int fd;
298 int i;
299 u64 *r = 0;
300
301 if ((fd = open ((char *) "/proc/self/pagemap", O_RDONLY)) == -1)
302 return 0;
303
304 for (i = 0; i < n_pages; i++)
305 {
306 u64 seek, pagemap = 0;
307 uword vaddr = pointer_to_uword (mem) + (((u64) i) << log2_page_size);
308 seek = ((u64) vaddr / pagesize) * sizeof (u64);
309 if (lseek (fd, seek, SEEK_SET) != seek)
310 goto done;
311
312 if (read (fd, &pagemap, sizeof (pagemap)) != (sizeof (pagemap)))
313 goto done;
314
315 if ((pagemap & (1ULL << 63)) == 0)
316 goto done;
317
318 pagemap &= pow2_mask (55);
319 vec_add1 (r, pagemap * pagesize);
320 }
321
322done:
323 close (fd);
324 if (vec_len (r) != n_pages)
325 {
326 vec_free (r);
327 return 0;
328 }
329 return r;
330}
331
Florin Corasd3e83a92018-01-16 02:40:18 -0800332clib_error_t *
333clib_mem_vm_ext_map (clib_mem_vm_map_t * a)
334{
335 int mmap_flags = MAP_SHARED;
336 void *addr;
Damjan Marion01914ce2017-09-14 19:04:50 +0200337
Florin Corasd3e83a92018-01-16 02:40:18 -0800338 if (a->requested_va)
339 mmap_flags |= MAP_FIXED;
340
341 addr = (void *) mmap (uword_to_pointer (a->requested_va, void *), a->size,
342 PROT_READ | PROT_WRITE, mmap_flags, a->fd, 0);
343
344 if (addr == MAP_FAILED)
345 return clib_error_return_unix (0, "mmap");
346
347 a->addr = addr;
348 return 0;
349}
Damjan Marion01914ce2017-09-14 19:04:50 +0200350
351/*
352 * fd.io coding-style-patch-verification: ON
353 *
354 * Local Variables:
355 * eval: (c-set-style "gnu")
356 * End:
357 */