linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] target: Fine-tuning for some function implementations
@ 2017-04-09 19:43 SF Markus Elfring
  2017-04-09 19:46 ` [PATCH 1/5] target: Use kcalloc() in two functions SF Markus Elfring
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: SF Markus Elfring @ 2017-04-09 19:43 UTC (permalink / raw)
  To: linux-scsi, target-devel, Bart Van Assche, Nicholas A. Bellinger
  Cc: LKML, kernel-janitors

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sun, 9 Apr 2017 21:33:21 +0200

A few update suggestions were taken into account
from static source code analysis.

Markus Elfring (5):
  rd: Use kcalloc() in two functions
  rd: Delete error messages for failed memory allocations
  rd: Improve size determinations in two functions
  sbc: Use kmalloc_array() in compare_and_write_callback()
  transport: Use kmalloc_array() in transport_kmap_data_sg()

 drivers/target/target_core_rd.c        | 33 +++++++++------------------------
 drivers/target/target_core_sbc.c       |  4 ++--
 drivers/target/target_core_transport.c |  2 +-
 3 files changed, 12 insertions(+), 27 deletions(-)

-- 
2.12.2

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

* [PATCH 1/5] target: Use kcalloc() in two functions
  2017-04-09 19:43 [PATCH 0/5] target: Fine-tuning for some function implementations SF Markus Elfring
@ 2017-04-09 19:46 ` SF Markus Elfring
  2017-04-09 19:47 ` [PATCH 2/5] target: Delete error messages for failed memory allocations SF Markus Elfring
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: SF Markus Elfring @ 2017-04-09 19:46 UTC (permalink / raw)
  To: linux-scsi, target-devel, Bart Van Assche, Nicholas A. Bellinger
  Cc: LKML, kernel-janitors

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sun, 9 Apr 2017 19:02:38 +0200

* Multiplications for the size determination of memory allocations
  indicated that array data structures should be processed.
  Thus use the corresponding function "kcalloc".

  This issue was detected by using the Coccinelle software.

* Replace the specification of data structures by pointer dereferences
  to make the corresponding size determination a bit safer according to
  the Linux coding style convention.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/target/target_core_rd.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/target/target_core_rd.c b/drivers/target/target_core_rd.c
index 5f23f341f8d3..026857861107 100644
--- a/drivers/target/target_core_rd.c
+++ b/drivers/target/target_core_rd.c
@@ -210,8 +210,7 @@ static int rd_build_device_space(struct rd_dev *rd_dev)
 	total_sg_needed = rd_dev->rd_page_count;
 
 	sg_tables = (total_sg_needed / max_sg_per_table) + 1;
-
-	sg_table = kzalloc(sg_tables * sizeof(struct rd_dev_sg_table), GFP_KERNEL);
+	sg_table = kcalloc(sg_tables, sizeof(*sg_table), GFP_KERNEL);
 	if (!sg_table) {
 		pr_err("Unable to allocate memory for Ramdisk"
 		       " scatterlist tables\n");
@@ -271,8 +270,7 @@ static int rd_build_prot_space(struct rd_dev *rd_dev, int prot_length, int block
 	total_sg_needed = (rd_dev->rd_page_count * prot_length / block_size) + 1;
 
 	sg_tables = (total_sg_needed / max_sg_per_table) + 1;
-
-	sg_table = kzalloc(sg_tables * sizeof(struct rd_dev_sg_table), GFP_KERNEL);
+	sg_table = kcalloc(sg_tables, sizeof(*sg_table), GFP_KERNEL);
 	if (!sg_table) {
 		pr_err("Unable to allocate memory for Ramdisk protection"
 		       " scatterlist tables\n");
-- 
2.12.2

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

* [PATCH 2/5] target: Delete error messages for failed memory allocations
  2017-04-09 19:43 [PATCH 0/5] target: Fine-tuning for some function implementations SF Markus Elfring
  2017-04-09 19:46 ` [PATCH 1/5] target: Use kcalloc() in two functions SF Markus Elfring
@ 2017-04-09 19:47 ` SF Markus Elfring
  2017-04-09 19:50 ` [PATCH 3/5] target: Improve size determinations in two functions SF Markus Elfring
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: SF Markus Elfring @ 2017-04-09 19:47 UTC (permalink / raw)
  To: linux-scsi, target-devel, Bart Van Assche, Nicholas A. Bellinger
  Cc: LKML, kernel-janitors, Wolfram Sang

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sun, 9 Apr 2017 19:20:26 +0200

The script "checkpatch.pl" pointed information out like the following.

WARNING: Possible unnecessary 'out of memory' message

Thus remove such statements here.

Link: http://events.linuxfoundation.org/sites/events/files/slides/LCJ16-Refactor_Strings-WSang_0.pdf
Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/target/target_core_rd.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)

diff --git a/drivers/target/target_core_rd.c b/drivers/target/target_core_rd.c
index 026857861107..838dc128d494 100644
--- a/drivers/target/target_core_rd.c
+++ b/drivers/target/target_core_rd.c
@@ -48,10 +48,8 @@ static int rd_attach_hba(struct se_hba *hba, u32 host_id)
 	struct rd_host *rd_host;
 
 	rd_host = kzalloc(sizeof(struct rd_host), GFP_KERNEL);
-	if (!rd_host) {
-		pr_err("Unable to allocate memory for struct rd_host\n");
+	if (!rd_host)
 		return -ENOMEM;
-	}
 
 	rd_host->rd_host_id = host_id;
 
@@ -148,11 +146,8 @@ static int rd_allocate_sgl_table(struct rd_dev *rd_dev, struct rd_dev_sg_table *
 
 		sg = kcalloc(sg_per_table + chain_entry, sizeof(*sg),
 				GFP_KERNEL);
-		if (!sg) {
-			pr_err("Unable to allocate scatterlist array"
-				" for struct rd_dev\n");
+		if (!sg)
 			return -ENOMEM;
-		}
 
 		sg_init_table(sg, sg_per_table + chain_entry);
 
@@ -211,11 +206,8 @@ static int rd_build_device_space(struct rd_dev *rd_dev)
 
 	sg_tables = (total_sg_needed / max_sg_per_table) + 1;
 	sg_table = kcalloc(sg_tables, sizeof(*sg_table), GFP_KERNEL);
-	if (!sg_table) {
-		pr_err("Unable to allocate memory for Ramdisk"
-		       " scatterlist tables\n");
+	if (!sg_table)
 		return -ENOMEM;
-	}
 
 	rd_dev->sg_table_array = sg_table;
 	rd_dev->sg_table_count = sg_tables;
@@ -271,11 +263,8 @@ static int rd_build_prot_space(struct rd_dev *rd_dev, int prot_length, int block
 
 	sg_tables = (total_sg_needed / max_sg_per_table) + 1;
 	sg_table = kcalloc(sg_tables, sizeof(*sg_table), GFP_KERNEL);
-	if (!sg_table) {
-		pr_err("Unable to allocate memory for Ramdisk protection"
-		       " scatterlist tables\n");
+	if (!sg_table)
 		return -ENOMEM;
-	}
 
 	rd_dev->sg_prot_array = sg_table;
 	rd_dev->sg_prot_count = sg_tables;
@@ -297,10 +286,8 @@ static struct se_device *rd_alloc_device(struct se_hba *hba, const char *name)
 	struct rd_host *rd_host = hba->hba_ptr;
 
 	rd_dev = kzalloc(sizeof(struct rd_dev), GFP_KERNEL);
-	if (!rd_dev) {
-		pr_err("Unable to allocate memory for struct rd_dev\n");
+	if (!rd_dev)
 		return NULL;
-	}
 
 	rd_dev->rd_host = rd_host;
 
-- 
2.12.2

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

* [PATCH 3/5] target: Improve size determinations in two functions
  2017-04-09 19:43 [PATCH 0/5] target: Fine-tuning for some function implementations SF Markus Elfring
  2017-04-09 19:46 ` [PATCH 1/5] target: Use kcalloc() in two functions SF Markus Elfring
  2017-04-09 19:47 ` [PATCH 2/5] target: Delete error messages for failed memory allocations SF Markus Elfring
@ 2017-04-09 19:50 ` SF Markus Elfring
  2017-04-09 19:51 ` [PATCH 4/5] target: Use kmalloc_array() in compare_and_write_callback() SF Markus Elfring
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: SF Markus Elfring @ 2017-04-09 19:50 UTC (permalink / raw)
  To: linux-scsi, target-devel, Bart Van Assche, Nicholas A. Bellinger
  Cc: LKML, kernel-janitors

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sun, 9 Apr 2017 20:15:12 +0200

Replace the specification of two data structures by pointer dereferences
as the parameter for the operator "sizeof" to make the corresponding size
determinations a bit safer according to the Linux coding style convention.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/target/target_core_rd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/target/target_core_rd.c b/drivers/target/target_core_rd.c
index 838dc128d494..20253d04103f 100644
--- a/drivers/target/target_core_rd.c
+++ b/drivers/target/target_core_rd.c
@@ -47,7 +47,7 @@ static int rd_attach_hba(struct se_hba *hba, u32 host_id)
 {
 	struct rd_host *rd_host;
 
-	rd_host = kzalloc(sizeof(struct rd_host), GFP_KERNEL);
+	rd_host = kzalloc(sizeof(*rd_host), GFP_KERNEL);
 	if (!rd_host)
 		return -ENOMEM;
 
@@ -285,7 +285,7 @@ static struct se_device *rd_alloc_device(struct se_hba *hba, const char *name)
 	struct rd_dev *rd_dev;
 	struct rd_host *rd_host = hba->hba_ptr;
 
-	rd_dev = kzalloc(sizeof(struct rd_dev), GFP_KERNEL);
+	rd_dev = kzalloc(sizeof(*rd_dev), GFP_KERNEL);
 	if (!rd_dev)
 		return NULL;
 
-- 
2.12.2

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

* [PATCH 4/5] target: Use kmalloc_array() in compare_and_write_callback()
  2017-04-09 19:43 [PATCH 0/5] target: Fine-tuning for some function implementations SF Markus Elfring
                   ` (2 preceding siblings ...)
  2017-04-09 19:50 ` [PATCH 3/5] target: Improve size determinations in two functions SF Markus Elfring
@ 2017-04-09 19:51 ` SF Markus Elfring
  2017-04-09 19:53 ` [PATCH 5/5] target: Use kmalloc_array() in transport_kmap_data_sg() SF Markus Elfring
  2017-05-02  3:37 ` [PATCH 0/5] target: Fine-tuning for some function implementations Nicholas A. Bellinger
  5 siblings, 0 replies; 7+ messages in thread
From: SF Markus Elfring @ 2017-04-09 19:51 UTC (permalink / raw)
  To: linux-scsi, target-devel, Bart Van Assche, Nicholas A. Bellinger
  Cc: LKML, kernel-janitors

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sun, 9 Apr 2017 20:25:11 +0200

* A multiplication for the size determination of a memory allocation
  indicated that an array data structure should be processed.
  Thus use the corresponding function "kmalloc_array".

  This issue was detected by using the Coccinelle software.

* Replace the specification of a data structure by a pointer dereference
  to make the corresponding size determination a bit safer according to
  the Linux coding style convention.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/target/target_core_sbc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/target/target_core_sbc.c b/drivers/target/target_core_sbc.c
index ee35c90e3b8d..a7fa4a7339db 100644
--- a/drivers/target/target_core_sbc.c
+++ b/drivers/target/target_core_sbc.c
@@ -519,8 +519,8 @@ static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool succes
 		goto out;
 	}
 
-	write_sg = kmalloc(sizeof(struct scatterlist) * cmd->t_data_nents,
-			   GFP_KERNEL);
+	write_sg = kmalloc_array(cmd->t_data_nents, sizeof(*write_sg),
+				 GFP_KERNEL);
 	if (!write_sg) {
 		pr_err("Unable to allocate compare_and_write sg\n");
 		ret = TCM_OUT_OF_RESOURCES;
-- 
2.12.2

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

* [PATCH 5/5] target: Use kmalloc_array() in transport_kmap_data_sg()
  2017-04-09 19:43 [PATCH 0/5] target: Fine-tuning for some function implementations SF Markus Elfring
                   ` (3 preceding siblings ...)
  2017-04-09 19:51 ` [PATCH 4/5] target: Use kmalloc_array() in compare_and_write_callback() SF Markus Elfring
@ 2017-04-09 19:53 ` SF Markus Elfring
  2017-05-02  3:37 ` [PATCH 0/5] target: Fine-tuning for some function implementations Nicholas A. Bellinger
  5 siblings, 0 replies; 7+ messages in thread
From: SF Markus Elfring @ 2017-04-09 19:53 UTC (permalink / raw)
  To: linux-scsi, target-devel, Bart Van Assche, Nicholas A. Bellinger
  Cc: LKML, kernel-janitors

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sun, 9 Apr 2017 21:07:14 +0200

A multiplication for the size determination of a memory allocation
indicated that an array data structure should be processed.
Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/target/target_core_transport.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/target/target_core_transport.c b/drivers/target/target_core_transport.c
index b1a3cdb29468..3ef1cadd3c32 100644
--- a/drivers/target/target_core_transport.c
+++ b/drivers/target/target_core_transport.c
@@ -2280,7 +2280,7 @@ void *transport_kmap_data_sg(struct se_cmd *cmd)
 		return kmap(sg_page(sg)) + sg->offset;
 
 	/* >1 page. use vmap */
-	pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
+	pages = kmalloc_array(cmd->t_data_nents, sizeof(*pages), GFP_KERNEL);
 	if (!pages)
 		return NULL;
 
-- 
2.12.2

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

* Re: [PATCH 0/5] target: Fine-tuning for some function implementations
  2017-04-09 19:43 [PATCH 0/5] target: Fine-tuning for some function implementations SF Markus Elfring
                   ` (4 preceding siblings ...)
  2017-04-09 19:53 ` [PATCH 5/5] target: Use kmalloc_array() in transport_kmap_data_sg() SF Markus Elfring
@ 2017-05-02  3:37 ` Nicholas A. Bellinger
  5 siblings, 0 replies; 7+ messages in thread
From: Nicholas A. Bellinger @ 2017-05-02  3:37 UTC (permalink / raw)
  To: SF Markus Elfring
  Cc: linux-scsi, target-devel, Bart Van Assche, LKML, kernel-janitors

Hi Markus,

Apologies for the delayed follow-up here as well.

On Sun, 2017-04-09 at 21:43 +0200, SF Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Sun, 9 Apr 2017 21:33:21 +0200
> 
> A few update suggestions were taken into account
> from static source code analysis.
> 
> Markus Elfring (5):
>   rd: Use kcalloc() in two functions
>   rd: Delete error messages for failed memory allocations
>   rd: Improve size determinations in two functions
>   sbc: Use kmalloc_array() in compare_and_write_callback()
>   transport: Use kmalloc_array() in transport_kmap_data_sg()
> 
>  drivers/target/target_core_rd.c        | 33 +++++++++------------------------
>  drivers/target/target_core_sbc.c       |  4 ++--
>  drivers/target/target_core_transport.c |  2 +-
>  3 files changed, 12 insertions(+), 27 deletions(-)
> 

Applied.

Thank you.

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

end of thread, other threads:[~2017-05-02  3:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-09 19:43 [PATCH 0/5] target: Fine-tuning for some function implementations SF Markus Elfring
2017-04-09 19:46 ` [PATCH 1/5] target: Use kcalloc() in two functions SF Markus Elfring
2017-04-09 19:47 ` [PATCH 2/5] target: Delete error messages for failed memory allocations SF Markus Elfring
2017-04-09 19:50 ` [PATCH 3/5] target: Improve size determinations in two functions SF Markus Elfring
2017-04-09 19:51 ` [PATCH 4/5] target: Use kmalloc_array() in compare_and_write_callback() SF Markus Elfring
2017-04-09 19:53 ` [PATCH 5/5] target: Use kmalloc_array() in transport_kmap_data_sg() SF Markus Elfring
2017-05-02  3:37 ` [PATCH 0/5] target: Fine-tuning for some function implementations Nicholas A. Bellinger

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