Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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