Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread H. Peter Anvin
On 05/28/2015 11:33 AM, Borislav Petkov wrote:
> On Thu, May 28, 2015 at 09:57:15AM -0700, H. Peter Anvin wrote:
>> Why?!
>>
>> We are taking about 48 bytes run once per cpu. It isn't worth it to
>> optimize, in fact the extra code size hurts more.
> 
> I wanted to save us the redundant copying of the exact same bytes.
> Because when there's no preceding whitespace, p and q point at the same
> thing so we end up doing *p = *p.
> 
> OTOH, without the optimization, the code is even simpler.
> 
> I can remove it if you wanna - I don't care all that much.
> 

Yes, please.  Actually, with a test inside the loop the way you have it,
the resulting code will almost certainly be slower -- a redundant write
to an already dirty cache line is way cheaper than a branch.

-hpa

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread Borislav Petkov
On Thu, May 28, 2015 at 09:57:15AM -0700, H. Peter Anvin wrote:
> Why?!
>
> We are taking about 48 bytes run once per cpu. It isn't worth it to
> optimize, in fact the extra code size hurts more.

I wanted to save us the redundant copying of the exact same bytes.
Because when there's no preceding whitespace, p and q point at the same
thing so we end up doing *p = *p.

OTOH, without the optimization, the code is even simpler.

I can remove it if you wanna - I don't care all that much.

-- 
Regards/Gruss,
Boris.

ECO tip #101: Trim your mails when you reply.
--
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread H. Peter Anvin
Why?!

We are taking about 48 bytes run once per cpu.  It isn't worth it to optimize, 
in fact the extra code size hurts more.

On May 28, 2015 5:58:19 AM PDT, Borislav Petkov  wrote:
>On Thu, May 28, 2015 at 01:32:29PM +0200, Borislav Petkov wrote:
>> +while (*p) {
>> +/* Note the last non-whitespace index */
>> +if (!isspace(*p))
>> +s = q;
>> +
>> +*q++ = *p++;
>
>This should be optimized to not copy if there's no preceding whitespace
>and p == q:
>
>From: Borislav Petkov 
>Date: Tue, 26 May 2015 10:28:17 +0200
>Subject: [PATCH] x86/cpu: Trim model id whitespace
>
>We did try trimming whitespace surrounding the 'model name' field
>in /proc/cpuinfo since reportedly some userspace uses it in string
>comparisons and there were discrepancies:
>
>[thetango@prarit ~]# grep "^model name" /proc/cpuinfo | uniq -c | sed
>'s/\ /_/g'
>  __1_model_name  :_AMD_Opteron(TM)_Processor_6272
>_63_model_name 
>:_AMD_Opteron(TM)_Processor_6272_
>
>However, there were issues with overlapping buffers, string sizes and
>non-byte-sized copies in the previous proposed solutions; see Link tags
>below for the whole farce.
>
>So, instead of diddling with this more, let's simply extend what was
>there originally with trimming any present trailing whitespace. Final
>result is really simple and obvious.
>
>Testing with the most insane model IDs qemu can generate, looks good:
>
>  .model_id = "My funny model ID CPU  ",
>  __4_model_name  :_My_funny_model_ID_CPU
>
>  .model_id = "My funny model ID CPU  ",
>  __4_model_name  :_My_funny_model_ID_CPU
>
>  .model_id = "My funny model ID CPU",
>  __4_model_name  :_My_funny_model_ID_CPU
>
>  .model_id = "",
>  __4_model_name  :__
>
>  .model_id = "",
>  __4_model_name  :_15/02
>
>Cc: Andy Lutomirski 
>Cc: Brian Gerst 
>Cc: Dave Hansen 
>Cc: Denys Vlasenko 
>Cc: Fenghua Yu 
>Cc: H. Peter Anvin 
>Cc: Igor Mammedov 
>Cc: Linus Torvalds 
>Cc: Peter Zijlstra 
>Cc: Thomas Gleixner 
>Link:
>http://lkml.kernel.org/r/1432050210-32036-1-git-send-email-pra...@redhat.com
>Link:
>http://lkml.kernel.org/r/1432628901-18044-15-git-send-email...@alien8.de
>Signed-off-by: Borislav Petkov 
>---
> arch/x86/kernel/cpu/common.c | 27 ++-
> 1 file changed, 22 insertions(+), 5 deletions(-)
>
>diff --git a/arch/x86/kernel/cpu/common.c
>b/arch/x86/kernel/cpu/common.c
>index 41a8e9cb30bc..18120a33a2c1 100644
>--- a/arch/x86/kernel/cpu/common.c
>+++ b/arch/x86/kernel/cpu/common.c
>@@ -5,6 +5,7 @@
> #include 
> #include 
> #include 
>+#include 
> #include 
> #include 
> #include 
>@@ -419,6 +420,7 @@ static const struct cpu_dev
>*cpu_devs[X86_VENDOR_NUM] = {};
> static void get_model_name(struct cpuinfo_x86 *c)
> {
>   unsigned int *v;
>+  char *p, *q, *s;
> 
>   if (c->extended_cpuid_level < 0x8004)
>   return;
>@@ -429,11 +431,26 @@ static void get_model_name(struct cpuinfo_x86 *c)
>   cpuid(0x8004, [8], [9], [10], [11]);
>   c->x86_model_id[48] = 0;
> 
>-  /*
>-   * Remove leading whitespace on Intel processors and trailing
>-   * whitespace on AMD processors.
>-   */
>-  memmove(c->x86_model_id, strim(c->x86_model_id), 48);
>+  /* Trim whitespace */
>+  p = q = s = >x86_model_id[0];
>+
>+  while (*p == ' ')
>+  p++;
>+
>+  while (*p) {
>+  /* Note the last non-whitespace index: */
>+  if (!isspace(*p))
>+  s = q;
>+
>+  /* Only copy if p advanced due to whitespace: */
>+  if (p != q)
>+  *q = *p;
>+
>+  p++;
>+  q++;
>+  }
>+
>+  *(s + 1) = '\0';
> }
> 
> void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)

-- 
Sent from my mobile phone.  Please pardon brevity and lack of formatting.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread Borislav Petkov
On Thu, May 28, 2015 at 01:32:29PM +0200, Borislav Petkov wrote:
> + while (*p) {
> + /* Note the last non-whitespace index */
> + if (!isspace(*p))
> + s = q;
> +
> + *q++ = *p++;

This should be optimized to not copy if there's no preceding whitespace
and p == q:

From: Borislav Petkov 
Date: Tue, 26 May 2015 10:28:17 +0200
Subject: [PATCH] x86/cpu: Trim model id whitespace

We did try trimming whitespace surrounding the 'model name' field
in /proc/cpuinfo since reportedly some userspace uses it in string
comparisons and there were discrepancies:

  [thetango@prarit ~]# grep "^model name" /proc/cpuinfo | uniq -c | sed 's/\ 
/_/g'
  __1_model_name  :_AMD_Opteron(TM)_Processor_6272
  _63_model_name  :_AMD_Opteron(TM)_Processor_6272_

However, there were issues with overlapping buffers, string sizes and
non-byte-sized copies in the previous proposed solutions; see Link tags
below for the whole farce.

So, instead of diddling with this more, let's simply extend what was
there originally with trimming any present trailing whitespace. Final
result is really simple and obvious.

Testing with the most insane model IDs qemu can generate, looks good:

  .model_id = "My funny model ID CPU  ",
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = "My funny model ID CPU  ",
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = "My funny model ID CPU",
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = "",
  __4_model_name  :__

  .model_id = "",
  __4_model_name  :_15/02

Cc: Andy Lutomirski 
Cc: Brian Gerst 
Cc: Dave Hansen 
Cc: Denys Vlasenko 
Cc: Fenghua Yu 
Cc: H. Peter Anvin 
Cc: Igor Mammedov 
Cc: Linus Torvalds 
Cc: Peter Zijlstra 
Cc: Thomas Gleixner 
Link: 
http://lkml.kernel.org/r/1432050210-32036-1-git-send-email-pra...@redhat.com
Link: http://lkml.kernel.org/r/1432628901-18044-15-git-send-email...@alien8.de
Signed-off-by: Borislav Petkov 
---
 arch/x86/kernel/cpu/common.c | 27 ++-
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 41a8e9cb30bc..18120a33a2c1 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -5,6 +5,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -419,6 +420,7 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
 static void get_model_name(struct cpuinfo_x86 *c)
 {
unsigned int *v;
+   char *p, *q, *s;
 
if (c->extended_cpuid_level < 0x8004)
return;
@@ -429,11 +431,26 @@ static void get_model_name(struct cpuinfo_x86 *c)
cpuid(0x8004, [8], [9], [10], [11]);
c->x86_model_id[48] = 0;
 
-   /*
-* Remove leading whitespace on Intel processors and trailing
-* whitespace on AMD processors.
-*/
-   memmove(c->x86_model_id, strim(c->x86_model_id), 48);
+   /* Trim whitespace */
+   p = q = s = >x86_model_id[0];
+
+   while (*p == ' ')
+   p++;
+
+   while (*p) {
+   /* Note the last non-whitespace index: */
+   if (!isspace(*p))
+   s = q;
+
+   /* Only copy if p advanced due to whitespace: */
+   if (p != q)
+   *q = *p;
+
+   p++;
+   q++;
+   }
+
+   *(s + 1) = '\0';
 }
 
 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
-- 
2.3.5

-- 
Regards/Gruss,
Boris.

ECO tip #101: Trim your mails when you reply.
--
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread Borislav Petkov
On Thu, May 28, 2015 at 07:27:19AM -0400, Prarit Bhargava wrote:
> FWIW, I agree with Joe here and don't think the #define is necessary.
> I will post a follow-up patch against tip on LKML shortly.

No need, I have a better one:

---
From: Borislav Petkov 
Date: Tue, 26 May 2015 10:28:17 +0200
Subject: [PATCH] x86/cpu: Trim model id whitespace

We did try trimming whitespace surrounding the 'model name' field
in /proc/cpuinfo since reportedly some userspace uses it in string
comparisons and there were discrepancies:

  [thetango@prarit ~]# grep "^model name" /proc/cpuinfo | uniq -c | sed 's/\ 
/_/g'
  __1_model_name  :_AMD_Opteron(TM)_Processor_6272
  _63_model_name  :_AMD_Opteron(TM)_Processor_6272_

However, there were issues with overlapping buffers, string sizes and
non-byte-sized copies in the previous proposed solutions; see Link tags
below for the whole farce.

So, instead of diddling with this more, let's simply extend what was
there originally with trimming any present trailing whitespace. Final
result is really simple and obvious.

Testing with the most insane model IDs qemu can generate, looks good:

  .model_id = "My funny model ID CPU  ",
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = "My funny model ID CPU  ",
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = "My funny model ID CPU",
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = "",
  __4_model_name  :__

  .model_id = "",
  __4_model_name  :_15/02

Cc: Andy Lutomirski 
Cc: Brian Gerst 
Cc: Dave Hansen 
Cc: Denys Vlasenko 
Cc: Fenghua Yu 
Cc: H. Peter Anvin 
Cc: Igor Mammedov 
Cc: Linus Torvalds 
Cc: Peter Zijlstra 
Cc: Thomas Gleixner 
Link: 
http://lkml.kernel.org/r/1432050210-32036-1-git-send-email-pra...@redhat.com
Link: http://lkml.kernel.org/r/1432628901-18044-15-git-send-email...@alien8.de
Signed-off-by: Borislav Petkov 
---
 arch/x86/kernel/cpu/common.c | 22 +-
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 41a8e9cb30bc..351197cbbc8e 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -5,6 +5,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -419,6 +420,7 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
 static void get_model_name(struct cpuinfo_x86 *c)
 {
unsigned int *v;
+   char *p, *q, *s;
 
if (c->extended_cpuid_level < 0x8004)
return;
@@ -429,11 +431,21 @@ static void get_model_name(struct cpuinfo_x86 *c)
cpuid(0x8004, [8], [9], [10], [11]);
c->x86_model_id[48] = 0;
 
-   /*
-* Remove leading whitespace on Intel processors and trailing
-* whitespace on AMD processors.
-*/
-   memmove(c->x86_model_id, strim(c->x86_model_id), 48);
+   /* Trim whitespace */
+   p = q = s = >x86_model_id[0];
+
+   while (*p == ' ')
+   p++;
+
+   while (*p) {
+   /* Note the last non-whitespace index */
+   if (!isspace(*p))
+   s = q;
+
+   *q++ = *p++;
+   }
+
+   *(s + 1) = '\0';
 }
 
 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
-- 
2.3.5

-- 
Regards/Gruss,
Boris.

ECO tip #101: Trim your mails when you reply.
--
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread Prarit Bhargava
On 05/27/2015 03:16 PM, Joe Perches wrote:
> On Wed, 2015-05-27 at 21:06 +0200, Borislav Petkov wrote:
>> On Wed, May 27, 2015 at 10:07:34AM -0700, Joe Perches wrote:
>>> This code can memmove from beyond the x86_model_id field.
>>
>> ... in the theoretical case where some model ID has more than 64 - 48
>> preceding white spaces.
>>
>> I guess we want to be prepared here for insane CPU model IDs coming from
>> virtualization.
>>
>>> Maybe:
>>> char *model = strim(c->x86_model_id);
>>> memmove(c->x86_model_id, model, strlen(model) + 1);
>>
>> Yes, and additionally limit that string length:
>>
>> ---
>> diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
> []
>> @@ -383,6 +383,9 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = 
>> {};
>>  static void get_model_name(struct cpuinfo_x86 *c)
>>  {
>>  unsigned int *v;
>> +const char *model;
>> +
>> +#define MODEL_ID_MAXLEN 48
>>  
>>  if (c->extended_cpuid_level < 0x8004)
>>  return;
>> @@ -391,13 +394,15 @@ static void get_model_name(struct cpuinfo_x86 *c)
>>  cpuid(0x8002, [0], [1], [2], [3]);
>>  cpuid(0x8003, [4], [5], [6], [7]);
>>  cpuid(0x8004, [8], [9], [10], [11]);
>> -c->x86_model_id[48] = 0;
>> +c->x86_model_id[MODEL_ID_MAXLEN] = 0;
>>  
>>  /*
>>   * Remove leading whitespace on Intel processors and trailing
>>   * whitespace on AMD processors.
>>   */
>> -memmove(c->x86_model_id, strim(c->x86_model_id), 48);
>> +model = strim(c->x86_model_id);
>> +
>> +memmove(c->x86_model_id, model, strnlen(model, MODEL_ID_MAXLEN) + 1);
> 
> I don't see any value in the #define or strnlen over strlen as
> it's guaranteed terminated by the = 0 above, but  thanks.
> 

FWIW, I agree with Joe here and don't think the #define is necessary.
I will post a follow-up patch against tip on LKML shortly.

P.

> cheers, Joe
> 
> 
> 

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread H. Peter Anvin
On 05/28/2015 11:33 AM, Borislav Petkov wrote:
 On Thu, May 28, 2015 at 09:57:15AM -0700, H. Peter Anvin wrote:
 Why?!

 We are taking about 48 bytes run once per cpu. It isn't worth it to
 optimize, in fact the extra code size hurts more.
 
 I wanted to save us the redundant copying of the exact same bytes.
 Because when there's no preceding whitespace, p and q point at the same
 thing so we end up doing *p = *p.
 
 OTOH, without the optimization, the code is even simpler.
 
 I can remove it if you wanna - I don't care all that much.
 

Yes, please.  Actually, with a test inside the loop the way you have it,
the resulting code will almost certainly be slower -- a redundant write
to an already dirty cache line is way cheaper than a branch.

-hpa

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread Borislav Petkov
On Thu, May 28, 2015 at 09:57:15AM -0700, H. Peter Anvin wrote:
 Why?!

 We are taking about 48 bytes run once per cpu. It isn't worth it to
 optimize, in fact the extra code size hurts more.

I wanted to save us the redundant copying of the exact same bytes.
Because when there's no preceding whitespace, p and q point at the same
thing so we end up doing *p = *p.

OTOH, without the optimization, the code is even simpler.

I can remove it if you wanna - I don't care all that much.

-- 
Regards/Gruss,
Boris.

ECO tip #101: Trim your mails when you reply.
--
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread Prarit Bhargava
On 05/27/2015 03:16 PM, Joe Perches wrote:
 On Wed, 2015-05-27 at 21:06 +0200, Borislav Petkov wrote:
 On Wed, May 27, 2015 at 10:07:34AM -0700, Joe Perches wrote:
 This code can memmove from beyond the x86_model_id field.

 ... in the theoretical case where some model ID has more than 64 - 48
 preceding white spaces.

 I guess we want to be prepared here for insane CPU model IDs coming from
 virtualization.

 Maybe:
 char *model = strim(c-x86_model_id);
 memmove(c-x86_model_id, model, strlen(model) + 1);

 Yes, and additionally limit that string length:

 ---
 diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
 []
 @@ -383,6 +383,9 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = 
 {};
  static void get_model_name(struct cpuinfo_x86 *c)
  {
  unsigned int *v;
 +const char *model;
 +
 +#define MODEL_ID_MAXLEN 48
  
  if (c-extended_cpuid_level  0x8004)
  return;
 @@ -391,13 +394,15 @@ static void get_model_name(struct cpuinfo_x86 *c)
  cpuid(0x8002, v[0], v[1], v[2], v[3]);
  cpuid(0x8003, v[4], v[5], v[6], v[7]);
  cpuid(0x8004, v[8], v[9], v[10], v[11]);
 -c-x86_model_id[48] = 0;
 +c-x86_model_id[MODEL_ID_MAXLEN] = 0;
  
  /*
   * Remove leading whitespace on Intel processors and trailing
   * whitespace on AMD processors.
   */
 -memmove(c-x86_model_id, strim(c-x86_model_id), 48);
 +model = strim(c-x86_model_id);
 +
 +memmove(c-x86_model_id, model, strnlen(model, MODEL_ID_MAXLEN) + 1);
 
 I don't see any value in the #define or strnlen over strlen as
 it's guaranteed terminated by the = 0 above, but shrug thanks.
 

FWIW, I agree with Joe here and don't think the #define is necessary.
I will post a follow-up patch against tip on LKML shortly.

P.

 cheers, Joe
 
 
 

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread Borislav Petkov
On Thu, May 28, 2015 at 07:27:19AM -0400, Prarit Bhargava wrote:
 FWIW, I agree with Joe here and don't think the #define is necessary.
 I will post a follow-up patch against tip on LKML shortly.

No need, I have a better one:

---
From: Borislav Petkov b...@suse.de
Date: Tue, 26 May 2015 10:28:17 +0200
Subject: [PATCH] x86/cpu: Trim model id whitespace

We did try trimming whitespace surrounding the 'model name' field
in /proc/cpuinfo since reportedly some userspace uses it in string
comparisons and there were discrepancies:

  [thetango@prarit ~]# grep ^model name /proc/cpuinfo | uniq -c | sed 's/\ 
/_/g'
  __1_model_name  :_AMD_Opteron(TM)_Processor_6272
  _63_model_name  :_AMD_Opteron(TM)_Processor_6272_

However, there were issues with overlapping buffers, string sizes and
non-byte-sized copies in the previous proposed solutions; see Link tags
below for the whole farce.

So, instead of diddling with this more, let's simply extend what was
there originally with trimming any present trailing whitespace. Final
result is really simple and obvious.

Testing with the most insane model IDs qemu can generate, looks good:

  .model_id = My funny model ID CPU  ,
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = My funny model ID CPU  ,
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = My funny model ID CPU,
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = ,
  __4_model_name  :__

  .model_id = ,
  __4_model_name  :_15/02

Cc: Andy Lutomirski l...@amacapital.net
Cc: Brian Gerst brge...@gmail.com
Cc: Dave Hansen dave.han...@linux.intel.com
Cc: Denys Vlasenko dvlas...@redhat.com
Cc: Fenghua Yu fenghua...@intel.com
Cc: H. Peter Anvin h...@zytor.com
Cc: Igor Mammedov imamm...@redhat.com
Cc: Linus Torvalds torva...@linux-foundation.org
Cc: Peter Zijlstra pet...@infradead.org
Cc: Thomas Gleixner t...@linutronix.de
Link: 
http://lkml.kernel.org/r/1432050210-32036-1-git-send-email-pra...@redhat.com
Link: http://lkml.kernel.org/r/1432628901-18044-15-git-send-email...@alien8.de
Signed-off-by: Borislav Petkov b...@suse.de
---
 arch/x86/kernel/cpu/common.c | 22 +-
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 41a8e9cb30bc..351197cbbc8e 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -5,6 +5,7 @@
 #include linux/module.h
 #include linux/percpu.h
 #include linux/string.h
+#include linux/ctype.h
 #include linux/delay.h
 #include linux/sched.h
 #include linux/init.h
@@ -419,6 +420,7 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
 static void get_model_name(struct cpuinfo_x86 *c)
 {
unsigned int *v;
+   char *p, *q, *s;
 
if (c-extended_cpuid_level  0x8004)
return;
@@ -429,11 +431,21 @@ static void get_model_name(struct cpuinfo_x86 *c)
cpuid(0x8004, v[8], v[9], v[10], v[11]);
c-x86_model_id[48] = 0;
 
-   /*
-* Remove leading whitespace on Intel processors and trailing
-* whitespace on AMD processors.
-*/
-   memmove(c-x86_model_id, strim(c-x86_model_id), 48);
+   /* Trim whitespace */
+   p = q = s = c-x86_model_id[0];
+
+   while (*p == ' ')
+   p++;
+
+   while (*p) {
+   /* Note the last non-whitespace index */
+   if (!isspace(*p))
+   s = q;
+
+   *q++ = *p++;
+   }
+
+   *(s + 1) = '\0';
 }
 
 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
-- 
2.3.5

-- 
Regards/Gruss,
Boris.

ECO tip #101: Trim your mails when you reply.
--
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread Borislav Petkov
On Thu, May 28, 2015 at 01:32:29PM +0200, Borislav Petkov wrote:
 + while (*p) {
 + /* Note the last non-whitespace index */
 + if (!isspace(*p))
 + s = q;
 +
 + *q++ = *p++;

This should be optimized to not copy if there's no preceding whitespace
and p == q:

From: Borislav Petkov b...@suse.de
Date: Tue, 26 May 2015 10:28:17 +0200
Subject: [PATCH] x86/cpu: Trim model id whitespace

We did try trimming whitespace surrounding the 'model name' field
in /proc/cpuinfo since reportedly some userspace uses it in string
comparisons and there were discrepancies:

  [thetango@prarit ~]# grep ^model name /proc/cpuinfo | uniq -c | sed 's/\ 
/_/g'
  __1_model_name  :_AMD_Opteron(TM)_Processor_6272
  _63_model_name  :_AMD_Opteron(TM)_Processor_6272_

However, there were issues with overlapping buffers, string sizes and
non-byte-sized copies in the previous proposed solutions; see Link tags
below for the whole farce.

So, instead of diddling with this more, let's simply extend what was
there originally with trimming any present trailing whitespace. Final
result is really simple and obvious.

Testing with the most insane model IDs qemu can generate, looks good:

  .model_id = My funny model ID CPU  ,
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = My funny model ID CPU  ,
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = My funny model ID CPU,
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = ,
  __4_model_name  :__

  .model_id = ,
  __4_model_name  :_15/02

Cc: Andy Lutomirski l...@amacapital.net
Cc: Brian Gerst brge...@gmail.com
Cc: Dave Hansen dave.han...@linux.intel.com
Cc: Denys Vlasenko dvlas...@redhat.com
Cc: Fenghua Yu fenghua...@intel.com
Cc: H. Peter Anvin h...@zytor.com
Cc: Igor Mammedov imamm...@redhat.com
Cc: Linus Torvalds torva...@linux-foundation.org
Cc: Peter Zijlstra pet...@infradead.org
Cc: Thomas Gleixner t...@linutronix.de
Link: 
http://lkml.kernel.org/r/1432050210-32036-1-git-send-email-pra...@redhat.com
Link: http://lkml.kernel.org/r/1432628901-18044-15-git-send-email...@alien8.de
Signed-off-by: Borislav Petkov b...@suse.de
---
 arch/x86/kernel/cpu/common.c | 27 ++-
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 41a8e9cb30bc..18120a33a2c1 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -5,6 +5,7 @@
 #include linux/module.h
 #include linux/percpu.h
 #include linux/string.h
+#include linux/ctype.h
 #include linux/delay.h
 #include linux/sched.h
 #include linux/init.h
@@ -419,6 +420,7 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
 static void get_model_name(struct cpuinfo_x86 *c)
 {
unsigned int *v;
+   char *p, *q, *s;
 
if (c-extended_cpuid_level  0x8004)
return;
@@ -429,11 +431,26 @@ static void get_model_name(struct cpuinfo_x86 *c)
cpuid(0x8004, v[8], v[9], v[10], v[11]);
c-x86_model_id[48] = 0;
 
-   /*
-* Remove leading whitespace on Intel processors and trailing
-* whitespace on AMD processors.
-*/
-   memmove(c-x86_model_id, strim(c-x86_model_id), 48);
+   /* Trim whitespace */
+   p = q = s = c-x86_model_id[0];
+
+   while (*p == ' ')
+   p++;
+
+   while (*p) {
+   /* Note the last non-whitespace index: */
+   if (!isspace(*p))
+   s = q;
+
+   /* Only copy if p advanced due to whitespace: */
+   if (p != q)
+   *q = *p;
+
+   p++;
+   q++;
+   }
+
+   *(s + 1) = '\0';
 }
 
 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
-- 
2.3.5

-- 
Regards/Gruss,
Boris.

ECO tip #101: Trim your mails when you reply.
--
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-28 Thread H. Peter Anvin
Why?!

We are taking about 48 bytes run once per cpu.  It isn't worth it to optimize, 
in fact the extra code size hurts more.

On May 28, 2015 5:58:19 AM PDT, Borislav Petkov b...@alien8.de wrote:
On Thu, May 28, 2015 at 01:32:29PM +0200, Borislav Petkov wrote:
 +while (*p) {
 +/* Note the last non-whitespace index */
 +if (!isspace(*p))
 +s = q;
 +
 +*q++ = *p++;

This should be optimized to not copy if there's no preceding whitespace
and p == q:

From: Borislav Petkov b...@suse.de
Date: Tue, 26 May 2015 10:28:17 +0200
Subject: [PATCH] x86/cpu: Trim model id whitespace

We did try trimming whitespace surrounding the 'model name' field
in /proc/cpuinfo since reportedly some userspace uses it in string
comparisons and there were discrepancies:

[thetango@prarit ~]# grep ^model name /proc/cpuinfo | uniq -c | sed
's/\ /_/g'
  __1_model_name  :_AMD_Opteron(TM)_Processor_6272
_63_model_name 
:_AMD_Opteron(TM)_Processor_6272_

However, there were issues with overlapping buffers, string sizes and
non-byte-sized copies in the previous proposed solutions; see Link tags
below for the whole farce.

So, instead of diddling with this more, let's simply extend what was
there originally with trimming any present trailing whitespace. Final
result is really simple and obvious.

Testing with the most insane model IDs qemu can generate, looks good:

  .model_id = My funny model ID CPU  ,
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = My funny model ID CPU  ,
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = My funny model ID CPU,
  __4_model_name  :_My_funny_model_ID_CPU

  .model_id = ,
  __4_model_name  :__

  .model_id = ,
  __4_model_name  :_15/02

Cc: Andy Lutomirski l...@amacapital.net
Cc: Brian Gerst brge...@gmail.com
Cc: Dave Hansen dave.han...@linux.intel.com
Cc: Denys Vlasenko dvlas...@redhat.com
Cc: Fenghua Yu fenghua...@intel.com
Cc: H. Peter Anvin h...@zytor.com
Cc: Igor Mammedov imamm...@redhat.com
Cc: Linus Torvalds torva...@linux-foundation.org
Cc: Peter Zijlstra pet...@infradead.org
Cc: Thomas Gleixner t...@linutronix.de
Link:
http://lkml.kernel.org/r/1432050210-32036-1-git-send-email-pra...@redhat.com
Link:
http://lkml.kernel.org/r/1432628901-18044-15-git-send-email...@alien8.de
Signed-off-by: Borislav Petkov b...@suse.de
---
 arch/x86/kernel/cpu/common.c | 27 ++-
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/common.c
b/arch/x86/kernel/cpu/common.c
index 41a8e9cb30bc..18120a33a2c1 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -5,6 +5,7 @@
 #include linux/module.h
 #include linux/percpu.h
 #include linux/string.h
+#include linux/ctype.h
 #include linux/delay.h
 #include linux/sched.h
 #include linux/init.h
@@ -419,6 +420,7 @@ static const struct cpu_dev
*cpu_devs[X86_VENDOR_NUM] = {};
 static void get_model_name(struct cpuinfo_x86 *c)
 {
   unsigned int *v;
+  char *p, *q, *s;
 
   if (c-extended_cpuid_level  0x8004)
   return;
@@ -429,11 +431,26 @@ static void get_model_name(struct cpuinfo_x86 *c)
   cpuid(0x8004, v[8], v[9], v[10], v[11]);
   c-x86_model_id[48] = 0;
 
-  /*
-   * Remove leading whitespace on Intel processors and trailing
-   * whitespace on AMD processors.
-   */
-  memmove(c-x86_model_id, strim(c-x86_model_id), 48);
+  /* Trim whitespace */
+  p = q = s = c-x86_model_id[0];
+
+  while (*p == ' ')
+  p++;
+
+  while (*p) {
+  /* Note the last non-whitespace index: */
+  if (!isspace(*p))
+  s = q;
+
+  /* Only copy if p advanced due to whitespace: */
+  if (p != q)
+  *q = *p;
+
+  p++;
+  q++;
+  }
+
+  *(s + 1) = '\0';
 }
 
 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)

-- 
Sent from my mobile phone.  Please pardon brevity and lack of formatting.
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-27 Thread Joe Perches
On Wed, 2015-05-27 at 21:06 +0200, Borislav Petkov wrote:
> On Wed, May 27, 2015 at 10:07:34AM -0700, Joe Perches wrote:
> > This code can memmove from beyond the x86_model_id field.
> 
> ... in the theoretical case where some model ID has more than 64 - 48
> preceding white spaces.
> 
> I guess we want to be prepared here for insane CPU model IDs coming from
> virtualization.
> 
> > Maybe:
> > char *model = strim(c->x86_model_id);
> > memmove(c->x86_model_id, model, strlen(model) + 1);
> 
> Yes, and additionally limit that string length:
> 
> ---
> diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
[]
> @@ -383,6 +383,9 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = 
> {};
>  static void get_model_name(struct cpuinfo_x86 *c)
>  {
>   unsigned int *v;
> + const char *model;
> +
> +#define MODEL_ID_MAXLEN 48
>  
>   if (c->extended_cpuid_level < 0x8004)
>   return;
> @@ -391,13 +394,15 @@ static void get_model_name(struct cpuinfo_x86 *c)
>   cpuid(0x8002, [0], [1], [2], [3]);
>   cpuid(0x8003, [4], [5], [6], [7]);
>   cpuid(0x8004, [8], [9], [10], [11]);
> - c->x86_model_id[48] = 0;
> + c->x86_model_id[MODEL_ID_MAXLEN] = 0;
>  
>   /*
>* Remove leading whitespace on Intel processors and trailing
>* whitespace on AMD processors.
>*/
> - memmove(c->x86_model_id, strim(c->x86_model_id), 48);
> + model = strim(c->x86_model_id);
> +
> + memmove(c->x86_model_id, model, strnlen(model, MODEL_ID_MAXLEN) + 1);

I don't see any value in the #define or strnlen over strlen as
it's guaranteed terminated by the = 0 above, but  thanks.

cheers, Joe

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-27 Thread Borislav Petkov
On Wed, May 27, 2015 at 10:07:34AM -0700, Joe Perches wrote:
> This code can memmove from beyond the x86_model_id field.

... in the theoretical case where some model ID has more than 64 - 48
preceding white spaces.

I guess we want to be prepared here for insane CPU model IDs coming from
virtualization.

> Maybe:
>   char *model = strim(c->x86_model_id);
>   memmove(c->x86_model_id, model, strlen(model) + 1);

Yes, and additionally limit that string length:

---
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index b35c777df6df..9d1fd48486d6 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -383,6 +383,9 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
 static void get_model_name(struct cpuinfo_x86 *c)
 {
unsigned int *v;
+   const char *model;
+
+#define MODEL_ID_MAXLEN 48
 
if (c->extended_cpuid_level < 0x8004)
return;
@@ -391,13 +394,15 @@ static void get_model_name(struct cpuinfo_x86 *c)
cpuid(0x8002, [0], [1], [2], [3]);
cpuid(0x8003, [4], [5], [6], [7]);
cpuid(0x8004, [8], [9], [10], [11]);
-   c->x86_model_id[48] = 0;
+   c->x86_model_id[MODEL_ID_MAXLEN] = 0;
 
/*
 * Remove leading whitespace on Intel processors and trailing
 * whitespace on AMD processors.
 */
-   memmove(c->x86_model_id, strim(c->x86_model_id), 48);
+   model = strim(c->x86_model_id);
+
+   memmove(c->x86_model_id, model, strnlen(model, MODEL_ID_MAXLEN) + 1);
 }
 
 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)

-- 
Regards/Gruss,
Boris.

ECO tip #101: Trim your mails when you reply.
--
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-27 Thread Joe Perches
On Wed, 2015-05-27 at 07:16 -0700, tip-bot for Prarit Bhargava wrote:

> x86/cpu: Strip any /proc/cpuinfo model name field whitespace
[]
> diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
> @@ -431,18 +430,10 @@ static void get_model_name(struct cpuinfo_x86 *c)
>   c->x86_model_id[48] = 0;
>  
>   /*
> -  * Intel chips right-justify this string for some dumb reason;
> -  * undo that brain damage:
> +  * Remove leading whitespace on Intel processors and trailing
> +  * whitespace on AMD processors.
>*/
> - p = q = >x86_model_id[0];
> - while (*p == ' ')
> - p++;
> - if (p != q) {
> - while (*p)
> - *q++ = *p++;
> - while (q <= >x86_model_id[48])
> - *q++ = '\0';/* Zero-pad the rest */
> - }
> + memmove(c->x86_model_id, strim(c->x86_model_id), 48);

This code can memmove from beyond the x86_model_id field.

If the id was a single right justified char, to avoid overrunning
the field, it'd be safer moving only the actual string and
terminating 0 though this code is sub-optimal:

memmove(c->x86_model_id, strim(c->x86_model_id),
strlen(strim(c->x86_model_id) + 1);

Maybe:
char *model = strim(c->x86_model_id);
memmove(c->x86_model_id, model, strlen(model) + 1);

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-27 Thread tip-bot for Prarit Bhargava
Commit-ID:  adafb98da6a7af5e45362933a7dae6ab0e5076bf
Gitweb: http://git.kernel.org/tip/adafb98da6a7af5e45362933a7dae6ab0e5076bf
Author: Prarit Bhargava 
AuthorDate: Tue, 26 May 2015 10:28:17 +0200
Committer:  Ingo Molnar 
CommitDate: Wed, 27 May 2015 14:38:24 +0200

x86/cpu: Strip any /proc/cpuinfo model name field whitespace

When comparing the 'model name' field of each core in
/proc/cpuinfo it was noticed that there is a whitespace
difference between the cores' model names.

After some quick investigation it was noticed that the model
name fields were actually different -- processor 0's model name
field had trailing whitespace removed, while the other
processors did not.

Another way of seeing this behaviour is to convert spaces into
underscores in the output of /proc/cpuinfo,

  [thetango@prarit ~]# grep "^model name" /proc/cpuinfo | uniq -c | sed 's/\ 
/_/g'
  __1_model_name  :_AMD_Opteron(TM)_Processor_6272
  _63_model_name  :_AMD_Opteron(TM)_Processor_6272_

which shows the discrepancy.

This occurs because the kernel calls strim() on cpu 0's
x86_model_id field to output a pretty message to the console in
print_cpu_info(), and as a result strips the whitespace at the
end of the ->x86_model_id field.

But, the ->x86_model_id field should be the same for the all
identical CPUs in the box. Thus, we need to remove both leading
and trailing whitespace.

As a result, the print_cpu_info() output looks like

  smpboot: CPU0: AMD Opteron(TM) Processor 6272 (fam: 15, model: 01, stepping: 
02)

and the x86_model_id field is correct on all processors on AMD
platforms:

  _64_model_name  :_AMD_Opteron(TM)_Processor_6272

Output is still correct on an Intel box:

  144_model_name  :_Intel(R)_Xeon(R)_CPU_E7-8890_v3_@_2.50GHz

Signed-off-by: Prarit Bhargava 
Signed-off-by: Borislav Petkov 
Cc: Andy Lutomirski 
Cc: Borislav Petkov 
Cc: Brian Gerst 
Cc: Dave Hansen 
Cc: Denys Vlasenko 
Cc: Fenghua Yu 
Cc: H. Peter Anvin 
Cc: Igor Mammedov 
Cc: Linus Torvalds 
Cc: Peter Zijlstra 
Cc: Thomas Gleixner 
Link: 
http://lkml.kernel.org/r/1432050210-32036-1-git-send-email-pra...@redhat.com
Link: http://lkml.kernel.org/r/1432628901-18044-15-git-send-email...@alien8.de
Signed-off-by: Ingo Molnar 
---
 arch/x86/kernel/cpu/common.c | 17 -
 1 file changed, 4 insertions(+), 13 deletions(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index a62cf04..41a8e9c 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -419,7 +419,6 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
 static void get_model_name(struct cpuinfo_x86 *c)
 {
unsigned int *v;
-   char *p, *q;
 
if (c->extended_cpuid_level < 0x8004)
return;
@@ -431,18 +430,10 @@ static void get_model_name(struct cpuinfo_x86 *c)
c->x86_model_id[48] = 0;
 
/*
-* Intel chips right-justify this string for some dumb reason;
-* undo that brain damage:
+* Remove leading whitespace on Intel processors and trailing
+* whitespace on AMD processors.
 */
-   p = q = >x86_model_id[0];
-   while (*p == ' ')
-   p++;
-   if (p != q) {
-   while (*p)
-   *q++ = *p++;
-   while (q <= >x86_model_id[48])
-   *q++ = '\0';/* Zero-pad the rest */
-   }
+   memmove(c->x86_model_id, strim(c->x86_model_id), 48);
 }
 
 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
@@ -1122,7 +1113,7 @@ void print_cpu_info(struct cpuinfo_x86 *c)
printk(KERN_CONT "%s ", vendor);
 
if (c->x86_model_id[0])
-   printk(KERN_CONT "%s", strim(c->x86_model_id));
+   printk(KERN_CONT "%s", c->x86_model_id);
else
printk(KERN_CONT "%d86", c->x86);
 
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-27 Thread Borislav Petkov
On Wed, May 27, 2015 at 10:07:34AM -0700, Joe Perches wrote:
 This code can memmove from beyond the x86_model_id field.

... in the theoretical case where some model ID has more than 64 - 48
preceding white spaces.

I guess we want to be prepared here for insane CPU model IDs coming from
virtualization.

 Maybe:
   char *model = strim(c-x86_model_id);
   memmove(c-x86_model_id, model, strlen(model) + 1);

Yes, and additionally limit that string length:

---
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index b35c777df6df..9d1fd48486d6 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -383,6 +383,9 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
 static void get_model_name(struct cpuinfo_x86 *c)
 {
unsigned int *v;
+   const char *model;
+
+#define MODEL_ID_MAXLEN 48
 
if (c-extended_cpuid_level  0x8004)
return;
@@ -391,13 +394,15 @@ static void get_model_name(struct cpuinfo_x86 *c)
cpuid(0x8002, v[0], v[1], v[2], v[3]);
cpuid(0x8003, v[4], v[5], v[6], v[7]);
cpuid(0x8004, v[8], v[9], v[10], v[11]);
-   c-x86_model_id[48] = 0;
+   c-x86_model_id[MODEL_ID_MAXLEN] = 0;
 
/*
 * Remove leading whitespace on Intel processors and trailing
 * whitespace on AMD processors.
 */
-   memmove(c-x86_model_id, strim(c-x86_model_id), 48);
+   model = strim(c-x86_model_id);
+
+   memmove(c-x86_model_id, model, strnlen(model, MODEL_ID_MAXLEN) + 1);
 }
 
 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)

-- 
Regards/Gruss,
Boris.

ECO tip #101: Trim your mails when you reply.
--
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-27 Thread Joe Perches
On Wed, 2015-05-27 at 21:06 +0200, Borislav Petkov wrote:
 On Wed, May 27, 2015 at 10:07:34AM -0700, Joe Perches wrote:
  This code can memmove from beyond the x86_model_id field.
 
 ... in the theoretical case where some model ID has more than 64 - 48
 preceding white spaces.
 
 I guess we want to be prepared here for insane CPU model IDs coming from
 virtualization.
 
  Maybe:
  char *model = strim(c-x86_model_id);
  memmove(c-x86_model_id, model, strlen(model) + 1);
 
 Yes, and additionally limit that string length:
 
 ---
 diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
[]
 @@ -383,6 +383,9 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = 
 {};
  static void get_model_name(struct cpuinfo_x86 *c)
  {
   unsigned int *v;
 + const char *model;
 +
 +#define MODEL_ID_MAXLEN 48
  
   if (c-extended_cpuid_level  0x8004)
   return;
 @@ -391,13 +394,15 @@ static void get_model_name(struct cpuinfo_x86 *c)
   cpuid(0x8002, v[0], v[1], v[2], v[3]);
   cpuid(0x8003, v[4], v[5], v[6], v[7]);
   cpuid(0x8004, v[8], v[9], v[10], v[11]);
 - c-x86_model_id[48] = 0;
 + c-x86_model_id[MODEL_ID_MAXLEN] = 0;
  
   /*
* Remove leading whitespace on Intel processors and trailing
* whitespace on AMD processors.
*/
 - memmove(c-x86_model_id, strim(c-x86_model_id), 48);
 + model = strim(c-x86_model_id);
 +
 + memmove(c-x86_model_id, model, strnlen(model, MODEL_ID_MAXLEN) + 1);

I don't see any value in the #define or strnlen over strlen as
it's guaranteed terminated by the = 0 above, but shrug thanks.

cheers, Joe

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-27 Thread tip-bot for Prarit Bhargava
Commit-ID:  adafb98da6a7af5e45362933a7dae6ab0e5076bf
Gitweb: http://git.kernel.org/tip/adafb98da6a7af5e45362933a7dae6ab0e5076bf
Author: Prarit Bhargava pra...@redhat.com
AuthorDate: Tue, 26 May 2015 10:28:17 +0200
Committer:  Ingo Molnar mi...@kernel.org
CommitDate: Wed, 27 May 2015 14:38:24 +0200

x86/cpu: Strip any /proc/cpuinfo model name field whitespace

When comparing the 'model name' field of each core in
/proc/cpuinfo it was noticed that there is a whitespace
difference between the cores' model names.

After some quick investigation it was noticed that the model
name fields were actually different -- processor 0's model name
field had trailing whitespace removed, while the other
processors did not.

Another way of seeing this behaviour is to convert spaces into
underscores in the output of /proc/cpuinfo,

  [thetango@prarit ~]# grep ^model name /proc/cpuinfo | uniq -c | sed 's/\ 
/_/g'
  __1_model_name  :_AMD_Opteron(TM)_Processor_6272
  _63_model_name  :_AMD_Opteron(TM)_Processor_6272_

which shows the discrepancy.

This occurs because the kernel calls strim() on cpu 0's
x86_model_id field to output a pretty message to the console in
print_cpu_info(), and as a result strips the whitespace at the
end of the -x86_model_id field.

But, the -x86_model_id field should be the same for the all
identical CPUs in the box. Thus, we need to remove both leading
and trailing whitespace.

As a result, the print_cpu_info() output looks like

  smpboot: CPU0: AMD Opteron(TM) Processor 6272 (fam: 15, model: 01, stepping: 
02)

and the x86_model_id field is correct on all processors on AMD
platforms:

  _64_model_name  :_AMD_Opteron(TM)_Processor_6272

Output is still correct on an Intel box:

  144_model_name  :_Intel(R)_Xeon(R)_CPU_E7-8890_v3_@_2.50GHz

Signed-off-by: Prarit Bhargava pra...@redhat.com
Signed-off-by: Borislav Petkov b...@suse.de
Cc: Andy Lutomirski l...@amacapital.net
Cc: Borislav Petkov b...@alien8.de
Cc: Brian Gerst brge...@gmail.com
Cc: Dave Hansen dave.han...@linux.intel.com
Cc: Denys Vlasenko dvlas...@redhat.com
Cc: Fenghua Yu fenghua...@intel.com
Cc: H. Peter Anvin h...@zytor.com
Cc: Igor Mammedov imamm...@redhat.com
Cc: Linus Torvalds torva...@linux-foundation.org
Cc: Peter Zijlstra pet...@infradead.org
Cc: Thomas Gleixner t...@linutronix.de
Link: 
http://lkml.kernel.org/r/1432050210-32036-1-git-send-email-pra...@redhat.com
Link: http://lkml.kernel.org/r/1432628901-18044-15-git-send-email...@alien8.de
Signed-off-by: Ingo Molnar mi...@kernel.org
---
 arch/x86/kernel/cpu/common.c | 17 -
 1 file changed, 4 insertions(+), 13 deletions(-)

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index a62cf04..41a8e9c 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -419,7 +419,6 @@ static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
 static void get_model_name(struct cpuinfo_x86 *c)
 {
unsigned int *v;
-   char *p, *q;
 
if (c-extended_cpuid_level  0x8004)
return;
@@ -431,18 +430,10 @@ static void get_model_name(struct cpuinfo_x86 *c)
c-x86_model_id[48] = 0;
 
/*
-* Intel chips right-justify this string for some dumb reason;
-* undo that brain damage:
+* Remove leading whitespace on Intel processors and trailing
+* whitespace on AMD processors.
 */
-   p = q = c-x86_model_id[0];
-   while (*p == ' ')
-   p++;
-   if (p != q) {
-   while (*p)
-   *q++ = *p++;
-   while (q = c-x86_model_id[48])
-   *q++ = '\0';/* Zero-pad the rest */
-   }
+   memmove(c-x86_model_id, strim(c-x86_model_id), 48);
 }
 
 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
@@ -1122,7 +1113,7 @@ void print_cpu_info(struct cpuinfo_x86 *c)
printk(KERN_CONT %s , vendor);
 
if (c-x86_model_id[0])
-   printk(KERN_CONT %s, strim(c-x86_model_id));
+   printk(KERN_CONT %s, c-x86_model_id);
else
printk(KERN_CONT %d86, c-x86);
 
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace

2015-05-27 Thread Joe Perches
On Wed, 2015-05-27 at 07:16 -0700, tip-bot for Prarit Bhargava wrote:

 x86/cpu: Strip any /proc/cpuinfo model name field whitespace
[]
 diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
 @@ -431,18 +430,10 @@ static void get_model_name(struct cpuinfo_x86 *c)
   c-x86_model_id[48] = 0;
  
   /*
 -  * Intel chips right-justify this string for some dumb reason;
 -  * undo that brain damage:
 +  * Remove leading whitespace on Intel processors and trailing
 +  * whitespace on AMD processors.
*/
 - p = q = c-x86_model_id[0];
 - while (*p == ' ')
 - p++;
 - if (p != q) {
 - while (*p)
 - *q++ = *p++;
 - while (q = c-x86_model_id[48])
 - *q++ = '\0';/* Zero-pad the rest */
 - }
 + memmove(c-x86_model_id, strim(c-x86_model_id), 48);

This code can memmove from beyond the x86_model_id field.

If the id was a single right justified char, to avoid overrunning
the field, it'd be safer moving only the actual string and
terminating 0 though this code is sub-optimal:

memmove(c-x86_model_id, strim(c-x86_model_id),
strlen(strim(c-x86_model_id) + 1);

Maybe:
char *model = strim(c-x86_model_id);
memmove(c-x86_model_id, model, strlen(model) + 1);

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/