[PATCH 2/5] staging: rtl8723bs: replace kmalloc + memset with kzalloc

Erick Karanja posted 5 patches 8 months, 3 weeks ago
There is a newer version of this series
[PATCH 2/5] staging: rtl8723bs: replace kmalloc + memset with kzalloc
Posted by Erick Karanja 8 months, 3 weeks ago
Replace a call to kmalloc followed by memset with a single call to
kzalloc, which both allocates memory and zeroes it in one step.

This change improves readability and reduces redundant code.

Signed-off-by: Erick Karanja <karanja99erick@gmail.com>
---
 drivers/staging/rtl8723bs/os_dep/osdep_service.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/drivers/staging/rtl8723bs/os_dep/osdep_service.c b/drivers/staging/rtl8723bs/os_dep/osdep_service.c
index a00f9f0c85c5..be46132a533a 100644
--- a/drivers/staging/rtl8723bs/os_dep/osdep_service.c
+++ b/drivers/staging/rtl8723bs/os_dep/osdep_service.c
@@ -24,12 +24,7 @@ void *_rtw_malloc(u32 sz)
 
 void *_rtw_zmalloc(u32 sz)
 {
-	void *pbuf = _rtw_malloc(sz);
-
-	if (pbuf)
-		memset(pbuf, 0, sz);
-
-	return pbuf;
+	return kzalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
 }
 
 inline struct sk_buff *_rtw_skb_alloc(u32 sz)
-- 
2.43.0
Re: [PATCH 2/5] staging: rtl8723bs: replace kmalloc + memset with kzalloc
Posted by Julia Lawall 8 months, 3 weeks ago

On Mon, 31 Mar 2025, Erick Karanja wrote:

> Replace a call to kmalloc followed by memset with a single call to
> kzalloc, which both allocates memory and zeroes it in one step.
>
> This change improves readability and reduces redundant code.
>
> Signed-off-by: Erick Karanja <karanja99erick@gmail.com>
> ---
>  drivers/staging/rtl8723bs/os_dep/osdep_service.c | 7 +------
>  1 file changed, 1 insertion(+), 6 deletions(-)
>
> diff --git a/drivers/staging/rtl8723bs/os_dep/osdep_service.c b/drivers/staging/rtl8723bs/os_dep/osdep_service.c
> index a00f9f0c85c5..be46132a533a 100644
> --- a/drivers/staging/rtl8723bs/os_dep/osdep_service.c
> +++ b/drivers/staging/rtl8723bs/os_dep/osdep_service.c
> @@ -24,12 +24,7 @@ void *_rtw_malloc(u32 sz)
>
>  void *_rtw_zmalloc(u32 sz)
>  {
> -	void *pbuf = _rtw_malloc(sz);
> -
> -	if (pbuf)
> -		memset(pbuf, 0, sz);
> -
> -	return pbuf;
> +	return kzalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
>  }

So _rtw_malloc calls kmalloc?

How about gettind rid of both _rtw_malloc and _rtw_zmalloc and calling
kmalloc and kzalloc instead?  You should try to figure out if GFP_ATOMIC
or GFP_KERNEL should be used based on the context.  If you search for
those constants, you should be able to find some explanations about when
they should be used.

julia

>
>  inline struct sk_buff *_rtw_skb_alloc(u32 sz)
> --
> 2.43.0
>
>
>