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_buildid(ns, id, seq) \
67 ipc_buildid(&shm_ids(ns), id, seq)
69 static int newseg(struct ipc_namespace *, struct ipc_params *);
70 static void shm_open(struct vm_area_struct *vma);
71 static void shm_close(struct vm_area_struct *vma);
72 static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
74 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
77 static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
79 ns->ids[IPC_SHM_IDS] = ids;
80 ns->shm_ctlmax = SHMMAX;
81 ns->shm_ctlall = SHMALL;
82 ns->shm_ctlmni = SHMMNI;
87 static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp)
90 shp->shm_perm.mode |= SHM_DEST;
91 /* Do not find it any more */
92 shp->shm_perm.key = IPC_PRIVATE;
98 int shm_init_ns(struct ipc_namespace *ns)
102 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
106 __shm_init_ns(ns, ids);
110 void shm_exit_ns(struct ipc_namespace *ns)
112 struct shmid_kernel *shp;
116 mutex_lock(&shm_ids(ns).mutex);
118 in_use = shm_ids(ns).in_use;
120 for (total = 0, next_id = 0; total < in_use; next_id++) {
121 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
124 ipc_lock_by_ptr(&shp->shm_perm);
125 do_shm_rmid(ns, shp);
128 mutex_unlock(&shm_ids(ns).mutex);
130 kfree(ns->ids[IPC_SHM_IDS]);
131 ns->ids[IPC_SHM_IDS] = NULL;
134 void __init shm_init (void)
136 __shm_init_ns(&init_ipc_ns, &init_shm_ids);
137 ipc_init_proc_interface("sysvipc/shm",
138 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
139 IPC_SHM_IDS, sysvipc_shm_proc_show);
142 static inline int shm_checkid(struct ipc_namespace *ns,
143 struct shmid_kernel *s, int id)
145 if (ipc_checkid(&shm_ids(ns), &s->shm_perm, id))
150 static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
152 ipc_rmid(&shm_ids(ns), &s->shm_perm);
155 static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
157 return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
162 /* This is called by fork, once for every shm attach. */
163 static void shm_open(struct vm_area_struct *vma)
165 struct file *file = vma->vm_file;
166 struct shm_file_data *sfd = shm_file_data(file);
167 struct shmid_kernel *shp;
169 shp = shm_lock(sfd->ns, sfd->id);
171 shp->shm_atim = get_seconds();
172 shp->shm_lprid = task_tgid_vnr(current);
178 * shm_destroy - free the struct shmid_kernel
180 * @shp: struct to free
182 * It has to be called with shp and shm_ids.mutex locked,
183 * but returns with shp unlocked and freed.
185 static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
187 ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
190 if (!is_file_hugepages(shp->shm_file))
191 shmem_lock(shp->shm_file, 0, shp->mlock_user);
193 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
195 fput (shp->shm_file);
196 security_shm_free(shp);
201 * remove the attach descriptor vma.
202 * free memory for segment if it is marked destroyed.
203 * The descriptor has already been removed from the current->mm->mmap list
204 * and will later be kfree()d.
206 static void shm_close(struct vm_area_struct *vma)
208 struct file * file = vma->vm_file;
209 struct shm_file_data *sfd = shm_file_data(file);
210 struct shmid_kernel *shp;
211 struct ipc_namespace *ns = sfd->ns;
213 mutex_lock(&shm_ids(ns).mutex);
214 /* remove from the list of attaches of the shm segment */
215 shp = shm_lock(ns, sfd->id);
217 shp->shm_lprid = task_tgid_vnr(current);
218 shp->shm_dtim = get_seconds();
220 if(shp->shm_nattch == 0 &&
221 shp->shm_perm.mode & SHM_DEST)
222 shm_destroy(ns, shp);
225 mutex_unlock(&shm_ids(ns).mutex);
228 static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
230 struct file *file = vma->vm_file;
231 struct shm_file_data *sfd = shm_file_data(file);
233 return sfd->vm_ops->fault(vma, vmf);
237 static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
239 struct file *file = vma->vm_file;
240 struct shm_file_data *sfd = shm_file_data(file);
242 if (sfd->vm_ops->set_policy)
243 err = sfd->vm_ops->set_policy(vma, new);
247 static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
250 struct file *file = vma->vm_file;
251 struct shm_file_data *sfd = shm_file_data(file);
252 struct mempolicy *pol = NULL;
254 if (sfd->vm_ops->get_policy)
255 pol = sfd->vm_ops->get_policy(vma, addr);
256 else if (vma->vm_policy)
257 pol = vma->vm_policy;
259 pol = current->mempolicy;
264 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
266 struct shm_file_data *sfd = shm_file_data(file);
269 ret = sfd->file->f_op->mmap(sfd->file, vma);
272 sfd->vm_ops = vma->vm_ops;
274 BUG_ON(!sfd->vm_ops->fault);
276 vma->vm_ops = &shm_vm_ops;
282 static int shm_release(struct inode *ino, struct file *file)
284 struct shm_file_data *sfd = shm_file_data(file);
287 shm_file_data(file) = NULL;
292 static int shm_fsync(struct file *file, struct dentry *dentry, int datasync)
294 int (*fsync) (struct file *, struct dentry *, int datasync);
295 struct shm_file_data *sfd = shm_file_data(file);
298 fsync = sfd->file->f_op->fsync;
300 ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync);
304 static unsigned long shm_get_unmapped_area(struct file *file,
305 unsigned long addr, unsigned long len, unsigned long pgoff,
308 struct shm_file_data *sfd = shm_file_data(file);
309 return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
312 int is_file_shm_hugepages(struct file *file)
316 if (file->f_op == &shm_file_operations) {
317 struct shm_file_data *sfd;
318 sfd = shm_file_data(file);
319 ret = is_file_hugepages(sfd->file);
324 static const struct file_operations shm_file_operations = {
327 .release = shm_release,
328 .get_unmapped_area = shm_get_unmapped_area,
331 static struct vm_operations_struct shm_vm_ops = {
332 .open = shm_open, /* callback for a new vm-area open */
333 .close = shm_close, /* callback for when the vm-area is released */
335 #if defined(CONFIG_NUMA)
336 .set_policy = shm_set_policy,
337 .get_policy = shm_get_policy,
341 static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
343 key_t key = params->key;
344 int shmflg = params->flg;
345 size_t size = params->u.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 static inline int shm_security(void *shp, int shmflg)
428 return security_shm_associate((struct shmid_kernel *) shp, shmflg);
431 static inline int shm_more_checks(void *shp, struct ipc_params *params)
433 if (((struct shmid_kernel *)shp)->shm_segsz < params->u.size)
439 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
441 struct ipc_namespace *ns;
442 struct ipc_ops shm_ops;
443 struct ipc_params shm_params;
445 ns = current->nsproxy->ipc_ns;
447 shm_ops.getnew = newseg;
448 shm_ops.associate = shm_security;
449 shm_ops.more_checks = shm_more_checks;
451 shm_params.key = key;
452 shm_params.flg = shmflg;
453 shm_params.u.size = size;
455 return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
458 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
462 return copy_to_user(buf, in, sizeof(*in));
467 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
468 out.shm_segsz = in->shm_segsz;
469 out.shm_atime = in->shm_atime;
470 out.shm_dtime = in->shm_dtime;
471 out.shm_ctime = in->shm_ctime;
472 out.shm_cpid = in->shm_cpid;
473 out.shm_lpid = in->shm_lpid;
474 out.shm_nattch = in->shm_nattch;
476 return copy_to_user(buf, &out, sizeof(out));
489 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
494 struct shmid64_ds tbuf;
496 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
499 out->uid = tbuf.shm_perm.uid;
500 out->gid = tbuf.shm_perm.gid;
501 out->mode = tbuf.shm_perm.mode;
507 struct shmid_ds tbuf_old;
509 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
512 out->uid = tbuf_old.shm_perm.uid;
513 out->gid = tbuf_old.shm_perm.gid;
514 out->mode = tbuf_old.shm_perm.mode;
523 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
527 return copy_to_user(buf, in, sizeof(*in));
532 if(in->shmmax > INT_MAX)
533 out.shmmax = INT_MAX;
535 out.shmmax = (int)in->shmmax;
537 out.shmmin = in->shmmin;
538 out.shmmni = in->shmmni;
539 out.shmseg = in->shmseg;
540 out.shmall = in->shmall;
542 return copy_to_user(buf, &out, sizeof(out));
549 static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
558 in_use = shm_ids(ns).in_use;
560 for (total = 0, next_id = 0; total < in_use; next_id++) {
561 struct shmid_kernel *shp;
565 * idr_find() is called via shm_get(), so with shm_ids.mutex
566 * locked. Since ipc_addid() is also called with
567 * shm_ids.mutex down, there is no need to add read barriers
568 * here to gurantee the writes in ipc_addid() are seen in
569 * order here (for Alpha).
570 * However idr_find() itself does not necessary require
571 * ipc_ids.mutex down. So if idr_find() is used by other
572 * places without ipc_ids.mutex down, then it needs read
573 * read memory barriers as ipc_lock() does.
576 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
580 inode = shp->shm_file->f_path.dentry->d_inode;
582 if (is_file_hugepages(shp->shm_file)) {
583 struct address_space *mapping = inode->i_mapping;
584 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
586 struct shmem_inode_info *info = SHMEM_I(inode);
587 spin_lock(&info->lock);
588 *rss += inode->i_mapping->nrpages;
589 *swp += info->swapped;
590 spin_unlock(&info->lock);
597 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
599 struct shm_setbuf setbuf;
600 struct shmid_kernel *shp;
602 struct ipc_namespace *ns;
604 if (cmd < 0 || shmid < 0) {
609 version = ipc_parse_version(&cmd);
610 ns = current->nsproxy->ipc_ns;
612 switch (cmd) { /* replace with proc interface ? */
615 struct shminfo64 shminfo;
617 err = security_shm_shmctl(NULL, cmd);
621 memset(&shminfo,0,sizeof(shminfo));
622 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
623 shminfo.shmmax = ns->shm_ctlmax;
624 shminfo.shmall = ns->shm_ctlall;
626 shminfo.shmmin = SHMMIN;
627 if(copy_shminfo_to_user (buf, &shminfo, version))
629 /* reading a integer is always atomic */
630 err = ipc_get_maxid(&shm_ids(ns));
637 struct shm_info shm_info;
639 err = security_shm_shmctl(NULL, cmd);
643 memset(&shm_info,0,sizeof(shm_info));
644 mutex_lock(&shm_ids(ns).mutex);
645 shm_info.used_ids = shm_ids(ns).in_use;
646 shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
647 shm_info.shm_tot = ns->shm_tot;
648 shm_info.swap_attempts = 0;
649 shm_info.swap_successes = 0;
650 err = ipc_get_maxid(&shm_ids(ns));
651 mutex_unlock(&shm_ids(ns).mutex);
652 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
657 err = err < 0 ? 0 : err;
663 struct shmid64_ds tbuf;
665 memset(&tbuf, 0, sizeof(tbuf));
666 shp = shm_lock(ns, shmid);
670 } else if (cmd == SHM_STAT) {
671 result = shp->shm_perm.id;
673 err = shm_checkid(ns, shp,shmid);
679 if (ipcperms (&shp->shm_perm, S_IRUGO))
681 err = security_shm_shmctl(shp, cmd);
684 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
685 tbuf.shm_segsz = shp->shm_segsz;
686 tbuf.shm_atime = shp->shm_atim;
687 tbuf.shm_dtime = shp->shm_dtim;
688 tbuf.shm_ctime = shp->shm_ctim;
689 tbuf.shm_cpid = shp->shm_cprid;
690 tbuf.shm_lpid = shp->shm_lprid;
691 tbuf.shm_nattch = shp->shm_nattch;
693 if(copy_shmid_to_user (buf, &tbuf, version))
702 shp = shm_lock(ns, shmid);
707 err = shm_checkid(ns, shp,shmid);
711 err = audit_ipc_obj(&(shp->shm_perm));
715 if (!capable(CAP_IPC_LOCK)) {
717 if (current->euid != shp->shm_perm.uid &&
718 current->euid != shp->shm_perm.cuid)
720 if (cmd == SHM_LOCK &&
721 !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
725 err = security_shm_shmctl(shp, cmd);
730 struct user_struct * user = current->user;
731 if (!is_file_hugepages(shp->shm_file)) {
732 err = shmem_lock(shp->shm_file, 1, user);
733 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
734 shp->shm_perm.mode |= SHM_LOCKED;
735 shp->mlock_user = user;
738 } else if (!is_file_hugepages(shp->shm_file)) {
739 shmem_lock(shp->shm_file, 0, shp->mlock_user);
740 shp->shm_perm.mode &= ~SHM_LOCKED;
741 shp->mlock_user = NULL;
749 * We cannot simply remove the file. The SVID states
750 * that the block remains until the last person
751 * detaches from it, then is deleted. A shmat() on
752 * an RMID segment is legal in older Linux and if
753 * we change it apps break...
755 * Instead we set a destroyed flag, and then blow
756 * the name away when the usage hits zero.
758 mutex_lock(&shm_ids(ns).mutex);
759 shp = shm_lock(ns, shmid);
763 err = shm_checkid(ns, shp, shmid);
767 err = audit_ipc_obj(&(shp->shm_perm));
771 if (current->euid != shp->shm_perm.uid &&
772 current->euid != shp->shm_perm.cuid &&
773 !capable(CAP_SYS_ADMIN)) {
778 err = security_shm_shmctl(shp, cmd);
782 do_shm_rmid(ns, shp);
783 mutex_unlock(&shm_ids(ns).mutex);
789 if (copy_shmid_from_user (&setbuf, buf, version)) {
793 mutex_lock(&shm_ids(ns).mutex);
794 shp = shm_lock(ns, shmid);
798 err = shm_checkid(ns, shp,shmid);
801 err = audit_ipc_obj(&(shp->shm_perm));
804 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
808 if (current->euid != shp->shm_perm.uid &&
809 current->euid != shp->shm_perm.cuid &&
810 !capable(CAP_SYS_ADMIN)) {
814 err = security_shm_shmctl(shp, cmd);
818 shp->shm_perm.uid = setbuf.uid;
819 shp->shm_perm.gid = setbuf.gid;
820 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
821 | (setbuf.mode & S_IRWXUGO);
822 shp->shm_ctim = get_seconds();
835 mutex_unlock(&shm_ids(ns).mutex);
844 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
846 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
847 * "raddr" thing points to kernel space, and there has to be a wrapper around
850 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
852 struct shmid_kernel *shp;
860 unsigned long user_addr;
861 struct ipc_namespace *ns;
862 struct shm_file_data *sfd;
869 else if ((addr = (ulong)shmaddr)) {
870 if (addr & (SHMLBA-1)) {
871 if (shmflg & SHM_RND)
872 addr &= ~(SHMLBA-1); /* round down */
874 #ifndef __ARCH_FORCE_SHMLBA
875 if (addr & ~PAGE_MASK)
879 flags = MAP_SHARED | MAP_FIXED;
881 if ((shmflg & SHM_REMAP))
887 if (shmflg & SHM_RDONLY) {
892 prot = PROT_READ | PROT_WRITE;
893 acc_mode = S_IRUGO | S_IWUGO;
894 f_mode = FMODE_READ | FMODE_WRITE;
896 if (shmflg & SHM_EXEC) {
902 * We cannot rely on the fs check since SYSV IPC does have an
903 * additional creator id...
905 ns = current->nsproxy->ipc_ns;
906 shp = shm_lock(ns, shmid);
910 err = shm_checkid(ns, shp,shmid);
915 if (ipcperms(&shp->shm_perm, acc_mode))
918 err = security_shm_shmat(shp, shmaddr, shmflg);
922 path.dentry = dget(shp->shm_file->f_path.dentry);
923 path.mnt = shp->shm_file->f_path.mnt;
925 size = i_size_read(path.dentry->d_inode);
929 sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
935 file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
939 file->private_data = sfd;
940 file->f_mapping = shp->shm_file->f_mapping;
941 sfd->id = shp->shm_perm.id;
942 sfd->ns = get_ipc_ns(ns);
943 sfd->file = shp->shm_file;
946 down_write(¤t->mm->mmap_sem);
947 if (addr && !(shmflg & SHM_REMAP)) {
949 if (find_vma_intersection(current->mm, addr, addr + size))
952 * If shm segment goes below stack, make sure there is some
953 * space left for the stack to grow (at least 4 pages).
955 if (addr < current->mm->start_stack &&
956 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
960 user_addr = do_mmap (file, addr, size, prot, flags, 0);
963 if (IS_ERR_VALUE(user_addr))
964 err = (long)user_addr;
966 up_write(¤t->mm->mmap_sem);
971 mutex_lock(&shm_ids(ns).mutex);
972 shp = shm_lock(ns, shmid);
975 if(shp->shm_nattch == 0 &&
976 shp->shm_perm.mode & SHM_DEST)
977 shm_destroy(ns, shp);
980 mutex_unlock(&shm_ids(ns).mutex);
996 asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
1001 err = do_shmat(shmid, shmaddr, shmflg, &ret);
1004 force_successful_syscall_return();
1009 * detach and kill segment if marked destroyed.
1010 * The work is done in shm_close.
1012 asmlinkage long sys_shmdt(char __user *shmaddr)
1014 struct mm_struct *mm = current->mm;
1015 struct vm_area_struct *vma, *next;
1016 unsigned long addr = (unsigned long)shmaddr;
1018 int retval = -EINVAL;
1020 if (addr & ~PAGE_MASK)
1023 down_write(&mm->mmap_sem);
1026 * This function tries to be smart and unmap shm segments that
1027 * were modified by partial mlock or munmap calls:
1028 * - It first determines the size of the shm segment that should be
1029 * unmapped: It searches for a vma that is backed by shm and that
1030 * started at address shmaddr. It records it's size and then unmaps
1032 * - Then it unmaps all shm vmas that started at shmaddr and that
1033 * are within the initially determined size.
1034 * Errors from do_munmap are ignored: the function only fails if
1035 * it's called with invalid parameters or if it's called to unmap
1036 * a part of a vma. Both calls in this function are for full vmas,
1037 * the parameters are directly copied from the vma itself and always
1038 * valid - therefore do_munmap cannot fail. (famous last words?)
1041 * If it had been mremap()'d, the starting address would not
1042 * match the usual checks anyway. So assume all vma's are
1043 * above the starting address given.
1045 vma = find_vma(mm, addr);
1048 next = vma->vm_next;
1051 * Check if the starting address would match, i.e. it's
1052 * a fragment created by mprotect() and/or munmap(), or it
1053 * otherwise it starts at this address with no hassles.
1055 if ((vma->vm_ops == &shm_vm_ops) &&
1056 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1059 size = vma->vm_file->f_path.dentry->d_inode->i_size;
1060 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1062 * We discovered the size of the shm segment, so
1063 * break out of here and fall through to the next
1064 * loop that uses the size information to stop
1065 * searching for matching vma's.
1075 * We need look no further than the maximum address a fragment
1076 * could possibly have landed at. Also cast things to loff_t to
1077 * prevent overflows and make comparisions vs. equal-width types.
1079 size = PAGE_ALIGN(size);
1080 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1081 next = vma->vm_next;
1083 /* finding a matching vma now does not alter retval */
1084 if ((vma->vm_ops == &shm_vm_ops) &&
1085 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
1087 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1091 up_write(&mm->mmap_sem);
1095 #ifdef CONFIG_PROC_FS
1096 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
1098 struct shmid_kernel *shp = it;
1101 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1102 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1104 if (sizeof(size_t) <= sizeof(int))
1105 format = SMALL_STRING;
1107 format = BIG_STRING;
1108 return seq_printf(s, format,