From patchwork Fri Aug 21 19:42:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 248138 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:522:0:0:0:0 with SMTP id h2csp634399ils; Fri, 21 Aug 2020 12:46:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzAHc+Z6RUV+J8BnNOGeZHjHZl7zz83xe53Zq7ItQpCM81CKyOXvrectf8pNKy6V7RKIoze X-Received: by 2002:a17:906:2ddb:: with SMTP id h27mr4416191eji.203.1598039181416; Fri, 21 Aug 2020 12:46:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598039181; cv=none; d=google.com; s=arc-20160816; b=tsefe62Xq+IvH10HjxtA3HRzPTwyWJjyHXAKUJThmS+KAwTwXQGcLemubm+hT3zEwL kiu17MzQSTCp9lanxNPNRJiPJ5xXZ+MCLXKDFrPQRlvG1nk7a8MJVx/3CZjiH9uw1XTq M3jyVBebTl5s0yB1bUpNqky3/5wLJ/PO9nr/59vn71LuaaealZ5D+QSHyz+B5KWtyLty uxdJASnyZHyYmkMz+ImHJhqg8RDU242QFnsbqM5nLZGBsJ6DKmczE372+Y5HWA+3zJFX fLd+ly/45cJX/Y0fNw0Lsjd8z/SxgKQqqwwlFvjplMhgdzQc16x4OLrfh23oyGFTHKkp VXHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=LhHa8hwizWicyR7AmABsSaI963QK2ejKgNvGzEzQJyI=; b=Ugt9Nr5zke43kIorh1PYWXh9S4tsufvhBpBeG4BB3PmC7nZJL8OcrEBZsyBX6CADOm 7FnsGRssOgmvZ0nAt/ZRWupZWsuXNrUyHYETRneZQeO0U60j1e6auHCb8gH88jJFUecJ pG8xl+LV3hZvgl6R5WleJSvirezI68T6tVqe6zGekAmAPeV38/vda0SZ7ol9IEkCmHpy bLZBN838A3Ij9/uggutd+11nIUnUIWpNRviwA924BGYzp3Cm8fv+M4E3DLsaUET6VmsZ NOudEkm/mtKvihxbgjoYkQWr51QDGILd2fDK7854kLegUV/H7RhkZ6AEofF0/aZ7yd+B 0RZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=S9JWqqSo; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i20si1883860edx.426.2020.08.21.12.46.20; Fri, 21 Aug 2020 12:46:21 -0700 (PDT) Received-SPF: pass (google.com: domain of stable-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=@chromium.org header.s=google header.b=S9JWqqSo; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727010AbgHUTqU (ORCPT + 15 others); Fri, 21 Aug 2020 15:46:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726719AbgHUTo3 (ORCPT ); Fri, 21 Aug 2020 15:44:29 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82812C0617A3 for ; Fri, 21 Aug 2020 12:44:25 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id mw10so1251178pjb.2 for ; Fri, 21 Aug 2020 12:44:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LhHa8hwizWicyR7AmABsSaI963QK2ejKgNvGzEzQJyI=; b=S9JWqqSou1s/FK9u3DQ4o9uu0Z66hNrndmRM9lwRys0IRRdCU3reoSB3x/0i4D3/0d FE38SPzh4Sjdcmnpux/wEu2YEvs49KaQ4x96TdKBVxMC1MQo/Pu6aF3uTjn2T4WoNN1/ 69NCo9CYct3Ga+OFZvqV0/uxV0gjNFTPm/uUo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LhHa8hwizWicyR7AmABsSaI963QK2ejKgNvGzEzQJyI=; b=mjjPR+CCdWhIJfGN4P3bHgnDL8fjpEGf+r8x4+nTz5NoSB4QEnO+YDqx/xi6NF2PrE dbZkKvs4wDWQU8fEZm22dbmwNDS9PP+uFnG98G1lFg2fKD3Mg70M01vd7y0ynLSLVE1n WhNPGI51DVYeWnDeHDLwrqqZ6U8kSZWtqdo4UsGtaqwJNXjoVVenvf02IdFe0o44blbx CuNmaeMeHu6cst1cPpnJxvNyM+4EgVgLVoAKuzZNxT7lJ5iV4Hc9E44U7Bx7mYvbK8Eh psOhVYTk+NTW1T8aS2CNK0TkuHQmQDh0F30JztMrIDMasjmi4Pds/17YVsOM0YYtpPS9 iOzw== X-Gm-Message-State: AOAM533HF7TZ11VC8bnKL7qKOhnsooqK+JPkwgkhj1eakk9gQIogHObT +l14ylxOaUMX7CeaTOs3QxJL5g== X-Received: by 2002:a17:902:b70e:: with SMTP id d14mr3365610pls.253.1598039065065; Fri, 21 Aug 2020 12:44:25 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id a193sm3460814pfa.105.2020.08.21.12.44.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Aug 2020 12:44:21 -0700 (PDT) From: Kees Cook To: Ingo Molnar Cc: Kees Cook , Nick Desaulniers , Jian Cai , =?utf-8?b?RsSBbmctcnXDrCBTw7Ju?= =?utf-8?q?g?= , Luis Lozano , Manoj Gupta , stable@vger.kernel.org, Catalin Marinas , Mark Rutland , Ard Biesheuvel , Peter Collingbourne , James Morse , Borislav Petkov , Ingo Molnar , Russell King , Masahiro Yamada , Arvind Sankar , Nathan Chancellor , Arnd Bergmann , x86@kernel.org, clang-built-linux@googlegroups.com, linux-arch@vger.kernel.org, linux-efi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 06/29] vmlinux.lds.h: add PGO and AutoFDO input sections Date: Fri, 21 Aug 2020 12:42:47 -0700 Message-Id: <20200821194310.3089815-7-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200821194310.3089815-1-keescook@chromium.org> References: <20200821194310.3089815-1-keescook@chromium.org> MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Nick Desaulniers Basically, consider .text.{hot|unlikely|unknown}.* part of .text, too. When compiling with profiling information (collected via PGO instrumentations or AutoFDO sampling), Clang will separate code into .text.hot, .text.unlikely, or .text.unknown sections based on profiling information. After D79600 (clang-11), these sections will have a trailing `.` suffix, ie. .text.hot., .text.unlikely., .text.unknown.. When using -ffunction-sections together with profiling infomation, either explicitly (FGKASLR) or implicitly (LTO), code may be placed in sections following the convention: .text.hot., .text.unlikely., .text.unknown. where , , and are functions. (This produces one section per function; we generally try to merge these all back via linker script so that we don't have 50k sections). For the above cases, we need to teach our linker scripts that such sections might exist and that we'd explicitly like them grouped together, otherwise we can wind up with code outside of the _stext/_etext boundaries that might not be mapped properly for some architectures, resulting in boot failures. If the linker script is not told about possible input sections, then where the section is placed as output is a heuristic-laiden mess that's non-portable between linkers (ie. BFD and LLD), and has resulted in many hard to debug bugs. Kees Cook is working on cleaning this up by adding --orphan-handling=warn linker flag used in ARCH=powerpc to additional architectures. In the case of linker scripts, borrowing from the Zen of Python: explicit is better than implicit. Also, ld.bfd's internal linker script considers .text.hot AND .text.hot.* to be part of .text, as well as .text.unlikely and .text.unlikely.*. I didn't see support for .text.unknown.*, and didn't see Clang producing such code in our kernel builds, but I see code in LLVM that can produce such section names if profiling information is missing. That may point to a larger issue with generating or collecting profiles, but I would much rather be safe and explicit than have to debug yet another issue related to orphan section placement. Reported-by: Jian Cai Suggested-by: Fāng-ruì Sòng Tested-by: Luis Lozano Tested-by: Manoj Gupta Acked-by: Kees Cook Cc: stable@vger.kernel.org Link: https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=add44f8d5c5c05e08b11e033127a744d61c26aee Link: https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=1de778ed23ce7492c523d5850c6c6dbb34152655 Link: https://reviews.llvm.org/D79600 Link: https://bugs.chromium.org/p/chromium/issues/detail?id=1084760 Debugged-by: Luis Lozano Signed-off-by: Nick Desaulniers Signed-off-by: Kees Cook --- include/asm-generic/vmlinux.lds.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) -- 2.25.1 diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index 98d013dcc11a..91dcfb91ac45 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -581,7 +581,10 @@ */ #define TEXT_TEXT \ ALIGN_FUNCTION(); \ - *(.text.hot TEXT_MAIN .text.fixup .text.unlikely) \ + *(.text.hot .text.hot.*) \ + *(TEXT_MAIN .text.fixup) \ + *(.text.unlikely .text.unlikely.*) \ + *(.text.unknown .text.unknown.*) \ NOINSTR_TEXT \ *(.text..refcount) \ *(.ref.text) \