Skip to contentSkip to conversation

net/core/sock.c remove extra wakeup

PATCH Gregory Haskins 3 participants 5 messages May 30, 2008 – Jun 16, 2008

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

Sorry for the top post (on a phone)

Point taken that the fix needs to be analysed for races, but the problem is very real. We noticed that our udp netperf test would get two wake-ups per packet. I don't remember the exact path where the udp waits occur off the top of my head, but the fundamental problem is the overloaded use of the single wait-queue.

So basically it was waiting for a packet to arrive, and the net-rx softirq would first trigger a NOSPACE wakeup (of which was a noop by the udp-rx code) followed by an rx-wakeup. So the netperf thread woke twice for one packet. Fixing this boosted performance by a large margin (don't recall exact figures off the top of my head....somewhere around 20%)

Long stort short, its potentially worth fixing this, even if this patch isn't "it" :)

HTH

Regards
-Greg

-----Original Message-----
From: David Miller <[email protected]>
To: Patrick Mullaney <[email protected]>
Cc: Gregory Haskins <[email protected]>
Cc: <[email protected]>

Sent: 5/30/2008 3:52:54 AM
Subject: Re: [PATCH] net/core/sock.c remove extra wakeup

From: "Patrick Mullaney" <[email protected]>
Date: Thu, 29 May 2008 10:08:43 -0600

Quoted 7 lines

We have noticed an extra wakeup of a task waiting on a udp receive while testing with CONFIG_PREEMPT_RT. The following patch eliminates the source of the extra wakeup by only waking a task if it is waiting on the writable condition(the SOCK_NOSPACE bit has been set). The patch is against 2.6.25.

This looks Ok on the surface, but I'm really worried about races.

If you clear that bit, another thread which set that bit and already checked the socket space, might sleep and never wake up.

I think that is fundamentally why we don't try to optimize this in that way.

And why is this so expensive? Once the first wakeup occurs, the subsequent attemps will merely see that the wait queue is empty and do nothing.

The only cost is the read lock on the callback lock, and that is about as expensive as this new atomic operation you are adding to clear the SOCK_NOSPACE bit.

So the benefit is not clear, and neither is the correctness of this change.

Sorry.

#1 lore raw
Quoted 2 lines · Gregory Haskins

So basically it was waiting for a packet to arrive, and the net-rx softirq would first trigger a NOSPACE wakeup (of which was a noop by the udp-rx code) followed by an rx-wakeup. So the netperf thread woke twice for one packet. Fixing this boosted performance by a large margin (don't recall exact figures off the top of my head....somewhere around 20%)

Please wrap your lines.

Anyway, you've lost me with this scenario. The patch is stopping wake-ups on the write path yet you're talking about the read path. So my question is why are you getting the extra wake-up on that write path when you receive a packet?

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
maintainer
#2 lore raw

Killing sk->sk_callback_lock (was Re: [PATCH] net/core/sock.c remove extra wakeup)

From: Herbert Xu <[email protected]>
Date: Wed, 11 Jun 2008 16:46:12 +1000

Quoted 4 lines

Anyway, you've lost me with this scenario. The patch is stopping wake-ups on the write path yet you're talking about the read path. So my question is why are you getting the extra wake-up on that write path when you receive a packet?

During a short discussion on IRC with Herbert I explained that indeed it's the write wakeup path that's causing the issue here and we both agreed that the cost is likely simply from the sk->sk_callback_lock and not the wakeups themselves. Once the first wakeup happens, the wait queues should be inactive and the task awake already.

The sk->sk_callback_lock is a pile of mud that has existed since around 1999 when we initially SMP threaded the networking stack. It's long overdue to kill this thing, because for the most part it is superfluous.

So I just sat for a bit and compiled some notes about the situation and how we can move forward on this. If I get some energy I will attack this task some time soon:

--------------------

Deletion of sk->sk_callback_lock.

This lock is overloaded tremendously to protect several aspects of socket state, most of which is superfluous and can be eliminated.

One aspect protected by this lock is the parent socket to child sock relationship. It makes sure that both the waitqueue and socket pointer are updated atomically wrt. threads executing the sock callbacks such as sk->sk_write_space().

A seperate lock was needed for this because socket callbacks can be invoked asynchronously in softirq context via kfree_skb() and therefore the socket lock would not be suitable for this purpose.

Basically, for this usage, all the callback cares about is that the sk->sk_socket and sk->sk_sleep point to stable items. Fortunately we can ensure this by simply never letting these two things point to NULL. Instead we can point them at dummy waitqueue and socket objects when there is no real parent currently associated with the sock.

This would allow large amounts of callback code to run lockless. For example, sock_def_write_space() would most not need the sk_callback_lock held. The exception being sk_wake_async() processing.

sk_wake_async() uses the callback lock to guard the state of the socket's fasync list. It is taken as a reader for scanners, and taken as a writer for thread paths which add entries to the fasync list.

These locking schemes date back to 1999 as indicated by comments above sock_fasync().

To be honest, fasync usage is rare. It's a way to register processes which will receive SIGIO signals during IO events. The generic fasync code fs/fcntl.c:kill_fasync() uses a global lock for list protection and nobody has noticed. But we should be careful for this observation because the other uses of fasync handling are devices like input (keyboards, mice) and TTY devices.

If the "fasync on sockets is rare" observation is accurate, we could simply use fasync_helper() and kill_fasync() to implement the necessary locking.

Also, fasync is how SIGURG is implemented, again another rarely used feature of sockets.

The next layer of (mis-)use of the sk_callback_lock are mis-level protocol layers that sit on top of real protocol stacks. For example sunrpc and iscsi. These subsystems want to override the socket callbacks so that they can do data-ready etc. processing directly or with modified behavior.

Typically these code bases stack away the callbacks they override so that when they are done with the socket they restore those pointers. During this "callback pointer swap" they hold the sk_callback_lock, which on the surface seems pretty pointless since this does not protect other threads of execution from invoking these callbacks.

SUNRPC also seems to use the sk->sk_callback_lock to stabilize the state of the sk->sk_user_data pointer in the XPRT socket layer. It also has an xprt->shutdown state which is tested additionally to the NULL'ness of the sk->sk_user_data XPRT pointer. A seperate xprt->transport_lock synchornizes the processing of incoming packets for a given XID session, so the sk_callback_lock isn't needed for that purpose.

sock_orphan() and sock_graft(), in include/net/sock.h, are the main transition points of the parent socket relationship state. Unfortunately, not all protocols use these helper routines consistently. In particular the radio protocols (ax25, etc.), IPX, ECONET, and some others do this work by hand and in particular without holding the sk->sk_callback_lock.

This should be consolidated so that the protocols do this stuff consistently. It also points us to the crummy SOCK_DEAD state bit we have. Really, all of the things guarded by SOCK_DEAD are totally harmless if we point sk->sk_socket and sk->sk_sleep to dummy objects instead of NULL. So, there is strong evidence that SOCK_DEAD could be eliminated.

Also, this lack of consistent usage of sock_orphan() and sock_graft() means that some protocols are not invoking security_sock_graft() properly.

So likely the first task in all of this is to get the protocol to use sock_orphan() and sock_graft() across the board without any exceptions.

At this point we can build dummy socket and wait queue objects that sock allocation and sock_orphan() can point to. I believe at this point that sk_callback_lock acquisition can be removed from sock_orphan() and sock_graft().

Next, we convert the fasync bits to use the global fasync_lock via the helper functions fasync_helper() and kill_fasync() in fs/fcntl.c instead of the per-socket locking, by-hand list insertion, and __kill_fasync() invocations the socket layer uses currently.

Now, it should be possible to entirely remove the sk_callback_lock grabs in the sock callbacks.

The rest of the work is composed of walking over the socket wrapping layers such as SUNRPC, OCFS, and ISCSI and auditing their sk_callback_lock usage, trying to eliminate the lock as much as possible. The unconvertable parts are probably replacable with subsystem local object locks.

It should be possible to delete sk->sk_callback_lock at that point. Killing SOCK_DEAD could be a followon task.

#3 lore raw

Re: Killing sk->sk_callback_lock (was Re: [PATCH] net/core/sock.c remove extra wakeup)

Quoted 2 lines · David Miller

Deletion of sk->sk_callback_lock.

Looks pretty reasonable. Thanks!

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
#4 lore raw
In reply to Herbert Xu
Quoted 1 line
Quoted 1 line
Quoted 1 line

On Wed, Jun 11, 2008 at 2:46 AM, in message

<[email protected]>, Herbert Xu

Quoted 9 lines · <[email protected]> wrote:
Quoted 2 lines · Gregory Haskins

So basically it was waiting for a packet to arrive, and the net-rx softirq

would first trigger a NOSPACE wakeup (of which was a noop by the udp-rx code) followed by an rx-wakeup. So the netperf thread woke twice for one packet. Fixing this boosted performance by a large margin (don't recall exact figures off the top of my head....somewhere around 20%)

Please wrap your lines.

Apologies. I don't think I have control over it in my corporate mailer environment :( but if I can figure out a way to do it I will make this change. You are not the first to complain ;)

Quoted 5 lines

Anyway, you've lost me with this scenario. The patch is stopping wake-ups on the write path yet you're talking about the read path. So my question is why are you getting the extra wake-up on that write path when you receive a packet?

Heh..you tell me :)

I don't recall the specifics off the top of my head, but IIRC it had to do with the fact that we were running a tx/rx netperf test and the way tx-completions are handled in the stack w.r.t. the softirqs.

The tx-completions code is executing inside the softirq-net-rx context and is freeing up the transmitted skbs. The problem is that it is blindly signaling the wait-queue that there was now SPACE (even though no-one cared about that event at the moment...the only waiter was waiting on an rx event).

So one way to look at it is the problem is that the single wait-queue serves both rx-wakeup, and tx-nospace events. Rather than open the can of worms of splitting the wait-queues into finer granularity, Pat chose to at least make the overloaded use of the single wait-queue more intelligent to know if anyone cared about the NOSPACE event or not.

Quoted 1 line

From my perspective, I don't care how the issue is specifically solved...I would ultimately just like to see this wasted wake-up "go away" one way or the other. ;) It significantly degrades performance (at least in this synthetic test) and as far as I can tell there is no reason to structure the code this way. If we are mistaken in that assessment, please let me know.

If we can offer any more details (such as the specific codepath that triggers the NOSPACE wakeup, for instance) we would be happy to oblige. Long story short this is easily repeatable.

Regards,
-Greg