Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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: |
| 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 | * physmem.c: Unix physical memory |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 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: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 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. |
| 38 | */ |
| 39 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 40 | #include <unistd.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/mount.h> |
| 43 | #include <sys/mman.h> |
| 44 | #include <sys/fcntl.h> |
| 45 | #include <sys/stat.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 47 | #include <vppinfra/linux/syscall.h> |
| 48 | #include <vppinfra/linux/sysfs.h> |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 49 | #include <vlib/vlib.h> |
| 50 | #include <vlib/physmem.h> |
| 51 | #include <vlib/unix/unix.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | |
| 53 | static void * |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 54 | unix_physmem_alloc_aligned (vlib_main_t * vm, vlib_physmem_region_index_t idx, |
| 55 | uword n_bytes, uword alignment) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | { |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 57 | vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | uword lo_offset, hi_offset; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 59 | uword *to_free = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 61 | if (pr->heap == 0) |
| 62 | return 0; |
| 63 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | /* IO memory is always at least cache aligned. */ |
| 65 | alignment = clib_max (alignment, CLIB_CACHE_LINE_BYTES); |
| 66 | |
| 67 | while (1) |
| 68 | { |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 69 | mheap_get_aligned (pr->heap, n_bytes, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | /* align */ alignment, |
| 71 | /* align offset */ 0, |
| 72 | &lo_offset); |
| 73 | |
| 74 | /* Allocation failed? */ |
| 75 | if (lo_offset == ~0) |
| 76 | break; |
| 77 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 78 | if (pr->flags & VLIB_PHYSMEM_F_FAKE) |
| 79 | break; |
| 80 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | /* Make sure allocation does not span DMA physical chunk boundary. */ |
| 82 | hi_offset = lo_offset + n_bytes - 1; |
| 83 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 84 | if ((lo_offset >> pr->log2_page_size) == |
| 85 | (hi_offset >> pr->log2_page_size)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | break; |
| 87 | |
| 88 | /* Allocation would span chunk boundary, queue it to be freed as soon as |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 89 | we find suitable chunk. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | vec_add1 (to_free, lo_offset); |
| 91 | } |
| 92 | |
| 93 | if (to_free != 0) |
| 94 | { |
| 95 | uword i; |
| 96 | for (i = 0; i < vec_len (to_free); i++) |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 97 | mheap_put (pr->heap, to_free[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | vec_free (to_free); |
| 99 | } |
| 100 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 101 | return lo_offset != ~0 ? pr->heap + lo_offset : 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 104 | static void |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 105 | unix_physmem_free (vlib_main_t * vm, vlib_physmem_region_index_t idx, void *x) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | { |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 107 | vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | /* Return object to region's heap. */ |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 109 | mheap_put (pr->heap, x - pr->heap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 112 | static clib_error_t * |
| 113 | unix_physmem_region_alloc (vlib_main_t * vm, char *name, u32 size, |
| 114 | u8 numa_node, u32 flags, |
| 115 | vlib_physmem_region_index_t * idx) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 117 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 118 | vlib_physmem_region_t *pr; |
| 119 | clib_error_t *error = 0; |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 120 | clib_mem_vm_alloc_t alloc = { 0 }; |
| 121 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 123 | if (geteuid () != 0 && (flags & VLIB_PHYSMEM_F_FAKE) == 0) |
| 124 | return clib_error_return (0, "not allowed"); |
| 125 | |
| 126 | pool_get (vpm->regions, pr); |
| 127 | |
| 128 | if ((pr - vpm->regions) >= 256) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | { |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 130 | error = clib_error_return (0, "maximum number of regions reached"); |
| 131 | goto error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 134 | alloc.name = name; |
| 135 | alloc.size = size; |
| 136 | alloc.numa_node = numa_node; |
| 137 | alloc.flags = CLIB_MEM_VM_F_SHARED; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 139 | if ((flags & VLIB_PHYSMEM_F_FAKE) == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | { |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 141 | alloc.flags |= CLIB_MEM_VM_F_HUGETLB; |
| 142 | alloc.flags |= CLIB_MEM_VM_F_HUGETLB_PREALLOC; |
| 143 | alloc.flags |= CLIB_MEM_VM_F_NUMA_FORCE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | } |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 145 | else |
| 146 | { |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 147 | alloc.flags |= CLIB_MEM_VM_F_NUMA_PREFER; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 150 | error = clib_mem_vm_ext_alloc (&alloc); |
| 151 | if (error) |
| 152 | goto error; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 153 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 154 | pr->index = pr - vpm->regions; |
| 155 | pr->flags = flags; |
| 156 | pr->fd = alloc.fd; |
| 157 | pr->mem = alloc.addr; |
| 158 | pr->log2_page_size = alloc.log2_page_size; |
| 159 | pr->n_pages = alloc.n_pages; |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 160 | pr->size = (u64) pr->n_pages << (u64) pr->log2_page_size; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 161 | pr->page_mask = (1 << pr->log2_page_size) - 1; |
| 162 | pr->numa_node = numa_node; |
| 163 | pr->name = format (0, "%s", name); |
| 164 | |
| 165 | if ((flags & VLIB_PHYSMEM_F_FAKE) == 0) |
| 166 | { |
| 167 | int i; |
| 168 | for (i = 0; i < pr->n_pages; i++) |
| 169 | { |
| 170 | void *ptr = pr->mem + (i << pr->log2_page_size); |
| 171 | int node; |
| 172 | move_pages (0, 1, &ptr, 0, &node, 0); |
| 173 | if (numa_node != node) |
| 174 | { |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 175 | clib_warning ("physmem page for region \'%s\' allocated on the" |
| 176 | " wrong numa node (requested %u actual %u)", |
| 177 | pr->name, pr->numa_node, node, i); |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | } |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 181 | pr->page_table = clib_mem_vm_get_paddr (pr->mem, pr->log2_page_size, |
| 182 | pr->n_pages); |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (flags & VLIB_PHYSMEM_F_INIT_MHEAP) |
| 186 | { |
| 187 | pr->heap = mheap_alloc_with_flags (pr->mem, pr->size, |
| 188 | /* Don't want mheap mmap/munmap with IO memory. */ |
| 189 | MHEAP_FLAG_DISABLE_VM | |
| 190 | MHEAP_FLAG_THREAD_SAFE); |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | if (flags & VLIB_PHYSMEM_F_HAVE_BUFFERS) |
| 194 | { |
| 195 | vlib_buffer_add_mem_range (vm, pointer_to_uword (pr->mem), pr->size); |
| 196 | } |
| 197 | |
| 198 | *idx = pr->index; |
| 199 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 200 | goto done; |
| 201 | |
| 202 | error: |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 203 | memset (pr, 0, sizeof (*pr)); |
| 204 | pool_put (vpm->regions, pr); |
| 205 | |
| 206 | done: |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 207 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 210 | static void |
| 211 | unix_physmem_region_free (vlib_main_t * vm, vlib_physmem_region_index_t idx) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | { |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 213 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
| 214 | vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx); |
| 215 | |
| 216 | if (pr->fd > 0) |
| 217 | close (pr->fd); |
| 218 | munmap (pr->mem, pr->size); |
| 219 | vec_free (pr->name); |
| 220 | pool_put (vpm->regions, pr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 223 | clib_error_t * |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 224 | unix_physmem_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 226 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | |
| 228 | /* Avoid multiple calls. */ |
| 229 | if (vm->os_physmem_alloc_aligned) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 230 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | |
| 232 | vm->os_physmem_alloc_aligned = unix_physmem_alloc_aligned; |
| 233 | vm->os_physmem_free = unix_physmem_free; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 234 | vm->os_physmem_region_alloc = unix_physmem_region_alloc; |
| 235 | vm->os_physmem_region_free = unix_physmem_region_free; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | return error; |
| 238 | } |
| 239 | |
| 240 | static clib_error_t * |
| 241 | show_physmem (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 242 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | { |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 244 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
| 245 | vlib_physmem_region_t *pr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 247 | /* *INDENT-OFF* */ |
| 248 | pool_foreach (pr, vpm->regions, ( |
| 249 | { |
| 250 | vlib_cli_output (vm, "index %u name '%s' page-size %uKB num-pages %d " |
| 251 | "numa-node %u fd %d\n", |
| 252 | pr->index, pr->name, (1 << (pr->log2_page_size -10)), |
| 253 | pr->n_pages, pr->numa_node, pr->fd); |
| 254 | if (pr->heap) |
| 255 | vlib_cli_output (vm, " %U", format_mheap, pr->heap, /* verbose */ 1); |
| 256 | else |
| 257 | vlib_cli_output (vm, " no heap\n"); |
| 258 | })); |
| 259 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 263 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | VLIB_CLI_COMMAND (show_physmem_command, static) = { |
| 265 | .path = "show physmem", |
| 266 | .short_help = "Show physical memory allocation", |
| 267 | .function = show_physmem, |
| 268 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 269 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 271 | /* |
| 272 | * fd.io coding-style-patch-verification: ON |
| 273 | * |
| 274 | * Local Variables: |
| 275 | * eval: (c-set-style "gnu") |
| 276 | * End: |
| 277 | */ |