[PATCH] libata-dev: irq-pio minor fix 2
[pv_ops_mirror.git] / ipc / shm.c
blob6b0c9af5bbf7c4a2569cf982ea4eba3821e9634b
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/shmem_fs.h>
27 #include <linux/security.h>
28 #include <linux/syscalls.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/ptrace.h>
32 #include <linux/seq_file.h>
33 #include <linux/mutex.h>
35 #include <asm/uaccess.h>
37 #include "util.h"
39 static struct file_operations shm_file_operations;
40 static struct vm_operations_struct shm_vm_ops;
42 static struct ipc_ids shm_ids;
44 #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
45 #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
46 #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
47 #define shm_buildid(id, seq) \
48 ipc_buildid(&shm_ids, id, seq)
50 static int newseg (key_t key, int shmflg, size_t size);
51 static void shm_open (struct vm_area_struct *shmd);
52 static void shm_close (struct vm_area_struct *shmd);
53 #ifdef CONFIG_PROC_FS
54 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
55 #endif
57 size_t shm_ctlmax = SHMMAX;
58 size_t shm_ctlall = SHMALL;
59 int shm_ctlmni = SHMMNI;
61 static int shm_tot; /* total number of shared memory pages */
63 void __init shm_init (void)
65 ipc_init_ids(&shm_ids, 1);
66 ipc_init_proc_interface("sysvipc/shm",
67 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
68 &shm_ids,
69 sysvipc_shm_proc_show);
72 static inline int shm_checkid(struct shmid_kernel *s, int id)
74 if (ipc_checkid(&shm_ids,&s->shm_perm,id))
75 return -EIDRM;
76 return 0;
79 static inline struct shmid_kernel *shm_rmid(int id)
81 return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
84 static inline int shm_addid(struct shmid_kernel *shp)
86 return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni);
91 static inline void shm_inc (int id) {
92 struct shmid_kernel *shp;
94 shp = shm_lock(id);
95 BUG_ON(!shp);
96 shp->shm_atim = get_seconds();
97 shp->shm_lprid = current->tgid;
98 shp->shm_nattch++;
99 shm_unlock(shp);
102 /* This is called by fork, once for every shm attach. */
103 static void shm_open (struct vm_area_struct *shmd)
105 shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
109 * shm_destroy - free the struct shmid_kernel
111 * @shp: struct to free
113 * It has to be called with shp and shm_ids.mutex locked,
114 * but returns with shp unlocked and freed.
116 static void shm_destroy (struct shmid_kernel *shp)
118 shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
119 shm_rmid (shp->id);
120 shm_unlock(shp);
121 if (!is_file_hugepages(shp->shm_file))
122 shmem_lock(shp->shm_file, 0, shp->mlock_user);
123 else
124 user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size,
125 shp->mlock_user);
126 fput (shp->shm_file);
127 security_shm_free(shp);
128 ipc_rcu_putref(shp);
132 * remove the attach descriptor shmd.
133 * free memory for segment if it is marked destroyed.
134 * The descriptor has already been removed from the current->mm->mmap list
135 * and will later be kfree()d.
137 static void shm_close (struct vm_area_struct *shmd)
139 struct file * file = shmd->vm_file;
140 int id = file->f_dentry->d_inode->i_ino;
141 struct shmid_kernel *shp;
143 mutex_lock(&shm_ids.mutex);
144 /* remove from the list of attaches of the shm segment */
145 shp = shm_lock(id);
146 BUG_ON(!shp);
147 shp->shm_lprid = current->tgid;
148 shp->shm_dtim = get_seconds();
149 shp->shm_nattch--;
150 if(shp->shm_nattch == 0 &&
151 shp->shm_perm.mode & SHM_DEST)
152 shm_destroy (shp);
153 else
154 shm_unlock(shp);
155 mutex_unlock(&shm_ids.mutex);
158 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
160 int ret;
162 ret = shmem_mmap(file, vma);
163 if (ret == 0) {
164 vma->vm_ops = &shm_vm_ops;
165 shm_inc(file->f_dentry->d_inode->i_ino);
168 return ret;
171 static struct file_operations shm_file_operations = {
172 .mmap = shm_mmap,
173 #ifndef CONFIG_MMU
174 .get_unmapped_area = shmem_get_unmapped_area,
175 #endif
178 static struct vm_operations_struct shm_vm_ops = {
179 .open = shm_open, /* callback for a new vm-area open */
180 .close = shm_close, /* callback for when the vm-area is released */
181 .nopage = shmem_nopage,
182 #if defined(CONFIG_NUMA) && defined(CONFIG_SHMEM)
183 .set_policy = shmem_set_policy,
184 .get_policy = shmem_get_policy,
185 #endif
188 static int newseg (key_t key, int shmflg, size_t size)
190 int error;
191 struct shmid_kernel *shp;
192 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
193 struct file * file;
194 char name[13];
195 int id;
197 if (size < SHMMIN || size > shm_ctlmax)
198 return -EINVAL;
200 if (shm_tot + numpages >= shm_ctlall)
201 return -ENOSPC;
203 shp = ipc_rcu_alloc(sizeof(*shp));
204 if (!shp)
205 return -ENOMEM;
207 shp->shm_perm.key = key;
208 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
209 shp->mlock_user = NULL;
211 shp->shm_perm.security = NULL;
212 error = security_shm_alloc(shp);
213 if (error) {
214 ipc_rcu_putref(shp);
215 return error;
218 if (shmflg & SHM_HUGETLB) {
219 /* hugetlb_zero_setup takes care of mlock user accounting */
220 file = hugetlb_zero_setup(size);
221 shp->mlock_user = current->user;
222 } else {
223 int acctflag = VM_ACCOUNT;
225 * Do not allow no accounting for OVERCOMMIT_NEVER, even
226 * if it's asked for.
228 if ((shmflg & SHM_NORESERVE) &&
229 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
230 acctflag = 0;
231 sprintf (name, "SYSV%08x", key);
232 file = shmem_file_setup(name, size, acctflag);
234 error = PTR_ERR(file);
235 if (IS_ERR(file))
236 goto no_file;
238 error = -ENOSPC;
239 id = shm_addid(shp);
240 if(id == -1)
241 goto no_id;
243 shp->shm_cprid = current->tgid;
244 shp->shm_lprid = 0;
245 shp->shm_atim = shp->shm_dtim = 0;
246 shp->shm_ctim = get_seconds();
247 shp->shm_segsz = size;
248 shp->shm_nattch = 0;
249 shp->id = shm_buildid(id,shp->shm_perm.seq);
250 shp->shm_file = file;
251 file->f_dentry->d_inode->i_ino = shp->id;
253 /* Hugetlb ops would have already been assigned. */
254 if (!(shmflg & SHM_HUGETLB))
255 file->f_op = &shm_file_operations;
257 shm_tot += numpages;
258 shm_unlock(shp);
259 return shp->id;
261 no_id:
262 fput(file);
263 no_file:
264 security_shm_free(shp);
265 ipc_rcu_putref(shp);
266 return error;
269 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
271 struct shmid_kernel *shp;
272 int err, id = 0;
274 mutex_lock(&shm_ids.mutex);
275 if (key == IPC_PRIVATE) {
276 err = newseg(key, shmflg, size);
277 } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
278 if (!(shmflg & IPC_CREAT))
279 err = -ENOENT;
280 else
281 err = newseg(key, shmflg, size);
282 } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
283 err = -EEXIST;
284 } else {
285 shp = shm_lock(id);
286 BUG_ON(shp==NULL);
287 if (shp->shm_segsz < size)
288 err = -EINVAL;
289 else if (ipcperms(&shp->shm_perm, shmflg))
290 err = -EACCES;
291 else {
292 int shmid = shm_buildid(id, shp->shm_perm.seq);
293 err = security_shm_associate(shp, shmflg);
294 if (!err)
295 err = shmid;
297 shm_unlock(shp);
299 mutex_unlock(&shm_ids.mutex);
301 return err;
304 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
306 switch(version) {
307 case IPC_64:
308 return copy_to_user(buf, in, sizeof(*in));
309 case IPC_OLD:
311 struct shmid_ds out;
313 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
314 out.shm_segsz = in->shm_segsz;
315 out.shm_atime = in->shm_atime;
316 out.shm_dtime = in->shm_dtime;
317 out.shm_ctime = in->shm_ctime;
318 out.shm_cpid = in->shm_cpid;
319 out.shm_lpid = in->shm_lpid;
320 out.shm_nattch = in->shm_nattch;
322 return copy_to_user(buf, &out, sizeof(out));
324 default:
325 return -EINVAL;
329 struct shm_setbuf {
330 uid_t uid;
331 gid_t gid;
332 mode_t mode;
335 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
337 switch(version) {
338 case IPC_64:
340 struct shmid64_ds tbuf;
342 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
343 return -EFAULT;
345 out->uid = tbuf.shm_perm.uid;
346 out->gid = tbuf.shm_perm.gid;
347 out->mode = tbuf.shm_perm.mode;
349 return 0;
351 case IPC_OLD:
353 struct shmid_ds tbuf_old;
355 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
356 return -EFAULT;
358 out->uid = tbuf_old.shm_perm.uid;
359 out->gid = tbuf_old.shm_perm.gid;
360 out->mode = tbuf_old.shm_perm.mode;
362 return 0;
364 default:
365 return -EINVAL;
369 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
371 switch(version) {
372 case IPC_64:
373 return copy_to_user(buf, in, sizeof(*in));
374 case IPC_OLD:
376 struct shminfo out;
378 if(in->shmmax > INT_MAX)
379 out.shmmax = INT_MAX;
380 else
381 out.shmmax = (int)in->shmmax;
383 out.shmmin = in->shmmin;
384 out.shmmni = in->shmmni;
385 out.shmseg = in->shmseg;
386 out.shmall = in->shmall;
388 return copy_to_user(buf, &out, sizeof(out));
390 default:
391 return -EINVAL;
395 static void shm_get_stat(unsigned long *rss, unsigned long *swp)
397 int i;
399 *rss = 0;
400 *swp = 0;
402 for (i = 0; i <= shm_ids.max_id; i++) {
403 struct shmid_kernel *shp;
404 struct inode *inode;
406 shp = shm_get(i);
407 if(!shp)
408 continue;
410 inode = shp->shm_file->f_dentry->d_inode;
412 if (is_file_hugepages(shp->shm_file)) {
413 struct address_space *mapping = inode->i_mapping;
414 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
415 } else {
416 struct shmem_inode_info *info = SHMEM_I(inode);
417 spin_lock(&info->lock);
418 *rss += inode->i_mapping->nrpages;
419 *swp += info->swapped;
420 spin_unlock(&info->lock);
425 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
427 struct shm_setbuf setbuf;
428 struct shmid_kernel *shp;
429 int err, version;
431 if (cmd < 0 || shmid < 0) {
432 err = -EINVAL;
433 goto out;
436 version = ipc_parse_version(&cmd);
438 switch (cmd) { /* replace with proc interface ? */
439 case IPC_INFO:
441 struct shminfo64 shminfo;
443 err = security_shm_shmctl(NULL, cmd);
444 if (err)
445 return err;
447 memset(&shminfo,0,sizeof(shminfo));
448 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
449 shminfo.shmmax = shm_ctlmax;
450 shminfo.shmall = shm_ctlall;
452 shminfo.shmmin = SHMMIN;
453 if(copy_shminfo_to_user (buf, &shminfo, version))
454 return -EFAULT;
455 /* reading a integer is always atomic */
456 err= shm_ids.max_id;
457 if(err<0)
458 err = 0;
459 goto out;
461 case SHM_INFO:
463 struct shm_info shm_info;
465 err = security_shm_shmctl(NULL, cmd);
466 if (err)
467 return err;
469 memset(&shm_info,0,sizeof(shm_info));
470 mutex_lock(&shm_ids.mutex);
471 shm_info.used_ids = shm_ids.in_use;
472 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
473 shm_info.shm_tot = shm_tot;
474 shm_info.swap_attempts = 0;
475 shm_info.swap_successes = 0;
476 err = shm_ids.max_id;
477 mutex_unlock(&shm_ids.mutex);
478 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
479 err = -EFAULT;
480 goto out;
483 err = err < 0 ? 0 : err;
484 goto out;
486 case SHM_STAT:
487 case IPC_STAT:
489 struct shmid64_ds tbuf;
490 int result;
491 memset(&tbuf, 0, sizeof(tbuf));
492 shp = shm_lock(shmid);
493 if(shp==NULL) {
494 err = -EINVAL;
495 goto out;
496 } else if(cmd==SHM_STAT) {
497 err = -EINVAL;
498 if (shmid > shm_ids.max_id)
499 goto out_unlock;
500 result = shm_buildid(shmid, shp->shm_perm.seq);
501 } else {
502 err = shm_checkid(shp,shmid);
503 if(err)
504 goto out_unlock;
505 result = 0;
507 err=-EACCES;
508 if (ipcperms (&shp->shm_perm, S_IRUGO))
509 goto out_unlock;
510 err = security_shm_shmctl(shp, cmd);
511 if (err)
512 goto out_unlock;
513 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
514 tbuf.shm_segsz = shp->shm_segsz;
515 tbuf.shm_atime = shp->shm_atim;
516 tbuf.shm_dtime = shp->shm_dtim;
517 tbuf.shm_ctime = shp->shm_ctim;
518 tbuf.shm_cpid = shp->shm_cprid;
519 tbuf.shm_lpid = shp->shm_lprid;
520 if (!is_file_hugepages(shp->shm_file))
521 tbuf.shm_nattch = shp->shm_nattch;
522 else
523 tbuf.shm_nattch = file_count(shp->shm_file) - 1;
524 shm_unlock(shp);
525 if(copy_shmid_to_user (buf, &tbuf, version))
526 err = -EFAULT;
527 else
528 err = result;
529 goto out;
531 case SHM_LOCK:
532 case SHM_UNLOCK:
534 shp = shm_lock(shmid);
535 if(shp==NULL) {
536 err = -EINVAL;
537 goto out;
539 err = shm_checkid(shp,shmid);
540 if(err)
541 goto out_unlock;
543 if (!capable(CAP_IPC_LOCK)) {
544 err = -EPERM;
545 if (current->euid != shp->shm_perm.uid &&
546 current->euid != shp->shm_perm.cuid)
547 goto out_unlock;
548 if (cmd == SHM_LOCK &&
549 !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
550 goto out_unlock;
553 err = security_shm_shmctl(shp, cmd);
554 if (err)
555 goto out_unlock;
557 if(cmd==SHM_LOCK) {
558 struct user_struct * user = current->user;
559 if (!is_file_hugepages(shp->shm_file)) {
560 err = shmem_lock(shp->shm_file, 1, user);
561 if (!err) {
562 shp->shm_perm.mode |= SHM_LOCKED;
563 shp->mlock_user = user;
566 } else if (!is_file_hugepages(shp->shm_file)) {
567 shmem_lock(shp->shm_file, 0, shp->mlock_user);
568 shp->shm_perm.mode &= ~SHM_LOCKED;
569 shp->mlock_user = NULL;
571 shm_unlock(shp);
572 goto out;
574 case IPC_RMID:
577 * We cannot simply remove the file. The SVID states
578 * that the block remains until the last person
579 * detaches from it, then is deleted. A shmat() on
580 * an RMID segment is legal in older Linux and if
581 * we change it apps break...
583 * Instead we set a destroyed flag, and then blow
584 * the name away when the usage hits zero.
586 mutex_lock(&shm_ids.mutex);
587 shp = shm_lock(shmid);
588 err = -EINVAL;
589 if (shp == NULL)
590 goto out_up;
591 err = shm_checkid(shp, shmid);
592 if(err)
593 goto out_unlock_up;
595 if (current->euid != shp->shm_perm.uid &&
596 current->euid != shp->shm_perm.cuid &&
597 !capable(CAP_SYS_ADMIN)) {
598 err=-EPERM;
599 goto out_unlock_up;
602 err = security_shm_shmctl(shp, cmd);
603 if (err)
604 goto out_unlock_up;
606 if (shp->shm_nattch){
607 shp->shm_perm.mode |= SHM_DEST;
608 /* Do not find it any more */
609 shp->shm_perm.key = IPC_PRIVATE;
610 shm_unlock(shp);
611 } else
612 shm_destroy (shp);
613 mutex_unlock(&shm_ids.mutex);
614 goto out;
617 case IPC_SET:
619 if (copy_shmid_from_user (&setbuf, buf, version)) {
620 err = -EFAULT;
621 goto out;
623 mutex_lock(&shm_ids.mutex);
624 shp = shm_lock(shmid);
625 err=-EINVAL;
626 if(shp==NULL)
627 goto out_up;
628 if ((err = audit_ipc_perms(0, setbuf.uid, setbuf.gid,
629 setbuf.mode, &(shp->shm_perm))))
630 goto out_unlock_up;
631 err = shm_checkid(shp,shmid);
632 if(err)
633 goto out_unlock_up;
634 err=-EPERM;
635 if (current->euid != shp->shm_perm.uid &&
636 current->euid != shp->shm_perm.cuid &&
637 !capable(CAP_SYS_ADMIN)) {
638 goto out_unlock_up;
641 err = security_shm_shmctl(shp, cmd);
642 if (err)
643 goto out_unlock_up;
645 shp->shm_perm.uid = setbuf.uid;
646 shp->shm_perm.gid = setbuf.gid;
647 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
648 | (setbuf.mode & S_IRWXUGO);
649 shp->shm_ctim = get_seconds();
650 break;
653 default:
654 err = -EINVAL;
655 goto out;
658 err = 0;
659 out_unlock_up:
660 shm_unlock(shp);
661 out_up:
662 mutex_unlock(&shm_ids.mutex);
663 goto out;
664 out_unlock:
665 shm_unlock(shp);
666 out:
667 return err;
671 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
673 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
674 * "raddr" thing points to kernel space, and there has to be a wrapper around
675 * this.
677 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
679 struct shmid_kernel *shp;
680 unsigned long addr;
681 unsigned long size;
682 struct file * file;
683 int err;
684 unsigned long flags;
685 unsigned long prot;
686 unsigned long o_flags;
687 int acc_mode;
688 void *user_addr;
690 if (shmid < 0) {
691 err = -EINVAL;
692 goto out;
693 } else if ((addr = (ulong)shmaddr)) {
694 if (addr & (SHMLBA-1)) {
695 if (shmflg & SHM_RND)
696 addr &= ~(SHMLBA-1); /* round down */
697 else
698 #ifndef __ARCH_FORCE_SHMLBA
699 if (addr & ~PAGE_MASK)
700 #endif
701 return -EINVAL;
703 flags = MAP_SHARED | MAP_FIXED;
704 } else {
705 if ((shmflg & SHM_REMAP))
706 return -EINVAL;
708 flags = MAP_SHARED;
711 if (shmflg & SHM_RDONLY) {
712 prot = PROT_READ;
713 o_flags = O_RDONLY;
714 acc_mode = S_IRUGO;
715 } else {
716 prot = PROT_READ | PROT_WRITE;
717 o_flags = O_RDWR;
718 acc_mode = S_IRUGO | S_IWUGO;
720 if (shmflg & SHM_EXEC) {
721 prot |= PROT_EXEC;
722 acc_mode |= S_IXUGO;
726 * We cannot rely on the fs check since SYSV IPC does have an
727 * additional creator id...
729 shp = shm_lock(shmid);
730 if(shp == NULL) {
731 err = -EINVAL;
732 goto out;
734 err = shm_checkid(shp,shmid);
735 if (err) {
736 shm_unlock(shp);
737 goto out;
739 if (ipcperms(&shp->shm_perm, acc_mode)) {
740 shm_unlock(shp);
741 err = -EACCES;
742 goto out;
745 err = security_shm_shmat(shp, shmaddr, shmflg);
746 if (err) {
747 shm_unlock(shp);
748 return err;
751 file = shp->shm_file;
752 size = i_size_read(file->f_dentry->d_inode);
753 shp->shm_nattch++;
754 shm_unlock(shp);
756 down_write(&current->mm->mmap_sem);
757 if (addr && !(shmflg & SHM_REMAP)) {
758 user_addr = ERR_PTR(-EINVAL);
759 if (find_vma_intersection(current->mm, addr, addr + size))
760 goto invalid;
762 * If shm segment goes below stack, make sure there is some
763 * space left for the stack to grow (at least 4 pages).
765 if (addr < current->mm->start_stack &&
766 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
767 goto invalid;
770 user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
772 invalid:
773 up_write(&current->mm->mmap_sem);
775 mutex_lock(&shm_ids.mutex);
776 shp = shm_lock(shmid);
777 BUG_ON(!shp);
778 shp->shm_nattch--;
779 if(shp->shm_nattch == 0 &&
780 shp->shm_perm.mode & SHM_DEST)
781 shm_destroy (shp);
782 else
783 shm_unlock(shp);
784 mutex_unlock(&shm_ids.mutex);
786 *raddr = (unsigned long) user_addr;
787 err = 0;
788 if (IS_ERR(user_addr))
789 err = PTR_ERR(user_addr);
790 out:
791 return err;
794 asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
796 unsigned long ret;
797 long err;
799 err = do_shmat(shmid, shmaddr, shmflg, &ret);
800 if (err)
801 return err;
802 force_successful_syscall_return();
803 return (long)ret;
807 * detach and kill segment if marked destroyed.
808 * The work is done in shm_close.
810 asmlinkage long sys_shmdt(char __user *shmaddr)
812 struct mm_struct *mm = current->mm;
813 struct vm_area_struct *vma, *next;
814 unsigned long addr = (unsigned long)shmaddr;
815 loff_t size = 0;
816 int retval = -EINVAL;
818 if (addr & ~PAGE_MASK)
819 return retval;
821 down_write(&mm->mmap_sem);
824 * This function tries to be smart and unmap shm segments that
825 * were modified by partial mlock or munmap calls:
826 * - It first determines the size of the shm segment that should be
827 * unmapped: It searches for a vma that is backed by shm and that
828 * started at address shmaddr. It records it's size and then unmaps
829 * it.
830 * - Then it unmaps all shm vmas that started at shmaddr and that
831 * are within the initially determined size.
832 * Errors from do_munmap are ignored: the function only fails if
833 * it's called with invalid parameters or if it's called to unmap
834 * a part of a vma. Both calls in this function are for full vmas,
835 * the parameters are directly copied from the vma itself and always
836 * valid - therefore do_munmap cannot fail. (famous last words?)
839 * If it had been mremap()'d, the starting address would not
840 * match the usual checks anyway. So assume all vma's are
841 * above the starting address given.
843 vma = find_vma(mm, addr);
845 while (vma) {
846 next = vma->vm_next;
849 * Check if the starting address would match, i.e. it's
850 * a fragment created by mprotect() and/or munmap(), or it
851 * otherwise it starts at this address with no hassles.
853 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
854 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
857 size = vma->vm_file->f_dentry->d_inode->i_size;
858 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
860 * We discovered the size of the shm segment, so
861 * break out of here and fall through to the next
862 * loop that uses the size information to stop
863 * searching for matching vma's.
865 retval = 0;
866 vma = next;
867 break;
869 vma = next;
873 * We need look no further than the maximum address a fragment
874 * could possibly have landed at. Also cast things to loff_t to
875 * prevent overflows and make comparisions vs. equal-width types.
877 size = PAGE_ALIGN(size);
878 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
879 next = vma->vm_next;
881 /* finding a matching vma now does not alter retval */
882 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
883 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
885 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
886 vma = next;
889 up_write(&mm->mmap_sem);
890 return retval;
893 #ifdef CONFIG_PROC_FS
894 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
896 struct shmid_kernel *shp = it;
897 char *format;
899 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
900 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
902 if (sizeof(size_t) <= sizeof(int))
903 format = SMALL_STRING;
904 else
905 format = BIG_STRING;
906 return seq_printf(s, format,
907 shp->shm_perm.key,
908 shp->id,
909 shp->shm_perm.mode,
910 shp->shm_segsz,
911 shp->shm_cprid,
912 shp->shm_lprid,
913 is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
914 shp->shm_perm.uid,
915 shp->shm_perm.gid,
916 shp->shm_perm.cuid,
917 shp->shm_perm.cgid,
918 shp->shm_atim,
919 shp->shm_dtim,
920 shp->shm_ctim);
922 #endif