Skip to content

Commit 2d48d67

Browse files
Eliezer Tamirdavem330
authored andcommitted
net: poll/select low latency socket support
select/poll busy-poll support. Split sysctl value into two separate ones, one for read and one for poll. updated Documentation/sysctl/net.txt Add a new poll flag POLL_LL. When this flag is set, sock_poll will call sk_poll_ll if possible. sock_poll sets this flag in its return value to indicate to select/poll when a socket that can busy poll is found. When poll/select have nothing to report, call the low-level sock_poll again until we are out of time or we find something. Once the system call finds something, it stops setting POLL_LL, so it can return the result to the user ASAP. Signed-off-by: Eliezer Tamir <eliezer.tamir@linux.intel.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent e4f2379 commit 2d48d67

7 files changed

Lines changed: 91 additions & 22 deletions

File tree

‎Documentation/sysctl/net.txt‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,27 @@ The maximum number of packets that kernel can handle on a NAPI interrupt,
5050
it's a Per-CPU variable.
5151
Default: 64
5252

53-
low_latency_poll
53+
low_latency_read
5454
----------------
55-
Low latency busy poll timeout. (needs CONFIG_NET_LL_RX_POLL)
55+
Low latency busy poll timeout for socket reads. (needs CONFIG_NET_LL_RX_POLL)
5656
Approximate time in us to spin waiting for packets on the device queue.
57+
This sets the default value of the SO_LL socket option.
58+
Can be set or overridden per socket by setting socket option SO_LL.
5759
Recommended value is 50. May increase power usage.
5860
Default: 0 (off)
5961

62+
low_latency_poll
63+
----------------
64+
Low latency busy poll timeout for poll and select. (needs CONFIG_NET_LL_RX_POLL)
65+
Approximate time in us to spin waiting for packets on the device queue.
66+
Recommended value depends on the number of sockets you poll on.
67+
For several sockets 50, for several hundreds 100.
68+
For more than that you probably want to use epoll.
69+
Note that only sockets with SO_LL set will be busy polled, so you want to either
70+
selectively set SO_LL on those sockets or set sysctl.net.low_latency_read globally.
71+
May increase power usage.
72+
Default: 0 (off)
73+
6074
rmem_default
6175
------------
6276

‎fs/select.c‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/rcupdate.h>
2828
#include <linux/hrtimer.h>
2929
#include <linux/sched/rt.h>
30+
#include <net/ll_poll.h>
3031

3132
#include <asm/uaccess.h>
3233

@@ -384,9 +385,10 @@ static int max_select_fd(unsigned long n, fd_set_bits *fds)
384385
#define POLLEX_SET (POLLPRI)
385386

386387
static inline void wait_key_set(poll_table *wait, unsigned long in,
387-
unsigned long out, unsigned long bit)
388+
unsigned long out, unsigned long bit,
389+
unsigned int ll_flag)
388390
{
389-
wait->_key = POLLEX_SET;
391+
wait->_key = POLLEX_SET | ll_flag;
390392
if (in & bit)
391393
wait->_key |= POLLIN_SET;
392394
if (out & bit)
@@ -400,6 +402,8 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
400402
poll_table *wait;
401403
int retval, i, timed_out = 0;
402404
unsigned long slack = 0;
405+
unsigned int ll_flag = POLL_LL;
406+
u64 ll_time = ll_end_time();
403407

404408
rcu_read_lock();
405409
retval = max_select_fd(n, fds);
@@ -422,6 +426,7 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
422426
retval = 0;
423427
for (;;) {
424428
unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
429+
bool can_ll = false;
425430

426431
inp = fds->in; outp = fds->out; exp = fds->ex;
427432
rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
@@ -449,7 +454,8 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
449454
f_op = f.file->f_op;
450455
mask = DEFAULT_POLLMASK;
451456
if (f_op && f_op->poll) {
452-
wait_key_set(wait, in, out, bit);
457+
wait_key_set(wait, in, out,
458+
bit, ll_flag);
453459
mask = (*f_op->poll)(f.file, wait);
454460
}
455461
fdput(f);
@@ -468,6 +474,11 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
468474
retval++;
469475
wait->_qproc = NULL;
470476
}
477+
if (mask & POLL_LL)
478+
can_ll = true;
479+
/* got something, stop busy polling */
480+
if (retval)
481+
ll_flag = 0;
471482
}
472483
}
473484
if (res_in)
@@ -486,6 +497,9 @@ int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
486497
break;
487498
}
488499

500+
if (can_ll && can_poll_ll(ll_time))
501+
continue;
502+
489503
/*
490504
* If this is the first loop and we have a timeout
491505
* given, then we convert to ktime_t and set the to
@@ -717,7 +731,8 @@ struct poll_list {
717731
* pwait poll_table will be used by the fd-provided poll handler for waiting,
718732
* if pwait->_qproc is non-NULL.
719733
*/
720-
static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
734+
static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait,
735+
bool *can_ll, unsigned int ll_flag)
721736
{
722737
unsigned int mask;
723738
int fd;
@@ -731,7 +746,10 @@ static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
731746
mask = DEFAULT_POLLMASK;
732747
if (f.file->f_op && f.file->f_op->poll) {
733748
pwait->_key = pollfd->events|POLLERR|POLLHUP;
749+
pwait->_key |= ll_flag;
734750
mask = f.file->f_op->poll(f.file, pwait);
751+
if (mask & POLL_LL)
752+
*can_ll = true;
735753
}
736754
/* Mask out unneeded events. */
737755
mask &= pollfd->events | POLLERR | POLLHUP;
@@ -750,6 +768,8 @@ static int do_poll(unsigned int nfds, struct poll_list *list,
750768
ktime_t expire, *to = NULL;
751769
int timed_out = 0, count = 0;
752770
unsigned long slack = 0;
771+
unsigned int ll_flag = POLL_LL;
772+
u64 ll_time = ll_end_time();
753773

754774
/* Optimise the no-wait case */
755775
if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
@@ -762,6 +782,7 @@ static int do_poll(unsigned int nfds, struct poll_list *list,
762782

763783
for (;;) {
764784
struct poll_list *walk;
785+
bool can_ll = false;
765786

766787
for (walk = list; walk != NULL; walk = walk->next) {
767788
struct pollfd * pfd, * pfd_end;
@@ -776,9 +797,10 @@ static int do_poll(unsigned int nfds, struct poll_list *list,
776797
* this. They'll get immediately deregistered
777798
* when we break out and return.
778799
*/
779-
if (do_pollfd(pfd, pt)) {
800+
if (do_pollfd(pfd, pt, &can_ll, ll_flag)) {
780801
count++;
781802
pt->_qproc = NULL;
803+
ll_flag = 0;
782804
}
783805
}
784806
}
@@ -795,6 +817,8 @@ static int do_poll(unsigned int nfds, struct poll_list *list,
795817
if (count || timed_out)
796818
break;
797819

820+
if (can_ll && can_poll_ll(ll_time))
821+
continue;
798822
/*
799823
* If this is the first loop and we have a timeout
800824
* given, then we convert to ktime_t and set the to

‎include/net/ll_poll.h‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#ifdef CONFIG_NET_LL_RX_POLL
3131

3232
struct napi_struct;
33+
extern unsigned int sysctl_net_ll_read __read_mostly;
3334
extern unsigned int sysctl_net_ll_poll __read_mostly;
3435

3536
/* return values from ndo_ll_poll */
@@ -38,17 +39,18 @@ extern unsigned int sysctl_net_ll_poll __read_mostly;
3839

3940
/* we can use sched_clock() because we don't care much about precision
4041
* we only care that the average is bounded
42+
* we don't mind a ~2.5% imprecision so <<10 instead of *1000
43+
* sk->sk_ll_usec is a u_int so this can't overflow
4144
*/
42-
static inline u64 ll_end_time(struct sock *sk)
45+
static inline u64 ll_sk_end_time(struct sock *sk)
4346
{
44-
u64 end_time = ACCESS_ONCE(sk->sk_ll_usec);
45-
46-
/* we don't mind a ~2.5% imprecision
47-
* sk->sk_ll_usec is a u_int so this can't overflow
48-
*/
49-
end_time = (end_time << 10) + sched_clock();
47+
return ((u64)ACCESS_ONCE(sk->sk_ll_usec) << 10) + sched_clock();
48+
}
5049

51-
return end_time;
50+
/* in poll/select we use the global sysctl_net_ll_poll value */
51+
static inline u64 ll_end_time(void)
52+
{
53+
return ((u64)ACCESS_ONCE(sysctl_net_ll_poll) << 10) + sched_clock();
5254
}
5355

5456
static inline bool sk_valid_ll(struct sock *sk)
@@ -62,10 +64,13 @@ static inline bool can_poll_ll(u64 end_time)
6264
return !time_after64(sched_clock(), end_time);
6365
}
6466

67+
/* when used in sock_poll() nonblock is known at compile time to be true
68+
* so the loop and end_time will be optimized out
69+
*/
6570
static inline bool sk_poll_ll(struct sock *sk, int nonblock)
6671
{
72+
u64 end_time = nonblock ? 0 : ll_sk_end_time(sk);
6773
const struct net_device_ops *ops;
68-
u64 end_time = ll_end_time(sk);
6974
struct napi_struct *napi;
7075
int rc = false;
7176

@@ -84,7 +89,6 @@ static inline bool sk_poll_ll(struct sock *sk, int nonblock)
8489
goto out;
8590

8691
do {
87-
8892
rc = ops->ndo_ll_poll(napi);
8993

9094
if (rc == LL_FLUSH_FAILED)
@@ -95,8 +99,8 @@ static inline bool sk_poll_ll(struct sock *sk, int nonblock)
9599
NET_ADD_STATS_BH(sock_net(sk),
96100
LINUX_MIB_LOWLATENCYRXPACKETS, rc);
97101

98-
} while (skb_queue_empty(&sk->sk_receive_queue)
99-
&& can_poll_ll(end_time) && !nonblock);
102+
} while (!nonblock && skb_queue_empty(&sk->sk_receive_queue) &&
103+
can_poll_ll(end_time));
100104

101105
rc = !skb_queue_empty(&sk->sk_receive_queue);
102106
out:
@@ -118,7 +122,12 @@ static inline void sk_mark_ll(struct sock *sk, struct sk_buff *skb)
118122

119123
#else /* CONFIG_NET_LL_RX_POLL */
120124

121-
static inline u64 ll_end_time(struct sock *sk)
125+
static inline u64 sk_ll_end_time(struct sock *sk)
126+
{
127+
return 0;
128+
}
129+
130+
static inline u64 ll_end_time(void)
122131
{
123132
return 0;
124133
}

‎include/uapi/asm-generic/poll.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
#define POLLFREE 0x4000 /* currently only for epoll */
3232

33+
#define POLL_LL 0x8000
34+
3335
struct pollfd {
3436
int fd;
3537
short events;

‎net/core/sock.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2307,7 +2307,7 @@ void sock_init_data(struct socket *sock, struct sock *sk)
23072307

23082308
#ifdef CONFIG_NET_LL_RX_POLL
23092309
sk->sk_napi_id = 0;
2310-
sk->sk_ll_usec = sysctl_net_ll_poll;
2310+
sk->sk_ll_usec = sysctl_net_ll_read;
23112311
#endif
23122312

23132313
/*

‎net/core/sysctl_net_core.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ static struct ctl_table net_core_table[] = {
306306
.mode = 0644,
307307
.proc_handler = proc_dointvec
308308
},
309+
{
310+
.procname = "low_latency_read",
311+
.data = &sysctl_net_ll_read,
312+
.maxlen = sizeof(unsigned int),
313+
.mode = 0644,
314+
.proc_handler = proc_dointvec
315+
},
316+
#
309317
#endif
310318
#endif /* CONFIG_NET */
311319
{

‎net/socket.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
#include <net/ll_poll.h>
108108

109109
#ifdef CONFIG_NET_LL_RX_POLL
110+
unsigned int sysctl_net_ll_read __read_mostly;
110111
unsigned int sysctl_net_ll_poll __read_mostly;
111112
#endif
112113

@@ -1147,13 +1148,24 @@ EXPORT_SYMBOL(sock_create_lite);
11471148
/* No kernel lock held - perfect */
11481149
static unsigned int sock_poll(struct file *file, poll_table *wait)
11491150
{
1151+
unsigned int ll_flag = 0;
11501152
struct socket *sock;
11511153

11521154
/*
11531155
* We can't return errors to poll, so it's either yes or no.
11541156
*/
11551157
sock = file->private_data;
1156-
return sock->ops->poll(file, sock, wait);
1158+
1159+
if (sk_valid_ll(sock->sk)) {
1160+
/* this socket can poll_ll so tell the system call */
1161+
ll_flag = POLL_LL;
1162+
1163+
/* once, only if requested by syscall */
1164+
if (wait && (wait->_key & POLL_LL))
1165+
sk_poll_ll(sock->sk, 1);
1166+
}
1167+
1168+
return ll_flag | sock->ops->poll(file, sock, wait);
11571169
}
11581170

11591171
static int sock_mmap(struct file *file, struct vm_area_struct *vma)

0 commit comments

Comments
 (0)