From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754123AbbE1Q7u (ORCPT ); Thu, 28 May 2015 12:59:50 -0400 Received: from terminus.zytor.com ([198.137.202.10]:50957 "EHLO mail.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751672AbbE1Q7l (ORCPT ); Thu, 28 May 2015 12:59:41 -0400 User-Agent: K-9 Mail for Android In-Reply-To: <20150528125819.GB31800@pd.tnic> References: <1432050210-32036-1-git-send-email-prarit@redhat.com> <1432628901-18044-15-git-send-email-bp@alien8.de> <1432746454.2846.154.camel@perches.com> <20150527190626.GC19407@pd.tnic> <1432754207.2846.162.camel@perches.com> <5566FB97.5020904@redhat.com> <20150528113229.GA31800@pd.tnic> <20150528125819.GB31800@pd.tnic> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Subject: Re: [tip:x86/cpu] x86/cpu: Strip any /proc/ cpuinfo model name field whitespace From: "H. Peter Anvin" Date: Thu, 28 May 2015 09:57:15 -0700 To: Borislav Petkov , Prarit Bhargava CC: Joe Perches , luto@amacapital.net, peterz@infradead.org, dvlasenk@redhat.com, torvalds@linux-foundation.org, imammedo@redhat.com, brgerst@gmail.com, mingo@kernel.org, dave.hansen@linux.intel.com, fenghua.yu@intel.com, linux-kernel@vger.kernel.org, tglx@linutronix.de, bp@suse.de, linux-tip-commits@vger.kernel.org Message-ID: <2A88ADE0-90EF-4A94-BF4D-463E478B2FDA@zytor.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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-prarit@redhat.com >Link: >http://lkml.kernel.org/r/1432628901-18044-15-git-send-email-bp@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 < 0x80000004) > return; >@@ -429,11 +431,26 @@ static void get_model_name(struct cpuinfo_x86 *c) > cpuid(0x80000004, &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.