For portabiliy reasons it is better to have all wrapped in clib code.
I.e. instead of using getcpu() we have clib_get_current_numa_node () and
clib_get_current_cpu_id().
Type: refactor
Change-Id: I29b52d7f29bc7f93873402c4070561f564b71c63
Signed-off-by: Damjan Marion <damarion@cisco.com>
-static \
-I ${src} \
-I ${tmp} \
- -DHAVE_MEMFD_CREATE \
- -DHAVE_GETCPU \
${src}/vppinfra/backtrace.c \
${src}/vppinfra/dlmalloc.c \
${src}/vppinfra/elf.c \
# See the License for the specific language governing permissions and
# limitations under the License.
-##############################################################################
-# Check for memfd_create headers and libs
-##############################################################################
-check_c_source_compiles("
- #define _GNU_SOURCE
- #include <sys/mman.h>
- int main() { return memfd_create (\"/dev/false\", 0); }
-" HAVE_MEMFD_CREATE)
-
-if (HAVE_MEMFD_CREATE)
- add_definitions(-DHAVE_MEMFD_CREATE)
-endif()
-
-check_c_source_compiles("
- #define _GNU_SOURCE
- #include <sched.h>
- int main() { return getcpu (0, 0); }
-" HAVE_GETCPU)
-
-if (HAVE_GETCPU)
- add_definitions(-DHAVE_GETCPU)
-endif()
-
check_c_source_compiles("
#define _GNU_SOURCE
#include <fcntl.h>
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
-#include <vppinfra/linux/syscall.h>
#include <vnet/plugin/plugin.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/interface/rx_queue_funcs.h>
#include <sys/stat.h>
#include <unistd.h>
-#include <vppinfra/linux/syscall.h>
#include <vppinfra/linux/sysfs.h>
#include <vlib/vlib.h>
#include <vlib/physmem.h>
#include "stat_segment.h"
#include <vnet/vnet.h>
#include <vnet/devices/devices.h> /* vnet_get_aggregate_rx_packets */
-#undef HAVE_MEMFD_CREATE
-#include <vppinfra/linux/syscall.h>
#include <vpp-api/client/stat_client.h>
stat_segment_main_t stat_segment_main;
vector_sse42.h
warnings.h
xxhash.h
- linux/syscall.h
linux/sysfs.h
)
ASSERT (memory_size < (1ULL << 32));
/* Set up for memfd sharing */
- if ((fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1)
+ if ((fd = clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT, name) == -1)
{
clib_unix_warning ("memfd_create");
return;
#endif
#ifdef BIHASH_32_64_SVM
-#undef HAVE_MEMFD_CREATE
#include <vppinfra/linux/syscall.h>
#include <fcntl.h>
#define F_LINUX_SPECIFIC_BASE 1024
#endif
}
+__clib_export u32
+clib_get_current_cpu_id ()
+{
+ unsigned cpu, node;
+ syscall (__NR_getcpu, &cpu, &node, 0);
+ return cpu;
+}
+__clib_export u32
+clib_get_current_numa_node ()
+{
+ unsigned cpu, node;
+ syscall (__NR_getcpu, &cpu, &node, 0);
+ return node;
+}
/*
* fd.io coding-style-patch-verification: ON
_ (sha512, 21) \
_ (sve, 22)
-static inline u32
-clib_get_current_cpu_id ()
-{
- unsigned cpu, node;
- syscall (__NR_getcpu, &cpu, &node, 0);
- return cpu;
-}
-
-static inline u32
-clib_get_current_numa_node ()
-{
- unsigned cpu, node;
- syscall (__NR_getcpu, &cpu, &node, 0);
- return node;
-}
+u32 clib_get_current_cpu_id ();
+u32 clib_get_current_numa_node ();
#if defined(__x86_64__)
#include "cpuid.h"
#include <vppinfra/time.h>
#include <vppinfra/format.h>
#include <vppinfra/clib_error.h>
-#include <vppinfra/linux/syscall.h>
#include <vppinfra/linux/sysfs.h>
#ifndef F_LINUX_SPECIFIC_BASE
mm->log2_page_sz = min_log2 (page_size);
/* default system hugeppage size */
- if ((fd = memfd_create ("test", MFD_HUGETLB)) != -1)
+ if ((fd = syscall (__NR_memfd_create, "test", MFD_HUGETLB)) != -1)
{
mm->log2_default_hugepage_sz = clib_mem_get_fd_log2_page_size (fd);
close (fd);
for (int i = 0; i < CLIB_MAX_NUMAS; i++)
{
int status;
- if (move_pages (0, 1, &va, &i, &status, 0) == 0)
+ if (syscall (__NR_move_pages, 0, 1, &va, &i, &status, 0) == 0)
mm->numa_node_bitmap |= 1ULL << i;
}
vec_add1 (s, 0);
/* memfd_create introduced in kernel 3.17, we don't support older kernels */
- fd = memfd_create ((char *) s, memfd_flags);
+ fd = syscall (__NR_memfd_create, (char *) s, memfd_flags);
/* kernel versions < 4.14 does not support memfd_create for huge pages */
if (fd == -1 && errno == EINVAL &&
stats->total = n_pages;
stats->log2_page_sz = log2_page_size;
- if (move_pages (0, n_pages, ptr, 0, status, 0) != 0)
+ if (syscall (__NR_move_pages, 0, n_pages, ptr, 0, status, 0) != 0)
{
stats->unknown = n_pages;
goto done;
mask[0] = 1 << numa_node;
- if (set_mempolicy (force ? MPOL_BIND : MPOL_PREFERRED, mask, mask_len))
+ if (syscall (__NR_set_mempolicy, force ? MPOL_BIND : MPOL_PREFERRED, mask,
+ mask_len))
goto error;
vec_reset_length (mm->error);
{
clib_mem_main_t *mm = &clib_mem_main;
- if (set_mempolicy (MPOL_DEFAULT, 0, 0))
+ if (syscall (__NR_set_mempolicy, MPOL_DEFAULT, 0, 0))
{
vec_reset_length (mm->error);
mm->error = clib_error_return_unix (mm->error, (char *) __func__);
+++ /dev/null
-/*
- * Copyright (c) 2017 Cisco and/or its affiliates.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef included_linux_syscall_h
-#define included_linux_syscall_h
-
-#include <unistd.h>
-#include <sys/syscall.h>
-
-#ifndef HAVE_GETCPU
-static inline int
-getcpu (unsigned *cpu, unsigned *node)
-{
- return syscall (__NR_getcpu, cpu, node, 0);
-}
-#endif
-
-static inline long
-set_mempolicy (int mode, const unsigned long *nodemask, unsigned long maxnode)
-{
- return syscall (__NR_set_mempolicy, mode, nodemask, maxnode);
-}
-
-static inline int
-get_mempolicy (int *mode, unsigned long *nodemask, unsigned long maxnode,
- void *addr, unsigned long flags)
-{
- return syscall (__NR_get_mempolicy, mode, nodemask, maxnode, addr, flags);
-}
-
-static inline long
-move_pages (int pid, unsigned long count, void **pages, const int *nodes,
- int *status, int flags)
-{
- return syscall (__NR_move_pages, pid, count, pages, nodes, status, flags);
-}
-
-#ifndef HAVE_MEMFD_CREATE
-static inline int
-memfd_create (const char *name, unsigned int flags)
-{
- return syscall (__NR_memfd_create, name, flags);
-}
-#endif
-
-#endif /* included_linux_syscall_h */
-
-/*
- * fd.io coding-style-patch-verification: ON
- *
- * Local Variables:
- * eval: (c-set-style "gnu")
- * End:
- */
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
-#include <linux/mempolicy.h>
-#include <linux/memfd.h>
#include <sched.h>
#include <vppinfra/format.h>
-#include <vppinfra/linux/syscall.h>
#include <vppinfra/linux/sysfs.h>
#include <vppinfra/mem.h>
#include <vppinfra/hash.h>
#include <vppinfra/pmalloc.h>
+#include <vppinfra/cpu.h>
#if __SIZEOF_POINTER__ >= 8
#define DEFAULT_RESERVED_MB 16384
return round_pow2 (size, 1ULL << log2_page_sz) >> log2_page_sz;
}
-static inline int
-pmalloc_validate_numa_node (u32 * numa_node)
-{
- if (*numa_node == CLIB_PMALLOC_NUMA_LOCAL)
- {
- u32 cpu;
- if (getcpu (&cpu, numa_node) != 0)
- return 1;
- }
- return 0;
-}
-
__clib_export int
clib_pmalloc_init (clib_pmalloc_main_t * pm, uword base_addr, uword size)
{
pmalloc_map_pages (clib_pmalloc_main_t * pm, clib_pmalloc_arena_t * a,
u32 numa_node, u32 n_pages)
{
+ clib_mem_page_stats_t stats = {};
clib_pmalloc_page_t *pp = 0;
- int status, rv, i, mmap_flags;
+ int rv, i, mmap_flags;
void *va = MAP_FAILED;
- int old_mpol = -1;
- long unsigned int mask[16] = { 0 };
- long unsigned int old_mask[16] = { 0 };
uword size = (uword) n_pages << pm->def_log2_page_sz;
clib_error_free (pm->error);
return 0;
}
- rv = get_mempolicy (&old_mpol, old_mask, sizeof (old_mask) * 8 + 1, 0, 0);
- /* failure to get mempolicy means we can only proceed with numa 0 maps */
- if (rv == -1 && numa_node != 0)
- {
- pm->error = clib_error_return_unix (0, "failed to get mempolicy");
- return 0;
- }
-
- mask[0] = 1 << numa_node;
- rv = set_mempolicy (MPOL_BIND, mask, sizeof (mask) * 8 + 1);
- if (rv == -1 && numa_node != 0)
+ rv = clib_mem_set_numa_affinity (numa_node, /* force */ 1);
+ if (rv == CLIB_MEM_ERROR && numa_node != 0)
{
pm->error = clib_error_return_unix (0, "failed to set mempolicy for "
"numa node %u", numa_node);
clib_memset (va, 0, size);
- rv = set_mempolicy (old_mpol, old_mask, sizeof (old_mask) * 8 + 1);
- if (rv == -1 && numa_node != 0)
+ rv = clib_mem_set_default_numa_affinity ();
+ if (rv == CLIB_MEM_ERROR && numa_node != 0)
{
pm->error = clib_error_return_unix (0, "failed to restore mempolicy");
goto error;
/* we tolerate move_pages failure only if request os for numa node 0
to support non-numa kernels */
- rv = move_pages (0, 1, &va, 0, &status, 0);
- if ((rv == 0 && status != numa_node) || (rv != 0 && numa_node != 0))
+ clib_mem_get_page_stats (va, CLIB_MEM_PAGE_SZ_DEFAULT, 1, &stats);
+
+ if (stats.per_numa[numa_node] != 1)
{
- pm->error = rv == -1 ?
- clib_error_return_unix (0, "page allocated on wrong node, numa node "
- "%u status %d", numa_node, status) :
- clib_error_return (0, "page allocated on wrong node, numa node "
- "%u status %d", numa_node, status);
+ u16 allocated_at = ~0;
+ if (stats.unknown)
+ clib_error_return (0,
+ "unable to get information about numa allocation");
+
+ for (u16 i = 0; i < CLIB_MAX_NUMAS; i++)
+ if (stats.per_numa[i] == 1)
+ allocated_at = i;
+
+ clib_error_return (0,
+ "page allocated on the wrong numa node (%u), "
+ "expected %u",
+ allocated_at, numa_node);
goto error;
}
if (n_pages + vec_len (pm->pages) > pm->max_pages)
return 0;
- if (pmalloc_validate_numa_node (&numa_node))
- return 0;
+ if (numa_node == CLIB_PMALLOC_NUMA_LOCAL)
+ numa_node = clib_get_current_numa_node ();
pool_get (pm->arenas, a);
a->index = a - pm->arenas;
ASSERT (is_pow2 (align));
- if (pmalloc_validate_numa_node (&numa_node))
- return 0;
+ if (numa_node == CLIB_PMALLOC_NUMA_LOCAL)
+ numa_node = clib_get_current_numa_node ();
if (a == 0)
{
#include <vppinfra/time.h>
#if __linux__
-#include <vppinfra/linux/syscall.h>
-
#ifdef AF_NETLINK
#include <linux/types.h>
#include <linux/netlink.h>