From: Markus Elfring <[email protected]>
Date: Sat, 6 Jan 2018 16:00:01 +0100
A few update suggestions were taken into account
from static source code analysis.
Markus Elfring (5):
Delete an error message for a failed memory allocation in scan_header()
Less function calls in scan_header() after error detection
Improve a size determination in two functions
Return directly after a failed kmalloc() in erase_block()
Add some spaces for better code readability
drivers/mtd/rfd_ftl.c | 45 +++++++++++++++++++--------------------------
1 file changed, 19 insertions(+), 26 deletions(-)
--
2.15.1
From: Markus Elfring <[email protected]>
Date: Sat, 6 Jan 2018 14:07:31 +0100
Omit an extra message for a memory allocation failure in this function.
This issue was detected by using the Coccinelle software.
Signed-off-by: Markus Elfring <[email protected]>
---
drivers/mtd/rfd_ftl.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/drivers/mtd/rfd_ftl.c b/drivers/mtd/rfd_ftl.c
index d1cbf26db2c0..efff25f15d5b 100644
--- a/drivers/mtd/rfd_ftl.c
+++ b/drivers/mtd/rfd_ftl.c
@@ -190,11 +190,8 @@ static int scan_header(struct partition *part)
goto err;
part->sector_map = vmalloc(part->sector_count * sizeof(u_long));
- if (!part->sector_map) {
- printk(KERN_ERR PREFIX "'%s': unable to allocate memory for "
- "sector map", part->mbd.mtd->name);
+ if (!part->sector_map)
goto err;
- }
for (i=0; i<part->sector_count; i++)
part->sector_map[i] = -1;
--
2.15.1
From: Markus Elfring <[email protected]>
Date: Sat, 6 Jan 2018 14:50:58 +0100
The functions "kfree" and "vfree" were called in three cases
by the scan_header() function during error handling even if
the passed data structure member contained a null pointer.
Adjust jump targets according to the Linux coding style convention.
Signed-off-by: Markus Elfring <[email protected]>
---
drivers/mtd/rfd_ftl.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/drivers/mtd/rfd_ftl.c b/drivers/mtd/rfd_ftl.c
index efff25f15d5b..19e14f909dc6 100644
--- a/drivers/mtd/rfd_ftl.c
+++ b/drivers/mtd/rfd_ftl.c
@@ -182,16 +182,16 @@ static int scan_header(struct partition *part)
part->header_cache = kmalloc(part->header_size, GFP_KERNEL);
if (!part->header_cache)
- goto err;
+ return -ENOMEM;
part->blocks = kcalloc(part->total_blocks, sizeof(struct block),
GFP_KERNEL);
if (!part->blocks)
- goto err;
+ goto free_cache;
part->sector_map = vmalloc(part->sector_count * sizeof(u_long));
if (!part->sector_map)
- goto err;
+ goto free_blocks;
for (i=0; i<part->sector_count; i++)
part->sector_map[i] = -1;
@@ -229,9 +229,10 @@ static int scan_header(struct partition *part)
err:
vfree(part->sector_map);
- kfree(part->header_cache);
+free_blocks:
kfree(part->blocks);
-
+free_cache:
+ kfree(part->header_cache);
return rc;
}
--
2.15.1
From: Markus Elfring <[email protected]>
Date: Sat, 6 Jan 2018 15:03:29 +0100
Replace the specification of data structures by pointer dereferences
as the parameter for the operator "sizeof" to make the corresponding size
determination a bit safer according to the Linux coding style convention.
This issue was detected by using the Coccinelle software.
Signed-off-by: Markus Elfring <[email protected]>
---
drivers/mtd/rfd_ftl.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/drivers/mtd/rfd_ftl.c b/drivers/mtd/rfd_ftl.c
index 19e14f909dc6..dcf1b88e1193 100644
--- a/drivers/mtd/rfd_ftl.c
+++ b/drivers/mtd/rfd_ftl.c
@@ -323,10 +323,9 @@ static void erase_callback(struct erase_info *erase)
static int erase_block(struct partition *part, int block)
{
- struct erase_info *erase;
int rc = -ENOMEM;
+ struct erase_info *erase = kmalloc(sizeof(*erase), GFP_KERNEL);
- erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
if (!erase)
goto err;
@@ -759,7 +758,7 @@ static void rfd_ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
if (mtd->type != MTD_NORFLASH || mtd->size > UINT_MAX)
return;
- part = kzalloc(sizeof(struct partition), GFP_KERNEL);
+ part = kzalloc(sizeof(*part), GFP_KERNEL);
if (!part)
return;
--
2.15.1
From: Markus Elfring <[email protected]>
Date: Sat, 6 Jan 2018 15:18:28 +0100
* Return directly after a call of the function "kmalloc" failed
at the beginning.
* Delete an initialisation for the variable "rc" and the jump label "err"
which became unnecessary with this refactoring.
Signed-off-by: Markus Elfring <[email protected]>
---
drivers/mtd/rfd_ftl.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/drivers/mtd/rfd_ftl.c b/drivers/mtd/rfd_ftl.c
index dcf1b88e1193..e3155ec4be64 100644
--- a/drivers/mtd/rfd_ftl.c
+++ b/drivers/mtd/rfd_ftl.c
@@ -323,11 +323,11 @@ static void erase_callback(struct erase_info *erase)
static int erase_block(struct partition *part, int block)
{
- int rc = -ENOMEM;
+ int rc;
struct erase_info *erase = kmalloc(sizeof(*erase), GFP_KERNEL);
if (!erase)
- goto err;
+ return -ENOMEM;
erase->mtd = part->mbd.mtd;
erase->callback = erase_callback;
@@ -346,8 +346,6 @@ static int erase_block(struct partition *part, int block)
(unsigned long long)erase->len, part->mbd.mtd->name);
kfree(erase);
}
-
-err:
return rc;
}
--
2.15.1
From: Markus Elfring <[email protected]>
Date: Sat, 6 Jan 2018 15:47:01 +0100
Use space characters at some source code places according to
the Linux coding style convention.
Signed-off-by: Markus Elfring <[email protected]>
---
drivers/mtd/rfd_ftl.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/drivers/mtd/rfd_ftl.c b/drivers/mtd/rfd_ftl.c
index e3155ec4be64..b6cc3690b9ed 100644
--- a/drivers/mtd/rfd_ftl.c
+++ b/drivers/mtd/rfd_ftl.c
@@ -103,7 +103,7 @@ static int build_block_map(struct partition *part, int block_no)
block->state = BLOCK_OK;
- for (i=0; i<part->data_sectors_per_block; i++) {
+ for (i = 0; i < part->data_sectors_per_block; i++) {
u16 entry;
entry = le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]);
@@ -193,10 +193,10 @@ static int scan_header(struct partition *part)
if (!part->sector_map)
goto free_blocks;
- for (i=0; i<part->sector_count; i++)
+ for (i = 0; i < part->sector_count; i++)
part->sector_map[i] = -1;
- for (i=0, blocks_found=0; i<part->total_blocks; i++) {
+ for (i = 0, blocks_found = 0; i < part->total_blocks; i++) {
rc = mtd_read(part->mbd.mtd, i * part->block_size,
part->header_size, &retlen,
(u_char *)part->header_cache);
@@ -380,7 +380,7 @@ static int move_block_contents(struct partition *part, int block_no, u_long *old
goto err;
}
- for (i=0; i<part->data_sectors_per_block; i++) {
+ for (i = 0; i < part->data_sectors_per_block; i++) {
u16 entry = le16_to_cpu(map[HEADER_MAP_OFFSET + i]);
u_long addr;
@@ -452,7 +452,7 @@ static int reclaim_block(struct partition *part, u_long *old_sector)
else
old_sector_block = -1;
- for (block=0; block<part->total_blocks; block++) {
+ for (block = 0; block < part->total_blocks; block++) {
int this_score;
if (block == part->reserved_block)
@@ -625,8 +625,7 @@ static int find_free_sector(const struct partition *part, const struct block *bl
if (++i == part->data_sectors_per_block)
i = 0;
- }
- while(i != stop);
+ } while (i != stop);
return -1;
}
@@ -718,7 +717,7 @@ static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *
old_addr = part->sector_map[sector];
- for (i=0; i<SECTOR_SIZE; i++) {
+ for (i = 0; i < SECTOR_SIZE; i++) {
if (!buf[i])
continue;
@@ -799,10 +798,9 @@ static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
struct partition *part = (struct partition*)dev;
int i;
- for (i=0; i<part->total_blocks; i++) {
+ for (i = 0; i < part->total_blocks; i++)
pr_debug("rfd_ftl_remove_dev:'%s': erase unit #%02d: %d erases\n",
part->mbd.mtd->name, i, part->blocks[i].erases);
- }
del_mtd_blktrans_dev(dev);
vfree(part->sector_map);
--
2.15.1