| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#define _GNU_SOURCE |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <unistd.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <string.h> |
| 27 |
#include <sys/utsname.h> |
| 28 |
#include "kernel-features.h" |
| 29 |
#include "hugetlbfs.h" |
| 30 |
#include "libhugetlbfs_privutils.h" |
| 31 |
#include "libhugetlbfs_internal.h" |
| 32 |
#include "libhugetlbfs_debug.h" |
| 33 |
|
| 34 |
static struct kernel_version running_kernel_version; |
| 35 |
|
| 36 |
|
| 37 |
static unsigned int feature_mask; |
| 38 |
|
| 39 |
static struct feature kernel_features[] = { |
| 40 |
[HUGETLB_FEATURE_PRIVATE_RESV] = { |
| 41 |
.name = "private_reservations", |
| 42 |
.required_version = "2.6.27-rc1", |
| 43 |
}, |
| 44 |
[HUGETLB_FEATURE_SAFE_NORESERVE] = { |
| 45 |
.name = "noreserve_safe", |
| 46 |
.required_version = "2.6.34", |
| 47 |
}, |
| 48 |
[HUGETLB_FEATURE_MAP_HUGETLB] = { |
| 49 |
.name = "map_hugetlb", |
| 50 |
.required_version = "2.6.32", |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
static void debug_kernel_version(void) |
| 55 |
{ |
| 56 |
struct kernel_version *ver = &running_kernel_version; |
| 57 |
|
| 58 |
INFO("Parsed kernel version: [%u] . [%u] . [%u] ", |
| 59 |
ver->major, ver->minor, ver->release); |
| 60 |
if (ver->post) |
| 61 |
INFO_CONT(" [post-release: %u]\n", ver->post); |
| 62 |
else if (ver->pre) |
| 63 |
INFO_CONT(" [pre-release: %u]\n", ver->pre); |
| 64 |
else |
| 65 |
INFO_CONT("\n"); |
| 66 |
} |
| 67 |
|
| 68 |
static int str_to_ver(const char *str, struct kernel_version *ver) |
| 69 |
{ |
| 70 |
char *start; |
| 71 |
char *end; |
| 72 |
|
| 73 |
|
| 74 |
ver->major = ver->minor = ver->release = ver->post = ver->pre = 0; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
errno = 0; |
| 86 |
ver->major = strtol(str, &end, 10); |
| 87 |
if (!ver->major && errno == EINVAL) { |
| 88 |
ERROR("Unable to determine base kernel version: %s\n", |
| 89 |
strerror(errno)); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
|
| 93 |
start = end + 1; |
| 94 |
errno = 0; |
| 95 |
ver->minor = strtol(start, &end, 10); |
| 96 |
if (!ver->minor && errno == EINVAL) { |
| 97 |
ERROR("Unable to determine base kernel version: %s\n", |
| 98 |
strerror(errno)); |
| 99 |
return -1; |
| 100 |
} |
| 101 |
|
| 102 |
start = end + 1; |
| 103 |
errno = 0; |
| 104 |
ver->release = strtol(start, &end, 10); |
| 105 |
if (!ver->release && errno == EINVAL) { |
| 106 |
ERROR("Unable to determine base kernel version: %s\n", |
| 107 |
strerror(errno)); |
| 108 |
return -1; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
start = end + 1; |
| 113 |
if (*end == '.') { |
| 114 |
ver->post = strtol(start, &end, 10); |
| 115 |
if (!ver->post && errno == EINVAL) |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
start = end + 1; |
| 121 |
if (*end == '-') { |
| 122 |
if (*start == 'r' && *(start + 1) == 'c') |
| 123 |
start += 2; |
| 124 |
else if (*start == 'p' && |
| 125 |
*(start + 1) == 'r' && |
| 126 |
*(start + 2) == 'e') |
| 127 |
start += 3; |
| 128 |
else { |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
ver->pre = strtol(start, &end, 10); |
| 138 |
} |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
static int int_cmp(int a, int b) |
| 144 |
{ |
| 145 |
if (a < b) |
| 146 |
return -1; |
| 147 |
if (a > b) |
| 148 |
return 1; |
| 149 |
else |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
static int ver_cmp_release(struct kernel_version *ver) |
| 160 |
{ |
| 161 |
if (ver->pre) |
| 162 |
return ver->release - 1; |
| 163 |
else |
| 164 |
return ver->release; |
| 165 |
} |
| 166 |
|
| 167 |
static int ver_cmp(struct kernel_version *a, struct kernel_version *b) |
| 168 |
{ |
| 169 |
int ret, a_release, b_release; |
| 170 |
|
| 171 |
if ((ret = int_cmp(a->major, b->major)) != 0) |
| 172 |
return ret; |
| 173 |
|
| 174 |
if ((ret = int_cmp(a->minor, b->minor)) != 0) |
| 175 |
return ret; |
| 176 |
|
| 177 |
a_release = ver_cmp_release(a); |
| 178 |
b_release = ver_cmp_release(b); |
| 179 |
if ((ret = int_cmp(a_release, b_release)) != 0) |
| 180 |
return ret; |
| 181 |
|
| 182 |
if ((ret = int_cmp(a->post, b->post)) != 0) |
| 183 |
return ret; |
| 184 |
|
| 185 |
if ((ret = int_cmp(a->pre, b->pre)) != 0) |
| 186 |
return ret; |
| 187 |
|
| 188 |
|
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
int test_compare_kver(const char *a, const char *b) |
| 193 |
{ |
| 194 |
struct kernel_version ka, kb; |
| 195 |
|
| 196 |
if (str_to_ver(a, &ka) < 0) |
| 197 |
return -EINVAL; |
| 198 |
if (str_to_ver(b, &kb) < 0) |
| 199 |
return -EINVAL; |
| 200 |
return ver_cmp(&ka, &kb); |
| 201 |
} |
| 202 |
|
| 203 |
int hugetlbfs_test_feature(int feature_code) |
| 204 |
{ |
| 205 |
if (feature_code >= HUGETLB_FEATURE_NR) { |
| 206 |
ERROR("hugetlbfs_test_feature: invalid feature code\n"); |
| 207 |
return -EINVAL; |
| 208 |
} |
| 209 |
return feature_mask & (1 << feature_code); |
| 210 |
} |
| 211 |
|
| 212 |
static void print_valid_features(void) |
| 213 |
{ |
| 214 |
int i; |
| 215 |
|
| 216 |
ERROR("HUGETLB_FEATURES=\"<feature>[,<feature>] ...\"\n"); |
| 217 |
ERROR_CONT("Valid features:\n"); |
| 218 |
for (i = 0; i < HUGETLB_FEATURE_NR; i++) |
| 219 |
ERROR_CONT("\t%s, no_%s\n", kernel_features[i].name, |
| 220 |
kernel_features[i].name); |
| 221 |
} |
| 222 |
|
| 223 |
static int check_features_env_valid(const char *env) |
| 224 |
{ |
| 225 |
const char *pos = env; |
| 226 |
int i; |
| 227 |
|
| 228 |
while (pos && *pos != '\0') { |
| 229 |
int match = 0; |
| 230 |
char *next; |
| 231 |
|
| 232 |
if (*pos == ',') |
| 233 |
pos++; |
| 234 |
next = strchrnul(pos, ','); |
| 235 |
if (strncmp(pos, "no_", 3) == 0) |
| 236 |
pos += 3; |
| 237 |
|
| 238 |
for (i = 0; i < HUGETLB_FEATURE_NR; i++) { |
| 239 |
char *name = kernel_features[i].name; |
| 240 |
if (strncmp(pos, name, next - pos) == 0) { |
| 241 |
match = 1; |
| 242 |
break; |
| 243 |
} |
| 244 |
} |
| 245 |
if (!match) { |
| 246 |
print_valid_features(); |
| 247 |
return -1; |
| 248 |
} |
| 249 |
pos = next; |
| 250 |
} |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
void setup_features() |
| 255 |
{ |
| 256 |
struct utsname u; |
| 257 |
int i; |
| 258 |
|
| 259 |
if (uname(&u)) { |
| 260 |
ERROR("Getting kernel version failed: %s\n", strerror(errno)); |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
str_to_ver(u.release, &running_kernel_version); |
| 265 |
debug_kernel_version(); |
| 266 |
|
| 267 |
|
| 268 |
if (__hugetlb_opts.features && |
| 269 |
check_features_env_valid(__hugetlb_opts.features) == -1) { |
| 270 |
ERROR("HUGETLB_FEATURES was invalid -- ignoring.\n"); |
| 271 |
__hugetlb_opts.features = NULL; |
| 272 |
} |
| 273 |
|
| 274 |
for (i = 0; i < HUGETLB_FEATURE_NR; i++) { |
| 275 |
struct kernel_version ver; |
| 276 |
char *name = kernel_features[i].name; |
| 277 |
char *pos; |
| 278 |
|
| 279 |
str_to_ver(kernel_features[i].required_version, &ver); |
| 280 |
|
| 281 |
|
| 282 |
if (__hugetlb_opts.features && |
| 283 |
(pos = strstr(__hugetlb_opts.features, name))) { |
| 284 |
INFO("Overriding feature %s: ", name); |
| 285 |
|
| 286 |
if (((pos - 3) >= __hugetlb_opts.features) && |
| 287 |
!strncmp(pos - 3, "no_", 3)) |
| 288 |
INFO_CONT("no\n"); |
| 289 |
else { |
| 290 |
INFO_CONT("yes\n"); |
| 291 |
feature_mask |= (1UL << i); |
| 292 |
} |
| 293 |
continue; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
if (ver_cmp(&running_kernel_version, &ver) >= 0) { |
| 298 |
INFO("Feature %s is present in this kernel\n", |
| 299 |
kernel_features[i].name); |
| 300 |
feature_mask |= (1UL << i); |
| 301 |
} |
| 302 |
} |
| 303 |
} |