]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/ipc/shm.c | |
3 | * Copyright (C) 1992, 1993 Krishna Balasubramanian | |
4 | * Many improvements/fixes by Bruno Haible. | |
5 | * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994. | |
6 | * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli. | |
7 | * | |
8 | * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <[email protected]> | |
9 | * BIGMEM support, Andrea Arcangeli <[email protected]> | |
10 | * SMP thread shm, Jean-Luc Boyard <[email protected]> | |
11 | * HIGHMEM support, Ingo Molnar <[email protected]> | |
12 | * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <[email protected]> | |
13 | * Shared /dev/zero support, Kanoj Sarcar <[email protected]> | |
14 | * Move the mm functionality over to mm/shmem.c, Christoph Rohland <[email protected]> | |
15 | * | |
073115d6 SG |
16 | * support for audit of ipc object properties and permission changes |
17 | * Dustin Kirkland <[email protected]> | |
4e982311 KK |
18 | * |
19 | * namespaces support | |
20 | * OpenVZ, SWsoft Inc. | |
21 | * Pavel Emelianov <[email protected]> | |
1da177e4 LT |
22 | */ |
23 | ||
1da177e4 LT |
24 | #include <linux/slab.h> |
25 | #include <linux/mm.h> | |
26 | #include <linux/hugetlb.h> | |
27 | #include <linux/shm.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/file.h> | |
30 | #include <linux/mman.h> | |
1da177e4 LT |
31 | #include <linux/shmem_fs.h> |
32 | #include <linux/security.h> | |
33 | #include <linux/syscalls.h> | |
34 | #include <linux/audit.h> | |
c59ede7b | 35 | #include <linux/capability.h> |
7d87e14c | 36 | #include <linux/ptrace.h> |
19b4946c | 37 | #include <linux/seq_file.h> |
5f921ae9 | 38 | #include <linux/mutex.h> |
4e982311 | 39 | #include <linux/nsproxy.h> |
bc56bba8 | 40 | #include <linux/mount.h> |
7d87e14c | 41 | |
1da177e4 LT |
42 | #include <asm/uaccess.h> |
43 | ||
44 | #include "util.h" | |
45 | ||
bc56bba8 EB |
46 | struct shm_file_data { |
47 | int id; | |
48 | struct ipc_namespace *ns; | |
49 | struct file *file; | |
50 | const struct vm_operations_struct *vm_ops; | |
51 | }; | |
52 | ||
53 | #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data)) | |
54 | ||
9a32144e | 55 | static const struct file_operations shm_file_operations; |
1da177e4 LT |
56 | static struct vm_operations_struct shm_vm_ops; |
57 | ||
4e982311 KK |
58 | static struct ipc_ids init_shm_ids; |
59 | ||
60 | #define shm_ids(ns) (*((ns)->ids[IPC_SHM_IDS])) | |
1da177e4 | 61 | |
4e982311 KK |
62 | #define shm_lock(ns, id) \ |
63 | ((struct shmid_kernel*)ipc_lock(&shm_ids(ns),id)) | |
64 | #define shm_unlock(shp) \ | |
65 | ipc_unlock(&(shp)->shm_perm) | |
66 | #define shm_get(ns, id) \ | |
67 | ((struct shmid_kernel*)ipc_get(&shm_ids(ns),id)) | |
68 | #define shm_buildid(ns, id, seq) \ | |
69 | ipc_buildid(&shm_ids(ns), id, seq) | |
1da177e4 | 70 | |
4e982311 KK |
71 | static int newseg (struct ipc_namespace *ns, key_t key, |
72 | int shmflg, size_t size); | |
bc56bba8 EB |
73 | static void shm_open(struct vm_area_struct *vma); |
74 | static void shm_close(struct vm_area_struct *vma); | |
4e982311 | 75 | static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp); |
1da177e4 | 76 | #ifdef CONFIG_PROC_FS |
19b4946c | 77 | static int sysvipc_shm_proc_show(struct seq_file *s, void *it); |
1da177e4 LT |
78 | #endif |
79 | ||
7d69a1f4 | 80 | static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids) |
4e982311 KK |
81 | { |
82 | ns->ids[IPC_SHM_IDS] = ids; | |
83 | ns->shm_ctlmax = SHMMAX; | |
84 | ns->shm_ctlall = SHMALL; | |
85 | ns->shm_ctlmni = SHMMNI; | |
86 | ns->shm_tot = 0; | |
7ca7e564 | 87 | ipc_init_ids(ids); |
4e982311 KK |
88 | } |
89 | ||
90 | static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp) | |
91 | { | |
92 | if (shp->shm_nattch){ | |
93 | shp->shm_perm.mode |= SHM_DEST; | |
94 | /* Do not find it any more */ | |
95 | shp->shm_perm.key = IPC_PRIVATE; | |
96 | shm_unlock(shp); | |
97 | } else | |
98 | shm_destroy(ns, shp); | |
99 | } | |
100 | ||
4e982311 KK |
101 | int shm_init_ns(struct ipc_namespace *ns) |
102 | { | |
103 | struct ipc_ids *ids; | |
104 | ||
105 | ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL); | |
106 | if (ids == NULL) | |
107 | return -ENOMEM; | |
1da177e4 | 108 | |
4e982311 KK |
109 | __shm_init_ns(ns, ids); |
110 | return 0; | |
111 | } | |
112 | ||
113 | void shm_exit_ns(struct ipc_namespace *ns) | |
114 | { | |
4e982311 | 115 | struct shmid_kernel *shp; |
7ca7e564 ND |
116 | int next_id; |
117 | int total, in_use; | |
4e982311 KK |
118 | |
119 | mutex_lock(&shm_ids(ns).mutex); | |
7ca7e564 ND |
120 | |
121 | in_use = shm_ids(ns).in_use; | |
122 | ||
123 | for (total = 0, next_id = 0; total < in_use; next_id++) { | |
124 | shp = idr_find(&shm_ids(ns).ipcs_idr, next_id); | |
4e982311 KK |
125 | if (shp == NULL) |
126 | continue; | |
7ca7e564 | 127 | ipc_lock_by_ptr(&shp->shm_perm); |
4e982311 | 128 | do_shm_rmid(ns, shp); |
7ca7e564 | 129 | total++; |
4e982311 KK |
130 | } |
131 | mutex_unlock(&shm_ids(ns).mutex); | |
132 | ||
133 | kfree(ns->ids[IPC_SHM_IDS]); | |
134 | ns->ids[IPC_SHM_IDS] = NULL; | |
135 | } | |
1da177e4 LT |
136 | |
137 | void __init shm_init (void) | |
138 | { | |
4e982311 | 139 | __shm_init_ns(&init_ipc_ns, &init_shm_ids); |
19b4946c MW |
140 | ipc_init_proc_interface("sysvipc/shm", |
141 | " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n", | |
4e982311 | 142 | IPC_SHM_IDS, sysvipc_shm_proc_show); |
1da177e4 LT |
143 | } |
144 | ||
4e982311 KK |
145 | static inline int shm_checkid(struct ipc_namespace *ns, |
146 | struct shmid_kernel *s, int id) | |
1da177e4 | 147 | { |
4e982311 | 148 | if (ipc_checkid(&shm_ids(ns), &s->shm_perm, id)) |
1da177e4 LT |
149 | return -EIDRM; |
150 | return 0; | |
151 | } | |
152 | ||
7ca7e564 | 153 | static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s) |
1da177e4 | 154 | { |
7ca7e564 | 155 | ipc_rmid(&shm_ids(ns), &s->shm_perm); |
1da177e4 LT |
156 | } |
157 | ||
4e982311 | 158 | static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp) |
1da177e4 | 159 | { |
4e982311 | 160 | return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni); |
1da177e4 LT |
161 | } |
162 | ||
163 | ||
164 | ||
bc56bba8 EB |
165 | /* This is called by fork, once for every shm attach. */ |
166 | static void shm_open(struct vm_area_struct *vma) | |
4e982311 | 167 | { |
bc56bba8 EB |
168 | struct file *file = vma->vm_file; |
169 | struct shm_file_data *sfd = shm_file_data(file); | |
1da177e4 LT |
170 | struct shmid_kernel *shp; |
171 | ||
bc56bba8 | 172 | shp = shm_lock(sfd->ns, sfd->id); |
9ba025f1 | 173 | BUG_ON(!shp); |
1da177e4 | 174 | shp->shm_atim = get_seconds(); |
b488893a | 175 | shp->shm_lprid = task_tgid_vnr(current); |
1da177e4 LT |
176 | shp->shm_nattch++; |
177 | shm_unlock(shp); | |
178 | } | |
179 | ||
1da177e4 LT |
180 | /* |
181 | * shm_destroy - free the struct shmid_kernel | |
182 | * | |
183 | * @shp: struct to free | |
184 | * | |
5f921ae9 | 185 | * It has to be called with shp and shm_ids.mutex locked, |
1da177e4 LT |
186 | * but returns with shp unlocked and freed. |
187 | */ | |
4e982311 | 188 | static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp) |
1da177e4 | 189 | { |
4e982311 | 190 | ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT; |
7ca7e564 | 191 | shm_rmid(ns, shp); |
1da177e4 LT |
192 | shm_unlock(shp); |
193 | if (!is_file_hugepages(shp->shm_file)) | |
194 | shmem_lock(shp->shm_file, 0, shp->mlock_user); | |
195 | else | |
6d63079a | 196 | user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size, |
1da177e4 LT |
197 | shp->mlock_user); |
198 | fput (shp->shm_file); | |
199 | security_shm_free(shp); | |
200 | ipc_rcu_putref(shp); | |
201 | } | |
202 | ||
203 | /* | |
bc56bba8 | 204 | * remove the attach descriptor vma. |
1da177e4 LT |
205 | * free memory for segment if it is marked destroyed. |
206 | * The descriptor has already been removed from the current->mm->mmap list | |
207 | * and will later be kfree()d. | |
208 | */ | |
bc56bba8 | 209 | static void shm_close(struct vm_area_struct *vma) |
1da177e4 | 210 | { |
bc56bba8 EB |
211 | struct file * file = vma->vm_file; |
212 | struct shm_file_data *sfd = shm_file_data(file); | |
1da177e4 | 213 | struct shmid_kernel *shp; |
bc56bba8 | 214 | struct ipc_namespace *ns = sfd->ns; |
4e982311 KK |
215 | |
216 | mutex_lock(&shm_ids(ns).mutex); | |
1da177e4 | 217 | /* remove from the list of attaches of the shm segment */ |
bc56bba8 | 218 | shp = shm_lock(ns, sfd->id); |
9ba025f1 | 219 | BUG_ON(!shp); |
b488893a | 220 | shp->shm_lprid = task_tgid_vnr(current); |
1da177e4 LT |
221 | shp->shm_dtim = get_seconds(); |
222 | shp->shm_nattch--; | |
223 | if(shp->shm_nattch == 0 && | |
b33291c0 | 224 | shp->shm_perm.mode & SHM_DEST) |
4e982311 | 225 | shm_destroy(ns, shp); |
1da177e4 LT |
226 | else |
227 | shm_unlock(shp); | |
4e982311 | 228 | mutex_unlock(&shm_ids(ns).mutex); |
1da177e4 LT |
229 | } |
230 | ||
d0217ac0 | 231 | static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
bc56bba8 EB |
232 | { |
233 | struct file *file = vma->vm_file; | |
234 | struct shm_file_data *sfd = shm_file_data(file); | |
235 | ||
d0217ac0 | 236 | return sfd->vm_ops->fault(vma, vmf); |
bc56bba8 EB |
237 | } |
238 | ||
239 | #ifdef CONFIG_NUMA | |
d823e3e7 | 240 | static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new) |
bc56bba8 EB |
241 | { |
242 | struct file *file = vma->vm_file; | |
243 | struct shm_file_data *sfd = shm_file_data(file); | |
244 | int err = 0; | |
245 | if (sfd->vm_ops->set_policy) | |
246 | err = sfd->vm_ops->set_policy(vma, new); | |
247 | return err; | |
248 | } | |
249 | ||
d823e3e7 AB |
250 | static struct mempolicy *shm_get_policy(struct vm_area_struct *vma, |
251 | unsigned long addr) | |
bc56bba8 EB |
252 | { |
253 | struct file *file = vma->vm_file; | |
254 | struct shm_file_data *sfd = shm_file_data(file); | |
255 | struct mempolicy *pol = NULL; | |
256 | ||
257 | if (sfd->vm_ops->get_policy) | |
258 | pol = sfd->vm_ops->get_policy(vma, addr); | |
22741925 | 259 | else if (vma->vm_policy) |
bc56bba8 | 260 | pol = vma->vm_policy; |
22741925 AL |
261 | else |
262 | pol = current->mempolicy; | |
bc56bba8 EB |
263 | return pol; |
264 | } | |
265 | #endif | |
266 | ||
1da177e4 LT |
267 | static int shm_mmap(struct file * file, struct vm_area_struct * vma) |
268 | { | |
bc56bba8 | 269 | struct shm_file_data *sfd = shm_file_data(file); |
b0e15190 DH |
270 | int ret; |
271 | ||
bc56bba8 EB |
272 | ret = sfd->file->f_op->mmap(sfd->file, vma); |
273 | if (ret != 0) | |
274 | return ret; | |
275 | sfd->vm_ops = vma->vm_ops; | |
2e92a3ba | 276 | #ifdef CONFIG_MMU |
54cb8821 | 277 | BUG_ON(!sfd->vm_ops->fault); |
2e92a3ba | 278 | #endif |
bc56bba8 EB |
279 | vma->vm_ops = &shm_vm_ops; |
280 | shm_open(vma); | |
b0e15190 DH |
281 | |
282 | return ret; | |
1da177e4 LT |
283 | } |
284 | ||
4e982311 KK |
285 | static int shm_release(struct inode *ino, struct file *file) |
286 | { | |
bc56bba8 | 287 | struct shm_file_data *sfd = shm_file_data(file); |
4e982311 | 288 | |
bc56bba8 EB |
289 | put_ipc_ns(sfd->ns); |
290 | shm_file_data(file) = NULL; | |
291 | kfree(sfd); | |
4e982311 KK |
292 | return 0; |
293 | } | |
294 | ||
516dffdc AL |
295 | static int shm_fsync(struct file *file, struct dentry *dentry, int datasync) |
296 | { | |
297 | int (*fsync) (struct file *, struct dentry *, int datasync); | |
298 | struct shm_file_data *sfd = shm_file_data(file); | |
299 | int ret = -EINVAL; | |
300 | ||
301 | fsync = sfd->file->f_op->fsync; | |
302 | if (fsync) | |
303 | ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync); | |
304 | return ret; | |
305 | } | |
306 | ||
bc56bba8 EB |
307 | static unsigned long shm_get_unmapped_area(struct file *file, |
308 | unsigned long addr, unsigned long len, unsigned long pgoff, | |
309 | unsigned long flags) | |
310 | { | |
311 | struct shm_file_data *sfd = shm_file_data(file); | |
516dffdc AL |
312 | return get_unmapped_area(sfd->file, addr, len, pgoff, flags); |
313 | } | |
314 | ||
315 | int is_file_shm_hugepages(struct file *file) | |
316 | { | |
317 | int ret = 0; | |
318 | ||
319 | if (file->f_op == &shm_file_operations) { | |
320 | struct shm_file_data *sfd; | |
321 | sfd = shm_file_data(file); | |
322 | ret = is_file_hugepages(sfd->file); | |
323 | } | |
324 | return ret; | |
bc56bba8 | 325 | } |
bc56bba8 | 326 | |
9a32144e | 327 | static const struct file_operations shm_file_operations = { |
4e982311 | 328 | .mmap = shm_mmap, |
516dffdc | 329 | .fsync = shm_fsync, |
4e982311 | 330 | .release = shm_release, |
bc56bba8 | 331 | .get_unmapped_area = shm_get_unmapped_area, |
1da177e4 LT |
332 | }; |
333 | ||
334 | static struct vm_operations_struct shm_vm_ops = { | |
335 | .open = shm_open, /* callback for a new vm-area open */ | |
336 | .close = shm_close, /* callback for when the vm-area is released */ | |
54cb8821 | 337 | .fault = shm_fault, |
bc56bba8 EB |
338 | #if defined(CONFIG_NUMA) |
339 | .set_policy = shm_set_policy, | |
340 | .get_policy = shm_get_policy, | |
1da177e4 LT |
341 | #endif |
342 | }; | |
343 | ||
4e982311 | 344 | static int newseg (struct ipc_namespace *ns, key_t key, int shmflg, size_t size) |
1da177e4 LT |
345 | { |
346 | int error; | |
347 | struct shmid_kernel *shp; | |
348 | int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT; | |
349 | struct file * file; | |
350 | char name[13]; | |
351 | int id; | |
352 | ||
4e982311 | 353 | if (size < SHMMIN || size > ns->shm_ctlmax) |
1da177e4 LT |
354 | return -EINVAL; |
355 | ||
f66d45e9 | 356 | if (ns->shm_tot + numpages > ns->shm_ctlall) |
1da177e4 LT |
357 | return -ENOSPC; |
358 | ||
359 | shp = ipc_rcu_alloc(sizeof(*shp)); | |
360 | if (!shp) | |
361 | return -ENOMEM; | |
362 | ||
363 | shp->shm_perm.key = key; | |
b33291c0 | 364 | shp->shm_perm.mode = (shmflg & S_IRWXUGO); |
1da177e4 LT |
365 | shp->mlock_user = NULL; |
366 | ||
367 | shp->shm_perm.security = NULL; | |
368 | error = security_shm_alloc(shp); | |
369 | if (error) { | |
370 | ipc_rcu_putref(shp); | |
371 | return error; | |
372 | } | |
373 | ||
9d66586f | 374 | sprintf (name, "SYSV%08x", key); |
1da177e4 | 375 | if (shmflg & SHM_HUGETLB) { |
9d66586f EB |
376 | /* hugetlb_file_setup takes care of mlock user accounting */ |
377 | file = hugetlb_file_setup(name, size); | |
1da177e4 LT |
378 | shp->mlock_user = current->user; |
379 | } else { | |
bf8f972d BP |
380 | int acctflag = VM_ACCOUNT; |
381 | /* | |
382 | * Do not allow no accounting for OVERCOMMIT_NEVER, even | |
383 | * if it's asked for. | |
384 | */ | |
385 | if ((shmflg & SHM_NORESERVE) && | |
386 | sysctl_overcommit_memory != OVERCOMMIT_NEVER) | |
387 | acctflag = 0; | |
bf8f972d | 388 | file = shmem_file_setup(name, size, acctflag); |
1da177e4 LT |
389 | } |
390 | error = PTR_ERR(file); | |
391 | if (IS_ERR(file)) | |
392 | goto no_file; | |
393 | ||
394 | error = -ENOSPC; | |
4e982311 | 395 | id = shm_addid(ns, shp); |
1da177e4 LT |
396 | if(id == -1) |
397 | goto no_id; | |
398 | ||
b488893a | 399 | shp->shm_cprid = task_tgid_vnr(current); |
1da177e4 LT |
400 | shp->shm_lprid = 0; |
401 | shp->shm_atim = shp->shm_dtim = 0; | |
402 | shp->shm_ctim = get_seconds(); | |
403 | shp->shm_segsz = size; | |
404 | shp->shm_nattch = 0; | |
7ca7e564 | 405 | shp->shm_perm.id = shm_buildid(ns, id, shp->shm_perm.seq); |
1da177e4 | 406 | shp->shm_file = file; |
30475cc1 BP |
407 | /* |
408 | * shmid gets reported as "inode#" in /proc/pid/maps. | |
409 | * proc-ps tools use this. Changing this will break them. | |
410 | */ | |
7ca7e564 | 411 | file->f_dentry->d_inode->i_ino = shp->shm_perm.id; |
551110a9 | 412 | |
4e982311 | 413 | ns->shm_tot += numpages; |
7ca7e564 | 414 | error = shp->shm_perm.id; |
1da177e4 | 415 | shm_unlock(shp); |
7ca7e564 | 416 | return error; |
1da177e4 LT |
417 | |
418 | no_id: | |
419 | fput(file); | |
420 | no_file: | |
421 | security_shm_free(shp); | |
422 | ipc_rcu_putref(shp); | |
423 | return error; | |
424 | } | |
425 | ||
426 | asmlinkage long sys_shmget (key_t key, size_t size, int shmflg) | |
427 | { | |
428 | struct shmid_kernel *shp; | |
7ca7e564 | 429 | int err; |
4e982311 KK |
430 | struct ipc_namespace *ns; |
431 | ||
432 | ns = current->nsproxy->ipc_ns; | |
1da177e4 | 433 | |
7ca7e564 ND |
434 | err = idr_pre_get(&shm_ids(ns).ipcs_idr, GFP_KERNEL); |
435 | ||
1da177e4 | 436 | if (key == IPC_PRIVATE) { |
7ca7e564 ND |
437 | if (!err) |
438 | err = -ENOMEM; | |
439 | else { | |
440 | mutex_lock(&shm_ids(ns).mutex); | |
4e982311 | 441 | err = newseg(ns, key, shmflg, size); |
7ca7e564 ND |
442 | mutex_unlock(&shm_ids(ns).mutex); |
443 | } | |
1da177e4 | 444 | } else { |
7ca7e564 ND |
445 | mutex_lock(&shm_ids(ns).mutex); |
446 | shp = (struct shmid_kernel *) ipc_findkey(&shm_ids(ns), key); | |
447 | if (shp == NULL) { | |
448 | if (!(shmflg & IPC_CREAT)) | |
449 | err = -ENOENT; | |
450 | else if (!err) | |
451 | err = -ENOMEM; | |
452 | else | |
453 | err = newseg(ns, key, shmflg, size); | |
454 | } else { | |
455 | /* shp has been locked by ipc_findkey() */ | |
456 | ||
457 | if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) | |
458 | err = -EEXIST; | |
459 | else { | |
460 | if (shp->shm_segsz < size) | |
461 | err = -EINVAL; | |
462 | else if (ipcperms(&shp->shm_perm, shmflg)) | |
463 | err = -EACCES; | |
464 | else { | |
465 | err = security_shm_associate(shp, | |
466 | shmflg); | |
467 | if (!err) | |
468 | err = shp->shm_perm.id; | |
469 | } | |
470 | } | |
471 | shm_unlock(shp); | |
1da177e4 | 472 | } |
7ca7e564 | 473 | mutex_unlock(&shm_ids(ns).mutex); |
1da177e4 | 474 | } |
1da177e4 LT |
475 | |
476 | return err; | |
477 | } | |
478 | ||
479 | static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version) | |
480 | { | |
481 | switch(version) { | |
482 | case IPC_64: | |
483 | return copy_to_user(buf, in, sizeof(*in)); | |
484 | case IPC_OLD: | |
485 | { | |
486 | struct shmid_ds out; | |
487 | ||
488 | ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm); | |
489 | out.shm_segsz = in->shm_segsz; | |
490 | out.shm_atime = in->shm_atime; | |
491 | out.shm_dtime = in->shm_dtime; | |
492 | out.shm_ctime = in->shm_ctime; | |
493 | out.shm_cpid = in->shm_cpid; | |
494 | out.shm_lpid = in->shm_lpid; | |
495 | out.shm_nattch = in->shm_nattch; | |
496 | ||
497 | return copy_to_user(buf, &out, sizeof(out)); | |
498 | } | |
499 | default: | |
500 | return -EINVAL; | |
501 | } | |
502 | } | |
503 | ||
504 | struct shm_setbuf { | |
505 | uid_t uid; | |
506 | gid_t gid; | |
507 | mode_t mode; | |
508 | }; | |
509 | ||
510 | static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version) | |
511 | { | |
512 | switch(version) { | |
513 | case IPC_64: | |
514 | { | |
515 | struct shmid64_ds tbuf; | |
516 | ||
517 | if (copy_from_user(&tbuf, buf, sizeof(tbuf))) | |
518 | return -EFAULT; | |
519 | ||
520 | out->uid = tbuf.shm_perm.uid; | |
521 | out->gid = tbuf.shm_perm.gid; | |
b33291c0 | 522 | out->mode = tbuf.shm_perm.mode; |
1da177e4 LT |
523 | |
524 | return 0; | |
525 | } | |
526 | case IPC_OLD: | |
527 | { | |
528 | struct shmid_ds tbuf_old; | |
529 | ||
530 | if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old))) | |
531 | return -EFAULT; | |
532 | ||
533 | out->uid = tbuf_old.shm_perm.uid; | |
534 | out->gid = tbuf_old.shm_perm.gid; | |
b33291c0 | 535 | out->mode = tbuf_old.shm_perm.mode; |
1da177e4 LT |
536 | |
537 | return 0; | |
538 | } | |
539 | default: | |
540 | return -EINVAL; | |
541 | } | |
542 | } | |
543 | ||
544 | static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version) | |
545 | { | |
546 | switch(version) { | |
547 | case IPC_64: | |
548 | return copy_to_user(buf, in, sizeof(*in)); | |
549 | case IPC_OLD: | |
550 | { | |
551 | struct shminfo out; | |
552 | ||
553 | if(in->shmmax > INT_MAX) | |
554 | out.shmmax = INT_MAX; | |
555 | else | |
556 | out.shmmax = (int)in->shmmax; | |
557 | ||
558 | out.shmmin = in->shmmin; | |
559 | out.shmmni = in->shmmni; | |
560 | out.shmseg = in->shmseg; | |
561 | out.shmall = in->shmall; | |
562 | ||
563 | return copy_to_user(buf, &out, sizeof(out)); | |
564 | } | |
565 | default: | |
566 | return -EINVAL; | |
567 | } | |
568 | } | |
569 | ||
4e982311 KK |
570 | static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss, |
571 | unsigned long *swp) | |
1da177e4 | 572 | { |
7ca7e564 ND |
573 | int next_id; |
574 | int total, in_use; | |
1da177e4 LT |
575 | |
576 | *rss = 0; | |
577 | *swp = 0; | |
578 | ||
7ca7e564 ND |
579 | in_use = shm_ids(ns).in_use; |
580 | ||
581 | for (total = 0, next_id = 0; total < in_use; next_id++) { | |
1da177e4 LT |
582 | struct shmid_kernel *shp; |
583 | struct inode *inode; | |
584 | ||
7ca7e564 ND |
585 | shp = shm_get(ns, next_id); |
586 | if (shp == NULL) | |
1da177e4 LT |
587 | continue; |
588 | ||
6d63079a | 589 | inode = shp->shm_file->f_path.dentry->d_inode; |
1da177e4 LT |
590 | |
591 | if (is_file_hugepages(shp->shm_file)) { | |
592 | struct address_space *mapping = inode->i_mapping; | |
593 | *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages; | |
594 | } else { | |
595 | struct shmem_inode_info *info = SHMEM_I(inode); | |
596 | spin_lock(&info->lock); | |
597 | *rss += inode->i_mapping->nrpages; | |
598 | *swp += info->swapped; | |
599 | spin_unlock(&info->lock); | |
600 | } | |
7ca7e564 ND |
601 | |
602 | total++; | |
1da177e4 LT |
603 | } |
604 | } | |
605 | ||
606 | asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf) | |
607 | { | |
608 | struct shm_setbuf setbuf; | |
609 | struct shmid_kernel *shp; | |
610 | int err, version; | |
4e982311 | 611 | struct ipc_namespace *ns; |
1da177e4 LT |
612 | |
613 | if (cmd < 0 || shmid < 0) { | |
614 | err = -EINVAL; | |
615 | goto out; | |
616 | } | |
617 | ||
618 | version = ipc_parse_version(&cmd); | |
4e982311 | 619 | ns = current->nsproxy->ipc_ns; |
1da177e4 LT |
620 | |
621 | switch (cmd) { /* replace with proc interface ? */ | |
622 | case IPC_INFO: | |
623 | { | |
624 | struct shminfo64 shminfo; | |
625 | ||
626 | err = security_shm_shmctl(NULL, cmd); | |
627 | if (err) | |
628 | return err; | |
629 | ||
630 | memset(&shminfo,0,sizeof(shminfo)); | |
4e982311 KK |
631 | shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni; |
632 | shminfo.shmmax = ns->shm_ctlmax; | |
633 | shminfo.shmall = ns->shm_ctlall; | |
1da177e4 LT |
634 | |
635 | shminfo.shmmin = SHMMIN; | |
636 | if(copy_shminfo_to_user (buf, &shminfo, version)) | |
637 | return -EFAULT; | |
638 | /* reading a integer is always atomic */ | |
7ca7e564 | 639 | err = ipc_get_maxid(&shm_ids(ns)); |
1da177e4 LT |
640 | if(err<0) |
641 | err = 0; | |
642 | goto out; | |
643 | } | |
644 | case SHM_INFO: | |
645 | { | |
646 | struct shm_info shm_info; | |
647 | ||
648 | err = security_shm_shmctl(NULL, cmd); | |
649 | if (err) | |
650 | return err; | |
651 | ||
652 | memset(&shm_info,0,sizeof(shm_info)); | |
4e982311 KK |
653 | mutex_lock(&shm_ids(ns).mutex); |
654 | shm_info.used_ids = shm_ids(ns).in_use; | |
655 | shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp); | |
656 | shm_info.shm_tot = ns->shm_tot; | |
1da177e4 LT |
657 | shm_info.swap_attempts = 0; |
658 | shm_info.swap_successes = 0; | |
7ca7e564 | 659 | err = ipc_get_maxid(&shm_ids(ns)); |
4e982311 | 660 | mutex_unlock(&shm_ids(ns).mutex); |
1da177e4 LT |
661 | if(copy_to_user (buf, &shm_info, sizeof(shm_info))) { |
662 | err = -EFAULT; | |
663 | goto out; | |
664 | } | |
665 | ||
666 | err = err < 0 ? 0 : err; | |
667 | goto out; | |
668 | } | |
669 | case SHM_STAT: | |
670 | case IPC_STAT: | |
671 | { | |
672 | struct shmid64_ds tbuf; | |
673 | int result; | |
674 | memset(&tbuf, 0, sizeof(tbuf)); | |
4e982311 | 675 | shp = shm_lock(ns, shmid); |
1da177e4 LT |
676 | if(shp==NULL) { |
677 | err = -EINVAL; | |
678 | goto out; | |
7ca7e564 ND |
679 | } else if (cmd == SHM_STAT) { |
680 | result = shp->shm_perm.id; | |
1da177e4 | 681 | } else { |
4e982311 | 682 | err = shm_checkid(ns, shp,shmid); |
1da177e4 LT |
683 | if(err) |
684 | goto out_unlock; | |
685 | result = 0; | |
686 | } | |
687 | err=-EACCES; | |
688 | if (ipcperms (&shp->shm_perm, S_IRUGO)) | |
689 | goto out_unlock; | |
690 | err = security_shm_shmctl(shp, cmd); | |
691 | if (err) | |
692 | goto out_unlock; | |
693 | kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm); | |
694 | tbuf.shm_segsz = shp->shm_segsz; | |
695 | tbuf.shm_atime = shp->shm_atim; | |
696 | tbuf.shm_dtime = shp->shm_dtim; | |
697 | tbuf.shm_ctime = shp->shm_ctim; | |
698 | tbuf.shm_cpid = shp->shm_cprid; | |
699 | tbuf.shm_lpid = shp->shm_lprid; | |
bc56bba8 | 700 | tbuf.shm_nattch = shp->shm_nattch; |
1da177e4 LT |
701 | shm_unlock(shp); |
702 | if(copy_shmid_to_user (buf, &tbuf, version)) | |
703 | err = -EFAULT; | |
704 | else | |
705 | err = result; | |
706 | goto out; | |
707 | } | |
708 | case SHM_LOCK: | |
709 | case SHM_UNLOCK: | |
710 | { | |
4e982311 | 711 | shp = shm_lock(ns, shmid); |
1da177e4 LT |
712 | if(shp==NULL) { |
713 | err = -EINVAL; | |
714 | goto out; | |
715 | } | |
4e982311 | 716 | err = shm_checkid(ns, shp,shmid); |
1da177e4 LT |
717 | if(err) |
718 | goto out_unlock; | |
719 | ||
073115d6 SG |
720 | err = audit_ipc_obj(&(shp->shm_perm)); |
721 | if (err) | |
722 | goto out_unlock; | |
723 | ||
1da177e4 LT |
724 | if (!capable(CAP_IPC_LOCK)) { |
725 | err = -EPERM; | |
726 | if (current->euid != shp->shm_perm.uid && | |
727 | current->euid != shp->shm_perm.cuid) | |
728 | goto out_unlock; | |
729 | if (cmd == SHM_LOCK && | |
730 | !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur) | |
731 | goto out_unlock; | |
732 | } | |
733 | ||
734 | err = security_shm_shmctl(shp, cmd); | |
735 | if (err) | |
736 | goto out_unlock; | |
737 | ||
738 | if(cmd==SHM_LOCK) { | |
739 | struct user_struct * user = current->user; | |
740 | if (!is_file_hugepages(shp->shm_file)) { | |
741 | err = shmem_lock(shp->shm_file, 1, user); | |
7be77e20 | 742 | if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){ |
b33291c0 | 743 | shp->shm_perm.mode |= SHM_LOCKED; |
1da177e4 LT |
744 | shp->mlock_user = user; |
745 | } | |
746 | } | |
747 | } else if (!is_file_hugepages(shp->shm_file)) { | |
748 | shmem_lock(shp->shm_file, 0, shp->mlock_user); | |
b33291c0 | 749 | shp->shm_perm.mode &= ~SHM_LOCKED; |
1da177e4 LT |
750 | shp->mlock_user = NULL; |
751 | } | |
752 | shm_unlock(shp); | |
753 | goto out; | |
754 | } | |
755 | case IPC_RMID: | |
756 | { | |
757 | /* | |
758 | * We cannot simply remove the file. The SVID states | |
759 | * that the block remains until the last person | |
760 | * detaches from it, then is deleted. A shmat() on | |
761 | * an RMID segment is legal in older Linux and if | |
762 | * we change it apps break... | |
763 | * | |
764 | * Instead we set a destroyed flag, and then blow | |
765 | * the name away when the usage hits zero. | |
766 | */ | |
4e982311 KK |
767 | mutex_lock(&shm_ids(ns).mutex); |
768 | shp = shm_lock(ns, shmid); | |
1da177e4 LT |
769 | err = -EINVAL; |
770 | if (shp == NULL) | |
771 | goto out_up; | |
4e982311 | 772 | err = shm_checkid(ns, shp, shmid); |
1da177e4 LT |
773 | if(err) |
774 | goto out_unlock_up; | |
775 | ||
073115d6 SG |
776 | err = audit_ipc_obj(&(shp->shm_perm)); |
777 | if (err) | |
778 | goto out_unlock_up; | |
779 | ||
1da177e4 LT |
780 | if (current->euid != shp->shm_perm.uid && |
781 | current->euid != shp->shm_perm.cuid && | |
782 | !capable(CAP_SYS_ADMIN)) { | |
783 | err=-EPERM; | |
784 | goto out_unlock_up; | |
785 | } | |
786 | ||
787 | err = security_shm_shmctl(shp, cmd); | |
788 | if (err) | |
789 | goto out_unlock_up; | |
790 | ||
4e982311 KK |
791 | do_shm_rmid(ns, shp); |
792 | mutex_unlock(&shm_ids(ns).mutex); | |
1da177e4 LT |
793 | goto out; |
794 | } | |
795 | ||
796 | case IPC_SET: | |
797 | { | |
798 | if (copy_shmid_from_user (&setbuf, buf, version)) { | |
799 | err = -EFAULT; | |
800 | goto out; | |
801 | } | |
4e982311 KK |
802 | mutex_lock(&shm_ids(ns).mutex); |
803 | shp = shm_lock(ns, shmid); | |
1da177e4 LT |
804 | err=-EINVAL; |
805 | if(shp==NULL) | |
806 | goto out_up; | |
4e982311 | 807 | err = shm_checkid(ns, shp,shmid); |
1da177e4 LT |
808 | if(err) |
809 | goto out_unlock_up; | |
073115d6 SG |
810 | err = audit_ipc_obj(&(shp->shm_perm)); |
811 | if (err) | |
812 | goto out_unlock_up; | |
ac03221a | 813 | err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode); |
073115d6 SG |
814 | if (err) |
815 | goto out_unlock_up; | |
1da177e4 LT |
816 | err=-EPERM; |
817 | if (current->euid != shp->shm_perm.uid && | |
818 | current->euid != shp->shm_perm.cuid && | |
819 | !capable(CAP_SYS_ADMIN)) { | |
820 | goto out_unlock_up; | |
821 | } | |
822 | ||
823 | err = security_shm_shmctl(shp, cmd); | |
824 | if (err) | |
825 | goto out_unlock_up; | |
826 | ||
827 | shp->shm_perm.uid = setbuf.uid; | |
828 | shp->shm_perm.gid = setbuf.gid; | |
b33291c0 | 829 | shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO) |
1da177e4 LT |
830 | | (setbuf.mode & S_IRWXUGO); |
831 | shp->shm_ctim = get_seconds(); | |
832 | break; | |
833 | } | |
834 | ||
835 | default: | |
836 | err = -EINVAL; | |
837 | goto out; | |
838 | } | |
839 | ||
840 | err = 0; | |
841 | out_unlock_up: | |
842 | shm_unlock(shp); | |
843 | out_up: | |
4e982311 | 844 | mutex_unlock(&shm_ids(ns).mutex); |
1da177e4 LT |
845 | goto out; |
846 | out_unlock: | |
847 | shm_unlock(shp); | |
848 | out: | |
849 | return err; | |
850 | } | |
851 | ||
852 | /* | |
853 | * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists. | |
854 | * | |
855 | * NOTE! Despite the name, this is NOT a direct system call entrypoint. The | |
856 | * "raddr" thing points to kernel space, and there has to be a wrapper around | |
857 | * this. | |
858 | */ | |
859 | long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr) | |
860 | { | |
861 | struct shmid_kernel *shp; | |
862 | unsigned long addr; | |
863 | unsigned long size; | |
864 | struct file * file; | |
865 | int err; | |
866 | unsigned long flags; | |
867 | unsigned long prot; | |
1da177e4 | 868 | int acc_mode; |
bc56bba8 | 869 | unsigned long user_addr; |
4e982311 | 870 | struct ipc_namespace *ns; |
bc56bba8 EB |
871 | struct shm_file_data *sfd; |
872 | struct path path; | |
873 | mode_t f_mode; | |
1da177e4 | 874 | |
bc56bba8 EB |
875 | err = -EINVAL; |
876 | if (shmid < 0) | |
1da177e4 | 877 | goto out; |
bc56bba8 | 878 | else if ((addr = (ulong)shmaddr)) { |
1da177e4 LT |
879 | if (addr & (SHMLBA-1)) { |
880 | if (shmflg & SHM_RND) | |
881 | addr &= ~(SHMLBA-1); /* round down */ | |
882 | else | |
883 | #ifndef __ARCH_FORCE_SHMLBA | |
884 | if (addr & ~PAGE_MASK) | |
885 | #endif | |
bc56bba8 | 886 | goto out; |
1da177e4 LT |
887 | } |
888 | flags = MAP_SHARED | MAP_FIXED; | |
889 | } else { | |
890 | if ((shmflg & SHM_REMAP)) | |
bc56bba8 | 891 | goto out; |
1da177e4 LT |
892 | |
893 | flags = MAP_SHARED; | |
894 | } | |
895 | ||
896 | if (shmflg & SHM_RDONLY) { | |
897 | prot = PROT_READ; | |
1da177e4 | 898 | acc_mode = S_IRUGO; |
bc56bba8 | 899 | f_mode = FMODE_READ; |
1da177e4 LT |
900 | } else { |
901 | prot = PROT_READ | PROT_WRITE; | |
1da177e4 | 902 | acc_mode = S_IRUGO | S_IWUGO; |
bc56bba8 | 903 | f_mode = FMODE_READ | FMODE_WRITE; |
1da177e4 LT |
904 | } |
905 | if (shmflg & SHM_EXEC) { | |
906 | prot |= PROT_EXEC; | |
907 | acc_mode |= S_IXUGO; | |
908 | } | |
909 | ||
910 | /* | |
911 | * We cannot rely on the fs check since SYSV IPC does have an | |
912 | * additional creator id... | |
913 | */ | |
4e982311 KK |
914 | ns = current->nsproxy->ipc_ns; |
915 | shp = shm_lock(ns, shmid); | |
bc56bba8 | 916 | if(shp == NULL) |
1da177e4 | 917 | goto out; |
bc56bba8 | 918 | |
4e982311 | 919 | err = shm_checkid(ns, shp,shmid); |
bc56bba8 EB |
920 | if (err) |
921 | goto out_unlock; | |
922 | ||
923 | err = -EACCES; | |
924 | if (ipcperms(&shp->shm_perm, acc_mode)) | |
925 | goto out_unlock; | |
1da177e4 LT |
926 | |
927 | err = security_shm_shmat(shp, shmaddr, shmflg); | |
bc56bba8 EB |
928 | if (err) |
929 | goto out_unlock; | |
930 | ||
931 | path.dentry = dget(shp->shm_file->f_path.dentry); | |
ce8d2cdf | 932 | path.mnt = shp->shm_file->f_path.mnt; |
1da177e4 | 933 | shp->shm_nattch++; |
bc56bba8 | 934 | size = i_size_read(path.dentry->d_inode); |
1da177e4 LT |
935 | shm_unlock(shp); |
936 | ||
bc56bba8 EB |
937 | err = -ENOMEM; |
938 | sfd = kzalloc(sizeof(*sfd), GFP_KERNEL); | |
939 | if (!sfd) | |
ce8d2cdf | 940 | goto out_put_dentry; |
bc56bba8 EB |
941 | |
942 | err = -ENOMEM; | |
ce8d2cdf DH |
943 | |
944 | file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations); | |
bc56bba8 EB |
945 | if (!file) |
946 | goto out_free; | |
947 | ||
bc56bba8 | 948 | file->private_data = sfd; |
bc56bba8 | 949 | file->f_mapping = shp->shm_file->f_mapping; |
7ca7e564 | 950 | sfd->id = shp->shm_perm.id; |
bc56bba8 EB |
951 | sfd->ns = get_ipc_ns(ns); |
952 | sfd->file = shp->shm_file; | |
953 | sfd->vm_ops = NULL; | |
954 | ||
1da177e4 LT |
955 | down_write(¤t->mm->mmap_sem); |
956 | if (addr && !(shmflg & SHM_REMAP)) { | |
bc56bba8 | 957 | err = -EINVAL; |
1da177e4 LT |
958 | if (find_vma_intersection(current->mm, addr, addr + size)) |
959 | goto invalid; | |
960 | /* | |
961 | * If shm segment goes below stack, make sure there is some | |
962 | * space left for the stack to grow (at least 4 pages). | |
963 | */ | |
964 | if (addr < current->mm->start_stack && | |
965 | addr > current->mm->start_stack - size - PAGE_SIZE * 5) | |
966 | goto invalid; | |
967 | } | |
968 | ||
bc56bba8 EB |
969 | user_addr = do_mmap (file, addr, size, prot, flags, 0); |
970 | *raddr = user_addr; | |
971 | err = 0; | |
972 | if (IS_ERR_VALUE(user_addr)) | |
973 | err = (long)user_addr; | |
1da177e4 LT |
974 | invalid: |
975 | up_write(¤t->mm->mmap_sem); | |
976 | ||
bc56bba8 EB |
977 | fput(file); |
978 | ||
979 | out_nattch: | |
4e982311 KK |
980 | mutex_lock(&shm_ids(ns).mutex); |
981 | shp = shm_lock(ns, shmid); | |
9ba025f1 | 982 | BUG_ON(!shp); |
1da177e4 LT |
983 | shp->shm_nattch--; |
984 | if(shp->shm_nattch == 0 && | |
b33291c0 | 985 | shp->shm_perm.mode & SHM_DEST) |
4e982311 | 986 | shm_destroy(ns, shp); |
1da177e4 LT |
987 | else |
988 | shm_unlock(shp); | |
4e982311 | 989 | mutex_unlock(&shm_ids(ns).mutex); |
1da177e4 | 990 | |
1da177e4 LT |
991 | out: |
992 | return err; | |
bc56bba8 EB |
993 | |
994 | out_unlock: | |
995 | shm_unlock(shp); | |
996 | goto out; | |
997 | ||
998 | out_free: | |
999 | kfree(sfd); | |
ce8d2cdf | 1000 | out_put_dentry: |
bc56bba8 | 1001 | dput(path.dentry); |
bc56bba8 | 1002 | goto out_nattch; |
1da177e4 LT |
1003 | } |
1004 | ||
7d87e14c SR |
1005 | asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg) |
1006 | { | |
1007 | unsigned long ret; | |
1008 | long err; | |
1009 | ||
1010 | err = do_shmat(shmid, shmaddr, shmflg, &ret); | |
1011 | if (err) | |
1012 | return err; | |
1013 | force_successful_syscall_return(); | |
1014 | return (long)ret; | |
1015 | } | |
1016 | ||
1da177e4 LT |
1017 | /* |
1018 | * detach and kill segment if marked destroyed. | |
1019 | * The work is done in shm_close. | |
1020 | */ | |
1021 | asmlinkage long sys_shmdt(char __user *shmaddr) | |
1022 | { | |
1023 | struct mm_struct *mm = current->mm; | |
1024 | struct vm_area_struct *vma, *next; | |
1025 | unsigned long addr = (unsigned long)shmaddr; | |
1026 | loff_t size = 0; | |
1027 | int retval = -EINVAL; | |
1028 | ||
df1e2fb5 HD |
1029 | if (addr & ~PAGE_MASK) |
1030 | return retval; | |
1031 | ||
1da177e4 LT |
1032 | down_write(&mm->mmap_sem); |
1033 | ||
1034 | /* | |
1035 | * This function tries to be smart and unmap shm segments that | |
1036 | * were modified by partial mlock or munmap calls: | |
1037 | * - It first determines the size of the shm segment that should be | |
1038 | * unmapped: It searches for a vma that is backed by shm and that | |
1039 | * started at address shmaddr. It records it's size and then unmaps | |
1040 | * it. | |
1041 | * - Then it unmaps all shm vmas that started at shmaddr and that | |
1042 | * are within the initially determined size. | |
1043 | * Errors from do_munmap are ignored: the function only fails if | |
1044 | * it's called with invalid parameters or if it's called to unmap | |
1045 | * a part of a vma. Both calls in this function are for full vmas, | |
1046 | * the parameters are directly copied from the vma itself and always | |
1047 | * valid - therefore do_munmap cannot fail. (famous last words?) | |
1048 | */ | |
1049 | /* | |
1050 | * If it had been mremap()'d, the starting address would not | |
1051 | * match the usual checks anyway. So assume all vma's are | |
1052 | * above the starting address given. | |
1053 | */ | |
1054 | vma = find_vma(mm, addr); | |
1055 | ||
1056 | while (vma) { | |
1057 | next = vma->vm_next; | |
1058 | ||
1059 | /* | |
1060 | * Check if the starting address would match, i.e. it's | |
1061 | * a fragment created by mprotect() and/or munmap(), or it | |
1062 | * otherwise it starts at this address with no hassles. | |
1063 | */ | |
bc56bba8 | 1064 | if ((vma->vm_ops == &shm_vm_ops) && |
1da177e4 LT |
1065 | (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) { |
1066 | ||
1067 | ||
6d63079a | 1068 | size = vma->vm_file->f_path.dentry->d_inode->i_size; |
1da177e4 LT |
1069 | do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start); |
1070 | /* | |
1071 | * We discovered the size of the shm segment, so | |
1072 | * break out of here and fall through to the next | |
1073 | * loop that uses the size information to stop | |
1074 | * searching for matching vma's. | |
1075 | */ | |
1076 | retval = 0; | |
1077 | vma = next; | |
1078 | break; | |
1079 | } | |
1080 | vma = next; | |
1081 | } | |
1082 | ||
1083 | /* | |
1084 | * We need look no further than the maximum address a fragment | |
1085 | * could possibly have landed at. Also cast things to loff_t to | |
1086 | * prevent overflows and make comparisions vs. equal-width types. | |
1087 | */ | |
8e36709d | 1088 | size = PAGE_ALIGN(size); |
1da177e4 LT |
1089 | while (vma && (loff_t)(vma->vm_end - addr) <= size) { |
1090 | next = vma->vm_next; | |
1091 | ||
1092 | /* finding a matching vma now does not alter retval */ | |
bc56bba8 | 1093 | if ((vma->vm_ops == &shm_vm_ops) && |
1da177e4 LT |
1094 | (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) |
1095 | ||
1096 | do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start); | |
1097 | vma = next; | |
1098 | } | |
1099 | ||
1100 | up_write(&mm->mmap_sem); | |
1101 | return retval; | |
1102 | } | |
1103 | ||
1104 | #ifdef CONFIG_PROC_FS | |
19b4946c | 1105 | static int sysvipc_shm_proc_show(struct seq_file *s, void *it) |
1da177e4 | 1106 | { |
19b4946c MW |
1107 | struct shmid_kernel *shp = it; |
1108 | char *format; | |
1da177e4 | 1109 | |
1da177e4 LT |
1110 | #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n" |
1111 | #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n" | |
1da177e4 | 1112 | |
19b4946c MW |
1113 | if (sizeof(size_t) <= sizeof(int)) |
1114 | format = SMALL_STRING; | |
1115 | else | |
1116 | format = BIG_STRING; | |
1117 | return seq_printf(s, format, | |
1118 | shp->shm_perm.key, | |
7ca7e564 | 1119 | shp->shm_perm.id, |
b33291c0 | 1120 | shp->shm_perm.mode, |
19b4946c MW |
1121 | shp->shm_segsz, |
1122 | shp->shm_cprid, | |
1123 | shp->shm_lprid, | |
bc56bba8 | 1124 | shp->shm_nattch, |
19b4946c MW |
1125 | shp->shm_perm.uid, |
1126 | shp->shm_perm.gid, | |
1127 | shp->shm_perm.cuid, | |
1128 | shp->shm_perm.cgid, | |
1129 | shp->shm_atim, | |
1130 | shp->shm_dtim, | |
1131 | shp->shm_ctim); | |
1da177e4 LT |
1132 | } |
1133 | #endif |