Skip to contentSkip to conversation

input: ipaq-micro-keys: simplify allocation

PATCH Superseded Rosen Penev 2 participants 4 messages Jun 8 – Jun 9

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 input: ipaq-micro-keys: simplify allocation

Use a flexible array member to have a single allocation.

---
 drivers/input/keyboard/ipaq-micro-keys.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/input/keyboard/ipaq-micro-keys.c +3 -6
index ca7ec054b1ce..0fc5b5dcdef5 100644
@@ -23,7 +23,7 @@
 struct ipaq_micro_keys {
 	struct ipaq_micro *micro;
 	struct input_dev *input;
-	u16 *codes;
+	u16 codes[];
 };
 
 static const u16 micro_keycodes[] = {
@@ -90,10 +90,11 @@ static int micro_key_probe(struct platform_device *pdev)
 	int error;
 	int i;
 
-	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
+	keys = devm_kzalloc(&pdev->dev, struct_size(keys, codes, ARRAY_SIZE(micro_keycodes)), GFP_KERNEL);
 	if (!keys)
 		return -ENOMEM;
 
+	memcpy(keys->codes, micro_keycodes, ARRAY_SIZE(micro_keycodes) * sizeof(*keys->codes));
 	keys->micro = dev_get_drvdata(pdev->dev.parent);
 
 	keys->input = devm_input_allocate_device(&pdev->dev);
@@ -102,10 +103,6 @@ static int micro_key_probe(struct platform_device *pdev)
 
 	keys->input->keycodesize = sizeof(micro_keycodes[0]);
 	keys->input->keycodemax = ARRAY_SIZE(micro_keycodes);
-	keys->codes = devm_kmemdup_array(&pdev->dev, micro_keycodes, keys->input->keycodemax,
-					 keys->input->keycodesize, GFP_KERNEL);
-	if (!keys->codes)
-		return -ENOMEM;
 
 	keys->input->keycode = keys->codes;
 
Signature · 1 line
2.54.0
maintainer
#1 lore raw

Hi Rosen,

Quoted 1 line · Rosen Penev

Use a flexible array member to have a single allocation.

Why does it have to be flexible? The size is known.

Quoted 20 lines
---
 drivers/input/keyboard/ipaq-micro-keys.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/input/keyboard/ipaq-micro-keys.c +1 -1
index ca7ec054b1ce..0fc5b5dcdef5 100644
@@ -23,7 +23,7 @@
 struct ipaq_micro_keys {
 	struct ipaq_micro *micro;
 	struct input_dev *input;
-	u16 *codes;
+	u16 codes[];
 };
 
 static const u16 micro_keycodes[] = {

drivers/input/keyboard/ipaq-micro-keys.c static int micro_key_probe(struct platform_device *pdev) · line 93

Quoted 5 lines
drivers/input/keyboard/ipaq-micro-keys.c +1 -1
@@ -90,10 +90,11 @@ static int micro_key_probe(struct platform_device *pdev)
 	int error;
 	int i;
 
-	keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
+	keys = devm_kzalloc(&pdev->dev, struct_size(keys, codes, ARRAY_SIZE(micro_keycodes)), GFP_KERNEL);

Please keep to the limits for the kernel. 100 columns is acceptable when leads to better readability, but I prefer 80 when it makes sense.

checkpatch.pl would have warned you.

Thanks.

Signature · 1 line
Dmitry
#2 lore raw
Quoted 7 lines · Dmitry Torokhov

Hi Rosen,

Quoted 1 line · Rosen Penev

Use a flexible array member to have a single allocation.

Why does it have to be flexible? The size is known.

From a quick glance, that would be a compilation error. micro_keycodes gets declared after ipaq_micro_keys

From looking at a bunch of struct definitions, [ARRAY_SIZE(foo)] seems to be atypical.

Quoted 24 lines
Quoted 24 lines
---
 drivers/input/keyboard/ipaq-micro-keys.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/input/keyboard/ipaq-micro-keys.c +1 -1
index ca7ec054b1ce..0fc5b5dcdef5 100644
@@ -23,7 +23,7 @@
 struct ipaq_micro_keys {
      struct ipaq_micro *micro;
      struct input_dev *input;
-     u16 *codes;
+     u16 codes[];
 };
 
 static const u16 micro_keycodes[] = {
@@ -90,10 +90,11 @@ static int micro_key_probe(struct platform_device *pdev)
      int error;
      int i;
 
Quoted 5 lines

drivers/input/keyboard/ipaq-micro-keys.c static int micro_key_probe(struct platform_device *pdev) · line 93

Quoted 2 lines
drivers/input/keyboard/ipaq-micro-keys.c +1 -1
-     keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL);
+     keys = devm_kzalloc(&pdev->dev, struct_size(keys, codes, ARRAY_SIZE(micro_keycodes)), GFP_KERNEL);

Please keep to the limits for the kernel. 100 columns is acceptable when leads to better readability, but I prefer 80 when it makes sense.

Seems I forgot to run git clang-format here. Will resend.

Quoted 7 lines

checkpatch.pl would have warned you.

Thanks.

--
Dmitry

maintainer
#3 lore raw
Quoted 11 lines · Rosen Penev
Quoted 7 lines · Dmitry Torokhov

Hi Rosen,

Quoted 1 line · Rosen Penev

Use a flexible array member to have a single allocation.

Why does it have to be flexible? The size is known.

From a quick glance, that would be a compilation error. micro_keycodes gets declared after ipaq_micro_keys

We could swap them around, no big deal.

Quoted 3 lines

From looking at a bunch of struct definitions, [ARRAY_SIZE(foo)] seems to be atypical.

There are a lot of "#define BLAH_SIZE ARRAY_SIZE(blah)" that later
used in array definitions.

Thanks.

Signature · 1 line
Dmitry