Skip to contentSkip to conversation

Suspend IRQs during application busy periods

PATCH Superseded Joe Damato 4 participants 17 messages Nov 4, 2024 – Nov 8, 2024

Subsystems (7)

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Joe Damato

author
#0 lore raw

v6 cover Suspend IRQs during application busy periods

Greetings:

Welcome to v6, see changelog below. This revision includes only documentation changes: patch 7 has been updated with Bagas' suggestions and the htmldoc looks better as a result. In addition, this cover letter has been updated with a full re-run of the test data. We've included a new test case highlighting a case Sridhar asked about in our v3. See below.

This series introduces a new mechanism, IRQ suspension, which allows network applications using epoll to mask IRQs during periods of high traffic while also reducing tail latency (compared to existing mechanisms, see below) during periods of low traffic. In doing so, this balances CPU consumption with network processing efficiency.

Martin Karsten (CC'd) and I have been collaborating on this series for several months and have appreciated the feedback from the community on our RFC [1]. We've updated the cover letter and kernel documentation in an attempt to more clearly explain how this mechanism works, how applications can use it, and how it compares to existing mechanisms in the kernel. We've added an additional test case, 'fullbusy', achieved by modifying libevent for comparison. See below for a detailed description, link to the patch, and test results.

I briefly mentioned this idea at netdev conf 2024 (for those who were there) and Martin described this idea in an earlier paper presented at Sigmetrics 2024 [2].

~ The short explanation (TL;DR)

We propose adding a new napi config parameter: irq_suspend_timeout to help balance CPU usage and network processing efficiency when using IRQ deferral and napi busy poll.

If this parameter is set to a non-zero value *and* a user application has enabled preferred busy poll on a busy poll context (via the EPIOCSPARAMS ioctl introduced in commit 18e2bf0edf4d ("eventpoll: Add epoll ioctl for epoll_params")), then application calls to epoll_wait for that context will cause device IRQs and softirq processing to be suspended as long as epoll_wait successfully retrieves data from the NAPI. Each time data is retrieved, the irq_suspend_timeout is deferred.

If/when network traffic subsides and epoll_wait returns no data, IRQ suspension is immediately reverted back to the existing napi_defer_hard_irqs and gro_flush_timeout mechanism which was introduced in commit 6f8b12d661d0 ("net: napi: add hard irqs deferral feature")).

The irq_suspend_timeout serves as a safety mechanism. If userland takes a long time processing data, irq_suspend_timeout will fire and restart normal NAPI processing.

For a more in depth explanation, please continue reading.

~ Comparison with existing mechanisms

Interrupt mitigation can be accomplished in napi software, by setting napi_defer_hard_irqs and gro_flush_timeout, or via interrupt coalescing in the NIC. This can be quite efficient, but in both cases, a fixed timeout (or packet count) needs to be configured. However, a fixed timeout cannot effectively support both low- and high-load situations:

At low load, an application typically processes a few requests and then waits to receive more input data. In this scenario, a large timeout will cause unnecessary latency.

At high load, an application typically processes many requests before being ready to receive more input data. In this case, a small timeout will likely fire prematurely and trigger irq/softirq processing, which interferes with the application's execution. This causes overhead, most likely due to cache contention.

While NICs attempt to provide adaptive interrupt coalescing schemes, these cannot properly take into account application-level processing.

An alternative packet delivery mechanism is busy-polling, which results in perfect alignment of application processing and network polling. It delivers optimal performance (throughput and latency), but results in 100% cpu utilization and is thus inefficient for below-capacity workloads.

We propose to add a new packet delivery mode that properly alternates between busy polling and interrupt-based delivery depending on busy and idle periods of the application. During a busy period, the system operates in busy-polling mode, which avoids interference. During an idle period, the system falls back to interrupt deferral, but with a small timeout to avoid excessive latencies. This delivery mode can also be viewed as an extension of basic interrupt deferral, but alternating between a small and a very large timeout.

This delivery mode is efficient, because it avoids softirq execution interfering with application processing during busy periods. It can be used with blocking epoll_wait to conserve cpu cycles during idle periods. The effect of alternating between busy and idle periods is that performance (throughput and latency) is very close to full busy polling, while cpu utilization is lower and very close to interrupt mitigation.

~ Usage details

IRQ suspension is introduced via a per-NAPI configuration parameter that controls the maximum time that IRQs can be suspended.

Here's how it is intended to work:
- The user application (or system administrator) uses the netdev-genl netlink interface to set the pre-existing napi_defer_hard_irqs and gro_flush_timeout NAPI config parameters to enable IRQ deferral.

- The user application (or system administrator) sets the proposed irq_suspend_timeout parameter via the netdev-genl netlink interface to a larger value than gro_flush_timeout to enable IRQ suspension.

- The user application issues the existing epoll ioctl to set the prefer_busy_poll flag on the epoll context.

- The user application then calls epoll_wait to busy poll for network events, as it normally would.

- If epoll_wait returns events to userland, IRQs are suspended for the duration of irq_suspend_timeout.

- If epoll_wait finds no events and the thread is about to go to sleep, IRQ handling using napi_defer_hard_irqs and gro_flush_timeout is resumed.

As long as epoll_wait is retrieving events, IRQs (and softirq processing) for the NAPI being polled remain disabled. When network traffic reduces, eventually a busy poll loop in the kernel will retrieve no data. When this occurs, regular IRQ deferral using gro_flush_timeout for the polled NAPI is re-enabled.

Unless IRQ suspension is continued by subsequent calls to epoll_wait, it automatically times out after the irq_suspend_timeout timer expires. Regular deferral is also immediately re-enabled when the epoll context is destroyed.

~ Usage scenario

The target scenario for IRQ suspension as packet delivery mode is a system that runs a dominant application with substantial network I/O. The target application can be configured to receive input data up to a certain batch size (via epoll_wait maxevents parameter) and this batch size determines the worst-case latency that application requests might experience. Because packet delivery is suspended during the target application's processing, the batch size also determines the worst-case latency of concurrent applications using the same RX queue(s).

gro_flush_timeout should be set as small as possible, but large enough to make sure that a single request is likely not being interfered with.

irq_suspend_timeout is largely a safety mechanism against misbehaving applications. It should be set large enough to cover the processing of an entire application batch, i.e., the factor between gro_flush_timeout and irq_suspend_timeout should roughly correspond to the maximum batch size that the target application would process in one go.

~ Design rationale

The implementation of the IRQ suspension mechanism very nicely dovetails with the existing mechanism for IRQ deferral when preferred busy poll is enabled (introduced in commit 7fd3253a7de6 ("net: Introduce preferred busy-polling"), see that commit message for more details).

While it would be possible to inject the suspend timeout via the existing epoll ioctl, it is more natural to avoid this path for one main reason:

An epoll context is linked to NAPI IDs as file descriptors are added; this means any epoll context might suddenly be associated with a different net_device if the application were to replace all existing fds with fds from a different device. In this case, the scope of the suspend timeout becomes unclear and many edge cases for both the user application and the kernel are introduced

Only a single iteration through napi busy polling is needed for this mechanism to work effectively. Since an important objective for this mechanism is preserving cpu cycles, exactly one iteration of the napi busy loop is invoked when busy_poll_usecs is set to 0.

~ Important call out in the implementation

- Enabling per epoll-context preferred busy poll will now effectively lead to a nonblocking iteration through napi_busy_loop, even when busy_poll_usecs is 0. See patch 4.

~ Benchmark configs & descriptions

The changes were benchmarked with memcached [3] using the benchmarking tool mutilate [4].

To facilitate benchmarking, a small patch [5] was applied to memcached 1.6.29 to allow setting per-epoll context preferred busy poll and other settings via environment variables. Another small patch [6] was applied to libevent to enable full busy-polling.

Multiple scenarios were benchmarked as described below and the scripts used for producing these results can be found on github [7] (note: all scenarios use NAPI-based traffic splitting via SO_INCOMING_ID by passing -N to memcached):

- base:
- no other options enabled
- deferX:
- set defer_hard_irqs to 100
- set gro_flush_timeout to X,000
- napibusy:
- set defer_hard_irqs to 100
- set gro_flush_timeout to 200,000
- enable busy poll via the existing ioctl (busy_poll_usecs = 64, busy_poll_budget = 64, prefer_busy_poll = true)
- fullbusy:
- set defer_hard_irqs to 100
- set gro_flush_timeout to 5,000,000
- enable busy poll via the existing ioctl (busy_poll_usecs = 1000, busy_poll_budget = 64, prefer_busy_poll = true)
- change memcached's nonblocking epoll_wait invocation (via libevent) to using a 1 ms timeout
- suspend0:
- set defer_hard_irqs to 0
- set gro_flush_timeout to 0
- set irq_suspend_timeout to 20,000,000
- enable busy poll via the existing ioctl (busy_poll_usecs = 0, busy_poll_budget = 64, prefer_busy_poll = true)
- suspendX:
- set defer_hard_irqs to 100
- set gro_flush_timeout to X,000
- set irq_suspend_timeout to 20,000,000
- enable busy poll via the existing ioctl (busy_poll_usecs = 0, busy_poll_budget = 64, prefer_busy_poll = true)

~ Benchmark results

Tested on:

Single socket AMD EPYC 7662 64-Core Processor Hyperthreading disabled
4 NUMA Zones (NPS=4)
16 CPUs per NUMA zone (64 cores total)
2 x Dual port 100gbps Mellanox Technologies ConnectX-5 Ex EN NIC

The test machine is configured such that a single interface has 8 RX queues. The queues' IRQs and memcached are pinned to CPUs that are NUMA-local to the interface which is under test. The NIC's interrupt coalescing configuration is left at boot-time defaults.

Results:

Results are shown below. The mechanism added by this series is represented by the 'suspend' cases. Data presented shows a summary over at least 15 runs of each test case [8] using the scripts on github [7]. For latency, the median is shown. For throughput and CPU utilization, the average is shown.

The results also include cycles-per-query (cpq) and instruction-per-query (ipq) metrics, following the methodology proposed in [2], to augment the CPU utilization numbers, which could be skewed due to frequency scaling. We find that this does not appear to be the case as CPU utilization and low-level metrics show similar trends.

These results were captured using the scripts on github [7] to illustrate how this approach compares with other pre-existing mechanisms. This data is not to be interpreted as scientific data captured in a fully isolated lab setting, but instead as best effort, illustrative information comparing and contrasting tradeoffs.

The absolute QPS results are higher than our previous submission, but the relative differences between variants are equivalent. Because the patches have been rebased on 6.12, several factors have likely influenced the overall performance. Most importantly, we had to switch to a new set of basic kernel options, which has likely altered the baseline performance. Because the overall comparison of variants still holds, we have not attempted to recreate the exact set of kernel options from the previous submission.

Compare:
- Throughput (MAX) and latencies of base vs suspend.
- CPU usage of napibusy and fullbusy during lower load (200K, 400K for example) vs suspend.
- Latency of the defer variants vs suspend as timeout and load increases.
- suspend0, which sets defer_hard_irqs and gro_flush_timeout to 0, has nearly the same performance as the base case (this is FAQ item #1).

The overall takeaway is that the suspend variants provide a superior combination of high throughput, low latency, and low cpu utilization compared to all other variants. Each of the suspend variants works very well, but some fine-tuning between latency and cpu utilization is still possible by tuning the small timeout (gro_flush_timeout).

Note: we've reorganized the results to make comparison among testcases with the same load easier.

  testcase  load     qps  avglat  95%lat  99%lat     cpu     cpq     ipq
      base  200K  199954     112     237     415      26   13040   11336
   defer10  200K  200002      54     123     142      28   19033   16508
   defer20  200K  199985      60     130     153      26   15737   14247
   defer50  200K  199968      78     142     181      23   12113   11609
  defer200  200K  199997     163     252     304      18    8449    9155
  fullbusy  200K  199993      46     117     132     100   43959   23320
  napibusy  200K  200006     100     237     275      56   25016   24866
  suspend0  200K  200012     105     249     432      29   14369   11844
 suspend10  200K  200004      53     123     141      32   19432   16752
 suspend20  200K  200014      58     126     151      30   16356   14670
 suspend50  200K  200018      73     134     176      26   13245   12416
suspend200  200K  200027     149     250     302      20    9508    9781
  testcase  load     qps  avglat  95%lat  99%lat     cpu     cpq     ipq
      base  400K  399984     139     268     715      40    9437    9299
   defer10  400K  400002      59     133     165      53   14089   12908
   defer20  400K  400016      66     140     171      47   12085   11682
   defer50  400K  400037      87     161     198      39    9528    9879
  defer200  400K  399954     181     273     329      32    7326    8438
  fullbusy  400K  399951      50     123     155     100   21990   16097
  napibusy  400K  399997      76     221     271      83   18260   16511
  suspend0  400K  399991     125     337     768      48   11051    9629
 suspend10  400K  399990      57     129     161      54   13629   12841
 suspend20  400K  399922      61     135     167      49   12055   11715
 suspend50  400K  400024      75     148     186      42   10049   10243
suspend200  400K  399936     154     267     325      34    7770    8677
  testcase  load     qps  avglat  95%lat  99%lat     cpu     cpq     ipq
      base  600K  600064     148     265     576      61    9276    8757
   defer10  600K  599985      71     147     204      76   12048   10863
   defer20  600K  600024      75     151     199      66   10572   10328
   defer50  600K  600054      94     172     217      55    8584    9144
  defer200  600K  600030     200     299     355      45    6874    8189
  fullbusy  600K  599956      55     127     176     100   14650   13968
  napibusy  600K  599956      64     163     252      96   14022   14153
  suspend0  600K  600029     126     306     724      70   10393    8977
 suspend10  600K  599997      63     137     194      70   10991   11005
 suspend20  600K  600012      67     141     194      65   10108   10359
 suspend50  600K  600045      80     157     203      57    8747    9320
suspend200  600K  599940     158     277     344      48    7221    8354
  testcase  load     qps  avglat  95%lat  99%lat     cpu     cpq     ipq
      base  800K  800025     179     298     555      86    9572    8297
   defer10  800K  799275     224     633    1271      96   10679    8904
   defer20  800K  800041     114     226     328      90   10122    8917
   defer50  800K  799936     118     207     288      77    8820    8607
  defer200  800K  799994     228     341     403      65    7424    8130
  fullbusy  800K  799964      62     136     192     100   10992   12518
  napibusy  800K  799971      65     142     216      99   10911   12529
  suspend0  800K  799965     126     250     533      86    9489    8496
 suspend10  800K  799995      69     145     201      83    9475    9764
 suspend20  800K  799931      74     151     209      79    8976    9336
 suspend50  800K  799946      87     168     224      71    7993    8794
suspend200  800K  799993     160     292     357      62    6967    8184

testcase load qps avglat 95%lat 99%lat cpu cpq ipq

      base 1000K  915792    3498    5740    6239      97    9388    7930
   defer10 1000K  876285    3896    6095    6418      99    9960    8542
   defer20 1000K  914909    3107    5771    6283      97    9407    8284
   defer50 1000K  928426    2977    5591    5931      97    9214    8012
  defer200 1000K  959989    3097    5306    5929      96    8816    7908
  fullbusy 1000K 1000102      74     155     213     100    8796   10559
  napibusy 1000K 1000006      74     154     216     100    8787   10654
  suspend0 1000K  960757    2223    5715    7029      98    8964    7993
 suspend10 1000K  999926      80     162     222      92    8246    8922
 suspend20 1000K 1000095      85     166     226      89    7966    8719
 suspend50 1000K 1000067      96     180     238      84    7476    8419
suspend200 1000K  999968     163     298     363      76    6798    8061
  testcase  load     qps  avglat  95%lat  99%lat     cpu     cpq     ipq
      base   MAX 1054805    4152    5298    5743     100    8332    7890
   defer10   MAX  937098    4598    6010    6347     100    9378    8407
   defer20   MAX  988905    4389    5637    5990     100    8886    8106
   defer50   MAX 1067194    3960    5216    5544     100    8235    7911
  defer200   MAX 1054967    4084    5496    5821     100    8323    7871
  fullbusy   MAX 1248006    3472    3918    3979     100    7050    7919
  napibusy   MAX 1128384    3742    7958   10753     100    7776    7872
  suspend0   MAX 1034456    4242    5668    6042     100    8497    7912
 suspend10   MAX 1229229    3513    3926    3986     100    7156    7937
 suspend20   MAX 1226845    3514    3939    3985     100    7171    7937
 suspend50   MAX 1230757    3513    3935    3983     100    7140    7935
suspend200   MAX 1230424    3503    3934    3984     100    7142    7927

~ FAQ

- Why is a new parameter needed? Does irq_suspend_timeout override gro_flush_timeout?

Using the suspend mechanism causes the system to alternate between polling mode and irq-driven packet delivery. During busy periods, irq_suspend_timeout overrides gro_flush_timeout and keeps the system busy polling, but when epoll finds no events, the setting of gro_flush_timeout and napi_defer_hard_irqs determine the next step.

There are essentially three possible loops for network processing and packet delivery:

1) hardirq -> softirq -> napi poll; basic interrupt delivery
2) timer -> softirq -> napi poll; deferred irq processing
3) epoll -> busy-poll -> napi poll; busy looping

Loop 2 can take control from Loop 1, if gro_flush_timeout and napi_defer_hard_irqs are set.

If gro_flush_timeout and napi_defer_hard_irqs are set, Loops 2 and 3 "wrestle" with each other for control. During busy periods, irq_suspend_timeout is used as timer in Loop 2, which essentially tilts this in favour of Loop 3.

If gro_flush_timeout and napi_defer_hard_irqs are not set, Loop 3 cannot take control from Loop 1.

Therefore, setting gro_flush_timeout and napi_defer_hard_irqs is the recommended usage, because otherwise setting irq_suspend_timeout might not have any discernible effect.

This is shown in the results above: compare suspend0 with the base case. Note that the lack of napi_defer_hard_irqs and gro_flush_timeout produce similar results for both, which encourages the use of napi_defer_hard_irqs and gro_flush_timeout in addition to irq_suspend_timeout.

- Can the new timeout value be threaded through the new epoll ioctl ?

Only with difficulty. The epoll ioctl sets options on an epoll context and the NAPI ID associated with an epoll context can change based on what file descriptors a user app adds to the epoll context. This would introduce complexity in the API from the user perspective and also complexity in the kernel.

- Can irq suspend be built by combining NIC coalescing and gro_flush_timeout ?

No. The problem is that the long timeout must engage if and only if prefer-busy is active.

When using NIC coalescing for the short timeout (without napi_defer_hard_irqs/gro_flush_timeout), an interrupt after an idle period will trigger softirq, which will run napi polling. At this point, prefer-busy is not active, so NIC interrupts would be re-enabled. Then it is not possible for the longer timeout to interject to switch control back to polling. In other words, only by using the software timer for the short timeout, it is possible to extend the timeout without having to reprogram the NIC timer or reach down directly and disable interrupts.

Using gro_flush_timeout for the long timeout also has problems, for the same underlying reason. In the current napi implementation, gro_flush_timeout is not tied to prefer-busy. We'd either have to change that and in the process modify the existing deferral mechanism, or introduce a state variable to determine whether gro_flush_timeout is used as long timeout for irq suspend or whether it is used for its default purpose. In an earlier version, we did try something similar to the latter and made it work, but it ends up being a lot more convoluted than our current proposal.

- Isn't it already possible to combine busy looping with irq deferral?

Yes, in fact enabling irq deferral via napi_defer_hard_irqs and gro_flush_timeout is a precondition for prefer_busy_poll to have an effect. If the application also uses a tight busy loop with essentially nonblocking epoll_wait (accomplished with a very short timeout parameter), this is the fullbusy case shown in the results. An application using blocking epoll_wait is shown as the napibusy case in the results. It's a hybrid approach that provides limited latency benefits compared to the base case and plain irq deferral, but not as good as fullbusy or suspend.

~ Special thanks

Several people were involved in earlier stages of the development of this mechanism whom we'd like to thank:

- Peter Cai (CC'd), for the initial kernel patch and his contributions to the paper.

- Mohammadamin Shafie (CC'd), for testing various versions of the kernel patch and providing helpful feedback.

Thanks,
Martin and Joe

[1]: https://lore.kernel.org/netdev/[email protected]/
[2]: https://doi.org/10.1145/3626780
[3]: https://github.com/memcached/memcached/blob/master/doc/napi_ids.txt
[4]: https://github.com/leverich/mutilate
[5]: https://raw.githubusercontent.com/martinkarsten/irqsuspend/main/patches/memcached.patch
[6]: https://raw.githubusercontent.com/martinkarsten/irqsuspend/main/patches/libevent.patch
[7]: https://github.com/martinkarsten/irqsuspend
[8]: https://github.com/martinkarsten/irqsuspend/tree/main/results

v6:
- Updated the cover letter with a full re-run of all test cases, including a new case suspend0, as requested by Sridhar previously.
- Updated the kernel documentation in patch 7 as suggested by Bagas Sanjaya, which improved the htmldoc output.

v5: https://lore.kernel.org/netdev/[email protected]/
- Adjusted patch 5 to only suspend IRQs when ep_send_events returns a positive return value. This issue was pointed out by Hillf Danton.
- Updated the commit message of patch 6 which still mentioned netcat, despite the code being updated in v4 to replace it with socat and fixed misspelling of netdevsim.
- Fixed a minor typo in patch 7 and removed an unnecessary paragraph.
- Added Sridhar Samudrala's Reviewed-by to patch 1-5 and 7.

v4: https://lore.kernel.org/netdev/[email protected]/
- Added a new FAQ item to cover letter.
- Updated patch 6 to use socat instead of nc in busy_poll_test.sh and updated busy_poller.c to use netlink directly to configure napi params.
- Updated the kernel documentation in patch 7 to include more details.
- Dropped Stanislav's Acked-by and Bagas' Reviewed-by from patch 7 since the documentation was updated.

v3: https://lore.kernel.org/netdev/[email protected]/
- Added Stanislav Fomichev's Acked-by to every patch except the newly added selftest.
- Added Bagas Sanjaya's Reviewed-by to the documentation patch.
- Fixed the commit message of patch 2 to remove a reference to the now non-existent sysfs setting.
- Added a self test which tests both "regular" busy poll and busy poll with suspend enabled. This was added as patch 6 as requested by Paolo. netdevsim was chosen instead of veth due to netdevsim's pre-existing support for netdev-genl. See the commit message of patch 6 for more details.

v2: https://lore.kernel.org/bpf/[email protected]/
- Cover letter updated, including a re-run of test data.
- Patch 1 rewritten to use netdev-genl instead of sysfs.
- Patch 3 updated with a comment added to napi_resume_irqs.
- Patch 4 rebased to apply now that commit b9ca079dd6b0 ("eventpoll:
Annotate data-race of busy_poll_usecs") has been picked up from VFS.
- Patch 6 updated the kernel documentation.

rfc -> v1:
- Cover letter updated to include more details.
- Patch 1 updated to remove the documentation added. This was moved to patch 6 with the rest of the docs (see below).
- Patch 5 updated to fix an error uncovered by the kernel build robot. See patch 5's changelog for more details.
- Patch 6 added which updates kernel documentation.

Joe Damato (2):
selftests: net: Add busy_poll_test
docs: networking: Describe irq suspension

Martin Karsten (5):
net: Add napi_struct parameter irq_suspend_timeout
net: Suspend softirq when prefer_busy_poll is set
net: Add control functions for irq suspension
eventpoll: Trigger napi_busy_loop, if prefer_busy_poll is set
eventpoll: Control irq suspension for prefer_busy_poll

 Documentation/netlink/specs/netdev.yaml       |   7 +
 Documentation/networking/napi.rst             | 170 ++++++++-
 fs/eventpoll.c                                |  36 +-
 include/linux/netdevice.h                     |   2 +
 include/net/busy_poll.h                       |   3 +
 include/uapi/linux/netdev.h                   |   1 +
 net/core/dev.c                                |  58 +++-
 net/core/dev.h                                |  25 ++
 net/core/netdev-genl-gen.c                    |   5 +-
 net/core/netdev-genl.c                        |  12 +
 tools/include/uapi/linux/netdev.h             |   1 +
 tools/testing/selftests/net/.gitignore        |   1 +
 tools/testing/selftests/net/Makefile          |   3 +-
 tools/testing/selftests/net/busy_poll_test.sh | 164 +++++++++
 tools/testing/selftests/net/busy_poller.c     | 328 ++++++++++++++++++
 15 files changed, 805 insertions(+), 11 deletions(-)
 create mode 100755 tools/testing/selftests/net/busy_poll_test.sh
 create mode 100644 tools/testing/selftests/net/busy_poller.c

base-commit: dbb9a7ef347828870df3e5e6ddf19469a3277fc9

Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v6 1/7 net: Add napi_struct parameter irq_suspend_timeout

From: Martin Karsten <[email protected]>

Add a per-NAPI IRQ suspension parameter, which can be get/set with netdev-genl.

This patch doesn't change any behavior but prepares the code for other changes in the following commits which use irq_suspend_timeout as a timeout for IRQ suspension.

---
v1 -> v2:
- rewrote this patch to make irq_suspend_timeout per-napi via netdev-genl.

rfc -> v1:
- removed napi.rst documentation from this patch; added to patch 6.

 Documentation/netlink/specs/netdev.yaml |  7 +++++++
 include/linux/netdevice.h               |  2 ++
 include/uapi/linux/netdev.h             |  1 +
 net/core/dev.c                          |  2 ++
 net/core/dev.h                          | 25 +++++++++++++++++++++++++
 net/core/netdev-genl-gen.c              |  5 +++--
 net/core/netdev-genl.c                  | 12 ++++++++++++
 tools/include/uapi/linux/netdev.h       |  1 +
 8 files changed, 53 insertions(+), 2 deletions(-)
Documentation/netlink/specs/netdev.yaml +7 -0
index f9cb97d6106c..cbb544bd6c84 100644
@@ -263,6 +263,11 @@ attribute-sets:
              the end of a NAPI cycle. This may add receive latency in exchange
              for reducing the number of frames processed by the network stack.
         type: uint
+      -
+        name: irq-suspend-timeout
+        doc: The timeout, in nanoseconds, of how long to suspend irq
+             processing, if event polling finds events
+        type: uint
   -
     name: queue
     attributes:
@@ -653,6 +658,7 @@ operations:
             - pid
             - defer-hard-irqs
             - gro-flush-timeout
+            - irq-suspend-timeout
       dump:
         request:
           attributes:
@@ -704,6 +710,7 @@ operations:
             - id
             - defer-hard-irqs
             - gro-flush-timeout
+            - irq-suspend-timeout
 
 kernel-family:
   headers: [ "linux/list.h"]
include/linux/netdevice.h +2 -0
index 3c552b648b27..c8ab5f08092b 100644
@@ -347,6 +347,7 @@ struct gro_list {
  */
 struct napi_config {
 	u64 gro_flush_timeout;
+	u64 irq_suspend_timeout;
 	u32 defer_hard_irqs;
 	unsigned int napi_id;
 };
@@ -383,6 +384,7 @@ struct napi_struct {
 	struct hrtimer		timer;
 	struct task_struct	*thread;
 	unsigned long		gro_flush_timeout;
+	unsigned long		irq_suspend_timeout;
 	u32			defer_hard_irqs;
 	/* control-path-only fields follow */
 	struct list_head	dev_list;
include/uapi/linux/netdev.h +1 -0
index e3ebb49f60d2..e4be227d3ad6 100644
@@ -124,6 +124,7 @@ enum {
 	NETDEV_A_NAPI_PID,
 	NETDEV_A_NAPI_DEFER_HARD_IRQS,
 	NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT,
+	NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT,
 
 	__NETDEV_A_NAPI_MAX,
 	NETDEV_A_NAPI_MAX = (__NETDEV_A_NAPI_MAX - 1)
net/core/dev.c +2 -0
index 6a31152e4606..4d910872963f 100644
@@ -6666,6 +6666,7 @@ static void napi_restore_config(struct napi_struct *n)
 {
 	n->defer_hard_irqs = n->config->defer_hard_irqs;
 	n->gro_flush_timeout = n->config->gro_flush_timeout;
+	n->irq_suspend_timeout = n->config->irq_suspend_timeout;
 	/* a NAPI ID might be stored in the config, if so use it. if not, use
 	 * napi_hash_add to generate one for us. It will be saved to the config
 	 * in napi_disable.
@@ -6680,6 +6681,7 @@ static void napi_save_config(struct napi_struct *n)
 {
 	n->config->defer_hard_irqs = n->defer_hard_irqs;
 	n->config->gro_flush_timeout = n->gro_flush_timeout;
+	n->config->irq_suspend_timeout = n->irq_suspend_timeout;
 	n->config->napi_id = n->napi_id;
 	napi_hash_del(n);
 }
net/core/dev.h +25 -0
index 7881bced70a9..d043dee25a68 100644
@@ -236,6 +236,31 @@ static inline void netdev_set_gro_flush_timeout(struct net_device *netdev,
 		netdev->napi_config[i].gro_flush_timeout = timeout;
 }
 
+/**
+ * napi_get_irq_suspend_timeout - get the irq_suspend_timeout
+ * @n: napi struct to get the irq_suspend_timeout from
+ *
+ * Return: the per-NAPI value of the irq_suspend_timeout field.
+ */
+static inline unsigned long
+napi_get_irq_suspend_timeout(const struct napi_struct *n)
+{
+	return READ_ONCE(n->irq_suspend_timeout);
+}
+
+/**
+ * napi_set_irq_suspend_timeout - set the irq_suspend_timeout for a napi
+ * @n: napi struct to set the irq_suspend_timeout
+ * @timeout: timeout value to set
+ *
+ * napi_set_irq_suspend_timeout sets the per-NAPI irq_suspend_timeout
+ */
+static inline void napi_set_irq_suspend_timeout(struct napi_struct *n,
+						unsigned long timeout)
+{
+	WRITE_ONCE(n->irq_suspend_timeout, timeout);
+}
+
 int rps_cpumask_housekeeping(struct cpumask *mask);
 
 #if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)
net/core/netdev-genl-gen.c +3 -2
index 21de7e10be16..a89cbd8d87c3 100644
@@ -92,10 +92,11 @@ static const struct nla_policy netdev_bind_rx_nl_policy[NETDEV_A_DMABUF_FD + 1]
 };
 
 /* NETDEV_CMD_NAPI_SET - do */
-static const struct nla_policy netdev_napi_set_nl_policy[NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT + 1] = {
+static const struct nla_policy netdev_napi_set_nl_policy[NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT + 1] = {
 	[NETDEV_A_NAPI_ID] = { .type = NLA_U32, },
 	[NETDEV_A_NAPI_DEFER_HARD_IRQS] = NLA_POLICY_FULL_RANGE(NLA_U32, &netdev_a_napi_defer_hard_irqs_range),
 	[NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT] = { .type = NLA_UINT, },
+	[NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT] = { .type = NLA_UINT, },
 };
 
 /* Ops table for netdev */
@@ -186,7 +187,7 @@ static const struct genl_split_ops netdev_nl_ops[] = {
 		.cmd		= NETDEV_CMD_NAPI_SET,
 		.doit		= netdev_nl_napi_set_doit,
 		.policy		= netdev_napi_set_nl_policy,
-		.maxattr	= NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT,
+		.maxattr	= NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT,
 		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
 	},
 };
net/core/netdev-genl.c +12 -0
index b49c3b4e5fbe..765ce7c9d73b 100644
@@ -161,6 +161,7 @@ static int
 netdev_nl_napi_fill_one(struct sk_buff *rsp, struct napi_struct *napi,
 			const struct genl_info *info)
 {
+	unsigned long irq_suspend_timeout;
 	unsigned long gro_flush_timeout;
 	u32 napi_defer_hard_irqs;
 	void *hdr;
@@ -196,6 +197,11 @@ netdev_nl_napi_fill_one(struct sk_buff *rsp, struct napi_struct *napi,
 			napi_defer_hard_irqs))
 		goto nla_put_failure;
 
+	irq_suspend_timeout = napi_get_irq_suspend_timeout(napi);
+	if (nla_put_uint(rsp, NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT,
+			 irq_suspend_timeout))
+		goto nla_put_failure;
+
 	gro_flush_timeout = napi_get_gro_flush_timeout(napi);
 	if (nla_put_uint(rsp, NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT,
 			 gro_flush_timeout))
@@ -306,6 +312,7 @@ int netdev_nl_napi_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
 static int
 netdev_nl_napi_set_config(struct napi_struct *napi, struct genl_info *info)
 {
+	u64 irq_suspend_timeout = 0;
 	u64 gro_flush_timeout = 0;
 	u32 defer = 0;
 
@@ -314,6 +321,11 @@ netdev_nl_napi_set_config(struct napi_struct *napi, struct genl_info *info)
 		napi_set_defer_hard_irqs(napi, defer);
 	}
 
+	if (info->attrs[NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT]) {
+		irq_suspend_timeout = nla_get_uint(info->attrs[NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT]);
+		napi_set_irq_suspend_timeout(napi, irq_suspend_timeout);
+	}
+
 	if (info->attrs[NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT]) {
 		gro_flush_timeout = nla_get_uint(info->attrs[NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT]);
 		napi_set_gro_flush_timeout(napi, gro_flush_timeout);
tools/include/uapi/linux/netdev.h +1 -0
index e3ebb49f60d2..e4be227d3ad6 100644
@@ -124,6 +124,7 @@ enum {
 	NETDEV_A_NAPI_PID,
 	NETDEV_A_NAPI_DEFER_HARD_IRQS,
 	NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT,
+	NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT,
 
 	__NETDEV_A_NAPI_MAX,
 	NETDEV_A_NAPI_MAX = (__NETDEV_A_NAPI_MAX - 1)
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v6 2/7 net: Suspend softirq when prefer_busy_poll is set

In reply to Joe Damato

From: Martin Karsten <[email protected]>

When NAPI_F_PREFER_BUSY_POLL is set during busy_poll_stop and the irq_suspend_timeout is nonzero, this timeout is used to defer softirq scheduling, potentially longer than gro_flush_timeout. This can be used to effectively suspend softirq processing during the time it takes for an application to process data and return to the next busy-poll.

The call to napi->poll in busy_poll_stop might lead to an invocation of napi_complete_done, but the prefer-busy flag is still set at that time, so the same logic is used to defer softirq scheduling for irq_suspend_timeout.

---
v3:
- Removed reference to non-existent sysfs parameter from commit message. No functional/code changes.

 net/core/dev.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
net/core/dev.c +13 -4
index 4d910872963f..51d88f758e2e 100644
@@ -6239,7 +6239,12 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 			timeout = napi_get_gro_flush_timeout(n);
 		n->defer_hard_irqs_count = napi_get_defer_hard_irqs(n);
 	}
-	if (n->defer_hard_irqs_count > 0) {
+	if (napi_prefer_busy_poll(n)) {
+		timeout = napi_get_irq_suspend_timeout(n);
+		if (timeout)
+			ret = false;
+	}
+	if (ret && n->defer_hard_irqs_count > 0) {
 		n->defer_hard_irqs_count--;
 		timeout = napi_get_gro_flush_timeout(n);
 		if (timeout)
@@ -6375,9 +6380,13 @@ static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock,
 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
 
 	if (flags & NAPI_F_PREFER_BUSY_POLL) {
-		napi->defer_hard_irqs_count = napi_get_defer_hard_irqs(napi);
-		timeout = napi_get_gro_flush_timeout(napi);
-		if (napi->defer_hard_irqs_count && timeout) {
+		timeout = napi_get_irq_suspend_timeout(napi);
+		if (!timeout) {
+			napi->defer_hard_irqs_count = napi_get_defer_hard_irqs(napi);
+			if (napi->defer_hard_irqs_count)
+				timeout = napi_get_gro_flush_timeout(napi);
+		}
+		if (timeout) {
 			hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED);
 			skip_schedule = true;
 		}
Signature · 1 line
2.25.1
maintainer
#3 lore raw

Re: [PATCH net-next v6 2/7] net: Suspend softirq when prefer_busy_poll is set

Quoted 9 lines · On Mon, 4 Nov 2024 21:55:26 +0000 Joe Damato wrote:

From: Martin Karsten <[email protected]>

When NAPI_F_PREFER_BUSY_POLL is set during busy_poll_stop and the irq_suspend_timeout is nonzero, this timeout is used to defer softirq scheduling, potentially longer than gro_flush_timeout. This can be used to effectively suspend softirq processing during the time it takes for an application to process data and return to the next busy-poll.

The call to napi->poll in busy_poll_stop might lead to an invocation of

The call to napi->poll when we're arming the timer is counter productive, right? Maybe we can take this opportunity to add the seemingly missing logic to skip over it?

Quoted 24 lines

napi_complete_done, but the prefer-busy flag is still set at that time, so the same logic is used to defer softirq scheduling for irq_suspend_timeout.

---
v3:
- Removed reference to non-existent sysfs parameter from commit message. No functional/code changes.

 net/core/dev.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
net/core/dev.c +0 -0
index 4d910872963f..51d88f758e2e 100644

net/core/dev.c bool napi_complete_done(struct napi_struct *n, int work_done) · line 6243

Quoted 6 lines
net/core/dev.c +2 -1
@@ -6239,7 +6239,12 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 			timeout = napi_get_gro_flush_timeout(n);
 		n->defer_hard_irqs_count = napi_get_defer_hard_irqs(n);
 	}
-	if (n->defer_hard_irqs_count > 0) {
+	if (napi_prefer_busy_poll(n)) {
+		timeout = napi_get_irq_suspend_timeout(n);

Why look at the suspend timeout in napi_complete_done()? We are unlikely to be exiting busy poll here. Is it because we need more time than gro_flush_timeout for the application to take over the polling?

Quoted 8 lines
net/core/dev.c +4 -0
+		if (timeout)
+			ret = false;
+	}
+	if (ret && n->defer_hard_irqs_count > 0) {
 		n->defer_hard_irqs_count--;
 		timeout = napi_get_gro_flush_timeout(n);
 		if (timeout)

net/core/dev.c static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, · line 6383

Quoted 7 lines
net/core/dev.c +1 -3
@@ -6375,9 +6380,13 @@ static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock,
 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
 
 	if (flags & NAPI_F_PREFER_BUSY_POLL) {
-		napi->defer_hard_irqs_count = napi_get_defer_hard_irqs(napi);
-		timeout = napi_get_gro_flush_timeout(napi);
-		if (napi->defer_hard_irqs_count && timeout) {
+		timeout = napi_get_irq_suspend_timeout(napi);

Even here I'm not sure if we need to trigger suspend.
I don't know the eventpoll code well but it seems like you suspend and resume based on events when exiting epoll. Why also here?

Quoted 9 lines
net/core/dev.c +6 -0
+		if (!timeout) {
+			napi->defer_hard_irqs_count = napi_get_defer_hard_irqs(napi);
+			if (napi->defer_hard_irqs_count)
+				timeout = napi_get_gro_flush_timeout(napi);
+		}
+		if (timeout) {
 			hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED);
 			skip_schedule = true;
 		}
author
#4 lore raw

Re: [PATCH net-next v6 2/7] net: Suspend softirq when prefer_busy_poll is set

Quoted 9 lines · Jakub Kicinski
Quoted 8 lines · On Mon, 4 Nov 2024 21:55:26 +0000 Joe Damato wrote:

From: Martin Karsten <[email protected]>

When NAPI_F_PREFER_BUSY_POLL is set during busy_poll_stop and the irq_suspend_timeout is nonzero, this timeout is used to defer softirq scheduling, potentially longer than gro_flush_timeout. This can be used to effectively suspend softirq processing during the time it takes for an application to process data and return to the next busy-poll.

Quoted 5 lines
Quoted 1 line

The call to napi->poll in busy_poll_stop might lead to an invocation of

The call to napi->poll when we're arming the timer is counter productive, right? Maybe we can take this opportunity to add the seemingly missing logic to skip over it?

It seems like the call to napi->poll in busy_poll_stop is counter productive and we're not opposed to making an optimization like that in the future.

When we tried it, it triggered several bugs/system hangs, so we left as much of the original code in place as possible.

The existing patch works and streamlining busy_poll_stop to skip the call to napi->poll is an optimization that can be added as a later series that focuses solely on when/where/how napi->poll is called.

Our focus was on:
- Not breaking any of the existing mechanisms
- Adding a new mechanism

I think we should avoid pulling the optimization you suggest into this particular series and save that for the future.

Quoted 19 lines
Quoted 19 lines

napi_complete_done, but the prefer-busy flag is still set at that time, so the same logic is used to defer softirq scheduling for irq_suspend_timeout.

---
v3:
- Removed reference to non-existent sysfs parameter from commit message. No functional/code changes.

 net/core/dev.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
Quoted 14 lines

net/core/dev.c bool napi_complete_done(struct napi_struct *n, int work_done) · line 6243

Quoted 11 lines
net/core/dev.c +2 -1
index 4d910872963f..51d88f758e2e 100644
@@ -6239,7 +6239,12 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 			timeout = napi_get_gro_flush_timeout(n);
 		n->defer_hard_irqs_count = napi_get_defer_hard_irqs(n);
 	}
-	if (n->defer_hard_irqs_count > 0) {
+	if (napi_prefer_busy_poll(n)) {
+		timeout = napi_get_irq_suspend_timeout(n);

Why look at the suspend timeout in napi_complete_done()? We are unlikely to be exiting busy poll here.

The idea is similar to commit 7fd3253a7de6 ("net: Introduce preferred busy-polling"); continue to defer IRQs as long as forward progress is being made. In this case, napi->poll ran, called napi_complete_done -- the system is moving forward with processing so prevent IRQs from interrupting us.

epoll_wait will re-enable IRQs (by calling napi_schedule) if there are no events ready for processing.

Quoted 2 lines

Is it because we need more time than gro_flush_timeout for the application to take over the polling?

That's right; we want the application to retain control of packet processing. That's why we connected this to the "prefer_busy_poll" flag.

Quoted 10 lines
Quoted 10 lines
net/core/dev.c +4 -0
+		if (timeout)
+			ret = false;
+	}
+	if (ret && n->defer_hard_irqs_count > 0) {
 		n->defer_hard_irqs_count--;
 		timeout = napi_get_gro_flush_timeout(n);
 		if (timeout)
@@ -6375,9 +6380,13 @@ static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock,
 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
 
Quoted 9 lines

net/core/dev.c static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, · line 6383

Quoted 5 lines
net/core/dev.c +1 -3
 	if (flags & NAPI_F_PREFER_BUSY_POLL) {
-		napi->defer_hard_irqs_count = napi_get_defer_hard_irqs(napi);
-		timeout = napi_get_gro_flush_timeout(napi);
-		if (napi->defer_hard_irqs_count && timeout) {
+		timeout = napi_get_irq_suspend_timeout(napi);

Even here I'm not sure if we need to trigger suspend.
I don't know the eventpoll code well but it seems like you suspend and resume based on events when exiting epoll. Why also here?

There's two questions wrapped up here and an overall point to make:

1. Suspend and resume based on events when exiting epoll - that's right and as you'll see in those patches that happens by:
- arming the suspend timer (via a call to napi_suspend_irqs) when a positive number of events are retrieved
- calling napi_schedule (via napi_resume_irqs) when there are no events or the epoll context is being freed.

2. Why defer the suspend timer here in busy_poll_stop? Note that the original code would set the timer to gro_flush_timeout, which would introduce the trade offs we mention in the cover letter (latency for large values, IRQ interruption for small values).

We don't want the gro_flush_timeout to take over yet because we want to avoid these tradeoffs up until the point where epoll_wait finds no events for processing.

Does that make sense? If we skipped the IRQ suspend deferral here, we'd be giving packet processing control back to gro_flush_timeout and napi_defer_hard_irqs, but the system might still have packets that can be processed in the next call to epoll_wait.

The overall point to make is that: the suspend timer is used to prevent misbehaving userland applications from taking too long. It's essentially a backstop and, as long as the app is making forward progress, allows the app to continue running its busy poll loop undisturbed (via napi_complete_done preventing the driver from enabling IRQs).

Does that make sense?

Quoted 10 lines
Quoted 9 lines
net/core/dev.c +6 -0
+		if (!timeout) {
+			napi->defer_hard_irqs_count = napi_get_defer_hard_irqs(napi);
+			if (napi->defer_hard_irqs_count)
+				timeout = napi_get_gro_flush_timeout(napi);
+		}
+		if (timeout) {
 			hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED);
 			skip_schedule = true;
 		}
maintainer
#5 lore raw

Re: [PATCH net-next v6 2/7] net: Suspend softirq when prefer_busy_poll is set

Quoted 16 lines · On Wed, 6 Nov 2024 08:52:00 -0800 Joe Damato wrote:
Quoted 9 lines · Jakub Kicinski
Quoted 8 lines · On Mon, 4 Nov 2024 21:55:26 +0000 Joe Damato wrote:

From: Martin Karsten <[email protected]>

When NAPI_F_PREFER_BUSY_POLL is set during busy_poll_stop and the irq_suspend_timeout is nonzero, this timeout is used to defer softirq scheduling, potentially longer than gro_flush_timeout. This can be used to effectively suspend softirq processing during the time it takes for an application to process data and return to the next busy-poll.

Quoted 5 lines
Quoted 1 line

The call to napi->poll in busy_poll_stop might lead to an invocation of

The call to napi->poll when we're arming the timer is counter productive, right? Maybe we can take this opportunity to add the seemingly missing logic to skip over it?

Quoted 6 lines

It seems like the call to napi->poll in busy_poll_stop is counter productive and we're not opposed to making an optimization like that in the future.

When we tried it, it triggered several bugs/system hangs, so we left as much of the original code in place as possible.

You don't happen to have the patch you used? Many ways to get the skipping wrong.

Quoted 3 lines

The existing patch works and streamlining busy_poll_stop to skip the call to napi->poll is an optimization that can be added as a later series that focuses solely on when/where/how napi->poll is called.

The reason I brought it up is that it rearms the timer, if driver ends up calling napi_complete_done(). So we arm the timer in napi_poll_stop(), then call the driver which may rearm again, making the already complex code even harder to reason about.

Quoted 6 lines

Our focus was on:
- Not breaking any of the existing mechanisms
- Adding a new mechanism

I think we should avoid pulling the optimization you suggest into this particular series and save that for the future.

I'm primarily worried about maintainability of this code.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
net/core/dev.c +0 -0
index 4d910872963f..51d88f758e2e 100644
@@ -6239,7 +6239,12 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 			timeout = napi_get_gro_flush_timeout(n);
Quoted 8 lines

net/core/dev.c bool napi_complete_done(struct napi_struct *n, int work_done) · line 6243

Quoted 5 lines
net/core/dev.c +2 -1
 		n->defer_hard_irqs_count = napi_get_defer_hard_irqs(n);
 	}
-	if (n->defer_hard_irqs_count > 0) {
+	if (napi_prefer_busy_poll(n)) {
+		timeout = napi_get_irq_suspend_timeout(n);  

Why look at the suspend timeout in napi_complete_done()? We are unlikely to be exiting busy poll here.

Quoted 5 lines

The idea is similar to commit 7fd3253a7de6 ("net: Introduce preferred busy-polling"); continue to defer IRQs as long as forward progress is being made. In this case, napi->poll ran, called napi_complete_done -- the system is moving forward with processing so prevent IRQs from interrupting us.

We should clarify the mental models. You're describing IRQ deferal, but say prefer busy poll.

Prefer busy poll has only one function - if we are at 100% busy and always see >= budget of packets on the ring, we never call napi_complete_done(). Drivers don't call napi_complete_done() if they consumed full budget. So we need a way to break that re-polling loop, release the NAPI ownership and give busy poll a chance to claim the NAPI instance ownership (the SCHED bit). We check for prefer busy poll in __napi_poll(), because, again, in the target scenario napi_complete_done() is never called.

The IRQ deferal mechanism is necessary for prefer busy poll to work, but it's separate and used by some drivers without good IRQ coalescing, no user space polling involved.

In your case, when machine is not melting under 100% load - prefer busy poll will be set once or not at all.

Quoted 2 lines

epoll_wait will re-enable IRQs (by calling napi_schedule) if there are no events ready for processing.

To be 100% precise calling napi_schedule will not reenable IRQs if IRQ deferal is active. It only guarantees one NAPI run in softirq (or thread if threaded).

Quoted 45 lines
Quoted 2 lines

Is it because we need more time than gro_flush_timeout for the application to take over the polling?

That's right; we want the application to retain control of packet processing. That's why we connected this to the "prefer_busy_poll" flag.

Quoted 10 lines
Quoted 10 lines
net/core/dev.c +4 -0
+		if (timeout)
+			ret = false;
+	}
+	if (ret && n->defer_hard_irqs_count > 0) {
 		n->defer_hard_irqs_count--;
 		timeout = napi_get_gro_flush_timeout(n);
 		if (timeout)
@@ -6375,9 +6380,13 @@ static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock,
 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
 
Quoted 9 lines

net/core/dev.c static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, · line 6383

Quoted 5 lines
net/core/dev.c +1 -3
 	if (flags & NAPI_F_PREFER_BUSY_POLL) {
-		napi->defer_hard_irqs_count = napi_get_defer_hard_irqs(napi);
-		timeout = napi_get_gro_flush_timeout(napi);
-		if (napi->defer_hard_irqs_count && timeout) {
+		timeout = napi_get_irq_suspend_timeout(napi);  

Even here I'm not sure if we need to trigger suspend.
I don't know the eventpoll code well but it seems like you suspend and resume based on events when exiting epoll. Why also here?

There's two questions wrapped up here and an overall point to make:

1. Suspend and resume based on events when exiting epoll - that's right and as you'll see in those patches that happens by:
- arming the suspend timer (via a call to napi_suspend_irqs) when a positive number of events are retrieved
- calling napi_schedule (via napi_resume_irqs) when there are no events or the epoll context is being freed.

2. Why defer the suspend timer here in busy_poll_stop? Note that the original code would set the timer to gro_flush_timeout, which would introduce the trade offs we mention in the cover letter (latency for large values, IRQ interruption for small values).

We don't want the gro_flush_timeout to take over yet because we want to avoid these tradeoffs up until the point where epoll_wait finds no events for processing.

Quoted 5 lines

Does that make sense? If we skipped the IRQ suspend deferral here, we'd be giving packet processing control back to gro_flush_timeout and napi_defer_hard_irqs, but the system might still have packets that can be processed in the next call to epoll_wait.

Let me tell you what I think happens and then you can correct me.

0 epoll
1 # ..does its magic..
2 __napi_busy_loop()
3 # finds a waking packet
4 busy_poll_stop()
5 # arms the timer for long suspend
6 # epoll sees events
7 ep_suspend_napi_irqs()
8 napi_suspend_irqs()
9 # arms for long timer again

The timer we arm here only has to survive from line 5 to line 9, because we will override the timeout on line 9.

Quoted 8 lines

The overall point to make is that: the suspend timer is used to prevent misbehaving userland applications from taking too long. It's essentially a backstop and, as long as the app is making forward progress, allows the app to continue running its busy poll loop undisturbed (via napi_complete_done preventing the driver from enabling IRQs).

Does that make sense?

My mental model put in yet another way is that only epoll knows if it has events, and therefore whether the timeout should be short or long. So the suspend timer should only be applied by epoll.

author
#6 lore raw

Re: [PATCH net-next v6 2/7] net: Suspend softirq when prefer_busy_poll is set

Quoted 21 lines · Jakub Kicinski
Quoted 20 lines · On Wed, 6 Nov 2024 08:52:00 -0800 Joe Damato wrote:
Quoted 9 lines · Jakub Kicinski
Quoted 8 lines · On Mon, 4 Nov 2024 21:55:26 +0000 Joe Damato wrote:

From: Martin Karsten <[email protected]>

When NAPI_F_PREFER_BUSY_POLL is set during busy_poll_stop and the irq_suspend_timeout is nonzero, this timeout is used to defer softirq scheduling, potentially longer than gro_flush_timeout. This can be used to effectively suspend softirq processing during the time it takes for an application to process data and return to the next busy-poll.

Quoted 5 lines
Quoted 1 line

The call to napi->poll in busy_poll_stop might lead to an invocation of

The call to napi->poll when we're arming the timer is counter productive, right? Maybe we can take this opportunity to add the seemingly missing logic to skip over it?

It seems like the call to napi->poll in busy_poll_stop is counter productive and we're not opposed to making an optimization like that in the future.

Quoted 5 lines
Quoted 2 lines

When we tried it, it triggered several bugs/system hangs, so we left as much of the original code in place as possible.

You don't happen to have the patch you used? Many ways to get the skipping wrong.

Please see below; I think we've found a solution.

Quoted 8 lines
Quoted 3 lines

The existing patch works and streamlining busy_poll_stop to skip the call to napi->poll is an optimization that can be added as a later series that focuses solely on when/where/how napi->poll is called.

The reason I brought it up is that it rearms the timer, if driver ends up calling napi_complete_done(). So we arm the timer in napi_poll_stop(), then call the driver which may rearm again, making the already complex code even harder to reason about.

Agreed that the timer is unnecessarily re-armed twice.

Martin ran some initial tests of this series but with this patch (patch 2) dropped and the initial results from a small number of runs seem fine.

In other words: I think we can simply drop this patch entirely, re-run our tests to regenerate the data, update the documentation, and send a v7.

But please continue reading below.

Quoted 8 lines
Quoted 6 lines

Our focus was on:
- Not breaking any of the existing mechanisms
- Adding a new mechanism

I think we should avoid pulling the optimization you suggest into this particular series and save that for the future.

I'm primarily worried about maintainability of this code.

Of course and we're open to figuring out how to help with that.

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

net/core/dev.c bool napi_complete_done(struct napi_struct *n, int work_done) · line 6243

Quoted 11 lines
net/core/dev.c +2 -1
index 4d910872963f..51d88f758e2e 100644
@@ -6239,7 +6239,12 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 			timeout = napi_get_gro_flush_timeout(n);
 		n->defer_hard_irqs_count = napi_get_defer_hard_irqs(n);
 	}
-	if (n->defer_hard_irqs_count > 0) {
+	if (napi_prefer_busy_poll(n)) {
+		timeout = napi_get_irq_suspend_timeout(n);  

Why look at the suspend timeout in napi_complete_done()? We are unlikely to be exiting busy poll here.

Quoted 8 lines
Quoted 5 lines

The idea is similar to commit 7fd3253a7de6 ("net: Introduce preferred busy-polling"); continue to defer IRQs as long as forward progress is being made. In this case, napi->poll ran, called napi_complete_done -- the system is moving forward with processing so prevent IRQs from interrupting us.

We should clarify the mental models. You're describing IRQ deferal, but say prefer busy poll.

OK; we're open to using different language if that would be helpful.

Quoted 9 lines

Prefer busy poll has only one function - if we are at 100% busy and always see >= budget of packets on the ring, we never call napi_complete_done(). Drivers don't call napi_complete_done() if they consumed full budget. So we need a way to break that re-polling loop, release the NAPI ownership and give busy poll a chance to claim the NAPI instance ownership (the SCHED bit). We check for prefer busy poll in __napi_poll(), because, again, in the target scenario napi_complete_done() is never called.

Quoted 3 lines

The IRQ deferal mechanism is necessary for prefer busy poll to work, but it's separate and used by some drivers without good IRQ coalescing, no user space polling involved.

Sure, we agree and are on the same page on the above about what prefer busy poll is and its interaction with IRQ deferral.

Quoted 2 lines

In your case, when machine is not melting under 100% load - prefer busy poll will be set once or not at all.

I am not sure what you mean by that last sentence, because the prefer busy poll flag is set by the application?

Similar to prefer busy poll piggybacking on IRQ deferral, we piggyback on prefer busy polling by allowing the application to use an even larger timeout while it is processing incoming data, i.e., deferring IRQs for a longer period, but only after a successful busy poll. This makes prefer busy poll + irq suspend useful when utilization is below 100%.

Quoted 6 lines
Quoted 2 lines

epoll_wait will re-enable IRQs (by calling napi_schedule) if there are no events ready for processing.

To be 100% precise calling napi_schedule will not reenable IRQs if IRQ deferal is active. It only guarantees one NAPI run in softirq (or thread if threaded).

Yes, I should have been more precise.

Calling napi_schedule doesn't enable IRQs, but runs NAPI which sets about the process of *eventually* causing napi_complete_done to return true which triggers the re-arming of IRQs by the driver.

Quoted 64 lines
Quoted 45 lines
Quoted 2 lines

Is it because we need more time than gro_flush_timeout for the application to take over the polling?

That's right; we want the application to retain control of packet processing. That's why we connected this to the "prefer_busy_poll" flag.

Quoted 10 lines
Quoted 10 lines
net/core/dev.c +4 -0
+		if (timeout)
+			ret = false;
+	}
+	if (ret && n->defer_hard_irqs_count > 0) {
 		n->defer_hard_irqs_count--;
 		timeout = napi_get_gro_flush_timeout(n);
 		if (timeout)
@@ -6375,9 +6380,13 @@ static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock,
 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
 
Quoted 9 lines

net/core/dev.c static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, · line 6383

Quoted 5 lines
net/core/dev.c +1 -3
 	if (flags & NAPI_F_PREFER_BUSY_POLL) {
-		napi->defer_hard_irqs_count = napi_get_defer_hard_irqs(napi);
-		timeout = napi_get_gro_flush_timeout(napi);
-		if (napi->defer_hard_irqs_count && timeout) {
+		timeout = napi_get_irq_suspend_timeout(napi);  

Even here I'm not sure if we need to trigger suspend.
I don't know the eventpoll code well but it seems like you suspend and resume based on events when exiting epoll. Why also here?

There's two questions wrapped up here and an overall point to make:

1. Suspend and resume based on events when exiting epoll - that's right and as you'll see in those patches that happens by:
- arming the suspend timer (via a call to napi_suspend_irqs) when a positive number of events are retrieved
- calling napi_schedule (via napi_resume_irqs) when there are no events or the epoll context is being freed.

2. Why defer the suspend timer here in busy_poll_stop? Note that the original code would set the timer to gro_flush_timeout, which would introduce the trade offs we mention in the cover letter (latency for large values, IRQ interruption for small values).

We don't want the gro_flush_timeout to take over yet because we want to avoid these tradeoffs up until the point where epoll_wait finds no events for processing.

Quoted 5 lines

Does that make sense? If we skipped the IRQ suspend deferral here, we'd be giving packet processing control back to gro_flush_timeout and napi_defer_hard_irqs, but the system might still have packets that can be processed in the next call to epoll_wait.

Let me tell you what I think happens and then you can correct me.

0 epoll
1 # ..does its magic..
2 __napi_busy_loop()
3 # finds a waking packet
4 busy_poll_stop()
5 # arms the timer for long suspend
6 # epoll sees events
7 ep_suspend_napi_irqs()
8 napi_suspend_irqs()
9 # arms for long timer again

Quoted 2 lines

The timer we arm here only has to survive from line 5 to line 9, because we will override the timeout on line 9.

Yes, you are right. Thanks for highlighting and catching this.

Quoted 7 lines
Quoted 7 lines

The overall point to make is that: the suspend timer is used to prevent misbehaving userland applications from taking too long. It's essentially a backstop and, as long as the app is making forward progress, allows the app to continue running its busy poll loop undisturbed (via napi_complete_done preventing the driver from enabling IRQs).

Quoted 5 lines
Quoted 1 line

Does that make sense?

My mental model put in yet another way is that only epoll knows if it has events, and therefore whether the timeout should be short or long. So the suspend timer should only be applied by epoll.

Here's what we are thinking, can you let me know if you agree with this?

- We can drop patch 2 entirely
- Update the documentation about IRQ suspension as needed now that patch 2 has been dropped
- Leave the rest of the series as is
- Re-run our tests to gather sufficient data for the test cases outlined in the cover letter to ensure that the performance numbers hold over several iterations

Does that seem reasonable for the v7 to you?

I am asking because generating the amount of data over the number of scenarios we are testing takes a long time and I want to make sure we are as aligned as we can be before I kick off another run :)

author
#7 lore raw

Re: [PATCH net-next v6 2/7] net: Suspend softirq when prefer_busy_poll is set

Quoted 4 lines · Joe Damato
Quoted 3 lines · Jakub Kicinski
Quoted 2 lines · On Wed, 6 Nov 2024 08:52:00 -0800 Joe Damato wrote:
Quoted 1 line · Jakub Kicinski

On Mon, 4 Nov 2024 21:55:26 +0000 Joe Damato wrote:

[...]

Quoted 40 lines
Quoted 11 lines

0 epoll
1 # ..does its magic..
2 __napi_busy_loop()
3 # finds a waking packet
4 busy_poll_stop()
5 # arms the timer for long suspend
6 # epoll sees events
7 ep_suspend_napi_irqs()
8 napi_suspend_irqs()
9 # arms for long timer again

Quoted 2 lines

The timer we arm here only has to survive from line 5 to line 9, because we will override the timeout on line 9.

Yes, you are right. Thanks for highlighting and catching this.

Quoted 7 lines
Quoted 7 lines

The overall point to make is that: the suspend timer is used to prevent misbehaving userland applications from taking too long. It's essentially a backstop and, as long as the app is making forward progress, allows the app to continue running its busy poll loop undisturbed (via napi_complete_done preventing the driver from enabling IRQs).

Quoted 5 lines
Quoted 1 line

Does that make sense?

My mental model put in yet another way is that only epoll knows if it has events, and therefore whether the timeout should be short or long. So the suspend timer should only be applied by epoll.

Here's what we are thinking, can you let me know if you agree with this?

- We can drop patch 2 entirely
- Update the documentation about IRQ suspension as needed now that patch 2 has been dropped
- Leave the rest of the series as is
- Re-run our tests to gather sufficient data for the test cases outlined in the cover letter to ensure that the performance numbers hold over several iterations

Quoted 5 lines

Does that seem reasonable for the v7 to you?

I am asking because generating the amount of data over the number of scenarios we are testing takes a long time and I want to make sure we are as aligned as we can be before I kick off another run :)

I just noticed this was marked "changes requested". I re-ran the tests overnight and have the data to confirm results are the same even after dropping patch 2, which simplifies the code and removes the double arming of the timer.

I wasn't sure if you were asking for other changes other than dropping patch 2, but since I have the data I'm going to proceed as specified in my previous email above:
- Drop patch 2
- Update cover letter with new data
- Send that as v7

Unless you'd like me to hold off for some reason? Or if there was some other feedback I need to address that I am missing?

maintainer
#8 lore raw

Re: [PATCH net-next v6 2/7] net: Suspend softirq when prefer_busy_poll is set

In reply to Joe Damato
Quoted 5 lines · On Wed, 6 Nov 2024 19:24:32 -0800 Joe Damato wrote:
Quoted 2 lines

In your case, when machine is not melting under 100% load - prefer busy poll will be set once or not at all.

I am not sure what you mean by that last sentence, because the prefer busy poll flag is set by the application?

There are two flags with almost exactly the same name, that's probably the source of misunderstanding. NAPI_F_PREFER_BUSY_POLL will always be set, but unless we are actually in a "napi live lock" NAPI_STATE_PREFER_BUSY_POLL will not get set, and the latter is what napi_prefer_busy_poll() tests. But we're dropping this patch so doesn't matter. I was trying to pile up reasons why checking napi_prefer_busy_poll() should not be necessary.

Quoted 31 lines

Similar to prefer busy poll piggybacking on IRQ deferral, we piggyback on prefer busy polling by allowing the application to use an even larger timeout while it is processing incoming data, i.e., deferring IRQs for a longer period, but only after a successful busy poll. This makes prefer busy poll + irq suspend useful when utilization is below 100%.

Quoted 7 lines
Quoted 7 lines

The overall point to make is that: the suspend timer is used to prevent misbehaving userland applications from taking too long. It's essentially a backstop and, as long as the app is making forward progress, allows the app to continue running its busy poll loop undisturbed (via napi_complete_done preventing the driver from enabling IRQs).

Quoted 5 lines
Quoted 1 line

Does that make sense?

My mental model put in yet another way is that only epoll knows if it has events, and therefore whether the timeout should be short or long. So the suspend timer should only be applied by epoll.

Here's what we are thinking, can you let me know if you agree with this?

- We can drop patch 2 entirely
- Update the documentation about IRQ suspension as needed now that patch 2 has been dropped
- Leave the rest of the series as is
- Re-run our tests to gather sufficient data for the test cases outlined in the cover letter to ensure that the performance numbers hold over several iterations

Quoted 5 lines

Does that seem reasonable for the v7 to you?

I am asking because generating the amount of data over the number of scenarios we are testing takes a long time and I want to make sure we are as aligned as we can be before I kick off another run :)

SGTM, the rest of the series makes perfect sense to me. Sorry for the delay..

author
#9 lore raw

PATCH v6 3/7 net: Add control functions for irq suspension

In reply to Joe Damato

From: Martin Karsten <[email protected]>

The napi_suspend_irqs routine bootstraps irq suspension by elongating the defer timeout to irq_suspend_timeout.

The napi_resume_irqs routine effectively cancels irq suspension by forcing the napi to be scheduled immediately.

---
v1 -> v2:
- Added a comment to napi_resume_irqs.

 include/net/busy_poll.h |  3 +++
 net/core/dev.c          | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)
include/net/busy_poll.h +3 -0
index f03040baaefd..c858270141bc 100644
@@ -52,6 +52,9 @@ void napi_busy_loop_rcu(unsigned int napi_id,
 			bool (*loop_end)(void *, unsigned long),
 			void *loop_end_arg, bool prefer_busy_poll, u16 budget);
 
+void napi_suspend_irqs(unsigned int napi_id);
+void napi_resume_irqs(unsigned int napi_id);
+
 #else /* CONFIG_NET_RX_BUSY_POLL */
 static inline unsigned long net_busy_loop_on(void)
 {
net/core/dev.c +39 -0
index 51d88f758e2e..9d903ce0c2b0 100644
@@ -6516,6 +6516,45 @@ void napi_busy_loop(unsigned int napi_id,
 }
 EXPORT_SYMBOL(napi_busy_loop);
 
+void napi_suspend_irqs(unsigned int napi_id)
+{
+	struct napi_struct *napi;
+
+	rcu_read_lock();
+	napi = napi_by_id(napi_id);
+	if (napi) {
+		unsigned long timeout = napi_get_irq_suspend_timeout(napi);
+
+		if (timeout)
+			hrtimer_start(&napi->timer, ns_to_ktime(timeout),
+				      HRTIMER_MODE_REL_PINNED);
+	}
+	rcu_read_unlock();
+}
+EXPORT_SYMBOL(napi_suspend_irqs);
+
+void napi_resume_irqs(unsigned int napi_id)
+{
+	struct napi_struct *napi;
+
+	rcu_read_lock();
+	napi = napi_by_id(napi_id);
+	if (napi) {
+		/* If irq_suspend_timeout is set to 0 between the call to
+		 * napi_suspend_irqs and now, the original value still
+		 * determines the safety timeout as intended and napi_watchdog
+		 * will resume irq processing.
+		 */
+		if (napi_get_irq_suspend_timeout(napi)) {
+			local_bh_disable();
+			napi_schedule(napi);
+			local_bh_enable();
+		}
+	}
+	rcu_read_unlock();
+}
+EXPORT_SYMBOL(napi_resume_irqs);
+
 #endif /* CONFIG_NET_RX_BUSY_POLL */
 
 static void __napi_hash_add_with_id(struct napi_struct *napi,
Signature · 1 line
2.25.1
author
#10 lore raw

PATCH v6 4/7 eventpoll: Trigger napi_busy_loop, if prefer_busy_poll is set

In reply to Joe Damato

From: Martin Karsten <[email protected]>

Setting prefer_busy_poll now leads to an effectively nonblocking iteration though napi_busy_loop, even when busy_poll_usecs is 0.

---
v1 -> v2:
- Rebased to apply now that commit b9ca079dd6b0 ("eventpoll: Annotate data-race of busy_poll_usecs") has been picked up from VFS.

 fs/eventpoll.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/eventpoll.c +3 -1
index 1ae4542f0bd8..f9e0d9307dad 100644
@@ -420,7 +420,9 @@ static bool busy_loop_ep_timeout(unsigned long start_time,
 
 static bool ep_busy_loop_on(struct eventpoll *ep)
 {
-	return !!READ_ONCE(ep->busy_poll_usecs) || net_busy_loop_on();
+	return !!READ_ONCE(ep->busy_poll_usecs) ||
+	       READ_ONCE(ep->prefer_busy_poll) ||
+	       net_busy_loop_on();
 }
 
 static bool ep_busy_loop_end(void *p, unsigned long start_time)
Signature · 1 line
2.25.1
author
#11 lore raw

PATCH v6 5/7 eventpoll: Control irq suspension for prefer_busy_poll

In reply to Joe Damato

From: Martin Karsten <[email protected]>

When events are reported to userland and prefer_busy_poll is set, irqs are temporarily suspended using napi_suspend_irqs.

If no events are found and ep_poll would go to sleep, irq suspension is cancelled using napi_resume_irqs.

---
v5:
- Only call ep_suspend_napi_irqs when ep_send_events returns a positive value. IRQs are not suspended in error (e.g. EINTR) cases. This issue was pointed out by Hillf Danton.

rfc -> v1:
- move irq resume code from ep_free to a helper which either resumes IRQs or does nothing if !defined(CONFIG_NET_RX_BUSY_POLL).

 fs/eventpoll.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
fs/eventpoll.c +31 -1
index f9e0d9307dad..83bcb559b89f 100644
@@ -457,6 +457,8 @@ static bool ep_busy_loop(struct eventpoll *ep, int nonblock)
 		 * it back in when we have moved a socket with a valid NAPI
 		 * ID onto the ready list.
 		 */
+		if (prefer_busy_poll)
+			napi_resume_irqs(napi_id);
 		ep->napi_id = 0;
 		return false;
 	}
@@ -540,6 +542,22 @@ static long ep_eventpoll_bp_ioctl(struct file *file, unsigned int cmd,
 	}
 }
 
+static void ep_suspend_napi_irqs(struct eventpoll *ep)
+{
+	unsigned int napi_id = READ_ONCE(ep->napi_id);
+
+	if (napi_id >= MIN_NAPI_ID && READ_ONCE(ep->prefer_busy_poll))
+		napi_suspend_irqs(napi_id);
+}
+
+static void ep_resume_napi_irqs(struct eventpoll *ep)
+{
+	unsigned int napi_id = READ_ONCE(ep->napi_id);
+
+	if (napi_id >= MIN_NAPI_ID && READ_ONCE(ep->prefer_busy_poll))
+		napi_resume_irqs(napi_id);
+}
+
 #else
 
 static inline bool ep_busy_loop(struct eventpoll *ep, int nonblock)
@@ -557,6 +575,14 @@ static long ep_eventpoll_bp_ioctl(struct file *file, unsigned int cmd,
 	return -EOPNOTSUPP;
 }
 
+static void ep_suspend_napi_irqs(struct eventpoll *ep)
+{
+}
+
+static void ep_resume_napi_irqs(struct eventpoll *ep)
+{
+}
+
 #endif /* CONFIG_NET_RX_BUSY_POLL */
 
 /*
@@ -788,6 +814,7 @@ static bool ep_refcount_dec_and_test(struct eventpoll *ep)
 
 static void ep_free(struct eventpoll *ep)
 {
+	ep_resume_napi_irqs(ep);
 	mutex_destroy(&ep->mtx);
 	free_uid(ep->user);
 	wakeup_source_unregister(ep->ws);
@@ -2005,8 +2032,11 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
 			 * trying again in search of more luck.
 			 */
 			res = ep_send_events(ep, events, maxevents);
-			if (res)
+			if (res) {
+				if (res > 0)
+					ep_suspend_napi_irqs(ep);
 				return res;
+			}
 		}
 
 		if (timed_out)
Signature · 1 line
2.25.1
author
#12 lore raw

PATCH v6 6/7 selftests: net: Add busy_poll_test

In reply to Joe Damato

Add an epoll busy poll test using netdevsim.

This test is comprised of:
- busy_poller (via busy_poller.c)
- busy_poll_test.sh which loads netdevsim, sets up network namespaces, and runs busy_poller to receive data and socat to send data.

The selftest tests two different scenarios:
- busy poll (the pre-existing version in the kernel)
- busy poll with suspend enabled (what this series adds)

The data transmit is a 1MiB temporary file generated from /dev/urandom and the test is considered passing if the md5sum of the input file to socat matches the md5sum of the output file from busy_poller.

netdevsim was chosen instead of veth due to netdevsim's support for netdev-genl.

For now, this test uses the functionality that netdevsim provides. In the future, perhaps netdevsim can be extended to emulate device IRQs to more thoroughly test all pre-existing kernel options (like defer_hard_irqs) and suspend.

---
v5:
- Updated commit message to replace netcat with socat and fixed misspelling of netdevsim. No functional/code changes.

v4:
- Updated busy_poll_test.sh:
- use socat instead of nc
- drop cli.py usage from the script
- removed check_ynl
- Updated busy_poller.c:
- use netlink to configure napi parameters

v3:
- New in v3

 tools/testing/selftests/net/.gitignore        |   1 +
 tools/testing/selftests/net/Makefile          |   3 +-
 tools/testing/selftests/net/busy_poll_test.sh | 164 +++++++++
 tools/testing/selftests/net/busy_poller.c     | 328 ++++++++++++++++++
 4 files changed, 495 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/net/busy_poll_test.sh
 create mode 100644 tools/testing/selftests/net/busy_poller.c
tools/testing/selftests/net/.gitignore +1 -0
index 217d8b7a7365..85b0c4a2179f 100644
@@ -2,6 +2,7 @@
 bind_bhash
 bind_timewait
 bind_wildcard
+busy_poller
 cmsg_sender
 diag_uid
 epoll_busy_poll
tools/testing/selftests/net/Makefile +2 -1
index 26a4883a65c9..3ccfe454db1a 100644
@@ -96,9 +96,10 @@ TEST_PROGS += fdb_flush.sh
 TEST_PROGS += fq_band_pktlimit.sh
 TEST_PROGS += vlan_hw_filter.sh
 TEST_PROGS += bpf_offload.py
+TEST_PROGS += busy_poll_test.sh
 
 # YNL files, must be before "include ..lib.mk"
-YNL_GEN_FILES := ncdevmem
+YNL_GEN_FILES := ncdevmem busy_poller
 TEST_GEN_FILES += $(YNL_GEN_FILES)
 
 TEST_FILES := settings
tools/testing/selftests/net/busy_poll_test.sh new file +164 -0

164 lines not on this page · raw message

tools/testing/selftests/net/busy_poller.c new file +328 -0

328 lines not on this page · raw message

Signature · 1 line
2.25.1
Acked-by on v6 6/7
#13 lore raw

Re: [PATCH net-next v6 6/7] selftests: net: Add busy_poll_test

Quoted 501 lines · Joe Damato

Add an epoll busy poll test using netdevsim.

This test is comprised of:
- busy_poller (via busy_poller.c)
- busy_poll_test.sh which loads netdevsim, sets up network namespaces, and runs busy_poller to receive data and socat to send data.

The selftest tests two different scenarios:
- busy poll (the pre-existing version in the kernel)
- busy poll with suspend enabled (what this series adds)

The data transmit is a 1MiB temporary file generated from /dev/urandom and the test is considered passing if the md5sum of the input file to socat matches the md5sum of the output file from busy_poller.

netdevsim was chosen instead of veth due to netdevsim's support for netdev-genl.

For now, this test uses the functionality that netdevsim provides. In the future, perhaps netdevsim can be extended to emulate device IRQs to more thoroughly test all pre-existing kernel options (like defer_hard_irqs) and suspend.

---
v5:
- Updated commit message to replace netcat with socat and fixed misspelling of netdevsim. No functional/code changes.

v4:
- Updated busy_poll_test.sh:
- use socat instead of nc
- drop cli.py usage from the script
- removed check_ynl
- Updated busy_poller.c:
- use netlink to configure napi parameters

v3:
- New in v3

 tools/testing/selftests/net/.gitignore        |   1 +
 tools/testing/selftests/net/Makefile          |   3 +-
 tools/testing/selftests/net/busy_poll_test.sh | 164 +++++++++
 tools/testing/selftests/net/busy_poller.c     | 328 ++++++++++++++++++
 4 files changed, 495 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/net/busy_poll_test.sh
 create mode 100644 tools/testing/selftests/net/busy_poller.c
tools/testing/selftests/net/.gitignore +1 -0
index 217d8b7a7365..85b0c4a2179f 100644
@@ -2,6 +2,7 @@
 bind_bhash
 bind_timewait
 bind_wildcard
+busy_poller
 cmsg_sender
 diag_uid
 epoll_busy_poll
tools/testing/selftests/net/Makefile +2 -1
index 26a4883a65c9..3ccfe454db1a 100644
@@ -96,9 +96,10 @@ TEST_PROGS += fdb_flush.sh
 TEST_PROGS += fq_band_pktlimit.sh
 TEST_PROGS += vlan_hw_filter.sh
 TEST_PROGS += bpf_offload.py
+TEST_PROGS += busy_poll_test.sh
 
 # YNL files, must be before "include ..lib.mk"
-YNL_GEN_FILES := ncdevmem
+YNL_GEN_FILES := ncdevmem busy_poller
 TEST_GEN_FILES += $(YNL_GEN_FILES)
 
 TEST_FILES := settings
tools/testing/selftests/net/busy_poll_test.sh new file +164 -0
new file mode 100755
index 000000000000..ffc74bc62e5a
@@ -0,0 +1,164 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+source net_helper.sh
+
+NSIM_DEV_1_ID=$((256 + RANDOM % 256))
+NSIM_DEV_1_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_1_ID
+NSIM_DEV_2_ID=$((512 + RANDOM % 256))
+NSIM_DEV_2_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_2_ID
+
+NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device
+NSIM_DEV_SYS_DEL=/sys/bus/netdevsim/del_device
+NSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_device
+NSIM_DEV_SYS_UNLINK=/sys/bus/netdevsim/unlink_device
+
+setup_ns()
+{
+	set -e
+	ip netns add nssv
+	ip netns add nscl
+
+	NSIM_DEV_1_NAME=$(find $NSIM_DEV_1_SYS/net -maxdepth 1 -type d ! \
+		-path $NSIM_DEV_1_SYS/net -exec basename {} \;)
+	NSIM_DEV_2_NAME=$(find $NSIM_DEV_2_SYS/net -maxdepth 1 -type d ! \
+		-path $NSIM_DEV_2_SYS/net -exec basename {} \;)
+
+	# ensure the server has 1 queue
+	ethtool -L $NSIM_DEV_1_NAME combined 1 2>/dev/null
+
+	ip link set $NSIM_DEV_1_NAME netns nssv
+	ip link set $NSIM_DEV_2_NAME netns nscl
+
+	ip netns exec nssv ip addr add '192.168.1.1/24' dev $NSIM_DEV_1_NAME
+	ip netns exec nscl ip addr add '192.168.1.2/24' dev $NSIM_DEV_2_NAME
+
+	ip netns exec nssv ip link set dev $NSIM_DEV_1_NAME up
+	ip netns exec nscl ip link set dev $NSIM_DEV_2_NAME up
+
+	set +e
+}
+
+cleanup_ns()
+{
+	ip netns del nscl
+	ip netns del nssv
+}
+
+test_busypoll()
+{
+	tmp_file=$(mktemp)
+	out_file=$(mktemp)
+
+	# fill a test file with random data
+	dd if=/dev/urandom of=${tmp_file} bs=1M count=1 2> /dev/null
+
+	timeout -k 1s 30s ip netns exec nssv ./busy_poller -p48675 -b192.168.1.1 -m8 -u0 -P1 -g16 -i${NSIM_DEV_1_IFIDX} -o${out_file}&
+
+	wait_local_port_listen nssv 48675 tcp
+
+	ip netns exec nscl socat -u $tmp_file TCP:192.168.1.1:48675
+
+	wait
+
+	tmp_file_md5sum=$(md5sum $tmp_file | cut -f1 -d' ')
+	out_file_md5sum=$(md5sum $out_file | cut -f1 -d' ')
+
+	if [ "$tmp_file_md5sum" = "$out_file_md5sum" ]; then
+		res=0
+	else
+		echo "md5sum mismatch"
+		echo "input file md5sum: ${tmp_file_md5sum}";
+		echo "output file md5sum: ${out_file_md5sum}";
+		res=1
+	fi
+
+	rm $out_file $tmp_file
+
+	return $res
+}
+
+test_busypoll_with_suspend()
+{
+	tmp_file=$(mktemp)
+	out_file=$(mktemp)
+
+	# fill a test file with random data
+	dd if=/dev/urandom of=${tmp_file} bs=1M count=1 2> /dev/null
+
+	timeout -k 1s 30s ip netns exec nssv ./busy_poller -p48675 -b192.168.1.1 -m8 -u0 -P1 -g16 -d100 -r50000 -s20000000 -i${NSIM_DEV_1_IFIDX} -o${out_file}&
+
+	wait_local_port_listen nssv 48675 tcp
+
+	ip netns exec nscl socat -u $tmp_file TCP:192.168.1.1:48675
+
+	wait
+
+	tmp_file_md5sum=$(md5sum $tmp_file | cut -f1 -d' ')
+	out_file_md5sum=$(md5sum $out_file | cut -f1 -d' ')
+
+	if [ "$tmp_file_md5sum" = "$out_file_md5sum" ]; then
+		res=0
+	else
+		echo "md5sum mismatch"
+		echo "input file md5sum: ${tmp_file_md5sum}";
+		echo "output file md5sum: ${out_file_md5sum}";
+		res=1
+	fi
+
+	rm $out_file $tmp_file
+
+	return $res
+}
+
+###
+### Code start
+###
+
+modprobe netdevsim
+
+# linking
+
+echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_NEW
+echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_NEW
+udevadm settle
+
+setup_ns
+
+NSIM_DEV_1_FD=$((256 + RANDOM % 256))
+exec {NSIM_DEV_1_FD}</var/run/netns/nssv
+NSIM_DEV_1_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_DEV_1_NAME/ifindex)
+
+NSIM_DEV_2_FD=$((256 + RANDOM % 256))
+exec {NSIM_DEV_2_FD}</var/run/netns/nscl
+NSIM_DEV_2_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_DEV_2_NAME/ifindex)
+
+echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK
+if [ $? -ne 0 ]; then
+	echo "linking netdevsim1 with netdevsim2 should succeed"
+	cleanup_ns
+	exit 1
+fi
+
+test_busypoll
+if [ $? -ne 0 ]; then
+	echo "test_busypoll failed"
+	cleanup_ns
+	exit 1
+fi
+
+test_busypoll_with_suspend
+if [ $? -ne 0 ]; then
+	echo "test_busypoll_with_suspend failed"
+	cleanup_ns
+	exit 1
+fi
+
+echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_UNLINK
+
+echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_DEL
+
+cleanup_ns
+
+modprobe -r netdevsim
+
+exit 0
tools/testing/selftests/net/busy_poller.c new file +247 -0
new file mode 100644
index 000000000000..8d8aa9e5939a
@@ -0,0 +1,328 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <assert.h>
+#include <errno.h>
+#include <error.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#include <sys/ioctl.h>
+#include <sys/epoll.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+
+#include <linux/netlink.h>
+#include <linux/genetlink.h>
+#include "netdev-user.h"
+#include <ynl.h>
+
+/* if the headers haven't been updated, we need to define some things */
+#if !defined(EPOLL_IOC_TYPE)
+struct epoll_params {
+	uint32_t busy_poll_usecs;
+	uint16_t busy_poll_budget;
+	uint8_t prefer_busy_poll;
+
+	/* pad the struct to a multiple of 64bits */
+	uint8_t __pad;
+};
+
+#define EPOLL_IOC_TYPE 0x8A
+#define EPIOCSPARAMS _IOW(EPOLL_IOC_TYPE, 0x01, struct epoll_params)
+#define EPIOCGPARAMS _IOR(EPOLL_IOC_TYPE, 0x02, struct epoll_params)
+#endif
+
+static uint32_t cfg_port = 8000;
+static struct in_addr cfg_bind_addr = { .s_addr = INADDR_ANY };
+static char *cfg_outfile;
+static int cfg_max_events = 8;
+static int cfg_ifindex;
+
+/* busy poll params */
+static uint32_t cfg_busy_poll_usecs;
+static uint16_t cfg_busy_poll_budget;
+static uint8_t cfg_prefer_busy_poll;
+
+/* IRQ params */
+static uint32_t cfg_defer_hard_irqs;
+static uint64_t cfg_gro_flush_timeout;
+static uint64_t cfg_irq_suspend_timeout;
+
+static void usage(const char *filepath)
+{
+	error(1, 0,
+	      "Usage: %s -p<port> -b<addr> -m<max_events> -u<busy_poll_usecs> -P<prefer_busy_poll> -g<busy_poll_budget> -o<outfile> -d<defer_hard_irqs> -r<gro_flush_timeout> -s<irq_suspend_timeout> -i<ifindex>",
+	      filepath);
+}
+
+static void parse_opts(int argc, char **argv)
+{
+	int ret;
+	int c;
+
+	if (argc <= 1)
+		usage(argv[0]);
+
+	while ((c = getopt(argc, argv, "p:m:b:u:P:g:o:d:r:s:i:")) != -1) {
+		switch (c) {
+		case 'u':
+			cfg_busy_poll_usecs = strtoul(optarg, NULL, 0);
+			if (cfg_busy_poll_usecs == ULONG_MAX ||
+			    cfg_busy_poll_usecs > UINT32_MAX)
+				error(1, ERANGE, "busy_poll_usecs too large");
+			break;
+		case 'P':
+			cfg_prefer_busy_poll = strtoul(optarg, NULL, 0);
+			if (cfg_prefer_busy_poll == ULONG_MAX ||
+			    cfg_prefer_busy_poll > 1)
+				error(1, ERANGE,
+				      "prefer busy poll should be 0 or 1");
+			break;
+		case 'g':
+			cfg_busy_poll_budget = strtoul(optarg, NULL, 0);
+			if (cfg_busy_poll_budget == ULONG_MAX ||
+			    cfg_busy_poll_budget > UINT16_MAX)
+				error(1, ERANGE,
+				      "busy poll budget must be [0, UINT16_MAX]");
+			break;
+		case 'p':
+			cfg_port = strtoul(optarg, NULL, 0);
+			if (cfg_port > UINT16_MAX)
+				error(1, ERANGE, "port must be <= 65535");
+			break;
+		case 'b':
+			ret = inet_aton(optarg, &cfg_bind_addr);
+			if (ret == 0)
+				error(1, errno,
+				      "bind address %s invalid", optarg);
+			break;
+		case 'o':
+			cfg_outfile = strdup(optarg);
+			if (!cfg_outfile)
+				error(1, 0, "outfile invalid");
+			break;
+		case 'm':
+			cfg_max_events = strtol(optarg, NULL, 0);
+
+			if (cfg_max_events == LONG_MIN ||
+			    cfg_max_events == LONG_MAX ||
+			    cfg_max_events <= 0)
+				error(1, ERANGE,
+				      "max events must be > 0 and < LONG_MAX");
+			break;
+		case 'd':
+			cfg_defer_hard_irqs = strtoul(optarg, NULL, 0);
+
+			if (cfg_defer_hard_irqs == ULONG_MAX ||
+			    cfg_defer_hard_irqs > INT32_MAX)
+				error(1, ERANGE,
+				      "defer_hard_irqs must be <= INT32_MAX");
+			break;
+		case 'r':
+			cfg_gro_flush_timeout = strtoull(optarg, NULL, 0);
+
+			if (cfg_gro_flush_timeout == ULLONG_MAX)
+				error(1, ERANGE,
+				      "gro_flush_timeout must be < ULLONG_MAX");
+			break;
+		case 's':
+			cfg_irq_suspend_timeout = strtoull(optarg, NULL, 0);
+
+			if (cfg_irq_suspend_timeout == ULLONG_MAX)
+				error(1, ERANGE,
+				      "irq_suspend_timeout must be < ULLONG_MAX");
+			break;
+		case 'i':
+			cfg_ifindex = strtoul(optarg, NULL, 0);
+			if (cfg_ifindex == ULONG_MAX)
+				error(1, ERANGE,
+				      "ifindex must be < ULONG_MAX");
+			break;
+		}
+	}
+
+	if (!cfg_ifindex)
+		usage(argv[0]);
+
+	if (optind != argc)
+		usage(argv[0]);
+}
+
+static void epoll_ctl_add(int epfd, int fd, uint32_t events)
+{
+	struct epoll_event ev;
+
+	ev.events = events;
+	ev.data.fd = fd;
+	if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) == -1)
+		error(1, errno, "epoll_ctl add fd: %d", fd);
+}
+
+static void setnonblock(int sockfd)
+{
+	int flags;
+
+	flags = fcntl(sockfd, F_GETFL, 0);
+
+	if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1)
+		error(1, errno, "unable to set socket to nonblocking mode");
+}
+
+static void write_chunk(int fd, char *buf, ssize_t buflen)
+{
+	ssize_t remaining = buflen;
+	char *buf_offset = buf;
+	ssize_t writelen = 0;
+	ssize_t write_result;
+
+	while (writelen < buflen) {
+		write_result = write(fd, buf_offset, remaining);
+		if (write_result == -1)
+			error(1, errno, "unable to write data to outfile");
+
+		writelen += write_result;
+		remaining -= write_result;
+		buf_offset += write_result;
+	}
+}
+
+static void setup_queue(void)
+{
+	struct netdev_napi_get_list *napi_list = NULL;
+	struct netdev_napi_get_req_dump *req = NULL;
+	struct netdev_napi_set_req *set_req = NULL;
+	struct ynl_sock *ys;
+	struct ynl_error yerr;
+	uint32_t napi_id;
+
+	ys = ynl_sock_create(&ynl_netdev_family, &yerr);
+	if (!ys)
+		error(1, 0, "YNL: %s", yerr.msg);
+
+	req = netdev_napi_get_req_dump_alloc();
+	netdev_napi_get_req_dump_set_ifindex(req, cfg_ifindex);
+	napi_list = netdev_napi_get_dump(ys, req);
+
+	/* assume there is 1 NAPI configured and take the first */
+	if (napi_list->obj._present.id)
+		napi_id = napi_list->obj.id;
+	else
+		error(1, 0, "napi ID not present?");
+
+	set_req = netdev_napi_set_req_alloc();
+	netdev_napi_set_req_set_id(set_req, napi_id);
+	netdev_napi_set_req_set_defer_hard_irqs(set_req, cfg_defer_hard_irqs);
+	netdev_napi_set_req_set_gro_flush_timeout(set_req,
+						  cfg_gro_flush_timeout);
+	netdev_napi_set_req_set_irq_suspend_timeout(set_req,
+						    cfg_irq_suspend_timeout);
+
+	if (netdev_napi_set(ys, set_req))
+		error(1, 0, "can't set NAPI params: %s\n", yerr.msg);
+
+	netdev_napi_get_list_free(napi_list);
+	netdev_napi_get_req_dump_free(req);
+	netdev_napi_set_req_free(set_req);
+	ynl_sock_destroy(ys);
+}
+
+static void run_poller(void)
+{
+	struct epoll_event events[cfg_max_events];
+	struct epoll_params epoll_params = {0};
+	struct sockaddr_in server_addr;
+	int i, epfd, nfds;
+	ssize_t readlen;
+	int outfile_fd;
+	char buf[1024];
+	int sockfd;
+	int conn;
+	int val;

[..]

tools/testing/selftests/net/busy_poller.c line 251

Quoted 3 lines
tools/testing/selftests/net/busy_poller.c new file +3 -0
+	outfile_fd = open(cfg_outfile, O_WRONLY | O_CREAT, 0644);
+	if (outfile_fd == -1)
+		error(1, errno, "unable to open outfile: %s", cfg_outfile);

Any reason you're not printing to stdout? And then redirect it to a file in the shell script if needed. Lets you save some code on open/close and flag parsing :-p But I guess can keep it since you already have it all working.

author
#14 lore raw

Re: [PATCH net-next v6 6/7] selftests: net: Add busy_poll_test

Quoted 28 lines · Stanislav Fomichev
Quoted 27 lines · Joe Damato

Add an epoll busy poll test using netdevsim.

This test is comprised of:
- busy_poller (via busy_poller.c)
- busy_poll_test.sh which loads netdevsim, sets up network namespaces, and runs busy_poller to receive data and socat to send data.

The selftest tests two different scenarios:
- busy poll (the pre-existing version in the kernel)
- busy poll with suspend enabled (what this series adds)

The data transmit is a 1MiB temporary file generated from /dev/urandom and the test is considered passing if the md5sum of the input file to socat matches the md5sum of the output file from busy_poller.

netdevsim was chosen instead of veth due to netdevsim's support for netdev-genl.

For now, this test uses the functionality that netdevsim provides. In the future, perhaps netdevsim can be extended to emulate device IRQs to more thoroughly test all pre-existing kernel options (like defer_hard_irqs) and suspend.

---

[...]

Quoted 16 lines
Quoted 13 lines
tools/testing/selftests/net/busy_poller.c new file +13 -0
+
+static void run_poller(void)
+{
+	struct epoll_event events[cfg_max_events];
+	struct epoll_params epoll_params = {0};
+	struct sockaddr_in server_addr;
+	int i, epfd, nfds;
+	ssize_t readlen;
+	int outfile_fd;
+	char buf[1024];
+	int sockfd;
+	int conn;
+	int val;

[..]

Quoted 8 lines

tools/testing/selftests/net/busy_poller.c line 251

Quoted 3 lines
tools/testing/selftests/net/busy_poller.c new file +3 -0
+	outfile_fd = open(cfg_outfile, O_WRONLY | O_CREAT, 0644);
+	if (outfile_fd == -1)
+		error(1, errno, "unable to open outfile: %s", cfg_outfile);

Any reason you're not printing to stdout? And then redirect it to a file in the shell script if needed. Lets you save some code on open/close and flag parsing :-p But I guess can keep it since you already have it all working.

No reason in particular; I thought about this while writing it, but ended up adding it as a flag in case others come along to extend this test in some capacity.

Quoted 1 line

Thanks for the ack!

author
#15 lore raw

PATCH v6 7/7 docs: networking: Describe irq suspension

In reply to Joe Damato

Describe irq suspension, the epoll ioctls, and the tradeoffs of using different gro_flush_timeout values.

---
v6:
- Fixed packet processing loop description based on feedback from Bagas Sanjaya so that it renders properly when generated as html

v5:
- Fixed a minor typo in the epoll-based busy polling section
- Removed short paragraph referring to experimental data as that data is not included in the documentation

v4:
- Updated documentation to further explain irq suspension
- Dropped Stanislav's Acked-by tag because of the doc changes
- Dropped Bagas' Reviewed-by tag because of the doc changes

v1 -> v2:
- Updated documentation to describe the per-NAPI configuration parameters.

 Documentation/networking/napi.rst | 170 +++++++++++++++++++++++++++++-
 1 file changed, 168 insertions(+), 2 deletions(-)
Documentation/networking/napi.rst +168 -2
index dfa5d549be9c..02720dd71a76 100644
@@ -192,6 +192,33 @@ is reused to control the delay of the timer, while
 ``napi_defer_hard_irqs`` controls the number of consecutive empty polls
 before NAPI gives up and goes back to using hardware IRQs.
 
+The above parameters can also be set on a per-NAPI basis using netlink via
+netdev-genl. When used with netlink and configured on a per-NAPI basis, the
+parameters mentioned above use hyphens instead of underscores:
+``gro-flush-timeout`` and ``napi-defer-hard-irqs``.
+
+Per-NAPI configuration can be done programmatically in a user application
+or by using a script included in the kernel source tree:
+``tools/net/ynl/cli.py``.
+
+For example, using the script:
+
+.. code-block:: bash
+
+  $ kernel-source/tools/net/ynl/cli.py \
+            --spec Documentation/netlink/specs/netdev.yaml \
+            --do napi-set \
+            --json='{"id": 345,
+                     "defer-hard-irqs": 111,
+                     "gro-flush-timeout": 11111}'
+
+Similarly, the parameter ``irq-suspend-timeout`` can be set using netlink
+via netdev-genl. There is no global sysfs parameter for this value.
+
+``irq-suspend-timeout`` is used to determine how long an application can
+completely suspend IRQs. It is used in combination with SO_PREFER_BUSY_POLL,
+which can be set on a per-epoll context basis with ``EPIOCSPARAMS`` ioctl.
+
 .. _poll:
 
 Busy polling
@@ -207,6 +234,46 @@ selected sockets or using the global ``net.core.busy_poll`` and
 ``net.core.busy_read`` sysctls. An io_uring API for NAPI busy polling
 also exists.
 
+epoll-based busy polling
+------------------------
+
+It is possible to trigger packet processing directly from calls to
+``epoll_wait``. In order to use this feature, a user application must ensure
+all file descriptors which are added to an epoll context have the same NAPI ID.
+
+If the application uses a dedicated acceptor thread, the application can obtain
+the NAPI ID of the incoming connection using SO_INCOMING_NAPI_ID and then
+distribute that file descriptor to a worker thread. The worker thread would add
+the file descriptor to its epoll context. This would ensure each worker thread
+has an epoll context with FDs that have the same NAPI ID.
+
+Alternatively, if the application uses SO_REUSEPORT, a bpf or ebpf program can
+be inserted to distribute incoming connections to threads such that each thread
+is only given incoming connections with the same NAPI ID. Care must be taken to
+carefully handle cases where a system may have multiple NICs.
+
+In order to enable busy polling, there are two choices:
+
+1. ``/proc/sys/net/core/busy_poll`` can be set with a time in useconds to busy
+   loop waiting for events. This is a system-wide setting and will cause all
+   epoll-based applications to busy poll when they call epoll_wait. This may
+   not be desirable as many applications may not have the need to busy poll.
+
+2. Applications using recent kernels can issue an ioctl on the epoll context
+   file descriptor to set (``EPIOCSPARAMS``) or get (``EPIOCGPARAMS``) ``struct
+   epoll_params``:, which user programs can define as follows:
+
+.. code-block:: c
+
+  struct epoll_params {
+      uint32_t busy_poll_usecs;
+      uint16_t busy_poll_budget;
+      uint8_t prefer_busy_poll;
+
+      /* pad the struct to a multiple of 64bits */
+      uint8_t __pad;
+  };
+
 IRQ mitigation
 ---------------
 
@@ -222,12 +289,111 @@ Such applications can pledge to the kernel that they will perform a busy
 polling operation periodically, and the driver should keep the device IRQs
 permanently masked. This mode is enabled by using the ``SO_PREFER_BUSY_POLL``
 socket option. To avoid system misbehavior the pledge is revoked
-if ``gro_flush_timeout`` passes without any busy poll call.
+if ``gro_flush_timeout`` passes without any busy poll call. For epoll-based
+busy polling applications, the ``prefer_busy_poll`` field of ``struct
+epoll_params`` can be set to 1 and the ``EPIOCSPARAMS`` ioctl can be issued to
+enable this mode. See the above section for more details.
 
 The NAPI budget for busy polling is lower than the default (which makes
 sense given the low latency intention of normal busy polling). This is
 not the case with IRQ mitigation, however, so the budget can be adjusted
-with the ``SO_BUSY_POLL_BUDGET`` socket option.
+with the ``SO_BUSY_POLL_BUDGET`` socket option. For epoll-based busy polling
+applications, the ``busy_poll_budget`` field can be adjusted to the desired value
+in ``struct epoll_params`` and set on a specific epoll context using the ``EPIOCSPARAMS``
+ioctl. See the above section for more details.
+
+It is important to note that choosing a large value for ``gro_flush_timeout``
+will defer IRQs to allow for better batch processing, but will induce latency
+when the system is not fully loaded. Choosing a small value for
+``gro_flush_timeout`` can cause interference of the user application which is
+attempting to busy poll by device IRQs and softirq processing. This value
+should be chosen carefully with these tradeoffs in mind. epoll-based busy
+polling applications may be able to mitigate how much user processing happens
+by choosing an appropriate value for ``maxevents``.
+
+Users may want to consider an alternate approach, IRQ suspension, to help deal
+with these tradeoffs.
+
+IRQ suspension
+--------------
+
+IRQ suspension is a mechanism wherein device IRQs are masked while epoll
+triggers NAPI packet processing.
+
+While application calls to epoll_wait successfully retrieve events, the kernel will
+defer the IRQ suspension timer. If the kernel does not retrieve any events
+while busy polling (for example, because network traffic levels subsided), IRQ
+suspension is disabled and the IRQ mitigation strategies described above are
+engaged.
+
+This allows users to balance CPU consumption with network processing
+efficiency.
+
+To use this mechanism:
+
+  1. The per-NAPI config parameter ``irq-suspend-timeout`` should be set to the
+     maximum time (in nanoseconds) the application can have its IRQs
+     suspended. This is done using netlink, as described above. This timeout
+     serves as a safety mechanism to restart IRQ driver interrupt processing if
+     the application has stalled. This value should be chosen so that it covers
+     the amount of time the user application needs to process data from its
+     call to epoll_wait, noting that applications can control how much data
+     they retrieve by setting ``max_events`` when calling epoll_wait.
+
+  2. The sysfs parameter or per-NAPI config parameters ``gro_flush_timeout``
+     and ``napi_defer_hard_irqs`` can be set to low values. They will be used
+     to defer IRQs after busy poll has found no data.
+
+  3. The ``prefer_busy_poll`` flag must be set to true. This can be done using
+     the ``EPIOCSPARAMS`` ioctl as described above.
+
+  4. The application uses epoll as described above to trigger NAPI packet
+     processing.
+
+As mentioned above, as long as subsequent calls to epoll_wait return events to
+userland, the ``irq-suspend-timeout`` is deferred and IRQs are disabled. This
+allows the application to process data without interference.
+
+Once a call to epoll_wait results in no events being found, IRQ suspension is
+automatically disabled and the ``gro_flush_timeout`` and
+``napi_defer_hard_irqs`` mitigation mechanisms take over.
+
+It is expected that ``irq-suspend-timeout`` will be set to a value much larger
+than ``gro_flush_timeout`` as ``irq-suspend-timeout`` should suspend IRQs for
+the duration of one userland processing cycle.
+
+While it is not stricly necessary to use ``napi_defer_hard_irqs`` and
+``gro_flush_timeout`` to use IRQ suspension, their use is strongly
+recommended.
+
+IRQ suspension causes the system to alternate between polling mode and
+irq-driven packet delivery. During busy periods, ``irq-suspend-timeout``
+overrides ``gro_flush_timeout`` and keeps the system busy polling, but when
+epoll finds no events, the setting of ``gro_flush_timeout`` and
+``napi_defer_hard_irqs`` determine the next step.
+
+There are essentially three possible loops for network processing and
+packet delivery:
+
+1) hardirq -> softirq -> napi poll; basic interrupt delivery
+2) timer -> softirq -> napi poll; deferred irq processing
+3) epoll -> busy-poll -> napi poll; busy looping
+
+Loop 2 can take control from Loop 1, if ``gro_flush_timeout`` and
+``napi_defer_hard_irqs`` are set.
+
+If ``gro_flush_timeout`` and ``napi_defer_hard_irqs`` are set, Loops 2
+and 3 "wrestle" with each other for control.
+
+During busy periods, ``irq-suspend-timeout`` is used as timer in Loop 2,
+which essentially tilts network processing in favour of Loop 3.
+
+If ``gro_flush_timeout`` and ``napi_defer_hard_irqs`` are not set, Loop 3
+cannot take control from Loop 1.
+
+Therefore, setting ``gro_flush_timeout`` and ``napi_defer_hard_irqs`` is
+the recommended usage, because otherwise setting ``irq-suspend-timeout``
+might not have any discernible effect.
 
 .. _threaded:
 
Signature · 1 line
2.25.1
Reviewed-by on v6 7/7
#16 lore raw

Re: [PATCH net-next v6 7/7] docs: networking: Describe irq suspension

Quoted 3 lines · Joe Damato

Describe irq suspension, the epoll ioctls, and the tradeoffs of using different gro_flush_timeout values.

The docs LGTM, thanks!

Signature · 1 line
An old man doll... just what I always wanted! - Clara