Subject: Re: [PATCH net-next 3/8] net: psample: add user cookie

From: Ido Schimmel <[email-masked]>
Date: Thu, 25 Apr 2024 10:32:54 +0300

On Wed, Apr 24, 2024 at 03:50:50PM +0200, Adrian Moreno wrote:
> @@ -579,6 +580,15 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
>  			goto error;
>  	}
>  #endif
> +	if (md->user_cookie && md->user_cookie_len) {
> +		int nla_len = nla_total_size(md->user_cookie_len);
> +		struct nlattr *nla;
> +
> +		nla = skb_put(nl_skb, nla_len);
> +		nla->nla_type = PSAMPLE_ATTR_USER_COOKIE;
> +		nla->nla_len = nla_attr_size(md->user_cookie_len);
> +		memcpy(nla_data(nla), md->user_cookie, md->user_cookie_len);
> +	}

Did you consider using nla_put() instead?

diff --git a/net/psample/psample.c b/net/psample/psample.c
index 92db8ffa2ba2..ea59ca46b119 100644
--- a/net/psample/psample.c
+++ b/net/psample/psample.c
@@ -589,15 +589,10 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
                        goto error;
        }
 #endif
-       if (md->user_cookie && md->user_cookie_len) {
-               int nla_len = nla_total_size(md->user_cookie_len);
-               struct nlattr *nla;
-
-               nla = skb_put(nl_skb, nla_len);
-               nla->nla_type = PSAMPLE_ATTR_USER_COOKIE;
-               nla->nla_len = nla_attr_size(md->user_cookie_len);
-               memcpy(nla_data(nla), md->user_cookie, md->user_cookie_len);
-       }
+       if (md->user_cookie && md->user_cookie_len &&
+           nla_put(nl_skb, PSAMPLE_ATTR_USER_COOKIE, md->user_cookie_len,
+                   md->user_cookie))
+               goto error;
 
        genlmsg_end(nl_skb, data);
        psample_nl_obj_desc_init(&desc, group->group_num);