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