[PATCH] i386/x86-64: Return defined error value for bad PCI config space accesses
[linux-2.6/mini2440.git] / mm / mlock.c
blobb90c59573abf5da8bd00dcc7b224796ce66befc5
1 /*
2 * linux/mm/mlock.c
4 * (C) Copyright 1995 Linus Torvalds
5 * (C) Copyright 2002 Christoph Hellwig
6 */
8 #include <linux/capability.h>
9 #include <linux/mman.h>
10 #include <linux/mm.h>
11 #include <linux/mempolicy.h>
12 #include <linux/syscalls.h>
15 static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
16 unsigned long start, unsigned long end, unsigned int newflags)
18 struct mm_struct * mm = vma->vm_mm;
19 pgoff_t pgoff;
20 int pages;
21 int ret = 0;
23 if (newflags == vma->vm_flags) {
24 *prev = vma;
25 goto out;
28 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
29 *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
30 vma->vm_file, pgoff, vma_policy(vma));
31 if (*prev) {
32 vma = *prev;
33 goto success;
36 *prev = vma;
38 if (start != vma->vm_start) {
39 ret = split_vma(mm, vma, start, 1);
40 if (ret)
41 goto out;
44 if (end != vma->vm_end) {
45 ret = split_vma(mm, vma, end, 0);
46 if (ret)
47 goto out;
50 success:
52 * vm_flags is protected by the mmap_sem held in write mode.
53 * It's okay if try_to_unmap_one unmaps a page just after we
54 * set VM_LOCKED, make_pages_present below will bring it back.
56 vma->vm_flags = newflags;
59 * Keep track of amount of locked VM.
61 pages = (end - start) >> PAGE_SHIFT;
62 if (newflags & VM_LOCKED) {
63 pages = -pages;
64 if (!(newflags & VM_IO))
65 ret = make_pages_present(start, end);
68 vma->vm_mm->locked_vm -= pages;
69 out:
70 if (ret == -ENOMEM)
71 ret = -EAGAIN;
72 return ret;
75 static int do_mlock(unsigned long start, size_t len, int on)
77 unsigned long nstart, end, tmp;
78 struct vm_area_struct * vma, * prev;
79 int error;
81 len = PAGE_ALIGN(len);
82 end = start + len;
83 if (end < start)
84 return -EINVAL;
85 if (end == start)
86 return 0;
87 vma = find_vma_prev(current->mm, start, &prev);
88 if (!vma || vma->vm_start > start)
89 return -ENOMEM;
91 if (start > vma->vm_start)
92 prev = vma;
94 for (nstart = start ; ; ) {
95 unsigned int newflags;
97 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
99 newflags = vma->vm_flags | VM_LOCKED;
100 if (!on)
101 newflags &= ~VM_LOCKED;
103 tmp = vma->vm_end;
104 if (tmp > end)
105 tmp = end;
106 error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
107 if (error)
108 break;
109 nstart = tmp;
110 if (nstart < prev->vm_end)
111 nstart = prev->vm_end;
112 if (nstart >= end)
113 break;
115 vma = prev->vm_next;
116 if (!vma || vma->vm_start != nstart) {
117 error = -ENOMEM;
118 break;
121 return error;
124 asmlinkage long sys_mlock(unsigned long start, size_t len)
126 unsigned long locked;
127 unsigned long lock_limit;
128 int error = -ENOMEM;
130 if (!can_do_mlock())
131 return -EPERM;
133 down_write(&current->mm->mmap_sem);
134 len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
135 start &= PAGE_MASK;
137 locked = len >> PAGE_SHIFT;
138 locked += current->mm->locked_vm;
140 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
141 lock_limit >>= PAGE_SHIFT;
143 /* check against resource limits */
144 if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
145 error = do_mlock(start, len, 1);
146 up_write(&current->mm->mmap_sem);
147 return error;
150 asmlinkage long sys_munlock(unsigned long start, size_t len)
152 int ret;
154 down_write(&current->mm->mmap_sem);
155 len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
156 start &= PAGE_MASK;
157 ret = do_mlock(start, len, 0);
158 up_write(&current->mm->mmap_sem);
159 return ret;
162 static int do_mlockall(int flags)
164 struct vm_area_struct * vma, * prev = NULL;
165 unsigned int def_flags = 0;
167 if (flags & MCL_FUTURE)
168 def_flags = VM_LOCKED;
169 current->mm->def_flags = def_flags;
170 if (flags == MCL_FUTURE)
171 goto out;
173 for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
174 unsigned int newflags;
176 newflags = vma->vm_flags | VM_LOCKED;
177 if (!(flags & MCL_CURRENT))
178 newflags &= ~VM_LOCKED;
180 /* Ignore errors */
181 mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
183 out:
184 return 0;
187 asmlinkage long sys_mlockall(int flags)
189 unsigned long lock_limit;
190 int ret = -EINVAL;
192 if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE)))
193 goto out;
195 ret = -EPERM;
196 if (!can_do_mlock())
197 goto out;
199 down_write(&current->mm->mmap_sem);
201 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
202 lock_limit >>= PAGE_SHIFT;
204 ret = -ENOMEM;
205 if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
206 capable(CAP_IPC_LOCK))
207 ret = do_mlockall(flags);
208 up_write(&current->mm->mmap_sem);
209 out:
210 return ret;
213 asmlinkage long sys_munlockall(void)
215 int ret;
217 down_write(&current->mm->mmap_sem);
218 ret = do_mlockall(0);
219 up_write(&current->mm->mmap_sem);
220 return ret;
224 * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
225 * shm segments) get accounted against the user_struct instead.
227 static DEFINE_SPINLOCK(shmlock_user_lock);
229 int user_shm_lock(size_t size, struct user_struct *user)
231 unsigned long lock_limit, locked;
232 int allowed = 0;
234 locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
235 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
236 lock_limit >>= PAGE_SHIFT;
237 spin_lock(&shmlock_user_lock);
238 if (locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK))
239 goto out;
240 get_uid(user);
241 user->locked_shm += locked;
242 allowed = 1;
243 out:
244 spin_unlock(&shmlock_user_lock);
245 return allowed;
248 void user_shm_unlock(size_t size, struct user_struct *user)
250 spin_lock(&shmlock_user_lock);
251 user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
252 spin_unlock(&shmlock_user_lock);
253 free_uid(user);