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