linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86/microcode/intel: Fix allocation size of struct ucode_patch
@ 2017-01-05  1:03 Junichi Nomura
  2017-01-05 10:23 ` Borislav Petkov
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Junichi Nomura @ 2017-01-05  1:03 UTC (permalink / raw)
  To: bp, x86, linux-kernel; +Cc: tglx, mingo, hpa

We allocate struct ucode_patch here.

"size" is a size of microcode data and used for kmemdup() later
in this function.

Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>
Fixes: 06b8534cb728 ("x86/microcode: Rework microcode loading")

diff --git a/arch/x86/kernel/cpu/microcode/intel.c b/arch/x86/kernel/cpu/microcode/intel.c
index aee3cb5..042f329 100644
--- a/arch/x86/kernel/cpu/microcode/intel.c
+++ b/arch/x86/kernel/cpu/microcode/intel.c
@@ -150,7 +150,7 @@ static struct ucode_patch *__alloc_microcode_buf(void *data, unsigned int size)
 {
 	struct ucode_patch *p;
 
-	p = kzalloc(size, GFP_KERNEL);
+	p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL);
 	if (!p)
 		return ERR_PTR(-ENOMEM);
 
-- 
Jun'ichi Nomura, NEC Corporation / NEC Solution Innovators, Ltd.

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] x86/microcode/intel: Fix allocation size of struct ucode_patch
  2017-01-05  1:03 [PATCH] x86/microcode/intel: Fix allocation size of struct ucode_patch Junichi Nomura
@ 2017-01-05 10:23 ` Borislav Petkov
  2017-01-05 17:44 ` Andy Shevchenko
  2017-01-09 22:17 ` [tip:x86/urgent] " tip-bot for Junichi Nomura
  2 siblings, 0 replies; 8+ messages in thread
From: Borislav Petkov @ 2017-01-05 10:23 UTC (permalink / raw)
  To: Junichi Nomura; +Cc: x86, linux-kernel, tglx, mingo, hpa

On Thu, Jan 05, 2017 at 01:03:51AM +0000, Junichi Nomura wrote:
> We allocate struct ucode_patch here.
> 
> "size" is a size of microcode data and used for kmemdup() later
> in this function.
> 
> Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>
> Fixes: 06b8534cb728 ("x86/microcode: Rework microcode loading")
> 
> diff --git a/arch/x86/kernel/cpu/microcode/intel.c b/arch/x86/kernel/cpu/microcode/intel.c
> index aee3cb5..042f329 100644
> --- a/arch/x86/kernel/cpu/microcode/intel.c
> +++ b/arch/x86/kernel/cpu/microcode/intel.c
> @@ -150,7 +150,7 @@ static struct ucode_patch *__alloc_microcode_buf(void *data, unsigned int size)
>  {
>  	struct ucode_patch *p;
>  
> -	p = kzalloc(size, GFP_KERNEL);
> +	p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL);
>  	if (!p)
>  		return ERR_PTR(-ENOMEM);
>  
> -- 

Applied, thanks.

-- 
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] x86/microcode/intel: Fix allocation size of struct ucode_patch
  2017-01-05  1:03 [PATCH] x86/microcode/intel: Fix allocation size of struct ucode_patch Junichi Nomura
  2017-01-05 10:23 ` Borislav Petkov
@ 2017-01-05 17:44 ` Andy Shevchenko
  2017-01-05 23:52   ` Junichi Nomura
  2017-01-09 22:17 ` [tip:x86/urgent] " tip-bot for Junichi Nomura
  2 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2017-01-05 17:44 UTC (permalink / raw)
  To: Junichi Nomura; +Cc: bp, x86, linux-kernel, tglx, mingo, hpa

On Thu, Jan 5, 2017 at 3:03 AM, Junichi Nomura <j-nomura@ce.jp.nec.com> wrote:
> We allocate struct ucode_patch here.
>
> "size" is a size of microcode data and used for kmemdup() later
> in this function.
>
> Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>
> Fixes: 06b8534cb728 ("x86/microcode: Rework microcode loading")
>
> diff --git a/arch/x86/kernel/cpu/microcode/intel.c b/arch/x86/kernel/cpu/microcode/intel.c
> index aee3cb5..042f329 100644
> --- a/arch/x86/kernel/cpu/microcode/intel.c
> +++ b/arch/x86/kernel/cpu/microcode/intel.c
> @@ -150,7 +150,7 @@ static struct ucode_patch *__alloc_microcode_buf(void *data, unsigned int size)
>  {
>         struct ucode_patch *p;
>
> -       p = kzalloc(size, GFP_KERNEL);
> +       p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL);

Perhaps sizeof(*p) ?

>         if (!p)
>                 return ERR_PTR(-ENOMEM);

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] x86/microcode/intel: Fix allocation size of struct ucode_patch
  2017-01-05 17:44 ` Andy Shevchenko
@ 2017-01-05 23:52   ` Junichi Nomura
  2017-01-06  0:02     ` Borislav Petkov
  0 siblings, 1 reply; 8+ messages in thread
From: Junichi Nomura @ 2017-01-05 23:52 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: bp, x86, linux-kernel, tglx, mingo, hpa

On 01/06/17 02:44, Andy Shevchenko wrote:
> On Thu, Jan 5, 2017 at 3:03 AM, Junichi Nomura <j-nomura@ce.jp.nec.com> wrote:
>> We allocate struct ucode_patch here.
>>
>> "size" is a size of microcode data and used for kmemdup() later
>> in this function.
>>
>> Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>
>> Fixes: 06b8534cb728 ("x86/microcode: Rework microcode loading")
>>
>> diff --git a/arch/x86/kernel/cpu/microcode/intel.c b/arch/x86/kernel/cpu/microcode/intel.c
>> index aee3cb5..042f329 100644
>> --- a/arch/x86/kernel/cpu/microcode/intel.c
>> +++ b/arch/x86/kernel/cpu/microcode/intel.c
>> @@ -150,7 +150,7 @@ static struct ucode_patch *__alloc_microcode_buf(void *data, unsigned int size)
>>  {
>>         struct ucode_patch *p;
>>
>> -       p = kzalloc(size, GFP_KERNEL);
>> +       p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL);
> 
> Perhaps sizeof(*p) ?

Yeah, that might be preferred.

>>         if (!p)
>>                 return ERR_PTR(-ENOMEM);

-- 
Jun'ichi Nomura, NEC Corporation / NEC Solution Innovators, Ltd.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] x86/microcode/intel: Fix allocation size of struct ucode_patch
  2017-01-05 23:52   ` Junichi Nomura
@ 2017-01-06  0:02     ` Borislav Petkov
  2017-01-06  0:14       ` Junichi Nomura
  0 siblings, 1 reply; 8+ messages in thread
From: Borislav Petkov @ 2017-01-06  0:02 UTC (permalink / raw)
  To: Junichi Nomura; +Cc: Andy Shevchenko, x86, linux-kernel, tglx, mingo, hpa

On Thu, Jan 05, 2017 at 11:52:07PM +0000, Junichi Nomura wrote:
> >> +       p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL);
> > 
> > Perhaps sizeof(*p) ?
> 
> Yeah, that might be preferred.

No, those things are never preferred because

	sizeof(struct <type>)

tells you exactly the size of what kind of object you're getting vs

	sizeof(*p)

which tells you you're getting the size of what p points to.

Now you have to go look at p and what type it is. In the current case, p
is defined not far away from the use site but in a larger function, you
most likely need to eyeball up to its type when reading the code. Which
makes the whole thing less readable.

-- 
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] x86/microcode/intel: Fix allocation size of struct ucode_patch
  2017-01-06  0:02     ` Borislav Petkov
@ 2017-01-06  0:14       ` Junichi Nomura
  2017-01-06 10:56         ` Borislav Petkov
  0 siblings, 1 reply; 8+ messages in thread
From: Junichi Nomura @ 2017-01-06  0:14 UTC (permalink / raw)
  To: Borislav Petkov; +Cc: Andy Shevchenko, x86, linux-kernel, tglx, mingo, hpa

On 01/06/17 09:02, Borislav Petkov wrote:
> On Thu, Jan 05, 2017 at 11:52:07PM +0000, Junichi Nomura wrote:
>>>> +       p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL);
>>>
>>> Perhaps sizeof(*p) ?
>>
>> Yeah, that might be preferred.
> 
> No, those things are never preferred because
> 
> 	sizeof(struct <type>)
> 
> tells you exactly the size of what kind of object you're getting vs
> 
> 	sizeof(*p)
> 
> which tells you you're getting the size of what p points to.
> 
> Now you have to go look at p and what type it is. In the current case, p
> is defined not far away from the use site but in a larger function, you
> most likely need to eyeball up to its type when reading the code. Which
> makes the whole thing less readable.

Personally I have same opinion as yours. :)

But according to Documentation/process/coding-style.rst, it seems
"sizeof(*p)" is preferred style and the reason there makes some
sense.

Quote from coding-style.rst:
  > The preferred form for passing a size of a struct is the following:
  > 
  >         p = kmalloc(sizeof(*p), ...);
  > 
  > The alternative form where struct name is spelled out hurts readability and
  > introduces an opportunity for a bug when the pointer variable type is changed
  > but the corresponding sizeof that is passed to a memory allocator is not.

I'm fine with either way.

-- 
Jun'ichi Nomura, NEC Corporation / NEC Solution Innovators, Ltd.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] x86/microcode/intel: Fix allocation size of struct ucode_patch
  2017-01-06  0:14       ` Junichi Nomura
@ 2017-01-06 10:56         ` Borislav Petkov
  0 siblings, 0 replies; 8+ messages in thread
From: Borislav Petkov @ 2017-01-06 10:56 UTC (permalink / raw)
  To: Junichi Nomura; +Cc: Andy Shevchenko, x86, linux-kernel, tglx, mingo, hpa

On Fri, Jan 06, 2017 at 12:14:13AM +0000, Junichi Nomura wrote:
> Personally I have same opinion as yours. :)
> 
> But according to Documentation/process/coding-style.rst, it seems
> "sizeof(*p)" is preferred style and the reason there makes some
> sense.
> 
> Quote from coding-style.rst:
>   > The preferred form for passing a size of a struct is the following:
>   > 
>   >         p = kmalloc(sizeof(*p), ...);
>   > 
>   > The alternative form where struct name is spelled out hurts readability and
							^^^^^^^^^^^^^^^^^^^^^

Yeah, right. Change is from 2005 (af4e5a218e18a). Pekka didn't know better then. :-P

-- 
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [tip:x86/urgent] x86/microcode/intel: Fix allocation size of struct ucode_patch
  2017-01-05  1:03 [PATCH] x86/microcode/intel: Fix allocation size of struct ucode_patch Junichi Nomura
  2017-01-05 10:23 ` Borislav Petkov
  2017-01-05 17:44 ` Andy Shevchenko
@ 2017-01-09 22:17 ` tip-bot for Junichi Nomura
  2 siblings, 0 replies; 8+ messages in thread
From: tip-bot for Junichi Nomura @ 2017-01-09 22:17 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: j-nomura, mingo, hpa, bp, tglx, linux-kernel

Commit-ID:  9fcf5ba2ef908af916e9002891fbbca20ce4dc98
Gitweb:     http://git.kernel.org/tip/9fcf5ba2ef908af916e9002891fbbca20ce4dc98
Author:     Junichi Nomura <j-nomura@ce.jp.nec.com>
AuthorDate: Mon, 9 Jan 2017 12:41:46 +0100
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Mon, 9 Jan 2017 23:11:14 +0100

x86/microcode/intel: Fix allocation size of struct ucode_patch

We allocate struct ucode_patch here. @size is the size of microcode data
and used for kmemdup() later in this function.

Fixes: 06b8534cb728 ("x86/microcode: Rework microcode loading")
Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: http://lkml.kernel.org/r/7a730dc9-ac17-35c4-fe76-dfc94e5ecd95@ce.jp.nec.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

---
 arch/x86/kernel/cpu/microcode/intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/microcode/intel.c b/arch/x86/kernel/cpu/microcode/intel.c
index faec8fa..9434865 100644
--- a/arch/x86/kernel/cpu/microcode/intel.c
+++ b/arch/x86/kernel/cpu/microcode/intel.c
@@ -150,7 +150,7 @@ static struct ucode_patch *__alloc_microcode_buf(void *data, unsigned int size)
 {
 	struct ucode_patch *p;
 
-	p = kzalloc(size, GFP_KERNEL);
+	p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL);
 	if (!p)
 		return ERR_PTR(-ENOMEM);
 

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-01-09 22:18 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-05  1:03 [PATCH] x86/microcode/intel: Fix allocation size of struct ucode_patch Junichi Nomura
2017-01-05 10:23 ` Borislav Petkov
2017-01-05 17:44 ` Andy Shevchenko
2017-01-05 23:52   ` Junichi Nomura
2017-01-06  0:02     ` Borislav Petkov
2017-01-06  0:14       ` Junichi Nomura
2017-01-06 10:56         ` Borislav Petkov
2017-01-09 22:17 ` [tip:x86/urgent] " tip-bot for Junichi Nomura

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).