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/syscalls.h>
30 #include <linux/vs_context.h>
31 #include <linux/vs_limit.h>
32 #include <asm/uaccess.h>
36 #define shm_flags shm_perm.mode
38 static struct file_operations shm_file_operations;
39 static struct vm_operations_struct shm_vm_ops;
41 static struct ipc_ids shm_ids;
43 #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
44 #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
45 #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
46 #define shm_buildid(id, seq) \
47 ipc_buildid(&shm_ids, id, seq)
49 static int newseg (key_t key, int shmflg, size_t size);
50 static void shm_open (struct vm_area_struct *shmd);
51 static void shm_close (struct vm_area_struct *shmd);
53 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
56 size_t shm_ctlmax = SHMMAX;
57 size_t shm_ctlall = SHMALL;
58 int shm_ctlmni = SHMMNI;
60 static int shm_tot; /* total number of shared memory pages */
62 void __init shm_init (void)
64 ipc_init_ids(&shm_ids, 1);
66 create_proc_read_entry("sysvipc/shm", 0, NULL, sysvipc_shm_read_proc, NULL);
70 static inline int shm_checkid(struct shmid_kernel *s, int id)
72 if (ipc_checkid(&shm_ids,&s->shm_perm,id))
77 static inline struct shmid_kernel *shm_rmid(int id)
79 return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
82 static inline int shm_addid(struct shmid_kernel *shp)
84 return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni);
89 static inline void shm_inc (int id) {
90 struct shmid_kernel *shp;
92 if(!(shp = shm_lock(id)))
94 shp->shm_atim = get_seconds();
95 shp->shm_lprid = current->tgid;
100 /* This is called by fork, once for every shm attach. */
101 static void shm_open (struct vm_area_struct *shmd)
103 shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
107 * shm_destroy - free the struct shmid_kernel
109 * @shp: struct to free
111 * It has to be called with shp and shm_ids.sem locked,
112 * but returns with shp unlocked and freed.
114 static void shm_destroy (struct shmid_kernel *shp)
116 struct vx_info *vxi = locate_vx_info(shp->shm_perm.xid);
117 int numpages = (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
119 vx_ipcshm_sub(vxi, shp, numpages);
124 if (!is_file_hugepages(shp->shm_file))
125 shmem_lock(shp->shm_file, 0, shp->mlock_user);
127 user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size,
129 fput (shp->shm_file);
130 security_shm_free(shp);
136 * remove the attach descriptor shmd.
137 * free memory for segment if it is marked destroyed.
138 * The descriptor has already been removed from the current->mm->mmap list
139 * and will later be kfree()d.
141 static void shm_close (struct vm_area_struct *shmd)
143 struct file * file = shmd->vm_file;
144 int id = file->f_dentry->d_inode->i_ino;
145 struct shmid_kernel *shp;
148 /* remove from the list of attaches of the shm segment */
149 if(!(shp = shm_lock(id)))
151 shp->shm_lprid = current->tgid;
152 shp->shm_dtim = get_seconds();
154 if(shp->shm_nattch == 0 &&
155 shp->shm_flags & SHM_DEST)
162 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
165 vma->vm_ops = &shm_vm_ops;
166 shm_inc(file->f_dentry->d_inode->i_ino);
170 static struct file_operations shm_file_operations = {
174 static struct vm_operations_struct shm_vm_ops = {
175 .open = shm_open, /* callback for a new vm-area open */
176 .close = shm_close, /* callback for when the vm-area is released */
177 .nopage = shmem_nopage,
179 .set_policy = shmem_set_policy,
180 .get_policy = shmem_get_policy,
184 static int newseg (key_t key, int shmflg, size_t size)
187 struct shmid_kernel *shp;
188 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
193 if (size < SHMMIN || size > shm_ctlmax)
196 if (shm_tot + numpages >= shm_ctlall)
198 if (!vx_ipcshm_avail(current->vx_info, numpages))
201 shp = ipc_rcu_alloc(sizeof(*shp));
205 shp->shm_perm.key = key;
206 shp->shm_perm.xid = vx_current_xid();
207 shp->shm_flags = (shmflg & S_IRWXUGO);
208 shp->mlock_user = NULL;
210 shp->shm_perm.security = NULL;
211 error = security_shm_alloc(shp);
217 if (shmflg & SHM_HUGETLB) {
218 /* hugetlb_zero_setup takes care of mlock user accounting */
219 file = hugetlb_zero_setup(size);
220 shp->mlock_user = current->user;
222 sprintf (name, "SYSV%08x", key);
223 file = shmem_file_setup(name, size, VM_ACCOUNT);
225 error = PTR_ERR(file);
234 shp->shm_cprid = current->tgid;
236 shp->shm_atim = shp->shm_dtim = 0;
237 shp->shm_ctim = get_seconds();
238 shp->shm_segsz = size;
240 shp->id = shm_buildid(id,shp->shm_perm.seq);
241 shp->shm_file = file;
242 file->f_dentry->d_inode->i_ino = shp->id;
243 if (shmflg & SHM_HUGETLB)
244 set_file_hugepages(file);
246 file->f_op = &shm_file_operations;
248 vx_ipcshm_add(current->vx_info, key, numpages);
255 security_shm_free(shp);
260 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
262 struct shmid_kernel *shp;
266 if (key == IPC_PRIVATE) {
267 err = newseg(key, shmflg, size);
268 } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
269 if (!(shmflg & IPC_CREAT))
272 err = newseg(key, shmflg, size);
273 } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
279 if (shp->shm_segsz < size)
281 else if (ipcperms(&shp->shm_perm, shmflg))
284 int shmid = shm_buildid(id, shp->shm_perm.seq);
285 err = security_shm_associate(shp, shmflg);
296 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
300 return copy_to_user(buf, in, sizeof(*in));
305 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
306 out.shm_segsz = in->shm_segsz;
307 out.shm_atime = in->shm_atime;
308 out.shm_dtime = in->shm_dtime;
309 out.shm_ctime = in->shm_ctime;
310 out.shm_cpid = in->shm_cpid;
311 out.shm_lpid = in->shm_lpid;
312 out.shm_nattch = in->shm_nattch;
314 return copy_to_user(buf, &out, sizeof(out));
327 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
332 struct shmid64_ds tbuf;
334 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
337 out->uid = tbuf.shm_perm.uid;
338 out->gid = tbuf.shm_perm.gid;
339 out->mode = tbuf.shm_flags;
345 struct shmid_ds tbuf_old;
347 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
350 out->uid = tbuf_old.shm_perm.uid;
351 out->gid = tbuf_old.shm_perm.gid;
352 out->mode = tbuf_old.shm_flags;
361 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
365 return copy_to_user(buf, in, sizeof(*in));
370 if(in->shmmax > INT_MAX)
371 out.shmmax = INT_MAX;
373 out.shmmax = (int)in->shmmax;
375 out.shmmin = in->shmmin;
376 out.shmmni = in->shmmni;
377 out.shmseg = in->shmseg;
378 out.shmall = in->shmall;
380 return copy_to_user(buf, &out, sizeof(out));
387 static void shm_get_stat(unsigned long *rss, unsigned long *swp)
394 for (i = 0; i <= shm_ids.max_id; i++) {
395 struct shmid_kernel *shp;
402 inode = shp->shm_file->f_dentry->d_inode;
404 if (is_file_hugepages(shp->shm_file)) {
405 struct address_space *mapping = inode->i_mapping;
406 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
408 struct shmem_inode_info *info = SHMEM_I(inode);
409 spin_lock(&info->lock);
410 *rss += inode->i_mapping->nrpages;
411 *swp += info->swapped;
412 spin_unlock(&info->lock);
417 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
419 struct shm_setbuf setbuf;
420 struct shmid_kernel *shp;
423 if (cmd < 0 || shmid < 0) {
428 version = ipc_parse_version(&cmd);
430 switch (cmd) { /* replace with proc interface ? */
433 struct shminfo64 shminfo;
435 err = security_shm_shmctl(NULL, cmd);
439 memset(&shminfo,0,sizeof(shminfo));
440 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
441 shminfo.shmmax = shm_ctlmax;
442 shminfo.shmall = shm_ctlall;
444 shminfo.shmmin = SHMMIN;
445 if(copy_shminfo_to_user (buf, &shminfo, version))
447 /* reading a integer is always atomic */
455 struct shm_info shm_info;
457 err = security_shm_shmctl(NULL, cmd);
461 memset(&shm_info,0,sizeof(shm_info));
463 shm_info.used_ids = shm_ids.in_use;
464 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
465 shm_info.shm_tot = shm_tot;
466 shm_info.swap_attempts = 0;
467 shm_info.swap_successes = 0;
468 err = shm_ids.max_id;
470 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
475 err = err < 0 ? 0 : err;
481 struct shmid64_ds tbuf;
483 memset(&tbuf, 0, sizeof(tbuf));
484 shp = shm_lock(shmid);
488 } else if(cmd==SHM_STAT) {
490 if (shmid > shm_ids.max_id)
492 result = shm_buildid(shmid, shp->shm_perm.seq);
494 err = shm_checkid(shp,shmid);
500 if (ipcperms (&shp->shm_perm, S_IRUGO))
502 err = security_shm_shmctl(shp, cmd);
505 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
506 tbuf.shm_segsz = shp->shm_segsz;
507 tbuf.shm_atime = shp->shm_atim;
508 tbuf.shm_dtime = shp->shm_dtim;
509 tbuf.shm_ctime = shp->shm_ctim;
510 tbuf.shm_cpid = shp->shm_cprid;
511 tbuf.shm_lpid = shp->shm_lprid;
512 if (!is_file_hugepages(shp->shm_file))
513 tbuf.shm_nattch = shp->shm_nattch;
515 tbuf.shm_nattch = file_count(shp->shm_file) - 1;
517 if(copy_shmid_to_user (buf, &tbuf, version))
526 shp = shm_lock(shmid);
531 err = shm_checkid(shp,shmid);
535 if (!capable(CAP_IPC_LOCK)) {
537 if (current->euid != shp->shm_perm.uid &&
538 current->euid != shp->shm_perm.cuid)
540 if (cmd == SHM_LOCK &&
541 !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
545 err = security_shm_shmctl(shp, cmd);
550 struct user_struct * user = current->user;
551 if (!is_file_hugepages(shp->shm_file)) {
552 err = shmem_lock(shp->shm_file, 1, user);
554 shp->shm_flags |= SHM_LOCKED;
555 shp->mlock_user = user;
558 } else if (!is_file_hugepages(shp->shm_file)) {
559 shmem_lock(shp->shm_file, 0, shp->mlock_user);
560 shp->shm_flags &= ~SHM_LOCKED;
561 shp->mlock_user = NULL;
569 * We cannot simply remove the file. The SVID states
570 * that the block remains until the last person
571 * detaches from it, then is deleted. A shmat() on
572 * an RMID segment is legal in older Linux and if
573 * we change it apps break...
575 * Instead we set a destroyed flag, and then blow
576 * the name away when the usage hits zero.
579 shp = shm_lock(shmid);
583 err = shm_checkid(shp, shmid);
587 if (current->euid != shp->shm_perm.uid &&
588 current->euid != shp->shm_perm.cuid &&
589 !capable(CAP_SYS_ADMIN)) {
594 err = security_shm_shmctl(shp, cmd);
598 if (shp->shm_nattch){
599 shp->shm_flags |= SHM_DEST;
600 /* Do not find it any more */
601 shp->shm_perm.key = IPC_PRIVATE;
611 if (copy_shmid_from_user (&setbuf, buf, version)) {
616 shp = shm_lock(shmid);
620 err = shm_checkid(shp,shmid);
624 if (current->euid != shp->shm_perm.uid &&
625 current->euid != shp->shm_perm.cuid &&
626 !capable(CAP_SYS_ADMIN)) {
630 err = security_shm_shmctl(shp, cmd);
634 shp->shm_perm.uid = setbuf.uid;
635 shp->shm_perm.gid = setbuf.gid;
636 shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
637 | (setbuf.mode & S_IRWXUGO);
638 shp->shm_ctim = get_seconds();
660 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
662 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
663 * "raddr" thing points to kernel space, and there has to be a wrapper around
666 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
668 struct shmid_kernel *shp;
675 unsigned long o_flags;
682 } else if ((addr = (ulong)shmaddr)) {
683 if (addr & (SHMLBA-1)) {
684 if (shmflg & SHM_RND)
685 addr &= ~(SHMLBA-1); /* round down */
687 #ifndef __ARCH_FORCE_SHMLBA
688 if (addr & ~PAGE_MASK)
692 flags = MAP_SHARED | MAP_FIXED;
694 if ((shmflg & SHM_REMAP))
700 if (shmflg & SHM_RDONLY) {
705 prot = PROT_READ | PROT_WRITE;
707 acc_mode = S_IRUGO | S_IWUGO;
709 if (shmflg & SHM_EXEC) {
715 * We cannot rely on the fs check since SYSV IPC does have an
716 * additional creator id...
718 shp = shm_lock(shmid);
723 err = shm_checkid(shp,shmid);
728 if (ipcperms(&shp->shm_perm, acc_mode)) {
734 err = security_shm_shmat(shp, shmaddr, shmflg);
740 file = shp->shm_file;
741 size = i_size_read(file->f_dentry->d_inode);
745 down_write(¤t->mm->mmap_sem);
746 if (addr && !(shmflg & SHM_REMAP)) {
747 user_addr = ERR_PTR(-EINVAL);
748 if (find_vma_intersection(current->mm, addr, addr + size))
751 * If shm segment goes below stack, make sure there is some
752 * space left for the stack to grow (at least 4 pages).
754 if (addr < current->mm->start_stack &&
755 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
759 user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
762 up_write(¤t->mm->mmap_sem);
765 if(!(shp = shm_lock(shmid)))
768 if(shp->shm_nattch == 0 &&
769 shp->shm_flags & SHM_DEST)
775 *raddr = (unsigned long) user_addr;
777 if (IS_ERR(user_addr))
778 err = PTR_ERR(user_addr);
784 * detach and kill segment if marked destroyed.
785 * The work is done in shm_close.
787 asmlinkage long sys_shmdt(char __user *shmaddr)
789 struct mm_struct *mm = current->mm;
790 struct vm_area_struct *vma, *next;
791 unsigned long addr = (unsigned long)shmaddr;
793 int retval = -EINVAL;
795 down_write(&mm->mmap_sem);
798 * This function tries to be smart and unmap shm segments that
799 * were modified by partial mlock or munmap calls:
800 * - It first determines the size of the shm segment that should be
801 * unmapped: It searches for a vma that is backed by shm and that
802 * started at address shmaddr. It records it's size and then unmaps
804 * - Then it unmaps all shm vmas that started at shmaddr and that
805 * are within the initially determined size.
806 * Errors from do_munmap are ignored: the function only fails if
807 * it's called with invalid parameters or if it's called to unmap
808 * a part of a vma. Both calls in this function are for full vmas,
809 * the parameters are directly copied from the vma itself and always
810 * valid - therefore do_munmap cannot fail. (famous last words?)
813 * If it had been mremap()'d, the starting address would not
814 * match the usual checks anyway. So assume all vma's are
815 * above the starting address given.
817 vma = find_vma(mm, addr);
823 * Check if the starting address would match, i.e. it's
824 * a fragment created by mprotect() and/or munmap(), or it
825 * otherwise it starts at this address with no hassles.
827 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
828 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
831 size = vma->vm_file->f_dentry->d_inode->i_size;
832 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
834 * We discovered the size of the shm segment, so
835 * break out of here and fall through to the next
836 * loop that uses the size information to stop
837 * searching for matching vma's.
847 * We need look no further than the maximum address a fragment
848 * could possibly have landed at. Also cast things to loff_t to
849 * prevent overflows and make comparisions vs. equal-width types.
851 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
854 /* finding a matching vma now does not alter retval */
855 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
856 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
858 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
862 up_write(&mm->mmap_sem);
866 #ifdef CONFIG_PROC_FS
867 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
874 len += sprintf(buffer, " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n");
876 for(i = 0; i <= shm_ids.max_id; i++) {
877 struct shmid_kernel* shp;
881 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
882 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
885 if (!vx_check(shp->shm_perm.xid, VX_IDENT)) {
889 if (sizeof(size_t) <= sizeof(int))
890 format = SMALL_STRING;
893 len += sprintf(buffer + len, format,
895 shm_buildid(i, shp->shm_perm.seq),
900 is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
915 if(pos > offset + length)
922 *start = buffer + (offset - begin);
923 len -= (offset - begin);