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