Subject: Re: [PATCH net-next v2] tcp: Dump bound-only sockets in inet_diag.

From: Kuniyuki Iwashima <[email-masked]>
Date: Mon, 27 Nov 2023 09:56:43 -0800

From: Guillaume Nault <[email-masked]>
Date: Mon, 27 Nov 2023 18:26:05 +0100
> On Fri, Nov 24, 2023 at 05:39:42PM -0800, Kuniyuki Iwashima wrote:
> > > +			spin_lock_bh(&ibb->lock);
> > > +			inet_bind_bucket_for_each(tb2, &ibb->chain) {
> > > +				if (!net_eq(ib2_net(tb2), net))
> > > +					continue;
> > > +
> > > +				sk_for_each_bound_bhash2(sk, &tb2->owners) {
> > > +					struct inet_sock *inet = inet_sk(sk);
> > > +
> > > +					if (num < s_num)
> > > +						goto next_bind;
> > > +
> > > +					if (sk->sk_state != TCP_CLOSE ||
> > > +					    !inet->inet_num)
> > > +						goto next_bind;
> > > +
> > > +					if (r->sdiag_family != AF_UNSPEC &&
> > > +					    r->sdiag_family != sk->sk_family)
> > > +						goto next_bind;
> > > +
> > > +					if (!inet_diag_bc_sk(bc, sk))
> > > +						goto next_bind;
> > > +
> > > +					if (!refcount_inc_not_zero(&sk->sk_refcnt))
> > > +						goto next_bind;
> > 
> > I guess this is copied from the ehash code below, but could
> > refcount_inc_not_zero() fail for bhash2 under spin_lock_bh() ?
> 
> My understanding is that it can't fail, but I prefered to keep the test
> to be on the safe side.
> 
> I can post a v3 using a plain sock_hold(), if you prefer.

I prefer sock_hold() because refcount_inc_not_zero() implies that it could
fail and is confusing if it never fails.