All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: wharms@bfs.de
Cc: "Julia Lawall" <julia.lawall@lip6.fr>,
	"SF Markus Elfring" <elfring@users.sourceforge.net>,
	kvm@vger.kernel.org, linux-s390@vger.kernel.org,
	"Christian Bornträger" <borntraeger@de.ibm.com>,
	"Cornelia Huck" <cornelia.huck@de.ibm.com>,
	"David Hildenbrand" <dahi@linux.vnet.ibm.com>,
	"Heiko Carstens" <heiko.carstens@de.ibm.com>,
	"Martin Schwidefsky" <schwidefsky@de.ibm.com>,
	"Radim Krčmář" <rkrcmar@redhat.com>,
	LKML <linux-kernel@vger.kernel.org>,
	kernel-janitors@vger.kernel.org
Subject: Re: [PATCH 1/4] KVM-S390: Improve determination of sizes in kvm_s390_import_bp_data()
Date: Thu, 18 Aug 2016 12:55:11 +0200	[thread overview]
Message-ID: <38bba070-d157-dc49-b428-47768ad647ca@redhat.com> (raw)
In-Reply-To: <57B5935B.3050602@bfs.de>



On 18/08/2016 12:52, walter harms wrote:
> 
> 
> Am 18.08.2016 11:48, schrieb Paolo Bonzini:
>>
>>
>> On 18/08/2016 11:02, Julia Lawall wrote:
>>>
>>>
>>> On Thu, 18 Aug 2016, walter harms wrote:
>>>
>>>>
>>>>
>>>> Am 17.08.2016 20:06, schrieb SF Markus Elfring:
>>>>> From: Markus Elfring <elfring@users.sourceforge.net>
>>>>> Date: Wed, 17 Aug 2016 18:29:04 +0200
>>>>>
>>>>> Replace the specification of data structures by pointer dereferences
>>>>> 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>
>>>>> ---
>>>>>  arch/s390/kvm/guestdbg.c | 6 +++---
>>>>>  1 file changed, 3 insertions(+), 3 deletions(-)
>>>>>
>>>>> diff --git a/arch/s390/kvm/guestdbg.c b/arch/s390/kvm/guestdbg.c
>>>>> index d1f8241..b68db4b 100644
>>>>> --- a/arch/s390/kvm/guestdbg.c
>>>>> +++ b/arch/s390/kvm/guestdbg.c
>>>>> @@ -216,7 +216,7 @@ int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
>>>>>  	else if (dbg->arch.nr_hw_bp > MAX_BP_COUNT)
>>>>>  		return -EINVAL;
>>>>>
>>>>> -	size = dbg->arch.nr_hw_bp * sizeof(struct kvm_hw_breakpoint);
>>>>> +	size = dbg->arch.nr_hw_bp * sizeof(*bp_data);
>>>>>  	bp_data = kmalloc(size, GFP_KERNEL);
>>>>>  	if (!bp_data) {
>>>>>  		ret = -ENOMEM;
>>>>> @@ -241,7 +241,7 @@ int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
>>>>>  		}
>>>>>  	}
>>>>>
>>>>> -	size = nr_wp * sizeof(struct kvm_hw_wp_info_arch);
>>>>> +	size = nr_wp * sizeof(*wp_info);
>>>>>  	if (size > 0) {
>>>>>  		wp_info = kmalloc(size, GFP_KERNEL);
>>>>>  		if (!wp_info) {
>>>>> @@ -249,7 +249,7 @@ int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
>>>>>  			goto error;
>>>>>  		}
>>>>>  	}
>>>>> -	size = nr_bp * sizeof(struct kvm_hw_bp_info_arch);
>>>>> +	size = nr_bp * sizeof(*bp_info);
>>>>>  	if (size > 0) {
>>>>>  		bp_info = kmalloc(size, GFP_KERNEL);
>>>>>  		if (!bp_info) {
>>>>
>>>>
>>>> IMHO the common pattern for kmalloc is
>>>>   bp_info = kmalloc( nr_bp * sizeof(*bp_info), GFP_KERNEL);
>>>>
>>>> i can not remember code with a check for size < 0, i guess it is here
>>>> to avoid an overflow ? since kmalloc takes a size_t argument this would cause
>>>> a malloc failure an can be ignored.
>>>
>>> Shoudn't it be kcalloc?
>>
>> Or kmalloc_array, since zeroing is not necessary.  Might be an idea for
>> a new Coccinelle script, like
>>
>> - kmalloc (N * sizeof T, GFP)
>> + kmalloc_array(N, sizeof T, GFP)
>>
> 
> 
> my personal taste is to stay close to the libc functions.
> technical there is no difference
> 
> static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
>  {
>         return kmalloc_array(n, size, flags | __GFP_ZERO);
>  }
> 
> and i do not see any time critical things here,

This is _not_ premature optimization.  (k)calloc tells the reader that
it's safe not to initialize part of the array.  kmalloc_array says the
opposite.  Using the right function adds important hints in the
code---which unlike comments cannot get stale without also introducing
visible bugs.

Paolo

WARNING: multiple messages have this Message-ID (diff)
From: Paolo Bonzini <pbonzini@redhat.com>
To: wharms@bfs.de
Cc: "Julia Lawall" <julia.lawall@lip6.fr>,
	"SF Markus Elfring" <elfring@users.sourceforge.net>,
	kvm@vger.kernel.org, linux-s390@vger.kernel.org,
	"Christian Bornträger" <borntraeger@de.ibm.com>,
	"Cornelia Huck" <cornelia.huck@de.ibm.com>,
	"David Hildenbrand" <dahi@linux.vnet.ibm.com>,
	"Heiko Carstens" <heiko.carstens@de.ibm.com>,
	"Martin Schwidefsky" <schwidefsky@de.ibm.com>,
	"Radim Krčmář" <rkrcmar@redhat.com>,
	LKML <linux-kernel@vger.kernel.org>,
	kernel-janitors@vger.kernel.org
Subject: Re: [PATCH 1/4] KVM-S390: Improve determination of sizes in kvm_s390_import_bp_data()
Date: Thu, 18 Aug 2016 10:55:11 +0000	[thread overview]
Message-ID: <38bba070-d157-dc49-b428-47768ad647ca@redhat.com> (raw)
In-Reply-To: <57B5935B.3050602@bfs.de>



On 18/08/2016 12:52, walter harms wrote:
> 
> 
> Am 18.08.2016 11:48, schrieb Paolo Bonzini:
>>
>>
>> On 18/08/2016 11:02, Julia Lawall wrote:
>>>
>>>
>>> On Thu, 18 Aug 2016, walter harms wrote:
>>>
>>>>
>>>>
>>>> Am 17.08.2016 20:06, schrieb SF Markus Elfring:
>>>>> From: Markus Elfring <elfring@users.sourceforge.net>
>>>>> Date: Wed, 17 Aug 2016 18:29:04 +0200
>>>>>
>>>>> Replace the specification of data structures by pointer dereferences
>>>>> 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>
>>>>> ---
>>>>>  arch/s390/kvm/guestdbg.c | 6 +++---
>>>>>  1 file changed, 3 insertions(+), 3 deletions(-)
>>>>>
>>>>> diff --git a/arch/s390/kvm/guestdbg.c b/arch/s390/kvm/guestdbg.c
>>>>> index d1f8241..b68db4b 100644
>>>>> --- a/arch/s390/kvm/guestdbg.c
>>>>> +++ b/arch/s390/kvm/guestdbg.c
>>>>> @@ -216,7 +216,7 @@ int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
>>>>>  	else if (dbg->arch.nr_hw_bp > MAX_BP_COUNT)
>>>>>  		return -EINVAL;
>>>>>
>>>>> -	size = dbg->arch.nr_hw_bp * sizeof(struct kvm_hw_breakpoint);
>>>>> +	size = dbg->arch.nr_hw_bp * sizeof(*bp_data);
>>>>>  	bp_data = kmalloc(size, GFP_KERNEL);
>>>>>  	if (!bp_data) {
>>>>>  		ret = -ENOMEM;
>>>>> @@ -241,7 +241,7 @@ int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
>>>>>  		}
>>>>>  	}
>>>>>
>>>>> -	size = nr_wp * sizeof(struct kvm_hw_wp_info_arch);
>>>>> +	size = nr_wp * sizeof(*wp_info);
>>>>>  	if (size > 0) {
>>>>>  		wp_info = kmalloc(size, GFP_KERNEL);
>>>>>  		if (!wp_info) {
>>>>> @@ -249,7 +249,7 @@ int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
>>>>>  			goto error;
>>>>>  		}
>>>>>  	}
>>>>> -	size = nr_bp * sizeof(struct kvm_hw_bp_info_arch);
>>>>> +	size = nr_bp * sizeof(*bp_info);
>>>>>  	if (size > 0) {
>>>>>  		bp_info = kmalloc(size, GFP_KERNEL);
>>>>>  		if (!bp_info) {
>>>>
>>>>
>>>> IMHO the common pattern for kmalloc is
>>>>   bp_info = kmalloc( nr_bp * sizeof(*bp_info), GFP_KERNEL);
>>>>
>>>> i can not remember code with a check for size < 0, i guess it is here
>>>> to avoid an overflow ? since kmalloc takes a size_t argument this would cause
>>>> a malloc failure an can be ignored.
>>>
>>> Shoudn't it be kcalloc?
>>
>> Or kmalloc_array, since zeroing is not necessary.  Might be an idea for
>> a new Coccinelle script, like
>>
>> - kmalloc (N * sizeof T, GFP)
>> + kmalloc_array(N, sizeof T, GFP)
>>
> 
> 
> my personal taste is to stay close to the libc functions.
> technical there is no difference
> 
> static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
>  {
>         return kmalloc_array(n, size, flags | __GFP_ZERO);
>  }
> 
> and i do not see any time critical things here,

This is _not_ premature optimization.  (k)calloc tells the reader that
it's safe not to initialize part of the array.  kmalloc_array says the
opposite.  Using the right function adds important hints in the
code---which unlike comments cannot get stale without also introducing
visible bugs.

Paolo

  reply	other threads:[~2016-08-18 10:56 UTC|newest]

Thread overview: 94+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-17 18:02 [PATCH 0/4] KVM-S390: Fine-tuning for kvm_s390_import_bp_data() SF Markus Elfring
2016-08-17 18:02 ` SF Markus Elfring
2016-08-17 18:06 ` [PATCH 1/4] KVM-S390: Improve determination of sizes in kvm_s390_import_bp_data() SF Markus Elfring
2016-08-17 18:06   ` SF Markus Elfring
2016-08-18  7:25   ` walter harms
2016-08-18  7:25     ` walter harms
2016-08-18  9:02     ` Julia Lawall
2016-08-18  9:02       ` Julia Lawall
2016-08-18  9:48       ` Paolo Bonzini
2016-08-18  9:48         ` Paolo Bonzini
2016-08-18 10:52         ` walter harms
2016-08-18 10:52           ` walter harms
2016-08-18 10:55           ` Paolo Bonzini [this message]
2016-08-18 10:55             ` Paolo Bonzini
2016-08-22 12:58             ` Cornelia Huck
2016-08-22 12:58               ` Cornelia Huck
2016-08-24 12:10         ` Replacing specific kmalloc() calls by kmalloc_array()? SF Markus Elfring
2016-08-24 12:10           ` SF Markus Elfring
2016-08-24 15:00           ` Paolo Bonzini
2016-08-24 15:00             ` Paolo Bonzini
2016-08-17 18:08 ` [PATCH 2/4] KVM-S390: Use memdup_user() rather than duplicating its implementation SF Markus Elfring
2016-08-17 18:08   ` SF Markus Elfring
2016-08-22 13:02   ` David Hildenbrand
2016-08-22 13:02     ` David Hildenbrand
2016-08-22 13:05   ` Cornelia Huck
2016-08-22 13:05     ` Cornelia Huck
2016-08-24 15:19   ` Christian Borntraeger
2016-08-24 15:19     ` Christian Borntraeger
2016-08-24 18:30     ` [PATCH v2 0/2] KVM: s390: Fine-tuning for kvm_s390_import_bp_data() SF Markus Elfring
2016-08-24 18:30       ` SF Markus Elfring
2016-08-24 18:36       ` [PATCH v2 1/2] KVM: s390: Improve determination of sizes in kvm_s390_import_bp_data() SF Markus Elfring
2016-08-24 18:36         ` SF Markus Elfring
2016-08-25 16:10         ` Cornelia Huck
2016-08-25 16:10           ` Cornelia Huck
2016-08-25 16:45           ` SF Markus Elfring
2016-08-25 16:45             ` SF Markus Elfring
2016-08-25 17:34           ` Software evolution around scripts for the semantic patch langugae SF Markus Elfring
2016-08-25 17:34             ` SF Markus Elfring
2016-08-25 17:40             ` Cornelia Huck
2016-08-25 17:40               ` Cornelia Huck
2016-08-25 17:54               ` SF Markus Elfring
2016-08-25 17:54                 ` SF Markus Elfring
2016-08-25 18:14             ` Julia Lawall
2016-08-25 18:14               ` Julia Lawall
2016-08-25 18:20               ` SF Markus Elfring
2016-08-25 18:20                 ` SF Markus Elfring
2016-08-25 18:23                 ` Julia Lawall
2016-08-25 18:23                   ` Julia Lawall
2016-08-25 21:04                   ` Cornelia Huck
2016-08-25 21:04                     ` Cornelia Huck
2016-08-24 18:40       ` [PATCH v2 2/2] KVM: s390: Use memdup_user() rather than duplicating code SF Markus Elfring
2016-08-24 18:40         ` SF Markus Elfring
2016-08-25 16:11         ` Cornelia Huck
2016-08-25 16:11           ` Cornelia Huck
2016-10-03 12:11         ` Geert Uytterhoeven
2016-10-03 12:11           ` Geert Uytterhoeven
2016-10-03 12:28           ` SF Markus Elfring
2016-10-03 12:28             ` SF Markus Elfring
2016-10-03 13:10             ` Geert Uytterhoeven
2016-10-03 13:10               ` Geert Uytterhoeven
2016-10-03 13:47               ` SF Markus Elfring
2016-10-03 13:47                 ` SF Markus Elfring
2016-10-03 14:00                 ` Geert Uytterhoeven
2016-10-03 14:00                   ` Geert Uytterhoeven
2016-10-03 14:25                   ` SF Markus Elfring
2016-10-03 14:25                     ` SF Markus Elfring
2016-08-25 16:12       ` [PATCH v2 0/2] KVM: s390: Fine-tuning for kvm_s390_import_bp_data() Christian Borntraeger
2016-08-25 16:12         ` Christian Borntraeger
2016-08-17 18:10 ` [PATCH 3/4] KVM-S390: Less function calls in kvm_s390_import_bp_data() after error detection SF Markus Elfring
2016-08-17 18:10   ` SF Markus Elfring
2016-08-22 12:58   ` David Hildenbrand
2016-08-22 12:58     ` David Hildenbrand
2016-08-22 13:00   ` Cornelia Huck
2016-08-22 13:00     ` Cornelia Huck
2016-08-22 16:56     ` SF Markus Elfring
2016-08-22 16:56       ` SF Markus Elfring
2016-08-22 19:37       ` Cornelia Huck
2016-08-22 19:37         ` Cornelia Huck
2016-08-22 21:17         ` SF Markus Elfring
2016-08-22 21:17           ` SF Markus Elfring
2016-08-22 21:28           ` Cornelia Huck
2016-08-22 21:28             ` Cornelia Huck
2016-08-31 12:29           ` Paolo Bonzini
2016-08-31 12:29             ` Paolo Bonzini
2016-08-24 15:10   ` Christian Borntraeger
2016-08-24 15:10     ` Christian Borntraeger
2016-08-27 16:12     ` SF Markus Elfring
2016-08-27 16:12       ` SF Markus Elfring
2016-08-17 18:12 ` [PATCH 4/4] KVM-S390: Delete an unnecessary initialisation for a buffer variable SF Markus Elfring
2016-08-17 18:12   ` SF Markus Elfring
2016-08-22 12:59   ` David Hildenbrand
2016-08-22 12:59     ` David Hildenbrand
2016-08-22 13:01   ` Cornelia Huck
2016-08-22 13:01     ` Cornelia Huck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=38bba070-d157-dc49-b428-47768ad647ca@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=borntraeger@de.ibm.com \
    --cc=cornelia.huck@de.ibm.com \
    --cc=dahi@linux.vnet.ibm.com \
    --cc=elfring@users.sourceforge.net \
    --cc=heiko.carstens@de.ibm.com \
    --cc=julia.lawall@lip6.fr \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=rkrcmar@redhat.com \
    --cc=schwidefsky@de.ibm.com \
    --cc=wharms@bfs.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.