|
| 1 | +/* |
| 2 | + * crun - OCI runtime written in C |
| 3 | + * |
| 4 | + * Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <[email protected]> |
| 5 | + * crun is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation; either version 2.1 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * crun is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with crun. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <config.h> |
| 20 | +#include "linux.h" |
| 21 | +#include "utils.h" |
| 22 | +#include <ocispec/runtime_spec_schema_config_schema.h> |
| 23 | + |
| 24 | +#ifdef HAVE_NUMA |
| 25 | +# include <numa.h> |
| 26 | +# include <numaif.h> |
| 27 | +# include "mempolicy_internal.h" |
| 28 | + |
| 29 | +# define CRUN_NUMA_API_VERSION 2 /* numa.h LIBNUMA_API_VERSION at the time of writing */ |
| 30 | +# define CRUN_NUMA_MPOL_MAX 7 /* numaif.h MPOL_MAX at the time of writing */ |
| 31 | + |
| 32 | +# ifndef LIBNUMA_API_VERSION |
| 33 | +# error "Unable to determine libnuma api version" |
| 34 | +# else |
| 35 | +# if LIBNUMA_API_VERSION > CRUN_NUMA_API_VERSION |
| 36 | +# warning "This code was written with libnuma API version 2. numa.h reports a higher version" |
| 37 | +# endif |
| 38 | +# endif |
| 39 | +# ifndef MPOL_MAX |
| 40 | +# error "Unable to determine numaif interface version" |
| 41 | +# else |
| 42 | +# if MPOL_MAX > CRUN_NUMA_MPOL_MAX |
| 43 | +# warning "This code was written with numaif MPOL_MAX 7. numaif.h reports a higher version" |
| 44 | +# endif |
| 45 | +# endif |
| 46 | + |
| 47 | +static int |
| 48 | +mpol_str2int (const char *str, const str2int_map_t *map) |
| 49 | +{ |
| 50 | + int idx = 0; |
| 51 | + |
| 52 | + while (map[idx].name != NULL) |
| 53 | + { |
| 54 | + if (! strcmp (map[idx].name, str)) |
| 55 | + { |
| 56 | + return map[idx].value; |
| 57 | + } |
| 58 | + idx++; |
| 59 | + } |
| 60 | + |
| 61 | + errno = EINVAL; |
| 62 | + return -1; |
| 63 | +} |
| 64 | +#endif |
| 65 | + |
| 66 | +int |
| 67 | +libcrun_set_mempolicy (runtime_spec_schema_config_schema *def, libcrun_error_t *err) |
| 68 | +{ |
| 69 | +#ifdef HAVE_NUMA |
| 70 | + runtime_spec_schema_config_linux_memory_policy *memory_policy = NULL; |
| 71 | + int mpol_mode = 0; |
| 72 | + int mpol_flag = 0; |
| 73 | + int mpol_mode_flags = 0; |
| 74 | + struct bitmask *nodemask = NULL; |
| 75 | + size_t i = 0; |
| 76 | + int ret = 0; |
| 77 | + int savederrno = 0; |
| 78 | + |
| 79 | + libcrun_debug ("Initializing linux numa mempolicy"); |
| 80 | + |
| 81 | + if (def->linux && def->linux->memory_policy) |
| 82 | + { |
| 83 | + memory_policy = def->linux->memory_policy; |
| 84 | + |
| 85 | + libcrun_debug ("Checking hardware numa availability"); |
| 86 | + if (numa_available () < 0) |
| 87 | + { |
| 88 | + return crun_make_error (err, ENOENT, "linux numa not supported on current hardware"); |
| 89 | + } |
| 90 | + |
| 91 | + libcrun_debug ("Validating linux numa mempolicy"); |
| 92 | + |
| 93 | + /* validate memory policy mode */ |
| 94 | + if (! memory_policy->mode) |
| 95 | + { |
| 96 | + return crun_make_error (err, EINVAL, "linux numa mempolicy mode is missing from the configuration"); |
| 97 | + } |
| 98 | + libcrun_debug ("Validating mode: %s", memory_policy->mode); |
| 99 | + mpol_mode = mpol_str2int (memory_policy->mode, mpol_mode_map); |
| 100 | + if (mpol_mode < 0) |
| 101 | + { |
| 102 | + return crun_make_error (err, EINVAL, "Requested linux numa mempolicy mode '%s' is unknown", memory_policy->mode); |
| 103 | + } |
| 104 | + mpol_mode_flags = mpol_mode; |
| 105 | + |
| 106 | + /* both MPOL_DEFAULT and MPOL_LOCAL calls to set_mempolicy expects only |
| 107 | + * the mpol_mode, no nodemask or flags */ |
| 108 | + if (mpol_mode != MPOL_DEFAULT && mpol_mode != MPOL_LOCAL) |
| 109 | + { |
| 110 | + /* validating memory policy flags */ |
| 111 | + libcrun_debug ("Validating mode flags: %zu configured", memory_policy->flags_len); |
| 112 | + for (i = 0; i < memory_policy->flags_len; i++) |
| 113 | + { |
| 114 | + libcrun_debug ("Validating mode flag: %s", memory_policy->flags[i]); |
| 115 | + mpol_flag = mpol_str2int (memory_policy->flags[i], mpol_flag_map); |
| 116 | + if (mpol_flag < 0) |
| 117 | + { |
| 118 | + return crun_make_error (err, EINVAL, "Requested linux numa mempolicy flag '%s' is unknown", memory_policy->flags[i]); |
| 119 | + } |
| 120 | + mpol_mode_flags = mpol_mode_flags | mpol_flag; |
| 121 | + } |
| 122 | + |
| 123 | + /* sanity check mode and flags combinations */ |
| 124 | +# if defined MPOL_F_NUMA_BALANCING |
| 125 | + if ((mpol_mode_flags & MPOL_F_NUMA_BALANCING) && mpol_mode != MPOL_BIND) |
| 126 | + { |
| 127 | + return crun_make_error (err, EINVAL, "Requested linux numa mempolicy flag MPOL_F_NUMA_BALANCING is incompatible with %s", memory_policy->mode); |
| 128 | + } |
| 129 | +# endif |
| 130 | +# if defined MPOL_F_RELATIVE_NODES && defined MPOL_F_STATIC_NODES |
| 131 | + if ((mpol_mode_flags & MPOL_F_RELATIVE_NODES) && (mpol_mode_flags & MPOL_F_STATIC_NODES)) |
| 132 | + { |
| 133 | + return crun_make_error (err, EINVAL, "Requested linux numa mempolicy flag MPOL_F_RELATIVE_NODES and MPOL_F_STATIC_NODES cannot be combined"); |
| 134 | + } |
| 135 | +# endif |
| 136 | + /* validate memory nodes */ |
| 137 | + if (! memory_policy->nodes) |
| 138 | + { |
| 139 | + return crun_make_error (err, EINVAL, "linux numa mempolicy nodes is missing from the configuration"); |
| 140 | + } |
| 141 | + libcrun_debug ("Validating nodes: %s", memory_policy->nodes); |
| 142 | + /* validation is done by libnuma based on hw environment |
| 143 | + * and numa_warn symbol is overridden in error.c to convert |
| 144 | + * numa logging to libcrun logging */ |
| 145 | + nodemask = numa_parse_nodestring_all (memory_policy->nodes); |
| 146 | + if (! nodemask) |
| 147 | + { |
| 148 | + return crun_make_error (err, EINVAL, "numa_parse_nodestring_all validation failed"); |
| 149 | + } |
| 150 | + |
| 151 | + ret = set_mempolicy (mpol_mode_flags, nodemask->maskp, nodemask->size - 1); |
| 152 | + savederrno = errno; |
| 153 | + numa_bitmask_free (nodemask); |
| 154 | + errno = savederrno; |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + ret = set_mempolicy (mpol_mode, NULL, 0); |
| 159 | + } |
| 160 | + |
| 161 | + if (ret < 0) |
| 162 | + { |
| 163 | + return crun_make_error (err, errno, "set_mempolicy: %d errno: %d\n", ret, errno); |
| 164 | + } |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + libcrun_debug ("no linux numa mempolicy configuration found"); |
| 169 | + } |
| 170 | +#endif |
| 171 | + return ret; |
| 172 | +} |
0 commit comments