blob: 6a551ec0554bd6bcd8e02e69766f1bc94e83d403 [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
Damjan Marion9787f5f2018-10-24 12:56:32 +020049
50uword
51clib_mem_get_page_size (void)
52{
53 return getpagesize ();
54}
55
56uword
57clib_mem_get_default_hugepage_size (void)
58{
59 unformat_input_t input;
60 static u32 size = 0;
61 int fd;
62
63 if (size)
64 goto done;
65
66 if ((fd = open ("/proc/meminfo", 0)) == -1)
67 return 0;
68
69 unformat_init_clib_file (&input, fd);
70
71 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
72 {
73 if (unformat (&input, "Hugepagesize:%_%u kB", &size))
74 ;
75 else
76 unformat_skip_line (&input);
77 }
78 unformat_free (&input);
79 close (fd);
80done:
81 return 1024ULL * size;
82}
83
Dave Barach9466c452018-08-24 17:21:14 -040084u64
Florin Corasd3e83a92018-01-16 02:40:18 -080085clib_mem_vm_get_page_size (int fd)
Damjan Marion01914ce2017-09-14 19:04:50 +020086{
87 struct stat st = { 0 };
Chris Lukeb2bcad62017-09-18 08:51:22 -040088 if (fstat (fd, &st) == -1)
Damjan Marion01914ce2017-09-14 19:04:50 +020089 return 0;
Florin Corasd3e83a92018-01-16 02:40:18 -080090 return st.st_blksize;
91}
92
93int
94clib_mem_vm_get_log2_page_size (int fd)
95{
96 return min_log2 (clib_mem_vm_get_page_size (fd));
97}
98
Florin Corasb384b542018-01-15 01:08:33 -080099void
Florin Corasd3e83a92018-01-16 02:40:18 -0800100clib_mem_vm_randomize_va (uword * requested_va, u32 log2_page_size)
101{
102 u8 bit_mask = 15;
103
104 if (log2_page_size <= 12)
105 bit_mask = 15;
106 else if (log2_page_size > 12 && log2_page_size <= 16)
107 bit_mask = 3;
108 else
109 bit_mask = 0;
110
Haiyang Tana5ab5032018-10-15 06:17:55 -0700111 *requested_va +=
112 (clib_cpu_time_now () & bit_mask) * (1ull << log2_page_size);
Damjan Marion01914ce2017-09-14 19:04:50 +0200113}
114
Damjan Marion1636b162018-10-19 12:54:42 +0200115#ifndef MFD_HUGETLB
116#define MFD_HUGETLB 0x0004U
117#endif
118
119clib_error_t *
120clib_mem_create_hugetlb_fd (char *name, int *fdp)
121{
122 clib_error_t *err = 0;
123 int fd = -1;
124 static int memfd_hugetlb_supported = 1;
125 char *mount_dir;
126 char template[] = "/tmp/hugepage_mount.XXXXXX";
127 u8 *filename;
128
Juraj Sloboda430634c2018-10-22 10:59:45 +0200129 ASSERT (name);
130
Damjan Marion1636b162018-10-19 12:54:42 +0200131 if (memfd_hugetlb_supported)
132 {
133 if ((fd = memfd_create (name, MFD_HUGETLB)) != -1)
134 goto done;
135
136 /* avoid further tries if memfd MFD_HUGETLB is not supported */
137 if (errno == EINVAL && strnlen (name, 256) <= 249)
138 memfd_hugetlb_supported = 0;
139 }
140
141 mount_dir = mkdtemp (template);
142 if (mount_dir == 0)
143 return clib_error_return_unix (0, "mkdtemp \'%s\'", template);
144
145 if (mount ("none", (char *) mount_dir, "hugetlbfs", 0, NULL))
146 {
147 rmdir ((char *) mount_dir);
148 err = clib_error_return_unix (0, "mount hugetlb directory '%s'",
149 mount_dir);
150 }
151
152 filename = format (0, "%s/%s%c", mount_dir, name, 0);
153 fd = open ((char *) filename, O_CREAT | O_RDWR, 0755);
154 umount2 ((char *) mount_dir, MNT_DETACH);
155 rmdir ((char *) mount_dir);
156
157 if (fd == -1)
158 err = clib_error_return_unix (0, "open");
159
160done:
161 if (fd != -1)
162 fdp[0] = fd;
163 return err;
164}
165
Damjan Marion01914ce2017-09-14 19:04:50 +0200166clib_error_t *
167clib_mem_vm_ext_alloc (clib_mem_vm_alloc_t * a)
168{
169 int fd = -1;
170 clib_error_t *err = 0;
171 void *addr = 0;
172 u8 *filename = 0;
Damjan Marion7b185362018-03-04 16:41:35 +0100173 int mmap_flags = 0;
Damjan Marion01914ce2017-09-14 19:04:50 +0200174 int log2_page_size;
175 int n_pages;
176 int old_mpol = -1;
Dave Barach9466c452018-08-24 17:21:14 -0400177 long unsigned int old_mask[16] = { 0 };
Damjan Marion01914ce2017-09-14 19:04:50 +0200178
179 /* save old numa mem policy if needed */
180 if (a->flags & (CLIB_MEM_VM_F_NUMA_PREFER | CLIB_MEM_VM_F_NUMA_FORCE))
181 {
182 int rv;
Damjan Marion2e172ea2018-01-03 15:48:34 +0000183 rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1,
184 0, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200185
186 if (rv == -1)
187 {
Damjan Marion2e172ea2018-01-03 15:48:34 +0000188 if (a->numa_node != 0 && (a->flags & CLIB_MEM_VM_F_NUMA_FORCE) != 0)
Damjan Marion01914ce2017-09-14 19:04:50 +0200189 {
190 err = clib_error_return_unix (0, "get_mempolicy");
191 goto error;
192 }
193 else
194 old_mpol = -1;
195 }
196 }
197
Damjan Marion7b185362018-03-04 16:41:35 +0100198 if (a->flags & CLIB_MEM_VM_F_LOCKED)
199 mmap_flags |= MAP_LOCKED;
200
Damjan Marion01914ce2017-09-14 19:04:50 +0200201 /* if we are creating shared segment, we need file descriptor */
202 if (a->flags & CLIB_MEM_VM_F_SHARED)
203 {
Damjan Marion7b185362018-03-04 16:41:35 +0100204 mmap_flags |= MAP_SHARED;
Damjan Marion01914ce2017-09-14 19:04:50 +0200205 /* if hugepages are needed we need to create mount point */
206 if (a->flags & CLIB_MEM_VM_F_HUGETLB)
207 {
Damjan Marion1636b162018-10-19 12:54:42 +0200208 if ((err = clib_mem_create_hugetlb_fd (a->name, &fd)))
209 goto error;
Damjan Marion01914ce2017-09-14 19:04:50 +0200210
Damjan Marion01914ce2017-09-14 19:04:50 +0200211 mmap_flags |= MAP_LOCKED;
212 }
213 else
214 {
215 if ((fd = memfd_create (a->name, MFD_ALLOW_SEALING)) == -1)
216 {
217 err = clib_error_return_unix (0, "memfd_create");
218 goto error;
219 }
220
221 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
222 {
223 err = clib_error_return_unix (0, "fcntl (F_ADD_SEALS)");
224 goto error;
225 }
226 }
Chris Luke879ace32017-09-26 13:15:16 -0400227
Florin Corasd3e83a92018-01-16 02:40:18 -0800228 log2_page_size = clib_mem_vm_get_log2_page_size (fd);
Chris Luke879ace32017-09-26 13:15:16 -0400229 if (log2_page_size == 0)
230 {
231 err = clib_error_return_unix (0, "cannot determine page size");
232 goto error;
233 }
Florin Corasd3e83a92018-01-16 02:40:18 -0800234
235 if (a->requested_va)
236 {
237 clib_mem_vm_randomize_va (&a->requested_va, log2_page_size);
238 mmap_flags |= MAP_FIXED;
239 }
Damjan Marion01914ce2017-09-14 19:04:50 +0200240 }
241 else /* not CLIB_MEM_VM_F_SHARED */
242 {
Damjan Marion7b185362018-03-04 16:41:35 +0100243 mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marion01914ce2017-09-14 19:04:50 +0200244 if (a->flags & CLIB_MEM_VM_F_HUGETLB)
245 {
Damjan Marion7b185362018-03-04 16:41:35 +0100246 mmap_flags |= MAP_HUGETLB;
Damjan Marion01914ce2017-09-14 19:04:50 +0200247 log2_page_size = 21;
248 }
249 else
250 {
Damjan Marion01914ce2017-09-14 19:04:50 +0200251 log2_page_size = min_log2 (sysconf (_SC_PAGESIZE));
252 }
253 }
254
255 n_pages = ((a->size - 1) >> log2_page_size) + 1;
256
Damjan Marion01914ce2017-09-14 19:04:50 +0200257 if (a->flags & CLIB_MEM_VM_F_HUGETLB_PREALLOC)
258 {
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200259 err = clib_sysfs_prealloc_hugepages (a->numa_node, log2_page_size,
Damjan Marion01914ce2017-09-14 19:04:50 +0200260 n_pages);
261 if (err)
262 goto error;
263
264 }
265
266 if (fd != -1)
Lee Roberts45a09462018-03-07 19:47:00 -0700267 if ((ftruncate (fd, (u64) n_pages * (1 << log2_page_size))) == -1)
Damjan Marion01914ce2017-09-14 19:04:50 +0200268 {
269 err = clib_error_return_unix (0, "ftruncate");
270 goto error;
271 }
272
273 if (old_mpol != -1)
274 {
275 int rv;
Dave Barach9466c452018-08-24 17:21:14 -0400276 long unsigned int mask[16] = { 0 };
Damjan Marion01914ce2017-09-14 19:04:50 +0200277 mask[0] = 1 << a->numa_node;
278 rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1);
Damjan Marion915e3f12018-04-18 09:21:24 +0200279 if (rv == -1 && a->numa_node != 0 &&
280 (a->flags & CLIB_MEM_VM_F_NUMA_FORCE) != 0)
Damjan Marion01914ce2017-09-14 19:04:50 +0200281 {
282 err = clib_error_return_unix (0, "set_mempolicy");
283 goto error;
284 }
285 }
286
Florin Corasd3e83a92018-01-16 02:40:18 -0800287 addr = mmap (uword_to_pointer (a->requested_va, void *), a->size,
288 (PROT_READ | PROT_WRITE), mmap_flags, fd, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200289 if (addr == MAP_FAILED)
290 {
291 err = clib_error_return_unix (0, "mmap");
292 goto error;
293 }
294
Damjan Marion2e172ea2018-01-03 15:48:34 +0000295 /* re-apply old numa memory policy */
Damjan Marion01914ce2017-09-14 19:04:50 +0200296 if (old_mpol != -1 &&
297 set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1) == -1)
298 {
299 err = clib_error_return_unix (0, "set_mempolicy");
300 goto error;
301 }
302
303 a->log2_page_size = log2_page_size;
304 a->n_pages = n_pages;
305 a->addr = addr;
306 a->fd = fd;
307 goto done;
308
309error:
310 if (fd != -1)
311 close (fd);
312
313done:
314 vec_free (filename);
315 return err;
316}
317
Haiyang Tan642829d2018-10-09 19:09:45 -0700318void
319clib_mem_vm_ext_free (clib_mem_vm_alloc_t * a)
320{
321 if (a != 0)
322 {
Haiyang Tana5ab5032018-10-15 06:17:55 -0700323 clib_mem_vm_free (a->addr, 1ull << a->log2_page_size);
Haiyang Tan642829d2018-10-09 19:09:45 -0700324 if (a->fd != -1)
325 close (a->fd);
326 }
327}
328
Damjan Marion01914ce2017-09-14 19:04:50 +0200329u64 *
330clib_mem_vm_get_paddr (void *mem, int log2_page_size, int n_pages)
331{
332 int pagesize = sysconf (_SC_PAGESIZE);
333 int fd;
334 int i;
335 u64 *r = 0;
336
337 if ((fd = open ((char *) "/proc/self/pagemap", O_RDONLY)) == -1)
338 return 0;
339
340 for (i = 0; i < n_pages; i++)
341 {
342 u64 seek, pagemap = 0;
343 uword vaddr = pointer_to_uword (mem) + (((u64) i) << log2_page_size);
344 seek = ((u64) vaddr / pagesize) * sizeof (u64);
345 if (lseek (fd, seek, SEEK_SET) != seek)
346 goto done;
347
348 if (read (fd, &pagemap, sizeof (pagemap)) != (sizeof (pagemap)))
349 goto done;
350
351 if ((pagemap & (1ULL << 63)) == 0)
352 goto done;
353
354 pagemap &= pow2_mask (55);
355 vec_add1 (r, pagemap * pagesize);
356 }
357
358done:
359 close (fd);
360 if (vec_len (r) != n_pages)
361 {
362 vec_free (r);
363 return 0;
364 }
365 return r;
366}
367
Florin Corasd3e83a92018-01-16 02:40:18 -0800368clib_error_t *
369clib_mem_vm_ext_map (clib_mem_vm_map_t * a)
370{
371 int mmap_flags = MAP_SHARED;
372 void *addr;
Damjan Marion01914ce2017-09-14 19:04:50 +0200373
Florin Corasd3e83a92018-01-16 02:40:18 -0800374 if (a->requested_va)
375 mmap_flags |= MAP_FIXED;
376
377 addr = (void *) mmap (uword_to_pointer (a->requested_va, void *), a->size,
378 PROT_READ | PROT_WRITE, mmap_flags, a->fd, 0);
379
380 if (addr == MAP_FAILED)
381 return clib_error_return_unix (0, "mmap");
382
383 a->addr = addr;
384 return 0;
385}
Damjan Marion01914ce2017-09-14 19:04:50 +0200386
387/*
388 * fd.io coding-style-patch-verification: ON
389 *
390 * Local Variables:
391 * eval: (c-set-style "gnu")
392 * End:
393 */