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