All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net] neigh: make sure used and confirmed times are valid
@ 2023-02-02 15:25 Julian Anastasov
  2023-02-06  9:10 ` patchwork-bot+netdevbpf
  0 siblings, 1 reply; 2+ messages in thread
From: Julian Anastasov @ 2023-02-02 15:25 UTC (permalink / raw)
  To: David S . Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev,
	Zhang Changzhong, YueHaibing

Entries can linger in cache without timer for days, thanks to
the gc_thresh1 limit. As result, without traffic, the confirmed
time can be outdated and to appear to be in the future. Later,
on traffic, NUD_STALE entries can switch to NUD_DELAY and start
the timer which can see the invalid confirmed time and wrongly
switch to NUD_REACHABLE state instead of NUD_PROBE. As result,
timer is set many days in the future. This is more visible on
32-bit platforms, with higher HZ value.

Why this is a problem? While we expect unused entries to expire,
such entries stay in REACHABLE state for too long, locked in
cache. They are not expired normally, only when cache is full.

Problem and the wrong state change reported by Zhang Changzhong:

172.16.1.18 dev bond0 lladdr 0a:0e:0f:01:12:01 ref 1 used 350521/15994171/350520 probes 4 REACHABLE

350520 seconds have elapsed since this entry was last updated, but it is
still in the REACHABLE state (base_reachable_time_ms is 30000),
preventing lladdr from being updated through probe.

Fix it by ensuring timer is started with valid used/confirmed
times. Considering the valid time range is LONG_MAX jiffies,
we try not to go too much in the past while we are in
DELAY/PROBE state. There are also places that need
used/updated times to be validated while timer is not running.

Reported-by: Zhang Changzhong <zhangchangzhong@huawei.com>
Signed-off-by: Julian Anastasov <ja@ssi.bg>
Tested-by: Zhang Changzhong <zhangchangzhong@huawei.com>
---
 net/core/neighbour.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

 This solution prefers to add code in neigh_add_timer()
 assuming it is less used than the timer code. The
 alternative would be to add time_in_range* calls in
 neigh_timer_handler() to be more safe.
 Another solution would be to add the time correction
 only in __neigh_event_send() where we switch from
 STALE to DELAY as it looks to be the only path that
 is affected and where we switch to states that
 consider the confirmed time. OTOH, NUD_INCOMPLETE
 is not affected from invalid times.

diff --git a/net/core/neighbour.c b/net/core/neighbour.c
index f00a79fc301b..4edd2176e238 100644
--- a/net/core/neighbour.c
+++ b/net/core/neighbour.c
@@ -269,7 +269,7 @@ static int neigh_forced_gc(struct neigh_table *tbl)
 			    (n->nud_state == NUD_NOARP) ||
 			    (tbl->is_multicast &&
 			     tbl->is_multicast(n->primary_key)) ||
-			    time_after(tref, n->updated))
+			    !time_in_range(n->updated, tref, jiffies))
 				remove = true;
 			write_unlock(&n->lock);
 
@@ -289,7 +289,17 @@ static int neigh_forced_gc(struct neigh_table *tbl)
 
 static void neigh_add_timer(struct neighbour *n, unsigned long when)
 {
+	/* Use safe distance from the jiffies - LONG_MAX point while timer
+	 * is running in DELAY/PROBE state but still show to user space
+	 * large times in the past.
+	 */
+	unsigned long mint = jiffies - (LONG_MAX - 86400 * HZ);
+
 	neigh_hold(n);
+	if (!time_in_range(n->confirmed, mint, jiffies))
+		n->confirmed = mint;
+	if (time_before(n->used, n->confirmed))
+		n->used = n->confirmed;
 	if (unlikely(mod_timer(&n->timer, when))) {
 		printk("NEIGH: BUG, double timer add, state is %x\n",
 		       n->nud_state);
@@ -1001,12 +1011,14 @@ static void neigh_periodic_work(struct work_struct *work)
 				goto next_elt;
 			}
 
-			if (time_before(n->used, n->confirmed))
+			if (time_before(n->used, n->confirmed) &&
+			    time_is_before_eq_jiffies(n->confirmed))
 				n->used = n->confirmed;
 
 			if (refcount_read(&n->refcnt) == 1 &&
 			    (state == NUD_FAILED ||
-			     time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
+			     !time_in_range_open(jiffies, n->used,
+						 n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
 				*np = n->next;
 				neigh_mark_dead(n);
 				write_unlock(&n->lock);
-- 
2.39.1



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

* Re: [PATCH net] neigh: make sure used and confirmed times are valid
  2023-02-02 15:25 [PATCH net] neigh: make sure used and confirmed times are valid Julian Anastasov
@ 2023-02-06  9:10 ` patchwork-bot+netdevbpf
  0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-02-06  9:10 UTC (permalink / raw)
  To: Julian Anastasov
  Cc: davem, edumazet, kuba, pabeni, netdev, zhangchangzhong, yuehaibing

Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <davem@davemloft.net>:

On Thu,  2 Feb 2023 17:25:51 +0200 you wrote:
> Entries can linger in cache without timer for days, thanks to
> the gc_thresh1 limit. As result, without traffic, the confirmed
> time can be outdated and to appear to be in the future. Later,
> on traffic, NUD_STALE entries can switch to NUD_DELAY and start
> the timer which can see the invalid confirmed time and wrongly
> switch to NUD_REACHABLE state instead of NUD_PROBE. As result,
> timer is set many days in the future. This is more visible on
> 32-bit platforms, with higher HZ value.
> 
> [...]

Here is the summary with links:
  - [net] neigh: make sure used and confirmed times are valid
    https://git.kernel.org/netdev/net/c/c1d2ecdf5e38

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2023-02-06  9:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-02 15:25 [PATCH net] neigh: make sure used and confirmed times are valid Julian Anastasov
2023-02-06  9:10 ` patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.