Received: by 10.223.164.202 with SMTP id h10csp1287919wrb; Wed, 15 Nov 2017 17:02:42 -0800 (PST) X-Google-Smtp-Source: AGs4zMaW8cN/VTw8BMq7Hm8FC1dcO59LfrrY/H4YleUCac594onPK+k8saLIfrehPdAYFKdsVxGN X-Received: by 10.98.207.70 with SMTP id b67mr4992739pfg.220.1510794162214; Wed, 15 Nov 2017 17:02:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510794162; cv=none; d=google.com; s=arc-20160816; b=hVzHyNWj9aaJQn/L0eDmFbPLJ4jk0hwJTGSaxn3UBw1WC9FM+5ZU3mES2qUAvkdax7 3Y2aMPfp1VdFfab72HcLPihfD+/wk4Dqq0cwnRR1GkjRUCOFlNrp1C5WlkXclzCAxn0F SN7z2CDdhFsSHJqOfZ2Ul66R2ac87GvwY3odTQU0rIy8ajxXjmnMSdDutsW91wKbo3JF Gw2GNi66k0nKx+4eyaEuinSASLBOyjpnMrXTKzefwh0YJrx8Iq99Gcse1S1+xty1v3Zo SHq2M9TumK0pvLcwA4KQ/tgXOB3lMV9IleNeHjfmwsmXALX22UTG+YXvdEFhTJ2ovSfN VdNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature:arc-authentication-results; bh=xpSlY8G/+NUvoaY8hkrL9DT98HhId34ojy2z1RXUyos=; b=G7ML4Ohs0RFJJTbihidRIJn/r5h6OfB3hMBpSf3fvoL6jSGYDSwHJIhej+WLpg4HoS vfjXb2e/KusU0La/5ZNZ2+uVmvbeWulJxjRI5nxYows6+f8oMJEkRQdiYh9i3SHDu50C tpGaSfJVPYoUGiTuM4hYxiIgcmFBSoKrfbGeDjd12aPCiRnC6FMy9lzQNJiyx3Jgl472 RUo7IVoAhOdeyU3Fjbx0BQUc0EAMDo4S6RAQ8OP7xNFJPpBf04uBR19Fz6df6Dryian/ /YTwexjz9SMHQj3a2dKNNf2jvZLXnEJqJwUng0MTsO1xGe2cGUPm0gWMsZmkHE8MDvhs 1SIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=iP0qn43V; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b6si11492798plm.319.2017.11.15.17.02.29; Wed, 15 Nov 2017 17:02:42 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=iP0qn43V; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933848AbdKOXcE (ORCPT + 89 others); Wed, 15 Nov 2017 18:32:04 -0500 Received: from mail-wm0-f67.google.com ([74.125.82.67]:46172 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756360AbdKOXb5 (ORCPT ); Wed, 15 Nov 2017 18:31:57 -0500 Received: by mail-wm0-f67.google.com with SMTP id z3so6196367wme.5 for ; Wed, 15 Nov 2017 15:31:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=xpSlY8G/+NUvoaY8hkrL9DT98HhId34ojy2z1RXUyos=; b=iP0qn43Vy6GveLS8Gxnylu4QvE8gf+yFD11eFpFf4RTYWyGVs3wmwKcpiCZJBJeKVs ECP+FfDAPOoM2mZjgy1qvxo4fobHmKiDH1VClCPDTW1x6YNXzr+W9DuuEJBcKGbsrmDE YIEczbn5ugKWTc+Rw7s6TIdT7Wjrbpplo+HZ95ML1eDzQSCy2CDNQdMPFc11QrPXBg3M AqQO21VCW/Cy7R8PHxK1vHg1rxGh/fqP4soQzh9yCdHBo2gn1R3W5p34chCPM5ju6ncr 0et9gqtqyHpx2aiGiUnsX7B8i+BUEWgGpmj9h4ZKSz3S4Wv4cTNiX9aINLSOoLRYGMqV zcbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=xpSlY8G/+NUvoaY8hkrL9DT98HhId34ojy2z1RXUyos=; b=YI3ESTnyhQdG7WrqVfbW8+6Ir8dLYmG+Js8FnXfx7Cx3iNTF7ogOCEyYEFcf1tFw9d S43Ek8osAIzqYjV0KV3LO86y3nKiHt8/iSd48hjpO/xOyKv7oRyWraCu2HHcGgckVx2o bQDlpzGXZI8OAcg97aMCnKu8IauPmpTaC+lvCv7IrfATse3gSeQZJUv0BDrFdtnb5Fs6 0E4DC7lPbwpen0tT256/dlVKZIbGn/iMmG5NGpjpckarb4q09BuLjSe1LUd/1CHdh3aT DvgW6XmWWco8ZLqbUw6alumkBOh0yb7G1EIsSgTjFU5yknkvpFzmT8BKFVqnL8qe/Eq8 B/jQ== X-Gm-Message-State: AJaThX7SjPR4QBCmfyuKhOFK164kMHfg23sf/qdgwRftSjhcnh0ZHLCd zOjXmETv/CI+xNWgSVE19GI= X-Received: by 10.80.139.65 with SMTP id l59mr78123edl.187.1510788715914; Wed, 15 Nov 2017 15:31:55 -0800 (PST) Received: from [10.2.44.15] ([208.91.2.2]) by smtp.gmail.com with ESMTPSA id y40sm70014ede.0.2017.11.15.15.31.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Nov 2017 15:31:55 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: x86: CR4 update when IRQs are enabled From: Nadav Amit In-Reply-To: <2DE32618-CB44-43C7-BAFB-F6967248F97C@gmail.com> Date: Wed, 15 Nov 2017 15:31:50 -0800 Cc: LKML Content-Transfer-Encoding: quoted-printable Message-Id: References: <2DE32618-CB44-43C7-BAFB-F6967248F97C@gmail.com> To: Dave Hansen , "H. Peter Anvin" , Thomas Gleixner , Borislav Petkov , the arch/x86 maintainers , Andy Lutomirski X-Mailer: Apple Mail (2.3273) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Ping? Nadav Amit wrote: > CC=E2=80=99ing more people, and adding a patch to clarify... >=20 > Nadav Amit wrote: >=20 >> I am puzzled by the comment in tlb_state.cr4 , which says: >>=20 >> /* >> * Access to this CR4 shadow and to H/W CR4 is protected by >> * disabling interrupts when modifying either one. >> */ >>=20 >> This does not seem to be true and adding a warning to CR4 writes when >> !irqs_disabled() immediately fires in identify_cpu() and >> __mcheck_cpu_init_generic(). While these two are called during boot, = I think >> there other CR4 changes with enabled IRQs, for example, PR_SET_TSC. >>=20 >> So my question(s): Is the comment correct? Is the current behavior = correct? >=20 > So here is what I have in mind. I am not sure whether = CONFIG_DEBUG_PREEMPT is > the right #ifdef. Let me know what you think. >=20 > -- >8 -- >=20 > Subject: [PATCH] x86: disable IRQs before changing CR4 >=20 > CR4 changes need to be performed while IRQs are disabled in order to > update the CR4 shadow and the actual register atomically. >=20 > Signed-off-by: Nadav Amit > --- > arch/x86/include/asm/tlbflush.h | 18 ++++++++++++------ > arch/x86/kernel/cpu/common.c | 13 ++++++++++++- > arch/x86/kernel/cpu/mcheck/mce.c | 3 +++ > arch/x86/kernel/cpu/mcheck/p5.c | 4 ++++ > arch/x86/kernel/cpu/mcheck/winchip.c | 3 +++ > arch/x86/kernel/process.c | 14 ++++++++++++-- > 6 files changed, 46 insertions(+), 9 deletions(-) >=20 > diff --git a/arch/x86/include/asm/tlbflush.h = b/arch/x86/include/asm/tlbflush.h > index 50ea3482e1d1..bc70dd1cc7c6 100644 > --- a/arch/x86/include/asm/tlbflush.h > +++ b/arch/x86/include/asm/tlbflush.h > @@ -89,6 +89,15 @@ static inline void cr4_init_shadow(void) > this_cpu_write(cpu_tlbstate.cr4, __read_cr4()); > } >=20 > +static inline void update_cr4(unsigned long cr4) > +{ > +#ifdef CONFIG_DEBUG_PREEMPT > + WARN_ON_ONCE(!irqs_disabled()); > +#endif > + this_cpu_write(cpu_tlbstate.cr4, cr4); > + __write_cr4(cr4); > +} > + > /* Set in this cpu's CR4. */ > static inline void cr4_set_bits(unsigned long mask) > { > @@ -97,8 +106,7 @@ static inline void cr4_set_bits(unsigned long mask) > cr4 =3D this_cpu_read(cpu_tlbstate.cr4); > if ((cr4 | mask) !=3D cr4) { > cr4 |=3D mask; > - this_cpu_write(cpu_tlbstate.cr4, cr4); > - __write_cr4(cr4); > + update_cr4(cr4); > } > } >=20 > @@ -110,8 +118,7 @@ static inline void cr4_clear_bits(unsigned long = mask) > cr4 =3D this_cpu_read(cpu_tlbstate.cr4); > if ((cr4 & ~mask) !=3D cr4) { > cr4 &=3D ~mask; > - this_cpu_write(cpu_tlbstate.cr4, cr4); > - __write_cr4(cr4); > + update_cr4(cr4); > } > } >=20 > @@ -121,8 +128,7 @@ static inline void cr4_toggle_bits(unsigned long = mask) >=20 > cr4 =3D this_cpu_read(cpu_tlbstate.cr4); > cr4 ^=3D mask; > - this_cpu_write(cpu_tlbstate.cr4, cr4); > - __write_cr4(cr4); > + update_cr4(cr4); > } >=20 > /* Read the CR4 shadow. */ > diff --git a/arch/x86/kernel/cpu/common.c = b/arch/x86/kernel/cpu/common.c > index c8b39870f33e..82e6b41fd5e9 100644 > --- a/arch/x86/kernel/cpu/common.c > +++ b/arch/x86/kernel/cpu/common.c > @@ -318,6 +318,8 @@ static bool pku_disabled; >=20 > static __always_inline void setup_pku(struct cpuinfo_x86 *c) > { > + unsigned long flags; > + > /* check the boot processor, plus compile options for PKU: */ > if (!cpu_feature_enabled(X86_FEATURE_PKU)) > return; > @@ -327,7 +329,10 @@ static __always_inline void setup_pku(struct = cpuinfo_x86 *c) > if (pku_disabled) > return; >=20 > + local_irq_save(flags); > cr4_set_bits(X86_CR4_PKE); > + local_irq_restore(flags); > + > /* > * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE > * cpuid bit to be set. We need to ensure that we > @@ -1069,6 +1074,7 @@ static void validate_apic_and_package_id(struct = cpuinfo_x86 *c) > */ > static void identify_cpu(struct cpuinfo_x86 *c) > { > + unsigned long flags; > int i; >=20 > c->loops_per_jiffy =3D loops_per_jiffy; > @@ -1121,9 +1127,14 @@ static void identify_cpu(struct cpuinfo_x86 *c) > /* Disable the PN if appropriate */ > squash_the_stupid_serial_number(c); >=20 > - /* Set up SMEP/SMAP */ > + /* > + * Set up SMEP/SMAP. Disable interrupts to prevent triggering a = warning > + * as CR4 changes must be done with disabled interrupts. > + */ > + local_irq_save(flags); > setup_smep(c); > setup_smap(c); > + local_irq_restore(flags); >=20 > /* > * The vendor-specific functions might have changed features. > diff --git a/arch/x86/kernel/cpu/mcheck/mce.c = b/arch/x86/kernel/cpu/mcheck/mce.c > index 3b413065c613..497c07e33c25 100644 > --- a/arch/x86/kernel/cpu/mcheck/mce.c > +++ b/arch/x86/kernel/cpu/mcheck/mce.c > @@ -1508,6 +1508,7 @@ static void __mcheck_cpu_init_generic(void) > { > enum mcp_flags m_fl =3D 0; > mce_banks_t all_banks; > + unsigned long flags; > u64 cap; >=20 > if (!mca_cfg.bootlog) > @@ -1519,7 +1520,9 @@ static void __mcheck_cpu_init_generic(void) > bitmap_fill(all_banks, MAX_NR_BANKS); > machine_check_poll(MCP_UC | m_fl, &all_banks); >=20 > + local_irq_save(flags); > cr4_set_bits(X86_CR4_MCE); > + local_irq_restore(flags); >=20 > rdmsrl(MSR_IA32_MCG_CAP, cap); > if (cap & MCG_CTL_P) > diff --git a/arch/x86/kernel/cpu/mcheck/p5.c = b/arch/x86/kernel/cpu/mcheck/p5.c > index 2a0717bf8033..d5d4963415e9 100644 > --- a/arch/x86/kernel/cpu/mcheck/p5.c > +++ b/arch/x86/kernel/cpu/mcheck/p5.c > @@ -42,6 +42,7 @@ static void pentium_machine_check(struct pt_regs = *regs, long error_code) > /* Set up machine check reporting for processors with Intel style MCE: = */ > void intel_p5_mcheck_init(struct cpuinfo_x86 *c) > { > + unsigned long flags; > u32 l, h; >=20 > /* Default P5 to off as its often misconnected: */ > @@ -62,7 +63,10 @@ void intel_p5_mcheck_init(struct cpuinfo_x86 *c) > pr_info("Intel old style machine check architecture = supported.\n"); >=20 > /* Enable MCE: */ > + local_irq_save(flags); > cr4_set_bits(X86_CR4_MCE); > + local_irq_restore(flags); > + > pr_info("Intel old style machine check reporting enabled on = CPU#%d.\n", > smp_processor_id()); > } > diff --git a/arch/x86/kernel/cpu/mcheck/winchip.c = b/arch/x86/kernel/cpu/mcheck/winchip.c > index c6a722e1d011..6dd985e3849d 100644 > --- a/arch/x86/kernel/cpu/mcheck/winchip.c > +++ b/arch/x86/kernel/cpu/mcheck/winchip.c > @@ -26,6 +26,7 @@ static void winchip_machine_check(struct pt_regs = *regs, long error_code) > /* Set up machine check reporting on the Winchip C6 series */ > void winchip_mcheck_init(struct cpuinfo_x86 *c) > { > + unsigned long flags; > u32 lo, hi; >=20 > machine_check_vector =3D winchip_machine_check; > @@ -37,7 +38,9 @@ void winchip_mcheck_init(struct cpuinfo_x86 *c) > lo &=3D ~(1<<4); /* Enable MCE */ > wrmsr(MSR_IDT_FCR1, lo, hi); >=20 > + local_irq_save(flags); > cr4_set_bits(X86_CR4_MCE); > + local_irq_restore(flags); >=20 > pr_info("Winchip machine check reporting enabled on CPU#0.\n"); > } > diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c > index 3ca198080ea9..09e44e784745 100644 > --- a/arch/x86/kernel/process.c > +++ b/arch/x86/kernel/process.c > @@ -127,25 +127,35 @@ void flush_thread(void) >=20 > void disable_TSC(void) > { > + unsigned long flags; > + > preempt_disable(); > - if (!test_and_set_thread_flag(TIF_NOTSC)) > + if (!test_and_set_thread_flag(TIF_NOTSC)) { > /* > * Must flip the CPU state synchronously with > * TIF_NOTSC in the current running context. > */ > + local_irq_save(flags); > cr4_set_bits(X86_CR4_TSD); > + local_irq_restore(flags); > + } > preempt_enable(); > } >=20 > static void enable_TSC(void) > { > + unsigned long flags; > + > preempt_disable(); > - if (test_and_clear_thread_flag(TIF_NOTSC)) > + if (test_and_clear_thread_flag(TIF_NOTSC)) { > /* > * Must flip the CPU state synchronously with > * TIF_NOTSC in the current running context. > */ > + local_irq_save(flags); > cr4_clear_bits(X86_CR4_TSD); > + local_irq_restore(flags); > + } > preempt_enable(); > } From 1583568946812193160@xxx Thu Nov 09 06:30:32 +0000 2017 X-GM-THRID: 1583536401885830151 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread