MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / ipc / shm.c
blob530fb1f6ae1d63043e4f3f904d69672245bb92aa
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.
8 * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
9 * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
10 * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
11 * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
12 * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
13 * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
14 * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
18 #include <linux/config.h>
19 #include <linux/slab.h>
20 #include <linux/mm.h>
21 #include <linux/hugetlb.h>
22 #include <linux/shm.h>
23 #include <linux/init.h>
24 #include <linux/file.h>
25 #include <linux/mman.h>
26 #include <linux/proc_fs.h>
27 #include <linux/shmem_fs.h>
28 #include <linux/security.h>
29 #include <asm/uaccess.h>
31 #include "util.h"
33 #define shm_flags shm_perm.mode
35 static struct file_operations shm_file_operations;
36 static struct vm_operations_struct shm_vm_ops;
38 static struct ipc_ids shm_ids;
40 #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
41 #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
42 #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
43 #define shm_buildid(id, seq) \
44 ipc_buildid(&shm_ids, id, seq)
46 static int newseg (key_t key, int shmflg, size_t size);
47 static void shm_open (struct vm_area_struct *shmd);
48 static void shm_close (struct vm_area_struct *shmd);
49 #ifdef CONFIG_PROC_FS
50 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
51 #endif
53 size_t shm_ctlmax = SHMMAX;
54 size_t shm_ctlall = SHMALL;
55 int shm_ctlmni = SHMMNI;
57 static int shm_tot; /* total number of shared memory pages */
59 void __init shm_init (void)
61 ipc_init_ids(&shm_ids, 1);
62 #ifdef CONFIG_PROC_FS
63 create_proc_read_entry("sysvipc/shm", 0, NULL, sysvipc_shm_read_proc, NULL);
64 #endif
67 static inline int shm_checkid(struct shmid_kernel *s, int id)
69 if (ipc_checkid(&shm_ids,&s->shm_perm,id))
70 return -EIDRM;
71 return 0;
74 static inline struct shmid_kernel *shm_rmid(int id)
76 return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
79 static inline int shm_addid(struct shmid_kernel *shp)
81 return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni);
86 static inline void shm_inc (int id) {
87 struct shmid_kernel *shp;
89 if(!(shp = shm_lock(id)))
90 BUG();
91 shp->shm_atim = get_seconds();
92 shp->shm_lprid = current->tgid;
93 shp->shm_nattch++;
94 shm_unlock(shp);
97 /* This is called by fork, once for every shm attach. */
98 static void shm_open (struct vm_area_struct *shmd)
100 shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
104 * shm_destroy - free the struct shmid_kernel
106 * @shp: struct to free
108 * It has to be called with shp and shm_ids.sem locked,
109 * but returns with shp unlocked and freed.
111 static void shm_destroy (struct shmid_kernel *shp)
113 shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
114 shm_rmid (shp->id);
115 shm_unlock(shp);
116 if (!is_file_hugepages(shp->shm_file))
117 shmem_lock(shp->shm_file, 0, shp->mlock_user);
118 else
119 user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size,
120 shp->mlock_user);
121 fput (shp->shm_file);
122 security_shm_free(shp);
123 ipc_rcu_putref(shp);
127 * remove the attach descriptor shmd.
128 * free memory for segment if it is marked destroyed.
129 * The descriptor has already been removed from the current->mm->mmap list
130 * and will later be kfree()d.
132 static void shm_close (struct vm_area_struct *shmd)
134 struct file * file = shmd->vm_file;
135 int id = file->f_dentry->d_inode->i_ino;
136 struct shmid_kernel *shp;
138 down (&shm_ids.sem);
139 /* remove from the list of attaches of the shm segment */
140 if(!(shp = shm_lock(id)))
141 BUG();
142 shp->shm_lprid = current->tgid;
143 shp->shm_dtim = get_seconds();
144 shp->shm_nattch--;
145 if(shp->shm_nattch == 0 &&
146 shp->shm_flags & SHM_DEST)
147 shm_destroy (shp);
148 else
149 shm_unlock(shp);
150 up (&shm_ids.sem);
153 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
155 file_accessed(file);
156 vma->vm_ops = &shm_vm_ops;
157 shm_inc(file->f_dentry->d_inode->i_ino);
158 return 0;
161 static struct file_operations shm_file_operations = {
162 .mmap = shm_mmap
165 static struct vm_operations_struct shm_vm_ops = {
166 .open = shm_open, /* callback for a new vm-area open */
167 .close = shm_close, /* callback for when the vm-area is released */
168 .nopage = shmem_nopage,
169 #ifdef CONFIG_NUMA
170 .set_policy = shmem_set_policy,
171 .get_policy = shmem_get_policy,
172 #endif
175 static int newseg (key_t key, int shmflg, size_t size)
177 int error;
178 struct shmid_kernel *shp;
179 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
180 struct file * file;
181 char name[13];
182 int id;
184 if (size < SHMMIN || size > shm_ctlmax)
185 return -EINVAL;
187 if (shm_tot + numpages >= shm_ctlall)
188 return -ENOSPC;
190 shp = ipc_rcu_alloc(sizeof(*shp));
191 if (!shp)
192 return -ENOMEM;
194 shp->shm_perm.key = key;
195 shp->shm_flags = (shmflg & S_IRWXUGO);
196 shp->mlock_user = NULL;
198 shp->shm_perm.security = NULL;
199 error = security_shm_alloc(shp);
200 if (error) {
201 ipc_rcu_putref(shp);
202 return error;
205 if (shmflg & SHM_HUGETLB) {
206 /* hugetlb_zero_setup takes care of mlock user accounting */
207 file = hugetlb_zero_setup(size);
208 shp->mlock_user = current->user;
209 } else {
210 sprintf (name, "SYSV%08x", key);
211 file = shmem_file_setup(name, size, VM_ACCOUNT);
213 error = PTR_ERR(file);
214 if (IS_ERR(file))
215 goto no_file;
217 error = -ENOSPC;
218 id = shm_addid(shp);
219 if(id == -1)
220 goto no_id;
222 shp->shm_cprid = current->tgid;
223 shp->shm_lprid = 0;
224 shp->shm_atim = shp->shm_dtim = 0;
225 shp->shm_ctim = get_seconds();
226 shp->shm_segsz = size;
227 shp->shm_nattch = 0;
228 shp->id = shm_buildid(id,shp->shm_perm.seq);
229 shp->shm_file = file;
230 file->f_dentry->d_inode->i_ino = shp->id;
231 if (shmflg & SHM_HUGETLB)
232 set_file_hugepages(file);
233 else
234 file->f_op = &shm_file_operations;
235 shm_tot += numpages;
236 shm_unlock(shp);
237 return shp->id;
239 no_id:
240 fput(file);
241 no_file:
242 security_shm_free(shp);
243 ipc_rcu_putref(shp);
244 return error;
247 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
249 struct shmid_kernel *shp;
250 int err, id = 0;
252 down(&shm_ids.sem);
253 if (key == IPC_PRIVATE) {
254 err = newseg(key, shmflg, size);
255 } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
256 if (!(shmflg & IPC_CREAT))
257 err = -ENOENT;
258 else
259 err = newseg(key, shmflg, size);
260 } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
261 err = -EEXIST;
262 } else {
263 shp = shm_lock(id);
264 if(shp==NULL)
265 BUG();
266 if (shp->shm_segsz < size)
267 err = -EINVAL;
268 else if (ipcperms(&shp->shm_perm, shmflg))
269 err = -EACCES;
270 else {
271 int shmid = shm_buildid(id, shp->shm_perm.seq);
272 err = security_shm_associate(shp, shmflg);
273 if (!err)
274 err = shmid;
276 shm_unlock(shp);
278 up(&shm_ids.sem);
280 return err;
283 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
285 switch(version) {
286 case IPC_64:
287 return copy_to_user(buf, in, sizeof(*in));
288 case IPC_OLD:
290 struct shmid_ds out;
292 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
293 out.shm_segsz = in->shm_segsz;
294 out.shm_atime = in->shm_atime;
295 out.shm_dtime = in->shm_dtime;
296 out.shm_ctime = in->shm_ctime;
297 out.shm_cpid = in->shm_cpid;
298 out.shm_lpid = in->shm_lpid;
299 out.shm_nattch = in->shm_nattch;
301 return copy_to_user(buf, &out, sizeof(out));
303 default:
304 return -EINVAL;
308 struct shm_setbuf {
309 uid_t uid;
310 gid_t gid;
311 mode_t mode;
314 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
316 switch(version) {
317 case IPC_64:
319 struct shmid64_ds tbuf;
321 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
322 return -EFAULT;
324 out->uid = tbuf.shm_perm.uid;
325 out->gid = tbuf.shm_perm.gid;
326 out->mode = tbuf.shm_flags;
328 return 0;
330 case IPC_OLD:
332 struct shmid_ds tbuf_old;
334 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
335 return -EFAULT;
337 out->uid = tbuf_old.shm_perm.uid;
338 out->gid = tbuf_old.shm_perm.gid;
339 out->mode = tbuf_old.shm_flags;
341 return 0;
343 default:
344 return -EINVAL;
348 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
350 switch(version) {
351 case IPC_64:
352 return copy_to_user(buf, in, sizeof(*in));
353 case IPC_OLD:
355 struct shminfo out;
357 if(in->shmmax > INT_MAX)
358 out.shmmax = INT_MAX;
359 else
360 out.shmmax = (int)in->shmmax;
362 out.shmmin = in->shmmin;
363 out.shmmni = in->shmmni;
364 out.shmseg = in->shmseg;
365 out.shmall = in->shmall;
367 return copy_to_user(buf, &out, sizeof(out));
369 default:
370 return -EINVAL;
374 static void shm_get_stat(unsigned long *rss, unsigned long *swp)
376 int i;
378 *rss = 0;
379 *swp = 0;
381 for (i = 0; i <= shm_ids.max_id; i++) {
382 struct shmid_kernel *shp;
383 struct inode *inode;
385 shp = shm_get(i);
386 if(!shp)
387 continue;
389 inode = shp->shm_file->f_dentry->d_inode;
391 if (is_file_hugepages(shp->shm_file)) {
392 struct address_space *mapping = inode->i_mapping;
393 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
394 } else {
395 struct shmem_inode_info *info = SHMEM_I(inode);
396 spin_lock(&info->lock);
397 *rss += inode->i_mapping->nrpages;
398 *swp += info->swapped;
399 spin_unlock(&info->lock);
404 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
406 struct shm_setbuf setbuf;
407 struct shmid_kernel *shp;
408 int err, version;
410 if (cmd < 0 || shmid < 0) {
411 err = -EINVAL;
412 goto out;
415 version = ipc_parse_version(&cmd);
417 switch (cmd) { /* replace with proc interface ? */
418 case IPC_INFO:
420 struct shminfo64 shminfo;
422 err = security_shm_shmctl(NULL, cmd);
423 if (err)
424 return err;
426 memset(&shminfo,0,sizeof(shminfo));
427 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
428 shminfo.shmmax = shm_ctlmax;
429 shminfo.shmall = shm_ctlall;
431 shminfo.shmmin = SHMMIN;
432 if(copy_shminfo_to_user (buf, &shminfo, version))
433 return -EFAULT;
434 /* reading a integer is always atomic */
435 err= shm_ids.max_id;
436 if(err<0)
437 err = 0;
438 goto out;
440 case SHM_INFO:
442 struct shm_info shm_info;
444 err = security_shm_shmctl(NULL, cmd);
445 if (err)
446 return err;
448 memset(&shm_info,0,sizeof(shm_info));
449 down(&shm_ids.sem);
450 shm_info.used_ids = shm_ids.in_use;
451 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
452 shm_info.shm_tot = shm_tot;
453 shm_info.swap_attempts = 0;
454 shm_info.swap_successes = 0;
455 err = shm_ids.max_id;
456 up(&shm_ids.sem);
457 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
458 err = -EFAULT;
459 goto out;
462 err = err < 0 ? 0 : err;
463 goto out;
465 case SHM_STAT:
466 case IPC_STAT:
468 struct shmid64_ds tbuf;
469 int result;
470 memset(&tbuf, 0, sizeof(tbuf));
471 shp = shm_lock(shmid);
472 if(shp==NULL) {
473 err = -EINVAL;
474 goto out;
475 } else if(cmd==SHM_STAT) {
476 err = -EINVAL;
477 if (shmid > shm_ids.max_id)
478 goto out_unlock;
479 result = shm_buildid(shmid, shp->shm_perm.seq);
480 } else {
481 err = shm_checkid(shp,shmid);
482 if(err)
483 goto out_unlock;
484 result = 0;
486 err=-EACCES;
487 if (ipcperms (&shp->shm_perm, S_IRUGO))
488 goto out_unlock;
489 err = security_shm_shmctl(shp, cmd);
490 if (err)
491 goto out_unlock;
492 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
493 tbuf.shm_segsz = shp->shm_segsz;
494 tbuf.shm_atime = shp->shm_atim;
495 tbuf.shm_dtime = shp->shm_dtim;
496 tbuf.shm_ctime = shp->shm_ctim;
497 tbuf.shm_cpid = shp->shm_cprid;
498 tbuf.shm_lpid = shp->shm_lprid;
499 if (!is_file_hugepages(shp->shm_file))
500 tbuf.shm_nattch = shp->shm_nattch;
501 else
502 tbuf.shm_nattch = file_count(shp->shm_file) - 1;
503 shm_unlock(shp);
504 if(copy_shmid_to_user (buf, &tbuf, version))
505 err = -EFAULT;
506 else
507 err = result;
508 goto out;
510 case SHM_LOCK:
511 case SHM_UNLOCK:
513 /* Allow superuser to lock segment in memory */
514 if (!can_do_mlock() && cmd == SHM_LOCK) {
515 err = -EPERM;
516 goto out;
518 shp = shm_lock(shmid);
519 if(shp==NULL) {
520 err = -EINVAL;
521 goto out;
523 err = shm_checkid(shp,shmid);
524 if(err)
525 goto out_unlock;
527 err = security_shm_shmctl(shp, cmd);
528 if (err)
529 goto out_unlock;
531 if(cmd==SHM_LOCK) {
532 struct user_struct * user = current->user;
533 if (!is_file_hugepages(shp->shm_file)) {
534 err = shmem_lock(shp->shm_file, 1, user);
535 if (!err) {
536 shp->shm_flags |= SHM_LOCKED;
537 shp->mlock_user = user;
540 } else if (!is_file_hugepages(shp->shm_file)) {
541 shmem_lock(shp->shm_file, 0, shp->mlock_user);
542 shp->shm_flags &= ~SHM_LOCKED;
543 shp->mlock_user = NULL;
545 shm_unlock(shp);
546 goto out;
548 case IPC_RMID:
551 * We cannot simply remove the file. The SVID states
552 * that the block remains until the last person
553 * detaches from it, then is deleted. A shmat() on
554 * an RMID segment is legal in older Linux and if
555 * we change it apps break...
557 * Instead we set a destroyed flag, and then blow
558 * the name away when the usage hits zero.
560 down(&shm_ids.sem);
561 shp = shm_lock(shmid);
562 err = -EINVAL;
563 if (shp == NULL)
564 goto out_up;
565 err = shm_checkid(shp, shmid);
566 if(err)
567 goto out_unlock_up;
569 if (current->euid != shp->shm_perm.uid &&
570 current->euid != shp->shm_perm.cuid &&
571 !capable(CAP_SYS_ADMIN)) {
572 err=-EPERM;
573 goto out_unlock_up;
576 err = security_shm_shmctl(shp, cmd);
577 if (err)
578 goto out_unlock_up;
580 if (shp->shm_nattch){
581 shp->shm_flags |= SHM_DEST;
582 /* Do not find it any more */
583 shp->shm_perm.key = IPC_PRIVATE;
584 shm_unlock(shp);
585 } else
586 shm_destroy (shp);
587 up(&shm_ids.sem);
588 goto out;
591 case IPC_SET:
593 if (copy_shmid_from_user (&setbuf, buf, version)) {
594 err = -EFAULT;
595 goto out;
597 down(&shm_ids.sem);
598 shp = shm_lock(shmid);
599 err=-EINVAL;
600 if(shp==NULL)
601 goto out_up;
602 err = shm_checkid(shp,shmid);
603 if(err)
604 goto out_unlock_up;
605 err=-EPERM;
606 if (current->euid != shp->shm_perm.uid &&
607 current->euid != shp->shm_perm.cuid &&
608 !capable(CAP_SYS_ADMIN)) {
609 goto out_unlock_up;
612 err = security_shm_shmctl(shp, cmd);
613 if (err)
614 goto out_unlock_up;
616 shp->shm_perm.uid = setbuf.uid;
617 shp->shm_perm.gid = setbuf.gid;
618 shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
619 | (setbuf.mode & S_IRWXUGO);
620 shp->shm_ctim = get_seconds();
621 break;
624 default:
625 err = -EINVAL;
626 goto out;
629 err = 0;
630 out_unlock_up:
631 shm_unlock(shp);
632 out_up:
633 up(&shm_ids.sem);
634 goto out;
635 out_unlock:
636 shm_unlock(shp);
637 out:
638 return err;
642 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
644 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
645 * "raddr" thing points to kernel space, and there has to be a wrapper around
646 * this.
648 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
650 struct shmid_kernel *shp;
651 unsigned long addr;
652 unsigned long size;
653 struct file * file;
654 int err;
655 unsigned long flags;
656 unsigned long prot;
657 unsigned long o_flags;
658 int acc_mode;
659 void *user_addr;
661 if (shmid < 0) {
662 err = -EINVAL;
663 goto out;
664 } else if ((addr = (ulong)shmaddr)) {
665 if (addr & (SHMLBA-1)) {
666 if (shmflg & SHM_RND)
667 addr &= ~(SHMLBA-1); /* round down */
668 else
669 #ifndef __ARCH_FORCE_SHMLBA
670 if (addr & ~PAGE_MASK)
671 #endif
672 return -EINVAL;
674 flags = MAP_SHARED | MAP_FIXED;
675 } else {
676 if ((shmflg & SHM_REMAP))
677 return -EINVAL;
679 flags = MAP_SHARED;
682 if (shmflg & SHM_RDONLY) {
683 prot = PROT_READ;
684 o_flags = O_RDONLY;
685 acc_mode = S_IRUGO;
686 } else {
687 prot = PROT_READ | PROT_WRITE;
688 o_flags = O_RDWR;
689 acc_mode = S_IRUGO | S_IWUGO;
691 if (shmflg & SHM_EXEC) {
692 prot |= PROT_EXEC;
693 acc_mode |= S_IXUGO;
697 * We cannot rely on the fs check since SYSV IPC does have an
698 * additional creator id...
700 shp = shm_lock(shmid);
701 if(shp == NULL) {
702 err = -EINVAL;
703 goto out;
705 err = shm_checkid(shp,shmid);
706 if (err) {
707 shm_unlock(shp);
708 goto out;
710 if (ipcperms(&shp->shm_perm, acc_mode)) {
711 shm_unlock(shp);
712 err = -EACCES;
713 goto out;
716 err = security_shm_shmat(shp, shmaddr, shmflg);
717 if (err) {
718 shm_unlock(shp);
719 return err;
722 file = shp->shm_file;
723 size = i_size_read(file->f_dentry->d_inode);
724 shp->shm_nattch++;
725 shm_unlock(shp);
727 down_write(&current->mm->mmap_sem);
728 if (addr && !(shmflg & SHM_REMAP)) {
729 user_addr = ERR_PTR(-EINVAL);
730 if (find_vma_intersection(current->mm, addr, addr + size))
731 goto invalid;
733 * If shm segment goes below stack, make sure there is some
734 * space left for the stack to grow (at least 4 pages).
736 if (addr < current->mm->start_stack &&
737 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
738 goto invalid;
741 user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
743 invalid:
744 up_write(&current->mm->mmap_sem);
746 down (&shm_ids.sem);
747 if(!(shp = shm_lock(shmid)))
748 BUG();
749 shp->shm_nattch--;
750 if(shp->shm_nattch == 0 &&
751 shp->shm_flags & SHM_DEST)
752 shm_destroy (shp);
753 else
754 shm_unlock(shp);
755 up (&shm_ids.sem);
757 *raddr = (unsigned long) user_addr;
758 err = 0;
759 if (IS_ERR(user_addr))
760 err = PTR_ERR(user_addr);
761 out:
762 return err;
766 * detach and kill segment if marked destroyed.
767 * The work is done in shm_close.
769 asmlinkage long sys_shmdt(char __user *shmaddr)
771 struct mm_struct *mm = current->mm;
772 struct vm_area_struct *vma, *next;
773 unsigned long addr = (unsigned long)shmaddr;
774 loff_t size = 0;
775 int retval = -EINVAL;
777 down_write(&mm->mmap_sem);
780 * This function tries to be smart and unmap shm segments that
781 * were modified by partial mlock or munmap calls:
782 * - It first determines the size of the shm segment that should be
783 * unmapped: It searches for a vma that is backed by shm and that
784 * started at address shmaddr. It records it's size and then unmaps
785 * it.
786 * - Then it unmaps all shm vmas that started at shmaddr and that
787 * are within the initially determined size.
788 * Errors from do_munmap are ignored: the function only fails if
789 * it's called with invalid parameters or if it's called to unmap
790 * a part of a vma. Both calls in this function are for full vmas,
791 * the parameters are directly copied from the vma itself and always
792 * valid - therefore do_munmap cannot fail. (famous last words?)
795 * If it had been mremap()'d, the starting address would not
796 * match the usual checks anyway. So assume all vma's are
797 * above the starting address given.
799 vma = find_vma(mm, addr);
801 while (vma) {
802 next = vma->vm_next;
805 * Check if the starting address would match, i.e. it's
806 * a fragment created by mprotect() and/or munmap(), or it
807 * otherwise it starts at this address with no hassles.
809 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
810 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
813 size = vma->vm_file->f_dentry->d_inode->i_size;
814 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
816 * We discovered the size of the shm segment, so
817 * break out of here and fall through to the next
818 * loop that uses the size information to stop
819 * searching for matching vma's.
821 retval = 0;
822 vma = next;
823 break;
825 vma = next;
829 * We need look no further than the maximum address a fragment
830 * could possibly have landed at. Also cast things to loff_t to
831 * prevent overflows and make comparisions vs. equal-width types.
833 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
834 next = vma->vm_next;
836 /* finding a matching vma now does not alter retval */
837 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
838 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
840 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
841 vma = next;
844 up_write(&mm->mmap_sem);
845 return retval;
848 #ifdef CONFIG_PROC_FS
849 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
851 off_t pos = 0;
852 off_t begin = 0;
853 int i, len = 0;
855 down(&shm_ids.sem);
856 len += sprintf(buffer, " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n");
858 for(i = 0; i <= shm_ids.max_id; i++) {
859 struct shmid_kernel* shp;
861 shp = shm_lock(i);
862 if(shp!=NULL) {
863 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
864 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
865 char *format;
867 if (sizeof(size_t) <= sizeof(int))
868 format = SMALL_STRING;
869 else
870 format = BIG_STRING;
871 len += sprintf(buffer + len, format,
872 shp->shm_perm.key,
873 shm_buildid(i, shp->shm_perm.seq),
874 shp->shm_flags,
875 shp->shm_segsz,
876 shp->shm_cprid,
877 shp->shm_lprid,
878 is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
879 shp->shm_perm.uid,
880 shp->shm_perm.gid,
881 shp->shm_perm.cuid,
882 shp->shm_perm.cgid,
883 shp->shm_atim,
884 shp->shm_dtim,
885 shp->shm_ctim);
886 shm_unlock(shp);
888 pos += len;
889 if(pos < offset) {
890 len = 0;
891 begin = pos;
893 if(pos > offset + length)
894 goto done;
897 *eof = 1;
898 done:
899 up(&shm_ids.sem);
900 *start = buffer + (offset - begin);
901 len -= (offset - begin);
902 if(len > length)
903 len = length;
904 if(len < 0)
905 len = 0;
906 return len;
908 #endif