2017-04-24 20:13:41

by SF Markus Elfring

[permalink] [raw]
Subject: [PATCH 0/7] HID: Fine-tuning for some function implementations

From: Markus Elfring <[email protected]>
Date: Mon, 24 Apr 2017 22:00:22 +0200

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

Markus Elfring (7):
Use devm_kcalloc() in sensor_hub_probe()
Delete error messages for failed memory allocations in sensor_hub_probe()
Adjust two checks for null pointers in sensor_hub_probe()
Replace five seq_printf() calls by seq_putc()
Replace 12 seq_printf() calls by seq_puts()
Combine two seq_printf() calls into one call in hid_dump_device()
Replace two seq_printf() calls by seq_puts() in picolcd_debug_reset_show()

drivers/hid/hid-debug.c | 38 +++++++++++++++++++-------------------
drivers/hid/hid-picolcd_debugfs.c | 4 ++--
drivers/hid/hid-sensor-hub.c | 18 ++++++++----------
3 files changed, 29 insertions(+), 31 deletions(-)

--
2.12.2


2017-04-24 20:16:31

by SF Markus Elfring

[permalink] [raw]
Subject: [PATCH 1/7] HID: sensor-hub: Use devm_kcalloc() in sensor_hub_probe()

From: Markus Elfring <[email protected]>
Date: Mon, 24 Apr 2017 19:25:29 +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 "devm_kcalloc".

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 <[email protected]>
---
drivers/hid/hid-sensor-hub.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-hub.c
index 4ef73374a8f9..349494fc8def 100644
--- a/drivers/hid/hid-sensor-hub.c
+++ b/drivers/hid/hid-sensor-hub.c
@@ -672,9 +672,11 @@ static int sensor_hub_probe(struct hid_device *hdev,
ret = -EINVAL;
goto err_stop_hw;
}
- sd->hid_sensor_hub_client_devs = devm_kzalloc(&hdev->dev, dev_cnt *
- sizeof(struct mfd_cell),
- GFP_KERNEL);
+ sd->hid_sensor_hub_client_devs
+ = devm_kcalloc(&hdev->dev,
+ dev_cnt,
+ sizeof(*sd->hid_sensor_hub_client_devs),
+ GFP_KERNEL);
if (sd->hid_sensor_hub_client_devs == NULL) {
hid_err(hdev, "Failed to allocate memory for mfd cells\n");
ret = -ENOMEM;
--
2.12.2

2017-04-24 20:18:13

by SF Markus Elfring

[permalink] [raw]
Subject: [PATCH 2/7] HID: sensor-hub: Delete error messages for failed memory allocations in sensor_hub_probe()

From: Markus Elfring <[email protected]>
Date: Mon, 24 Apr 2017 19:45:25 +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 <[email protected]>
---
drivers/hid/hid-sensor-hub.c | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-hub.c
index 349494fc8def..feb4bb8ee3ec 100644
--- a/drivers/hid/hid-sensor-hub.c
+++ b/drivers/hid/hid-sensor-hub.c
@@ -640,10 +640,8 @@ static int sensor_hub_probe(struct hid_device *hdev,
struct hid_sensor_hub_device *collection_hsdev = NULL;

sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
- if (!sd) {
- hid_err(hdev, "cannot allocate Sensor data\n");
+ if (!sd)
return -ENOMEM;
- }

hid_set_drvdata(hdev, sd);
sd->quirks = id->driver_data;
@@ -678,7 +676,6 @@ static int sensor_hub_probe(struct hid_device *hdev,
sizeof(*sd->hid_sensor_hub_client_devs),
GFP_KERNEL);
if (sd->hid_sensor_hub_client_devs == NULL) {
- hid_err(hdev, "Failed to allocate memory for mfd cells\n");
ret = -ENOMEM;
goto err_stop_hw;
}
@@ -692,7 +689,6 @@ static int sensor_hub_probe(struct hid_device *hdev,
hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
GFP_KERNEL);
if (!hsdev) {
- hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
ret = -ENOMEM;
goto err_stop_hw;
}
--
2.12.2

2017-04-24 20:19:19

by SF Markus Elfring

[permalink] [raw]
Subject: [PATCH 3/7] HID: sensor-hub: Adjust two checks for null pointers in sensor_hub_probe()

From: Markus Elfring <[email protected]>
Date: Mon, 24 Apr 2017 20:00:06 +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 the affected source code places.

Signed-off-by: Markus Elfring <[email protected]>
---
drivers/hid/hid-sensor-hub.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-hub.c
index feb4bb8ee3ec..4eadaef19e6a 100644
--- a/drivers/hid/hid-sensor-hub.c
+++ b/drivers/hid/hid-sensor-hub.c
@@ -675,7 +675,7 @@ static int sensor_hub_probe(struct hid_device *hdev,
dev_cnt,
sizeof(*sd->hid_sensor_hub_client_devs),
GFP_KERNEL);
- if (sd->hid_sensor_hub_client_devs == NULL) {
+ if (!sd->hid_sensor_hub_client_devs) {
ret = -ENOMEM;
goto err_stop_hw;
}
@@ -711,7 +711,7 @@ static int sensor_hub_probe(struct hid_device *hdev,
name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
"HID-SENSOR-%x",
collection->usage);
- if (name == NULL) {
+ if (!name) {
hid_err(hdev, "Failed MFD device name\n");
ret = -ENOMEM;
goto err_stop_hw;
--
2.12.2

2017-04-24 20:20:37

by SF Markus Elfring

[permalink] [raw]
Subject: [PATCH 4/7] HID: debug: Replace five seq_printf() calls by seq_putc()

From: Markus Elfring <[email protected]>
Date: Mon, 24 Apr 2017 20:34:29 +0200

Five single characters should be put into a sequence.
Thus use the corresponding function "seq_putc".

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <[email protected]>
---
drivers/hid/hid-debug.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
index 5271db593478..d47d5085bc99 100644
--- a/drivers/hid/hid-debug.c
+++ b/drivers/hid/hid-debug.c
@@ -499,7 +499,7 @@ char *hid_resolv_usage(unsigned usage, struct seq_file *f) {
len++;
}
else {
- seq_printf(f, ".");
+ seq_putc(f, '.');
}
for (p = hid_usage_table; p->description; p++)
if (p->page == (usage >> 16)) {
@@ -550,7 +550,8 @@ void hid_dump_field(struct hid_field *field, int n, struct seq_file *f) {
}
tab(n, f); seq_printf(f, "Usage(%d)\n", field->maxusage);
for (j = 0; j < field->maxusage; j++) {
- tab(n+2, f); hid_resolv_usage(field->usage[j].hid, f); seq_printf(f, "\n");
+ tab(n + 2, f); hid_resolv_usage(field->usage[j].hid, f);
+ seq_putc(f, '\n');
}
if (field->logical_minimum != field->logical_maximum) {
tab(n, f); seq_printf(f, "Logical Minimum(%d)\n", field->logical_minimum);
@@ -594,7 +595,7 @@ void hid_dump_field(struct hid_field *field, int n, struct seq_file *f) {
data >>= 4;
if (nibble != 0) {
if(earlier_unit++ > 0)
- seq_printf(f, "*");
+ seq_putc(f, '*');
seq_printf(f, "%s", units[sys][i]);
if(nibble != 1) {
/* This is a _signed_ nibble(!) */
@@ -1041,5 +1042,5 @@ static void hid_dump_input_mapping(struct hid_device *hid, struct seq_file *f)
hid_resolv_event(usage->type, usage->code, f);
- seq_printf(f, "\n");
+ seq_putc(f, '\n');
}
}
}
@@ -1066,7 +1067,7 @@ static int hid_debug_rdesc_show(struct seq_file *f, void *p)

/* dump parsed data and input mappings */
hid_dump_device(hdev, f);
- seq_printf(f, "\n");
+ seq_putc(f, '\n');
hid_dump_input_mapping(hdev, f);

return 0;
--
2.12.2

2017-04-24 20:21:35

by SF Markus Elfring

[permalink] [raw]
Subject: [PATCH 5/7] HID: debug: Replace 12 seq_printf() calls by seq_puts()

From: Markus Elfring <[email protected]>
Date: Mon, 24 Apr 2017 20:55:27 +0200

Strings which did not contain data format specifications should be put
into a sequence. Thus use the corresponding function "seq_puts".

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <[email protected]>
---
drivers/hid/hid-debug.c | 24 ++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
index d47d5085bc99..ce850f80d9e3 100644
--- a/drivers/hid/hid-debug.c
+++ b/drivers/hid/hid-debug.c
@@ -535,18 +535,18 @@ void hid_dump_field(struct hid_field *field, int n, struct seq_file *f) {

if (field->physical) {
tab(n, f);
- seq_printf(f, "Physical(");
- hid_resolv_usage(field->physical, f); seq_printf(f, ")\n");
+ seq_puts(f, "Physical(");
+ hid_resolv_usage(field->physical, f); seq_puts(f, ")\n");
}
if (field->logical) {
tab(n, f);
- seq_printf(f, "Logical(");
- hid_resolv_usage(field->logical, f); seq_printf(f, ")\n");
+ seq_puts(f, "Logical(");
+ hid_resolv_usage(field->logical, f); seq_puts(f, ")\n");
}
if (field->application) {
tab(n, f);
- seq_printf(f, "Application(");
- hid_resolv_usage(field->application, f); seq_printf(f, ")\n");
+ seq_puts(f, "Application(");
+ hid_resolv_usage(field->application, f); seq_puts(f, ")\n");
}
tab(n, f); seq_printf(f, "Usage(%d)\n", field->maxusage);
for (j = 0; j < field->maxusage; j++) {
@@ -583,7 +583,7 @@ void hid_dump_field(struct hid_field *field, int n, struct seq_file *f) {
data >>= 4;

if(sys > 4) {
- tab(n, f); seq_printf(f, "Unit(Invalid)\n");
+ tab(n, f); seq_puts(f, "Unit(Invalid)\n");
}
else {
int earlier_unit = 0;
@@ -607,14 +607,14 @@ void hid_dump_field(struct hid_field *field, int n, struct seq_file *f) {
}
}
}
- seq_printf(f, ")\n");
+ seq_puts(f, ")\n");
}
}
tab(n, f); seq_printf(f, "Report Size(%u)\n", field->report_size);
tab(n, f); seq_printf(f, "Report Count(%u)\n", field->report_count);
tab(n, f); seq_printf(f, "Report Offset(%u)\n", field->report_offset);

- tab(n, f); seq_printf(f, "Flags( ");
+ tab(n, f); seq_puts(f, "Flags( ");
j = field->flags;
seq_printf(f, "%s", HID_MAIN_ITEM_CONSTANT & j ? "Constant " : "");
seq_printf(f, "%s", HID_MAIN_ITEM_VARIABLE & j ? "Variable " : "Array ");
@@ -625,7 +625,7 @@ void hid_dump_field(struct hid_field *field, int n, struct seq_file *f) {
seq_printf(f, "%s", HID_MAIN_ITEM_NULL_STATE & j ? "NullState " : "");
seq_printf(f, "%s", HID_MAIN_ITEM_VOLATILE & j ? "Volatile " : "");
seq_printf(f, "%s", HID_MAIN_ITEM_BUFFERED_BYTE & j ? "BufferedByte " : "");
- seq_printf(f, ")\n");
+ seq_puts(f, ")\n");
}
EXPORT_SYMBOL_GPL(hid_dump_field);

@@ -1038,5 +1038,5 @@ static void hid_dump_input_mapping(struct hid_device *hid, struct seq_file *f)
for ( j = 0; j < report->field[i]->maxusage; j++) {
usage = report->field[i]->usage + j;
hid_resolv_usage(usage->hid, f);
- seq_printf(f, " ---> ");
+ seq_puts(f, " ---> ");
hid_resolv_event(usage->type, usage->code, f);
@@ -1063,7 +1063,7 @@ static int hid_debug_rdesc_show(struct seq_file *f, void *p)
/* dump HID report descriptor */
for (i = 0; i < rsize; i++)
seq_printf(f, "%02x ", rdesc[i]);
- seq_printf(f, "\n\n");
+ seq_puts(f, "\n\n");

/* dump parsed data and input mappings */
hid_dump_device(hdev, f);
--
2.12.2

2017-04-24 20:22:37

by SF Markus Elfring

[permalink] [raw]
Subject: [PATCH 6/7] HID: debug: Combine two seq_printf() calls into one call in hid_dump_device()

From: Markus Elfring <[email protected]>
Date: Mon, 24 Apr 2017 21:07:52 +0200

A bit of data was put into a sequence by two separate function calls.
Print the same data by a single function call instead.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <[email protected]>
---
drivers/hid/hid-debug.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
index ce850f80d9e3..1e8ac6e569a2 100644
--- a/drivers/hid/hid-debug.c
+++ b/drivers/hid/hid-debug.c
@@ -646,8 +646,7 @@ void hid_dump_device(struct hid_device *device, struct seq_file *f)
seq_printf(f, "%s", table[i]);
if (report->id)
seq_printf(f, "(%d)", report->id);
- seq_printf(f, "[%s]", table[report->type]);
- seq_printf(f, "\n");
+ seq_printf(f, "[%s]\n", table[report->type]);
for (k = 0; k < report->maxfield; k++) {
tab(4, f);
seq_printf(f, "Field(%d)\n", k);
--
2.12.2

2017-04-24 20:24:22

by SF Markus Elfring

[permalink] [raw]
Subject: [PATCH 7/7] HID: picoLCD: Replace two seq_printf() calls by seq_puts() in picolcd_debug_reset_show()

From: Markus Elfring <[email protected]>
Date: Mon, 24 Apr 2017 21:27:42 +0200

Strings which did not contain data format specifications should be put
into a sequence. Thus use the corresponding function "seq_puts".

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <[email protected]>
---
drivers/hid/hid-picolcd_debugfs.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/hid/hid-picolcd_debugfs.c b/drivers/hid/hid-picolcd_debugfs.c
index 3e0feb4bb538..975f36601d59 100644
--- a/drivers/hid/hid-picolcd_debugfs.c
+++ b/drivers/hid/hid-picolcd_debugfs.c
@@ -33,9 +33,9 @@
static int picolcd_debug_reset_show(struct seq_file *f, void *p)
{
if (picolcd_fbinfo((struct picolcd_data *)f->private))
- seq_printf(f, "all fb\n");
+ seq_puts(f, "all fb\n");
else
- seq_printf(f, "all\n");
+ seq_puts(f, "all\n");
return 0;
}

--
2.12.2

2017-04-25 06:50:16

by Bruno Prémont

[permalink] [raw]
Subject: Re: [PATCH 7/7] HID: picoLCD: Replace two seq_printf() calls by seq_puts() in picolcd_debug_reset_show()

Acked-by: Bruno Prémont <[email protected]>

On Mon, 24 Apr 2017 22:23:02 +0200 SF Markus Elfring wrote:
> From: Markus Elfring <[email protected]>
> Date: Mon, 24 Apr 2017 21:27:42 +0200
>
> Strings which did not contain data format specifications should be put
> into a sequence. Thus use the corresponding function "seq_puts".
>
> This issue was detected by using the Coccinelle software.
>
> Signed-off-by: Markus Elfring <[email protected]>
> ---
> drivers/hid/hid-picolcd_debugfs.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/hid/hid-picolcd_debugfs.c b/drivers/hid/hid-picolcd_debugfs.c
> index 3e0feb4bb538..975f36601d59 100644
> --- a/drivers/hid/hid-picolcd_debugfs.c
> +++ b/drivers/hid/hid-picolcd_debugfs.c
> @@ -33,9 +33,9 @@
> static int picolcd_debug_reset_show(struct seq_file *f, void *p)
> {
> if (picolcd_fbinfo((struct picolcd_data *)f->private))
> - seq_printf(f, "all fb\n");
> + seq_puts(f, "all fb\n");
> else
> - seq_printf(f, "all\n");
> + seq_puts(f, "all\n");
> return 0;
> }
>

2017-04-27 18:17:07

by SF Markus Elfring

[permalink] [raw]
Subject: Re: HID: sensor-hub: Delete error messages for failed memory allocations in sensor_hub_probe()

> WARNING: Possible unnecessary 'out of memory' message

I have noticed a moment ago that a similar change is also contained
in the update suggestion “HID: Remove unnecessary OOM messages”
by Joe Perches from 2017-03-01.
https://patchwork.kernel.org/patch/9598997/
https://lkml.kernel.org/r/<0fbe692283802d36df389af7cbda9a4bff44db5e.1488395879.git.joe@perches.com>

How would you like to continue with source code adjustments there?

Regards,
Markus