4 * (C) Copyright 1995 Linus Torvalds
5 * (C) Copyright 2002 Christoph Hellwig
8 #include <linux/capability.h>
9 #include <linux/mman.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
;
23 if (newflags
== vma
->vm_flags
) {
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
));
38 if (start
!= vma
->vm_start
) {
39 ret
= split_vma(mm
, vma
, start
, 1);
44 if (end
!= vma
->vm_end
) {
45 ret
= split_vma(mm
, vma
, end
, 0);
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
) {
64 if (!(newflags
& VM_IO
))
65 ret
= make_pages_present(start
, end
);
68 vma
->vm_mm
->locked_vm
-= pages
;
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
;
81 len
= PAGE_ALIGN(len
);
87 vma
= find_vma_prev(current
->mm
, start
, &prev
);
88 if (!vma
|| vma
->vm_start
> start
)
91 if (start
> vma
->vm_start
)
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
;
101 newflags
&= ~VM_LOCKED
;
106 error
= mlock_fixup(vma
, &prev
, nstart
, tmp
, newflags
);
110 if (nstart
< prev
->vm_end
)
111 nstart
= prev
->vm_end
;
116 if (!vma
|| vma
->vm_start
!= nstart
) {
124 asmlinkage
long sys_mlock(unsigned long start
, size_t len
)
126 unsigned long locked
;
127 unsigned long lock_limit
;
133 down_write(¤t
->mm
->mmap_sem
);
134 len
= PAGE_ALIGN(len
+ (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(¤t
->mm
->mmap_sem
);
150 asmlinkage
long sys_munlock(unsigned long start
, size_t len
)
154 down_write(¤t
->mm
->mmap_sem
);
155 len
= PAGE_ALIGN(len
+ (start
& ~PAGE_MASK
));
157 ret
= do_mlock(start
, len
, 0);
158 up_write(¤t
->mm
->mmap_sem
);
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
)
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
;
181 mlock_fixup(vma
, &prev
, vma
->vm_start
, vma
->vm_end
, newflags
);
187 asmlinkage
long sys_mlockall(int flags
)
189 unsigned long lock_limit
;
192 if (!flags
|| (flags
& ~(MCL_CURRENT
| MCL_FUTURE
)))
199 down_write(¤t
->mm
->mmap_sem
);
201 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
202 lock_limit
>>= PAGE_SHIFT
;
205 if (!(flags
& MCL_CURRENT
) || (current
->mm
->total_vm
<= lock_limit
) ||
206 capable(CAP_IPC_LOCK
))
207 ret
= do_mlockall(flags
);
208 up_write(¤t
->mm
->mmap_sem
);
213 asmlinkage
long sys_munlockall(void)
217 down_write(¤t
->mm
->mmap_sem
);
218 ret
= do_mlockall(0);
219 up_write(¤t
->mm
->mmap_sem
);
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
;
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
))
241 user
->locked_shm
+= locked
;
244 spin_unlock(&shmlock_user_lock
);
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
);