Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | #ifndef included_palloc_h |
| 17 | #define included_palloc_h |
| 18 | #include <vppinfra/format.h> |
| 19 | #include <vppinfra/pool.h> |
| 20 | |
| 21 | #define PMALLOC_LOG2_BLOCK_SZ CLIB_LOG2_CACHE_LINE_BYTES |
| 22 | #define PMALLOC_BLOCK_SZ (1 << 6) |
| 23 | |
| 24 | #define CLIB_PMALLOC_NUMA_LOCAL 0xffffffff |
| 25 | |
| 26 | typedef struct |
| 27 | { |
| 28 | u32 start, prev, next; |
| 29 | u32 size:31; |
| 30 | u32 used:1; |
| 31 | } clib_pmalloc_chunk_t; |
| 32 | |
| 33 | STATIC_ASSERT_SIZEOF (clib_pmalloc_chunk_t, 16); |
| 34 | |
| 35 | typedef struct |
| 36 | { |
| 37 | u32 index; |
| 38 | u32 arena_index; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 39 | clib_pmalloc_chunk_t *chunks; |
| 40 | u32 first_chunk_index; |
| 41 | u32 n_free_chunks; |
| 42 | u32 n_free_blocks; |
| 43 | } clib_pmalloc_page_t; |
| 44 | |
| 45 | typedef struct |
| 46 | { |
| 47 | u32 index; |
| 48 | u32 flags; |
| 49 | #define CLIB_PMALLOC_ARENA_F_SHARED_MEM (1 << 0) |
| 50 | int fd; |
| 51 | u32 numa_node; |
| 52 | u32 first_page_index; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 53 | u32 log2_subpage_sz; |
| 54 | u32 subpages_per_page; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 55 | u32 n_pages; |
| 56 | u8 *name; |
| 57 | u32 *page_indices; |
| 58 | } clib_pmalloc_arena_t; |
| 59 | |
| 60 | typedef struct |
| 61 | { |
Damjan Marion | c04e2b0 | 2018-10-25 15:56:04 +0200 | [diff] [blame] | 62 | /* flags */ |
| 63 | u32 flags; |
| 64 | #define CLIB_PMALLOC_F_NO_PAGEMAP (1 << 0) |
| 65 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 66 | /* base VA address */ |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 67 | u8 *base; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 68 | |
| 69 | /* default page size - typically 2M */ |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame^] | 70 | clib_mem_page_sz_t def_log2_page_sz; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 71 | |
| 72 | /* maximum number of pages, limited by VA preallocation size */ |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 73 | u32 max_pages; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 74 | |
| 75 | /* vector of pages - each page have own alloc pool and it can be split |
| 76 | into subpages (i.e. 2M page build out of 512 4K pages) */ |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 77 | clib_pmalloc_page_t *pages; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 78 | |
| 79 | /* hash used to find chunk index out of VA, chunk index is defined |
| 80 | per page */ |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 81 | uword *chunk_index_by_va; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 82 | |
| 83 | /* alloc arenas are group of pages which share same attributes |
| 84 | shared arenas are represented by FD and they are not grovable |
| 85 | private arenas are growable */ |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 86 | clib_pmalloc_arena_t *arenas; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 87 | |
| 88 | /* vector of per numa node alloc arena indices |
| 89 | each numa node have own default privat alloc arena */ |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 90 | u32 *default_arena_for_numa_node; |
| 91 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 92 | /* VA to PA lookup table */ |
| 93 | uword *lookup_table; |
| 94 | |
| 95 | /* lookup page size - equals to smalles subpage used */ |
| 96 | u32 lookup_log2_page_sz; |
| 97 | |
| 98 | /* last error */ |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 99 | clib_error_t *error; |
| 100 | } clib_pmalloc_main_t; |
| 101 | |
| 102 | |
Damjan Marion | 5a6c809 | 2019-02-21 14:44:59 +0100 | [diff] [blame] | 103 | int clib_pmalloc_init (clib_pmalloc_main_t * pm, uword base_addr, uword size); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 104 | void *clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm, |
| 105 | uword size, uword align, |
| 106 | u32 numa_node); |
| 107 | void *clib_pmalloc_alloc_aligned (clib_pmalloc_main_t * pm, uword size, |
| 108 | uword align); |
| 109 | void clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va); |
| 110 | |
| 111 | void *clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name, |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 112 | uword size, u32 log2_page_sz, |
| 113 | u32 numa_node); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 114 | |
| 115 | void *clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t * pm, void *arena_va, |
| 116 | uword size, uword align); |
| 117 | |
| 118 | format_function_t format_pmalloc; |
Mohsin Kazmi | 6ec99c3 | 2018-11-07 16:55:18 +0100 | [diff] [blame] | 119 | format_function_t format_pmalloc_map; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 120 | |
| 121 | always_inline clib_error_t * |
| 122 | clib_pmalloc_last_error (clib_pmalloc_main_t * pm) |
| 123 | { |
| 124 | return pm->error; |
| 125 | } |
| 126 | |
| 127 | always_inline u32 |
| 128 | clib_pmalloc_get_page_index (clib_pmalloc_main_t * pm, void *va) |
| 129 | { |
| 130 | uword index = (pointer_to_uword (va) - pointer_to_uword (pm->base)) >> |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 131 | pm->def_log2_page_sz; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 132 | |
| 133 | ASSERT (index < vec_len (pm->pages)); |
| 134 | |
| 135 | return index; |
| 136 | } |
| 137 | |
| 138 | always_inline clib_pmalloc_arena_t * |
| 139 | clib_pmalloc_get_arena (clib_pmalloc_main_t * pm, void *va) |
| 140 | { |
| 141 | u32 index = clib_pmalloc_get_page_index (pm, va); |
| 142 | return pm->arenas + pm->pages[index].arena_index; |
| 143 | } |
| 144 | |
| 145 | always_inline uword |
| 146 | clib_pmalloc_get_pa (clib_pmalloc_main_t * pm, void *va) |
| 147 | { |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 148 | uword index = (pointer_to_uword (va) - pointer_to_uword (pm->base)) >> |
| 149 | pm->lookup_log2_page_sz; |
| 150 | return pointer_to_uword (va) - pm->lookup_table[index]; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
| 154 | #endif /* included_palloc_h */ |
| 155 | |
| 156 | /* |
| 157 | * fd.io coding-style-patch-verification: ON |
| 158 | * |
| 159 | * Local Variables: |
| 160 | * eval: (c-set-style "gnu") |
| 161 | * End: |
| 162 | */ |