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.
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>
18 #include <linux/config.h>
19 #include <linux/slab.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>
29 struct shmid_kernel /* private to the kernel */
31 struct kern_ipc_perm shm_perm;
32 struct file * shm_file;
34 unsigned long shm_nattch;
35 unsigned long shm_segsz;
43 #define shm_flags shm_perm.mode
45 static struct file_operations shm_file_operations;
46 static struct vm_operations_struct shm_vm_ops;
48 static struct ipc_ids shm_ids;
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)
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);
62 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
65 size_t shm_ctlmax = SHMMAX;
66 size_t shm_ctlall = SHMALL;
67 int shm_ctlmni = SHMMNI;
69 static int shm_tot; /* total number of shared memory pages */
71 void __init shm_init (void)
73 ipc_init_ids(&shm_ids, 1);
75 create_proc_read_entry("sysvipc/shm", 0, 0, sysvipc_shm_read_proc, NULL);
79 static inline int shm_checkid(struct shmid_kernel *s, int id)
81 if (ipc_checkid(&shm_ids,&s->shm_perm,id))
86 static inline struct shmid_kernel *shm_rmid(int id)
88 return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
91 static inline int shm_addid(struct shmid_kernel *shp)
93 return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni+1);
98 static inline void shm_inc (int id) {
99 struct shmid_kernel *shp;
101 if(!(shp = shm_lock(id)))
103 shp->shm_atim = CURRENT_TIME;
104 shp->shm_lprid = current->pid;
109 /* This is called by fork, once for every shm attach. */
110 static void shm_open (struct vm_area_struct *shmd)
112 shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
116 * shm_destroy - free the struct shmid_kernel
118 * @shp: struct to free
120 * It has to be called with shp and shm_ids.sem locked,
121 * but returns with shp unlocked and freed.
123 static void shm_destroy (struct shmid_kernel *shp)
125 shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
128 shmem_lock(shp->shm_file, 0);
129 fput (shp->shm_file);
134 * remove the attach descriptor shmd.
135 * free memory for segment if it is marked destroyed.
136 * The descriptor has already been removed from the current->mm->mmap list
137 * and will later be kfree()d.
139 static void shm_close (struct vm_area_struct *shmd)
141 struct file * file = shmd->vm_file;
142 int id = file->f_dentry->d_inode->i_ino;
143 struct shmid_kernel *shp;
146 /* remove from the list of attaches of the shm segment */
147 if(!(shp = shm_lock(id)))
149 shp->shm_lprid = current->pid;
150 shp->shm_dtim = CURRENT_TIME;
152 if(shp->shm_nattch == 0 &&
153 shp->shm_flags & SHM_DEST)
160 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
162 UPDATE_ATIME(file->f_dentry->d_inode);
163 vma->vm_ops = &shm_vm_ops;
164 shm_inc(file->f_dentry->d_inode->i_ino);
168 static struct file_operations shm_file_operations = {
172 static struct vm_operations_struct shm_vm_ops = {
173 open: shm_open, /* callback for a new vm-area open */
174 close: shm_close, /* callback for when the vm-area is released */
175 nopage: shmem_nopage,
178 static int newseg (key_t key, int shmflg, size_t size)
181 struct shmid_kernel *shp;
182 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
187 if (size < SHMMIN || size > shm_ctlmax)
190 if (shm_tot + numpages >= shm_ctlall)
193 shp = (struct shmid_kernel *) kmalloc (sizeof (*shp), GFP_USER);
196 sprintf (name, "SYSV%08x", key);
197 file = shmem_file_setup(name, size);
198 error = PTR_ERR(file);
206 shp->shm_perm.key = key;
207 shp->shm_flags = (shmflg & S_IRWXUGO);
208 shp->shm_cprid = current->pid;
210 shp->shm_atim = shp->shm_dtim = 0;
211 shp->shm_ctim = CURRENT_TIME;
212 shp->shm_segsz = size;
214 shp->id = shm_buildid(id,shp->shm_perm.seq);
215 shp->shm_file = file;
216 file->f_dentry->d_inode->i_ino = shp->id;
217 file->f_op = &shm_file_operations;
229 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
231 struct shmid_kernel *shp;
235 if (key == IPC_PRIVATE) {
236 err = newseg(key, shmflg, size);
237 } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
238 if (!(shmflg & IPC_CREAT))
241 err = newseg(key, shmflg, size);
242 } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
248 if (shp->shm_segsz < size)
250 else if (ipcperms(&shp->shm_perm, shmflg))
253 err = shm_buildid(id, shp->shm_perm.seq);
260 static inline unsigned long copy_shmid_to_user(void *buf, struct shmid64_ds *in, int version)
264 return copy_to_user(buf, in, sizeof(*in));
269 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
270 out.shm_segsz = in->shm_segsz;
271 out.shm_atime = in->shm_atime;
272 out.shm_dtime = in->shm_dtime;
273 out.shm_ctime = in->shm_ctime;
274 out.shm_cpid = in->shm_cpid;
275 out.shm_lpid = in->shm_lpid;
276 out.shm_nattch = in->shm_nattch;
278 return copy_to_user(buf, &out, sizeof(out));
291 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void *buf, int version)
296 struct shmid64_ds tbuf;
298 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
301 out->uid = tbuf.shm_perm.uid;
302 out->gid = tbuf.shm_perm.gid;
303 out->mode = tbuf.shm_flags;
309 struct shmid_ds tbuf_old;
311 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
314 out->uid = tbuf_old.shm_perm.uid;
315 out->gid = tbuf_old.shm_perm.gid;
316 out->mode = tbuf_old.shm_flags;
325 static inline unsigned long copy_shminfo_to_user(void *buf, struct shminfo64 *in, int version)
329 return copy_to_user(buf, in, sizeof(*in));
334 if(in->shmmax > INT_MAX)
335 out.shmmax = INT_MAX;
337 out.shmmax = (int)in->shmmax;
339 out.shmmin = in->shmmin;
340 out.shmmni = in->shmmni;
341 out.shmseg = in->shmseg;
342 out.shmall = in->shmall;
344 return copy_to_user(buf, &out, sizeof(out));
351 static void shm_get_stat (unsigned long *rss, unsigned long *swp)
353 struct shmem_inode_info *info;
359 for(i = 0; i <= shm_ids.max_id; i++) {
360 struct shmid_kernel* shp;
361 struct inode * inode;
366 inode = shp->shm_file->f_dentry->d_inode;
367 info = SHMEM_I(inode);
368 spin_lock (&info->lock);
369 *rss += inode->i_mapping->nrpages;
370 *swp += info->swapped;
371 spin_unlock (&info->lock);
375 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds *buf)
377 struct shm_setbuf setbuf;
378 struct shmid_kernel *shp;
381 if (cmd < 0 || shmid < 0)
384 version = ipc_parse_version(&cmd);
386 switch (cmd) { /* replace with proc interface ? */
389 struct shminfo64 shminfo;
391 memset(&shminfo,0,sizeof(shminfo));
392 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
393 shminfo.shmmax = shm_ctlmax;
394 shminfo.shmall = shm_ctlall;
396 shminfo.shmmin = SHMMIN;
397 if(copy_shminfo_to_user (buf, &shminfo, version))
399 /* reading a integer is always atomic */
407 struct shm_info shm_info;
409 memset(&shm_info,0,sizeof(shm_info));
412 shm_info.used_ids = shm_ids.in_use;
413 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
414 shm_info.shm_tot = shm_tot;
415 shm_info.swap_attempts = 0;
416 shm_info.swap_successes = 0;
417 err = shm_ids.max_id;
420 if(copy_to_user (buf, &shm_info, sizeof(shm_info)))
423 return err < 0 ? 0 : err;
428 struct shmid64_ds tbuf;
430 memset(&tbuf, 0, sizeof(tbuf));
431 shp = shm_lock(shmid);
436 if (shmid > shm_ids.max_id)
438 result = shm_buildid(shmid, shp->shm_perm.seq);
440 err = shm_checkid(shp,shmid);
446 if (ipcperms (&shp->shm_perm, S_IRUGO))
448 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
449 tbuf.shm_segsz = shp->shm_segsz;
450 tbuf.shm_atime = shp->shm_atim;
451 tbuf.shm_dtime = shp->shm_dtim;
452 tbuf.shm_ctime = shp->shm_ctim;
453 tbuf.shm_cpid = shp->shm_cprid;
454 tbuf.shm_lpid = shp->shm_lprid;
455 tbuf.shm_nattch = shp->shm_nattch;
457 if(copy_shmid_to_user (buf, &tbuf, version))
464 /* Allow superuser to lock segment in memory */
465 /* Should the pages be faulted in here or leave it to user? */
466 /* need to determine interaction with current->swappable */
467 if (!capable(CAP_IPC_LOCK))
470 shp = shm_lock(shmid);
473 err = shm_checkid(shp,shmid);
477 shmem_lock(shp->shm_file, 1);
478 shp->shm_flags |= SHM_LOCKED;
480 shmem_lock(shp->shm_file, 0);
481 shp->shm_flags &= ~SHM_LOCKED;
489 * We cannot simply remove the file. The SVID states
490 * that the block remains until the last person
491 * detaches from it, then is deleted. A shmat() on
492 * an RMID segment is legal in older Linux and if
493 * we change it apps break...
495 * Instead we set a destroyed flag, and then blow
496 * the name away when the usage hits zero.
499 shp = shm_lock(shmid);
503 err = shm_checkid(shp, shmid);
506 if (current->euid != shp->shm_perm.uid &&
507 current->euid != shp->shm_perm.cuid &&
508 !capable(CAP_SYS_ADMIN)) {
512 if (shp->shm_nattch){
513 shp->shm_flags |= SHM_DEST;
514 /* Do not find it any more */
515 shp->shm_perm.key = IPC_PRIVATE;
525 if(copy_shmid_from_user (&setbuf, buf, version))
528 shp = shm_lock(shmid);
532 err = shm_checkid(shp,shmid);
536 if (current->euid != shp->shm_perm.uid &&
537 current->euid != shp->shm_perm.cuid &&
538 !capable(CAP_SYS_ADMIN)) {
542 shp->shm_perm.uid = setbuf.uid;
543 shp->shm_perm.gid = setbuf.gid;
544 shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
545 | (setbuf.mode & S_IRWXUGO);
546 shp->shm_ctim = CURRENT_TIME;
566 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
568 asmlinkage long sys_shmat (int shmid, char *shmaddr, int shmflg, ulong *raddr)
570 struct shmid_kernel *shp;
577 unsigned long o_flags;
584 if ((addr = (ulong)shmaddr)) {
585 if (addr & (SHMLBA-1)) {
586 if (shmflg & SHM_RND)
587 addr &= ~(SHMLBA-1); /* round down */
591 flags = MAP_SHARED | MAP_FIXED;
593 if ((shmflg & SHM_REMAP))
599 if (shmflg & SHM_RDONLY) {
604 prot = PROT_READ | PROT_WRITE;
606 acc_mode = S_IRUGO | S_IWUGO;
610 * We cannot rely on the fs check since SYSV IPC does have an
611 * additional creator id...
613 shp = shm_lock(shmid);
616 err = shm_checkid(shp,shmid);
621 if (ipcperms(&shp->shm_perm, acc_mode)) {
625 file = shp->shm_file;
626 size = file->f_dentry->d_inode->i_size;
630 down_write(¤t->mm->mmap_sem);
631 if (addr && !(shmflg & SHM_REMAP)) {
632 user_addr = ERR_PTR(-EINVAL);
633 if (find_vma_intersection(current->mm, addr, addr + size))
636 * If shm segment goes below stack, make sure there is some
637 * space left for the stack to grow (at least 4 pages).
639 if (addr < current->mm->start_stack &&
640 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
644 user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
647 up_write(¤t->mm->mmap_sem);
650 if(!(shp = shm_lock(shmid)))
653 if(shp->shm_nattch == 0 &&
654 shp->shm_flags & SHM_DEST)
660 *raddr = (unsigned long) user_addr;
662 if (IS_ERR(user_addr))
663 err = PTR_ERR(user_addr);
669 * detach and kill segment if marked destroyed.
670 * The work is done in shm_close.
672 asmlinkage long sys_shmdt (char *shmaddr)
674 struct mm_struct *mm = current->mm;
675 struct vm_area_struct *shmd, *shmdnext;
676 int retval = -EINVAL;
678 down_write(&mm->mmap_sem);
679 for (shmd = mm->mmap; shmd; shmd = shmdnext) {
680 shmdnext = shmd->vm_next;
681 if (shmd->vm_ops == &shm_vm_ops
682 && shmd->vm_start - (shmd->vm_pgoff << PAGE_SHIFT) == (ulong) shmaddr) {
683 do_munmap(mm, shmd->vm_start, shmd->vm_end - shmd->vm_start);
687 up_write(&mm->mmap_sem);
691 #ifdef CONFIG_PROC_FS
692 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
699 len += sprintf(buffer, " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n");
701 for(i = 0; i <= shm_ids.max_id; i++) {
702 struct shmid_kernel* shp;
706 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
707 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
710 if (sizeof(size_t) <= sizeof(int))
711 format = SMALL_STRING;
714 len += sprintf(buffer + len, format,
716 shm_buildid(i, shp->shm_perm.seq),
736 if(pos > offset + length)
743 *start = buffer + (offset - begin);
744 len -= (offset - begin);