Skip to contentSkip to conversation

net: fix race in the receive/select

PATCH Superseded Jiri Olsa 10 participants 39 messages Jul 3, 2009 – Jul 8, 2009

Subsystems (10)
Networking Network drivers x86 TCP +6 more subsystems

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

v5 cover net: fix race in the receive/select

This patchset fixies the race within the poll_wait call and the receive callbacks, by adding memory barrier.

- 1/2 includes the actual fix
- 2/2 includes optimization for the x86 arch

It is discussed and described in the following discussions:

http://lkml.org/lkml/2009/6/18/124
http://lkml.org/lkml/2009/6/25/188
http://lkml.org/lkml/2009/6/29/216

Patchset is made on top of Linus'es tree 7c5371c403abb29f01bc6cff6c5096abdf2dc524 .

Booted on x86_64.

wbr,
jirka

---
 arch/x86/include/asm/spinlock.h |    3 ++
 include/linux/spinlock.h        |    5 +++
 include/net/sock.h              |   69 +++++++++++++++++++++++++++++++++++++++
 net/atm/common.c                |    6 ++--
 net/core/datagram.c             |    2 +-
 net/core/sock.c                 |    8 ++--
 net/dccp/output.c               |    2 +-
 net/dccp/proto.c                |    2 +-
 net/ipv4/tcp.c                  |    2 +-
 net/iucv/af_iucv.c              |    4 +-
 net/rxrpc/af_rxrpc.c            |    4 +-
 net/unix/af_unix.c              |    8 ++--
 12 files changed, 96 insertions(+), 19 deletions(-)
author
#1 lore raw

PATCH v5 1/2 net: adding memory barrier to the poll and receive callbacks

Adding memory barrier after the poll_wait function, paired with receive callbacks. Adding fuctions sk_poll_wait and sk_has_sleeper to wrap the memory barrier.

Without the memory barrier, following race can happen. The race fires, when following code paths meet, and the tp->rcv_nxt and __add_wait_queue updates stay in CPU caches.

CPU1 CPU2

sys_select                   receive packet
  ...                        ...
  __add_wait_queue           update tp->rcv_nxt
  ...                        ...
  tp->rcv_nxt check          sock_def_readable
  ...                        {
  schedule                      ...
                                if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
                                        wake_up_interruptible(sk->sk_sleep)
                                ...
                             }

If there was no cache the code would work ok, since the wait_queue and rcv_nxt are opposit to each other.

Meaning that once tp->rcv_nxt is updated by CPU2, the CPU1 either already passed the tp->rcv_nxt check and sleeps, or will get the new value for tp->rcv_nxt and will return with new data mask.
In both cases the process (CPU1) is being added to the wait queue, so the waitqueue_active (CPU2) call cannot miss and will wake up CPU1.

The bad case is when the __add_wait_queue changes done by CPU1 stay in its cache, and so does the tp->rcv_nxt update on CPU2 side. The CPU1 will then endup calling schedule and sleep forever if there are no more data on the socket.

Calls to poll_wait in following modules were ommited:

	net/bluetooth/af_bluetooth.c
	net/irda/af_irda.c
	net/irda/irnet/irnet_ppp.c
	net/mac80211/rc80211_pid_debugfs.c
	net/phonet/socket.c
	net/rds/af_rds.c
	net/rfkill/core.c
	net/sunrpc/cache.c
	net/sunrpc/rpc_pipe.c
	net/tipc/socket.c

wbr,
jirka

---
 include/net/sock.h   |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++
 net/atm/common.c     |    6 ++--
 net/core/datagram.c  |    2 +-
 net/core/sock.c      |    8 +++---
 net/dccp/output.c    |    2 +-
 net/dccp/proto.c     |    2 +-
 net/ipv4/tcp.c       |    2 +-
 net/iucv/af_iucv.c   |    4 +-
 net/rxrpc/af_rxrpc.c |    4 +-
 net/unix/af_unix.c   |    8 +++---
 10 files changed, 85 insertions(+), 19 deletions(-)
include/net/sock.h +66 -0
index 352f06b..4eb8409 100644
@@ -54,6 +54,7 @@
 
 #include <linux/filter.h>
 #include <linux/rculist_nulls.h>
+#include <linux/poll.h>
 
 #include <asm/atomic.h>
 #include <net/dst.h>
@@ -1241,6 +1242,71 @@ static inline int sk_has_allocations(const struct sock *sk)
 	return sk_wmem_alloc_get(sk) || sk_rmem_alloc_get(sk);
 }
 
+/**
+ * sk_has_sleeper - check if there are any waiting processes
+ * @sk: socket
+ *
+ * Returns true if socket has waiting processes
+ *
+ * The purpose of the sk_has_sleeper and sock_poll_wait is to wrap the memory
+ * barrier call. They were added due to the race found within the tcp code.
+ *
+ * Consider following tcp code paths:
+ *
+ * CPU1                  CPU2
+ *
+ * sys_select            receive packet
+ *   ...                 ...
+ *   __add_wait_queue    update tp->rcv_nxt
+ *   ...                 ...
+ *   tp->rcv_nxt check   sock_def_readable
+ *   ...                 {
+ *   schedule               ...
+ *                          if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+ *                              wake_up_interruptible(sk->sk_sleep)
+ *                          ...
+ *                       }
+ *
+ * The race for tcp fires when the __add_wait_queue changes done by CPU1 stay
+ * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
+ * could then endup calling schedule and sleep forever if there are no more
+ * data on the socket.
+ */
+static inline int sk_has_sleeper(struct sock *sk)
+{
+	/*
+	 * We need to be sure we are in sync with the
+	 * add_wait_queue modifications to the wait queue.
+	 *
+	 * This memory barrier is paired in the sock_poll_wait.
+	 */
+	smp_mb();
+	return sk->sk_sleep && waitqueue_active(sk->sk_sleep);
+}
+
+/**
+ * sock_poll_wait - place memory barrier behind the poll_wait call.
+ * @filp:           file
+ * @wait_address:   socket wait queue
+ * @p:              poll_table
+ *
+ * See the comments in the sk_has_sleeper function.
+ */
+static inline void sock_poll_wait(struct file *filp,
+		wait_queue_head_t *wait_address, poll_table *p)
+{
+	if (p && wait_address) {
+		poll_wait(filp, wait_address, p);
+		/*
+		 * We need to be sure we are in sync with the
+		 * socket flags modification.
+		 *
+		 * This memory barrier is paired in the sk_has_sleeper.
+		*/
+		smp_mb();
+	}
+}
+
 /*
  * 	Queue a received datagram if it will fit. Stream and sequenced
  *	protocols can't normally use this as they need to fit buffers in
net/atm/common.c +3 -3
index c1c9793..8c4d843 100644
@@ -92,7 +92,7 @@ static void vcc_sock_destruct(struct sock *sk)
 static void vcc_def_wakeup(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up(sk->sk_sleep);
 	read_unlock(&sk->sk_callback_lock);
 }
@@ -110,7 +110,7 @@ static void vcc_write_space(struct sock *sk)
 	read_lock(&sk->sk_callback_lock);
 
 	if (vcc_writable(sk)) {
-		if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+		if (sk_has_sleeper(sk))
 			wake_up_interruptible(sk->sk_sleep);
 
 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
@@ -594,7 +594,7 @@ unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
 	struct atm_vcc *vcc;
 	unsigned int mask;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	mask = 0;
 
 	vcc = ATM_SD(sock);
net/core/datagram.c +1 -1
index 58abee1..b0fe692 100644
@@ -712,7 +712,7 @@ unsigned int datagram_poll(struct file *file, struct socket *sock,
 	struct sock *sk = sock->sk;
 	unsigned int mask;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	mask = 0;
 
 	/* exceptional events? */
net/core/sock.c +4 -4
index b0ba569..6354863 100644
@@ -1715,7 +1715,7 @@ EXPORT_SYMBOL(sock_no_sendpage);
 static void sock_def_wakeup(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up_interruptible_all(sk->sk_sleep);
 	read_unlock(&sk->sk_callback_lock);
 }
@@ -1723,7 +1723,7 @@ static void sock_def_wakeup(struct sock *sk)
 static void sock_def_error_report(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up_interruptible_poll(sk->sk_sleep, POLLERR);
 	sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
 	read_unlock(&sk->sk_callback_lock);
@@ -1732,7 +1732,7 @@ static void sock_def_error_report(struct sock *sk)
 static void sock_def_readable(struct sock *sk, int len)
 {
 	read_lock(&sk->sk_callback_lock);
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up_interruptible_sync_poll(sk->sk_sleep, POLLIN |
 						POLLRDNORM | POLLRDBAND);
 	sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
@@ -1747,7 +1747,7 @@ static void sock_def_write_space(struct sock *sk)
 	 * progress.  --DaveM
 	 */
 	if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
-		if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+		if (sk_has_sleeper(sk))
 			wake_up_interruptible_sync_poll(sk->sk_sleep, POLLOUT |
 						POLLWRNORM | POLLWRBAND);
 
net/dccp/output.c +1 -1
index c0e88c1..c96119f 100644
@@ -196,7 +196,7 @@ void dccp_write_space(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
 
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up_interruptible(sk->sk_sleep);
 	/* Should agree with poll, otherwise some programs break */
 	if (sock_writeable(sk))
net/dccp/proto.c +1 -1
index 314a1b5..94ca8ea 100644
@@ -311,7 +311,7 @@ unsigned int dccp_poll(struct file *file, struct socket *sock,
 	unsigned int mask;
 	struct sock *sk = sock->sk;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	if (sk->sk_state == DCCP_LISTEN)
 		return inet_csk_listen_poll(sk);
 
net/ipv4/tcp.c +1 -1
index 7870a53..9114524 100644
@@ -339,7 +339,7 @@ unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
 	struct sock *sk = sock->sk;
 	struct tcp_sock *tp = tcp_sk(sk);
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	if (sk->sk_state == TCP_LISTEN)
 		return inet_csk_listen_poll(sk);
 
net/iucv/af_iucv.c +2 -2
index 6be5f92..49c15b4 100644
@@ -306,7 +306,7 @@ static inline int iucv_below_msglim(struct sock *sk)
 static void iucv_sock_wake_msglim(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up_interruptible_all(sk->sk_sleep);
 	sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
 	read_unlock(&sk->sk_callback_lock);
@@ -1256,7 +1256,7 @@ unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
 	struct sock *sk = sock->sk;
 	unsigned int mask = 0;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 
 	if (sk->sk_state == IUCV_LISTEN)
 		return iucv_accept_poll(sk);
net/rxrpc/af_rxrpc.c +2 -2
index eac5e7b..bfe493e 100644
@@ -63,7 +63,7 @@ static void rxrpc_write_space(struct sock *sk)
 	_enter("%p", sk);
 	read_lock(&sk->sk_callback_lock);
 	if (rxrpc_writable(sk)) {
-		if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+		if (sk_has_sleeper(sk))
 			wake_up_interruptible(sk->sk_sleep);
 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
 	}
@@ -588,7 +588,7 @@ static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
 	unsigned int mask;
 	struct sock *sk = sock->sk;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	mask = 0;
 
 	/* the socket is readable if there are any messages waiting on the Rx
net/unix/af_unix.c +4 -4
index 36d4e44..fc3ebb9 100644
@@ -315,7 +315,7 @@ static void unix_write_space(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
 	if (unix_writable(sk)) {
-		if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+		if (sk_has_sleeper(sk))
 			wake_up_interruptible_sync(sk->sk_sleep);
 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
 	}
@@ -1985,7 +1985,7 @@ static unsigned int unix_poll(struct file *file, struct socket *sock, poll_table
 	struct sock *sk = sock->sk;
 	unsigned int mask;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	mask = 0;
 
 	/* exceptional events? */
@@ -2022,7 +2022,7 @@ static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
 	struct sock *sk = sock->sk, *other;
 	unsigned int mask, writable;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	mask = 0;
 
 	/* exceptional events? */
@@ -2053,7 +2053,7 @@ static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
 		other = unix_peer_get(sk);
 		if (other) {
 			if (unix_peer(other) != sk) {
-				poll_wait(file, &unix_sk(other)->peer_wait,
+				sock_poll_wait(file, &unix_sk(other)->peer_wait,
 					  wait);
 				if (unix_recvq_full(other))
 					writable = 0;
#2 lore raw

Re: [PATCHv5 1/2] net: adding memory barrier to the poll and receive callbacks

Quoted 50 lines · Jiri Olsa

Adding memory barrier after the poll_wait function, paired with receive callbacks. Adding fuctions sk_poll_wait and sk_has_sleeper to wrap the memory barrier.

Without the memory barrier, following race can happen. The race fires, when following code paths meet, and the tp->rcv_nxt and __add_wait_queue updates stay in CPU caches.

CPU1 CPU2

sys_select                   receive packet
  ...                        ...
  __add_wait_queue           update tp->rcv_nxt
  ...                        ...
  tp->rcv_nxt check          sock_def_readable
  ...                        {
  schedule                      ...
                                if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
                                        wake_up_interruptible(sk->sk_sleep)
                                ...
                             }

If there was no cache the code would work ok, since the wait_queue and rcv_nxt are opposit to each other.

Meaning that once tp->rcv_nxt is updated by CPU2, the CPU1 either already passed the tp->rcv_nxt check and sleeps, or will get the new value for tp->rcv_nxt and will return with new data mask.
In both cases the process (CPU1) is being added to the wait queue, so the waitqueue_active (CPU2) call cannot miss and will wake up CPU1.

The bad case is when the __add_wait_queue changes done by CPU1 stay in its cache, and so does the tp->rcv_nxt update on CPU2 side. The CPU1 will then endup calling schedule and sleep forever if there are no more data on the socket.

Calls to poll_wait in following modules were ommited:

	net/bluetooth/af_bluetooth.c
	net/irda/af_irda.c
	net/irda/irnet/irnet_ppp.c
	net/mac80211/rc80211_pid_debugfs.c
	net/phonet/socket.c
	net/rds/af_rds.c
	net/rfkill/core.c
	net/sunrpc/cache.c
	net/sunrpc/rpc_pipe.c
	net/tipc/socket.c
Quoted 5 lines

wbr,
jirka

Quoted 305 lines
---
 include/net/sock.h   |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++
 net/atm/common.c     |    6 ++--
 net/core/datagram.c  |    2 +-
 net/core/sock.c      |    8 +++---
 net/dccp/output.c    |    2 +-
 net/dccp/proto.c     |    2 +-
 net/ipv4/tcp.c       |    2 +-
 net/iucv/af_iucv.c   |    4 +-
 net/rxrpc/af_rxrpc.c |    4 +-
 net/unix/af_unix.c   |    8 +++---
 10 files changed, 85 insertions(+), 19 deletions(-)
include/net/sock.h +66 -0
index 352f06b..4eb8409 100644
@@ -54,6 +54,7 @@
 
 #include <linux/filter.h>
 #include <linux/rculist_nulls.h>
+#include <linux/poll.h>
 
 #include <asm/atomic.h>
 #include <net/dst.h>
@@ -1241,6 +1242,71 @@ static inline int sk_has_allocations(const struct sock *sk)
 	return sk_wmem_alloc_get(sk) || sk_rmem_alloc_get(sk);
 }
 
+/**
+ * sk_has_sleeper - check if there are any waiting processes
+ * @sk: socket
+ *
+ * Returns true if socket has waiting processes
+ *
+ * The purpose of the sk_has_sleeper and sock_poll_wait is to wrap the memory
+ * barrier call. They were added due to the race found within the tcp code.
+ *
+ * Consider following tcp code paths:
+ *
+ * CPU1                  CPU2
+ *
+ * sys_select            receive packet
+ *   ...                 ...
+ *   __add_wait_queue    update tp->rcv_nxt
+ *   ...                 ...
+ *   tp->rcv_nxt check   sock_def_readable
+ *   ...                 {
+ *   schedule               ...
+ *                          if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+ *                              wake_up_interruptible(sk->sk_sleep)
+ *                          ...
+ *                       }
+ *
+ * The race for tcp fires when the __add_wait_queue changes done by CPU1 stay
+ * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
+ * could then endup calling schedule and sleep forever if there are no more
+ * data on the socket.
+ */
+static inline int sk_has_sleeper(struct sock *sk)
+{
+	/*
+	 * We need to be sure we are in sync with the
+	 * add_wait_queue modifications to the wait queue.
+	 *
+	 * This memory barrier is paired in the sock_poll_wait.
+	 */
+	smp_mb();
+	return sk->sk_sleep && waitqueue_active(sk->sk_sleep);
+}
+
+/**
+ * sock_poll_wait - place memory barrier behind the poll_wait call.
+ * @filp:           file
+ * @wait_address:   socket wait queue
+ * @p:              poll_table
+ *
+ * See the comments in the sk_has_sleeper function.
+ */
+static inline void sock_poll_wait(struct file *filp,
+		wait_queue_head_t *wait_address, poll_table *p)
+{
+	if (p && wait_address) {
+		poll_wait(filp, wait_address, p);
+		/*
+		 * We need to be sure we are in sync with the
+		 * socket flags modification.
+		 *
+		 * This memory barrier is paired in the sk_has_sleeper.
+		*/
+		smp_mb();
+	}
+}
+
 /*
  * 	Queue a received datagram if it will fit. Stream and sequenced
  *	protocols can't normally use this as they need to fit buffers in
net/atm/common.c +3 -3
index c1c9793..8c4d843 100644
@@ -92,7 +92,7 @@ static void vcc_sock_destruct(struct sock *sk)
 static void vcc_def_wakeup(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up(sk->sk_sleep);
 	read_unlock(&sk->sk_callback_lock);
 }
@@ -110,7 +110,7 @@ static void vcc_write_space(struct sock *sk)
 	read_lock(&sk->sk_callback_lock);
 
 	if (vcc_writable(sk)) {
-		if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+		if (sk_has_sleeper(sk))
 			wake_up_interruptible(sk->sk_sleep);
 
 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
@@ -594,7 +594,7 @@ unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
 	struct atm_vcc *vcc;
 	unsigned int mask;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	mask = 0;
 
 	vcc = ATM_SD(sock);
net/core/datagram.c +1 -1
index 58abee1..b0fe692 100644
@@ -712,7 +712,7 @@ unsigned int datagram_poll(struct file *file, struct socket *sock,
 	struct sock *sk = sock->sk;
 	unsigned int mask;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	mask = 0;
 
 	/* exceptional events? */
net/core/sock.c +4 -4
index b0ba569..6354863 100644
@@ -1715,7 +1715,7 @@ EXPORT_SYMBOL(sock_no_sendpage);
 static void sock_def_wakeup(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up_interruptible_all(sk->sk_sleep);
 	read_unlock(&sk->sk_callback_lock);
 }
@@ -1723,7 +1723,7 @@ static void sock_def_wakeup(struct sock *sk)
 static void sock_def_error_report(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up_interruptible_poll(sk->sk_sleep, POLLERR);
 	sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
 	read_unlock(&sk->sk_callback_lock);
@@ -1732,7 +1732,7 @@ static void sock_def_error_report(struct sock *sk)
 static void sock_def_readable(struct sock *sk, int len)
 {
 	read_lock(&sk->sk_callback_lock);
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up_interruptible_sync_poll(sk->sk_sleep, POLLIN |
 						POLLRDNORM | POLLRDBAND);
 	sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
@@ -1747,7 +1747,7 @@ static void sock_def_write_space(struct sock *sk)
 	 * progress.  --DaveM
 	 */
 	if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
-		if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+		if (sk_has_sleeper(sk))
 			wake_up_interruptible_sync_poll(sk->sk_sleep, POLLOUT |
 						POLLWRNORM | POLLWRBAND);
 
net/dccp/output.c +1 -1
index c0e88c1..c96119f 100644
@@ -196,7 +196,7 @@ void dccp_write_space(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
 
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up_interruptible(sk->sk_sleep);
 	/* Should agree with poll, otherwise some programs break */
 	if (sock_writeable(sk))
net/dccp/proto.c +1 -1
index 314a1b5..94ca8ea 100644
@@ -311,7 +311,7 @@ unsigned int dccp_poll(struct file *file, struct socket *sock,
 	unsigned int mask;
 	struct sock *sk = sock->sk;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	if (sk->sk_state == DCCP_LISTEN)
 		return inet_csk_listen_poll(sk);
 
net/ipv4/tcp.c +1 -1
index 7870a53..9114524 100644
@@ -339,7 +339,7 @@ unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
 	struct sock *sk = sock->sk;
 	struct tcp_sock *tp = tcp_sk(sk);
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	if (sk->sk_state == TCP_LISTEN)
 		return inet_csk_listen_poll(sk);
 
net/iucv/af_iucv.c +2 -2
index 6be5f92..49c15b4 100644
@@ -306,7 +306,7 @@ static inline int iucv_below_msglim(struct sock *sk)
 static void iucv_sock_wake_msglim(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
-	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+	if (sk_has_sleeper(sk))
 		wake_up_interruptible_all(sk->sk_sleep);
 	sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
 	read_unlock(&sk->sk_callback_lock);
@@ -1256,7 +1256,7 @@ unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
 	struct sock *sk = sock->sk;
 	unsigned int mask = 0;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 
 	if (sk->sk_state == IUCV_LISTEN)
 		return iucv_accept_poll(sk);
net/rxrpc/af_rxrpc.c +2 -2
index eac5e7b..bfe493e 100644
@@ -63,7 +63,7 @@ static void rxrpc_write_space(struct sock *sk)
 	_enter("%p", sk);
 	read_lock(&sk->sk_callback_lock);
 	if (rxrpc_writable(sk)) {
-		if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+		if (sk_has_sleeper(sk))
 			wake_up_interruptible(sk->sk_sleep);
 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
 	}
@@ -588,7 +588,7 @@ static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
 	unsigned int mask;
 	struct sock *sk = sock->sk;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	mask = 0;
 
 	/* the socket is readable if there are any messages waiting on the Rx
net/unix/af_unix.c +4 -4
index 36d4e44..fc3ebb9 100644
@@ -315,7 +315,7 @@ static void unix_write_space(struct sock *sk)
 {
 	read_lock(&sk->sk_callback_lock);
 	if (unix_writable(sk)) {
-		if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+		if (sk_has_sleeper(sk))
 			wake_up_interruptible_sync(sk->sk_sleep);
 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
 	}
@@ -1985,7 +1985,7 @@ static unsigned int unix_poll(struct file *file, struct socket *sock, poll_table
 	struct sock *sk = sock->sk;
 	unsigned int mask;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	mask = 0;
 
 	/* exceptional events? */
@@ -2022,7 +2022,7 @@ static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
 	struct sock *sk = sock->sk, *other;
 	unsigned int mask, writable;
 
-	poll_wait(file, sk->sk_sleep, wait);
+	sock_poll_wait(file, sk->sk_sleep, wait);
 	mask = 0;
 
 	/* exceptional events? */
@@ -2053,7 +2053,7 @@ static unsigned int unix_dgram_poll(struct file *file, struct socket *sock,
 		other = unix_peer_get(sk);
 		if (other) {
 			if (unix_peer(other) != sk) {
-				poll_wait(file, &unix_sk(other)->peer_wait,
+				sock_poll_wait(file, &unix_sk(other)->peer_wait,
 					  wait);
 				if (unix_recvq_full(other))
 					writable = 0;
author
#3 lore raw

PATCH v5 2/2 memory barrier: adding smp_mb__after_lock

In reply to Jiri Olsa

Adding smp_mb__after_lock define to be used as a smp_mb call after a lock.

Making it nop for x86, since {read|write|spin}_lock() on x86 are full memory barriers.

wbr,
jirka

---
 arch/x86/include/asm/spinlock.h |    3 +++
 include/linux/spinlock.h        |    5 +++++
 include/net/sock.h              |    5 ++++-
 3 files changed, 12 insertions(+), 1 deletions(-)
arch/x86/include/asm/spinlock.h +3 -0
index b7e5db8..39ecc5f 100644
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)
+
 #endif /* _ASM_X86_SPINLOCK_H */
include/linux/spinlock.h +5 -0
index 252b245..ae053bd 100644
@@ -132,6 +132,11 @@ do {								\
 #endif /*__raw_spin_is_contended*/
 #endif
 
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif
+
 /**
  * spin_unlock_wait - wait until the spinlock gets unlocked
  * @lock: the spinlock in question.
include/net/sock.h +4 -1
index 4eb8409..98afcd9 100644
@@ -1271,6 +1271,9 @@ static inline int sk_has_allocations(const struct sock *sk)
  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
  * could then endup calling schedule and sleep forever if there are no more
  * data on the socket.
+ *
+ * The sk_has_helper is always called right after a call to read_lock, so we
+ * can use smp_mb__after_lock barrier.
  */
 static inline int sk_has_sleeper(struct sock *sk)
 {
@@ -1280,7 +1283,7 @@ static inline int sk_has_sleeper(struct sock *sk)
 	 *
 	 * This memory barrier is paired in the sock_poll_wait.
 	 */
-	smp_mb();
+	smp_mb__after_lock();
 	return sk->sk_sleep && waitqueue_active(sk->sk_sleep);
 }
 
#4 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

#5 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 17 lines · Ingo Molnar

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

Quoted 8 lines

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

This was following existing implementations of various smp_mb__??? helpers :

# grep -4 smp_mb__before_clear_bit include/asm-generic/bitops.h

/*
* clear_bit may not imply a memory barrier
*/
#ifndef smp_mb__before_clear_bit
#define smp_mb__before_clear_bit() smp_mb()
#define smp_mb__after_clear_bit() smp_mb()
#endif

#6 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 31 lines · Eric Dumazet
Quoted 17 lines · Ingo Molnar

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

Quoted 8 lines

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

This was following existing implementations of various smp_mb__??? helpers :

# grep -4 smp_mb__before_clear_bit include/asm-generic/bitops.h
Quoted 7 lines

/*
* clear_bit may not imply a memory barrier
*/
#ifndef smp_mb__before_clear_bit
#define smp_mb__before_clear_bit() smp_mb()
#define smp_mb__after_clear_bit() smp_mb()
#endif

Did i mention that those should be fixed too? :-)

Ingo

author
#7 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 42 lines · Ingo Molnar
Quoted 31 lines · Eric Dumazet
Quoted 17 lines · Ingo Molnar

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

Quoted 8 lines

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

This was following existing implementations of various smp_mb__??? helpers :

# grep -4 smp_mb__before_clear_bit include/asm-generic/bitops.h
Quoted 7 lines

/*
* clear_bit may not imply a memory barrier
*/
#ifndef smp_mb__before_clear_bit
#define smp_mb__before_clear_bit() smp_mb()
#define smp_mb__after_clear_bit() smp_mb()
#endif

Quoted 3 lines

Did i mention that those should be fixed too? :-)

Ingo

ok, could I include it in the 2/2 or you prefer separate patch?

jirka

#8 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 43 lines · Jiri Olsa
Quoted 42 lines · Ingo Molnar
Quoted 38 lines · Eric Dumazet
Quoted 17 lines · Ingo Molnar

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

Quoted 8 lines

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

This was following existing implementations of various smp_mb__??? helpers :

# grep -4 smp_mb__before_clear_bit include/asm-generic/bitops.h

/*
* clear_bit may not imply a memory barrier
*/
#ifndef smp_mb__before_clear_bit
#define smp_mb__before_clear_bit() smp_mb()
#define smp_mb__after_clear_bit() smp_mb()
#endif

Quoted 5 lines
Quoted 3 lines

Did i mention that those should be fixed too? :-)

Ingo

ok, could I include it in the 2/2 or you prefer separate patch?

depends on whether it will regress ;-)

If it regresses, it's better to have it separate. If it wont, it can be included. If unsure, default to the more conservative option.

Ingo

author
#9 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 50 lines · Ingo Molnar
Quoted 47 lines · Jiri Olsa
Quoted 45 lines · Ingo Molnar
Quoted 31 lines · Eric Dumazet
Quoted 17 lines · Ingo Molnar

Review static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

(quoted diff) +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

Quoted 8 lines

Review static inline void __raw_write_unlock(raw_rwlock_t *rw)

Quoted 4 lines
(quoted diff) +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

This was following existing implementations of various smp_mb__??? helpers :

# grep -4 smp_mb__before_clear_bit include/asm-generic/bitops.h
Quoted 7 lines

/*
* clear_bit may not imply a memory barrier
*/
#ifndef smp_mb__before_clear_bit
#define smp_mb__before_clear_bit() smp_mb()
#define smp_mb__after_clear_bit() smp_mb()
#endif

Did i mention that those should be fixed too? :-)

Ingo

Quoted 8 lines
Quoted 1 line

ok, could I include it in the 2/2 or you prefer separate patch?

depends on whether it will regress ;-)

If it regresses, it's better to have it separate. If it wont, it can be included. If unsure, default to the more conservative option.

Ingo

how about this..
and similar change for smp_mb__before_clear_bit in a separate patch

arch/x86/include/asm/spinlock.h +4 -0
index b7e5db8..4e77853 100644
@@ -302,4 +302,8 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+static inline void smp_mb__after_lock(void) { }
+#define ARCH_HAS_SMP_MB_AFTER_LOCK
+
 #endif /* _ASM_X86_SPINLOCK_H */
include/linux/spinlock.h +5 -0
index 252b245..4be57ab 100644
@@ -132,6 +132,11 @@ do {								\
 #endif /*__raw_spin_is_contended*/
 #endif
 
+/* The lock does not imply full memory barrier. */
+#ifndef ARCH_HAS_SMP_MB_AFTER_LOCK
+static inline void smp_mb__after_lock(void) { smp_mb(); }
+#endif
+
 /**
  * spin_unlock_wait - wait until the spinlock gets unlocked
  * @lock: the spinlock in question.
include/net/sock.h +4 -1
index 4eb8409..98afcd9 100644
@@ -1271,6 +1271,9 @@ static inline int sk_has_allocations(const struct sock *sk)
  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
  * could then endup calling schedule and sleep forever if there are no more
  * data on the socket.
+ *
+ * The sk_has_helper is always called right after a call to read_lock, so we
+ * can use smp_mb__after_lock barrier.
  */
 static inline int sk_has_sleeper(struct sock *sk)
 {
@@ -1280,7 +1283,7 @@ static inline int sk_has_sleeper(struct sock *sk)
 	 *
 	 * This memory barrier is paired in the sock_poll_wait.
 	 */
-	smp_mb();
+	smp_mb__after_lock();
 	return sk->sk_sleep && waitqueue_active(sk->sk_sleep);
 }
 
#10 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

On Fri, Jul 03, 2009 at 01:18:48PM +0200, Jiri Olsa wrote:
...

Quoted 4 lines
arch/x86/include/asm/spinlock.h +0 -0
index b7e5db8..4e77853 100644

...

arch/x86/include/asm/spinlock.h static inline int sk_has_allocations(const struct sock *sk) · line 1275

Quoted 6 lines
arch/x86/include/asm/spinlock.h +2 -0
@@ -1271,6 +1271,9 @@ static inline int sk_has_allocations(const struct sock *sk)
  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
  * could then endup calling schedule and sleep forever if there are no more
  * data on the socket.
+ *
+ * The sk_has_helper is always called right after a call to read_lock, so we

Btw.:
- * The sk_has_helper is always called right after a call to read_lock, so we
+ * The sk_has_sleeper is always called right after a call to read_lock, so we

Jarek P.

author
#11 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 17 lines · Jarek Poplawski

On Fri, Jul 03, 2009 at 01:18:48PM +0200, Jiri Olsa wrote:
...

Quoted 4 lines
arch/x86/include/asm/spinlock.h +0 -0
index b7e5db8..4e77853 100644

...

arch/x86/include/asm/spinlock.h static inline int sk_has_allocations(const struct sock *sk) · line 1275

Quoted 6 lines
arch/x86/include/asm/spinlock.h +2 -0
@@ -1271,6 +1271,9 @@ static inline int sk_has_allocations(const struct sock *sk)
  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
  * could then endup calling schedule and sleep forever if there are no more
  * data on the socket.
+ *
+ * The sk_has_helper is always called right after a call to read_lock, so we

Btw.:
- * The sk_has_helper is always called right after a call to read_lock, so we
+ * The sk_has_sleeper is always called right after a call to read_lock, so we

Quoted 1 line

Jarek P.

oops, thanks

author
#12 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

In reply to Jiri Olsa
Quoted 109 lines · Jiri Olsa
Quoted 50 lines · Ingo Molnar
Quoted 47 lines · Jiri Olsa
Quoted 45 lines · Ingo Molnar
Quoted 31 lines · Eric Dumazet
Quoted 17 lines · Ingo Molnar

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

Quoted 8 lines

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

This was following existing implementations of various smp_mb__??? helpers :

# grep -4 smp_mb__before_clear_bit include/asm-generic/bitops.h
Quoted 7 lines

/*
* clear_bit may not imply a memory barrier
*/
#ifndef smp_mb__before_clear_bit
#define smp_mb__before_clear_bit() smp_mb()
#define smp_mb__after_clear_bit() smp_mb()
#endif

Did i mention that those should be fixed too? :-)

Ingo

Quoted 8 lines
Quoted 1 line

ok, could I include it in the 2/2 or you prefer separate patch?

depends on whether it will regress ;-)

If it regresses, it's better to have it separate. If it wont, it can be included. If unsure, default to the more conservative option.

Ingo

how about this..
and similar change for smp_mb__before_clear_bit in a separate patch

arch/x86/include/asm/spinlock.h +4 -0
index b7e5db8..4e77853 100644
@@ -302,4 +302,8 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+static inline void smp_mb__after_lock(void) { }
+#define ARCH_HAS_SMP_MB_AFTER_LOCK
+
 #endif /* _ASM_X86_SPINLOCK_H */
include/linux/spinlock.h +5 -0
index 252b245..4be57ab 100644
@@ -132,6 +132,11 @@ do {								\
 #endif /*__raw_spin_is_contended*/
 #endif
 
+/* The lock does not imply full memory barrier. */
+#ifndef ARCH_HAS_SMP_MB_AFTER_LOCK
+static inline void smp_mb__after_lock(void) { smp_mb(); }
+#endif
+
 /**
  * spin_unlock_wait - wait until the spinlock gets unlocked
  * @lock: the spinlock in question.
include/net/sock.h +3 -0
index 4eb8409..98afcd9 100644
@@ -1271,6 +1271,9 @@ static inline int sk_has_allocations(const struct sock *sk)
  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
  * could then endup calling schedule and sleep forever if there are no more
  * data on the socket.
+ *
+ * The sk_has_helper is always called right after a call to read_lock, so we
+ * can use smp_mb__after_lock barrier.
  */
 static inline int sk_has_sleeper(struct sock *sk)
 {

include/net/sock.h static inline int sk_has_sleeper(struct sock *sk) · line 1289

Quoted 8 lines
include/net/sock.h +1 -1
@@ -1280,7 +1283,7 @@ static inline int sk_has_sleeper(struct sock *sk)
 	 *
 	 * This memory barrier is paired in the sock_poll_wait.
 	 */
-	smp_mb();
+	smp_mb__after_lock();
 	return sk->sk_sleep && waitqueue_active(sk->sk_sleep);
 }
 

any feedback on this?
I'd send v6 if this way is acceptable..

thanks,
jirka

author
#13 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 122 lines · Jiri Olsa
Quoted 109 lines · Jiri Olsa
Quoted 50 lines · Ingo Molnar
Quoted 47 lines · Jiri Olsa
Quoted 45 lines · Ingo Molnar
Quoted 31 lines · Eric Dumazet
Quoted 17 lines · Ingo Molnar

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

Quoted 8 lines

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

This was following existing implementations of various smp_mb__??? helpers :

# grep -4 smp_mb__before_clear_bit include/asm-generic/bitops.h
Quoted 7 lines

/*
* clear_bit may not imply a memory barrier
*/
#ifndef smp_mb__before_clear_bit
#define smp_mb__before_clear_bit() smp_mb()
#define smp_mb__after_clear_bit() smp_mb()
#endif

Did i mention that those should be fixed too? :-)

Ingo

Quoted 8 lines
Quoted 1 line

ok, could I include it in the 2/2 or you prefer separate patch?

depends on whether it will regress ;-)

If it regresses, it's better to have it separate. If it wont, it can be included. If unsure, default to the more conservative option.

Ingo

how about this..
and similar change for smp_mb__before_clear_bit in a separate patch

arch/x86/include/asm/spinlock.h +4 -0
index b7e5db8..4e77853 100644
@@ -302,4 +302,8 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+static inline void smp_mb__after_lock(void) { }
+#define ARCH_HAS_SMP_MB_AFTER_LOCK
+
 #endif /* _ASM_X86_SPINLOCK_H */
include/linux/spinlock.h +5 -0
index 252b245..4be57ab 100644
@@ -132,6 +132,11 @@ do {								\
 #endif /*__raw_spin_is_contended*/
 #endif
 
+/* The lock does not imply full memory barrier. */
+#ifndef ARCH_HAS_SMP_MB_AFTER_LOCK
+static inline void smp_mb__after_lock(void) { smp_mb(); }
+#endif
+
 /**
  * spin_unlock_wait - wait until the spinlock gets unlocked
  * @lock: the spinlock in question.
include/net/sock.h +3 -0
index 4eb8409..98afcd9 100644
@@ -1271,6 +1271,9 @@ static inline int sk_has_allocations(const struct sock *sk)
  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
  * could then endup calling schedule and sleep forever if there are no more
  * data on the socket.
+ *
+ * The sk_has_helper is always called right after a call to read_lock, so we
+ * can use smp_mb__after_lock barrier.
  */
 static inline int sk_has_sleeper(struct sock *sk)
 {

include/net/sock.h static inline int sk_has_sleeper(struct sock *sk) · line 1289

Quoted 8 lines
include/net/sock.h +1 -1
@@ -1280,7 +1283,7 @@ static inline int sk_has_sleeper(struct sock *sk)
 	 *
 	 * This memory barrier is paired in the sock_poll_wait.
 	 */
-	smp_mb();
+	smp_mb__after_lock();
 	return sk->sk_sleep && waitqueue_active(sk->sk_sleep);
 }
 

any feedback on this?
I'd send v6 if this way is acceptable..

Quoted 2 lines

thanks,
jirka

also I checked the smp_mb__before_clear_bit/smp_mb__after_clear_bit and it is used quite extensivelly.

I'd prefer to send it in a separate patch, so we can move on with the changes I've sent so far..

regards,
jirka

#14 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 123 lines · * Jiri Olsa ([email protected]) wrote:
Quoted 122 lines · Jiri Olsa
Quoted 109 lines · Jiri Olsa
Quoted 50 lines · Ingo Molnar
Quoted 47 lines · Jiri Olsa
Quoted 45 lines · Ingo Molnar
Quoted 31 lines · Eric Dumazet
Quoted 17 lines · Ingo Molnar

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

Quoted 8 lines

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

This was following existing implementations of various smp_mb__??? helpers :

# grep -4 smp_mb__before_clear_bit include/asm-generic/bitops.h
Quoted 7 lines

/*
* clear_bit may not imply a memory barrier
*/
#ifndef smp_mb__before_clear_bit
#define smp_mb__before_clear_bit() smp_mb()
#define smp_mb__after_clear_bit() smp_mb()
#endif

Did i mention that those should be fixed too? :-)

Ingo

Quoted 8 lines
Quoted 1 line

ok, could I include it in the 2/2 or you prefer separate patch?

depends on whether it will regress ;-)

If it regresses, it's better to have it separate. If it wont, it can be included. If unsure, default to the more conservative option.

Ingo

how about this..
and similar change for smp_mb__before_clear_bit in a separate patch

arch/x86/include/asm/spinlock.h +4 -0
index b7e5db8..4e77853 100644
@@ -302,4 +302,8 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+static inline void smp_mb__after_lock(void) { }
+#define ARCH_HAS_SMP_MB_AFTER_LOCK
+
 #endif /* _ASM_X86_SPINLOCK_H */
include/linux/spinlock.h +5 -0
index 252b245..4be57ab 100644
@@ -132,6 +132,11 @@ do {								\
 #endif /*__raw_spin_is_contended*/
 #endif
 
+/* The lock does not imply full memory barrier. */
+#ifndef ARCH_HAS_SMP_MB_AFTER_LOCK
+static inline void smp_mb__after_lock(void) { smp_mb(); }
+#endif
+
 /**
  * spin_unlock_wait - wait until the spinlock gets unlocked
  * @lock: the spinlock in question.
include/net/sock.h +3 -0
index 4eb8409..98afcd9 100644
@@ -1271,6 +1271,9 @@ static inline int sk_has_allocations(const struct sock *sk)
  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
  * could then endup calling schedule and sleep forever if there are no more
  * data on the socket.
+ *
+ * The sk_has_helper is always called right after a call to read_lock, so we
+ * can use smp_mb__after_lock barrier.
  */
 static inline int sk_has_sleeper(struct sock *sk)
 {

include/net/sock.h static inline int sk_has_sleeper(struct sock *sk) · line 1289

Quoted 8 lines
include/net/sock.h +1 -1
@@ -1280,7 +1283,7 @@ static inline int sk_has_sleeper(struct sock *sk)
 	 *
 	 * This memory barrier is paired in the sock_poll_wait.
 	 */
-	smp_mb();
+	smp_mb__after_lock();
 	return sk->sk_sleep && waitqueue_active(sk->sk_sleep);
 }
 

any feedback on this?
I'd send v6 if this way is acceptable..

Quoted 9 lines
Quoted 2 lines

thanks,
jirka

also I checked the smp_mb__before_clear_bit/smp_mb__after_clear_bit and it is used quite extensivelly.

I'd prefer to send it in a separate patch, so we can move on with the changes I've sent so far..

As with any optimization (and this is one that adds a semantic that will just grow the memory barrier/locking rule complexity), it should come with performance benchmarks showing real-life improvements.

Otherwise I'd recommend sticking to smp_mb() if this execution path is not that critical, or to move to RCU if it's _that_ critical.

A valid argument would be if the data structures protected are so complex that RCU is out of question but still the few cycles saved by removing a memory barrier are really significant. And even then, the proper solution would be more something like a __read_lock()+smp_mb+smp_mb+__read_unlock(), so we get the performance improvements on architectures other than x86 as well.

So in all cases, I don't think the smp_mb__after_lock() is the appropriate solution.

Mathieu

Quoted 2 lines

regards,
jirka

Signature · 2 lines
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
author
#15 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 148 lines · Mathieu Desnoyers
Quoted 123 lines · * Jiri Olsa ([email protected]) wrote:
Quoted 122 lines · Jiri Olsa
Quoted 109 lines · Jiri Olsa
Quoted 50 lines · Ingo Molnar
Quoted 47 lines · Jiri Olsa
Quoted 45 lines · Ingo Molnar
Quoted 31 lines · Eric Dumazet
Quoted 17 lines · Ingo Molnar

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

Quoted 8 lines

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

This was following existing implementations of various smp_mb__??? helpers :

# grep -4 smp_mb__before_clear_bit include/asm-generic/bitops.h
Quoted 7 lines

/*
* clear_bit may not imply a memory barrier
*/
#ifndef smp_mb__before_clear_bit
#define smp_mb__before_clear_bit() smp_mb()
#define smp_mb__after_clear_bit() smp_mb()
#endif

Did i mention that those should be fixed too? :-)

Ingo

Quoted 8 lines
Quoted 1 line

ok, could I include it in the 2/2 or you prefer separate patch?

depends on whether it will regress ;-)

If it regresses, it's better to have it separate. If it wont, it can be included. If unsure, default to the more conservative option.

Ingo

how about this..
and similar change for smp_mb__before_clear_bit in a separate patch

arch/x86/include/asm/spinlock.h +4 -0
index b7e5db8..4e77853 100644
@@ -302,4 +302,8 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+static inline void smp_mb__after_lock(void) { }
+#define ARCH_HAS_SMP_MB_AFTER_LOCK
+
 #endif /* _ASM_X86_SPINLOCK_H */
include/linux/spinlock.h +5 -0
index 252b245..4be57ab 100644
@@ -132,6 +132,11 @@ do {								\
 #endif /*__raw_spin_is_contended*/
 #endif
 
+/* The lock does not imply full memory barrier. */
+#ifndef ARCH_HAS_SMP_MB_AFTER_LOCK
+static inline void smp_mb__after_lock(void) { smp_mb(); }
+#endif
+
 /**
  * spin_unlock_wait - wait until the spinlock gets unlocked
  * @lock: the spinlock in question.
include/net/sock.h +3 -0
index 4eb8409..98afcd9 100644
@@ -1271,6 +1271,9 @@ static inline int sk_has_allocations(const struct sock *sk)
  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
  * could then endup calling schedule and sleep forever if there are no more
  * data on the socket.
+ *
+ * The sk_has_helper is always called right after a call to read_lock, so we
+ * can use smp_mb__after_lock barrier.
  */
 static inline int sk_has_sleeper(struct sock *sk)
 {

include/net/sock.h static inline int sk_has_sleeper(struct sock *sk) · line 1289

Quoted 8 lines
include/net/sock.h +1 -1
@@ -1280,7 +1283,7 @@ static inline int sk_has_sleeper(struct sock *sk)
 	 *
 	 * This memory barrier is paired in the sock_poll_wait.
 	 */
-	smp_mb();
+	smp_mb__after_lock();
 	return sk->sk_sleep && waitqueue_active(sk->sk_sleep);
 }
 

any feedback on this?
I'd send v6 if this way is acceptable..

Quoted 9 lines
Quoted 2 lines

thanks,
jirka

also I checked the smp_mb__before_clear_bit/smp_mb__after_clear_bit and it is used quite extensivelly.

I'd prefer to send it in a separate patch, so we can move on with the changes I've sent so far..

As with any optimization (and this is one that adds a semantic that will just grow the memory barrier/locking rule complexity), it should come with performance benchmarks showing real-life improvements.

Otherwise I'd recommend sticking to smp_mb() if this execution path is not that critical, or to move to RCU if it's _that_ critical.

A valid argument would be if the data structures protected are so complex that RCU is out of question but still the few cycles saved by removing a memory barrier are really significant. And even then, the proper solution would be more something like a __read_lock()+smp_mb+smp_mb+__read_unlock(), so we get the performance improvements on architectures other than x86 as well.

Quoted 2 lines

So in all cases, I don't think the smp_mb__after_lock() is the appropriate solution.

well, I'm not that familiar with RCU, but I dont mind to omit the smp_mb__after_lock change as long as the first one (1/2) stays :)

how about others, any ideas?

thanks,
jirka

Quoted 9 lines

Mathieu

Quoted 2 lines

regards,
jirka

Signature · 2 lines
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
#16 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

In reply to Mathieu Desnoyers
Quoted 4 lines · Mathieu Desnoyers

As with any optimization (and this is one that adds a semantic that will just grow the memory barrier/locking rule complexity), it should come with performance benchmarks showing real-life improvements.

Well, the same applies to smp_mb__xxx_atomic_yyy or smp_mb__before_clear_bit.

Imho the new helper is not worse, and it could be also used by try_to_wake_up(), __pollwake(), insert_work() at least.

Quoted 6 lines

Otherwise I'd recommend sticking to smp_mb() if this execution path is not that critical, or to move to RCU if it's _that_ critical.

A valid argument would be if the data structures protected are so complex that RCU is out of question but still the few cycles saved by removing a memory barrier are really significant.

Not sure I understand how RCU can help,

Quoted 4 lines

And even then, the
proper solution would be more something like a __read_lock()+smp_mb+smp_mb+__read_unlock(), so we get the performance improvements on architectures other than x86 as well.

Hmm. could you explain what you mean?

Oleg.

#17 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 10 lines · * Oleg Nesterov ([email protected]) wrote:
Quoted 4 lines · Mathieu Desnoyers

As with any optimization (and this is one that adds a semantic that will just grow the memory barrier/locking rule complexity), it should come with performance benchmarks showing real-life improvements.

Well, the same applies to smp_mb__xxx_atomic_yyy or smp_mb__before_clear_bit.

Imho the new helper is not worse, and it could be also used by try_to_wake_up(), __pollwake(), insert_work() at least.

It's basically related to Amdahl law. If the smp_mb is a small portion of the overall read_lock cost, then it may not be worth it to remove it. At the contrary, if the mb is a big portion of set/clear bit, then it's worth it. We also have to consider the frequency at which these operations are done to figure out the overall performance impact. Also, locks imply cache-line bouncing, which are typically costly. clear/set bit does not imply this as much. So the tradeoffs are very different there.

So it's not as simple as "we do this for set/clear bit, we should therefore do this for locks".

Quoted 10 lines
Quoted 7 lines

Otherwise I'd recommend sticking to smp_mb() if this execution path is not that critical, or to move to RCU if it's _that_ critical.

A valid argument would be if the data structures protected are so complex that RCU is out of question but still the few cycles saved by removing a memory barrier are really significant.

Not sure I understand how RCU can help,

Changing a read_lock to a rcu_read_lock would save the whole atomic cache-line bouncing operation on that fast path. But it may imply data structure redesign. So it is more for future development than current kernel releases.

Quoted 7 lines
Quoted 4 lines

And even then, the
proper solution would be more something like a __read_lock()+smp_mb+smp_mb+__read_unlock(), so we get the performance improvements on architectures other than x86 as well.

Hmm. could you explain what you mean?

Actually, thinking about it more, to appropriately support x86, as well as powerpc, arm and mips, we would need something like:

read_lock_smp_mb()

Which would be a read_lock with an included memory barrier.

Mathieu

Quoted 2 lines

Oleg.

Signature · 2 lines
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
#18 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 7 lines · Mathieu Desnoyers

Actually, thinking about it more, to appropriately support x86, as well as powerpc, arm and mips, we would need something like:

read_lock_smp_mb()

Which would be a read_lock with an included memory barrier.

Then we need read_lock_irq_smp_mb, read_lock_irqsave__smp_mb, write_lock_xxx, otherwise it is not clear why only read_lock() has _smp_mb() version.

The same for spin_lock_xxx...

Oleg.

#19 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 5 lines · Oleg Nesterov
Quoted 4 lines · Mathieu Desnoyers

Actually, thinking about it more, to appropriately support x86, as well as powerpc, arm and mips, we would need something like:

Quoted 8 lines
Quoted 3 lines

read_lock_smp_mb()

Which would be a read_lock with an included memory barrier.

Then we need read_lock_irq_smp_mb, read_lock_irqsave__smp_mb, write_lock_xxx, otherwise it is not clear why only read_lock() has _smp_mb() version.

The same for spin_lock_xxx...

At which time the smp_mb__{before,after}_{un,}lock become attractive again.

#20 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 10 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 9 lines · Oleg Nesterov
Quoted 7 lines · Mathieu Desnoyers

Actually, thinking about it more, to appropriately support x86, as well as powerpc, arm and mips, we would need something like:

read_lock_smp_mb()

Which would be a read_lock with an included memory barrier.

Quoted 8 lines
Quoted 4 lines

Then we need read_lock_irq_smp_mb, read_lock_irqsave__smp_mb, write_lock_xxx, otherwise it is not clear why only read_lock() has _smp_mb() version.

The same for spin_lock_xxx...

At which time the smp_mb__{before,after}_{un,}lock become attractive again.

Then having a new __read_lock() (without acquire semantic) which would be required to be followed by a smp__mb_after_lock() would make sense. I think this would fit all of x86, powerpc, arm, mips without having to create tons of new primitives. Only "simpler" ones that clearly separate locking from memory barriers.

Mathieu

Signature · 2 lines
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
#21 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 17 lines · Mathieu Desnoyers
Quoted 7 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 6 lines · Oleg Nesterov
Quoted 5 lines · Mathieu Desnoyers

Actually, thinking about it more, to appropriately support x86, as well as powerpc, arm and mips, we would need something like:

read_lock_smp_mb()

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Which would be a read_lock with an included memory barrier.

Then we need read_lock_irq_smp_mb, read_lock_irqsave__smp_mb, write_lock_xxx, otherwise it is not clear why only read_lock() has _smp_mb() version.

The same for spin_lock_xxx...

At which time the smp_mb__{before,after}_{un,}lock become attractive again.

Quoted 6 lines

Then having a new __read_lock() (without acquire semantic) which would be required to be followed by a smp__mb_after_lock() would make sense. I think this would fit all of x86, powerpc, arm, mips without having to create tons of new primitives. Only "simpler" ones that clearly separate locking from memory barriers.

Hmm... On x86, read_lock() is :

	lock subl $0x1,(%eax)
	jns   .Lok
	call	__read_lock_failed

.Lok: ret

What would be __read_lock() ? I cant see how it could *not* use lock prefix actually and or being cheaper...

#22 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 27 lines · * Eric Dumazet ([email protected]) wrote:
Quoted 17 lines · Mathieu Desnoyers
Quoted 7 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 6 lines · Oleg Nesterov
Quoted 5 lines · Mathieu Desnoyers

Actually, thinking about it more, to appropriately support x86, as well as powerpc, arm and mips, we would need something like:

read_lock_smp_mb()

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Which would be a read_lock with an included memory barrier.

Then we need read_lock_irq_smp_mb, read_lock_irqsave__smp_mb, write_lock_xxx, otherwise it is not clear why only read_lock() has _smp_mb() version.

The same for spin_lock_xxx...

At which time the smp_mb__{before,after}_{un,}lock become attractive again.

Quoted 6 lines

Then having a new __read_lock() (without acquire semantic) which would be required to be followed by a smp__mb_after_lock() would make sense. I think this would fit all of x86, powerpc, arm, mips without having to create tons of new primitives. Only "simpler" ones that clearly separate locking from memory barriers.

Hmm... On x86, read_lock() is :

Quoted 9 lines
	lock subl $0x1,(%eax)
	jns   .Lok
	call	__read_lock_failed

.Lok: ret

What would be __read_lock() ? I cant see how it could *not* use lock prefix actually and or being cheaper...

(I'll use read_lock_noacquire() instead of __read_lock() because __read_lock() is already used for low-level primitives and will produce name clashes. But I recognise that noacquire is just an ugly name.)

Here, a __read_lock_noacquire _must_ be followed by a smp__mb_after_lock(), and a __read_unlock_norelease() _must_ be preceded by a smp__mb_before_unlock().

x86 :

#define __read_lock_noacquire read_lock
/* Assumes all __*_lock_noacquire primitives act as a full smp_mb() */ #define smp__mb_after_lock()

/* Assumes all __*_unlock_norelease primitives act as a full smp_mb() */ #define smp__mb_before_unlock()
#define __read_unlock_norelease read_unlock

it's that easy :-)

however, on powerpc, we have to stop and think about it a bit more:

quoting http://www.linuxjournal.com/article/8212

"lwsync, or lightweight sync, orders loads with respect to subsequent loads and stores, and it also orders stores. However, it does not order stores with respect to subsequent loads. Interestingly enough, the lwsync instruction enforces the same ordering as does the zSeries and, coincidentally, the SPARC TSO."

static inline long __read_trylock_noacquire(raw_rwlock_t *rw)

{
        long tmp;

        __asm__ __volatile__(
"1:     lwarx           %0,0,%1\n"
        __DO_SIGN_EXTEND
"       addic.          %0,%0,1\n\
        ble-            2f\n"
        PPC405_ERR77(0,%1)
"       stwcx.          %0,0,%1\n\
        bne-            1b\n\
        /* isync\n\ Removed the isync because following smp_mb (sync
         * instruction) includes a core synchronizing barrier. */
2:"     : "=&r" (tmp)
        : "r" (&rw->lock)
        : "cr0", "xer", "memory");

        return tmp;
}

#define smp__mb_after_lock() smp_mb()

#define smp__mb_before_unlock() smp_mb()

static inline void __raw_read_unlock_norelease(raw_rwlock_t *rw)

{
        long tmp;

        __asm__ __volatile__(
        "# read_unlock\n\t"
        /* LWSYNC_ON_SMP -------- can be removed, replace by prior
         * smp_mb() */
"1:     lwarx           %0,0,%1\n\
        addic           %0,%0,-1\n"
        PPC405_ERR77(0,%1)
"       stwcx.          %0,0,%1\n\
        bne-            1b"
        : "=&r"(tmp)
        : "r"(&rw->lock)
        : "cr0", "xer", "memory");
}

I assume here that lwarx/stwcx pairs for different addresses cannot be reordered with other pairs. If they can, then we already have a problem with the current powerpc read lock implementation.

I just wrote this as an example to show how this could become a performance improvement on architectures different than x86. The code proposed above comes without warranty and should be tested with care. :)

Mathieu

Signature · 2 lines
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
#23 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 7 lines · Mathieu Desnoyers
Quoted 4 lines · * Eric Dumazet ([email protected]) wrote:

What would be __read_lock() ? I cant see how it could *not* use lock prefix actually and or being cheaper...

Quoted 7 lines

(I'll use read_lock_noacquire() instead of __read_lock() because __read_lock() is already used for low-level primitives and will produce name clashes. But I recognise that noacquire is just an ugly name.)

Here, a __read_lock_noacquire _must_ be followed by a smp__mb_after_lock(), and a __read_unlock_norelease() _must_ be preceded by a smp__mb_before_unlock().

Your point was, smp_mb__after_lock() adds more complexity to the barriers/locking rules.

Do you really think __read_lock_noacquire() makes this all more simple/understandable? And again, we need __read_lock_irq_noaquire/etc.

Personally, I disagree. In fact, I do not understand when/why _noacquire can be used, but this is another story.

Let's look from the different angle. The first patch from Jiri fixes the bug. Yes, it is not clear if this is possible to trigger this bug in practice, but still nobody disagrees the bug does exist. The second patch fixes the added pessimization.

So, if you do not agree with these patches, perhaps you can send fixes on top of these changes?

Sadly, I already removed the previous emails so I can't add my acked-by to Jiri's patches. I didn't do this before because I thought I am in no position to ack these changes. But looking at this discussion, I'd like to vote for both these patches anyway ;)

Oleg.

#24 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 16 lines · * Oleg Nesterov ([email protected]) wrote:
Quoted 7 lines · Mathieu Desnoyers
Quoted 4 lines · * Eric Dumazet ([email protected]) wrote:

What would be __read_lock() ? I cant see how it could *not* use lock prefix actually and or being cheaper...

Quoted 7 lines

(I'll use read_lock_noacquire() instead of __read_lock() because __read_lock() is already used for low-level primitives and will produce name clashes. But I recognise that noacquire is just an ugly name.)

Here, a __read_lock_noacquire _must_ be followed by a smp__mb_after_lock(), and a __read_unlock_norelease() _must_ be preceded by a smp__mb_before_unlock().

Quoted 6 lines

Your point was, smp_mb__after_lock() adds more complexity to the barriers/locking rules.

Do you really think __read_lock_noacquire() makes this all more simple/understandable? And again, we need __read_lock_irq_noaquire/etc.

Yep, agreed that it also sounds like added complexity in locking rules, and I've not yet seen the benefit of it.

Quoted 3 lines

Personally, I disagree. In fact, I do not understand when/why _noacquire can be used, but this is another story.

Because adding smp_mb__after_lock() is _only_ useful on x86. Most other architectures _will_ suffer from a performance degradation, unless you implement the __read_lock_noacquire.

Quoted 4 lines

Let's look from the different angle. The first patch from Jiri fixes the bug. Yes, it is not clear if this is possible to trigger this bug in practice, but still nobody disagrees the bug does exist. The second patch fixes the added pessimization.

I fully agree with the bugfix.

Quoted 3 lines

So, if you do not agree with these patches, perhaps you can send fixes on top of these changes?

Given we can later build around the smp__mb_after_lock() to eliminate the performance deterioration on non-x86 architectures by adding a __read_lock_noacquire() primitive, I guess this can be done in a later phase as an optimization.

I do not care if performance are not perfect for all architectures at this point. What I really care about is that we do not introduce new locking, atomic ops or memory barrier semantics that only make sense for a single architecture and limit others.

Given that we can eventually move to a __read_lock_noacquire()/smp_mb__after_lock() scheme, then adding just smp_mb__after_lock() in the first place does not seem like a bad move. It will just degrade performance of non-x86 architectures until __read_lock_noacquire() or something similar comes.

So it looks fine if the code path is critical enough to justify adding such new memory barrier. As long as we don't end up having smp_mb__after_ponies().

Cheers,

Mathieu

Quoted 11 lines

Sadly, I already removed the previous emails so I can't add my acked-by to Jiri's patches. I didn't do this before because I thought I am in no position to ack these changes. But looking at this discussion, I'd like to vote for both these patches anyway ;)

Oleg.

Signature · 2 lines
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
#25 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

On Wed, Jul 08, 2009 at 12:34:32AM -0400, Mathieu Desnoyers wrote:
...

Quoted 3 lines

Because adding smp_mb__after_lock() is _only_ useful on x86. Most other architectures _will_ suffer from a performance degradation, unless you implement the __read_lock_noacquire.

It's completely backwards: processor barriers are just expected to add a performance degradation. That's like:

x86 developer:
OK, we need to add a barrier here: even x86 might need this.

Alpha developer:
Right, than we need this even more.

x86 developer:
But wait, we can avoid it using a dummy after some locks, because they have such a barrier already.

Alpha developer:
Then it's not OK: it's _only_ useful on x86; our architecture _will_ suffer from a performance degradation...

Cheers,
Jarek P.

#26 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

In reply to Mathieu Desnoyers
Quoted 137 lines · Mathieu Desnoyers
Quoted 113 lines · * Jiri Olsa ([email protected]) wrote:
Quoted 112 lines · Jiri Olsa
Quoted 100 lines · Jiri Olsa
Quoted 42 lines · Ingo Molnar
Quoted 40 lines · Jiri Olsa
Quoted 39 lines · Ingo Molnar
Quoted 28 lines · Eric Dumazet
Quoted 16 lines · Ingo Molnar

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

Quoted 7 lines

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

This was following existing implementations of various smp_mb__??? helpers :

# grep -4 smp_mb__before_clear_bit include/asm-generic/bitops.h
Quoted 7 lines

/*
* clear_bit may not imply a memory barrier
*/
#ifndef smp_mb__before_clear_bit
#define smp_mb__before_clear_bit() smp_mb()
#define smp_mb__after_clear_bit() smp_mb()
#endif

Did i mention that those should be fixed too? :-)

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

Ingo

ok, could I include it in the 2/2 or you prefer separate patch?

depends on whether it will regress ;-)

If it regresses, it's better to have it separate. If it wont, it can be included. If unsure, default to the more conservative option.

Ingo

how about this..
and similar change for smp_mb__before_clear_bit in a separate patch

arch/x86/include/asm/spinlock.h +4 -0
index b7e5db8..4e77853 100644
@@ -302,4 +302,8 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+static inline void smp_mb__after_lock(void) { }
+#define ARCH_HAS_SMP_MB_AFTER_LOCK
+
 #endif /* _ASM_X86_SPINLOCK_H */
include/linux/spinlock.h +5 -0
index 252b245..4be57ab 100644
@@ -132,6 +132,11 @@ do {								\
 #endif /*__raw_spin_is_contended*/
 #endif
 
+/* The lock does not imply full memory barrier. */
+#ifndef ARCH_HAS_SMP_MB_AFTER_LOCK
+static inline void smp_mb__after_lock(void) { smp_mb(); }
+#endif
+
 /**
  * spin_unlock_wait - wait until the spinlock gets unlocked
  * @lock: the spinlock in question.
include/net/sock.h +3 -0
index 4eb8409..98afcd9 100644
@@ -1271,6 +1271,9 @@ static inline int sk_has_allocations(const struct sock *sk)
  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
  * could then endup calling schedule and sleep forever if there are no more
  * data on the socket.
+ *
+ * The sk_has_helper is always called right after a call to read_lock, so we
+ * can use smp_mb__after_lock barrier.
  */
 static inline int sk_has_sleeper(struct sock *sk)
 {

include/net/sock.h static inline int sk_has_sleeper(struct sock *sk) · line 1289

Quoted 8 lines
include/net/sock.h +1 -1
@@ -1280,7 +1283,7 @@ static inline int sk_has_sleeper(struct sock *sk)
 	 *
 	 * This memory barrier is paired in the sock_poll_wait.
 	 */
-	smp_mb();
+	smp_mb__after_lock();
 	return sk->sk_sleep && waitqueue_active(sk->sk_sleep);
 }
 

any feedback on this?
I'd send v6 if this way is acceptable..

Quoted 8 lines
Quoted 2 lines

thanks,
jirka

also I checked the smp_mb__before_clear_bit/smp_mb__after_clear_bit and it is used quite extensivelly.

I'd prefer to send it in a separate patch, so we can move on with the changes I've sent so far..

As with any optimization (and this is one that adds a semantic that will just grow the memory barrier/locking rule complexity), it should come with performance benchmarks showing real-life improvements.

Otherwise I'd recommend sticking to smp_mb() if this execution path is not that critical, or to move to RCU if it's _that_ critical.

A valid argument would be if the data structures protected are so complex that RCU is out of question but still the few cycles saved by removing a memory barrier are really significant. And even then, the proper solution would be more something like a __read_lock()+smp_mb+smp_mb+__read_unlock(), so we get the performance improvements on architectures other than x86 as well.

Quoted 2 lines

So in all cases, I don't think the smp_mb__after_lock() is the appropriate solution.

RCU on this part is out of the question, as David already mentioned it.

It would be a regression for short lived tcp/udp sessions, and some workloads use them a lot...

We gained about 20% performance between 2.6.26 and 2.6.31, carefuly removing some atomic ops in network stack, adding RCU where it was sensible, but this is a painful process, not something Jiri can use to fix bugs on legacy RedHat kernels :) (We still are sorting out regressions)

To solve problem pointed by Jiri, we have to insert an smp_mb() at this point, (not mentioning the other change in select() logic of course)

static void sock_def_readable(struct sock *sk, int len)
{
read_lock(&sk->sk_callback_lock);
+ smp_mb(); /* paired with opposite smp_mb() in sk poll logic */
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
wake_up_interruptible_sync_poll(sk->sk_sleep, POLLIN |
POLLRDNORM | POLLRDBAND);
sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); read_unlock(&sk->sk_callback_lock);
}

As about every incoming packet calls this path, we should be very careful not slowing down stack if not necessary.

On x86 this extra smp_mb() is not needed, since previous call to read_lock() already gives the full barrier for free.

#27 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 138 lines · * Eric Dumazet ([email protected]) wrote:
Quoted 137 lines · Mathieu Desnoyers
Quoted 113 lines · * Jiri Olsa ([email protected]) wrote:
Quoted 112 lines · Jiri Olsa
Quoted 100 lines · Jiri Olsa
Quoted 42 lines · Ingo Molnar
Quoted 40 lines · Jiri Olsa
Quoted 39 lines · Ingo Molnar
Quoted 28 lines · Eric Dumazet
Quoted 16 lines · Ingo Molnar

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

Quoted 7 lines

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

This was following existing implementations of various smp_mb__??? helpers :

# grep -4 smp_mb__before_clear_bit include/asm-generic/bitops.h
Quoted 7 lines

/*
* clear_bit may not imply a memory barrier
*/
#ifndef smp_mb__before_clear_bit
#define smp_mb__before_clear_bit() smp_mb()
#define smp_mb__after_clear_bit() smp_mb()
#endif

Did i mention that those should be fixed too? :-)

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

Ingo

ok, could I include it in the 2/2 or you prefer separate patch?

depends on whether it will regress ;-)

If it regresses, it's better to have it separate. If it wont, it can be included. If unsure, default to the more conservative option.

Ingo

how about this..
and similar change for smp_mb__before_clear_bit in a separate patch

arch/x86/include/asm/spinlock.h +4 -0
index b7e5db8..4e77853 100644
@@ -302,4 +302,8 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+static inline void smp_mb__after_lock(void) { }
+#define ARCH_HAS_SMP_MB_AFTER_LOCK
+
 #endif /* _ASM_X86_SPINLOCK_H */
include/linux/spinlock.h +5 -0
index 252b245..4be57ab 100644
@@ -132,6 +132,11 @@ do {								\
 #endif /*__raw_spin_is_contended*/
 #endif
 
+/* The lock does not imply full memory barrier. */
+#ifndef ARCH_HAS_SMP_MB_AFTER_LOCK
+static inline void smp_mb__after_lock(void) { smp_mb(); }
+#endif
+
 /**
  * spin_unlock_wait - wait until the spinlock gets unlocked
  * @lock: the spinlock in question.
include/net/sock.h +3 -0
index 4eb8409..98afcd9 100644
@@ -1271,6 +1271,9 @@ static inline int sk_has_allocations(const struct sock *sk)
  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
  * could then endup calling schedule and sleep forever if there are no more
  * data on the socket.
+ *
+ * The sk_has_helper is always called right after a call to read_lock, so we
+ * can use smp_mb__after_lock barrier.
  */
 static inline int sk_has_sleeper(struct sock *sk)
 {

include/net/sock.h static inline int sk_has_sleeper(struct sock *sk) · line 1289

Quoted 8 lines
include/net/sock.h +1 -1
@@ -1280,7 +1283,7 @@ static inline int sk_has_sleeper(struct sock *sk)
 	 *
 	 * This memory barrier is paired in the sock_poll_wait.
 	 */
-	smp_mb();
+	smp_mb__after_lock();
 	return sk->sk_sleep && waitqueue_active(sk->sk_sleep);
 }
 

any feedback on this?
I'd send v6 if this way is acceptable..

Quoted 8 lines
Quoted 2 lines

thanks,
jirka

also I checked the smp_mb__before_clear_bit/smp_mb__after_clear_bit and it is used quite extensivelly.

I'd prefer to send it in a separate patch, so we can move on with the changes I've sent so far..

As with any optimization (and this is one that adds a semantic that will just grow the memory barrier/locking rule complexity), it should come with performance benchmarks showing real-life improvements.

Otherwise I'd recommend sticking to smp_mb() if this execution path is not that critical, or to move to RCU if it's _that_ critical.

A valid argument would be if the data structures protected are so complex that RCU is out of question but still the few cycles saved by removing a memory barrier are really significant. And even then, the proper solution would be more something like a __read_lock()+smp_mb+smp_mb+__read_unlock(), so we get the performance improvements on architectures other than x86 as well.

Quoted 5 lines
Quoted 2 lines

So in all cases, I don't think the smp_mb__after_lock() is the appropriate solution.

RCU on this part is out of the question, as David already mentioned it.

OK

Quoted 8 lines

It would be a regression for short lived tcp/udp sessions, and some workloads use them a lot...

We gained about 20% performance between 2.6.26 and 2.6.31, carefuly removing some atomic ops in network stack, adding RCU where it was sensible, but this is a painful process, not something Jiri can use to fix bugs on legacy RedHat kernels :) (We still are sorting out regressions)

Yep, I can understand that. Tbench on localhost is an especially good benchmark for this ;)

Quoted 14 lines

To solve problem pointed by Jiri, we have to insert an smp_mb() at this point, (not mentioning the other change in select() logic of course)

static void sock_def_readable(struct sock *sk, int len) {

(quoted diff) +1 -0
 	read_lock(&sk->sk_callback_lock);
+	smp_mb(); /* paired with opposite smp_mb() in sk poll logic */
 	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
 		wake_up_interruptible_sync_poll(sk->sk_sleep, POLLIN |
 						POLLRDNORM | POLLRDBAND);
 	sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
 	read_unlock(&sk->sk_callback_lock);
 }
Quoted 7 lines

As about every incoming packet calls this path, we should be very careful not slowing down stack if not necessary.

On x86 this extra smp_mb() is not needed, since previous call to read_lock() already gives the full barrier for free.

Well, I see the __read_lock()+2x smp_mb+__read_unlock is not well suited for x86. You're right.

But read_lock + smp_mb__after_lock + read_unlock is not well suited for powerpc, arm, mips and probably others where there is an explicit memory barrier at the end of the read lock primitive.

One thing that would be efficient for all architectures is to create a locking primitive that contains the smp_mb, e.g.:

read_lock_smp_mb()

which would act as a read_lock which does a full smp_mb after the lock is taken.

The naming may be a bit odd, better ideas are welcome.

Mathieu

Signature · 2 lines
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
#28 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 7 lines · Mathieu Desnoyers

But read_lock + smp_mb__after_lock + read_unlock is not well suited for powerpc, arm, mips and probably others where there is an explicit memory barrier at the end of the read lock primitive.

One thing that would be efficient for all architectures is to create a locking primitive that contains the smp_mb, e.g.:

Quoted 6 lines

read_lock_smp_mb()

which would act as a read_lock which does a full smp_mb after the lock is taken.

The naming may be a bit odd, better ideas are welcome.

I see your point now, thanks for your patience.

Jiri, I think your first patch can be applied (including the full smp_mb()),
then we will optimize both for x86 and other arches, when all
arch maintainers have a chance to change
"read_lock();smp_mb()" to a faster "read_lock_mb()" or something :)

author
#29 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 14 lines · Eric Dumazet
Quoted 12 lines · Mathieu Desnoyers

But read_lock + smp_mb__after_lock + read_unlock is not well suited for powerpc, arm, mips and probably others where there is an explicit memory barrier at the end of the read lock primitive.

One thing that would be efficient for all architectures is to create a locking primitive that contains the smp_mb, e.g.:

read_lock_smp_mb()

which would act as a read_lock which does a full smp_mb after the lock is taken.

Quoted 9 lines
Quoted 1 line

The naming may be a bit odd, better ideas are welcome.

I see your point now, thanks for your patience.

Jiri, I think your first patch can be applied (including the full smp_mb()),
then we will optimize both for x86 and other arches, when all
arch maintainers have a chance to change
"read_lock();smp_mb()" to a faster "read_lock_mb()" or something :)

great, I saw you Signed-off the 1/2 part.. could I leave it, or do I need to resend as a single patch?

jirka

maintainer
#30 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

From: Jiri Olsa <[email protected]>
Date: Wed, 8 Jul 2009 19:47:48 +0200

Quoted 2 lines

great, I saw you Signed-off the 1/2 part.. could I leave it, or do I need to resend as a single patch?

Could you formally resubmit things, regardless of whether they are kept split up or combined? That will help me a lot. Thanks.

I think things are settled down at this point and we can push these patches into the tree.

author
#31 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 6 lines · David Miller

From: Jiri Olsa <[email protected]>
Date: Wed, 8 Jul 2009 19:47:48 +0200

Quoted 2 lines

great, I saw you Signed-off the 1/2 part.. could I leave it, or do I need to resend as a single patch?

Quoted 6 lines

Could you formally resubmit things, regardless of whether they are kept split up or combined? That will help me a lot. Thanks.

I think things are settled down at this point and we can push these patches into the tree.

np, I'll send out shortly complete v6 then..

jirka

#32 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

In reply to Ingo Molnar
Quoted 11 lines · * Ingo Molnar ([email protected]) wrote:

arch/x86/include/asm/spinlock.h static inline void __raw_write_unlock(raw_rwlock_t *rw) · line 306

Quoted 7 lines · Jiri Olsa

+++ b/arch/x86/include/asm/spinlock.h

arch/x86/include/asm/spinlock.h +2 -0
@@ -302,4 +302,7 @@ static inline void __raw_write_unlock(raw_rwlock_t *rw)
 #define _raw_read_relax(lock)	cpu_relax()
 #define _raw_write_relax(lock)	cpu_relax()
 
+/* The {read|write|spin}_lock() on x86 are full memory barriers. */
+#define smp_mb__after_lock() do { } while (0)

Hm. Looking at http://lkml.org/lkml/2009/6/23/192, a very basic question comes to my mind :

Why don't we create a read_lock without acquire semantic instead (e.g. read_lock_nomb(), or something with a better name like __read_lock()) ? On architectures where memory barriers are needed to provide the acquire semantic, it would be faster to do :

__read_lock();
smp_mb();

than :

read_lock(); <- e.g. lwsync + isync or something like that smp_mb(); <- full sync.

Second point : __add_wait_queue/waitqueue_active/wake_up_interruptible would probably benefit from adding comments about their combined use with other checks and how nice memory barriers are.

Mathieu

Quoted 15 lines

Two small stylistic comments, please make this an inline function:

static inline void smp_mb__after_lock(void) { }
#define smp_mb__after_lock

(untested)

include/linux/spinlock.h do { \ · line 138

Quoted 4 lines
include/linux/spinlock.h +4 -0
+/* The lock does not imply full memory barrier. */
+#ifndef smp_mb__after_lock
+#define smp_mb__after_lock() smp_mb()
+#endif

ditto.

Ingo

Signature · 2 lines
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
#33 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 6 lines · Mathieu Desnoyers

Why don't we create a read_lock without acquire semantic instead (e.g. read_lock_nomb(), or something with a better name like __read_lock()) ? On architectures where memory barriers are needed to provide the acquire semantic, it would be faster to do :

Quoted 7 lines

__read_lock();
smp_mb();

than :

read_lock(); <- e.g. lwsync + isync or something like that smp_mb(); <- full sync.

Hmm, why do we even care when read_lock should just die?

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#34 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 9 lines · Herbert Xu
Quoted 8 lines · Mathieu Desnoyers

Why don't we create a read_lock without acquire semantic instead (e.g. read_lock_nomb(), or something with a better name like __read_lock()) ? On architectures where memory barriers are needed to provide the acquire semantic, it would be faster to do :

__read_lock();
smp_mb();

Quoted 8 lines
Quoted 4 lines

than :

read_lock(); <- e.g. lwsync + isync or something like that smp_mb(); <- full sync.

Hmm, why do we even care when read_lock should just die?

Cheers,

+1 :)

Do you mean using a spinlock instead or what ?

Also, how many arches are able to have a true __read_lock() (or __spin_lock() if that matters), without acquire semantic ?

#35 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 15 lines · * Eric Dumazet ([email protected]) wrote:
Quoted 14 lines · Herbert Xu
Quoted 12 lines · Mathieu Desnoyers

Why don't we create a read_lock without acquire semantic instead (e.g. read_lock_nomb(), or something with a better name like __read_lock()) ? On architectures where memory barriers are needed to provide the acquire semantic, it would be faster to do :

__read_lock();
smp_mb();

than :

read_lock(); <- e.g. lwsync + isync or something like that smp_mb(); <- full sync.

Quoted 8 lines
Quoted 3 lines

Hmm, why do we even care when read_lock should just die?

Cheers,

+1 :)

Do you mean using a spinlock instead or what ?

I think he meant RCU.

Quoted 2 lines

Also, how many arches are able to have a true __read_lock() (or __spin_lock() if that matters), without acquire semantic ?

At least PowerPC, MIPS, recent ARM, alpha.

Mathieu

Signature · 2 lines
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
#36 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 25 lines · Mathieu Desnoyers
Quoted 15 lines · * Eric Dumazet ([email protected]) wrote:
Quoted 14 lines · Herbert Xu
Quoted 12 lines · Mathieu Desnoyers

Why don't we create a read_lock without acquire semantic instead (e.g. read_lock_nomb(), or something with a better name like __read_lock()) ? On architectures where memory barriers are needed to provide the acquire semantic, it would be faster to do :

__read_lock();
smp_mb();

than :

read_lock(); <- e.g. lwsync + isync or something like that smp_mb(); <- full sync.

Quoted 8 lines
Quoted 3 lines

Hmm, why do we even care when read_lock should just die?

Cheers,

+1 :)

Do you mean using a spinlock instead or what ?

Quoted 6 lines

I think he meant RCU.

Quoted 2 lines

Also, how many arches are able to have a true __read_lock() (or __spin_lock() if that matters), without acquire semantic ?

At least PowerPC, MIPS, recent ARM, alpha.

Are you guys sure you are in agreement about what you all mean by "acquire semantics"?

Clearly, any correct __read_lock() implementation must enforce ordering with respect to the most recent __write_unlock(), but this does not necesarily imply all possible definitions of "acquire semantics".

Thanx, Paul

#37 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

Quoted 28 lines · * Paul E. McKenney ([email protected]) wrote:
Quoted 27 lines · Mathieu Desnoyers
Quoted 15 lines · * Eric Dumazet ([email protected]) wrote:
Quoted 14 lines · Herbert Xu
Quoted 12 lines · Mathieu Desnoyers

Why don't we create a read_lock without acquire semantic instead (e.g. read_lock_nomb(), or something with a better name like __read_lock()) ? On architectures where memory barriers are needed to provide the acquire semantic, it would be faster to do :

__read_lock();
smp_mb();

than :

read_lock(); <- e.g. lwsync + isync or something like that smp_mb(); <- full sync.

Quoted 8 lines
Quoted 3 lines

Hmm, why do we even care when read_lock should just die?

Cheers,

+1 :)

Do you mean using a spinlock instead or what ?

I think he meant RCU.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Also, how many arches are able to have a true __read_lock() (or __spin_lock() if that matters), without acquire semantic ?

At least PowerPC, MIPS, recent ARM, alpha.

Are you guys sure you are in agreement about what you all mean by "acquire semantics"?

I use acquire/release semantic with the following meaning :

...
read A
read_unlock()

read B

read_lock();
read C

read_unlock would provide release semantic by disallowing read A to move after the read_unlock.

read_lock would provide acquire semantic by disallowing read C to move before read_lock.

read B is free to move.

Quoted 4 lines

Clearly, any correct __read_lock() implementation must enforce ordering with respect to the most recent __write_unlock(), but this does not necesarily imply all possible definitions of "acquire semantics".

Yes, you are right. We could never remove _all_ memory barriers from __read_lock()/__read_unlock implementations even if we require something such as :

__read_lock()
smp_mb()

critical section.

smp_mb()
__read_unlock()

Because we also need to guarantee that consecutive unlock/lock won't be reordered, which implies a barrier _outside_ of the read lock/unlock atomic operations.

But anyway I'm not sure it's worth trying to optimize rwlocks, given that for critical sections where the performance hit of a memory barrier would be perceivable, we should really think about using RCU rather than beating this dead horse. :)

Thanks,

Mathieu.

Quoted 1 line

Thanx, Paul

Signature · 2 lines
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
#38 lore raw

Re: [PATCHv5 2/2] memory barrier: adding smp_mb__after_lock

In reply to Herbert Xu
Quoted 10 lines · * Herbert Xu ([email protected]) wrote:
Quoted 9 lines · Mathieu Desnoyers

Why don't we create a read_lock without acquire semantic instead (e.g. read_lock_nomb(), or something with a better name like __read_lock()) ? On architectures where memory barriers are needed to provide the acquire semantic, it would be faster to do :

__read_lock();
smp_mb();

Quoted 7 lines
Quoted 4 lines

than :

read_lock(); <- e.g. lwsync + isync or something like that smp_mb(); <- full sync.

Hmm, why do we even care when read_lock should just die?

I guess you are proposing migration from rwlock to RCU ?

Well, in cases where critical sections are in the order of 20000 cycles or more, and with 8 to 64-core machines, there is no significant gain in using RCU vs rwlocks, so the added complexity might not be justified if the critical sections are very long.

But then it's a case by case thing. We would have to see what exactly is protected by this read lock and how long the critical section is. However, in any case, you are right: rwlocks are acceptable only for long critical sections, for which we just don't care about one extra memory barrier.

Instead of optimizing away these read-side rwlock barriers, we would spend our time much more efficiently switching to RCU read side.

Mathieu

Quoted 6 lines

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Signature · 2 lines
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68