From fc639ff2d7abd8599f59078fac99f731026215b3 Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Fri, 11 Sep 2020 22:25:34 +0200 Subject: vlib: map thread stack instead of allocating them from heap Heap may use different page sizes so we will not be able to create stack protection page. Type: improvement Change-Id: Ibb35c9f0a151c464ee0167d17f2bd773ef6f530b Signed-off-by: Damjan Marion --- src/vlib/node.c | 29 +++++++++-------------------- src/vlib/unix/main.c | 19 +++++++++---------- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/src/vlib/node.c b/src/vlib/node.c index 169c67b0888..00660373607 100644 --- a/src/vlib/node.c +++ b/src/vlib/node.c @@ -295,7 +295,6 @@ register_node (vlib_main_t * vm, vlib_node_registration_t * r) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n; - u32 page_size = clib_mem_get_page_size (); int i; if (CLIB_DEBUG > 0) @@ -409,36 +408,26 @@ register_node (vlib_main_t * vm, vlib_node_registration_t * r) if (n->type == VLIB_NODE_TYPE_PROCESS) { vlib_process_t *p; - void *map; - uword log2_n_stack_bytes, stack_bytes; - int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS; + uword log2_n_stack_bytes; log2_n_stack_bytes = clib_max (r->process_log2_n_stack_bytes, VLIB_PROCESS_LOG2_STACK_SIZE); log2_n_stack_bytes = clib_max (log2_n_stack_bytes, - min_log2 (page_size)); + clib_mem_get_log2_page_size ()); p = clib_mem_alloc_aligned (sizeof (p[0]), CLIB_CACHE_LINE_BYTES); clib_memset (p, 0, sizeof (p[0])); p->log2_n_stack_bytes = log2_n_stack_bytes; - stack_bytes = 1ULL << log2_n_stack_bytes; - /* map stack size + 2 extra guard pages */ - map = mmap (0, stack_bytes + page_size, PROT_READ | PROT_WRITE, - mmap_flags, -1, 0); + p->stack = clib_mem_vm_map_stack (1ULL << log2_n_stack_bytes, + CLIB_MEM_PAGE_SZ_DEFAULT, + "process stack: %U", + format_vlib_node_name, vm, + n->index); - if (map == MAP_FAILED) + if (p->stack == CLIB_MEM_VM_MAP_FAILED) clib_panic ("failed to allocate process stack (%d bytes)", - stack_bytes); - - /* skip the guard page */ - p->stack = map + page_size; - - mmap_flags |= MAP_FIXED; - map = mmap (map, page_size, PROT_NONE, mmap_flags, -1, 0); - - if (map == MAP_FAILED) - clib_unix_warning ("failed to create stack guard page"); + 1ULL << log2_n_stack_bytes); /* Process node's runtime index is really index into process pointer vector. */ diff --git a/src/vlib/unix/main.c b/src/vlib/unix/main.c index 95aeecf2da2..9dde666bbef 100644 --- a/src/vlib/unix/main.c +++ b/src/vlib/unix/main.c @@ -666,18 +666,17 @@ thread0 (uword arg) u8 * vlib_thread_stack_init (uword thread_index) { + void *stack; ASSERT (thread_index < vec_len (vlib_thread_stacks)); - vlib_thread_stacks[thread_index] = clib_mem_alloc_aligned - (VLIB_THREAD_STACK_SIZE, clib_mem_get_page_size ()); + stack = clib_mem_vm_map_stack (VLIB_THREAD_STACK_SIZE, + CLIB_MEM_PAGE_SZ_DEFAULT, + "thread stack: thread %u", thread_index); - /* - * Disallow writes to the bottom page of the stack, to - * catch stack overflows. - */ - if (mprotect (vlib_thread_stacks[thread_index], - clib_mem_get_page_size (), PROT_READ) < 0) - clib_unix_warning ("thread stack"); - return vlib_thread_stacks[thread_index]; + if (stack == CLIB_MEM_VM_MAP_FAILED) + clib_panic ("failed to allocate thread %u stack", thread_index); + + vlib_thread_stacks[thread_index] = stack; + return stack; } int -- cgit 1.2.3-korg