Message ID | 20170323213742.12615.85793.stgit@localhost.localdomain |
---|---|
State | Superseded, archived |
Delegated to: | David Miller |
Headers | show |
On Thu, 2017-03-23 at 14:37 -0700, Alexander Duyck wrote: > From: Alexander Duyck <alexander.h.duyck@intel.com> > > The last bit I changed is to move from using a shift by 10 to just using > NSEC_PER_USEC and using multiplication for any run time calculations and > division for a few compile time ones. This should be more accurate and > perform about the same on most architectures since modern CPUs typically > handle multiplication without too much overhead. busy polling thread can be preempted for more than 2 seconds. Using usec instead of nanoseconds gave us 3 orders of magnitude cushion. We do not need nsec accuracy for busy polling users, if this restricts range and usability under stress.
On Thu, Mar 23, 2017 at 6:24 PM, Eric Dumazet <eric.dumazet@gmail.com> wrote: > On Thu, 2017-03-23 at 14:37 -0700, Alexander Duyck wrote: >> From: Alexander Duyck <alexander.h.duyck@intel.com> >> > >> The last bit I changed is to move from using a shift by 10 to just using >> NSEC_PER_USEC and using multiplication for any run time calculations and >> division for a few compile time ones. This should be more accurate and >> perform about the same on most architectures since modern CPUs typically >> handle multiplication without too much overhead. > > > busy polling thread can be preempted for more than 2 seconds. If it is preempted is the timing value even valid anymore? I was wondering about that. Also when preemption is enabled is there anything to prevent us from being migrated to a CPU? If so what do we do about architectures that allow drift between the clocks? > Using usec instead of nanoseconds gave us 3 orders of magnitude cushion. Yes, but the problem is we also opened up an issue where if the clock was approaching a roll-over we could add a value to it that would put us in a state where we would never time out. > We do not need nsec accuracy for busy polling users, if this restricts > range and usability under stress. Yes and no. So the standard use cases suggest using values of 50 to 100 microseconds. I suspect that for most people that is probably what they are using. The addition of preemption kind of threw a wrench in the works because now instead of spending that time busy polling you can get preempted and then are off doing something else for the entire period of time. What would you think of changing this so that instead of tracking the total time this function is active, instead we tracked the total time we spent with preemption disabled? What I would do is move the start_time configuration to just after the preempt_disable() call. Then if we end up yielding to another thread we would just reset the start_time when we restarted instead of trying to deal with all the extra clock nonsense that we would have to deal with otherwise since I don't know if we actually want to count time where we aren't actually doing anything. In addition this would bring us closer to how NAPI already works since it essentially will either find an event, or if we time out we hand it off to the softirq which in turn can handle it or hand it off to softirqd. The only item that might be a bit more difficult to deal with then would be the way the times are used in fs/select.c but I don't know if that is really the right way to go anyway. With the preemption changes and such it might just make sense to drop those bits and rely on just the socket polling alone. The other option is to switch over everything from using unsigned long to using uint64_t and time_after64. Then we can guarantee the range needed and then some, but then we are playing with a u64 time value on 32b architectures which might be a bit more expensive. Even with that though I still need to clean up the sysctl since it doesn't make sense to allow negative values for the busy_poll usec to be used which is currently the case. Anyway let me know what you think and I can probably spin out a new set tomorrow. - Alex
On Thu, 2017-03-23 at 20:42 -0700, Alexander Duyck wrote: > On Thu, Mar 23, 2017 at 6:24 PM, Eric Dumazet <eric.dumazet@gmail.com> wrote: > > On Thu, 2017-03-23 at 14:37 -0700, Alexander Duyck wrote: > >> From: Alexander Duyck <alexander.h.duyck@intel.com> > >> > > > >> The last bit I changed is to move from using a shift by 10 to just using > >> NSEC_PER_USEC and using multiplication for any run time calculations and > >> division for a few compile time ones. This should be more accurate and > >> perform about the same on most architectures since modern CPUs typically > >> handle multiplication without too much overhead. > > > > > > busy polling thread can be preempted for more than 2 seconds. > > If it is preempted is the timing value even valid anymore? I was > wondering about that. Also when preemption is enabled is there > anything to prevent us from being migrated to a CPU? If so what do we > do about architectures that allow drift between the clocks? > > > Using usec instead of nanoseconds gave us 3 orders of magnitude cushion. > > Yes, but the problem is we also opened up an issue where if the clock > was approaching a roll-over we could add a value to it that would put > us in a state where we would never time out. If you believe there is a bug, send a fix for net tree ? I really do not see a bug, given we use time_after(now, end_time) which handles roll-over just fine. > > > We do not need nsec accuracy for busy polling users, if this restricts > > range and usability under stress. > > Yes and no. So the standard use cases suggest using values of 50 to > 100 microseconds. I suspect that for most people that is probably > what they are using. The addition of preemption kind of threw a > wrench in the works because now instead of spending that time busy > polling you can get preempted and then are off doing something else > for the entire period of time. That is fine. Busy polling heavy users are pinning threads to cpu, and the re-schedule check is basically a way to make sure ksoftirqd will get a chance to service BH. > > What would you think of changing this so that instead of tracking the > total time this function is active, instead we tracked the total time > we spent with preemption disabled? What I would do is move the > start_time configuration to just after the preempt_disable() call. > Then if we end up yielding to another thread we would just reset the > start_time when we restarted instead of trying to deal with all the > extra clock nonsense that we would have to deal with otherwise since I > don't know if we actually want to count time where we aren't actually > doing anything. In addition this would bring us closer to how NAPI > already works since it essentially will either find an event, or if we > time out we hand it off to the softirq which in turn can handle it or > hand it off to softirqd. The only item that might be a bit more > difficult to deal with then would be the way the times are used in > fs/select.c but I don't know if that is really the right way to go > anyway. With the preemption changes and such it might just make sense > to drop those bits and rely on just the socket polling alone. > > The other option is to switch over everything from using unsigned long > to using uint64_t and time_after64. Then we can guarantee the range > needed and then some, but then we are playing with a u64 time value on > 32b architectures which might be a bit more expensive. Even with that > though I still need to clean up the sysctl since it doesn't make sense > to allow negative values for the busy_poll usec to be used which is > currently the case. > > Anyway let me know what you think and I can probably spin out a new > set tomorrow. Leave usec resolution, I see no point switching to nsec, as long we support 32bit kernels. If you believe min/max values should be added to the sysctls, because we do not trust root anymore, please send patches only addressing that. Thanks.
On Thu, 2017-03-23 at 21:27 -0700, Eric Dumazet wrote: > If you believe min/max values should be added to the sysctls, because we > do not trust root anymore, please send patches only addressing that. extern unsigned int sysctl_net_busy_read; extern unsigned int sysctl_net_busy_poll; ... unsigned int sk_ll_usec; These are unsigned values, so no min/max would be required, only maybe use proc_douintvec() instead of proc_dointvec() as most sysctls use because we were lazy and had to wait linux-4.8 to actually get proc_douintvec() in the kernel ;)
On Thu, Mar 23, 2017 at 9:27 PM, Eric Dumazet <eric.dumazet@gmail.com> wrote: > On Thu, 2017-03-23 at 20:42 -0700, Alexander Duyck wrote: >> On Thu, Mar 23, 2017 at 6:24 PM, Eric Dumazet <eric.dumazet@gmail.com> wrote: >> > On Thu, 2017-03-23 at 14:37 -0700, Alexander Duyck wrote: >> >> From: Alexander Duyck <alexander.h.duyck@intel.com> >> >> >> > >> >> The last bit I changed is to move from using a shift by 10 to just using >> >> NSEC_PER_USEC and using multiplication for any run time calculations and >> >> division for a few compile time ones. This should be more accurate and >> >> perform about the same on most architectures since modern CPUs typically >> >> handle multiplication without too much overhead. >> > >> > >> > busy polling thread can be preempted for more than 2 seconds. >> >> If it is preempted is the timing value even valid anymore? I was >> wondering about that. Also when preemption is enabled is there >> anything to prevent us from being migrated to a CPU? If so what do we >> do about architectures that allow drift between the clocks? >> >> > Using usec instead of nanoseconds gave us 3 orders of magnitude cushion. >> >> Yes, but the problem is we also opened up an issue where if the clock >> was approaching a roll-over we could add a value to it that would put >> us in a state where we would never time out. > > If you believe there is a bug, send a fix for net tree ? > > I really do not see a bug, given we use time_after(now, end_time) which > handles roll-over just fine. Right, but time_after assumes roll over. When you are using a time value based off of local_clock() >> 10, you don't ever roll over when you do addition. Just the clock rolls over. At least on 64 bit systems. So if local time approaches something like all 1's, and we have shifted it by 10 it is then the max it can ever reach is 0x003FFFFFFFFFFFFF. I can add our loop time to that and it won't roll over. In the mean time the busy_loop_us_ can never exceed whatever I added to that so we are now locked into a loop. I realize I am probably being pedantic, and it will have an exceedingly small rate of occurrence, but it is still an issue. >> >> > We do not need nsec accuracy for busy polling users, if this restricts >> > range and usability under stress. >> >> Yes and no. So the standard use cases suggest using values of 50 to >> 100 microseconds. I suspect that for most people that is probably >> what they are using. The addition of preemption kind of threw a >> wrench in the works because now instead of spending that time busy >> polling you can get preempted and then are off doing something else >> for the entire period of time. > > That is fine. Busy polling heavy users are pinning threads to cpu, and > the re-schedule check is basically a way to make sure ksoftirqd will get > a chance to service BH. > >> >> What would you think of changing this so that instead of tracking the >> total time this function is active, instead we tracked the total time >> we spent with preemption disabled? What I would do is move the >> start_time configuration to just after the preempt_disable() call. >> Then if we end up yielding to another thread we would just reset the >> start_time when we restarted instead of trying to deal with all the >> extra clock nonsense that we would have to deal with otherwise since I >> don't know if we actually want to count time where we aren't actually >> doing anything. In addition this would bring us closer to how NAPI >> already works since it essentially will either find an event, or if we >> time out we hand it off to the softirq which in turn can handle it or >> hand it off to softirqd. The only item that might be a bit more >> difficult to deal with then would be the way the times are used in >> fs/select.c but I don't know if that is really the right way to go >> anyway. With the preemption changes and such it might just make sense >> to drop those bits and rely on just the socket polling alone. >> >> The other option is to switch over everything from using unsigned long >> to using uint64_t and time_after64. Then we can guarantee the range >> needed and then some, but then we are playing with a u64 time value on >> 32b architectures which might be a bit more expensive. Even with that >> though I still need to clean up the sysctl since it doesn't make sense >> to allow negative values for the busy_poll usec to be used which is >> currently the case. >> >> Anyway let me know what you think and I can probably spin out a new >> set tomorrow. > > > Leave usec resolution, I see no point switching to nsec, as long we > support 32bit kernels. I wonder how much it really matters in the grand scheme of things. The question I would have is which is more expensive. We are either having to do a set of double precision shifts, or a multiplication, double precision addition, and double precision compare. I need to take a look at some instruction tables to see which is more expensive. My preference would be to switch things over to a u64 at this point as it fixes the time_after problem, and has no additional cost on 64b and bumps up the maximum usable value on 32b systems by a power of 2, but I will have to think about it and see if there isn't a better way to deal with that issue. > If you believe min/max values should be added to the sysctls, because we > do not trust root anymore, please send patches only addressing that. It isn't so much a min/max as just the wrong function used. I will switch it over to using proc_douintvec(). Also it isn't that I don't trust root, I don't like us giving root the option of configuring things wrongly without giving them some sort of warning. So switching things over to using unsigned int will match what we are actually storing but the problem is anything over INT_MAX on 32 bit systems will give you busy polling with an immediate time out since the signed bit is what is being used in "time_after" to determine if we crossed the boundary or not. - Alex
On Thu, 2017-03-23 at 22:55 -0700, Alexander Duyck wrote: > Right, but time_after assumes roll over. When you are using a time > value based off of local_clock() >> 10, you don't ever roll over when > you do addition. Just the clock rolls over. At least on 64 bit > systems. > > So if local time approaches something like all 1's, and we have > shifted it by 10 it is then the max it can ever reach is > 0x003FFFFFFFFFFFFF. I can add our loop time to that and it won't roll > over. In the mean time the busy_loop_us_ can never exceed whatever I > added to that so we are now locked into a loop. I realize I am > probably being pedantic, and it will have an exceedingly small rate of > occurrence, but it is still an issue. Do you realize that a 64bit clock wont rollover before the host has reached 584 years of uptime ?
On Fri, Mar 24, 2017 at 4:16 AM, Eric Dumazet <eric.dumazet@gmail.com> wrote: > On Thu, 2017-03-23 at 22:55 -0700, Alexander Duyck wrote: > >> Right, but time_after assumes roll over. When you are using a time >> value based off of local_clock() >> 10, you don't ever roll over when >> you do addition. Just the clock rolls over. At least on 64 bit >> systems. >> >> So if local time approaches something like all 1's, and we have >> shifted it by 10 it is then the max it can ever reach is >> 0x003FFFFFFFFFFFFF. I can add our loop time to that and it won't roll >> over. In the mean time the busy_loop_us_ can never exceed whatever I >> added to that so we are now locked into a loop. I realize I am >> probably being pedantic, and it will have an exceedingly small rate of >> occurrence, but it is still an issue. > > Do you realize that a 64bit clock wont rollover before the host has > reached 584 years of uptime ? Yeah, that is what I meant by "probably being pedantic". I was being a too much of a perfectionist. So I can work with the ">> 10" approach. The only thing I think I may still want to change is that on 32b systems I will still use the do_procintvec_minmax for busy_poll and busy_read to prevent us from inputting values less than 0. For 64b systems we can do_procuintvec. It isn't so much that I don't trust root, it is just that we didn't really document the ranges anywhere for this so I figure if we at least lock that down to the usable ranges since root may not be aware of the implementation details.
diff --git a/fs/select.c b/fs/select.c index e2112270d75a..9287d3a96e35 100644 --- a/fs/select.c +++ b/fs/select.c @@ -409,7 +409,7 @@ int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time) int retval, i, timed_out = 0; u64 slack = 0; unsigned int busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0; - unsigned long busy_end = 0; + unsigned long busy_start = 0; rcu_read_lock(); retval = max_select_fd(n, fds); @@ -512,11 +512,11 @@ int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time) /* only if found POLL_BUSY_LOOP sockets && not out of time */ if (can_busy_loop && !need_resched()) { - if (!busy_end) { - busy_end = busy_loop_end_time(); + if (!busy_start) { + busy_start = busy_loop_current_time(); continue; } - if (!busy_loop_timeout(busy_end)) + if (!busy_loop_timeout(busy_start)) continue; } busy_flag = 0; @@ -800,7 +800,7 @@ static int do_poll(struct poll_list *list, struct poll_wqueues *wait, int timed_out = 0, count = 0; u64 slack = 0; unsigned int busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0; - unsigned long busy_end = 0; + unsigned long busy_start = 0; /* Optimise the no-wait case */ if (end_time && !end_time->tv_sec && !end_time->tv_nsec) { @@ -853,11 +853,11 @@ static int do_poll(struct poll_list *list, struct poll_wqueues *wait, /* only if found POLL_BUSY_LOOP sockets && not out of time */ if (can_busy_loop && !need_resched()) { - if (!busy_end) { - busy_end = busy_loop_end_time(); + if (!busy_start) { + busy_start = busy_loop_current_time(); continue; } - if (!busy_loop_timeout(busy_end)) + if (!busy_loop_timeout(busy_start)) continue; } busy_flag = 0; diff --git a/include/net/busy_poll.h b/include/net/busy_poll.h index c55760f4820f..4626cb22f625 100644 --- a/include/net/busy_poll.h +++ b/include/net/busy_poll.h @@ -41,67 +41,85 @@ */ #define MIN_NAPI_ID ((unsigned int)(NR_CPUS + 1)) +/* The timer checks are using time_after() to determine if we have + * exceeded out timeout period. In order to support that we have to limit + * ourselves to the smallest possible signed type and then in addition + * account for the fact that we are recording our value in microseconds + * instead of nanoseconds. + * + * This limit should be just a little over 2 seconds. + */ +#define MAX_BUSY_POLL (INT_MAX / NSEC_PER_USEC) + static inline bool net_busy_loop_on(void) { return sysctl_net_busy_poll; } -static inline u64 busy_loop_us_clock(void) +static inline bool sk_can_busy_loop(const struct sock *sk) { - return local_clock() >> 10; + return sk->sk_ll_usec && !signal_pending(current); } -static inline unsigned long sk_busy_loop_end_time(struct sock *sk) -{ - return busy_loop_us_clock() + ACCESS_ONCE(sk->sk_ll_usec); -} +void sk_busy_loop(struct sock *sk, int nonblock); -/* in poll/select we use the global sysctl_net_ll_poll value */ -static inline unsigned long busy_loop_end_time(void) +#else /* CONFIG_NET_RX_BUSY_POLL */ +static inline unsigned long net_busy_loop_on(void) { - return busy_loop_us_clock() + ACCESS_ONCE(sysctl_net_busy_poll); + return 0; } -static inline bool sk_can_busy_loop(const struct sock *sk) +static inline bool sk_can_busy_loop(struct sock *sk) { - return sk->sk_ll_usec && !signal_pending(current); + return false; } -static inline bool busy_loop_timeout(unsigned long end_time) +static inline void sk_busy_loop(struct sock *sk, int nonblock) { - unsigned long now = busy_loop_us_clock(); - - return time_after(now, end_time); } -void sk_busy_loop(struct sock *sk, int nonblock); +#endif /* CONFIG_NET_RX_BUSY_POLL */ -#else /* CONFIG_NET_RX_BUSY_POLL */ -static inline unsigned long net_busy_loop_on(void) +static inline unsigned long busy_loop_current_time(void) { +#ifdef CONFIG_NET_RX_BUSY_POLL + return (unsigned long)local_clock(); +#else return 0; +#endif } -static inline unsigned long busy_loop_end_time(void) +/* in poll/select we use the global sysctl_net_ll_poll value */ +static inline bool busy_loop_timeout(unsigned long start_time) { - return 0; -} +#ifdef CONFIG_NET_RX_BUSY_POLL + unsigned long bp_usec = READ_ONCE(sysctl_net_busy_poll); -static inline bool sk_can_busy_loop(struct sock *sk) -{ - return false; -} + if (bp_usec) { + unsigned long end_time = start_time + (bp_usec * NSEC_PER_USEC); + unsigned long now = busy_loop_current_time(); -static inline bool busy_loop_timeout(unsigned long end_time) -{ + return time_after(now, end_time); + } +#endif return true; } -static inline void sk_busy_loop(struct sock *sk, int nonblock) +static inline bool sk_busy_loop_timeout(struct sock *sk, + unsigned long start_time) { -} +#ifdef CONFIG_NET_RX_BUSY_POLL + unsigned long bp_usec = READ_ONCE(sk->sk_ll_usec); -#endif /* CONFIG_NET_RX_BUSY_POLL */ + if (bp_usec) { + unsigned long end_time = start_time + (bp_usec * NSEC_PER_USEC); + unsigned long now = busy_loop_current_time(); + + return time_after(now, end_time); + } +#endif + return true; +} /* used in the NIC receive handler to mark the skb */ static inline void skb_mark_napi_id(struct sk_buff *skb, diff --git a/net/core/dev.c b/net/core/dev.c index 6b0458b5afe0..73ebf2f5600e 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -5062,7 +5062,7 @@ static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock) void sk_busy_loop(struct sock *sk, int nonblock) { - unsigned long end_time = !nonblock ? sk_busy_loop_end_time(sk) : 0; + unsigned long start_time = nonblock ? 0 : busy_loop_current_time(); int (*napi_poll)(struct napi_struct *napi, int budget); void *have_poll_lock = NULL; struct napi_struct *napi; @@ -5112,7 +5112,7 @@ void sk_busy_loop(struct sock *sk, int nonblock) local_bh_enable(); if (nonblock || !skb_queue_empty(&sk->sk_receive_queue) || - busy_loop_timeout(end_time)) + sk_busy_loop_timeout(sk, start_time)) break; if (unlikely(need_resched())) { @@ -5122,7 +5122,7 @@ void sk_busy_loop(struct sock *sk, int nonblock) rcu_read_unlock(); cond_resched(); if (!skb_queue_empty(&sk->sk_receive_queue) || - busy_loop_timeout(end_time)) + sk_busy_loop_timeout(sk, start_time)) return; goto restart; } diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c index 4ead336e14ea..88417a0a179a 100644 --- a/net/core/sysctl_net_core.c +++ b/net/core/sysctl_net_core.c @@ -27,6 +27,9 @@ static int min_sndbuf = SOCK_MIN_SNDBUF; static int min_rcvbuf = SOCK_MIN_RCVBUF; static int max_skb_frags = MAX_SKB_FRAGS; +#ifdef CONFIG_NET_RX_BUSY_POLL +static int max_busy_poll = MAX_BUSY_POLL; +#endif static int net_msg_warn; /* Unused, but still a sysctl */ @@ -408,14 +411,18 @@ static int proc_do_rss_key(struct ctl_table *table, int write, .data = &sysctl_net_busy_poll, .maxlen = sizeof(unsigned int), .mode = 0644, - .proc_handler = proc_dointvec + .proc_handler = proc_dointvec_minmax, + .extra1 = &zero, + .extra2 = &max_busy_poll, }, { .procname = "busy_read", .data = &sysctl_net_busy_read, .maxlen = sizeof(unsigned int), .mode = 0644, - .proc_handler = proc_dointvec + .proc_handler = proc_dointvec_minmax, + .extra1 = &zero, + .extra2 = &max_busy_poll, }, #endif #ifdef CONFIG_NET_SCHED