pstore: Adjustments for some function implementations
Subsystems (2)
cover pstore: Adjustments for some function implementations
From: Markus Elfring <[email protected]>
Date: Wed, 16 Aug 2017 21:06:54 +0200
Three update suggestions were taken into account
from static source code analysis.
Markus Elfring (3):
Delete six error messages for a failed memory allocation Improve a size determination in three functions Adjust two checks for null pointers
fs/pstore/ram.c | 11 +++-------- fs/pstore/ram_core.c | 21 +++++++-------------- 2 files changed, 10 insertions(+), 22 deletions(-)
Signature · 1 line
2.14.0
PATCH 1/3 pstore: Delete six error messages for a failed memory allocation
From: Markus Elfring <[email protected]>
Date: Wed, 16 Aug 2017 20:30:44 +0200
Omit extra messages for a memory allocation failure in these functions.
This issue was detected by using the Coccinelle software.
--- fs/pstore/ram.c | 6 +----- fs/pstore/ram_core.c | 15 ++++----------- 2 files changed, 5 insertions(+), 16 deletions(-)
fs/pstore/ram.c +1 -5
@@ -719,7 +719,6 @@ static int ramoops_probe(struct platform_device *pdev)if (dev_of_node (dev) && !pdata) { pdata =devm_kzalloc (&pdev->dev,sizeof (*pdata),GFP_KERNEL );if (!pdata) { -pr_err ("cannot allocate platform data buffer\n" ); err = -ENOMEM ; goto fail_out; }
@@ -814,7 +813,6 @@ static int ramoops_probe(struct platform_device *pdev) cxt->pstore.bufsize =max (cxt->record_size, cxt->pstore.bufsize); cxt->pstore.buf =kmalloc (cxt->pstore.bufsize,GFP_KERNEL );if (!cxt->pstore.buf) { -pr_err ("cannot allocate pstore buffer\n" ); err = -ENOMEM ; goto fail_clear; }
@@ -904,10 +902,8 @@ static void ramoops_register_dummy(void)pr_info ("using module parameters\n" ); dummy_data =kzalloc (sizeof (*dummy_data),GFP_KERNEL ); -if (!dummy_data) { -pr_info ("could not allocate pdata\n" ); +if (!dummy_data)return ; - } dummy_data->mem_size = mem_size; dummy_data->mem_address = mem_address;
fs/pstore/ram_core.c +4 -11
@@ -292,10 +292,8 @@ void persistent_ram_save_old(struct persistent_ram_zone *prz)if (!prz->old_log) {persistent_ram_ecc_old (prz); prz->old_log =kmalloc (size,GFP_KERNEL ); - } -if (!prz->old_log) { -pr_err ("failed to allocate buffer\n" ); -return ; +if (!prz->old_log) +return ; } prz->old_log_size = size;
@@ -411,8 +409,5 @@ static void *persistent_ram_vmap(phys_addr_t start, size_t size, -if (!pages) { -pr_err ("%s: Failed to allocate array for %u pages\n" , - __func__, page_count); +if (!pages)return NULL ; - }for (i =0 ; i < page_count; i++) {phys_addr_t addr = page_start + i *PAGE_SIZE ;
@@ -529,7 +524,5 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size, -if (!prz) { -pr_err ("failed to allocate persistent ram zone\n" ); +if (!prz) goto err; - }/* Initialize general buffer state. */ raw_spin_lock_init (&prz->buffer_lock);
Signature · 1 line
2.14.0
PATCH 2/3 pstore: Improve a size determination in three functions
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Wed, 16 Aug 2017 20:50:15 +0200
Replace the specification of data types 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.
--- fs/pstore/ram.c | 3 +-- fs/pstore/ram_core.c | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-)
fs/pstore/ram.c +1 -2
@@ -293,8 +293,7 @@ static ssize_t ramoops_pstore_read(struct pstore_record *record)*/ struct persistent_ram_zone *tmp_prz, *prz_next; - tmp_prz =kzalloc (sizeof (struct persistent_ram_zone ), -GFP_KERNEL ); + tmp_prz =kzalloc (sizeof (*tmp_prz),GFP_KERNEL );if (!tmp_prz)return -ENOMEM ; free_prz = true;
fs/pstore/ram_core.c +2 -2
@@ -405,7 +405,7 @@ static void *persistent_ram_vmap(phys_addr_t start, size_t size,else prot =pgprot_writecombine (PAGE_KERNEL ); - pages =kmalloc_array (page_count,sizeof (struct page *),GFP_KERNEL ); + pages =kmalloc_array (page_count,sizeof (*pages),GFP_KERNEL );if (!pages)return NULL ;
@@ -520,7 +520,7 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,struct persistent_ram_zone *prz;int ret = -ENOMEM ; - prz =kzalloc (sizeof (struct persistent_ram_zone ),GFP_KERNEL ); + prz =kzalloc (sizeof (*prz),GFP_KERNEL );if (!prz) goto err;
Signature · 1 line
2.14.0
PATCH 3/3 pstore: Adjust two checks for null pointers
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Wed, 16 Aug 2017 21:00:16 +0200
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The script “checkpatch.pl” pointed information out like the following.
Comparison to NULL could be written !…
Thus fix affected source code places.
--- fs/pstore/ram.c | 2 +- fs/pstore/ram_core.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
fs/pstore/ram.c +1 -1
@@ -333,7 +333,7 @@ static ssize_t ramoops_pstore_read(struct pstore_record *record) record->ecc_notice_size =persistent_ram_ecc_string (prz,NULL ,0 ); record->buf =kmalloc (size + record->ecc_notice_size +1 ,GFP_KERNEL ); -if (record->buf =NULL ) { +if (!record->buf) { size = -ENOMEM ; goto out; }
fs/pstore/ram_core.c +1 -1
@@ -223,7 +223,7 @@ static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,*/ prz->rs_decoder =init_rs (prz->ecc_info.symsize, prz->ecc_info.poly,0 ,1 , prz->ecc_info.ecc_size); -if (prz->rs_decoder =NULL ) { +if (!prz->rs_decoder) {pr_info ("init_rs failed\n" );return -EINVAL ; }
Signature · 1 line
2.14.0
Re: [PATCH 1/3] pstore: Delete six error messages for a failed memory allocation
In reply to SF Markus ElfringQuoted 4 lines · SF Markus Elfring
From: Markus Elfring <[email protected]>
Date: Wed, 16 Aug 2017 20:30:44 +0200Omit extra messages for a memory allocation failure in these functions.
I'm not interested in removing these since they provide additional details about which specific allocation failed.
Quoted 1 line
This issue was detected by using the Coccinelle software.
For all these kinds of patches please include details on which coccinelle script did the detection...
-Kees
Quoted 78 lines
Signed-off-by Markus Elfring <[email protected]>--- fs/pstore/ram.c | 6 +----- fs/pstore/ram_core.c | 15 ++++----------- 2 files changed, 5 insertions(+), 16 deletions(-)fs/pstore/ram.c +1
-5@@ -719,7 +719,6 @@ static int ramoops_probe(struct platform_device *pdev)if (dev_of_node (dev) && !pdata) { pdata =devm_kzalloc (&pdev->dev,sizeof (*pdata),GFP_KERNEL );if (!pdata) { -pr_err ("cannot allocate platform data buffer\n" ); err = -ENOMEM ; goto fail_out; }@@ -814,7 +813,6 @@ static int ramoops_probe(struct platform_device *pdev) cxt->pstore.bufsize =max (cxt->record_size, cxt->pstore.bufsize); cxt->pstore.buf =kmalloc (cxt->pstore.bufsize,GFP_KERNEL );if (!cxt->pstore.buf) { -pr_err ("cannot allocate pstore buffer\n" ); err = -ENOMEM ; goto fail_clear; }@@ -904,10 +902,8 @@ static void ramoops_register_dummy(void)pr_info ("using module parameters\n" ); dummy_data =kzalloc (sizeof (*dummy_data),GFP_KERNEL ); -if (!dummy_data) { -pr_info ("could not allocate pdata\n" ); +if (!dummy_data)return ; - } dummy_data->mem_size = mem_size; dummy_data->mem_address = mem_address;fs/pstore/ram_core.c +4
-12@@ -292,10 +292,8 @@ void persistent_ram_save_old(struct persistent_ram_zone *prz)if (!prz->old_log) {persistent_ram_ecc_old (prz); prz->old_log =kmalloc (size,GFP_KERNEL ); - } -if (!prz->old_log) { -pr_err ("failed to allocate buffer\n" ); -return ; +if (!prz->old_log) +return ; } prz->old_log_size = size;@@ -411,8 +409,5 @@ static void *persistent_ram_vmap(phys_addr_t start, size_t size, -if (!pages) { -pr_err ("%s: Failed to allocate array for %u pages\n" , - __func__, page_count); +if (!pages)return NULL ; - }for (i =0 ; i < page_count; i++) {phys_addr_t addr = page_start + i *PAGE_SIZE ;@@ -529,7 +524,5 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size, -if (!prz) { -pr_err ("failed to allocate persistent ram zone\n" ); +if (!prz) goto err; - }/* Initialize general buffer state. */ raw_spin_lock_init (&prz->buffer_lock); --2.14.0
Signature · 2 lines
Kees Cook Pixel Security
Re: [PATCH 3/3] pstore: Adjust two checks for null pointers
In reply to SF Markus ElfringQuoted 11 lines · SF Markus Elfring
From: Markus Elfring <[email protected]>
Date: Wed, 16 Aug 2017 21:00:16 +0200
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bitThe script “checkpatch.pl” pointed information out like the following.
Comparison to NULL could be written !…
Thus fix affected source code places.
I think these are find to do when changing other code, but as stand-alone, I'm not interested in taking them, sorry!
-Kees
Quoted 36 lines
Signed-off-by Markus Elfring <[email protected]>--- fs/pstore/ram.c | 2 +- fs/pstore/ram_core.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)fs/pstore/ram.c +1
-1@@ -333,7 +333,7 @@ static ssize_t ramoops_pstore_read(struct pstore_record *record) record->ecc_notice_size =persistent_ram_ecc_string (prz,NULL ,0 ); record->buf =kmalloc (size + record->ecc_notice_size +1 ,GFP_KERNEL ); -if (record->buf ==NULL ) { +if (!record->buf) { size = -ENOMEM ; goto out; }fs/pstore/ram_core.c +1
-2@@ -223,7 +223,7 @@ static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,*/ prz->rs_decoder =init_rs (prz->ecc_info.symsize, prz->ecc_info.poly,0 ,1 , prz->ecc_info.ecc_size); -if (prz->rs_decoder ==NULL ) { +if (!prz->rs_decoder) {pr_info ("init_rs failed\n" );return -EINVAL ; } --2.14.0
Signature · 2 lines
Kees Cook Pixel Security
Re: [PATCH 2/3] pstore: Improve a size determination in three functions
In reply to SF Markus ElfringQuoted 6 lines · SF Markus Elfring
From: Markus Elfring <[email protected]>
Date: Wed, 16 Aug 2017 20:50:15 +0200Replace the specification of data types 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.
Agreed; this is a robustness change in that changes to the variable structure will be correctly flowed through to the allocations.
Quoted 2 lines
This issue was detected by using the Coccinelle software.
Which script detected this?
-Kees
Quoted 46 lines
Signed-off-by Markus Elfring <[email protected]>--- fs/pstore/ram.c | 3 +-- fs/pstore/ram_core.c | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-)fs/pstore/ram.c +1
-2@@ -293,8 +293,7 @@ static ssize_t ramoops_pstore_read(struct pstore_record *record)*/ struct persistent_ram_zone *tmp_prz, *prz_next; - tmp_prz =kzalloc (sizeof (struct persistent_ram_zone ), -GFP_KERNEL ); + tmp_prz =kzalloc (sizeof (*tmp_prz),GFP_KERNEL );if (!tmp_prz)return -ENOMEM ; free_prz = true;fs/pstore/ram_core.c +2
-3@@ -405,7 +405,7 @@ static void *persistent_ram_vmap(phys_addr_t start, size_t size,else prot =pgprot_writecombine (PAGE_KERNEL ); - pages =kmalloc_array (page_count,sizeof (struct page *),GFP_KERNEL ); + pages =kmalloc_array (page_count,sizeof (*pages),GFP_KERNEL );if (!pages)return NULL ;@@ -520,7 +520,7 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,struct persistent_ram_zone *prz;int ret = -ENOMEM ; - prz =kzalloc (sizeof (struct persistent_ram_zone ),GFP_KERNEL ); + prz =kzalloc (sizeof (*prz),GFP_KERNEL );if (!prz) goto err; --2.14.0
Signature · 2 lines
Kees Cook Pixel Security
Re: pstore: Delete six error messages for a failed memory allocation
In reply to Kees CookQuoted 2 lines
I'm not interested in removing these since they provide additional details about which specific allocation failed.
Do you find the default allocation failure report insufficient?
Regards,
Markus
Re: pstore: Improve a size determination in three functions
In reply to Kees CookQuoted 1 line
Which script detected this?
* checkpatch.pl
* The following approach for the semantic patch language.
@replacement@
identifier action, var, work;
statement s1, s2;
statement list sl;
type T, X;
@@
T work(...)
{
... when any
X* var;
... when any
var = action(...,
sizeof(
- X
+ *var
),
...
) ... when any
}