Skip to contentSkip to conversation

target: Fine-tuning for some function implementations

PATCH Applied SF Markus Elfring 2 participants 7 messages Apr 9, 2017 – May 2, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover target: Fine-tuning for some function implementations

From: Markus Elfring <[email protected]>
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(-)
Signature · 1 line
2.12.2
#1 lore raw

PATCH 1/5 target: Use kcalloc() in two functions

From: Markus Elfring <[email protected]>
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.

---
 drivers/target/target_core_rd.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/target/target_core_rd.c +2 -4
index 5f23f341f8d3..026857861107 100644
@@ -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");
Signature · 1 line
2.12.2
#2 lore raw

PATCH 2/5 target: Delete error messages for failed memory allocations

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
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.

---
 drivers/target/target_core_rd.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)
drivers/target/target_core_rd.c +5 -18
index 026857861107..838dc128d494 100644
@@ -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;
 
Signature · 1 line
2.12.2
#3 lore raw

PATCH 3/5 target: Improve size determinations in two functions

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
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.

---
 drivers/target/target_core_rd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/target/target_core_rd.c +2 -2
index 838dc128d494..20253d04103f 100644
@@ -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;
 
Signature · 1 line
2.12.2
#4 lore raw

PATCH 4/5 target: Use kmalloc_array() in compare_and_write_callback()

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
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.

---
 drivers/target/target_core_sbc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/target/target_core_sbc.c +2 -2
index ee35c90e3b8d..a7fa4a7339db 100644
@@ -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;
Signature · 1 line
2.12.2
#5 lore raw

PATCH 5/5 target: Use kmalloc_array() in transport_kmap_data_sg()

In reply to SF Markus Elfring

From: Markus Elfring <[email protected]>
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.

---
 drivers/target/target_core_transport.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/target/target_core_transport.c +1 -1
index b1a3cdb29468..3ef1cadd3c32 100644
@@ -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;
 
Signature · 1 line
2.12.2
#6 lore raw
In reply to SF Markus Elfring

Hi Markus,

Apologies for the delayed follow-up here as well.

Quoted 13 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
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()

Quoted 5 lines
 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.