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