[PATCH] Driver Core: remove driver model detach_state
[linux-2.6/verdex.git] / mm / mlock.c
blob4ae3a46ff7688a4d12d0022854120be978844242
1 /*
2 * linux/mm/mlock.c
4 * (C) Copyright 1995 Linus Torvalds
5 * (C) Copyright 2002 Christoph Hellwig
6 */
8 #include <linux/mman.h>
9 #include <linux/mm.h>
10 #include <linux/mempolicy.h>
11 #include <linux/syscalls.h>
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)
17 struct mm_struct * mm = vma->vm_mm;
18 pgoff_t pgoff;
19 int pages;
20 int ret = 0;
22 if (newflags == vma->vm_flags) {
23 *prev = vma;
24 goto out;
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;
35 *prev = vma;
37 if (start != vma->vm_start) {
38 ret = split_vma(mm, vma, start, 1);
39 if (ret)
40 goto out;
43 if (end != vma->vm_end) {
44 ret = split_vma(mm, vma, end, 0);
45 if (ret)
46 goto out;
49 success:
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.
55 vma->vm_flags = newflags;
58 * Keep track of amount of locked VM.
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);
67 vma->vm_mm->locked_vm -= pages;
68 out:
69 if (ret == -ENOMEM)
70 ret = -EAGAIN;
71 return ret;
74 static int do_mlock(unsigned long start, size_t len, int on)
76 unsigned long nstart, end, tmp;
77 struct vm_area_struct * vma, * prev;
78 int error;
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;
90 if (start > vma->vm_start)
91 prev = vma;
93 for (nstart = start ; ; ) {
94 unsigned int newflags;
96 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
98 newflags = vma->vm_flags | VM_LOCKED;
99 if (!on)
100 newflags &= ~VM_LOCKED;
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;
114 vma = prev->vm_next;
115 if (!vma || vma->vm_start != nstart) {
116 error = -ENOMEM;
117 break;
120 return error;
123 asmlinkage long sys_mlock(unsigned long start, size_t len)
125 unsigned long locked;
126 unsigned long lock_limit;
127 int error = -ENOMEM;
129 if (!can_do_mlock())
130 return -EPERM;
132 down_write(&current->mm->mmap_sem);
133 len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
134 start &= PAGE_MASK;
136 locked = len >> PAGE_SHIFT;
137 locked += current->mm->locked_vm;
139 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
140 lock_limit >>= PAGE_SHIFT;
142 /* check against resource limits */
143 if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
144 error = do_mlock(start, len, 1);
145 up_write(&current->mm->mmap_sem);
146 return error;
149 asmlinkage long sys_munlock(unsigned long start, size_t len)
151 int ret;
153 down_write(&current->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(&current->mm->mmap_sem);
158 return ret;
161 static int do_mlockall(int flags)
163 struct vm_area_struct * vma, * prev = NULL;
164 unsigned int def_flags = 0;
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;
172 for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
173 unsigned int newflags;
175 newflags = vma->vm_flags | VM_LOCKED;
176 if (!(flags & MCL_CURRENT))
177 newflags &= ~VM_LOCKED;
179 /* Ignore errors */
180 mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
182 out:
183 return 0;
186 asmlinkage long sys_mlockall(int flags)
188 unsigned long lock_limit;
189 int ret = -EINVAL;
191 if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE)))
192 goto out;
194 ret = -EPERM;
195 if (!can_do_mlock())
196 goto out;
198 down_write(&current->mm->mmap_sem);
200 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
201 lock_limit >>= PAGE_SHIFT;
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(&current->mm->mmap_sem);
208 out:
209 return ret;
212 asmlinkage long sys_munlockall(void)
214 int ret;
216 down_write(&current->mm->mmap_sem);
217 ret = do_mlockall(0);
218 up_write(&current->mm->mmap_sem);
219 return ret;
223 * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
224 * shm segments) get accounted against the user_struct instead.
226 static DEFINE_SPINLOCK(shmlock_user_lock);
228 int user_shm_lock(size_t size, struct user_struct *user)
230 unsigned long lock_limit, locked;
231 int allowed = 0;
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;
247 void user_shm_unlock(size_t size, struct user_struct *user)
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);