Hi Florian,
> This is rather complex, do we really need all of this extra machinery?
I added the worker so a queue that was shrunk while full could return
the excess charge as its packets drained. I agree with Chris that the
refund can wait until the next configuration change or queue teardown.
That would let me drop the refund worker and its mutex. Verdicts would
just update the queue's byte count.
I'd keep v1's choice to capture the owner at BIND and take the first
charge when the queue is enabled. That lets userspace bind a queue
and set a smaller limit before it has to pay for the default size.
Does that fit the lifecycle you had in mind?
Thanks,
Alireza
From: Florian Westphal <f…@strlen.de>
To: <alir…@asgari.net>
Cc: <netf…@vger.kernel.org>, <linu…@kvack.org>, "Johannes Weiner"<hann…@cmpxchg.org>, "Michal Hocko"<mhoc…@kernel.org>, "Roman Gushchin"<roma…@linux.dev>, "Shakeel Butt"<shak…@linux.dev>, "Muchun Song"<much…@linux.dev>, "Andrew Morton"<ak…@linux-foundation.org>, "David Hildenbrand"<dav…@kernel.org>, "Pablo Neira Ayuso"<pab…@netfilter.org>, "Phil Sutter"<ph…@nwl.cc>, "Shuah Khan"<shu…@kernel.org>, "Tejun Heo"<t…@kernel.org>, "Vladimir Davydov"<vdav…@gmail.com>, "Chris Down"<chr…@chrisdown.name>, "Zhang Jiao"<zhan…@cmss.chinamobile.com>, "Song Hu"<huso…@kylinos.cn>, <cgro…@vger.kernel.org>, <netd…@vger.kernel.org>, <linu…@vger.kernel.org>
Date: Tue, 17 Nov 2026 00:17:09 +0100
Subject: Re: [PATCH RFC nf-next 7/9] netfilter: nfqueue: reserve capacity against the bound owner
> Alireza Asgari via B4 Relay <devn…@kernel.org> wrote:
> > +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);
> > +}
>
> This is rather complex, do we really need all of this extra machinery?
>
> I had hoped we could do the memcg equivalent of
>
> vmalloc(4 * 1024 * 1024, GFP_KERNEL_ACCOUNT);
> on queue creation / instantiation, and vfree() on instance destruction.
>
> The only minor inconvenience is queue limit reconfiguration. But that
> also happens from process context, so it should be enough to just
> do the equivalent of vrealloc; or, if memcg precharge cannot easily
> support that, just vmalloc, then vfree(old).
>