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