linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Davidlohr Bueso <dave@stgolabs.net>
To: George Spelvin <linux@horizon.com>
Cc: mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de,
	bigeasy@linutronix.de, clm@fb.com, linux-kernel@vger.kernel.org,
	manfred@colorfullife.com, rostedt@goodmis.org,
	torvalds@linux-foundation.org
Subject: Re: [PATCH 3/3] ipc/mqueue: lockless pipelined wakeups
Date: Fri, 01 May 2015 17:35:56 -0700	[thread overview]
Message-ID: <1430526956.1940.8.camel@stgolabs.net> (raw)
In-Reply-To: <20150501215207.25731.qmail@ns.horizon.com>

On Fri, 2015-05-01 at 17:52 -0400, George Spelvin wrote:
> In general, Acked-by, but you're making me fix all your comments. :-)
> 
> This is a nice use of the wake queue, since the code was already handling
> the same problem in a similar way with STATE_PENDING.
> 
> >  * The receiver accepts the message and returns without grabbing the queue
> >+ * spinlock. The used algorithm is different from sysv semaphores (ipc/sem.c):
> 
> Is that last sentence even wanted?

Yeah, we can probably remove it now.

> >+ *
> >+ * - Set pointer to message.
> >+ * - Queue the receiver task's for later wakeup (without the info->lock).
> 
> It's "task" singular, and the apostrophe would be wrong if it were plural.
> 
> >+ * - Update its state to STATE_READY. Now the receiver can continue.
> >+ * - Wake up the process after the lock is dropped. Should the process wake up
> >+ *   before this wakeup (due to a timeout or a signal) it will either see
> >+ *   STATE_READY and continue or acquire the lock to check the sate again.
> 
> "check the sTate again".
> 
> >+	wake_q_add(wake_q, receiver->task);
> >+	/*
> >+	 * Rely on the implicit cmpxchg barrier from wake_q_add such
> >+	 * that we can ensure that updating receiver->state is the last
> >+	 * write operation: As once set, the receiver can continue,
> >+	 * and if we don't have the reference count from the wake_q,
> >+	 * yet, at that point we can later have a use-after-free
> >+	 * condition and bogus wakeup.
> >+	 */
> > 	receiver->state = STATE_READY;
> 
> How about:
> 	/*
> 	 * There must be a write barrier here; setting STATE_READY
> 	 * lets the receiver proceed without further synchronization.
> 	 * The cmpxchg inside wake_q_add serves as the barrier here.
> 	 */
> 
> The need for a wake queue to take a reference to avoid use-after-free
> is generic to wake queues, and handled in generic code; I don't see why
> it needs a comment here.

You are not wrong, but I'd rather leave the comment as is, as it will
vary from user to user. The comments in the sched wake_q bits are
already pretty clear, and if users cannot see the need for holding
reference and the task disappearing on their own they have no business
using wake_q. Furthermore, I think my comment serves better in mqueues
as the need for it isn't immediately obvious.

 
> >@@ -1084,6 +1094,7 @@ SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
> > 	ktime_t expires, *timeout = NULL;
> > 	struct timespec ts;
> > 	struct posix_msg_tree_node *new_leaf = NULL;
> >+	WAKE_Q(wake_q);
> > 
> > 	if (u_abs_timeout) {
> > 		int res = prepare_timeout(u_abs_timeout, &expires, &ts);
> >@@ -1155,8 +1166,9 @@ SYSCALL_DEFINE5(mq_timedreceive, mqd_t, mqdes, char __user *, u_msg_ptr,
> > 				CURRENT_TIME;
> > 
> > 		/* There is now free space in queue. */
> >-		pipelined_receive(info);
> >+		pipelined_receive(&wake_q, info);
> > 		spin_unlock(&info->lock);
> >+		wake_up_q(&wake_q);
> > 		ret = 0;
> > 	}
> > 	if (ret == 0) {
> 
> Since WAKE_Q actually involves some initialization, would it make sense to
> move its declaration to inside the condition that needs it?
> 
> (I'm also a fan of declaring variables in the smallest scope possible,
> just on general principles.)

Agreed.

Thanks,
Davidlohr



  reply	other threads:[~2015-05-02  0:36 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-01 15:27 [PATCH v2 0/3] kernel: lockless wake-queues Davidlohr Bueso
2015-05-01 15:27 ` [PATCH 1/3] sched: " Davidlohr Bueso
2015-05-08 13:23   ` [tip:sched/core] sched: Implement " tip-bot for Peter Zijlstra
2015-05-01 15:27 ` [PATCH 2/3] futex: lockless wakeups Davidlohr Bueso
2015-05-08 13:23   ` [tip:sched/core] futex: Implement " tip-bot for Davidlohr Bueso
2015-05-01 15:27 ` [PATCH 3/3] ipc/mqueue: lockless pipelined wakeups Davidlohr Bueso
2015-05-01 21:52   ` George Spelvin
2015-05-02  0:35     ` Davidlohr Bueso [this message]
2015-05-05  0:37       ` George Spelvin
2015-05-04 14:02   ` [PATCH v2 " Davidlohr Bueso
2015-05-08 13:24     ` [tip:sched/core] ipc/mqueue: Implement " tip-bot for Davidlohr Bueso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1430526956.1940.8.camel@stgolabs.net \
    --to=dave@stgolabs.net \
    --cc=bigeasy@linutronix.de \
    --cc=clm@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@horizon.com \
    --cc=manfred@colorfullife.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).