[PATCH 09/14] sunrpc: add netlink upcall for the auth.unix.gid cache

Jeff Layton posted 14 patches 3 weeks ago
There is a newer version of this series
Re: [PATCH 09/14] sunrpc: add netlink upcall for the auth.unix.gid cache
Posted by Chuck Lever 2 weeks, 3 days ago
On 3/16/26 11:14 AM, Jeff Layton wrote:

> diff --git a/net/sunrpc/svcauth_unix.c b/net/sunrpc/svcauth_unix.c
> index e4b196742877bd3abf199f2bf815b90615a2be04..b84511ff726c1836f777c802943f6d8e112a0998 100644
> --- a/net/sunrpc/svcauth_unix.c
> +++ b/net/sunrpc/svcauth_unix.c
> @@ -585,12 +585,241 @@ static int unix_gid_show(struct seq_file *m,
>  	return 0;
>  }
>  
> +static int unix_gid_notify(struct cache_detail *cd, struct cache_head *h)
> +{
> +	return sunrpc_cache_notify(cd, h, SUNRPC_CACHE_TYPE_UNIX_GID);
> +}
> +
> +/**
> + * sunrpc_nl_unix_gid_get_reqs_dumpit - dump pending unix_gid requests
> + * @skb: reply buffer
> + * @cb: netlink metadata and command arguments
> + *
> + * Walk the unix_gid cache's pending request list and create a netlink
> + * message with a nested entry for each cache_request, containing the
> + * seqno and uid.
> + *
> + * Returns the size of the reply or a negative errno.
> + */
> +int sunrpc_nl_unix_gid_get_reqs_dumpit(struct sk_buff *skb,
> +					struct netlink_callback *cb)
> +{
> +	struct sunrpc_net *sn;
> +	struct cache_detail *cd;
> +	struct cache_head **items;
> +	u64 *seqnos;
> +	int cnt, i;
> +	void *hdr;
> +	int ret;
> +
> +	sn = net_generic(sock_net(skb->sk), sunrpc_net_id);
> +
> +	cd = sn->unix_gid_cache;
> +	if (!cd)
> +		return -ENODEV;
> +
> +	/* Second call means we've already dumped everything */
> +	if (cb->args[0])
> +		return 0;
> +
> +	cnt = sunrpc_cache_requests_count(cd);
> +	if (!cnt)
> +		return 0;
> +
> +	items = kcalloc(cnt, sizeof(*items), GFP_KERNEL);
> +	seqnos = kcalloc(cnt, sizeof(*seqnos), GFP_KERNEL);
> +	if (!items || !seqnos) {
> +		ret = -ENOMEM;
> +		goto out_alloc;
> +	}
> +
> +	cnt = sunrpc_cache_requests_snapshot(cd, items, seqnos, cnt);
> +
> +	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
> +			  cb->nlh->nlmsg_seq, &sunrpc_nl_family,
> +			  NLM_F_MULTI, SUNRPC_CMD_UNIX_GID_GET_REQS);
> +	if (!hdr) {
> +		ret = -ENOBUFS;
> +		goto out_put;
> +	}
> +
> +	for (i = 0; i < cnt; i++) {
> +		struct unix_gid *ug;
> +		struct nlattr *nest;
> +
> +		ug = container_of(items[i], struct unix_gid, h);
> +
> +		nest = nla_nest_start(skb,
> +				      SUNRPC_A_UNIX_GID_REQS_REQUESTS);
> +		if (!nest) {
> +			ret = -ENOBUFS;
> +			goto out_cancel;
> +		}
> +
> +		if (nla_put_u64_64bit(skb, SUNRPC_A_UNIX_GID_SEQNO,
> +				      seqnos[i], 0) ||
> +		    nla_put_u32(skb, SUNRPC_A_UNIX_GID_UID,
> +				from_kuid(&init_user_ns, ug->uid))) {
> +			nla_nest_cancel(skb, nest);
> +			ret = -ENOBUFS;
> +			goto out_cancel;
> +		}
> +
> +		nla_nest_end(skb, nest);
> +	}
> +
> +	genlmsg_end(skb, hdr);
> +	cb->args[0] = 1;
> +	ret = skb->len;
> +	goto out_put;
> +
> +out_cancel:
> +	genlmsg_cancel(skb, hdr);
> +out_put:
> +	for (i = 0; i < cnt; i++)
> +		cache_put(items[i], cd);
> +out_alloc:
> +	kfree(seqnos);
> +	kfree(items);
> +	return ret;
> +}
sunrpc_nl_unix_gid_get_reqs_dumpit() packs its reply entries into a
single netlink message. The default SKB size is ~8 KiB, and each entry
encodes to ~24 bytes, so the buffer fills at roughly 340 entries. When
nla_put fails, the function returns -ENOBUFS but never sets

  cb->args[0] = 1;

causing the netlink subsystem to retry indefinitely. A big NFS server
can easily exceed 340 pending GID cache entries.

Should we implement proper netlink dump continuation: snapshot once
then emit entries across multiple dumpit calls using a cursor in
cb->args?

-- 
Chuck Lever