Skip to contentSkip to conversation

HID: Fine-tuning for some function implementations

PATCH In review SF Markus Elfring 2 participants 10 messages Apr 24, 2017 – Apr 27, 2017

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

cover 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(-)
Signature · 1 line
2.12.2
#1 lore raw

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.

---
 drivers/hid/hid-sensor-hub.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/hid/hid-sensor-hub.c +5 -3
index 4ef73374a8f9..349494fc8def 100644
@@ -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;
Signature · 1 line
2.12.2
#2 lore raw

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

In reply to SF Markus Elfring

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.

---
 drivers/hid/hid-sensor-hub.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/hid/hid-sensor-hub.c +1 -5
index 349494fc8def..feb4bb8ee3ec 100644
@@ -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;
 			}
Signature · 1 line
2.12.2
#4 lore raw

PATCH 3/7 HID: sensor-hub: Adjust two checks for null pointers in sensor_hub_probe()

In reply to SF Markus Elfring

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.

---
 drivers/hid/hid-sensor-hub.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hid/hid-sensor-hub.c +2 -2
index feb4bb8ee3ec..4eadaef19e6a 100644
@@ -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;
Signature · 1 line
2.12.2
#5 lore raw

PATCH 4/7 HID: debug: Replace five seq_printf() calls by seq_putc()

In reply to SF Markus Elfring

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.

---
 drivers/hid/hid-debug.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/hid/hid-debug.c +6 -5
index 5271db593478..d47d5085bc99 100644
@@ -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;
Signature · 1 line
2.12.2
#6 lore raw

PATCH 5/7 HID: debug: Replace 12 seq_printf() calls by seq_puts()

In reply to SF Markus Elfring

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.

---
 drivers/hid/hid-debug.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/hid/hid-debug.c +12 -12
index d47d5085bc99..ce850f80d9e3 100644
@@ -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);
Signature · 1 line
2.12.2
#7 lore raw

PATCH 6/7 HID: debug: Combine two seq_printf() calls into one call in hid_dump_device()

In reply to SF Markus Elfring

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.

---
 drivers/hid/hid-debug.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/hid/hid-debug.c +1 -2
index ce850f80d9e3..1e8ac6e569a2 100644
@@ -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);
Signature · 1 line
2.12.2
#8 lore raw

PATCH 7/7 HID: picoLCD: Replace two seq_printf() calls by seq_puts() in picolcd_debug_reset_show()

In reply to SF Markus Elfring

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.

---
 drivers/hid/hid-picolcd_debugfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hid/hid-picolcd_debugfs.c +2 -2
index 3e0feb4bb538..975f36601d59 100644
@@ -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;
 }
 
Signature · 1 line
2.12.2
maintainer Acked-by on v1 7/7
#9 lore raw

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

Quoted 19 lines · 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.

---
 drivers/hid/hid-picolcd_debugfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hid/hid-picolcd_debugfs.c +0 -0
index 3e0feb4bb538..975f36601d59 100644
@@ -33,9 +33,9 @@
 static int picolcd_debug_reset_show(struct seq_file *f, void *p)

drivers/hid/hid-picolcd_debugfs.c line 41

Quoted 10 lines
drivers/hid/hid-picolcd_debugfs.c +2 -2
 {
 	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;
 }
 

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw

Re: HID: sensor-hub: Delete error messages for failed memory allocations in sensor_hub_probe()

In reply to SF Markus Elfring
Quoted 1 line

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