[PATCH] DVB: misc. updates to the dvb-core
[linux-2.6/history.git] / ipc / shm.c
blobf5ca90c8addb5d53c54418f988f994cae63fef2b
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 <asm/uaccess.h>
32 #include "util.h"
34 #define shm_flags shm_perm.mode
36 static struct file_operations shm_file_operations;
37 static struct vm_operations_struct shm_vm_ops;
39 static struct ipc_ids shm_ids;
41 #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
42 #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
43 #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
44 #define shm_buildid(id, seq) \
45 ipc_buildid(&shm_ids, id, seq)
47 static int newseg (key_t key, int shmflg, size_t size);
48 static void shm_open (struct vm_area_struct *shmd);
49 static void shm_close (struct vm_area_struct *shmd);
50 #ifdef CONFIG_PROC_FS
51 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
52 #endif
54 size_t shm_ctlmax = SHMMAX;
55 size_t shm_ctlall = SHMALL;
56 int shm_ctlmni = SHMMNI;
58 static int shm_tot; /* total number of shared memory pages */
60 void __init shm_init (void)
62 ipc_init_ids(&shm_ids, 1);
63 #ifdef CONFIG_PROC_FS
64 create_proc_read_entry("sysvipc/shm", 0, NULL, sysvipc_shm_read_proc, NULL);
65 #endif
68 static inline int shm_checkid(struct shmid_kernel *s, int id)
70 if (ipc_checkid(&shm_ids,&s->shm_perm,id))
71 return -EIDRM;
72 return 0;
75 static inline struct shmid_kernel *shm_rmid(int id)
77 return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
80 static inline int shm_addid(struct shmid_kernel *shp)
82 return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni);
87 static inline void shm_inc (int id) {
88 struct shmid_kernel *shp;
90 if(!(shp = shm_lock(id)))
91 BUG();
92 shp->shm_atim = get_seconds();
93 shp->shm_lprid = current->tgid;
94 shp->shm_nattch++;
95 shm_unlock(shp);
98 /* This is called by fork, once for every shm attach. */
99 static void shm_open (struct vm_area_struct *shmd)
101 shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
105 * shm_destroy - free the struct shmid_kernel
107 * @shp: struct to free
109 * It has to be called with shp and shm_ids.sem locked,
110 * but returns with shp unlocked and freed.
112 static void shm_destroy (struct shmid_kernel *shp)
114 shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
115 shm_rmid (shp->id);
116 shm_unlock(shp);
117 if (!is_file_hugepages(shp->shm_file))
118 shmem_lock(shp->shm_file, 0, shp->mlock_user);
119 else
120 user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size,
121 shp->mlock_user);
122 fput (shp->shm_file);
123 security_shm_free(shp);
124 ipc_rcu_putref(shp);
128 * remove the attach descriptor shmd.
129 * free memory for segment if it is marked destroyed.
130 * The descriptor has already been removed from the current->mm->mmap list
131 * and will later be kfree()d.
133 static void shm_close (struct vm_area_struct *shmd)
135 struct file * file = shmd->vm_file;
136 int id = file->f_dentry->d_inode->i_ino;
137 struct shmid_kernel *shp;
139 down (&shm_ids.sem);
140 /* remove from the list of attaches of the shm segment */
141 if(!(shp = shm_lock(id)))
142 BUG();
143 shp->shm_lprid = current->tgid;
144 shp->shm_dtim = get_seconds();
145 shp->shm_nattch--;
146 if(shp->shm_nattch == 0 &&
147 shp->shm_flags & SHM_DEST)
148 shm_destroy (shp);
149 else
150 shm_unlock(shp);
151 up (&shm_ids.sem);
154 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
156 file_accessed(file);
157 vma->vm_ops = &shm_vm_ops;
158 shm_inc(file->f_dentry->d_inode->i_ino);
159 return 0;
162 static struct file_operations shm_file_operations = {
163 .mmap = shm_mmap
166 static struct vm_operations_struct shm_vm_ops = {
167 .open = shm_open, /* callback for a new vm-area open */
168 .close = shm_close, /* callback for when the vm-area is released */
169 .nopage = shmem_nopage,
170 #ifdef CONFIG_NUMA
171 .set_policy = shmem_set_policy,
172 .get_policy = shmem_get_policy,
173 #endif
176 static int newseg (key_t key, int shmflg, size_t size)
178 int error;
179 struct shmid_kernel *shp;
180 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
181 struct file * file;
182 char name[13];
183 int id;
185 if (size < SHMMIN || size > shm_ctlmax)
186 return -EINVAL;
188 if (shm_tot + numpages >= shm_ctlall)
189 return -ENOSPC;
191 shp = ipc_rcu_alloc(sizeof(*shp));
192 if (!shp)
193 return -ENOMEM;
195 shp->shm_perm.key = key;
196 shp->shm_flags = (shmflg & S_IRWXUGO);
197 shp->mlock_user = NULL;
199 shp->shm_perm.security = NULL;
200 error = security_shm_alloc(shp);
201 if (error) {
202 ipc_rcu_putref(shp);
203 return error;
206 if (shmflg & SHM_HUGETLB) {
207 /* hugetlb_zero_setup takes care of mlock user accounting */
208 file = hugetlb_zero_setup(size);
209 shp->mlock_user = current->user;
210 } else {
211 sprintf (name, "SYSV%08x", key);
212 file = shmem_file_setup(name, size, VM_ACCOUNT);
214 error = PTR_ERR(file);
215 if (IS_ERR(file))
216 goto no_file;
218 error = -ENOSPC;
219 id = shm_addid(shp);
220 if(id == -1)
221 goto no_id;
223 shp->shm_cprid = current->tgid;
224 shp->shm_lprid = 0;
225 shp->shm_atim = shp->shm_dtim = 0;
226 shp->shm_ctim = get_seconds();
227 shp->shm_segsz = size;
228 shp->shm_nattch = 0;
229 shp->id = shm_buildid(id,shp->shm_perm.seq);
230 shp->shm_file = file;
231 file->f_dentry->d_inode->i_ino = shp->id;
232 if (shmflg & SHM_HUGETLB)
233 set_file_hugepages(file);
234 else
235 file->f_op = &shm_file_operations;
236 shm_tot += numpages;
237 shm_unlock(shp);
238 return shp->id;
240 no_id:
241 fput(file);
242 no_file:
243 security_shm_free(shp);
244 ipc_rcu_putref(shp);
245 return error;
248 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
250 struct shmid_kernel *shp;
251 int err, id = 0;
253 down(&shm_ids.sem);
254 if (key == IPC_PRIVATE) {
255 err = newseg(key, shmflg, size);
256 } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
257 if (!(shmflg & IPC_CREAT))
258 err = -ENOENT;
259 else
260 err = newseg(key, shmflg, size);
261 } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
262 err = -EEXIST;
263 } else {
264 shp = shm_lock(id);
265 if(shp==NULL)
266 BUG();
267 if (shp->shm_segsz < size)
268 err = -EINVAL;
269 else if (ipcperms(&shp->shm_perm, shmflg))
270 err = -EACCES;
271 else {
272 int shmid = shm_buildid(id, shp->shm_perm.seq);
273 err = security_shm_associate(shp, shmflg);
274 if (!err)
275 err = shmid;
277 shm_unlock(shp);
279 up(&shm_ids.sem);
281 return err;
284 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
286 switch(version) {
287 case IPC_64:
288 return copy_to_user(buf, in, sizeof(*in));
289 case IPC_OLD:
291 struct shmid_ds out;
293 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
294 out.shm_segsz = in->shm_segsz;
295 out.shm_atime = in->shm_atime;
296 out.shm_dtime = in->shm_dtime;
297 out.shm_ctime = in->shm_ctime;
298 out.shm_cpid = in->shm_cpid;
299 out.shm_lpid = in->shm_lpid;
300 out.shm_nattch = in->shm_nattch;
302 return copy_to_user(buf, &out, sizeof(out));
304 default:
305 return -EINVAL;
309 struct shm_setbuf {
310 uid_t uid;
311 gid_t gid;
312 mode_t mode;
315 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
317 switch(version) {
318 case IPC_64:
320 struct shmid64_ds tbuf;
322 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
323 return -EFAULT;
325 out->uid = tbuf.shm_perm.uid;
326 out->gid = tbuf.shm_perm.gid;
327 out->mode = tbuf.shm_flags;
329 return 0;
331 case IPC_OLD:
333 struct shmid_ds tbuf_old;
335 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
336 return -EFAULT;
338 out->uid = tbuf_old.shm_perm.uid;
339 out->gid = tbuf_old.shm_perm.gid;
340 out->mode = tbuf_old.shm_flags;
342 return 0;
344 default:
345 return -EINVAL;
349 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
351 switch(version) {
352 case IPC_64:
353 return copy_to_user(buf, in, sizeof(*in));
354 case IPC_OLD:
356 struct shminfo out;
358 if(in->shmmax > INT_MAX)
359 out.shmmax = INT_MAX;
360 else
361 out.shmmax = (int)in->shmmax;
363 out.shmmin = in->shmmin;
364 out.shmmni = in->shmmni;
365 out.shmseg = in->shmseg;
366 out.shmall = in->shmall;
368 return copy_to_user(buf, &out, sizeof(out));
370 default:
371 return -EINVAL;
375 static void shm_get_stat(unsigned long *rss, unsigned long *swp)
377 int i;
379 *rss = 0;
380 *swp = 0;
382 for (i = 0; i <= shm_ids.max_id; i++) {
383 struct shmid_kernel *shp;
384 struct inode *inode;
386 shp = shm_get(i);
387 if(!shp)
388 continue;
390 inode = shp->shm_file->f_dentry->d_inode;
392 if (is_file_hugepages(shp->shm_file)) {
393 struct address_space *mapping = inode->i_mapping;
394 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
395 } else {
396 struct shmem_inode_info *info = SHMEM_I(inode);
397 spin_lock(&info->lock);
398 *rss += inode->i_mapping->nrpages;
399 *swp += info->swapped;
400 spin_unlock(&info->lock);
405 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
407 struct shm_setbuf setbuf;
408 struct shmid_kernel *shp;
409 int err, version;
411 if (cmd < 0 || shmid < 0) {
412 err = -EINVAL;
413 goto out;
416 version = ipc_parse_version(&cmd);
418 switch (cmd) { /* replace with proc interface ? */
419 case IPC_INFO:
421 struct shminfo64 shminfo;
423 err = security_shm_shmctl(NULL, cmd);
424 if (err)
425 return err;
427 memset(&shminfo,0,sizeof(shminfo));
428 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
429 shminfo.shmmax = shm_ctlmax;
430 shminfo.shmall = shm_ctlall;
432 shminfo.shmmin = SHMMIN;
433 if(copy_shminfo_to_user (buf, &shminfo, version))
434 return -EFAULT;
435 /* reading a integer is always atomic */
436 err= shm_ids.max_id;
437 if(err<0)
438 err = 0;
439 goto out;
441 case SHM_INFO:
443 struct shm_info shm_info;
445 err = security_shm_shmctl(NULL, cmd);
446 if (err)
447 return err;
449 memset(&shm_info,0,sizeof(shm_info));
450 down(&shm_ids.sem);
451 shm_info.used_ids = shm_ids.in_use;
452 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
453 shm_info.shm_tot = shm_tot;
454 shm_info.swap_attempts = 0;
455 shm_info.swap_successes = 0;
456 err = shm_ids.max_id;
457 up(&shm_ids.sem);
458 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
459 err = -EFAULT;
460 goto out;
463 err = err < 0 ? 0 : err;
464 goto out;
466 case SHM_STAT:
467 case IPC_STAT:
469 struct shmid64_ds tbuf;
470 int result;
471 memset(&tbuf, 0, sizeof(tbuf));
472 shp = shm_lock(shmid);
473 if(shp==NULL) {
474 err = -EINVAL;
475 goto out;
476 } else if(cmd==SHM_STAT) {
477 err = -EINVAL;
478 if (shmid > shm_ids.max_id)
479 goto out_unlock;
480 result = shm_buildid(shmid, shp->shm_perm.seq);
481 } else {
482 err = shm_checkid(shp,shmid);
483 if(err)
484 goto out_unlock;
485 result = 0;
487 err=-EACCES;
488 if (ipcperms (&shp->shm_perm, S_IRUGO))
489 goto out_unlock;
490 err = security_shm_shmctl(shp, cmd);
491 if (err)
492 goto out_unlock;
493 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
494 tbuf.shm_segsz = shp->shm_segsz;
495 tbuf.shm_atime = shp->shm_atim;
496 tbuf.shm_dtime = shp->shm_dtim;
497 tbuf.shm_ctime = shp->shm_ctim;
498 tbuf.shm_cpid = shp->shm_cprid;
499 tbuf.shm_lpid = shp->shm_lprid;
500 if (!is_file_hugepages(shp->shm_file))
501 tbuf.shm_nattch = shp->shm_nattch;
502 else
503 tbuf.shm_nattch = file_count(shp->shm_file) - 1;
504 shm_unlock(shp);
505 if(copy_shmid_to_user (buf, &tbuf, version))
506 err = -EFAULT;
507 else
508 err = result;
509 goto out;
511 case SHM_LOCK:
512 case SHM_UNLOCK:
514 /* Allow superuser to lock segment in memory */
515 if (!can_do_mlock() && cmd == SHM_LOCK) {
516 err = -EPERM;
517 goto out;
519 shp = shm_lock(shmid);
520 if(shp==NULL) {
521 err = -EINVAL;
522 goto out;
524 err = shm_checkid(shp,shmid);
525 if(err)
526 goto out_unlock;
528 err = security_shm_shmctl(shp, cmd);
529 if (err)
530 goto out_unlock;
532 if(cmd==SHM_LOCK) {
533 struct user_struct * user = current->user;
534 if (!is_file_hugepages(shp->shm_file)) {
535 err = shmem_lock(shp->shm_file, 1, user);
536 if (!err) {
537 shp->shm_flags |= SHM_LOCKED;
538 shp->mlock_user = user;
541 } else if (!is_file_hugepages(shp->shm_file)) {
542 shmem_lock(shp->shm_file, 0, shp->mlock_user);
543 shp->shm_flags &= ~SHM_LOCKED;
544 shp->mlock_user = NULL;
546 shm_unlock(shp);
547 goto out;
549 case IPC_RMID:
552 * We cannot simply remove the file. The SVID states
553 * that the block remains until the last person
554 * detaches from it, then is deleted. A shmat() on
555 * an RMID segment is legal in older Linux and if
556 * we change it apps break...
558 * Instead we set a destroyed flag, and then blow
559 * the name away when the usage hits zero.
561 down(&shm_ids.sem);
562 shp = shm_lock(shmid);
563 err = -EINVAL;
564 if (shp == NULL)
565 goto out_up;
566 err = shm_checkid(shp, shmid);
567 if(err)
568 goto out_unlock_up;
570 if (current->euid != shp->shm_perm.uid &&
571 current->euid != shp->shm_perm.cuid &&
572 !capable(CAP_SYS_ADMIN)) {
573 err=-EPERM;
574 goto out_unlock_up;
577 err = security_shm_shmctl(shp, cmd);
578 if (err)
579 goto out_unlock_up;
581 if (shp->shm_nattch){
582 shp->shm_flags |= SHM_DEST;
583 /* Do not find it any more */
584 shp->shm_perm.key = IPC_PRIVATE;
585 shm_unlock(shp);
586 } else
587 shm_destroy (shp);
588 up(&shm_ids.sem);
589 goto out;
592 case IPC_SET:
594 if (copy_shmid_from_user (&setbuf, buf, version)) {
595 err = -EFAULT;
596 goto out;
598 down(&shm_ids.sem);
599 shp = shm_lock(shmid);
600 err=-EINVAL;
601 if(shp==NULL)
602 goto out_up;
603 err = shm_checkid(shp,shmid);
604 if(err)
605 goto out_unlock_up;
606 err=-EPERM;
607 if (current->euid != shp->shm_perm.uid &&
608 current->euid != shp->shm_perm.cuid &&
609 !capable(CAP_SYS_ADMIN)) {
610 goto out_unlock_up;
613 err = security_shm_shmctl(shp, cmd);
614 if (err)
615 goto out_unlock_up;
617 shp->shm_perm.uid = setbuf.uid;
618 shp->shm_perm.gid = setbuf.gid;
619 shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
620 | (setbuf.mode & S_IRWXUGO);
621 shp->shm_ctim = get_seconds();
622 break;
625 default:
626 err = -EINVAL;
627 goto out;
630 err = 0;
631 out_unlock_up:
632 shm_unlock(shp);
633 out_up:
634 up(&shm_ids.sem);
635 goto out;
636 out_unlock:
637 shm_unlock(shp);
638 out:
639 return err;
643 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
645 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
646 * "raddr" thing points to kernel space, and there has to be a wrapper around
647 * this.
649 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
651 struct shmid_kernel *shp;
652 unsigned long addr;
653 unsigned long size;
654 struct file * file;
655 int err;
656 unsigned long flags;
657 unsigned long prot;
658 unsigned long o_flags;
659 int acc_mode;
660 void *user_addr;
662 if (shmid < 0) {
663 err = -EINVAL;
664 goto out;
665 } else if ((addr = (ulong)shmaddr)) {
666 if (addr & (SHMLBA-1)) {
667 if (shmflg & SHM_RND)
668 addr &= ~(SHMLBA-1); /* round down */
669 else
670 #ifndef __ARCH_FORCE_SHMLBA
671 if (addr & ~PAGE_MASK)
672 #endif
673 return -EINVAL;
675 flags = MAP_SHARED | MAP_FIXED;
676 } else {
677 if ((shmflg & SHM_REMAP))
678 return -EINVAL;
680 flags = MAP_SHARED;
683 if (shmflg & SHM_RDONLY) {
684 prot = PROT_READ;
685 o_flags = O_RDONLY;
686 acc_mode = S_IRUGO;
687 } else {
688 prot = PROT_READ | PROT_WRITE;
689 o_flags = O_RDWR;
690 acc_mode = S_IRUGO | S_IWUGO;
692 if (shmflg & SHM_EXEC) {
693 prot |= PROT_EXEC;
694 acc_mode |= S_IXUGO;
698 * We cannot rely on the fs check since SYSV IPC does have an
699 * additional creator id...
701 shp = shm_lock(shmid);
702 if(shp == NULL) {
703 err = -EINVAL;
704 goto out;
706 err = shm_checkid(shp,shmid);
707 if (err) {
708 shm_unlock(shp);
709 goto out;
711 if (ipcperms(&shp->shm_perm, acc_mode)) {
712 shm_unlock(shp);
713 err = -EACCES;
714 goto out;
717 err = security_shm_shmat(shp, shmaddr, shmflg);
718 if (err) {
719 shm_unlock(shp);
720 return err;
723 file = shp->shm_file;
724 size = i_size_read(file->f_dentry->d_inode);
725 shp->shm_nattch++;
726 shm_unlock(shp);
728 down_write(&current->mm->mmap_sem);
729 if (addr && !(shmflg & SHM_REMAP)) {
730 user_addr = ERR_PTR(-EINVAL);
731 if (find_vma_intersection(current->mm, addr, addr + size))
732 goto invalid;
734 * If shm segment goes below stack, make sure there is some
735 * space left for the stack to grow (at least 4 pages).
737 if (addr < current->mm->start_stack &&
738 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
739 goto invalid;
742 user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
744 invalid:
745 up_write(&current->mm->mmap_sem);
747 down (&shm_ids.sem);
748 if(!(shp = shm_lock(shmid)))
749 BUG();
750 shp->shm_nattch--;
751 if(shp->shm_nattch == 0 &&
752 shp->shm_flags & SHM_DEST)
753 shm_destroy (shp);
754 else
755 shm_unlock(shp);
756 up (&shm_ids.sem);
758 *raddr = (unsigned long) user_addr;
759 err = 0;
760 if (IS_ERR(user_addr))
761 err = PTR_ERR(user_addr);
762 out:
763 return err;
767 * detach and kill segment if marked destroyed.
768 * The work is done in shm_close.
770 asmlinkage long sys_shmdt(char __user *shmaddr)
772 struct mm_struct *mm = current->mm;
773 struct vm_area_struct *vma, *next;
774 unsigned long addr = (unsigned long)shmaddr;
775 loff_t size = 0;
776 int retval = -EINVAL;
778 down_write(&mm->mmap_sem);
781 * This function tries to be smart and unmap shm segments that
782 * were modified by partial mlock or munmap calls:
783 * - It first determines the size of the shm segment that should be
784 * unmapped: It searches for a vma that is backed by shm and that
785 * started at address shmaddr. It records it's size and then unmaps
786 * it.
787 * - Then it unmaps all shm vmas that started at shmaddr and that
788 * are within the initially determined size.
789 * Errors from do_munmap are ignored: the function only fails if
790 * it's called with invalid parameters or if it's called to unmap
791 * a part of a vma. Both calls in this function are for full vmas,
792 * the parameters are directly copied from the vma itself and always
793 * valid - therefore do_munmap cannot fail. (famous last words?)
796 * If it had been mremap()'d, the starting address would not
797 * match the usual checks anyway. So assume all vma's are
798 * above the starting address given.
800 vma = find_vma(mm, addr);
802 while (vma) {
803 next = vma->vm_next;
806 * Check if the starting address would match, i.e. it's
807 * a fragment created by mprotect() and/or munmap(), or it
808 * otherwise it starts at this address with no hassles.
810 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
811 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
814 size = vma->vm_file->f_dentry->d_inode->i_size;
815 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
817 * We discovered the size of the shm segment, so
818 * break out of here and fall through to the next
819 * loop that uses the size information to stop
820 * searching for matching vma's.
822 retval = 0;
823 vma = next;
824 break;
826 vma = next;
830 * We need look no further than the maximum address a fragment
831 * could possibly have landed at. Also cast things to loff_t to
832 * prevent overflows and make comparisions vs. equal-width types.
834 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
835 next = vma->vm_next;
837 /* finding a matching vma now does not alter retval */
838 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
839 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
841 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
842 vma = next;
845 up_write(&mm->mmap_sem);
846 return retval;
849 #ifdef CONFIG_PROC_FS
850 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
852 off_t pos = 0;
853 off_t begin = 0;
854 int i, len = 0;
856 down(&shm_ids.sem);
857 len += sprintf(buffer, " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n");
859 for(i = 0; i <= shm_ids.max_id; i++) {
860 struct shmid_kernel* shp;
862 shp = shm_lock(i);
863 if(shp!=NULL) {
864 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
865 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
866 char *format;
868 if (sizeof(size_t) <= sizeof(int))
869 format = SMALL_STRING;
870 else
871 format = BIG_STRING;
872 len += sprintf(buffer + len, format,
873 shp->shm_perm.key,
874 shm_buildid(i, shp->shm_perm.seq),
875 shp->shm_flags,
876 shp->shm_segsz,
877 shp->shm_cprid,
878 shp->shm_lprid,
879 is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
880 shp->shm_perm.uid,
881 shp->shm_perm.gid,
882 shp->shm_perm.cuid,
883 shp->shm_perm.cgid,
884 shp->shm_atim,
885 shp->shm_dtim,
886 shp->shm_ctim);
887 shm_unlock(shp);
889 pos += len;
890 if(pos < offset) {
891 len = 0;
892 begin = pos;
894 if(pos > offset + length)
895 goto done;
898 *eof = 1;
899 done:
900 up(&shm_ids.sem);
901 *start = buffer + (offset - begin);
902 len -= (offset - begin);
903 if(len > length)
904 len = length;
905 if(len < 0)
906 len = 0;
907 return len;
909 #endif