blob: 587d836d80d9d07f075259d3efe73917413f8010 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/shm.c
3 * Copyright (C) 1992, 1993 Krishna Balasubramanian
4 * Many improvements/fixes by Bruno Haible.
5 * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
6 * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
7 *
8 * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
9 * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
10 * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
11 * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
12 * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
13 * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
14 * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
15 *
16 */
17
18#include <linux/config.h>
19#include <linux/slab.h>
20#include <linux/mm.h>
21#include <linux/hugetlb.h>
22#include <linux/shm.h>
23#include <linux/init.h>
24#include <linux/file.h>
25#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/shmem_fs.h>
27#include <linux/security.h>
28#include <linux/syscalls.h>
29#include <linux/audit.h>
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -070030#include <linux/ptrace.h>
Mike Waychison19b49462005-09-06 15:17:10 -070031#include <linux/seq_file.h>
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/uaccess.h>
34
35#include "util.h"
36
37#define shm_flags shm_perm.mode
38
39static struct file_operations shm_file_operations;
40static struct vm_operations_struct shm_vm_ops;
41
42static struct ipc_ids shm_ids;
43
44#define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
45#define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
46#define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
47#define shm_buildid(id, seq) \
48 ipc_buildid(&shm_ids, id, seq)
49
50static int newseg (key_t key, int shmflg, size_t size);
51static void shm_open (struct vm_area_struct *shmd);
52static void shm_close (struct vm_area_struct *shmd);
53#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070054static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#endif
56
57size_t shm_ctlmax = SHMMAX;
58size_t shm_ctlall = SHMALL;
59int shm_ctlmni = SHMMNI;
60
61static int shm_tot; /* total number of shared memory pages */
62
63void __init shm_init (void)
64{
65 ipc_init_ids(&shm_ids, 1);
Mike Waychison19b49462005-09-06 15:17:10 -070066 ipc_init_proc_interface("sysvipc/shm",
67 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n",
68 &shm_ids,
69 sysvipc_shm_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72static inline int shm_checkid(struct shmid_kernel *s, int id)
73{
74 if (ipc_checkid(&shm_ids,&s->shm_perm,id))
75 return -EIDRM;
76 return 0;
77}
78
79static inline struct shmid_kernel *shm_rmid(int id)
80{
81 return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
82}
83
84static inline int shm_addid(struct shmid_kernel *shp)
85{
86 return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni);
87}
88
89
90
91static inline void shm_inc (int id) {
92 struct shmid_kernel *shp;
93
94 if(!(shp = shm_lock(id)))
95 BUG();
96 shp->shm_atim = get_seconds();
97 shp->shm_lprid = current->tgid;
98 shp->shm_nattch++;
99 shm_unlock(shp);
100}
101
102/* This is called by fork, once for every shm attach. */
103static void shm_open (struct vm_area_struct *shmd)
104{
105 shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
106}
107
108/*
109 * shm_destroy - free the struct shmid_kernel
110 *
111 * @shp: struct to free
112 *
113 * It has to be called with shp and shm_ids.sem locked,
114 * but returns with shp unlocked and freed.
115 */
116static void shm_destroy (struct shmid_kernel *shp)
117{
118 shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
119 shm_rmid (shp->id);
120 shm_unlock(shp);
121 if (!is_file_hugepages(shp->shm_file))
122 shmem_lock(shp->shm_file, 0, shp->mlock_user);
123 else
124 user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size,
125 shp->mlock_user);
126 fput (shp->shm_file);
127 security_shm_free(shp);
128 ipc_rcu_putref(shp);
129}
130
131/*
132 * remove the attach descriptor shmd.
133 * free memory for segment if it is marked destroyed.
134 * The descriptor has already been removed from the current->mm->mmap list
135 * and will later be kfree()d.
136 */
137static void shm_close (struct vm_area_struct *shmd)
138{
139 struct file * file = shmd->vm_file;
140 int id = file->f_dentry->d_inode->i_ino;
141 struct shmid_kernel *shp;
142
143 down (&shm_ids.sem);
144 /* remove from the list of attaches of the shm segment */
145 if(!(shp = shm_lock(id)))
146 BUG();
147 shp->shm_lprid = current->tgid;
148 shp->shm_dtim = get_seconds();
149 shp->shm_nattch--;
150 if(shp->shm_nattch == 0 &&
151 shp->shm_flags & SHM_DEST)
152 shm_destroy (shp);
153 else
154 shm_unlock(shp);
155 up (&shm_ids.sem);
156}
157
158static int shm_mmap(struct file * file, struct vm_area_struct * vma)
159{
160 file_accessed(file);
161 vma->vm_ops = &shm_vm_ops;
162 shm_inc(file->f_dentry->d_inode->i_ino);
163 return 0;
164}
165
166static struct file_operations shm_file_operations = {
167 .mmap = shm_mmap
168};
169
170static struct vm_operations_struct shm_vm_ops = {
171 .open = shm_open, /* callback for a new vm-area open */
172 .close = shm_close, /* callback for when the vm-area is released */
173 .nopage = shmem_nopage,
Andrew Morton6ade43f2005-08-01 21:11:45 -0700174#if defined(CONFIG_NUMA) && defined(CONFIG_SHMEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .set_policy = shmem_set_policy,
176 .get_policy = shmem_get_policy,
177#endif
178};
179
180static int newseg (key_t key, int shmflg, size_t size)
181{
182 int error;
183 struct shmid_kernel *shp;
184 int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
185 struct file * file;
186 char name[13];
187 int id;
188
189 if (size < SHMMIN || size > shm_ctlmax)
190 return -EINVAL;
191
192 if (shm_tot + numpages >= shm_ctlall)
193 return -ENOSPC;
194
195 shp = ipc_rcu_alloc(sizeof(*shp));
196 if (!shp)
197 return -ENOMEM;
198
199 shp->shm_perm.key = key;
200 shp->shm_flags = (shmflg & S_IRWXUGO);
201 shp->mlock_user = NULL;
202
203 shp->shm_perm.security = NULL;
204 error = security_shm_alloc(shp);
205 if (error) {
206 ipc_rcu_putref(shp);
207 return error;
208 }
209
210 if (shmflg & SHM_HUGETLB) {
211 /* hugetlb_zero_setup takes care of mlock user accounting */
212 file = hugetlb_zero_setup(size);
213 shp->mlock_user = current->user;
214 } else {
Badari Pulavartybf8f9722005-11-07 00:59:27 -0800215 int acctflag = VM_ACCOUNT;
216 /*
217 * Do not allow no accounting for OVERCOMMIT_NEVER, even
218 * if it's asked for.
219 */
220 if ((shmflg & SHM_NORESERVE) &&
221 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
222 acctflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 sprintf (name, "SYSV%08x", key);
Badari Pulavartybf8f9722005-11-07 00:59:27 -0800224 file = shmem_file_setup(name, size, acctflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226 error = PTR_ERR(file);
227 if (IS_ERR(file))
228 goto no_file;
229
230 error = -ENOSPC;
231 id = shm_addid(shp);
232 if(id == -1)
233 goto no_id;
234
235 shp->shm_cprid = current->tgid;
236 shp->shm_lprid = 0;
237 shp->shm_atim = shp->shm_dtim = 0;
238 shp->shm_ctim = get_seconds();
239 shp->shm_segsz = size;
240 shp->shm_nattch = 0;
241 shp->id = shm_buildid(id,shp->shm_perm.seq);
242 shp->shm_file = file;
243 file->f_dentry->d_inode->i_ino = shp->id;
Krishnakumar R551110a2005-10-29 18:16:45 -0700244
245 /* Hugetlb ops would have already been assigned. */
246 if (!(shmflg & SHM_HUGETLB))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 file->f_op = &shm_file_operations;
Krishnakumar R551110a2005-10-29 18:16:45 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 shm_tot += numpages;
250 shm_unlock(shp);
251 return shp->id;
252
253no_id:
254 fput(file);
255no_file:
256 security_shm_free(shp);
257 ipc_rcu_putref(shp);
258 return error;
259}
260
261asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
262{
263 struct shmid_kernel *shp;
264 int err, id = 0;
265
266 down(&shm_ids.sem);
267 if (key == IPC_PRIVATE) {
268 err = newseg(key, shmflg, size);
269 } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
270 if (!(shmflg & IPC_CREAT))
271 err = -ENOENT;
272 else
273 err = newseg(key, shmflg, size);
274 } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
275 err = -EEXIST;
276 } else {
277 shp = shm_lock(id);
278 if(shp==NULL)
279 BUG();
280 if (shp->shm_segsz < size)
281 err = -EINVAL;
282 else if (ipcperms(&shp->shm_perm, shmflg))
283 err = -EACCES;
284 else {
285 int shmid = shm_buildid(id, shp->shm_perm.seq);
286 err = security_shm_associate(shp, shmflg);
287 if (!err)
288 err = shmid;
289 }
290 shm_unlock(shp);
291 }
292 up(&shm_ids.sem);
293
294 return err;
295}
296
297static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
298{
299 switch(version) {
300 case IPC_64:
301 return copy_to_user(buf, in, sizeof(*in));
302 case IPC_OLD:
303 {
304 struct shmid_ds out;
305
306 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
307 out.shm_segsz = in->shm_segsz;
308 out.shm_atime = in->shm_atime;
309 out.shm_dtime = in->shm_dtime;
310 out.shm_ctime = in->shm_ctime;
311 out.shm_cpid = in->shm_cpid;
312 out.shm_lpid = in->shm_lpid;
313 out.shm_nattch = in->shm_nattch;
314
315 return copy_to_user(buf, &out, sizeof(out));
316 }
317 default:
318 return -EINVAL;
319 }
320}
321
322struct shm_setbuf {
323 uid_t uid;
324 gid_t gid;
325 mode_t mode;
326};
327
328static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
329{
330 switch(version) {
331 case IPC_64:
332 {
333 struct shmid64_ds tbuf;
334
335 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
336 return -EFAULT;
337
338 out->uid = tbuf.shm_perm.uid;
339 out->gid = tbuf.shm_perm.gid;
340 out->mode = tbuf.shm_flags;
341
342 return 0;
343 }
344 case IPC_OLD:
345 {
346 struct shmid_ds tbuf_old;
347
348 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
349 return -EFAULT;
350
351 out->uid = tbuf_old.shm_perm.uid;
352 out->gid = tbuf_old.shm_perm.gid;
353 out->mode = tbuf_old.shm_flags;
354
355 return 0;
356 }
357 default:
358 return -EINVAL;
359 }
360}
361
362static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
363{
364 switch(version) {
365 case IPC_64:
366 return copy_to_user(buf, in, sizeof(*in));
367 case IPC_OLD:
368 {
369 struct shminfo out;
370
371 if(in->shmmax > INT_MAX)
372 out.shmmax = INT_MAX;
373 else
374 out.shmmax = (int)in->shmmax;
375
376 out.shmmin = in->shmmin;
377 out.shmmni = in->shmmni;
378 out.shmseg = in->shmseg;
379 out.shmall = in->shmall;
380
381 return copy_to_user(buf, &out, sizeof(out));
382 }
383 default:
384 return -EINVAL;
385 }
386}
387
388static void shm_get_stat(unsigned long *rss, unsigned long *swp)
389{
390 int i;
391
392 *rss = 0;
393 *swp = 0;
394
395 for (i = 0; i <= shm_ids.max_id; i++) {
396 struct shmid_kernel *shp;
397 struct inode *inode;
398
399 shp = shm_get(i);
400 if(!shp)
401 continue;
402
403 inode = shp->shm_file->f_dentry->d_inode;
404
405 if (is_file_hugepages(shp->shm_file)) {
406 struct address_space *mapping = inode->i_mapping;
407 *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
408 } else {
409 struct shmem_inode_info *info = SHMEM_I(inode);
410 spin_lock(&info->lock);
411 *rss += inode->i_mapping->nrpages;
412 *swp += info->swapped;
413 spin_unlock(&info->lock);
414 }
415 }
416}
417
418asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
419{
420 struct shm_setbuf setbuf;
421 struct shmid_kernel *shp;
422 int err, version;
423
424 if (cmd < 0 || shmid < 0) {
425 err = -EINVAL;
426 goto out;
427 }
428
429 version = ipc_parse_version(&cmd);
430
431 switch (cmd) { /* replace with proc interface ? */
432 case IPC_INFO:
433 {
434 struct shminfo64 shminfo;
435
436 err = security_shm_shmctl(NULL, cmd);
437 if (err)
438 return err;
439
440 memset(&shminfo,0,sizeof(shminfo));
441 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
442 shminfo.shmmax = shm_ctlmax;
443 shminfo.shmall = shm_ctlall;
444
445 shminfo.shmmin = SHMMIN;
446 if(copy_shminfo_to_user (buf, &shminfo, version))
447 return -EFAULT;
448 /* reading a integer is always atomic */
449 err= shm_ids.max_id;
450 if(err<0)
451 err = 0;
452 goto out;
453 }
454 case SHM_INFO:
455 {
456 struct shm_info shm_info;
457
458 err = security_shm_shmctl(NULL, cmd);
459 if (err)
460 return err;
461
462 memset(&shm_info,0,sizeof(shm_info));
463 down(&shm_ids.sem);
464 shm_info.used_ids = shm_ids.in_use;
465 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
466 shm_info.shm_tot = shm_tot;
467 shm_info.swap_attempts = 0;
468 shm_info.swap_successes = 0;
469 err = shm_ids.max_id;
470 up(&shm_ids.sem);
471 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
472 err = -EFAULT;
473 goto out;
474 }
475
476 err = err < 0 ? 0 : err;
477 goto out;
478 }
479 case SHM_STAT:
480 case IPC_STAT:
481 {
482 struct shmid64_ds tbuf;
483 int result;
484 memset(&tbuf, 0, sizeof(tbuf));
485 shp = shm_lock(shmid);
486 if(shp==NULL) {
487 err = -EINVAL;
488 goto out;
489 } else if(cmd==SHM_STAT) {
490 err = -EINVAL;
491 if (shmid > shm_ids.max_id)
492 goto out_unlock;
493 result = shm_buildid(shmid, shp->shm_perm.seq);
494 } else {
495 err = shm_checkid(shp,shmid);
496 if(err)
497 goto out_unlock;
498 result = 0;
499 }
500 err=-EACCES;
501 if (ipcperms (&shp->shm_perm, S_IRUGO))
502 goto out_unlock;
503 err = security_shm_shmctl(shp, cmd);
504 if (err)
505 goto out_unlock;
506 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
507 tbuf.shm_segsz = shp->shm_segsz;
508 tbuf.shm_atime = shp->shm_atim;
509 tbuf.shm_dtime = shp->shm_dtim;
510 tbuf.shm_ctime = shp->shm_ctim;
511 tbuf.shm_cpid = shp->shm_cprid;
512 tbuf.shm_lpid = shp->shm_lprid;
513 if (!is_file_hugepages(shp->shm_file))
514 tbuf.shm_nattch = shp->shm_nattch;
515 else
516 tbuf.shm_nattch = file_count(shp->shm_file) - 1;
517 shm_unlock(shp);
518 if(copy_shmid_to_user (buf, &tbuf, version))
519 err = -EFAULT;
520 else
521 err = result;
522 goto out;
523 }
524 case SHM_LOCK:
525 case SHM_UNLOCK:
526 {
527 shp = shm_lock(shmid);
528 if(shp==NULL) {
529 err = -EINVAL;
530 goto out;
531 }
532 err = shm_checkid(shp,shmid);
533 if(err)
534 goto out_unlock;
535
536 if (!capable(CAP_IPC_LOCK)) {
537 err = -EPERM;
538 if (current->euid != shp->shm_perm.uid &&
539 current->euid != shp->shm_perm.cuid)
540 goto out_unlock;
541 if (cmd == SHM_LOCK &&
542 !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
543 goto out_unlock;
544 }
545
546 err = security_shm_shmctl(shp, cmd);
547 if (err)
548 goto out_unlock;
549
550 if(cmd==SHM_LOCK) {
551 struct user_struct * user = current->user;
552 if (!is_file_hugepages(shp->shm_file)) {
553 err = shmem_lock(shp->shm_file, 1, user);
554 if (!err) {
555 shp->shm_flags |= SHM_LOCKED;
556 shp->mlock_user = user;
557 }
558 }
559 } else if (!is_file_hugepages(shp->shm_file)) {
560 shmem_lock(shp->shm_file, 0, shp->mlock_user);
561 shp->shm_flags &= ~SHM_LOCKED;
562 shp->mlock_user = NULL;
563 }
564 shm_unlock(shp);
565 goto out;
566 }
567 case IPC_RMID:
568 {
569 /*
570 * We cannot simply remove the file. The SVID states
571 * that the block remains until the last person
572 * detaches from it, then is deleted. A shmat() on
573 * an RMID segment is legal in older Linux and if
574 * we change it apps break...
575 *
576 * Instead we set a destroyed flag, and then blow
577 * the name away when the usage hits zero.
578 */
579 down(&shm_ids.sem);
580 shp = shm_lock(shmid);
581 err = -EINVAL;
582 if (shp == NULL)
583 goto out_up;
584 err = shm_checkid(shp, shmid);
585 if(err)
586 goto out_unlock_up;
587
588 if (current->euid != shp->shm_perm.uid &&
589 current->euid != shp->shm_perm.cuid &&
590 !capable(CAP_SYS_ADMIN)) {
591 err=-EPERM;
592 goto out_unlock_up;
593 }
594
595 err = security_shm_shmctl(shp, cmd);
596 if (err)
597 goto out_unlock_up;
598
599 if (shp->shm_nattch){
600 shp->shm_flags |= SHM_DEST;
601 /* Do not find it any more */
602 shp->shm_perm.key = IPC_PRIVATE;
603 shm_unlock(shp);
604 } else
605 shm_destroy (shp);
606 up(&shm_ids.sem);
607 goto out;
608 }
609
610 case IPC_SET:
611 {
612 if (copy_shmid_from_user (&setbuf, buf, version)) {
613 err = -EFAULT;
614 goto out;
615 }
616 if ((err = audit_ipc_perms(0, setbuf.uid, setbuf.gid, setbuf.mode)))
617 return err;
618 down(&shm_ids.sem);
619 shp = shm_lock(shmid);
620 err=-EINVAL;
621 if(shp==NULL)
622 goto out_up;
623 err = shm_checkid(shp,shmid);
624 if(err)
625 goto out_unlock_up;
626 err=-EPERM;
627 if (current->euid != shp->shm_perm.uid &&
628 current->euid != shp->shm_perm.cuid &&
629 !capable(CAP_SYS_ADMIN)) {
630 goto out_unlock_up;
631 }
632
633 err = security_shm_shmctl(shp, cmd);
634 if (err)
635 goto out_unlock_up;
636
637 shp->shm_perm.uid = setbuf.uid;
638 shp->shm_perm.gid = setbuf.gid;
639 shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
640 | (setbuf.mode & S_IRWXUGO);
641 shp->shm_ctim = get_seconds();
642 break;
643 }
644
645 default:
646 err = -EINVAL;
647 goto out;
648 }
649
650 err = 0;
651out_unlock_up:
652 shm_unlock(shp);
653out_up:
654 up(&shm_ids.sem);
655 goto out;
656out_unlock:
657 shm_unlock(shp);
658out:
659 return err;
660}
661
662/*
663 * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
664 *
665 * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
666 * "raddr" thing points to kernel space, and there has to be a wrapper around
667 * this.
668 */
669long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
670{
671 struct shmid_kernel *shp;
672 unsigned long addr;
673 unsigned long size;
674 struct file * file;
675 int err;
676 unsigned long flags;
677 unsigned long prot;
678 unsigned long o_flags;
679 int acc_mode;
680 void *user_addr;
681
682 if (shmid < 0) {
683 err = -EINVAL;
684 goto out;
685 } else if ((addr = (ulong)shmaddr)) {
686 if (addr & (SHMLBA-1)) {
687 if (shmflg & SHM_RND)
688 addr &= ~(SHMLBA-1); /* round down */
689 else
690#ifndef __ARCH_FORCE_SHMLBA
691 if (addr & ~PAGE_MASK)
692#endif
693 return -EINVAL;
694 }
695 flags = MAP_SHARED | MAP_FIXED;
696 } else {
697 if ((shmflg & SHM_REMAP))
698 return -EINVAL;
699
700 flags = MAP_SHARED;
701 }
702
703 if (shmflg & SHM_RDONLY) {
704 prot = PROT_READ;
705 o_flags = O_RDONLY;
706 acc_mode = S_IRUGO;
707 } else {
708 prot = PROT_READ | PROT_WRITE;
709 o_flags = O_RDWR;
710 acc_mode = S_IRUGO | S_IWUGO;
711 }
712 if (shmflg & SHM_EXEC) {
713 prot |= PROT_EXEC;
714 acc_mode |= S_IXUGO;
715 }
716
717 /*
718 * We cannot rely on the fs check since SYSV IPC does have an
719 * additional creator id...
720 */
721 shp = shm_lock(shmid);
722 if(shp == NULL) {
723 err = -EINVAL;
724 goto out;
725 }
726 err = shm_checkid(shp,shmid);
727 if (err) {
728 shm_unlock(shp);
729 goto out;
730 }
731 if (ipcperms(&shp->shm_perm, acc_mode)) {
732 shm_unlock(shp);
733 err = -EACCES;
734 goto out;
735 }
736
737 err = security_shm_shmat(shp, shmaddr, shmflg);
738 if (err) {
739 shm_unlock(shp);
740 return err;
741 }
742
743 file = shp->shm_file;
744 size = i_size_read(file->f_dentry->d_inode);
745 shp->shm_nattch++;
746 shm_unlock(shp);
747
748 down_write(&current->mm->mmap_sem);
749 if (addr && !(shmflg & SHM_REMAP)) {
750 user_addr = ERR_PTR(-EINVAL);
751 if (find_vma_intersection(current->mm, addr, addr + size))
752 goto invalid;
753 /*
754 * If shm segment goes below stack, make sure there is some
755 * space left for the stack to grow (at least 4 pages).
756 */
757 if (addr < current->mm->start_stack &&
758 addr > current->mm->start_stack - size - PAGE_SIZE * 5)
759 goto invalid;
760 }
761
762 user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
763
764invalid:
765 up_write(&current->mm->mmap_sem);
766
767 down (&shm_ids.sem);
768 if(!(shp = shm_lock(shmid)))
769 BUG();
770 shp->shm_nattch--;
771 if(shp->shm_nattch == 0 &&
772 shp->shm_flags & SHM_DEST)
773 shm_destroy (shp);
774 else
775 shm_unlock(shp);
776 up (&shm_ids.sem);
777
778 *raddr = (unsigned long) user_addr;
779 err = 0;
780 if (IS_ERR(user_addr))
781 err = PTR_ERR(user_addr);
782out:
783 return err;
784}
785
Stephen Rothwell7d87e14c2005-05-01 08:59:12 -0700786asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
787{
788 unsigned long ret;
789 long err;
790
791 err = do_shmat(shmid, shmaddr, shmflg, &ret);
792 if (err)
793 return err;
794 force_successful_syscall_return();
795 return (long)ret;
796}
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/*
799 * detach and kill segment if marked destroyed.
800 * The work is done in shm_close.
801 */
802asmlinkage long sys_shmdt(char __user *shmaddr)
803{
804 struct mm_struct *mm = current->mm;
805 struct vm_area_struct *vma, *next;
806 unsigned long addr = (unsigned long)shmaddr;
807 loff_t size = 0;
808 int retval = -EINVAL;
809
810 down_write(&mm->mmap_sem);
811
812 /*
813 * This function tries to be smart and unmap shm segments that
814 * were modified by partial mlock or munmap calls:
815 * - It first determines the size of the shm segment that should be
816 * unmapped: It searches for a vma that is backed by shm and that
817 * started at address shmaddr. It records it's size and then unmaps
818 * it.
819 * - Then it unmaps all shm vmas that started at shmaddr and that
820 * are within the initially determined size.
821 * Errors from do_munmap are ignored: the function only fails if
822 * it's called with invalid parameters or if it's called to unmap
823 * a part of a vma. Both calls in this function are for full vmas,
824 * the parameters are directly copied from the vma itself and always
825 * valid - therefore do_munmap cannot fail. (famous last words?)
826 */
827 /*
828 * If it had been mremap()'d, the starting address would not
829 * match the usual checks anyway. So assume all vma's are
830 * above the starting address given.
831 */
832 vma = find_vma(mm, addr);
833
834 while (vma) {
835 next = vma->vm_next;
836
837 /*
838 * Check if the starting address would match, i.e. it's
839 * a fragment created by mprotect() and/or munmap(), or it
840 * otherwise it starts at this address with no hassles.
841 */
842 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
843 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
844
845
846 size = vma->vm_file->f_dentry->d_inode->i_size;
847 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
848 /*
849 * We discovered the size of the shm segment, so
850 * break out of here and fall through to the next
851 * loop that uses the size information to stop
852 * searching for matching vma's.
853 */
854 retval = 0;
855 vma = next;
856 break;
857 }
858 vma = next;
859 }
860
861 /*
862 * We need look no further than the maximum address a fragment
863 * could possibly have landed at. Also cast things to loff_t to
864 * prevent overflows and make comparisions vs. equal-width types.
865 */
866 while (vma && (loff_t)(vma->vm_end - addr) <= size) {
867 next = vma->vm_next;
868
869 /* finding a matching vma now does not alter retval */
870 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
871 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
872
873 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
874 vma = next;
875 }
876
877 up_write(&mm->mmap_sem);
878 return retval;
879}
880
881#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700882static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Mike Waychison19b49462005-09-06 15:17:10 -0700884 struct shmid_kernel *shp = it;
885 char *format;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
888#define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Mike Waychison19b49462005-09-06 15:17:10 -0700890 if (sizeof(size_t) <= sizeof(int))
891 format = SMALL_STRING;
892 else
893 format = BIG_STRING;
894 return seq_printf(s, format,
895 shp->shm_perm.key,
896 shp->id,
897 shp->shm_flags,
898 shp->shm_segsz,
899 shp->shm_cprid,
900 shp->shm_lprid,
901 is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
902 shp->shm_perm.uid,
903 shp->shm_perm.gid,
904 shp->shm_perm.cuid,
905 shp->shm_perm.cgid,
906 shp->shm_atim,
907 shp->shm_dtim,
908 shp->shm_ctim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910#endif