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>
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/vs_base.h>
31 #include <asm/uaccess.h>
35 #define shm_flags shm_perm.mode
37 static struct file_operations shm_file_operations;
38 static struct vm_operations_struct shm_vm_ops;
40 static struct ipc_ids shm_ids;
42 #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
43 #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
44 #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
45 #define shm_buildid(id, seq) \
46 ipc_buildid(&shm_ids, id, seq)
48 static int newseg (key_t key, int shmflg, size_t size);
49 static void shm_open (struct vm_area_struct *shmd);
50 static void shm_close (struct vm_area_struct *shmd);
52 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
55 size_t shm_ctlmax = SHMMAX;
56 size_t shm_ctlall = SHMALL;
57 int shm_ctlmni = SHMMNI;
59 static int shm_tot; /* total number of shared memory pages */
61 void __init shm_init (void)
63 ipc_init_ids(&shm_ids, 1);
65 create_proc_read_entry("sysvipc/shm", 0, 0, sysvipc_shm_read_proc, NULL);
69 static inline int shm_checkid(struct shmid_kernel *s, int id)
71 if (ipc_checkid(&shm_ids,&s->shm_perm,id))
76 static inline struct shmid_kernel *shm_rmid(int id)
78 return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
81 static inline int shm_addid(struct shmid_kernel *shp)
83 return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni+1);
88 static inline void shm_inc (int id) {
89 struct shmid_kernel *shp;
91 if(!(shp = shm_lock(id)))
93 shp->shm_atim = get_seconds();
94 shp->shm_lprid = current->tgid;
99 /* This is called by fork, once for every shm attach. */
100 static void shm_open (struct vm_area_struct *shmd)
102 shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
106 * shm_destroy - free the struct shmid_kernel
108 * @shp: struct to free
110 * It has to be called with shp and shm_ids.sem locked,
111 * but returns with shp unlocked and freed.
113 static void shm_destroy (struct shmid_kernel *shp)
115 shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
118 if (!is_file_hugepages(shp->shm_file))
119 shmem_lock(shp->shm_file, 0);
120 fput (shp->shm_file);
121 security_shm_free(shp);
122 ipc_rcu_free(shp, sizeof(struct shmid_kernel));
126 * remove the attach descriptor shmd.
127 * free memory for segment if it is marked destroyed.
128 * The descriptor has already been removed from the current->mm->mmap list
129 * and will later be kfree()d.
131 static void shm_close (struct vm_area_struct *shmd)
133 struct file * file = shmd->vm_file;
134 int id = file->f_dentry->d_inode->i_ino;
135 struct shmid_kernel *shp;
138 /* remove from the list of attaches of the shm segment */
139 if(!(shp = shm_lock(id)))
141 shp->shm_lprid = current->tgid;
142 shp->shm_dtim = get_seconds();
144 if(shp->shm_nattch == 0 &&
145 shp->shm_flags & SHM_DEST)
152 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
155 vma->vm_ops = &shm_vm_ops;
156 shm_inc(file->f_dentry->d_inode->i_ino);
160 static struct file_operations shm_file_operations = {
164 static struct vm_operations_struct shm_vm_ops = {
165 .open = shm_open, /* callback for a new vm-area open */
166 .close = shm_close, /* callback for when the vm-area is released */
167 .nopage = shmem_nopage,
169 .set_policy = shmem_set_policy,
170 .get_policy = shmem_get_policy,
174 static int newseg (key_t key, int shmflg, size_t size)
177 struct shmid_kernel *shp;
178 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
183 if (size < SHMMIN || size > shm_ctlmax)
186 if (shm_tot + numpages >= shm_ctlall)
189 shp = ipc_rcu_alloc(sizeof(*shp));
193 shp->shm_perm.key = key;
194 shp->shm_perm.xid = current->xid;
195 shp->shm_flags = (shmflg & S_IRWXUGO);
197 shp->shm_perm.security = NULL;
198 error = security_shm_alloc(shp);
200 ipc_rcu_free(shp, sizeof(*shp));
204 if (shmflg & SHM_HUGETLB)
205 file = hugetlb_zero_setup(size);
207 sprintf (name, "SYSV%08x", key);
208 file = shmem_file_setup(name, size, VM_ACCOUNT);
210 error = PTR_ERR(file);
219 shp->shm_cprid = current->tgid;
221 shp->shm_atim = shp->shm_dtim = 0;
222 shp->shm_ctim = get_seconds();
223 shp->shm_segsz = size;
225 shp->id = shm_buildid(id,shp->shm_perm.seq);
226 shp->shm_file = file;
227 file->f_dentry->d_inode->i_ino = shp->id;
228 if (shmflg & SHM_HUGETLB)
229 set_file_hugepages(file);
231 file->f_op = &shm_file_operations;
239 security_shm_free(shp);
240 ipc_rcu_free(shp, sizeof(*shp));
244 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
246 struct shmid_kernel *shp;
250 if (key == IPC_PRIVATE) {
251 err = newseg(key, shmflg, size);
252 } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
253 if (!(shmflg & IPC_CREAT))
256 err = newseg(key, shmflg, size);
257 } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
263 if (shp->shm_segsz < size)
265 else if (ipcperms(&shp->shm_perm, shmflg))
268 int shmid = shm_buildid(id, shp->shm_perm.seq);
269 err = security_shm_associate(shp, shmflg);
280 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
284 return copy_to_user(buf, in, sizeof(*in));
289 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
290 out.shm_segsz = in->shm_segsz;
291 out.shm_atime = in->shm_atime;
292 out.shm_dtime = in->shm_dtime;
293 out.shm_ctime = in->shm_ctime;
294 out.shm_cpid = in->shm_cpid;
295 out.shm_lpid = in->shm_lpid;
296 out.shm_nattch = in->shm_nattch;
298 return copy_to_user(buf, &out, sizeof(out));
311 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
316 struct shmid64_ds tbuf;
318 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
321 out->uid = tbuf.shm_perm.uid;
322 out->gid = tbuf.shm_perm.gid;
323 out->mode = tbuf.shm_flags;
329 struct shmid_ds tbuf_old;
331 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
334 out->uid = tbuf_old.shm_perm.uid;
335 out->gid = tbuf_old.shm_perm.gid;
336 out->mode = tbuf_old.shm_flags;
345 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
349 return copy_to_user(buf, in, sizeof(*in));
354 if(in->shmmax > INT_MAX)
355 out.shmmax = INT_MAX;
357 out.shmmax = (int)in->shmmax;
359 out.shmmin = in->shmmin;
360 out.shmmni = in->shmmni;
361 out.shmseg = in->shmseg;
362 out.shmall = in->shmall;
364 return copy_to_user(buf, &out, sizeof(out));
371 static void shm_get_stat(unsigned long *rss, unsigned long *swp)
378 for (i = 0; i <= shm_ids.max_id; i++) {
379 struct shmid_kernel *shp;
386 inode = shp->shm_file->f_dentry->d_inode;
388 if (is_file_hugepages(shp->shm_file)) {
389 struct address_space *mapping = inode->i_mapping;
390 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
392 struct shmem_inode_info *info = SHMEM_I(inode);
393 spin_lock(&info->lock);
394 *rss += inode->i_mapping->nrpages;
395 *swp += info->swapped;
396 spin_unlock(&info->lock);
401 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
403 struct shm_setbuf setbuf;
404 struct shmid_kernel *shp;
407 if (cmd < 0 || shmid < 0) {
412 version = ipc_parse_version(&cmd);
414 switch (cmd) { /* replace with proc interface ? */
417 struct shminfo64 shminfo;
419 err = security_shm_shmctl(NULL, cmd);
423 memset(&shminfo,0,sizeof(shminfo));
424 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
425 shminfo.shmmax = shm_ctlmax;
426 shminfo.shmall = shm_ctlall;
428 shminfo.shmmin = SHMMIN;
429 if(copy_shminfo_to_user (buf, &shminfo, version))
431 /* reading a integer is always atomic */
439 struct shm_info shm_info;
441 err = security_shm_shmctl(NULL, cmd);
445 memset(&shm_info,0,sizeof(shm_info));
447 shm_info.used_ids = shm_ids.in_use;
448 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
449 shm_info.shm_tot = shm_tot;
450 shm_info.swap_attempts = 0;
451 shm_info.swap_successes = 0;
452 err = shm_ids.max_id;
454 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
459 err = err < 0 ? 0 : err;
465 struct shmid64_ds tbuf;
467 memset(&tbuf, 0, sizeof(tbuf));
468 shp = shm_lock(shmid);
472 } else if(cmd==SHM_STAT) {
474 if (shmid > shm_ids.max_id)
476 result = shm_buildid(shmid, shp->shm_perm.seq);
478 err = shm_checkid(shp,shmid);
484 if (ipcperms (&shp->shm_perm, S_IRUGO))
486 err = security_shm_shmctl(shp, cmd);
489 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
490 tbuf.shm_segsz = shp->shm_segsz;
491 tbuf.shm_atime = shp->shm_atim;
492 tbuf.shm_dtime = shp->shm_dtim;
493 tbuf.shm_ctime = shp->shm_ctim;
494 tbuf.shm_cpid = shp->shm_cprid;
495 tbuf.shm_lpid = shp->shm_lprid;
496 if (!is_file_hugepages(shp->shm_file))
497 tbuf.shm_nattch = shp->shm_nattch;
499 tbuf.shm_nattch = file_count(shp->shm_file) - 1;
501 if(copy_shmid_to_user (buf, &tbuf, version))
510 /* Allow superuser to lock segment in memory */
511 /* Should the pages be faulted in here or leave it to user? */
512 /* need to determine interaction with current->swappable */
513 if (!capable(CAP_IPC_LOCK)) {
518 shp = shm_lock(shmid);
523 err = shm_checkid(shp,shmid);
527 err = security_shm_shmctl(shp, cmd);
532 if (!is_file_hugepages(shp->shm_file))
533 shmem_lock(shp->shm_file, 1);
534 shp->shm_flags |= SHM_LOCKED;
536 if (!is_file_hugepages(shp->shm_file))
537 shmem_lock(shp->shm_file, 0);
538 shp->shm_flags &= ~SHM_LOCKED;
546 * We cannot simply remove the file. The SVID states
547 * that the block remains until the last person
548 * detaches from it, then is deleted. A shmat() on
549 * an RMID segment is legal in older Linux and if
550 * we change it apps break...
552 * Instead we set a destroyed flag, and then blow
553 * the name away when the usage hits zero.
556 shp = shm_lock(shmid);
560 err = shm_checkid(shp, shmid);
564 if (current->euid != shp->shm_perm.uid &&
565 current->euid != shp->shm_perm.cuid &&
566 !capable(CAP_SYS_ADMIN)) {
571 err = security_shm_shmctl(shp, cmd);
575 if (shp->shm_nattch){
576 shp->shm_flags |= SHM_DEST;
577 /* Do not find it any more */
578 shp->shm_perm.key = IPC_PRIVATE;
588 if (copy_shmid_from_user (&setbuf, buf, version)) {
593 shp = shm_lock(shmid);
597 err = shm_checkid(shp,shmid);
601 if (current->euid != shp->shm_perm.uid &&
602 current->euid != shp->shm_perm.cuid &&
603 !capable(CAP_SYS_ADMIN)) {
607 err = security_shm_shmctl(shp, cmd);
611 shp->shm_perm.uid = setbuf.uid;
612 shp->shm_perm.gid = setbuf.gid;
613 shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
614 | (setbuf.mode & S_IRWXUGO);
615 shp->shm_ctim = get_seconds();
637 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
639 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
640 * "raddr" thing points to kernel space, and there has to be a wrapper around
643 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
645 struct shmid_kernel *shp;
652 unsigned long o_flags;
659 } else if ((addr = (ulong)shmaddr)) {
660 if (addr & (SHMLBA-1)) {
661 if (shmflg & SHM_RND)
662 addr &= ~(SHMLBA-1); /* round down */
664 #ifndef __ARCH_FORCE_SHMLBA
665 if (addr & ~PAGE_MASK)
669 flags = MAP_SHARED | MAP_FIXED;
671 if ((shmflg & SHM_REMAP))
677 if (shmflg & SHM_RDONLY) {
682 prot = PROT_READ | PROT_WRITE;
684 acc_mode = S_IRUGO | S_IWUGO;
688 * We cannot rely on the fs check since SYSV IPC does have an
689 * additional creator id...
691 shp = shm_lock(shmid);
696 err = shm_checkid(shp,shmid);
701 if (ipcperms(&shp->shm_perm, acc_mode)) {
707 err = security_shm_shmat(shp, shmaddr, shmflg);
713 file = shp->shm_file;
714 size = i_size_read(file->f_dentry->d_inode);
718 down_write(¤t->mm->mmap_sem);
719 if (addr && !(shmflg & SHM_REMAP)) {
720 user_addr = ERR_PTR(-EINVAL);
721 if (find_vma_intersection(current->mm, addr, addr + size))
724 * If shm segment goes below stack, make sure there is some
725 * space left for the stack to grow (at least 4 pages).
727 if (addr < current->mm->start_stack &&
728 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
732 user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
735 up_write(¤t->mm->mmap_sem);
738 if(!(shp = shm_lock(shmid)))
741 if(shp->shm_nattch == 0 &&
742 shp->shm_flags & SHM_DEST)
748 *raddr = (unsigned long) user_addr;
750 if (IS_ERR(user_addr))
751 err = PTR_ERR(user_addr);
757 * detach and kill segment if marked destroyed.
758 * The work is done in shm_close.
760 asmlinkage long sys_shmdt(char __user *shmaddr)
762 struct mm_struct *mm = current->mm;
763 struct vm_area_struct *vma, *next;
764 unsigned long addr = (unsigned long)shmaddr;
766 int retval = -EINVAL;
768 down_write(&mm->mmap_sem);
771 * This function tries to be smart and unmap shm segments that
772 * were modified by partial mlock or munmap calls:
773 * - It first determines the size of the shm segment that should be
774 * unmapped: It searches for a vma that is backed by shm and that
775 * started at address shmaddr. It records it's size and then unmaps
777 * - Then it unmaps all shm vmas that started at shmaddr and that
778 * are within the initially determined size.
779 * Errors from do_munmap are ignored: the function only fails if
780 * it's called with invalid parameters or if it's called to unmap
781 * a part of a vma. Both calls in this function are for full vmas,
782 * the parameters are directly copied from the vma itself and always
783 * valid - therefore do_munmap cannot fail. (famous last words?)
786 * If it had been mremap()'d, the starting address would not
787 * match the usual checks anyway. So assume all vma's are
788 * above the starting address given.
790 vma = find_vma(mm, addr);
796 * Check if the starting address would match, i.e. it's
797 * a fragment created by mprotect() and/or munmap(), or it
798 * otherwise it starts at this address with no hassles.
800 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
801 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
804 size = vma->vm_file->f_dentry->d_inode->i_size;
805 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
807 * We discovered the size of the shm segment, so
808 * break out of here and fall through to the next
809 * loop that uses the size information to stop
810 * searching for matching vma's.
820 * We need look no further than the maximum address a fragment
821 * could possibly have landed at. Also cast things to loff_t to
822 * prevent overflows and make comparisions vs. equal-width types.
824 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
827 /* finding a matching vma now does not alter retval */
828 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
829 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
831 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
835 up_write(&mm->mmap_sem);
839 #ifdef CONFIG_PROC_FS
840 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
847 len += sprintf(buffer, " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n");
849 for(i = 0; i <= shm_ids.max_id; i++) {
850 struct shmid_kernel* shp;
854 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
855 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
858 if (!vx_check(shp->shm_perm.xid, VX_IDENT)) {
862 if (sizeof(size_t) <= sizeof(int))
863 format = SMALL_STRING;
866 len += sprintf(buffer + len, format,
868 shm_buildid(i, shp->shm_perm.seq),
873 is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
888 if(pos > offset + length)
895 *start = buffer + (offset - begin);
896 len -= (offset - begin);