2020-04-23 20:48:27

by Christophe JAILLET

[permalink] [raw]
Subject: [PATCH] scsi: fnic: Use kmalloc instead of vmalloc for a small memory allocation

'struct fc_trace_flag_type' is just a few bytes long. There is no need
to allocate such a structure with vmalloc. Using kmalloc instead.

While at it, axe a useless test when freeing the memory.

Signed-off-by: Christophe JAILLET <[email protected]>
---
drivers/scsi/fnic/fnic_debugfs.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/scsi/fnic/fnic_debugfs.c b/drivers/scsi/fnic/fnic_debugfs.c
index 13f7d88d6e57..8d6ce3470594 100644
--- a/drivers/scsi/fnic/fnic_debugfs.c
+++ b/drivers/scsi/fnic/fnic_debugfs.c
@@ -58,8 +58,7 @@ int fnic_debugfs_init(void)
fnic_trace_debugfs_root);

/* Allocate memory to structure */
- fc_trc_flag = (struct fc_trace_flag_type *)
- vmalloc(sizeof(struct fc_trace_flag_type));
+ fc_trc_flag = kmalloc(sizeof(*fc_trc_flag), GFP_KERNEL);

if (fc_trc_flag) {
fc_trc_flag->fc_row_file = 0;
@@ -87,8 +86,7 @@ void fnic_debugfs_terminate(void)
debugfs_remove(fnic_trace_debugfs_root);
fnic_trace_debugfs_root = NULL;

- if (fc_trc_flag)
- vfree(fc_trc_flag);
+ kfree(fc_trc_flag);
}

/*
--
2.20.1


2020-04-24 09:36:08

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH] scsi: fnic: Use kmalloc instead of vmalloc for a small memory allocation

On Thu, Apr 23, 2020 at 10:46:20PM +0200, Christophe JAILLET wrote:
> 'struct fc_trace_flag_type' is just a few bytes long. There is no need
> to allocate such a structure with vmalloc. Using kmalloc instead.
>
> While at it, axe a useless test when freeing the memory.
>
> Signed-off-by: Christophe JAILLET <[email protected]>
> ---
> drivers/scsi/fnic/fnic_debugfs.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/scsi/fnic/fnic_debugfs.c b/drivers/scsi/fnic/fnic_debugfs.c
> index 13f7d88d6e57..8d6ce3470594 100644
> --- a/drivers/scsi/fnic/fnic_debugfs.c
> +++ b/drivers/scsi/fnic/fnic_debugfs.c
> @@ -58,8 +58,7 @@ int fnic_debugfs_init(void)
> fnic_trace_debugfs_root);
>
> /* Allocate memory to structure */
> - fc_trc_flag = (struct fc_trace_flag_type *)
> - vmalloc(sizeof(struct fc_trace_flag_type));
> + fc_trc_flag = kmalloc(sizeof(*fc_trc_flag), GFP_KERNEL);
>
> if (fc_trc_flag) {

I hate success handling... This test should be reversed so that we do
error handling. It should return -ENOMEM instead of 0 on allocation
failure, otherwise it leads to a NULL dereference down the road.
Although, of course in current kernel small size allocations like this
never fail in real life.

The other thing I wonder is if we should just replace the vmalloc()
implementation with kvmalloc(). IOW rename vmalloc() to __vmalloc() and
"#define vmalloc kvmalloc" (not literally). That was allocations of
less than a page would always be done with kmalloc().

regards,
dan carpenter

2020-04-24 14:52:25

by Markus Elfring

[permalink] [raw]
Subject: Re: [PATCH] scsi: fnic: Use kmalloc instead of vmalloc for a small memory allocation

> 'struct fc_trace_flag_type' is just a few bytes long. There is no need
> to allocate such a structure with vmalloc. Using kmalloc instead.

Can such an adjustment be relevant for the specification of the tag “Fixes”?


> While at it, axe a useless test when freeing the memory.

I find it more appropriate to provide this source code improvement
by a separate update step.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?id=b4f633221f0aeac102e463a4be46a643b2e3b819#n138

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/scripts/coccinelle/free/ifnullfree.cocci?id=b4f633221f0aeac102e463a4be46a643b2e3b819#n4

Regards,
Markus

2020-05-06 12:32:44

by Hannes Reinecke

[permalink] [raw]
Subject: Re: [PATCH] scsi: fnic: Use kmalloc instead of vmalloc for a small memory allocation

On 4/23/20 10:46 PM, Christophe JAILLET wrote:
> 'struct fc_trace_flag_type' is just a few bytes long. There is no need
> to allocate such a structure with vmalloc. Using kmalloc instead.
>
> While at it, axe a useless test when freeing the memory.
>
> Signed-off-by: Christophe JAILLET <[email protected]>
> ---
> drivers/scsi/fnic/fnic_debugfs.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/scsi/fnic/fnic_debugfs.c b/drivers/scsi/fnic/fnic_debugfs.c
> index 13f7d88d6e57..8d6ce3470594 100644
> --- a/drivers/scsi/fnic/fnic_debugfs.c
> +++ b/drivers/scsi/fnic/fnic_debugfs.c
> @@ -58,8 +58,7 @@ int fnic_debugfs_init(void)
> fnic_trace_debugfs_root);
>
> /* Allocate memory to structure */
> - fc_trc_flag = (struct fc_trace_flag_type *)
> - vmalloc(sizeof(struct fc_trace_flag_type));
> + fc_trc_flag = kmalloc(sizeof(*fc_trc_flag), GFP_KERNEL);
>
> if (fc_trc_flag) {
> fc_trc_flag->fc_row_file = 0;
> @@ -87,8 +86,7 @@ void fnic_debugfs_terminate(void)
> debugfs_remove(fnic_trace_debugfs_root);
> fnic_trace_debugfs_root = NULL;
>
> - if (fc_trc_flag)
> - vfree(fc_trc_flag);
> + kfree(fc_trc_flag);
> }
>
> /*
>
Reviewed-by: Hannes Reinecke <[email protected]>

Cheers,

Hannes
--
Dr. Hannes Reinecke Teamlead Storage & Networking
[email protected] +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer