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>
16 * support for audit of ipc object properties and permission changes
17 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
20 #include <linux/slab.h>
22 #include <linux/hugetlb.h>
23 #include <linux/shm.h>
24 #include <linux/init.h>
25 #include <linux/file.h>
26 #include <linux/mman.h>
27 #include <linux/shmem_fs.h>
28 #include <linux/security.h>
29 #include <linux/syscalls.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/ptrace.h>
33 #include <linux/seq_file.h>
34 #include <linux/mutex.h>
35 #include <linux/vs_base.h>
36 #include <linux/vs_context.h>
37 #include <linux/vs_limit.h>
39 #include <asm/uaccess.h>
43 static struct file_operations shm_file_operations;
44 static struct vm_operations_struct shm_vm_ops;
46 static struct ipc_ids shm_ids;
48 #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
49 #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
50 #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
51 #define shm_buildid(id, seq) \
52 ipc_buildid(&shm_ids, id, seq)
54 static int newseg (key_t key, int shmflg, size_t size);
55 static void shm_open (struct vm_area_struct *shmd);
56 static void shm_close (struct vm_area_struct *shmd);
58 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
61 size_t shm_ctlmax = SHMMAX;
62 size_t shm_ctlall = SHMALL;
63 int shm_ctlmni = SHMMNI;
65 static int shm_tot; /* total number of shared memory pages */
67 void __init shm_init (void)
69 ipc_init_ids(&shm_ids, 1);
70 ipc_init_proc_interface("sysvipc/shm",
71 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
73 sysvipc_shm_proc_show);
76 static inline int shm_checkid(struct shmid_kernel *s, int id)
78 if (ipc_checkid(&shm_ids,&s->shm_perm,id))
83 static inline struct shmid_kernel *shm_rmid(int id)
85 return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
88 static inline int shm_addid(struct shmid_kernel *shp)
90 return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni);
95 static inline void shm_inc (int id) {
96 struct shmid_kernel *shp;
100 shp->shm_atim = get_seconds();
101 shp->shm_lprid = current->tgid;
106 /* This is called by fork, once for every shm attach. */
107 static void shm_open (struct vm_area_struct *shmd)
109 shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
113 * shm_destroy - free the struct shmid_kernel
115 * @shp: struct to free
117 * It has to be called with shp and shm_ids.mutex locked,
118 * but returns with shp unlocked and freed.
120 static void shm_destroy (struct shmid_kernel *shp)
122 struct vx_info *vxi = lookup_vx_info(shp->shm_perm.xid);
123 int numpages = (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
125 vx_ipcshm_sub(vxi, shp, numpages);
130 if (!is_file_hugepages(shp->shm_file))
131 shmem_lock(shp->shm_file, 0, shp->mlock_user);
133 user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size,
135 fput (shp->shm_file);
136 security_shm_free(shp);
142 * remove the attach descriptor shmd.
143 * free memory for segment if it is marked destroyed.
144 * The descriptor has already been removed from the current->mm->mmap list
145 * and will later be kfree()d.
147 static void shm_close (struct vm_area_struct *shmd)
149 struct file * file = shmd->vm_file;
150 int id = file->f_dentry->d_inode->i_ino;
151 struct shmid_kernel *shp;
153 mutex_lock(&shm_ids.mutex);
154 /* remove from the list of attaches of the shm segment */
157 shp->shm_lprid = current->tgid;
158 shp->shm_dtim = get_seconds();
160 if(shp->shm_nattch == 0 &&
161 shp->shm_perm.mode & SHM_DEST)
165 mutex_unlock(&shm_ids.mutex);
168 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
172 ret = shmem_mmap(file, vma);
174 vma->vm_ops = &shm_vm_ops;
175 if (!(vma->vm_flags & VM_WRITE))
176 vma->vm_flags &= ~VM_MAYWRITE;
177 shm_inc(file->f_dentry->d_inode->i_ino);
183 static struct file_operations shm_file_operations = {
186 .get_unmapped_area = shmem_get_unmapped_area,
190 static struct vm_operations_struct shm_vm_ops = {
191 .open = shm_open, /* callback for a new vm-area open */
192 .close = shm_close, /* callback for when the vm-area is released */
193 .nopage = shmem_nopage,
194 #if defined(CONFIG_NUMA) && defined(CONFIG_SHMEM)
195 .set_policy = shmem_set_policy,
196 .get_policy = shmem_get_policy,
200 static int newseg (key_t key, int shmflg, size_t size)
203 struct shmid_kernel *shp;
204 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
209 if (size < SHMMIN || size > shm_ctlmax)
212 if (shm_tot + numpages >= shm_ctlall)
214 if (!vx_ipcshm_avail(current->vx_info, numpages))
217 shp = ipc_rcu_alloc(sizeof(*shp));
221 shp->shm_perm.key = key;
222 shp->shm_perm.xid = vx_current_xid();
223 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
224 shp->mlock_user = NULL;
226 shp->shm_perm.security = NULL;
227 error = security_shm_alloc(shp);
233 if (shmflg & SHM_HUGETLB) {
234 /* hugetlb_zero_setup takes care of mlock user accounting */
235 file = hugetlb_zero_setup(size);
236 shp->mlock_user = current->user;
238 int acctflag = VM_ACCOUNT;
240 * Do not allow no accounting for OVERCOMMIT_NEVER, even
243 if ((shmflg & SHM_NORESERVE) &&
244 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
246 sprintf (name, "SYSV%08x", key);
247 file = shmem_file_setup(name, size, acctflag);
249 error = PTR_ERR(file);
258 shp->shm_cprid = current->tgid;
260 shp->shm_atim = shp->shm_dtim = 0;
261 shp->shm_ctim = get_seconds();
262 shp->shm_segsz = size;
264 shp->id = shm_buildid(id,shp->shm_perm.seq);
265 shp->shm_file = file;
266 file->f_dentry->d_inode->i_ino = shp->id;
268 /* Hugetlb ops would have already been assigned. */
269 if (!(shmflg & SHM_HUGETLB))
270 file->f_op = &shm_file_operations;
273 vx_ipcshm_add(current->vx_info, key, numpages);
280 security_shm_free(shp);
285 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
287 struct shmid_kernel *shp;
290 mutex_lock(&shm_ids.mutex);
291 if (key == IPC_PRIVATE) {
292 err = newseg(key, shmflg, size);
293 } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
294 if (!(shmflg & IPC_CREAT))
297 err = newseg(key, shmflg, size);
298 } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
303 if (shp->shm_segsz < size)
305 else if (ipcperms(&shp->shm_perm, shmflg))
308 int shmid = shm_buildid(id, shp->shm_perm.seq);
309 err = security_shm_associate(shp, shmflg);
315 mutex_unlock(&shm_ids.mutex);
320 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
324 return copy_to_user(buf, in, sizeof(*in));
329 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
330 out.shm_segsz = in->shm_segsz;
331 out.shm_atime = in->shm_atime;
332 out.shm_dtime = in->shm_dtime;
333 out.shm_ctime = in->shm_ctime;
334 out.shm_cpid = in->shm_cpid;
335 out.shm_lpid = in->shm_lpid;
336 out.shm_nattch = in->shm_nattch;
338 return copy_to_user(buf, &out, sizeof(out));
351 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
356 struct shmid64_ds tbuf;
358 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
361 out->uid = tbuf.shm_perm.uid;
362 out->gid = tbuf.shm_perm.gid;
363 out->mode = tbuf.shm_perm.mode;
369 struct shmid_ds tbuf_old;
371 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
374 out->uid = tbuf_old.shm_perm.uid;
375 out->gid = tbuf_old.shm_perm.gid;
376 out->mode = tbuf_old.shm_perm.mode;
385 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
389 return copy_to_user(buf, in, sizeof(*in));
394 if(in->shmmax > INT_MAX)
395 out.shmmax = INT_MAX;
397 out.shmmax = (int)in->shmmax;
399 out.shmmin = in->shmmin;
400 out.shmmni = in->shmmni;
401 out.shmseg = in->shmseg;
402 out.shmall = in->shmall;
404 return copy_to_user(buf, &out, sizeof(out));
411 static void shm_get_stat(unsigned long *rss, unsigned long *swp)
418 for (i = 0; i <= shm_ids.max_id; i++) {
419 struct shmid_kernel *shp;
426 inode = shp->shm_file->f_dentry->d_inode;
428 if (is_file_hugepages(shp->shm_file)) {
429 struct address_space *mapping = inode->i_mapping;
430 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
432 struct shmem_inode_info *info = SHMEM_I(inode);
433 spin_lock(&info->lock);
434 *rss += inode->i_mapping->nrpages;
435 *swp += info->swapped;
436 spin_unlock(&info->lock);
441 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
443 struct shm_setbuf setbuf;
444 struct shmid_kernel *shp;
447 if (cmd < 0 || shmid < 0) {
452 version = ipc_parse_version(&cmd);
454 switch (cmd) { /* replace with proc interface ? */
457 struct shminfo64 shminfo;
459 err = security_shm_shmctl(NULL, cmd);
463 memset(&shminfo,0,sizeof(shminfo));
464 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
465 shminfo.shmmax = shm_ctlmax;
466 shminfo.shmall = shm_ctlall;
468 shminfo.shmmin = SHMMIN;
469 if(copy_shminfo_to_user (buf, &shminfo, version))
471 /* reading a integer is always atomic */
479 struct shm_info shm_info;
481 err = security_shm_shmctl(NULL, cmd);
485 memset(&shm_info,0,sizeof(shm_info));
486 mutex_lock(&shm_ids.mutex);
487 shm_info.used_ids = shm_ids.in_use;
488 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
489 shm_info.shm_tot = shm_tot;
490 shm_info.swap_attempts = 0;
491 shm_info.swap_successes = 0;
492 err = shm_ids.max_id;
493 mutex_unlock(&shm_ids.mutex);
494 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
499 err = err < 0 ? 0 : err;
505 struct shmid64_ds tbuf;
507 memset(&tbuf, 0, sizeof(tbuf));
508 shp = shm_lock(shmid);
512 } else if(cmd==SHM_STAT) {
514 if (shmid > shm_ids.max_id)
516 result = shm_buildid(shmid, shp->shm_perm.seq);
518 err = shm_checkid(shp,shmid);
524 if (ipcperms (&shp->shm_perm, S_IRUGO))
526 err = security_shm_shmctl(shp, cmd);
529 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
530 tbuf.shm_segsz = shp->shm_segsz;
531 tbuf.shm_atime = shp->shm_atim;
532 tbuf.shm_dtime = shp->shm_dtim;
533 tbuf.shm_ctime = shp->shm_ctim;
534 tbuf.shm_cpid = shp->shm_cprid;
535 tbuf.shm_lpid = shp->shm_lprid;
536 if (!is_file_hugepages(shp->shm_file))
537 tbuf.shm_nattch = shp->shm_nattch;
539 tbuf.shm_nattch = file_count(shp->shm_file) - 1;
541 if(copy_shmid_to_user (buf, &tbuf, version))
550 shp = shm_lock(shmid);
555 err = shm_checkid(shp,shmid);
559 err = audit_ipc_obj(&(shp->shm_perm));
563 if (!capable(CAP_IPC_LOCK)) {
565 if (current->euid != shp->shm_perm.uid &&
566 current->euid != shp->shm_perm.cuid)
568 if (cmd == SHM_LOCK &&
569 !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
573 err = security_shm_shmctl(shp, cmd);
578 struct user_struct * user = current->user;
579 if (!is_file_hugepages(shp->shm_file)) {
580 err = shmem_lock(shp->shm_file, 1, user);
582 shp->shm_perm.mode |= SHM_LOCKED;
583 shp->mlock_user = user;
586 } else if (!is_file_hugepages(shp->shm_file)) {
587 shmem_lock(shp->shm_file, 0, shp->mlock_user);
588 shp->shm_perm.mode &= ~SHM_LOCKED;
589 shp->mlock_user = NULL;
597 * We cannot simply remove the file. The SVID states
598 * that the block remains until the last person
599 * detaches from it, then is deleted. A shmat() on
600 * an RMID segment is legal in older Linux and if
601 * we change it apps break...
603 * Instead we set a destroyed flag, and then blow
604 * the name away when the usage hits zero.
606 mutex_lock(&shm_ids.mutex);
607 shp = shm_lock(shmid);
611 err = shm_checkid(shp, shmid);
615 err = audit_ipc_obj(&(shp->shm_perm));
619 if (current->euid != shp->shm_perm.uid &&
620 current->euid != shp->shm_perm.cuid &&
621 !capable(CAP_SYS_ADMIN)) {
626 err = security_shm_shmctl(shp, cmd);
630 if (shp->shm_nattch){
631 shp->shm_perm.mode |= SHM_DEST;
632 /* Do not find it any more */
633 shp->shm_perm.key = IPC_PRIVATE;
637 mutex_unlock(&shm_ids.mutex);
643 if (copy_shmid_from_user (&setbuf, buf, version)) {
647 mutex_lock(&shm_ids.mutex);
648 shp = shm_lock(shmid);
652 err = shm_checkid(shp,shmid);
655 err = audit_ipc_obj(&(shp->shm_perm));
658 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
662 if (current->euid != shp->shm_perm.uid &&
663 current->euid != shp->shm_perm.cuid &&
664 !capable(CAP_SYS_ADMIN)) {
668 err = security_shm_shmctl(shp, cmd);
672 shp->shm_perm.uid = setbuf.uid;
673 shp->shm_perm.gid = setbuf.gid;
674 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
675 | (setbuf.mode & S_IRWXUGO);
676 shp->shm_ctim = get_seconds();
689 mutex_unlock(&shm_ids.mutex);
698 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
700 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
701 * "raddr" thing points to kernel space, and there has to be a wrapper around
704 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
706 struct shmid_kernel *shp;
719 } else if ((addr = (ulong)shmaddr)) {
720 if (addr & (SHMLBA-1)) {
721 if (shmflg & SHM_RND)
722 addr &= ~(SHMLBA-1); /* round down */
724 #ifndef __ARCH_FORCE_SHMLBA
725 if (addr & ~PAGE_MASK)
729 flags = MAP_SHARED | MAP_FIXED;
731 if ((shmflg & SHM_REMAP))
737 if (shmflg & SHM_RDONLY) {
741 prot = PROT_READ | PROT_WRITE;
742 acc_mode = S_IRUGO | S_IWUGO;
744 if (shmflg & SHM_EXEC) {
750 * We cannot rely on the fs check since SYSV IPC does have an
751 * additional creator id...
753 shp = shm_lock(shmid);
758 err = shm_checkid(shp,shmid);
763 if (ipcperms(&shp->shm_perm, acc_mode)) {
769 err = security_shm_shmat(shp, shmaddr, shmflg);
775 file = shp->shm_file;
776 size = i_size_read(file->f_dentry->d_inode);
780 down_write(¤t->mm->mmap_sem);
781 if (addr && !(shmflg & SHM_REMAP)) {
782 user_addr = ERR_PTR(-EINVAL);
783 if (find_vma_intersection(current->mm, addr, addr + size))
786 * If shm segment goes below stack, make sure there is some
787 * space left for the stack to grow (at least 4 pages).
789 if (addr < current->mm->start_stack &&
790 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
794 user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
797 up_write(¤t->mm->mmap_sem);
799 mutex_lock(&shm_ids.mutex);
800 shp = shm_lock(shmid);
803 if(shp->shm_nattch == 0 &&
804 shp->shm_perm.mode & SHM_DEST)
808 mutex_unlock(&shm_ids.mutex);
810 *raddr = (unsigned long) user_addr;
812 if (IS_ERR(user_addr))
813 err = PTR_ERR(user_addr);
818 asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
823 err = do_shmat(shmid, shmaddr, shmflg, &ret);
826 force_successful_syscall_return();
831 * detach and kill segment if marked destroyed.
832 * The work is done in shm_close.
834 asmlinkage long sys_shmdt(char __user *shmaddr)
836 struct mm_struct *mm = current->mm;
837 struct vm_area_struct *vma, *next;
838 unsigned long addr = (unsigned long)shmaddr;
840 int retval = -EINVAL;
842 if (addr & ~PAGE_MASK)
845 down_write(&mm->mmap_sem);
848 * This function tries to be smart and unmap shm segments that
849 * were modified by partial mlock or munmap calls:
850 * - It first determines the size of the shm segment that should be
851 * unmapped: It searches for a vma that is backed by shm and that
852 * started at address shmaddr. It records it's size and then unmaps
854 * - Then it unmaps all shm vmas that started at shmaddr and that
855 * are within the initially determined size.
856 * Errors from do_munmap are ignored: the function only fails if
857 * it's called with invalid parameters or if it's called to unmap
858 * a part of a vma. Both calls in this function are for full vmas,
859 * the parameters are directly copied from the vma itself and always
860 * valid - therefore do_munmap cannot fail. (famous last words?)
863 * If it had been mremap()'d, the starting address would not
864 * match the usual checks anyway. So assume all vma's are
865 * above the starting address given.
867 vma = find_vma(mm, addr);
873 * Check if the starting address would match, i.e. it's
874 * a fragment created by mprotect() and/or munmap(), or it
875 * otherwise it starts at this address with no hassles.
877 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
878 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
881 size = vma->vm_file->f_dentry->d_inode->i_size;
882 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
884 * We discovered the size of the shm segment, so
885 * break out of here and fall through to the next
886 * loop that uses the size information to stop
887 * searching for matching vma's.
897 * We need look no further than the maximum address a fragment
898 * could possibly have landed at. Also cast things to loff_t to
899 * prevent overflows and make comparisions vs. equal-width types.
901 size = PAGE_ALIGN(size);
902 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
905 /* finding a matching vma now does not alter retval */
906 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
907 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
909 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
913 up_write(&mm->mmap_sem);
917 #ifdef CONFIG_PROC_FS
918 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
920 struct shmid_kernel *shp = it;
923 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
924 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
926 if (!vx_check(shp->shm_perm.xid, VX_IDENT))
929 if (sizeof(size_t) <= sizeof(int))
930 format = SMALL_STRING;
933 return seq_printf(s, format,
940 is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,