blob: e2de16efe10ddaa45c1667874c64cb3dc6410860 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Grubb073115d2006-04-02 17:07:33 -040016 * support for audit of ipc object properties and permission changes
17 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaev4e982312006-10-02 02:18:22 -070018 *
19 * namespaces support
20 * OpenVZ, SWsoft Inc.
21 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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 Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/shmem_fs.h>
32#include <linux/security.h>
33#include <linux/syscalls.h>
34#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -070036#include <linux/ptrace.h>
Mike Waychison19b49462005-09-06 15:17:10 -070037#include <linux/seq_file.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080038#include <linux/mutex.h>
Kirill Korotaev4e982312006-10-02 02:18:22 -070039#include <linux/nsproxy.h>
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -080040#include <linux/mount.h>
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
43
44#include "util.h"
45
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -080046struct 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 Ven9a321442007-02-12 00:55:35 -080055static const struct file_operations shm_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static struct vm_operations_struct shm_vm_ops;
57
Kirill Korotaev4e982312006-10-02 02:18:22 -070058static struct ipc_ids init_shm_ids;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Kirill Korotaev4e982312006-10-02 02:18:22 -070060#define shm_ids(ns) (*((ns)->ids[IPC_SHM_IDS]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Kirill Korotaev4e982312006-10-02 02:18:22 -070062#define shm_unlock(shp) \
63 ipc_unlock(&(shp)->shm_perm)
Kirill Korotaev4e982312006-10-02 02:18:22 -070064#define shm_buildid(ns, id, seq) \
65 ipc_buildid(&shm_ids(ns), id, seq)
66
Nadia Derbey7748dbf2007-10-18 23:40:49 -070067static int newseg(struct ipc_namespace *, struct ipc_params *);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -080068static void shm_open(struct vm_area_struct *vma);
69static void shm_close(struct vm_area_struct *vma);
Kirill Korotaev4e982312006-10-02 02:18:22 -070070static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070072static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070075static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Kirill Korotaev4e982312006-10-02 02:18:22 -070076{
77 ns->ids[IPC_SHM_IDS] = ids;
78 ns->shm_ctlmax = SHMMAX;
79 ns->shm_ctlall = SHMALL;
80 ns->shm_ctlmni = SHMMNI;
81 ns->shm_tot = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -070082 ipc_init_ids(ids);
Kirill Korotaev4e982312006-10-02 02:18:22 -070083}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Kirill Korotaev4e982312006-10-02 02:18:22 -070085static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp)
86{
87 if (shp->shm_nattch){
88 shp->shm_perm.mode |= SHM_DEST;
89 /* Do not find it any more */
90 shp->shm_perm.key = IPC_PRIVATE;
91 shm_unlock(shp);
92 } else
93 shm_destroy(ns, shp);
94}
95
Kirill Korotaev4e982312006-10-02 02:18:22 -070096int shm_init_ns(struct ipc_namespace *ns)
97{
98 struct ipc_ids *ids;
99
100 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
101 if (ids == NULL)
102 return -ENOMEM;
103
104 __shm_init_ns(ns, ids);
105 return 0;
106}
107
108void shm_exit_ns(struct ipc_namespace *ns)
109{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700110 struct shmid_kernel *shp;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700111 int next_id;
112 int total, in_use;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700113
114 mutex_lock(&shm_ids(ns).mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700115
116 in_use = shm_ids(ns).in_use;
117
118 for (total = 0, next_id = 0; total < in_use; next_id++) {
119 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700120 if (shp == NULL)
121 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700122 ipc_lock_by_ptr(&shp->shm_perm);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700123 do_shm_rmid(ns, shp);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700124 total++;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700125 }
126 mutex_unlock(&shm_ids(ns).mutex);
127
128 kfree(ns->ids[IPC_SHM_IDS]);
129 ns->ids[IPC_SHM_IDS] = NULL;
130}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132void __init shm_init (void)
133{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700134 __shm_init_ns(&init_ipc_ns, &init_shm_ids);
Mike Waychison19b49462005-09-06 15:17:10 -0700135 ipc_init_proc_interface("sysvipc/shm",
136 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
Kirill Korotaev4e982312006-10-02 02:18:22 -0700137 IPC_SHM_IDS, sysvipc_shm_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Nadia Derbey023a5352007-10-18 23:40:51 -0700140static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700142 struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
143
144 return container_of(ipcp, struct shmid_kernel, shm_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700145}
146
147static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns,
148 int id)
149{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700150 struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id);
151
152 return container_of(ipcp, struct shmid_kernel, shm_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700155static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700157 ipc_rmid(&shm_ids(ns), &s->shm_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Kirill Korotaev4e982312006-10-02 02:18:22 -0700160static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700162 return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165
166
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800167/* This is called by fork, once for every shm attach. */
168static void shm_open(struct vm_area_struct *vma)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700169{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800170 struct file *file = vma->vm_file;
171 struct shm_file_data *sfd = shm_file_data(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct shmid_kernel *shp;
173
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800174 shp = shm_lock(sfd->ns, sfd->id);
Nadia Derbey023a5352007-10-18 23:40:51 -0700175 BUG_ON(IS_ERR(shp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 shp->shm_atim = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700177 shp->shm_lprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 shp->shm_nattch++;
179 shm_unlock(shp);
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
183 * shm_destroy - free the struct shmid_kernel
184 *
185 * @shp: struct to free
186 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800187 * It has to be called with shp and shm_ids.mutex locked,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * but returns with shp unlocked and freed.
189 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700190static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700192 ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700193 shm_rmid(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 shm_unlock(shp);
195 if (!is_file_hugepages(shp->shm_file))
196 shmem_lock(shp->shm_file, 0, shp->mlock_user);
197 else
Josef Sipek6d630792006-12-08 02:37:11 -0800198 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 shp->mlock_user);
200 fput (shp->shm_file);
201 security_shm_free(shp);
202 ipc_rcu_putref(shp);
203}
204
205/*
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800206 * remove the attach descriptor vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * free memory for segment if it is marked destroyed.
208 * The descriptor has already been removed from the current->mm->mmap list
209 * and will later be kfree()d.
210 */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800211static void shm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800213 struct file * file = vma->vm_file;
214 struct shm_file_data *sfd = shm_file_data(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct shmid_kernel *shp;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800216 struct ipc_namespace *ns = sfd->ns;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700217
218 mutex_lock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* remove from the list of attaches of the shm segment */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800220 shp = shm_lock(ns, sfd->id);
Nadia Derbey023a5352007-10-18 23:40:51 -0700221 BUG_ON(IS_ERR(shp));
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700222 shp->shm_lprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 shp->shm_dtim = get_seconds();
224 shp->shm_nattch--;
225 if(shp->shm_nattch == 0 &&
Andrew Mortonb33291c2006-01-08 01:02:21 -0800226 shp->shm_perm.mode & SHM_DEST)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700227 shm_destroy(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 else
229 shm_unlock(shp);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700230 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Nick Piggind0217ac2007-07-19 01:47:03 -0700233static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800234{
235 struct file *file = vma->vm_file;
236 struct shm_file_data *sfd = shm_file_data(file);
237
Nick Piggind0217ac2007-07-19 01:47:03 -0700238 return sfd->vm_ops->fault(vma, vmf);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800239}
240
241#ifdef CONFIG_NUMA
Adrian Bunkd823e3e2007-10-16 23:26:42 -0700242static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800243{
244 struct file *file = vma->vm_file;
245 struct shm_file_data *sfd = shm_file_data(file);
246 int err = 0;
247 if (sfd->vm_ops->set_policy)
248 err = sfd->vm_ops->set_policy(vma, new);
249 return err;
250}
251
Adrian Bunkd823e3e2007-10-16 23:26:42 -0700252static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
253 unsigned long addr)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800254{
255 struct file *file = vma->vm_file;
256 struct shm_file_data *sfd = shm_file_data(file);
257 struct mempolicy *pol = NULL;
258
259 if (sfd->vm_ops->get_policy)
260 pol = sfd->vm_ops->get_policy(vma, addr);
Adam Litke22741922007-06-16 10:16:15 -0700261 else if (vma->vm_policy)
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800262 pol = vma->vm_policy;
Adam Litke22741922007-06-16 10:16:15 -0700263 else
264 pol = current->mempolicy;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800265 return pol;
266}
267#endif
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static int shm_mmap(struct file * file, struct vm_area_struct * vma)
270{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800271 struct shm_file_data *sfd = shm_file_data(file);
David Howellsb0e15192006-01-06 00:11:42 -0800272 int ret;
273
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800274 ret = sfd->file->f_op->mmap(sfd->file, vma);
275 if (ret != 0)
276 return ret;
277 sfd->vm_ops = vma->vm_ops;
David Howells2e92a3b2007-07-31 00:37:24 -0700278#ifdef CONFIG_MMU
Nick Piggin54cb8822007-07-19 01:46:59 -0700279 BUG_ON(!sfd->vm_ops->fault);
David Howells2e92a3b2007-07-31 00:37:24 -0700280#endif
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800281 vma->vm_ops = &shm_vm_ops;
282 shm_open(vma);
David Howellsb0e15192006-01-06 00:11:42 -0800283
284 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Kirill Korotaev4e982312006-10-02 02:18:22 -0700287static int shm_release(struct inode *ino, struct file *file)
288{
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800289 struct shm_file_data *sfd = shm_file_data(file);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700290
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800291 put_ipc_ns(sfd->ns);
292 shm_file_data(file) = NULL;
293 kfree(sfd);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700294 return 0;
295}
296
Adam Litke516dffd2007-03-01 15:46:08 -0800297static int shm_fsync(struct file *file, struct dentry *dentry, int datasync)
298{
299 int (*fsync) (struct file *, struct dentry *, int datasync);
300 struct shm_file_data *sfd = shm_file_data(file);
301 int ret = -EINVAL;
302
303 fsync = sfd->file->f_op->fsync;
304 if (fsync)
305 ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync);
306 return ret;
307}
308
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800309static unsigned long shm_get_unmapped_area(struct file *file,
310 unsigned long addr, unsigned long len, unsigned long pgoff,
311 unsigned long flags)
312{
313 struct shm_file_data *sfd = shm_file_data(file);
Adam Litke516dffd2007-03-01 15:46:08 -0800314 return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800315}
Adam Litke516dffd2007-03-01 15:46:08 -0800316
317int is_file_shm_hugepages(struct file *file)
318{
319 int ret = 0;
320
321 if (file->f_op == &shm_file_operations) {
322 struct shm_file_data *sfd;
323 sfd = shm_file_data(file);
324 ret = is_file_hugepages(sfd->file);
325 }
326 return ret;
327}
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800328
Arjan van de Ven9a321442007-02-12 00:55:35 -0800329static const struct file_operations shm_file_operations = {
Kirill Korotaev4e982312006-10-02 02:18:22 -0700330 .mmap = shm_mmap,
Adam Litke516dffd2007-03-01 15:46:08 -0800331 .fsync = shm_fsync,
Kirill Korotaev4e982312006-10-02 02:18:22 -0700332 .release = shm_release,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800333 .get_unmapped_area = shm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
335
336static struct vm_operations_struct shm_vm_ops = {
337 .open = shm_open, /* callback for a new vm-area open */
338 .close = shm_close, /* callback for when the vm-area is released */
Nick Piggin54cb8822007-07-19 01:46:59 -0700339 .fault = shm_fault,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800340#if defined(CONFIG_NUMA)
341 .set_policy = shm_set_policy,
342 .get_policy = shm_get_policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343#endif
344};
345
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700346static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700348 key_t key = params->key;
349 int shmflg = params->flg;
350 size_t size = params->u.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int error;
352 struct shmid_kernel *shp;
353 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
354 struct file * file;
355 char name[13];
356 int id;
357
Kirill Korotaev4e982312006-10-02 02:18:22 -0700358 if (size < SHMMIN || size > ns->shm_ctlmax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return -EINVAL;
360
Guy Streeterf66d45e2007-01-23 12:20:04 -0600361 if (ns->shm_tot + numpages > ns->shm_ctlall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return -ENOSPC;
363
364 shp = ipc_rcu_alloc(sizeof(*shp));
365 if (!shp)
366 return -ENOMEM;
367
368 shp->shm_perm.key = key;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800369 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 shp->mlock_user = NULL;
371
372 shp->shm_perm.security = NULL;
373 error = security_shm_alloc(shp);
374 if (error) {
375 ipc_rcu_putref(shp);
376 return error;
377 }
378
Eric W. Biederman9d665862007-06-16 10:16:16 -0700379 sprintf (name, "SYSV%08x", key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (shmflg & SHM_HUGETLB) {
Eric W. Biederman9d665862007-06-16 10:16:16 -0700381 /* hugetlb_file_setup takes care of mlock user accounting */
382 file = hugetlb_file_setup(name, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 shp->mlock_user = current->user;
384 } else {
Badari Pulavartybf8f9722005-11-07 00:59:27 -0800385 int acctflag = VM_ACCOUNT;
386 /*
387 * Do not allow no accounting for OVERCOMMIT_NEVER, even
388 * if it's asked for.
389 */
390 if ((shmflg & SHM_NORESERVE) &&
391 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
392 acctflag = 0;
Badari Pulavartybf8f9722005-11-07 00:59:27 -0800393 file = shmem_file_setup(name, size, acctflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 error = PTR_ERR(file);
396 if (IS_ERR(file))
397 goto no_file;
398
399 error = -ENOSPC;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700400 id = shm_addid(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if(id == -1)
402 goto no_id;
403
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700404 shp->shm_cprid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 shp->shm_lprid = 0;
406 shp->shm_atim = shp->shm_dtim = 0;
407 shp->shm_ctim = get_seconds();
408 shp->shm_segsz = size;
409 shp->shm_nattch = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700410 shp->shm_perm.id = shm_buildid(ns, id, shp->shm_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 shp->shm_file = file;
Badari Pulavarty30475cc2007-06-16 10:15:59 -0700412 /*
413 * shmid gets reported as "inode#" in /proc/pid/maps.
414 * proc-ps tools use this. Changing this will break them.
415 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700416 file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
Krishnakumar R551110a2005-10-29 18:16:45 -0700417
Kirill Korotaev4e982312006-10-02 02:18:22 -0700418 ns->shm_tot += numpages;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700419 error = shp->shm_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 shm_unlock(shp);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700421 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423no_id:
424 fput(file);
425no_file:
426 security_shm_free(shp);
427 ipc_rcu_putref(shp);
428 return error;
429}
430
Nadia Derbey03f02c72007-10-18 23:40:51 -0700431static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700432{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700433 struct shmid_kernel *shp;
434
435 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
436 return security_shm_associate(shp, shmflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700437}
438
Nadia Derbey03f02c72007-10-18 23:40:51 -0700439static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
440 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700441{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700442 struct shmid_kernel *shp;
443
444 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
445 if (shp->shm_segsz < params->u.size)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700446 return -EINVAL;
447
448 return 0;
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
452{
Kirill Korotaev4e982312006-10-02 02:18:22 -0700453 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700454 struct ipc_ops shm_ops;
455 struct ipc_params shm_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Kirill Korotaev4e982312006-10-02 02:18:22 -0700457 ns = current->nsproxy->ipc_ns;
458
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700459 shm_ops.getnew = newseg;
460 shm_ops.associate = shm_security;
461 shm_ops.more_checks = shm_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700462
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700463 shm_params.key = key;
464 shm_params.flg = shmflg;
465 shm_params.u.size = size;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700466
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700467 return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
471{
472 switch(version) {
473 case IPC_64:
474 return copy_to_user(buf, in, sizeof(*in));
475 case IPC_OLD:
476 {
477 struct shmid_ds out;
478
479 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
480 out.shm_segsz = in->shm_segsz;
481 out.shm_atime = in->shm_atime;
482 out.shm_dtime = in->shm_dtime;
483 out.shm_ctime = in->shm_ctime;
484 out.shm_cpid = in->shm_cpid;
485 out.shm_lpid = in->shm_lpid;
486 out.shm_nattch = in->shm_nattch;
487
488 return copy_to_user(buf, &out, sizeof(out));
489 }
490 default:
491 return -EINVAL;
492 }
493}
494
495struct shm_setbuf {
496 uid_t uid;
497 gid_t gid;
498 mode_t mode;
499};
500
501static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
502{
503 switch(version) {
504 case IPC_64:
505 {
506 struct shmid64_ds tbuf;
507
508 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
509 return -EFAULT;
510
511 out->uid = tbuf.shm_perm.uid;
512 out->gid = tbuf.shm_perm.gid;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800513 out->mode = tbuf.shm_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 return 0;
516 }
517 case IPC_OLD:
518 {
519 struct shmid_ds tbuf_old;
520
521 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
522 return -EFAULT;
523
524 out->uid = tbuf_old.shm_perm.uid;
525 out->gid = tbuf_old.shm_perm.gid;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800526 out->mode = tbuf_old.shm_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 return 0;
529 }
530 default:
531 return -EINVAL;
532 }
533}
534
535static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
536{
537 switch(version) {
538 case IPC_64:
539 return copy_to_user(buf, in, sizeof(*in));
540 case IPC_OLD:
541 {
542 struct shminfo out;
543
544 if(in->shmmax > INT_MAX)
545 out.shmmax = INT_MAX;
546 else
547 out.shmmax = (int)in->shmmax;
548
549 out.shmmin = in->shmmin;
550 out.shmmni = in->shmmni;
551 out.shmseg = in->shmseg;
552 out.shmall = in->shmall;
553
554 return copy_to_user(buf, &out, sizeof(out));
555 }
556 default:
557 return -EINVAL;
558 }
559}
560
Kirill Korotaev4e982312006-10-02 02:18:22 -0700561static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
562 unsigned long *swp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700564 int next_id;
565 int total, in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 *rss = 0;
568 *swp = 0;
569
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700570 in_use = shm_ids(ns).in_use;
571
572 for (total = 0, next_id = 0; total < in_use; next_id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct shmid_kernel *shp;
574 struct inode *inode;
575
Nadia Derbey637c3662007-10-18 23:40:50 -0700576 /*
577 * idr_find() is called via shm_get(), so with shm_ids.mutex
578 * locked. Since ipc_addid() is also called with
579 * shm_ids.mutex down, there is no need to add read barriers
580 * here to gurantee the writes in ipc_addid() are seen in
581 * order here (for Alpha).
582 * However idr_find() itself does not necessary require
583 * ipc_ids.mutex down. So if idr_find() is used by other
584 * places without ipc_ids.mutex down, then it needs read
585 * read memory barriers as ipc_lock() does.
586 */
587
588 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700589 if (shp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 continue;
591
Josef Sipek6d630792006-12-08 02:37:11 -0800592 inode = shp->shm_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (is_file_hugepages(shp->shm_file)) {
595 struct address_space *mapping = inode->i_mapping;
596 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
597 } else {
598 struct shmem_inode_info *info = SHMEM_I(inode);
599 spin_lock(&info->lock);
600 *rss += inode->i_mapping->nrpages;
601 *swp += info->swapped;
602 spin_unlock(&info->lock);
603 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700604
605 total++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607}
608
609asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
610{
611 struct shm_setbuf setbuf;
612 struct shmid_kernel *shp;
613 int err, version;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700614 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (cmd < 0 || shmid < 0) {
617 err = -EINVAL;
618 goto out;
619 }
620
621 version = ipc_parse_version(&cmd);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700622 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 switch (cmd) { /* replace with proc interface ? */
625 case IPC_INFO:
626 {
627 struct shminfo64 shminfo;
628
629 err = security_shm_shmctl(NULL, cmd);
630 if (err)
631 return err;
632
633 memset(&shminfo,0,sizeof(shminfo));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700634 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
635 shminfo.shmmax = ns->shm_ctlmax;
636 shminfo.shmall = ns->shm_ctlall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 shminfo.shmmin = SHMMIN;
639 if(copy_shminfo_to_user (buf, &shminfo, version))
640 return -EFAULT;
641 /* reading a integer is always atomic */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700642 err = ipc_get_maxid(&shm_ids(ns));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if(err<0)
644 err = 0;
645 goto out;
646 }
647 case SHM_INFO:
648 {
649 struct shm_info shm_info;
650
651 err = security_shm_shmctl(NULL, cmd);
652 if (err)
653 return err;
654
655 memset(&shm_info,0,sizeof(shm_info));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700656 mutex_lock(&shm_ids(ns).mutex);
657 shm_info.used_ids = shm_ids(ns).in_use;
658 shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
659 shm_info.shm_tot = ns->shm_tot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 shm_info.swap_attempts = 0;
661 shm_info.swap_successes = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700662 err = ipc_get_maxid(&shm_ids(ns));
Kirill Korotaev4e982312006-10-02 02:18:22 -0700663 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
665 err = -EFAULT;
666 goto out;
667 }
668
669 err = err < 0 ? 0 : err;
670 goto out;
671 }
672 case SHM_STAT:
673 case IPC_STAT:
674 {
675 struct shmid64_ds tbuf;
676 int result;
Nadia Derbey023a5352007-10-18 23:40:51 -0700677
678 if (!buf) {
679 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 goto out;
Nadia Derbey023a5352007-10-18 23:40:51 -0700681 }
682
683 if (cmd == SHM_STAT) {
684 shp = shm_lock(ns, shmid);
685 if (IS_ERR(shp)) {
686 err = PTR_ERR(shp);
687 goto out;
688 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700689 result = shp->shm_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 } else {
Nadia Derbey023a5352007-10-18 23:40:51 -0700691 shp = shm_lock_check(ns, shmid);
692 if (IS_ERR(shp)) {
693 err = PTR_ERR(shp);
694 goto out;
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 result = 0;
697 }
698 err=-EACCES;
699 if (ipcperms (&shp->shm_perm, S_IRUGO))
700 goto out_unlock;
701 err = security_shm_shmctl(shp, cmd);
702 if (err)
703 goto out_unlock;
Nadia Derbey023a5352007-10-18 23:40:51 -0700704 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
706 tbuf.shm_segsz = shp->shm_segsz;
707 tbuf.shm_atime = shp->shm_atim;
708 tbuf.shm_dtime = shp->shm_dtim;
709 tbuf.shm_ctime = shp->shm_ctim;
710 tbuf.shm_cpid = shp->shm_cprid;
711 tbuf.shm_lpid = shp->shm_lprid;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800712 tbuf.shm_nattch = shp->shm_nattch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 shm_unlock(shp);
714 if(copy_shmid_to_user (buf, &tbuf, version))
715 err = -EFAULT;
716 else
717 err = result;
718 goto out;
719 }
720 case SHM_LOCK:
721 case SHM_UNLOCK:
722 {
Nadia Derbey023a5352007-10-18 23:40:51 -0700723 shp = shm_lock_check(ns, shmid);
724 if (IS_ERR(shp)) {
725 err = PTR_ERR(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto out;
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Steve Grubb073115d2006-04-02 17:07:33 -0400729 err = audit_ipc_obj(&(shp->shm_perm));
730 if (err)
731 goto out_unlock;
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!capable(CAP_IPC_LOCK)) {
734 err = -EPERM;
735 if (current->euid != shp->shm_perm.uid &&
736 current->euid != shp->shm_perm.cuid)
737 goto out_unlock;
738 if (cmd == SHM_LOCK &&
739 !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
740 goto out_unlock;
741 }
742
743 err = security_shm_shmctl(shp, cmd);
744 if (err)
745 goto out_unlock;
746
747 if(cmd==SHM_LOCK) {
748 struct user_struct * user = current->user;
749 if (!is_file_hugepages(shp->shm_file)) {
750 err = shmem_lock(shp->shm_file, 1, user);
Pavel Emelianov7be77e22007-07-31 00:38:48 -0700751 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
Andrew Mortonb33291c2006-01-08 01:02:21 -0800752 shp->shm_perm.mode |= SHM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 shp->mlock_user = user;
754 }
755 }
756 } else if (!is_file_hugepages(shp->shm_file)) {
757 shmem_lock(shp->shm_file, 0, shp->mlock_user);
Andrew Mortonb33291c2006-01-08 01:02:21 -0800758 shp->shm_perm.mode &= ~SHM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 shp->mlock_user = NULL;
760 }
761 shm_unlock(shp);
762 goto out;
763 }
764 case IPC_RMID:
765 {
766 /*
767 * We cannot simply remove the file. The SVID states
768 * that the block remains until the last person
769 * detaches from it, then is deleted. A shmat() on
770 * an RMID segment is legal in older Linux and if
771 * we change it apps break...
772 *
773 * Instead we set a destroyed flag, and then blow
774 * the name away when the usage hits zero.
775 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700776 mutex_lock(&shm_ids(ns).mutex);
Nadia Derbey023a5352007-10-18 23:40:51 -0700777 shp = shm_lock_check(ns, shmid);
778 if (IS_ERR(shp)) {
779 err = PTR_ERR(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 goto out_up;
Nadia Derbey023a5352007-10-18 23:40:51 -0700781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Steve Grubb073115d2006-04-02 17:07:33 -0400783 err = audit_ipc_obj(&(shp->shm_perm));
784 if (err)
785 goto out_unlock_up;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (current->euid != shp->shm_perm.uid &&
788 current->euid != shp->shm_perm.cuid &&
789 !capable(CAP_SYS_ADMIN)) {
790 err=-EPERM;
791 goto out_unlock_up;
792 }
793
794 err = security_shm_shmctl(shp, cmd);
795 if (err)
796 goto out_unlock_up;
797
Kirill Korotaev4e982312006-10-02 02:18:22 -0700798 do_shm_rmid(ns, shp);
799 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 goto out;
801 }
802
803 case IPC_SET:
804 {
Nadia Derbey023a5352007-10-18 23:40:51 -0700805 if (!buf) {
806 err = -EFAULT;
807 goto out;
808 }
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (copy_shmid_from_user (&setbuf, buf, version)) {
811 err = -EFAULT;
812 goto out;
813 }
Kirill Korotaev4e982312006-10-02 02:18:22 -0700814 mutex_lock(&shm_ids(ns).mutex);
Nadia Derbey023a5352007-10-18 23:40:51 -0700815 shp = shm_lock_check(ns, shmid);
816 if (IS_ERR(shp)) {
817 err = PTR_ERR(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto out_up;
Nadia Derbey023a5352007-10-18 23:40:51 -0700819 }
Steve Grubb073115d2006-04-02 17:07:33 -0400820 err = audit_ipc_obj(&(shp->shm_perm));
821 if (err)
822 goto out_unlock_up;
Linda Knippersac032212006-05-16 22:03:48 -0400823 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
Steve Grubb073115d2006-04-02 17:07:33 -0400824 if (err)
825 goto out_unlock_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 err=-EPERM;
827 if (current->euid != shp->shm_perm.uid &&
828 current->euid != shp->shm_perm.cuid &&
829 !capable(CAP_SYS_ADMIN)) {
830 goto out_unlock_up;
831 }
832
833 err = security_shm_shmctl(shp, cmd);
834 if (err)
835 goto out_unlock_up;
836
837 shp->shm_perm.uid = setbuf.uid;
838 shp->shm_perm.gid = setbuf.gid;
Andrew Mortonb33291c2006-01-08 01:02:21 -0800839 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 | (setbuf.mode & S_IRWXUGO);
841 shp->shm_ctim = get_seconds();
842 break;
843 }
844
845 default:
846 err = -EINVAL;
847 goto out;
848 }
849
850 err = 0;
851out_unlock_up:
852 shm_unlock(shp);
853out_up:
Kirill Korotaev4e982312006-10-02 02:18:22 -0700854 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 goto out;
856out_unlock:
857 shm_unlock(shp);
858out:
859 return err;
860}
861
862/*
863 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
864 *
865 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
866 * "raddr" thing points to kernel space, and there has to be a wrapper around
867 * this.
868 */
869long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
870{
871 struct shmid_kernel *shp;
872 unsigned long addr;
873 unsigned long size;
874 struct file * file;
875 int err;
876 unsigned long flags;
877 unsigned long prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 int acc_mode;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800879 unsigned long user_addr;
Kirill Korotaev4e982312006-10-02 02:18:22 -0700880 struct ipc_namespace *ns;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800881 struct shm_file_data *sfd;
882 struct path path;
883 mode_t f_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800885 err = -EINVAL;
886 if (shmid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 goto out;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800888 else if ((addr = (ulong)shmaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (addr & (SHMLBA-1)) {
890 if (shmflg & SHM_RND)
891 addr &= ~(SHMLBA-1); /* round down */
892 else
893#ifndef __ARCH_FORCE_SHMLBA
894 if (addr & ~PAGE_MASK)
895#endif
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800896 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 flags = MAP_SHARED | MAP_FIXED;
899 } else {
900 if ((shmflg & SHM_REMAP))
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800901 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 flags = MAP_SHARED;
904 }
905
906 if (shmflg & SHM_RDONLY) {
907 prot = PROT_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 acc_mode = S_IRUGO;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800909 f_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 } else {
911 prot = PROT_READ | PROT_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 acc_mode = S_IRUGO | S_IWUGO;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800913 f_mode = FMODE_READ | FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915 if (shmflg & SHM_EXEC) {
916 prot |= PROT_EXEC;
917 acc_mode |= S_IXUGO;
918 }
919
920 /*
921 * We cannot rely on the fs check since SYSV IPC does have an
922 * additional creator id...
923 */
Kirill Korotaev4e982312006-10-02 02:18:22 -0700924 ns = current->nsproxy->ipc_ns;
Nadia Derbey023a5352007-10-18 23:40:51 -0700925 shp = shm_lock_check(ns, shmid);
926 if (IS_ERR(shp)) {
927 err = PTR_ERR(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 goto out;
Nadia Derbey023a5352007-10-18 23:40:51 -0700929 }
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800930
931 err = -EACCES;
932 if (ipcperms(&shp->shm_perm, acc_mode))
933 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 err = security_shm_shmat(shp, shmaddr, shmflg);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800936 if (err)
937 goto out_unlock;
938
939 path.dentry = dget(shp->shm_file->f_path.dentry);
Dave Hansence8d2cd2007-10-16 23:31:13 -0700940 path.mnt = shp->shm_file->f_path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 shp->shm_nattch++;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800942 size = i_size_read(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 shm_unlock(shp);
944
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800945 err = -ENOMEM;
946 sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
947 if (!sfd)
Dave Hansence8d2cd2007-10-16 23:31:13 -0700948 goto out_put_dentry;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800949
950 err = -ENOMEM;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700951
952 file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800953 if (!file)
954 goto out_free;
955
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800956 file->private_data = sfd;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800957 file->f_mapping = shp->shm_file->f_mapping;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700958 sfd->id = shp->shm_perm.id;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800959 sfd->ns = get_ipc_ns(ns);
960 sfd->file = shp->shm_file;
961 sfd->vm_ops = NULL;
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 down_write(&current->mm->mmap_sem);
964 if (addr && !(shmflg & SHM_REMAP)) {
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800965 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (find_vma_intersection(current->mm, addr, addr + size))
967 goto invalid;
968 /*
969 * If shm segment goes below stack, make sure there is some
970 * space left for the stack to grow (at least 4 pages).
971 */
972 if (addr < current->mm->start_stack &&
973 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
974 goto invalid;
975 }
976
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800977 user_addr = do_mmap (file, addr, size, prot, flags, 0);
978 *raddr = user_addr;
979 err = 0;
980 if (IS_ERR_VALUE(user_addr))
981 err = (long)user_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982invalid:
983 up_write(&current->mm->mmap_sem);
984
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -0800985 fput(file);
986
987out_nattch:
Kirill Korotaev4e982312006-10-02 02:18:22 -0700988 mutex_lock(&shm_ids(ns).mutex);
989 shp = shm_lock(ns, shmid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700990 BUG_ON(IS_ERR(shp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 shp->shm_nattch--;
992 if(shp->shm_nattch == 0 &&
Andrew Mortonb33291c2006-01-08 01:02:21 -0800993 shp->shm_perm.mode & SHM_DEST)
Kirill Korotaev4e982312006-10-02 02:18:22 -0700994 shm_destroy(ns, shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 else
996 shm_unlock(shp);
Kirill Korotaev4e982312006-10-02 02:18:22 -0700997 mutex_unlock(&shm_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999out:
1000 return err;
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001001
1002out_unlock:
1003 shm_unlock(shp);
1004 goto out;
1005
1006out_free:
1007 kfree(sfd);
Dave Hansence8d2cd2007-10-16 23:31:13 -07001008out_put_dentry:
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001009 dput(path.dentry);
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001010 goto out_nattch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -07001013asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
1014{
1015 unsigned long ret;
1016 long err;
1017
1018 err = do_shmat(shmid, shmaddr, shmflg, &ret);
1019 if (err)
1020 return err;
1021 force_successful_syscall_return();
1022 return (long)ret;
1023}
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025/*
1026 * detach and kill segment if marked destroyed.
1027 * The work is done in shm_close.
1028 */
1029asmlinkage long sys_shmdt(char __user *shmaddr)
1030{
1031 struct mm_struct *mm = current->mm;
1032 struct vm_area_struct *vma, *next;
1033 unsigned long addr = (unsigned long)shmaddr;
1034 loff_t size = 0;
1035 int retval = -EINVAL;
1036
Hugh Dickinsdf1e2fb2006-03-24 03:18:06 -08001037 if (addr & ~PAGE_MASK)
1038 return retval;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 down_write(&mm->mmap_sem);
1041
1042 /*
1043 * This function tries to be smart and unmap shm segments that
1044 * were modified by partial mlock or munmap calls:
1045 * - It first determines the size of the shm segment that should be
1046 * unmapped: It searches for a vma that is backed by shm and that
1047 * started at address shmaddr. It records it's size and then unmaps
1048 * it.
1049 * - Then it unmaps all shm vmas that started at shmaddr and that
1050 * are within the initially determined size.
1051 * Errors from do_munmap are ignored: the function only fails if
1052 * it's called with invalid parameters or if it's called to unmap
1053 * a part of a vma. Both calls in this function are for full vmas,
1054 * the parameters are directly copied from the vma itself and always
1055 * valid - therefore do_munmap cannot fail. (famous last words?)
1056 */
1057 /*
1058 * If it had been mremap()'d, the starting address would not
1059 * match the usual checks anyway. So assume all vma's are
1060 * above the starting address given.
1061 */
1062 vma = find_vma(mm, addr);
1063
1064 while (vma) {
1065 next = vma->vm_next;
1066
1067 /*
1068 * Check if the starting address would match, i.e. it's
1069 * a fragment created by mprotect() and/or munmap(), or it
1070 * otherwise it starts at this address with no hassles.
1071 */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001072 if ((vma->vm_ops == &shm_vm_ops) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1074
1075
Josef Sipek6d630792006-12-08 02:37:11 -08001076 size = vma->vm_file->f_path.dentry->d_inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1078 /*
1079 * We discovered the size of the shm segment, so
1080 * break out of here and fall through to the next
1081 * loop that uses the size information to stop
1082 * searching for matching vma's.
1083 */
1084 retval = 0;
1085 vma = next;
1086 break;
1087 }
1088 vma = next;
1089 }
1090
1091 /*
1092 * We need look no further than the maximum address a fragment
1093 * could possibly have landed at. Also cast things to loff_t to
1094 * prevent overflows and make comparisions vs. equal-width types.
1095 */
KAMEZAWA Hiroyuki8e367092006-02-10 01:51:12 -08001096 size = PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1098 next = vma->vm_next;
1099
1100 /* finding a matching vma now does not alter retval */
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001101 if ((vma->vm_ops == &shm_vm_ops) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
1103
1104 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1105 vma = next;
1106 }
1107
1108 up_write(&mm->mmap_sem);
1109 return retval;
1110}
1111
1112#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001113static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Mike Waychison19b49462005-09-06 15:17:10 -07001115 struct shmid_kernel *shp = it;
1116 char *format;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118#define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1119#define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Mike Waychison19b49462005-09-06 15:17:10 -07001121 if (sizeof(size_t) <= sizeof(int))
1122 format = SMALL_STRING;
1123 else
1124 format = BIG_STRING;
1125 return seq_printf(s, format,
1126 shp->shm_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001127 shp->shm_perm.id,
Andrew Mortonb33291c2006-01-08 01:02:21 -08001128 shp->shm_perm.mode,
Mike Waychison19b49462005-09-06 15:17:10 -07001129 shp->shm_segsz,
1130 shp->shm_cprid,
1131 shp->shm_lprid,
Eric W. Biedermanbc56bba2007-02-20 13:57:53 -08001132 shp->shm_nattch,
Mike Waychison19b49462005-09-06 15:17:10 -07001133 shp->shm_perm.uid,
1134 shp->shm_perm.gid,
1135 shp->shm_perm.cuid,
1136 shp->shm_perm.cgid,
1137 shp->shm_atim,
1138 shp->shm_dtim,
1139 shp->shm_ctim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141#endif