IPC: fix error check in all new xxx_lock() and xxx_exit_ns() functions
[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  * namespaces support
20  * OpenVZ, SWsoft Inc.
21  * Pavel Emelianov <xemul@openvz.org>
22  */
23
24 #include <linux/slab.h>
25 #include <linux/mm.h>
26 #include <linux/hugetlb.h>
27 #include <linux/shm.h>
28 #include <linux/init.h>
29 #include <linux/file.h>
30 #include <linux/mman.h>
31 #include <linux/shmem_fs.h>
32 #include <linux/security.h>
33 #include <linux/syscalls.h>
34 #include <linux/audit.h>
35 #include <linux/capability.h>
36 #include <linux/ptrace.h>
37 #include <linux/seq_file.h>
38 #include <linux/rwsem.h>
39 #include <linux/nsproxy.h>
40 #include <linux/mount.h>
41
42 #include <asm/uaccess.h>
43
44 #include "util.h"
45
46 struct shm_file_data {
47         int id;
48         struct ipc_namespace *ns;
49         struct file *file;
50         const struct vm_operations_struct *vm_ops;
51 };
52
53 #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
54
55 static const struct file_operations shm_file_operations;
56 static struct vm_operations_struct shm_vm_ops;
57
58 static struct ipc_ids init_shm_ids;
59
60 #define shm_ids(ns)     (*((ns)->ids[IPC_SHM_IDS]))
61
62 #define shm_unlock(shp)                 \
63         ipc_unlock(&(shp)->shm_perm)
64 #define shm_buildid(id, seq)    ipc_buildid(id, seq)
65
66 static int newseg(struct ipc_namespace *, struct ipc_params *);
67 static void shm_open(struct vm_area_struct *vma);
68 static void shm_close(struct vm_area_struct *vma);
69 static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
70 #ifdef CONFIG_PROC_FS
71 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
72 #endif
73
74 static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
75 {
76         ns->ids[IPC_SHM_IDS] = ids;
77         ns->shm_ctlmax = SHMMAX;
78         ns->shm_ctlall = SHMALL;
79         ns->shm_ctlmni = SHMMNI;
80         ns->shm_tot = 0;
81         ipc_init_ids(ids);
82 }
83
84 /*
85  * Called with shm_ids.rw_mutex (writer) and the shp structure locked.
86  * Only shm_ids.rw_mutex remains locked on exit.
87  */
88 static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp)
89 {
90         if (shp->shm_nattch){
91                 shp->shm_perm.mode |= SHM_DEST;
92                 /* Do not find it any more */
93                 shp->shm_perm.key = IPC_PRIVATE;
94                 shm_unlock(shp);
95         } else
96                 shm_destroy(ns, shp);
97 }
98
99 int shm_init_ns(struct ipc_namespace *ns)
100 {
101         struct ipc_ids *ids;
102
103         ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
104         if (ids == NULL)
105                 return -ENOMEM;
106
107         __shm_init_ns(ns, ids);
108         return 0;
109 }
110
111 void shm_exit_ns(struct ipc_namespace *ns)
112 {
113         struct shmid_kernel *shp;
114         struct kern_ipc_perm *perm;
115         int next_id;
116         int total, in_use;
117
118         down_write(&shm_ids(ns).rw_mutex);
119
120         in_use = shm_ids(ns).in_use;
121
122         for (total = 0, next_id = 0; total < in_use; next_id++) {
123                 perm = idr_find(&shm_ids(ns).ipcs_idr, next_id);
124                 if (perm == NULL)
125                         continue;
126                 ipc_lock_by_ptr(perm);
127                 shp = container_of(perm, struct shmid_kernel, shm_perm);
128                 do_shm_rmid(ns, shp);
129                 total++;
130         }
131         up_write(&shm_ids(ns).rw_mutex);
132
133         kfree(ns->ids[IPC_SHM_IDS]);
134         ns->ids[IPC_SHM_IDS] = NULL;
135 }
136
137 void __init shm_init (void)
138 {
139         __shm_init_ns(&init_ipc_ns, &init_shm_ids);
140         ipc_init_proc_interface("sysvipc/shm",
141                                 "       key      shmid perms       size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime\n",
142                                 IPC_SHM_IDS, sysvipc_shm_proc_show);
143 }
144
145 /*
146  * shm_lock_(check_)down routines are called in the paths where the rw_mutex
147  * is held to protect access to the idr tree.
148  */
149 static inline struct shmid_kernel *shm_lock_down(struct ipc_namespace *ns,
150                                                 int id)
151 {
152         struct kern_ipc_perm *ipcp = ipc_lock_down(&shm_ids(ns), id);
153
154         if (IS_ERR(ipcp))
155                 return (struct shmid_kernel *)ipcp;
156
157         return container_of(ipcp, struct shmid_kernel, shm_perm);
158 }
159
160 static inline struct shmid_kernel *shm_lock_check_down(
161                                                 struct ipc_namespace *ns,
162                                                 int id)
163 {
164         struct kern_ipc_perm *ipcp = ipc_lock_check_down(&shm_ids(ns), id);
165
166         if (IS_ERR(ipcp))
167                 return (struct shmid_kernel *)ipcp;
168
169         return container_of(ipcp, struct shmid_kernel, shm_perm);
170 }
171
172 /*
173  * shm_lock_(check_) routines are called in the paths where the rw_mutex
174  * is not held.
175  */
176 static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
177 {
178         struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
179
180         if (IS_ERR(ipcp))
181                 return (struct shmid_kernel *)ipcp;
182
183         return container_of(ipcp, struct shmid_kernel, shm_perm);
184 }
185
186 static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns,
187                                                 int id)
188 {
189         struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id);
190
191         if (IS_ERR(ipcp))
192                 return (struct shmid_kernel *)ipcp;
193
194         return container_of(ipcp, struct shmid_kernel, shm_perm);
195 }
196
197 static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
198 {
199         ipc_rmid(&shm_ids(ns), &s->shm_perm);
200 }
201
202 static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
203 {
204         return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
205 }
206
207
208
209 /* This is called by fork, once for every shm attach. */
210 static void shm_open(struct vm_area_struct *vma)
211 {
212         struct file *file = vma->vm_file;
213         struct shm_file_data *sfd = shm_file_data(file);
214         struct shmid_kernel *shp;
215
216         shp = shm_lock(sfd->ns, sfd->id);
217         BUG_ON(IS_ERR(shp));
218         shp->shm_atim = get_seconds();
219         shp->shm_lprid = task_tgid_vnr(current);
220         shp->shm_nattch++;
221         shm_unlock(shp);
222 }
223
224 /*
225  * shm_destroy - free the struct shmid_kernel
226  *
227  * @ns: namespace
228  * @shp: struct to free
229  *
230  * It has to be called with shp and shm_ids.rw_mutex (writer) locked,
231  * but returns with shp unlocked and freed.
232  */
233 static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
234 {
235         ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
236         shm_rmid(ns, shp);
237         shm_unlock(shp);
238         if (!is_file_hugepages(shp->shm_file))
239                 shmem_lock(shp->shm_file, 0, shp->mlock_user);
240         else
241                 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
242                                                 shp->mlock_user);
243         fput (shp->shm_file);
244         security_shm_free(shp);
245         ipc_rcu_putref(shp);
246 }
247
248 /*
249  * remove the attach descriptor vma.
250  * free memory for segment if it is marked destroyed.
251  * The descriptor has already been removed from the current->mm->mmap list
252  * and will later be kfree()d.
253  */
254 static void shm_close(struct vm_area_struct *vma)
255 {
256         struct file * file = vma->vm_file;
257         struct shm_file_data *sfd = shm_file_data(file);
258         struct shmid_kernel *shp;
259         struct ipc_namespace *ns = sfd->ns;
260
261         down_write(&shm_ids(ns).rw_mutex);
262         /* remove from the list of attaches of the shm segment */
263         shp = shm_lock_down(ns, sfd->id);
264         BUG_ON(IS_ERR(shp));
265         shp->shm_lprid = task_tgid_vnr(current);
266         shp->shm_dtim = get_seconds();
267         shp->shm_nattch--;
268         if(shp->shm_nattch == 0 &&
269            shp->shm_perm.mode & SHM_DEST)
270                 shm_destroy(ns, shp);
271         else
272                 shm_unlock(shp);
273         up_write(&shm_ids(ns).rw_mutex);
274 }
275
276 static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
277 {
278         struct file *file = vma->vm_file;
279         struct shm_file_data *sfd = shm_file_data(file);
280
281         return sfd->vm_ops->fault(vma, vmf);
282 }
283
284 #ifdef CONFIG_NUMA
285 static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
286 {
287         struct file *file = vma->vm_file;
288         struct shm_file_data *sfd = shm_file_data(file);
289         int err = 0;
290         if (sfd->vm_ops->set_policy)
291                 err = sfd->vm_ops->set_policy(vma, new);
292         return err;
293 }
294
295 static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
296                                         unsigned long addr)
297 {
298         struct file *file = vma->vm_file;
299         struct shm_file_data *sfd = shm_file_data(file);
300         struct mempolicy *pol = NULL;
301
302         if (sfd->vm_ops->get_policy)
303                 pol = sfd->vm_ops->get_policy(vma, addr);
304         else if (vma->vm_policy)
305                 pol = vma->vm_policy;
306         else
307                 pol = current->mempolicy;
308         return pol;
309 }
310 #endif
311
312 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
313 {
314         struct shm_file_data *sfd = shm_file_data(file);
315         int ret;
316
317         ret = sfd->file->f_op->mmap(sfd->file, vma);
318         if (ret != 0)
319                 return ret;
320         sfd->vm_ops = vma->vm_ops;
321 #ifdef CONFIG_MMU
322         BUG_ON(!sfd->vm_ops->fault);
323 #endif
324         vma->vm_ops = &shm_vm_ops;
325         shm_open(vma);
326
327         return ret;
328 }
329
330 static int shm_release(struct inode *ino, struct file *file)
331 {
332         struct shm_file_data *sfd = shm_file_data(file);
333
334         put_ipc_ns(sfd->ns);
335         shm_file_data(file) = NULL;
336         kfree(sfd);
337         return 0;
338 }
339
340 static int shm_fsync(struct file *file, struct dentry *dentry, int datasync)
341 {
342         int (*fsync) (struct file *, struct dentry *, int datasync);
343         struct shm_file_data *sfd = shm_file_data(file);
344         int ret = -EINVAL;
345
346         fsync = sfd->file->f_op->fsync;
347         if (fsync)
348                 ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync);
349         return ret;
350 }
351
352 static unsigned long shm_get_unmapped_area(struct file *file,
353         unsigned long addr, unsigned long len, unsigned long pgoff,
354         unsigned long flags)
355 {
356         struct shm_file_data *sfd = shm_file_data(file);
357         return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
358 }
359
360 int is_file_shm_hugepages(struct file *file)
361 {
362         int ret = 0;
363
364         if (file->f_op == &shm_file_operations) {
365                 struct shm_file_data *sfd;
366                 sfd = shm_file_data(file);
367                 ret = is_file_hugepages(sfd->file);
368         }
369         return ret;
370 }
371
372 static const struct file_operations shm_file_operations = {
373         .mmap           = shm_mmap,
374         .fsync          = shm_fsync,
375         .release        = shm_release,
376         .get_unmapped_area      = shm_get_unmapped_area,
377 };
378
379 static struct vm_operations_struct shm_vm_ops = {
380         .open   = shm_open,     /* callback for a new vm-area open */
381         .close  = shm_close,    /* callback for when the vm-area is released */
382         .fault  = shm_fault,
383 #if defined(CONFIG_NUMA)
384         .set_policy = shm_set_policy,
385         .get_policy = shm_get_policy,
386 #endif
387 };
388
389 /**
390  * newseg - Create a new shared memory segment
391  * @ns: namespace
392  * @params: ptr to the structure that contains key, size and shmflg
393  *
394  * Called with shm_ids.rw_mutex held as a writer.
395  */
396
397 static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
398 {
399         key_t key = params->key;
400         int shmflg = params->flg;
401         size_t size = params->u.size;
402         int error;
403         struct shmid_kernel *shp;
404         int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
405         struct file * file;
406         char name[13];
407         int id;
408
409         if (size < SHMMIN || size > ns->shm_ctlmax)
410                 return -EINVAL;
411
412         if (ns->shm_tot + numpages > ns->shm_ctlall)
413                 return -ENOSPC;
414
415         shp = ipc_rcu_alloc(sizeof(*shp));
416         if (!shp)
417                 return -ENOMEM;
418
419         shp->shm_perm.key = key;
420         shp->shm_perm.mode = (shmflg & S_IRWXUGO);
421         shp->mlock_user = NULL;
422
423         shp->shm_perm.security = NULL;
424         error = security_shm_alloc(shp);
425         if (error) {
426                 ipc_rcu_putref(shp);
427                 return error;
428         }
429
430         sprintf (name, "SYSV%08x", key);
431         if (shmflg & SHM_HUGETLB) {
432                 /* hugetlb_file_setup takes care of mlock user accounting */
433                 file = hugetlb_file_setup(name, size);
434                 shp->mlock_user = current->user;
435         } else {
436                 int acctflag = VM_ACCOUNT;
437                 /*
438                  * Do not allow no accounting for OVERCOMMIT_NEVER, even
439                  * if it's asked for.
440                  */
441                 if  ((shmflg & SHM_NORESERVE) &&
442                                 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
443                         acctflag = 0;
444                 file = shmem_file_setup(name, size, acctflag);
445         }
446         error = PTR_ERR(file);
447         if (IS_ERR(file))
448                 goto no_file;
449
450         id = shm_addid(ns, shp);
451         if (id < 0) {
452                 error = id;
453                 goto no_id;
454         }
455
456         shp->shm_cprid = task_tgid_vnr(current);
457         shp->shm_lprid = 0;
458         shp->shm_atim = shp->shm_dtim = 0;
459         shp->shm_ctim = get_seconds();
460         shp->shm_segsz = size;
461         shp->shm_nattch = 0;
462         shp->shm_perm.id = shm_buildid(id, shp->shm_perm.seq);
463         shp->shm_file = file;
464         /*
465          * shmid gets reported as "inode#" in /proc/pid/maps.
466          * proc-ps tools use this. Changing this will break them.
467          */
468         file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
469
470         ns->shm_tot += numpages;
471         error = shp->shm_perm.id;
472         shm_unlock(shp);
473         return error;
474
475 no_id:
476         fput(file);
477 no_file:
478         security_shm_free(shp);
479         ipc_rcu_putref(shp);
480         return error;
481 }
482
483 /*
484  * Called with shm_ids.rw_mutex and ipcp locked.
485  */
486 static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
487 {
488         struct shmid_kernel *shp;
489
490         shp = container_of(ipcp, struct shmid_kernel, shm_perm);
491         return security_shm_associate(shp, shmflg);
492 }
493
494 /*
495  * Called with shm_ids.rw_mutex and ipcp locked.
496  */
497 static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
498                                 struct ipc_params *params)
499 {
500         struct shmid_kernel *shp;
501
502         shp = container_of(ipcp, struct shmid_kernel, shm_perm);
503         if (shp->shm_segsz < params->u.size)
504                 return -EINVAL;
505
506         return 0;
507 }
508
509 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
510 {
511         struct ipc_namespace *ns;
512         struct ipc_ops shm_ops;
513         struct ipc_params shm_params;
514
515         ns = current->nsproxy->ipc_ns;
516
517         shm_ops.getnew = newseg;
518         shm_ops.associate = shm_security;
519         shm_ops.more_checks = shm_more_checks;
520
521         shm_params.key = key;
522         shm_params.flg = shmflg;
523         shm_params.u.size = size;
524
525         return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
526 }
527
528 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
529 {
530         switch(version) {
531         case IPC_64:
532                 return copy_to_user(buf, in, sizeof(*in));
533         case IPC_OLD:
534             {
535                 struct shmid_ds out;
536
537                 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
538                 out.shm_segsz   = in->shm_segsz;
539                 out.shm_atime   = in->shm_atime;
540                 out.shm_dtime   = in->shm_dtime;
541                 out.shm_ctime   = in->shm_ctime;
542                 out.shm_cpid    = in->shm_cpid;
543                 out.shm_lpid    = in->shm_lpid;
544                 out.shm_nattch  = in->shm_nattch;
545
546                 return copy_to_user(buf, &out, sizeof(out));
547             }
548         default:
549                 return -EINVAL;
550         }
551 }
552
553 struct shm_setbuf {
554         uid_t   uid;
555         gid_t   gid;
556         mode_t  mode;
557 };      
558
559 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
560 {
561         switch(version) {
562         case IPC_64:
563             {
564                 struct shmid64_ds tbuf;
565
566                 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
567                         return -EFAULT;
568
569                 out->uid        = tbuf.shm_perm.uid;
570                 out->gid        = tbuf.shm_perm.gid;
571                 out->mode       = tbuf.shm_perm.mode;
572
573                 return 0;
574             }
575         case IPC_OLD:
576             {
577                 struct shmid_ds tbuf_old;
578
579                 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
580                         return -EFAULT;
581
582                 out->uid        = tbuf_old.shm_perm.uid;
583                 out->gid        = tbuf_old.shm_perm.gid;
584                 out->mode       = tbuf_old.shm_perm.mode;
585
586                 return 0;
587             }
588         default:
589                 return -EINVAL;
590         }
591 }
592
593 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
594 {
595         switch(version) {
596         case IPC_64:
597                 return copy_to_user(buf, in, sizeof(*in));
598         case IPC_OLD:
599             {
600                 struct shminfo out;
601
602                 if(in->shmmax > INT_MAX)
603                         out.shmmax = INT_MAX;
604                 else
605                         out.shmmax = (int)in->shmmax;
606
607                 out.shmmin      = in->shmmin;
608                 out.shmmni      = in->shmmni;
609                 out.shmseg      = in->shmseg;
610                 out.shmall      = in->shmall; 
611
612                 return copy_to_user(buf, &out, sizeof(out));
613             }
614         default:
615                 return -EINVAL;
616         }
617 }
618
619 /*
620  * Called with shm_ids.rw_mutex held as a reader
621  */
622 static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
623                 unsigned long *swp)
624 {
625         int next_id;
626         int total, in_use;
627
628         *rss = 0;
629         *swp = 0;
630
631         in_use = shm_ids(ns).in_use;
632
633         for (total = 0, next_id = 0; total < in_use; next_id++) {
634                 struct shmid_kernel *shp;
635                 struct inode *inode;
636
637                 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
638                 if (shp == NULL)
639                         continue;
640
641                 inode = shp->shm_file->f_path.dentry->d_inode;
642
643                 if (is_file_hugepages(shp->shm_file)) {
644                         struct address_space *mapping = inode->i_mapping;
645                         *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
646                 } else {
647                         struct shmem_inode_info *info = SHMEM_I(inode);
648                         spin_lock(&info->lock);
649                         *rss += inode->i_mapping->nrpages;
650                         *swp += info->swapped;
651                         spin_unlock(&info->lock);
652                 }
653
654                 total++;
655         }
656 }
657
658 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
659 {
660         struct shm_setbuf setbuf;
661         struct shmid_kernel *shp;
662         int err, version;
663         struct ipc_namespace *ns;
664
665         if (cmd < 0 || shmid < 0) {
666                 err = -EINVAL;
667                 goto out;
668         }
669
670         version = ipc_parse_version(&cmd);
671         ns = current->nsproxy->ipc_ns;
672
673         switch (cmd) { /* replace with proc interface ? */
674         case IPC_INFO:
675         {
676                 struct shminfo64 shminfo;
677
678                 err = security_shm_shmctl(NULL, cmd);
679                 if (err)
680                         return err;
681
682                 memset(&shminfo,0,sizeof(shminfo));
683                 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
684                 shminfo.shmmax = ns->shm_ctlmax;
685                 shminfo.shmall = ns->shm_ctlall;
686
687                 shminfo.shmmin = SHMMIN;
688                 if(copy_shminfo_to_user (buf, &shminfo, version))
689                         return -EFAULT;
690
691                 down_read(&shm_ids(ns).rw_mutex);
692                 err = ipc_get_maxid(&shm_ids(ns));
693                 up_read(&shm_ids(ns).rw_mutex);
694
695                 if(err<0)
696                         err = 0;
697                 goto out;
698         }
699         case SHM_INFO:
700         {
701                 struct shm_info shm_info;
702
703                 err = security_shm_shmctl(NULL, cmd);
704                 if (err)
705                         return err;
706
707                 memset(&shm_info,0,sizeof(shm_info));
708                 down_read(&shm_ids(ns).rw_mutex);
709                 shm_info.used_ids = shm_ids(ns).in_use;
710                 shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
711                 shm_info.shm_tot = ns->shm_tot;
712                 shm_info.swap_attempts = 0;
713                 shm_info.swap_successes = 0;
714                 err = ipc_get_maxid(&shm_ids(ns));
715                 up_read(&shm_ids(ns).rw_mutex);
716                 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
717                         err = -EFAULT;
718                         goto out;
719                 }
720
721                 err = err < 0 ? 0 : err;
722                 goto out;
723         }
724         case SHM_STAT:
725         case IPC_STAT:
726         {
727                 struct shmid64_ds tbuf;
728                 int result;
729
730                 if (!buf) {
731                         err = -EFAULT;
732                         goto out;
733                 }
734
735                 if (cmd == SHM_STAT) {
736                         shp = shm_lock(ns, shmid);
737                         if (IS_ERR(shp)) {
738                                 err = PTR_ERR(shp);
739                                 goto out;
740                         }
741                         result = shp->shm_perm.id;
742                 } else {
743                         shp = shm_lock_check(ns, shmid);
744                         if (IS_ERR(shp)) {
745                                 err = PTR_ERR(shp);
746                                 goto out;
747                         }
748                         result = 0;
749                 }
750                 err=-EACCES;
751                 if (ipcperms (&shp->shm_perm, S_IRUGO))
752                         goto out_unlock;
753                 err = security_shm_shmctl(shp, cmd);
754                 if (err)
755                         goto out_unlock;
756                 memset(&tbuf, 0, sizeof(tbuf));
757                 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
758                 tbuf.shm_segsz  = shp->shm_segsz;
759                 tbuf.shm_atime  = shp->shm_atim;
760                 tbuf.shm_dtime  = shp->shm_dtim;
761                 tbuf.shm_ctime  = shp->shm_ctim;
762                 tbuf.shm_cpid   = shp->shm_cprid;
763                 tbuf.shm_lpid   = shp->shm_lprid;
764                 tbuf.shm_nattch = shp->shm_nattch;
765                 shm_unlock(shp);
766                 if(copy_shmid_to_user (buf, &tbuf, version))
767                         err = -EFAULT;
768                 else
769                         err = result;
770                 goto out;
771         }
772         case SHM_LOCK:
773         case SHM_UNLOCK:
774         {
775                 shp = shm_lock_check(ns, shmid);
776                 if (IS_ERR(shp)) {
777                         err = PTR_ERR(shp);
778                         goto out;
779                 }
780
781                 err = audit_ipc_obj(&(shp->shm_perm));
782                 if (err)
783                         goto out_unlock;
784
785                 if (!capable(CAP_IPC_LOCK)) {
786                         err = -EPERM;
787                         if (current->euid != shp->shm_perm.uid &&
788                             current->euid != shp->shm_perm.cuid)
789                                 goto out_unlock;
790                         if (cmd == SHM_LOCK &&
791                             !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
792                                 goto out_unlock;
793                 }
794
795                 err = security_shm_shmctl(shp, cmd);
796                 if (err)
797                         goto out_unlock;
798                 
799                 if(cmd==SHM_LOCK) {
800                         struct user_struct * user = current->user;
801                         if (!is_file_hugepages(shp->shm_file)) {
802                                 err = shmem_lock(shp->shm_file, 1, user);
803                                 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
804                                         shp->shm_perm.mode |= SHM_LOCKED;
805                                         shp->mlock_user = user;
806                                 }
807                         }
808                 } else if (!is_file_hugepages(shp->shm_file)) {
809                         shmem_lock(shp->shm_file, 0, shp->mlock_user);
810                         shp->shm_perm.mode &= ~SHM_LOCKED;
811                         shp->mlock_user = NULL;
812                 }
813                 shm_unlock(shp);
814                 goto out;
815         }
816         case IPC_RMID:
817         {
818                 /*
819                  *      We cannot simply remove the file. The SVID states
820                  *      that the block remains until the last person
821                  *      detaches from it, then is deleted. A shmat() on
822                  *      an RMID segment is legal in older Linux and if 
823                  *      we change it apps break...
824                  *
825                  *      Instead we set a destroyed flag, and then blow
826                  *      the name away when the usage hits zero.
827                  */
828                 down_write(&shm_ids(ns).rw_mutex);
829                 shp = shm_lock_check_down(ns, shmid);
830                 if (IS_ERR(shp)) {
831                         err = PTR_ERR(shp);
832                         goto out_up;
833                 }
834
835                 err = audit_ipc_obj(&(shp->shm_perm));
836                 if (err)
837                         goto out_unlock_up;
838
839                 if (current->euid != shp->shm_perm.uid &&
840                     current->euid != shp->shm_perm.cuid && 
841                     !capable(CAP_SYS_ADMIN)) {
842                         err=-EPERM;
843                         goto out_unlock_up;
844                 }
845
846                 err = security_shm_shmctl(shp, cmd);
847                 if (err)
848                         goto out_unlock_up;
849
850                 do_shm_rmid(ns, shp);
851                 up_write(&shm_ids(ns).rw_mutex);
852                 goto out;
853         }
854
855         case IPC_SET:
856         {
857                 if (!buf) {
858                         err = -EFAULT;
859                         goto out;
860                 }
861
862                 if (copy_shmid_from_user (&setbuf, buf, version)) {
863                         err = -EFAULT;
864                         goto out;
865                 }
866                 down_write(&shm_ids(ns).rw_mutex);
867                 shp = shm_lock_check_down(ns, shmid);
868                 if (IS_ERR(shp)) {
869                         err = PTR_ERR(shp);
870                         goto out_up;
871                 }
872                 err = audit_ipc_obj(&(shp->shm_perm));
873                 if (err)
874                         goto out_unlock_up;
875                 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
876                 if (err)
877                         goto out_unlock_up;
878                 err=-EPERM;
879                 if (current->euid != shp->shm_perm.uid &&
880                     current->euid != shp->shm_perm.cuid && 
881                     !capable(CAP_SYS_ADMIN)) {
882                         goto out_unlock_up;
883                 }
884
885                 err = security_shm_shmctl(shp, cmd);
886                 if (err)
887                         goto out_unlock_up;
888                 
889                 shp->shm_perm.uid = setbuf.uid;
890                 shp->shm_perm.gid = setbuf.gid;
891                 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
892                         | (setbuf.mode & S_IRWXUGO);
893                 shp->shm_ctim = get_seconds();
894                 break;
895         }
896
897         default:
898                 err = -EINVAL;
899                 goto out;
900         }
901
902         err = 0;
903 out_unlock_up:
904         shm_unlock(shp);
905 out_up:
906         up_write(&shm_ids(ns).rw_mutex);
907         goto out;
908 out_unlock:
909         shm_unlock(shp);
910 out:
911         return err;
912 }
913
914 /*
915  * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
916  *
917  * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
918  * "raddr" thing points to kernel space, and there has to be a wrapper around
919  * this.
920  */
921 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
922 {
923         struct shmid_kernel *shp;
924         unsigned long addr;
925         unsigned long size;
926         struct file * file;
927         int    err;
928         unsigned long flags;
929         unsigned long prot;
930         int acc_mode;
931         unsigned long user_addr;
932         struct ipc_namespace *ns;
933         struct shm_file_data *sfd;
934         struct path path;
935         mode_t f_mode;
936
937         err = -EINVAL;
938         if (shmid < 0)
939                 goto out;
940         else if ((addr = (ulong)shmaddr)) {
941                 if (addr & (SHMLBA-1)) {
942                         if (shmflg & SHM_RND)
943                                 addr &= ~(SHMLBA-1);       /* round down */
944                         else
945 #ifndef __ARCH_FORCE_SHMLBA
946                                 if (addr & ~PAGE_MASK)
947 #endif
948                                         goto out;
949                 }
950                 flags = MAP_SHARED | MAP_FIXED;
951         } else {
952                 if ((shmflg & SHM_REMAP))
953                         goto out;
954
955                 flags = MAP_SHARED;
956         }
957
958         if (shmflg & SHM_RDONLY) {
959                 prot = PROT_READ;
960                 acc_mode = S_IRUGO;
961                 f_mode = FMODE_READ;
962         } else {
963                 prot = PROT_READ | PROT_WRITE;
964                 acc_mode = S_IRUGO | S_IWUGO;
965                 f_mode = FMODE_READ | FMODE_WRITE;
966         }
967         if (shmflg & SHM_EXEC) {
968                 prot |= PROT_EXEC;
969                 acc_mode |= S_IXUGO;
970         }
971
972         /*
973          * We cannot rely on the fs check since SYSV IPC does have an
974          * additional creator id...
975          */
976         ns = current->nsproxy->ipc_ns;
977         shp = shm_lock_check(ns, shmid);
978         if (IS_ERR(shp)) {
979                 err = PTR_ERR(shp);
980                 goto out;
981         }
982
983         err = -EACCES;
984         if (ipcperms(&shp->shm_perm, acc_mode))
985                 goto out_unlock;
986
987         err = security_shm_shmat(shp, shmaddr, shmflg);
988         if (err)
989                 goto out_unlock;
990
991         path.dentry = dget(shp->shm_file->f_path.dentry);
992         path.mnt    = shp->shm_file->f_path.mnt;
993         shp->shm_nattch++;
994         size = i_size_read(path.dentry->d_inode);
995         shm_unlock(shp);
996
997         err = -ENOMEM;
998         sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
999         if (!sfd)
1000                 goto out_put_dentry;
1001
1002         err = -ENOMEM;
1003
1004         file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
1005         if (!file)
1006                 goto out_free;
1007
1008         file->private_data = sfd;
1009         file->f_mapping = shp->shm_file->f_mapping;
1010         sfd->id = shp->shm_perm.id;
1011         sfd->ns = get_ipc_ns(ns);
1012         sfd->file = shp->shm_file;
1013         sfd->vm_ops = NULL;
1014
1015         down_write(&current->mm->mmap_sem);
1016         if (addr && !(shmflg & SHM_REMAP)) {
1017                 err = -EINVAL;
1018                 if (find_vma_intersection(current->mm, addr, addr + size))
1019                         goto invalid;
1020                 /*
1021                  * If shm segment goes below stack, make sure there is some
1022                  * space left for the stack to grow (at least 4 pages).
1023                  */
1024                 if (addr < current->mm->start_stack &&
1025                     addr > current->mm->start_stack - size - PAGE_SIZE * 5)
1026                         goto invalid;
1027         }
1028                 
1029         user_addr = do_mmap (file, addr, size, prot, flags, 0);
1030         *raddr = user_addr;
1031         err = 0;
1032         if (IS_ERR_VALUE(user_addr))
1033                 err = (long)user_addr;
1034 invalid:
1035         up_write(&current->mm->mmap_sem);
1036
1037         fput(file);
1038
1039 out_nattch:
1040         down_write(&shm_ids(ns).rw_mutex);
1041         shp = shm_lock_down(ns, shmid);
1042         BUG_ON(IS_ERR(shp));
1043         shp->shm_nattch--;
1044         if(shp->shm_nattch == 0 &&
1045            shp->shm_perm.mode & SHM_DEST)
1046                 shm_destroy(ns, shp);
1047         else
1048                 shm_unlock(shp);
1049         up_write(&shm_ids(ns).rw_mutex);
1050
1051 out:
1052         return err;
1053
1054 out_unlock:
1055         shm_unlock(shp);
1056         goto out;
1057
1058 out_free:
1059         kfree(sfd);
1060 out_put_dentry:
1061         dput(path.dentry);
1062         goto out_nattch;
1063 }
1064
1065 asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
1066 {
1067         unsigned long ret;
1068         long err;
1069
1070         err = do_shmat(shmid, shmaddr, shmflg, &ret);
1071         if (err)
1072                 return err;
1073         force_successful_syscall_return();
1074         return (long)ret;
1075 }
1076
1077 /*
1078  * detach and kill segment if marked destroyed.
1079  * The work is done in shm_close.
1080  */
1081 asmlinkage long sys_shmdt(char __user *shmaddr)
1082 {
1083         struct mm_struct *mm = current->mm;
1084         struct vm_area_struct *vma, *next;
1085         unsigned long addr = (unsigned long)shmaddr;
1086         loff_t size = 0;
1087         int retval = -EINVAL;
1088
1089         if (addr & ~PAGE_MASK)
1090                 return retval;
1091
1092         down_write(&mm->mmap_sem);
1093
1094         /*
1095          * This function tries to be smart and unmap shm segments that
1096          * were modified by partial mlock or munmap calls:
1097          * - It first determines the size of the shm segment that should be
1098          *   unmapped: It searches for a vma that is backed by shm and that
1099          *   started at address shmaddr. It records it's size and then unmaps
1100          *   it.
1101          * - Then it unmaps all shm vmas that started at shmaddr and that
1102          *   are within the initially determined size.
1103          * Errors from do_munmap are ignored: the function only fails if
1104          * it's called with invalid parameters or if it's called to unmap
1105          * a part of a vma. Both calls in this function are for full vmas,
1106          * the parameters are directly copied from the vma itself and always
1107          * valid - therefore do_munmap cannot fail. (famous last words?)
1108          */
1109         /*
1110          * If it had been mremap()'d, the starting address would not
1111          * match the usual checks anyway. So assume all vma's are
1112          * above the starting address given.
1113          */
1114         vma = find_vma(mm, addr);
1115
1116         while (vma) {
1117                 next = vma->vm_next;
1118
1119                 /*
1120                  * Check if the starting address would match, i.e. it's
1121                  * a fragment created by mprotect() and/or munmap(), or it
1122                  * otherwise it starts at this address with no hassles.
1123                  */
1124                 if ((vma->vm_ops == &shm_vm_ops) &&
1125                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1126
1127
1128                         size = vma->vm_file->f_path.dentry->d_inode->i_size;
1129                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1130                         /*
1131                          * We discovered the size of the shm segment, so
1132                          * break out of here and fall through to the next
1133                          * loop that uses the size information to stop
1134                          * searching for matching vma's.
1135                          */
1136                         retval = 0;
1137                         vma = next;
1138                         break;
1139                 }
1140                 vma = next;
1141         }
1142
1143         /*
1144          * We need look no further than the maximum address a fragment
1145          * could possibly have landed at. Also cast things to loff_t to
1146          * prevent overflows and make comparisions vs. equal-width types.
1147          */
1148         size = PAGE_ALIGN(size);
1149         while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1150                 next = vma->vm_next;
1151
1152                 /* finding a matching vma now does not alter retval */
1153                 if ((vma->vm_ops == &shm_vm_ops) &&
1154                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
1155
1156                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1157                 vma = next;
1158         }
1159
1160         up_write(&mm->mmap_sem);
1161         return retval;
1162 }
1163
1164 #ifdef CONFIG_PROC_FS
1165 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
1166 {
1167         struct shmid_kernel *shp = it;
1168         char *format;
1169
1170 #define SMALL_STRING "%10d %10d  %4o %10u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1171 #define BIG_STRING   "%10d %10d  %4o %21u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1172
1173         if (sizeof(size_t) <= sizeof(int))
1174                 format = SMALL_STRING;
1175         else
1176                 format = BIG_STRING;
1177         return seq_printf(s, format,
1178                           shp->shm_perm.key,
1179                           shp->shm_perm.id,
1180                           shp->shm_perm.mode,
1181                           shp->shm_segsz,
1182                           shp->shm_cprid,
1183                           shp->shm_lprid,
1184                           shp->shm_nattch,
1185                           shp->shm_perm.uid,
1186                           shp->shm_perm.gid,
1187                           shp->shm_perm.cuid,
1188                           shp->shm_perm.cgid,
1189                           shp->shm_atim,
1190                           shp->shm_dtim,
1191                           shp->shm_ctim);
1192 }
1193 #endif