diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /mm/mlock.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'mm/mlock.c')
-rw-r--r-- | mm/mlock.c | 253 |
1 files changed, 253 insertions, 0 deletions
diff --git a/mm/mlock.c b/mm/mlock.c new file mode 100644 index 000000000000..4ae3a46ff768 --- /dev/null +++ b/mm/mlock.c | |||
@@ -0,0 +1,253 @@ | |||
1 | /* | ||
2 | * linux/mm/mlock.c | ||
3 | * | ||
4 | * (C) Copyright 1995 Linus Torvalds | ||
5 | * (C) Copyright 2002 Christoph Hellwig | ||
6 | */ | ||
7 | |||
8 | #include <linux/mman.h> | ||
9 | #include <linux/mm.h> | ||
10 | #include <linux/mempolicy.h> | ||
11 | #include <linux/syscalls.h> | ||
12 | |||
13 | |||
14 | static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev, | ||
15 | unsigned long start, unsigned long end, unsigned int newflags) | ||
16 | { | ||
17 | struct mm_struct * mm = vma->vm_mm; | ||
18 | pgoff_t pgoff; | ||
19 | int pages; | ||
20 | int ret = 0; | ||
21 | |||
22 | if (newflags == vma->vm_flags) { | ||
23 | *prev = vma; | ||
24 | goto out; | ||
25 | } | ||
26 | |||
27 | pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); | ||
28 | *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma, | ||
29 | vma->vm_file, pgoff, vma_policy(vma)); | ||
30 | if (*prev) { | ||
31 | vma = *prev; | ||
32 | goto success; | ||
33 | } | ||
34 | |||
35 | *prev = vma; | ||
36 | |||
37 | if (start != vma->vm_start) { | ||
38 | ret = split_vma(mm, vma, start, 1); | ||
39 | if (ret) | ||
40 | goto out; | ||
41 | } | ||
42 | |||
43 | if (end != vma->vm_end) { | ||
44 | ret = split_vma(mm, vma, end, 0); | ||
45 | if (ret) | ||
46 | goto out; | ||
47 | } | ||
48 | |||
49 | success: | ||
50 | /* | ||
51 | * vm_flags is protected by the mmap_sem held in write mode. | ||
52 | * It's okay if try_to_unmap_one unmaps a page just after we | ||
53 | * set VM_LOCKED, make_pages_present below will bring it back. | ||
54 | */ | ||
55 | vma->vm_flags = newflags; | ||
56 | |||
57 | /* | ||
58 | * Keep track of amount of locked VM. | ||
59 | */ | ||
60 | pages = (end - start) >> PAGE_SHIFT; | ||
61 | if (newflags & VM_LOCKED) { | ||
62 | pages = -pages; | ||
63 | if (!(newflags & VM_IO)) | ||
64 | ret = make_pages_present(start, end); | ||
65 | } | ||
66 | |||
67 | vma->vm_mm->locked_vm -= pages; | ||
68 | out: | ||
69 | if (ret == -ENOMEM) | ||
70 | ret = -EAGAIN; | ||
71 | return ret; | ||
72 | } | ||
73 | |||
74 | static int do_mlock(unsigned long start, size_t len, int on) | ||
75 | { | ||
76 | unsigned long nstart, end, tmp; | ||
77 | struct vm_area_struct * vma, * prev; | ||
78 | int error; | ||
79 | |||
80 | len = PAGE_ALIGN(len); | ||
81 | end = start + len; | ||
82 | if (end < start) | ||
83 | return -EINVAL; | ||
84 | if (end == start) | ||
85 | return 0; | ||
86 | vma = find_vma_prev(current->mm, start, &prev); | ||
87 | if (!vma || vma->vm_start > start) | ||
88 | return -ENOMEM; | ||
89 | |||
90 | if (start > vma->vm_start) | ||
91 | prev = vma; | ||
92 | |||
93 | for (nstart = start ; ; ) { | ||
94 | unsigned int newflags; | ||
95 | |||
96 | /* Here we know that vma->vm_start <= nstart < vma->vm_end. */ | ||
97 | |||
98 | newflags = vma->vm_flags | VM_LOCKED; | ||
99 | if (!on) | ||
100 | newflags &= ~VM_LOCKED; | ||
101 | |||
102 | tmp = vma->vm_end; | ||
103 | if (tmp > end) | ||
104 | tmp = end; | ||
105 | error = mlock_fixup(vma, &prev, nstart, tmp, newflags); | ||
106 | if (error) | ||
107 | break; | ||
108 | nstart = tmp; | ||
109 | if (nstart < prev->vm_end) | ||
110 | nstart = prev->vm_end; | ||
111 | if (nstart >= end) | ||
112 | break; | ||
113 | |||
114 | vma = prev->vm_next; | ||
115 | if (!vma || vma->vm_start != nstart) { | ||
116 | error = -ENOMEM; | ||
117 | break; | ||
118 | } | ||
119 | } | ||
120 | return error; | ||
121 | } | ||
122 | |||
123 | asmlinkage long sys_mlock(unsigned long start, size_t len) | ||
124 | { | ||
125 | unsigned long locked; | ||
126 | unsigned long lock_limit; | ||
127 | int error = -ENOMEM; | ||
128 | |||
129 | if (!can_do_mlock()) | ||
130 | return -EPERM; | ||
131 | |||
132 | down_write(¤t->mm->mmap_sem); | ||
133 | len = PAGE_ALIGN(len + (start & ~PAGE_MASK)); | ||
134 | start &= PAGE_MASK; | ||
135 | |||
136 | locked = len >> PAGE_SHIFT; | ||
137 | locked += current->mm->locked_vm; | ||
138 | |||
139 | lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur; | ||
140 | lock_limit >>= PAGE_SHIFT; | ||
141 | |||
142 | /* check against resource limits */ | ||
143 | if ((locked <= lock_limit) || capable(CAP_IPC_LOCK)) | ||
144 | error = do_mlock(start, len, 1); | ||
145 | up_write(¤t->mm->mmap_sem); | ||
146 | return error; | ||
147 | } | ||
148 | |||
149 | asmlinkage long sys_munlock(unsigned long start, size_t len) | ||
150 | { | ||
151 | int ret; | ||
152 | |||
153 | down_write(¤t->mm->mmap_sem); | ||
154 | len = PAGE_ALIGN(len + (start & ~PAGE_MASK)); | ||
155 | start &= PAGE_MASK; | ||
156 | ret = do_mlock(start, len, 0); | ||
157 | up_write(¤t->mm->mmap_sem); | ||
158 | return ret; | ||
159 | } | ||
160 | |||
161 | static int do_mlockall(int flags) | ||
162 | { | ||
163 | struct vm_area_struct * vma, * prev = NULL; | ||
164 | unsigned int def_flags = 0; | ||
165 | |||
166 | if (flags & MCL_FUTURE) | ||
167 | def_flags = VM_LOCKED; | ||
168 | current->mm->def_flags = def_flags; | ||
169 | if (flags == MCL_FUTURE) | ||
170 | goto out; | ||
171 | |||
172 | for (vma = current->mm->mmap; vma ; vma = prev->vm_next) { | ||
173 | unsigned int newflags; | ||
174 | |||
175 | newflags = vma->vm_flags | VM_LOCKED; | ||
176 | if (!(flags & MCL_CURRENT)) | ||
177 | newflags &= ~VM_LOCKED; | ||
178 | |||
179 | /* Ignore errors */ | ||
180 | mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags); | ||
181 | } | ||
182 | out: | ||
183 | return 0; | ||
184 | } | ||
185 | |||
186 | asmlinkage long sys_mlockall(int flags) | ||
187 | { | ||
188 | unsigned long lock_limit; | ||
189 | int ret = -EINVAL; | ||
190 | |||
191 | if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE))) | ||
192 | goto out; | ||
193 | |||
194 | ret = -EPERM; | ||
195 | if (!can_do_mlock()) | ||
196 | goto out; | ||
197 | |||
198 | down_write(¤t->mm->mmap_sem); | ||
199 | |||
200 | lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur; | ||
201 | lock_limit >>= PAGE_SHIFT; | ||
202 | |||
203 | ret = -ENOMEM; | ||
204 | if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) || | ||
205 | capable(CAP_IPC_LOCK)) | ||
206 | ret = do_mlockall(flags); | ||
207 | up_write(¤t->mm->mmap_sem); | ||
208 | out: | ||
209 | return ret; | ||
210 | } | ||
211 | |||
212 | asmlinkage long sys_munlockall(void) | ||
213 | { | ||
214 | int ret; | ||
215 | |||
216 | down_write(¤t->mm->mmap_sem); | ||
217 | ret = do_mlockall(0); | ||
218 | up_write(¤t->mm->mmap_sem); | ||
219 | return ret; | ||
220 | } | ||
221 | |||
222 | /* | ||
223 | * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB | ||
224 | * shm segments) get accounted against the user_struct instead. | ||
225 | */ | ||
226 | static DEFINE_SPINLOCK(shmlock_user_lock); | ||
227 | |||
228 | int user_shm_lock(size_t size, struct user_struct *user) | ||
229 | { | ||
230 | unsigned long lock_limit, locked; | ||
231 | int allowed = 0; | ||
232 | |||
233 | locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; | ||
234 | lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur; | ||
235 | lock_limit >>= PAGE_SHIFT; | ||
236 | spin_lock(&shmlock_user_lock); | ||
237 | if (locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK)) | ||
238 | goto out; | ||
239 | get_uid(user); | ||
240 | user->locked_shm += locked; | ||
241 | allowed = 1; | ||
242 | out: | ||
243 | spin_unlock(&shmlock_user_lock); | ||
244 | return allowed; | ||
245 | } | ||
246 | |||
247 | void user_shm_unlock(size_t size, struct user_struct *user) | ||
248 | { | ||
249 | spin_lock(&shmlock_user_lock); | ||
250 | user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT; | ||
251 | spin_unlock(&shmlock_user_lock); | ||
252 | free_uid(user); | ||
253 | } | ||