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


Groups > linux.kernel > #1490629 > unrolled thread

Re: [PATCH 01/] firewire-net: Use kmalloc_array() in fwnet_broadcast_start()

Started byStefan Richter <stefanr@s5r6.in-berlin.de>
First post2016-09-24 13:50 +0200
Last post2016-09-24 17:40 +0200
Articles 2 — 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

  Re: [PATCH 01/] firewire-net: Use kmalloc_array() in  fwnet_broadcast_start() Stefan Richter <stefanr@s5r6.in-berlin.de> - 2016-09-24 13:50 +0200
    Re: [PATCH 01/10] firewire-net: Use kmalloc_array() in  fwnet_broadcast_start() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-09-24 17:40 +0200

#1490629 — Re: [PATCH 01/] firewire-net: Use kmalloc_array() in fwnet_broadcast_start()

FromStefan Richter <stefanr@s5r6.in-berlin.de>
Date2016-09-24 13:50 +0200
SubjectRe: [PATCH 01/] firewire-net: Use kmalloc_array() in fwnet_broadcast_start()
Message-ID<skTCV-tL-5@gated-at.bofh.it>
On Sep 18 SF Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Sat, 17 Sep 2016 21:55:42 +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.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  drivers/firewire/net.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/firewire/net.c b/drivers/firewire/net.c
> index 309311b..7911f13 100644
> --- a/drivers/firewire/net.c
> +++ b/drivers/firewire/net.c
> @@ -1103,8 +1103,7 @@ static int fwnet_broadcast_start(struct fwnet_device *dev)
>  
>  	max_receive = 1U << (dev->card->max_receive + 1);
>  	num_packets = (FWNET_ISO_PAGE_COUNT * PAGE_SIZE) / max_receive;
> -
> -	ptrptr = kmalloc(sizeof(void *) * num_packets, GFP_KERNEL);
> +	ptrptr = kmalloc_array(num_packets, sizeof(*ptrptr), GFP_KERNEL);
>  	if (!ptrptr) {
>  		retval = -ENOMEM;
>  		goto failed;

Coccinelle enabled you to determine that kmalloc_array /could/ be used
here.  But whether it /should/ be used here is another question, and it is
not addressed in your changelog.  (You state that there is an "issue" but
do not explain.)

kmalloc_array is a kmalloc wrapper which adds an inline check for integer
overflow.  So, can sizeof(void *) * num_packets ever overflow size_t?

If yes,
	do we want a runtime check here (which kmalloc_array provides),
	or do we want a compile-time check?

If no,
	then the remaining benefit of the patch is that it is more obvious
	to the reader that dev->broadcast_rcv_buffer_ptrs is an array,
	but possibly at the cost of superfluous code.  Is gcc's optimizer
	able to resolve kmalloc_array's check at compile time as always
	false, such that the superfluous code is eliminated as dead code?

I believe I know answers to this but prefer to hear what you as the patch
author think about it.
-- 
Stefan Richter
-======----- =--= ==---
http://arcgraph.de/sr/

[toc] | [next] | [standalone]


#1490666 — Re: [PATCH 01/10] firewire-net: Use kmalloc_array() in fwnet_broadcast_start()

FromSF Markus Elfring <elfring@users.sourceforge.net>
Date2016-09-24 17:40 +0200
SubjectRe: [PATCH 01/10] firewire-net: Use kmalloc_array() in fwnet_broadcast_start()
Message-ID<skXdv-2Iz-11@gated-at.bofh.it>
In reply to#1490629
>> @@ -1103,8 +1103,7 @@ static int fwnet_broadcast_start(struct fwnet_device *dev)
>>  
>>  	max_receive = 1U << (dev->card->max_receive + 1);
>>  	num_packets = (FWNET_ISO_PAGE_COUNT * PAGE_SIZE) / max_receive;
>> -
>> -	ptrptr = kmalloc(sizeof(void *) * num_packets, GFP_KERNEL);
>> +	ptrptr = kmalloc_array(num_packets, sizeof(*ptrptr), GFP_KERNEL);
>>  	if (!ptrptr) {
>>  		retval = -ENOMEM;
>>  		goto failed;
> 
> Coccinelle enabled you to determine that kmalloc_array /could/ be used here.

A script for the semantic patch language pointed hundreds of source files out
with such software update opportunities.


> But whether it /should/ be used here is another question, and it is
> not addressed in your changelog.

I can expand the corresponding description when it will be desired.


> (You state that there is an "issue" but do not explain.)

Do you prefer an other wording for such an update candidate?


> kmalloc_array is a kmalloc wrapper which adds an inline check for integer
> overflow.  So, can sizeof(void *) * num_packets ever overflow size_t?
> 
> If yes,

Is there a probability that the calculated number of packets will become
too big for the preferred system limits anyhow?


> 	do we want a runtime check here (which kmalloc_array provides),

Did you notice the information from the commit "mm: faster kmalloc_array(), kcalloc()"
(from 2016-07-26) already?
https://git.kernel.org/cgit/linux/kernel/git/next/linux-next.git/commit/?id=91c6a05f72a996bee5133e76374ab3ad7d3b9b72


> 	or do we want a compile-time check?

I guess that some software developers and subsystem maintainers are looking
for a bit more clarification around involved design dependencies.


> If no,
> 	then the remaining benefit of the patch is that it is more obvious
> 	to the reader that dev->broadcast_rcv_buffer_ptrs is an array,

How do you value such a kind of source code annotation?


> 	but possibly at the cost of superfluous code.

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


> 	Is gcc's optimizer able to resolve kmalloc_array's check at compile time
> 	as always false, such that the superfluous code is eliminated as dead code?

Which versions of compiler implementations would you like to check further?


> I believe I know answers to this but prefer to hear what you as the patch
> author think about it.

I presented another update suggestion also for this software module as a result
from a general source code search pattern.
The corresponding change acceptance varies and is evolving as usual.

Regards,
Markus

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web