linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scsi: fnic: Use kmalloc instead of vmalloc for a small memory allocation
@ 2020-04-23 20:46 Christophe JAILLET
  2020-04-24  9:30 ` Dan Carpenter
  2020-05-06 12:27 ` Hannes Reinecke
  0 siblings, 2 replies; 4+ messages in thread
From: Christophe JAILLET @ 2020-04-23 20:46 UTC (permalink / raw)
  To: satishkh, sebaddel, kartilak, jejb, martin.petersen
  Cc: linux-scsi, linux-kernel, kernel-janitors, Christophe JAILLET

'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 <christophe.jaillet@wanadoo.fr>
---
 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


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

* Re: [PATCH] scsi: fnic: Use kmalloc instead of vmalloc for a small memory allocation
  2020-04-23 20:46 [PATCH] scsi: fnic: Use kmalloc instead of vmalloc for a small memory allocation Christophe JAILLET
@ 2020-04-24  9:30 ` Dan Carpenter
  2020-05-06 12:27 ` Hannes Reinecke
  1 sibling, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2020-04-24  9:30 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: satishkh, sebaddel, kartilak, jejb, martin.petersen, linux-scsi,
	linux-kernel, kernel-janitors

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 <christophe.jaillet@wanadoo.fr>
> ---
>  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


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

* Re: [PATCH] scsi: fnic: Use kmalloc instead of vmalloc for a small memory allocation
  2020-04-23 20:46 [PATCH] scsi: fnic: Use kmalloc instead of vmalloc for a small memory allocation Christophe JAILLET
  2020-04-24  9:30 ` Dan Carpenter
@ 2020-05-06 12:27 ` Hannes Reinecke
  1 sibling, 0 replies; 4+ messages in thread
From: Hannes Reinecke @ 2020-05-06 12:27 UTC (permalink / raw)
  To: Christophe JAILLET, satishkh, sebaddel, kartilak, jejb, martin.petersen
  Cc: linux-scsi, linux-kernel, kernel-janitors

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 <christophe.jaillet@wanadoo.fr>
> ---
>   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 <har@suse.de>

Cheers,

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

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

* Re: [PATCH] scsi: fnic: Use kmalloc instead of vmalloc for a small memory allocation
@ 2020-04-24 14:50 Markus Elfring
  0 siblings, 0 replies; 4+ messages in thread
From: Markus Elfring @ 2020-04-24 14:50 UTC (permalink / raw)
  To: Christophe Jaillet, linux-scsi
  Cc: kernel-janitors, linux-kernel, Dan Carpenter,
	James E. J. Bottomley, Karan Tilak Kumar, Martin K. Petersen,
	Satish Kharat, Sesidhar Baddela

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

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

end of thread, other threads:[~2020-05-06 12:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-23 20:46 [PATCH] scsi: fnic: Use kmalloc instead of vmalloc for a small memory allocation Christophe JAILLET
2020-04-24  9:30 ` Dan Carpenter
2020-05-06 12:27 ` Hannes Reinecke
2020-04-24 14:50 Markus Elfring

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