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/malloc.h>
20 #include <linux/shm.h>
21 #include <linux/init.h>
22 #include <linux/file.h>
23 #include <linux/mman.h>
24 #include <linux/proc_fs.h>
25 #include <asm/uaccess.h>
26
27 #include "util.h"
28
29 struct shmid_kernel /* private to the kernel */
30 {
31 struct kern_ipc_perm shm_perm;
32 struct file * shm_file;
33 int id;
34 unsigned long shm_nattch;
35 unsigned long shm_segsz;
36 time_t shm_atim;
37 time_t shm_dtim;
38 time_t shm_ctim;
39 pid_t shm_cprid;
40 pid_t shm_lprid;
41 };
42
43 #define shm_flags shm_perm.mode
44
45 static struct file_operations shm_file_operations;
46 static struct vm_operations_struct shm_vm_ops;
47
48 static struct ipc_ids shm_ids;
49
50 #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
51 #define shm_unlock(id) ipc_unlock(&shm_ids,id)
52 #define shm_lockall() ipc_lockall(&shm_ids)
53 #define shm_unlockall() ipc_unlockall(&shm_ids)
54 #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
55 #define shm_buildid(id, seq) \
56 ipc_buildid(&shm_ids, id, seq)
57
58 static int newseg (key_t key, int shmflg, size_t size);
59 static void shm_open (struct vm_area_struct *shmd);
60 static void shm_close (struct vm_area_struct *shmd);
61 #ifdef CONFIG_PROC_FS
62 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
63 #endif
64
65 size_t shm_ctlmax = SHMMAX;
66 size_t shm_ctlall = SHMALL;
67 int shm_ctlmni = SHMMNI;
68
69 static int shm_tot; /* total number of shared memory pages */
70
71 void __init shm_init (void)
72 {
73 ipc_init_ids(&shm_ids, 1);
74 create_proc_read_entry("sysvipc/shm", 0, 0, sysvipc_shm_read_proc, NULL);
75 }
76
77 static inline int shm_checkid(struct shmid_kernel *s, int id)
78 {
79 if (ipc_checkid(&shm_ids,&s->shm_perm,id))
80 return -EIDRM;
81 return 0;
82 }
83
84 static inline struct shmid_kernel *shm_rmid(int id)
85 {
86 return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
87 }
88
89 static inline int shm_addid(struct shmid_kernel *shp)
90 {
91 return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni+1);
92 }
93
94
95
96 static inline void shm_inc (int id) {
97 struct shmid_kernel *shp;
98
99 if(!(shp = shm_lock(id)))
100 BUG();
101 shp->shm_atim = CURRENT_TIME;
102 shp->shm_lprid = current->pid;
103 shp->shm_nattch++;
104 shm_unlock(id);
105 }
106
107 /* This is called by fork, once for every shm attach. */
108 static void shm_open (struct vm_area_struct *shmd)
109 {
110 shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
111 }
112
113 /*
114 * shm_destroy - free the struct shmid_kernel
115 *
116 * @shp: struct to free
117 *
118 * It has to be called with shp and shm_ids.sem locked
119 */
120 static void shm_destroy (struct shmid_kernel *shp)
121 {
122 shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
123 shm_rmid (shp->id);
124 fput (shp->shm_file);
125 kfree (shp);
126 }
127
128 /*
129 * remove the attach descriptor shmd.
130 * free memory for segment if it is marked destroyed.
131 * The descriptor has already been removed from the current->mm->mmap list
132 * and will later be kfree()d.
133 */
134 static void shm_close (struct vm_area_struct *shmd)
135 {
136 struct file * file = shmd->vm_file;
137 int id = file->f_dentry->d_inode->i_ino;
138 struct shmid_kernel *shp;
139
140 down (&shm_ids.sem);
141 /* remove from the list of attaches of the shm segment */
142 if(!(shp = shm_lock(id)))
143 BUG();
144 shp->shm_lprid = current->pid;
145 shp->shm_dtim = CURRENT_TIME;
146 shp->shm_nattch--;
147 if(shp->shm_nattch == 0 &&
148 shp->shm_flags & SHM_DEST)
149 shm_destroy (shp);
150
151 shm_unlock(id);
152 up (&shm_ids.sem);
153 }
154
155 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
156 {
157 UPDATE_ATIME(file->f_dentry->d_inode);
158 vma->vm_ops = &shm_vm_ops;
159 shm_inc(file->f_dentry->d_inode->i_ino);
160 return 0;
161 }
162
163 static struct file_operations shm_file_operations = {
164 mmap: shm_mmap
165 };
166
167 static struct vm_operations_struct shm_vm_ops = {
168 open: shm_open, /* callback for a new vm-area open */
169 close: shm_close, /* callback for when the vm-area is released */
170 nopage: shmem_nopage,
171 };
172
173 static int newseg (key_t key, int shmflg, size_t size)
174 {
175 int error;
176 struct shmid_kernel *shp;
177 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
178 struct file * file;
179 char name[13];
180 int id;
181
182 if (size < SHMMIN || size > shm_ctlmax)
183 return -EINVAL;
184
185 if (shm_tot + numpages >= shm_ctlall)
186 return -ENOSPC;
187
188 shp = (struct shmid_kernel *) kmalloc (sizeof (*shp), GFP_USER);
189 if (!shp)
190 return -ENOMEM;
191 sprintf (name, "SYSV%08x", key);
192 file = shmem_file_setup(name, size);
193 error = PTR_ERR(file);
194 if (IS_ERR(file))
195 goto no_file;
196
197 error = -ENOSPC;
198 id = shm_addid(shp);
199 if(id == -1)
200 goto no_id;
201 shp->shm_perm.key = key;
202 shp->shm_flags = (shmflg & S_IRWXUGO);
203 shp->shm_cprid = current->pid;
204 shp->shm_lprid = 0;
205 shp->shm_atim = shp->shm_dtim = 0;
206 shp->shm_ctim = CURRENT_TIME;
207 shp->shm_segsz = size;
208 shp->shm_nattch = 0;
209 shp->id = shm_buildid(id,shp->shm_perm.seq);
210 shp->shm_file = file;
211 file->f_dentry->d_inode->i_ino = shp->id;
212 file->f_op = &shm_file_operations;
213 shm_tot += numpages;
214 shm_unlock (id);
215 return shp->id;
216
217 no_id:
218 fput(file);
219 no_file:
220 kfree(shp);
221 return error;
222 }
223
224 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
225 {
226 struct shmid_kernel *shp;
227 int err, id = 0;
228
229 down(&shm_ids.sem);
230 if (key == IPC_PRIVATE) {
231 err = newseg(key, shmflg, size);
232 } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
233 if (!(shmflg & IPC_CREAT))
234 err = -ENOENT;
235 else
236 err = newseg(key, shmflg, size);
237 } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
238 err = -EEXIST;
239 } else {
240 shp = shm_lock(id);
241 if(shp==NULL)
242 BUG();
243 if (shp->shm_segsz < size)
244 err = -EINVAL;
245 else if (ipcperms(&shp->shm_perm, shmflg))
246 err = -EACCES;
247 else
248 err = shm_buildid(id, shp->shm_perm.seq);
249 shm_unlock(id);
250 }
251 up(&shm_ids.sem);
252 return err;
253 }
254
255 static inline unsigned long copy_shmid_to_user(void *buf, struct shmid64_ds *in, int version)
256 {
257 switch(version) {
258 case IPC_64:
259 return copy_to_user(buf, in, sizeof(*in));
260 case IPC_OLD:
261 {
262 struct shmid_ds out;
263
264 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
265 out.shm_segsz = in->shm_segsz;
266 out.shm_atime = in->shm_atime;
267 out.shm_dtime = in->shm_dtime;
268 out.shm_ctime = in->shm_ctime;
269 out.shm_cpid = in->shm_cpid;
270 out.shm_lpid = in->shm_lpid;
271 out.shm_nattch = in->shm_nattch;
272
273 return copy_to_user(buf, &out, sizeof(out));
274 }
275 default:
276 return -EINVAL;
277 }
278 }
279
280 struct shm_setbuf {
281 uid_t uid;
282 gid_t gid;
283 mode_t mode;
284 };
285
286 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void *buf, int version)
287 {
288 switch(version) {
289 case IPC_64:
290 {
291 struct shmid64_ds tbuf;
292
293 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
294 return -EFAULT;
295
296 out->uid = tbuf.shm_perm.uid;
297 out->gid = tbuf.shm_perm.gid;
298 out->mode = tbuf.shm_flags;
299
300 return 0;
301 }
302 case IPC_OLD:
303 {
304 struct shmid_ds tbuf_old;
305
306 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
307 return -EFAULT;
308
309 out->uid = tbuf_old.shm_perm.uid;
310 out->gid = tbuf_old.shm_perm.gid;
311 out->mode = tbuf_old.shm_flags;
312
313 return 0;
314 }
315 default:
316 return -EINVAL;
317 }
318 }
319
320 static inline unsigned long copy_shminfo_to_user(void *buf, struct shminfo64 *in, int version)
321 {
322 switch(version) {
323 case IPC_64:
324 return copy_to_user(buf, in, sizeof(*in));
325 case IPC_OLD:
326 {
327 struct shminfo out;
328
329 if(in->shmmax > INT_MAX)
330 out.shmmax = INT_MAX;
331 else
332 out.shmmax = (int)in->shmmax;
333
334 out.shmmin = in->shmmin;
335 out.shmmni = in->shmmni;
336 out.shmseg = in->shmseg;
337 out.shmall = in->shmall;
338
339 return copy_to_user(buf, &out, sizeof(out));
340 }
341 default:
342 return -EINVAL;
343 }
344 }
345
346 static void shm_get_stat (unsigned long *rss, unsigned long *swp)
347 {
348 int i;
349
350 *rss = 0;
351 *swp = 0;
352
353 for(i = 0; i <= shm_ids.max_id; i++) {
354 struct shmid_kernel* shp;
355 struct inode * inode;
356
357 shp = shm_get(i);
358 if(shp == NULL)
359 continue;
360 inode = shp->shm_file->f_dentry->d_inode;
361 spin_lock (&inode->u.shmem_i.lock);
362 *rss += inode->i_mapping->nrpages;
363 *swp += inode->u.shmem_i.swapped;
364 spin_unlock (&inode->u.shmem_i.lock);
365 }
366 }
367
368 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds *buf)
369 {
370 struct shm_setbuf setbuf;
371 struct shmid_kernel *shp;
372 int err, version;
373
374 if (cmd < 0 || shmid < 0)
375 return -EINVAL;
376
377 version = ipc_parse_version(&cmd);
378
379 switch (cmd) { /* replace with proc interface ? */
380 case IPC_INFO:
381 {
382 struct shminfo64 shminfo;
383
384 memset(&shminfo,0,sizeof(shminfo));
385 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
386 shminfo.shmmax = shm_ctlmax;
387 shminfo.shmall = shm_ctlall;
388
389 shminfo.shmmin = SHMMIN;
390 if(copy_shminfo_to_user (buf, &shminfo, version))
391 return -EFAULT;
392 /* reading a integer is always atomic */
393 err= shm_ids.max_id;
394 if(err<0)
395 err = 0;
396 return err;
397 }
398 case SHM_INFO:
399 {
400 struct shm_info shm_info;
401
402 memset(&shm_info,0,sizeof(shm_info));
403 down(&shm_ids.sem);
404 shm_lockall();
405 shm_info.used_ids = shm_ids.in_use;
406 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
407 shm_info.shm_tot = shm_tot;
408 shm_info.swap_attempts = 0;
409 shm_info.swap_successes = 0;
410 err = shm_ids.max_id;
411 shm_unlockall();
412 up(&shm_ids.sem);
413 if(copy_to_user (buf, &shm_info, sizeof(shm_info)))
414 return -EFAULT;
415
416 return err < 0 ? 0 : err;
417 }
418 case SHM_STAT:
419 case IPC_STAT:
420 {
421 struct shmid64_ds tbuf;
422 int result;
423 memset(&tbuf, 0, sizeof(tbuf));
424 shp = shm_lock(shmid);
425 if(shp==NULL)
426 return -EINVAL;
427 if(cmd==SHM_STAT) {
428 err = -EINVAL;
429 if (shmid > shm_ids.max_id)
430 goto out_unlock;
431 result = shm_buildid(shmid, shp->shm_perm.seq);
432 } else {
433 err = shm_checkid(shp,shmid);
434 if(err)
435 goto out_unlock;
436 result = 0;
437 }
438 err=-EACCES;
439 if (ipcperms (&shp->shm_perm, S_IRUGO))
440 goto out_unlock;
441 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
442 tbuf.shm_segsz = shp->shm_segsz;
443 tbuf.shm_atime = shp->shm_atim;
444 tbuf.shm_dtime = shp->shm_dtim;
445 tbuf.shm_ctime = shp->shm_ctim;
446 tbuf.shm_cpid = shp->shm_cprid;
447 tbuf.shm_lpid = shp->shm_lprid;
448 tbuf.shm_nattch = shp->shm_nattch;
449 shm_unlock(shmid);
450 if(copy_shmid_to_user (buf, &tbuf, version))
451 return -EFAULT;
452 return result;
453 }
454 case SHM_LOCK:
455 case SHM_UNLOCK:
456 {
457 /* Allow superuser to lock segment in memory */
458 /* Should the pages be faulted in here or leave it to user? */
459 /* need to determine interaction with current->swappable */
460 if (!capable(CAP_IPC_LOCK))
461 return -EPERM;
462
463 shp = shm_lock(shmid);
464 if(shp==NULL)
465 return -EINVAL;
466 err = shm_checkid(shp,shmid);
467 if(err)
468 goto out_unlock;
469 if(cmd==SHM_LOCK) {
470 shp->shm_file->f_dentry->d_inode->u.shmem_i.locked = 1;
471 shp->shm_flags |= SHM_LOCKED;
472 } else {
473 shp->shm_file->f_dentry->d_inode->u.shmem_i.locked = 0;
474 shp->shm_flags &= ~SHM_LOCKED;
475 }
476 shm_unlock(shmid);
477 return err;
478 }
479 case IPC_RMID:
480 {
481 /*
482 * We cannot simply remove the file. The SVID states
483 * that the block remains until the last person
484 * detaches from it, then is deleted. A shmat() on
485 * an RMID segment is legal in older Linux and if
486 * we change it apps break...
487 *
488 * Instead we set a destroyed flag, and then blow
489 * the name away when the usage hits zero.
490 */
491 down(&shm_ids.sem);
492 shp = shm_lock(shmid);
493 err = -EINVAL;
494 if (shp == NULL)
495 goto out_up;
496 err = shm_checkid(shp, shmid);
497 if (err == 0) {
498 if (shp->shm_nattch){
499 shp->shm_flags |= SHM_DEST;
500 /* Do not find it any more */
501 shp->shm_perm.key = IPC_PRIVATE;
502 } else
503 shm_destroy (shp);
504 }
505 /* Unlock */
506 shm_unlock(shmid);
507 up(&shm_ids.sem);
508 return err;
509 }
510
511 case IPC_SET:
512 {
513 if(copy_shmid_from_user (&setbuf, buf, version))
514 return -EFAULT;
515 down(&shm_ids.sem);
516 shp = shm_lock(shmid);
517 err=-EINVAL;
518 if(shp==NULL)
519 goto out_up;
520 err = shm_checkid(shp,shmid);
521 if(err)
522 goto out_unlock_up;
523 err=-EPERM;
524 if (current->euid != shp->shm_perm.uid &&
525 current->euid != shp->shm_perm.cuid &&
526 !capable(CAP_SYS_ADMIN)) {
527 goto out_unlock_up;
528 }
529
530 shp->shm_perm.uid = setbuf.uid;
531 shp->shm_perm.gid = setbuf.gid;
532 shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
533 | (setbuf.mode & S_IRWXUGO);
534 shp->shm_ctim = CURRENT_TIME;
535 break;
536 }
537
538 default:
539 return -EINVAL;
540 }
541
542 err = 0;
543 out_unlock_up:
544 shm_unlock(shmid);
545 out_up:
546 up(&shm_ids.sem);
547 return err;
548 out_unlock:
549 shm_unlock(shmid);
550 return err;
551 }
552
553 /*
554 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
555 */
556 asmlinkage long sys_shmat (int shmid, char *shmaddr, int shmflg, ulong *raddr)
557 {
558 struct shmid_kernel *shp;
559 unsigned long addr;
560 struct file * file;
561 int err;
562 unsigned long flags;
563 unsigned long prot;
564 unsigned long o_flags;
565 int acc_mode;
566 void *user_addr;
567
568 if (shmid < 0)
569 return -EINVAL;
570
571 if ((addr = (ulong)shmaddr)) {
572 if (addr & (SHMLBA-1)) {
573 if (shmflg & SHM_RND)
574 addr &= ~(SHMLBA-1); /* round down */
575 else
576 return -EINVAL;
577 }
578 flags = MAP_SHARED | MAP_FIXED;
579 } else
580 flags = MAP_SHARED;
581
582 if (shmflg & SHM_RDONLY) {
583 prot = PROT_READ;
584 o_flags = O_RDONLY;
585 acc_mode = S_IRUGO;
586 } else {
587 prot = PROT_READ | PROT_WRITE;
588 o_flags = O_RDWR;
589 acc_mode = S_IRUGO | S_IWUGO;
590 }
591
592 /*
593 * We cannot rely on the fs check since SYSV IPC does have an
594 * aditional creator id...
595 */
596 shp = shm_lock(shmid);
597 if(shp == NULL)
598 return -EINVAL;
599 if (ipcperms(&shp->shm_perm, acc_mode)) {
600 shm_unlock(shmid);
601 return -EACCES;
602 }
603 file = shp->shm_file;
604 shp->shm_nattch++;
605 shm_unlock(shmid);
606
607 down(¤t->mm->mmap_sem);
608 user_addr = (void *) do_mmap (file, addr, file->f_dentry->d_inode->i_size, prot, flags, 0);
609 up(¤t->mm->mmap_sem);
610
611 down (&shm_ids.sem);
612 if(!(shp = shm_lock(shmid)))
613 BUG();
614 shp->shm_nattch--;
615 if(shp->shm_nattch == 0 &&
616 shp->shm_flags & SHM_DEST)
617 shm_destroy (shp);
618 shm_unlock(shmid);
619 up (&shm_ids.sem);
620
621 *raddr = (unsigned long) user_addr;
622 err = 0;
623 if (IS_ERR(user_addr))
624 err = PTR_ERR(user_addr);
625 return err;
626
627 }
628
629 /*
630 * detach and kill segment if marked destroyed.
631 * The work is done in shm_close.
632 */
633 asmlinkage long sys_shmdt (char *shmaddr)
634 {
635 struct mm_struct *mm = current->mm;
636 struct vm_area_struct *shmd, *shmdnext;
637
638 down(&mm->mmap_sem);
639 for (shmd = mm->mmap; shmd; shmd = shmdnext) {
640 shmdnext = shmd->vm_next;
641 if (shmd->vm_ops == &shm_vm_ops
642 && shmd->vm_start - (shmd->vm_pgoff << PAGE_SHIFT) == (ulong) shmaddr)
643 do_munmap(mm, shmd->vm_start, shmd->vm_end - shmd->vm_start);
644 }
645 up(&mm->mmap_sem);
646 return 0;
647 }
648
649 #ifdef CONFIG_PROC_FS
650 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
651 {
652 off_t pos = 0;
653 off_t begin = 0;
654 int i, len = 0;
655
656 down(&shm_ids.sem);
657 len += sprintf(buffer, " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n");
658
659 for(i = 0; i <= shm_ids.max_id; i++) {
660 struct shmid_kernel* shp;
661
662 shp = shm_lock(i);
663 if(shp!=NULL) {
664 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
665 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
666 char *format;
667
668 if (sizeof(size_t) <= sizeof(int))
669 format = SMALL_STRING;
670 else
671 format = BIG_STRING;
672 len += sprintf(buffer + len, format,
673 shp->shm_perm.key,
674 shm_buildid(i, shp->shm_perm.seq),
675 shp->shm_flags,
676 shp->shm_segsz,
677 shp->shm_cprid,
678 shp->shm_lprid,
679 shp->shm_nattch,
680 shp->shm_perm.uid,
681 shp->shm_perm.gid,
682 shp->shm_perm.cuid,
683 shp->shm_perm.cgid,
684 shp->shm_atim,
685 shp->shm_dtim,
686 shp->shm_ctim);
687 shm_unlock(i);
688
689 pos += len;
690 if(pos < offset) {
691 len = 0;
692 begin = pos;
693 }
694 if(pos > offset + length)
695 goto done;
696 }
697 }
698 *eof = 1;
699 done:
700 up(&shm_ids.sem);
701 *start = buffer + (offset - begin);
702 len -= (offset - begin);
703 if(len > length)
704 len = length;
705 if(len < 0)
706 len = 0;
707 return len;
708 }
709 #endif
710
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.