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 * avoid vmalloc and make shmmax, shmall, shmmni sysctl'able,
13 * Christoph Rohland <hans-christoph.rohland@sap.com>
14 * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
15 * make it a file system, Christoph Rohland <hans-christoph.rohland@sap.com>
17 * The filesystem has the following restrictions/bugs:
18 * 1) It only can handle one directory.
19 * 2) Private writeable mappings are not supported
20 * 3) Read and write are not implemented (should they?)
21 * 4) No special nodes are supported
23 * There are the following mount options:
24 * - nr_blocks (^= shmall) is the number of blocks of size PAGE_SIZE
25 * we are allowed to allocate
26 * - nr_inodes (^= shmmni) is the number of files we are allowed to
28 * - mode is the mode for the root directory (default S_IRWXUGO | S_ISVTX)
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/malloc.h>
34 #include <linux/shm.h>
35 #include <linux/swap.h>
36 #include <linux/smp_lock.h>
37 #include <linux/init.h>
38 #include <linux/locks.h>
39 #include <linux/file.h>
40 #include <linux/mman.h>
41 #include <linux/vmalloc.h>
42 #include <linux/pagemap.h>
43 #include <linux/proc_fs.h>
44 #include <linux/highmem.h>
46 #include <asm/uaccess.h>
47 #include <asm/pgtable.h>
51 static struct super_block
*shm_read_super(struct super_block
*,void *, int);
52 static void shm_put_super (struct super_block
*);
53 static int shm_remount_fs (struct super_block
*, int *, char *);
54 static void shm_read_inode (struct inode
*);
55 static void shm_write_inode(struct inode
*);
56 static int shm_statfs (struct super_block
*, struct statfs
*);
57 static int shm_create (struct inode
*,struct dentry
*,int);
58 static struct dentry
*shm_lookup (struct inode
*,struct dentry
*);
59 static int shm_unlink (struct inode
*,struct dentry
*);
60 static int shm_setattr (struct dentry
*dent
, struct iattr
*attr
);
61 static void shm_delete (struct inode
*);
62 static int shm_mmap (struct file
*, struct vm_area_struct
*);
63 static int shm_readdir (struct file
*, void *, filldir_t
);
65 #define SHM_NAME_LEN NAME_MAX
66 #define SHM_FMT ".IPC_%08x"
67 #define SHM_FMT_LEN 13
69 /* shm_mode upper byte flags */
70 /* SHM_DEST and SHM_LOCKED are used in ipcs(8) */
71 #define PRV_DEST 0010000 /* segment will be destroyed on last detach */
72 #define PRV_LOCKED 0020000 /* segment will not be swapped */
73 #define SHM_UNLK 0040000 /* filename is unlinked */
74 #define SHM_SYSV 0100000 /* It is a SYSV shm segment */
76 struct shmid_kernel
/* private to the kernel */
78 struct kern_ipc_perm shm_perm
;
80 unsigned long shm_nattch
;
81 unsigned long shm_npages
; /* size of segment (pages) */
82 pte_t
**shm_dir
; /* ptr to arr of ptrs to frames */
95 struct semaphore sema
;
96 struct list_head list
;
101 #define shm_atim permap.shmem.atime
102 #define shm_dtim permap.shmem.dtime
103 #define shm_ctim permap.shmem.ctime
104 #define shm_cprid permap.shmem.cpid
105 #define shm_lprid permap.shmem.lpid
106 #define shm_namelen permap.shmem.nlen
107 #define shm_name permap.shmem.nm
108 #define shm_flags shm_perm.mode
109 #define zsem permap.zero.sema
110 #define zero_list permap.zero.list
112 static struct ipc_ids shm_ids
;
114 #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
115 #define shm_unlock(id) ipc_unlock(&shm_ids,id)
116 #define shm_lockall() ipc_lockall(&shm_ids)
117 #define shm_unlockall() ipc_unlockall(&shm_ids)
118 #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
119 #define shm_buildid(id, seq) \
120 ipc_buildid(&shm_ids, id, seq)
122 static int newseg (key_t key
, const char *name
, int namelen
, int shmflg
, size_t size
);
123 static void seg_free(struct shmid_kernel
*shp
, int doacc
);
124 static void shm_open (struct vm_area_struct
*shmd
);
125 static void shm_close (struct vm_area_struct
*shmd
);
126 static int shm_remove_name(int id
);
127 static struct page
* shm_nopage(struct vm_area_struct
*, unsigned long, int);
128 static int shm_swapout(struct page
*, struct file
*);
129 #ifdef CONFIG_PROC_FS
130 static int sysvipc_shm_read_proc(char *buffer
, char **start
, off_t offset
, int length
, int *eof
, void *data
);
133 static void zshm_swap (int prio
, int gfp_mask
);
134 static void zmap_unuse(swp_entry_t entry
, struct page
*page
);
135 static void shmzero_open(struct vm_area_struct
*shmd
);
136 static void shmzero_close(struct vm_area_struct
*shmd
);
137 static struct page
*shmzero_nopage(struct vm_area_struct
* shmd
, unsigned long address
, int no_share
);
139 static struct shmid_kernel zshmid_kernel
;
140 static struct dentry
*zdent
;
142 #define SHM_FS_MAGIC 0x02011994
144 static struct super_block
* shm_sb
;
146 static DECLARE_FSTYPE(shm_fs_type
, "shm", shm_read_super
, FS_SINGLE
);
148 static struct super_operations shm_sops
= {
149 read_inode
: shm_read_inode
,
150 write_inode
: shm_write_inode
,
151 delete_inode
: shm_delete
,
152 put_super
: shm_put_super
,
154 remount_fs
: shm_remount_fs
,
157 static struct file_operations shm_root_operations
= {
158 readdir
: shm_readdir
,
161 static struct inode_operations shm_root_inode_operations
= {
167 static struct file_operations shm_file_operations
= {
171 static struct inode_operations shm_inode_operations
= {
172 setattr
: shm_setattr
,
175 static struct vm_operations_struct shm_vm_ops
= {
176 open
: shm_open
, /* callback for a new vm-area open */
177 close
: shm_close
, /* callback for when the vm-area is released */
182 size_t shm_ctlmax
= SHMMAX
;
184 /* These parameters should be part of the superblock */
185 static int shm_ctlall
;
186 static int shm_ctlmni
;
189 static int shm_tot
; /* total number of shared memory pages */
190 static int shm_rss
; /* number of shared memory pages that are in memory */
191 static int shm_swp
; /* number of shared memory pages that are in swap */
195 shm_lock()/shm_lockall()
202 - swap_free() never sleeps
203 - add_to_swap_cache() never sleeps
204 - add_to_swap_cache() doesn't acquire the big kernel lock.
205 - shm_unuse() is called with the kernel lock acquired.
208 /* some statistics */
209 static ulong swap_attempts
;
210 static ulong swap_successes
;
211 static ulong used_segs
;
213 void __init
shm_init (void)
215 struct vfsmount
*res
;
216 ipc_init_ids(&shm_ids
, 1);
218 register_filesystem (&shm_fs_type
);
219 res
= kern_mount(&shm_fs_type
);
221 unregister_filesystem(&shm_fs_type
);
224 #ifdef CONFIG_PROC_FS
225 create_proc_read_entry("sysvipc/shm", 0, 0, sysvipc_shm_read_proc
, NULL
);
227 zero_id
= ipc_addid(&shm_ids
, &zshmid_kernel
.shm_perm
, 1);
229 INIT_LIST_HEAD(&zshmid_kernel
.zero_list
);
230 zdent
= d_alloc_root(get_empty_inode());
234 static int shm_parse_options(char *options
)
236 int blocks
= shm_ctlall
;
237 int inodes
= shm_ctlmni
;
238 umode_t mode
= shm_mode
;
239 char *this_char
, *value
;
243 this_char
= strtok(options
,",");
244 for ( ; this_char
; this_char
= strtok(NULL
,",")) {
245 if ((value
= strchr(this_char
,'=')) != NULL
)
247 if (!strcmp(this_char
,"nr_blocks")) {
248 if (!value
|| !*value
)
250 blocks
= simple_strtoul(value
,&value
,0);
254 else if (!strcmp(this_char
,"nr_inodes")) {
255 if (!value
|| !*value
)
257 inodes
= simple_strtoul(value
,&value
,0);
261 else if (!strcmp(this_char
,"mode")) {
262 if (!value
|| !*value
)
264 mode
= simple_strtoul(value
,&value
,8);
278 static struct super_block
*shm_read_super(struct super_block
*s
,void *data
,
281 struct inode
* root_inode
;
285 shm_mode
= S_IRWXUGO
| S_ISVTX
;
286 if (shm_parse_options (data
)) {
287 printk(KERN_ERR
"shm fs invalid option\n");
291 s
->s_blocksize
= PAGE_SIZE
;
292 s
->s_blocksize_bits
= PAGE_SHIFT
;
293 s
->s_magic
= SHM_FS_MAGIC
;
295 root_inode
= iget (s
, SEQ_MULTIPLIER
);
298 root_inode
->i_op
= &shm_root_inode_operations
;
299 root_inode
->i_sb
= s
;
300 root_inode
->i_nlink
= 2;
301 root_inode
->i_mode
= S_IFDIR
| shm_mode
;
302 s
->s_root
= d_alloc_root(root_inode
);
309 printk(KERN_ERR
"proc_read_super: get root inode failed\n");
315 static int shm_remount_fs (struct super_block
*sb
, int *flags
, char *data
)
317 if (shm_parse_options (data
))
322 static inline int shm_checkid(struct shmid_kernel
*s
, int id
)
324 if (!(s
->shm_flags
& SHM_SYSV
))
326 if (ipc_checkid(&shm_ids
,&s
->shm_perm
,id
))
331 static inline struct shmid_kernel
*shm_rmid(int id
)
333 return (struct shmid_kernel
*)ipc_rmid(&shm_ids
,id
);
336 static inline int shm_addid(struct shmid_kernel
*shp
)
338 return ipc_addid(&shm_ids
, &shp
->shm_perm
, shm_ctlmni
+1);
341 static void shm_put_super(struct super_block
*sb
)
344 struct shmid_kernel
*shp
;
347 for(i
= 0; i
<= shm_ids
.max_id
; i
++) {
350 if (!(shp
= shm_lock (i
)))
353 printk(KERN_DEBUG
"shm_nattch = %ld\n", shp
->shm_nattch
);
362 static int shm_statfs(struct super_block
*sb
, struct statfs
*buf
)
364 buf
->f_type
= SHM_FS_MAGIC
;
365 buf
->f_bsize
= PAGE_SIZE
;
366 buf
->f_blocks
= shm_ctlall
;
367 buf
->f_bavail
= buf
->f_bfree
= shm_ctlall
- shm_tot
;
368 buf
->f_files
= shm_ctlmni
;
369 buf
->f_ffree
= shm_ctlmni
- used_segs
;
370 buf
->f_namelen
= SHM_NAME_LEN
;
374 static void shm_write_inode(struct inode
* inode
)
378 static void shm_read_inode(struct inode
* inode
)
381 struct shmid_kernel
*shp
;
386 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
388 if (id
< SEQ_MULTIPLIER
) {
389 if (!(shp
= shm_lock (id
)))
391 inode
->i_mode
= (shp
->shm_flags
& S_IALLUGO
) | S_IFREG
;
392 inode
->i_uid
= shp
->shm_perm
.uid
;
393 inode
->i_gid
= shp
->shm_perm
.gid
;
394 inode
->i_size
= shp
->shm_segsz
;
396 inode
->i_op
= &shm_inode_operations
;
397 inode
->i_fop
= &shm_file_operations
;
400 inode
->i_op
= &shm_root_inode_operations
;
401 inode
->i_fop
= &shm_root_operations
;
402 inode
->i_sb
= shm_sb
;
404 inode
->i_mode
= S_IFDIR
| shm_mode
;
405 inode
->i_uid
= inode
->i_gid
= 0;
409 static int shm_create (struct inode
*dir
, struct dentry
*dent
, int mode
)
412 struct inode
* inode
;
415 err
= id
= newseg (IPC_PRIVATE
, dent
->d_name
.name
, dent
->d_name
.len
, mode
, 0);
420 inode
= iget (shm_sb
, id
% SEQ_MULTIPLIER
);
425 down (&inode
->i_sem
);
426 inode
->i_mode
= mode
| S_IFREG
;
427 inode
->i_op
= &shm_inode_operations
;
428 d_instantiate(dent
, inode
);
436 static int shm_readdir (struct file
*filp
, void *dirent
, filldir_t filldir
)
438 struct inode
* inode
= filp
->f_dentry
->d_inode
;
439 struct shmid_kernel
*shp
;
447 if (filldir(dirent
, ".", 1, nr
, inode
->i_ino
) < 0)
452 if (filldir(dirent
, "..", 2, nr
, inode
->i_ino
) < 0)
458 for (; nr
-2 <= shm_ids
.max_id
; nr
++ ) {
461 if (!(shp
= shm_get (nr
-2)))
463 if (shp
->shm_flags
& SHM_UNLK
)
465 if (filldir(dirent
, shp
->shm_name
, shp
->shm_namelen
, nr
, nr
) < 0 )
477 static struct dentry
*shm_lookup (struct inode
*dir
, struct dentry
*dent
)
480 struct shmid_kernel
* shp
;
481 struct inode
*inode
= NULL
;
483 if (dent
->d_name
.len
> SHM_NAME_LEN
)
484 return ERR_PTR(-ENAMETOOLONG
);
487 for(i
= 0; i
<= shm_ids
.max_id
; i
++) {
490 if (!(shp
= shm_lock(i
)))
492 if (!(shp
->shm_flags
& SHM_UNLK
) &&
493 dent
->d_name
.len
== shp
->shm_namelen
&&
494 strncmp(dent
->d_name
.name
, shp
->shm_name
, shp
->shm_namelen
) == 0)
500 * prevent the reserved names as negative dentries.
501 * This also prevents object creation through the filesystem
503 if (dent
->d_name
.len
== SHM_FMT_LEN
&&
504 memcmp (SHM_FMT
, dent
->d_name
.name
, SHM_FMT_LEN
- 8) == 0)
505 err
= -EINVAL
; /* EINVAL to give IPC_RMID the right error */
511 inode
= iget(dir
->i_sb
, i
);
522 static int shm_unlink (struct inode
*dir
, struct dentry
*dent
)
524 struct inode
* inode
= dent
->d_inode
;
525 struct shmid_kernel
*shp
;
528 if (!(shp
= shm_lock (inode
->i_ino
)))
530 shp
->shm_flags
|= SHM_UNLK
| PRV_DEST
;
531 shp
->shm_perm
.key
= IPC_PRIVATE
; /* Do not find it any more */
532 shm_unlock (inode
->i_ino
);
536 * If it's a reserved name we have to drop the dentry instead
537 * of creating a negative dentry
539 if (dent
->d_name
.len
== SHM_FMT_LEN
&&
540 memcmp (SHM_FMT
, dent
->d_name
.name
, SHM_FMT_LEN
- 8) == 0)
546 * We cannot use kmalloc for shm_alloc since this restricts the
547 * maximum size of the segments.
549 * We also cannot use vmalloc, since this uses too much of the vmalloc
550 * space and we run out of this on highend machines.
552 * So we have to use this complicated indirect scheme to alloc the shm
558 static inline void init_ptes (pte_t
*pte
, int number
) {
563 static inline void init_ptes (pte_t
*pte
, int number
) {
564 memset (pte
, 0, number
*sizeof(*pte
));
568 #define PTES_PER_PAGE (PAGE_SIZE/sizeof(pte_t))
569 #define SHM_ENTRY(shp, index) (shp)->shm_dir[(index)/PTES_PER_PAGE][(index)%PTES_PER_PAGE]
571 static pte_t
**shm_alloc(unsigned long pages
, int doacc
)
573 unsigned short dir
= pages
/ PTES_PER_PAGE
;
574 unsigned short last
= pages
% PTES_PER_PAGE
;
580 ret
= kmalloc ((dir
+1) * sizeof(pte_t
*), GFP_KERNEL
);
584 for (ptr
= ret
; ptr
< ret
+dir
; ptr
++)
586 *ptr
= (pte_t
*)__get_free_page (GFP_KERNEL
);
589 init_ptes (*ptr
, PTES_PER_PAGE
);
592 /* The last one is probably not of PAGE_SIZE: we use kmalloc */
594 *ptr
= kmalloc (last
*sizeof(pte_t
), GFP_KERNEL
);
597 init_ptes (*ptr
, last
);
608 /* The last failed: we decrement first */
610 free_page ((unsigned long)*ptr
);
614 return ERR_PTR(-ENOMEM
);
617 static void shm_free(pte_t
** dir
, unsigned long pages
, int doacc
)
620 pte_t
**ptr
= dir
+pages
/PTES_PER_PAGE
;
625 for (i
= 0, rss
= 0, swp
= 0; i
< pages
; i
++) {
627 pte
= dir
[i
/PTES_PER_PAGE
][i
%PTES_PER_PAGE
];
630 if (pte_present(pte
)) {
631 __free_page (pte_page(pte
));
634 swap_free(pte_to_swp_entry(pte
));
639 /* first the last page */
640 if (pages
%PTES_PER_PAGE
)
642 /* now the whole pages */
645 free_page ((unsigned long)*ptr
);
647 /* Now the indirect block */
660 static int shm_setattr (struct dentry
*dentry
, struct iattr
*attr
)
663 struct inode
*inode
= dentry
->d_inode
;
664 struct shmid_kernel
*shp
;
665 unsigned long new_pages
, old_pages
;
666 pte_t
**new_dir
, **old_dir
;
668 error
= inode_change_ok(inode
, attr
);
671 if (!(attr
->ia_valid
& ATTR_SIZE
))
673 if (attr
->ia_size
> shm_ctlmax
)
676 /* We set old_pages and old_dir for easier cleanup */
677 old_pages
= new_pages
= (attr
->ia_size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
678 old_dir
= new_dir
= shm_alloc(new_pages
, 1);
680 return PTR_ERR(new_dir
);
682 if (!(shp
= shm_lock(inode
->i_ino
)))
685 if (shm_tot
- shp
->shm_npages
>= shm_ctlall
)
688 if (shp
->shm_segsz
== attr
->ia_size
)
690 /* Now we set them to the real values */
691 old_dir
= shp
->shm_dir
;
692 old_pages
= shp
->shm_npages
;
696 i
= old_pages
< new_pages
? old_pages
: new_pages
;
697 j
= i
% PTES_PER_PAGE
;
700 memcpy (new_dir
[i
], old_dir
[i
], j
* sizeof (pte_t
));
703 new_dir
[i
] = old_dir
[i
];
707 shp
->shm_dir
= new_dir
;
708 shp
->shm_npages
= new_pages
;
709 shp
->shm_segsz
= attr
->ia_size
;
711 shm_unlock(inode
->i_ino
);
712 shm_free (old_dir
, old_pages
, 1);
715 if (!(shp
= shm_lock(inode
->i_ino
)))
717 if (attr
->ia_valid
& ATTR_MODE
)
718 shp
->shm_perm
.mode
= attr
->ia_mode
;
719 if (attr
->ia_valid
& ATTR_UID
)
720 shp
->shm_perm
.uid
= attr
->ia_uid
;
721 if (attr
->ia_valid
& ATTR_GID
)
722 shp
->shm_perm
.gid
= attr
->ia_gid
;
723 shm_unlock (inode
->i_ino
);
725 inode_setattr(inode
, attr
);
729 static struct shmid_kernel
*seg_alloc(int numpages
, size_t namelen
)
731 struct shmid_kernel
*shp
;
734 shp
= (struct shmid_kernel
*) kmalloc (sizeof (*shp
) + namelen
, GFP_KERNEL
);
736 return ERR_PTR(-ENOMEM
);
738 dir
= shm_alloc (numpages
, namelen
);
741 return ERR_PTR(PTR_ERR(dir
));
744 shp
->shm_npages
= numpages
;
746 shp
->shm_namelen
= namelen
;
750 static void seg_free(struct shmid_kernel
*shp
, int doacc
)
752 shm_free (shp
->shm_dir
, shp
->shm_npages
, doacc
);
756 static int newseg (key_t key
, const char *name
, int namelen
,
757 int shmflg
, size_t size
)
759 struct shmid_kernel
*shp
;
760 int numpages
= (size
+ PAGE_SIZE
-1) >> PAGE_SHIFT
;
763 if (namelen
> SHM_NAME_LEN
)
764 return -ENAMETOOLONG
;
766 if (size
> shm_ctlmax
)
769 if (shm_tot
+ numpages
>= shm_ctlall
)
772 shp
= seg_alloc(numpages
, namelen
? namelen
: SHM_FMT_LEN
+ 1);
780 shp
->shm_perm
.key
= key
;
781 shp
->shm_flags
= (shmflg
& S_IRWXUGO
);
782 shp
->shm_segsz
= size
;
783 shp
->shm_cprid
= current
->pid
;
785 shp
->shm_atim
= shp
->shm_dtim
= 0;
786 shp
->shm_ctim
= CURRENT_TIME
;
787 shp
->id
= shm_buildid(id
,shp
->shm_perm
.seq
);
789 shp
->shm_namelen
= namelen
;
790 memcpy (shp
->shm_name
, name
, namelen
);
792 shp
->shm_flags
|= SHM_SYSV
;
793 shp
->shm_namelen
= sprintf (shp
->shm_name
, SHM_FMT
, shp
->id
);
800 asmlinkage
long sys_shmget (key_t key
, size_t size
, int shmflg
)
802 struct shmid_kernel
*shp
;
809 if (key
== IPC_PRIVATE
) {
810 err
= newseg(key
, NULL
, 0, shmflg
, size
);
811 } else if ((id
= ipc_findkey(&shm_ids
,key
)) == -1) {
812 if (!(shmflg
& IPC_CREAT
))
815 err
= newseg(key
, NULL
, 0, shmflg
, size
);
816 } else if ((shmflg
& IPC_CREAT
) && (shmflg
& IPC_EXCL
)) {
822 if (shp
->shm_segsz
< size
)
824 else if (ipcperms(&shp
->shm_perm
, shmflg
))
827 err
= shm_buildid(id
, shp
->shm_perm
.seq
);
834 static void shm_delete (struct inode
*ino
)
836 int shmid
= ino
->i_ino
;
837 struct shmid_kernel
*shp
;
840 shp
= shm_lock(shmid
);
844 shp
= shm_rmid(shmid
);
851 static inline unsigned long copy_shmid_to_user(void *buf
, struct shmid64_ds
*in
, int version
)
855 return copy_to_user(buf
, in
, sizeof(*in
));
860 ipc64_perm_to_ipc_perm(&in
->shm_perm
, &out
.shm_perm
);
861 out
.shm_segsz
= in
->shm_segsz
;
862 out
.shm_atime
= in
->shm_atime
;
863 out
.shm_dtime
= in
->shm_dtime
;
864 out
.shm_ctime
= in
->shm_ctime
;
865 out
.shm_cpid
= in
->shm_cpid
;
866 out
.shm_lpid
= in
->shm_lpid
;
867 out
.shm_nattch
= in
->shm_nattch
;
869 return copy_to_user(buf
, &out
, sizeof(out
));
882 static inline unsigned long copy_shmid_from_user(struct shm_setbuf
*out
, void *buf
, int version
)
887 struct shmid64_ds tbuf
;
889 if (copy_from_user(&tbuf
, buf
, sizeof(tbuf
)))
892 out
->uid
= tbuf
.shm_perm
.uid
;
893 out
->gid
= tbuf
.shm_perm
.gid
;
894 out
->mode
= tbuf
.shm_flags
;
900 struct shmid_ds tbuf_old
;
902 if (copy_from_user(&tbuf_old
, buf
, sizeof(tbuf_old
)))
905 out
->uid
= tbuf_old
.shm_perm
.uid
;
906 out
->gid
= tbuf_old
.shm_perm
.gid
;
907 out
->mode
= tbuf_old
.shm_flags
;
916 static inline unsigned long copy_shminfo_to_user(void *buf
, struct shminfo64
*in
, int version
)
920 return copy_to_user(buf
, in
, sizeof(*in
));
925 if(in
->shmmax
> INT_MAX
)
926 out
.shmmax
= INT_MAX
;
928 out
.shmmax
= (int)in
->shmmax
;
930 out
.shmmin
= in
->shmmin
;
931 out
.shmmni
= in
->shmmni
;
932 out
.shmseg
= in
->shmseg
;
933 out
.shmall
= in
->shmall
;
935 return copy_to_user(buf
, &out
, sizeof(out
));
942 asmlinkage
long sys_shmctl (int shmid
, int cmd
, struct shmid_ds
*buf
)
944 struct shm_setbuf setbuf
;
945 struct shmid_kernel
*shp
;
948 if (cmd
< 0 || shmid
< 0)
951 version
= ipc_parse_version(&cmd
);
953 switch (cmd
) { /* replace with proc interface ? */
956 struct shminfo64 shminfo
;
958 memset(&shminfo
,0,sizeof(shminfo
));
959 shminfo
.shmmni
= shminfo
.shmseg
= shm_ctlmni
;
960 shminfo
.shmmax
= shm_ctlmax
;
961 shminfo
.shmall
= shm_ctlall
;
963 shminfo
.shmmin
= SHMMIN
;
964 if(copy_shminfo_to_user (buf
, &shminfo
, version
))
966 /* reading a integer is always atomic */
974 struct shm_info shm_info
;
976 memset(&shm_info
,0,sizeof(shm_info
));
978 shm_info
.used_ids
= shm_ids
.in_use
;
979 shm_info
.shm_rss
= shm_rss
;
980 shm_info
.shm_tot
= shm_tot
;
981 shm_info
.shm_swp
= shm_swp
;
982 shm_info
.swap_attempts
= swap_attempts
;
983 shm_info
.swap_successes
= swap_successes
;
984 err
= shm_ids
.max_id
;
986 if(copy_to_user (buf
, &shm_info
, sizeof(shm_info
)))
989 return err
< 0 ? 0 : err
;
994 struct shmid64_ds tbuf
;
996 if ((shmid
% SEQ_MULTIPLIER
) == zero_id
)
998 memset(&tbuf
, 0, sizeof(tbuf
));
999 shp
= shm_lock(shmid
);
1004 if (!(shp
->shm_flags
& SHM_SYSV
) ||
1005 shmid
> shm_ids
.max_id
)
1007 result
= shm_buildid(shmid
, shp
->shm_perm
.seq
);
1009 err
= shm_checkid(shp
,shmid
);
1015 if (ipcperms (&shp
->shm_perm
, S_IRUGO
))
1017 kernel_to_ipc64_perm(&shp
->shm_perm
, &tbuf
.shm_perm
);
1018 /* ugly hack to keep binary compatibility for ipcs */
1019 tbuf
.shm_flags
&= PRV_DEST
| PRV_LOCKED
| S_IRWXUGO
;
1020 if (tbuf
.shm_flags
& PRV_DEST
)
1021 tbuf
.shm_flags
|= SHM_DEST
;
1022 if (tbuf
.shm_flags
& PRV_LOCKED
)
1023 tbuf
.shm_flags
|= SHM_LOCKED
;
1024 tbuf
.shm_flags
&= SHM_DEST
| SHM_LOCKED
| S_IRWXUGO
;
1025 tbuf
.shm_segsz
= shp
->shm_segsz
;
1026 tbuf
.shm_atime
= shp
->shm_atim
;
1027 tbuf
.shm_dtime
= shp
->shm_dtim
;
1028 tbuf
.shm_ctime
= shp
->shm_ctim
;
1029 tbuf
.shm_cpid
= shp
->shm_cprid
;
1030 tbuf
.shm_lpid
= shp
->shm_lprid
;
1031 tbuf
.shm_nattch
= shp
->shm_nattch
;
1033 if(copy_shmid_to_user (buf
, &tbuf
, version
))
1040 /* Allow superuser to lock segment in memory */
1041 /* Should the pages be faulted in here or leave it to user? */
1042 /* need to determine interaction with current->swappable */
1043 if ((shmid
% SEQ_MULTIPLIER
)== zero_id
)
1045 if (!capable(CAP_IPC_LOCK
))
1048 shp
= shm_lock(shmid
);
1051 err
= shm_checkid(shp
,shmid
);
1055 shp
->shm_flags
|= PRV_LOCKED
;
1057 shp
->shm_flags
&= ~PRV_LOCKED
;
1064 * We cannot simply remove the file. The SVID states
1065 * that the block remains until the last person
1066 * detaches from it, then is deleted. A shmat() on
1067 * an RMID segment is legal in older Linux and if
1068 * we change it apps break...
1070 * Instead we set a destroyed flag, and then blow
1071 * the name away when the usage hits zero.
1073 if ((shmid
% SEQ_MULTIPLIER
) == zero_id
)
1076 shp
= shm_lock(shmid
);
1081 err
= shm_checkid(shp
, shmid
);
1083 if (shp
->shm_nattch
== 0 &&
1084 !(shp
->shm_flags
& SHM_UNLK
)) {
1089 * We can't hold shm_lock here else we
1090 * will deadlock in shm_lookup when we
1091 * try to recursively grab it.
1093 return shm_remove_name(id
);
1095 shp
->shm_flags
|= PRV_DEST
;
1096 /* Do not find it any more */
1097 shp
->shm_perm
.key
= IPC_PRIVATE
;
1107 struct dentry
* dentry
;
1108 char name
[SHM_FMT_LEN
+1];
1110 if ((shmid
% SEQ_MULTIPLIER
)== zero_id
)
1113 if(copy_shmid_from_user (&setbuf
, buf
, version
))
1116 shp
= shm_lock(shmid
);
1120 err
= shm_checkid(shp
,shmid
);
1124 if (current
->euid
!= shp
->shm_perm
.uid
&&
1125 current
->euid
!= shp
->shm_perm
.cuid
&&
1126 !capable(CAP_SYS_ADMIN
)) {
1130 shp
->shm_perm
.uid
= setbuf
.uid
;
1131 shp
->shm_perm
.gid
= setbuf
.gid
;
1132 shp
->shm_flags
= (shp
->shm_flags
& ~S_IRWXUGO
)
1133 | (setbuf
.mode
& S_IRWXUGO
);
1134 shp
->shm_ctim
= CURRENT_TIME
;
1138 sprintf (name
, SHM_FMT
, shmid
);
1140 dentry
= lookup_one(name
, lock_parent(shm_sb
->s_root
));
1141 unlock_dir(shm_sb
->s_root
);
1142 err
= PTR_ERR(dentry
);
1146 if (dentry
->d_inode
) {
1147 struct inode
*ino
= dentry
->d_inode
;
1148 ino
->i_uid
= setbuf
.uid
;
1149 ino
->i_gid
= setbuf
.gid
;
1150 ino
->i_mode
= (setbuf
.mode
& S_IRWXUGO
) | (ino
->i_mode
& ~S_IALLUGO
);;
1151 ino
->i_atime
= ino
->i_mtime
= ino
->i_ctime
= CURRENT_TIME
;
1175 static inline void shm_inc (int id
) {
1176 struct shmid_kernel
*shp
;
1178 if(!(shp
= shm_lock(id
)))
1180 shp
->shm_atim
= CURRENT_TIME
;
1181 shp
->shm_lprid
= current
->pid
;
1186 static int shm_mmap(struct file
* file
, struct vm_area_struct
* vma
)
1188 if ((vma
->vm_flags
& VM_WRITE
) && !(vma
->vm_flags
& VM_SHARED
))
1189 return -EINVAL
; /* we cannot do private writable mappings */
1190 UPDATE_ATIME(file
->f_dentry
->d_inode
);
1191 vma
->vm_ops
= &shm_vm_ops
;
1192 shm_inc(file
->f_dentry
->d_inode
->i_ino
);
1197 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
1199 asmlinkage
long sys_shmat (int shmid
, char *shmaddr
, int shmflg
, ulong
*raddr
)
1201 struct shmid_kernel
*shp
;
1205 unsigned long flags
;
1207 unsigned long o_flags
;
1209 struct dentry
*dentry
;
1210 char name
[SHM_FMT_LEN
+1];
1212 if (!shm_sb
|| (shmid
% SEQ_MULTIPLIER
) == zero_id
)
1215 if ((addr
= (ulong
)shmaddr
)) {
1216 if (addr
& (SHMLBA
-1)) {
1217 if (shmflg
& SHM_RND
)
1218 addr
&= ~(SHMLBA
-1); /* round down */
1222 flags
= MAP_SHARED
| MAP_FIXED
;
1226 if (shmflg
& SHM_RDONLY
) {
1231 prot
= PROT_READ
| PROT_WRITE
;
1233 acc_mode
= S_IRUGO
| S_IWUGO
;
1237 * We cannot rely on the fs check since SYSV IPC does have an
1238 * aditional creator id...
1240 shp
= shm_lock(shmid
);
1243 err
= ipcperms(&shp
->shm_perm
, acc_mode
);
1248 sprintf (name
, SHM_FMT
, shmid
);
1251 mntget(shm_fs_type
.kern_mnt
);
1252 dentry
= lookup_one(name
, lock_parent(shm_sb
->s_root
));
1253 unlock_dir(shm_sb
->s_root
);
1254 err
= PTR_ERR(dentry
);
1258 if (!dentry
->d_inode
)
1260 file
= dentry_open(dentry
, shm_fs_type
.kern_mnt
, o_flags
);
1261 err
= PTR_ERR(file
);
1264 down(¤t
->mm
->mmap_sem
);
1265 *raddr
= do_mmap (file
, addr
, file
->f_dentry
->d_inode
->i_size
,
1267 up(¤t
->mm
->mmap_sem
);
1270 err
= PTR_ERR(*raddr
);
1279 mntput(shm_fs_type
.kern_mnt
);
1286 /* This is called by fork, once for every shm attach. */
1287 static void shm_open (struct vm_area_struct
*shmd
)
1289 shm_inc (shmd
->vm_file
->f_dentry
->d_inode
->i_ino
);
1296 static int shm_remove_name(int id
)
1299 struct dentry
*dentry
;
1301 char name
[SHM_FMT_LEN
+1];
1303 sprintf (name
, SHM_FMT
, id
);
1305 dir
= lock_parent(shm_sb
->s_root
);
1306 dentry
= lookup_one(name
, dir
);
1307 error
= PTR_ERR(dentry
);
1308 if (!IS_ERR(dentry
)) {
1310 * We have to do our own unlink to prevent the vfs
1311 * permission check. The SYSV IPC layer has already
1312 * checked the permissions which do not comply to the
1315 struct inode
*inode
= dir
->d_inode
;
1316 down(&inode
->i_zombie
);
1317 error
= shm_unlink(inode
, dentry
);
1320 up(&inode
->i_zombie
);
1329 * remove the attach descriptor shmd.
1330 * free memory for segment if it is marked destroyed.
1331 * The descriptor has already been removed from the current->mm->mmap list
1332 * and will later be kfree()d.
1334 static void shm_close (struct vm_area_struct
*shmd
)
1336 int id
= shmd
->vm_file
->f_dentry
->d_inode
->i_ino
;
1337 struct shmid_kernel
*shp
;
1339 /* remove from the list of attaches of the shm segment */
1340 if(!(shp
= shm_lock(id
)))
1342 shp
->shm_lprid
= current
->pid
;
1343 shp
->shm_dtim
= CURRENT_TIME
;
1345 if(shp
->shm_nattch
== 0 &&
1346 shp
->shm_flags
& PRV_DEST
&&
1347 !(shp
->shm_flags
& SHM_UNLK
)) {
1352 /* The kernel lock prevents new attaches from
1353 * being happening. We can't hold shm_lock here
1354 * else we will deadlock in shm_lookup when we
1355 * try to recursively grab it.
1357 err
= shm_remove_name(pid
);
1358 if(err
&& err
!= -EINVAL
&& err
!= -ENOENT
)
1359 printk(KERN_ERR
"Unlink of SHM id %d failed (%d).\n", pid
, err
);
1367 * detach and kill segment if marked destroyed.
1368 * The work is done in shm_close.
1370 asmlinkage
long sys_shmdt (char *shmaddr
)
1372 struct mm_struct
*mm
= current
->mm
;
1373 struct vm_area_struct
*shmd
, *shmdnext
;
1375 down(&mm
->mmap_sem
);
1376 for (shmd
= mm
->mmap
; shmd
; shmd
= shmdnext
) {
1377 shmdnext
= shmd
->vm_next
;
1378 if (shmd
->vm_ops
== &shm_vm_ops
1379 && shmd
->vm_start
- (shmd
->vm_pgoff
<< PAGE_SHIFT
) == (ulong
) shmaddr
)
1380 do_munmap(mm
, shmd
->vm_start
, shmd
->vm_end
- shmd
->vm_start
);
1387 * Enter the shm page into the SHM data structures.
1389 * The way "nopage" is done, we don't actually have to
1390 * do anything here: nopage will have filled in the shm
1391 * data structures already, and shm_swap_out() will just
1394 static int shm_swapout(struct page
* page
, struct file
*file
)
1400 * page not present ... go through shm_dir
1402 static struct page
* shm_nopage_core(struct shmid_kernel
*shp
, unsigned int idx
, int *swp
, int *rss
, unsigned long address
)
1407 if (idx
>= shp
->shm_npages
)
1408 return NOPAGE_SIGBUS
;
1410 pte
= SHM_ENTRY(shp
,idx
);
1411 if (!pte_present(pte
)) {
1412 /* page not present so shm_swap can't race with us
1413 and the semaphore protects us by other tasks that
1414 could potentially fault on our pte under us */
1415 if (pte_none(pte
)) {
1416 shm_unlock(shp
->id
);
1417 page
= page_cache_alloc();
1420 clear_user_highpage(page
, address
);
1421 if ((shp
!= shm_lock(shp
->id
)) && (shp
->id
!= zero_id
))
1424 swp_entry_t entry
= pte_to_swp_entry(pte
);
1426 shm_unlock(shp
->id
);
1427 page
= lookup_swap_cache(entry
);
1430 swapin_readahead(entry
);
1431 page
= read_swap_cache(entry
);
1436 delete_from_swap_cache(page
);
1437 page
= replace_with_highmem(page
);
1439 if ((shp
!= shm_lock(shp
->id
)) && (shp
->id
!= zero_id
))
1444 pte
= pte_mkdirty(mk_pte(page
, PAGE_SHARED
));
1445 SHM_ENTRY(shp
, idx
) = pte
;
1448 /* pte_val(pte) == SHM_ENTRY (shp, idx) */
1449 page_cache_get(pte_page(pte
));
1450 return pte_page(pte
);
1457 static struct page
* shm_nopage(struct vm_area_struct
* shmd
, unsigned long address
, int no_share
)
1460 struct shmid_kernel
*shp
;
1462 struct inode
* inode
= shmd
->vm_file
->f_dentry
->d_inode
;
1464 idx
= (address
- shmd
->vm_start
) >> PAGE_SHIFT
;
1465 idx
+= shmd
->vm_pgoff
;
1467 down(&inode
->i_sem
);
1468 if(!(shp
= shm_lock(inode
->i_ino
)))
1470 page
= shm_nopage_core(shp
, idx
, &shm_swp
, &shm_rss
, address
);
1471 shm_unlock(inode
->i_ino
);
1480 static int shm_swap_core(struct shmid_kernel
*shp
, unsigned long idx
, swp_entry_t swap_entry
, int *counter
, struct page
**outpage
)
1483 struct page
*page_map
;
1485 page
= SHM_ENTRY(shp
, idx
);
1486 if (!pte_present(page
))
1488 page_map
= pte_page(page
);
1489 if (page_map
->zone
->free_pages
> page_map
->zone
->pages_high
)
1491 if (shp
->id
!= zero_id
) swap_attempts
++;
1493 if (--counter
< 0) /* failed */
1495 if (page_count(page_map
) != 1)
1498 lock_page(page_map
);
1499 if (!(page_map
= prepare_highmem_swapout(page_map
)))
1501 SHM_ENTRY (shp
, idx
) = swp_entry_to_pte(swap_entry
);
1503 /* add the locked page to the swap cache before allowing
1504 the swapin path to run lookup_swap_cache(). This avoids
1505 reading a not yet uptodate block from disk.
1506 NOTE: we just accounted the swap space reference for this
1507 swap cache page at __get_swap_page() time. */
1508 add_to_swap_cache(*outpage
= page_map
, swap_entry
);
1512 static void shm_swap_postop(struct page
*page
)
1515 rw_swap_page(WRITE
, page
, 0);
1517 page_cache_release(page
);
1520 static int shm_swap_preop(swp_entry_t
*swap_entry
)
1523 /* subtle: preload the swap count for the swap cache. We can't
1524 increase the count inside the critical section as we can't release
1525 the shm_lock there. And we can't acquire the big lock with the
1526 shm_lock held (otherwise we would deadlock too easily). */
1527 *swap_entry
= __get_swap_page(2);
1528 if (!(*swap_entry
).val
) {
1537 * Goes through counter = (shm_rss / (prio + 1)) present shm pages.
1539 static unsigned long swap_id
; /* currently being swapped */
1540 static unsigned long swap_idx
; /* next to swap */
1542 int shm_swap (int prio
, int gfp_mask
)
1544 struct shmid_kernel
*shp
;
1545 swp_entry_t swap_entry
;
1546 unsigned long id
, idx
;
1549 struct page
* page_map
;
1551 zshm_swap(prio
, gfp_mask
);
1552 counter
= shm_rss
/ (prio
+ 1);
1555 if (shm_swap_preop(&swap_entry
))
1560 shp
= shm_get(swap_id
);
1561 if(shp
==NULL
|| shp
->shm_flags
& PRV_LOCKED
) {
1564 if (++swap_id
> shm_ids
.max_id
) {
1569 __swap_free(swap_entry
, 2);
1580 if (idx
>= shp
->shm_npages
)
1583 switch (shm_swap_core(shp
, idx
, swap_entry
, &counter
, &page_map
)) {
1584 case RETRY
: goto check_table
;
1585 case FAILED
: goto failed
;
1592 shm_swap_postop(page_map
);
1597 * Free the swap entry and set the new pte for the shm page.
1599 static void shm_unuse_page(struct shmid_kernel
*shp
, unsigned long idx
,
1600 swp_entry_t entry
, struct page
*page
)
1604 pte
= pte_mkdirty(mk_pte(page
, PAGE_SHARED
));
1605 SHM_ENTRY(shp
, idx
) = pte
;
1606 page_cache_get(page
);
1614 static int shm_unuse_core(struct shmid_kernel
*shp
, swp_entry_t entry
, struct page
*page
)
1618 for (n
= 0; n
< shp
->shm_npages
; n
++) {
1619 if (pte_none(SHM_ENTRY(shp
,n
)))
1621 if (pte_present(SHM_ENTRY(shp
,n
)))
1623 if (pte_to_swp_entry(SHM_ENTRY(shp
,n
)).val
== entry
.val
) {
1624 shm_unuse_page(shp
, n
, entry
, page
);
1632 * unuse_shm() search for an eventually swapped out shm page.
1634 void shm_unuse(swp_entry_t entry
, struct page
*page
)
1639 for (i
= 0; i
<= shm_ids
.max_id
; i
++) {
1640 struct shmid_kernel
*shp
= shm_get(i
);
1643 if (shm_unuse_core(shp
, entry
, page
))
1648 zmap_unuse(entry
, page
);
1651 #ifdef CONFIG_PROC_FS
1652 static int sysvipc_shm_read_proc(char *buffer
, char **start
, off_t offset
, int length
, int *eof
, void *data
)
1659 len
+= sprintf(buffer
, " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime name\n");
1661 for(i
= 0; i
<= shm_ids
.max_id
; i
++) {
1662 struct shmid_kernel
* shp
;
1668 #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu %.*s%s\n"
1669 #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu %.*s%s\n"
1672 if (sizeof(size_t) <= sizeof(int))
1673 format
= SMALL_STRING
;
1675 format
= BIG_STRING
;
1676 len
+= sprintf(buffer
+ len
, format
,
1678 shm_buildid(i
, shp
->shm_perm
.seq
),
1693 shp
->shm_flags
& SHM_UNLK
? " (deleted)" : "");
1701 if(pos
> offset
+ length
)
1708 *start
= buffer
+ (offset
- begin
);
1709 len
-= (offset
- begin
);
1718 #define VMA_TO_SHP(vma) ((vma)->vm_file->private_data)
1720 static spinlock_t zmap_list_lock
= SPIN_LOCK_UNLOCKED
;
1721 static unsigned long zswap_idx
; /* next to swap */
1722 static struct shmid_kernel
*zswap_shp
= &zshmid_kernel
;
1723 static int zshm_rss
;
1725 static struct vm_operations_struct shmzero_vm_ops
= {
1727 close
: shmzero_close
,
1728 nopage
: shmzero_nopage
,
1729 swapout
: shm_swapout
,
1733 * In this implementation, the "unuse" and "swapout" interfaces are
1734 * interlocked out via the kernel_lock, as well as shm_lock(zero_id).
1735 * "unuse" and "nopage/swapin", as well as "swapout" and "nopage/swapin"
1736 * interlock via shm_lock(zero_id). All these interlocks can be based
1737 * on a per mapping lock instead of being a global lock.
1740 * Reference (existance) counting on the file/dentry/inode is done
1741 * by generic vm_file code. The zero code does not hold any reference
1742 * on the pseudo-file. This is possible because the open/close calls
1743 * are bracketed by the file count update calls.
1745 static struct file
*file_setup(struct file
*fzero
, struct shmid_kernel
*shp
)
1750 if ((filp
= get_empty_filp()) == 0)
1752 if ((inp
= get_empty_inode()) == 0) {
1756 if ((filp
->f_dentry
= d_alloc(zdent
, &(const struct qstr
) { "dev/zero",
1762 filp
->f_vfsmnt
= mntget(shm_fs_type
.kern_mnt
);
1763 d_instantiate(filp
->f_dentry
, inp
);
1766 * Copy over dev/ino for benefit of procfs. Use
1767 * ino to indicate seperate mappings.
1769 filp
->f_dentry
->d_inode
->i_dev
= shm_fs_type
.kern_mnt
->mnt_sb
->s_dev
;
1770 filp
->f_dentry
->d_inode
->i_ino
= (unsigned long)shp
;
1772 fput(fzero
); /* release /dev/zero file */
1776 int map_zero_setup(struct vm_area_struct
*vma
)
1778 extern int vm_enough_memory(long pages
);
1779 struct shmid_kernel
*shp
;
1782 if (!vm_enough_memory((vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
))
1784 if (IS_ERR(shp
= seg_alloc((vma
->vm_end
- vma
->vm_start
) / PAGE_SIZE
, 0)))
1785 return PTR_ERR(shp
);
1786 if ((filp
= file_setup(vma
->vm_file
, shp
)) == 0) {
1790 vma
->vm_file
= filp
;
1791 VMA_TO_SHP(vma
) = (void *)shp
;
1793 init_MUTEX(&shp
->zsem
);
1794 vma
->vm_ops
= &shmzero_vm_ops
;
1796 spin_lock(&zmap_list_lock
);
1797 list_add(&shp
->zero_list
, &zshmid_kernel
.zero_list
);
1798 spin_unlock(&zmap_list_lock
);
1802 static void shmzero_open(struct vm_area_struct
*shmd
)
1804 struct shmid_kernel
*shp
;
1806 shp
= VMA_TO_SHP(shmd
);
1812 static void shmzero_close(struct vm_area_struct
*shmd
)
1815 struct shmid_kernel
*shp
;
1817 shp
= VMA_TO_SHP(shmd
);
1819 if (--shp
->shm_nattch
== 0)
1823 spin_lock(&zmap_list_lock
);
1824 if (shp
== zswap_shp
)
1825 zswap_shp
= list_entry(zswap_shp
->zero_list
.next
,
1826 struct shmid_kernel
, zero_list
);
1827 list_del(&shp
->zero_list
);
1828 spin_unlock(&zmap_list_lock
);
1833 static struct page
* shmzero_nopage(struct vm_area_struct
* shmd
, unsigned long address
, int no_share
)
1836 struct shmid_kernel
*shp
;
1840 idx
= (address
- shmd
->vm_start
) >> PAGE_SHIFT
;
1841 idx
+= shmd
->vm_pgoff
;
1843 shp
= VMA_TO_SHP(shmd
);
1846 page
= shm_nopage_core(shp
, idx
, &dummy
, &zshm_rss
, address
);
1847 shm_unlock(zero_id
);
1852 static void zmap_unuse(swp_entry_t entry
, struct page
*page
)
1854 struct shmid_kernel
*shp
;
1856 spin_lock(&zmap_list_lock
);
1858 for (shp
= list_entry(zshmid_kernel
.zero_list
.next
, struct shmid_kernel
,
1859 zero_list
); shp
!= &zshmid_kernel
;
1860 shp
= list_entry(shp
->zero_list
.next
, struct shmid_kernel
,
1862 if (shm_unuse_core(shp
, entry
, page
))
1865 shm_unlock(zero_id
);
1866 spin_unlock(&zmap_list_lock
);
1869 static void zshm_swap (int prio
, int gfp_mask
)
1871 struct shmid_kernel
*shp
;
1872 swp_entry_t swap_entry
;
1876 struct page
* page_map
;
1878 counter
= zshm_rss
/ (prio
+ 1);
1882 if (shm_swap_preop(&swap_entry
))
1885 spin_lock(&zmap_list_lock
);
1887 if (zshmid_kernel
.zero_list
.next
== 0)
1890 if (zswap_shp
== &zshmid_kernel
) {
1893 shm_unlock(zero_id
);
1894 spin_unlock(&zmap_list_lock
);
1895 __swap_free(swap_entry
, 2);
1898 zswap_shp
= list_entry(zshmid_kernel
.zero_list
.next
,
1899 struct shmid_kernel
, zero_list
);
1907 if (idx
>= shp
->shm_npages
) {
1908 zswap_shp
= list_entry(zswap_shp
->zero_list
.next
,
1909 struct shmid_kernel
, zero_list
);
1914 switch (shm_swap_core(shp
, idx
, swap_entry
, &counter
, &page_map
)) {
1915 case RETRY
: goto check_table
;
1916 case FAILED
: goto failed
;
1918 shm_unlock(zero_id
);
1919 spin_unlock(&zmap_list_lock
);
1921 shm_swap_postop(page_map
);