Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Bobby Eshleman <bobbyeshleman@gmail.com>
To: Jakub Kicinski <kuba@kernel.org>
Cc: davem@davemloft.net, netdev@vger.kernel.org, edumazet@google.com,
	pabeni@redhat.com, andrew+netdev@lunn.ch, horms@kernel.org,
	shuah@kernel.org, cjubran@nvidia.com, cratiu@nvidia.com,
	noren@nvidia.com, willemb@google.com, petrm@nvidia.com,
	linux-kselftest@vger.kernel.org
Subject: Re: [PATCH net-next] selftests: net: add ctl_file_write() helper
Date: Fri, 4 Sep 2026 16:34:40 -0700	[thread overview]
Message-ID: <aptVkE6fRuj7f3bl@devvm29614.prn0.facebook.com> (raw)
In-Reply-To: <20260904174134.3789400-1-kuba@kernel.org>

On Fri, Sep 04, 2026 at 10:41:34AM -0700, Jakub Kicinski wrote:
> Setting a sysctl or a sysfs attribute for the duration of a test and
> putting the old value back has been open coded multiple times.
> 
> We generally avoid creating library helpers but this one is very
> common, and the defer is a little tricky as using the same function
> for defer as the initial write leads to an infinite loop (not that
> I would ever make such mistake!)
> 
> Some of the conversions are not identical, but arguably ctl_file_write()
> semantics are more correct.
> 
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
> CC: shuah@kernel.org
> CC: cjubran@nvidia.com
> CC: cratiu@nvidia.com
> CC: noren@nvidia.com
> CC: willemb@google.com
> CC: petrm@nvidia.com
> CC: linux-kselftest@vger.kernel.org
> ---
>  tools/testing/selftests/drivers/net/gro.py    | 17 +++---------
>  .../drivers/net/hw/devlink_rate_tc_bw.py      |  5 ++--
>  tools/testing/selftests/drivers/net/hw/irq.py | 11 ++------
>  .../drivers/net/hw/lib/py/__init__.py         |  4 +--
>  .../selftests/drivers/net/hw/toeplitz.py      | 10 +++----
>  .../selftests/drivers/net/lib/py/__init__.py  |  4 +--
>  .../selftests/drivers/net/ring_reconfig.py    | 26 ++++---------------
>  .../testing/selftests/net/lib/py/__init__.py  |  4 +--
>  tools/testing/selftests/net/lib/py/utils.py   | 22 ++++++++++++++++
>  9 files changed, 44 insertions(+), 59 deletions(-)
> 
> diff --git a/tools/testing/selftests/drivers/net/gro.py b/tools/testing/selftests/drivers/net/gro.py
> index 6ab8c97880d1..e0b21703cc3c 100755
> --- a/tools/testing/selftests/drivers/net/gro.py
> +++ b/tools/testing/selftests/drivers/net/gro.py
> @@ -42,7 +42,7 @@ import re
>  from lib.py import ksft_run, ksft_exit, ksft_pr
>  from lib.py import NetDrvEpEnv, KsftFailEx, KsftXfailEx
>  from lib.py import NetdevFamily, EthtoolFamily
> -from lib.py import bkg, cmd, defer, ethtool, ip
> +from lib.py import bkg, cmd, ctl_file_write, defer, ethtool, ip
>  from lib.py import ksft_variants, KsftNamedVariant
>  
>  
> @@ -79,17 +79,6 @@ GRO_DPORT = 8000
>      return getattr(cfg, attr)
>  
>  
> -def _write_defer_restore(cfg, path, val, defer_undo=False):
> -    with open(path, "r", encoding="utf-8") as fp:
> -        orig_val = fp.read().strip()
> -        if str(val) == orig_val:
> -            return
> -    with open(path, "w", encoding="utf-8") as fp:
> -        fp.write(val)
> -    if defer_undo:
> -        defer(_write_defer_restore, cfg, path, orig_val)
> -
> -
>  def _set_mtu_restore(dev, mtu, host):
>      if dev['mtu'] < mtu:
>          ip(f"link set dev {dev['ifname']} mtu {mtu}", host=host)
> @@ -244,8 +233,8 @@ def _run_gro_bin(cfg, test_name, protocol=None, num_flows=None,
>          flush_path = f"/sys/class/net/{cfg.ifname}/gro_flush_timeout"
>          irq_path = f"/sys/class/net/{cfg.ifname}/napi_defer_hard_irqs"
>  
> -        _write_defer_restore(cfg, flush_path, "200000", defer_undo=True)
> -        _write_defer_restore(cfg, irq_path, "10", defer_undo=True)
> +        ctl_file_write(flush_path, "200000")
> +        ctl_file_write(irq_path, "10")
>  
>          _set_ethtool_feat(cfg.ifname, cfg.feat,
>                            {"generic-receive-offload": True,
> diff --git a/tools/testing/selftests/drivers/net/hw/devlink_rate_tc_bw.py b/tools/testing/selftests/drivers/net/hw/devlink_rate_tc_bw.py
> index 4e4faa9275bb..38d712755257 100755
> --- a/tools/testing/selftests/drivers/net/hw/devlink_rate_tc_bw.py
> +++ b/tools/testing/selftests/drivers/net/hw/devlink_rate_tc_bw.py
> @@ -63,7 +63,7 @@ from lib.py import ksft_pr, ksft_run, ksft_exit
>  from lib.py import KsftSkipEx, KsftFailEx, KsftXfailEx
>  from lib.py import NetDrvEpEnv, DevlinkFamily
>  from lib.py import NlError
> -from lib.py import cmd, defer, ethtool, ip
> +from lib.py import cmd, ctl_file_write, defer, ethtool, ip
>  from lib.py import Iperf3Runner
>  
>  
> @@ -113,8 +113,7 @@ from lib.py import Iperf3Runner
>      except Exception as exc:
>          raise KsftSkipEx(f"Failed to enable switchdev mode on {cfg.pci}") from exc
>      try:
> -        cmd(f"echo 1 > /sys/class/net/{cfg.ifname}/device/sriov_numvfs", shell=True)
> -        defer(cmd, f"echo 0 > /sys/class/net/{cfg.ifname}/device/sriov_numvfs", shell=True)
> +        ctl_file_write(f"/sys/class/net/{cfg.ifname}/device/sriov_numvfs", 1)
>      except Exception as exc:
>          raise KsftSkipEx(f"Failed to enable SR-IOV on {cfg.ifname}") from exc
>  
> diff --git a/tools/testing/selftests/drivers/net/hw/irq.py b/tools/testing/selftests/drivers/net/hw/irq.py
> index 0699d6a8b4e2..3d1c56b51054 100755
> --- a/tools/testing/selftests/drivers/net/hw/irq.py
> +++ b/tools/testing/selftests/drivers/net/hw/irq.py
> @@ -7,7 +7,7 @@ from lib.py import KsftSkipEx
>  from lib.py import ksft_disruptive
>  from lib.py import EthtoolFamily, NetdevFamily
>  from lib.py import NetDrvEnv
> -from lib.py import cmd, ip, defer
> +from lib.py import cmd, ctl_file_write, ip
>  
>  
>  def read_affinity(irq) -> str:
> @@ -15,12 +15,6 @@ from lib.py import cmd, ip, defer
>          return fp.read().lstrip("0,").strip()
>  
>  
> -def write_affinity(irq, what) -> str:
> -    if what != read_affinity(irq):
> -        with open(f'/proc/irq/{irq}/smp_affinity', 'w') as fp:
> -            fp.write(what)
> -
> -
>  def check_irqs_reported(cfg) -> None:
>      """ Check that device reports IRQs for NAPI instances """
>      napis = cfg.netnl.napi_get({"ifindex": cfg.ifindex}, dump=True)
> @@ -41,8 +35,7 @@ from lib.py import cmd, ip, defer
>      old = read_affinity(n['irq'])
>      # pick an affinity that's not the current one
>      new = "3" if old != "3" else "5"
> -    write_affinity(n['irq'], new)
> -    defer(write_affinity, n['irq'], old)
> +    ctl_file_write(f"/proc/irq/{n['irq']}/smp_affinity", new)

This one might miss the ctl_file_write's "old == str(val)" case, where
old is still prepended with zeroes (e.g., 0000,0000,0003)?

The rest LGTM.

Best,
Bobby

      reply	other threads:[~2026-09-04 23:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-04 17:41 [PATCH net-next] selftests: net: add ctl_file_write() helper Jakub Kicinski
2026-09-04 23:34 ` Bobby Eshleman [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aptVkE6fRuj7f3bl@devvm29614.prn0.facebook.com \
    --to=bobbyeshleman@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=cjubran@nvidia.com \
    --cc=cratiu@nvidia.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=noren@nvidia.com \
    --cc=pabeni@redhat.com \
    --cc=petrm@nvidia.com \
    --cc=shuah@kernel.org \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox