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