]> bbs.cooldavid.org Git - net-next-2.6.git/blame - ipc/shm.c
ipc: inline ipc_buildid()
[net-next-2.6.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>
5f921ae9 38#include <linux/mutex.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)
4e982311
KK
64#define shm_buildid(ns, id, seq) \
65 ipc_buildid(&shm_ids(ns), id, seq)
1da177e4 66
7748dbfa 67static int newseg(struct ipc_namespace *, struct ipc_params *);
bc56bba8
EB
68static void shm_open(struct vm_area_struct *vma);
69static void shm_close(struct vm_area_struct *vma);
4e982311 70static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
1da177e4 71#ifdef CONFIG_PROC_FS
19b4946c 72static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
1da177e4
LT
73#endif
74
7d69a1f4 75static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
4e982311
KK
76{
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;
7ca7e564 82 ipc_init_ids(ids);
4e982311
KK
83}
84
85static 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
4e982311
KK
96int 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;
1da177e4 103
4e982311
KK
104 __shm_init_ns(ns, ids);
105 return 0;
106}
107
108void shm_exit_ns(struct ipc_namespace *ns)
109{
4e982311 110 struct shmid_kernel *shp;
7ca7e564
ND
111 int next_id;
112 int total, in_use;
4e982311
KK
113
114 mutex_lock(&shm_ids(ns).mutex);
7ca7e564
ND
115
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);
4e982311
KK
120 if (shp == NULL)
121 continue;
7ca7e564 122 ipc_lock_by_ptr(&shp->shm_perm);
4e982311 123 do_shm_rmid(ns, shp);
7ca7e564 124 total++;
4e982311
KK
125 }
126 mutex_unlock(&shm_ids(ns).mutex);
127
128 kfree(ns->ids[IPC_SHM_IDS]);
129 ns->ids[IPC_SHM_IDS] = NULL;
130}
1da177e4
LT
131
132void __init shm_init (void)
133{
4e982311 134 __shm_init_ns(&init_ipc_ns, &init_shm_ids);
19b4946c
MW
135 ipc_init_proc_interface("sysvipc/shm",
136 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
4e982311 137 IPC_SHM_IDS, sysvipc_shm_proc_show);
1da177e4
LT
138}
139
023a5355 140static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
1da177e4 141{
03f02c76
ND
142 struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
143
144 return container_of(ipcp, struct shmid_kernel, shm_perm);
023a5355
ND
145}
146
147static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns,
148 int id)
149{
03f02c76
ND
150 struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id);
151
152 return container_of(ipcp, struct shmid_kernel, shm_perm);
1da177e4
LT
153}
154
7ca7e564 155static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
1da177e4 156{
7ca7e564 157 ipc_rmid(&shm_ids(ns), &s->shm_perm);
1da177e4
LT
158}
159
4e982311 160static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
1da177e4 161{
4e982311 162 return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
1da177e4
LT
163}
164
165
166
bc56bba8
EB
167/* This is called by fork, once for every shm attach. */
168static void shm_open(struct vm_area_struct *vma)
4e982311 169{
bc56bba8
EB
170 struct file *file = vma->vm_file;
171 struct shm_file_data *sfd = shm_file_data(file);
1da177e4
LT
172 struct shmid_kernel *shp;
173
bc56bba8 174 shp = shm_lock(sfd->ns, sfd->id);
023a5355 175 BUG_ON(IS_ERR(shp));
1da177e4 176 shp->shm_atim = get_seconds();
b488893a 177 shp->shm_lprid = task_tgid_vnr(current);
1da177e4
LT
178 shp->shm_nattch++;
179 shm_unlock(shp);
180}
181
1da177e4
LT
182/*
183 * shm_destroy - free the struct shmid_kernel
184 *
185 * @shp: struct to free
186 *
5f921ae9 187 * It has to be called with shp and shm_ids.mutex locked,
1da177e4
LT
188 * but returns with shp unlocked and freed.
189 */
4e982311 190static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
1da177e4 191{
4e982311 192 ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
7ca7e564 193 shm_rmid(ns, shp);
1da177e4
LT
194 shm_unlock(shp);
195 if (!is_file_hugepages(shp->shm_file))
196 shmem_lock(shp->shm_file, 0, shp->mlock_user);
197 else
6d63079a 198 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
1da177e4
LT
199 shp->mlock_user);
200 fput (shp->shm_file);
201 security_shm_free(shp);
202 ipc_rcu_putref(shp);
203}
204
205/*
bc56bba8 206 * remove the attach descriptor vma.
1da177e4
LT
207 * 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 */
bc56bba8 211static void shm_close(struct vm_area_struct *vma)
1da177e4 212{
bc56bba8
EB
213 struct file * file = vma->vm_file;
214 struct shm_file_data *sfd = shm_file_data(file);
1da177e4 215 struct shmid_kernel *shp;
bc56bba8 216 struct ipc_namespace *ns = sfd->ns;
4e982311
KK
217
218 mutex_lock(&shm_ids(ns).mutex);
1da177e4 219 /* remove from the list of attaches of the shm segment */
bc56bba8 220 shp = shm_lock(ns, sfd->id);
023a5355 221 BUG_ON(IS_ERR(shp));
b488893a 222 shp->shm_lprid = task_tgid_vnr(current);
1da177e4
LT
223 shp->shm_dtim = get_seconds();
224 shp->shm_nattch--;
225 if(shp->shm_nattch == 0 &&
b33291c0 226 shp->shm_perm.mode & SHM_DEST)
4e982311 227 shm_destroy(ns, shp);
1da177e4
LT
228 else
229 shm_unlock(shp);
4e982311 230 mutex_unlock(&shm_ids(ns).mutex);
1da177e4
LT
231}
232
d0217ac0 233static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
bc56bba8
EB
234{
235 struct file *file = vma->vm_file;
236 struct shm_file_data *sfd = shm_file_data(file);
237
d0217ac0 238 return sfd->vm_ops->fault(vma, vmf);
bc56bba8
EB
239}
240
241#ifdef CONFIG_NUMA
d823e3e7 242static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
bc56bba8
EB
243{
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
d823e3e7
AB
252static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
253 unsigned long addr)
bc56bba8
EB
254{
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);
22741925 261 else if (vma->vm_policy)
bc56bba8 262 pol = vma->vm_policy;
22741925
AL
263 else
264 pol = current->mempolicy;
bc56bba8
EB
265 return pol;
266}
267#endif
268
1da177e4
LT
269static int shm_mmap(struct file * file, struct vm_area_struct * vma)
270{
bc56bba8 271 struct shm_file_data *sfd = shm_file_data(file);
b0e15190
DH
272 int ret;
273
bc56bba8
EB
274 ret = sfd->file->f_op->mmap(sfd->file, vma);
275 if (ret != 0)
276 return ret;
277 sfd->vm_ops = vma->vm_ops;
2e92a3ba 278#ifdef CONFIG_MMU
54cb8821 279 BUG_ON(!sfd->vm_ops->fault);
2e92a3ba 280#endif
bc56bba8
EB
281 vma->vm_ops = &shm_vm_ops;
282 shm_open(vma);
b0e15190
DH
283
284 return ret;
1da177e4
LT
285}
286
4e982311
KK
287static int shm_release(struct inode *ino, struct file *file)
288{
bc56bba8 289 struct shm_file_data *sfd = shm_file_data(file);
4e982311 290
bc56bba8
EB
291 put_ipc_ns(sfd->ns);
292 shm_file_data(file) = NULL;
293 kfree(sfd);
4e982311
KK
294 return 0;
295}
296
516dffdc
AL
297static 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
bc56bba8
EB
309static 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);
516dffdc
AL
314 return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
315}
316
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;
bc56bba8 327}
bc56bba8 328
9a32144e 329static const struct file_operations shm_file_operations = {
4e982311 330 .mmap = shm_mmap,
516dffdc 331 .fsync = shm_fsync,
4e982311 332 .release = shm_release,
bc56bba8 333 .get_unmapped_area = shm_get_unmapped_area,
1da177e4
LT
334};
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 */
54cb8821 339 .fault = shm_fault,
bc56bba8
EB
340#if defined(CONFIG_NUMA)
341 .set_policy = shm_set_policy,
342 .get_policy = shm_get_policy,
1da177e4
LT
343#endif
344};
345
7748dbfa 346static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
1da177e4 347{
7748dbfa
ND
348 key_t key = params->key;
349 int shmflg = params->flg;
350 size_t size = params->u.size;
1da177e4
LT
351 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
4e982311 358 if (size < SHMMIN || size > ns->shm_ctlmax)
1da177e4
LT
359 return -EINVAL;
360
f66d45e9 361 if (ns->shm_tot + numpages > ns->shm_ctlall)
1da177e4
LT
362 return -ENOSPC;
363
364 shp = ipc_rcu_alloc(sizeof(*shp));
365 if (!shp)
366 return -ENOMEM;
367
368 shp->shm_perm.key = key;
b33291c0 369 shp->shm_perm.mode = (shmflg & S_IRWXUGO);
1da177e4
LT
370 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
9d66586f 379 sprintf (name, "SYSV%08x", key);
1da177e4 380 if (shmflg & SHM_HUGETLB) {
9d66586f
EB
381 /* hugetlb_file_setup takes care of mlock user accounting */
382 file = hugetlb_file_setup(name, size);
1da177e4
LT
383 shp->mlock_user = current->user;
384 } else {
bf8f972d
BP
385 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;
bf8f972d 393 file = shmem_file_setup(name, size, acctflag);
1da177e4
LT
394 }
395 error = PTR_ERR(file);
396 if (IS_ERR(file))
397 goto no_file;
398
399 error = -ENOSPC;
4e982311 400 id = shm_addid(ns, shp);
1da177e4
LT
401 if(id == -1)
402 goto no_id;
403
b488893a 404 shp->shm_cprid = task_tgid_vnr(current);
1da177e4
LT
405 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;
7ca7e564 410 shp->shm_perm.id = shm_buildid(ns, id, shp->shm_perm.seq);
1da177e4 411 shp->shm_file = file;
30475cc1
BP
412 /*
413 * shmid gets reported as "inode#" in /proc/pid/maps.
414 * proc-ps tools use this. Changing this will break them.
415 */
7ca7e564 416 file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
551110a9 417
4e982311 418 ns->shm_tot += numpages;
7ca7e564 419 error = shp->shm_perm.id;
1da177e4 420 shm_unlock(shp);
7ca7e564 421 return error;
1da177e4
LT
422
423no_id:
424 fput(file);
425no_file:
426 security_shm_free(shp);
427 ipc_rcu_putref(shp);
428 return error;
429}
430
03f02c76 431static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
7748dbfa 432{
03f02c76
ND
433 struct shmid_kernel *shp;
434
435 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
436 return security_shm_associate(shp, shmflg);
7748dbfa
ND
437}
438
03f02c76
ND
439static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
440 struct ipc_params *params)
7748dbfa 441{
03f02c76
ND
442 struct shmid_kernel *shp;
443
444 shp = container_of(ipcp, struct shmid_kernel, shm_perm);
445 if (shp->shm_segsz < params->u.size)
7748dbfa
ND
446 return -EINVAL;
447
448 return 0;
449}
450
1da177e4
LT
451asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
452{
4e982311 453 struct ipc_namespace *ns;
7748dbfa
ND
454 struct ipc_ops shm_ops;
455 struct ipc_params shm_params;
4e982311
KK
456
457 ns = current->nsproxy->ipc_ns;
1da177e4 458
7748dbfa
ND
459 shm_ops.getnew = newseg;
460 shm_ops.associate = shm_security;
461 shm_ops.more_checks = shm_more_checks;
7ca7e564 462
7748dbfa
ND
463 shm_params.key = key;
464 shm_params.flg = shmflg;
465 shm_params.u.size = size;
1da177e4 466
7748dbfa 467 return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
1da177e4
LT
468}
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;
b33291c0 513 out->mode = tbuf.shm_perm.mode;
1da177e4
LT
514
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;
b33291c0 526 out->mode = tbuf_old.shm_perm.mode;
1da177e4
LT
527
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
4e982311
KK
561static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
562 unsigned long *swp)
1da177e4 563{
7ca7e564
ND
564 int next_id;
565 int total, in_use;
1da177e4
LT
566
567 *rss = 0;
568 *swp = 0;
569
7ca7e564
ND
570 in_use = shm_ids(ns).in_use;
571
572 for (total = 0, next_id = 0; total < in_use; next_id++) {
1da177e4
LT
573 struct shmid_kernel *shp;
574 struct inode *inode;
575
637c3663
ND
576 /*
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);
7ca7e564 589 if (shp == NULL)
1da177e4
LT
590 continue;
591
6d63079a 592 inode = shp->shm_file->f_path.dentry->d_inode;
1da177e4
LT
593
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 }
7ca7e564
ND
604
605 total++;
1da177e4
LT
606 }
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;
4e982311 614 struct ipc_namespace *ns;
1da177e4
LT
615
616 if (cmd < 0 || shmid < 0) {
617 err = -EINVAL;
618 goto out;
619 }
620
621 version = ipc_parse_version(&cmd);
4e982311 622 ns = current->nsproxy->ipc_ns;
1da177e4
LT
623
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));
4e982311
KK
634 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
635 shminfo.shmmax = ns->shm_ctlmax;
636 shminfo.shmall = ns->shm_ctlall;
1da177e4
LT
637
638 shminfo.shmmin = SHMMIN;
639 if(copy_shminfo_to_user (buf, &shminfo, version))
640 return -EFAULT;
641 /* reading a integer is always atomic */
7ca7e564 642 err = ipc_get_maxid(&shm_ids(ns));
1da177e4
LT
643 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));
4e982311
KK
656 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;
1da177e4
LT
660 shm_info.swap_attempts = 0;
661 shm_info.swap_successes = 0;
7ca7e564 662 err = ipc_get_maxid(&shm_ids(ns));
4e982311 663 mutex_unlock(&shm_ids(ns).mutex);
1da177e4
LT
664 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;
023a5355
ND
677
678 if (!buf) {
679 err = -EFAULT;
1da177e4 680 goto out;
023a5355
ND
681 }
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 }
7ca7e564 689 result = shp->shm_perm.id;
1da177e4 690 } else {
023a5355
ND
691 shp = shm_lock_check(ns, shmid);
692 if (IS_ERR(shp)) {
693 err = PTR_ERR(shp);
694 goto out;
695 }
1da177e4
LT
696 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;
023a5355 704 memset(&tbuf, 0, sizeof(tbuf));
1da177e4
LT
705 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;
bc56bba8 712 tbuf.shm_nattch = shp->shm_nattch;
1da177e4
LT
713 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 {
023a5355
ND
723 shp = shm_lock_check(ns, shmid);
724 if (IS_ERR(shp)) {
725 err = PTR_ERR(shp);
1da177e4
LT
726 goto out;
727 }
1da177e4 728
073115d6
SG
729 err = audit_ipc_obj(&(shp->shm_perm));
730 if (err)
731 goto out_unlock;
732
1da177e4
LT
733 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);
7be77e20 751 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
b33291c0 752 shp->shm_perm.mode |= SHM_LOCKED;
1da177e4
LT
753 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);
b33291c0 758 shp->shm_perm.mode &= ~SHM_LOCKED;
1da177e4
LT
759 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 */
4e982311 776 mutex_lock(&shm_ids(ns).mutex);
023a5355
ND
777 shp = shm_lock_check(ns, shmid);
778 if (IS_ERR(shp)) {
779 err = PTR_ERR(shp);
1da177e4 780 goto out_up;
023a5355 781 }
1da177e4 782
073115d6
SG
783 err = audit_ipc_obj(&(shp->shm_perm));
784 if (err)
785 goto out_unlock_up;
786
1da177e4
LT
787 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
4e982311
KK
798 do_shm_rmid(ns, shp);
799 mutex_unlock(&shm_ids(ns).mutex);
1da177e4
LT
800 goto out;
801 }
802
803 case IPC_SET:
804 {
023a5355
ND
805 if (!buf) {
806 err = -EFAULT;
807 goto out;
808 }
809
1da177e4
LT
810 if (copy_shmid_from_user (&setbuf, buf, version)) {
811 err = -EFAULT;
812 goto out;
813 }
4e982311 814 mutex_lock(&shm_ids(ns).mutex);
023a5355
ND
815 shp = shm_lock_check(ns, shmid);
816 if (IS_ERR(shp)) {
817 err = PTR_ERR(shp);
1da177e4 818 goto out_up;
023a5355 819 }
073115d6
SG
820 err = audit_ipc_obj(&(shp->shm_perm));
821 if (err)
822 goto out_unlock_up;
ac03221a 823 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
073115d6
SG
824 if (err)
825 goto out_unlock_up;
1da177e4
LT
826 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;
b33291c0 839 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
1da177e4
LT
840 | (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:
4e982311 854 mutex_unlock(&shm_ids(ns).mutex);
1da177e4
LT
855 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;
1da177e4 878 int acc_mode;
bc56bba8 879 unsigned long user_addr;
4e982311 880 struct ipc_namespace *ns;
bc56bba8
EB
881 struct shm_file_data *sfd;
882 struct path path;
883 mode_t f_mode;
1da177e4 884
bc56bba8
EB
885 err = -EINVAL;
886 if (shmid < 0)
1da177e4 887 goto out;
bc56bba8 888 else if ((addr = (ulong)shmaddr)) {
1da177e4
LT
889 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
bc56bba8 896 goto out;
1da177e4
LT
897 }
898 flags = MAP_SHARED | MAP_FIXED;
899 } else {
900 if ((shmflg & SHM_REMAP))
bc56bba8 901 goto out;
1da177e4
LT
902
903 flags = MAP_SHARED;
904 }
905
906 if (shmflg & SHM_RDONLY) {
907 prot = PROT_READ;
1da177e4 908 acc_mode = S_IRUGO;
bc56bba8 909 f_mode = FMODE_READ;
1da177e4
LT
910 } else {
911 prot = PROT_READ | PROT_WRITE;
1da177e4 912 acc_mode = S_IRUGO | S_IWUGO;
bc56bba8 913 f_mode = FMODE_READ | FMODE_WRITE;
1da177e4
LT
914 }
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 */
4e982311 924 ns = current->nsproxy->ipc_ns;
023a5355
ND
925 shp = shm_lock_check(ns, shmid);
926 if (IS_ERR(shp)) {
927 err = PTR_ERR(shp);
1da177e4 928 goto out;
023a5355 929 }
bc56bba8
EB
930
931 err = -EACCES;
932 if (ipcperms(&shp->shm_perm, acc_mode))
933 goto out_unlock;
1da177e4
LT
934
935 err = security_shm_shmat(shp, shmaddr, shmflg);
bc56bba8
EB
936 if (err)
937 goto out_unlock;
938
939 path.dentry = dget(shp->shm_file->f_path.dentry);
ce8d2cdf 940 path.mnt = shp->shm_file->f_path.mnt;
1da177e4 941 shp->shm_nattch++;
bc56bba8 942 size = i_size_read(path.dentry->d_inode);
1da177e4
LT
943 shm_unlock(shp);
944
bc56bba8
EB
945 err = -ENOMEM;
946 sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
947 if (!sfd)
ce8d2cdf 948 goto out_put_dentry;
bc56bba8
EB
949
950 err = -ENOMEM;
ce8d2cdf
DH
951
952 file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
bc56bba8
EB
953 if (!file)
954 goto out_free;
955
bc56bba8 956 file->private_data = sfd;
bc56bba8 957 file->f_mapping = shp->shm_file->f_mapping;
7ca7e564 958 sfd->id = shp->shm_perm.id;
bc56bba8
EB
959 sfd->ns = get_ipc_ns(ns);
960 sfd->file = shp->shm_file;
961 sfd->vm_ops = NULL;
962
1da177e4
LT
963 down_write(&current->mm->mmap_sem);
964 if (addr && !(shmflg & SHM_REMAP)) {
bc56bba8 965 err = -EINVAL;
1da177e4
LT
966 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
bc56bba8
EB
977 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;
1da177e4
LT
982invalid:
983 up_write(&current->mm->mmap_sem);
984
bc56bba8
EB
985 fput(file);
986
987out_nattch:
4e982311
KK
988 mutex_lock(&shm_ids(ns).mutex);
989 shp = shm_lock(ns, shmid);
023a5355 990 BUG_ON(IS_ERR(shp));
1da177e4
LT
991 shp->shm_nattch--;
992 if(shp->shm_nattch == 0 &&
b33291c0 993 shp->shm_perm.mode & SHM_DEST)
4e982311 994 shm_destroy(ns, shp);
1da177e4
LT
995 else
996 shm_unlock(shp);
4e982311 997 mutex_unlock(&shm_ids(ns).mutex);
1da177e4 998
1da177e4
LT
999out:
1000 return err;
bc56bba8
EB
1001
1002out_unlock:
1003 shm_unlock(shp);
1004 goto out;
1005
1006out_free:
1007 kfree(sfd);
ce8d2cdf 1008out_put_dentry:
bc56bba8 1009 dput(path.dentry);
bc56bba8 1010 goto out_nattch;
1da177e4
LT
1011}
1012
7d87e14c
SR
1013asmlinkage 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
1da177e4
LT
1025/*
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
df1e2fb5
HD
1037 if (addr & ~PAGE_MASK)
1038 return retval;
1039
1da177e4
LT
1040 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 */
bc56bba8 1072 if ((vma->vm_ops == &shm_vm_ops) &&
1da177e4
LT
1073 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1074
1075
6d63079a 1076 size = vma->vm_file->f_path.dentry->d_inode->i_size;
1da177e4
LT
1077 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 */
8e36709d 1096 size = PAGE_ALIGN(size);
1da177e4
LT
1097 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 */
bc56bba8 1101 if ((vma->vm_ops == &shm_vm_ops) &&
1da177e4
LT
1102 (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
19b4946c 1113static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
1da177e4 1114{
19b4946c
MW
1115 struct shmid_kernel *shp = it;
1116 char *format;
1da177e4 1117
1da177e4
LT
1118#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"
1da177e4 1120
19b4946c
MW
1121 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,
7ca7e564 1127 shp->shm_perm.id,
b33291c0 1128 shp->shm_perm.mode,
19b4946c
MW
1129 shp->shm_segsz,
1130 shp->shm_cprid,
1131 shp->shm_lprid,
bc56bba8 1132 shp->shm_nattch,
19b4946c
MW
1133 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);
1da177e4
LT
1140}
1141#endif