Skip to contentSkip to conversation

unix: add read side socket memory accounting for dgram sockets

PATCH Superseded Hannes Frederic Sowa 2 participants 13 messages Feb 17, 2014 – Mar 10, 2014

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

PATCH 1/1 unix: add read side socket memory accounting for dgram sockets

The current situation:

We allocate memory with sock_alloc_send_pskb which accounts skb to the sending socket's sk_wmem_alloc. The skb->destructor does a sock_wfree as soon as the recipient retrieves the data and skb is destroyed.

The problem with this implementation is that an unconnected unix dgram server could get blocked sending further data, if a client does not retrieve its frames from its sk_receive_queue because sk_wmem_alloc is under control of the client.

This patch does the following improvment:

We still allocate dgram packets with sock_alloc_send_pskb, which now does normally not block if the socket has too many packets in flight.

While delivering the skb to the client in unix_dgram_sendmsg we check for the recipients sockets rmem and block there if sk_rcvbuf is surpassed.

As the sender socket is not attached to the skb while retrieving it any more, we must attach the unix dgram peers address to the skb directly, this is done via a new field in unix_skb_parms.

Note, socket buffer limitation is already in place from the generic socket layer. The client cannot request more than sysctl_rmem_max (/proc/sys/net/core/rmem_max) bytes of receive buffer.

The (unix_peer(other) != sk) checks are removed as we now no longer block in in sock_alloc_send_pskb if we try to send data to ourself, as send buffer will always be free. To avoid DoS we need to bring the sender to a stop trying to deliver skbs to its own socket receive queue here. The same logic applies to the unix_dgram_poll change.

So, currently the following protections are in place to not get a victim of a DoS:
* sk_receive_queue length limitation by sk_max_ack_backlog
* sk_rcvbuf length limiting on the receiving socket
* sending buffer limitations in case of too may concurrent send requests

With this patch it is possible to maybe relax the unix_recq_full check in future or make the sk_max_ack_backlog configurable in future on a per-socket basis.

CC Yannick Koehler <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc David Miller <[email protected]>
Cc Cong Wang <[email protected]>
Cc Dan Ballard <[email protected]>
---
 include/net/af_unix.h |  1 +
 net/unix/af_unix.c    | 79 ++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 63 insertions(+), 17 deletions(-)
include/net/af_unix.h +1 -0
index a175ba4..52fbabd 100644
@@ -36,6 +36,7 @@ struct unix_skb_parms {
 	u32			secid;		/* Security ID		*/
 #endif
 	u32			consumed;
+	struct unix_address	*dgram_peer;
 };
 
 #define UNIXCB(skb) 	(*(struct unix_skb_parms *)&((skb)->cb))
net/unix/af_unix.c +62 -17

161 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#1 lore raw

PATCH v2 1/1 unix: add read side socket memory accounting for dgram sockets

The current situation:

We allocate memory with sock_alloc_send_pskb which accounts skb to the sending socket's sk_wmem_alloc. The skb->destructor does a sock_wfree as soon as the recipient retrieves the data and skb is destroyed.

The problem with this implementation is that an unconnected unix dgram server could get blocked sending further data, if a client does not retrieve its frames from its sk_receive_queue because sk_wmem_alloc is under control of the client.

This patch does the following improvment:

We still allocate dgram packets with sock_alloc_send_pskb, which now does normally not block if the socket has too many packets in flight.

While delivering the skb to the client in unix_dgram_sendmsg we check for the recipients sockets rmem and block there if sk_rcvbuf is surpassed.

As the sender socket is not attached to the skb while retrieving it any more, we must attach the unix dgram peers address to the skb directly, this is done via a new field in unix_skb_parms.

Note, socket buffer limitation is already in place from the generic socket layer. The client cannot request more than sysctl_rmem_max (/proc/sys/net/core/rmem_max) bytes of receive buffer.

The (unix_peer(other) != sk) checks are removed as we now no longer block in in sock_alloc_send_pskb if we try to send data to ourself, as send buffer will always be free. To avoid DoS we need to bring the sender to a stop trying to deliver skbs to its own socket receive queue here. The same logic applies to the unix_dgram_poll change.

So, currently the following protections are in place to not get a victim of a DoS:
* sk_receive_queue length limitation by sk_max_ack_backlog
* sk_rcvbuf length limiting on the receiving socket
* sending buffer limitations in case of too may concurrent send requests

With this patch it is possible to maybe relax the unix_recq_full check in future or make the sk_max_ack_backlog configurable in future on a per-socket basis.

CC Yannick Koehler <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc David Miller <[email protected]>
Cc Cong Wang <[email protected]>
Cc Dan Ballard <[email protected]>

---
Changelog v2:

In unix_wait_for_peer I messed up the condition of unix_other_writable to determine if we should call schedule_timeout:

Instead of

                sched = sched && (unix_recvq_full(other) ||
                                  unix_other_writable(other));

it is now

                sched = sched && (unix_recvq_full(other) ||
                                  !unix_other_writable(other));
 include/net/af_unix.h |  1 +
 net/unix/af_unix.c    | 79 ++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 63 insertions(+), 17 deletions(-)
include/net/af_unix.h +1 -0
index a175ba4..52fbabd 100644
@@ -36,6 +36,7 @@ struct unix_skb_parms {
 	u32			secid;		/* Security ID		*/
 #endif
 	u32			consumed;
+	struct unix_address	*dgram_peer;
 };
 
 #define UNIXCB(skb) 	(*(struct unix_skb_parms *)&((skb)->cb))
net/unix/af_unix.c +62 -17

161 lines not on this page · raw message

Signature · 1 line
1.8.5.3
maintainer
#2 lore raw

From: Hannes Frederic Sowa <[email protected]>
Date: Mon, 17 Feb 2014 14:03:53 +0100

Quoted 2 lines

We still allocate dgram packets with sock_alloc_send_pskb, which now does normally not block if the socket has too many packets in flight.

It seems like it does to me, it does sock_wait_for_wmem(), right?

Or are you trying to say that usually this is not the point at which we block, but rather it's when we check the peer's receive queue?

#3 lore raw
Quoted 7 lines · David Miller

From: Hannes Frederic Sowa <[email protected]>
Date: Mon, 17 Feb 2014 14:03:53 +0100

Quoted 2 lines

We still allocate dgram packets with sock_alloc_send_pskb, which now does normally not block if the socket has too many packets in flight.

It seems like it does to me, it does sock_wait_for_wmem(), right?

We still do the sock_wait_for_wmem in sock_alloc_send_pskb, correct, but we rarly block there, maybe in highly concurrent cases with big payloads where one process got interrupted to account the memory to the receiving socket.

Quoted 2 lines

Or are you trying to say that usually this is not the point at which we block, but rather it's when we check the peer's receive queue?

Exactly.

Bye,

Hannes

maintainer
#4 lore raw

From: Hannes Frederic Sowa <[email protected]>
Date: Wed, 19 Feb 2014 21:08:11 +0100

Quoted 14 lines · David Miller
Quoted 7 lines

From: Hannes Frederic Sowa <[email protected]>
Date: Mon, 17 Feb 2014 14:03:53 +0100

Quoted 2 lines

We still allocate dgram packets with sock_alloc_send_pskb, which now does normally not block if the socket has too many packets in flight.

It seems like it does to me, it does sock_wait_for_wmem(), right?

We still do the sock_wait_for_wmem in sock_alloc_send_pskb, correct, but we rarly block there, maybe in highly concurrent cases with big payloads where one process got interrupted to account the memory to the receiving socket.

Quoted 4 lines
Quoted 2 lines

Or are you trying to say that usually this is not the point at which we block, but rather it's when we check the peer's receive queue?

Exactly.

Just FYI, I'm not ignoring this patch, I'm just thinking about it a lot.

maintainer
#5 lore raw
In reply to Hannes Frederic Sowa

From: Hannes Frederic Sowa <[email protected]>
Date: Wed, 19 Feb 2014 21:08:11 +0100

Quoted 14 lines · David Miller
Quoted 7 lines

From: Hannes Frederic Sowa <[email protected]>
Date: Mon, 17 Feb 2014 14:03:53 +0100

Quoted 2 lines

We still allocate dgram packets with sock_alloc_send_pskb, which now does normally not block if the socket has too many packets in flight.

It seems like it does to me, it does sock_wait_for_wmem(), right?

We still do the sock_wait_for_wmem in sock_alloc_send_pskb, correct, but we rarly block there, maybe in highly concurrent cases with big payloads where one process got interrupted to account the memory to the receiving socket.

Quoted 4 lines
Quoted 2 lines

Or are you trying to say that usually this is not the point at which we block, but rather it's when we check the peer's receive queue?

Exactly.

So my only major objection is that we're now going to incur two atomics on every single datagram UNIX packet sent between two peers.

I think you can safely put the 'other' sock pointer into the control block. Because when we disassociate from a peer any pending packets in flight to him will be consumed/dropped, so there can't be any lingering references, right?

#6 lore raw
Quoted 17 lines · David Miller

From: Hannes Frederic Sowa <[email protected]>
Date: Wed, 19 Feb 2014 21:08:11 +0100

Quoted 14 lines · David Miller
Quoted 7 lines

From: Hannes Frederic Sowa <[email protected]>
Date: Mon, 17 Feb 2014 14:03:53 +0100

Quoted 2 lines

We still allocate dgram packets with sock_alloc_send_pskb, which now does normally not block if the socket has too many packets in flight.

It seems like it does to me, it does sock_wait_for_wmem(), right?

We still do the sock_wait_for_wmem in sock_alloc_send_pskb, correct, but we rarly block there, maybe in highly concurrent cases with big payloads where one process got interrupted to account the memory to the receiving socket.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Or are you trying to say that usually this is not the point at which we block, but rather it's when we check the peer's receive queue?

Exactly.

So my only major objection is that we're now going to incur two atomics on every single datagram UNIX packet sent between two peers.

Ok, I see. Daniel Borkmann pointed me to hackbench off-list and I will do some before/after benchmarks.

Quoted 4 lines

I think you can safely put the 'other' sock pointer into the control block. Because when we disassociate from a peer any pending packets in flight to him will be consumed/dropped, so there can't be any lingering references, right?

I am afraid lingering socket references will happen if the sockets are used in unconnected mode.

Greetings,

Hannes

maintainer
#7 lore raw

From: Hannes Frederic Sowa <[email protected]>
Date: Wed, 26 Feb 2014 21:21:54 +0100

Quoted 8 lines · David Miller
Quoted 4 lines

I think you can safely put the 'other' sock pointer into the control block. Because when we disassociate from a peer any pending packets in flight to him will be consumed/dropped, so there can't be any lingering references, right?

I am afraid lingering socket references will happen if the sockets are used in unconnected mode.

Sigh...

Ok, let's wait for your hackbench results and work from there.

#8 lore raw
Quoted 9 lines · David Miller

From: Hannes Frederic Sowa <[email protected]>
Date: Wed, 26 Feb 2014 21:21:54 +0100

Quoted 6 lines · David Miller
Quoted 4 lines

I think you can safely put the 'other' sock pointer into the control block. Because when we disassociate from a peer any pending packets in flight to him will be consumed/dropped, so there can't be any lingering references, right?

Quoted 6 lines
Quoted 2 lines

I am afraid lingering socket references will happen if the sockets are used in unconnected mode.

Sigh...

Ok, let's wait for your hackbench results and work from there.

Just a small followup:

Because of the additional sock_wfree during skb handover in unix_dgram_sendmsg the spinlock in __wake_up_sync_key is hit more often thus more cacheline bouncing.

Deferring the POLLOUT notification after the sock_rfree in unix_dgram_recvmsg already halfed the number of context switches with the new patch.

I try to play with some other ideas this week and will submit new patch with performance numbers.

Haven't noticed any problems with the additional atomic operations performance-wise so far.

Bye,

Hannes

maintainer
#9 lore raw

From: Hannes Frederic Sowa <[email protected]>
Date: Tue, 4 Mar 2014 21:13:36 +0100

Quoted 6 lines

Just a small followup:

Because of the additional sock_wfree during skb handover in unix_dgram_sendmsg the spinlock in __wake_up_sync_key is hit more often thus more cacheline bouncing.

Quoted 8 lines

Deferring the POLLOUT notification after the sock_rfree in unix_dgram_recvmsg already halfed the number of context switches with the new patch.

I try to play with some other ideas this week and will submit new patch with performance numbers.

Haven't noticed any problems with the additional atomic operations performance-wise so far.

Ok, thanks for the update.

#10 lore raw
Quoted 12 lines · David Miller

From: Hannes Frederic Sowa <[email protected]>
Date: Tue, 4 Mar 2014 21:13:36 +0100

Quoted 9 lines

Just a small followup:

Because of the additional sock_wfree during skb handover in unix_dgram_sendmsg the spinlock in __wake_up_sync_key is hit more often thus more cacheline bouncing.

Deferring the POLLOUT notification after the sock_rfree in unix_dgram_recvmsg already halfed the number of context switches with the new patch.

Quoted 7 lines
Quoted 5 lines

I try to play with some other ideas this week and will submit new patch with performance numbers.

Haven't noticed any problems with the additional atomic operations performance-wise so far.

Ok, thanks for the update.

I need to have some feedback regarding backwards compatibility, so still no final patch:

First the unconnected send/reply benchmark, which look good.

/home/hannes/netperf-2.6.0/src/netperf -t DG_RR -- -r 1,1

=== Unpatched net-next: ===
DG REQUEST/RESPONSE TEST
Local /Remote

Socket Size   Request  Resp.   Elapsed  Trans.
Send   Recv   Size     Size    Time     Rate
bytes  Bytes  bytes    bytes   secs.    per sec

212992 212992 1 1 10.00 23587.28
4608 2304

=== Patched net-next: ===
DG REQUEST/RESPONSE TEST
Local /Remote

Socket Size   Request  Resp.   Elapsed  Trans.
Send   Recv   Size     Size    Time     Rate
bytes  Bytes  bytes    bytes   secs.    per sec

212992 212992 1 1 10.00 21607.50
4608 2304

I guess it doesn't matter much. As soon as I start profiling the difference vanishes even more. perf diff shows now difference in unix_dgram_sendmsg.

I couldn't do the parameterless netperf DG_STREAM benchmarks, because netserver unconditionally sets SO_RCVBUF to 0 and such the clamps this value to SOCK_MIN_RCVBUF. The sender cannot send netperf's normal packet size through the socket. In case there are other applications out there, are we allowed to break them?

Otherwise benchmark results for stream tests don't look that good because of rescheduling from sock_wfree and I didn't find an easy way to reduce them besides playing dirty tricks with SOCK_USE_WRITE_QUEUE for which I could not come up with a proper and race-free idea.

/home/hannes/netperf-2.6.0/src/netperf -t DG_STREAM -- -m 1024

=== Unpatched net-next: ===
DG UNIDIRECTIONAL SEND TEST

Socket  Message  Elapsed      Messages
Size    Size     Time         Okay Errors   Throughput
bytes   bytes    secs            #      #   10^6bits/sec

212992 1024 10.02 963846 0 787.68
2304 10.02 963846 787.68

=== Patched net-next: ===
DG UNIDIRECTIONAL SEND TEST

Socket  Message  Elapsed      Messages
Size    Size     Time         Okay Errors   Throughput
bytes   bytes    secs            #      #   10^6bits/sec

212992 1024 10.00 347507 0 284.68
2304 10.00 347507 284.68

The important question for me is if we can assume applications already setting SO_RCVBUF to some minimal value and because of this not receiving packets being buggy and ignore them or do we need to introduce some way around this?

Greetings,

Hannes

maintainer
#11 lore raw

From: Hannes Frederic Sowa <[email protected]>
Date: Mon, 10 Mar 2014 02:07:54 +0100

Quoted 41 lines · David Miller
Quoted 12 lines

From: Hannes Frederic Sowa <[email protected]>
Date: Tue, 4 Mar 2014 21:13:36 +0100

Quoted 9 lines

Just a small followup:

Because of the additional sock_wfree during skb handover in unix_dgram_sendmsg the spinlock in __wake_up_sync_key is hit more often thus more cacheline bouncing.

Deferring the POLLOUT notification after the sock_rfree in unix_dgram_recvmsg already halfed the number of context switches with the new patch.

Quoted 7 lines
Quoted 5 lines

I try to play with some other ideas this week and will submit new patch with performance numbers.

Haven't noticed any problems with the additional atomic operations performance-wise so far.

Ok, thanks for the update.

I need to have some feedback regarding backwards compatibility, so still no final patch:

First the unconnected send/reply benchmark, which look good.

/home/hannes/netperf-2.6.0/src/netperf -t DG_RR -- -r 1,1

=== Unpatched net-next: ===
DG REQUEST/RESPONSE TEST
Local /Remote

Socket Size   Request  Resp.   Elapsed  Trans.
Send   Recv   Size     Size    Time     Rate
bytes  Bytes  bytes    bytes   secs.    per sec

212992 212992 1 1 10.00 23587.28
4608 2304

=== Patched net-next: ===
DG REQUEST/RESPONSE TEST
Local /Remote

Quoted 6 lines
Socket Size   Request  Resp.   Elapsed  Trans.
Send   Recv   Size     Size    Time     Rate
bytes  Bytes  bytes    bytes   secs.    per sec

212992 212992 1 1 10.00 21607.50
4608 2304

If I read those transaction rate numbers correctly, it's slowing down by more then %8. I'm not so sure that looks "good" to me.

Quoted 5 lines

I couldn't do the parameterless netperf DG_STREAM benchmarks, because netserver unconditionally sets SO_RCVBUF to 0 and such the clamps this value to SOCK_MIN_RCVBUF. The sender cannot send netperf's normal packet size through the socket. In case there are other applications out there, are we allowed to break them?

...

Quoted 4 lines

The important question for me is if we can assume applications already setting SO_RCVBUF to some minimal value and because of this not receiving packets being buggy and ignore them or do we need to introduce some way around this?

I think if it works currently, the risk of breaking a lot of things is simply too high to change this.

#12 lore raw

Hi!

Quoted 21 lines · David Miller
Quoted 21 lines

First the unconnected send/reply benchmark, which look good.

/home/hannes/netperf-2.6.0/src/netperf -t DG_RR -- -r 1,1

=== Unpatched net-next: ===
DG REQUEST/RESPONSE TEST
Local /Remote

Socket Size   Request  Resp.   Elapsed  Trans.
Send   Recv   Size     Size    Time     Rate
bytes  Bytes  bytes    bytes   secs.    per sec

212992 212992 1 1 10.00 23587.28
4608 2304

=== Patched net-next: ===
DG REQUEST/RESPONSE TEST
Local /Remote

Socket Size   Request  Resp.   Elapsed  Trans.
Send   Recv   Size     Size    Time     Rate
bytes  Bytes  bytes    bytes   secs.    per sec
Quoted 5 lines
Quoted 2 lines

212992 212992 1 1 10.00 21607.50
4608 2304

If I read those transaction rate numbers correctly, it's slowing down by more then %8. I'm not so sure that looks "good" to me.

You haven't seen the other benchmarks before I fixed the wakeup. ;)

(I should really have done more benchmarks before, I only was concerened about the "correctness" issue.)

Ok, sure, a performance drop is clearly visible, I have to admit, even though I don't know if lot's of people use this style of communication.

Quoted 5 lines
Quoted 5 lines

I couldn't do the parameterless netperf DG_STREAM benchmarks, because netserver unconditionally sets SO_RCVBUF to 0 and such the clamps this value to SOCK_MIN_RCVBUF. The sender cannot send netperf's normal packet size through the socket. In case there are other applications out there, are we allowed to break them?

Quoted 8 lines

...

Quoted 4 lines

The important question for me is if we can assume applications already setting SO_RCVBUF to some minimal value and because of this not receiving packets being buggy and ignore them or do we need to introduce some way around this?

I think if it works currently, the risk of breaking a lot of things is simply too high to change this.

Yes, it currently works.

I have to think more about this, but the only solutions which I came up with is adding more special cases for connected dgram sockets etc. and that cannot be the solution either... (especially because even connected sockets can communicate with other sockets if msg->msg_name is specified).

Thanks,

Hannes