Debian Codegraph @

kernel-features.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2008 Adam Litke, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #define _GNU_SOURCE /* For strchrnul */
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 /* This mask should always be 32 bits, regardless of the platform word size */
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 /* Clear out version struct */
74 ver->major = ver->minor = ver->release = ver->post = ver->pre = 0;
75
76 /*
77 * The kernel always starts x.y.z
78 *
79 * Note: strtol is used in place of sscanf because when heap override is
80 * used this step happens before the _morecore replacement and sscanf
81 * does an internal heap allocation. This mean that the next allocation
82 * from the heap would be on small pages until the first block allocated
83 * by _morecore is exhausted
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 /* Try to match a post/stable version */
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 /* Try to match a preN/rcN version */
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 * For now we ignore any extraversions besides
131 * pre and rc versions and treat them as equal
132 * to the base version.
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 * Pre-release kernels have the following compare rules:
155 * X.Y.(Z - 1) < X.Y.Z-rcN < X.Y.X
156 * This order can be enforced by simply decrementing the release (for
157 * comparison purposes) when there is a pre/rc modifier in effect.
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 /* We ignore forks (such as -mm and -mjb) */
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 /* Check if the user has overrided any features */
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 /* Has the user overridden feature detection? */
282 if (__hugetlb_opts.features &&
283 (pos = strstr(__hugetlb_opts.features, name))) {
284 INFO("Overriding feature %s: ", name);
285 /* If feature is preceeded by 'no_' then turn it off */
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 /* Is the running kernel version newer? */
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 }