mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Vasyl Gomonovych <gomonovych@gmail.com>,
	satishkh@cisco.com, sebaddel@cisco.com, kartilak@cisco.com,
	jejb@linux.ibm.com, martin.petersen@oracle.com,
	linux-scsi@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] fnic: remove casting vmalloc
Date: Thu, 18 Jul 2019 12:36:44 -0700	[thread overview]
Message-ID: <5cce2a9f73f6bf5ee55c576f98a28b04fdbe05d6.camel@perches.com> (raw)
In-Reply-To: <20190718190235.8089-1-gomonovych@gmail.com>

On Thu, 2019-07-18 at 21:02 +0200, Vasyl Gomonovych wrote:
> Generated by:  alloc_cast.cocci

Please look deeper than just the cocci output.

> diff --git a/drivers/scsi/fnic/fnic_debugfs.c b/drivers/scsi/fnic/fnic_debugfs.c
[]
> @@ -59,8 +59,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 = vmalloc(sizeof(struct fc_trace_flag_type));
>  
>  	if (fc_trc_flag) {
>  		fc_trc_flag->fc_row_file = 0;

Not your fault, but this code is not well written.

struct fc_trace_flag_type is:

drivers/scsi/fnic/fnic_debugfs.c:struct fc_trace_flag_type {
drivers/scsi/fnic/fnic_debugfs.c-       u8 fc_row_file;
drivers/scsi/fnic/fnic_debugfs.c-       u8 fc_normal_file;
drivers/scsi/fnic/fnic_debugfs.c-       u8 fnic_trace;
drivers/scsi/fnic/fnic_debugfs.c-       u8 fc_trace;
drivers/scsi/fnic/fnic_debugfs.c-       u8 fc_clear;
drivers/scsi/fnic/fnic_debugfs.c-};

It doesn't require vmalloc as it's just 5 bytes in size.
Might as well do either of allocate it with kmalloc
or because it's a singleton, just create a static.

Maybe:
---
 drivers/scsi/fnic/fnic_debugfs.c | 55 ++++++++++++++++------------------------
 1 file changed, 22 insertions(+), 33 deletions(-)

diff --git a/drivers/scsi/fnic/fnic_debugfs.c b/drivers/scsi/fnic/fnic_debugfs.c
index 21991c99db7c..72071bd4c9a1 100644
--- a/drivers/scsi/fnic/fnic_debugfs.c
+++ b/drivers/scsi/fnic/fnic_debugfs.c
@@ -31,15 +31,13 @@ static struct dentry *fnic_fc_rdata_trace_debugfs_file;
 static struct dentry *fnic_fc_trace_enable;
 static struct dentry *fnic_fc_trace_clear;
 
-struct fc_trace_flag_type {
+static struct {
 	u8 fc_row_file;
 	u8 fc_normal_file;
 	u8 fnic_trace;
 	u8 fc_trace;
 	u8 fc_clear;
-};
-
-static struct fc_trace_flag_type *fc_trc_flag;
+} fc_trc_flag;
 
 /*
  * fnic_debugfs_init - Initialize debugfs for fnic debug logging
@@ -52,26 +50,18 @@ static struct fc_trace_flag_type *fc_trc_flag;
  */
 int fnic_debugfs_init(void)
 {
-	int rc = -1;
 	fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
 
 	fnic_stats_debugfs_root = debugfs_create_dir("statistics",
 						fnic_trace_debugfs_root);
 
-	/* Allocate memory to structure */
-	fc_trc_flag = (struct fc_trace_flag_type *)
-		vmalloc(sizeof(struct fc_trace_flag_type));
-
-	if (fc_trc_flag) {
-		fc_trc_flag->fc_row_file = 0;
-		fc_trc_flag->fc_normal_file = 1;
-		fc_trc_flag->fnic_trace = 2;
-		fc_trc_flag->fc_trace = 3;
-		fc_trc_flag->fc_clear = 4;
-	}
+	fc_trc_flag.fc_row_file = 0;
+	fc_trc_flag.fc_normal_file = 1;
+	fc_trc_flag.fnic_trace = 2;
+	fc_trc_flag.fc_trace = 3;
+	fc_trc_flag.fc_clear = 4;
 
-	rc = 0;
-	return rc;
+	return 0;
 }
 
 /*
@@ -89,8 +79,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);
+	memset(&fc_trc_flag, 0, sizeof(fc_trc_flag));
 }
 
 /*
@@ -121,11 +110,11 @@ static ssize_t fnic_trace_ctrl_read(struct file *filp,
 	u8 *trace_type;
 	len = 0;
 	trace_type = (u8 *)filp->private_data;
-	if (*trace_type == fc_trc_flag->fnic_trace)
+	if (*trace_type == fc_trc_flag.fnic_trace)
 		len = sprintf(buf, "%u\n", fnic_tracing_enabled);
-	else if (*trace_type == fc_trc_flag->fc_trace)
+	else if (*trace_type == fc_trc_flag.fc_trace)
 		len = sprintf(buf, "%u\n", fnic_fc_tracing_enabled);
-	else if (*trace_type == fc_trc_flag->fc_clear)
+	else if (*trace_type == fc_trc_flag.fc_clear)
 		len = sprintf(buf, "%u\n", fnic_fc_trace_cleared);
 	else
 		pr_err("fnic: Cannot read to any debugfs file\n");
@@ -172,11 +161,11 @@ static ssize_t fnic_trace_ctrl_write(struct file *filp,
 	if (ret < 0)
 		return ret;
 
-	if (*trace_type == fc_trc_flag->fnic_trace)
+	if (*trace_type == fc_trc_flag.fnic_trace)
 		fnic_tracing_enabled = val;
-	else if (*trace_type == fc_trc_flag->fc_trace)
+	else if (*trace_type == fc_trc_flag.fc_trace)
 		fnic_fc_tracing_enabled = val;
-	else if (*trace_type == fc_trc_flag->fc_clear)
+	else if (*trace_type == fc_trc_flag.fc_clear)
 		fnic_fc_trace_cleared = val;
 	else
 		pr_err("fnic: cannot write to any debugfs file\n");
@@ -218,7 +207,7 @@ static int fnic_trace_debugfs_open(struct inode *inode,
 	if (!fnic_dbg_prt)
 		return -ENOMEM;
 
-	if (*rdata_ptr == fc_trc_flag->fnic_trace) {
+	if (*rdata_ptr == fc_trc_flag.fnic_trace) {
 		fnic_dbg_prt->buffer = vmalloc(array3_size(3, trace_max_pages,
 							   PAGE_SIZE));
 		if (!fnic_dbg_prt->buffer) {
@@ -347,13 +336,13 @@ void fnic_trace_debugfs_init(void)
 	fnic_trace_enable = debugfs_create_file("tracing_enable",
 					S_IFREG|S_IRUGO|S_IWUSR,
 					fnic_trace_debugfs_root,
-					&(fc_trc_flag->fnic_trace),
+					&fc_trc_flag.fnic_trace,
 					&fnic_trace_ctrl_fops);
 
 	fnic_trace_debugfs_file = debugfs_create_file("trace",
 					S_IFREG|S_IRUGO|S_IWUSR,
 					fnic_trace_debugfs_root,
-					&(fc_trc_flag->fnic_trace),
+					&fc_trc_flag.fnic_trace,
 					&fnic_trace_debugfs_fops);
 }
 
@@ -390,27 +379,27 @@ void fnic_fc_trace_debugfs_init(void)
 	fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
 					S_IFREG|S_IRUGO|S_IWUSR,
 					fnic_trace_debugfs_root,
-					&(fc_trc_flag->fc_trace),
+					&fc_trc_flag.fc_trace,
 					&fnic_trace_ctrl_fops);
 
 	fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
 					S_IFREG|S_IRUGO|S_IWUSR,
 					fnic_trace_debugfs_root,
-					&(fc_trc_flag->fc_clear),
+					&fc_trc_flag.fc_clear,
 					&fnic_trace_ctrl_fops);
 
 	fnic_fc_rdata_trace_debugfs_file =
 		debugfs_create_file("fc_trace_rdata",
 				    S_IFREG|S_IRUGO|S_IWUSR,
 				    fnic_trace_debugfs_root,
-				    &(fc_trc_flag->fc_normal_file),
+				    &fc_trc_flag.fc_normal_file,
 				    &fnic_trace_debugfs_fops);
 
 	fnic_fc_trace_debugfs_file =
 		debugfs_create_file("fc_trace",
 				    S_IFREG|S_IRUGO|S_IWUSR,
 				    fnic_trace_debugfs_root,
-				    &(fc_trc_flag->fc_row_file),
+				    &fc_trc_flag.fc_row_file,
 				    &fnic_trace_debugfs_fops);
 }
 


      reply	other threads:[~2019-07-18 19:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-18 19:02 Vasyl Gomonovych
2019-07-18 19:36 ` Joe Perches [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5cce2a9f73f6bf5ee55c576f98a28b04fdbe05d6.camel@perches.com \
    --to=joe@perches.com \
    --cc=gomonovych@gmail.com \
    --cc=jejb@linux.ibm.com \
    --cc=kartilak@cisco.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=satishkh@cisco.com \
    --cc=sebaddel@cisco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®