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/shmem_fs.h>
27 #include <linux/security.h>
28 #include <linux/syscalls.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/ptrace.h>
32 #include <linux/seq_file.h>
33 #include <linux/mutex.h>
35 #include <asm/uaccess.h>
39 static struct file_operations shm_file_operations
;
40 static struct vm_operations_struct shm_vm_ops
;
42 static struct ipc_ids shm_ids
;
44 #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
45 #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
46 #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
47 #define shm_buildid(id, seq) \
48 ipc_buildid(&shm_ids, id, seq)
50 static int newseg (key_t key
, int shmflg
, size_t size
);
51 static void shm_open (struct vm_area_struct
*shmd
);
52 static void shm_close (struct vm_area_struct
*shmd
);
54 static int sysvipc_shm_proc_show(struct seq_file
*s
, void *it
);
57 size_t shm_ctlmax
= SHMMAX
;
58 size_t shm_ctlall
= SHMALL
;
59 int shm_ctlmni
= SHMMNI
;
61 static int shm_tot
; /* total number of shared memory pages */
63 void __init
shm_init (void)
65 ipc_init_ids(&shm_ids
, 1);
66 ipc_init_proc_interface("sysvipc/shm",
67 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
69 sysvipc_shm_proc_show
);
72 static inline int shm_checkid(struct shmid_kernel
*s
, int id
)
74 if (ipc_checkid(&shm_ids
,&s
->shm_perm
,id
))
79 static inline struct shmid_kernel
*shm_rmid(int id
)
81 return (struct shmid_kernel
*)ipc_rmid(&shm_ids
,id
);
84 static inline int shm_addid(struct shmid_kernel
*shp
)
86 return ipc_addid(&shm_ids
, &shp
->shm_perm
, shm_ctlmni
);
91 static inline void shm_inc (int id
) {
92 struct shmid_kernel
*shp
;
94 if(!(shp
= shm_lock(id
)))
96 shp
->shm_atim
= get_seconds();
97 shp
->shm_lprid
= current
->tgid
;
102 /* This is called by fork, once for every shm attach. */
103 static void shm_open (struct vm_area_struct
*shmd
)
105 shm_inc (shmd
->vm_file
->f_dentry
->d_inode
->i_ino
);
109 * shm_destroy - free the struct shmid_kernel
111 * @shp: struct to free
113 * It has to be called with shp and shm_ids.mutex locked,
114 * but returns with shp unlocked and freed.
116 static void shm_destroy (struct shmid_kernel
*shp
)
118 shm_tot
-= (shp
->shm_segsz
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
121 if (!is_file_hugepages(shp
->shm_file
))
122 shmem_lock(shp
->shm_file
, 0, shp
->mlock_user
);
124 user_shm_unlock(shp
->shm_file
->f_dentry
->d_inode
->i_size
,
126 fput (shp
->shm_file
);
127 security_shm_free(shp
);
132 * remove the attach descriptor shmd.
133 * free memory for segment if it is marked destroyed.
134 * The descriptor has already been removed from the current->mm->mmap list
135 * and will later be kfree()d.
137 static void shm_close (struct vm_area_struct
*shmd
)
139 struct file
* file
= shmd
->vm_file
;
140 int id
= file
->f_dentry
->d_inode
->i_ino
;
141 struct shmid_kernel
*shp
;
143 mutex_lock(&shm_ids
.mutex
);
144 /* remove from the list of attaches of the shm segment */
145 if(!(shp
= shm_lock(id
)))
147 shp
->shm_lprid
= current
->tgid
;
148 shp
->shm_dtim
= get_seconds();
150 if(shp
->shm_nattch
== 0 &&
151 shp
->shm_perm
.mode
& SHM_DEST
)
155 mutex_unlock(&shm_ids
.mutex
);
158 static int shm_mmap(struct file
* file
, struct vm_area_struct
* vma
)
162 ret
= shmem_mmap(file
, vma
);
164 vma
->vm_ops
= &shm_vm_ops
;
165 shm_inc(file
->f_dentry
->d_inode
->i_ino
);
171 static struct file_operations shm_file_operations
= {
174 .get_unmapped_area
= shmem_get_unmapped_area
,
178 static struct vm_operations_struct shm_vm_ops
= {
179 .open
= shm_open
, /* callback for a new vm-area open */
180 .close
= shm_close
, /* callback for when the vm-area is released */
181 .nopage
= shmem_nopage
,
182 #if defined(CONFIG_NUMA) && defined(CONFIG_SHMEM)
183 .set_policy
= shmem_set_policy
,
184 .get_policy
= shmem_get_policy
,
188 static int newseg (key_t key
, int shmflg
, size_t size
)
191 struct shmid_kernel
*shp
;
192 int numpages
= (size
+ PAGE_SIZE
-1) >> PAGE_SHIFT
;
197 if (size
< SHMMIN
|| size
> shm_ctlmax
)
200 if (shm_tot
+ numpages
>= shm_ctlall
)
203 shp
= ipc_rcu_alloc(sizeof(*shp
));
207 shp
->shm_perm
.key
= key
;
208 shp
->shm_perm
.mode
= (shmflg
& S_IRWXUGO
);
209 shp
->mlock_user
= NULL
;
211 shp
->shm_perm
.security
= NULL
;
212 error
= security_shm_alloc(shp
);
218 if (shmflg
& SHM_HUGETLB
) {
219 /* hugetlb_zero_setup takes care of mlock user accounting */
220 file
= hugetlb_zero_setup(size
);
221 shp
->mlock_user
= current
->user
;
223 int acctflag
= VM_ACCOUNT
;
225 * Do not allow no accounting for OVERCOMMIT_NEVER, even
228 if ((shmflg
& SHM_NORESERVE
) &&
229 sysctl_overcommit_memory
!= OVERCOMMIT_NEVER
)
231 sprintf (name
, "SYSV%08x", key
);
232 file
= shmem_file_setup(name
, size
, acctflag
);
234 error
= PTR_ERR(file
);
243 shp
->shm_cprid
= current
->tgid
;
245 shp
->shm_atim
= shp
->shm_dtim
= 0;
246 shp
->shm_ctim
= get_seconds();
247 shp
->shm_segsz
= size
;
249 shp
->id
= shm_buildid(id
,shp
->shm_perm
.seq
);
250 shp
->shm_file
= file
;
251 file
->f_dentry
->d_inode
->i_ino
= shp
->id
;
253 /* Hugetlb ops would have already been assigned. */
254 if (!(shmflg
& SHM_HUGETLB
))
255 file
->f_op
= &shm_file_operations
;
264 security_shm_free(shp
);
269 asmlinkage
long sys_shmget (key_t key
, size_t size
, int shmflg
)
271 struct shmid_kernel
*shp
;
274 mutex_lock(&shm_ids
.mutex
);
275 if (key
== IPC_PRIVATE
) {
276 err
= newseg(key
, shmflg
, size
);
277 } else if ((id
= ipc_findkey(&shm_ids
, key
)) == -1) {
278 if (!(shmflg
& IPC_CREAT
))
281 err
= newseg(key
, shmflg
, size
);
282 } else if ((shmflg
& IPC_CREAT
) && (shmflg
& IPC_EXCL
)) {
288 if (shp
->shm_segsz
< size
)
290 else if (ipcperms(&shp
->shm_perm
, shmflg
))
293 int shmid
= shm_buildid(id
, shp
->shm_perm
.seq
);
294 err
= security_shm_associate(shp
, shmflg
);
300 mutex_unlock(&shm_ids
.mutex
);
305 static inline unsigned long copy_shmid_to_user(void __user
*buf
, struct shmid64_ds
*in
, int version
)
309 return copy_to_user(buf
, in
, sizeof(*in
));
314 ipc64_perm_to_ipc_perm(&in
->shm_perm
, &out
.shm_perm
);
315 out
.shm_segsz
= in
->shm_segsz
;
316 out
.shm_atime
= in
->shm_atime
;
317 out
.shm_dtime
= in
->shm_dtime
;
318 out
.shm_ctime
= in
->shm_ctime
;
319 out
.shm_cpid
= in
->shm_cpid
;
320 out
.shm_lpid
= in
->shm_lpid
;
321 out
.shm_nattch
= in
->shm_nattch
;
323 return copy_to_user(buf
, &out
, sizeof(out
));
336 static inline unsigned long copy_shmid_from_user(struct shm_setbuf
*out
, void __user
*buf
, int version
)
341 struct shmid64_ds tbuf
;
343 if (copy_from_user(&tbuf
, buf
, sizeof(tbuf
)))
346 out
->uid
= tbuf
.shm_perm
.uid
;
347 out
->gid
= tbuf
.shm_perm
.gid
;
348 out
->mode
= tbuf
.shm_perm
.mode
;
354 struct shmid_ds tbuf_old
;
356 if (copy_from_user(&tbuf_old
, buf
, sizeof(tbuf_old
)))
359 out
->uid
= tbuf_old
.shm_perm
.uid
;
360 out
->gid
= tbuf_old
.shm_perm
.gid
;
361 out
->mode
= tbuf_old
.shm_perm
.mode
;
370 static inline unsigned long copy_shminfo_to_user(void __user
*buf
, struct shminfo64
*in
, int version
)
374 return copy_to_user(buf
, in
, sizeof(*in
));
379 if(in
->shmmax
> INT_MAX
)
380 out
.shmmax
= INT_MAX
;
382 out
.shmmax
= (int)in
->shmmax
;
384 out
.shmmin
= in
->shmmin
;
385 out
.shmmni
= in
->shmmni
;
386 out
.shmseg
= in
->shmseg
;
387 out
.shmall
= in
->shmall
;
389 return copy_to_user(buf
, &out
, sizeof(out
));
396 static void shm_get_stat(unsigned long *rss
, unsigned long *swp
)
403 for (i
= 0; i
<= shm_ids
.max_id
; i
++) {
404 struct shmid_kernel
*shp
;
411 inode
= shp
->shm_file
->f_dentry
->d_inode
;
413 if (is_file_hugepages(shp
->shm_file
)) {
414 struct address_space
*mapping
= inode
->i_mapping
;
415 *rss
+= (HPAGE_SIZE
/PAGE_SIZE
)*mapping
->nrpages
;
417 struct shmem_inode_info
*info
= SHMEM_I(inode
);
418 spin_lock(&info
->lock
);
419 *rss
+= inode
->i_mapping
->nrpages
;
420 *swp
+= info
->swapped
;
421 spin_unlock(&info
->lock
);
426 asmlinkage
long sys_shmctl (int shmid
, int cmd
, struct shmid_ds __user
*buf
)
428 struct shm_setbuf setbuf
;
429 struct shmid_kernel
*shp
;
432 if (cmd
< 0 || shmid
< 0) {
437 version
= ipc_parse_version(&cmd
);
439 switch (cmd
) { /* replace with proc interface ? */
442 struct shminfo64 shminfo
;
444 err
= security_shm_shmctl(NULL
, cmd
);
448 memset(&shminfo
,0,sizeof(shminfo
));
449 shminfo
.shmmni
= shminfo
.shmseg
= shm_ctlmni
;
450 shminfo
.shmmax
= shm_ctlmax
;
451 shminfo
.shmall
= shm_ctlall
;
453 shminfo
.shmmin
= SHMMIN
;
454 if(copy_shminfo_to_user (buf
, &shminfo
, version
))
456 /* reading a integer is always atomic */
464 struct shm_info shm_info
;
466 err
= security_shm_shmctl(NULL
, cmd
);
470 memset(&shm_info
,0,sizeof(shm_info
));
471 mutex_lock(&shm_ids
.mutex
);
472 shm_info
.used_ids
= shm_ids
.in_use
;
473 shm_get_stat (&shm_info
.shm_rss
, &shm_info
.shm_swp
);
474 shm_info
.shm_tot
= shm_tot
;
475 shm_info
.swap_attempts
= 0;
476 shm_info
.swap_successes
= 0;
477 err
= shm_ids
.max_id
;
478 mutex_unlock(&shm_ids
.mutex
);
479 if(copy_to_user (buf
, &shm_info
, sizeof(shm_info
))) {
484 err
= err
< 0 ? 0 : err
;
490 struct shmid64_ds tbuf
;
492 memset(&tbuf
, 0, sizeof(tbuf
));
493 shp
= shm_lock(shmid
);
497 } else if(cmd
==SHM_STAT
) {
499 if (shmid
> shm_ids
.max_id
)
501 result
= shm_buildid(shmid
, shp
->shm_perm
.seq
);
503 err
= shm_checkid(shp
,shmid
);
509 if (ipcperms (&shp
->shm_perm
, S_IRUGO
))
511 err
= security_shm_shmctl(shp
, cmd
);
514 kernel_to_ipc64_perm(&shp
->shm_perm
, &tbuf
.shm_perm
);
515 tbuf
.shm_segsz
= shp
->shm_segsz
;
516 tbuf
.shm_atime
= shp
->shm_atim
;
517 tbuf
.shm_dtime
= shp
->shm_dtim
;
518 tbuf
.shm_ctime
= shp
->shm_ctim
;
519 tbuf
.shm_cpid
= shp
->shm_cprid
;
520 tbuf
.shm_lpid
= shp
->shm_lprid
;
521 if (!is_file_hugepages(shp
->shm_file
))
522 tbuf
.shm_nattch
= shp
->shm_nattch
;
524 tbuf
.shm_nattch
= file_count(shp
->shm_file
) - 1;
526 if(copy_shmid_to_user (buf
, &tbuf
, version
))
535 shp
= shm_lock(shmid
);
540 err
= shm_checkid(shp
,shmid
);
544 if (!capable(CAP_IPC_LOCK
)) {
546 if (current
->euid
!= shp
->shm_perm
.uid
&&
547 current
->euid
!= shp
->shm_perm
.cuid
)
549 if (cmd
== SHM_LOCK
&&
550 !current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
)
554 err
= security_shm_shmctl(shp
, cmd
);
559 struct user_struct
* user
= current
->user
;
560 if (!is_file_hugepages(shp
->shm_file
)) {
561 err
= shmem_lock(shp
->shm_file
, 1, user
);
563 shp
->shm_perm
.mode
|= SHM_LOCKED
;
564 shp
->mlock_user
= user
;
567 } else if (!is_file_hugepages(shp
->shm_file
)) {
568 shmem_lock(shp
->shm_file
, 0, shp
->mlock_user
);
569 shp
->shm_perm
.mode
&= ~SHM_LOCKED
;
570 shp
->mlock_user
= NULL
;
578 * We cannot simply remove the file. The SVID states
579 * that the block remains until the last person
580 * detaches from it, then is deleted. A shmat() on
581 * an RMID segment is legal in older Linux and if
582 * we change it apps break...
584 * Instead we set a destroyed flag, and then blow
585 * the name away when the usage hits zero.
587 mutex_lock(&shm_ids
.mutex
);
588 shp
= shm_lock(shmid
);
592 err
= shm_checkid(shp
, shmid
);
596 if (current
->euid
!= shp
->shm_perm
.uid
&&
597 current
->euid
!= shp
->shm_perm
.cuid
&&
598 !capable(CAP_SYS_ADMIN
)) {
603 err
= security_shm_shmctl(shp
, cmd
);
607 if (shp
->shm_nattch
){
608 shp
->shm_perm
.mode
|= SHM_DEST
;
609 /* Do not find it any more */
610 shp
->shm_perm
.key
= IPC_PRIVATE
;
614 mutex_unlock(&shm_ids
.mutex
);
620 if (copy_shmid_from_user (&setbuf
, buf
, version
)) {
624 mutex_lock(&shm_ids
.mutex
);
625 shp
= shm_lock(shmid
);
629 if ((err
= audit_ipc_perms(0, setbuf
.uid
, setbuf
.gid
,
630 setbuf
.mode
, &(shp
->shm_perm
))))
632 err
= shm_checkid(shp
,shmid
);
636 if (current
->euid
!= shp
->shm_perm
.uid
&&
637 current
->euid
!= shp
->shm_perm
.cuid
&&
638 !capable(CAP_SYS_ADMIN
)) {
642 err
= security_shm_shmctl(shp
, cmd
);
646 shp
->shm_perm
.uid
= setbuf
.uid
;
647 shp
->shm_perm
.gid
= setbuf
.gid
;
648 shp
->shm_perm
.mode
= (shp
->shm_perm
.mode
& ~S_IRWXUGO
)
649 | (setbuf
.mode
& S_IRWXUGO
);
650 shp
->shm_ctim
= get_seconds();
663 mutex_unlock(&shm_ids
.mutex
);
672 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
674 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
675 * "raddr" thing points to kernel space, and there has to be a wrapper around
678 long do_shmat(int shmid
, char __user
*shmaddr
, int shmflg
, ulong
*raddr
)
680 struct shmid_kernel
*shp
;
687 unsigned long o_flags
;
694 } else if ((addr
= (ulong
)shmaddr
)) {
695 if (addr
& (SHMLBA
-1)) {
696 if (shmflg
& SHM_RND
)
697 addr
&= ~(SHMLBA
-1); /* round down */
699 #ifndef __ARCH_FORCE_SHMLBA
700 if (addr
& ~PAGE_MASK
)
704 flags
= MAP_SHARED
| MAP_FIXED
;
706 if ((shmflg
& SHM_REMAP
))
712 if (shmflg
& SHM_RDONLY
) {
717 prot
= PROT_READ
| PROT_WRITE
;
719 acc_mode
= S_IRUGO
| S_IWUGO
;
721 if (shmflg
& SHM_EXEC
) {
727 * We cannot rely on the fs check since SYSV IPC does have an
728 * additional creator id...
730 shp
= shm_lock(shmid
);
735 err
= shm_checkid(shp
,shmid
);
740 if (ipcperms(&shp
->shm_perm
, acc_mode
)) {
746 err
= security_shm_shmat(shp
, shmaddr
, shmflg
);
752 file
= shp
->shm_file
;
753 size
= i_size_read(file
->f_dentry
->d_inode
);
757 down_write(¤t
->mm
->mmap_sem
);
758 if (addr
&& !(shmflg
& SHM_REMAP
)) {
759 user_addr
= ERR_PTR(-EINVAL
);
760 if (find_vma_intersection(current
->mm
, addr
, addr
+ size
))
763 * If shm segment goes below stack, make sure there is some
764 * space left for the stack to grow (at least 4 pages).
766 if (addr
< current
->mm
->start_stack
&&
767 addr
> current
->mm
->start_stack
- size
- PAGE_SIZE
* 5)
771 user_addr
= (void*) do_mmap (file
, addr
, size
, prot
, flags
, 0);
774 up_write(¤t
->mm
->mmap_sem
);
776 mutex_lock(&shm_ids
.mutex
);
777 if(!(shp
= shm_lock(shmid
)))
780 if(shp
->shm_nattch
== 0 &&
781 shp
->shm_perm
.mode
& SHM_DEST
)
785 mutex_unlock(&shm_ids
.mutex
);
787 *raddr
= (unsigned long) user_addr
;
789 if (IS_ERR(user_addr
))
790 err
= PTR_ERR(user_addr
);
795 asmlinkage
long sys_shmat(int shmid
, char __user
*shmaddr
, int shmflg
)
800 err
= do_shmat(shmid
, shmaddr
, shmflg
, &ret
);
803 force_successful_syscall_return();
808 * detach and kill segment if marked destroyed.
809 * The work is done in shm_close.
811 asmlinkage
long sys_shmdt(char __user
*shmaddr
)
813 struct mm_struct
*mm
= current
->mm
;
814 struct vm_area_struct
*vma
, *next
;
815 unsigned long addr
= (unsigned long)shmaddr
;
817 int retval
= -EINVAL
;
819 if (addr
& ~PAGE_MASK
)
822 down_write(&mm
->mmap_sem
);
825 * This function tries to be smart and unmap shm segments that
826 * were modified by partial mlock or munmap calls:
827 * - It first determines the size of the shm segment that should be
828 * unmapped: It searches for a vma that is backed by shm and that
829 * started at address shmaddr. It records it's size and then unmaps
831 * - Then it unmaps all shm vmas that started at shmaddr and that
832 * are within the initially determined size.
833 * Errors from do_munmap are ignored: the function only fails if
834 * it's called with invalid parameters or if it's called to unmap
835 * a part of a vma. Both calls in this function are for full vmas,
836 * the parameters are directly copied from the vma itself and always
837 * valid - therefore do_munmap cannot fail. (famous last words?)
840 * If it had been mremap()'d, the starting address would not
841 * match the usual checks anyway. So assume all vma's are
842 * above the starting address given.
844 vma
= find_vma(mm
, addr
);
850 * Check if the starting address would match, i.e. it's
851 * a fragment created by mprotect() and/or munmap(), or it
852 * otherwise it starts at this address with no hassles.
854 if ((vma
->vm_ops
== &shm_vm_ops
|| is_vm_hugetlb_page(vma
)) &&
855 (vma
->vm_start
- addr
)/PAGE_SIZE
== vma
->vm_pgoff
) {
858 size
= vma
->vm_file
->f_dentry
->d_inode
->i_size
;
859 do_munmap(mm
, vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
861 * We discovered the size of the shm segment, so
862 * break out of here and fall through to the next
863 * loop that uses the size information to stop
864 * searching for matching vma's.
874 * We need look no further than the maximum address a fragment
875 * could possibly have landed at. Also cast things to loff_t to
876 * prevent overflows and make comparisions vs. equal-width types.
878 size
= PAGE_ALIGN(size
);
879 while (vma
&& (loff_t
)(vma
->vm_end
- addr
) <= size
) {
882 /* finding a matching vma now does not alter retval */
883 if ((vma
->vm_ops
== &shm_vm_ops
|| is_vm_hugetlb_page(vma
)) &&
884 (vma
->vm_start
- addr
)/PAGE_SIZE
== vma
->vm_pgoff
)
886 do_munmap(mm
, vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
890 up_write(&mm
->mmap_sem
);
894 #ifdef CONFIG_PROC_FS
895 static int sysvipc_shm_proc_show(struct seq_file
*s
, void *it
)
897 struct shmid_kernel
*shp
= it
;
900 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
901 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
903 if (sizeof(size_t) <= sizeof(int))
904 format
= SMALL_STRING
;
907 return seq_printf(s
, format
,
914 is_file_hugepages(shp
->shm_file
) ? (file_count(shp
->shm_file
) - 1) : shp
->shm_nattch
,