forked from libhugetlbfs/libhugetlbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel-features.c
303 lines (262 loc) · 7.21 KB
/
kernel-features.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* libhugetlbfs - Easy use of Linux hugepages
* Copyright (C) 2008 Adam Litke, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE /* For strchrnul */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/utsname.h>
#include "kernel-features.h"
#include "hugetlbfs.h"
#include "libhugetlbfs_privutils.h"
#include "libhugetlbfs_internal.h"
#include "libhugetlbfs_debug.h"
static struct kernel_version running_kernel_version;
/* This mask should always be 32 bits, regardless of the platform word size */
static unsigned int feature_mask;
static struct feature kernel_features[] = {
[HUGETLB_FEATURE_PRIVATE_RESV] = {
.name = "private_reservations",
.required_version = "2.6.27-rc1",
},
[HUGETLB_FEATURE_SAFE_NORESERVE] = {
.name = "noreserve_safe",
.required_version = "2.6.34",
},
[HUGETLB_FEATURE_MAP_HUGETLB] = {
.name = "map_hugetlb",
.required_version = "2.6.32",
}
};
static void debug_kernel_version(void)
{
struct kernel_version *ver = &running_kernel_version;
INFO("Parsed kernel version: [%u] . [%u] . [%u] ",
ver->major, ver->minor, ver->release);
if (ver->post)
INFO_CONT(" [post-release: %u]\n", ver->post);
else if (ver->pre)
INFO_CONT(" [pre-release: %u]\n", ver->pre);
else
INFO_CONT("\n");
}
static int str_to_ver(const char *str, struct kernel_version *ver)
{
char *start;
char *end;
/* Clear out version struct */
ver->major = ver->minor = ver->release = ver->post = ver->pre = 0;
/*
* The kernel always starts x.y.z
*
* Note: strtol is used in place of sscanf because when heap override is
* used this step happens before the _morecore replacement and sscanf
* does an internal heap allocation. This mean that the next allocation
* from the heap would be on small pages until the first block allocated
* by _morecore is exhausted
*/
errno = 0;
ver->major = strtol(str, &end, 10);
if (!ver->major && errno == EINVAL) {
ERROR("Unable to determine base kernel version: %s\n",
strerror(errno));
return -1;
}
start = end + 1;
errno = 0;
ver->minor = strtol(start, &end, 10);
if (!ver->minor && errno == EINVAL) {
ERROR("Unable to determine base kernel version: %s\n",
strerror(errno));
return -1;
}
start = end + 1;
errno = 0;
ver->release = strtol(start, &end, 10);
if (!ver->release && errno == EINVAL) {
ERROR("Unable to determine base kernel version: %s\n",
strerror(errno));
return -1;
}
/* Try to match a post/stable version */
start = end + 1;
if (*end == '.') {
ver->post = strtol(start, &end, 10);
if (!ver->post && errno == EINVAL)
return 0;
}
/* Try to match a preN/rcN version */
start = end + 1;
if (*end == '-') {
if (*start == 'r' && *(start + 1) == 'c')
start += 2;
else if (*start == 'p' &&
*(start + 1) == 'r' &&
*(start + 2) == 'e')
start += 3;
else {
/*
* For now we ignore any extraversions besides
* pre and rc versions and treat them as equal
* to the base version.
*/
return 0;
}
ver->pre = strtol(start, &end, 10);
}
return 0;
}
static int int_cmp(int a, int b)
{
if (a < b)
return -1;
if (a > b)
return 1;
else
return 0;
}
/*
* Pre-release kernels have the following compare rules:
* X.Y.(Z - 1) < X.Y.Z-rcN < X.Y.X
* This order can be enforced by simply decrementing the release (for
* comparison purposes) when there is a pre/rc modifier in effect.
*/
static int ver_cmp_release(struct kernel_version *ver)
{
if (ver->pre)
return ver->release - 1;
else
return ver->release;
}
static int ver_cmp(struct kernel_version *a, struct kernel_version *b)
{
int ret, a_release, b_release;
if ((ret = int_cmp(a->major, b->major)) != 0)
return ret;
if ((ret = int_cmp(a->minor, b->minor)) != 0)
return ret;
a_release = ver_cmp_release(a);
b_release = ver_cmp_release(b);
if ((ret = int_cmp(a_release, b_release)) != 0)
return ret;
if ((ret = int_cmp(a->post, b->post)) != 0)
return ret;
if ((ret = int_cmp(a->pre, b->pre)) != 0)
return ret;
/* We ignore forks (such as -mm and -mjb) */
return 0;
}
int test_compare_kver(const char *a, const char *b)
{
struct kernel_version ka, kb;
if (str_to_ver(a, &ka) < 0)
return -EINVAL;
if (str_to_ver(b, &kb) < 0)
return -EINVAL;
return ver_cmp(&ka, &kb);
}
int hugetlbfs_test_feature(int feature_code)
{
if (feature_code >= HUGETLB_FEATURE_NR) {
ERROR("hugetlbfs_test_feature: invalid feature code\n");
return -EINVAL;
}
return feature_mask & (1 << feature_code);
}
static void print_valid_features(void)
{
int i;
ERROR("HUGETLB_FEATURES=\"<feature>[,<feature>] ...\"\n");
ERROR_CONT("Valid features:\n");
for (i = 0; i < HUGETLB_FEATURE_NR; i++)
ERROR_CONT("\t%s, no_%s\n", kernel_features[i].name,
kernel_features[i].name);
}
static int check_features_env_valid(const char *env)
{
const char *pos = env;
int i;
while (pos && *pos != '\0') {
int match = 0;
char *next;
if (*pos == ',')
pos++;
next = strchrnul(pos, ',');
if (strncmp(pos, "no_", 3) == 0)
pos += 3;
for (i = 0; i < HUGETLB_FEATURE_NR; i++) {
char *name = kernel_features[i].name;
if (strncmp(pos, name, next - pos) == 0) {
match = 1;
break;
}
}
if (!match) {
print_valid_features();
return -1;
}
pos = next;
}
return 0;
}
void setup_features()
{
struct utsname u;
int i;
if (uname(&u)) {
ERROR("Getting kernel version failed: %s\n", strerror(errno));
return;
}
str_to_ver(u.release, &running_kernel_version);
debug_kernel_version();
/* Check if the user has overrided any features */
if (__hugetlb_opts.features &&
check_features_env_valid(__hugetlb_opts.features) == -1) {
ERROR("HUGETLB_FEATURES was invalid -- ignoring.\n");
__hugetlb_opts.features = NULL;
}
for (i = 0; i < HUGETLB_FEATURE_NR; i++) {
struct kernel_version ver;
char *name = kernel_features[i].name;
char *pos;
str_to_ver(kernel_features[i].required_version, &ver);
/* Has the user overridden feature detection? */
if (__hugetlb_opts.features &&
(pos = strstr(__hugetlb_opts.features, name))) {
INFO("Overriding feature %s: ", name);
/* If feature is preceeded by 'no_' then turn it off */
if (((pos - 3) >= __hugetlb_opts.features) &&
!strncmp(pos - 3, "no_", 3))
INFO_CONT("no\n");
else {
INFO_CONT("yes\n");
feature_mask |= (1UL << i);
}
continue;
}
/* Is the running kernel version newer? */
if (ver_cmp(&running_kernel_version, &ver) >= 0) {
INFO("Feature %s is present in this kernel\n",
kernel_features[i].name);
feature_mask |= (1UL << i);
}
}
}