Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp696379ybk; Fri, 15 May 2020 11:08:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwWJ4rto9JGUenxzFj3QluPTGObMagOZ7jZvW7tIs1sE4yY58pwMKihEegGQrA4IkFaRDXT X-Received: by 2002:a17:906:1e47:: with SMTP id i7mr4089984ejj.61.1589566111923; Fri, 15 May 2020 11:08:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589566111; cv=none; d=google.com; s=arc-20160816; b=juxhm+U5SJKs7R4cyHIjNzuySQ/K5swoBF9whfKuNBS9bWFWSJ55pdU5InhR/6/QAr IWQQm2HtWvciX7TOfA3CqHb6KsiF4ZiRPT5ekFURWn4TlIATdOidcddmzBKatFq9lwTl Ef72JIkwSyvcg/n6O4YfuwVfHEAqQ8ME6/K3avGlozXenQuhpdoE1B8HAg5uwwtvMtbu gT/jTZekLHd6iqgappR3dNa66mFJpCwMHttCtQlrNW+FOuoO0CAUYj9O0PeHlKBTIokG h/J69i+W9D1czFBBMoxbDfNCNg8dZ7/dr6LQqKowjug6YOxPirk2geiH++0sQrCSxHCZ 2TiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=L/+EYSk36TST2rIE8NznK9E9+/xuLx5a1Z0fVdYAEAY=; b=hBVSxayQ16U6W/1TYw+1acjf/H3mJH2qC5gsl9N836aAXyb9ik8pxJFr3TDshgmiuX zUzHgjeAUdNYmZEer3UbgAmunwaX/Nm2QGXqhx9LDM6V4uN4NEEhEkmZb3kAgK+wud7N k7J7dSVvqgNYTJ0h/LXrbpakeEM9zVgKdiZSqQqUij41eq2iZ/EoC8IFZ2x6UQgnc3Jl OHR8LjY34WQhwA7gkm/FLbO1DUni6S59y5RxcaBYPxMF0mS9xlEgKjevGoXf14uwlpaG S7gn8o5F9ZIIFxS/zM73ts215q7AkJhIZUSXTeOMxuzp4EYaALFbkRjdVLaH44SuK6C4 hl0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=qt025oqx; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d32si1669126edc.454.2020.05.15.11.08.07; Fri, 15 May 2020 11:08:31 -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=@google.com header.s=20161025 header.b=qt025oqx; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726292AbgEOSGd (ORCPT + 99 others); Fri, 15 May 2020 14:06:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726250AbgEOSGc (ORCPT ); Fri, 15 May 2020 14:06:32 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABF37C05BD09 for ; Fri, 15 May 2020 11:06:31 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id l4so3124984qke.2 for ; Fri, 15 May 2020 11:06:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=L/+EYSk36TST2rIE8NznK9E9+/xuLx5a1Z0fVdYAEAY=; b=qt025oqxlccbdZIKGGePBSeQj+NCxNAypwS2fm5s7SgZ9lqy37Pi84+SlnfTpTPOsS mxf/QQ6AGrrgLzSDCrVT4/cEDTlfMi9DB5vYjzvQc1T5BITZyPJ3ngCJVIxbe0dW6EWt Zgevf/JvOHpzF+Lju6skjTrB35btnsBv7iPqTWYOabX0KXaTtScs0pH9A/MKWDCFRsS0 MBDhM5W5wPiIAZMb3lDPa7wANlc8OoW52cAsqIthcJEalPRg3FQ+nDjhS9JFXnAcFp23 Pb6tahUikecLS3UQTpHQK1WfVZCpnocrhUdU6RaRIHm1StsLfZazMj25CINrYUIoemZO FlNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=L/+EYSk36TST2rIE8NznK9E9+/xuLx5a1Z0fVdYAEAY=; b=ZpMNnEEyHxXB7euj8qwCKY5oLXIdzh2pq4Fi1C/f0RV2tlWqIr7E8Hwek4guiAiScd DUYUKvlTwCJFysfdBL/GD9rNq8jt8WQjgEEBQ+2gt4t3DskIvt7tTG99/fs7GcxVvsQ7 ekdk5elndFwgCKZBoMtCNP0lyeCAIQzkI7vxdKOX/dcBzCMvoTLCrq1RVb0am1ZQy5A6 cuW8LZlhyaQLvCy4gQmAY5n8TDvBJkcVdUkyXTigrGIBL9pfM1XWURXHAIdp4DrtTZ38 yhm9zdgRYX2aVMoakvaeke1eEdln+SiPzode87sCTQwPmqSnlLb4OB9DSbx14uhv0SGO 3wDg== X-Gm-Message-State: AOAM533l4HghpJWdRYj9LXjzr2WDyr1hgiuCuVZx+BHdJP1ijpnQmq8r Q76s2u2lRW2+t4PjHHCEGKpYuK8ZoiGics8= X-Received: by 2002:a0c:9c4f:: with SMTP id w15mr4602062qve.245.1589565990696; Fri, 15 May 2020 11:06:30 -0700 (PDT) Date: Fri, 15 May 2020 11:05:40 -0700 Message-Id: <20200515180544.59824-1-inglorion@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.26.2.761.g0e0b3e54be-goog Subject: [PATCH] x86_64: fix jiffies ODR violation From: Bob Haarman To: Thomas Gleixner , Ingo Molnar , Borislav Petkov Cc: Fangrui Song , Sami Tolvanen , Bob Haarman , stable@vger.kernel.org, Nathan Chancellor , Alistair Delva , Nick Desaulniers , x86@kernel.org, "H. Peter Anvin" , Masami Hiramatsu , Josh Poimboeuf , Vincenzo Frascino , Kyung Min Park , afzal mohammed , Peter Zijlstra , Kees Cook , Heiko Carstens , Dave Hansen , Baoquan He , Thomas Lendacky , Ross Zwisler , Arvind Sankar , Dmitry Safonov <0x7f454c46@gmail.com>, Andi Kleen , linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org `jiffies` and `jiffies_64` are meant to alias (two different symbols that share the same address). Most architectures make the symbols alias to the same address via linker script assignment in their arch//kernel/vmlinux.lds.S: jiffies = jiffies_64; which is effectively a definition of jiffies. jiffies and jiffies_64 are both forward declared for all arch's in: include/linux/jiffies.h. jiffies_64 is defined in kernel/time/timer.c for all arch's. x86_64 was peculiar in that it wasn't doing the above linker script assignment, but rather was: 1. defining jiffies in arch/x86/kernel/time.c instead via linker script. 2. overriding the symbol jiffies_64 from kernel/time/timer.c in arch/x86/kernel/vmlinux.lds.s via `jiffies_64 = jiffies;`. As Fangrui notes: ``` In LLD, symbol assignments in linker scripts override definitions in object files. GNU ld appears to have the same behavior. It would probably make sense for LLD to error "duplicate symbol" but GNU ld is unlikely to adopt for compatibility reasons. ``` So we have an ODR violation (UB), which we seem to have gotten away with thus far. Where it becomes harmful is when we: 1. Use -fno-semantic-interposition. As Fangrui notes: ``` Clang after LLVM commit 5b22bcc2b70d ("[X86][ELF] Prefer to lower MC_GlobalAddress operands to .Lfoo$local") defaults to -fno-semantic-interposition similar semantics which help -fpic/-fPIC code avoid GOT/PLT when the referenced symbol is defined within the same translation unit. Unlike GCC -fno-semantic-interposition, Clang emits such relocations referencing local symbols for non-pic code as well. ``` This causes references to jiffies to refer to `.Ljiffies$local` when jiffies is defined in the same translation unit. Likewise, references to jiffies_64 become references to `.Ljiffies_64$local` in translation units that define jiffies_64. Because these differ from the names used in the linker script, they will not be rewritten to alias one another. Combined with ... 2. Full LTO effectively treats all source files as one translation unit, causing these local references to be produced everywhere. When the linker processes the linker script, there are no longer any references to `jiffies_64` anywhere to replace with `jiffies`. And thus `.Ljiffies$local` and `.Ljiffies_64$local` no longer alias at all. In the process of porting patches enabling Full LTO from arm64 to x86_64, we observe spooky bugs where the kernel appeared to boot, but init doesn't get scheduled. Instead, we can avoid the ODR violation by matching other arch's by defining jiffies only by linker script. For -fno-semantic-interposition + Full LTO, there is no longer a global definition of jiffies for the compiler to produce a local symbol which the linker script won't ensure aliases to jiffies_64. Link: https://github.com/ClangBuiltLinux/linux/issues/852 Fixes: 40747ffa5aa8 ("asmlinkage: Make jiffies visible") Cc: stable@vger.kernel.org Reported-by: Nathan Chancellor Reported-by: Alistair Delva Suggested-by: Fangrui Song Debugged-by: Nick Desaulniers Debugged-by: Sami Tolvanen Signed-off-by: Bob Haarman --- arch/x86/kernel/time.c | 4 ---- arch/x86/kernel/vmlinux.lds.S | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/time.c b/arch/x86/kernel/time.c index 371a6b348e44..e42faa792c07 100644 --- a/arch/x86/kernel/time.c +++ b/arch/x86/kernel/time.c @@ -25,10 +25,6 @@ #include #include -#ifdef CONFIG_X86_64 -__visible volatile unsigned long jiffies __cacheline_aligned_in_smp = INITIAL_JIFFIES; -#endif - unsigned long profile_pc(struct pt_regs *regs) { unsigned long pc = instruction_pointer(regs); diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index 1bf7e312361f..7c35556c7827 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -40,13 +40,13 @@ OUTPUT_FORMAT(CONFIG_OUTPUT_FORMAT) #ifdef CONFIG_X86_32 OUTPUT_ARCH(i386) ENTRY(phys_startup_32) -jiffies = jiffies_64; #else OUTPUT_ARCH(i386:x86-64) ENTRY(phys_startup_64) -jiffies_64 = jiffies; #endif +jiffies = jiffies_64; + #if defined(CONFIG_X86_64) /* * On 64-bit, align RODATA to 2MB so we retain large page mappings for -- 2.26.2.761.g0e0b3e54be-goog