Commit 55cdbf47 authored by Manfred Spraul's avatar Manfred Spraul Committed by Linus Torvalds

[PATCH] cleanup of ipc/msg.c

Attached is a cleanup of the main loops in sys_msgrcv and sys_msgsnd, based on
ipc_lock_by_ptr().  Most backward gotos are gone, instead normal "for(;;)"
loops until a suitable message is found.

Description:

- General cleanup of sys_msgrcv and sys_msgsnd: the function were too
  convoluted.

- Enable lockless receive, update comments.

- Use ipc_getref for sys_msgsnd(), it's better than rechecking that the
  msqid is still valid.
Signed-Off-By: default avatarManfred Spraul <manfred@colorfullife.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 8dd459f2
...@@ -163,8 +163,10 @@ static void expunge_all(struct msg_queue* msq, int res) ...@@ -163,8 +163,10 @@ static void expunge_all(struct msg_queue* msq, int res)
msr = list_entry(tmp,struct msg_receiver,r_list); msr = list_entry(tmp,struct msg_receiver,r_list);
tmp = tmp->next; tmp = tmp->next;
msr->r_msg = ERR_PTR(res); msr->r_msg = NULL;
wake_up_process(msr->r_tsk); wake_up_process(msr->r_tsk);
smp_mb();
msr->r_msg = ERR_PTR(res);
} }
} }
/* /*
...@@ -525,13 +527,17 @@ static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg) ...@@ -525,13 +527,17 @@ static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg)
!security_msg_queue_msgrcv(msq, msg, msr->r_tsk, msr->r_msgtype, msr->r_mode)) { !security_msg_queue_msgrcv(msq, msg, msr->r_tsk, msr->r_msgtype, msr->r_mode)) {
list_del(&msr->r_list); list_del(&msr->r_list);
if(msr->r_maxsize < msg->m_ts) { if(msr->r_maxsize < msg->m_ts) {
msr->r_msg = ERR_PTR(-E2BIG); msr->r_msg = NULL;
wake_up_process(msr->r_tsk); wake_up_process(msr->r_tsk);
smp_mb();
msr->r_msg = ERR_PTR(-E2BIG);
} else { } else {
msr->r_msg = msg; msr->r_msg = NULL;
msq->q_lrpid = msr->r_tsk->pid; msq->q_lrpid = msr->r_tsk->pid;
msq->q_rtime = get_seconds(); msq->q_rtime = get_seconds();
wake_up_process(msr->r_tsk); wake_up_process(msr->r_tsk);
smp_mb();
msr->r_msg = msg;
return 1; return 1;
} }
} }
...@@ -564,43 +570,49 @@ asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -564,43 +570,49 @@ asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz,
err=-EINVAL; err=-EINVAL;
if(msq==NULL) if(msq==NULL)
goto out_free; goto out_free;
retry:
err= -EIDRM; err= -EIDRM;
if (msg_checkid(msq,msqid)) if (msg_checkid(msq,msqid))
goto out_unlock_free; goto out_unlock_free;
err=-EACCES; for (;;) {
if (ipcperms(&msq->q_perm, S_IWUGO)) struct msg_sender s;
goto out_unlock_free;
err = security_msg_queue_msgsnd(msq, msg, msgflg); err=-EACCES;
if (err) if (ipcperms(&msq->q_perm, S_IWUGO))
goto out_unlock_free; goto out_unlock_free;
if(msgsz + msq->q_cbytes > msq->q_qbytes || err = security_msg_queue_msgsnd(msq, msg, msgflg);
1 + msq->q_qnum > msq->q_qbytes) { if (err)
struct msg_sender s; goto out_unlock_free;
if(msgsz + msq->q_cbytes <= msq->q_qbytes &&
1 + msq->q_qnum <= msq->q_qbytes) {
break;
}
/* queue full, wait: */
if(msgflg&IPC_NOWAIT) { if(msgflg&IPC_NOWAIT) {
err=-EAGAIN; err=-EAGAIN;
goto out_unlock_free; goto out_unlock_free;
} }
ss_add(msq, &s); ss_add(msq, &s);
ipc_rcu_getref(msq);
msg_unlock(msq); msg_unlock(msq);
schedule(); schedule();
current->state= TASK_RUNNING;
msq = msg_lock(msqid); ipc_lock_by_ptr(&msq->q_perm);
err = -EIDRM; ipc_rcu_putref(msq);
if(msq==NULL) if (msq->q_perm.deleted) {
goto out_free; err = -EIDRM;
goto out_unlock_free;
}
ss_del(&s); ss_del(&s);
if (signal_pending(current)) { if (signal_pending(current)) {
err=-EINTR; err=-ERESTARTNOHAND;
goto out_unlock_free; goto out_unlock_free;
} }
goto retry;
} }
msq->q_lspid = current->tgid; msq->q_lspid = current->tgid;
...@@ -649,10 +661,7 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -649,10 +661,7 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
long msgtyp, int msgflg) long msgtyp, int msgflg)
{ {
struct msg_queue *msq; struct msg_queue *msq;
struct msg_receiver msr_d; struct msg_msg *msg;
struct list_head* tmp;
struct msg_msg* msg, *found_msg;
int err;
int mode; int mode;
if (msqid < 0 || (long) msgsz < 0) if (msqid < 0 || (long) msgsz < 0)
...@@ -662,62 +671,57 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -662,62 +671,57 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
msq = msg_lock(msqid); msq = msg_lock(msqid);
if(msq==NULL) if(msq==NULL)
return -EINVAL; return -EINVAL;
retry:
err = -EIDRM; msg = ERR_PTR(-EIDRM);
if (msg_checkid(msq,msqid)) if (msg_checkid(msq,msqid))
goto out_unlock; goto out_unlock;
err=-EACCES; for (;;) {
if (ipcperms (&msq->q_perm, S_IRUGO)) struct msg_receiver msr_d;
goto out_unlock; struct list_head* tmp;
tmp = msq->q_messages.next; msg = ERR_PTR(-EACCES);
found_msg=NULL; if (ipcperms (&msq->q_perm, S_IRUGO))
while (tmp != &msq->q_messages) {
msg = list_entry(tmp,struct msg_msg,m_list);
if(testmsg(msg,msgtyp,mode) &&
!security_msg_queue_msgrcv(msq, msg, current, msgtyp, mode)) {
found_msg = msg;
if(mode == SEARCH_LESSEQUAL && msg->m_type != 1) {
found_msg=msg;
msgtyp=msg->m_type-1;
} else {
found_msg=msg;
break;
}
}
tmp = tmp->next;
}
if(found_msg) {
msg=found_msg;
if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
err=-E2BIG;
goto out_unlock; goto out_unlock;
msg = ERR_PTR(-EAGAIN);
tmp = msq->q_messages.next;
while (tmp != &msq->q_messages) {
struct msg_msg *walk_msg;
walk_msg = list_entry(tmp,struct msg_msg,m_list);
if(testmsg(walk_msg,msgtyp,mode) &&
!security_msg_queue_msgrcv(msq, walk_msg, current, msgtyp, mode)) {
msg = walk_msg;
if(mode == SEARCH_LESSEQUAL && walk_msg->m_type != 1) {
msg=walk_msg;
msgtyp=walk_msg->m_type-1;
} else {
msg=walk_msg;
break;
}
}
tmp = tmp->next;
} }
list_del(&msg->m_list); if(!IS_ERR(msg)) {
msq->q_qnum--; /* Found a suitable message. Unlink it from the queue. */
msq->q_rtime = get_seconds(); if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
msq->q_lrpid = current->tgid; msg = ERR_PTR(-E2BIG);
msq->q_cbytes -= msg->m_ts; goto out_unlock;
atomic_sub(msg->m_ts,&msg_bytes); }
atomic_dec(&msg_hdrs); list_del(&msg->m_list);
ss_wakeup(&msq->q_senders,0); msq->q_qnum--;
msg_unlock(msq); msq->q_rtime = get_seconds();
out_success: msq->q_lrpid = current->tgid;
msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz; msq->q_cbytes -= msg->m_ts;
if (put_user (msg->m_type, &msgp->mtype) || atomic_sub(msg->m_ts,&msg_bytes);
store_msg(msgp->mtext, msg, msgsz)) { atomic_dec(&msg_hdrs);
msgsz = -EFAULT; ss_wakeup(&msq->q_senders,0);
msg_unlock(msq);
break;
} }
free_msg(msg); /* No message waiting. Wait for a message */
return msgsz;
} else
{
/* no message waiting. Prepare for pipelined
* receive.
*/
if (msgflg & IPC_NOWAIT) { if (msgflg & IPC_NOWAIT) {
err=-ENOMSG; msg = ERR_PTR(-ENOMSG);
goto out_unlock; goto out_unlock;
} }
list_add_tail(&msr_d.r_list,&msq->q_receivers); list_add_tail(&msr_d.r_list,&msq->q_receivers);
...@@ -727,52 +731,76 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz, ...@@ -727,52 +731,76 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
if(msgflg & MSG_NOERROR) if(msgflg & MSG_NOERROR)
msr_d.r_maxsize = INT_MAX; msr_d.r_maxsize = INT_MAX;
else else
msr_d.r_maxsize = msgsz; msr_d.r_maxsize = msgsz;
msr_d.r_msg = ERR_PTR(-EAGAIN); msr_d.r_msg = ERR_PTR(-EAGAIN);
current->state = TASK_INTERRUPTIBLE; current->state = TASK_INTERRUPTIBLE;
msg_unlock(msq); msg_unlock(msq);
schedule(); schedule();
/* /* Lockless receive, part 1:
* The below optimisation is buggy. A sleeping thread that is * Disable preemption. We don't hold a reference to the queue
* woken up checks if it got a message and if so, copies it to * and getting a reference would defeat the idea of a lockless
* userspace and just returns without taking any locks. * operation, thus the code relies on rcu to guarantee the
* But this return to user space can be faster than the message * existance of msq:
* send, and if the receiver immediately exits the * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
* wake_up_process performed by the sender will oops. * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
* rcu_read_lock() prevents preemption between reading r_msg
* and the spin_lock() inside ipc_lock_by_ptr().
*/
rcu_read_lock();
/* Lockless receive, part 2:
* Wait until pipelined_send or expunge_all are outside of
* wake_up_process(). There is a race with exit(), see
* ipc/mqueue.c for the details.
*/ */
#if 0
msg = (struct msg_msg*) msr_d.r_msg; msg = (struct msg_msg*) msr_d.r_msg;
if(!IS_ERR(msg)) while (msg == NULL) {
goto out_success; cpu_relax();
#endif msg = (struct msg_msg*) msr_d.r_msg;
}
msq = msg_lock(msqid); /* Lockless receive, part 3:
msg = (struct msg_msg*)msr_d.r_msg; * If there is a message or an error then accept it without
if(!IS_ERR(msg)) { * locking.
/* our message arived while we waited for */
* the spinlock. Process it. if(msg != ERR_PTR(-EAGAIN)) {
*/ rcu_read_unlock();
if(msq) break;
msg_unlock(msq);
goto out_success;
} }
err = PTR_ERR(msg);
if(err == -EAGAIN) { /* Lockless receive, part 3:
if(!msq) * Acquire the queue spinlock.
BUG(); */
list_del(&msr_d.r_list); ipc_lock_by_ptr(&msq->q_perm);
if (signal_pending(current)) rcu_read_unlock();
err=-EINTR;
else /* Lockless receive, part 4:
goto retry; * Repeat test after acquiring the spinlock.
*/
msg = (struct msg_msg*)msr_d.r_msg;
if(msg != ERR_PTR(-EAGAIN))
goto out_unlock;
list_del(&msr_d.r_list);
if (signal_pending(current)) {
msg = ERR_PTR(-ERESTARTNOHAND);
out_unlock:
msg_unlock(msq);
break;
} }
} }
out_unlock: if (IS_ERR(msg))
if(msq) return PTR_ERR(msg);
msg_unlock(msq);
return err; msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
if (put_user (msg->m_type, &msgp->mtype) ||
store_msg(msgp->mtext, msg, msgsz)) {
msgsz = -EFAULT;
}
free_msg(msg);
return msgsz;
} }
#ifdef CONFIG_PROC_FS #ifdef CONFIG_PROC_FS
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment