All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/3] Staging: rtl8192e: Use struct_size
       [not found] <cover.1655909205.git.f3sch.git@outlook.com>
@ 2022-06-22 15:12 ` Felix Schlepper
  2022-06-22 15:12 ` [PATCH v3 2/3] Staging: rtl8192e: Using kzalloc and delete memset Felix Schlepper
  2022-06-22 15:12 ` [PATCH v3 3/3] Staging: rtl8192e: Cleaning up error handling Felix Schlepper
  2 siblings, 0 replies; 5+ messages in thread
From: Felix Schlepper @ 2022-06-22 15:12 UTC (permalink / raw)
  To: gregkh; +Cc: linux-staging, linux-kernel

Using struct_size is encouraged because it is safer
than using sizeof and calculations by hand.

Signed-off-by: Felix Schlepper <f3sch.git@outlook.com>
---
 drivers/staging/rtl8192e/rtllib_tx.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/staging/rtl8192e/rtllib_tx.c b/drivers/staging/rtl8192e/rtllib_tx.c
index 37715afb0210..f2ef32e943ae 100644
--- a/drivers/staging/rtl8192e/rtllib_tx.c
+++ b/drivers/staging/rtl8192e/rtllib_tx.c
@@ -205,8 +205,7 @@ static struct rtllib_txb *rtllib_alloc_txb(int nr_frags, int txb_size,
 	struct rtllib_txb *txb;
 	int i;
 
-	txb = kmalloc(sizeof(struct rtllib_txb) + (sizeof(u8 *) * nr_frags),
-		      gfp_mask);
+	txb = kmalloc(struct_size(txb, fragments, nr_frags), gfp_mask);
 	if (!txb)
 		return NULL;
 
-- 
2.36.1


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

* [PATCH v3 2/3] Staging: rtl8192e: Using kzalloc and delete memset
       [not found] <cover.1655909205.git.f3sch.git@outlook.com>
  2022-06-22 15:12 ` [PATCH v3 1/3] Staging: rtl8192e: Use struct_size Felix Schlepper
@ 2022-06-22 15:12 ` Felix Schlepper
  2022-06-23  7:05   ` Dan Carpenter
  2022-06-22 15:12 ` [PATCH v3 3/3] Staging: rtl8192e: Cleaning up error handling Felix Schlepper
  2 siblings, 1 reply; 5+ messages in thread
From: Felix Schlepper @ 2022-06-22 15:12 UTC (permalink / raw)
  To: gregkh; +Cc: linux-staging, linux-kernel

By using kzalloc, we can delete a memset and avoid
memory initialization defects.

Signed-off-by: Felix Schlepper <f3sch.git@outlook.com>
---
 drivers/staging/rtl8192e/rtllib_tx.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/staging/rtl8192e/rtllib_tx.c b/drivers/staging/rtl8192e/rtllib_tx.c
index f2ef32e943ae..1307cf55741a 100644
--- a/drivers/staging/rtl8192e/rtllib_tx.c
+++ b/drivers/staging/rtl8192e/rtllib_tx.c
@@ -205,11 +205,10 @@ static struct rtllib_txb *rtllib_alloc_txb(int nr_frags, int txb_size,
 	struct rtllib_txb *txb;
 	int i;
 
-	txb = kmalloc(struct_size(txb, fragments, nr_frags), gfp_mask);
+	txb = kzalloc(struct_size(txb, fragments, nr_frags), gfp_mask);
 	if (!txb)
 		return NULL;
 
-	memset(txb, 0, sizeof(struct rtllib_txb));
 	txb->nr_frags = nr_frags;
 	txb->frag_size = cpu_to_le16(txb_size);
 
-- 
2.36.1


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

* [PATCH v3 3/3] Staging: rtl8192e: Cleaning up error handling
       [not found] <cover.1655909205.git.f3sch.git@outlook.com>
  2022-06-22 15:12 ` [PATCH v3 1/3] Staging: rtl8192e: Use struct_size Felix Schlepper
  2022-06-22 15:12 ` [PATCH v3 2/3] Staging: rtl8192e: Using kzalloc and delete memset Felix Schlepper
@ 2022-06-22 15:12 ` Felix Schlepper
  2022-06-23  6:54   ` Dan Carpenter
  2 siblings, 1 reply; 5+ messages in thread
From: Felix Schlepper @ 2022-06-22 15:12 UTC (permalink / raw)
  To: gregkh; +Cc: linux-staging, linux-kernel

This error handling is more readable and reduces code size.

Signed-off-by: Felix Schlepper <f3sch.git@outlook.com>
---
 drivers/staging/rtl8192e/rtllib_tx.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/drivers/staging/rtl8192e/rtllib_tx.c b/drivers/staging/rtl8192e/rtllib_tx.c
index 1307cf55741a..b246f5e0cad7 100644
--- a/drivers/staging/rtl8192e/rtllib_tx.c
+++ b/drivers/staging/rtl8192e/rtllib_tx.c
@@ -214,19 +214,18 @@ static struct rtllib_txb *rtllib_alloc_txb(int nr_frags, int txb_size,
 
 	for (i = 0; i < nr_frags; i++) {
 		txb->fragments[i] = dev_alloc_skb(txb_size);
-		if (unlikely(!txb->fragments[i])) {
-			i--;
-			break;
-		}
+		if (unlikely(!txb->fragments[i]))
+			goto err_free;
 		memset(txb->fragments[i]->cb, 0, sizeof(txb->fragments[i]->cb));
 	}
-	if (unlikely(i != nr_frags)) {
-		while (i >= 0)
-			dev_kfree_skb_any(txb->fragments[i--]);
-		kfree(txb);
-		return NULL;
-	}
+
 	return txb;
+
+err_free:
+	while (--i >= 0)
+		dev_kfree_skb_any(txb->fragments[i]);
+
+	return NULL;
 }
 
 static int rtllib_classify(struct sk_buff *skb, u8 bIsAmsdu)
-- 
2.36.1


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

* Re: [PATCH v3 3/3] Staging: rtl8192e: Cleaning up error handling
  2022-06-22 15:12 ` [PATCH v3 3/3] Staging: rtl8192e: Cleaning up error handling Felix Schlepper
@ 2022-06-23  6:54   ` Dan Carpenter
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2022-06-23  6:54 UTC (permalink / raw)
  To: Felix Schlepper; +Cc: gregkh, linux-staging, linux-kernel

On Wed, Jun 22, 2022 at 05:12:46PM +0200, Felix Schlepper wrote:
> This error handling is more readable and reduces code size.

There isn't a reduction in code size.

> 
> Signed-off-by: Felix Schlepper <f3sch.git@outlook.com>
> ---
>  drivers/staging/rtl8192e/rtllib_tx.c | 19 +++++++++----------
>  1 file changed, 9 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/staging/rtl8192e/rtllib_tx.c b/drivers/staging/rtl8192e/rtllib_tx.c
> index 1307cf55741a..b246f5e0cad7 100644
> --- a/drivers/staging/rtl8192e/rtllib_tx.c
> +++ b/drivers/staging/rtl8192e/rtllib_tx.c
> @@ -214,19 +214,18 @@ static struct rtllib_txb *rtllib_alloc_txb(int nr_frags, int txb_size,
>  
>  	for (i = 0; i < nr_frags; i++) {
>  		txb->fragments[i] = dev_alloc_skb(txb_size);
> -		if (unlikely(!txb->fragments[i])) {
> -			i--;
> -			break;
> -		}
> +		if (unlikely(!txb->fragments[i]))
> +			goto err_free;
>  		memset(txb->fragments[i]->cb, 0, sizeof(txb->fragments[i]->cb));
>  	}
> -	if (unlikely(i != nr_frags)) {
> -		while (i >= 0)
> -			dev_kfree_skb_any(txb->fragments[i--]);
> -		kfree(txb);
> -		return NULL;
> -	}
> +
>  	return txb;
> +
> +err_free:
> +	while (--i >= 0)
> +		dev_kfree_skb_any(txb->fragments[i]);

There needs to be a kfree(txb);

regards,
dan carpenter


> +
> +	return NULL;
>  }
>  


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

* Re: [PATCH v3 2/3] Staging: rtl8192e: Using kzalloc and delete memset
  2022-06-22 15:12 ` [PATCH v3 2/3] Staging: rtl8192e: Using kzalloc and delete memset Felix Schlepper
@ 2022-06-23  7:05   ` Dan Carpenter
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2022-06-23  7:05 UTC (permalink / raw)
  To: Felix Schlepper; +Cc: gregkh, linux-staging, linux-kernel

On Wed, Jun 22, 2022 at 05:12:45PM +0200, Felix Schlepper wrote:
> By using kzalloc, we can delete a memset and avoid
> memory initialization defects.
>

This commit message is slightly confusing.  The "avoid memory
initialization defects" bit is very vague.

The difference between the original code and patched code is that the
patched code zeroes out the txb->fragments[] array.  The original code
worked.  The new code is slightly cleaner, but also slower.  Whatever,
either way is fine.

But just state it clearly in the commit message.

  "By using kzalloc, we can delete a memset.  The practical difference
   is that using kzalloc() will zero out the txb->fragments[] array.
   The original code worked fine, but zeroing everything seems nicer."

regards,
dan carpenter


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

end of thread, other threads:[~2022-06-23  7:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <cover.1655909205.git.f3sch.git@outlook.com>
2022-06-22 15:12 ` [PATCH v3 1/3] Staging: rtl8192e: Use struct_size Felix Schlepper
2022-06-22 15:12 ` [PATCH v3 2/3] Staging: rtl8192e: Using kzalloc and delete memset Felix Schlepper
2022-06-23  7:05   ` Dan Carpenter
2022-06-22 15:12 ` [PATCH v3 3/3] Staging: rtl8192e: Cleaning up error handling Felix Schlepper
2022-06-23  6:54   ` Dan Carpenter

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.