* [PATCH] tools/virtio: Use the __GFP_ZERO flag of kmalloc to complete the memory initialization.
@ 2024-06-05 13:52 cuitao
2024-06-06 0:29 ` Jason Wang
2024-06-06 7:18 ` Michael S. Tsirkin
0 siblings, 2 replies; 4+ messages in thread
From: cuitao @ 2024-06-05 13:52 UTC (permalink / raw)
To: mst, jasowang, virtualization; +Cc: xuanzhuo, eperezma, linux-kernel, cuitao
Use the __GFP_ZERO flag of kmalloc to initialize memory while allocating it,
without the need for an additional memset call.
Signed-off-by: cuitao <cuitao@kylinos.cn>
---
tools/virtio/linux/kernel.h | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/tools/virtio/linux/kernel.h b/tools/virtio/linux/kernel.h
index 6702008f7f5c..9e401fb7c215 100644
--- a/tools/virtio/linux/kernel.h
+++ b/tools/virtio/linux/kernel.h
@@ -66,10 +66,7 @@ static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp)
static inline void *kzalloc(size_t s, gfp_t gfp)
{
- void *p = kmalloc(s, gfp);
-
- memset(p, 0, s);
- return p;
+ return kmalloc(s, gfp | __GFP_ZERO);
}
static inline void *alloc_pages_exact(size_t s, gfp_t gfp)
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] tools/virtio: Use the __GFP_ZERO flag of kmalloc to complete the memory initialization.
2024-06-05 13:52 [PATCH] tools/virtio: Use the __GFP_ZERO flag of kmalloc to complete the memory initialization cuitao
@ 2024-06-06 0:29 ` Jason Wang
2024-06-06 7:18 ` Michael S. Tsirkin
1 sibling, 0 replies; 4+ messages in thread
From: Jason Wang @ 2024-06-06 0:29 UTC (permalink / raw)
To: cuitao; +Cc: mst, virtualization, xuanzhuo, eperezma, linux-kernel
On Wed, Jun 5, 2024 at 9:56 PM cuitao <cuitao@kylinos.cn> wrote:
>
> Use the __GFP_ZERO flag of kmalloc to initialize memory while allocating it,
> without the need for an additional memset call.
>
> Signed-off-by: cuitao <cuitao@kylinos.cn>
> ---
> tools/virtio/linux/kernel.h | 5 +----
> 1 file changed, 1 insertion(+), 4 deletions(-)
>
> diff --git a/tools/virtio/linux/kernel.h b/tools/virtio/linux/kernel.h
> index 6702008f7f5c..9e401fb7c215 100644
> --- a/tools/virtio/linux/kernel.h
> +++ b/tools/virtio/linux/kernel.h
> @@ -66,10 +66,7 @@ static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp)
>
> static inline void *kzalloc(size_t s, gfp_t gfp)
> {
> - void *p = kmalloc(s, gfp);
> -
> - memset(p, 0, s);
> - return p;
> + return kmalloc(s, gfp | __GFP_ZERO);
> }
>
> static inline void *alloc_pages_exact(size_t s, gfp_t gfp)
> --
> 2.25.1
>
Does this really work?
extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
static inline void *kmalloc(size_t s, gfp_t gfp)
{
if (__kmalloc_fake)
return __kmalloc_fake;
return malloc(s);
}
Thanks
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] tools/virtio: Use the __GFP_ZERO flag of kmalloc to complete the memory initialization.
2024-06-05 13:52 [PATCH] tools/virtio: Use the __GFP_ZERO flag of kmalloc to complete the memory initialization cuitao
2024-06-06 0:29 ` Jason Wang
@ 2024-06-06 7:18 ` Michael S. Tsirkin
2024-06-06 7:25 ` cuitao
1 sibling, 1 reply; 4+ messages in thread
From: Michael S. Tsirkin @ 2024-06-06 7:18 UTC (permalink / raw)
To: cuitao; +Cc: jasowang, virtualization, xuanzhuo, eperezma, linux-kernel
On Wed, Jun 05, 2024 at 09:52:45PM +0800, cuitao wrote:
> Use the __GFP_ZERO flag of kmalloc to initialize memory while allocating it,
> without the need for an additional memset call.
>
> Signed-off-by: cuitao <cuitao@kylinos.cn>
> ---
> tools/virtio/linux/kernel.h | 5 +----
> 1 file changed, 1 insertion(+), 4 deletions(-)
>
> diff --git a/tools/virtio/linux/kernel.h b/tools/virtio/linux/kernel.h
> index 6702008f7f5c..9e401fb7c215 100644
> --- a/tools/virtio/linux/kernel.h
> +++ b/tools/virtio/linux/kernel.h
> @@ -66,10 +66,7 @@ static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp)
>
> static inline void *kzalloc(size_t s, gfp_t gfp)
> {
> - void *p = kmalloc(s, gfp);
> -
> - memset(p, 0, s);
> - return p;
> + return kmalloc(s, gfp | __GFP_ZERO);
> }
Why do we care? It's just here to make things compile. The simpler the
better.
> static inline void *alloc_pages_exact(size_t s, gfp_t gfp)
> --
> 2.25.1
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] tools/virtio: Use the __GFP_ZERO flag of kmalloc to complete the memory initialization.
2024-06-06 7:18 ` Michael S. Tsirkin
@ 2024-06-06 7:25 ` cuitao
0 siblings, 0 replies; 4+ messages in thread
From: cuitao @ 2024-06-06 7:25 UTC (permalink / raw)
To: Michael S. Tsirkin
Cc: jasowang, virtualization, xuanzhuo, eperezma, linux-kernel
Sorry, maybe I'm wrong.
I wonder if the gfp parameter in static inline void *kmalloc(size_t s,
gfp_t gfp) can be deleted if it is not used.
Or would be better to move memset to kmalloc.
Like this:
#define __GFP_ZERO 0x1
static inline void *kmalloc(size_t s, gfp_t gfp)
{
void *p;
if (__kmalloc_fake)
return __kmalloc_fake;
p = malloc(s);
if (!p)
return p;
if (gfp & __GFP_ZERO)
memset(p, 0, s);
return p;
}
在 2024/6/6 15:18, Michael S. Tsirkin 写道:
> On Wed, Jun 05, 2024 at 09:52:45PM +0800, cuitao wrote:
>> Use the __GFP_ZERO flag of kmalloc to initialize memory while allocating it,
>> without the need for an additional memset call.
>>
>> Signed-off-by: cuitao <cuitao@kylinos.cn>
>> ---
>> tools/virtio/linux/kernel.h | 5 +----
>> 1 file changed, 1 insertion(+), 4 deletions(-)
>>
>> diff --git a/tools/virtio/linux/kernel.h b/tools/virtio/linux/kernel.h
>> index 6702008f7f5c..9e401fb7c215 100644
>> --- a/tools/virtio/linux/kernel.h
>> +++ b/tools/virtio/linux/kernel.h
>> @@ -66,10 +66,7 @@ static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp)
>>
>> static inline void *kzalloc(size_t s, gfp_t gfp)
>> {
>> - void *p = kmalloc(s, gfp);
>> -
>> - memset(p, 0, s);
>> - return p;
>> + return kmalloc(s, gfp | __GFP_ZERO);
>> }
>
> Why do we care? It's just here to make things compile. The simpler the
> better.
>
>> static inline void *alloc_pages_exact(size_t s, gfp_t gfp)
>> --
>> 2.25.1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-06-06 7:25 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-05 13:52 [PATCH] tools/virtio: Use the __GFP_ZERO flag of kmalloc to complete the memory initialization cuitao
2024-06-06 0:29 ` Jason Wang
2024-06-06 7:18 ` Michael S. Tsirkin
2024-06-06 7:25 ` cuitao
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox