4 * (C) Copyright 1995 Linus Torvalds
5 * (C) Copyright 2002 Christoph Hellwig
8 #include <linux/mman.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
;
22 if (newflags
== vma
->vm_flags
) {
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
));
37 if (start
!= vma
->vm_start
) {
38 ret
= split_vma(mm
, vma
, start
, 1);
43 if (end
!= vma
->vm_end
) {
44 ret
= split_vma(mm
, vma
, end
, 0);
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
) {
63 if (!(newflags
& VM_IO
))
64 ret
= make_pages_present(start
, end
);
67 vma
->vm_mm
->locked_vm
-= pages
;
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
;
80 len
= PAGE_ALIGN(len
);
86 vma
= find_vma_prev(current
->mm
, start
, &prev
);
87 if (!vma
|| vma
->vm_start
> start
)
90 if (start
> vma
->vm_start
)
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
;
100 newflags
&= ~VM_LOCKED
;
105 error
= mlock_fixup(vma
, &prev
, nstart
, tmp
, newflags
);
109 if (nstart
< prev
->vm_end
)
110 nstart
= prev
->vm_end
;
115 if (!vma
|| vma
->vm_start
!= nstart
) {
123 asmlinkage
long sys_mlock(unsigned long start
, size_t len
)
125 unsigned long locked
;
126 unsigned long lock_limit
;
132 down_write(¤t
->mm
->mmap_sem
);
133 len
= PAGE_ALIGN(len
+ (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(¤t
->mm
->mmap_sem
);
149 asmlinkage
long sys_munlock(unsigned long start
, size_t len
)
153 down_write(¤t
->mm
->mmap_sem
);
154 len
= PAGE_ALIGN(len
+ (start
& ~PAGE_MASK
));
156 ret
= do_mlock(start
, len
, 0);
157 up_write(¤t
->mm
->mmap_sem
);
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
)
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
;
180 mlock_fixup(vma
, &prev
, vma
->vm_start
, vma
->vm_end
, newflags
);
186 asmlinkage
long sys_mlockall(int flags
)
188 unsigned long lock_limit
;
191 if (!flags
|| (flags
& ~(MCL_CURRENT
| MCL_FUTURE
)))
198 down_write(¤t
->mm
->mmap_sem
);
200 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
201 lock_limit
>>= PAGE_SHIFT
;
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
);
212 asmlinkage
long sys_munlockall(void)
216 down_write(¤t
->mm
->mmap_sem
);
217 ret
= do_mlockall(0);
218 up_write(¤t
->mm
->mmap_sem
);
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
;
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
))
240 user
->locked_shm
+= locked
;
243 spin_unlock(&shmlock_user_lock
);
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
);