Skip to contentSkip to conversation

sunrpc: fix write space race causing stalls

PATCH In review David Vrabel 3 participants 6 messages Sep 16, 2016 1 message not in the archive

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sunrpc: fix write space race causing stalls

Write space becoming available may race with putting the task to sleep in xprt_wait_for_buffer_space(). The existing mechanism to avoid the race does not work.

This (edited) partial trace illustrates the problem:

[1] rpc_task_run_action: task:43546@5 ... action=call_transmit
[2] xs_write_space <-xs_tcp_write_space
[3] xprt_write_space <-xs_write_space
[4] rpc_task_sleep: task:43546@5 ...
[5] xs_write_space <-xs_tcp_write_space

[1] Task 43546 runs but is out of write space.

[2] Space becomes available, xs_write_space() clears the SOCKWQ_ASYNC_NOSPACE bit.

[3] xprt_write_space() attemts to wake xprt->snd_task (== 43546), but this has not yet been queued and the wake up is lost.

[4] xs_nospace() is called which calls xprt_wait_for_buffer_space() which queues task 43546.

[5] The call to sk->sk_write_space() at the end of xs_nospace() (which is supposed to handle the above race) does not call xprt_write_space() as the SOCKWQ_ASYNC_NOSPACE bit is clear and thus the task is not woken.

Fix the race by have xprt_wait_for_buffer_space() check for write space after putting the task to sleep.

---
 include/linux/sunrpc/xprt.h |  1 +
 net/sunrpc/xprt.c           |  4 ++++
 net/sunrpc/xprtsock.c       | 21 +++++++++++++++++++--
 3 files changed, 24 insertions(+), 2 deletions(-)
include/linux/sunrpc/xprt.h +1 -0
index a16070d..621e74b 100644
@@ -129,6 +129,7 @@ struct rpc_xprt_ops {
 	void		(*connect)(struct rpc_xprt *xprt, struct rpc_task *task);
 	void *		(*buf_alloc)(struct rpc_task *task, size_t size);
 	void		(*buf_free)(void *buffer);
+	bool            (*have_write_space)(struct rpc_xprt *task);
 	int		(*send_request)(struct rpc_task *task);
 	void		(*set_retrans_timeout)(struct rpc_task *task);
 	void		(*timer)(struct rpc_xprt *xprt, struct rpc_task *task);
net/sunrpc/xprt.c +4 -0
index ea244b2..d3c1b1e 100644
@@ -502,6 +502,10 @@ void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
 
 	task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
 	rpc_sleep_on(&xprt->pending, task, action);
+
+	/* Write space notification may race with putting task to sleep. */
+	if (xprt->ops->have_write_space(xprt))
+		rpc_wake_up_queued_task(&xprt->pending, task);
 }
 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
 
net/sunrpc/xprtsock.c +19 -2
index bf16883..211de5b 100644
@@ -472,8 +472,6 @@ static int xs_nospace(struct rpc_task *task)
 
 	spin_unlock_bh(&xprt->transport_lock);
 
-	/* Race breaker in case memory is freed before above code is called */
-	sk->sk_write_space(sk);
 	return ret;
 }
 
@@ -1679,6 +1677,22 @@ static void xs_tcp_write_space(struct sock *sk)
 	read_unlock_bh(&sk->sk_callback_lock);
 }
 
+static bool xs_udp_have_write_space(struct rpc_xprt *xprt)
+{
+	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
+	struct sock *sk = transport->inet;
+
+	return sock_writeable(sk);
+}
+
+static bool xs_tcp_have_write_space(struct rpc_xprt *xprt)
+{
+	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
+	struct sock *sk = transport->inet;
+
+	return sk_stream_is_writeable(sk);
+}
+
 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
 {
 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
@@ -2664,6 +2678,7 @@ static struct rpc_xprt_ops xs_local_ops = {
 	.connect		= xs_local_connect,
 	.buf_alloc		= rpc_malloc,
 	.buf_free		= rpc_free,
+	.have_write_space       = xs_udp_have_write_space,
 	.send_request		= xs_local_send_request,
 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
 	.close			= xs_close,
@@ -2683,6 +2698,7 @@ static struct rpc_xprt_ops xs_udp_ops = {
 	.connect		= xs_connect,
 	.buf_alloc		= rpc_malloc,
 	.buf_free		= rpc_free,
+	.have_write_space       = xs_udp_have_write_space,
 	.send_request		= xs_udp_send_request,
 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
 	.timer			= xs_udp_timer,
@@ -2704,6 +2720,7 @@ static struct rpc_xprt_ops xs_tcp_ops = {
 	.connect		= xs_connect,
 	.buf_alloc		= rpc_malloc,
 	.buf_free		= rpc_free,
+	.have_write_space       = xs_tcp_have_write_space,
 	.send_request		= xs_tcp_send_request,
 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
 	.close			= xs_tcp_shutdown,
Signature · 1 line
2.1.4
#1 lore raw

net/sunrpc/xprt.c void xprt_wait_for_buffer_space(struct rpc_task *tas=

Quoted 58 lines · David Vrabel

=20
Write space becoming available may race with putting the task to sleep in xprt_wait_for_buffer_space(). The existing mechanism to avoid the race does not work.
=20
This (edited) partial trace illustrates the problem:
=20
[1] rpc_task_run_action: task:43546@5 ... action=3Dcall_transmit
[2] xs_write_space <-xs_tcp_write_space
[3] xprt_write_space <-xs_write_space
[4] rpc_task_sleep: task:43546@5 ...
[5] xs_write_space <-xs_tcp_write_space
=20
[1] Task 43546 runs but is out of write space.
=20
[2] Space becomes available, xs_write_space() clears the SOCKWQ_ASYNC_NOSPACE bit.
=20
[3] xprt_write_space() attemts to wake xprt->snd_task (=3D=3D 43546), but this has not yet been queued and the wake up is lost.
=20
[4] xs_nospace() is called which calls xprt_wait_for_buffer_space() which queues task 43546.
=20
[5] The call to sk->sk_write_space() at the end of xs_nospace() (which is supposed to handle the above race) does not call xprt_write_space() as the SOCKWQ_ASYNC_NOSPACE bit is clear and thus the task is not woken.
=20
Fix the race by have xprt_wait_for_buffer_space() check for write space after putting the task to sleep.
=20

---

include/linux/sunrpc/xprt.h |  1 +
net/sunrpc/xprt.c           |  4 ++++
net/sunrpc/xprtsock.c       | 21 +++++++++++++++++++--
3 files changed, 24 insertions(+), 2 deletions(-)

=20

include/linux/sunrpc/xprt.h +1 -0
index a16070d..621e74b 100644
@@ -129,6 +129,7 @@ struct rpc_xprt_ops {
 =09void=09=09(*connect)(struct rpc_xprt *xprt, struct rpc_task *task);
 =09void *=09=09(*buf_alloc)(struct rpc_task *task, size_t size);
 =09void=09=09(*buf_free)(void *buffer);
+=09bool            (*have_write_space)(struct rpc_xprt *task);
 =09int=09=09(*send_request)(struct rpc_task *task);
 =09void=09=09(*set_retrans_timeout)(struct rpc_task *task);
 =09void=09=09(*timer)(struct rpc_xprt *xprt, struct rpc_task *task);
net/sunrpc/xprt.c +0 -0
index ea244b2..d3c1b1e 100644
@@ -502,6 +502,10 @@ void xprt_wait_for_buffer_space(struct rpc_task *tas=

k, rpc_action action)

Quoted 15 lines

=20
=09task->tk_timeout =3D RPC_IS_SOFT(task) ? req->rq_timeout : 0; =09rpc_sleep_on(&xprt->pending, task, action);
+

+=09/* Write space notification may race with putting task to sleep. */
+=09if (xprt->ops->have_write_space(xprt))
+=09=09rpc_wake_up_queued_task(&xprt->pending, task);

}
EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
=20

net/sunrpc/xprtsock.c +0 -0
index bf16883..211de5b 100644
@@ -472,8 +472,6 @@ static int xs_nospace(struct rpc_task *task)
Quoted 4 lines

=20
=09spin_unlock_bh(&xprt->transport_lock);
=20
-=09/* Race breaker in case memory is freed before above code is called *=

/

Quoted 3 lines

-=09sk->sk_write_space(sk);
=09return ret;
}

Instead of these callbacks, why not just add a call to sk_set_bit(SOCKWQ_AS= YNC_WAITDATA, sk) after queueing the task in xs_nospace()? Won=92t that fix=
the existing race breaker?

Cheers
Trond

#2 lore raw
Quoted 76 lines · Trond Myklebust
Quoted 76 lines · David Vrabel

Write space becoming available may race with putting the task to sleep in xprt_wait_for_buffer_space(). The existing mechanism to avoid the race does not work.

This (edited) partial trace illustrates the problem:

[1] rpc_task_run_action: task:43546@5 ... action=call_transmit
[2] xs_write_space <-xs_tcp_write_space
[3] xprt_write_space <-xs_write_space
[4] rpc_task_sleep: task:43546@5 ...
[5] xs_write_space <-xs_tcp_write_space

[1] Task 43546 runs but is out of write space.

[2] Space becomes available, xs_write_space() clears the SOCKWQ_ASYNC_NOSPACE bit.

[3] xprt_write_space() attemts to wake xprt->snd_task (== 43546), but this has not yet been queued and the wake up is lost.

[4] xs_nospace() is called which calls xprt_wait_for_buffer_space() which queues task 43546.

[5] The call to sk->sk_write_space() at the end of xs_nospace() (which is supposed to handle the above race) does not call xprt_write_space() as the SOCKWQ_ASYNC_NOSPACE bit is clear and thus the task is not woken.

Fix the race by have xprt_wait_for_buffer_space() check for write space after putting the task to sleep.

---

include/linux/sunrpc/xprt.h |  1 +
net/sunrpc/xprt.c           |  4 ++++
net/sunrpc/xprtsock.c       | 21 +++++++++++++++++++--
3 files changed, 24 insertions(+), 2 deletions(-)
include/linux/sunrpc/xprt.h +1 -0
index a16070d..621e74b 100644
@@ -129,6 +129,7 @@ struct rpc_xprt_ops {
 	void		(*connect)(struct rpc_xprt *xprt, struct rpc_task *task);
 	void *		(*buf_alloc)(struct rpc_task *task, size_t size);
 	void		(*buf_free)(void *buffer);
+	bool            (*have_write_space)(struct rpc_xprt *task);
 	int		(*send_request)(struct rpc_task *task);
 	void		(*set_retrans_timeout)(struct rpc_task *task);
 	void		(*timer)(struct rpc_xprt *xprt, struct rpc_task *task);
net/sunrpc/xprt.c +4 -0
index ea244b2..d3c1b1e 100644
@@ -502,6 +502,10 @@ void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
 
 	task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
 	rpc_sleep_on(&xprt->pending, task, action);
+
+	/* Write space notification may race with putting task to sleep. */
+	if (xprt->ops->have_write_space(xprt))
+		rpc_wake_up_queued_task(&xprt->pending, task);
 }
 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
 
net/sunrpc/xprtsock.c +0 -0
index bf16883..211de5b 100644
@@ -472,8 +472,6 @@ static int xs_nospace(struct rpc_task *task)
 
 	spin_unlock_bh(&xprt->transport_lock);
 
Quoted 8 lines

net/sunrpc/xprtsock.c static int xs_nospace(struct rpc_task *task) · line 476

Quoted 4 lines
net/sunrpc/xprtsock.c +0 -2
-	/* Race breaker in case memory is freed before above code is called */
-	sk->sk_write_space(sk);
 	return ret;
 }

Instead of these callbacks, why not just add a call to sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk) after queueing the task in xs_nospace()? Won’t that fix the existing race breaker?

I don't see how that would help. If sk->sk_write_space was already called, SOCKWQ_ASYNC_NOSPACE will still be clear and the next call to sk->sk_write_space will still be a nop.

Or did you mean SOCKWQ_ASYNC_NOSPACE here? It doesn't seem right to set this bit when we don't know if there's space or not.

David

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 89 lines · David Vrabel
Quoted 76 lines · Trond Myklebust
Quoted 76 lines · David Vrabel

Write space becoming available may race with putting the task to sleep in xprt_wait_for_buffer_space(). The existing mechanism to avoid the race does not work.

This (edited) partial trace illustrates the problem:

[1] rpc_task_run_action: task:43546@5 ... action=call_transmit
[2] xs_write_space <-xs_tcp_write_space
[3] xprt_write_space <-xs_write_space
[4] rpc_task_sleep: task:43546@5 ...
[5] xs_write_space <-xs_tcp_write_space

[1] Task 43546 runs but is out of write space.

[2] Space becomes available, xs_write_space() clears the SOCKWQ_ASYNC_NOSPACE bit.

[3] xprt_write_space() attemts to wake xprt->snd_task (== 43546), but this has not yet been queued and the wake up is lost.

[4] xs_nospace() is called which calls xprt_wait_for_buffer_space() which queues task 43546.

[5] The call to sk->sk_write_space() at the end of xs_nospace() (which is supposed to handle the above race) does not call xprt_write_space() as the SOCKWQ_ASYNC_NOSPACE bit is clear and thus the task is not woken.

Fix the race by have xprt_wait_for_buffer_space() check for write space after putting the task to sleep.

---

include/linux/sunrpc/xprt.h |  1 +
net/sunrpc/xprt.c           |  4 ++++
net/sunrpc/xprtsock.c       | 21 +++++++++++++++++++--
3 files changed, 24 insertions(+), 2 deletions(-)
include/linux/sunrpc/xprt.h +1 -0
index a16070d..621e74b 100644
@@ -129,6 +129,7 @@ struct rpc_xprt_ops {
 	void		(*connect)(struct rpc_xprt *xprt, struct rpc_task *task);
 	void *		(*buf_alloc)(struct rpc_task *task, size_t size);
 	void		(*buf_free)(void *buffer);
+	bool            (*have_write_space)(struct rpc_xprt *task);
 	int		(*send_request)(struct rpc_task *task);
 	void		(*set_retrans_timeout)(struct rpc_task *task);
 	void		(*timer)(struct rpc_xprt *xprt, struct rpc_task *task);
net/sunrpc/xprt.c +4 -0
index ea244b2..d3c1b1e 100644
@@ -502,6 +502,10 @@ void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
 
 	task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
 	rpc_sleep_on(&xprt->pending, task, action);
+
+	/* Write space notification may race with putting task to sleep. */
+	if (xprt->ops->have_write_space(xprt))
+		rpc_wake_up_queued_task(&xprt->pending, task);
 }
 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
 
net/sunrpc/xprtsock.c +0 -0
index bf16883..211de5b 100644
@@ -472,8 +472,6 @@ static int xs_nospace(struct rpc_task *task)
 
 	spin_unlock_bh(&xprt->transport_lock);
 
Quoted 8 lines

net/sunrpc/xprtsock.c static int xs_nospace(struct rpc_task *task) · line 476

Quoted 4 lines
net/sunrpc/xprtsock.c +0 -2
-	/* Race breaker in case memory is freed before above code is called */
-	sk->sk_write_space(sk);
 	return ret;
 }

Instead of these callbacks, why not just add a call to sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk) after queueing the task in xs_nospace()? Won’t that fix the existing race breaker?

Quoted 3 lines

I don't see how that would help. If sk->sk_write_space was already called, SOCKWQ_ASYNC_NOSPACE will still be clear and the next call to sk->sk_write_space will still be a nop.

Sorry. Copy+paste error. I meant SOCKWQ_ASYNC_NOSPACE.

Quoted 3 lines

Or did you mean SOCKWQ_ASYNC_NOSPACE here? It doesn't seem right to set this bit when we don't know if there's space or not.

Why not?

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 93 lines · Trond Myklebust
Quoted 89 lines · David Vrabel
Quoted 76 lines · Trond Myklebust
Quoted 76 lines · David Vrabel

Write space becoming available may race with putting the task to sleep in xprt_wait_for_buffer_space(). The existing mechanism to avoid the race does not work.

This (edited) partial trace illustrates the problem:

[1] rpc_task_run_action: task:43546@5 ... action=call_transmit
[2] xs_write_space <-xs_tcp_write_space
[3] xprt_write_space <-xs_write_space
[4] rpc_task_sleep: task:43546@5 ...
[5] xs_write_space <-xs_tcp_write_space

[1] Task 43546 runs but is out of write space.

[2] Space becomes available, xs_write_space() clears the SOCKWQ_ASYNC_NOSPACE bit.

[3] xprt_write_space() attemts to wake xprt->snd_task (== 43546), but this has not yet been queued and the wake up is lost.

[4] xs_nospace() is called which calls xprt_wait_for_buffer_space() which queues task 43546.

[5] The call to sk->sk_write_space() at the end of xs_nospace() (which is supposed to handle the above race) does not call xprt_write_space() as the SOCKWQ_ASYNC_NOSPACE bit is clear and thus the task is not woken.

Fix the race by have xprt_wait_for_buffer_space() check for write space after putting the task to sleep.

---

include/linux/sunrpc/xprt.h |  1 +
net/sunrpc/xprt.c           |  4 ++++
net/sunrpc/xprtsock.c       | 21 +++++++++++++++++++--
3 files changed, 24 insertions(+), 2 deletions(-)
include/linux/sunrpc/xprt.h +1 -0
index a16070d..621e74b 100644
@@ -129,6 +129,7 @@ struct rpc_xprt_ops {
 	void		(*connect)(struct rpc_xprt *xprt, struct rpc_task *task);
 	void *		(*buf_alloc)(struct rpc_task *task, size_t size);
 	void		(*buf_free)(void *buffer);
+	bool            (*have_write_space)(struct rpc_xprt *task);
 	int		(*send_request)(struct rpc_task *task);
 	void		(*set_retrans_timeout)(struct rpc_task *task);
 	void		(*timer)(struct rpc_xprt *xprt, struct rpc_task *task);
net/sunrpc/xprt.c +4 -0
index ea244b2..d3c1b1e 100644
@@ -502,6 +502,10 @@ void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
 
 	task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
 	rpc_sleep_on(&xprt->pending, task, action);
+
+	/* Write space notification may race with putting task to sleep. */
+	if (xprt->ops->have_write_space(xprt))
+		rpc_wake_up_queued_task(&xprt->pending, task);
 }
 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
 
net/sunrpc/xprtsock.c +0 -0
index bf16883..211de5b 100644
@@ -472,8 +472,6 @@ static int xs_nospace(struct rpc_task *task)
 
 	spin_unlock_bh(&xprt->transport_lock);
 
Quoted 8 lines

net/sunrpc/xprtsock.c static int xs_nospace(struct rpc_task *task) · line 476

Quoted 4 lines
net/sunrpc/xprtsock.c +0 -2
-	/* Race breaker in case memory is freed before above code is called */
-	sk->sk_write_space(sk);
 	return ret;
 }

Instead of these callbacks, why not just add a call to sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk) after queueing the task in xs_nospace()? Won’t that fix the existing race breaker?

Quoted 3 lines

I don't see how that would help. If sk->sk_write_space was already called, SOCKWQ_ASYNC_NOSPACE will still be clear and the next call to sk->sk_write_space will still be a nop.

Quoted 7 lines

Sorry. Copy+paste error. I meant SOCKWQ_ASYNC_NOSPACE.

Quoted 3 lines

Or did you mean SOCKWQ_ASYNC_NOSPACE here? It doesn't seem right to set this bit when we don't know if there's space or not.

Why not?

I prefer my solution because:

a) It obviously fixes the race (games with bits are less understandable).

b) It requires fewer atomic ops.

c) It doesn't require me to understand what the behaviour of the socket-internal SOCKWQ_ASYNC_NOSPACE bit is or should be.

d) I'm not sure I understand the objection to the additional have_write_space method -- it has simple, clear behaviour.

David

#6 lore raw
Quoted 6 lines · David Vrabel

=20

Quoted 2 lines · Trond Myklebust

=20

Quoted 1 line

On Sep 16, 2016, at 12:41, David Vrabel <[email protected]> wrote=

:

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

=20

Quoted 2 lines · Trond Myklebust

=20

Quoted 1 line

On Sep 16, 2016, at 08:28, David Vrabel <[email protected]> wro=

te:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

=20
Write space becoming available may race with putting the task to slee=

p

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

in xprt_wait_for_buffer_space(). The existing mechanism to avoid the race does not work.
=20
This (edited) partial trace illustrates the problem:
=20
[1] rpc_task_run_action: task:43546@5 ... action=3Dcall_transmit
[2] xs_write_space <-xs_tcp_write_space
[3] xprt_write_space <-xs_write_space
[4] rpc_task_sleep: task:43546@5 ...

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

[5] xs_write_space <-xs_tcp_write_space
=20
[1] Task 43546 runs but is out of write space.
=20
[2] Space becomes available, xs_write_space() clears the SOCKWQ_ASYNC_NOSPACE bit.
=20
[3] xprt_write_space() attemts to wake xprt->snd_task (=3D=3D 43546),=

but

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

this has not yet been queued and the wake up is lost.
=20
[4] xs_nospace() is called which calls xprt_wait_for_buffer_space() which queues task 43546.
=20
[5] The call to sk->sk_write_space() at the end of xs_nospace() (whic=

h

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

is supposed to handle the above race) does not call xprt_write_space() as the SOCKWQ_ASYNC_NOSPACE bit is clear and thus the task is not woken.
=20
Fix the race by have xprt_wait_for_buffer_space() check for write space after putting the task to sleep.
=20

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

include/linux/sunrpc/xprt.=

Quoted 8 lines

---

include/linux/sunrpc/xprt.h |  1 +
net/sunrpc/xprt.c           |  4 ++++
net/sunrpc/xprtsock.c       | 21 +++++++++++++++++++--
3 files changed, 24 insertions(+), 2 deletions(-)

=20

include/linux/sunrpc/xprt.= renamed from include/linux/sunrpc/xprt.h +0 -0

h

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

index a16070d..621e74b 100644

include/linux/sunrpc/xprt.h +0 -0
@@ -129,6 +129,7 @@ struct rpc_xprt_ops {

=09void=09=09(*connect)(struct rpc_xprt *xprt, struct rpc_task *task)=

;

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

net/sunrpc/xprt.c void xprt_wait_for_buffer_space(struct rpc_task =

Quoted 11 lines

=09void *=09=09(*buf_alloc)(struct rpc_task *task, size_t size); =09void=09=09(*buf_free)(void *buffer);
+=09bool (*have_write_space)(struct rpc_xprt *task);
=09int=09=09(*send_request)(struct rpc_task *task); =09void=09=09(*set_retrans_timeout)(struct rpc_task *task); =09void=09=09(*timer)(struct rpc_xprt *xprt, struct rpc_task *task);

net/sunrpc/xprt.c +0 -0
index ea244b2..d3c1b1e 100644
@@ -502,6 +502,10 @@ void xprt_wait_for_buffer_space(struct rpc_task =

*task, rpc_action action)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

=20
=09task->tk_timeout =3D RPC_IS_SOFT(task) ? req->rq_timeout : 0; =09rpc_sleep_on(&xprt->pending, task, action);
+
+=09/* Write space notification may race with putting task to sleep. =

*/

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
+=09if (xprt->ops->have_write_space(xprt))
+=09=09rpc_wake_up_queued_task(&xprt->pending, task);

}
EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
=20

net/sunrpc/xprtsock.c +0 -0
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

index bf16883..211de5b 100644

net/sunrpc/xprtsock.c +0 -0
@@ -472,8 +472,6 @@ static int xs_nospace(struct rpc_task *task)

=20
=09spin_unlock_bh(&xprt->transport_lock);
=20
-=09/* Race breaker in case memory is freed before above code is call=

ed */

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
Quoted 3 lines

-=09sk->sk_write_space(sk);
=09return ret;
}

=20
Instead of these callbacks, why not just add a call to sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk) after queueing the task in xs_nospace()? Won=92t that fix the existing race breaker?

=20
I don't see how that would help. If sk->sk_write_space was already called, SOCKWQ_ASYNC_NOSPACE will still be clear and the next call to sk->sk_write_space will still be a nop.

Quoted 5 lines
Quoted 5 lines

=20
Sorry. Copy+paste error. I meant SOCKWQ_ASYNC_NOSPACE. =20

Quoted 2 lines

=20
Or did you mean SOCKWQ_ASYNC_NOSPACE here? It doesn't seem right to se=

t

Quoted 16 lines
Quoted 3 lines
Quoted 1 line

this bit when we don't know if there's space or not.

=20
Why not?

=20
I prefer my solution because:
=20
a) It obviously fixes the race (games with bits are less understandable). =20
b) It requires fewer atomic ops.
=20
c) It doesn't require me to understand what the behaviour of the socket-internal SOCKWQ_ASYNC_NOSPACE bit is or should be.
=20
d) I'm not sure I understand the objection to the additional have_write_space method -- it has simple, clear behaviour. =20

I don=92t see the point of adding 24 lines of code over 3 different files i= f the problem can be solved with 1 line of code.