ipc/msg: fix race around refcount
[linux/fpc-iii.git] / ipc / msg.c
blob25f1a6139584a51000b272e6f703f004994110d9
1 /*
2 * linux/ipc/msg.c
3 * Copyright (C) 1992 Krishna Balasubramanian
5 * Removed all the remaining kerneld mess
6 * Catch the -EFAULT stuff properly
7 * Use GFP_KERNEL for messages as in 1.2
8 * Fixed up the unchecked user space derefs
9 * Copyright (C) 1998 Alan Cox & Andi Kleen
11 * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
13 * mostly rewritten, threaded and wake-one semantics added
14 * MSGMAX limit removed, sysctl's added
15 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
25 #include <linux/capability.h>
26 #include <linux/msg.h>
27 #include <linux/spinlock.h>
28 #include <linux/init.h>
29 #include <linux/mm.h>
30 #include <linux/proc_fs.h>
31 #include <linux/list.h>
32 #include <linux/security.h>
33 #include <linux/sched.h>
34 #include <linux/syscalls.h>
35 #include <linux/audit.h>
36 #include <linux/seq_file.h>
37 #include <linux/rwsem.h>
38 #include <linux/nsproxy.h>
39 #include <linux/ipc_namespace.h>
41 #include <asm/current.h>
42 #include <asm/uaccess.h>
43 #include "util.h"
46 * one msg_receiver structure for each sleeping receiver:
48 struct msg_receiver {
49 struct list_head r_list;
50 struct task_struct *r_tsk;
52 int r_mode;
53 long r_msgtype;
54 long r_maxsize;
56 struct msg_msg *volatile r_msg;
59 /* one msg_sender for each sleeping sender */
60 struct msg_sender {
61 struct list_head list;
62 struct task_struct *tsk;
65 #define SEARCH_ANY 1
66 #define SEARCH_EQUAL 2
67 #define SEARCH_NOTEQUAL 3
68 #define SEARCH_LESSEQUAL 4
70 #define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
72 #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
74 static void freeque(struct ipc_namespace *, struct kern_ipc_perm *);
75 static int newque(struct ipc_namespace *, struct ipc_params *);
76 #ifdef CONFIG_PROC_FS
77 static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
78 #endif
81 * Scale msgmni with the available lowmem size: the memory dedicated to msg
82 * queues should occupy at most 1/MSG_MEM_SCALE of lowmem.
83 * Also take into account the number of nsproxies created so far.
84 * This should be done staying within the (MSGMNI , IPCMNI/nr_ipc_ns) range.
86 void recompute_msgmni(struct ipc_namespace *ns)
88 struct sysinfo i;
89 unsigned long allowed;
90 int nb_ns;
92 si_meminfo(&i);
93 allowed = (((i.totalram - i.totalhigh) / MSG_MEM_SCALE) * i.mem_unit)
94 / MSGMNB;
95 nb_ns = atomic_read(&nr_ipc_ns);
96 allowed /= nb_ns;
98 if (allowed < MSGMNI) {
99 ns->msg_ctlmni = MSGMNI;
100 return;
103 if (allowed > IPCMNI / nb_ns) {
104 ns->msg_ctlmni = IPCMNI / nb_ns;
105 return;
108 ns->msg_ctlmni = allowed;
111 void msg_init_ns(struct ipc_namespace *ns)
113 ns->msg_ctlmax = MSGMAX;
114 ns->msg_ctlmnb = MSGMNB;
116 recompute_msgmni(ns);
118 atomic_set(&ns->msg_bytes, 0);
119 atomic_set(&ns->msg_hdrs, 0);
120 ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
123 #ifdef CONFIG_IPC_NS
124 void msg_exit_ns(struct ipc_namespace *ns)
126 free_ipcs(ns, &msg_ids(ns), freeque);
127 idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
129 #endif
131 void __init msg_init(void)
133 msg_init_ns(&init_ipc_ns);
135 printk(KERN_INFO "msgmni has been set to %d\n",
136 init_ipc_ns.msg_ctlmni);
138 ipc_init_proc_interface("sysvipc/msg",
139 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
140 IPC_MSG_IDS, sysvipc_msg_proc_show);
144 * msg_lock_(check_) routines are called in the paths where the rw_mutex
145 * is not held.
147 static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
149 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
151 if (IS_ERR(ipcp))
152 return (struct msg_queue *)ipcp;
154 return container_of(ipcp, struct msg_queue, q_perm);
157 static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
158 int id)
160 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
162 if (IS_ERR(ipcp))
163 return (struct msg_queue *)ipcp;
165 return container_of(ipcp, struct msg_queue, q_perm);
168 static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
170 ipc_rmid(&msg_ids(ns), &s->q_perm);
174 * newque - Create a new msg queue
175 * @ns: namespace
176 * @params: ptr to the structure that contains the key and msgflg
178 * Called with msg_ids.rw_mutex held (writer)
180 static int newque(struct ipc_namespace *ns, struct ipc_params *params)
182 struct msg_queue *msq;
183 int id, retval;
184 key_t key = params->key;
185 int msgflg = params->flg;
187 msq = ipc_rcu_alloc(sizeof(*msq));
188 if (!msq)
189 return -ENOMEM;
191 msq->q_perm.mode = msgflg & S_IRWXUGO;
192 msq->q_perm.key = key;
194 msq->q_perm.security = NULL;
195 retval = security_msg_queue_alloc(msq);
196 if (retval) {
197 ipc_rcu_putref(msq);
198 return retval;
202 * ipc_addid() locks msq
204 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
205 if (id < 0) {
206 security_msg_queue_free(msq);
207 ipc_rcu_putref(msq);
208 return id;
211 msq->q_stime = msq->q_rtime = 0;
212 msq->q_ctime = get_seconds();
213 msq->q_cbytes = msq->q_qnum = 0;
214 msq->q_qbytes = ns->msg_ctlmnb;
215 msq->q_lspid = msq->q_lrpid = 0;
216 INIT_LIST_HEAD(&msq->q_messages);
217 INIT_LIST_HEAD(&msq->q_receivers);
218 INIT_LIST_HEAD(&msq->q_senders);
220 msg_unlock(msq);
222 return msq->q_perm.id;
225 static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
227 mss->tsk = current;
228 current->state = TASK_INTERRUPTIBLE;
229 list_add_tail(&mss->list, &msq->q_senders);
232 static inline void ss_del(struct msg_sender *mss)
234 if (mss->list.next != NULL)
235 list_del(&mss->list);
238 static void ss_wakeup(struct list_head *h, int kill)
240 struct list_head *tmp;
242 tmp = h->next;
243 while (tmp != h) {
244 struct msg_sender *mss;
246 mss = list_entry(tmp, struct msg_sender, list);
247 tmp = tmp->next;
248 if (kill)
249 mss->list.next = NULL;
250 wake_up_process(mss->tsk);
254 static void expunge_all(struct msg_queue *msq, int res)
256 struct list_head *tmp;
258 tmp = msq->q_receivers.next;
259 while (tmp != &msq->q_receivers) {
260 struct msg_receiver *msr;
262 msr = list_entry(tmp, struct msg_receiver, r_list);
263 tmp = tmp->next;
264 msr->r_msg = NULL;
265 wake_up_process(msr->r_tsk);
266 smp_mb();
267 msr->r_msg = ERR_PTR(res);
272 * freeque() wakes up waiters on the sender and receiver waiting queue,
273 * removes the message queue from message queue ID IDR, and cleans up all the
274 * messages associated with this queue.
276 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
277 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
279 static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
281 struct list_head *tmp;
282 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
284 expunge_all(msq, -EIDRM);
285 ss_wakeup(&msq->q_senders, 1);
286 msg_rmid(ns, msq);
287 msg_unlock(msq);
289 tmp = msq->q_messages.next;
290 while (tmp != &msq->q_messages) {
291 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
293 tmp = tmp->next;
294 atomic_dec(&ns->msg_hdrs);
295 free_msg(msg);
297 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
298 security_msg_queue_free(msq);
299 ipc_lock_by_ptr(&msq->q_perm);
300 ipc_rcu_putref(msq);
301 ipc_unlock(&msq->q_perm);
305 * Called with msg_ids.rw_mutex and ipcp locked.
307 static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
309 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
311 return security_msg_queue_associate(msq, msgflg);
314 SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
316 struct ipc_namespace *ns;
317 struct ipc_ops msg_ops;
318 struct ipc_params msg_params;
320 ns = current->nsproxy->ipc_ns;
322 msg_ops.getnew = newque;
323 msg_ops.associate = msg_security;
324 msg_ops.more_checks = NULL;
326 msg_params.key = key;
327 msg_params.flg = msgflg;
329 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
332 static inline unsigned long
333 copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
335 switch(version) {
336 case IPC_64:
337 return copy_to_user(buf, in, sizeof(*in));
338 case IPC_OLD:
340 struct msqid_ds out;
342 memset(&out, 0, sizeof(out));
344 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
346 out.msg_stime = in->msg_stime;
347 out.msg_rtime = in->msg_rtime;
348 out.msg_ctime = in->msg_ctime;
350 if (in->msg_cbytes > USHRT_MAX)
351 out.msg_cbytes = USHRT_MAX;
352 else
353 out.msg_cbytes = in->msg_cbytes;
354 out.msg_lcbytes = in->msg_cbytes;
356 if (in->msg_qnum > USHRT_MAX)
357 out.msg_qnum = USHRT_MAX;
358 else
359 out.msg_qnum = in->msg_qnum;
361 if (in->msg_qbytes > USHRT_MAX)
362 out.msg_qbytes = USHRT_MAX;
363 else
364 out.msg_qbytes = in->msg_qbytes;
365 out.msg_lqbytes = in->msg_qbytes;
367 out.msg_lspid = in->msg_lspid;
368 out.msg_lrpid = in->msg_lrpid;
370 return copy_to_user(buf, &out, sizeof(out));
372 default:
373 return -EINVAL;
377 static inline unsigned long
378 copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
380 switch(version) {
381 case IPC_64:
382 if (copy_from_user(out, buf, sizeof(*out)))
383 return -EFAULT;
384 return 0;
385 case IPC_OLD:
387 struct msqid_ds tbuf_old;
389 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
390 return -EFAULT;
392 out->msg_perm.uid = tbuf_old.msg_perm.uid;
393 out->msg_perm.gid = tbuf_old.msg_perm.gid;
394 out->msg_perm.mode = tbuf_old.msg_perm.mode;
396 if (tbuf_old.msg_qbytes == 0)
397 out->msg_qbytes = tbuf_old.msg_lqbytes;
398 else
399 out->msg_qbytes = tbuf_old.msg_qbytes;
401 return 0;
403 default:
404 return -EINVAL;
409 * This function handles some msgctl commands which require the rw_mutex
410 * to be held in write mode.
411 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
413 static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
414 struct msqid_ds __user *buf, int version)
416 struct kern_ipc_perm *ipcp;
417 struct msqid64_ds uninitialized_var(msqid64);
418 struct msg_queue *msq;
419 int err;
421 if (cmd == IPC_SET) {
422 if (copy_msqid_from_user(&msqid64, buf, version))
423 return -EFAULT;
426 ipcp = ipcctl_pre_down(ns, &msg_ids(ns), msqid, cmd,
427 &msqid64.msg_perm, msqid64.msg_qbytes);
428 if (IS_ERR(ipcp))
429 return PTR_ERR(ipcp);
431 msq = container_of(ipcp, struct msg_queue, q_perm);
433 err = security_msg_queue_msgctl(msq, cmd);
434 if (err)
435 goto out_unlock;
437 switch (cmd) {
438 case IPC_RMID:
439 freeque(ns, ipcp);
440 goto out_up;
441 case IPC_SET:
442 if (msqid64.msg_qbytes > ns->msg_ctlmnb &&
443 !capable(CAP_SYS_RESOURCE)) {
444 err = -EPERM;
445 goto out_unlock;
448 msq->q_qbytes = msqid64.msg_qbytes;
450 ipc_update_perm(&msqid64.msg_perm, ipcp);
451 msq->q_ctime = get_seconds();
452 /* sleeping receivers might be excluded by
453 * stricter permissions.
455 expunge_all(msq, -EAGAIN);
456 /* sleeping senders might be able to send
457 * due to a larger queue size.
459 ss_wakeup(&msq->q_senders, 0);
460 break;
461 default:
462 err = -EINVAL;
464 out_unlock:
465 msg_unlock(msq);
466 out_up:
467 up_write(&msg_ids(ns).rw_mutex);
468 return err;
471 SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
473 struct msg_queue *msq;
474 int err, version;
475 struct ipc_namespace *ns;
477 if (msqid < 0 || cmd < 0)
478 return -EINVAL;
480 version = ipc_parse_version(&cmd);
481 ns = current->nsproxy->ipc_ns;
483 switch (cmd) {
484 case IPC_INFO:
485 case MSG_INFO:
487 struct msginfo msginfo;
488 int max_id;
490 if (!buf)
491 return -EFAULT;
493 * We must not return kernel stack data.
494 * due to padding, it's not enough
495 * to set all member fields.
497 err = security_msg_queue_msgctl(NULL, cmd);
498 if (err)
499 return err;
501 memset(&msginfo, 0, sizeof(msginfo));
502 msginfo.msgmni = ns->msg_ctlmni;
503 msginfo.msgmax = ns->msg_ctlmax;
504 msginfo.msgmnb = ns->msg_ctlmnb;
505 msginfo.msgssz = MSGSSZ;
506 msginfo.msgseg = MSGSEG;
507 down_read(&msg_ids(ns).rw_mutex);
508 if (cmd == MSG_INFO) {
509 msginfo.msgpool = msg_ids(ns).in_use;
510 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
511 msginfo.msgtql = atomic_read(&ns->msg_bytes);
512 } else {
513 msginfo.msgmap = MSGMAP;
514 msginfo.msgpool = MSGPOOL;
515 msginfo.msgtql = MSGTQL;
517 max_id = ipc_get_maxid(&msg_ids(ns));
518 up_read(&msg_ids(ns).rw_mutex);
519 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
520 return -EFAULT;
521 return (max_id < 0) ? 0 : max_id;
523 case MSG_STAT: /* msqid is an index rather than a msg queue id */
524 case IPC_STAT:
526 struct msqid64_ds tbuf;
527 int success_return;
529 if (!buf)
530 return -EFAULT;
532 if (cmd == MSG_STAT) {
533 msq = msg_lock(ns, msqid);
534 if (IS_ERR(msq))
535 return PTR_ERR(msq);
536 success_return = msq->q_perm.id;
537 } else {
538 msq = msg_lock_check(ns, msqid);
539 if (IS_ERR(msq))
540 return PTR_ERR(msq);
541 success_return = 0;
543 err = -EACCES;
544 if (ipcperms(ns, &msq->q_perm, S_IRUGO))
545 goto out_unlock;
547 err = security_msg_queue_msgctl(msq, cmd);
548 if (err)
549 goto out_unlock;
551 memset(&tbuf, 0, sizeof(tbuf));
553 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
554 tbuf.msg_stime = msq->q_stime;
555 tbuf.msg_rtime = msq->q_rtime;
556 tbuf.msg_ctime = msq->q_ctime;
557 tbuf.msg_cbytes = msq->q_cbytes;
558 tbuf.msg_qnum = msq->q_qnum;
559 tbuf.msg_qbytes = msq->q_qbytes;
560 tbuf.msg_lspid = msq->q_lspid;
561 tbuf.msg_lrpid = msq->q_lrpid;
562 msg_unlock(msq);
563 if (copy_msqid_to_user(buf, &tbuf, version))
564 return -EFAULT;
565 return success_return;
567 case IPC_SET:
568 case IPC_RMID:
569 err = msgctl_down(ns, msqid, cmd, buf, version);
570 return err;
571 default:
572 return -EINVAL;
575 out_unlock:
576 msg_unlock(msq);
577 return err;
580 static int testmsg(struct msg_msg *msg, long type, int mode)
582 switch(mode)
584 case SEARCH_ANY:
585 return 1;
586 case SEARCH_LESSEQUAL:
587 if (msg->m_type <=type)
588 return 1;
589 break;
590 case SEARCH_EQUAL:
591 if (msg->m_type == type)
592 return 1;
593 break;
594 case SEARCH_NOTEQUAL:
595 if (msg->m_type != type)
596 return 1;
597 break;
599 return 0;
602 static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
604 struct list_head *tmp;
606 tmp = msq->q_receivers.next;
607 while (tmp != &msq->q_receivers) {
608 struct msg_receiver *msr;
610 msr = list_entry(tmp, struct msg_receiver, r_list);
611 tmp = tmp->next;
612 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
613 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
614 msr->r_msgtype, msr->r_mode)) {
616 list_del(&msr->r_list);
617 if (msr->r_maxsize < msg->m_ts) {
618 msr->r_msg = NULL;
619 wake_up_process(msr->r_tsk);
620 smp_mb();
621 msr->r_msg = ERR_PTR(-E2BIG);
622 } else {
623 msr->r_msg = NULL;
624 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
625 msq->q_rtime = get_seconds();
626 wake_up_process(msr->r_tsk);
627 smp_mb();
628 msr->r_msg = msg;
630 return 1;
634 return 0;
637 long do_msgsnd(int msqid, long mtype, void __user *mtext,
638 size_t msgsz, int msgflg)
640 struct msg_queue *msq;
641 struct msg_msg *msg;
642 int err;
643 struct ipc_namespace *ns;
645 ns = current->nsproxy->ipc_ns;
647 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
648 return -EINVAL;
649 if (mtype < 1)
650 return -EINVAL;
652 msg = load_msg(mtext, msgsz);
653 if (IS_ERR(msg))
654 return PTR_ERR(msg);
656 msg->m_type = mtype;
657 msg->m_ts = msgsz;
659 msq = msg_lock_check(ns, msqid);
660 if (IS_ERR(msq)) {
661 err = PTR_ERR(msq);
662 goto out_free;
665 for (;;) {
666 struct msg_sender s;
668 err = -EACCES;
669 if (ipcperms(ns, &msq->q_perm, S_IWUGO))
670 goto out_unlock_free;
672 err = security_msg_queue_msgsnd(msq, msg, msgflg);
673 if (err)
674 goto out_unlock_free;
676 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
677 1 + msq->q_qnum <= msq->q_qbytes) {
678 break;
681 /* queue full, wait: */
682 if (msgflg & IPC_NOWAIT) {
683 err = -EAGAIN;
684 goto out_unlock_free;
686 ss_add(msq, &s);
687 ipc_rcu_getref(msq);
688 msg_unlock(msq);
689 schedule();
691 ipc_lock_by_ptr(&msq->q_perm);
692 ipc_rcu_putref(msq);
693 if (msq->q_perm.deleted) {
694 err = -EIDRM;
695 goto out_unlock_free;
697 ss_del(&s);
699 if (signal_pending(current)) {
700 err = -ERESTARTNOHAND;
701 goto out_unlock_free;
705 msq->q_lspid = task_tgid_vnr(current);
706 msq->q_stime = get_seconds();
708 if (!pipelined_send(msq, msg)) {
709 /* no one is waiting for this message, enqueue it */
710 list_add_tail(&msg->m_list, &msq->q_messages);
711 msq->q_cbytes += msgsz;
712 msq->q_qnum++;
713 atomic_add(msgsz, &ns->msg_bytes);
714 atomic_inc(&ns->msg_hdrs);
717 err = 0;
718 msg = NULL;
720 out_unlock_free:
721 msg_unlock(msq);
722 out_free:
723 if (msg != NULL)
724 free_msg(msg);
725 return err;
728 SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
729 int, msgflg)
731 long mtype;
733 if (get_user(mtype, &msgp->mtype))
734 return -EFAULT;
735 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
738 static inline int convert_mode(long *msgtyp, int msgflg)
741 * find message of correct type.
742 * msgtyp = 0 => get first.
743 * msgtyp > 0 => get first message of matching type.
744 * msgtyp < 0 => get message with least type must be < abs(msgtype).
746 if (*msgtyp == 0)
747 return SEARCH_ANY;
748 if (*msgtyp < 0) {
749 *msgtyp = -*msgtyp;
750 return SEARCH_LESSEQUAL;
752 if (msgflg & MSG_EXCEPT)
753 return SEARCH_NOTEQUAL;
754 return SEARCH_EQUAL;
757 long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
758 size_t msgsz, long msgtyp, int msgflg)
760 struct msg_queue *msq;
761 struct msg_msg *msg;
762 int mode;
763 struct ipc_namespace *ns;
765 if (msqid < 0 || (long) msgsz < 0)
766 return -EINVAL;
767 mode = convert_mode(&msgtyp, msgflg);
768 ns = current->nsproxy->ipc_ns;
770 msq = msg_lock_check(ns, msqid);
771 if (IS_ERR(msq))
772 return PTR_ERR(msq);
774 for (;;) {
775 struct msg_receiver msr_d;
776 struct list_head *tmp;
778 msg = ERR_PTR(-EACCES);
779 if (ipcperms(ns, &msq->q_perm, S_IRUGO))
780 goto out_unlock;
782 msg = ERR_PTR(-EAGAIN);
783 tmp = msq->q_messages.next;
784 while (tmp != &msq->q_messages) {
785 struct msg_msg *walk_msg;
787 walk_msg = list_entry(tmp, struct msg_msg, m_list);
788 if (testmsg(walk_msg, msgtyp, mode) &&
789 !security_msg_queue_msgrcv(msq, walk_msg, current,
790 msgtyp, mode)) {
792 msg = walk_msg;
793 if (mode == SEARCH_LESSEQUAL &&
794 walk_msg->m_type != 1) {
795 msg = walk_msg;
796 msgtyp = walk_msg->m_type - 1;
797 } else {
798 msg = walk_msg;
799 break;
802 tmp = tmp->next;
804 if (!IS_ERR(msg)) {
806 * Found a suitable message.
807 * Unlink it from the queue.
809 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
810 msg = ERR_PTR(-E2BIG);
811 goto out_unlock;
813 list_del(&msg->m_list);
814 msq->q_qnum--;
815 msq->q_rtime = get_seconds();
816 msq->q_lrpid = task_tgid_vnr(current);
817 msq->q_cbytes -= msg->m_ts;
818 atomic_sub(msg->m_ts, &ns->msg_bytes);
819 atomic_dec(&ns->msg_hdrs);
820 ss_wakeup(&msq->q_senders, 0);
821 msg_unlock(msq);
822 break;
824 /* No message waiting. Wait for a message */
825 if (msgflg & IPC_NOWAIT) {
826 msg = ERR_PTR(-ENOMSG);
827 goto out_unlock;
829 list_add_tail(&msr_d.r_list, &msq->q_receivers);
830 msr_d.r_tsk = current;
831 msr_d.r_msgtype = msgtyp;
832 msr_d.r_mode = mode;
833 if (msgflg & MSG_NOERROR)
834 msr_d.r_maxsize = INT_MAX;
835 else
836 msr_d.r_maxsize = msgsz;
837 msr_d.r_msg = ERR_PTR(-EAGAIN);
838 current->state = TASK_INTERRUPTIBLE;
839 msg_unlock(msq);
841 schedule();
843 /* Lockless receive, part 1:
844 * Disable preemption. We don't hold a reference to the queue
845 * and getting a reference would defeat the idea of a lockless
846 * operation, thus the code relies on rcu to guarantee the
847 * existence of msq:
848 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
849 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
850 * rcu_read_lock() prevents preemption between reading r_msg
851 * and the spin_lock() inside ipc_lock_by_ptr().
853 rcu_read_lock();
855 /* Lockless receive, part 2:
856 * Wait until pipelined_send or expunge_all are outside of
857 * wake_up_process(). There is a race with exit(), see
858 * ipc/mqueue.c for the details.
860 msg = (struct msg_msg*)msr_d.r_msg;
861 while (msg == NULL) {
862 cpu_relax();
863 msg = (struct msg_msg *)msr_d.r_msg;
866 /* Lockless receive, part 3:
867 * If there is a message or an error then accept it without
868 * locking.
870 if (msg != ERR_PTR(-EAGAIN)) {
871 rcu_read_unlock();
872 break;
875 /* Lockless receive, part 3:
876 * Acquire the queue spinlock.
878 ipc_lock_by_ptr(&msq->q_perm);
879 rcu_read_unlock();
881 /* Lockless receive, part 4:
882 * Repeat test after acquiring the spinlock.
884 msg = (struct msg_msg*)msr_d.r_msg;
885 if (msg != ERR_PTR(-EAGAIN))
886 goto out_unlock;
888 list_del(&msr_d.r_list);
889 if (signal_pending(current)) {
890 msg = ERR_PTR(-ERESTARTNOHAND);
891 out_unlock:
892 msg_unlock(msq);
893 break;
896 if (IS_ERR(msg))
897 return PTR_ERR(msg);
899 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
900 *pmtype = msg->m_type;
901 if (store_msg(mtext, msg, msgsz))
902 msgsz = -EFAULT;
904 free_msg(msg);
906 return msgsz;
909 SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
910 long, msgtyp, int, msgflg)
912 long err, mtype;
914 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
915 if (err < 0)
916 goto out;
918 if (put_user(mtype, &msgp->mtype))
919 err = -EFAULT;
920 out:
921 return err;
924 #ifdef CONFIG_PROC_FS
925 static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
927 struct msg_queue *msq = it;
929 return seq_printf(s,
930 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
931 msq->q_perm.key,
932 msq->q_perm.id,
933 msq->q_perm.mode,
934 msq->q_cbytes,
935 msq->q_qnum,
936 msq->q_lspid,
937 msq->q_lrpid,
938 msq->q_perm.uid,
939 msq->q_perm.gid,
940 msq->q_perm.cuid,
941 msq->q_perm.cgid,
942 msq->q_stime,
943 msq->q_rtime,
944 msq->q_ctime);
946 #endif