Netdev List
 help / color / mirror / Atom feed
From: Ido Schimmel <idosch@nvidia.com>
To: Ren Wei <weir@nebusec.ai>
Cc: netdev@vger.kernel.org, dsahern@kernel.org, davem@davemloft.net,
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
	willemb@google.com, horms@kernel.org, vega@nebusec.ai,
	caoruide123@gmail.com
Subject: Re: [PATCH net 1/1] inetpeer: bound the peer cache across network namespaces
Date: Sun, 6 Dec 2026 16:17:38 +0200	[thread overview]
Message-ID: <20261206141738.GA106118@shredder> (raw)
In-Reply-To: <56dff40001dac2b78e4d53ff9cdf5991751823bf.1795767225.git.caoruide123@gmail.com>

On Wed, Dec 02, 2026 at 02:08:59PM +0800, Ren Wei wrote:
>  net/ipv4/inetpeer.c | 26 +++++++++++++++++++++++---
>  1 file changed, 23 insertions(+), 3 deletions(-)
> 
> diff --git a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c
> index adf6dc8a95b4..06c26dc3ff04 100644
> --- a/net/ipv4/inetpeer.c
> +++ b/net/ipv4/inetpeer.c
> @@ -44,7 +44,7 @@
>   *  1.  Nodes may appear in the tree only with the pool lock held.
>   *  2.  Nodes may disappear from the tree only with the pool lock held
>   *      AND reference count being 0.
> - *  3.  Global variable peer_total is modified under the pool lock.
> + *  3.  Global variable peer_total is modified atomically.
>   *  4.  struct inet_peer fields modification:
>   *		rb_node: pool lock
>   *		refcnt: atomically against modifications on other CPU;
> @@ -53,6 +53,7 @@
>   */
>  
>  static struct kmem_cache *peer_cachep __ro_after_init;
> +static atomic_t peer_total;
>  static siphash_aligned_key_t inetpeer_hash_key __read_mostly;
>  
>  static u64 inetpeer_addr_hash(const struct inetpeer_addr *a)
> @@ -97,6 +98,20 @@ int inet_peer_threshold __read_mostly;	/* start to throw entries more
>  int inet_peer_minttl __read_mostly = 120 * HZ;	/* TTL under high load: 120 sec */
>  int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;	/* usual time to live: 10 min */
>  
> +static bool inet_peer_try_alloc(void)
> +{
> +	int threshold, total;
> +
> +	threshold = READ_ONCE(inet_peer_threshold);
> +	total = atomic_read(&peer_total);
> +	do {
> +		if (total >= threshold)
> +			return false;
> +	} while (!atomic_try_cmpxchg(&peer_total, &total, total + 1));
> +
> +	return true;
> +}
> +
>  /* Called from ip_output.c:ip_init  */
>  void __init inet_initpeers(void)
>  {
> @@ -108,7 +123,8 @@ void __init inet_initpeers(void)
>  
>  	inet_peer_threshold = clamp_val(nr_entries, 4096, 65536 + 128);
>  
> -	peer_cachep = KMEM_CACHE(inet_peer, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
> +	peer_cachep = KMEM_CACHE(inet_peer,
> +				 SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT);

Allocations are from softirq context, so I'm quite sure they are not
charged correctly.

>  }
>  
>  /* Called with rcu_read_lock() or base->lock held */
> @@ -190,6 +206,7 @@ static void inet_peer_gc(struct inet_peer_base *base,
>  		if (p) {
>  			rb_erase(&p->rb_node, &base->rb_root);
>  			base->total--;
> +			atomic_dec(&peer_total);
>  			kfree_rcu(p, rcu);
>  		}
>  	}
> @@ -222,7 +239,7 @@ struct inet_peer *inet_getpeer(struct inet_peer_base *base,
>  
>  	gc_cnt = 0;
>  	p = lookup(daddr, dhash, base, seq, gc_stack, &gc_cnt, &parent, &pp);
> -	if (!p) {
> +	if (!p && inet_peer_try_alloc()) {

I believe that Sashiko is correct and this can lead to one netns
starving others:

"
Can one network namespace permanently starve all other namespaces of
inet_peer entries once it fills peer_total to inet_peer_threshold and
goes idle?

Garbage collection only runs on the caller's own base tree during a cache
miss in inet_getpeer(), or when a namespace is destroyed.  If one
namespace fills the global peer_total budget and then stops experiencing
lookup misses, its entries are never reclaimed even after
inet_peer_maxttl expires.  Lookup misses in other namespaces (including
init_net) will fail inet_peer_try_alloc() and can only run inet_peer_gc()
on their own trees, causing inet_getpeer() to return NULL indefinitely
and disabling per-peer ICMP rate limiting, IPv4 fragment max_dist checks,
and ICMP redirects in those namespaces.

In addition, calling inet_peer_try_alloc() before inet_peer_gc() means a
miss when peer_total == inet_peer_threshold fails immediately even when
lookup() collected expired entries in gc_stack that inet_peer_gc() is
about to free.
"

https://sashiko.dev/#/patchset/cover.1795767225.git.caoruide123%40gmail.com

>  		p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);

A better approach would be to charge the peer allocations to the cgroup
the netns is charged to by passing __GFP_ACCOUNT, preceded by
set_active_memcg().

I suggest targeting this at net-next, similar to other GFP_ACCOUNT
annotations, such as commit 6126891c6d4f ("memcg: enable accounting for
IP address and routing-related objects").

pw-bot: cr

>  		if (p) {
>  			p->daddr = *daddr;
> @@ -241,6 +258,8 @@ struct inet_peer *inet_getpeer(struct inet_peer_base *base,
>  			rb_link_node(&p->rb_node, parent, pp);
>  			rb_insert_color(&p->rb_node, &base->rb_root);
>  			base->total++;
> +		} else {
> +			atomic_dec(&peer_total);
>  		}
>  	}
>  	if (gc_cnt)
> @@ -309,6 +328,7 @@ void inetpeer_invalidate_tree(struct inet_peer_base *base)
>  
>  		p = rb_next(p);
>  		rb_erase(&peer->rb_node, &base->rb_root);
> +		atomic_dec(&peer_total);
>  		inet_putpeer(peer);
>  		cond_resched();
>  	}
> -- 
> 2.47.3
> 

  reply	other threads:[~2026-12-06 14:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  6:08 [PATCH net 0/1] inetpeer: bound the peer cache across network namespaces Ren Wei
2026-12-02  6:08 ` [PATCH net 1/1] " Ren Wei
2026-12-06 14:17   ` Ido Schimmel [this message]
2026-12-02  6:14 ` [PATCH net 0/1] " netdev-bot+sinfo

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20261206141738.GA106118@shredder \
    --to=idosch@nvidia.com \
    --cc=caoruide123@gmail.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=vega@nebusec.ai \
    --cc=weir@nebusec.ai \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

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

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