Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp749107pxf; Thu, 25 Mar 2021 13:11:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz2BKmqFnsbIuvVa6N+OxPrEzRnlPa6L8UggOKUgF0VjDEWhrDp/VefE2vGBUkh0rDELBOf X-Received: by 2002:a05:6402:2215:: with SMTP id cq21mr11309931edb.281.1616703088473; Thu, 25 Mar 2021 13:11:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616703088; cv=none; d=google.com; s=arc-20160816; b=06TSpeTFQWkzzLLEjRTZk7eBd7+wTFrLATXQXc4FMxWJSVTIe2ivnLKmEVt7VqfsqK SWpSKErJst0dH8+PRf92pkuHjaXN6IPOt84WyfFzjEVJ5GVV0qp9QXPGptJ3csIz4OcV IKF23EOlVPItIIE8Ou75zjU4dSZaSmY/6SOZ6B8SZcD2Zg7pBiwpN/LHNKpgGDO8czvV qq/5NOJRT4O5iPBXgmBiYOrLI4T4Hh5m0zd5nNGuuH2FUTdeybFV2j60Srb/IhyeToYY w7lSwElZYWY0pg6aZWH5oduACockiAtoGxk+5uj1xBJZsbqple1J4zAWYWqUcceuZoH5 29rQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=mlAyvCnXo3hHmcqDcLa0T0+zPXCZVaDSlZjjwBtnDCY=; b=sHWVuxpFaJVgIAwuM/M833UNOWNt29d/uOwtGFkWGKxPiREFgUZAIMuTb9dw48/eqM vLQlNjHgGPN7Nn5WBa4gzRZLhIaJUtLYz043DT2ylVSYm7SHpWKwYXyRjo8mfJ0p3fMq oKefd/3G8tYMD3FEOLUD/Gr5ig1so7U/5EStq9/sAPgLG0hKVt3VLXk16h3p+itGDu6C /WAYNgG7pcLPV9EqTIB/02dbYiq8i/JXRMeL+iN2kNbhGCzPPWlzfPvYBUEDsH8swly/ ZeXEZmXK2w64hi0mZMQiEPMc4X23WmU+QvfmmNtWZongjyV9cPNIq/rOmusgHu12Ki3C YbbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=lkUeFKqn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z16si5207393edc.65.2021.03.25.13.11.05; Thu, 25 Mar 2021 13:11:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=lkUeFKqn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230267AbhCYUKI (ORCPT + 99 others); Thu, 25 Mar 2021 16:10:08 -0400 Received: from mail.skyhub.de ([5.9.137.197]:59234 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230258AbhCYUJk (ORCPT ); Thu, 25 Mar 2021 16:09:40 -0400 Received: from zn.tnic (p200300ec2f0d5d0094e6cb0f12bb7e2a.dip0.t-ipconnect.de [IPv6:2003:ec:2f0d:5d00:94e6:cb0f:12bb:7e2a]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 683AF1EC0249; Thu, 25 Mar 2021 21:09:38 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1616702978; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references; bh=mlAyvCnXo3hHmcqDcLa0T0+zPXCZVaDSlZjjwBtnDCY=; b=lkUeFKqn1wgy3MlXzWs7hStJOpvfYU/tTSZ8zOYUwS80eXwWt0+AaAq/qbqRDdR2dOttQd qDNk+HsALrwZvtX6EmcdW2Za2ZTKQEsCyb3Mt3PHOVCOECvGRx+9zhFJMJcbCU35iY/y2o W7f4GxpgqWzgUzEAKvw/RJPN4HHJMLs= Date: Thu, 25 Mar 2021 21:09:42 +0100 From: Borislav Petkov To: stable Cc: Hugh Dickins , Babu Moger , Paolo Bonzini , Jim Mattson , Vitaly Kuznetsov , Wanpeng Li , kvm list , Joerg Roedel , the arch/x86 maintainers , LKML , Ingo Molnar , "H . Peter Anvin" , Thomas Gleixner , Makarand Sonare , Sean Christopherson Subject: [PATCH] x86/tlb: Flush global mappings when KAISER is disabled Message-ID: <20210325200942.GJ31322@zn.tnic> References: <20210311214013.GH5829@zn.tnic> <4a72f780-3797-229e-a938-6dc5b14bec8d@amd.com> <20210311235215.GI5829@zn.tnic> <20210324212139.GN5010@zn.tnic> <20210325095619.GC31322@zn.tnic> <20210325102959.GD31322@zn.tnic> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210325102959.GD31322@zn.tnic> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi stable folks, the patch below fixes kernels 4.4 and 4.9 booting on AMD platforms with PCID support. It doesn't have an upstream counterpart because it patches the KAISER code which didn't go upstream. It applies fine to both of the aforementioned kernels - please pick it up. Thx. --- From: Borislav Petkov Date: Thu, 25 Mar 2021 11:02:31 +0100 Subject: [PATCH] x86/tlb: Flush global mappings when KAISER is disabled Jim Mattson reported that Debian 9 guests using a 4.9-stable kernel are exploding during alternatives patching: kernel BUG at /build/linux-dqnRSc/linux-4.9.228/arch/x86/kernel/alternative.c:709! invalid opcode: 0000 [#1] SMP Modules linked in: CPU: 1 PID: 1 Comm: swapper/0 Not tainted 4.9.0-13-amd64 #1 Debian 4.9.228-1 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: swap_entry_free swap_entry_free text_poke_bp swap_entry_free arch_jump_label_transform set_debug_rodata __jump_label_update static_key_slow_inc frontswap_register_ops init_zswap init_frontswap do_one_initcall set_debug_rodata kernel_init_freeable rest_init kernel_init ret_from_fork triggering the BUG_ON in text_poke() which verifies whether patched instruction bytes have actually landed at the destination. Further debugging showed that the TLB flush before that check is insufficient because there could be global mappings left in the TLB, leading to a stale mapping getting used. I say "global mappings" because the hardware configuration is a new one: machine is an AMD, which means, KAISER/PTI doesn't need to be enabled there, which also means there's no user/kernel pagetables split and therefore the TLB can have global mappings. And the configuration is new one for a second reason: because that AMD machine supports PCID and INVPCID, which leads the CPU detection code to set the synthetic X86_FEATURE_INVPCID_SINGLE flag. Now, __native_flush_tlb_single() does invalidate global mappings when X86_FEATURE_INVPCID_SINGLE is *not* set and returns. When X86_FEATURE_INVPCID_SINGLE is set, however, it invalidates the requested address from both PCIDs in the KAISER-enabled case. But if KAISER is not enabled and the machine has global mappings in the TLB, then those global mappings do not get invalidated, which would lead to the above mismatch from using a stale TLB entry. So make sure to flush those global mappings in the KAISER disabled case. Co-debugged by Babu Moger . Reported-by: Jim Mattson Signed-off-by: Borislav Petkov Acked-by: Hugh Dickins Reviewed-by: Paolo Bonzini Tested-by: Babu Moger Tested-by: Jim Mattson Link: https://lkml.kernel.org/r/CALMp9eRDSW66%2BXvbHVF4ohL7XhThoPoT0BrB0TcS0cgk=dkcBg@mail.gmail.com --- arch/x86/include/asm/tlbflush.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index f5ca15622dc9..2bfa4deb8cae 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -245,12 +245,15 @@ static inline void __native_flush_tlb_single(unsigned long addr) * ASID. But, userspace flushes are probably much more * important performance-wise. * - * Make sure to do only a single invpcid when KAISER is - * disabled and we have only a single ASID. + * In the KAISER disabled case, do an INVLPG to make sure + * the mapping is flushed in case it is a global one. */ - if (kaiser_enabled) + if (kaiser_enabled) { invpcid_flush_one(X86_CR3_PCID_ASID_USER, addr); - invpcid_flush_one(X86_CR3_PCID_ASID_KERN, addr); + invpcid_flush_one(X86_CR3_PCID_ASID_KERN, addr); + } else { + asm volatile("invlpg (%0)" ::"r" (addr) : "memory"); + } } static inline void __flush_tlb_all(void) -- 2.29.2 -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette