Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alireza Asgari" <alireza@asgari.net>, linux-mm@kvack.org
Cc: "Johannes Weiner" <hannes@cmpxchg.org>,
	"Michal Hocko" <mhocko@kernel.org>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Muchun Song" <muchun.song@linux.dev>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"David Hildenbrand" <david@kernel.org>,
	"Pablo Neira Ayuso" <pablo@netfilter.org>,
	"Florian Westphal" <fw@strlen.de>, "Phil Sutter" <phil@nwl.cc>,
	"Shuah Khan" <shuah@kernel.org>, "Tejun Heo" <tj@kernel.org>,
	"Vladimir Davydov" <vdavydov.dev@gmail.com>,
	"Chris Down" <chris@chrisdown.name>,
	"Zhang Jiao" <zhangjiao2@cmss.chinamobile.com>,
	"Song Hu" <husong@kylinos.cn>,
	mkoutny@suse.com
Subject: Re: [PATCH RFC nf-next 9/9] selftests: netfilter: exercise nfqueue memory accounting
Date: Mon, 16 Nov 2026 22:34:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-217804@kernel.org> (raw)
In-Reply-To: <20261115-b4-nfqueue-memory-accounting-v1-9-b9cc30819997@asgari.net>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] selftests: netfilter: missing kernel config skip checks in
  Traffic.__init__()
- [Medium] selftests: netfilter: broken cgroup v2 discovery and
  migration in reservation()
- [Medium] selftests: netfilter: unhandled EOPNOTSUPP on 32-bit kernels
  in reservation()
- [Low] selftests: netfilter: hand-rolled TAP runner and netns setup in
  main()
- [Low] selftests: netfilter: unrefunded queues before cgroup reuse in
  reservation()
--

commit 4752a896acbd3189476bf1437e148eaf01569f85
Author: Alireza Asgari <alireza@asgari.net>

selftests: netfilter: exercise nfqueue memory accounting

This commit adds a raw-Netlink NFQUEUE selftest exercising packet memory
retention, resizing, payload replacement, segmentation, and cgroup v2 memory
reservation accounting.

> diff --git a/tools/testing/selftests/net/netfilter/nft_queue_memory.py b/tools/testing/selftests/net/netfilter/nft_queue_memory.py
> new file mode 100755
> index 0000000000000..35ae4db09a484
> --- /dev/null
> +++ b/tools/testing/selftests/net/netfilter/nft_queue_memory.py

[ ... ]

> +    def stats(self):
> +        path = Path("/proc/self/net/netfilter/nfnetlink_queue")
> +        for line in path.read_text().splitlines():
> +            values = [int(value) for value in line.split()]
> +            if values[0] == self.number:
> +                return values
> +        raise AssertionError("bound queue missing from procfs")
> +
> +
> +class Traffic:
> +    def __init__(self):
> +        self.rx = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> +        self.rx.bind(("127.0.0.1", 0))
> +        self.destination = self.rx.getsockname()
> +        rules = f"""table ip nfq_memory {{
> + chain output {{
> +  type filter hook output priority 0; policy accept;
> +  udp dport {self.destination[1]} queue num 1
> + }}
> +}}
> +"""
> +        subprocess.run(["nft", "-f", "-"], input=rules, text=True, check=True)

[Severity: Medium]
Does Traffic.__init__() fail with an unhandled CalledProcessError instead of
skipping when the kernel lacks nftables or nft_queue support?

main() instantiates Traffic() before the test loop without catching
subprocess.CalledProcessError, so a missing CONFIG_NF_TABLES,
CONFIG_NF_TABLES_IPV4, or CONFIG_NFT_QUEUE aborts the script with exit code 1
(and leaks self.rx because contextlib.closing() is never entered) rather than
returning the kselftest skip code 4.  Likewise, if
CONFIG_NETFILTER_NETLINK_QUEUE or CONFIG_PROC_FS is disabled, Queue.__init__(),
Queue.request(), or Queue.stats() raises OSError or TimeoutError, which main()
reports as test failures rather than skips.

[ ... ]

> +def reservation(traffic, parent):
> +    if not parent:
> +        return "supply --cgroup with a delegated cgroup v2 parent in a test guest"
> +    parent = Path(parent).resolve()
> +    check("memory" in (parent / "cgroup.subtree_control").read_text().split(),
> +          "parent must already delegate the memory controller")
> +    membership = next(line[3:] for line in Path("/proc/self/cgroup").read_text().splitlines()
> +                      if line.startswith("0::"))
> +    original = Path("/sys/fs/cgroup") / membership.lstrip("/")
> +    group = parent / f"nfq-memory-{os.getpid()}"
> +    group.mkdir()
> +    try:
> +        (group / "memory.max").write_text(str(64 << 20))
> +        (group / "cgroup.procs").write_text(str(os.getpid()))

[Severity: Medium]
Can reservation() and reservation_startup() fail with EBUSY or StopIteration,
and are they ever exercised by default kselftest runs?

Standard kselftest runs do not pass --cgroup, and main() never probes
/sys/fs/cgroup by default, so both cgroup tests are unconditionally skipped.
When --cgroup is passed and the test process starts inside that non-root
delegated parent cgroup (original == parent), parent has memory enabled in
cgroup.subtree_control and a child directory (group), so migrating the process
back to original / "cgroup.procs" while group still exists violates the cgroup
v2 no-internal-processes rule and fails with OSError(EBUSY), including in the
finally block where it prevents group.rmdir() from running.

In addition, check("memory" in ...) fails the test with AssertionError instead
of enabling +memory or skipping when the controller is not delegated,
next(... if line.startswith("0::")) raises an uncaught StopIteration if cgroup
v2 is not mounted, and original hardcodes /sys/fs/cgroup even when --cgroup
points to a different mount point.

> +        with contextlib.closing(Queue()) as queue:
> +            try:
> +                queue.configure(1 << 20, bind=True)
> +            except OSError as error:
> +                check(error.errno == errno.ENOMEM, f"unexpected BIND refusal: {error}")

[Severity: Medium]
Does queue.configure() fail with EOPNOTSUPP instead of skipping on 32-bit
kernels?

In mm/memcontrol.c, mem_cgroup_reservation_resize() returns -EOPNOTSUPP when
!IS_ENABLED(CONFIG_64BIT) and res->objcg is non-NULL during positive
reservation growth.  Because the script runs in an unshared network namespace,
instance_create() initializes inst->memcg via mem_cgroup_reservation_init(),
causing reservation(), reservation_startup(), and any test started inside a
non-root cgroup (such as a systemd slice) to raise OSError(EOPNOTSUPP) on
32-bit kernels and fail instead of skipping.

[ ... ]

> +            check(queues and refused is not None,
> +                  "expected successful queues followed by aggregate refusal")
> +            queues[0].configure(0)
> +            refused.configure(4096, bind=True)
> +        # Reparent an occupied reservation, then grow and refund it in its parent.
> +        (group / "cgroup.procs").write_text(str(os.getpid()))
> +        with contextlib.closing(Queue()) as queue:
> +            queue.configure(32, bind=True)
> +            (original / "cgroup.procs").write_text(str(os.getpid()))
> +            group.rmdir()
> +            queue.configure(64)
> +            queue.configure(0)

[Severity: Low]
Does leaving the ExitStack block without calling configure(0) on queues[1:]
and refused leave 63 MiB charged to group when the reparenting test starts?

Closing a queue socket without shrinking its limit to 0 first defers
mem_cgroup_reservation_destroy() to instance_destroy_work() via
queue_rcu_work().  Until the RCU grace period and workqueue finish, 63 MiB of
the 64 MiB memory.max limit remains charged, which can cause the subsequent
queue.configure(32, bind=True) call (which uses GFP_NOWAIT try_charge() without
draining remote per-CPU stock caches, especially on 64K-page kernels where
MEMCG_CHARGE_BATCH is 4 MiB) to fail with ENOMEM and causes group.rmdir() to
reparent the dying queues alongside the 32-slot queue.  Calling configure(0) on
each open queue before closing makes the refund synchronous.

[ ... ]

> +def main():
> +    parser = argparse.ArgumentParser(description=__doc__)
> +    parser.add_argument("--cgroup")
> +    parser.add_argument("--inside-netns", action="store_true", help=argparse.SUPPRESS)
> +    args = parser.parse_args()
> +    if not all(shutil.which(tool) for tool in ("nft", "ip", "unshare")):
> +        print("1..0 # SKIP nft, ip and unshare are required")
> +        return 4
> +    if not args.inside_netns:
> +        probe = subprocess.run(["unshare", "-n", "true"], capture_output=True)
> +        if probe.returncode:
> +            print("1..0 # SKIP cannot create a network namespace")
> +            return 4
> +        return subprocess.call(["unshare", "-n", sys.executable,
> +                                str(Path(__file__).resolve()), "--inside-netns",
> +                                *(["--cgroup", args.cgroup] if args.cgroup else [])])

[Severity: Low]
Could this test use the shared Python kselftest helpers in
tools/testing/selftests/net/lib/py/ (ksft.py and netns.py) and factor the
duplicated cgroup setup in reservation() and reservation_startup() into a
shared helper?

Using the shared harness avoids hand-rolling namespace creation and KTAP
formatting (where early skips omit the "TAP version 13" header and multiline
exception messages print without a "#" prefix).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261115-b4-nfqueue-memory-accounting-v1-0-b9cc30819997@asgari.net?part=9


      reply	other threads:[~2026-11-16 22:35 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15 22:34 [PATCH RFC nf-next 0/9] netfilter: nfqueue: account queued memory and reserve capacity Alireza Asgari via B4 Relay
2026-11-15 22:34 ` [PATCH RFC nf-next 1/9] memcg: saturate deferred memory.high page debt Alireza Asgari via B4 Relay
2026-11-15 22:34 ` [PATCH RFC nf-next 2/9] memcg: bound memory.high delay arithmetic Alireza Asgari via B4 Relay
2026-11-15 22:34 ` [PATCH RFC nf-next 3/9] memcg: add KUnit coverage for memory.high accounting Alireza Asgari via B4 Relay
2026-11-15 22:34 ` [PATCH RFC nf-next 4/9] memcg: add an owner-bound capacity reservation interface Alireza Asgari via B4 Relay
2026-11-15 22:34 ` [PATCH RFC nf-next 5/9] memcg: test capacity reservation rollback and refunds Alireza Asgari via B4 Relay
2026-11-15 22:34 ` [PATCH RFC nf-next 6/9] netfilter: nfqueue: account saved skb bytes per queue Alireza Asgari via B4 Relay
2026-11-16 22:46   ` Florian Westphal
2026-11-15 22:34 ` [PATCH RFC nf-next 7/9] netfilter: nfqueue: reserve capacity against the bound owner Alireza Asgari via B4 Relay
2026-11-15 22:34 ` [PATCH RFC nf-next 8/9] netfilter: nfqueue: add KUnit memory accounting coverage Alireza Asgari via B4 Relay
2026-11-15 22:34 ` [PATCH RFC nf-next 9/9] selftests: netfilter: exercise nfqueue memory accounting Alireza Asgari via B4 Relay
2026-11-16 22:34   ` sashiko-bot [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=sashiko-outbox-217804@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alireza@asgari.net \
    --cc=chris@chrisdown.name \
    --cc=david@kernel.org \
    --cc=fw@strlen.de \
    --cc=hannes@cmpxchg.org \
    --cc=husong@kylinos.cn \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=shuah@kernel.org \
    --cc=tj@kernel.org \
    --cc=vdavydov.dev@gmail.com \
    --cc=zhangjiao2@cmss.chinamobile.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