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