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