2 * Copyright (c) 2015 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * physmem.c: Unix physical memory
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 #include <sys/types.h>
42 #include <sys/mount.h>
44 #include <sys/fcntl.h>
49 #include <vlib/vlib.h>
50 #include <vlib/physmem.h>
51 #include <vlib/unix/unix.h>
52 #include <vlib/linux/syscall.h>
53 #include <vlib/linux/sysfs.h>
56 unix_physmem_alloc_aligned (vlib_main_t * vm, vlib_physmem_region_index_t idx,
57 uword n_bytes, uword alignment)
59 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx);
60 uword lo_offset, hi_offset;
66 /* IO memory is always at least cache aligned. */
67 alignment = clib_max (alignment, CLIB_CACHE_LINE_BYTES);
71 mheap_get_aligned (pr->heap, n_bytes,
72 /* align */ alignment,
76 /* Allocation failed? */
80 if (pr->flags & VLIB_PHYSMEM_F_FAKE)
83 /* Make sure allocation does not span DMA physical chunk boundary. */
84 hi_offset = lo_offset + n_bytes - 1;
86 if ((lo_offset >> pr->log2_page_size) ==
87 (hi_offset >> pr->log2_page_size))
90 /* Allocation would span chunk boundary, queue it to be freed as soon as
91 we find suitable chunk. */
92 vec_add1 (to_free, lo_offset);
98 for (i = 0; i < vec_len (to_free); i++)
99 mheap_put (pr->heap, to_free[i]);
103 return lo_offset != ~0 ? pr->heap + lo_offset : 0;
107 unix_physmem_free (vlib_main_t * vm, vlib_physmem_region_index_t idx, void *x)
109 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx);
110 /* Return object to region's heap. */
111 mheap_put (pr->heap, x - pr->heap);
115 get_page_paddr (int fd, uword addr)
117 int pagesize = sysconf (_SC_PAGESIZE);
118 u64 seek, pagemap = 0;
120 seek = ((u64) addr / pagesize) * sizeof (u64);
121 if (lseek (fd, seek, SEEK_SET) != seek)
123 clib_unix_warning ("lseek to 0x%llx", seek);
126 if (read (fd, &pagemap, sizeof (pagemap)) != (sizeof (pagemap)))
128 clib_unix_warning ("read ptbits");
131 if ((pagemap & (1ULL << 63)) == 0)
134 pagemap &= pow2_mask (55);
136 return pagemap * pagesize;
139 static clib_error_t *
140 unix_physmem_region_alloc (vlib_main_t * vm, char *name, u32 size,
141 u8 numa_node, u32 flags,
142 vlib_physmem_region_index_t * idx)
144 vlib_physmem_main_t *vpm = &vm->physmem_main;
145 vlib_physmem_region_t *pr;
146 clib_error_t *error = 0;
153 struct bitmask *old_mask = numa_allocate_nodemask ();
155 if (geteuid () != 0 && (flags & VLIB_PHYSMEM_F_FAKE) == 0)
156 return clib_error_return (0, "not allowed");
158 pool_get (vpm->regions, pr);
160 if ((pr - vpm->regions) >= 256)
162 error = clib_error_return (0, "maximum number of regions reached");
166 pr->index = pr - vpm->regions;
170 if (get_mempolicy (&old_mpol, old_mask->maskp, old_mask->size + 1, NULL, 0)
173 if ((flags & VLIB_PHYSMEM_F_FAKE) == 0)
175 error = clib_error_return_unix (0, "get_mempolicy");
182 if ((flags & VLIB_PHYSMEM_F_FAKE) == 0)
184 if ((pagemap_fd = open ((char *) "/proc/self/pagemap", O_RDONLY)) == -1)
186 error = clib_error_return_unix (0, "open '/proc/self/pagemap'");
190 mount_dir = format (0, "%s/physmem_region%d%c",
191 vlib_unix_get_runtime_dir (), pr->index, 0);
192 filename = format (0, "%s/mem%c", mount_dir, 0);
194 unlink ((char *) mount_dir);
196 error = vlib_unix_recursive_mkdir ((char *) mount_dir);
200 if (mount ("none", (char *) mount_dir, "hugetlbfs", 0, NULL))
202 error = clib_error_return_unix (0, "mount hugetlb directory '%s'",
207 if ((pr->fd = open ((char *) filename, O_CREAT | O_RDWR, 0755)) == -1)
209 error = clib_error_return_unix (0, "open");
213 mmap_flags = MAP_SHARED | MAP_HUGETLB | MAP_LOCKED;
217 if ((pr->fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1)
218 return clib_error_return_unix (0, "memfd_create");
220 if ((fcntl (pr->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
223 clib_error_return_unix (0, "fcntl (F_ADD_SEALS, F_SEAL_SHRINK)");
226 mmap_flags = MAP_SHARED;
229 if (fstat (pr->fd, &st))
231 error = clib_error_return_unix (0, "fstat");
235 pr->log2_page_size = min_log2 (st.st_blksize);
236 pr->n_pages = ((size - 1) >> pr->log2_page_size) + 1;
237 size = pr->n_pages * (1 << pr->log2_page_size);
239 if ((ftruncate (pr->fd, size)) == -1)
241 error = clib_error_return_unix (0, "ftruncate length: %d", size);
245 if ((flags & VLIB_PHYSMEM_F_FAKE) == 0)
247 error = vlib_sysfs_prealloc_hugepages (numa_node,
248 1 << (pr->log2_page_size - 10),
255 numa_set_preferred (numa_node);
257 pr->mem = mmap (0, size, (PROT_READ | PROT_WRITE), mmap_flags, pr->fd, 0);
259 if (pr->mem == MAP_FAILED)
262 error = clib_error_return_unix (0, "mmap");
266 if (old_mpol != -1 &&
267 set_mempolicy (old_mpol, old_mask->maskp, old_mask->size + 1) == -1)
269 error = clib_error_return_unix (0, "set_mempolicy");
273 pr->size = pr->n_pages << pr->log2_page_size;
274 pr->page_mask = (1 << pr->log2_page_size) - 1;
275 pr->numa_node = numa_node;
276 pr->name = format (0, "%s", name);
278 if ((flags & VLIB_PHYSMEM_F_FAKE) == 0)
281 for (i = 0; i < pr->n_pages; i++)
283 void *ptr = pr->mem + (i << pr->log2_page_size);
285 move_pages (0, 1, &ptr, 0, &node, 0);
286 if (numa_node != node)
289 ("physmem page for region \'%s\' allocated on the wrong"
290 " numa node (requested %u actual %u)", pr->name,
291 pr->numa_node, node, i);
297 if (flags & VLIB_PHYSMEM_F_INIT_MHEAP)
299 pr->heap = mheap_alloc_with_flags (pr->mem, pr->size,
300 /* Don't want mheap mmap/munmap with IO memory. */
301 MHEAP_FLAG_DISABLE_VM |
302 MHEAP_FLAG_THREAD_SAFE);
305 if (flags & VLIB_PHYSMEM_F_HAVE_BUFFERS)
307 vlib_buffer_add_mem_range (vm, pointer_to_uword (pr->mem), pr->size);
312 if ((flags & VLIB_PHYSMEM_F_FAKE) == 0)
315 for (i = 0; i < pr->n_pages; i++)
318 pointer_to_uword (pr->mem) + (((u64) i) << pr->log2_page_size);
319 u64 page_paddr = get_page_paddr (pagemap_fd, vaddr);
320 vec_add1 (pr->page_table, page_paddr);
331 munmap (pr->mem, size);
333 memset (pr, 0, sizeof (*pr));
334 pool_put (vpm->regions, pr);
339 umount2 ((char *) mount_dir, MNT_DETACH);
340 rmdir ((char *) mount_dir);
341 vec_free (mount_dir);
343 numa_free_cpumask (old_mask);
351 unix_physmem_region_free (vlib_main_t * vm, vlib_physmem_region_index_t idx)
353 vlib_physmem_main_t *vpm = &vm->physmem_main;
354 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx);
358 munmap (pr->mem, pr->size);
360 pool_put (vpm->regions, pr);
364 unix_physmem_init (vlib_main_t * vm)
366 clib_error_t *error = 0;
368 /* Avoid multiple calls. */
369 if (vm->os_physmem_alloc_aligned)
372 vm->os_physmem_alloc_aligned = unix_physmem_alloc_aligned;
373 vm->os_physmem_free = unix_physmem_free;
374 vm->os_physmem_region_alloc = unix_physmem_region_alloc;
375 vm->os_physmem_region_free = unix_physmem_region_free;
380 static clib_error_t *
381 show_physmem (vlib_main_t * vm,
382 unformat_input_t * input, vlib_cli_command_t * cmd)
384 vlib_physmem_main_t *vpm = &vm->physmem_main;
385 vlib_physmem_region_t *pr;
388 pool_foreach (pr, vpm->regions, (
390 vlib_cli_output (vm, "index %u name '%s' page-size %uKB num-pages %d "
391 "numa-node %u fd %d\n",
392 pr->index, pr->name, (1 << (pr->log2_page_size -10)),
393 pr->n_pages, pr->numa_node, pr->fd);
395 vlib_cli_output (vm, " %U", format_mheap, pr->heap, /* verbose */ 1);
397 vlib_cli_output (vm, " no heap\n");
404 VLIB_CLI_COMMAND (show_physmem_command, static) = {
405 .path = "show physmem",
406 .short_help = "Show physical memory allocation",
407 .function = show_physmem,
412 * fd.io coding-style-patch-verification: ON
415 * eval: (c-set-style "gnu")