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