net/core: add optional threading for backlog processing
Subsystems (2)
RFC v3 1/1 net/core: add optional threading for backlog processing
When dealing with few flows or an imbalance on CPU utilization, static RPS CPU assignment can be too inflexible. Add support for enabling threaded NAPI for backlog processing in order to allow the scheduler to better balance processing. This helps better spread the load across idle CPUs.
---
RFC v3:
- make patch more generic, applies to backlog processing in general
- fix process queue access on flush
RFC v2:
- fix rebase error in rps locking
include/linux/netdevice.h | 2 + net/core/dev.c | 78 +++++++++++++++++++++++++++++++++++--- net/core/sysctl_net_core.c | 27 +++++++++++++ 3 files changed, 102 insertions(+), 5 deletions(-)
include/linux/netdevice.h +2 -0
@@ -522,6 +522,7 @@ static inline bool napi_complete(struct napi_struct *n) }int dev_set_threaded (struct net_device *dev,bool threaded); +int backlog_set_threaded (bool threaded);/** * napi_disable - prevent NAPI from scheduling
@@ -3192,6 +3193,7 @@ struct softnet_data {unsigned int cpu;unsigned int input_queue_tail;#endif +unsigned int process_queue_empty;unsigned int received_rps;unsigned int dropped;struct sk_buff_head input_pkt_queue;
net/core/dev.c +73 -5
129 lines not on this page · raw message
net/core/sysctl_net_core.c +27 -0
@@ -30,6 +30,7 @@ static int int_3600 = 3600;static int min_sndbuf =SOCK_MIN_SNDBUF ;static int min_rcvbuf =SOCK_MIN_RCVBUF ;static int max_skb_frags =MAX_SKB_FRAGS ; +static int backlog_threaded;static int net_msg_warn;/* Unused, but still a sysctl */
@@ -165,6 +166,23 @@ static int rps_sock_flow_sysctl(struct ctl_table *table, int write, }#endif /* CONFIG_RPS */ +static int backlog_threaded_sysctl (struct ctl_table *table,int write, +void *buffer,size_t *lenp,loff_t *ppos) +{ +static DEFINE_MUTEX (backlog_threaded_mutex); +int ret; + +mutex_lock (&backlog_threaded_mutex); + + ret =proc_dointvec_minmax (table, write, buffer, lenp, ppos); +if (write && !ret) + ret =backlog_set_threaded (backlog_threaded); + +mutex_unlock (&backlog_threaded_mutex); + +return ret; +} +#ifdef CONFIG_NET_FLOW_LIMIT static DEFINE_MUTEX (flow_limit_update_mutex);
@@ -514,6 +532,15 @@ static struct ctl_table net_core_table[] = { .proc_handler = rps_default_mask_sysctl },#endif + { + .procname = "backlog_threaded", + .data = &backlog_threaded, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = backlog_threaded_sysctl, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE + }, #ifdef CONFIG_NET_FLOW_LIMIT { .procname = "flow_limit_cpu_bitmap",
Signature · 1 line
2.39.0
kernel test robot
build bot[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in…
Hi Felix,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on net-next/master]
[also build test WARNING on horms-ipvs/master linus/master v6.2-rc8 next-20230217] [cannot apply to net/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Felix-Fietkau/net-core-add-optional-threading-for-backlog-processing/20230219-211100 patch link: https://lore.kernel.org/r/20230219131006.92681-1-nbd%40nbd.name patch subject: [RFC v3] net/core: add optional threading for backlog processing config: i386-randconfig-a002 (https://download.01.org/0day-ci/archive/20230219/[email protected]/config) compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/7912164b6828c63b948d6ff9d21599f6cecbf4fc
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Felix-Fietkau/net-core-add-optional-threading-for-backlog-processing/20230219-211100 git checkout 7912164b6828c63b948d6ff9d21599f6cecbf4fc
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash net/core/
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> net/core/dev.c:5812:20: warning: variable 'process_queue_empty' is uninitialized when used here [-Wuninitialized]
flush_processq = process_queue_empty == sd->process_queue_empty;
^~~~~~~~~~~~~~~~~~~
net/core/dev.c:5773:34: note: initialize the variable 'process_queue_empty' to silence this warning
unsigned int process_queue_empty;
^
= 0
net/core/dev.c:5093:1: warning: unused function 'sch_handle_ingress' [-Wunused-function]
sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
^
net/core/dev.c:5246:19: warning: unused function 'nf_ingress' [-Wunused-function]
static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
^3 warnings generated.
vim +/process_queue_empty +5812 net/core/dev.c
5769
5770 /* Network device is going away, flush any packets still pending */
5771 static void flush_backlog(struct work_struct *work)
5772 {
5773 unsigned int process_queue_empty;
5774 bool threaded, flush_processq;
5775 struct sk_buff *skb, *tmp;
5776 struct softnet_data *sd;
5777
5778 local_bh_disable();
5779 sd = this_cpu_ptr(&softnet_data);
5780
5781 rps_lock_irq_disable(sd);
5782 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
5783 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
5784 __skb_unlink(skb, &sd->input_pkt_queue);
5785 dev_kfree_skb_irq(skb);
5786 input_queue_head_incr(sd);
5787 }
5788 }
5789
5790 threaded = test_bit(NAPI_STATE_THREADED, &sd->backlog.state);
5791 flush_processq = threaded &&
5792 !skb_queue_empty_lockless(&sd->process_queue);
5793 rps_unlock_irq_enable(sd);
5794
5795 if (threaded)
5796 goto out;
5797
5798 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
5799 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
5800 __skb_unlink(skb, &sd->process_queue);
5801 kfree_skb(skb);
5802 input_queue_head_incr(sd);
5803 }
5804 }
5805
5806 out:
5807 local_bh_enable();
5808
5809 while (flush_processq) {
5810 msleep(1);
5811 rps_lock_irq_disable(sd);
> 5812 flush_processq = process_queue_empty == sd->process_queue_empty;
5813 rps_unlock_irq_enable(sd);
5814 }
5815 }
5816
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
kernel test robot
build bot[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in…
Hi Felix,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on net-next/master]
[also build test WARNING on horms-ipvs/master linus/master v6.2-rc8] [cannot apply to net/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Felix-Fietkau/net-core-add-optional-threading-for-backlog-processing/20230219-211100 patch link: https://lore.kernel.org/r/20230219131006.92681-1-nbd%40nbd.name patch subject: [RFC v3] net/core: add optional threading for backlog processing config: hexagon-randconfig-r045-20230219 (https://download.01.org/0day-ci/archive/20230219/[email protected]/config) compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project db89896bbbd2251fff457699635acbbedeead27f)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/7912164b6828c63b948d6ff9d21599f6cecbf4fc
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Felix-Fietkau/net-core-add-optional-threading-for-backlog-processing/20230219-211100 git checkout 7912164b6828c63b948d6ff9d21599f6cecbf4fc
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash net/core/
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from net/core/dev.c:88:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __raw_readb(PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
#define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
^
In file included from net/core/dev.c:88:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
#define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
^
In file included from net/core/dev.c:88:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writeb(value, PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
>> net/core/dev.c:5812:20: warning: variable 'process_queue_empty' is uninitialized when used here [-Wuninitialized]
flush_processq = process_queue_empty == sd->process_queue_empty;
^~~~~~~~~~~~~~~~~~~
net/core/dev.c:5773:34: note: initialize the variable 'process_queue_empty' to silence this warning
unsigned int process_queue_empty;
^
= 0
net/core/dev.c:5093:1: warning: unused function 'sch_handle_ingress' [-Wunused-function]
sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
^
net/core/dev.c:5246:19: warning: unused function 'nf_ingress' [-Wunused-function]
static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
^9 warnings generated.
vim +/process_queue_empty +5812 net/core/dev.c
5769
5770 /* Network device is going away, flush any packets still pending */
5771 static void flush_backlog(struct work_struct *work)
5772 {
5773 unsigned int process_queue_empty;
5774 bool threaded, flush_processq;
5775 struct sk_buff *skb, *tmp;
5776 struct softnet_data *sd;
5777
5778 local_bh_disable();
5779 sd = this_cpu_ptr(&softnet_data);
5780
5781 rps_lock_irq_disable(sd);
5782 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
5783 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
5784 __skb_unlink(skb, &sd->input_pkt_queue);
5785 dev_kfree_skb_irq(skb);
5786 input_queue_head_incr(sd);
5787 }
5788 }
5789
5790 threaded = test_bit(NAPI_STATE_THREADED, &sd->backlog.state);
5791 flush_processq = threaded &&
5792 !skb_queue_empty_lockless(&sd->process_queue);
5793 rps_unlock_irq_enable(sd);
5794
5795 if (threaded)
5796 goto out;
5797
5798 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
5799 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
5800 __skb_unlink(skb, &sd->process_queue);
5801 kfree_skb(skb);
5802 input_queue_head_incr(sd);
5803 }
5804 }
5805
5806 out:
5807 local_bh_enable();
5808
5809 while (flush_processq) {
5810 msleep(1);
5811 rps_lock_irq_disable(sd);
> 5812 flush_processq = process_queue_empty == sd->process_queue_empty;
5813 rps_unlock_irq_enable(sd);
5814 }
5815 }
5816
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
Quoted 61 lines · Felix Fietkau
When dealing with few flows or an imbalance on CPU utilization, static RPS CPU assignment can be too inflexible. Add support for enabling threaded NAPI for backlog processing in order to allow the scheduler to better balance processing. This helps better spread the load across idle CPUs.
Signed-off-by Felix Fietkau <[email protected]>---
RFC v3:
- make patch more generic, applies to backlog processing in general
- fix process queue access on flush
RFC v2:
- fix rebase error in rps lockinginclude/linux/netdevice.h | 2 + net/core/dev.c | 78 +++++++++++++++++++++++++++++++++++--- net/core/sysctl_net_core.c | 27 +++++++++++++ 3 files changed, 102 insertions(+), 5 deletions(-)include/linux/netdevice.h +2
-0@@ -522,6 +522,7 @@ static inline bool napi_complete(struct napi_struct *n) }int dev_set_threaded (struct net_device *dev,bool threaded); +int backlog_set_threaded (bool threaded);/** * napi_disable - prevent NAPI from scheduling @@ -3192,6 +3193,7 @@ struct softnet_data {unsigned int cpu;unsigned int input_queue_tail;#endif +unsigned int process_queue_empty;unsigned int received_rps;unsigned int dropped;struct sk_buff_head input_pkt_queue;net/core/dev.c +3
-1@@ -4597,7 +4597,7 @@ static int napi_schedule_rps(struct softnet_data *sd)struct softnet_data *mysd =this_cpu_ptr (&softnet_data);#ifdef CONFIG_RPS -if (sd != mysd) { +if (sd != mysd && !test_bit (NAPI_STATE_THREADED , &sd->backlog.state)) { sd->rps_ipi_next = mysd->rps_ipi_list; mysd->rps_ipi_list = sd;@@ -5778,6 +5778,8 @@ static DEFINE_PER_CPU(struct work_struct, flush_works);/* Network device is going away, flush any packets still pending */ static void flush_backlog (struct work_struct *work) { +unsigned int process_queue_empty; +bool threaded, flush_processq;struct sk_buff *skb, *tmp;struct softnet_data *sd;
net/core/dev.c static void flush_backlog(struct work_struct *work) · line 5800
Quoted 7 lines
net/core/dev.c +4
-0@@ -5792,8 +5794,15 @@ static void flush_backlog(struct work_struct *work)input_queue_head_incr (sd); } } + + threaded =test_bit (NAPI_STATE_THREADED , &sd->backlog.state); + flush_processq = threaded && + !skb_queue_empty_lockless (&sd->process_queue);
Sorry, the patch was missing these lines:
if (flush_processq) process_queue_empty = sd->process_queue_empty;
Quoted 8 lines
net/core/dev.c +3
-0rps_unlock_irq_enable (sd); +if (threaded) + goto out; + skb_queue_walk_safe(&sd->process_queue, skb, tmp) {if (skb->dev->reg_state ==NETREG_UNREGISTERING ) {__skb_unlink (skb, &sd->process_queue);
Quoted 11 lines · Felix Fietkau
Quoted 6 lines · Felix Fietkau
When dealing with few flows or an imbalance on CPU utilization, static RPS CPU assignment can be too inflexible. Add support for enabling threaded NAPI for backlog processing in order to allow the scheduler to better balance processing. This helps better spread the load across idle CPUs.
Signed-off-by Felix Fietkau <[email protected]>Sorry, the patch was missing these lines:
Also make sure the following loop won't leak kthreads.
while :
do
echo 1 >/proc/sys/net/core/backlog_threaded
echo 0 >/proc/sys/net/core/backlog_threaded
done
Some documentation would also be welcomed (Documentation/admin-guide/sysctl/net.rst &Documentation/networking/scaling.rst )