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_unlock(shp) \
63 ipc_unlock(&(shp)->shm_perm)
64 #define shm_buildid(ns, id, seq) \
65 ipc_buildid(&shm_ids(ns), id, seq)
67 static int newseg(struct ipc_namespace *, struct ipc_params *);
68 static void shm_open(struct vm_area_struct *vma);
69 static void shm_close(struct vm_area_struct *vma);
70 static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
72 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
75 static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
77 ns->ids[IPC_SHM_IDS] = ids;
78 ns->shm_ctlmax = SHMMAX;
79 ns->shm_ctlall = SHMALL;
80 ns->shm_ctlmni = SHMMNI;
85 static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp)
88 shp->shm_perm.mode |= SHM_DEST;
89 /* Do not find it any more */
90 shp->shm_perm.key = IPC_PRIVATE;
96 int shm_init_ns(struct ipc_namespace *ns)
100 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
104 __shm_init_ns(ns, ids);
108 void shm_exit_ns(struct ipc_namespace *ns)
110 struct shmid_kernel *shp;
114 mutex_lock(&shm_ids(ns).mutex);
116 in_use = shm_ids(ns).in_use;
118 for (total = 0, next_id = 0; total < in_use; next_id++) {
119 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
122 ipc_lock_by_ptr(&shp->shm_perm);
123 do_shm_rmid(ns, shp);
126 mutex_unlock(&shm_ids(ns).mutex);
128 kfree(ns->ids[IPC_SHM_IDS]);
129 ns->ids[IPC_SHM_IDS] = NULL;
132 void __init shm_init (void)
134 __shm_init_ns(&init_ipc_ns, &init_shm_ids);
135 ipc_init_proc_interface("sysvipc/shm",
136 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
137 IPC_SHM_IDS, sysvipc_shm_proc_show);
140 static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
142 struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
144 return container_of(ipcp, struct shmid_kernel, shm_perm);
147 static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns,
150 struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id);
152 return container_of(ipcp, struct shmid_kernel, shm_perm);
155 static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
157 ipc_rmid(&shm_ids(ns), &s->shm_perm);
160 static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
162 return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
167 /* This is called by fork, once for every shm attach. */
168 static void shm_open(struct vm_area_struct *vma)
170 struct file *file = vma->vm_file;
171 struct shm_file_data *sfd = shm_file_data(file);
172 struct shmid_kernel *shp;
174 shp = shm_lock(sfd->ns, sfd->id);
176 shp->shm_atim = get_seconds();
177 shp->shm_lprid = task_tgid_vnr(current);
183 * shm_destroy - free the struct shmid_kernel
185 * @shp: struct to free
187 * It has to be called with shp and shm_ids.mutex locked,
188 * but returns with shp unlocked and freed.
190 static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
192 ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
195 if (!is_file_hugepages(shp->shm_file))
196 shmem_lock(shp->shm_file, 0, shp->mlock_user);
198 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
200 fput (shp->shm_file);
201 security_shm_free(shp);
206 * remove the attach descriptor vma.
207 * free memory for segment if it is marked destroyed.
208 * The descriptor has already been removed from the current->mm->mmap list
209 * and will later be kfree()d.
211 static void shm_close(struct vm_area_struct *vma)
213 struct file * file = vma->vm_file;
214 struct shm_file_data *sfd = shm_file_data(file);
215 struct shmid_kernel *shp;
216 struct ipc_namespace *ns = sfd->ns;
218 mutex_lock(&shm_ids(ns).mutex);
219 /* remove from the list of attaches of the shm segment */
220 shp = shm_lock(ns, sfd->id);
222 shp->shm_lprid = task_tgid_vnr(current);
223 shp->shm_dtim = get_seconds();
225 if(shp->shm_nattch == 0 &&
226 shp->shm_perm.mode & SHM_DEST)
227 shm_destroy(ns, shp);
230 mutex_unlock(&shm_ids(ns).mutex);
233 static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
235 struct file *file = vma->vm_file;
236 struct shm_file_data *sfd = shm_file_data(file);
238 return sfd->vm_ops->fault(vma, vmf);
242 static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
244 struct file *file = vma->vm_file;
245 struct shm_file_data *sfd = shm_file_data(file);
247 if (sfd->vm_ops->set_policy)
248 err = sfd->vm_ops->set_policy(vma, new);
252 static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
255 struct file *file = vma->vm_file;
256 struct shm_file_data *sfd = shm_file_data(file);
257 struct mempolicy *pol = NULL;
259 if (sfd->vm_ops->get_policy)
260 pol = sfd->vm_ops->get_policy(vma, addr);
261 else if (vma->vm_policy)
262 pol = vma->vm_policy;
264 pol = current->mempolicy;
269 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
271 struct shm_file_data *sfd = shm_file_data(file);
274 ret = sfd->file->f_op->mmap(sfd->file, vma);
277 sfd->vm_ops = vma->vm_ops;
279 BUG_ON(!sfd->vm_ops->fault);
281 vma->vm_ops = &shm_vm_ops;
287 static int shm_release(struct inode *ino, struct file *file)
289 struct shm_file_data *sfd = shm_file_data(file);
292 shm_file_data(file) = NULL;
297 static int shm_fsync(struct file *file, struct dentry *dentry, int datasync)
299 int (*fsync) (struct file *, struct dentry *, int datasync);
300 struct shm_file_data *sfd = shm_file_data(file);
303 fsync = sfd->file->f_op->fsync;
305 ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync);
309 static unsigned long shm_get_unmapped_area(struct file *file,
310 unsigned long addr, unsigned long len, unsigned long pgoff,
313 struct shm_file_data *sfd = shm_file_data(file);
314 return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
317 int is_file_shm_hugepages(struct file *file)
321 if (file->f_op == &shm_file_operations) {
322 struct shm_file_data *sfd;
323 sfd = shm_file_data(file);
324 ret = is_file_hugepages(sfd->file);
329 static const struct file_operations shm_file_operations = {
332 .release = shm_release,
333 .get_unmapped_area = shm_get_unmapped_area,
336 static struct vm_operations_struct shm_vm_ops = {
337 .open = shm_open, /* callback for a new vm-area open */
338 .close = shm_close, /* callback for when the vm-area is released */
340 #if defined(CONFIG_NUMA)
341 .set_policy = shm_set_policy,
342 .get_policy = shm_get_policy,
346 static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
348 key_t key = params->key;
349 int shmflg = params->flg;
350 size_t size = params->u.size;
352 struct shmid_kernel *shp;
353 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
358 if (size < SHMMIN || size > ns->shm_ctlmax)
361 if (ns->shm_tot + numpages > ns->shm_ctlall)
364 shp = ipc_rcu_alloc(sizeof(*shp));
368 shp->shm_perm.key = key;
369 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
370 shp->mlock_user = NULL;
372 shp->shm_perm.security = NULL;
373 error = security_shm_alloc(shp);
379 sprintf (name, "SYSV%08x", key);
380 if (shmflg & SHM_HUGETLB) {
381 /* hugetlb_file_setup takes care of mlock user accounting */
382 file = hugetlb_file_setup(name, size);
383 shp->mlock_user = current->user;
385 int acctflag = VM_ACCOUNT;
387 * Do not allow no accounting for OVERCOMMIT_NEVER, even
390 if ((shmflg & SHM_NORESERVE) &&
391 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
393 file = shmem_file_setup(name, size, acctflag);
395 error = PTR_ERR(file);
400 id = shm_addid(ns, shp);
404 shp->shm_cprid = task_tgid_vnr(current);
406 shp->shm_atim = shp->shm_dtim = 0;
407 shp->shm_ctim = get_seconds();
408 shp->shm_segsz = size;
410 shp->shm_perm.id = shm_buildid(ns, id, shp->shm_perm.seq);
411 shp->shm_file = file;
413 * shmid gets reported as "inode#" in /proc/pid/maps.
414 * proc-ps tools use this. Changing this will break them.
416 file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
418 ns->shm_tot += numpages;
419 error = shp->shm_perm.id;
426 security_shm_free(shp);
431 static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
433 struct shmid_kernel *shp;
435 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
436 return security_shm_associate(shp, shmflg);
439 static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
440 struct ipc_params *params)
442 struct shmid_kernel *shp;
444 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
445 if (shp->shm_segsz < params->u.size)
451 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
453 struct ipc_namespace *ns;
454 struct ipc_ops shm_ops;
455 struct ipc_params shm_params;
457 ns = current->nsproxy->ipc_ns;
459 shm_ops.getnew = newseg;
460 shm_ops.associate = shm_security;
461 shm_ops.more_checks = shm_more_checks;
463 shm_params.key = key;
464 shm_params.flg = shmflg;
465 shm_params.u.size = size;
467 return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
470 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
474 return copy_to_user(buf, in, sizeof(*in));
479 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
480 out.shm_segsz = in->shm_segsz;
481 out.shm_atime = in->shm_atime;
482 out.shm_dtime = in->shm_dtime;
483 out.shm_ctime = in->shm_ctime;
484 out.shm_cpid = in->shm_cpid;
485 out.shm_lpid = in->shm_lpid;
486 out.shm_nattch = in->shm_nattch;
488 return copy_to_user(buf, &out, sizeof(out));
501 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
506 struct shmid64_ds tbuf;
508 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
511 out->uid = tbuf.shm_perm.uid;
512 out->gid = tbuf.shm_perm.gid;
513 out->mode = tbuf.shm_perm.mode;
519 struct shmid_ds tbuf_old;
521 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
524 out->uid = tbuf_old.shm_perm.uid;
525 out->gid = tbuf_old.shm_perm.gid;
526 out->mode = tbuf_old.shm_perm.mode;
535 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
539 return copy_to_user(buf, in, sizeof(*in));
544 if(in->shmmax > INT_MAX)
545 out.shmmax = INT_MAX;
547 out.shmmax = (int)in->shmmax;
549 out.shmmin = in->shmmin;
550 out.shmmni = in->shmmni;
551 out.shmseg = in->shmseg;
552 out.shmall = in->shmall;
554 return copy_to_user(buf, &out, sizeof(out));
561 static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
570 in_use = shm_ids(ns).in_use;
572 for (total = 0, next_id = 0; total < in_use; next_id++) {
573 struct shmid_kernel *shp;
577 * idr_find() is called via shm_get(), so with shm_ids.mutex
578 * locked. Since ipc_addid() is also called with
579 * shm_ids.mutex down, there is no need to add read barriers
580 * here to gurantee the writes in ipc_addid() are seen in
581 * order here (for Alpha).
582 * However idr_find() itself does not necessary require
583 * ipc_ids.mutex down. So if idr_find() is used by other
584 * places without ipc_ids.mutex down, then it needs read
585 * read memory barriers as ipc_lock() does.
588 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
592 inode = shp->shm_file->f_path.dentry->d_inode;
594 if (is_file_hugepages(shp->shm_file)) {
595 struct address_space *mapping = inode->i_mapping;
596 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
598 struct shmem_inode_info *info = SHMEM_I(inode);
599 spin_lock(&info->lock);
600 *rss += inode->i_mapping->nrpages;
601 *swp += info->swapped;
602 spin_unlock(&info->lock);
609 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
611 struct shm_setbuf setbuf;
612 struct shmid_kernel *shp;
614 struct ipc_namespace *ns;
616 if (cmd < 0 || shmid < 0) {
621 version = ipc_parse_version(&cmd);
622 ns = current->nsproxy->ipc_ns;
624 switch (cmd) { /* replace with proc interface ? */
627 struct shminfo64 shminfo;
629 err = security_shm_shmctl(NULL, cmd);
633 memset(&shminfo,0,sizeof(shminfo));
634 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
635 shminfo.shmmax = ns->shm_ctlmax;
636 shminfo.shmall = ns->shm_ctlall;
638 shminfo.shmmin = SHMMIN;
639 if(copy_shminfo_to_user (buf, &shminfo, version))
641 /* reading a integer is always atomic */
642 err = ipc_get_maxid(&shm_ids(ns));
649 struct shm_info shm_info;
651 err = security_shm_shmctl(NULL, cmd);
655 memset(&shm_info,0,sizeof(shm_info));
656 mutex_lock(&shm_ids(ns).mutex);
657 shm_info.used_ids = shm_ids(ns).in_use;
658 shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
659 shm_info.shm_tot = ns->shm_tot;
660 shm_info.swap_attempts = 0;
661 shm_info.swap_successes = 0;
662 err = ipc_get_maxid(&shm_ids(ns));
663 mutex_unlock(&shm_ids(ns).mutex);
664 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
669 err = err < 0 ? 0 : err;
675 struct shmid64_ds tbuf;
683 if (cmd == SHM_STAT) {
684 shp = shm_lock(ns, shmid);
689 result = shp->shm_perm.id;
691 shp = shm_lock_check(ns, shmid);
699 if (ipcperms (&shp->shm_perm, S_IRUGO))
701 err = security_shm_shmctl(shp, cmd);
704 memset(&tbuf, 0, sizeof(tbuf));
705 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
706 tbuf.shm_segsz = shp->shm_segsz;
707 tbuf.shm_atime = shp->shm_atim;
708 tbuf.shm_dtime = shp->shm_dtim;
709 tbuf.shm_ctime = shp->shm_ctim;
710 tbuf.shm_cpid = shp->shm_cprid;
711 tbuf.shm_lpid = shp->shm_lprid;
712 tbuf.shm_nattch = shp->shm_nattch;
714 if(copy_shmid_to_user (buf, &tbuf, version))
723 shp = shm_lock_check(ns, shmid);
729 err = audit_ipc_obj(&(shp->shm_perm));
733 if (!capable(CAP_IPC_LOCK)) {
735 if (current->euid != shp->shm_perm.uid &&
736 current->euid != shp->shm_perm.cuid)
738 if (cmd == SHM_LOCK &&
739 !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
743 err = security_shm_shmctl(shp, cmd);
748 struct user_struct * user = current->user;
749 if (!is_file_hugepages(shp->shm_file)) {
750 err = shmem_lock(shp->shm_file, 1, user);
751 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
752 shp->shm_perm.mode |= SHM_LOCKED;
753 shp->mlock_user = user;
756 } else if (!is_file_hugepages(shp->shm_file)) {
757 shmem_lock(shp->shm_file, 0, shp->mlock_user);
758 shp->shm_perm.mode &= ~SHM_LOCKED;
759 shp->mlock_user = NULL;
767 * We cannot simply remove the file. The SVID states
768 * that the block remains until the last person
769 * detaches from it, then is deleted. A shmat() on
770 * an RMID segment is legal in older Linux and if
771 * we change it apps break...
773 * Instead we set a destroyed flag, and then blow
774 * the name away when the usage hits zero.
776 mutex_lock(&shm_ids(ns).mutex);
777 shp = shm_lock_check(ns, shmid);
783 err = audit_ipc_obj(&(shp->shm_perm));
787 if (current->euid != shp->shm_perm.uid &&
788 current->euid != shp->shm_perm.cuid &&
789 !capable(CAP_SYS_ADMIN)) {
794 err = security_shm_shmctl(shp, cmd);
798 do_shm_rmid(ns, shp);
799 mutex_unlock(&shm_ids(ns).mutex);
810 if (copy_shmid_from_user (&setbuf, buf, version)) {
814 mutex_lock(&shm_ids(ns).mutex);
815 shp = shm_lock_check(ns, shmid);
820 err = audit_ipc_obj(&(shp->shm_perm));
823 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
827 if (current->euid != shp->shm_perm.uid &&
828 current->euid != shp->shm_perm.cuid &&
829 !capable(CAP_SYS_ADMIN)) {
833 err = security_shm_shmctl(shp, cmd);
837 shp->shm_perm.uid = setbuf.uid;
838 shp->shm_perm.gid = setbuf.gid;
839 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
840 | (setbuf.mode & S_IRWXUGO);
841 shp->shm_ctim = get_seconds();
854 mutex_unlock(&shm_ids(ns).mutex);
863 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
865 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
866 * "raddr" thing points to kernel space, and there has to be a wrapper around
869 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
871 struct shmid_kernel *shp;
879 unsigned long user_addr;
880 struct ipc_namespace *ns;
881 struct shm_file_data *sfd;
888 else if ((addr = (ulong)shmaddr)) {
889 if (addr & (SHMLBA-1)) {
890 if (shmflg & SHM_RND)
891 addr &= ~(SHMLBA-1); /* round down */
893 #ifndef __ARCH_FORCE_SHMLBA
894 if (addr & ~PAGE_MASK)
898 flags = MAP_SHARED | MAP_FIXED;
900 if ((shmflg & SHM_REMAP))
906 if (shmflg & SHM_RDONLY) {
911 prot = PROT_READ | PROT_WRITE;
912 acc_mode = S_IRUGO | S_IWUGO;
913 f_mode = FMODE_READ | FMODE_WRITE;
915 if (shmflg & SHM_EXEC) {
921 * We cannot rely on the fs check since SYSV IPC does have an
922 * additional creator id...
924 ns = current->nsproxy->ipc_ns;
925 shp = shm_lock_check(ns, shmid);
932 if (ipcperms(&shp->shm_perm, acc_mode))
935 err = security_shm_shmat(shp, shmaddr, shmflg);
939 path.dentry = dget(shp->shm_file->f_path.dentry);
940 path.mnt = shp->shm_file->f_path.mnt;
942 size = i_size_read(path.dentry->d_inode);
946 sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
952 file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
956 file->private_data = sfd;
957 file->f_mapping = shp->shm_file->f_mapping;
958 sfd->id = shp->shm_perm.id;
959 sfd->ns = get_ipc_ns(ns);
960 sfd->file = shp->shm_file;
963 down_write(¤t->mm->mmap_sem);
964 if (addr && !(shmflg & SHM_REMAP)) {
966 if (find_vma_intersection(current->mm, addr, addr + size))
969 * If shm segment goes below stack, make sure there is some
970 * space left for the stack to grow (at least 4 pages).
972 if (addr < current->mm->start_stack &&
973 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
977 user_addr = do_mmap (file, addr, size, prot, flags, 0);
980 if (IS_ERR_VALUE(user_addr))
981 err = (long)user_addr;
983 up_write(¤t->mm->mmap_sem);
988 mutex_lock(&shm_ids(ns).mutex);
989 shp = shm_lock(ns, shmid);
992 if(shp->shm_nattch == 0 &&
993 shp->shm_perm.mode & SHM_DEST)
994 shm_destroy(ns, shp);
997 mutex_unlock(&shm_ids(ns).mutex);
1013 asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
1018 err = do_shmat(shmid, shmaddr, shmflg, &ret);
1021 force_successful_syscall_return();
1026 * detach and kill segment if marked destroyed.
1027 * The work is done in shm_close.
1029 asmlinkage long sys_shmdt(char __user *shmaddr)
1031 struct mm_struct *mm = current->mm;
1032 struct vm_area_struct *vma, *next;
1033 unsigned long addr = (unsigned long)shmaddr;
1035 int retval = -EINVAL;
1037 if (addr & ~PAGE_MASK)
1040 down_write(&mm->mmap_sem);
1043 * This function tries to be smart and unmap shm segments that
1044 * were modified by partial mlock or munmap calls:
1045 * - It first determines the size of the shm segment that should be
1046 * unmapped: It searches for a vma that is backed by shm and that
1047 * started at address shmaddr. It records it's size and then unmaps
1049 * - Then it unmaps all shm vmas that started at shmaddr and that
1050 * are within the initially determined size.
1051 * Errors from do_munmap are ignored: the function only fails if
1052 * it's called with invalid parameters or if it's called to unmap
1053 * a part of a vma. Both calls in this function are for full vmas,
1054 * the parameters are directly copied from the vma itself and always
1055 * valid - therefore do_munmap cannot fail. (famous last words?)
1058 * If it had been mremap()'d, the starting address would not
1059 * match the usual checks anyway. So assume all vma's are
1060 * above the starting address given.
1062 vma = find_vma(mm, addr);
1065 next = vma->vm_next;
1068 * Check if the starting address would match, i.e. it's
1069 * a fragment created by mprotect() and/or munmap(), or it
1070 * otherwise it starts at this address with no hassles.
1072 if ((vma->vm_ops == &shm_vm_ops) &&
1073 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1076 size = vma->vm_file->f_path.dentry->d_inode->i_size;
1077 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1079 * We discovered the size of the shm segment, so
1080 * break out of here and fall through to the next
1081 * loop that uses the size information to stop
1082 * searching for matching vma's.
1092 * We need look no further than the maximum address a fragment
1093 * could possibly have landed at. Also cast things to loff_t to
1094 * prevent overflows and make comparisions vs. equal-width types.
1096 size = PAGE_ALIGN(size);
1097 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1098 next = vma->vm_next;
1100 /* finding a matching vma now does not alter retval */
1101 if ((vma->vm_ops == &shm_vm_ops) &&
1102 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
1104 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1108 up_write(&mm->mmap_sem);
1112 #ifdef CONFIG_PROC_FS
1113 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
1115 struct shmid_kernel *shp = it;
1118 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1119 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1121 if (sizeof(size_t) <= sizeof(int))
1122 format = SMALL_STRING;
1124 format = BIG_STRING;
1125 return seq_printf(s, format,