Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1490626 > unrolled thread

[PATCH 1/2] Input-evdev: Use kmalloc_array() in evdev_handle_get_val()

Started bySF Markus Elfring <elfring@users.sourceforge.net>
First post2016-09-24 13:10 +0200
Last post2016-09-24 21:10 +0200
Articles 5 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 1/2] Input-evdev: Use kmalloc_array() in  evdev_handle_get_val() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-09-24 13:10 +0200
    Re: [PATCH 1/2] Input-evdev: Use kmalloc_array() in evdev_handle_get_val() Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2016-09-24 20:00 +0200
      Re: Input-evdev: Use kmalloc_array() in evdev_handle_get_val() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-09-24 20:20 +0200
        Re: Input-evdev: Use kmalloc_array() in evdev_handle_get_val() Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2016-09-24 20:40 +0200
          Re: Input-evdev: Use kmalloc_array() in evdev_handle_get_val() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-09-24 21:10 +0200

#1490626 — [PATCH 1/2] Input-evdev: Use kmalloc_array() in evdev_handle_get_val()

FromSF Markus Elfring <elfring@users.sourceforge.net>
Date2016-09-24 13:10 +0200
Subject[PATCH 1/2] Input-evdev: Use kmalloc_array() in evdev_handle_get_val()
Message-ID<skT0d-hj-1@gated-at.bofh.it>
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sat, 24 Sep 2016 12:42:45 +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 "kmalloc_array".

  This issue was detected by using the Coccinelle software.

* Replace the specification of a data type by a pointer dereference
  to make the corresponding size determination a bit safer according to
  the Linux coding style convention.

* Delete the local variable "len" which became unnecessary with
  this refactoring.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/input/evdev.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c
index e9ae3d5..83fcfd6 100644
--- a/drivers/input/evdev.c
+++ b/drivers/input/evdev.c
@@ -919,18 +919,14 @@ static int evdev_handle_get_val(struct evdev_client *client,
 {
 	int ret;
 	unsigned long *mem;
-	size_t len;
 
-	len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
-	mem = kmalloc(len, GFP_KERNEL);
+	mem = kmalloc_array(BITS_TO_LONGS(maxbit), sizeof(*mem), GFP_KERNEL);
 	if (!mem)
 		return -ENOMEM;
 
 	spin_lock_irq(&dev->event_lock);
 	spin_lock(&client->buffer_lock);
-
-	memcpy(mem, bits, len);
-
+	memcpy(mem, bits, sizeof(*mem) * BITS_TO_LONGS(maxbit));
 	spin_unlock(&dev->event_lock);
 
 	__evdev_flush_queue(client, type);
-- 
2.10.0

[toc] | [next] | [standalone]


#1490704 — Re: [PATCH 1/2] Input-evdev: Use kmalloc_array() in evdev_handle_get_val()

FromDmitry Torokhov <dmitry.torokhov@gmail.com>
Date2016-09-24 20:00 +0200
SubjectRe: [PATCH 1/2] Input-evdev: Use kmalloc_array() in evdev_handle_get_val()
Message-ID<skZp0-3Zr-1@gated-at.bofh.it>
In reply to#1490626
On Sat, Sep 24, 2016 at 4:08 AM, SF Markus Elfring
<elfring@users.sourceforge.net> wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Sat, 24 Sep 2016 12:42:45 +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 "kmalloc_array".
>
>   This issue was detected by using the Coccinelle software.
>
> * Replace the specification of a data type by a pointer dereference
>   to make the corresponding size determination a bit safer according to
>   the Linux coding style convention.
>
> * Delete the local variable "len" which became unnecessary with
>   this refactoring.

So we have to multiply twice now, once in kmalloc_array, the second
time in memcpy(). No, thank you.

Also, please note that we do not really treat the allocated "mem" as
an array, but rather area of memory that holds all bits that we need
to transfer, and so I consider using kmalloc_array() actually wrong
here.

Please do not blindly follow checkpatch and coccinelle suggestions.
They are just that: suggestions and not hared rules.

Thanks.

-- 
Dmitry

[toc] | [prev] | [next] | [standalone]


#1490708 — Re: Input-evdev: Use kmalloc_array() in evdev_handle_get_val()

FromSF Markus Elfring <elfring@users.sourceforge.net>
Date2016-09-24 20:20 +0200
SubjectRe: Input-evdev: Use kmalloc_array() in evdev_handle_get_val()
Message-ID<skZIl-4oj-1@gated-at.bofh.it>
In reply to#1490704
> So we have to multiply twice now, once in kmalloc_array, the second
> time in memcpy().

It looks so in the source code after the suggested refactoring.


> No, thank you.

Would you like to check any further if a specific compiler implementation
will still optimise common subexpressions as you desired it?


> Also, please note that we do not really treat the allocated "mem" as an array,
> but rather area of memory that holds all bits that we need to transfer,
> and so I consider using kmalloc_array() actually wrong here.

Thanks for your explanation.


> Please do not blindly follow checkpatch and coccinelle suggestions.
> They are just that: suggestions and not hared rules.

I am curious on how to clarify corresponding deviations further.


Would you like to suggest any other details so that the evolving scripts
can become better and safer for static source code analysis?

Do you know any special properties which should be additionally checked
at call sites which are similar to the discussed place?

Regards,
Markus

[toc] | [prev] | [next] | [standalone]


#1490713 — Re: Input-evdev: Use kmalloc_array() in evdev_handle_get_val()

FromDmitry Torokhov <dmitry.torokhov@gmail.com>
Date2016-09-24 20:40 +0200
SubjectRe: Input-evdev: Use kmalloc_array() in evdev_handle_get_val()
Message-ID<sl01H-4uy-9@gated-at.bofh.it>
In reply to#1490708
On Sat, Sep 24, 2016 at 08:16:16PM +0200, SF Markus Elfring wrote:
> > So we have to multiply twice now, once in kmalloc_array, the second
> > time in memcpy().
> 
> It looks so in the source code after the suggested refactoring.
> 
> 
> > No, thank you.
> 
> Would you like to check any further if a specific compiler implementation
> will still optimise common subexpressions as you desired it?
> 
> 
> > Also, please note that we do not really treat the allocated "mem" as an array,
> > but rather area of memory that holds all bits that we need to transfer,
> > and so I consider using kmalloc_array() actually wrong here.
> 
> Thanks for your explanation.
> 
> 
> > Please do not blindly follow checkpatch and coccinelle suggestions.
> > They are just that: suggestions and not hared rules.
> 
> I am curious on how to clarify corresponding deviations further.
> 
> 
> Would you like to suggest any other details so that the evolving scripts
> can become better and safer for static source code analysis?
> 
> Do you know any special properties which should be additionally checked
> at call sites which are similar to the discussed place?

If you are asking for some formal rules then no.

Again, what is the purpose of the changes? Are you working on the code
and the fact that the driver is older-style hinders your progress? Or
there are runtime improvements from your changes? Correctness issues?

I do not very much appreciate changes just to satisfy checkpatch rule
du jour.

Thanks.

-- 
Dmitry

[toc] | [prev] | [next] | [standalone]


#1490724 — Re: Input-evdev: Use kmalloc_array() in evdev_handle_get_val()

FromSF Markus Elfring <elfring@users.sourceforge.net>
Date2016-09-24 21:10 +0200
SubjectRe: Input-evdev: Use kmalloc_array() in evdev_handle_get_val()
Message-ID<sl0uK-4TP-13@gated-at.bofh.it>
In reply to#1490713
> Again, what is the purpose of the changes?

I came also along a few source code places where their maintainers
requested the usage of a function like "kmalloc_array".

How do you think about to clarify the consistent use of programming
interfaces for Linux a bit more?


> Are you working on the code

I am trying to improve various free software (including Linux).


> and the fact that the driver is older-style hinders your progress?

Not for me directly.


> Or there are runtime improvements from your changes?

It depends on some factors.

Can an array memory allocator organise the desired data in a safer
and more efficient way than a "default approach"?


> Correctness issues?

This can be.

Do you care for source code annotations?


> I do not very much appreciate changes just to satisfy checkpatch rule
> du jour.

Do these rules contain knowledge which you give a significant value?

Regards,
Markus

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web