physmem: remove debug leftovers
[vpp.git] / src / vlib / linux / physmem.c
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
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>
46 #include <numa.h>
47 #include <numaif.h>
48
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>
54
55 static void *
56 unix_physmem_alloc_aligned (vlib_main_t * vm, vlib_physmem_region_index_t idx,
57                             uword n_bytes, uword alignment)
58 {
59   vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx);
60   uword lo_offset, hi_offset;
61   uword *to_free = 0;
62
63   if (pr->heap == 0)
64     return 0;
65
66   /* IO memory is always at least cache aligned. */
67   alignment = clib_max (alignment, CLIB_CACHE_LINE_BYTES);
68
69   while (1)
70     {
71       mheap_get_aligned (pr->heap, n_bytes,
72                          /* align */ alignment,
73                          /* align offset */ 0,
74                          &lo_offset);
75
76       /* Allocation failed? */
77       if (lo_offset == ~0)
78         break;
79
80       if (pr->flags & VLIB_PHYSMEM_F_FAKE)
81         break;
82
83       /* Make sure allocation does not span DMA physical chunk boundary. */
84       hi_offset = lo_offset + n_bytes - 1;
85
86       if ((lo_offset >> pr->log2_page_size) ==
87           (hi_offset >> pr->log2_page_size))
88         break;
89
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);
93     }
94
95   if (to_free != 0)
96     {
97       uword i;
98       for (i = 0; i < vec_len (to_free); i++)
99         mheap_put (pr->heap, to_free[i]);
100       vec_free (to_free);
101     }
102
103   return lo_offset != ~0 ? pr->heap + lo_offset : 0;
104 }
105
106 static void
107 unix_physmem_free (vlib_main_t * vm, vlib_physmem_region_index_t idx, void *x)
108 {
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);
112 }
113
114 static u64
115 get_page_paddr (int fd, uword addr)
116 {
117   int pagesize = sysconf (_SC_PAGESIZE);
118   u64 seek, pagemap = 0;
119
120   seek = ((u64) addr / pagesize) * sizeof (u64);
121   if (lseek (fd, seek, SEEK_SET) != seek)
122     {
123       clib_unix_warning ("lseek to 0x%llx", seek);
124       return 0;
125     }
126   if (read (fd, &pagemap, sizeof (pagemap)) != (sizeof (pagemap)))
127     {
128       clib_unix_warning ("read ptbits");
129       return 0;
130     }
131   if ((pagemap & (1ULL << 63)) == 0)
132     return 0;
133
134   pagemap &= pow2_mask (55);
135
136   return pagemap * pagesize;
137 }
138
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)
143 {
144   vlib_physmem_main_t *vpm = &vm->physmem_main;
145   vlib_physmem_region_t *pr;
146   clib_error_t *error = 0;
147   int pagemap_fd = -1;
148   u8 *mount_dir = 0;
149   u8 *filename = 0;
150   struct stat st;
151   int old_mpol;
152   int mmap_flags;
153   struct bitmask *old_mask = numa_allocate_nodemask ();
154
155   if (geteuid () != 0 && (flags & VLIB_PHYSMEM_F_FAKE) == 0)
156     return clib_error_return (0, "not allowed");
157
158   pool_get (vpm->regions, pr);
159
160   if ((pr - vpm->regions) >= 256)
161     {
162       error = clib_error_return (0, "maximum number of regions reached");
163       goto error;
164     }
165
166   pr->index = pr - vpm->regions;
167   pr->fd = -1;
168   pr->flags = flags;
169
170   if (get_mempolicy (&old_mpol, old_mask->maskp, old_mask->size + 1, NULL, 0)
171       == -1)
172     {
173       if ((flags & VLIB_PHYSMEM_F_FAKE) == 0)
174         {
175           error = clib_error_return_unix (0, "get_mempolicy");
176           goto error;
177         }
178       else
179         old_mpol = -1;
180     }
181
182   if ((flags & VLIB_PHYSMEM_F_FAKE) == 0)
183     {
184       if ((pagemap_fd = open ((char *) "/proc/self/pagemap", O_RDONLY)) == -1)
185         {
186           error = clib_error_return_unix (0, "open '/proc/self/pagemap'");
187           goto error;
188         }
189
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);
193
194       unlink ((char *) mount_dir);
195
196       error = vlib_unix_recursive_mkdir ((char *) mount_dir);
197       if (error)
198         goto error;
199
200       if (mount ("none", (char *) mount_dir, "hugetlbfs", 0, NULL))
201         {
202           error = clib_error_return_unix (0, "mount hugetlb directory '%s'",
203                                           mount_dir);
204           goto error;
205         }
206
207       if ((pr->fd = open ((char *) filename, O_CREAT | O_RDWR, 0755)) == -1)
208         {
209           error = clib_error_return_unix (0, "open");
210           goto error;
211         }
212
213       mmap_flags = MAP_SHARED | MAP_HUGETLB | MAP_LOCKED;
214     }
215   else
216     {
217       if ((pr->fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1)
218         return clib_error_return_unix (0, "memfd_create");
219
220       if ((fcntl (pr->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
221         {
222           error =
223             clib_error_return_unix (0, "fcntl (F_ADD_SEALS, F_SEAL_SHRINK)");
224           goto error;
225         }
226       mmap_flags = MAP_SHARED;
227     }
228
229   if (fstat (pr->fd, &st))
230     {
231       error = clib_error_return_unix (0, "fstat");
232       goto error;
233     }
234
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);
238
239   if ((ftruncate (pr->fd, size)) == -1)
240     {
241       error = clib_error_return_unix (0, "ftruncate length: %d", size);
242       goto error;
243     }
244
245   if ((flags & VLIB_PHYSMEM_F_FAKE) == 0)
246     {
247       error = vlib_sysfs_prealloc_hugepages (numa_node,
248                                              1 << (pr->log2_page_size - 10),
249                                              pr->n_pages);
250       if (error)
251         goto error;
252     }
253
254   if (old_mpol != -1)
255     numa_set_preferred (numa_node);
256
257   pr->mem = mmap (0, size, (PROT_READ | PROT_WRITE), mmap_flags, pr->fd, 0);
258
259   if (pr->mem == MAP_FAILED)
260     {
261       pr->mem = 0;
262       error = clib_error_return_unix (0, "mmap");
263       goto error;
264     }
265
266   if (old_mpol != -1 &&
267       set_mempolicy (old_mpol, old_mask->maskp, old_mask->size + 1) == -1)
268     {
269       error = clib_error_return_unix (0, "set_mempolicy");
270       goto error;
271     }
272
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);
277
278   if ((flags & VLIB_PHYSMEM_F_FAKE) == 0)
279     {
280       int i;
281       for (i = 0; i < pr->n_pages; i++)
282         {
283           void *ptr = pr->mem + (i << pr->log2_page_size);
284           int node;
285           move_pages (0, 1, &ptr, 0, &node, 0);
286           if (numa_node != node)
287             {
288               clib_warning
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);
292               break;
293             }
294         }
295     }
296
297   if (flags & VLIB_PHYSMEM_F_INIT_MHEAP)
298     {
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);
303     }
304
305   if (flags & VLIB_PHYSMEM_F_HAVE_BUFFERS)
306     {
307       vlib_buffer_add_mem_range (vm, pointer_to_uword (pr->mem), pr->size);
308     }
309
310   *idx = pr->index;
311
312   if ((flags & VLIB_PHYSMEM_F_FAKE) == 0)
313     {
314       int i;
315       for (i = 0; i < pr->n_pages; i++)
316         {
317           uword vaddr =
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);
321         }
322     }
323
324   goto done;
325
326 error:
327   if (pr->fd > -1)
328     close (pr->fd);
329
330   if (pr->mem)
331     munmap (pr->mem, size);
332
333   memset (pr, 0, sizeof (*pr));
334   pool_put (vpm->regions, pr);
335
336 done:
337   if (mount_dir)
338     {
339       umount2 ((char *) mount_dir, MNT_DETACH);
340       rmdir ((char *) mount_dir);
341       vec_free (mount_dir);
342     }
343   numa_free_cpumask (old_mask);
344   vec_free (filename);
345   if (pagemap_fd > -1)
346     close (pagemap_fd);
347   return error;
348 }
349
350 static void
351 unix_physmem_region_free (vlib_main_t * vm, vlib_physmem_region_index_t idx)
352 {
353   vlib_physmem_main_t *vpm = &vm->physmem_main;
354   vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx);
355
356   if (pr->fd > 0)
357     close (pr->fd);
358   munmap (pr->mem, pr->size);
359   vec_free (pr->name);
360   pool_put (vpm->regions, pr);
361 }
362
363 clib_error_t *
364 unix_physmem_init (vlib_main_t * vm)
365 {
366   clib_error_t *error = 0;
367
368   /* Avoid multiple calls. */
369   if (vm->os_physmem_alloc_aligned)
370     return error;
371
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;
376
377   return error;
378 }
379
380 static clib_error_t *
381 show_physmem (vlib_main_t * vm,
382               unformat_input_t * input, vlib_cli_command_t * cmd)
383 {
384   vlib_physmem_main_t *vpm = &vm->physmem_main;
385   vlib_physmem_region_t *pr;
386
387   /* *INDENT-OFF* */
388   pool_foreach (pr, vpm->regions, (
389     {
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);
394       if (pr->heap)
395         vlib_cli_output (vm, "  %U", format_mheap, pr->heap, /* verbose */ 1);
396       else
397         vlib_cli_output (vm, "  no heap\n");
398     }));
399   /* *INDENT-ON* */
400   return 0;
401 }
402
403 /* *INDENT-OFF* */
404 VLIB_CLI_COMMAND (show_physmem_command, static) = {
405   .path = "show physmem",
406   .short_help = "Show physical memory allocation",
407   .function = show_physmem,
408 };
409 /* *INDENT-ON* */
410
411 /*
412  * fd.io coding-style-patch-verification: ON
413  *
414  * Local Variables:
415  * eval: (c-set-style "gnu")
416  * End:
417  */