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>
16 * support for audit of ipc object properties and permission changes
17 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
21 * Pavel Emelianov <xemul@openvz.org>
24 #include <linux/slab.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>
31 #include <linux/shmem_fs.h>
32 #include <linux/security.h>
33 #include <linux/syscalls.h>
34 #include <linux/audit.h>
35 #include <linux/capability.h>
36 #include <linux/ptrace.h>
37 #include <linux/seq_file.h>
38 #include <linux/mutex.h>
39 #include <linux/nsproxy.h>
40 #include <linux/mount.h>
42 #include <asm/uaccess.h>
46 struct shm_file_data {
48 struct ipc_namespace *ns;
50 const struct vm_operations_struct *vm_ops;
53 #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
55 static const struct file_operations shm_file_operations;
56 static struct vm_operations_struct shm_vm_ops;
58 static struct ipc_ids init_shm_ids;
60 #define shm_ids(ns) (*((ns)->ids[IPC_SHM_IDS]))
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)
71 static int newseg (struct ipc_namespace *ns, key_t key,
72 int shmflg, size_t size);
73 static void shm_open(struct vm_area_struct *vma);
74 static void shm_close(struct vm_area_struct *vma);
75 static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
77 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
80 static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
82 ns->ids[IPC_SHM_IDS] = ids;
83 ns->shm_ctlmax = SHMMAX;
84 ns->shm_ctlall = SHMALL;
85 ns->shm_ctlmni = SHMMNI;
90 static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp)
93 shp->shm_perm.mode |= SHM_DEST;
94 /* Do not find it any more */
95 shp->shm_perm.key = IPC_PRIVATE;
101 int shm_init_ns(struct ipc_namespace *ns)
105 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
109 __shm_init_ns(ns, ids);
113 void shm_exit_ns(struct ipc_namespace *ns)
115 struct shmid_kernel *shp;
119 mutex_lock(&shm_ids(ns).mutex);
121 in_use = shm_ids(ns).in_use;
123 for (total = 0, next_id = 0; total < in_use; next_id++) {
124 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
127 ipc_lock_by_ptr(&shp->shm_perm);
128 do_shm_rmid(ns, shp);
131 mutex_unlock(&shm_ids(ns).mutex);
133 kfree(ns->ids[IPC_SHM_IDS]);
134 ns->ids[IPC_SHM_IDS] = NULL;
137 void __init shm_init (void)
139 __shm_init_ns(&init_ipc_ns, &init_shm_ids);
140 ipc_init_proc_interface("sysvipc/shm",
141 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
142 IPC_SHM_IDS, sysvipc_shm_proc_show);
145 static inline int shm_checkid(struct ipc_namespace *ns,
146 struct shmid_kernel *s, int id)
148 if (ipc_checkid(&shm_ids(ns), &s->shm_perm, id))
153 static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
155 ipc_rmid(&shm_ids(ns), &s->shm_perm);
158 static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
160 return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
165 /* This is called by fork, once for every shm attach. */
166 static void shm_open(struct vm_area_struct *vma)
168 struct file *file = vma->vm_file;
169 struct shm_file_data *sfd = shm_file_data(file);
170 struct shmid_kernel *shp;
172 shp = shm_lock(sfd->ns, sfd->id);
174 shp->shm_atim = get_seconds();
175 shp->shm_lprid = task_tgid_vnr(current);
181 * shm_destroy - free the struct shmid_kernel
183 * @shp: struct to free
185 * It has to be called with shp and shm_ids.mutex locked,
186 * but returns with shp unlocked and freed.
188 static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
190 ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
193 if (!is_file_hugepages(shp->shm_file))
194 shmem_lock(shp->shm_file, 0, shp->mlock_user);
196 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
198 fput (shp->shm_file);
199 security_shm_free(shp);
204 * remove the attach descriptor vma.
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.
209 static void shm_close(struct vm_area_struct *vma)
211 struct file * file = vma->vm_file;
212 struct shm_file_data *sfd = shm_file_data(file);
213 struct shmid_kernel *shp;
214 struct ipc_namespace *ns = sfd->ns;
216 mutex_lock(&shm_ids(ns).mutex);
217 /* remove from the list of attaches of the shm segment */
218 shp = shm_lock(ns, sfd->id);
220 shp->shm_lprid = task_tgid_vnr(current);
221 shp->shm_dtim = get_seconds();
223 if(shp->shm_nattch == 0 &&
224 shp->shm_perm.mode & SHM_DEST)
225 shm_destroy(ns, shp);
228 mutex_unlock(&shm_ids(ns).mutex);
231 static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
233 struct file *file = vma->vm_file;
234 struct shm_file_data *sfd = shm_file_data(file);
236 return sfd->vm_ops->fault(vma, vmf);
240 static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
242 struct file *file = vma->vm_file;
243 struct shm_file_data *sfd = shm_file_data(file);
245 if (sfd->vm_ops->set_policy)
246 err = sfd->vm_ops->set_policy(vma, new);
250 static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
253 struct file *file = vma->vm_file;
254 struct shm_file_data *sfd = shm_file_data(file);
255 struct mempolicy *pol = NULL;
257 if (sfd->vm_ops->get_policy)
258 pol = sfd->vm_ops->get_policy(vma, addr);
259 else if (vma->vm_policy)
260 pol = vma->vm_policy;
262 pol = current->mempolicy;
267 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
269 struct shm_file_data *sfd = shm_file_data(file);
272 ret = sfd->file->f_op->mmap(sfd->file, vma);
275 sfd->vm_ops = vma->vm_ops;
277 BUG_ON(!sfd->vm_ops->fault);
279 vma->vm_ops = &shm_vm_ops;
285 static int shm_release(struct inode *ino, struct file *file)
287 struct shm_file_data *sfd = shm_file_data(file);
290 shm_file_data(file) = NULL;
295 static int shm_fsync(struct file *file, struct dentry *dentry, int datasync)
297 int (*fsync) (struct file *, struct dentry *, int datasync);
298 struct shm_file_data *sfd = shm_file_data(file);
301 fsync = sfd->file->f_op->fsync;
303 ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync);
307 static unsigned long shm_get_unmapped_area(struct file *file,
308 unsigned long addr, unsigned long len, unsigned long pgoff,
311 struct shm_file_data *sfd = shm_file_data(file);
312 return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
315 int is_file_shm_hugepages(struct file *file)
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);
327 static const struct file_operations shm_file_operations = {
330 .release = shm_release,
331 .get_unmapped_area = shm_get_unmapped_area,
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 */
338 #if defined(CONFIG_NUMA)
339 .set_policy = shm_set_policy,
340 .get_policy = shm_get_policy,
344 static int newseg (struct ipc_namespace *ns, key_t key, int shmflg, size_t size)
347 struct shmid_kernel *shp;
348 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
353 if (size < SHMMIN || size > ns->shm_ctlmax)
356 if (ns->shm_tot + numpages > ns->shm_ctlall)
359 shp = ipc_rcu_alloc(sizeof(*shp));
363 shp->shm_perm.key = key;
364 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
365 shp->mlock_user = NULL;
367 shp->shm_perm.security = NULL;
368 error = security_shm_alloc(shp);
374 sprintf (name, "SYSV%08x", key);
375 if (shmflg & SHM_HUGETLB) {
376 /* hugetlb_file_setup takes care of mlock user accounting */
377 file = hugetlb_file_setup(name, size);
378 shp->mlock_user = current->user;
380 int acctflag = VM_ACCOUNT;
382 * Do not allow no accounting for OVERCOMMIT_NEVER, even
385 if ((shmflg & SHM_NORESERVE) &&
386 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
388 file = shmem_file_setup(name, size, acctflag);
390 error = PTR_ERR(file);
395 id = shm_addid(ns, shp);
399 shp->shm_cprid = task_tgid_vnr(current);
401 shp->shm_atim = shp->shm_dtim = 0;
402 shp->shm_ctim = get_seconds();
403 shp->shm_segsz = size;
405 shp->shm_perm.id = shm_buildid(ns, id, shp->shm_perm.seq);
406 shp->shm_file = file;
408 * shmid gets reported as "inode#" in /proc/pid/maps.
409 * proc-ps tools use this. Changing this will break them.
411 file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
413 ns->shm_tot += numpages;
414 error = shp->shm_perm.id;
421 security_shm_free(shp);
426 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
428 struct shmid_kernel *shp;
430 struct ipc_namespace *ns;
432 ns = current->nsproxy->ipc_ns;
434 err = idr_pre_get(&shm_ids(ns).ipcs_idr, GFP_KERNEL);
436 if (key == IPC_PRIVATE) {
440 mutex_lock(&shm_ids(ns).mutex);
441 err = newseg(ns, key, shmflg, size);
442 mutex_unlock(&shm_ids(ns).mutex);
445 mutex_lock(&shm_ids(ns).mutex);
446 shp = (struct shmid_kernel *) ipc_findkey(&shm_ids(ns), key);
448 if (!(shmflg & IPC_CREAT))
453 err = newseg(ns, key, shmflg, size);
455 /* shp has been locked by ipc_findkey() */
457 if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL))
460 if (shp->shm_segsz < size)
462 else if (ipcperms(&shp->shm_perm, shmflg))
465 err = security_shm_associate(shp,
468 err = shp->shm_perm.id;
473 mutex_unlock(&shm_ids(ns).mutex);
479 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
483 return copy_to_user(buf, in, sizeof(*in));
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;
497 return copy_to_user(buf, &out, sizeof(out));
510 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
515 struct shmid64_ds tbuf;
517 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
520 out->uid = tbuf.shm_perm.uid;
521 out->gid = tbuf.shm_perm.gid;
522 out->mode = tbuf.shm_perm.mode;
528 struct shmid_ds tbuf_old;
530 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
533 out->uid = tbuf_old.shm_perm.uid;
534 out->gid = tbuf_old.shm_perm.gid;
535 out->mode = tbuf_old.shm_perm.mode;
544 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
548 return copy_to_user(buf, in, sizeof(*in));
553 if(in->shmmax > INT_MAX)
554 out.shmmax = INT_MAX;
556 out.shmmax = (int)in->shmmax;
558 out.shmmin = in->shmmin;
559 out.shmmni = in->shmmni;
560 out.shmseg = in->shmseg;
561 out.shmall = in->shmall;
563 return copy_to_user(buf, &out, sizeof(out));
570 static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
579 in_use = shm_ids(ns).in_use;
581 for (total = 0, next_id = 0; total < in_use; next_id++) {
582 struct shmid_kernel *shp;
585 shp = shm_get(ns, next_id);
589 inode = shp->shm_file->f_path.dentry->d_inode;
591 if (is_file_hugepages(shp->shm_file)) {
592 struct address_space *mapping = inode->i_mapping;
593 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
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);
606 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
608 struct shm_setbuf setbuf;
609 struct shmid_kernel *shp;
611 struct ipc_namespace *ns;
613 if (cmd < 0 || shmid < 0) {
618 version = ipc_parse_version(&cmd);
619 ns = current->nsproxy->ipc_ns;
621 switch (cmd) { /* replace with proc interface ? */
624 struct shminfo64 shminfo;
626 err = security_shm_shmctl(NULL, cmd);
630 memset(&shminfo,0,sizeof(shminfo));
631 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
632 shminfo.shmmax = ns->shm_ctlmax;
633 shminfo.shmall = ns->shm_ctlall;
635 shminfo.shmmin = SHMMIN;
636 if(copy_shminfo_to_user (buf, &shminfo, version))
638 /* reading a integer is always atomic */
639 err = ipc_get_maxid(&shm_ids(ns));
646 struct shm_info shm_info;
648 err = security_shm_shmctl(NULL, cmd);
652 memset(&shm_info,0,sizeof(shm_info));
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;
657 shm_info.swap_attempts = 0;
658 shm_info.swap_successes = 0;
659 err = ipc_get_maxid(&shm_ids(ns));
660 mutex_unlock(&shm_ids(ns).mutex);
661 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
666 err = err < 0 ? 0 : err;
672 struct shmid64_ds tbuf;
674 memset(&tbuf, 0, sizeof(tbuf));
675 shp = shm_lock(ns, shmid);
679 } else if (cmd == SHM_STAT) {
680 result = shp->shm_perm.id;
682 err = shm_checkid(ns, shp,shmid);
688 if (ipcperms (&shp->shm_perm, S_IRUGO))
690 err = security_shm_shmctl(shp, cmd);
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;
700 tbuf.shm_nattch = shp->shm_nattch;
702 if(copy_shmid_to_user (buf, &tbuf, version))
711 shp = shm_lock(ns, shmid);
716 err = shm_checkid(ns, shp,shmid);
720 err = audit_ipc_obj(&(shp->shm_perm));
724 if (!capable(CAP_IPC_LOCK)) {
726 if (current->euid != shp->shm_perm.uid &&
727 current->euid != shp->shm_perm.cuid)
729 if (cmd == SHM_LOCK &&
730 !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
734 err = security_shm_shmctl(shp, cmd);
739 struct user_struct * user = current->user;
740 if (!is_file_hugepages(shp->shm_file)) {
741 err = shmem_lock(shp->shm_file, 1, user);
742 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
743 shp->shm_perm.mode |= SHM_LOCKED;
744 shp->mlock_user = user;
747 } else if (!is_file_hugepages(shp->shm_file)) {
748 shmem_lock(shp->shm_file, 0, shp->mlock_user);
749 shp->shm_perm.mode &= ~SHM_LOCKED;
750 shp->mlock_user = NULL;
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...
764 * Instead we set a destroyed flag, and then blow
765 * the name away when the usage hits zero.
767 mutex_lock(&shm_ids(ns).mutex);
768 shp = shm_lock(ns, shmid);
772 err = shm_checkid(ns, shp, shmid);
776 err = audit_ipc_obj(&(shp->shm_perm));
780 if (current->euid != shp->shm_perm.uid &&
781 current->euid != shp->shm_perm.cuid &&
782 !capable(CAP_SYS_ADMIN)) {
787 err = security_shm_shmctl(shp, cmd);
791 do_shm_rmid(ns, shp);
792 mutex_unlock(&shm_ids(ns).mutex);
798 if (copy_shmid_from_user (&setbuf, buf, version)) {
802 mutex_lock(&shm_ids(ns).mutex);
803 shp = shm_lock(ns, shmid);
807 err = shm_checkid(ns, shp,shmid);
810 err = audit_ipc_obj(&(shp->shm_perm));
813 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
817 if (current->euid != shp->shm_perm.uid &&
818 current->euid != shp->shm_perm.cuid &&
819 !capable(CAP_SYS_ADMIN)) {
823 err = security_shm_shmctl(shp, cmd);
827 shp->shm_perm.uid = setbuf.uid;
828 shp->shm_perm.gid = setbuf.gid;
829 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
830 | (setbuf.mode & S_IRWXUGO);
831 shp->shm_ctim = get_seconds();
844 mutex_unlock(&shm_ids(ns).mutex);
853 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
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
859 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
861 struct shmid_kernel *shp;
869 unsigned long user_addr;
870 struct ipc_namespace *ns;
871 struct shm_file_data *sfd;
878 else if ((addr = (ulong)shmaddr)) {
879 if (addr & (SHMLBA-1)) {
880 if (shmflg & SHM_RND)
881 addr &= ~(SHMLBA-1); /* round down */
883 #ifndef __ARCH_FORCE_SHMLBA
884 if (addr & ~PAGE_MASK)
888 flags = MAP_SHARED | MAP_FIXED;
890 if ((shmflg & SHM_REMAP))
896 if (shmflg & SHM_RDONLY) {
901 prot = PROT_READ | PROT_WRITE;
902 acc_mode = S_IRUGO | S_IWUGO;
903 f_mode = FMODE_READ | FMODE_WRITE;
905 if (shmflg & SHM_EXEC) {
911 * We cannot rely on the fs check since SYSV IPC does have an
912 * additional creator id...
914 ns = current->nsproxy->ipc_ns;
915 shp = shm_lock(ns, shmid);
919 err = shm_checkid(ns, shp,shmid);
924 if (ipcperms(&shp->shm_perm, acc_mode))
927 err = security_shm_shmat(shp, shmaddr, shmflg);
931 path.dentry = dget(shp->shm_file->f_path.dentry);
932 path.mnt = shp->shm_file->f_path.mnt;
934 size = i_size_read(path.dentry->d_inode);
938 sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
944 file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
948 file->private_data = sfd;
949 file->f_mapping = shp->shm_file->f_mapping;
950 sfd->id = shp->shm_perm.id;
951 sfd->ns = get_ipc_ns(ns);
952 sfd->file = shp->shm_file;
955 down_write(¤t->mm->mmap_sem);
956 if (addr && !(shmflg & SHM_REMAP)) {
958 if (find_vma_intersection(current->mm, addr, addr + size))
961 * If shm segment goes below stack, make sure there is some
962 * space left for the stack to grow (at least 4 pages).
964 if (addr < current->mm->start_stack &&
965 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
969 user_addr = do_mmap (file, addr, size, prot, flags, 0);
972 if (IS_ERR_VALUE(user_addr))
973 err = (long)user_addr;
975 up_write(¤t->mm->mmap_sem);
980 mutex_lock(&shm_ids(ns).mutex);
981 shp = shm_lock(ns, shmid);
984 if(shp->shm_nattch == 0 &&
985 shp->shm_perm.mode & SHM_DEST)
986 shm_destroy(ns, shp);
989 mutex_unlock(&shm_ids(ns).mutex);
1005 asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
1010 err = do_shmat(shmid, shmaddr, shmflg, &ret);
1013 force_successful_syscall_return();
1018 * detach and kill segment if marked destroyed.
1019 * The work is done in shm_close.
1021 asmlinkage long sys_shmdt(char __user *shmaddr)
1023 struct mm_struct *mm = current->mm;
1024 struct vm_area_struct *vma, *next;
1025 unsigned long addr = (unsigned long)shmaddr;
1027 int retval = -EINVAL;
1029 if (addr & ~PAGE_MASK)
1032 down_write(&mm->mmap_sem);
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
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?)
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.
1054 vma = find_vma(mm, addr);
1057 next = vma->vm_next;
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.
1064 if ((vma->vm_ops == &shm_vm_ops) &&
1065 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1068 size = vma->vm_file->f_path.dentry->d_inode->i_size;
1069 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
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.
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.
1088 size = PAGE_ALIGN(size);
1089 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1090 next = vma->vm_next;
1092 /* finding a matching vma now does not alter retval */
1093 if ((vma->vm_ops == &shm_vm_ops) &&
1094 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
1096 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1100 up_write(&mm->mmap_sem);
1104 #ifdef CONFIG_PROC_FS
1105 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
1107 struct shmid_kernel *shp = it;
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"
1113 if (sizeof(size_t) <= sizeof(int))
1114 format = SMALL_STRING;
1116 format = BIG_STRING;
1117 return seq_printf(s, format,