|
| 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 | +#include <sys/syscall.h> |
| 25 | +#include <unistd.h> |
| 26 | + |
| 27 | +/* follow runc go implementation and redefine everything here */ |
| 28 | +/* Policies */ |
| 29 | +#define MPOL_DEFAULT 0 |
| 30 | +#define MPOL_PREFERRED 1 |
| 31 | +#define MPOL_BIND 2 |
| 32 | +#define MPOL_INTERLEAVE 3 |
| 33 | +#define MPOL_LOCAL 4 |
| 34 | +#define MPOL_PREFERRED_MANY 5 |
| 35 | +#define MPOL_WEIGHTED_INTERLEAVE 6 |
| 36 | + |
| 37 | +/* Flags for set_mempolicy, specified in mode */ |
| 38 | +#define MPOL_F_NUMA_BALANCING (1 << 13) |
| 39 | +#define MPOL_F_RELATIVE_NODES (1 << 14) |
| 40 | +#define MPOL_F_STATIC_NODES (1 << 15) |
| 41 | + |
| 42 | +typedef struct |
| 43 | +{ |
| 44 | + const char *name; |
| 45 | + int value; |
| 46 | +} str2int_map_t; |
| 47 | + |
| 48 | +/* update mpol_mode_map based on numaif.h MPOL_MAX |
| 49 | + * the warn in mempolicy.c will indicate that an update is required. |
| 50 | + * MPOL_WEIGHTED_INTERLEAVE has been introduced in MPOL_MAX 7 (kernel 6.9+) |
| 51 | + * and some distros still has older kernel interfaces */ |
| 52 | +str2int_map_t mpol_mode_map[] = { |
| 53 | + { "MPOL_DEFAULT", MPOL_DEFAULT }, |
| 54 | + { "MPOL_PREFERRED", MPOL_PREFERRED }, |
| 55 | + { "MPOL_BIND", MPOL_BIND }, |
| 56 | + { "MPOL_INTERLEAVE", MPOL_INTERLEAVE }, |
| 57 | + { "MPOL_LOCAL", MPOL_LOCAL }, |
| 58 | + { "MPOL_PREFERRED_MANY", MPOL_PREFERRED_MANY }, |
| 59 | + { "MPOL_WEIGHTED_INTERLEAVE", MPOL_WEIGHTED_INTERLEAVE }, |
| 60 | + { NULL, -1 } |
| 61 | +}; |
| 62 | + |
| 63 | +/* flags cannot be tracked the same way as mode */ |
| 64 | +str2int_map_t mpol_flag_map[] = { |
| 65 | + { "MPOL_F_NUMA_BALANCING", MPOL_F_NUMA_BALANCING }, |
| 66 | + { "MPOL_F_RELATIVE_NODES", MPOL_F_RELATIVE_NODES }, |
| 67 | + { "MPOL_F_STATIC_NODES", MPOL_F_STATIC_NODES }, |
| 68 | + { NULL, -1 } |
| 69 | +}; |
| 70 | + |
| 71 | +#define MAX_NUMA_NODES 4096 |
| 72 | + |
| 73 | +static int |
| 74 | +mpol_str2int (const char *str, const str2int_map_t *map) |
| 75 | +{ |
| 76 | + int idx = 0; |
| 77 | + |
| 78 | + while (map[idx].name != NULL) |
| 79 | + { |
| 80 | + if (! strcmp (map[idx].name, str)) |
| 81 | + return map[idx].value; |
| 82 | + |
| 83 | + idx++; |
| 84 | + } |
| 85 | + |
| 86 | + errno = EINVAL; |
| 87 | + return -1; |
| 88 | +} |
| 89 | + |
| 90 | +int |
| 91 | +libcrun_set_mempolicy (runtime_spec_schema_config_schema *def, libcrun_error_t *err) |
| 92 | +{ |
| 93 | + runtime_spec_schema_config_linux_memory_policy *memory_policy = NULL; |
| 94 | + int mpol_mode = 0; |
| 95 | + int mpol_flag = 0; |
| 96 | + size_t i = 0; |
| 97 | + unsigned long nmask[MAX_NUMA_NODES / (sizeof (unsigned long) * 8)]; |
| 98 | + unsigned long *nmask_final = NULL; |
| 99 | + int ret = 0; |
| 100 | + |
| 101 | + if (def->linux == NULL || def->linux->memory_policy == NULL) |
| 102 | + { |
| 103 | + libcrun_debug ("no linux numa mempolicy configuration found"); |
| 104 | + return ret; |
| 105 | + } |
| 106 | + |
| 107 | + libcrun_debug ("Initializing linux numa mempolicy"); |
| 108 | + |
| 109 | + memory_policy = def->linux->memory_policy; |
| 110 | + |
| 111 | + libcrun_debug ("Validating linux numa mempolicy"); |
| 112 | + /* validate memory policy mode */ |
| 113 | + if (! memory_policy->mode) |
| 114 | + return crun_make_error (err, EINVAL, "linux NUMA mempolicy mode is missing from the configuration"); |
| 115 | + |
| 116 | + libcrun_debug ("Validating mode: %s", memory_policy->mode); |
| 117 | + mpol_mode = mpol_str2int (memory_policy->mode, mpol_mode_map); |
| 118 | + if (mpol_mode < 0) |
| 119 | + return crun_make_error (err, EINVAL, "requested linux NUMA mempolicy mode '%s' is unknown", memory_policy->mode); |
| 120 | + |
| 121 | + /* validating memory policy flags */ |
| 122 | + libcrun_debug ("Validating mode flags: %zu configured", memory_policy->flags_len); |
| 123 | + for (i = 0; i < memory_policy->flags_len; i++) |
| 124 | + { |
| 125 | + libcrun_debug ("Validating mode flag: %s", memory_policy->flags[i]); |
| 126 | + mpol_flag = mpol_str2int (memory_policy->flags[i], mpol_flag_map); |
| 127 | + if (mpol_flag < 0) |
| 128 | + return crun_make_error (err, EINVAL, "requested linux NUMA mempolicy flag '%s' is unknown", memory_policy->flags[i]); |
| 129 | + mpol_mode = mpol_mode | mpol_flag; |
| 130 | + } |
| 131 | + |
| 132 | + /* kernel will take care of validating the nodes */ |
| 133 | + if (memory_policy->nodes) |
| 134 | + { |
| 135 | + cleanup_free char *bitmask = NULL; |
| 136 | + size_t bitmask_size; |
| 137 | + |
| 138 | + ret = cpuset_string_to_bitmask (memory_policy->nodes, &bitmask, &bitmask_size, err); |
| 139 | + if (UNLIKELY (ret < 0)) |
| 140 | + return ret; |
| 141 | + |
| 142 | + if (bitmask_size > sizeof (nmask)) |
| 143 | + return crun_make_error (err, EINVAL, "requested NUMA bitmask bigger than kernel supported bitmask"); |
| 144 | + |
| 145 | + nmask_final = nmask; |
| 146 | + memset (nmask_final, 0, sizeof (nmask)); |
| 147 | + memcpy (nmask_final, bitmask, bitmask_size); |
| 148 | + } |
| 149 | + |
| 150 | + if (syscall (__NR_set_mempolicy, mpol_mode, nmask_final, nmask_final ? MAX_NUMA_NODES - 1 : 0) < 0) |
| 151 | + return crun_make_error (err, errno, "set_mempolicy"); |
| 152 | + |
| 153 | + return ret; |
| 154 | +} |
0 commit comments