mem.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stddef.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <string.h>
  12. #include <sys/stat.h>
  13. #include <sys/mman.h>
  14. #include <sys/vfs.h>
  15. #include <linux/magic.h>
  16. #include <init.h>
  17. #include <os.h>
  18. /* Set by make_tempfile() during early boot. */
  19. static char *tempdir = NULL;
  20. /* Check if dir is on tmpfs. Return 0 if yes, -1 if no or error. */
  21. static int __init check_tmpfs(const char *dir)
  22. {
  23. struct statfs st;
  24. printf("Checking if %s is on tmpfs...", dir);
  25. if (statfs(dir, &st) < 0) {
  26. printf("%s\n", strerror(errno));
  27. } else if (st.f_type != TMPFS_MAGIC) {
  28. printf("no\n");
  29. } else {
  30. printf("OK\n");
  31. return 0;
  32. }
  33. return -1;
  34. }
  35. /*
  36. * Choose the tempdir to use. We want something on tmpfs so that our memory is
  37. * not subject to the host's vm.dirty_ratio. If a tempdir is specified in the
  38. * environment, we use that even if it's not on tmpfs, but we warn the user.
  39. * Otherwise, we try common tmpfs locations, and if no tmpfs directory is found
  40. * then we fall back to /tmp.
  41. */
  42. static char * __init choose_tempdir(void)
  43. {
  44. static const char * const vars[] = {
  45. "TMPDIR",
  46. "TMP",
  47. "TEMP",
  48. NULL
  49. };
  50. static const char fallback_dir[] = "/tmp";
  51. static const char * const tmpfs_dirs[] = {
  52. "/dev/shm",
  53. fallback_dir,
  54. NULL
  55. };
  56. int i;
  57. const char *dir;
  58. printf("Checking environment variables for a tempdir...");
  59. for (i = 0; vars[i]; i++) {
  60. dir = getenv(vars[i]);
  61. if ((dir != NULL) && (*dir != '\0')) {
  62. printf("%s\n", dir);
  63. if (check_tmpfs(dir) >= 0)
  64. goto done;
  65. else
  66. goto warn;
  67. }
  68. }
  69. printf("none found\n");
  70. for (i = 0; tmpfs_dirs[i]; i++) {
  71. dir = tmpfs_dirs[i];
  72. if (check_tmpfs(dir) >= 0)
  73. goto done;
  74. }
  75. dir = fallback_dir;
  76. warn:
  77. printf("Warning: tempdir %s is not on tmpfs\n", dir);
  78. done:
  79. /* Make a copy since getenv results may not remain valid forever. */
  80. return strdup(dir);
  81. }
  82. /*
  83. * Create an unlinked tempfile in a suitable tempdir. template must be the
  84. * basename part of the template with a leading '/'.
  85. */
  86. static int __init make_tempfile(const char *template)
  87. {
  88. char *tempname;
  89. int fd;
  90. if (tempdir == NULL) {
  91. tempdir = choose_tempdir();
  92. if (tempdir == NULL) {
  93. fprintf(stderr, "Failed to choose tempdir: %s\n",
  94. strerror(errno));
  95. return -1;
  96. }
  97. }
  98. #ifdef O_TMPFILE
  99. fd = open(tempdir, O_CLOEXEC | O_RDWR | O_EXCL | O_TMPFILE, 0700);
  100. /*
  101. * If the running system does not support O_TMPFILE flag then retry
  102. * without it.
  103. */
  104. if (fd != -1 || (errno != EINVAL && errno != EISDIR &&
  105. errno != EOPNOTSUPP))
  106. return fd;
  107. #endif
  108. tempname = malloc(strlen(tempdir) + strlen(template) + 1);
  109. if (tempname == NULL)
  110. return -1;
  111. strcpy(tempname, tempdir);
  112. strcat(tempname, template);
  113. fd = mkstemp(tempname);
  114. if (fd < 0) {
  115. fprintf(stderr, "open - cannot create %s: %s\n", tempname,
  116. strerror(errno));
  117. goto out;
  118. }
  119. if (unlink(tempname) < 0) {
  120. perror("unlink");
  121. goto close;
  122. }
  123. free(tempname);
  124. return fd;
  125. close:
  126. close(fd);
  127. out:
  128. free(tempname);
  129. return -1;
  130. }
  131. #define TEMPNAME_TEMPLATE "/vm_file-XXXXXX"
  132. static int __init create_tmp_file(unsigned long long len)
  133. {
  134. int fd, err;
  135. char zero;
  136. fd = make_tempfile(TEMPNAME_TEMPLATE);
  137. if (fd < 0)
  138. exit(1);
  139. /*
  140. * Seek to len - 1 because writing a character there will
  141. * increase the file size by one byte, to the desired length.
  142. */
  143. if (lseek64(fd, len - 1, SEEK_SET) < 0) {
  144. perror("lseek64");
  145. exit(1);
  146. }
  147. zero = 0;
  148. err = write(fd, &zero, 1);
  149. if (err != 1) {
  150. perror("write");
  151. exit(1);
  152. }
  153. return fd;
  154. }
  155. int __init create_mem_file(unsigned long long len)
  156. {
  157. int err, fd;
  158. fd = create_tmp_file(len);
  159. err = os_set_exec_close(fd);
  160. if (err < 0) {
  161. errno = -err;
  162. perror("exec_close");
  163. }
  164. return fd;
  165. }
  166. void __init check_tmpexec(void)
  167. {
  168. void *addr;
  169. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  170. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  171. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  172. printf("Checking PROT_EXEC mmap in %s...", tempdir);
  173. if (addr == MAP_FAILED) {
  174. err = errno;
  175. printf("%s\n", strerror(err));
  176. close(fd);
  177. if (err == EPERM)
  178. printf("%s must be not mounted noexec\n", tempdir);
  179. exit(1);
  180. }
  181. printf("OK\n");
  182. munmap(addr, UM_KERN_PAGE_SIZE);
  183. close(fd);
  184. }