diff mbox series

[net-next,4/4] net/tcp_ao: use sock_kmemdup for tcp_ao_key

Message ID 38054b456a54cc5c7628c81a42816a770f0bff27.1740643844.git.tanggeliang@kylinos.cn (mailing list archive)
State Superseded, archived
Delegated to: Matthieu Baerts
Headers show
Series add sock_kmemdup helper | expand

Checks

Context Check Description
matttbe/build success Build and static analysis OK
matttbe/checkpatch success total: 0 errors, 0 warnings, 0 checks, 13 lines checked
matttbe/shellcheck success MPTCP selftests files have not been modified
matttbe/KVM_Validation__normal success Success! ✅
matttbe/KVM_Validation__debug success Success! ✅
matttbe/KVM_Validation__btf-normal__only_bpftest_all_ success Success! ✅
matttbe/KVM_Validation__btf-debug__only_bpftest_all_ success Success! ✅

Commit Message

Geliang Tang Feb. 27, 2025, 8:23 a.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

Instead of using sock_kmalloc() to allocate a tcp_ao_key "new_key" and
then immediately duplicate the input "key" to it in tcp_ao_copy_key(),
the newly added sock_kmemdup() helper can be used to simplify the code.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 net/ipv4/tcp_ao.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Eric Dumazet Feb. 27, 2025, 8:35 a.m. UTC | #1
On Thu, Feb 27, 2025 at 9:24 AM Geliang Tang <geliang@kernel.org> wrote:
>
> From: Geliang Tang <tanggeliang@kylinos.cn>
>
> Instead of using sock_kmalloc() to allocate a tcp_ao_key "new_key" and
> then immediately duplicate the input "key" to it in tcp_ao_copy_key(),
> the newly added sock_kmemdup() helper can be used to simplify the code.
>
> Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> ---
>  net/ipv4/tcp_ao.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c
> index bbb8d5f0eae7..d21412d469cc 100644
> --- a/net/ipv4/tcp_ao.c
> +++ b/net/ipv4/tcp_ao.c
> @@ -246,12 +246,11 @@ static struct tcp_ao_key *tcp_ao_copy_key(struct sock *sk,
>  {
>         struct tcp_ao_key *new_key;
>
> -       new_key = sock_kmalloc(sk, tcp_ao_sizeof_key(key),
> +       new_key = sock_kmemdup(sk, key, tcp_ao_sizeof_key(key),
>                                GFP_ATOMIC);
>         if (!new_key)
>                 return NULL;
>
> -       *new_key = *key;

Note that this only copies 'sizeof(struct tcp_ao_key)' bytes, which is
smaller than tcp_ao_sizeof_key(key)
Geliang Tang Feb. 27, 2025, 9:04 a.m. UTC | #2
On Thu, 2025-02-27 at 09:35 +0100, Eric Dumazet wrote:
> On Thu, Feb 27, 2025 at 9:24 AM Geliang Tang <geliang@kernel.org>
> wrote:
> > 
> > From: Geliang Tang <tanggeliang@kylinos.cn>
> > 
> > Instead of using sock_kmalloc() to allocate a tcp_ao_key "new_key"
> > and
> > then immediately duplicate the input "key" to it in
> > tcp_ao_copy_key(),
> > the newly added sock_kmemdup() helper can be used to simplify the
> > code.
> > 
> > Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> > ---
> >  net/ipv4/tcp_ao.c | 3 +--
> >  1 file changed, 1 insertion(+), 2 deletions(-)
> > 
> > diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c
> > index bbb8d5f0eae7..d21412d469cc 100644
> > --- a/net/ipv4/tcp_ao.c
> > +++ b/net/ipv4/tcp_ao.c
> > @@ -246,12 +246,11 @@ static struct tcp_ao_key
> > *tcp_ao_copy_key(struct sock *sk,
> >  {
> >         struct tcp_ao_key *new_key;
> > 
> > -       new_key = sock_kmalloc(sk, tcp_ao_sizeof_key(key),
> > +       new_key = sock_kmemdup(sk, key, tcp_ao_sizeof_key(key),
> >                                GFP_ATOMIC);
> >         if (!new_key)
> >                 return NULL;
> > 
> > -       *new_key = *key;
> 
> Note that this only copies 'sizeof(struct tcp_ao_key)' bytes, which
> is
> smaller than tcp_ao_sizeof_key(key)

Yes, indeed. sock_kmemdup() shouldn't be used here then. I'll drop this
patch in v2.

Thanks,
-Geliang
diff mbox series

Patch

diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c
index bbb8d5f0eae7..d21412d469cc 100644
--- a/net/ipv4/tcp_ao.c
+++ b/net/ipv4/tcp_ao.c
@@ -246,12 +246,11 @@  static struct tcp_ao_key *tcp_ao_copy_key(struct sock *sk,
 {
 	struct tcp_ao_key *new_key;
 
-	new_key = sock_kmalloc(sk, tcp_ao_sizeof_key(key),
+	new_key = sock_kmemdup(sk, key, tcp_ao_sizeof_key(key),
 			       GFP_ATOMIC);
 	if (!new_key)
 		return NULL;
 
-	*new_key = *key;
 	INIT_HLIST_NODE(&new_key->node);
 	tcp_sigpool_get(new_key->tcp_sigpool_id);
 	atomic64_set(&new_key->pkt_good, 0);