linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] ovpn: use kmalloc_array() for array space allocation
@ 2025-09-01 11:21 chuguangqing
  2025-09-02  8:18 ` Simon Horman
  2025-09-02  9:00 ` PATCH v2 Re: " chuguangqing
  0 siblings, 2 replies; 9+ messages in thread
From: chuguangqing @ 2025-09-01 11:21 UTC (permalink / raw)
  To: Antonio Quartulli, Sabrina Dubroca, Andrew Lunn, davem,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: netdev, linux-kernel, chuguangqing

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);
 	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);
 	if (unlikely(!ovpn_skb_cb(skb)->sg))
 		return -ENOMEM;
 
-- 
2.43.5


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/1] ovpn: use kmalloc_array() for array space allocation
  2025-09-01 11:21 [PATCH 1/1] ovpn: use kmalloc_array() for array space allocation chuguangqing
@ 2025-09-02  8:18 ` Simon Horman
  2025-09-02  9:00 ` PATCH v2 Re: " chuguangqing
  1 sibling, 0 replies; 9+ messages in thread
From: Simon Horman @ 2025-09-02  8:18 UTC (permalink / raw)
  To: chuguangqing
  Cc: Antonio Quartulli, Sabrina Dubroca, Andrew Lunn, davem,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, linux-kernel

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* PATCH v2 Re: Re: [PATCH 1/1] ovpn: use kmalloc_array() for array space allocation
  2025-09-01 11:21 [PATCH 1/1] ovpn: use kmalloc_array() for array space allocation chuguangqing
  2025-09-02  8:18 ` Simon Horman
@ 2025-09-02  9:00 ` chuguangqing
  2025-09-02  9:00   ` [PATCH v2 " chuguangqing
  1 sibling, 1 reply; 9+ messages in thread
From: chuguangqing @ 2025-09-02  9:00 UTC (permalink / raw)
  To: horms, Antonio Quartulli, Sabrina Dubroca, Andrew Lunn, davem,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: netdev, linux-kernel, chuguangqing

v1 -> v2:
Unnecessary parentheses have been removed and line alignment has been adjusted to 80 characters in width.

> 
> 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


chuguangqing (1):
  ovpn: use kmalloc_array() for array space allocation

 drivers/net/ovpn/crypto_aead.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

-- 
2.43.5


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 1/1] ovpn: use kmalloc_array() for array space allocation
  2025-09-02  9:00 ` PATCH v2 Re: " chuguangqing
@ 2025-09-02  9:00   ` chuguangqing
  2025-09-02 13:15     ` Markus Elfring
  2025-09-03  8:41     ` [PATCH v2 1/1] " Antonio Quartulli
  0 siblings, 2 replies; 9+ messages in thread
From: chuguangqing @ 2025-09-02  9:00 UTC (permalink / raw)
  To: horms, Antonio Quartulli, Sabrina Dubroca, Andrew Lunn, davem,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: netdev, linux-kernel, chuguangqing

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 | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/net/ovpn/crypto_aead.c b/drivers/net/ovpn/crypto_aead.c
index 2cca759feffa..da0ce4f348e6 100644
--- a/drivers/net/ovpn/crypto_aead.c
+++ b/drivers/net/ovpn/crypto_aead.c
@@ -72,8 +72,9 @@ 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);
 	if (unlikely(!ovpn_skb_cb(skb)->sg))
 		return -ENOMEM;
 
@@ -185,8 +186,9 @@ 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);
 	if (unlikely(!ovpn_skb_cb(skb)->sg))
 		return -ENOMEM;
 
-- 
2.43.5


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/1] ovpn: use kmalloc_array() for array space allocation
  2025-09-02  9:00   ` [PATCH v2 " chuguangqing
@ 2025-09-02 13:15     ` Markus Elfring
  2025-09-03 12:17       ` chuguangqing
  2025-09-03  8:41     ` [PATCH v2 1/1] " Antonio Quartulli
  1 sibling, 1 reply; 9+ messages in thread
From: Markus Elfring @ 2025-09-02 13:15 UTC (permalink / raw)
  To: chuguangqing, netdev
  Cc: LKML, Andrew Lunn, Antonio Quartulli, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Sabrina Dubroca,
	Simon Horman

> Signed-off-by: chuguangqing <chuguangqing@inspur.com>

Would the personal name usually deviate a bit from the email identifier
according to the Developer's Certificate of Origin?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.17-rc4#n436

Regards,
Markus

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/1] ovpn: use kmalloc_array() for array space allocation
  2025-09-02  9:00   ` [PATCH v2 " chuguangqing
  2025-09-02 13:15     ` Markus Elfring
@ 2025-09-03  8:41     ` Antonio Quartulli
  1 sibling, 0 replies; 9+ messages in thread
From: Antonio Quartulli @ 2025-09-03  8:41 UTC (permalink / raw)
  To: chuguangqing
  Cc: netdev, linux-kernel, horms, Sabrina Dubroca, Andrew Lunn, davem,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni

Hello and thanks a lot for contribution!

This is indeed a nice improvement, however, we've been working on 
changing this part of the code.

We already have a pending commit in the queue (to be sent to net-next 
soonish):

https://github.com/mandelbitdev/ovpn-net-next/commit/9b62844193de705502fdf4a693dfe0f6c7d94f13

With the mentioned patch, the line you are changing does not exist 
anymore, therefore I don't think it makes sense to merge your patch 
first and then removing it again in the next commit.

Sorry about this!

Please feel free to stick around and submit more patches in the future!

Regards,


-- 
Antonio Quartulli
OpenVPN Inc.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/1] ovpn: use kmalloc_array() for array space allocation
@ 2025-09-03 12:00 Gary Chu(楚光庆)
  0 siblings, 0 replies; 9+ messages in thread
From: Gary Chu(楚光庆) @ 2025-09-03 12:00 UTC (permalink / raw)
  To: Markus.Elfring@web.de, netdev@vger.kernel.org
  Cc: linux-kernel@vger.kernel.org, andrew+netdev@lunn.ch,
	antonio@openvpn.net, davem@davemloft.net, edumazet@google.com,
	kuba@kernel.org, pabeni@redhat.com, sd@queasysnail.net,
	horms@kernel.org

[-- Attachment #1: Type: text/plain, Size: 804 bytes --]

Hi Markus,
	First of all, thank you for your reminder. According to your suggestion, the format "Chu Guangqing" should be used. However, in line with our company's signature conventions and my previous contributions to the kernel community, I have been using "chuguangqing". Therefore, I have to continue using this signature. The signature should not be changed frequently.

> 
> > Signed-off-by: chuguangqing <chuguangqing@inspur.com>
> 
> Would the personal name usually deviate a bit from the email identifier
> according to the Developer's Certificate of Origin?
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Docume
> ntation/process/submitting-patches.rst?h=v6.17-rc4#n436
> 
> Regards,
> Markus

Best regards,
Chu Guangqing
<chuguangqing@inspur.com>

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 3931 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/1] ovpn: use kmalloc_array() for array space allocation
  2025-09-02 13:15     ` Markus Elfring
@ 2025-09-03 12:17       ` chuguangqing
  2025-09-03 12:54         ` [v2] " Markus Elfring
  0 siblings, 1 reply; 9+ messages in thread
From: chuguangqing @ 2025-09-03 12:17 UTC (permalink / raw)
  To: markus.elfring
  Cc: andrew+netdev, antonio, chuguangqing, davem, edumazet, horms,
	kuba, linux-kernel, netdev, pabeni, sd

Hi Markus,
	First of all, thank you for your reminder. According to your suggestion, the format "Chu Guangqing" should be used. However, in line with our company's signature conventions and my previous contributions to the kernel community, I have been using "chuguangqing". Therefore, I have to continue using this signature. The signature should not be changed frequently.

> 
> > Signed-off-by: chuguangqing <chuguangqing@inspur.com>
> 
> Would the personal name usually deviate a bit from the email identifier
> according to the Developer's Certificate of Origin?
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Docume
> ntation/process/submitting-patches.rst?h=v6.17-rc4#n436
> 
> Regards,
> Markus

Best regards,
Chu Guangqing
<chuguangqing@inspur.com>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [v2] ovpn: use kmalloc_array() for array space allocation
  2025-09-03 12:17       ` chuguangqing
@ 2025-09-03 12:54         ` Markus Elfring
  0 siblings, 0 replies; 9+ messages in thread
From: Markus Elfring @ 2025-09-03 12:54 UTC (permalink / raw)
  To: chuguangqing, netdev
  Cc: LKML, Andrew Lunn, Antonio Quartulli, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Sabrina Dubroca,
	Simon Horman

> According to your suggestion, the format "Chu Guangqing" should be used.
…

Would you like to acknowledge that your personal name is actually different from
the shown email identifier?


…> The signature should not be changed frequently.

I imagine that some Linux contributors would appreciate an appropriate Developer's Certificate of Origin.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.17-rc4#n436

Regards,
Markus

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-09-03 12:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-01 11:21 [PATCH 1/1] ovpn: use kmalloc_array() for array space allocation chuguangqing
2025-09-02  8:18 ` Simon Horman
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
  -- strict thread matches above, loose matches on Subject: below --
2025-09-03 12:00 Gary Chu(楚光庆)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).