All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Horman <horms@kernel.org>
To: chuguangqing <chuguangqing@inspur.com>
Cc: Antonio Quartulli <antonio@openvpn.net>,
	Sabrina Dubroca <sd@queasysnail.net>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	davem@davemloft.net, Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/1] ovpn: use kmalloc_array() for array space allocation
Date: Tue, 2 Sep 2025 09:18:12 +0100	[thread overview]
Message-ID: <20250902081812.GQ15473@horms.kernel.org> (raw)
In-Reply-To: <20250901112136.2919-1-chuguangqing@inspur.com>

On Mon, Sep 01, 2025 at 07:21:36PM +0800, chuguangqing wrote:
> Replace kmalloc(size * sizeof) with kmalloc_array() for safer memory
> allocation and overflow prevention.
> 
> Signed-off-by: chuguangqing <chuguangqing@inspur.com>
> ---
>  drivers/net/ovpn/crypto_aead.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/net/ovpn/crypto_aead.c b/drivers/net/ovpn/crypto_aead.c
> index 2cca759feffa..8274c3ae8d0b 100644
> --- a/drivers/net/ovpn/crypto_aead.c
> +++ b/drivers/net/ovpn/crypto_aead.c
> @@ -72,8 +72,8 @@ int ovpn_aead_encrypt(struct ovpn_peer *peer, struct ovpn_crypto_key_slot *ks,
>  		return -ENOSPC;
>  
>  	/* sg may be required by async crypto */
> -	ovpn_skb_cb(skb)->sg = kmalloc(sizeof(*ovpn_skb_cb(skb)->sg) *
> -				       (nfrags + 2), GFP_ATOMIC);
> +	ovpn_skb_cb(skb)->sg = kmalloc_array((nfrags + 2), sizeof(*ovpn_skb_cb(skb)->sg),
> +					     GFP_ATOMIC);

I think this could benefit from:
a) Removal of unnecessary parentheses
b) Line wrapping to 80 columns wide or less,
   as is still preferred for Networking code

(Completely untested!)

	ovpn_skb_cb(skb)->sg = kmalloc_array(nfrags + 2,
					     sizeof(*ovpn_skb_cb(skb)->sg),
					     GFP_ATOMIC);

>  	if (unlikely(!ovpn_skb_cb(skb)->sg))
>  		return -ENOMEM;
>  
> @@ -185,8 +185,8 @@ int ovpn_aead_decrypt(struct ovpn_peer *peer, struct ovpn_crypto_key_slot *ks,
>  		return -ENOSPC;
>  
>  	/* sg may be required by async crypto */
> -	ovpn_skb_cb(skb)->sg = kmalloc(sizeof(*ovpn_skb_cb(skb)->sg) *
> -				       (nfrags + 2), GFP_ATOMIC);
> +	ovpn_skb_cb(skb)->sg = kmalloc_array((nfrags + 2), sizeof(*ovpn_skb_cb(skb)->sg),
> +					     GFP_ATOMIC);

Likewise here.

>  	if (unlikely(!ovpn_skb_cb(skb)->sg))
>  		return -ENOMEM;
>  

-- 
pw-bot: changes-requested

  reply	other threads:[~2025-09-02  8:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-01 11:21 [PATCH 1/1] ovpn: use kmalloc_array() for array space allocation chuguangqing
2025-09-02  8:18 ` Simon Horman [this message]
2025-09-02  9:00 ` PATCH v2 Re: " chuguangqing
2025-09-02  9:00   ` [PATCH v2 " chuguangqing
2025-09-02 13:15     ` Markus Elfring
2025-09-03 12:17       ` chuguangqing
2025-09-03 12:54         ` [v2] " Markus Elfring
2025-09-03  8:41     ` [PATCH v2 1/1] " Antonio Quartulli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250902081812.GQ15473@horms.kernel.org \
    --to=horms@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=antonio@openvpn.net \
    --cc=chuguangqing@inspur.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sd@queasysnail.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.