Netdev List
 help / color / mirror / Atom feed
* [PATCH RFC nf-next 0/9] netfilter: nfqueue: account queued memory and reserve capacity
@ 2026-11-15 22:34 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
                   ` (8 more replies)
  0 siblings, 9 replies; 11+ messages in thread
From: Alireza Asgari via B4 Relay @ 2026-11-15 22:34 UTC (permalink / raw)
  To: netfilter-devel, linux-mm
  Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	Muchun Song, Andrew Morton, David Hildenbrand, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, Shuah Khan, Tejun Heo,
	Vladimir Davydov, Chris Down, Zhang Jiao, Song Hu, cgroups,
	netdev, linux-kselftest, Alireza Asgari

Hi,

NFQUEUE limits pending packets by count, although packet buffers can have
substantially different memory costs. Large skbs and GSO aggregates can
therefore consume much more memory than the packet-count limit suggests.
Independent queue limits also allow userspace to multiply the available
capacity by creating many queues.

This series implements the approach suggested by Florian Westphal in the
NFQUEUE memory-exhaustion discussion [1]: add a per-queue byte allowance
and reserve queue capacity against a memory cgroup. His message proposed
the overall approach; the ownership, reservation interface, and failure
policies below are implementation choices proposed for discussion in this
RFC.

The byte allowance is derived from the existing maximum queue length:

    (u64)queue_maxlen * (SKB_DATA_ALIGN(sizeof(struct sk_buff)) + 2048)

The 2048-byte contribution represents a nominal complete skb head,
including its shared-info area. The shell contribution follows skb
accounting alignment. The allowance is pooled across the queue, and the
existing packet-count limit remains enforced. Large skbs can exhaust the
byte allowance before the packet limit is reached. Existing drop and
FAIL_OPEN dispositions are preserved. No new userspace configuration
attribute is introduced.

The nominal head size is a policy choice, not a bound on ordinary receive
buffers. For example, mvneta can build a small packet's skb around a whole
page. On a 4 KiB-page system its initial cost is the aligned shell plus
4096 bytes, so it cannot enter an empty length-one queue under this policy.
With FAIL_OPEN disabled it is dropped; with FAIL_OPEN enabled it bypasses
that queue daemon's inspection and continues through reinjection. Later
Netfilter processing can still drop it. An independently configurable byte
limit could accommodate such buffers without increasing packet depth, but
the default for applications that omit it would also need agreement.

Each admitted entry saves its skb's admission-time truesize. That charge
remains through detachment from the pending list, including batch verdicts,
and is released when NFQUEUE reinjection processing begins. Failed
notification delivery rolls back admission and its charge. This ledger
measures saved accounting costs, rather than every allocation associated
with a packet; transient allocations and later payload expansion are
outside that measure.

The local byte limit applies in every network namespace. Outside the
initial network namespace, BIND captures an accounting owner from the
configuring task. The queue initially uses COPY_NONE and reserves no
capacity. Userspace can set its packet limit before enabling COPY_META or
COPY_PACKET; that first activation obtains the complete reservation before
enabling admission. A failed activation leaves the queue's prior limits,
mode, and flags intact. This allows a separate BIND, smaller-limit, and
enable sequence without first paying for the default queue size.

A BIND that includes configuration prepares the new instance privately,
including any activation reservation, and publishes it only on success.
Failed creation releases the private instance without exposing an inactive
queue to packet readers. In particular, a rule that bypasses an absent
queue cannot encounter a temporarily visible COPY_NONE queue while its
bundled creation is obtaining capacity. Standalone BIND still publishes an
inactive queue with its accounting owner captured.

Once activated, the queue retains its reservation policy even if userspace
later selects COPY_NONE. Resizing retains the BIND-time owner even if the
configuring task moves cgroups, subject to normal cgroup reparenting. The
charge makes queues owned within a constrained cgroup compete for its
budget. Reservations consume budget even for empty activated queues; they
do not preallocate packet storage or replace ordinary allocation charges.
Reservations captured with root ownership or disabled kmem accounting are
uncharged. A finite aggregate ceiling requires a constraining memory-cgroup
limit.

Charged capacity growth requires a 64-bit kernel. On 32-bit kernels,
in-range positive growth with a captured accounting owner fails with
-EOPNOTSUPP before charging or publishing a new configuration. Inactive
BIND and limit configuration remain possible; activating an affected queue
or increasing its existing reservation fails. Bundling activation into BIND
can therefore fail as well. The feature does not silently omit the charge.
Initial-network-namespace queues and reservations captured without a
chargeable owner remain supported. This restriction avoids making the
shared 32-bit page counters' arithmetic limits reachable with large virtual
charges; a per-queue maximum would not bound the aggregate hierarchy.

Using ordinary memcg charges means unused capacity contributes to
memory.current, kernel-memory statistics, and peak accounting. It can
affect configured memory.min/memory.low protection, memory.high pressure,
and the budget available to other allocations. Precharging makes capacity
refusal visible during configuration and avoids a new memcg charge
operation at packet admission. Charging retained bytes on admission instead
would avoid paying for unused capacity, but would move charging work and
possible refusal into the packet path. Neither approach reserves physical
memory or guarantees that later allocations succeed.

Increasing capacity obtains the additional reservation before publishing
the new limit. Failure leaves the existing configuration intact. Shrinking
preserves queued packets and retains page-rounded backing for the greater
of the configured allowance and outstanding saved charges. Excess capacity
is refunded as entries drain, with refund work serialized against resizing
and coordinated with queue destruction. Refunds settle the actual memory
counters as well as statistics, including charges inherited by root after
an owner's removal. On supported kernels, later growth under root ownership
remains uncharged and is excluded from those refunds.

The series also saturates the task's deferred memory.high page counter
instead of letting repeated nonblocking charges wrap it to zero. Large
virtual reservations can accumulate that debt before returning to userspace
without allocating corresponding physical pages. Saturation preserves
pending pressure handling; it does not change the owner-selection policy.

A separate MM prerequisite makes the memory.high delay calculation safe
across its scaling and multiplication steps, including consumption of the
deferred page debt. It corrects overflow and premature 32-bit truncation
while preserving the intended fixed-point rounding and previous
non-overflowing final delay results on 64-bit kernels. Large results remain
bounded at the existing maximum delay. This correction applies on both
32-bit and 64-bit kernels; it does not remove the reservation interface's
separate 64-bit restriction.

The arithmetic prerequisite overlaps Zhang Jiao's October submission [2]
and the earlier report from Song Hu [3]. Its description links the
associated MM review. The implementation here also covers the scaled
numerator and debt multiplication, and preserves sub-batch delay results.
The two generic MM fixes and their tests are separated from the reservation
interface for independent review; the arithmetic patch is an alternative
for discussion, not a revision of Zhang's submission.

The series introduces a bulk page-reservation interface in the memory
controller, with a referenced owner, transactional resizing, and module
support. Reservation growth uses GFP_NOWAIT and rejects PF_MEMALLOC growth,
avoiding direct reclaim, memcg OOM invocation, and forced overcharging.
This avoids repeated reclaim while queue configuration is serialized, but
permits refusal despite reclaimable memory. Deferred memory.high handling
remains possible, with an existing ownership limitation: the charge path
records deferred work on the current task, and the later handler selects
the cgroup from that task's memory context. If a queue bound in cgroup A is
enlarged after the task moves to sibling B, A is charged but deferred
handling can target B. A's memory.max remains enforced; handling A's
memory.high excess is not guaranteed by this path. The interface documents
this best-effort behavior rather than providing owner-aware pressure
handling on the stated base.

Shakeel Butt's queued memory.high enforcement series [4] changes this
behavior: remote charges schedule reclaim against the charged hierarchy
through high_work. It does not guarantee throttling of the configuring
task, and a fast charger can outrun that worker. That work is not included
in this nf-next base or attributed to this series. Its interaction with
reservations is assessed separately from the base tests.

I am posting this as RFC particularly for feedback on:

1. Whether packet depth and byte allowance should be independently
   configurable, and what default should apply to existing applications
   that only set NFQA_CFG_QUEUE_MAXLEN. Is the proposed 2 KiB head policy
   acceptable given its full-page-buffer and FAIL_OPEN consequences?

2. Whether configured capacity should consume ordinary memcg usage, or
   whether retained bytes should be charged at admission instead. Are the
   BIND-time owner and initial-network-namespace exception appropriate?

3. Whether the proposed bulk reservation interface is an appropriate MM
   interface for this use, including the explicit restriction of charged
   growth to 64-bit kernels. Is this compatibility tradeoff acceptable for
   the initial interface?

4. Whether non-reclaiming reservation growth is the desired failure policy.

5. Whether the queued MM work's best-effort background reclaim is
   sufficient for saved-owner capacity reservations, or stronger pressure
   semantics are needed. On the stated base, the existing owner mismatch
   described above remains.

The patched kernel passed three KUnit suites and the cgroup-enabled network
selftest in an x86-64 guest with two CPUs online, KASAN, and lockdep.
Coverage includes deferred-high saturation and delay arithmetic, creation
publication ordering, separate and bundled activation, refused activation,
retained accounting after COPY_NONE, actual root-counter refunds,
page-backed skbs, payload replacement, and bounded concurrent resizing,
verdicts, teardown, refunds, and queue reuse. The applicable KUnit cases
also passed on x86-32, with two charged-growth cases skipped as expected.
The delay tests exercise the production helpers; they do not reproduce the
complete reclaim-and-throttle path.

A separate probe requiring owner-aware memory.high handling failed: after
the task moved from the owner to its original/root cgroup, growth still
charged the owner but produced no new owner high events. This reproduces
the limitation described above, without establishing a bypass of
memory.max.

In a separate compatibility checkout with Shakeel's queued changes, that
probe observed an owner high event after migration. The growth operation
still returned promptly, consistent with background reclaim rather than
configuring-task throttling. That checkout also incorporates Pablo Neira
Ayuso's pending NFQUEUE reinjection series, with saved-charge releases
adapted to its terminal paths. Its KUnit and network tests passed. These
results concern the dependency integration, not the stated nf-next base.

Receive-driver/GRO behavior and throughput effects have not been evaluated.
Feedback is particularly requested on the allowance policy and reservation
interface, including the full-page-buffer, architecture-support, and
saved-owner memory.high behavior described above.

AI coding tools assisted with investigation, implementation, test
development, review, and drafting this RFC. I take responsibility for the
submitted changes and their technical justification.

[1] https://lists.openwall.net/netdev/2026/11/06/366
[2] https://lore.kernel.org/20261029073857.974-1-zhangjiao2@cmss.chinamobile.com/
[3] https://lore.kernel.org/20260818012115.65838-1-husong@kylinos.cn/
[4] https://lore.kernel.org/20261030205418.341214-1-shakeel.butt@linux.dev/

---
Alireza Asgari (9):
      memcg: saturate deferred memory.high page debt
      memcg: bound memory.high delay arithmetic
      memcg: add KUnit coverage for memory.high accounting
      memcg: add an owner-bound capacity reservation interface
      memcg: test capacity reservation rollback and refunds
      netfilter: nfqueue: account saved skb bytes per queue
      netfilter: nfqueue: reserve capacity against the bound owner
      netfilter: nfqueue: add KUnit memory accounting coverage
      selftests: netfilter: exercise nfqueue memory accounting

 include/linux/memcontrol.h                         |  35 ++
 include/net/netfilter/nf_queue.h                   |   1 +
 mm/Kconfig                                         |  21 +
 mm/memcontrol.c                                    | 216 +++++++++-
 mm/memcontrol_high_test.c                          | 162 ++++++++
 mm/memcontrol_reservation_test.c                   | 232 +++++++++++
 net/netfilter/Kconfig                              |  10 +
 net/netfilter/nfnetlink_queue.c                    | 281 +++++++++----
 net/netfilter/nfnetlink_queue_test.c               | 338 +++++++++++++++
 tools/testing/selftests/net/netfilter/Makefile     |   1 +
 .../selftests/net/netfilter/nft_queue_memory.py    | 460 +++++++++++++++++++++
 11 files changed, 1677 insertions(+), 80 deletions(-)
---
base-commit: 4810f60b9f0e9111cd0dcbfd2ca7a7c591a57464
change-id: 20261115-b4-nfqueue-memory-accounting-263e081fed39

Best regards,
--  
Alireza Asgari <alireza@asgari.net>



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH RFC nf-next 1/9] memcg: saturate deferred memory.high page debt
  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 ` 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
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: Alireza Asgari via B4 Relay @ 2026-11-15 22:34 UTC (permalink / raw)
  To: netfilter-devel, linux-mm
  Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	Muchun Song, Andrew Morton, David Hildenbrand, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, Shuah Khan, Tejun Heo,
	Vladimir Davydov, Chris Down, Zhang Jiao, Song Hu, cgroups,
	netdev, linux-kselftest, Alireza Asgari

From: Alireza Asgari <alireza@asgari.net>

Repeated nonblocking charges can accumulate deferred memory.high work
before the charging task returns to userspace. The unsigned-int page
counter can wrap, including to zero, and erase that pending work. Large
virtual capacity reservations can reach this range without allocating
corresponding physical memory, including on 64-bit kernels.

Saturate the addition at UINT_MAX. Ordinary additions retain their value;
very large debt retains the pressure signal instead of wrapping. This does
not change memory.max checks or promise exact debt beyond the saturation
point.

Found during AI-assisted source review of virtual capacity accounting.
The accompanying KUnit patch calls the real GFP_NOWAIT charging path with
an isolated counter fixture and a 65-page charge. It seeds debt near
UINT_MAX to cover exact-boundary, crossing, and already-saturated cases;
it does not allocate the corresponding large amount of physical memory.

The debt case passed on both 64-bit and 32-bit x86 KUnit kernels built
from this series. The test exercises the production charging and debt
update path; it does not reproduce end-to-end throttling.

Fixes: b23afb93d317 ("memcg: punt high overage reclaim to return-to-userland path")
Signed-off-by: Alireza Asgari <alireza@asgari.net>
---
 mm/memcontrol.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 84f4a8fd4d16..1980fb7431a8 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2894,8 +2894,12 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 			 * Target some best-effort fairness between the tasks,
 			 * and distribute reclaim work and delay penalties
 			 * based on how much each task is actually allocating.
+			 * Saturate so repeated nonblocking charges cannot erase
+			 * pending work by wrapping the counter back to zero.
 			 */
-			current->memcg_nr_pages_over_high += batch;
+			current->memcg_nr_pages_over_high += min(
+				batch,
+				UINT_MAX - current->memcg_nr_pages_over_high);
 			set_notify_resume(current);
 			break;
 		}

-- 
2.34.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH RFC nf-next 2/9] memcg: bound memory.high delay arithmetic
  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 ` 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
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: Alireza Asgari via B4 Relay @ 2026-11-15 22:34 UTC (permalink / raw)
  To: netfilter-devel, linux-mm
  Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	Muchun Song, Andrew Morton, David Hildenbrand, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, Shuah Khan, Tejun Heo,
	Vladimir Davydov, Chris Down, Zhang Jiao, Song Hu, cgroups,
	netdev, linux-kselftest, Alireza Asgari

From: Alireza Asgari <alireza@asgari.net>

The memory.high penalty can overflow while scaling the usage ratio,
squaring the scaled ratio, or multiplying by the task's deferred page
debt. An overflow can turn a large intended penalty into a small or zero
one. On 32-bit kernels the unsigned-long intermediate is also truncated
before two right shifts totaling 34 bits, making the delay zero.

Keep the intermediates in u64 and bound the relative excess at twice the
high limit. At that bound even one page of debt exceeds the existing
two-second final cap. A bound of one times the high limit would change
valid one-page results. Below the bound, use wide multiplication and
division so a large numerator with a modest ratio remains accurate.

Preserve the fixed-point rounding order. Bound each memory or swap
component before converting to unsigned long, then retain the existing
cap on their sum. With HZ below 12288, the bounded square times HZ fits
below 2^56, and the later product with UINT_MAX pages fits below 2^54.

This work was examined during AI-assisted source review of virtual
capacity accounting. Song Hu previously reported the square overflow;
Zhang Jiao proposed a fix for that overflow and the 32-bit intermediate.
Johannes Weiner and Bingfang Guo also discussed multiplication and return
width. This implementation addresses those problems and the numerator
and debt-scaling boundaries. It is an alternative for discussion, not a
claim of a newly discovered problem or a revision of Zhang's submission.

The accompanying KUnit cases call the arithmetic helpers directly on
32-bit and 64-bit kernels. They cover zero and sub-batch debt, ordinary
rounding, large numerators, the cap boundary, and the prior zero-delay
example. This isolates the arithmetic; it does not reproduce end-to-end
reclaim or throttling with large physical allocations.

Both arithmetic cases passed on 64-bit and 32-bit x86 KUnit kernels built
from this series. They exercise the production arithmetic helpers without
requiring large allocations or claiming an end-to-end throttling result.

Fixes: 0e4b01df8659 ("mm, memcg: throttle allocators when failing reclaim over memory.high")
Link: https://lore.kernel.org/20260818012115.65838-1-husong@kylinos.cn/
Link: https://lkml.rescloud.iu.edu/hypermail/linux/kernel/2610.3/11270.html
Link: https://lkml.rescloud.iu.edu/2610.3/12576.html
Link: https://lkml.rescloud.iu.edu/hypermail/linux/kernel/2610.3/11904.html
Signed-off-by: Alireza Asgari <alireza@asgari.net>
---
 mm/memcontrol.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 1980fb7431a8..665ce8dfb9ec 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2521,6 +2521,15 @@ static void high_irq_work_func(struct irq_work *work)
  #define MEMCG_DELAY_PRECISION_SHIFT 20
  #define MEMCG_DELAY_SCALING_SHIFT 14
 
+/*
+ * An overage of twice the high limit gives a nominal delay of 256 * HZ.
+ * Even one page of debt then exceeds the two-second cap. Bounding the
+ * overage here keeps the delay products in u64: with HZ < 12288, the
+ * square times HZ is below 2^56, and after scaling the product with
+ * UINT_MAX pages of debt is below 2^54.
+ */
+#define MEMCG_MAX_HIGH_OVERAGE (2ULL << MEMCG_DELAY_PRECISION_SHIFT)
+
 static u64 calculate_overage(unsigned long usage, unsigned long high)
 {
 	u64 overage;
@@ -2535,8 +2544,12 @@ static u64 calculate_overage(unsigned long usage, unsigned long high)
 	high = max(high, 1UL);
 
 	overage = usage - high;
-	overage <<= MEMCG_DELAY_PRECISION_SHIFT;
-	return div64_u64(overage, high);
+	if (overage >= high && overage - high >= high)
+		return MEMCG_MAX_HIGH_OVERAGE;
+
+	/* The scaled numerator may overflow u64 even for a small ratio. */
+	return mul_u64_u64_div_u64(overage, 1ULL << MEMCG_DELAY_PRECISION_SHIFT,
+				   high);
 }
 
 static u64 mem_find_max_overage(struct mem_cgroup *memcg)
@@ -2576,9 +2589,9 @@ static u64 swap_find_max_overage(struct mem_cgroup *memcg)
 static unsigned long calculate_high_delay(unsigned int nr_pages,
 					  u64 max_overage)
 {
-	unsigned long penalty_jiffies;
+	u64 penalty_jiffies;
 
-	if (!max_overage)
+	if (!nr_pages || !max_overage)
 		return 0;
 
 	/*
@@ -2589,6 +2602,7 @@ static unsigned long calculate_high_delay(unsigned int nr_pages,
 	 * its crazy behaviour, so we exponentially increase the delay based on
 	 * overage amount.
 	 */
+	max_overage = min(max_overage, MEMCG_MAX_HIGH_OVERAGE);
 	penalty_jiffies = max_overage * max_overage * HZ;
 	penalty_jiffies >>= MEMCG_DELAY_PRECISION_SHIFT;
 	penalty_jiffies >>= MEMCG_DELAY_SCALING_SHIFT;
@@ -2601,7 +2615,9 @@ static unsigned long calculate_high_delay(unsigned int nr_pages,
 	 * MEMCG_CHARGE_BATCH pages is nominal, so work out how much smaller or
 	 * larger the current charge patch is than that.
 	 */
-	return penalty_jiffies * nr_pages / MEMCG_CHARGE_BATCH;
+	penalty_jiffies =
+		div_u64(penalty_jiffies * nr_pages, MEMCG_CHARGE_BATCH);
+	return min_t(u64, penalty_jiffies, MEMCG_MAX_HIGH_DELAY_JIFFIES);
 }
 
 /*
@@ -2654,6 +2670,7 @@ void __mem_cgroup_handle_over_high(gfp_t gfp_mask)
 	penalty_jiffies = calculate_high_delay(nr_pages,
 					       mem_find_max_overage(memcg));
 
+	/* Each component is capped, so their sum also fits unsigned long. */
 	penalty_jiffies += calculate_high_delay(nr_pages,
 						swap_find_max_overage(memcg));
 

-- 
2.34.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH RFC nf-next 3/9] memcg: add KUnit coverage for memory.high accounting
  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 ` 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
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: Alireza Asgari via B4 Relay @ 2026-11-15 22:34 UTC (permalink / raw)
  To: netfilter-devel, linux-mm
  Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	Muchun Song, Andrew Morton, David Hildenbrand, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, Shuah Khan, Tejun Heo,
	Vladimir Davydov, Chris Down, Zhang Jiao, Song Hu, cgroups,
	netdev, linux-kselftest, Alireza Asgari

From: Alireza Asgari <alireza@asgari.net>

Exercise deferred page-debt saturation and the bounded delay arithmetic
without requiring very large memory allocations.

The debt test calls the real charge path using an isolated page-counter
fixture, small GFP_NOWAIT charges, and seeded debt values. It restores
task state and fixture counters before checking results. The arithmetic
cases call the internal helpers directly, including one-page debt and
large numerators whose scaled product would overflow u64.

Keep this coverage independent of the capacity-reservation interface so
the generic MM fixes can be reviewed and tested separately. The tests
require built-in KUnit and memcg support on either word size.

Signed-off-by: Alireza Asgari <alireza@asgari.net>
---
 mm/Kconfig                |  10 +++
 mm/memcontrol.c           |   4 ++
 mm/memcontrol_high_test.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 176 insertions(+)

diff --git a/mm/Kconfig b/mm/Kconfig
index afff1b25b543..dd763d3d0e36 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -1531,6 +1531,16 @@ config PTDUMP
 	bool
 
 
+config MEMCG_HIGH_KUNIT_TEST
+	bool "Memory cgroup high limit tests" if !KUNIT_ALL_TESTS
+	depends on MEMCG && KUNIT=y
+	default KUNIT_ALL_TESTS
+	help
+	  Test deferred memory.high debt saturation through the real charging
+	  path with isolated counters and small charges. Delay tests cover
+	  fixed-point rounding and arithmetic bounds on 32-bit and 64-bit
+	  kernels. Say N unless developing memory cgroup high limit handling.
+
 source "mm/damon/Kconfig"
 
 endmenu
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 665ce8dfb9ec..446b60ca6aef 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6532,3 +6532,7 @@ void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg)
 		K(atomic_long_read(&memcg->memory.children_min_usage)),
 		K(atomic_long_read(&memcg->memory.children_low_usage)));
 }
+
+#if IS_ENABLED(CONFIG_MEMCG_HIGH_KUNIT_TEST)
+#include "memcontrol_high_test.c"
+#endif
diff --git a/mm/memcontrol_high_test.c b/mm/memcontrol_high_test.c
new file mode 100644
index 000000000000..bf792d398021
--- /dev/null
+++ b/mm/memcontrol_high_test.c
@@ -0,0 +1,162 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <kunit/test.h>
+
+static void memcg_high_test_restore_counter(struct page_counter *counter,
+					    unsigned long before)
+{
+	unsigned long after = page_counter_read(counter);
+
+	if (after > before)
+		page_counter_uncharge(counter, after - before);
+	else if (after < before)
+		page_counter_charge(counter, before - after);
+}
+
+static void memcg_high_test_debt(struct kunit *test)
+{
+	const unsigned int nr_pages = MEMCG_CHARGE_BATCH + 1;
+	const struct {
+		unsigned int before;
+		unsigned int expected;
+	} cases[] = {
+		{ 0, nr_pages },
+		{ UINT_MAX - nr_pages, UINT_MAX },
+		{ UINT_MAX - nr_pages + 1, UINT_MAX },
+		{ UINT_MAX, UINT_MAX },
+	};
+	struct mem_cgroup *memcg;
+	unsigned int i;
+
+	memcg = kunit_kzalloc(test, sizeof(*memcg), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, memcg);
+	page_counter_init(&memcg->memory, NULL, false);
+	page_counter_init(&memcg->memsw, NULL, false);
+	page_counter_init(&memcg->swap, NULL, false);
+	page_counter_set_high(&memcg->memory, 0);
+	page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
+
+	for (i = 0; i < ARRAY_SIZE(cases); i++) {
+		unsigned int saved = current->memcg_nr_pages_over_high;
+		bool notify = test_thread_flag(TIF_NOTIFY_RESUME);
+		unsigned int debt;
+		bool notified;
+		int ret;
+
+		/*
+		 * Exceed the stock batch to use the real counter charge path
+		 * without leaving a reference to this isolated fixture in stock.
+		 * GFP_NOWAIT defers reclaim until return to userspace.
+		 */
+		current->memcg_nr_pages_over_high = cases[i].before;
+		ret = try_charge_memcg(memcg, GFP_NOWAIT, nr_pages);
+		debt = current->memcg_nr_pages_over_high;
+		notified = test_thread_flag(TIF_NOTIFY_RESUME);
+
+		/* Restore the test task before any assertion can abort the case. */
+		current->memcg_nr_pages_over_high = saved;
+		if (!notify)
+			clear_thread_flag(TIF_NOTIFY_RESUME);
+		memcg_high_test_restore_counter(&memcg->memory, 0);
+		if (do_memsw_account())
+			memcg_high_test_restore_counter(&memcg->memsw, 0);
+
+		KUNIT_EXPECT_EQ_MSG(test, ret, 0, "case %u", i);
+		KUNIT_EXPECT_EQ_MSG(test, debt, cases[i].expected, "case %u",
+				    i);
+		KUNIT_EXPECT_TRUE_MSG(test, notified, "case %u", i);
+	}
+}
+
+static void memcg_high_test_overage(struct kunit *test)
+{
+	const unsigned long large_high = 1UL << (BITS_PER_LONG - 3);
+	const struct {
+		unsigned long usage;
+		unsigned long high;
+		u64 expected;
+	} cases[] = {
+		{ 0, 0, 0 },
+		{ 1, 0, 0 },
+		{ 2, 0, 1ULL << 20 },
+		{ 99, 100, 0 },
+		{ 100, 100, 0 },
+		{ 110, 100, 104857 },
+		{ 150, 100, 524288 },
+		{ 299, 100, 2086666 },
+		{ 300, 100, 2ULL << 20 },
+		{ 301, 100, 2ULL << 20 },
+		{ ULONG_MAX, 1, 2ULL << 20 },
+		{ ULONG_MAX, ULONG_MAX - 1, 0 },
+		/* A modest ratio whose scaled numerator exceeds u64 on 64-bit. */
+		{ large_high + large_high / 16, large_high, 1ULL << 16 },
+	};
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(cases); i++)
+		KUNIT_EXPECT_EQ_MSG(
+			test, calculate_overage(cases[i].usage, cases[i].high),
+			cases[i].expected, "case %u", i);
+}
+
+static void memcg_high_test_delay(struct kunit *test)
+{
+	const unsigned long cap = MEMCG_MAX_HIGH_DELAY_JIFFIES;
+	const struct {
+		unsigned int pages;
+		u64 overage;
+		unsigned long expected;
+	} cases[] = {
+		{ 0, 0, 0 },
+		{ 0, U64_MAX, 0 },
+		{ UINT_MAX, 0, 0 },
+		{ 1, 1ULL << 16, (HZ / 4) / 64 },
+		{ 63, 1ULL << 16, (HZ / 4) * 63 / 64 },
+		{ 64, 1ULL << 16, HZ / 4 },
+		{ 65, 1ULL << 16, (HZ / 4) * 65 / 64 },
+		{ UINT_MAX, 1ULL << 16, cap },
+		{ 64, 1ULL << 17, HZ },
+		/* Apply debt scaling before the cap, including sub-batch debt. */
+		{ 1, 1ULL << 18, HZ / 16 },
+		{ 1, 1ULL << 20, HZ },
+		{ 2, 1ULL << 20, cap },
+		{ 1, 2ULL << 20, cap },
+		{ 64, 1ULL << 32, cap },
+		{ UINT_MAX, 1ULL << 32, cap },
+		{ 1, U64_MAX, cap },
+		{ UINT_MAX, U64_MAX, cap },
+	};
+	unsigned long delay;
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(cases); i++)
+		KUNIT_EXPECT_EQ_MSG(test,
+				    calculate_high_delay(cases[i].pages,
+							 cases[i].overage),
+				    cases[i].expected, "case %u", i);
+
+	/* 32 GiB over an 8 MiB high limit, expressed in 4 KiB pages. */
+	KUNIT_EXPECT_EQ(
+		test,
+		calculate_high_delay(64, calculate_overage((1UL << 23) + 2048,
+							   2048)),
+		cap);
+	/* Both memory and swap contributions are bounded before addition. */
+	delay = calculate_high_delay(UINT_MAX, U64_MAX);
+	delay += calculate_high_delay(UINT_MAX, U64_MAX);
+	KUNIT_EXPECT_EQ(test, delay, 2 * cap);
+}
+
+static struct kunit_case memcg_high_test_cases[] = {
+	KUNIT_CASE(memcg_high_test_debt),
+	KUNIT_CASE(memcg_high_test_overage),
+	KUNIT_CASE(memcg_high_test_delay),
+	{}
+};
+
+static struct kunit_suite memcg_high_test_suite = {
+	.name = "memcg_high",
+	.test_cases = memcg_high_test_cases,
+};
+
+kunit_test_suite(memcg_high_test_suite);

-- 
2.34.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH RFC nf-next 4/9] memcg: add an owner-bound capacity reservation interface
  2026-11-15 22:34 [PATCH RFC nf-next 0/9] netfilter: nfqueue: account queued memory and reserve capacity Alireza Asgari via B4 Relay
                   ` (2 preceding siblings ...)
  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 ` 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
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: Alireza Asgari via B4 Relay @ 2026-11-15 22:34 UTC (permalink / raw)
  To: netfilter-devel, linux-mm
  Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	Muchun Song, Andrew Morton, David Hildenbrand, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, Shuah Khan, Tejun Heo,
	Vladimir Davydov, Chris Down, Zhang Jiao, Song Hu, cgroups,
	netdev, linux-kselftest, Alireza Asgari

From: Alireza Asgari <alireza@asgari.net>

NFQUEUE capacity accounting needs to charge budget in configuration
context while preserving the owner selected when the queue was bound.
Add a proposed bulk page-reservation interface backed by a referenced
objcg. This reserves accounting budget; it does not allocate physical
pages or replace the charges for real allocations.

Track logical reserved pages separately from pages actually charged.
The distinction permits uncharged growth after reparenting to root while
still refunding inherited charges. Apply actual recorded refunds to the
page counters even when the current owner is root.

Use bounded GFP_NOWAIT charge batches. Failed growth rolls back its new
charges and leaves the old reservation intact. Reject PF_MEMALLOC growth
rather than allowing forced charges. Callers serialize operations in
sleepable context; ordinary memory.high handling can remain deferred.
On this base, deferred handling can target the current task's cgroup
instead of a saved owner after task migration.

Positive growth with a captured owner requires a 64-bit kernel. Virtual
reservations can otherwise bring shared 32-bit page counters near their
signed range without allocating matching physical memory. No-owner
reservations, shrinking, and destruction remain supported on 32-bit.

The API, use of ordinary memcg usage for empty capacity, and architecture
restriction are RFC policy choices requiring MM review.

Signed-off-by: Alireza Asgari <alireza@asgari.net>
---
 include/linux/memcontrol.h |  35 ++++++++++
 mm/memcontrol.c            | 163 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 198 insertions(+)

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 69e1bcc67c58..d25ff10976ec 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -32,6 +32,41 @@ struct mm_struct;
 struct kmem_cache;
 struct swap_cluster_info;
 
+/* Capacity accounting without allocating the reserved pages. */
+struct mem_cgroup_reservation {
+	struct obj_cgroup *objcg;
+	u64 pages;
+	u64 charged_pages; /* excludes growth after reparenting to root */
+};
+
+#ifdef CONFIG_MEMCG
+void mem_cgroup_reservation_init(struct mem_cgroup_reservation *res);
+int mem_cgroup_reservation_resize(struct mem_cgroup_reservation *res,
+				  u64 pages);
+void mem_cgroup_reservation_destroy(struct mem_cgroup_reservation *res);
+#else
+static inline void
+mem_cgroup_reservation_init(struct mem_cgroup_reservation *res)
+{
+	res->objcg = NULL;
+	res->pages = 0;
+	res->charged_pages = 0;
+}
+
+static inline int
+mem_cgroup_reservation_resize(struct mem_cgroup_reservation *res, u64 pages)
+{
+	res->pages = pages;
+	return 0;
+}
+
+static inline void
+mem_cgroup_reservation_destroy(struct mem_cgroup_reservation *res)
+{
+	mem_cgroup_reservation_init(res);
+}
+#endif
+
 /* Cgroup-specific page state, on top of universal node page state */
 enum memcg_stat_item {
 	MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS,
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 446b60ca6aef..0227d9b411f5 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3255,6 +3255,169 @@ static int obj_cgroup_charge_pages(struct obj_cgroup *objcg, gfp_t gfp,
 	return ret;
 }
 
+/* Keep both the page count and the signed kmem statistics deltas in range. */
+#define MEMCG_RESERVATION_BATCH (SZ_1G >> PAGE_SHIFT)
+
+static int mem_cgroup_reservation_charge(struct obj_cgroup *objcg,
+					 unsigned int pages, bool *charged)
+{
+	struct mem_cgroup *memcg;
+	int ret = 0;
+
+	memcg = get_mem_cgroup_from_objcg(objcg);
+	*charged = false;
+	/* Reparenting to root removes the limit, not previously held charges. */
+	if (!mem_cgroup_is_root(memcg)) {
+		/* Do not reclaim repeatedly while the caller serializes configuration. */
+		ret = try_charge(memcg, GFP_NOWAIT, pages);
+		if (!ret) {
+			account_kmem_nmi_safe(memcg, pages);
+			memcg1_account_kmem(memcg, pages);
+			*charged = true;
+		}
+	}
+	css_put(&memcg->css);
+	return ret;
+}
+
+static void mem_cgroup_reservation_uncharge_batch(struct mem_cgroup *memcg,
+						  unsigned int pages)
+{
+	account_kmem_nmi_safe(memcg, -pages);
+	memcg1_account_kmem(memcg, -pages);
+	/* These pages were charged, even if their owner is now root. */
+	memcg_uncharge(memcg, pages);
+}
+
+static void mem_cgroup_reservation_uncharge(struct obj_cgroup *objcg, u64 pages)
+{
+	while (pages) {
+		unsigned int batch = min_t(u64, pages, MEMCG_RESERVATION_BATCH);
+		struct mem_cgroup *memcg;
+
+		memcg = get_mem_cgroup_from_objcg(objcg);
+		mem_cgroup_reservation_uncharge_batch(memcg, batch);
+		css_put(&memcg->css);
+		pages -= batch;
+		cond_resched();
+	}
+}
+
+/**
+ * mem_cgroup_reservation_init - capture the current kernel memory owner
+ * @res: uninitialized reservation
+ *
+ * The owner is referenced until destruction and follows objcg reparenting.
+ * Root ownership or disabled kmem accounting leaves an uncharged reservation.
+ * The caller must serialize operations and use sleepable task context.
+ */
+void mem_cgroup_reservation_init(struct mem_cgroup_reservation *res)
+{
+	*res = (struct mem_cgroup_reservation){};
+	if (!mem_cgroup_disabled() && !mem_cgroup_kmem_disabled()) {
+		res->objcg = get_obj_cgroup_from_current();
+		if (res->objcg && obj_cgroup_is_root(res->objcg)) {
+			obj_cgroup_put(res->objcg);
+			res->objcg = NULL;
+		}
+	}
+}
+EXPORT_SYMBOL_GPL(mem_cgroup_reservation_init);
+
+/**
+ * mem_cgroup_reservation_resize - change a capacity charge in pages
+ * @res: initialized, externally serialized reservation
+ * @pages: new total reservation, in pages
+ *
+ * Growth does not reclaim, invoke the memcg OOM killer, or force-charge a
+ * failing reservation. It may therefore fail despite reclaimable memory.
+ * A failed growth leaves the old charge intact. This reserves budget, not
+ * physical pages, and does not replace charges for actual allocations.
+ * Growth with a captured owner requires a 64-bit kernel: virtual reservations
+ * must not bring 32-bit page counters close to their signed arithmetic limit.
+ * Unsupported growth returns -EOPNOTSUPP without changing the reservation.
+ * Reservations initialized without an owner, shrinking, and destruction
+ * remain supported.
+ *
+ * Successful growth may schedule deferred memory.high handling for current.
+ * That handler uses current's mm owner, which can differ from the reservation
+ * owner after task migration or cgroup reparenting. It does not guarantee
+ * reclaim or throttling for the charged owner. memory.max checks still use
+ * the reservation owner and its ancestors.
+ *
+ * Return: zero on success, or a negative error code.
+ */
+int mem_cgroup_reservation_resize(struct mem_cgroup_reservation *res, u64 pages)
+{
+	u64 charged = 0, remaining;
+	int ret;
+
+	BUILD_BUG_ON(!MEMCG_RESERVATION_BATCH);
+	BUILD_BUG_ON(MEMCG_RESERVATION_BATCH > INT_MAX);
+
+	if (!res->objcg)
+		goto commit;
+	if (pages > PAGE_COUNTER_MAX)
+		return -E2BIG;
+	if (pages <= res->pages) {
+		u64 retained = min(pages, res->charged_pages);
+
+		mem_cgroup_reservation_uncharge(res->objcg,
+						res->charged_pages - retained);
+		res->charged_pages = retained;
+		goto commit;
+	}
+	if (!IS_ENABLED(CONFIG_64BIT))
+		return -EOPNOTSUPP;
+	/* try_charge_memcg() otherwise allows reclaim contexts to overcharge. */
+	if (current->flags & PF_MEMALLOC)
+		return -ENOMEM;
+
+	remaining = pages - res->pages;
+	while (remaining) {
+		unsigned int batch =
+			min_t(u64, remaining, MEMCG_RESERVATION_BATCH);
+		bool accounted;
+
+		if (fatal_signal_pending(current)) {
+			ret = -EINTR;
+			goto rollback;
+		}
+		ret = mem_cgroup_reservation_charge(res->objcg, batch,
+						    &accounted);
+		if (ret)
+			goto rollback;
+		if (accounted)
+			charged += batch;
+		remaining -= batch;
+		cond_resched();
+	}
+	res->charged_pages += charged;
+commit:
+	res->pages = pages;
+	return 0;
+rollback:
+	mem_cgroup_reservation_uncharge(res->objcg, charged);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mem_cgroup_reservation_resize);
+
+/**
+ * mem_cgroup_reservation_destroy - release capacity and its owner reference
+ * @res: reservation with no remaining concurrent users
+ */
+void mem_cgroup_reservation_destroy(struct mem_cgroup_reservation *res)
+{
+	if (res->objcg) {
+		mem_cgroup_reservation_uncharge(res->objcg, res->charged_pages);
+		obj_cgroup_put(res->objcg);
+	}
+	res->objcg = NULL;
+	res->pages = 0;
+	res->charged_pages = 0;
+}
+EXPORT_SYMBOL_GPL(mem_cgroup_reservation_destroy);
+
 static struct obj_cgroup *page_objcg(const struct page *page)
 {
 	unsigned long memcg_data = page->memcg_data;

-- 
2.34.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH RFC nf-next 5/9] memcg: test capacity reservation rollback and refunds
  2026-11-15 22:34 [PATCH RFC nf-next 0/9] netfilter: nfqueue: account queued memory and reserve capacity Alireza Asgari via B4 Relay
                   ` (3 preceding siblings ...)
  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 ` 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
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 11+ messages in thread
From: Alireza Asgari via B4 Relay @ 2026-11-15 22:34 UTC (permalink / raw)
  To: netfilter-devel, linux-mm
  Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	Muchun Song, Andrew Morton, David Hildenbrand, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, Shuah Khan, Tejun Heo,
	Vladimir Davydov, Chris Down, Zhang Jiao, Song Hu, cgroups,
	netdev, linux-kselftest, Alireza Asgari

From: Alireza Asgari <alireza@asgari.net>

Add reservation-specific KUnit coverage for partial-growth rollback,
reparented-root growth, range and architecture checks, and root refunds.

Transaction cases inject charge results through KUnit static stubs.
The root-refund case uses temporary real root page-counter charges while
other CPUs are stopped, and restores the counters even if the expected
refund is absent. It models charges inherited after reparenting; the
network selftest separately exercises real cgroup removal.

Keep these tests separate from the generic memory.high suite and make
all redirection conditional on the reservation KUnit option.

Signed-off-by: Alireza Asgari <alireza@asgari.net>
---
 mm/Kconfig                       |  11 ++
 mm/memcontrol.c                  |  16 +++
 mm/memcontrol_reservation_test.c | 232 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 259 insertions(+)

diff --git a/mm/Kconfig b/mm/Kconfig
index dd763d3d0e36..39db388f193f 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -1541,6 +1541,17 @@ config MEMCG_HIGH_KUNIT_TEST
 	  fixed-point rounding and arithmetic bounds on 32-bit and 64-bit
 	  kernels. Say N unless developing memory cgroup high limit handling.
 
+config MEMCG_RESERVATION_KUNIT_TEST
+	bool "Memory cgroup capacity reservation tests" if !KUNIT_ALL_TESTS
+	depends on MEMCG && KUNIT=y
+	default KUNIT_ALL_TESTS
+	help
+	  Test capacity reservation transactions, including partial growth
+	  failure, architecture support, and uncharged growth after reparenting
+	  to root. Transaction tests inject charge results. The root refund test
+	  checks real counters with a temporary eight-page charge while other
+	  CPUs are stopped. Say N unless developing this interface.
+
 source "mm/damon/Kconfig"
 
 endmenu
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 0227d9b411f5..d49ce5a92c1f 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -73,6 +73,10 @@
 
 #include <linux/uaccess.h>
 
+#if IS_ENABLED(CONFIG_MEMCG_RESERVATION_KUNIT_TEST)
+#include <kunit/static_stub.h>
+#endif
+
 #define CREATE_TRACE_POINTS
 #include <trace/events/memcg.h>
 #undef CREATE_TRACE_POINTS
@@ -3264,6 +3268,10 @@ static int mem_cgroup_reservation_charge(struct obj_cgroup *objcg,
 	struct mem_cgroup *memcg;
 	int ret = 0;
 
+#if IS_ENABLED(CONFIG_MEMCG_RESERVATION_KUNIT_TEST)
+	KUNIT_STATIC_STUB_REDIRECT(mem_cgroup_reservation_charge, objcg, pages,
+				   charged);
+#endif
 	memcg = get_mem_cgroup_from_objcg(objcg);
 	*charged = false;
 	/* Reparenting to root removes the limit, not previously held charges. */
@@ -3291,6 +3299,10 @@ static void mem_cgroup_reservation_uncharge_batch(struct mem_cgroup *memcg,
 
 static void mem_cgroup_reservation_uncharge(struct obj_cgroup *objcg, u64 pages)
 {
+#if IS_ENABLED(CONFIG_MEMCG_RESERVATION_KUNIT_TEST)
+	KUNIT_STATIC_STUB_REDIRECT(mem_cgroup_reservation_uncharge, objcg,
+				   pages);
+#endif
 	while (pages) {
 		unsigned int batch = min_t(u64, pages, MEMCG_RESERVATION_BATCH);
 		struct mem_cgroup *memcg;
@@ -6699,3 +6711,7 @@ void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg)
 #if IS_ENABLED(CONFIG_MEMCG_HIGH_KUNIT_TEST)
 #include "memcontrol_high_test.c"
 #endif
+
+#if IS_ENABLED(CONFIG_MEMCG_RESERVATION_KUNIT_TEST)
+#include "memcontrol_reservation_test.c"
+#endif
diff --git a/mm/memcontrol_reservation_test.c b/mm/memcontrol_reservation_test.c
new file mode 100644
index 000000000000..5ce4f05431b3
--- /dev/null
+++ b/mm/memcontrol_reservation_test.c
@@ -0,0 +1,232 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <kunit/test.h>
+#include <linux/stop_machine.h>
+
+struct memcg_reservation_test {
+	struct obj_cgroup owner;
+	struct mem_cgroup_reservation res;
+	u64 refunds;
+	unsigned int calls;
+	unsigned int fail_call;
+	bool uncharged;
+};
+
+static int memcg_reservation_test_charge(struct obj_cgroup *objcg,
+					 unsigned int pages, bool *charged)
+{
+	struct memcg_reservation_test *ctx = kunit_get_current_test()->priv;
+
+	*charged = !ctx->uncharged;
+	if (++ctx->calls == ctx->fail_call)
+		return -ENOMEM;
+	return 0;
+}
+
+static void memcg_reservation_test_uncharge(struct obj_cgroup *objcg, u64 pages)
+{
+	struct memcg_reservation_test *ctx = kunit_get_current_test()->priv;
+
+	ctx->refunds += pages;
+}
+
+static int memcg_reservation_test_init(struct kunit *test)
+{
+	struct memcg_reservation_test *ctx;
+
+	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+	if (!ctx)
+		return -ENOMEM;
+	/* Transaction cases stub accounting; the root refund case calls the batch. */
+	ctx->owner.is_root = true;
+	ctx->res.objcg = &ctx->owner;
+	test->priv = ctx;
+	kunit_activate_static_stub(test, mem_cgroup_reservation_charge,
+				   memcg_reservation_test_charge);
+	kunit_activate_static_stub(test, mem_cgroup_reservation_uncharge,
+				   memcg_reservation_test_uncharge);
+	return 0;
+}
+
+static void memcg_reservation_test_rollback(struct kunit *test)
+{
+	struct memcg_reservation_test *ctx = test->priv;
+	u64 original = 7;
+
+	if (!IS_ENABLED(CONFIG_64BIT)) {
+		kunit_skip(test,
+			   "Charged reservations require 64-bit counters");
+		return;
+	}
+	KUNIT_ASSERT_EQ(test,
+			mem_cgroup_reservation_resize(&ctx->res, original), 0);
+	ctx->calls = 0;
+	ctx->fail_call = 2;
+	KUNIT_EXPECT_EQ(test,
+			mem_cgroup_reservation_resize(
+				&ctx->res,
+				original + MEMCG_RESERVATION_BATCH + 1),
+			-ENOMEM);
+	KUNIT_EXPECT_EQ(test, ctx->res.pages, original);
+	KUNIT_EXPECT_EQ(test, ctx->res.charged_pages, original);
+	KUNIT_EXPECT_EQ(test, ctx->refunds, (u64)MEMCG_RESERVATION_BATCH);
+	KUNIT_EXPECT_EQ(test, ctx->calls, 2U);
+	mem_cgroup_reservation_destroy(&ctx->res);
+	KUNIT_EXPECT_EQ(test, ctx->refunds, MEMCG_RESERVATION_BATCH + original);
+	KUNIT_EXPECT_PTR_EQ(test, ctx->res.objcg, NULL);
+}
+
+static void memcg_reservation_test_root_growth(struct kunit *test)
+{
+	struct memcg_reservation_test *ctx = test->priv;
+
+	if (!IS_ENABLED(CONFIG_64BIT)) {
+		kunit_skip(test,
+			   "Charged reservations require 64-bit counters");
+		return;
+	}
+	KUNIT_ASSERT_EQ(test, mem_cgroup_reservation_resize(&ctx->res, 8), 0);
+	/* Simulate the charge helper observing reparenting to root. */
+	ctx->uncharged = true;
+	KUNIT_ASSERT_EQ(test, mem_cgroup_reservation_resize(&ctx->res, 16), 0);
+	KUNIT_EXPECT_EQ(test, ctx->res.charged_pages, 8ULL);
+	KUNIT_ASSERT_EQ(test, mem_cgroup_reservation_resize(&ctx->res, 10), 0);
+	KUNIT_EXPECT_EQ(test, ctx->refunds, 0ULL);
+	KUNIT_ASSERT_EQ(test, mem_cgroup_reservation_resize(&ctx->res, 3), 0);
+	KUNIT_EXPECT_EQ(test, ctx->res.charged_pages, 3ULL);
+	KUNIT_EXPECT_EQ(test, ctx->refunds, 5ULL);
+	mem_cgroup_reservation_destroy(&ctx->res);
+	KUNIT_EXPECT_EQ(test, ctx->refunds, 8ULL);
+	KUNIT_EXPECT_EQ(test, ctx->res.pages, 0ULL);
+	KUNIT_EXPECT_EQ(test, ctx->res.charged_pages, 0ULL);
+}
+
+static void memcg_reservation_test_range(struct kunit *test)
+{
+	struct memcg_reservation_test *ctx = test->priv;
+
+	KUNIT_EXPECT_EQ(test,
+			mem_cgroup_reservation_resize(
+				&ctx->res, (u64)PAGE_COUNTER_MAX + 1),
+			-E2BIG);
+	KUNIT_EXPECT_EQ(test, ctx->calls, 0U);
+	KUNIT_EXPECT_EQ(test, ctx->res.pages, 0ULL);
+	mem_cgroup_reservation_destroy(&ctx->res);
+	/* Disabled/root ownership still records the logical reservation. */
+	KUNIT_EXPECT_EQ(test, mem_cgroup_reservation_resize(&ctx->res, 9), 0);
+	KUNIT_EXPECT_EQ(test, ctx->res.pages, 9ULL);
+	KUNIT_EXPECT_EQ(test, ctx->res.charged_pages, 0ULL);
+	mem_cgroup_reservation_destroy(&ctx->res);
+}
+
+static void memcg_reservation_test_arch_support(struct kunit *test)
+{
+	struct memcg_reservation_test *ctx = test->priv;
+	bool supported = IS_ENABLED(CONFIG_64BIT);
+
+	KUNIT_ASSERT_EQ(test, mem_cgroup_reservation_resize(&ctx->res, 0), 0);
+	KUNIT_EXPECT_EQ(test, ctx->calls, 0U);
+	KUNIT_EXPECT_EQ(test, mem_cgroup_reservation_resize(&ctx->res, 9),
+			supported ? 0 : -EOPNOTSUPP);
+	KUNIT_EXPECT_EQ(test, ctx->calls, supported ? 1U : 0U);
+	KUNIT_EXPECT_EQ(test, ctx->res.pages, supported ? 9ULL : 0ULL);
+	KUNIT_EXPECT_EQ(test, ctx->res.charged_pages, supported ? 9ULL : 0ULL);
+	KUNIT_EXPECT_EQ(test, ctx->refunds, 0ULL);
+	mem_cgroup_reservation_destroy(&ctx->res);
+	KUNIT_EXPECT_EQ(test, ctx->refunds, supported ? 9ULL : 0ULL);
+	KUNIT_EXPECT_PTR_EQ(test, ctx->res.objcg, NULL);
+
+	/* An uncharged reservation remains usable on either architecture. */
+	KUNIT_ASSERT_EQ(test, mem_cgroup_reservation_resize(&ctx->res, 9), 0);
+	KUNIT_EXPECT_EQ(test, ctx->res.pages, 9ULL);
+	KUNIT_EXPECT_EQ(test, ctx->res.charged_pages, 0ULL);
+	KUNIT_EXPECT_EQ(test, ctx->calls, supported ? 1U : 0U);
+	mem_cgroup_reservation_destroy(&ctx->res);
+	KUNIT_EXPECT_EQ(test, ctx->refunds, supported ? 9ULL : 0ULL);
+}
+
+struct memcg_reservation_root_refund {
+	unsigned long memory_before, memory_shrunk, memory_after;
+	unsigned long memsw_before, memsw_shrunk, memsw_after;
+	bool memsw;
+};
+
+static void memcg_reservation_test_restore_counter(struct page_counter *counter,
+						   unsigned long before)
+{
+	unsigned long after = page_counter_read(counter);
+
+	if (after > before)
+		page_counter_uncharge(counter, after - before);
+	else if (after < before)
+		page_counter_charge(counter, before - after);
+}
+
+static int memcg_reservation_test_root_refund_stopped(void *data)
+{
+	struct memcg_reservation_root_refund *result = data;
+	struct mem_cgroup *memcg = root_mem_cgroup;
+
+	/* Model charged capacity inherited from a removed child. */
+	result->memory_before = page_counter_read(&memcg->memory);
+	result->memsw_before = page_counter_read(&memcg->memsw);
+	page_counter_charge(&memcg->memory, 8);
+	if (result->memsw)
+		page_counter_charge(&memcg->memsw, 8);
+	account_kmem_nmi_safe(memcg, 8);
+	memcg1_account_kmem(memcg, 8);
+
+	mem_cgroup_reservation_uncharge_batch(memcg, 3);
+	result->memory_shrunk = page_counter_read(&memcg->memory);
+	result->memsw_shrunk = page_counter_read(&memcg->memsw);
+	mem_cgroup_reservation_uncharge_batch(memcg, 5);
+	result->memory_after = page_counter_read(&memcg->memory);
+	result->memsw_after = page_counter_read(&memcg->memsw);
+
+	/* Leave no counter imbalance behind if the refund regression returns. */
+	memcg_reservation_test_restore_counter(&memcg->memory,
+					       result->memory_before);
+	if (result->memsw)
+		memcg_reservation_test_restore_counter(&memcg->memsw,
+						       result->memsw_before);
+	return 0;
+}
+
+static void memcg_reservation_test_root_refund(struct kunit *test)
+{
+	struct memcg_reservation_root_refund result = {
+		.memsw = do_memsw_account(),
+	};
+
+	if (mem_cgroup_disabled() || mem_cgroup_kmem_disabled()) {
+		kunit_skip(test, "Memory cgroup accounting is disabled");
+		return;
+	}
+	/* Exclude background charges while checking the actual root counters. */
+	KUNIT_ASSERT_EQ(test,
+			stop_machine(memcg_reservation_test_root_refund_stopped,
+				     &result, NULL),
+			0);
+	KUNIT_EXPECT_EQ(test, result.memory_shrunk, result.memory_before + 5);
+	KUNIT_EXPECT_EQ(test, result.memory_after, result.memory_before);
+	KUNIT_EXPECT_EQ(test, result.memsw_shrunk,
+			result.memsw_before + (result.memsw ? 5 : 0));
+	KUNIT_EXPECT_EQ(test, result.memsw_after, result.memsw_before);
+}
+
+static struct kunit_case memcg_reservation_test_cases[] = {
+	KUNIT_CASE(memcg_reservation_test_rollback),
+	KUNIT_CASE(memcg_reservation_test_root_growth),
+	KUNIT_CASE(memcg_reservation_test_range),
+	KUNIT_CASE(memcg_reservation_test_arch_support),
+	KUNIT_CASE(memcg_reservation_test_root_refund),
+	{}
+};
+
+static struct kunit_suite memcg_reservation_test_suite = {
+	.name = "memcg_reservation",
+	.init = memcg_reservation_test_init,
+	.test_cases = memcg_reservation_test_cases,
+};
+
+kunit_test_suite(memcg_reservation_test_suite);

-- 
2.34.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH RFC nf-next 6/9] netfilter: nfqueue: account saved skb bytes per queue
  2026-11-15 22:34 [PATCH RFC nf-next 0/9] netfilter: nfqueue: account queued memory and reserve capacity Alireza Asgari via B4 Relay
                   ` (4 preceding siblings ...)
  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 ` 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
                   ` (2 subsequent siblings)
  8 siblings, 1 reply; 11+ messages in thread
From: Alireza Asgari via B4 Relay @ 2026-11-15 22:34 UTC (permalink / raw)
  To: netfilter-devel, linux-mm
  Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	Muchun Song, Andrew Morton, David Hildenbrand, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, Shuah Khan, Tejun Heo,
	Vladimir Davydov, Chris Down, Zhang Jiao, Song Hu, cgroups,
	netdev, linux-kselftest, Alireza Asgari

From: Alireza Asgari <alireza@asgari.net>

The packet-count limit does not bound how much skb memory a queue can
retain: packets with different truesize values consume the same one
entry. Add a local byte allowance alongside the existing packet limit.

Derive the allowance from queue_maxlen times an aligned skb shell plus
a nominal 2048-byte complete head. This is a proposed policy, not a
promise that every packet fitting a nominal payload size is admitted.
Page-backed receive heads can exceed the allowance of a small queue.

Record admission-time skb truesize only after insertion succeeds. Keep
that saved cost while verdict handlers detach entries, and subtract it
before reinjection, including early consume paths. Undo it if the
userspace notification fails. Later payload replacement does not change
the saved cost. Segmented packets account each inserted segment.

Use subtraction-based admission checks that remain safe after a live
limit reduction. Retain existing entries when shrinking and refuse new
ones until both limits permit admission. Byte refusal follows the
existing drop or FAIL_OPEN policy.

This implements the local accounting part of Florian Westphal's
suggestion; shared owner accounting follows in the next patch.

Link: https://lists.openwall.net/netdev/2026/11/06/366
Signed-off-by: Alireza Asgari <alireza@asgari.net>
---
 include/net/netfilter/nf_queue.h |  1 +
 net/netfilter/nfnetlink_queue.c  | 59 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/include/net/netfilter/nf_queue.h b/include/net/netfilter/nf_queue.h
index 4a8cefd8398d..5644a1ad32d5 100644
--- a/include/net/netfilter/nf_queue.h
+++ b/include/net/netfilter/nf_queue.h
@@ -16,6 +16,7 @@ struct nf_queue_entry {
 	struct sk_buff		*skb;
 	struct net_device	*skb_dev;
 	unsigned int		id;
+	unsigned int truesize; /* saved NFQUEUE charge, zero if uncharged */
 	unsigned int		hook_index;	/* index in hook_entries->hook[] */
 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
 	struct net_device	*bridge_dev;
diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_queue.c
index 862d2a762106..d1e6e02e144b 100644
--- a/net/netfilter/nfnetlink_queue.c
+++ b/net/netfilter/nfnetlink_queue.c
@@ -92,6 +92,8 @@ struct nfqnl_instance {
 	unsigned int	queue_total;
 	unsigned int	id_sequence;		/* 'sequence' of pkt ids */
 	struct list_head queue_list;		/* packets in queue */
+	u64 mem_limit;
+	u64 mem_used;
 };
 
 typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
@@ -138,6 +140,28 @@ instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)
 	return NULL;
 }
 
+static u64 nfqnl_mem_limit(unsigned int maxlen)
+{
+	/* Match the skb shell alignment included in skb->truesize. */
+	return (u64)maxlen * (SKB_DATA_ALIGN(sizeof(struct sk_buff)) + 2048);
+}
+
+static bool nfqnl_mem_full(const struct nfqnl_instance *queue,
+			   unsigned int truesize)
+{
+	return queue->mem_used > queue->mem_limit ||
+	       truesize > queue->mem_limit - queue->mem_used;
+}
+
+/* All release paths use the saved admission cost, including notification undo. */
+static void nfqnl_mem_uncharge(struct nfqnl_instance *queue,
+			       struct nf_queue_entry *entry)
+{
+	lockdep_assert_held(&queue->lock);
+	queue->mem_used -= entry->truesize;
+	entry->truesize = 0;
+}
+
 static struct nfqnl_instance *
 instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid)
 {
@@ -152,6 +176,7 @@ instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid)
 	inst->queue_num = queue_num;
 	inst->peer_portid = portid;
 	inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
+	inst->mem_limit = nfqnl_mem_limit(NFQNL_QMAX_DEFAULT);
 	inst->copy_range = NFQNL_MAX_COPY_RANGE;
 	inst->copy_mode = NFQNL_COPY_NONE;
 	spin_lock_init(&inst->lock);
@@ -199,6 +224,7 @@ static void instance_destroy_work(struct work_struct *work)
 	rcu_read_lock();
 	nfqnl_flush(inst, NULL, 0);
 	rcu_read_unlock();
+	WARN_ON_ONCE(inst->mem_used);
 
 	rhashtable_destroy(&inst->nfqnl_packet_map);
 
@@ -235,6 +261,8 @@ __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
 
 	list_add_tail(&entry->list, &queue->queue_list);
 	queue->queue_total++;
+	entry->truesize = entry->skb->truesize;
+	queue->mem_used += entry->truesize;
 
 	return 0;
 }
@@ -493,7 +521,7 @@ static bool nf_bridge_ports_valid(const struct nf_queue_entry *entry)
 	return true;
 }
 
-static void nfqnl_reinject(struct nf_queue_entry *entry, unsigned int verdict)
+static void __nfqnl_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 {
 	const struct nf_ct_hook *ct_hook;
 
@@ -544,6 +572,15 @@ static void nfqnl_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 	nf_reinject(entry, verdict);
 }
 
+static void nfqnl_reinject(struct nfqnl_instance *queue,
+			   struct nf_queue_entry *entry, unsigned int verdict)
+{
+	spin_lock_bh(&queue->lock);
+	nfqnl_mem_uncharge(queue, entry);
+	spin_unlock_bh(&queue->lock);
+	__nfqnl_reinject(entry, verdict);
+}
+
 static void
 nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
 {
@@ -553,7 +590,8 @@ nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
 	list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
 		if (!cmpfn || cmpfn(entry, data)) {
 			__dequeue_entry(queue, entry);
-			nfqnl_reinject(entry, NF_DROP);
+			nfqnl_mem_uncharge(queue, entry);
+			__nfqnl_reinject(entry, NF_DROP);
 		}
 	}
 	spin_unlock_bh(&queue->lock);
@@ -1004,6 +1042,7 @@ __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
 	__be32 *packet_id_ptr;
 	int failopen = 0;
 
+	entry->truesize = 0;
 	nskb = nfqnl_build_packet_message(net, queue, entry, &packet_id_ptr);
 	if (nskb == NULL) {
 		err = -ENOMEM;
@@ -1011,7 +1050,8 @@ __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
 	}
 	spin_lock_bh(&queue->lock);
 
-	if (queue->queue_total >= queue->queue_maxlen)
+	if (queue->queue_total >= queue->queue_maxlen ||
+	    nfqnl_mem_full(queue, entry->skb->truesize))
 		goto err_out_queue_drop;
 
 	entry->id = ++queue->id_sequence;
@@ -1027,6 +1067,7 @@ __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
 	if (err < 0) {
 		/* Unicast failed - remove entry we just inserted */
 		__dequeue_entry(queue, entry);
+		nfqnl_mem_uncharge(queue, entry);
 
 		if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
 			failopen = 1;
@@ -1050,6 +1091,9 @@ __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
 		if (queue->queue_total >= queue->queue_maxlen)
 			net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
 					     queue->queue_total);
+		else if (nfqnl_mem_full(queue, entry->skb->truesize))
+			net_warn_ratelimited(
+				"nf_queue: byte limit reached, dropping packet\n");
 		else
 			net_warn_ratelimited("nf_queue: hash insert failed: %d\n", err);
 	}
@@ -1057,7 +1101,7 @@ __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
 err_out_unlock:
 	spin_unlock_bh(&queue->lock);
 	if (failopen)
-		nfqnl_reinject(entry, NF_ACCEPT);
+		nfqnl_reinject(queue, entry, NF_ACCEPT);
 err_out:
 	return err;
 }
@@ -1773,7 +1817,7 @@ static int nfqnl_recv_verdict_batch(struct sk_buff *skb,
 		if (nfqa[NFQA_PRIORITY])
 			entry->skb->priority = ntohl(nla_get_be32(nfqa[NFQA_PRIORITY]));
 
-		nfqnl_reinject(entry, verdict);
+		nfqnl_reinject(queue, entry, verdict);
 	}
 	return 0;
 }
@@ -1885,7 +1929,7 @@ static int nfqnl_recv_verdict(struct sk_buff *skb, const struct nfnl_info *info,
 	if (entry->state.pf == PF_BRIDGE) {
 		err = nfqa_parse_bridge(entry, nfqa);
 		if (err < 0) {
-			nfqnl_reinject(entry, NF_DROP);
+			nfqnl_reinject(queue, entry, NF_DROP);
 			return err;
 		}
 	}
@@ -1907,7 +1951,7 @@ static int nfqnl_recv_verdict(struct sk_buff *skb, const struct nfnl_info *info,
 	if (nfqa[NFQA_PRIORITY])
 		entry->skb->priority = ntohl(nla_get_be32(nfqa[NFQA_PRIORITY]));
 
-	nfqnl_reinject(entry, verdict);
+	nfqnl_reinject(queue, entry, verdict);
 	return 0;
 }
 
@@ -2037,6 +2081,7 @@ static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,
 
 		spin_lock_bh(&queue->lock);
 		queue->queue_maxlen = ntohl(*queue_maxlen);
+		queue->mem_limit = nfqnl_mem_limit(queue->queue_maxlen);
 		spin_unlock_bh(&queue->lock);
 	}
 

-- 
2.34.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH RFC nf-next 7/9] netfilter: nfqueue: reserve capacity against the bound owner
  2026-11-15 22:34 [PATCH RFC nf-next 0/9] netfilter: nfqueue: account queued memory and reserve capacity Alireza Asgari via B4 Relay
                   ` (5 preceding siblings ...)
  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-15 22:34 ` 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
  8 siblings, 0 replies; 11+ messages in thread
From: Alireza Asgari via B4 Relay @ 2026-11-15 22:34 UTC (permalink / raw)
  To: netfilter-devel, linux-mm
  Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	Muchun Song, Andrew Morton, David Hildenbrand, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, Shuah Khan, Tejun Heo,
	Vladimir Davydov, Chris Down, Zhang Jiao, Song Hu, cgroups,
	netdev, linux-kselftest, Alireza Asgari

From: Alireza Asgari <alireza@asgari.net>

A local queue allowance does not by itself bound aggregate reservations
across queues. Outside the initial network namespace, capture the binding
task's objcg and charge activated capacity against that owner and its
ancestors. Effective aggregate protection requires an applicable finite
memory limit; root or otherwise unaccounted owners remain uncharged.

Reserve capacity on first activation, after any inactive sizing. Publish
copy mode, size, and flags together only after required growth succeeds.
Keep activation sticky so switching to COPY_NONE cannot release backing
for retained entries or an admission already in progress. Configure a new
instance privately before publishing it to packet readers.

Retain page-rounded backing for the greater of configured capacity and
saved bytes still in use. Serialize configuration and refunds with a
mutex. Packet releases schedule coalesced refund work; the worker rechecks
the target before refunding. Teardown waits for the RCU readers that can
hold detached entries, stops refund scheduling, cancels outstanding work,
flushes retained entries, and destroys the reservation.

The charges are ordinary memcg usage, additional to actual allocations.
Empty activated capacity therefore consumes budget and can affect
memory.high and protection accounting. The budget policy, nominal byte
allowance, and reservation API are proposed for maintainer discussion.

Link: https://lists.openwall.net/netdev/2026/11/06/366
Signed-off-by: Alireza Asgari <alireza@asgari.net>
---
 net/netfilter/nfnetlink_queue.c | 213 +++++++++++++++++++++++++++-------------
 1 file changed, 144 insertions(+), 69 deletions(-)

diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_queue.c
index d1e6e02e144b..b37fb6f21482 100644
--- a/net/netfilter/nfnetlink_queue.c
+++ b/net/netfilter/nfnetlink_queue.c
@@ -32,6 +32,8 @@
 #include <linux/icmpv6.h>
 #include <linux/ip.h>
 #include <linux/list.h>
+#include <linux/memcontrol.h>
+#include <linux/mutex.h>
 #include <linux/sctp.h>
 #include <linux/cgroup-defs.h>
 #include <linux/rhashtable.h>
@@ -73,6 +75,10 @@ struct nfqnl_instance {
 	struct hlist_node hlist;		/* global list of queues */
 	struct rhashtable nfqnl_packet_map;
 	struct rcu_work	rwork;
+	struct work_struct mem_work;
+	struct mutex mem_mutex; /* serializes capacity changes and refunds */
+	struct mem_cgroup_reservation memcg;
+	bool mem_activated; /* maintain backing after the first activation */
 
 	u32 peer_portid;
 	unsigned int queue_maxlen;
@@ -94,6 +100,8 @@ struct nfqnl_instance {
 	struct list_head queue_list;		/* packets in queue */
 	u64 mem_limit;
 	u64 mem_used;
+	u64 mem_reserved; /* pages, published under lock */
+	bool mem_stopping;
 };
 
 typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
@@ -146,6 +154,11 @@ static u64 nfqnl_mem_limit(unsigned int maxlen)
 	return (u64)maxlen * (SKB_DATA_ALIGN(sizeof(struct sk_buff)) + 2048);
 }
 
+static u64 nfqnl_mem_pages(u64 bytes)
+{
+	return (bytes >> PAGE_SHIFT) + !!(bytes & (PAGE_SIZE - 1));
+}
+
 static bool nfqnl_mem_full(const struct nfqnl_instance *queue,
 			   unsigned int truesize)
 {
@@ -153,6 +166,83 @@ static bool nfqnl_mem_full(const struct nfqnl_instance *queue,
 	       truesize > queue->mem_limit - queue->mem_used;
 }
 
+/* The mutex excludes limit changes; packet admission cannot raise this target. */
+static void nfqnl_mem_refund(struct nfqnl_instance *queue)
+{
+	u64 pages;
+
+	lockdep_assert_held(&queue->mem_mutex);
+	spin_lock_bh(&queue->lock);
+	pages = nfqnl_mem_pages(max(queue->mem_limit, queue->mem_used));
+	spin_unlock_bh(&queue->lock);
+
+	if (pages >= queue->memcg.pages)
+		return;
+
+	/* Shrinking cannot fail. Keep the packet-path copy conservative meanwhile. */
+	mem_cgroup_reservation_resize(&queue->memcg, pages);
+	spin_lock_bh(&queue->lock);
+	queue->mem_reserved = pages;
+	/* A final drain may have happened while the refund was in progress. */
+	if (!queue->mem_stopping &&
+	    pages > nfqnl_mem_pages(max(queue->mem_limit, queue->mem_used)))
+		queue_work(nfq_cleanup_wq, &queue->mem_work);
+	spin_unlock_bh(&queue->lock);
+}
+
+static void nfqnl_mem_work(struct work_struct *work)
+{
+	struct nfqnl_instance *queue =
+		container_of(work, struct nfqnl_instance, mem_work);
+
+	mutex_lock(&queue->mem_mutex);
+	nfqnl_mem_refund(queue);
+	mutex_unlock(&queue->mem_mutex);
+}
+
+static int nfqnl_set_config(struct nfqnl_instance *queue, unsigned int maxlen,
+			    const struct nfqnl_msg_config_params *params,
+			    u32 flags, u32 mask)
+{
+	u64 limit = nfqnl_mem_limit(maxlen);
+	u64 pages = nfqnl_mem_pages(limit);
+	unsigned int range = 0;
+	bool activated;
+	int err = 0;
+
+	if (params && params->copy_mode == NFQNL_COPY_PACKET) {
+		range = ntohl(params->copy_range);
+		if (!range || range > NFQNL_MAX_COPY_RANGE)
+			range = NFQNL_MAX_COPY_RANGE;
+	}
+
+	mutex_lock(&queue->mem_mutex);
+	activated = queue->mem_activated ||
+		    (params && params->copy_mode != NFQNL_COPY_NONE);
+	if (activated && pages > queue->memcg.pages && queue->memcg.objcg) {
+		err = mem_cgroup_reservation_resize(&queue->memcg, pages);
+		if (err)
+			goto out;
+	}
+	/* Publish activation only after the complete configuration is backed. */
+	spin_lock_bh(&queue->lock);
+	queue->queue_maxlen = maxlen;
+	queue->mem_limit = limit;
+	queue->mem_reserved = queue->memcg.objcg ? queue->memcg.pages : 0;
+	queue->mem_activated = activated;
+	if (params) {
+		queue->copy_range = range;
+		queue->copy_mode = params->copy_mode;
+	}
+	queue->flags = (queue->flags & ~mask) | (flags & mask);
+	spin_unlock_bh(&queue->lock);
+	if (activated && queue->memcg.objcg)
+		nfqnl_mem_refund(queue);
+out:
+	mutex_unlock(&queue->mem_mutex);
+	return err;
+}
+
 /* All release paths use the saved admission cost, including notification undo. */
 static void nfqnl_mem_uncharge(struct nfqnl_instance *queue,
 			       struct nf_queue_entry *entry)
@@ -160,10 +250,17 @@ static void nfqnl_mem_uncharge(struct nfqnl_instance *queue,
 	lockdep_assert_held(&queue->lock);
 	queue->mem_used -= entry->truesize;
 	entry->truesize = 0;
+	if (!queue->mem_stopping &&
+	    queue->mem_reserved >
+		    nfqnl_mem_pages(max(queue->mem_limit, queue->mem_used)))
+		queue_work(nfq_cleanup_wq, &queue->mem_work);
 }
 
 static struct nfqnl_instance *
-instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid)
+instance_create(struct net *net, struct nfnl_queue_net *q, u16 queue_num,
+		u32 portid, unsigned int maxlen,
+		const struct nfqnl_msg_config_params *params, u32 flags,
+		u32 mask)
 {
 	struct nfqnl_instance *inst;
 	unsigned int h;
@@ -175,16 +272,27 @@ instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid)
 
 	inst->queue_num = queue_num;
 	inst->peer_portid = portid;
-	inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
-	inst->mem_limit = nfqnl_mem_limit(NFQNL_QMAX_DEFAULT);
+	inst->queue_maxlen = maxlen;
+	inst->mem_limit = nfqnl_mem_limit(maxlen);
 	inst->copy_range = NFQNL_MAX_COPY_RANGE;
 	inst->copy_mode = NFQNL_COPY_NONE;
 	spin_lock_init(&inst->lock);
+	mutex_init(&inst->mem_mutex);
+	INIT_WORK(&inst->mem_work, nfqnl_mem_work);
 	INIT_LIST_HEAD(&inst->queue_list);
 
-	err = rhashtable_init(&inst->nfqnl_packet_map, &nfqnl_rhashtable_params);
+	if (!net_eq(net, &init_net))
+		mem_cgroup_reservation_init(&inst->memcg);
+
+	err = rhashtable_init(&inst->nfqnl_packet_map,
+			      &nfqnl_rhashtable_params);
 	if (err < 0)
-		goto out_free;
+		goto out_reservation;
+
+	/* Keep packet readers from seeing a partially configured new queue. */
+	err = nfqnl_set_config(inst, maxlen, params, flags, mask);
+	if (err)
+		goto out_map;
 
 	spin_lock(&q->instances_lock);
 	if (instance_lookup(q, queue_num)) {
@@ -206,8 +314,11 @@ instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid)
 
 out_unlock:
 	spin_unlock(&q->instances_lock);
+out_map:
+	/* Unpublished instances have no entries or pending refund work. */
 	rhashtable_destroy(&inst->nfqnl_packet_map);
-out_free:
+out_reservation:
+	mem_cgroup_reservation_destroy(&inst->memcg);
 	kfree(inst);
 	return ERR_PTR(err);
 }
@@ -219,12 +330,18 @@ static void instance_destroy_work(struct work_struct *work)
 {
 	struct nfqnl_instance *inst;
 
-	inst = container_of(to_rcu_work(work), struct nfqnl_instance,
-			    rwork);
+	inst = container_of(to_rcu_work(work), struct nfqnl_instance, rwork);
+	/* The grace period has drained verdict readers, including detached batches. */
+	spin_lock_bh(&inst->lock);
+	inst->mem_stopping = true;
+	spin_unlock_bh(&inst->lock);
+	cancel_work_sync(&inst->mem_work);
+
 	rcu_read_lock();
 	nfqnl_flush(inst, NULL, 0);
 	rcu_read_unlock();
 	WARN_ON_ONCE(inst->mem_used);
+	mem_cgroup_reservation_destroy(&inst->memcg);
 
 	rhashtable_destroy(&inst->nfqnl_packet_map);
 
@@ -1526,37 +1643,6 @@ nfqnl_mangle(void *data, unsigned int data_len, struct nf_queue_entry *e, int di
 	return 0;
 }
 
-static int
-nfqnl_set_mode(struct nfqnl_instance *queue,
-	       unsigned char mode, unsigned int range)
-{
-	int status = 0;
-
-	spin_lock_bh(&queue->lock);
-	switch (mode) {
-	case NFQNL_COPY_NONE:
-	case NFQNL_COPY_META:
-		queue->copy_mode = mode;
-		queue->copy_range = 0;
-		break;
-
-	case NFQNL_COPY_PACKET:
-		queue->copy_mode = mode;
-		if (range == 0 || range > NFQNL_MAX_COPY_RANGE)
-			queue->copy_range = NFQNL_MAX_COPY_RANGE;
-		else
-			queue->copy_range = range;
-		break;
-
-	default:
-		status = -EINVAL;
-
-	}
-	spin_unlock_bh(&queue->lock);
-
-	return status;
-}
-
 static int
 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
 {
@@ -1975,12 +2061,14 @@ static const struct nf_queue_handler nfqh = {
 };
 
 static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,
-			     const struct nlattr * const nfqa[])
+			     const struct nlattr *const nfqa[])
 {
 	struct nfnl_queue_net *q = nfnl_queue_pernet(info->net);
 	u_int16_t queue_num = ntohs(info->nfmsg->res_id);
 	struct nfqnl_msg_config_cmd *cmd = NULL;
 	struct nfqnl_instance *queue;
+	const struct nfqnl_msg_config_params *params = NULL;
+	unsigned int maxlen = NFQNL_QMAX_DEFAULT;
 	__u32 flags = 0, mask = 0;
 
 	WARN_ON_ONCE(!lockdep_nfnl_is_held(NFNL_SUBSYS_QUEUE));
@@ -2029,6 +2117,15 @@ static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,
 		}
 	}
 
+	/* Validate before reserving capacity or publishing a newly bound queue. */
+	if (nfqa[NFQA_CFG_PARAMS]) {
+		params = nla_data(nfqa[NFQA_CFG_PARAMS]);
+		if (params->copy_mode > NFQNL_COPY_PACKET)
+			return -EINVAL;
+	}
+	if (nfqa[NFQA_CFG_QUEUE_MAXLEN])
+		maxlen = ntohl(nla_get_be32(nfqa[NFQA_CFG_QUEUE_MAXLEN]));
+
 	/* Lookup queue under RCU. After peer_portid check (or for new queue
 	 * in BIND case), the queue is owned by the socket sending this message.
 	 * nfqnl_rcv_nl_event() will block on the nfnl subsys mutex that is
@@ -2048,10 +2145,10 @@ static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,
 		case NFQNL_CFG_CMD_BIND:
 			if (queue)
 				return -EBUSY;
-			queue = instance_create(q, queue_num, NETLINK_CB(skb).portid);
-			if (IS_ERR(queue))
-				return PTR_ERR(queue);
-			break;
+			queue = instance_create(info->net, q, queue_num,
+						NETLINK_CB(skb).portid, maxlen,
+						params, flags, mask);
+			return PTR_ERR_OR_ZERO(queue);
 		case NFQNL_CFG_CMD_UNBIND:
 			if (!queue)
 				return -ENODEV;
@@ -2068,31 +2165,9 @@ static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,
 	if (!queue)
 		return -ENODEV;
 
-	if (nfqa[NFQA_CFG_PARAMS]) {
-		struct nfqnl_msg_config_params *params =
-			nla_data(nfqa[NFQA_CFG_PARAMS]);
-
-		nfqnl_set_mode(queue, params->copy_mode,
-				ntohl(params->copy_range));
-	}
-
-	if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
-		__be32 *queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
-
-		spin_lock_bh(&queue->lock);
-		queue->queue_maxlen = ntohl(*queue_maxlen);
-		queue->mem_limit = nfqnl_mem_limit(queue->queue_maxlen);
-		spin_unlock_bh(&queue->lock);
-	}
-
-	if (nfqa[NFQA_CFG_FLAGS]) {
-		spin_lock_bh(&queue->lock);
-		queue->flags &= ~mask;
-		queue->flags |= flags & mask;
-		spin_unlock_bh(&queue->lock);
-	}
-
-	return 0;
+	if (!nfqa[NFQA_CFG_QUEUE_MAXLEN])
+		maxlen = queue->queue_maxlen;
+	return nfqnl_set_config(queue, maxlen, params, flags, mask);
 }
 
 static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {

-- 
2.34.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH RFC nf-next 8/9] netfilter: nfqueue: add KUnit memory accounting coverage
  2026-11-15 22:34 [PATCH RFC nf-next 0/9] netfilter: nfqueue: account queued memory and reserve capacity Alireza Asgari via B4 Relay
                   ` (6 preceding siblings ...)
  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 ` 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
  8 siblings, 0 replies; 11+ messages in thread
From: Alireza Asgari via B4 Relay @ 2026-11-15 22:34 UTC (permalink / raw)
  To: netfilter-devel, linux-mm
  Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	Muchun Song, Andrew Morton, David Hildenbrand, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, Shuah Khan, Tejun Heo,
	Vladimir Davydov, Chris Down, Zhang Jiao, Song Hu, cgroups,
	netdev, linux-kselftest, Alireza Asgari

From: Alireza Asgari <alireza@asgari.net>

Test local byte boundaries, a small nominal-head skb, a page-backed head,
saved accounting across detachment, and live limit changes.

Also test active, inactive, and failed instance creation against a private
queue table. A KUnit static stub checks that a new instance remains absent
from lookup until configuration succeeds. These tests exercise publication
ordering directly; they do not claim a concurrent queue-bypass reproducer.

Signed-off-by: Alireza Asgari <alireza@asgari.net>
---
 net/netfilter/Kconfig                |  10 ++
 net/netfilter/nfnetlink_queue.c      |  13 ++
 net/netfilter/nfnetlink_queue_test.c | 338 +++++++++++++++++++++++++++++++++++
 3 files changed, 361 insertions(+)

diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig
index 09874c26fd13..02818552ac8b 100644
--- a/net/netfilter/Kconfig
+++ b/net/netfilter/Kconfig
@@ -71,6 +71,16 @@ config NETFILTER_NETLINK_LOG
 	  and is also scheduled to replace the old syslog-based ipt_LOG
 	  and ip6t_LOG modules.
 
+config NETFILTER_NETLINK_QUEUE_KUNIT_TEST
+	bool "NFQUEUE memory accounting tests" if !KUNIT_ALL_TESTS
+	depends on NETFILTER_NETLINK_QUEUE=y && KUNIT=y
+	default KUNIT_ALL_TESTS
+	help
+	  Test NFQUEUE byte admission, saved charges across detachment, and
+	  live queue limit updates without sending network traffic.
+	  The tests run with KUnit and do not need a userspace queue listener.
+	  Say N unless you are testing changes to NFQUEUE accounting.
+
 config NETFILTER_NETLINK_OSF
 	tristate "Netfilter OSF over NFNETLINK interface"
 	depends on NETFILTER_ADVANCED
diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_queue.c
index b37fb6f21482..ad14c74f35a8 100644
--- a/net/netfilter/nfnetlink_queue.c
+++ b/net/netfilter/nfnetlink_queue.c
@@ -51,6 +51,10 @@
 
 #include <linux/atomic.h>
 
+#if IS_ENABLED(CONFIG_NETFILTER_NETLINK_QUEUE_KUNIT_TEST)
+#include <kunit/static_stub.h>
+#endif
+
 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
 #include "../bridge/br_private.h"
 #endif
@@ -210,6 +214,11 @@ static int nfqnl_set_config(struct nfqnl_instance *queue, unsigned int maxlen,
 	bool activated;
 	int err = 0;
 
+#if IS_ENABLED(CONFIG_NETFILTER_NETLINK_QUEUE_KUNIT_TEST)
+	KUNIT_STATIC_STUB_REDIRECT(nfqnl_set_config, queue, maxlen, params,
+				   flags, mask);
+#endif
+
 	if (params && params->copy_mode == NFQNL_COPY_PACKET) {
 		range = ntohl(params->copy_range);
 		if (!range || range > NFQNL_MAX_COPY_RANGE)
@@ -2390,3 +2399,7 @@ MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
 
 module_init(nfnetlink_queue_init);
 module_exit(nfnetlink_queue_fini);
+
+#if IS_ENABLED(CONFIG_NETFILTER_NETLINK_QUEUE_KUNIT_TEST)
+#include "nfnetlink_queue_test.c"
+#endif
diff --git a/net/netfilter/nfnetlink_queue_test.c b/net/netfilter/nfnetlink_queue_test.c
new file mode 100644
index 000000000000..e8b89ca83dff
--- /dev/null
+++ b/net/netfilter/nfnetlink_queue_test.c
@@ -0,0 +1,338 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <kunit/test.h>
+#include <linux/mm.h>
+
+static void nfqnl_test_destroy_map(void *data)
+{
+	struct nfqnl_instance *queue = data;
+
+	rhashtable_destroy(&queue->nfqnl_packet_map);
+}
+
+static void nfqnl_test_free_skb(void *data)
+{
+	kfree_skb(data);
+}
+
+static int nfqnl_test_init(struct kunit *test)
+{
+	struct nfqnl_instance *queue;
+	int err;
+
+	queue = kunit_kzalloc(test, sizeof(*queue), GFP_KERNEL);
+	if (!queue)
+		return -ENOMEM;
+	spin_lock_init(&queue->lock);
+	mutex_init(&queue->mem_mutex);
+	INIT_WORK(&queue->mem_work, nfqnl_mem_work);
+	INIT_LIST_HEAD(&queue->queue_list);
+	err = rhashtable_init(&queue->nfqnl_packet_map,
+			      &nfqnl_rhashtable_params);
+	if (err)
+		return err;
+	test->priv = queue;
+	return kunit_add_action_or_reset(test, nfqnl_test_destroy_map, queue);
+}
+
+static void nfqnl_test_boundaries(struct kunit *test)
+{
+	struct nfqnl_instance *queue = test->priv;
+
+	queue->mem_limit = 8192;
+	queue->mem_used = 4096;
+	KUNIT_EXPECT_FALSE(test, nfqnl_mem_full(queue, 4096));
+	KUNIT_EXPECT_TRUE(test, nfqnl_mem_full(queue, 4097));
+	queue->mem_used = 8193;
+	KUNIT_EXPECT_TRUE(test, nfqnl_mem_full(queue, 1));
+	queue->mem_used = 0;
+	queue->mem_limit = 0;
+	KUNIT_EXPECT_TRUE(test, nfqnl_mem_full(queue, 1));
+	queue->mem_limit = U64_MAX;
+	queue->mem_used = U64_MAX - 10;
+	KUNIT_EXPECT_FALSE(test, nfqnl_mem_full(queue, 10));
+	KUNIT_EXPECT_TRUE(test, nfqnl_mem_full(queue, 11));
+
+	KUNIT_EXPECT_GT(test, nfqnl_mem_limit(U32_MAX), (u64)U32_MAX);
+	KUNIT_EXPECT_EQ(test, nfqnl_mem_pages(0), 0ULL);
+	KUNIT_EXPECT_EQ(test, nfqnl_mem_pages(PAGE_SIZE), 1ULL);
+	KUNIT_EXPECT_EQ(test, nfqnl_mem_pages(PAGE_SIZE + 1), 2ULL);
+	KUNIT_EXPECT_EQ(test, nfqnl_mem_pages(U64_MAX),
+			(U64_MAX >> PAGE_SHIFT) + 1);
+}
+
+static void nfqnl_test_small_queue(struct kunit *test)
+{
+	struct nfqnl_instance *queue = test->priv;
+	struct sk_buff *skb;
+
+	skb = alloc_skb(SKB_WITH_OVERHEAD(2048), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, skb);
+	KUNIT_ASSERT_EQ(
+		test, kunit_add_action_or_reset(test, nfqnl_test_free_skb, skb),
+		0);
+	KUNIT_ASSERT_EQ(test, nfqnl_set_config(queue, 1, NULL, 0, 0), 0);
+	/* A standard 2 KiB head must fit in an empty length-one queue. */
+	KUNIT_EXPECT_FALSE(test, nfqnl_mem_full(queue, skb->truesize));
+}
+
+static void nfqnl_test_page_head(struct kunit *test)
+{
+	struct nfqnl_instance *queue = test->priv;
+	struct sk_buff *skb;
+	struct page *page;
+
+	page = alloc_page(GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, page);
+	skb = build_skb(page_address(page), PAGE_SIZE);
+	if (!skb)
+		__free_page(page);
+	KUNIT_ASSERT_NOT_NULL(test, skb);
+	KUNIT_ASSERT_EQ(
+		test, kunit_add_action_or_reset(test, nfqnl_test_free_skb, skb),
+		0);
+	skb_put_zero(skb, 64);
+
+	/* A small payload still accounts for the complete receive allocation. */
+	KUNIT_ASSERT_EQ(test, nfqnl_set_config(queue, 1, NULL, 0, 0), 0);
+	if (PAGE_SIZE > 2048)
+		KUNIT_EXPECT_TRUE(test, nfqnl_mem_full(queue, skb->truesize));
+	else
+		KUNIT_EXPECT_FALSE(test, nfqnl_mem_full(queue, skb->truesize));
+	KUNIT_ASSERT_EQ(test,
+			nfqnl_set_config(queue, DIV_ROUND_UP(PAGE_SIZE, 2048),
+					 NULL, 0, 0),
+			0);
+	KUNIT_EXPECT_FALSE(test, nfqnl_mem_full(queue, skb->truesize));
+
+	queue->mem_limit = skb->truesize - 1;
+	KUNIT_EXPECT_TRUE(test, nfqnl_mem_full(queue, skb->truesize));
+	queue->mem_limit = skb->truesize;
+	KUNIT_EXPECT_FALSE(test, nfqnl_mem_full(queue, skb->truesize));
+}
+
+static void nfqnl_test_detached_charge(struct kunit *test)
+{
+	struct nfqnl_instance *queue = test->priv;
+	struct nf_queue_entry entry = {};
+	u64 detached, released;
+	unsigned int saved;
+	int err;
+
+	entry.skb = alloc_skb(512, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, entry.skb);
+	KUNIT_ASSERT_EQ(test,
+			kunit_add_action_or_reset(test, nfqnl_test_free_skb,
+						  entry.skb),
+			0);
+	saved = entry.skb->truesize;
+	queue->mem_limit = saved;
+
+	spin_lock_bh(&queue->lock);
+	err = __enqueue_entry(queue, &entry);
+	if (!err) {
+		__dequeue_entry(queue, &entry);
+		detached = queue->mem_used;
+		/* Verdict payload replacement must not change the saved refund. */
+		entry.skb->truesize += 128;
+		nfqnl_mem_uncharge(queue, &entry);
+		released = queue->mem_used;
+		entry.skb->truesize = saved;
+	}
+	spin_unlock_bh(&queue->lock);
+	KUNIT_ASSERT_EQ(test, err, 0);
+	KUNIT_EXPECT_EQ(test, detached, (u64)saved);
+	KUNIT_EXPECT_EQ(test, released, 0ULL);
+	KUNIT_EXPECT_EQ(test, entry.truesize, 0U);
+	KUNIT_EXPECT_EQ(test, queue->queue_total, 0U);
+	KUNIT_EXPECT_TRUE(test, list_empty(&queue->queue_list));
+	KUNIT_EXPECT_FALSE(test, nfqnl_mem_full(queue, saved));
+
+	/* FAIL_OPEN and notification rollback can reach reinjection uncharged. */
+	spin_lock_bh(&queue->lock);
+	nfqnl_mem_uncharge(queue, &entry);
+	spin_unlock_bh(&queue->lock);
+	KUNIT_EXPECT_EQ(test, queue->mem_used, 0ULL);
+}
+
+static void nfqnl_test_limit_update(struct kunit *test)
+{
+	struct nfqnl_instance *queue = test->priv;
+	u64 used = nfqnl_mem_limit(8);
+
+	queue->mem_used = used;
+	queue->queue_total = 2;
+	KUNIT_ASSERT_EQ(test, nfqnl_set_config(queue, 1, NULL, 0, 0), 0);
+	KUNIT_EXPECT_EQ(test, queue->queue_maxlen, 1U);
+	KUNIT_EXPECT_EQ(test, queue->mem_limit, nfqnl_mem_limit(1));
+	KUNIT_EXPECT_EQ(test, queue->mem_used, used);
+	KUNIT_EXPECT_EQ(test, queue->queue_total, 2U);
+	KUNIT_EXPECT_TRUE(test, nfqnl_mem_full(queue, 1));
+	KUNIT_ASSERT_EQ(test, nfqnl_set_config(queue, 16, NULL, 0, 0), 0);
+	KUNIT_EXPECT_FALSE(test, nfqnl_mem_full(queue, 1));
+	KUNIT_EXPECT_EQ(test, queue->mem_used, used);
+	KUNIT_EXPECT_EQ(test, queue->mem_reserved, 0ULL);
+}
+
+struct nfqnl_test_creation {
+	struct nfnl_queue_net queues;
+	struct nfqnl_instance *instance;
+	unsigned int config_calls;
+	int config_error;
+};
+
+static struct nfqnl_instance *nfqnl_test_lookup(struct nfqnl_test_creation *ctx,
+						u16 queue_num)
+{
+	struct nfqnl_instance *queue;
+
+	rcu_read_lock();
+	queue = instance_lookup(&ctx->queues, queue_num);
+	rcu_read_unlock();
+	return queue;
+}
+
+static int
+nfqnl_test_config_private(struct nfqnl_instance *queue, unsigned int maxlen,
+			  const struct nfqnl_msg_config_params *params,
+			  u32 flags, u32 mask)
+{
+	struct kunit *test = kunit_get_current_test();
+	struct nfqnl_test_creation *ctx = test->priv;
+	int err;
+
+	ctx->config_calls++;
+	KUNIT_EXPECT_PTR_EQ(test, nfqnl_test_lookup(ctx, queue->queue_num),
+			    NULL);
+	if (ctx->config_error)
+		return ctx->config_error;
+
+	/* Exercise the real configuration while checking its visibility. */
+	kunit_deactivate_static_stub(test, nfqnl_set_config);
+	err = nfqnl_set_config(queue, maxlen, params, flags, mask);
+	KUNIT_EXPECT_PTR_EQ(test, nfqnl_test_lookup(ctx, queue->queue_num),
+			    NULL);
+	return err;
+}
+
+static void nfqnl_test_destroy_instance(void *data)
+{
+	struct nfqnl_test_creation *ctx = data;
+
+	if (!IS_ERR_OR_NULL(ctx->instance)) {
+		instance_destroy(&ctx->queues, ctx->instance);
+		rcu_barrier();
+		flush_workqueue(nfq_cleanup_wq);
+	}
+}
+
+static struct nfqnl_test_creation *nfqnl_test_creation_init(struct kunit *test)
+{
+	struct nfqnl_test_creation *ctx;
+	unsigned int i;
+
+	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+	if (!ctx)
+		return NULL;
+	spin_lock_init(&ctx->queues.instances_lock);
+	for (i = 0; i < INSTANCE_BUCKETS; i++)
+		INIT_HLIST_HEAD(&ctx->queues.instance_table[i]);
+	if (kunit_add_action_or_reset(test, nfqnl_test_destroy_instance, ctx))
+		return NULL;
+	test->priv = ctx;
+	kunit_activate_static_stub(test, nfqnl_set_config,
+				   nfqnl_test_config_private);
+	return ctx;
+}
+
+static void nfqnl_test_create_active(struct kunit *test)
+{
+	const struct nfqnl_msg_config_params params = {
+		.copy_range = htonl(512),
+		.copy_mode = NFQNL_COPY_PACKET,
+	};
+	struct nfqnl_test_creation *ctx = nfqnl_test_creation_init(test);
+	struct nfqnl_instance *queue, *duplicate;
+
+	KUNIT_ASSERT_NOT_NULL(test, ctx);
+	queue = instance_create(&init_net, &ctx->queues, 7, 1, 32, &params,
+				NFQA_CFG_F_FAIL_OPEN, NFQA_CFG_F_FAIL_OPEN);
+	ctx->instance = queue;
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, queue);
+	KUNIT_EXPECT_EQ(test, ctx->config_calls, 1U);
+	KUNIT_EXPECT_PTR_EQ(test, nfqnl_test_lookup(ctx, 7), queue);
+	KUNIT_EXPECT_TRUE(test, queue->mem_activated);
+	KUNIT_EXPECT_EQ(test, queue->queue_maxlen, 32U);
+	KUNIT_EXPECT_EQ(test, queue->mem_limit, nfqnl_mem_limit(32));
+	KUNIT_EXPECT_EQ(test, queue->copy_mode, (u8)NFQNL_COPY_PACKET);
+	KUNIT_EXPECT_EQ(test, queue->copy_range, 512U);
+	KUNIT_EXPECT_EQ(test, queue->flags, (u32)NFQA_CFG_F_FAIL_OPEN);
+
+	duplicate =
+		instance_create(&init_net, &ctx->queues, 7, 1, 1, NULL, 0, 0);
+	KUNIT_EXPECT_TRUE(test, IS_ERR(duplicate));
+	if (IS_ERR(duplicate)) {
+		KUNIT_EXPECT_EQ(test, PTR_ERR(duplicate), (long)-EEXIST);
+	} else {
+		instance_destroy(&ctx->queues, duplicate);
+		rcu_barrier();
+		flush_workqueue(nfq_cleanup_wq);
+	}
+	KUNIT_EXPECT_PTR_EQ(test, nfqnl_test_lookup(ctx, 7), queue);
+	KUNIT_EXPECT_EQ(test, queue->queue_maxlen, 32U);
+}
+
+static void nfqnl_test_create_inactive(struct kunit *test)
+{
+	struct nfqnl_test_creation *ctx = nfqnl_test_creation_init(test);
+	struct nfqnl_instance *queue;
+
+	KUNIT_ASSERT_NOT_NULL(test, ctx);
+	queue = instance_create(&init_net, &ctx->queues, 7, 1,
+				NFQNL_QMAX_DEFAULT, NULL, 0, 0);
+	ctx->instance = queue;
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, queue);
+	KUNIT_EXPECT_EQ(test, ctx->config_calls, 1U);
+	KUNIT_EXPECT_PTR_EQ(test, nfqnl_test_lookup(ctx, 7), queue);
+	KUNIT_EXPECT_FALSE(test, queue->mem_activated);
+	KUNIT_EXPECT_EQ(test, queue->copy_mode, (u8)NFQNL_COPY_NONE);
+	KUNIT_EXPECT_EQ(test, queue->queue_maxlen, NFQNL_QMAX_DEFAULT);
+	KUNIT_EXPECT_EQ(test, queue->memcg.pages, 0ULL);
+}
+
+static void nfqnl_test_create_failure(struct kunit *test)
+{
+	const struct nfqnl_msg_config_params params = {
+		.copy_mode = NFQNL_COPY_PACKET,
+	};
+	struct nfqnl_test_creation *ctx = nfqnl_test_creation_init(test);
+
+	KUNIT_ASSERT_NOT_NULL(test, ctx);
+	ctx->config_error = -ENOMEM;
+	ctx->instance = instance_create(&init_net, &ctx->queues, 7, 1, 32,
+					&params, 0, 0);
+	KUNIT_ASSERT_TRUE(test, IS_ERR(ctx->instance));
+	KUNIT_EXPECT_EQ(test, PTR_ERR(ctx->instance), (long)-ENOMEM);
+	KUNIT_EXPECT_EQ(test, ctx->config_calls, 1U);
+	KUNIT_EXPECT_PTR_EQ(test, nfqnl_test_lookup(ctx, 7), NULL);
+}
+
+static struct kunit_case nfqnl_mem_test_cases[] = {
+	KUNIT_CASE(nfqnl_test_boundaries),
+	KUNIT_CASE(nfqnl_test_small_queue),
+	KUNIT_CASE(nfqnl_test_page_head),
+	KUNIT_CASE(nfqnl_test_detached_charge),
+	KUNIT_CASE(nfqnl_test_limit_update),
+	KUNIT_CASE(nfqnl_test_create_active),
+	KUNIT_CASE(nfqnl_test_create_inactive),
+	KUNIT_CASE(nfqnl_test_create_failure),
+	{}
+};
+
+static struct kunit_suite nfqnl_mem_test_suite = {
+	.name = "nfnetlink_queue_memory",
+	.init = nfqnl_test_init,
+	.test_cases = nfqnl_mem_test_cases,
+};
+
+kunit_test_suite(nfqnl_mem_test_suite);

-- 
2.34.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH RFC nf-next 9/9] selftests: netfilter: exercise nfqueue memory accounting
  2026-11-15 22:34 [PATCH RFC nf-next 0/9] netfilter: nfqueue: account queued memory and reserve capacity Alireza Asgari via B4 Relay
                   ` (7 preceding siblings ...)
  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 ` Alireza Asgari via B4 Relay
  8 siblings, 0 replies; 11+ messages in thread
From: Alireza Asgari via B4 Relay @ 2026-11-15 22:34 UTC (permalink / raw)
  To: netfilter-devel, linux-mm
  Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	Muchun Song, Andrew Morton, David Hildenbrand, Pablo Neira Ayuso,
	Florian Westphal, Phil Sutter, Shuah Khan, Tejun Heo,
	Vladimir Davydov, Chris Down, Zhang Jiao, Song Hu, cgroups,
	netdev, linux-kselftest, Alireza Asgari

From: Alireza Asgari <alireza@asgari.net>

Add a raw-Netlink NFQUEUE selftest using isolated network namespaces and
loopback traffic. Cover individual and batch verdicts, resizing occupied
queues, oversized packets and FAIL_OPEN, payload replacement, and software
segmentation.

With cgroup v2 available, exercise reservation failure and rollback,
aggregate limits, saved ownership after task migration, reparenting, and
inactive sizing before first activation. The payload case checks the
modified UDP payload delivered after an ACCEPT verdict and reuses the
queue afterward.

The script reports unsupported environment requirements explicitly and
cleans up its namespaces, queue sockets, and cgroups.

Signed-off-by: Alireza Asgari <alireza@asgari.net>
---
 tools/testing/selftests/net/netfilter/Makefile     |   1 +
 .../selftests/net/netfilter/nft_queue_memory.py    | 460 +++++++++++++++++++++
 2 files changed, 461 insertions(+)

diff --git a/tools/testing/selftests/net/netfilter/Makefile b/tools/testing/selftests/net/netfilter/Makefile
index 1bc7f7a75f1e..21b58d2cfe86 100644
--- a/tools/testing/selftests/net/netfilter/Makefile
+++ b/tools/testing/selftests/net/netfilter/Makefile
@@ -37,6 +37,7 @@ TEST_PROGS := \
 	nft_nat_zones.sh \
 	nft_offload.sh \
 	nft_queue.sh \
+	nft_queue_memory.py \
 	nft_synproxy.sh \
 	nft_tproxy_tcp.sh \
 	nft_tproxy_udp.sh \
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 000000000000..35ae4db09a48
--- /dev/null
+++ b/tools/testing/selftests/net/netfilter/nft_queue_memory.py
@@ -0,0 +1,460 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""Bounded NFQUEUE retention tests; run in a disposable guest with the patch.
+
+The test creates its own network namespace and consumes notifications while
+withholding verdicts. --cgroup accepts a writable, delegated cgroup v2 parent
+for the optional capacity-reservation test. No public network is used.
+"""
+
+import argparse
+import contextlib
+import errno
+import os
+from pathlib import Path
+import select
+import shutil
+import socket
+import struct
+import subprocess
+import sys
+import time
+
+
+NFQ_SUBSYS = 3 << 8
+NFQ_PACKET, NFQ_VERDICT, NFQ_CONFIG, NFQ_BATCH = range(4)
+CFG_CMD, CFG_PARAMS, CFG_MAXLEN, CFG_MASK, CFG_FLAGS = range(1, 6)
+F_FAIL_OPEN, F_GSO = 1, 4
+SKB_GSO = 2
+MAX_HELD = 64
+
+
+def attr(kind, data):
+    result = struct.pack("=HH", 4 + len(data), kind) + data
+    return result + bytes((-len(result)) % 4)
+
+
+def u32(kind, value):
+    return attr(kind, struct.pack("!I", value))
+
+
+def check(condition, message):
+    if not condition:
+        raise AssertionError(message)
+
+
+class Queue:
+    def __init__(self, number=1):
+        self.number = number
+        self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, 12)
+        self.sock.bind((0, 0))
+        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1 << 20)
+        self.seq = 0
+        self.held = []
+
+    def close(self):
+        self.sock.close()
+
+    def receive(self, timeout):
+        if not select.select([self.sock], [], [], timeout)[0]:
+            return []
+        data, _, flags, _ = self.sock.recvmsg(65536)
+        check(not flags & socket.MSG_TRUNC, "truncated netlink message")
+        replies = []
+        while data:
+            check(len(data) >= 16, "short netlink header")
+            length, kind, _, seq, _ = struct.unpack_from("=IHHII", data)
+            check(16 <= length <= len(data), "invalid netlink length")
+            body = data[16:length]
+            if kind == 2:  # NLMSG_ERROR, including successful ACKs
+                replies.append((seq, struct.unpack_from("=i", body)[0]))
+            elif kind == NFQ_SUBSYS | NFQ_PACKET:
+                attrs = {}
+                offset = 4  # nfgenmsg
+                while offset < len(body):
+                    size, atype = struct.unpack_from("=HH", body, offset)
+                    check(size >= 4 and offset + size <= len(body),
+                          "invalid packet attribute")
+                    attrs[atype & 0x3fff] = body[offset + 4:offset + size]
+                    offset += (size + 3) & ~3
+                packet_id = struct.unpack_from("!I", attrs[1])[0]
+                skb_info = struct.unpack("!I", attrs.get(14, bytes(4)))[0]
+                check(len(self.held) < MAX_HELD, "bounded hold list exhausted")
+                self.held.append((packet_id, skb_info))
+            data = data[(length + 3) & ~3:]
+        return replies
+
+    def request(self, kind, attrs):
+        self.seq += 1
+        body = struct.pack("!BBH", 0, 0, self.number) + attrs
+        header = struct.pack("=IHHII", 16 + len(body), NFQ_SUBSYS | kind,
+                             1 | 4, self.seq, 0)  # REQUEST | ACK
+        self.sock.sendto(header + body, (0, 0))
+        deadline = time.monotonic() + 3
+        while time.monotonic() < deadline:
+            for seq, error in self.receive(max(0, deadline - time.monotonic())):
+                if seq == self.seq:
+                    if error:
+                        raise OSError(-error, os.strerror(-error))
+                    return
+        raise TimeoutError("NFQUEUE ACK timed out")
+
+    def configure(self, maximum, *, bind=False, flags=F_GSO, mode=1):
+        attrs = u32(CFG_MAXLEN, maximum)
+        attrs += attr(CFG_PARAMS, struct.pack("!IB", 0, mode))  # COPY_META by default
+        attrs += u32(CFG_MASK, F_GSO | F_FAIL_OPEN) + u32(CFG_FLAGS, flags)
+        if bind:
+            attrs += attr(CFG_CMD, struct.pack("!BBH", 1, 0, socket.AF_INET))
+        self.request(NFQ_CONFIG, attrs)
+
+    def collect(self):
+        while select.select([self.sock], [], [], 0.1)[0]:
+            replies = self.receive(0)
+            check(not replies, "unexpected asynchronous netlink error")
+
+    def release(self, batch=False):
+        self.collect()
+        ids = [packet_id for packet_id, _ in self.held]
+        self.held.clear()
+        if batch:
+            ids = ids[-1:]
+        for packet_id in ids:
+            self.request(NFQ_BATCH if batch else NFQ_VERDICT,
+                         attr(2, struct.pack("!II", 0, packet_id)))  # DROP
+
+    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)
+
+    def close(self):
+        self.rx.close()
+
+    def send(self, size=16384, segment=0):
+        cmsgs = [(socket.IPPROTO_UDP, 103, struct.pack("=H", segment))] if segment else []
+        # Retained output skbs can exhaust one sender's socket memory first.
+        # A fresh, bounded sender isolates the NFQUEUE retention limit.
+        with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sender:
+            sender.settimeout(1)
+            sender.sendmsg([bytes(size)], cmsgs, 0, self.destination)
+
+
+def retention(traffic, batch):
+    with contextlib.closing(Queue()) as queue:
+        queue.configure(32, bind=True)
+        for _ in range(40):
+            traffic.send()
+            queue.collect()
+        count = len(queue.held)
+        check(0 < count < 32, f"expected byte rejection below count limit, held {count}")
+        stats = queue.stats()
+        check(stats[2] == count and stats[5] > 0, "wrong queue/drop accounting")
+        check(stats[6] == 0, "netlink backpressure invalidated the test")
+        queue.release(batch=batch)
+        check(queue.stats()[2] == 0, "verdict did not drain queue")
+        traffic.send()
+        queue.collect()
+        check(len(queue.held) == 1, "verdict did not restore capacity")
+
+
+def resize(traffic):
+    with contextlib.closing(Queue()) as queue:
+        queue.configure(32, bind=True)
+        traffic.send()
+        queue.collect()
+        check(len(queue.held) == 1, "initial packet not queued")
+        queue.configure(0)
+        check(queue.stats()[2] == 1, "shrink discarded existing entry")
+        traffic.send()
+        queue.collect()
+        check(len(queue.held) == 1, "zero limit admitted a packet")
+        queue.release(batch=True)
+        queue.configure(32)
+        traffic.send()
+        queue.collect()
+        check(len(queue.held) == 1, "growth did not restore admission")
+
+
+def oversized(traffic):
+    with contextlib.closing(Queue()) as queue:
+        queue.configure(1, bind=True)
+        traffic.send(60000)
+        queue.collect()
+        check(not queue.held and queue.stats()[5] == 1,
+              "single skb above byte allowance was not rejected")
+        queue.configure(1, flags=F_GSO | F_FAIL_OPEN)
+        traffic.send(60000)
+        queue.collect()
+        check(not queue.held, "FAIL_OPEN packet was queued")
+        check(select.select([traffic.rx], [], [], 1)[0], "FAIL_OPEN did not deliver")
+        check(len(traffic.rx.recv(65536)) == 60000, "incorrect FAIL_OPEN payload")
+
+
+def ipv4_checksum(header):
+    total = sum(struct.unpack("!10H", header))
+    while total >> 16:
+        total = (total & 0xffff) + (total >> 16)
+    return (~total) & 0xffff
+
+
+def payload_accept(traffic):
+    with contextlib.closing(Queue()) as queue:
+        queue.configure(1, bind=True)
+        traffic.send(64)
+        queue.collect()
+        check(len(queue.held) == 1, "initial small packet was not queued")
+        packet_id = queue.held.pop()[0]
+        data = b"\xa5" * (60000 - 28)
+        address = socket.inet_aton("127.0.0.1")
+        header = struct.pack("!BBHHHBBH4s4s", 0x45, 0, 60000, 0, 0,
+                             64, socket.IPPROTO_UDP, 0, address, address)
+        checksum = ipv4_checksum(header)
+        header = header[:10] + struct.pack("!H", checksum) + header[12:]
+        udp = struct.pack("!HHHH", 12345, traffic.destination[1],
+                          len(data) + 8, 0)
+        # Delivery proves replacement succeeded; DROP could hide a failed edit.
+        queue.request(NFQ_VERDICT,
+                      attr(2, struct.pack("!II", 1, packet_id)) +  # ACCEPT
+                      attr(10, header + udp + data))  # NFQA_PAYLOAD
+        check(select.select([traffic.rx], [], [], 3)[0],
+              "expanded ACCEPT packet was not delivered")
+        check(traffic.rx.recv(65536) == data,
+              "expanded payload differs from supplied data")
+        check(queue.stats()[2] == 0, "ACCEPT verdict did not drain queue")
+        traffic.send(64)
+        queue.collect()
+        check(len(queue.held) == 1,
+              "successful payload expansion did not restore saved-charge capacity")
+        queue.release()
+        check(queue.stats()[2] == 0, "subsequent packet did not drain")
+
+
+def segmentation(traffic):
+    for flags, expected in [(F_GSO, 1), (0, 8)]:
+        with contextlib.closing(Queue()) as queue:
+            queue.configure(64, bind=True, flags=flags)
+            try:
+                traffic.send(8 * 1200, segment=1200)
+            except OSError as error:
+                if error.errno in (errno.EINVAL, errno.ENOPROTOOPT, errno.EOPNOTSUPP):
+                    return "UDP_SEGMENT is unavailable"
+                raise
+            queue.collect()
+            check(len(queue.held) == expected,
+                  f"GSO flags {flags}: expected {expected} entries, got {len(queue.held)}")
+            if flags:
+                check(queue.held[0][1] & SKB_GSO, "aggregate did not reach NFQUEUE")
+            queue.release()
+            check(queue.stats()[2] == 0, "segmented entries did not drain")
+
+
+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()))
+        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}")
+            else:
+                raise AssertionError("oversized capacity reservation succeeded")
+            # A refused BIND must leave no published queue behind.
+            queue.configure(32, bind=True)
+            # Later changes must continue charging the original owner.
+            (original / "cgroup.procs").write_text(str(os.getpid()))
+            try:
+                queue.configure(1 << 20, mode=0, flags=0)
+            except OSError as error:
+                check(error.errno == errno.ENOMEM, f"unexpected growth refusal: {error}")
+            else:
+                raise AssertionError("growth escaped its original owner")
+            traffic.send()
+            queue.collect()
+            check(len(queue.held) == 1, "failed growth changed the usable queue")
+            queue.release()
+            queue.configure(0)
+        # Individually bounded queues must still share their owner's budget.
+        (group / "cgroup.procs").write_text(str(os.getpid()))
+        with contextlib.ExitStack() as stack:
+            queues = []
+            refused = None
+            for number in range(10, 26):
+                candidate = stack.enter_context(contextlib.closing(Queue(number)))
+                try:
+                    candidate.configure(4096, bind=True)
+                except OSError as error:
+                    check(error.errno == errno.ENOMEM,
+                          f"unexpected aggregate refusal: {error}")
+                    refused = candidate
+                    break
+                queues.append(candidate)
+            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)
+    finally:
+        (original / "cgroup.procs").write_text(str(os.getpid()))
+        if group.exists():
+            group.rmdir()
+
+
+def reservation_startup(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-startup-{os.getpid()}"
+    group.mkdir()
+    bind = attr(CFG_CMD, struct.pack("!BBH", 1, 0, socket.AF_INET))
+    activate = attr(CFG_PARAMS, struct.pack("!IB", 0, 1))  # COPY_META
+    try:
+        # Enough for a small queue, but less than the default 1024-slot allowance.
+        (group / "memory.max").write_text(str(1 << 20))
+        (group / "cgroup.procs").write_text(str(os.getpid()))
+        with contextlib.closing(Queue()) as queue:
+            queue.request(NFQ_CONFIG, bind)
+            check(queue.stats()[3] == 0, "BIND enabled packet admission")
+            # Inactive sizing must not reserve capacity either.
+            queue.request(NFQ_CONFIG, u32(CFG_MAXLEN, 1 << 20))
+            queue.request(NFQ_CONFIG, u32(CFG_MAXLEN, 1))
+            (original / "cgroup.procs").write_text(str(os.getpid()))
+            try:
+                queue.configure(1 << 20, flags=F_GSO | F_FAIL_OPEN)
+            except OSError as error:
+                check(error.errno == errno.ENOMEM,
+                      f"unexpected activation refusal: {error}")
+            else:
+                raise AssertionError("activation escaped its BIND-time owner")
+            check(queue.stats()[3] == 0, "failed activation changed copy mode")
+            # A mode-only retry also verifies that failed activation kept MAXLEN.
+            queue.request(NFQ_CONFIG, activate)
+            traffic.send(64)
+            queue.collect()
+            check(len(queue.held) == 1, "small queue did not become usable")
+            queue.request(NFQ_CONFIG, attr(CFG_PARAMS, struct.pack("!IB", 0, 0)))
+            stats = queue.stats()
+            check(stats[3] == 0 and stats[2] == 1,
+                  "COPY_NONE discarded a retained packet")
+            try:
+                queue.request(NFQ_CONFIG, u32(CFG_MAXLEN, 1 << 20))
+            except OSError as error:
+                check(error.errno == errno.ENOMEM,
+                      f"unexpected COPY_NONE growth refusal: {error}")
+            else:
+                raise AssertionError("COPY_NONE disabled established accounting")
+            stats = queue.stats()
+            check(stats[3] == 0 and stats[2] == 1 and len(queue.held) == 1,
+                  "failed COPY_NONE growth changed mode or retained packet")
+            queue.request(NFQ_CONFIG, activate)
+            queue.release()
+            traffic.send(60000)
+            queue.collect()
+            check(not queue.held and queue.stats()[5] > 0,
+                  "failed activation changed the small queue limit")
+            check(not select.select([traffic.rx], [], [], 0.1)[0],
+                  "failed activation enabled FAIL_OPEN")
+
+        # BIND, final size, and mode may also arrive in one configuration message.
+        (group / "cgroup.procs").write_text(str(os.getpid()))
+        with contextlib.closing(Queue()) as queue:
+            queue.configure(1, bind=True)
+            (original / "cgroup.procs").write_text(str(os.getpid()))
+            traffic.send(64)
+            queue.collect()
+            check(len(queue.held) == 1, "bundled small-queue activation failed")
+            queue.release()
+
+        # Closing before activation must release the captured owner reference.
+        (group / "cgroup.procs").write_text(str(os.getpid()))
+        with contextlib.closing(Queue()) as queue:
+            queue.request(NFQ_CONFIG, bind)
+            (original / "cgroup.procs").write_text(str(os.getpid()))
+    finally:
+        (original / "cgroup.procs").write_text(str(os.getpid()))
+        group.rmdir()
+
+
+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 [])])
+    subprocess.run(["ip", "link", "set", "lo", "up"], check=True)
+    with contextlib.closing(Traffic()) as traffic:
+        tests = [
+            ("byte admission and individual release", lambda: retention(traffic, False)),
+            ("byte admission and batch release", lambda: retention(traffic, True)),
+            ("occupied shrink and growth", lambda: resize(traffic)),
+            ("oversized skb and FAIL_OPEN", lambda: oversized(traffic)),
+            ("payload replacement and saved-charge release", lambda: payload_accept(traffic)),
+            ("GSO and software segmentation", lambda: segmentation(traffic)),
+            ("reservation rollback and ownership", lambda: reservation(traffic, args.cgroup)),
+            ("reservation startup and activation", lambda: reservation_startup(traffic, args.cgroup)),
+        ]
+        print(f"TAP version 13\n1..{len(tests)}", flush=True)
+        failed = False
+        for number, (name, run) in enumerate(tests, 1):
+            try:
+                skip = run()
+                suffix = f" # SKIP {skip}" if skip else ""
+                print(f"ok {number} - {name}{suffix}", flush=True)
+            except (AssertionError, OSError, TimeoutError) as error:
+                print(f"not ok {number} - {name}\n# {error}", flush=True)
+                failed = True
+        return int(failed)
+
+
+if __name__ == "__main__":
+    sys.exit(main())

-- 
2.34.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH RFC nf-next 6/9] netfilter: nfqueue: account saved skb bytes per queue
  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
  0 siblings, 0 replies; 11+ messages in thread
From: Florian Westphal @ 2026-11-16 22:46 UTC (permalink / raw)
  To: alireza
  Cc: netfilter-devel, linux-mm, Johannes Weiner, Michal Hocko,
	Roman Gushchin, Shakeel Butt, Muchun Song, Andrew Morton,
	David Hildenbrand, Pablo Neira Ayuso, Phil Sutter, Shuah Khan,
	Tejun Heo, Vladimir Davydov, Chris Down, Zhang Jiao, Song Hu,
	cgroups, netdev, linux-kselftest

Alireza Asgari via B4 Relay <devnull+alireza.asgari.net@kernel.org> wrote:
> --- a/net/netfilter/nfnetlink_queue.c
> +++ b/net/netfilter/nfnetlink_queue.c
> @@ -92,6 +92,8 @@ struct nfqnl_instance {
>  	unsigned int	queue_total;
>  	unsigned int	id_sequence;		/* 'sequence' of pkt ids */
>  	struct list_head queue_list;		/* packets in queue */
> +	u64 mem_limit;
> +	u64 mem_used;

I think u32 is fine for those.  Or do you see a use case for 2**32 mbyte
worth of queued packets?

> +/* All release paths use the saved admission cost, including notification undo. */
> +static void nfqnl_mem_uncharge(struct nfqnl_instance *queue,
> +			       struct nf_queue_entry *entry)
> +{
> +	lockdep_assert_held(&queue->lock);
> +	queue->mem_used -= entry->truesize;
> +	entry->truesize = 0;
> +}
> +
>  static struct nfqnl_instance *
>  instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid)
>  {
> @@ -152,6 +176,7 @@ instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid)
>  	inst->queue_num = queue_num;
>  	inst->peer_portid = portid;
>  	inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
> +	inst->mem_limit = nfqnl_mem_limit(NFQNL_QMAX_DEFAULT);
>  	inst->copy_range = NFQNL_MAX_COPY_RANGE;
>  	inst->copy_mode = NFQNL_COPY_NONE;
>  	spin_lock_init(&inst->lock);
> @@ -199,6 +224,7 @@ static void instance_destroy_work(struct work_struct *work)
>  	rcu_read_lock();
>  	nfqnl_flush(inst, NULL, 0);
>  	rcu_read_unlock();
> +	WARN_ON_ONCE(inst->mem_used);

I'd suggest DEBUG_NET_WARN_ONCE() here, unless you have a strong
preference/reason for a full splat here.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-16 22:46 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox