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