Debian Codegraph @

hugeadm.c

raw symbols SCIP
1 /***************************************************************************
2 * User front end for using huge pages Copyright (C) 2008, IBM *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the Lesser GNU General Public License as *
6 * published by the Free Software Foundation; either version 2.1 of the *
7 * License, or at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the Lesser GNU General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20 /*
21 * hugeadm is designed to make an administrators life simpler, to automate
22 * and simplify basic system configuration as it relates to hugepages. It
23 * is designed to help with pool and mount configuration.
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <mntent.h>
32 #include <unistd.h>
33 #include <grp.h>
34 #include <pwd.h>
35 #include <fcntl.h>
36
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <sys/mount.h>
40 #include <sys/swap.h>
41 #include <sys/wait.h>
42
43 #define _GNU_SOURCE /* for getopt_long */
44 #include <unistd.h>
45 #include <getopt.h>
46
47 #define KB (1024)
48 #define MB (1024*KB)
49 #define GB (1024*MB)
50
51 #define REPORT_UTIL "hugeadm"
52 #define REPORT(level, prefix, format, ...) \
53 do { \
54 if (verbose_level >= level) \
55 fprintf(stderr, "hugeadm:" prefix ": " format, \
56 ##__VA_ARGS__); \
57 } while (0);
58
59 #include "libhugetlbfs_internal.h"
60 #include "hugetlbfs.h"
61
62 extern int optind;
63 extern char *optarg;
64
65 #define OPTION(opts, text) fprintf(stderr, " %-25s %s\n", opts, text)
66 #define CONT(text) fprintf(stderr, " %-25s %s\n", "", text)
67
68 #define MOUNT_DIR "/var/lib/hugetlbfs"
69 #define OPT_MAX 4096
70
71 #define PROCMOUNTS "/proc/mounts"
72 #define PROCHUGEPAGES_MOVABLE "/proc/sys/vm/hugepages_treat_as_movable"
73 #define PROCMINFREEKBYTES "/proc/sys/vm/min_free_kbytes"
74 #define PROCSHMMAX "/proc/sys/kernel/shmmax"
75 #define PROCHUGETLBGROUP "/proc/sys/vm/hugetlb_shm_group"
76 #define PROCZONEINFO "/proc/zoneinfo"
77 #define FS_NAME "hugetlbfs"
78 #define MIN_COL 20
79 #define MAX_SIZE_MNTENT (64 + PATH_MAX + 32 + 128 + 2 * sizeof(int))
80 #define FORMAT_LEN 20
81
82 #define MEM_TOTAL "MemTotal:"
83 #define SWAP_FREE "SwapFree:"
84 #define SWAP_TOTAL "SwapTotal:"
85
86 #define ALWAYS "always"
87 #define MADVISE "madvise"
88 #define NEVER "never"
89 #define TRANS_ENABLE "/sys/kernel/mm/transparent_hugepage/enabled"
90 #define KHUGE_SCAN_PAGES "/sys/kernel/mm/transparent_hugepage/khugepaged/pages_to_scan"
91 #define KHUGE_SCAN_SLEEP "/sys/kernel/mm/transparent_hugepage/khugepaged/scan_sleep_millisecs"
92 #define KHUGE_ALLOC_SLEEP "/sys/kernel/mm/transparent_hugepage/khugepaged/alloc_sleep_millisecs"
93
94 void print_usage()
95 {
96 fprintf(stderr, "hugeadm [options]\n");
97 fprintf(stderr, "options:\n");
98
99 OPTION("--list-all-mounts", "List all current hugetlbfs mount points");
100 OPTION("--pool-list", "List all pools");
101 OPTION("--hard", "specified with --pool-pages-min to make");
102 CONT("multiple attempts at adjusting the pool size to the");
103 CONT("specified count on failure");
104 OPTION("--pool-pages-min <size|DEFAULT>:[+|-]<pagecount|memsize<G|M|K>>", "");
105 CONT("Adjust pool 'size' lower bound");
106 OPTION("--obey-mempolicy", "Obey the NUMA memory policy when");
107 CONT("adjusting the pool 'size' lower bound");
108 OPTION("--thp-always", "Enable transparent huge pages always");
109 OPTION("--thp-madvise", "Enable transparent huge pages with madvise");
110 OPTION("--thp-never", "Disable transparent huge pages");
111 OPTION("--thp-khugepaged-pages <pages to scan>", "Number of pages that khugepaged");
112 CONT("should scan on each pass");
113 OPTION("--thp-khugepaged-scan-sleep <milliseconds>", "Time in ms to sleep between");
114 CONT("khugepaged passes");
115 OPTION("--thp-khugepaged-alloc-sleep <milliseconds>", "Time in ms for khugepaged");
116 CONT("to wait if there was a huge page allocation failure");
117 OPTION("--pool-pages-max <size|DEFAULT>:[+|-]<pagecount|memsize<G|M|K>>", "");
118 CONT("Adjust pool 'size' upper bound");
119 OPTION("--set-recommended-min_free_kbytes", "");
120 CONT("Sets min_free_kbytes to a recommended value to improve availability of");
121 CONT("huge pages at runtime");
122 OPTION("--set-recommended-shmmax", "Sets shmmax to a recommended value to");
123 CONT("maximise the size possible for shared memory pools");
124 OPTION("--set-shm-group <gid|groupname>", "Sets hugetlb_shm_group to the");
125 CONT("specified group, which has permission to use hugetlb shared memory pools");
126 OPTION("--add-temp-swap[=count]", "Specified with --pool-pages-min to create");
127 CONT("temporary swap space for the duration of the pool resize. Default swap");
128 CONT("size is 5 huge pages. Optional arg sets size to 'count' huge pages");
129 OPTION("--add-ramdisk-swap", "Specified with --pool-pages-min to create");
130 CONT("swap space on ramdisks. By default, swap is removed after the resize.");
131 OPTION("--persist", "Specified with --add-temp-swap or --add-ramdisk-swap");
132 CONT("options to make swap space persist after the resize.");
133 OPTION("--enable-zone-movable", "Use ZONE_MOVABLE for huge pages");
134 OPTION("--disable-zone-movable", "Do not use ZONE_MOVABLE for huge pages");
135 OPTION("--create-mounts", "Creates a mount point for each available");
136 CONT("huge page size on this system under /var/lib/hugetlbfs");
137 OPTION("--create-user-mounts <user>", "");
138 CONT("Creates a mount point for each available huge");
139 CONT("page size under /var/lib/hugetlbfs/<user>");
140 CONT("usable by user <user>");
141 OPTION("--create-group-mounts <group>", "");
142 CONT("Creates a mount point for each available huge");
143 CONT("page size under /var/lib/hugetlbfs/<group>");
144 CONT("usable by group <group>");
145 OPTION("--create-global-mounts", "");
146 CONT("Creates a mount point for each available huge");
147 CONT("page size under /var/lib/hugetlbfs/global");
148 CONT("usable by anyone");
149
150 OPTION("--max-size <size<G|M|K>>", "Limit the filesystem size of a new mount point");
151 OPTION("--max-inodes <number>", "Limit the number of inodes on a new mount point");
152
153 OPTION("--page-sizes", "Display page sizes that a configured pool");
154 OPTION("--page-sizes-all",
155 "Display page sizes support by the hardware");
156 OPTION("--dry-run", "Print the equivalent shell commands for what");
157 CONT("the specified options would have done without");
158 CONT("taking any action");
159
160 OPTION("--explain", "Gives a overview of the status of the system");
161 CONT("with respect to huge page availability");
162
163 OPTION("--verbose <level>, -v", "Increases/sets tracing levels");
164 OPTION("--help, -h", "Prints this message");
165 }
166
167 int opt_dry_run = 0;
168 int opt_hard = 0;
169 int opt_movable = -1;
170 int opt_set_recommended_minfreekbytes = 0;
171 int opt_set_recommended_shmmax = 0;
172 int opt_set_hugetlb_shm_group = 0;
173 int opt_temp_swap = 0;
174 int opt_ramdisk_swap = 0;
175 int opt_swap_persist = 0;
176 int opt_obey_mempolicy = 0;
177 unsigned long opt_limit_mount_size = 0;
178 int opt_limit_mount_inodes = 0;
179 int verbose_level = VERBOSITY_DEFAULT;
180 char ramdisk_list[PATH_MAX] = "";
181
182 void setup_environment(char *var, char *val)
183 {
184 if (opt_dry_run) {
185 printf("%s='%s'\n", var, val);
186 return;
187 }
188
189 setenv(var, val, 1);
190 DEBUG("%s='%s'\n", var, val);
191 }
192
193 /* Enable/disable allocation of hugepages from ZONE_MOVABLE */
194 void setup_zone_movable(int able)
195 {
196 if (opt_dry_run) {
197 printf("echo %d > %s\n", able, PROCHUGEPAGES_MOVABLE);
198 return;
199 }
200
201 DEBUG("Setting %s to %d\n", PROCHUGEPAGES_MOVABLE, able);
202
203 /* libhugetlbfs reports any error that occurs */
204 file_write_ulong(PROCHUGEPAGES_MOVABLE, (unsigned long)able);
205 }
206
207 void verbose_init(void)
208 {
209 char *env;
210
211 env = getenv("HUGETLB_VERBOSE");
212 if (env)
213 verbose_level = atoi(env);
214 env = getenv("HUGETLB_DEBUG");
215 if (env)
216 verbose_level = VERBOSITY_MAX;
217 }
218
219 void verbose(char *which)
220 {
221 int new_level;
222
223 if (which) {
224 new_level = atoi(which);
225 if (new_level < 0 || new_level > 99) {
226 ERROR("%d: verbosity out of range 0-99\n",
227 new_level);
228 exit(EXIT_FAILURE);
229 }
230 } else {
231 new_level = verbose_level + 1;
232 if (new_level == 100) {
233 WARNING("verbosity limited to 99\n");
234 new_level--;
235 }
236 }
237 verbose_level = new_level;
238 }
239
240 void verbose_expose(void)
241 {
242 char level[3];
243
244 if (verbose_level == 99) {
245 setup_environment("HUGETLB_DEBUG", "yes");
246 }
247 snprintf(level, sizeof(level), "%d", verbose_level);
248 setup_environment("HUGETLB_VERBOSE", level);
249 }
250
251 /*
252 * getopts return values for options which are long only.
253 */
254 #define LONG_POOL ('p' << 8)
255 #define LONG_POOL_LIST (LONG_POOL|'l')
256 #define LONG_POOL_MIN_ADJ (LONG_POOL|'m')
257 #define LONG_POOL_MAX_ADJ (LONG_POOL|'M')
258 #define LONG_POOL_MEMPOL (LONG_POOL|'p')
259
260 #define LONG_SET_RECOMMENDED_MINFREEKBYTES ('k' << 8)
261 #define LONG_SET_RECOMMENDED_SHMMAX ('x' << 8)
262 #define LONG_SET_HUGETLB_SHM_GROUP ('R' << 8)
263
264 #define LONG_MOVABLE ('z' << 8)
265 #define LONG_MOVABLE_ENABLE (LONG_MOVABLE|'e')
266 #define LONG_MOVABLE_DISABLE (LONG_MOVABLE|'d')
267
268 #define LONG_HARD ('h' << 8)
269 #define LONG_SWAP ('s' << 8)
270 #define LONG_SWAP_DISK (LONG_SWAP|'d')
271 #define LONG_SWAP_RAMDISK (LONG_SWAP|'r')
272 #define LONG_SWAP_PERSIST (LONG_SWAP|'p')
273
274 #define LONG_PAGE ('P' << 8)
275 #define LONG_PAGE_SIZES (LONG_PAGE|'s')
276 #define LONG_PAGE_AVAIL (LONG_PAGE|'a')
277
278 #define LONG_MOUNTS ('m' << 8)
279 #define LONG_CREATE_MOUNTS (LONG_MOUNTS|'C')
280 #define LONG_CREATE_USER_MOUNTS (LONG_MOUNTS|'U')
281 #define LONG_CREATE_GROUP_MOUNTS (LONG_MOUNTS|'g')
282 #define LONG_CREATE_GLOBAL_MOUNTS (LONG_MOUNTS|'G')
283 #define LONG_LIST_ALL_MOUNTS (LONG_MOUNTS|'A')
284
285 #define LONG_LIMITS ('l' << 8)
286 #define LONG_LIMIT_SIZE (LONG_LIMITS|'S')
287 #define LONG_LIMIT_INODES (LONG_LIMITS|'I')
288
289 #define LONG_EXPLAIN ('e' << 8)
290
291 #define LONG_TRANS ('t' << 8)
292 #define LONG_TRANS_ALWAYS (LONG_TRANS|'a')
293 #define LONG_TRANS_MADVISE (LONG_TRANS|'m')
294 #define LONG_TRANS_NEVER (LONG_TRANS|'n')
295
296 #define LONG_KHUGE ('K' << 8)
297 #define LONG_KHUGE_PAGES (LONG_KHUGE|'p')
298 #define LONG_KHUGE_SCAN (LONG_KHUGE|'s')
299 #define LONG_KHUGE_ALLOC (LONG_KHUGE|'a')
300
301 #define MAX_POOLS 32
302
303 static int cmpsizes(const void *p1, const void *p2)
304 {
305 return ((struct hpage_pool *)p1)->pagesize >
306 ((struct hpage_pool *)p2)->pagesize;
307 }
308
309 void pool_list(void)
310 {
311 struct hpage_pool pools[MAX_POOLS];
312 int pos;
313 int cnt;
314
315 cnt = hpool_sizes(pools, MAX_POOLS);
316 if (cnt < 0) {
317 ERROR("unable to obtain pools list");
318 exit(EXIT_FAILURE);
319 }
320 qsort(pools, cnt, sizeof(pools[0]), cmpsizes);
321
322 printf("%10s %8s %8s %8s %8s\n",
323 "Size", "Minimum", "Current", "Maximum", "Default");
324 for (pos = 0; cnt--; pos++) {
325 printf("%10ld %8ld %8ld %8ld %8s\n", pools[pos].pagesize,
326 pools[pos].minimum, pools[pos].size,
327 pools[pos].maximum, (pools[pos].is_default) ? "*" : "");
328 }
329 }
330
331 struct mount_list
332 {
333 struct mntent entry;
334 char data[MAX_SIZE_MNTENT];
335 struct mount_list *next;
336 };
337
338 void print_mounts(struct mount_list *current, int longest)
339 {
340 char format_str[FORMAT_LEN];
341
342 snprintf(format_str, FORMAT_LEN, "%%-%ds %%s\n", longest);
343 printf(format_str, "Mount Point", "Options");
344 while (current) {
345 printf(format_str, current->entry.mnt_dir,
346 current->entry.mnt_opts);
347 current = current->next;
348 }
349 }
350
351 /*
352 * collect_active_mounts returns a list of active hugetlbfs
353 * mount points, and, if longest is not NULL, the number of
354 * characters in the longest mount point to ease output
355 * formatting. Caller is expected to free the list of mounts.
356 */
357 struct mount_list *collect_active_mounts(int *longest)
358 {
359 FILE *mounts;
360 struct mount_list *list, *current, *previous = NULL;
361 int length;
362
363 /* First try /proc/mounts, then /etc/mtab */
364 mounts = setmntent(PROCMOUNTS, "r");
365 if (!mounts) {
366 mounts = setmntent(MOUNTED, "r");
367 if (!mounts) {
368 ERROR("unable to open %s or %s for reading",
369 PROCMOUNTS, MOUNTED);
370 exit(EXIT_FAILURE);
371 }
372 }
373
374 list = malloc(sizeof(struct mount_list));
375 if (!list) {
376 ERROR("out of memory");
377 exit(EXIT_FAILURE);
378 }
379
380 list->next = NULL;
381 current = list;
382 while (getmntent_r(mounts, &(current->entry), current->data, MAX_SIZE_MNTENT)) {
383 if (strcasecmp(current->entry.mnt_type, FS_NAME) == 0) {
384 length = strlen(current->entry.mnt_dir);
385 if (longest && length > *longest)
386 *longest = length;
387
388 current->next = malloc(sizeof(struct mount_list));
389 if (!current->next) {
390 ERROR("out of memory");
391 exit(EXIT_FAILURE);
392 }
393 previous = current;
394 current = current->next;
395 current->next = NULL;
396 }
397 }
398
399 endmntent(mounts);
400
401 if (previous) {
402 free(previous->next);
403 previous->next = NULL;
404 return list;
405 }
406 return NULL;
407 }
408
409 void mounts_list_all(void)
410 {
411 struct mount_list *list, *previous;
412 int longest = MIN_COL;
413
414 list = collect_active_mounts(&longest);
415
416 if (!list) {
417 ERROR("No hugetlbfs mount points found\n");
418 return;
419 }
420
421 print_mounts(list, longest);
422
423 while (list) {
424 previous = list;
425 list = list->next;
426 free(previous);
427 }
428 }
429
430 int make_dir(char *path, mode_t mode, uid_t uid, gid_t gid)
431 {
432 struct passwd *pwd;
433 struct group *grp;
434
435 if (opt_dry_run) {
436 pwd = getpwuid(uid);
437 grp = getgrgid(gid);
438 printf("if [ ! -e %s ]\n", path);
439 printf("then\n");
440 printf(" mkdir %s\n", path);
441 printf(" chown %s:%s %s\n", pwd->pw_name, grp->gr_name, path);
442 printf(" chmod %o %s\n", mode, path);
443 printf("fi\n");
444 return 0;
445 }
446
447 if (mkdir(path, mode)) {
448 if (errno != EEXIST) {
449 ERROR("Unable to create dir %s, error: %s\n",
450 path, strerror(errno));
451 return 1;
452 }
453 } else {
454 if (chown(path, uid, gid)) {
455 ERROR("Unable to change ownership of %s, error: %s\n",
456 path, strerror(errno));
457 return 1;
458 }
459
460 if (chmod(path, mode)) {
461 ERROR("Unable to change permission on %s, error: %s\n",
462 path, strerror(errno));
463 return 1;
464 }
465 }
466
467 return 0;
468 }
469
470 /**
471 * ensure_dir will build the entire directory structure up to and
472 * including path, all directories built will be owned by
473 * user:group and permissions will be set to mode.
474 */
475 int ensure_dir(char *path, mode_t mode, uid_t uid, gid_t gid)
476 {
477 char *idx;
478
479 if (!path || strlen(path) == 0)
480 return 0;
481
482 idx = strchr(path + 1, '/');
483
484 do {
485 if (idx)
486 *idx = '\0';
487
488 if (make_dir(path, mode, uid, gid))
489 return 1;
490
491 if (idx) {
492 *idx = '/';
493 idx++;
494 }
495 } while ((idx = strchr(idx, '/')) != NULL);
496
497 if (make_dir(path, mode, uid, gid))
498 return 1;
499
500 return 0;
501 }
502
503 int check_if_already_mounted(struct mount_list *list, char *path)
504 {
505 while (list) {
506 if (!strcmp(list->entry.mnt_dir, path))
507 return 1;
508 list = list->next;
509 }
510 return 0;
511 }
512
513 int mount_dir(char *path, char *options, mode_t mode, uid_t uid, gid_t gid)
514 {
515 struct passwd *pwd;
516 struct group *grp;
517 struct mntent entry;
518 FILE *mounts;
519 char dummy;
520 int useMtab;
521
522 struct mount_list *list, *previous;
523
524 list = collect_active_mounts(NULL);
525
526 if (list && check_if_already_mounted(list, path)) {
527 WARNING("Directory %s is already mounted.\n", path);
528
529 while (list) {
530 previous = list;
531 list = list->next;
532 free(previous);
533 }
534 return 0;
535 }
536
537 while (list) {
538 previous = list;
539 list = list->next;
540 free(previous);
541 }
542
543 if (opt_dry_run) {
544 pwd = getpwuid(uid);
545 grp = getgrgid(gid);
546 printf("mount -t %s none %s -o %s\n", FS_NAME,
547 path, options);
548 printf("chown %s:%s %s\n", pwd->pw_name, grp->gr_name,
549 path);
550 printf("chmod %o %s\n", mode, path);
551 } else {
552 if (mount("none", path, FS_NAME, 0, options)) {
553 ERROR("Unable to mount %s, error: %s\n",
554 path, strerror(errno));
555 return 1;
556 }
557
558 /* Check if mtab is a symlink */
559 useMtab = (readlink(MOUNTED, &dummy, 1) < 0);
560 if (useMtab) {
561 mounts = setmntent(MOUNTED, "a+");
562 if (mounts) {
563 entry.mnt_fsname = FS_NAME;
564 entry.mnt_dir = path;
565 entry.mnt_type = FS_NAME;
566 entry.mnt_opts = options;
567 entry.mnt_freq = 0;
568 entry.mnt_passno = 0;
569 if (addmntent(mounts, &entry))
570 WARNING("Unable to add entry %s to %s, error: %s\n",
571 path, MOUNTED, strerror(errno));
572 endmntent(mounts);
573 } else {
574 WARNING("Unable to open %s, error: %s\n",
575 MOUNTED, strerror(errno));
576 }
577 }
578
579 if (chown(path, uid, gid)) {
580 ERROR("Unable to change ownership of %s, error: %s\n",
581 path, strerror(errno));
582 return 1;
583 }
584
585 if (chmod(path, mode)) {
586 ERROR("Unable to set permissions on %s, error: %s\n",
587 path, strerror(errno));
588 return 1;
589 }
590 }
591 return 0;
592 }
593
594 void scale_size(char *buf, unsigned long pagesize)
595 {
596 if(pagesize >= GB)
597 snprintf(buf, OPT_MAX, "%luGB", pagesize / GB);
598 else if(pagesize >= MB)
599 snprintf(buf, OPT_MAX, "%luMB", pagesize / MB);
600 else
601 snprintf(buf, OPT_MAX, "%luKB", pagesize / KB);
602 }
603
604 void create_mounts(char *user, char *group, char *base, mode_t mode)
605 {
606 struct hpage_pool pools[MAX_POOLS];
607 char path[PATH_MAX];
608 char options[OPT_MAX];
609 char limits[OPT_MAX];
610 char scaled[OPT_MAX];
611 int cnt, pos;
612 struct passwd *pwd;
613 struct group *grp;
614 uid_t uid = 0;
615 gid_t gid = 0;
616
617 if (geteuid() != 0) {
618 ERROR("Mounts can only be created by root\n");
619 exit(EXIT_FAILURE);
620 }
621
622 if (user) {
623 pwd = getpwnam(user);
624 if (!pwd) {
625 ERROR("Could not find specified user %s\n", user);
626 exit(EXIT_FAILURE);
627 }
628 uid = pwd->pw_uid;
629 } else if (group) {
630 grp = getgrnam(group);
631 if (!grp) {
632 ERROR("Could not find specified group %s\n", group);
633 exit(EXIT_FAILURE);
634 }
635 gid = grp->gr_gid;
636 }
637
638 if (ensure_dir(base,
639 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH, 0, 0)) {
640 ERROR("Unable to ensure path %s\n", base);
641 exit(EXIT_FAILURE);
642 }
643
644 cnt = hpool_sizes(pools, MAX_POOLS);
645 if (cnt < 0) {
646 ERROR("Unable to obtain pools list\n");
647 exit(EXIT_FAILURE);
648 }
649
650 for (pos=0; cnt--; pos++) {
651 scaled[0] = 0;
652 scale_size(scaled, pools[pos].pagesize);
653 if (user) {
654 if (snprintf(path, PATH_MAX, "%s/%s/pagesize-%s",
655 base, user, scaled) >= PATH_MAX) {
656 ERROR("Truncated mount creation with user\n");
657 exit(EXIT_FAILURE);
658 }
659 } else if (group) {
660 if (snprintf(path, PATH_MAX, "%s/%s/pagesize-%s",
661 base, group, scaled) >= PATH_MAX) {
662 ERROR("Truncated mount creation with group\n");
663 exit(EXIT_FAILURE);
664 }
665 } else {
666 if (snprintf(path, PATH_MAX, "%s/pagesize-%s",
667 base, scaled) >= PATH_MAX) {
668 ERROR("Truncated base mount creation\n");
669 exit(EXIT_FAILURE);
670 }
671 }
672
673 if (snprintf(options, OPT_MAX, "pagesize=%ld",
674 pools[pos].pagesize) >= OPT_MAX) {
675 ERROR("Truncated mount options creation\n");
676 exit(EXIT_FAILURE);
677 }
678
679 /* Yes, this could be cleverer */
680 if (opt_limit_mount_size && opt_limit_mount_inodes) {
681 if (snprintf(limits, OPT_MAX, ",size=%lu,nr_inodes=%d",
682 opt_limit_mount_size, opt_limit_mount_inodes) >=
683 OPT_MAX) {
684 ERROR("Truncated limit option creation\n");
685 exit(EXIT_FAILURE);
686 }
687 } else {
688 if (opt_limit_mount_size) {
689 if (snprintf(limits, OPT_MAX, ",size=%lu",
690 opt_limit_mount_size) >= OPT_MAX) {
691 ERROR("Truncated mount size limit\n");
692 exit(EXIT_FAILURE);
693 }
694 }
695 if (opt_limit_mount_inodes) {
696 if (snprintf(limits, OPT_MAX, ",nr_inodes=%d",
697 opt_limit_mount_inodes) >= OPT_MAX) {
698 ERROR("Truncated mount inode limit\n");
699 exit(EXIT_FAILURE);
700 }
701 }
702 }
703
704 /* Append limits if specified */
705 if (limits[0] != 0) {
706 size_t maxlen = OPT_MAX - strlen(options);
707 if (maxlen > strlen(limits))
708 strcat(options, limits);
709 else
710 WARNING("String limitations met, cannot append limitations onto mount options string. Increase OPT_MAX");
711 }
712
713 if (ensure_dir(path, mode, uid, gid)) {
714 ERROR("Failed to create path %s\n", path);
715 exit(EXIT_FAILURE);
716 }
717
718 if (mount_dir(path, options, mode, uid, gid)) {
719 ERROR("Failued to mount path %s\n", path);
720 exit(EXIT_FAILURE);
721 }
722 }
723 }
724
725 /**
726 * show_mem shouldn't change the behavior of any of its
727 * callers, it only prints a message to the user showing the
728 * total amount of memory in the system (in megabytes).
729 */
730 void show_mem()
731 {
732 long mem_total;
733
734 mem_total = read_meminfo(MEM_TOTAL);
735 printf("Total System Memory: %ld MB\n\n", mem_total / 1024);
736 }
737
738 /**
739 * check_swap shouldn't change the behavior of any of its
740 * callers, it only prints a message to the user if something
741 * is being done that might fail without swap available. i.e.
742 * resizing a huge page pool
743 */
744 void check_swap()
745 {
746 long swap_sz;
747 long swap_total;
748
749 swap_total = read_meminfo(SWAP_TOTAL);
750 if (swap_total <= 0) {
751 WARNING("There is no swap space configured, resizing hugepage pool may fail\n");
752 WARNING("Use --add-temp-swap option to temporarily add swap during the resize\n");
753 return;
754 }
755
756 swap_sz = read_meminfo(SWAP_FREE);
757 /* meminfo keeps values in kb, but we use bytes for hpage sizes */
758 swap_sz *= 1024;
759 if (swap_sz <= gethugepagesize()) {
760 WARNING("There is very little swap space free, resizing hugepage pool may fail\n");
761 WARNING("Use --add-temp-swap option to temporarily add swap during the resize\n");
762 }
763 }
764
765 #define ZONEINFO_LINEBUF 1024
766 long recommended_minfreekbytes(void)
767 {
768 FILE *f;
769 char buf[ZONEINFO_LINEBUF];
770 int nr_zones = 0;
771 long recommended_min;
772 long pageblock_kbytes = kernel_default_hugepage_size() / 1024;
773
774 /* Detect the number of zones in the system */
775 f = fopen(PROCZONEINFO, "r");
776 if (f == NULL) {
777 WARNING("Unable to open " PROCZONEINFO);
778 return 0;
779 }
780 while (fgets(buf, ZONEINFO_LINEBUF, f) != NULL) {
781 if (strncmp(buf, "Node ", 5) == 0)
782 nr_zones++;
783 }
784 fclose(f);
785
786 /* Make sure at least 2 pageblocks are free for MIGRATE_RESERVE */
787 recommended_min = pageblock_kbytes * nr_zones * 2;
788
789 /*
790 * Make sure that on average at least two pageblocks are almost free
791 * of another type, one for a migratetype to fall back to and a
792 * second to avoid subsequent fallbacks of other types There are 3
793 * MIGRATE_TYPES we care about.
794 */
795 recommended_min += pageblock_kbytes * nr_zones * 3 * 3;
796 return recommended_min;
797 }
798
799 void set_recommended_minfreekbytes(void)
800 {
801 long recommended_min = recommended_minfreekbytes();
802
803 if (opt_dry_run) {
804 printf("echo \"%ld\" > %s\n", recommended_min,
805 PROCMINFREEKBYTES);
806 return;
807 }
808
809 DEBUG("Setting min_free_kbytes to %ld\n", recommended_min);
810 file_write_ulong(PROCMINFREEKBYTES, (unsigned long)recommended_min);
811 }
812
813 /*
814 * check_minfreekbytes does not alter the value of min_free_kbytes. It just
815 * reports what the current value is and what it should be
816 */
817 void check_minfreekbytes(void)
818 {
819 long min_free_kbytes = file_read_ulong(PROCMINFREEKBYTES, NULL);
820 long recommended_min = recommended_minfreekbytes();
821
822 /* There should be at least one pageblock free per zone in the system */
823 if (recommended_min > min_free_kbytes) {
824 printf("\n");
825 printf("The " PROCMINFREEKBYTES " of %ld is too small. To maximiuse efficiency\n", min_free_kbytes);
826 printf("of fragmentation avoidance, there should be at least one huge page free per zone\n");
827 printf("in the system which minimally requires a min_free_kbytes value of %ld\n", recommended_min);
828 }
829 }
830
831 unsigned long long recommended_shmmax(void)
832 {
833 struct hpage_pool pools[MAX_POOLS];
834 unsigned long long recommended_shmmax = 0;
835 int pos, cnt;
836
837 cnt = hpool_sizes(pools, MAX_POOLS);
838 if (cnt < 0) {
839 ERROR("unable to obtain pools list");
840 exit(EXIT_FAILURE);
841 }
842
843 for (pos = 0; cnt--; pos++)
844 recommended_shmmax += ((unsigned long long)pools[pos].maximum *
845 pools[pos].pagesize);
846
847 return recommended_shmmax;
848 }
849
850 void set_recommended_shmmax(void)
851 {
852 int ret;
853 unsigned long max_recommended = -1UL;
854 unsigned long long recommended = recommended_shmmax();
855
856 if (recommended == 0) {
857 printf("\n");
858 WARNING("We can only set a recommended shmmax when huge pages are configured!\n");
859 return;
860 }
861
862 if (recommended > max_recommended)
863 recommended = max_recommended;
864
865 DEBUG("Setting shmmax to %llu\n", recommended);
866 ret = file_write_ulong(PROCSHMMAX, (unsigned long)recommended);
867
868 if (!ret) {
869 INFO("To make shmmax settings persistent, add the following line to /etc/sysctl.conf:\n");
870 INFO(" kernel.shmmax = %llu\n", recommended);
871 }
872 }
873
874 void check_shmmax(void)
875 {
876 long current_shmmax = file_read_ulong(PROCSHMMAX, NULL);
877 long recommended = recommended_shmmax();
878
879 if (current_shmmax != recommended) {
880 printf("\n");
881 printf("A " PROCSHMMAX " value of %ld bytes may be sub-optimal. To maximise\n", current_shmmax);
882 printf("shared memory usage, this should be set to the size of the largest shared memory\n");
883 printf("segment size you want to be able to use. Alternatively, set it to a size matching\n");
884 printf("the maximum possible allocation size of all huge pages. This can be done\n");
885 printf("automatically, using the --set-recommended-shmmax option.\n");
886 }
887
888 if (recommended == 0) {
889 printf("\n");
890 WARNING("We can't make a shmmax recommendation until huge pages are configured!\n");
891 return;
892 }
893
894 printf("\n");
895 printf("The recommended shmmax for your currently allocated huge pages is %ld bytes.\n", recommended);
896 printf("To make shmmax settings persistent, add the following line to /etc/sysctl.conf:\n");
897 printf(" kernel.shmmax = %ld\n", recommended);
898 }
899
900 void set_hugetlb_shm_group(gid_t gid, char *group)
901 {
902 int ret;
903
904 DEBUG("Setting hugetlb_shm_group to %d (%s)\n", gid, group);
905 ret = file_write_ulong(PROCHUGETLBGROUP, (unsigned long)gid);
906
907 if (!ret) {
908 INFO("To make hugetlb_shm_group settings persistent, add the following line to /etc/sysctl.conf:\n");
909 INFO(" vm.hugetlb_shm_group = %d\n", gid);
910 }
911 }
912
913 /* heisted from shadow-utils/libmisc/list.c::is_on_list() */
914 static int user_in_group(char *const *list, const char *member)
915 {
916 while (*list != NULL) {
917 if (strcmp(*list, member) == 0) {
918 return 1;
919 }
920 list++;
921 }
922
923 return 0;
924 }
925
926 void check_user(void)
927 {
928 uid_t uid;
929 gid_t gid;
930 struct passwd *pwd;
931 struct group *grp;
932
933 gid = (gid_t)file_read_ulong(PROCHUGETLBGROUP, NULL);
934 grp = getgrgid(gid);
935 if (!grp) {
936 printf("\n");
937 WARNING("Group ID %d in hugetlb_shm_group doesn't appear to be a valid group!\n", gid);
938 return;
939 }
940
941 uid = getuid();
942 pwd = getpwuid(uid);
943
944 /* Don't segfault if user does not have a passwd entry. */
945 if (!pwd) {
946 printf("\n");
947 WARNING("User uid %d is not in the password file!\n", uid);
948 return;
949 }
950
951 if (gid != pwd->pw_gid && !user_in_group(grp->gr_mem, pwd->pw_name) && uid != 0) {
952 printf("\n");
953 WARNING("User %s (uid: %d) is not a member of the hugetlb_shm_group %s (gid: %d)!\n", pwd->pw_name, uid, grp->gr_name, gid);
954 } else {
955 printf("\n");
956 printf("To make your hugetlb_shm_group settings persistent, add the following line to /etc/sysctl.conf:\n");
957 printf(" vm.hugetlb_shm_group = %d\n", gid);
958 }
959 }
960
961 void add_temp_swap(long page_size)
962 {
963 char path[PATH_MAX];
964 char file[PATH_MAX];
965 char mkswap_cmd[PATH_MAX];
966 FILE *f;
967 char *buf;
968 long swap_size;
969 long pid;
970 int ret;
971 int num_pages;
972
973 if (geteuid() != 0) {
974 ERROR("Swap can only be manipulated by root\n");
975 exit(EXIT_FAILURE);
976 }
977
978 pid = getpid();
979 if (snprintf(path, PATH_MAX, "%s/swap/temp", MOUNT_DIR) >= PATH_MAX) {
980 ERROR("Truncated swap path creation\n");
981 exit(EXIT_FAILURE);
982 }
983
984 if (snprintf(file, PATH_MAX, "%s/swapfile-%ld", path, pid) >= PATH_MAX) {
985 ERROR("Truncated swap file path\n");
986 exit(EXIT_FAILURE);
987 }
988
989 /* swapsize is 5 hugepages */
990 if (opt_temp_swap == -1)
991 num_pages = 5;
992 else
993 num_pages = opt_temp_swap;
994 swap_size = num_pages * page_size;
995
996 if (ensure_dir(path, S_IRWXU | S_IRGRP | S_IXGRP, 0, 0)) {
997 ERROR("Failed to create path %s\n", path);
998 exit(EXIT_FAILURE);
999 }
1000
1001 if (opt_dry_run) {
1002 printf("dd bs=1024 count=%ld if=/dev/zero of=%s\n",
1003 swap_size / 1024, file);
1004 printf("mkswap %s\nswapon %s\n", file, file);
1005 return;
1006 }
1007
1008 f = fopen(file, "wx");
1009 if (!f) {
1010 WARNING("Couldn't open %s: %s\n", file, strerror(errno));
1011 opt_temp_swap = 0;
1012 return;
1013 }
1014
1015 buf = malloc(swap_size);
1016 memset(buf, 0, swap_size);
1017 fwrite(buf, sizeof(char), swap_size, f);
1018 free(buf);
1019 fclose(f);
1020
1021 if (snprintf(mkswap_cmd, PATH_MAX, "mkswap %s", file) >= PATH_MAX) {
1022 ERROR("Truncated mkswap command buffer\n");
1023 exit(EXIT_FAILURE);
1024 }
1025
1026 ret = system(mkswap_cmd);
1027 if (WIFSIGNALED(ret)) {
1028 WARNING("Call to mkswap failed\n");
1029 opt_temp_swap = 0;
1030 return;
1031 } else if (WIFEXITED(ret)) {
1032 ret = WEXITSTATUS(ret);
1033 if (ret) {
1034 WARNING("Call to mkswap failed\n");
1035 opt_temp_swap = 0;
1036 return;
1037 }
1038 }
1039
1040 DEBUG("swapon %s\n", file);
1041 if (swapon(file, 0)) {
1042 WARNING("swapon on %s failed: %s\n", file, strerror(errno));
1043 opt_temp_swap = 0;
1044 }
1045 }
1046
1047 void rem_temp_swap() {
1048 char file[PATH_MAX];
1049 long pid;
1050
1051 pid = getpid();
1052 if (snprintf(file, PATH_MAX, "%s/swap/temp/swapfile-%ld", MOUNT_DIR, pid) >=
1053 PATH_MAX) {
1054 ERROR("Truncated removal swap path creation\n");
1055 exit(EXIT_FAILURE);
1056 }
1057
1058 if (opt_dry_run) {
1059 printf("swapoff %s\nrm -f %s\n", file, file);
1060 return;
1061 }
1062
1063 if (swapoff(file))
1064 WARNING("swapoff on %s failed: %s\n", file, strerror(errno));
1065 remove(file);
1066 DEBUG("swapoff %s\n", file);
1067 }
1068
1069 void add_ramdisk_swap(long page_size) {
1070 char ramdisk[PATH_MAX];
1071 char mkswap_cmd[PATH_MAX];
1072 int disk_num=0;
1073 int count = 0;
1074 long ramdisk_size;
1075 int ret;
1076 int fd;
1077
1078 snprintf(ramdisk, PATH_MAX, "/dev/ram%i", disk_num);
1079 fd = open(ramdisk, O_RDONLY);
1080 ioctl(fd, BLKGETSIZE, &ramdisk_size);
1081 close(fd);
1082
1083 ramdisk_size = ramdisk_size * 512;
1084 count = (page_size/ramdisk_size) + 1;
1085
1086 if (count > 1) {
1087 INFO("Swap will be initialized on multiple ramdisks because\n\
1088 ramdisk size is less than huge page size. To avoid\n\
1089 this in the future, use kernel command line parameter\n\
1090 ramdisk_size=N, to set ramdisk size to N blocks.\n");
1091 }
1092
1093 while (count > 0) {
1094 if (snprintf(ramdisk, PATH_MAX, "/dev/ram%i", disk_num) >= PATH_MAX) {
1095 ERROR("Truncated ram disk path\n");
1096 exit(EXIT_FAILURE);
1097 }
1098
1099 if (access(ramdisk, F_OK) != 0){
1100 break;
1101 }
1102 disk_num++;
1103
1104 if (opt_dry_run) {
1105 printf("mkswap %s\nswapon %s\n", ramdisk, ramdisk);
1106 } else {
1107 if (snprintf(mkswap_cmd, PATH_MAX, "mkswap %s", ramdisk) >=
1108 PATH_MAX) {
1109 ERROR("Truncated ram disk mkswap command\n");
1110 exit(EXIT_FAILURE);
1111 }
1112 ret = system(mkswap_cmd);
1113 if (WIFSIGNALED(ret)) {
1114 WARNING("Call to mkswap failed\n");
1115 continue;
1116 } else if (WIFEXITED(ret)) {
1117 ret = WEXITSTATUS(ret);
1118 if (ret) {
1119 WARNING("Call to mkswap failed\n");
1120 continue;
1121 }
1122 }
1123 DEBUG("swapon %s\n", ramdisk);
1124 if (swapon(ramdisk, 0)) {
1125 WARNING("swapon on %s failed: %s\n", ramdisk, strerror(errno));
1126 opt_temp_swap = 0;
1127 continue;
1128 }
1129 }
1130 count--;
1131 strcat(ramdisk_list, " ");
1132 strcat(ramdisk_list, ramdisk);
1133 }
1134 }
1135
1136 void rem_ramdisk_swap(){
1137 char *ramdisk;
1138 char *iter = NULL;
1139
1140 ramdisk = strtok_r(ramdisk_list, " ", &iter);
1141 while (ramdisk != NULL) {
1142 if (opt_dry_run) {
1143 printf("swapoff %s\n", ramdisk);
1144 } else {
1145 DEBUG("swapoff %s\n", ramdisk);
1146 if (swapoff(ramdisk)) {
1147 WARNING("swapoff on %s failed: %s\n", ramdisk, strerror(errno));
1148 continue;
1149 }
1150 }
1151 ramdisk = strtok_r(NULL, " ", &iter);
1152 }
1153 }
1154
1155 void set_trans_opt(const char *file, const char *value)
1156 {
1157 FILE *f;
1158
1159 if (geteuid() != 0) {
1160 ERROR("Transparent huge page options can only be set by root\n");
1161 exit(EXIT_FAILURE);
1162 }
1163
1164 if (opt_dry_run) {
1165 printf("echo '%s' > %s\n", value, file);
1166 return;
1167 }
1168
1169 f = fopen(file, "w");
1170 if (!f) {
1171 ERROR("Couldn't open %s: %s\n", file, strerror(errno));
1172 return;
1173 }
1174
1175 fprintf(f, "%s", value);
1176 fclose(f);
1177 }
1178
1179 enum {
1180 POOL_MIN,
1181 POOL_MAX,
1182 POOL_BOTH,
1183 };
1184
1185 static long value_adjust(char *adjust_str, long base, long page_size)
1186 {
1187 long long adjust;
1188 char *iter;
1189
1190 /* Convert and validate the adjust. */
1191 errno = 0;
1192 adjust = strtol(adjust_str, &iter, 0);
1193 /* Catch strtol errors and sizes that overflow the native word size */
1194 if (errno || adjust_str == iter) {
1195 if (errno == ERANGE)
1196 errno = EOVERFLOW;
1197 else
1198 errno = EINVAL;
1199 ERROR("%s: invalid adjustment\n", adjust_str);
1200 exit(EXIT_FAILURE);
1201 }
1202
1203 /* size_to_smaller_unit() only works with positive values */
1204 if (adjust_str[0] == '-')
1205 adjust = -adjust;
1206
1207 switch (*iter) {
1208 case 'G':
1209 case 'g':
1210 adjust = size_to_smaller_unit(adjust);
1211 case 'M':
1212 case 'm':
1213 adjust = size_to_smaller_unit(adjust);
1214 case 'K':
1215 case 'k':
1216 adjust = size_to_smaller_unit(adjust);
1217 adjust = adjust / page_size;
1218 }
1219
1220 /* if previously negative, make negative again */
1221 if (adjust_str[0] == '-')
1222 adjust = -adjust;
1223
1224 if (adjust_str[0] != '+' && adjust_str[0] != '-')
1225 base = 0;
1226
1227 /* Ensure we neither go negative nor exceed LONG_MAX. */
1228 if (adjust < 0 && -adjust > base) {
1229 adjust = -base;
1230 }
1231 if (adjust > 0 && (base + adjust) < base) {
1232 adjust = LONG_MAX - base;
1233 }
1234 base += adjust;
1235
1236 DEBUG("Returning page count of %ld\n", base);
1237
1238 return base;
1239 }
1240
1241
1242 void pool_adjust(char *cmd, unsigned int counter)
1243 {
1244 struct hpage_pool pools[MAX_POOLS];
1245 int pos;
1246 int cnt;
1247
1248 char *iter = NULL;
1249 char *page_size_str = NULL;
1250 char *adjust_str = NULL;
1251 long page_size;
1252
1253 unsigned long min;
1254 unsigned long min_orig;
1255 unsigned long max;
1256 unsigned long last_pool_value;
1257
1258 /* Extract the pagesize and adjustment. */
1259 page_size_str = strtok_r(cmd, ":", &iter);
1260 if (page_size_str)
1261 adjust_str = strtok_r(NULL, ":", &iter);
1262
1263 if (!page_size_str || !adjust_str) {
1264 ERROR("%s: invalid resize specification\n", cmd);
1265 exit(EXIT_FAILURE);
1266 }
1267 INFO("page_size<%s> adjust<%s> counter<%d>\n",
1268 page_size_str, adjust_str, counter);
1269
1270 /* Convert and validate the page_size. */
1271 if (strcmp(page_size_str, "DEFAULT") == 0)
1272 page_size = kernel_default_hugepage_size();
1273 else
1274 page_size = parse_page_size(page_size_str);
1275
1276 DEBUG("Working with page_size of %ld\n", page_size);
1277
1278 cnt = hpool_sizes(pools, MAX_POOLS);
1279 if (cnt < 0) {
1280 ERROR("unable to obtain pools list");
1281 exit(EXIT_FAILURE);
1282 }
1283 for (pos = 0; cnt--; pos++) {
1284 if (pools[pos].pagesize == page_size)
1285 break;
1286 }
1287 if (cnt < 0) {
1288 ERROR("%s: unknown page size\n", page_size_str);
1289 exit(EXIT_FAILURE);
1290 }
1291
1292 min_orig = min = pools[pos].minimum;
1293 max = pools[pos].maximum;
1294
1295 if (counter == POOL_BOTH) {
1296 min = value_adjust(adjust_str, min, page_size);
1297 max = min;
1298 } else if (counter == POOL_MIN) {
1299 min = value_adjust(adjust_str, min, page_size);
1300 if (min > max)
1301 max = min;
1302 } else {
1303 max = value_adjust(adjust_str, max, page_size);
1304 if (max < min)
1305 min = max;
1306 }
1307
1308 INFO("%ld, %ld -> %ld, %ld\n", pools[pos].minimum, pools[pos].maximum,
1309 min, max);
1310
1311 if ((pools[pos].maximum - pools[pos].minimum) < (max - min)) {
1312 INFO("setting HUGEPAGES_OC to %ld\n", (max - min));
1313 set_huge_page_counter(page_size, HUGEPAGES_OC, (max - min));
1314 }
1315
1316 if (opt_hard)
1317 cnt = 5;
1318 else
1319 cnt = -1;
1320
1321 if (min > min_orig) {
1322 if (opt_temp_swap)
1323 add_temp_swap(page_size);
1324 if (opt_ramdisk_swap)
1325 add_ramdisk_swap(page_size);
1326 check_swap();
1327 }
1328
1329 if (opt_obey_mempolicy && get_huge_page_counter(page_size,
1330 HUGEPAGES_TOTAL_MEMPOL) < 0) {
1331 opt_obey_mempolicy = 0;
1332 WARNING("Counter for NUMA huge page allocations is not found, continuing with normal pool adjustment\n");
1333 }
1334
1335 INFO("setting HUGEPAGES_TOTAL%s to %ld\n",
1336 opt_obey_mempolicy ? "_MEMPOL" : "", min);
1337 set_huge_page_counter(page_size,
1338 opt_obey_mempolicy ? HUGEPAGES_TOTAL_MEMPOL : HUGEPAGES_TOTAL,
1339 min);
1340 get_pool_size(page_size, &pools[pos]);
1341
1342 /* If we fail to make an allocation, retry if user requests */
1343 last_pool_value = pools[pos].minimum;
1344 while ((pools[pos].minimum != min) && (cnt > 0)) {
1345 /* Make note if progress is being made and sleep for IO */
1346 if (last_pool_value == pools[pos].minimum)
1347 cnt--;
1348 else
1349 cnt = 5;
1350 sleep(6);
1351
1352 last_pool_value = pools[pos].minimum;
1353 INFO("Retrying allocation HUGEPAGES_TOTAL%s to %ld current %ld\n", opt_obey_mempolicy ? "_MEMPOL" : "", min, pools[pos].minimum);
1354 set_huge_page_counter(page_size,
1355 opt_obey_mempolicy ?
1356 HUGEPAGES_TOTAL_MEMPOL :
1357 HUGEPAGES_TOTAL,
1358 min);
1359 get_pool_size(page_size, &pools[pos]);
1360 }
1361
1362 if (min > min_orig && !opt_swap_persist) {
1363 if (opt_temp_swap)
1364 rem_temp_swap();
1365 else if (opt_ramdisk_swap)
1366 rem_ramdisk_swap();
1367 }
1368
1369 /*
1370 * HUGEPAGES_TOTAL is not guarenteed to check to exactly the figure
1371 * requested should there be insufficient pages. Check the new
1372 * value and adjust HUGEPAGES_OC accordingly.
1373 */
1374 if (pools[pos].minimum != min) {
1375 WARNING("failed to set pool minimum to %ld became %ld\n",
1376 min, pools[pos].minimum);
1377 min = pools[pos].minimum;
1378 }
1379 if (pools[pos].maximum != max) {
1380 INFO("setting HUGEPAGES_OC to %ld\n", (max - min));
1381 set_huge_page_counter(page_size, HUGEPAGES_OC, (max - min));
1382 }
1383 }
1384
1385 void page_sizes(int all)
1386 {
1387 struct hpage_pool pools[MAX_POOLS];
1388 int pos;
1389 int cnt;
1390
1391 cnt = hpool_sizes(pools, MAX_POOLS);
1392 if (cnt < 0) {
1393 ERROR("unable to obtain pools list");
1394 exit(EXIT_FAILURE);
1395 }
1396 qsort(pools, cnt, sizeof(pools[0]), cmpsizes);
1397
1398 for (pos = 0; cnt--; pos++) {
1399 if (all || (pools[pos].maximum &&
1400 hugetlbfs_find_path_for_size(pools[pos].pagesize)))
1401 printf("%ld\n", pools[pos].pagesize);
1402 }
1403 }
1404
1405 void explain()
1406 {
1407 show_mem();
1408 mounts_list_all();
1409 printf("\nHuge page pools:\n");
1410 pool_list();
1411 printf("\nHuge page sizes with configured pools:\n");
1412 page_sizes(0);
1413 check_minfreekbytes();
1414 check_shmmax();
1415 check_swap();
1416 check_user();
1417 printf("\nNote: Permanent swap space should be preferred when dynamic "
1418 "huge page pools are used.\n");
1419 }
1420
1421 int main(int argc, char** argv)
1422 {
1423 int ops;
1424 int has_hugepages = kernel_has_hugepages();
1425
1426 char opts[] = "+hdv";
1427 char base[PATH_MAX];
1428 char *opt_min_adj[MAX_POOLS], *opt_max_adj[MAX_POOLS];
1429 char *opt_user_mounts = NULL, *opt_group_mounts = NULL;
1430 int opt_list_mounts = 0, opt_pool_list = 0, opt_create_mounts = 0;
1431 int opt_global_mounts = 0, opt_pgsizes = 0, opt_pgsizes_all = 0;
1432 int opt_explain = 0, minadj_count = 0, maxadj_count = 0;
1433 int opt_trans_always = 0, opt_trans_never = 0, opt_trans_madvise = 0;
1434 int opt_khuge_pages = 0, opt_khuge_scan = 0, opt_khuge_alloc = 0;
1435 int ret = 0, index = 0;
1436 char *khuge_pages = NULL, *khuge_alloc = NULL, *khuge_scan = NULL;
1437 gid_t opt_gid = 0;
1438 struct group *opt_grp = NULL;
1439 int group_invalid = 0;
1440 struct option long_opts[] = {
1441 {"help", no_argument, NULL, 'h'},
1442 {"verbose", required_argument, NULL, 'v' },
1443
1444 {"list-all-mounts", no_argument, NULL, LONG_LIST_ALL_MOUNTS},
1445 {"pool-list", no_argument, NULL, LONG_POOL_LIST},
1446 {"pool-pages-min", required_argument, NULL, LONG_POOL_MIN_ADJ},
1447 {"pool-pages-max", required_argument, NULL, LONG_POOL_MAX_ADJ},
1448 {"obey-mempolicy", no_argument, NULL, LONG_POOL_MEMPOL},
1449 {"thp-always", no_argument, NULL, LONG_TRANS_ALWAYS},
1450 {"thp-madvise", no_argument, NULL, LONG_TRANS_MADVISE},
1451 {"thp-never", no_argument, NULL, LONG_TRANS_NEVER},
1452 {"thp-khugepaged-pages", required_argument, NULL, LONG_KHUGE_PAGES},
1453 {"thp-khugepaged-scan-sleep", required_argument, NULL, LONG_KHUGE_SCAN},
1454 {"thp-khugepaged-alloc-sleep", required_argument, NULL, LONG_KHUGE_ALLOC},
1455 {"set-recommended-min_free_kbytes", no_argument, NULL, LONG_SET_RECOMMENDED_MINFREEKBYTES},
1456 {"set-recommended-shmmax", no_argument, NULL, LONG_SET_RECOMMENDED_SHMMAX},
1457 {"set-shm-group", required_argument, NULL, LONG_SET_HUGETLB_SHM_GROUP},
1458 {"enable-zone-movable", no_argument, NULL, LONG_MOVABLE_ENABLE},
1459 {"disable-zone-movable", no_argument, NULL, LONG_MOVABLE_DISABLE},
1460 {"hard", no_argument, NULL, LONG_HARD},
1461 {"add-temp-swap", optional_argument, NULL, LONG_SWAP_DISK},
1462 {"add-ramdisk-swap", no_argument, NULL, LONG_SWAP_RAMDISK},
1463 {"persist", no_argument, NULL, LONG_SWAP_PERSIST},
1464 {"create-mounts", no_argument, NULL, LONG_CREATE_MOUNTS},
1465 {"create-user-mounts", required_argument, NULL, LONG_CREATE_USER_MOUNTS},
1466 {"create-group-mounts", required_argument, NULL, LONG_CREATE_GROUP_MOUNTS},
1467 {"create-global-mounts", no_argument, NULL, LONG_CREATE_GLOBAL_MOUNTS},
1468
1469 {"max-size", required_argument, NULL, LONG_LIMIT_SIZE},
1470 {"max-inodes", required_argument, NULL, LONG_LIMIT_INODES},
1471
1472 {"page-sizes", no_argument, NULL, LONG_PAGE_SIZES},
1473 {"page-sizes-all", no_argument, NULL, LONG_PAGE_AVAIL},
1474 {"dry-run", no_argument, NULL, 'd'},
1475 {"explain", no_argument, NULL, LONG_EXPLAIN},
1476
1477 {0},
1478 };
1479
1480 hugetlbfs_setup_debug();
1481 setup_mounts();
1482 verbose_init();
1483
1484 ops = 0;
1485 while (ret != -1) {
1486 ret = getopt_long(argc, argv, opts, long_opts, &index);
1487 switch (ret) {
1488 case -1:
1489 break;
1490
1491 case '?':
1492 print_usage();
1493 exit(EXIT_FAILURE);
1494
1495 case 'h':
1496 print_usage();
1497 exit(EXIT_SUCCESS);
1498
1499 case 'v':
1500 verbose(optarg);
1501 continue;
1502
1503 case 'd':
1504 opt_dry_run = 1;
1505 continue;
1506
1507 default:
1508 /* All other commands require hugepage support. */
1509 if (! has_hugepages) {
1510 ERROR("kernel does not support huge pages\n");
1511 exit(EXIT_FAILURE);
1512 }
1513 }
1514 switch (ret) {
1515 case -1:
1516 break;
1517
1518 case LONG_HARD:
1519 opt_hard = 1;
1520 continue;
1521
1522 case LONG_SWAP_DISK:
1523 if (optarg)
1524 opt_temp_swap = atoi(optarg);
1525 else
1526 opt_temp_swap = -1;
1527 break;
1528
1529 case LONG_SWAP_RAMDISK:
1530 opt_ramdisk_swap = 1;
1531 break;
1532
1533 case LONG_SWAP_PERSIST:
1534 opt_swap_persist = 1;
1535
1536 case LONG_LIST_ALL_MOUNTS:
1537 opt_list_mounts = 1;
1538 break;
1539
1540 case LONG_POOL_LIST:
1541 opt_pool_list = 1;
1542 break;
1543
1544 case LONG_POOL_MIN_ADJ:
1545 if (minadj_count == MAX_POOLS) {
1546 WARNING("Attempting to adjust an invalid "
1547 "pool or a pool multiple times, "
1548 "ignoring request: '%s'\n", optarg);
1549 } else {
1550 opt_min_adj[minadj_count++] = optarg;
1551 }
1552 break;
1553
1554 case LONG_POOL_MEMPOL:
1555 opt_obey_mempolicy = 1;
1556 break;
1557
1558 case LONG_TRANS_ALWAYS:
1559 opt_trans_always = 1;
1560 break;
1561
1562 case LONG_TRANS_MADVISE:
1563 opt_trans_madvise = 1;
1564 break;
1565
1566 case LONG_TRANS_NEVER:
1567 opt_trans_never = 1;
1568 break;
1569
1570 case LONG_KHUGE_PAGES:
1571 opt_khuge_pages = 1;
1572 khuge_pages = optarg;
1573 break;
1574
1575 case LONG_KHUGE_SCAN:
1576 opt_khuge_scan = 1;
1577 khuge_scan = optarg;
1578 break;
1579
1580 case LONG_KHUGE_ALLOC:
1581 opt_khuge_alloc = 1;
1582 khuge_alloc = optarg;
1583 break;
1584
1585 case LONG_POOL_MAX_ADJ:
1586 if (! kernel_has_overcommit()) {
1587 ERROR("kernel does not support overcommit, "
1588 "max cannot be adjusted\n");
1589 exit(EXIT_FAILURE);
1590 }
1591
1592 if (maxadj_count == MAX_POOLS) {
1593 WARNING("Attempting to adjust an invalid "
1594 "pool or a pool multiple times, "
1595 "ignoring request: '%s'\n", optarg);
1596 } else {
1597 opt_max_adj[maxadj_count++] = optarg;
1598 }
1599 break;
1600
1601 case LONG_MOVABLE_ENABLE:
1602 opt_movable = 1;
1603 break;
1604
1605 case LONG_SET_RECOMMENDED_MINFREEKBYTES:
1606 opt_set_recommended_minfreekbytes = 1;
1607 break;
1608
1609 case LONG_SET_RECOMMENDED_SHMMAX:
1610 opt_set_recommended_shmmax = 1;
1611 break;
1612
1613 case LONG_SET_HUGETLB_SHM_GROUP:
1614 opt_grp = getgrnam(optarg);
1615 if (!opt_grp) {
1616 opt_gid = atoi(optarg);
1617 if (opt_gid == 0 && strcmp(optarg, "0"))
1618 group_invalid = 1;
1619 opt_grp = getgrgid(opt_gid);
1620 if (!opt_grp)
1621 group_invalid = 1;
1622 } else {
1623 opt_gid = opt_grp->gr_gid;
1624 }
1625 if (group_invalid) {
1626 ERROR("Invalid group specification (%s)\n", optarg);
1627 exit(EXIT_FAILURE);
1628 }
1629 opt_set_hugetlb_shm_group = 1;
1630 break;
1631
1632 case LONG_MOVABLE_DISABLE:
1633 opt_movable = 0;
1634 break;
1635
1636 case LONG_CREATE_MOUNTS:
1637 opt_create_mounts = 1;
1638 break;
1639
1640 case LONG_CREATE_USER_MOUNTS:
1641 opt_user_mounts = optarg;
1642 break;
1643
1644 case LONG_CREATE_GROUP_MOUNTS:
1645 opt_group_mounts = optarg;
1646 break;
1647
1648 case LONG_CREATE_GLOBAL_MOUNTS:
1649 opt_global_mounts = 1;
1650 break;
1651
1652 case LONG_LIMIT_SIZE:
1653 /* Not a pagesize, but the conversions the same */
1654 opt_limit_mount_size = parse_page_size(optarg);
1655 if (!opt_limit_mount_size)
1656 WARNING("Mount max size specification 0, invalid or overflowed\n");
1657 break;
1658
1659 case LONG_LIMIT_INODES:
1660 opt_limit_mount_inodes = atoi(optarg);
1661 break;
1662
1663 case LONG_PAGE_SIZES:
1664 opt_pgsizes = 1;
1665 break;
1666
1667 case LONG_PAGE_AVAIL:
1668 opt_pgsizes_all = 1;
1669 break;
1670
1671 case LONG_EXPLAIN:
1672 opt_explain = 1;
1673 break;
1674
1675 default:
1676 WARNING("unparsed option %08x\n", ret);
1677 ret = -1;
1678 break;
1679 }
1680 if (ret != -1)
1681 ops++;
1682 }
1683
1684 verbose_expose();
1685
1686 if (opt_list_mounts)
1687 mounts_list_all();
1688
1689 if (opt_pool_list)
1690 pool_list();
1691
1692 if (opt_movable != -1)
1693 setup_zone_movable(opt_movable);
1694
1695 if (opt_trans_always)
1696 set_trans_opt(TRANS_ENABLE, ALWAYS);
1697
1698 if (opt_trans_madvise)
1699 set_trans_opt(TRANS_ENABLE, MADVISE);
1700
1701 if (opt_trans_never)
1702 set_trans_opt(TRANS_ENABLE, NEVER);
1703
1704 if (opt_khuge_pages)
1705 set_trans_opt(KHUGE_SCAN_PAGES, khuge_pages);
1706
1707 if (opt_khuge_alloc)
1708 set_trans_opt(KHUGE_ALLOC_SLEEP, khuge_alloc);
1709
1710 if (opt_khuge_scan)
1711 set_trans_opt(KHUGE_SCAN_SLEEP, khuge_scan);
1712
1713 if (opt_set_recommended_minfreekbytes)
1714 set_recommended_minfreekbytes();
1715
1716 if (opt_set_recommended_shmmax)
1717 set_recommended_shmmax();
1718
1719 if (opt_set_hugetlb_shm_group)
1720 set_hugetlb_shm_group(opt_gid, opt_grp->gr_name);
1721
1722 while (--minadj_count >= 0) {
1723 if (! kernel_has_overcommit())
1724 pool_adjust(opt_min_adj[minadj_count], POOL_BOTH);
1725 else
1726 pool_adjust(opt_min_adj[minadj_count], POOL_MIN);
1727 }
1728
1729 while (--maxadj_count >=0)
1730 pool_adjust(opt_max_adj[maxadj_count], POOL_MAX);
1731
1732 if (opt_create_mounts) {
1733 snprintf(base, PATH_MAX, "%s", MOUNT_DIR);
1734 create_mounts(NULL, NULL, base, S_IRWXU | S_IRWXG);
1735 }
1736
1737
1738 if (opt_user_mounts != NULL) {
1739 snprintf(base, PATH_MAX, "%s/user", MOUNT_DIR);
1740 create_mounts(opt_user_mounts, NULL, base, S_IRWXU);
1741 }
1742
1743 if (opt_group_mounts) {
1744 snprintf(base, PATH_MAX, "%s/group", MOUNT_DIR);
1745 create_mounts(NULL, opt_group_mounts, base, S_IRWXG);
1746 }
1747
1748 if (opt_global_mounts) {
1749 snprintf(base, PATH_MAX, "%s/global", MOUNT_DIR);
1750 create_mounts(NULL, NULL, base, S_IRWXU | S_IRWXG | S_IRWXO | S_ISVTX );
1751 }
1752
1753 if (opt_pgsizes)
1754 page_sizes(0);
1755
1756 if (opt_pgsizes_all)
1757 page_sizes(1);
1758
1759 if (opt_explain)
1760 explain();
1761
1762 index = optind;
1763
1764 if ((argc - index) != 0 || ops == 0) {
1765 print_usage();
1766 exit(EXIT_FAILURE);
1767 }
1768
1769 exit(EXIT_SUCCESS);
1770 }