Skip to contentSkip to conversation

net: rename low latency sockets functions to busy poll

PATCH In review Eliezer Tamir 6 participants 15 messages Jul 8, 2013 – Jul 10, 2013

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

PATCH 1/1 net: rename low latency sockets functions to busy poll

Rename functions in include/net/ll_poll.h to busy wait. Clarify documentation about expected power use increase. Rename POLL_LL to POLL_BUSY_LOOP.
Add need_resched() testing to poll/select busy loops.

Note, that in select and poll can_busy_poll is dynamic and is updated continuously to reflect the existence of supported sockets with valid queue information.

---

 Documentation/sysctl/net.txt    |   12 +++++---
 fs/select.c                     |   60 ++++++++++++++++++++++++---------------
 include/net/ll_poll.h           |   46 ++++++++++++++++--------------
 include/uapi/asm-generic/poll.h |    2 +
 net/core/datagram.c             |    3 +-
 net/ipv4/tcp.c                  |    6 ++--
 net/socket.c                    |   12 ++++----
 7 files changed, 80 insertions(+), 61 deletions(-)
Documentation/sysctl/net.txt +7 -5
index e658bbf..7323b88 100644
@@ -53,22 +53,24 @@ Default: 64
 low_latency_read
 ----------------
 Low latency busy poll timeout for socket reads. (needs CONFIG_NET_LL_RX_POLL)
-Approximate time in us to spin waiting for packets on the device queue.
+Approximate time in us to busy loop waiting for packets on the device queue.
 This sets the default value of the SO_LL socket option.
-Can be set or overridden per socket by setting socket option SO_LL.
-Recommended value is 50. May increase power usage.
+Can be set or overridden per socket by setting socket option SO_LL, which is
+the preferred method of enabling.
+If you need to enable the feature globally via sysctl, a value of 50 is recommended.
+Will increase power usage.
 Default: 0 (off)
 
 low_latency_poll
 ----------------
 Low latency busy poll timeout for poll and select. (needs CONFIG_NET_LL_RX_POLL)
-Approximate time in us to spin waiting for packets on the device queue.
+Approximate time in us to busy loop waiting for events.
 Recommended value depends on the number of sockets you poll on.
 For several sockets 50, for several hundreds 100.
 For more than that you probably want to use epoll.
 Note that only sockets with SO_LL set will be busy polled, so you want to either
 selectively set SO_LL on those sockets or set sysctl.net.low_latency_read globally.
-May increase power usage.
+Will increase power usage.
 Default: 0 (off)
 
 rmem_default
fs/select.c +37 -23

130 lines not on this page · raw message

include/net/ll_poll.h +24 -22

132 lines not on this page · raw message

include/uapi/asm-generic/poll.h +1 -1
index 4aee586..a969498 100644
@@ -30,7 +30,7 @@
 
 #define POLLFREE	0x4000	/* currently only for epoll */
 
-#define POLL_LL		0x8000
+#define POLL_BUSY_LOOP	0x8000
 
 struct pollfd {
 	int fd;
net/core/datagram.c +2 -1
index 9cbaba9..6e9ab31 100644
@@ -208,7 +208,8 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
 		}
 		spin_unlock_irqrestore(&queue->lock, cpu_flags);
 
-		if (sk_valid_ll(sk) && sk_poll_ll(sk, flags & MSG_DONTWAIT))
+		if (sk_can_busy_loop(sk) &&
+		    sk_busy_loop(sk, flags & MSG_DONTWAIT))
 			continue;
 
 		/* User doesn't want to wait */
net/ipv4/tcp.c +3 -3
index 46ed9af..15cbfa9 100644
@@ -1554,9 +1554,9 @@ int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
 	struct sk_buff *skb;
 	u32 urg_hole = 0;
 
-	if (sk_valid_ll(sk) && skb_queue_empty(&sk->sk_receive_queue)
-	    && (sk->sk_state == TCP_ESTABLISHED))
-		sk_poll_ll(sk, nonblock);
+	if (sk_can_busy_loop(sk) && skb_queue_empty(&sk->sk_receive_queue) &&
+	    (sk->sk_state == TCP_ESTABLISHED))
+		sk_busy_loop(sk, nonblock);
 
 	lock_sock(sk);
 
net/socket.c +6 -6
index 4da14cb..45afa64 100644
@@ -1148,7 +1148,7 @@ EXPORT_SYMBOL(sock_create_lite);
 /* No kernel lock held - perfect */
 static unsigned int sock_poll(struct file *file, poll_table *wait)
 {
-	unsigned int ll_flag = 0;
+	unsigned int busy_flag = 0;
 	struct socket *sock;
 
 	/*
@@ -1156,16 +1156,16 @@ static unsigned int sock_poll(struct file *file, poll_table *wait)
 	 */
 	sock = file->private_data;
 
-	if (sk_valid_ll(sock->sk)) {
+	if (sk_can_busy_loop(sock->sk)) {
 		/* this socket can poll_ll so tell the system call */
-		ll_flag = POLL_LL;
+		busy_flag = POLL_BUSY_LOOP;
 
 		/* once, only if requested by syscall */
-		if (wait && (wait->_key & POLL_LL))
-			sk_poll_ll(sock->sk, 1);
+		if (wait && (wait->_key & POLL_BUSY_LOOP))
+			sk_busy_loop(sock->sk, 1);
 	}
 
-	return ll_flag | sock->ops->poll(file, sock, wait);
+	return busy_flag | sock->ops->poll(file, sock, wait);
 }
 
 static int sock_mmap(struct file *file, struct vm_area_struct *vma)
#1 lore raw

fs/select.c int do_select(int n, fd_set_bits *fds, struct timespec *end_time) · line 511

Quoted 7 lines · Eliezer Tamir
fs/select.c +3 -2
-               /* only if on, have sockets with POLL_LL and not out of time */
-               if (ll_flag && can_ll && can_poll_ll(ll_start, ll_time))
+               /* only if found POLL_BUSY_LOOP sockets && not out of time */
+               if (!need_resched() && can_busy_loop &&
+                   busy_loop_range(busy_start, busy_end))
                        continue;

Better, but still horribly ugly. I also suspect the need_resched() test should be done after testing can_busy_loop, just in case the compiler can avoid having to load things off the current pointer.

I also think that we should clear busy_flag if we don't continue, no?

I also don't understand why the code keeps both busy_start and busy_end around. It all looks completely pointless. Why have two variables, and why make the comparisons more complicated, and the code look more complex than it actually is?

So the code *should* just do

        if (need_busy_loop) {
                if (!need_resched() && !busy_loop_timeout(start_time))
                        continue;
        }
        busy_flag = 0;

or something like that. Then, calculate busy_end there, since it's just an addition. Keeping two 64-bit variables around not only makes the code more complex, it generates worse code.

I also suspect there's a lot of other micro-optimizations that could be done: while keeping *two* 64-bit timeouts is just completely insane on a 32-bit architecture, keeping even just one is debatable. I suspect the timeouts should be just "unsigned long", so that 32-bit architectures don't bother with unnecessary 64-bit clocks. 32-bit clocks are several seconds even if you count nanoseconds, and you actually only do microseconds anyway (so instead of shifting the microseconds left by ten like you do, shift the nanoseconds of sched_clock right by ten, and now you only need 32-bit values).

select() and poll() performance is actually fairly critical, so trying to avoid bad code generation here is likely worth it.

Linus

#2 lore raw
Quoted 10 lines · Linus Torvalds

fs/select.c int do_select(int n, fd_set_bits *fds, struct timespec *end_time) · line 511

Quoted 7 lines · Eliezer Tamir
fs/select.c +3 -2
-               /* only if on, have sockets with POLL_LL and not out of time */
-               if (ll_flag && can_ll && can_poll_ll(ll_start, ll_time))
+               /* only if found POLL_BUSY_LOOP sockets && not out of time */
+               if (!need_resched() && can_busy_loop &&
+                   busy_loop_range(busy_start, busy_end))
                        continue;
Quoted 3 lines

Better, but still horribly ugly. I also suspect the need_resched() test should be done after testing can_busy_loop, just in case the compiler can avoid having to load things off the current pointer.

I think there is no way for the compiler to know the value of can_busy_loop at compile time. It depends on the replies we get from polling the sockets. ll_flag was there to make sure the compiler will know when things are defined out.

I would be very happy to hear suggestions for better names for things.

Quoted 1 line

I also think that we should clear busy_flag if we don't continue, no?

I'm not sure. If the time the user specified to busy-poll is not over, and the reason we didn't do it last time was that the sockets did not report that they have valid polling information (perhaps a route changed or a device we reset), but how we do have sockets that can busy-poll, wouldn't polling be the right thing to do?

Quoted 4 lines

I also don't understand why the code keeps both busy_start and busy_end around. It all looks completely pointless. Why have two variables, and why make the comparisons more complicated, and the code look more complex than it actually is?

Originally the code used time_after() and only kept the start time. People on the list voiced concerns that using sched_clock() might be risky since we may end up on another CPU with a different time.

We used sched_clock() because we don't need the time to be very accurate, this is only a cut-off time to make sure we never spin forever when no event ever happens.

I then changed this to use time_in_range() so that if we wake up with a completely random time, we will be out of the range and fail safely.

If you think that is wrong we can go back to use time_after().

Quoted 9 lines

I also suspect there's a lot of other micro-optimizations that could be done: while keeping *two* 64-bit timeouts is just completely insane on a 32-bit architecture, keeping even just one is debatable. I suspect the timeouts should be just "unsigned long", so that 32-bit architectures don't bother with unnecessary 64-bit clocks. 32-bit clocks are several seconds even if you count nanoseconds, and you actually only do microseconds anyway (so instead of shifting the microseconds left by ten like you do, shift the nanoseconds of sched_clock right by ten, and now you only need 32-bit values).

OK, but please answer my questions above, it is starting to be late here and I would really like to send a fix that everyone will find acceptable today.

Thanks,
Eliezer

#3 lore raw
Quoted 5 lines · Eliezer Tamir

I think there is no way for the compiler to know the value of can_busy_loop at compile time. It depends on the replies we get from polling the sockets. ll_flag was there to make sure the compiler will know when things are defined out.

No, my point was that we want to handle the easily seen register test first, and not even have to load current().

The compiler may end up scheduling the code to load current anyway, but the way you wrote it it's pretty much guaranteed that it will do it.

Quoted 7 lines
Quoted 1 line

I also think that we should clear busy_flag if we don't continue, no?

I'm not sure. If the time the user specified to busy-poll is not over, and the reason we didn't do it last time was that the sockets did not report that they have valid polling information (perhaps a route changed or a device we reset), but how we do have sockets that can busy-poll, wouldn't polling be the right thing to do?

Well, we would have slept already, and then microseconds will have passed. Also, if we schedule, we may overflow a 32-bit time which can screw up the later optimization:

Quoted 3 lines

Originally the code used time_after() and only kept the start time. People on the list voiced concerns that using sched_clock() might be risky since we may end up on another CPU with a different time.

No, I agree with the "don't call sched_clock() unless necessary".

It's the end_time I disagree with. There's no point. Just do the start-time (preferably as just a "unsigned long" in microseconds), and let it be.

In fact, I'd argue for initializing start_time to zero, and have the "have we timed out" logic load it only if necessary, rather than initializing it based on whether POLL_BUSY_WAIT was set or not. Because one common case - even with POLL_BUSY_WAIT - is that we go through the loop exactly once, and the data exists on the very first try. And that is in fact the case we want to optimize and not do any extra work for at all.

So I would actually argue that the whole timeout code might as well be something like

    unsigned long start_time = 0;
    ...
    if (want_busy_poll && !need_resched()) {
        unsigned long now = busy_poll_sched_clock();
        if (!start_time) {
            start_time = now + sysctl.busypoll;
            continue;
        }
        if (time_before(start_time, now))
            continue;
    }

or similar. Just one time variable, and it doesn't even need to be 64-bit on 32-bit architectures. Which makes all the code generation much simpler. And notice how this has basically no cost if we already found data, because we won't even get here. So there's only cost if we actually might want to busy-poll.

Quoted 3 lines

OK, but please answer my questions above, it is starting to be late here and I would really like to send a fix that everyone will find acceptable today.

I think it's getting closer, and I'm ok with the last final details being sorted out later. I just can't reasonably test any of my suggestions, so I'd like to get it to a point where when I pull, I don't feel like I'm pulling core code that I really detest. And the VFS layer in particular I'm pretty attached to.

Linus

#4 lore raw
Quoted 8 lines · Linus Torvalds
Quoted 5 lines · Eliezer Tamir

I think there is no way for the compiler to know the value of can_busy_loop at compile time. It depends on the replies we get from polling the sockets. ll_flag was there to make sure the compiler will know when things are defined out.

Quoted 6 lines

No, my point was that we want to handle the easily seen register test first, and not even have to load current().

The compiler may end up scheduling the code to load current anyway, but the way you wrote it it's pretty much guaranteed that it will do it.

I see. OK.

Quoted 11 lines

In fact, I'd argue for initializing start_time to zero, and have the "have we timed out" logic load it only if necessary, rather than initializing it based on whether POLL_BUSY_WAIT was set or not. Because one common case - even with POLL_BUSY_WAIT - is that we go through the loop exactly once, and the data exists on the very first try. And that is in fact the case we want to optimize and not do any extra work for at all.

So I would actually argue that the whole timeout code might as well be something like

Quoted 12 lines
    unsigned long start_time = 0;
    ...
    if (want_busy_poll && !need_resched()) {
        unsigned long now = busy_poll_sched_clock();
        if (!start_time) {
            start_time = now + sysctl.busypoll;
            continue;
        }
        if (time_before(start_time, now))
            continue;
    }

OK.

Thanks,
Eliezer

#5 lore raw
Quoted 42 lines · On Mon, 08 Jul 2013 22:46:04 +0300 Eliezer Tamir…
Quoted 8 lines · Linus Torvalds
Quoted 5 lines · Eliezer Tamir

I think there is no way for the compiler to know the value of can_busy_loop at compile time. It depends on the replies we get from polling the sockets. ll_flag was there to make sure the compiler will know when things are defined out.

Quoted 6 lines

No, my point was that we want to handle the easily seen register test first, and not even have to load current().

The compiler may end up scheduling the code to load current anyway, but the way you wrote it it's pretty much guaranteed that it will do it.

I see. OK.

Quoted 11 lines

In fact, I'd argue for initializing start_time to zero, and have the "have we timed out" logic load it only if necessary, rather than initializing it based on whether POLL_BUSY_WAIT was set or not. Because one common case - even with POLL_BUSY_WAIT - is that we go through the loop exactly once, and the data exists on the very first try. And that is in fact the case we want to optimize and not do any extra work for at all.

So I would actually argue that the whole timeout code might as well be something like

Quoted 12 lines
    unsigned long start_time = 0;
    ...
    if (want_busy_poll && !need_resched()) {
        unsigned long now = busy_poll_sched_clock();
        if (!start_time) {
            start_time = now + sysctl.busypoll;
            continue;
        }
        if (time_before(start_time, now))
            continue;
    }
Quoted 1 line

OK.

Since this is special case in the hot path, it looks like a good case for static branch logic.

#6 lore raw
In reply to Eliezer Tamir
Quoted 9 lines · On Mon, 08 Jul 2013 22:46:04 +0300 Eliezer Tamir…
Quoted 8 lines · Linus Torvalds
Quoted 5 lines · Eliezer Tamir

I think there is no way for the compiler to know the value of can_busy_loop at compile time. It depends on the replies we get from polling the sockets. ll_flag was there to make sure the compiler will know when things are defined out.

Quoted 33 lines
Quoted 6 lines

No, my point was that we want to handle the easily seen register test first, and not even have to load current().

The compiler may end up scheduling the code to load current anyway, but the way you wrote it it's pretty much guaranteed that it will do it.

I see. OK.

Quoted 23 lines

In fact, I'd argue for initializing start_time to zero, and have the "have we timed out" logic load it only if necessary, rather than initializing it based on whether POLL_BUSY_WAIT was set or not. Because one common case - even with POLL_BUSY_WAIT - is that we go through the loop exactly once, and the data exists on the very first try. And that is in fact the case we want to optimize and not do any extra work for at all.

So I would actually argue that the whole timeout code might as well be something like

    unsigned long start_time = 0;
    ...
    if (want_busy_poll && !need_resched()) {
        unsigned long now = busy_poll_sched_clock();
        if (!start_time) {
            start_time = now + sysctl.busypoll;
            continue;
        }
        if (time_before(start_time, now))
            continue;
    }

Since this code is in hot path, and a special case, looks like a good candidate for static branch.

#7 lore raw
Quoted 15 lines · Stephen Hemminger
Quoted 14 lines
Quoted 13 lines
    unsigned long start_time = 0;
    ...
    if (want_busy_poll && !need_resched()) {
        unsigned long now = busy_poll_sched_clock();
        if (!start_time) {
            start_time = now + sysctl.busypoll;
            continue;
        }
        if (time_before(start_time, now))
            continue;
    }
Quoted 2 lines

Since this code is in hot path, and a special case, looks like a good candidate for static branch.

No, it's not static. It's an initializer, yes, but it's not a static one, and it gets run on every single select()/poll(). Well, every single one that goes through the loop more than once.

So it's very much a dynamic condition. It's just placed that way to avoid doing the (relatively expensive) time read for the case where data is available immediately.

And the "data is available immediately" case is actually fairly common. Sometimes it's because the producer/network is faster than the consumer. And sometimes it's because you get big chunks at a time, but the reader/writer ends up always going through a select() loop without actually reading/writing everything in a loop until it gets a full/empty error.

And I bet that both of those cases happen even with programs/networks that end up using the magic low-latency/busyloop polling flags. So avoiding the timer read if at all possible is definitely a good idea.

Linus

maintainer
#8 lore raw
In reply to Linus Torvalds

From: Linus Torvalds <[email protected]>
Date: Mon, 8 Jul 2013 12:37:06 -0700

Quoted 5 lines

I think it's getting closer, and I'm ok with the last final details being sorted out later. I just can't reasonably test any of my suggestions, so I'd like to get it to a point where when I pull, I don't feel like I'm pulling core code that I really detest. And the VFS layer in particular I'm pretty attached to.

So I've applied Eliezer's rename patch and will send another pull request once he also takes care of the reduction of the number and size of the variables used to track the poll timestamps.

Thanks.

maintainer
#9 lore raw
In reply to Eliezer Tamir
Quoted 1 line · On Mon, 08 Jul 2013 16:20:34 +0300 Eliezer Tamir…

Rename POLL_LL to POLL_BUSY_LOOP.

So pardon me if I speak out of turn, but it occurs to me to wonder...should the SO_LL socket option be renamed in a similar fashion before this interface escapes into the wild?

Thanks,

jon

maintainer
#10 lore raw

From: Jonathan Corbet <[email protected]>
Date: Tue, 9 Jul 2013 16:25:14 -0600

Quoted 8 lines
Quoted 1 line · On Mon, 08 Jul 2013 16:20:34 +0300 Eliezer Tamir…

Rename POLL_LL to POLL_BUSY_LOOP.

So pardon me if I speak out of turn, but it occurs to me to wonder...should the SO_LL socket option be renamed in a similar fashion before this interface escapes into the wild?

Sure and we can rename include/net/ll_poll.h to something more fitting as well.

I'll make sure this happens before 3.11 gets even close to release.

#11 lore raw
Quoted 8 lines · David Miller

From: Jonathan Corbet <[email protected]>
Date: Tue, 9 Jul 2013 16:25:14 -0600

Quoted 5 lines
Quoted 1 line · On Mon, 08 Jul 2013 16:20:34 +0300 Eliezer Tamir…

Rename POLL_LL to POLL_BUSY_LOOP.

Quoted 8 lines
Quoted 3 lines

So pardon me if I speak out of turn, but it occurs to me to wonder...should the SO_LL socket option be renamed in a similar fashion before this interface escapes into the wild?

Sure and we can rename include/net/ll_poll.h to something more fitting as well.

I'll make sure this happens before 3.11 gets even close to release.

David,

If the following names changes are acceptable I will try to send out a patch today.

1. include/net/ll_poll.h -> include/net/busy_poll.h

2. ndo_ll_poll -> ndo_busy_poll

- not technically accurate since the ndo callback does not itself busy poll, it's just used to implement it.

maybe ndo_napi_id_poll? or ndo_id_poll? I don't really like any of them, so a suggestion would be nice.

3. sysctl_net_ll_{read,poll} -> sysctl_net_busy_{read,poll}
- along with matching file name changes.

4. {sk,skb}_mark_ll -> {sk,skb}_mark_napi_id

5. LL_SO -> BUSY_POLL_SO

Are you OK with the names?
Did I miss anything?

Thanks,
Eliezer

maintainer
#12 lore raw

From: Eliezer Tamir <[email protected]>
Date: Wed, 10 Jul 2013 06:29:16 +0300

Quoted 4 lines

If the following names changes are acceptable I will try to send out a patch today.

1. include/net/ll_poll.h -> include/net/busy_poll.h

Agreed.

Quoted 4 lines

2. ndo_ll_poll -> ndo_busy_poll

- not technically accurate since the ndo callback does not itself busy poll, it's just used to implement it.

I think this name change is accurate, it expresses the two elements of what it does. It's busy waiting, in that it's doing a synchronous scan of the device's RX queue, and it's polling just like NAPI polling does.

Quoted 2 lines

maybe ndo_napi_id_poll? or ndo_id_poll? I don't really like any of them, so a suggestion would be nice.

This would make it sound like it's some new version of the existing NAPI poll.

Well... what would be great would be to come up with some single interface that drivers can implement rather than having to have both napi->poll and netdevice_ops->ndo_ll_poll(). But that's a task for a later date.

Therefore, ndo_busy_poll is probably best for now.

Quoted 2 lines

3. sysctl_net_ll_{read,poll} -> sysctl_net_busy_{read,poll}
- along with matching file name changes.

Agreed.

Quoted 1 line

4. {sk,skb}_mark_ll -> {sk,skb}_mark_napi_id

Agreed.

Quoted 1 line

5. LL_SO -> BUSY_POLL_SO

Agreed.

Quoted 1 line

Did I miss anything?

Nope, looks complete. And also do the manpage update.

#13 lore raw
Quoted 8 lines · David Miller

From: Eliezer Tamir <[email protected]>
Date: Wed, 10 Jul 2013 06:29:16 +0300

Quoted 5 lines

If the following names changes are acceptable I will try to send out a patch today.

2. ndo_ll_poll -> ndo_busy_poll

Quoted 7 lines
Quoted 2 lines

- not technically accurate since the ndo callback does not itself busy poll, it's just used to implement it.

I think this name change is accurate, it expresses the two elements of what it does. It's busy waiting, in that it's doing a synchronous scan of the device's RX queue, and it's polling just like NAPI polling does.

OK

Quoted 6 lines

Well... what would be great would be to come up with some single interface that drivers can implement rather than having to have both napi->poll and netdevice_ops->ndo_ll_poll(). But that's a task for a later date.

Therefore, ndo_busy_poll is probably best for now.

I will think about this, maybe we could even unify ndo_poll_controller. It seems like said unified method would have to have an extra parameter that would indicate from which context it was called:
1. from napi poll (bh)
2. from poll controller (with interrupts disabled)
3. from busy poll (user context)
-of course not for today.

Quoted 1 line

Nope, looks complete. And also do the manpage update.

Where do I find the repository for the manpages?