Re: [PATCH net-next 1/3] net/tcp-ao: convert the getsockopt helpers to sockopt_t
From: Breno Leitao
Date: Sat Nov 21 2026 - 07:53:44 EST
On Fri, Nov 13, 2026 at 12:21:27PM -0800, Stanislav Fomichev wrote:
> On 11/12, Breno Leitao wrote:
> > @@ -2214,6 +2213,21 @@ static int tcp_ao_copy_mkts_to_user(const struct sock *sk,
> > }
> >
> > bytes_to_write = min_t(int, user_len, sizeof(struct tcp_ao_getsockopt));
> > +
>
> [..]
>
> > + /* The reply is up to nkeys entries, user_len apart, with the match
> > + * count in the first entry's nkeys, so it can outgrow optlen. Size it
> > + * by the keys on the socket rather than by the caller's nkeys.
> > + */
> > + hlist_for_each_entry_rcu(key, &ao_info->head, node,
> > + lockdep_sock_is_held(sk))
> > + nr_keys++;
> > + reply_len = max(array_size(min(max_keys, nr_keys), user_len),
> > + offsetofend(struct tcp_ao_getsockopt, nkeys));
> > + err = sockopt_expand_out(sopt, reply_len);
> > + if (err)
> > + return err;
> > + head = sopt->iter_out;
>
> Is that faster / cleaner / more preferable than calling sockopt_expand_out
> on every iteration hlist_for_each_entry_rcu below?
sockopt_expand_out() can't be called once something went through
iter_out. It re-anchors the iterator at the head of optval, so it WARNs
and returns -EINVAL if the iterator has already moved.
That check came in v2 of the series that added the helper [1], because
a late call would rewind the write cursor. With a call per key, the
second key would trip it.
So the size has to be known before the first entry is written, which is
why the keys are counted first. ip_mc_gsfget() does the same and sizes
its source list by min(sl_count, gf_numsrc). tcp_ao_info just keeps no
key count, hence the walk.
Sizing by nkeys alone doesn't work either: a large nkeys would hit the
MAX_RW_COUNT check and get -EINVAL, where today it copies only the keys
that match.
The extra walk seems very cheap. The keys come from sock_kmalloc(), so
optmem_max caps how many a socket holds, and the socket lock keeps the
list stable between the two walks.
I'll say this in the comment and resping a v2.
Thanks for the review, you are being key to this whole convertion,
--breno
[1] https://lore.kernel.org/20260914-getsockopt_phase6-v2-0-e48befc9602e@xxxxxxxxxx