Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/ipc/shm.c |
| 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. |
| 7 | * |
| 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> |
| 15 | * |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 16 | * support for audit of ipc object properties and permission changes |
| 17 | * Dustin Kirkland <dustin.kirkland@us.ibm.com> |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 18 | * |
| 19 | * namespaces support |
| 20 | * OpenVZ, SWsoft Inc. |
| 21 | * Pavel Emelianov <xemul@openvz.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/mm.h> |
| 26 | #include <linux/hugetlb.h> |
| 27 | #include <linux/shm.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/file.h> |
| 30 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/shmem_fs.h> |
| 32 | #include <linux/security.h> |
| 33 | #include <linux/syscalls.h> |
| 34 | #include <linux/audit.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 35 | #include <linux/capability.h> |
Stephen Rothwell | 7d87e14c | 2005-05-01 08:59:12 -0700 | [diff] [blame] | 36 | #include <linux/ptrace.h> |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 37 | #include <linux/seq_file.h> |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 38 | #include <linux/mutex.h> |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 39 | #include <linux/nsproxy.h> |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 40 | #include <linux/mount.h> |
Stephen Rothwell | 7d87e14c | 2005-05-01 08:59:12 -0700 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/uaccess.h> |
| 43 | |
| 44 | #include "util.h" |
| 45 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 46 | struct shm_file_data { |
| 47 | int id; |
| 48 | struct ipc_namespace *ns; |
| 49 | struct file *file; |
| 50 | const struct vm_operations_struct *vm_ops; |
| 51 | }; |
| 52 | |
| 53 | #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data)) |
| 54 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 55 | static const struct file_operations shm_file_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static struct vm_operations_struct shm_vm_ops; |
| 57 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 58 | static struct ipc_ids init_shm_ids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 60 | #define shm_ids(ns) (*((ns)->ids[IPC_SHM_IDS])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 62 | #define shm_lock(ns, id) \ |
| 63 | ((struct shmid_kernel*)ipc_lock(&shm_ids(ns),id)) |
| 64 | #define shm_unlock(shp) \ |
| 65 | ipc_unlock(&(shp)->shm_perm) |
| 66 | #define shm_get(ns, id) \ |
| 67 | ((struct shmid_kernel*)ipc_get(&shm_ids(ns),id)) |
| 68 | #define shm_buildid(ns, id, seq) \ |
| 69 | ipc_buildid(&shm_ids(ns), id, seq) |
| 70 | |
| 71 | static int newseg (struct ipc_namespace *ns, key_t key, |
| 72 | int shmflg, size_t size); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 73 | static void shm_open(struct vm_area_struct *vma); |
| 74 | static void shm_close(struct vm_area_struct *vma); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 75 | static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #ifdef CONFIG_PROC_FS |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 77 | static int sysvipc_shm_proc_show(struct seq_file *s, void *it); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #endif |
| 79 | |
Cedric Le Goater | 7d69a1f | 2007-07-15 23:40:58 -0700 | [diff] [blame] | 80 | static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids) |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 81 | { |
| 82 | ns->ids[IPC_SHM_IDS] = ids; |
| 83 | ns->shm_ctlmax = SHMMAX; |
| 84 | ns->shm_ctlall = SHMALL; |
| 85 | ns->shm_ctlmni = SHMMNI; |
| 86 | ns->shm_tot = 0; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 87 | ipc_init_ids(ids); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 88 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 90 | static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp) |
| 91 | { |
| 92 | if (shp->shm_nattch){ |
| 93 | shp->shm_perm.mode |= SHM_DEST; |
| 94 | /* Do not find it any more */ |
| 95 | shp->shm_perm.key = IPC_PRIVATE; |
| 96 | shm_unlock(shp); |
| 97 | } else |
| 98 | shm_destroy(ns, shp); |
| 99 | } |
| 100 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 101 | int shm_init_ns(struct ipc_namespace *ns) |
| 102 | { |
| 103 | struct ipc_ids *ids; |
| 104 | |
| 105 | ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL); |
| 106 | if (ids == NULL) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | __shm_init_ns(ns, ids); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | void shm_exit_ns(struct ipc_namespace *ns) |
| 114 | { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 115 | struct shmid_kernel *shp; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 116 | int next_id; |
| 117 | int total, in_use; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 118 | |
| 119 | mutex_lock(&shm_ids(ns).mutex); |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 120 | |
| 121 | in_use = shm_ids(ns).in_use; |
| 122 | |
| 123 | for (total = 0, next_id = 0; total < in_use; next_id++) { |
| 124 | shp = idr_find(&shm_ids(ns).ipcs_idr, next_id); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 125 | if (shp == NULL) |
| 126 | continue; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 127 | ipc_lock_by_ptr(&shp->shm_perm); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 128 | do_shm_rmid(ns, shp); |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 129 | total++; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 130 | } |
| 131 | mutex_unlock(&shm_ids(ns).mutex); |
| 132 | |
| 133 | kfree(ns->ids[IPC_SHM_IDS]); |
| 134 | ns->ids[IPC_SHM_IDS] = NULL; |
| 135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | void __init shm_init (void) |
| 138 | { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 139 | __shm_init_ns(&init_ipc_ns, &init_shm_ids); |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 140 | ipc_init_proc_interface("sysvipc/shm", |
| 141 | " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n", |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 142 | IPC_SHM_IDS, sysvipc_shm_proc_show); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 145 | static inline int shm_checkid(struct ipc_namespace *ns, |
| 146 | struct shmid_kernel *s, int id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 148 | if (ipc_checkid(&shm_ids(ns), &s->shm_perm, id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | return -EIDRM; |
| 150 | return 0; |
| 151 | } |
| 152 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 153 | static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 155 | ipc_rmid(&shm_ids(ns), &s->shm_perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 158 | static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 160 | return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
| 164 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 165 | /* This is called by fork, once for every shm attach. */ |
| 166 | static void shm_open(struct vm_area_struct *vma) |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 167 | { |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 168 | struct file *file = vma->vm_file; |
| 169 | struct shm_file_data *sfd = shm_file_data(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | struct shmid_kernel *shp; |
| 171 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 172 | shp = shm_lock(sfd->ns, sfd->id); |
Eric Sesterhenn | 9ba025f | 2006-04-02 13:42:42 +0200 | [diff] [blame] | 173 | BUG_ON(!shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | shp->shm_atim = get_seconds(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 175 | shp->shm_lprid = task_tgid_vnr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | shp->shm_nattch++; |
| 177 | shm_unlock(shp); |
| 178 | } |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* |
| 181 | * shm_destroy - free the struct shmid_kernel |
| 182 | * |
| 183 | * @shp: struct to free |
| 184 | * |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 185 | * It has to be called with shp and shm_ids.mutex locked, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | * but returns with shp unlocked and freed. |
| 187 | */ |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 188 | static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 190 | ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 191 | shm_rmid(ns, shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | shm_unlock(shp); |
| 193 | if (!is_file_hugepages(shp->shm_file)) |
| 194 | shmem_lock(shp->shm_file, 0, shp->mlock_user); |
| 195 | else |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 196 | user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | shp->mlock_user); |
| 198 | fput (shp->shm_file); |
| 199 | security_shm_free(shp); |
| 200 | ipc_rcu_putref(shp); |
| 201 | } |
| 202 | |
| 203 | /* |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 204 | * remove the attach descriptor vma. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | * free memory for segment if it is marked destroyed. |
| 206 | * The descriptor has already been removed from the current->mm->mmap list |
| 207 | * and will later be kfree()d. |
| 208 | */ |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 209 | static void shm_close(struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 211 | struct file * file = vma->vm_file; |
| 212 | struct shm_file_data *sfd = shm_file_data(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | struct shmid_kernel *shp; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 214 | struct ipc_namespace *ns = sfd->ns; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 215 | |
| 216 | mutex_lock(&shm_ids(ns).mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* remove from the list of attaches of the shm segment */ |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 218 | shp = shm_lock(ns, sfd->id); |
Eric Sesterhenn | 9ba025f | 2006-04-02 13:42:42 +0200 | [diff] [blame] | 219 | BUG_ON(!shp); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 220 | shp->shm_lprid = task_tgid_vnr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | shp->shm_dtim = get_seconds(); |
| 222 | shp->shm_nattch--; |
| 223 | if(shp->shm_nattch == 0 && |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 224 | shp->shm_perm.mode & SHM_DEST) |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 225 | shm_destroy(ns, shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | else |
| 227 | shm_unlock(shp); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 228 | mutex_unlock(&shm_ids(ns).mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 231 | static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 232 | { |
| 233 | struct file *file = vma->vm_file; |
| 234 | struct shm_file_data *sfd = shm_file_data(file); |
| 235 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 236 | return sfd->vm_ops->fault(vma, vmf); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | #ifdef CONFIG_NUMA |
Adrian Bunk | d823e3e | 2007-10-16 23:26:42 -0700 | [diff] [blame] | 240 | static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new) |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 241 | { |
| 242 | struct file *file = vma->vm_file; |
| 243 | struct shm_file_data *sfd = shm_file_data(file); |
| 244 | int err = 0; |
| 245 | if (sfd->vm_ops->set_policy) |
| 246 | err = sfd->vm_ops->set_policy(vma, new); |
| 247 | return err; |
| 248 | } |
| 249 | |
Adrian Bunk | d823e3e | 2007-10-16 23:26:42 -0700 | [diff] [blame] | 250 | static struct mempolicy *shm_get_policy(struct vm_area_struct *vma, |
| 251 | unsigned long addr) |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 252 | { |
| 253 | struct file *file = vma->vm_file; |
| 254 | struct shm_file_data *sfd = shm_file_data(file); |
| 255 | struct mempolicy *pol = NULL; |
| 256 | |
| 257 | if (sfd->vm_ops->get_policy) |
| 258 | pol = sfd->vm_ops->get_policy(vma, addr); |
Adam Litke | 2274192 | 2007-06-16 10:16:15 -0700 | [diff] [blame] | 259 | else if (vma->vm_policy) |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 260 | pol = vma->vm_policy; |
Adam Litke | 2274192 | 2007-06-16 10:16:15 -0700 | [diff] [blame] | 261 | else |
| 262 | pol = current->mempolicy; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 263 | return pol; |
| 264 | } |
| 265 | #endif |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | static int shm_mmap(struct file * file, struct vm_area_struct * vma) |
| 268 | { |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 269 | struct shm_file_data *sfd = shm_file_data(file); |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 270 | int ret; |
| 271 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 272 | ret = sfd->file->f_op->mmap(sfd->file, vma); |
| 273 | if (ret != 0) |
| 274 | return ret; |
| 275 | sfd->vm_ops = vma->vm_ops; |
David Howells | 2e92a3b | 2007-07-31 00:37:24 -0700 | [diff] [blame] | 276 | #ifdef CONFIG_MMU |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 277 | BUG_ON(!sfd->vm_ops->fault); |
David Howells | 2e92a3b | 2007-07-31 00:37:24 -0700 | [diff] [blame] | 278 | #endif |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 279 | vma->vm_ops = &shm_vm_ops; |
| 280 | shm_open(vma); |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 281 | |
| 282 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 285 | static int shm_release(struct inode *ino, struct file *file) |
| 286 | { |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 287 | struct shm_file_data *sfd = shm_file_data(file); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 288 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 289 | put_ipc_ns(sfd->ns); |
| 290 | shm_file_data(file) = NULL; |
| 291 | kfree(sfd); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
Adam Litke | 516dffd | 2007-03-01 15:46:08 -0800 | [diff] [blame] | 295 | static int shm_fsync(struct file *file, struct dentry *dentry, int datasync) |
| 296 | { |
| 297 | int (*fsync) (struct file *, struct dentry *, int datasync); |
| 298 | struct shm_file_data *sfd = shm_file_data(file); |
| 299 | int ret = -EINVAL; |
| 300 | |
| 301 | fsync = sfd->file->f_op->fsync; |
| 302 | if (fsync) |
| 303 | ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync); |
| 304 | return ret; |
| 305 | } |
| 306 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 307 | static unsigned long shm_get_unmapped_area(struct file *file, |
| 308 | unsigned long addr, unsigned long len, unsigned long pgoff, |
| 309 | unsigned long flags) |
| 310 | { |
| 311 | struct shm_file_data *sfd = shm_file_data(file); |
Adam Litke | 516dffd | 2007-03-01 15:46:08 -0800 | [diff] [blame] | 312 | return get_unmapped_area(sfd->file, addr, len, pgoff, flags); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 313 | } |
Adam Litke | 516dffd | 2007-03-01 15:46:08 -0800 | [diff] [blame] | 314 | |
| 315 | int is_file_shm_hugepages(struct file *file) |
| 316 | { |
| 317 | int ret = 0; |
| 318 | |
| 319 | if (file->f_op == &shm_file_operations) { |
| 320 | struct shm_file_data *sfd; |
| 321 | sfd = shm_file_data(file); |
| 322 | ret = is_file_hugepages(sfd->file); |
| 323 | } |
| 324 | return ret; |
| 325 | } |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 326 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 327 | static const struct file_operations shm_file_operations = { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 328 | .mmap = shm_mmap, |
Adam Litke | 516dffd | 2007-03-01 15:46:08 -0800 | [diff] [blame] | 329 | .fsync = shm_fsync, |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 330 | .release = shm_release, |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 331 | .get_unmapped_area = shm_get_unmapped_area, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | static struct vm_operations_struct shm_vm_ops = { |
| 335 | .open = shm_open, /* callback for a new vm-area open */ |
| 336 | .close = shm_close, /* callback for when the vm-area is released */ |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 337 | .fault = shm_fault, |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 338 | #if defined(CONFIG_NUMA) |
| 339 | .set_policy = shm_set_policy, |
| 340 | .get_policy = shm_get_policy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | #endif |
| 342 | }; |
| 343 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 344 | static int newseg (struct ipc_namespace *ns, key_t key, int shmflg, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
| 346 | int error; |
| 347 | struct shmid_kernel *shp; |
| 348 | int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT; |
| 349 | struct file * file; |
| 350 | char name[13]; |
| 351 | int id; |
| 352 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 353 | if (size < SHMMIN || size > ns->shm_ctlmax) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return -EINVAL; |
| 355 | |
Guy Streeter | f66d45e | 2007-01-23 12:20:04 -0600 | [diff] [blame] | 356 | if (ns->shm_tot + numpages > ns->shm_ctlall) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return -ENOSPC; |
| 358 | |
| 359 | shp = ipc_rcu_alloc(sizeof(*shp)); |
| 360 | if (!shp) |
| 361 | return -ENOMEM; |
| 362 | |
| 363 | shp->shm_perm.key = key; |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 364 | shp->shm_perm.mode = (shmflg & S_IRWXUGO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | shp->mlock_user = NULL; |
| 366 | |
| 367 | shp->shm_perm.security = NULL; |
| 368 | error = security_shm_alloc(shp); |
| 369 | if (error) { |
| 370 | ipc_rcu_putref(shp); |
| 371 | return error; |
| 372 | } |
| 373 | |
Eric W. Biederman | 9d66586 | 2007-06-16 10:16:16 -0700 | [diff] [blame] | 374 | sprintf (name, "SYSV%08x", key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | if (shmflg & SHM_HUGETLB) { |
Eric W. Biederman | 9d66586 | 2007-06-16 10:16:16 -0700 | [diff] [blame] | 376 | /* hugetlb_file_setup takes care of mlock user accounting */ |
| 377 | file = hugetlb_file_setup(name, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | shp->mlock_user = current->user; |
| 379 | } else { |
Badari Pulavarty | bf8f972 | 2005-11-07 00:59:27 -0800 | [diff] [blame] | 380 | int acctflag = VM_ACCOUNT; |
| 381 | /* |
| 382 | * Do not allow no accounting for OVERCOMMIT_NEVER, even |
| 383 | * if it's asked for. |
| 384 | */ |
| 385 | if ((shmflg & SHM_NORESERVE) && |
| 386 | sysctl_overcommit_memory != OVERCOMMIT_NEVER) |
| 387 | acctflag = 0; |
Badari Pulavarty | bf8f972 | 2005-11-07 00:59:27 -0800 | [diff] [blame] | 388 | file = shmem_file_setup(name, size, acctflag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | error = PTR_ERR(file); |
| 391 | if (IS_ERR(file)) |
| 392 | goto no_file; |
| 393 | |
| 394 | error = -ENOSPC; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 395 | id = shm_addid(ns, shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | if(id == -1) |
| 397 | goto no_id; |
| 398 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 399 | shp->shm_cprid = task_tgid_vnr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | shp->shm_lprid = 0; |
| 401 | shp->shm_atim = shp->shm_dtim = 0; |
| 402 | shp->shm_ctim = get_seconds(); |
| 403 | shp->shm_segsz = size; |
| 404 | shp->shm_nattch = 0; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 405 | shp->shm_perm.id = shm_buildid(ns, id, shp->shm_perm.seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | shp->shm_file = file; |
Badari Pulavarty | 30475cc | 2007-06-16 10:15:59 -0700 | [diff] [blame] | 407 | /* |
| 408 | * shmid gets reported as "inode#" in /proc/pid/maps. |
| 409 | * proc-ps tools use this. Changing this will break them. |
| 410 | */ |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 411 | file->f_dentry->d_inode->i_ino = shp->shm_perm.id; |
Krishnakumar R | 551110a | 2005-10-29 18:16:45 -0700 | [diff] [blame] | 412 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 413 | ns->shm_tot += numpages; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 414 | error = shp->shm_perm.id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | shm_unlock(shp); |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 416 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
| 418 | no_id: |
| 419 | fput(file); |
| 420 | no_file: |
| 421 | security_shm_free(shp); |
| 422 | ipc_rcu_putref(shp); |
| 423 | return error; |
| 424 | } |
| 425 | |
| 426 | asmlinkage long sys_shmget (key_t key, size_t size, int shmflg) |
| 427 | { |
| 428 | struct shmid_kernel *shp; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 429 | int err; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 430 | struct ipc_namespace *ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 432 | ns = current->nsproxy->ipc_ns; |
| 433 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 434 | err = idr_pre_get(&shm_ids(ns).ipcs_idr, GFP_KERNEL); |
| 435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | if (key == IPC_PRIVATE) { |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 437 | if (!err) |
| 438 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | else { |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 440 | mutex_lock(&shm_ids(ns).mutex); |
| 441 | err = newseg(ns, key, shmflg, size); |
| 442 | mutex_unlock(&shm_ids(ns).mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 444 | } else { |
| 445 | mutex_lock(&shm_ids(ns).mutex); |
| 446 | shp = (struct shmid_kernel *) ipc_findkey(&shm_ids(ns), key); |
| 447 | if (shp == NULL) { |
| 448 | if (!(shmflg & IPC_CREAT)) |
| 449 | err = -ENOENT; |
| 450 | else if (!err) |
| 451 | err = -ENOMEM; |
| 452 | else |
| 453 | err = newseg(ns, key, shmflg, size); |
| 454 | } else { |
| 455 | /* shp has been locked by ipc_findkey() */ |
| 456 | |
| 457 | if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) |
| 458 | err = -EEXIST; |
| 459 | else { |
| 460 | if (shp->shm_segsz < size) |
| 461 | err = -EINVAL; |
| 462 | else if (ipcperms(&shp->shm_perm, shmflg)) |
| 463 | err = -EACCES; |
| 464 | else { |
| 465 | err = security_shm_associate(shp, |
| 466 | shmflg); |
| 467 | if (!err) |
| 468 | err = shp->shm_perm.id; |
| 469 | } |
| 470 | } |
| 471 | shm_unlock(shp); |
| 472 | } |
| 473 | mutex_unlock(&shm_ids(ns).mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | return err; |
| 477 | } |
| 478 | |
| 479 | static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version) |
| 480 | { |
| 481 | switch(version) { |
| 482 | case IPC_64: |
| 483 | return copy_to_user(buf, in, sizeof(*in)); |
| 484 | case IPC_OLD: |
| 485 | { |
| 486 | struct shmid_ds out; |
| 487 | |
| 488 | ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm); |
| 489 | out.shm_segsz = in->shm_segsz; |
| 490 | out.shm_atime = in->shm_atime; |
| 491 | out.shm_dtime = in->shm_dtime; |
| 492 | out.shm_ctime = in->shm_ctime; |
| 493 | out.shm_cpid = in->shm_cpid; |
| 494 | out.shm_lpid = in->shm_lpid; |
| 495 | out.shm_nattch = in->shm_nattch; |
| 496 | |
| 497 | return copy_to_user(buf, &out, sizeof(out)); |
| 498 | } |
| 499 | default: |
| 500 | return -EINVAL; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | struct shm_setbuf { |
| 505 | uid_t uid; |
| 506 | gid_t gid; |
| 507 | mode_t mode; |
| 508 | }; |
| 509 | |
| 510 | static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version) |
| 511 | { |
| 512 | switch(version) { |
| 513 | case IPC_64: |
| 514 | { |
| 515 | struct shmid64_ds tbuf; |
| 516 | |
| 517 | if (copy_from_user(&tbuf, buf, sizeof(tbuf))) |
| 518 | return -EFAULT; |
| 519 | |
| 520 | out->uid = tbuf.shm_perm.uid; |
| 521 | out->gid = tbuf.shm_perm.gid; |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 522 | out->mode = tbuf.shm_perm.mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | case IPC_OLD: |
| 527 | { |
| 528 | struct shmid_ds tbuf_old; |
| 529 | |
| 530 | if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old))) |
| 531 | return -EFAULT; |
| 532 | |
| 533 | out->uid = tbuf_old.shm_perm.uid; |
| 534 | out->gid = tbuf_old.shm_perm.gid; |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 535 | out->mode = tbuf_old.shm_perm.mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | default: |
| 540 | return -EINVAL; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version) |
| 545 | { |
| 546 | switch(version) { |
| 547 | case IPC_64: |
| 548 | return copy_to_user(buf, in, sizeof(*in)); |
| 549 | case IPC_OLD: |
| 550 | { |
| 551 | struct shminfo out; |
| 552 | |
| 553 | if(in->shmmax > INT_MAX) |
| 554 | out.shmmax = INT_MAX; |
| 555 | else |
| 556 | out.shmmax = (int)in->shmmax; |
| 557 | |
| 558 | out.shmmin = in->shmmin; |
| 559 | out.shmmni = in->shmmni; |
| 560 | out.shmseg = in->shmseg; |
| 561 | out.shmall = in->shmall; |
| 562 | |
| 563 | return copy_to_user(buf, &out, sizeof(out)); |
| 564 | } |
| 565 | default: |
| 566 | return -EINVAL; |
| 567 | } |
| 568 | } |
| 569 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 570 | static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss, |
| 571 | unsigned long *swp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 573 | int next_id; |
| 574 | int total, in_use; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
| 576 | *rss = 0; |
| 577 | *swp = 0; |
| 578 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 579 | in_use = shm_ids(ns).in_use; |
| 580 | |
| 581 | for (total = 0, next_id = 0; total < in_use; next_id++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | struct shmid_kernel *shp; |
| 583 | struct inode *inode; |
| 584 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 585 | shp = shm_get(ns, next_id); |
| 586 | if (shp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | continue; |
| 588 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 589 | inode = shp->shm_file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
| 591 | if (is_file_hugepages(shp->shm_file)) { |
| 592 | struct address_space *mapping = inode->i_mapping; |
| 593 | *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages; |
| 594 | } else { |
| 595 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 596 | spin_lock(&info->lock); |
| 597 | *rss += inode->i_mapping->nrpages; |
| 598 | *swp += info->swapped; |
| 599 | spin_unlock(&info->lock); |
| 600 | } |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 601 | |
| 602 | total++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
| 606 | asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf) |
| 607 | { |
| 608 | struct shm_setbuf setbuf; |
| 609 | struct shmid_kernel *shp; |
| 610 | int err, version; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 611 | struct ipc_namespace *ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | if (cmd < 0 || shmid < 0) { |
| 614 | err = -EINVAL; |
| 615 | goto out; |
| 616 | } |
| 617 | |
| 618 | version = ipc_parse_version(&cmd); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 619 | ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
| 621 | switch (cmd) { /* replace with proc interface ? */ |
| 622 | case IPC_INFO: |
| 623 | { |
| 624 | struct shminfo64 shminfo; |
| 625 | |
| 626 | err = security_shm_shmctl(NULL, cmd); |
| 627 | if (err) |
| 628 | return err; |
| 629 | |
| 630 | memset(&shminfo,0,sizeof(shminfo)); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 631 | shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni; |
| 632 | shminfo.shmmax = ns->shm_ctlmax; |
| 633 | shminfo.shmall = ns->shm_ctlall; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
| 635 | shminfo.shmmin = SHMMIN; |
| 636 | if(copy_shminfo_to_user (buf, &shminfo, version)) |
| 637 | return -EFAULT; |
| 638 | /* reading a integer is always atomic */ |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 639 | err = ipc_get_maxid(&shm_ids(ns)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | if(err<0) |
| 641 | err = 0; |
| 642 | goto out; |
| 643 | } |
| 644 | case SHM_INFO: |
| 645 | { |
| 646 | struct shm_info shm_info; |
| 647 | |
| 648 | err = security_shm_shmctl(NULL, cmd); |
| 649 | if (err) |
| 650 | return err; |
| 651 | |
| 652 | memset(&shm_info,0,sizeof(shm_info)); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 653 | mutex_lock(&shm_ids(ns).mutex); |
| 654 | shm_info.used_ids = shm_ids(ns).in_use; |
| 655 | shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp); |
| 656 | shm_info.shm_tot = ns->shm_tot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | shm_info.swap_attempts = 0; |
| 658 | shm_info.swap_successes = 0; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 659 | err = ipc_get_maxid(&shm_ids(ns)); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 660 | mutex_unlock(&shm_ids(ns).mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | if(copy_to_user (buf, &shm_info, sizeof(shm_info))) { |
| 662 | err = -EFAULT; |
| 663 | goto out; |
| 664 | } |
| 665 | |
| 666 | err = err < 0 ? 0 : err; |
| 667 | goto out; |
| 668 | } |
| 669 | case SHM_STAT: |
| 670 | case IPC_STAT: |
| 671 | { |
| 672 | struct shmid64_ds tbuf; |
| 673 | int result; |
| 674 | memset(&tbuf, 0, sizeof(tbuf)); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 675 | shp = shm_lock(ns, shmid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if(shp==NULL) { |
| 677 | err = -EINVAL; |
| 678 | goto out; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 679 | } else if (cmd == SHM_STAT) { |
| 680 | result = shp->shm_perm.id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } else { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 682 | err = shm_checkid(ns, shp,shmid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | if(err) |
| 684 | goto out_unlock; |
| 685 | result = 0; |
| 686 | } |
| 687 | err=-EACCES; |
| 688 | if (ipcperms (&shp->shm_perm, S_IRUGO)) |
| 689 | goto out_unlock; |
| 690 | err = security_shm_shmctl(shp, cmd); |
| 691 | if (err) |
| 692 | goto out_unlock; |
| 693 | kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm); |
| 694 | tbuf.shm_segsz = shp->shm_segsz; |
| 695 | tbuf.shm_atime = shp->shm_atim; |
| 696 | tbuf.shm_dtime = shp->shm_dtim; |
| 697 | tbuf.shm_ctime = shp->shm_ctim; |
| 698 | tbuf.shm_cpid = shp->shm_cprid; |
| 699 | tbuf.shm_lpid = shp->shm_lprid; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 700 | tbuf.shm_nattch = shp->shm_nattch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | shm_unlock(shp); |
| 702 | if(copy_shmid_to_user (buf, &tbuf, version)) |
| 703 | err = -EFAULT; |
| 704 | else |
| 705 | err = result; |
| 706 | goto out; |
| 707 | } |
| 708 | case SHM_LOCK: |
| 709 | case SHM_UNLOCK: |
| 710 | { |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 711 | shp = shm_lock(ns, shmid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | if(shp==NULL) { |
| 713 | err = -EINVAL; |
| 714 | goto out; |
| 715 | } |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 716 | err = shm_checkid(ns, shp,shmid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | if(err) |
| 718 | goto out_unlock; |
| 719 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 720 | err = audit_ipc_obj(&(shp->shm_perm)); |
| 721 | if (err) |
| 722 | goto out_unlock; |
| 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | if (!capable(CAP_IPC_LOCK)) { |
| 725 | err = -EPERM; |
| 726 | if (current->euid != shp->shm_perm.uid && |
| 727 | current->euid != shp->shm_perm.cuid) |
| 728 | goto out_unlock; |
| 729 | if (cmd == SHM_LOCK && |
| 730 | !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur) |
| 731 | goto out_unlock; |
| 732 | } |
| 733 | |
| 734 | err = security_shm_shmctl(shp, cmd); |
| 735 | if (err) |
| 736 | goto out_unlock; |
| 737 | |
| 738 | if(cmd==SHM_LOCK) { |
| 739 | struct user_struct * user = current->user; |
| 740 | if (!is_file_hugepages(shp->shm_file)) { |
| 741 | err = shmem_lock(shp->shm_file, 1, user); |
Pavel Emelianov | 7be77e2 | 2007-07-31 00:38:48 -0700 | [diff] [blame] | 742 | if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){ |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 743 | shp->shm_perm.mode |= SHM_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | shp->mlock_user = user; |
| 745 | } |
| 746 | } |
| 747 | } else if (!is_file_hugepages(shp->shm_file)) { |
| 748 | shmem_lock(shp->shm_file, 0, shp->mlock_user); |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 749 | shp->shm_perm.mode &= ~SHM_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | shp->mlock_user = NULL; |
| 751 | } |
| 752 | shm_unlock(shp); |
| 753 | goto out; |
| 754 | } |
| 755 | case IPC_RMID: |
| 756 | { |
| 757 | /* |
| 758 | * We cannot simply remove the file. The SVID states |
| 759 | * that the block remains until the last person |
| 760 | * detaches from it, then is deleted. A shmat() on |
| 761 | * an RMID segment is legal in older Linux and if |
| 762 | * we change it apps break... |
| 763 | * |
| 764 | * Instead we set a destroyed flag, and then blow |
| 765 | * the name away when the usage hits zero. |
| 766 | */ |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 767 | mutex_lock(&shm_ids(ns).mutex); |
| 768 | shp = shm_lock(ns, shmid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | err = -EINVAL; |
| 770 | if (shp == NULL) |
| 771 | goto out_up; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 772 | err = shm_checkid(ns, shp, shmid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | if(err) |
| 774 | goto out_unlock_up; |
| 775 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 776 | err = audit_ipc_obj(&(shp->shm_perm)); |
| 777 | if (err) |
| 778 | goto out_unlock_up; |
| 779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | if (current->euid != shp->shm_perm.uid && |
| 781 | current->euid != shp->shm_perm.cuid && |
| 782 | !capable(CAP_SYS_ADMIN)) { |
| 783 | err=-EPERM; |
| 784 | goto out_unlock_up; |
| 785 | } |
| 786 | |
| 787 | err = security_shm_shmctl(shp, cmd); |
| 788 | if (err) |
| 789 | goto out_unlock_up; |
| 790 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 791 | do_shm_rmid(ns, shp); |
| 792 | mutex_unlock(&shm_ids(ns).mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | goto out; |
| 794 | } |
| 795 | |
| 796 | case IPC_SET: |
| 797 | { |
| 798 | if (copy_shmid_from_user (&setbuf, buf, version)) { |
| 799 | err = -EFAULT; |
| 800 | goto out; |
| 801 | } |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 802 | mutex_lock(&shm_ids(ns).mutex); |
| 803 | shp = shm_lock(ns, shmid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | err=-EINVAL; |
| 805 | if(shp==NULL) |
| 806 | goto out_up; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 807 | err = shm_checkid(ns, shp,shmid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | if(err) |
| 809 | goto out_unlock_up; |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 810 | err = audit_ipc_obj(&(shp->shm_perm)); |
| 811 | if (err) |
| 812 | goto out_unlock_up; |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 813 | err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode); |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 814 | if (err) |
| 815 | goto out_unlock_up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | err=-EPERM; |
| 817 | if (current->euid != shp->shm_perm.uid && |
| 818 | current->euid != shp->shm_perm.cuid && |
| 819 | !capable(CAP_SYS_ADMIN)) { |
| 820 | goto out_unlock_up; |
| 821 | } |
| 822 | |
| 823 | err = security_shm_shmctl(shp, cmd); |
| 824 | if (err) |
| 825 | goto out_unlock_up; |
| 826 | |
| 827 | shp->shm_perm.uid = setbuf.uid; |
| 828 | shp->shm_perm.gid = setbuf.gid; |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 829 | shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | | (setbuf.mode & S_IRWXUGO); |
| 831 | shp->shm_ctim = get_seconds(); |
| 832 | break; |
| 833 | } |
| 834 | |
| 835 | default: |
| 836 | err = -EINVAL; |
| 837 | goto out; |
| 838 | } |
| 839 | |
| 840 | err = 0; |
| 841 | out_unlock_up: |
| 842 | shm_unlock(shp); |
| 843 | out_up: |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 844 | mutex_unlock(&shm_ids(ns).mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | goto out; |
| 846 | out_unlock: |
| 847 | shm_unlock(shp); |
| 848 | out: |
| 849 | return err; |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists. |
| 854 | * |
| 855 | * NOTE! Despite the name, this is NOT a direct system call entrypoint. The |
| 856 | * "raddr" thing points to kernel space, and there has to be a wrapper around |
| 857 | * this. |
| 858 | */ |
| 859 | long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr) |
| 860 | { |
| 861 | struct shmid_kernel *shp; |
| 862 | unsigned long addr; |
| 863 | unsigned long size; |
| 864 | struct file * file; |
| 865 | int err; |
| 866 | unsigned long flags; |
| 867 | unsigned long prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | int acc_mode; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 869 | unsigned long user_addr; |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 870 | struct ipc_namespace *ns; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 871 | struct shm_file_data *sfd; |
| 872 | struct path path; |
| 873 | mode_t f_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 875 | err = -EINVAL; |
| 876 | if (shmid < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | goto out; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 878 | else if ((addr = (ulong)shmaddr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | if (addr & (SHMLBA-1)) { |
| 880 | if (shmflg & SHM_RND) |
| 881 | addr &= ~(SHMLBA-1); /* round down */ |
| 882 | else |
| 883 | #ifndef __ARCH_FORCE_SHMLBA |
| 884 | if (addr & ~PAGE_MASK) |
| 885 | #endif |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 886 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } |
| 888 | flags = MAP_SHARED | MAP_FIXED; |
| 889 | } else { |
| 890 | if ((shmflg & SHM_REMAP)) |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 891 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
| 893 | flags = MAP_SHARED; |
| 894 | } |
| 895 | |
| 896 | if (shmflg & SHM_RDONLY) { |
| 897 | prot = PROT_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | acc_mode = S_IRUGO; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 899 | f_mode = FMODE_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } else { |
| 901 | prot = PROT_READ | PROT_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | acc_mode = S_IRUGO | S_IWUGO; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 903 | f_mode = FMODE_READ | FMODE_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | } |
| 905 | if (shmflg & SHM_EXEC) { |
| 906 | prot |= PROT_EXEC; |
| 907 | acc_mode |= S_IXUGO; |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * We cannot rely on the fs check since SYSV IPC does have an |
| 912 | * additional creator id... |
| 913 | */ |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 914 | ns = current->nsproxy->ipc_ns; |
| 915 | shp = shm_lock(ns, shmid); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 916 | if(shp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | goto out; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 918 | |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 919 | err = shm_checkid(ns, shp,shmid); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 920 | if (err) |
| 921 | goto out_unlock; |
| 922 | |
| 923 | err = -EACCES; |
| 924 | if (ipcperms(&shp->shm_perm, acc_mode)) |
| 925 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
| 927 | err = security_shm_shmat(shp, shmaddr, shmflg); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 928 | if (err) |
| 929 | goto out_unlock; |
| 930 | |
| 931 | path.dentry = dget(shp->shm_file->f_path.dentry); |
Dave Hansen | ce8d2cd | 2007-10-16 23:31:13 -0700 | [diff] [blame] | 932 | path.mnt = shp->shm_file->f_path.mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | shp->shm_nattch++; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 934 | size = i_size_read(path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | shm_unlock(shp); |
| 936 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 937 | err = -ENOMEM; |
| 938 | sfd = kzalloc(sizeof(*sfd), GFP_KERNEL); |
| 939 | if (!sfd) |
Dave Hansen | ce8d2cd | 2007-10-16 23:31:13 -0700 | [diff] [blame] | 940 | goto out_put_dentry; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 941 | |
| 942 | err = -ENOMEM; |
Dave Hansen | ce8d2cd | 2007-10-16 23:31:13 -0700 | [diff] [blame] | 943 | |
| 944 | file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 945 | if (!file) |
| 946 | goto out_free; |
| 947 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 948 | file->private_data = sfd; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 949 | file->f_mapping = shp->shm_file->f_mapping; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 950 | sfd->id = shp->shm_perm.id; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 951 | sfd->ns = get_ipc_ns(ns); |
| 952 | sfd->file = shp->shm_file; |
| 953 | sfd->vm_ops = NULL; |
| 954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | down_write(¤t->mm->mmap_sem); |
| 956 | if (addr && !(shmflg & SHM_REMAP)) { |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 957 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | if (find_vma_intersection(current->mm, addr, addr + size)) |
| 959 | goto invalid; |
| 960 | /* |
| 961 | * If shm segment goes below stack, make sure there is some |
| 962 | * space left for the stack to grow (at least 4 pages). |
| 963 | */ |
| 964 | if (addr < current->mm->start_stack && |
| 965 | addr > current->mm->start_stack - size - PAGE_SIZE * 5) |
| 966 | goto invalid; |
| 967 | } |
| 968 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 969 | user_addr = do_mmap (file, addr, size, prot, flags, 0); |
| 970 | *raddr = user_addr; |
| 971 | err = 0; |
| 972 | if (IS_ERR_VALUE(user_addr)) |
| 973 | err = (long)user_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | invalid: |
| 975 | up_write(¤t->mm->mmap_sem); |
| 976 | |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 977 | fput(file); |
| 978 | |
| 979 | out_nattch: |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 980 | mutex_lock(&shm_ids(ns).mutex); |
| 981 | shp = shm_lock(ns, shmid); |
Eric Sesterhenn | 9ba025f | 2006-04-02 13:42:42 +0200 | [diff] [blame] | 982 | BUG_ON(!shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | shp->shm_nattch--; |
| 984 | if(shp->shm_nattch == 0 && |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 985 | shp->shm_perm.mode & SHM_DEST) |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 986 | shm_destroy(ns, shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | else |
| 988 | shm_unlock(shp); |
Kirill Korotaev | 4e98231 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 989 | mutex_unlock(&shm_ids(ns).mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | out: |
| 992 | return err; |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 993 | |
| 994 | out_unlock: |
| 995 | shm_unlock(shp); |
| 996 | goto out; |
| 997 | |
| 998 | out_free: |
| 999 | kfree(sfd); |
Dave Hansen | ce8d2cd | 2007-10-16 23:31:13 -0700 | [diff] [blame] | 1000 | out_put_dentry: |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1001 | dput(path.dentry); |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1002 | goto out_nattch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
Stephen Rothwell | 7d87e14c | 2005-05-01 08:59:12 -0700 | [diff] [blame] | 1005 | asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg) |
| 1006 | { |
| 1007 | unsigned long ret; |
| 1008 | long err; |
| 1009 | |
| 1010 | err = do_shmat(shmid, shmaddr, shmflg, &ret); |
| 1011 | if (err) |
| 1012 | return err; |
| 1013 | force_successful_syscall_return(); |
| 1014 | return (long)ret; |
| 1015 | } |
| 1016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | /* |
| 1018 | * detach and kill segment if marked destroyed. |
| 1019 | * The work is done in shm_close. |
| 1020 | */ |
| 1021 | asmlinkage long sys_shmdt(char __user *shmaddr) |
| 1022 | { |
| 1023 | struct mm_struct *mm = current->mm; |
| 1024 | struct vm_area_struct *vma, *next; |
| 1025 | unsigned long addr = (unsigned long)shmaddr; |
| 1026 | loff_t size = 0; |
| 1027 | int retval = -EINVAL; |
| 1028 | |
Hugh Dickins | df1e2fb | 2006-03-24 03:18:06 -0800 | [diff] [blame] | 1029 | if (addr & ~PAGE_MASK) |
| 1030 | return retval; |
| 1031 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | down_write(&mm->mmap_sem); |
| 1033 | |
| 1034 | /* |
| 1035 | * This function tries to be smart and unmap shm segments that |
| 1036 | * were modified by partial mlock or munmap calls: |
| 1037 | * - It first determines the size of the shm segment that should be |
| 1038 | * unmapped: It searches for a vma that is backed by shm and that |
| 1039 | * started at address shmaddr. It records it's size and then unmaps |
| 1040 | * it. |
| 1041 | * - Then it unmaps all shm vmas that started at shmaddr and that |
| 1042 | * are within the initially determined size. |
| 1043 | * Errors from do_munmap are ignored: the function only fails if |
| 1044 | * it's called with invalid parameters or if it's called to unmap |
| 1045 | * a part of a vma. Both calls in this function are for full vmas, |
| 1046 | * the parameters are directly copied from the vma itself and always |
| 1047 | * valid - therefore do_munmap cannot fail. (famous last words?) |
| 1048 | */ |
| 1049 | /* |
| 1050 | * If it had been mremap()'d, the starting address would not |
| 1051 | * match the usual checks anyway. So assume all vma's are |
| 1052 | * above the starting address given. |
| 1053 | */ |
| 1054 | vma = find_vma(mm, addr); |
| 1055 | |
| 1056 | while (vma) { |
| 1057 | next = vma->vm_next; |
| 1058 | |
| 1059 | /* |
| 1060 | * Check if the starting address would match, i.e. it's |
| 1061 | * a fragment created by mprotect() and/or munmap(), or it |
| 1062 | * otherwise it starts at this address with no hassles. |
| 1063 | */ |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1064 | if ((vma->vm_ops == &shm_vm_ops) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) { |
| 1066 | |
| 1067 | |
Josef Sipek | 6d63079 | 2006-12-08 02:37:11 -0800 | [diff] [blame] | 1068 | size = vma->vm_file->f_path.dentry->d_inode->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start); |
| 1070 | /* |
| 1071 | * We discovered the size of the shm segment, so |
| 1072 | * break out of here and fall through to the next |
| 1073 | * loop that uses the size information to stop |
| 1074 | * searching for matching vma's. |
| 1075 | */ |
| 1076 | retval = 0; |
| 1077 | vma = next; |
| 1078 | break; |
| 1079 | } |
| 1080 | vma = next; |
| 1081 | } |
| 1082 | |
| 1083 | /* |
| 1084 | * We need look no further than the maximum address a fragment |
| 1085 | * could possibly have landed at. Also cast things to loff_t to |
| 1086 | * prevent overflows and make comparisions vs. equal-width types. |
| 1087 | */ |
KAMEZAWA Hiroyuki | 8e36709 | 2006-02-10 01:51:12 -0800 | [diff] [blame] | 1088 | size = PAGE_ALIGN(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | while (vma && (loff_t)(vma->vm_end - addr) <= size) { |
| 1090 | next = vma->vm_next; |
| 1091 | |
| 1092 | /* finding a matching vma now does not alter retval */ |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1093 | if ((vma->vm_ops == &shm_vm_ops) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) |
| 1095 | |
| 1096 | do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start); |
| 1097 | vma = next; |
| 1098 | } |
| 1099 | |
| 1100 | up_write(&mm->mmap_sem); |
| 1101 | return retval; |
| 1102 | } |
| 1103 | |
| 1104 | #ifdef CONFIG_PROC_FS |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1105 | static int sysvipc_shm_proc_show(struct seq_file *s, void *it) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | { |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1107 | struct shmid_kernel *shp = it; |
| 1108 | char *format; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n" |
| 1111 | #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1113 | if (sizeof(size_t) <= sizeof(int)) |
| 1114 | format = SMALL_STRING; |
| 1115 | else |
| 1116 | format = BIG_STRING; |
| 1117 | return seq_printf(s, format, |
| 1118 | shp->shm_perm.key, |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame^] | 1119 | shp->shm_perm.id, |
Andrew Morton | b33291c | 2006-01-08 01:02:21 -0800 | [diff] [blame] | 1120 | shp->shm_perm.mode, |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1121 | shp->shm_segsz, |
| 1122 | shp->shm_cprid, |
| 1123 | shp->shm_lprid, |
Eric W. Biederman | bc56bba | 2007-02-20 13:57:53 -0800 | [diff] [blame] | 1124 | shp->shm_nattch, |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1125 | shp->shm_perm.uid, |
| 1126 | shp->shm_perm.gid, |
| 1127 | shp->shm_perm.cuid, |
| 1128 | shp->shm_perm.cgid, |
| 1129 | shp->shm_atim, |
| 1130 | shp->shm_dtim, |
| 1131 | shp->shm_ctim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | } |
| 1133 | #endif |