Linux-2.6.12-rc2
[linux-2.6/next.git] / ipc / shm.c
blob06cd5c91056f56af458a069aba6bb609bcf46989
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 <linux/syscalls.h>
30 #include <linux/audit.h>
31 #include <asm/uaccess.h>
33 #include "util.h"
35 #define shm_flags shm_perm.mode
37 static struct file_operations shm_file_operations;
38 static struct vm_operations_struct shm_vm_ops;
40 static struct ipc_ids shm_ids;
42 #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
43 #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
44 #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
45 #define shm_buildid(id, seq) \
46 ipc_buildid(&shm_ids, id, seq)
48 static int newseg (key_t key, int shmflg, size_t size);
49 static void shm_open (struct vm_area_struct *shmd);
50 static void shm_close (struct vm_area_struct *shmd);
51 #ifdef CONFIG_PROC_FS
52 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
53 #endif
55 size_t shm_ctlmax = SHMMAX;
56 size_t shm_ctlall = SHMALL;
57 int shm_ctlmni = SHMMNI;
59 static int shm_tot; /* total number of shared memory pages */
61 void __init shm_init (void)
63 ipc_init_ids(&shm_ids, 1);
64 #ifdef CONFIG_PROC_FS
65 create_proc_read_entry("sysvipc/shm", 0, NULL, sysvipc_shm_read_proc, NULL);
66 #endif
69 static inline int shm_checkid(struct shmid_kernel *s, int id)
71 if (ipc_checkid(&shm_ids,&s->shm_perm,id))
72 return -EIDRM;
73 return 0;
76 static inline struct shmid_kernel *shm_rmid(int id)
78 return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
81 static inline int shm_addid(struct shmid_kernel *shp)
83 return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni);
88 static inline void shm_inc (int id) {
89 struct shmid_kernel *shp;
91 if(!(shp = shm_lock(id)))
92 BUG();
93 shp->shm_atim = get_seconds();
94 shp->shm_lprid = current->tgid;
95 shp->shm_nattch++;
96 shm_unlock(shp);
99 /* This is called by fork, once for every shm attach. */
100 static void shm_open (struct vm_area_struct *shmd)
102 shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
106 * shm_destroy - free the struct shmid_kernel
108 * @shp: struct to free
110 * It has to be called with shp and shm_ids.sem locked,
111 * but returns with shp unlocked and freed.
113 static void shm_destroy (struct shmid_kernel *shp)
115 shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
116 shm_rmid (shp->id);
117 shm_unlock(shp);
118 if (!is_file_hugepages(shp->shm_file))
119 shmem_lock(shp->shm_file, 0, shp->mlock_user);
120 else
121 user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size,
122 shp->mlock_user);
123 fput (shp->shm_file);
124 security_shm_free(shp);
125 ipc_rcu_putref(shp);
129 * remove the attach descriptor shmd.
130 * free memory for segment if it is marked destroyed.
131 * The descriptor has already been removed from the current->mm->mmap list
132 * and will later be kfree()d.
134 static void shm_close (struct vm_area_struct *shmd)
136 struct file * file = shmd->vm_file;
137 int id = file->f_dentry->d_inode->i_ino;
138 struct shmid_kernel *shp;
140 down (&shm_ids.sem);
141 /* remove from the list of attaches of the shm segment */
142 if(!(shp = shm_lock(id)))
143 BUG();
144 shp->shm_lprid = current->tgid;
145 shp->shm_dtim = get_seconds();
146 shp->shm_nattch--;
147 if(shp->shm_nattch == 0 &&
148 shp->shm_flags & SHM_DEST)
149 shm_destroy (shp);
150 else
151 shm_unlock(shp);
152 up (&shm_ids.sem);
155 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
157 file_accessed(file);
158 vma->vm_ops = &shm_vm_ops;
159 shm_inc(file->f_dentry->d_inode->i_ino);
160 return 0;
163 static struct file_operations shm_file_operations = {
164 .mmap = shm_mmap
167 static struct vm_operations_struct shm_vm_ops = {
168 .open = shm_open, /* callback for a new vm-area open */
169 .close = shm_close, /* callback for when the vm-area is released */
170 .nopage = shmem_nopage,
171 #ifdef CONFIG_NUMA
172 .set_policy = shmem_set_policy,
173 .get_policy = shmem_get_policy,
174 #endif
177 static int newseg (key_t key, int shmflg, size_t size)
179 int error;
180 struct shmid_kernel *shp;
181 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
182 struct file * file;
183 char name[13];
184 int id;
186 if (size < SHMMIN || size > shm_ctlmax)
187 return -EINVAL;
189 if (shm_tot + numpages >= shm_ctlall)
190 return -ENOSPC;
192 shp = ipc_rcu_alloc(sizeof(*shp));
193 if (!shp)
194 return -ENOMEM;
196 shp->shm_perm.key = key;
197 shp->shm_flags = (shmflg & S_IRWXUGO);
198 shp->mlock_user = NULL;
200 shp->shm_perm.security = NULL;
201 error = security_shm_alloc(shp);
202 if (error) {
203 ipc_rcu_putref(shp);
204 return error;
207 if (shmflg & SHM_HUGETLB) {
208 /* hugetlb_zero_setup takes care of mlock user accounting */
209 file = hugetlb_zero_setup(size);
210 shp->mlock_user = current->user;
211 } else {
212 sprintf (name, "SYSV%08x", key);
213 file = shmem_file_setup(name, size, VM_ACCOUNT);
215 error = PTR_ERR(file);
216 if (IS_ERR(file))
217 goto no_file;
219 error = -ENOSPC;
220 id = shm_addid(shp);
221 if(id == -1)
222 goto no_id;
224 shp->shm_cprid = current->tgid;
225 shp->shm_lprid = 0;
226 shp->shm_atim = shp->shm_dtim = 0;
227 shp->shm_ctim = get_seconds();
228 shp->shm_segsz = size;
229 shp->shm_nattch = 0;
230 shp->id = shm_buildid(id,shp->shm_perm.seq);
231 shp->shm_file = file;
232 file->f_dentry->d_inode->i_ino = shp->id;
233 if (shmflg & SHM_HUGETLB)
234 set_file_hugepages(file);
235 else
236 file->f_op = &shm_file_operations;
237 shm_tot += numpages;
238 shm_unlock(shp);
239 return shp->id;
241 no_id:
242 fput(file);
243 no_file:
244 security_shm_free(shp);
245 ipc_rcu_putref(shp);
246 return error;
249 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
251 struct shmid_kernel *shp;
252 int err, id = 0;
254 down(&shm_ids.sem);
255 if (key == IPC_PRIVATE) {
256 err = newseg(key, shmflg, size);
257 } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
258 if (!(shmflg & IPC_CREAT))
259 err = -ENOENT;
260 else
261 err = newseg(key, shmflg, size);
262 } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
263 err = -EEXIST;
264 } else {
265 shp = shm_lock(id);
266 if(shp==NULL)
267 BUG();
268 if (shp->shm_segsz < size)
269 err = -EINVAL;
270 else if (ipcperms(&shp->shm_perm, shmflg))
271 err = -EACCES;
272 else {
273 int shmid = shm_buildid(id, shp->shm_perm.seq);
274 err = security_shm_associate(shp, shmflg);
275 if (!err)
276 err = shmid;
278 shm_unlock(shp);
280 up(&shm_ids.sem);
282 return err;
285 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
287 switch(version) {
288 case IPC_64:
289 return copy_to_user(buf, in, sizeof(*in));
290 case IPC_OLD:
292 struct shmid_ds out;
294 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
295 out.shm_segsz = in->shm_segsz;
296 out.shm_atime = in->shm_atime;
297 out.shm_dtime = in->shm_dtime;
298 out.shm_ctime = in->shm_ctime;
299 out.shm_cpid = in->shm_cpid;
300 out.shm_lpid = in->shm_lpid;
301 out.shm_nattch = in->shm_nattch;
303 return copy_to_user(buf, &out, sizeof(out));
305 default:
306 return -EINVAL;
310 struct shm_setbuf {
311 uid_t uid;
312 gid_t gid;
313 mode_t mode;
316 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
318 switch(version) {
319 case IPC_64:
321 struct shmid64_ds tbuf;
323 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
324 return -EFAULT;
326 out->uid = tbuf.shm_perm.uid;
327 out->gid = tbuf.shm_perm.gid;
328 out->mode = tbuf.shm_flags;
330 return 0;
332 case IPC_OLD:
334 struct shmid_ds tbuf_old;
336 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
337 return -EFAULT;
339 out->uid = tbuf_old.shm_perm.uid;
340 out->gid = tbuf_old.shm_perm.gid;
341 out->mode = tbuf_old.shm_flags;
343 return 0;
345 default:
346 return -EINVAL;
350 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
352 switch(version) {
353 case IPC_64:
354 return copy_to_user(buf, in, sizeof(*in));
355 case IPC_OLD:
357 struct shminfo out;
359 if(in->shmmax > INT_MAX)
360 out.shmmax = INT_MAX;
361 else
362 out.shmmax = (int)in->shmmax;
364 out.shmmin = in->shmmin;
365 out.shmmni = in->shmmni;
366 out.shmseg = in->shmseg;
367 out.shmall = in->shmall;
369 return copy_to_user(buf, &out, sizeof(out));
371 default:
372 return -EINVAL;
376 static void shm_get_stat(unsigned long *rss, unsigned long *swp)
378 int i;
380 *rss = 0;
381 *swp = 0;
383 for (i = 0; i <= shm_ids.max_id; i++) {
384 struct shmid_kernel *shp;
385 struct inode *inode;
387 shp = shm_get(i);
388 if(!shp)
389 continue;
391 inode = shp->shm_file->f_dentry->d_inode;
393 if (is_file_hugepages(shp->shm_file)) {
394 struct address_space *mapping = inode->i_mapping;
395 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
396 } else {
397 struct shmem_inode_info *info = SHMEM_I(inode);
398 spin_lock(&info->lock);
399 *rss += inode->i_mapping->nrpages;
400 *swp += info->swapped;
401 spin_unlock(&info->lock);
406 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
408 struct shm_setbuf setbuf;
409 struct shmid_kernel *shp;
410 int err, version;
412 if (cmd < 0 || shmid < 0) {
413 err = -EINVAL;
414 goto out;
417 version = ipc_parse_version(&cmd);
419 switch (cmd) { /* replace with proc interface ? */
420 case IPC_INFO:
422 struct shminfo64 shminfo;
424 err = security_shm_shmctl(NULL, cmd);
425 if (err)
426 return err;
428 memset(&shminfo,0,sizeof(shminfo));
429 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
430 shminfo.shmmax = shm_ctlmax;
431 shminfo.shmall = shm_ctlall;
433 shminfo.shmmin = SHMMIN;
434 if(copy_shminfo_to_user (buf, &shminfo, version))
435 return -EFAULT;
436 /* reading a integer is always atomic */
437 err= shm_ids.max_id;
438 if(err<0)
439 err = 0;
440 goto out;
442 case SHM_INFO:
444 struct shm_info shm_info;
446 err = security_shm_shmctl(NULL, cmd);
447 if (err)
448 return err;
450 memset(&shm_info,0,sizeof(shm_info));
451 down(&shm_ids.sem);
452 shm_info.used_ids = shm_ids.in_use;
453 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
454 shm_info.shm_tot = shm_tot;
455 shm_info.swap_attempts = 0;
456 shm_info.swap_successes = 0;
457 err = shm_ids.max_id;
458 up(&shm_ids.sem);
459 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
460 err = -EFAULT;
461 goto out;
464 err = err < 0 ? 0 : err;
465 goto out;
467 case SHM_STAT:
468 case IPC_STAT:
470 struct shmid64_ds tbuf;
471 int result;
472 memset(&tbuf, 0, sizeof(tbuf));
473 shp = shm_lock(shmid);
474 if(shp==NULL) {
475 err = -EINVAL;
476 goto out;
477 } else if(cmd==SHM_STAT) {
478 err = -EINVAL;
479 if (shmid > shm_ids.max_id)
480 goto out_unlock;
481 result = shm_buildid(shmid, shp->shm_perm.seq);
482 } else {
483 err = shm_checkid(shp,shmid);
484 if(err)
485 goto out_unlock;
486 result = 0;
488 err=-EACCES;
489 if (ipcperms (&shp->shm_perm, S_IRUGO))
490 goto out_unlock;
491 err = security_shm_shmctl(shp, cmd);
492 if (err)
493 goto out_unlock;
494 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
495 tbuf.shm_segsz = shp->shm_segsz;
496 tbuf.shm_atime = shp->shm_atim;
497 tbuf.shm_dtime = shp->shm_dtim;
498 tbuf.shm_ctime = shp->shm_ctim;
499 tbuf.shm_cpid = shp->shm_cprid;
500 tbuf.shm_lpid = shp->shm_lprid;
501 if (!is_file_hugepages(shp->shm_file))
502 tbuf.shm_nattch = shp->shm_nattch;
503 else
504 tbuf.shm_nattch = file_count(shp->shm_file) - 1;
505 shm_unlock(shp);
506 if(copy_shmid_to_user (buf, &tbuf, version))
507 err = -EFAULT;
508 else
509 err = result;
510 goto out;
512 case SHM_LOCK:
513 case SHM_UNLOCK:
515 shp = shm_lock(shmid);
516 if(shp==NULL) {
517 err = -EINVAL;
518 goto out;
520 err = shm_checkid(shp,shmid);
521 if(err)
522 goto out_unlock;
524 if (!capable(CAP_IPC_LOCK)) {
525 err = -EPERM;
526 if (current->euid != shp->shm_perm.uid &&
527 current->euid != shp->shm_perm.cuid)
528 goto out_unlock;
529 if (cmd == SHM_LOCK &&
530 !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
531 goto out_unlock;
534 err = security_shm_shmctl(shp, cmd);
535 if (err)
536 goto out_unlock;
538 if(cmd==SHM_LOCK) {
539 struct user_struct * user = current->user;
540 if (!is_file_hugepages(shp->shm_file)) {
541 err = shmem_lock(shp->shm_file, 1, user);
542 if (!err) {
543 shp->shm_flags |= SHM_LOCKED;
544 shp->mlock_user = user;
547 } else if (!is_file_hugepages(shp->shm_file)) {
548 shmem_lock(shp->shm_file, 0, shp->mlock_user);
549 shp->shm_flags &= ~SHM_LOCKED;
550 shp->mlock_user = NULL;
552 shm_unlock(shp);
553 goto out;
555 case IPC_RMID:
558 * We cannot simply remove the file. The SVID states
559 * that the block remains until the last person
560 * detaches from it, then is deleted. A shmat() on
561 * an RMID segment is legal in older Linux and if
562 * we change it apps break...
564 * Instead we set a destroyed flag, and then blow
565 * the name away when the usage hits zero.
567 down(&shm_ids.sem);
568 shp = shm_lock(shmid);
569 err = -EINVAL;
570 if (shp == NULL)
571 goto out_up;
572 err = shm_checkid(shp, shmid);
573 if(err)
574 goto out_unlock_up;
576 if (current->euid != shp->shm_perm.uid &&
577 current->euid != shp->shm_perm.cuid &&
578 !capable(CAP_SYS_ADMIN)) {
579 err=-EPERM;
580 goto out_unlock_up;
583 err = security_shm_shmctl(shp, cmd);
584 if (err)
585 goto out_unlock_up;
587 if (shp->shm_nattch){
588 shp->shm_flags |= SHM_DEST;
589 /* Do not find it any more */
590 shp->shm_perm.key = IPC_PRIVATE;
591 shm_unlock(shp);
592 } else
593 shm_destroy (shp);
594 up(&shm_ids.sem);
595 goto out;
598 case IPC_SET:
600 if (copy_shmid_from_user (&setbuf, buf, version)) {
601 err = -EFAULT;
602 goto out;
604 if ((err = audit_ipc_perms(0, setbuf.uid, setbuf.gid, setbuf.mode)))
605 return err;
606 down(&shm_ids.sem);
607 shp = shm_lock(shmid);
608 err=-EINVAL;
609 if(shp==NULL)
610 goto out_up;
611 err = shm_checkid(shp,shmid);
612 if(err)
613 goto out_unlock_up;
614 err=-EPERM;
615 if (current->euid != shp->shm_perm.uid &&
616 current->euid != shp->shm_perm.cuid &&
617 !capable(CAP_SYS_ADMIN)) {
618 goto out_unlock_up;
621 err = security_shm_shmctl(shp, cmd);
622 if (err)
623 goto out_unlock_up;
625 shp->shm_perm.uid = setbuf.uid;
626 shp->shm_perm.gid = setbuf.gid;
627 shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
628 | (setbuf.mode & S_IRWXUGO);
629 shp->shm_ctim = get_seconds();
630 break;
633 default:
634 err = -EINVAL;
635 goto out;
638 err = 0;
639 out_unlock_up:
640 shm_unlock(shp);
641 out_up:
642 up(&shm_ids.sem);
643 goto out;
644 out_unlock:
645 shm_unlock(shp);
646 out:
647 return err;
651 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
653 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
654 * "raddr" thing points to kernel space, and there has to be a wrapper around
655 * this.
657 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
659 struct shmid_kernel *shp;
660 unsigned long addr;
661 unsigned long size;
662 struct file * file;
663 int err;
664 unsigned long flags;
665 unsigned long prot;
666 unsigned long o_flags;
667 int acc_mode;
668 void *user_addr;
670 if (shmid < 0) {
671 err = -EINVAL;
672 goto out;
673 } else if ((addr = (ulong)shmaddr)) {
674 if (addr & (SHMLBA-1)) {
675 if (shmflg & SHM_RND)
676 addr &= ~(SHMLBA-1); /* round down */
677 else
678 #ifndef __ARCH_FORCE_SHMLBA
679 if (addr & ~PAGE_MASK)
680 #endif
681 return -EINVAL;
683 flags = MAP_SHARED | MAP_FIXED;
684 } else {
685 if ((shmflg & SHM_REMAP))
686 return -EINVAL;
688 flags = MAP_SHARED;
691 if (shmflg & SHM_RDONLY) {
692 prot = PROT_READ;
693 o_flags = O_RDONLY;
694 acc_mode = S_IRUGO;
695 } else {
696 prot = PROT_READ | PROT_WRITE;
697 o_flags = O_RDWR;
698 acc_mode = S_IRUGO | S_IWUGO;
700 if (shmflg & SHM_EXEC) {
701 prot |= PROT_EXEC;
702 acc_mode |= S_IXUGO;
706 * We cannot rely on the fs check since SYSV IPC does have an
707 * additional creator id...
709 shp = shm_lock(shmid);
710 if(shp == NULL) {
711 err = -EINVAL;
712 goto out;
714 err = shm_checkid(shp,shmid);
715 if (err) {
716 shm_unlock(shp);
717 goto out;
719 if (ipcperms(&shp->shm_perm, acc_mode)) {
720 shm_unlock(shp);
721 err = -EACCES;
722 goto out;
725 err = security_shm_shmat(shp, shmaddr, shmflg);
726 if (err) {
727 shm_unlock(shp);
728 return err;
731 file = shp->shm_file;
732 size = i_size_read(file->f_dentry->d_inode);
733 shp->shm_nattch++;
734 shm_unlock(shp);
736 down_write(&current->mm->mmap_sem);
737 if (addr && !(shmflg & SHM_REMAP)) {
738 user_addr = ERR_PTR(-EINVAL);
739 if (find_vma_intersection(current->mm, addr, addr + size))
740 goto invalid;
742 * If shm segment goes below stack, make sure there is some
743 * space left for the stack to grow (at least 4 pages).
745 if (addr < current->mm->start_stack &&
746 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
747 goto invalid;
750 user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
752 invalid:
753 up_write(&current->mm->mmap_sem);
755 down (&shm_ids.sem);
756 if(!(shp = shm_lock(shmid)))
757 BUG();
758 shp->shm_nattch--;
759 if(shp->shm_nattch == 0 &&
760 shp->shm_flags & SHM_DEST)
761 shm_destroy (shp);
762 else
763 shm_unlock(shp);
764 up (&shm_ids.sem);
766 *raddr = (unsigned long) user_addr;
767 err = 0;
768 if (IS_ERR(user_addr))
769 err = PTR_ERR(user_addr);
770 out:
771 return err;
775 * detach and kill segment if marked destroyed.
776 * The work is done in shm_close.
778 asmlinkage long sys_shmdt(char __user *shmaddr)
780 struct mm_struct *mm = current->mm;
781 struct vm_area_struct *vma, *next;
782 unsigned long addr = (unsigned long)shmaddr;
783 loff_t size = 0;
784 int retval = -EINVAL;
786 down_write(&mm->mmap_sem);
789 * This function tries to be smart and unmap shm segments that
790 * were modified by partial mlock or munmap calls:
791 * - It first determines the size of the shm segment that should be
792 * unmapped: It searches for a vma that is backed by shm and that
793 * started at address shmaddr. It records it's size and then unmaps
794 * it.
795 * - Then it unmaps all shm vmas that started at shmaddr and that
796 * are within the initially determined size.
797 * Errors from do_munmap are ignored: the function only fails if
798 * it's called with invalid parameters or if it's called to unmap
799 * a part of a vma. Both calls in this function are for full vmas,
800 * the parameters are directly copied from the vma itself and always
801 * valid - therefore do_munmap cannot fail. (famous last words?)
804 * If it had been mremap()'d, the starting address would not
805 * match the usual checks anyway. So assume all vma's are
806 * above the starting address given.
808 vma = find_vma(mm, addr);
810 while (vma) {
811 next = vma->vm_next;
814 * Check if the starting address would match, i.e. it's
815 * a fragment created by mprotect() and/or munmap(), or it
816 * otherwise it starts at this address with no hassles.
818 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
819 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
822 size = vma->vm_file->f_dentry->d_inode->i_size;
823 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
825 * We discovered the size of the shm segment, so
826 * break out of here and fall through to the next
827 * loop that uses the size information to stop
828 * searching for matching vma's.
830 retval = 0;
831 vma = next;
832 break;
834 vma = next;
838 * We need look no further than the maximum address a fragment
839 * could possibly have landed at. Also cast things to loff_t to
840 * prevent overflows and make comparisions vs. equal-width types.
842 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
843 next = vma->vm_next;
845 /* finding a matching vma now does not alter retval */
846 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
847 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
849 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
850 vma = next;
853 up_write(&mm->mmap_sem);
854 return retval;
857 #ifdef CONFIG_PROC_FS
858 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
860 off_t pos = 0;
861 off_t begin = 0;
862 int i, len = 0;
864 down(&shm_ids.sem);
865 len += sprintf(buffer, " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n");
867 for(i = 0; i <= shm_ids.max_id; i++) {
868 struct shmid_kernel* shp;
870 shp = shm_lock(i);
871 if(shp!=NULL) {
872 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
873 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
874 char *format;
876 if (sizeof(size_t) <= sizeof(int))
877 format = SMALL_STRING;
878 else
879 format = BIG_STRING;
880 len += sprintf(buffer + len, format,
881 shp->shm_perm.key,
882 shm_buildid(i, shp->shm_perm.seq),
883 shp->shm_flags,
884 shp->shm_segsz,
885 shp->shm_cprid,
886 shp->shm_lprid,
887 is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
888 shp->shm_perm.uid,
889 shp->shm_perm.gid,
890 shp->shm_perm.cuid,
891 shp->shm_perm.cgid,
892 shp->shm_atim,
893 shp->shm_dtim,
894 shp->shm_ctim);
895 shm_unlock(shp);
897 pos += len;
898 if(pos < offset) {
899 len = 0;
900 begin = pos;
902 if(pos > offset + length)
903 goto done;
906 *eof = 1;
907 done:
908 up(&shm_ids.sem);
909 *start = buffer + (offset - begin);
910 len -= (offset - begin);
911 if(len > length)
912 len = length;
913 if(len < 0)
914 len = 0;
915 return len;
917 #endif