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