From patchwork Mon May 1 12:38:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 89034 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2665413vqo; Mon, 1 May 2023 05:42:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7qLIrrDWJ0vqXJhVbSWwmWpgFKJpgi+/5Tc1ibEwyV1U1nfT61luvWgdSK4/Sidbqd7uAI X-Received: by 2002:a17:903:22d2:b0:19e:ad18:da5c with SMTP id y18-20020a17090322d200b0019ead18da5cmr17991800plg.37.1682944976926; Mon, 01 May 2023 05:42:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682944976; cv=none; d=google.com; s=arc-20160816; b=iwqn++OeTt+sODJuB1nijDX43ZhPYeNnG38pQZxXWrV6xEe5BqTdvwDSlSrWFfuC8o vSKEhBnm5fzvHCVz9vGFSbnoGLQyY1vc7p3Y1YBuWpGT7+Nic48FSY/ZwDFNtDrNyWIt XTzAKccaGr3WPP389fDhCIj1TYYiQVfwpXfhYsAjQ/73UMVGiIbiubVRwI65KcqzhXcx 6e+ar8jWJ9ZmSUc1WNFgHsO7gcIr8ljjRCDVZO+3xGEhf4UWrsQXkPcHIDempdaA/YWs uo4eQhTdaZQE4axmDOJCpPhp/MG3ou+JcFu8HtwFPRwCZRlyXRpBpMg4Qndz9UjWsFKt LIXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=3VFL3iZxIdOgDdsPxwYNOecpa455WH41HpG7wDJvLnU=; b=Mu7gRjdpcGxv3Czf491oQgR/V1XJ2z13ebVI5Ks2pvus6bQCY+0E1azrD9QOdAD1q9 1g0ZPWT36IbO8EA1JDCZvMmhsCOzHRcOz5/wzteKIQdNcGmbRUijExACybTBY/1o1wf6 r6kcuVN44x7dWGMR2ThwQMOwBJe8gK94gP3y9MsIyQmz4RKSbro7IE94S5nCFpnu9wGS z70X+nc1R6b/+NOu34Phe735PaQkbvU97MRhCkbJVXpSwJ+HqrFk+EShgkg5iEiMmHaF gWjS+WgnUN3JQpiSEJcqNYLVZs1tqkBc9KgqdqPm6IxvsUSn30tYnjUIOJ8NdB+vIiF+ 8HrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=QqU2UNe+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t5-20020a170902e84500b001a4f005d59dsi28381511plg.389.2023.05.01.05.42.44; Mon, 01 May 2023 05:42:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=QqU2UNe+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-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 S232554AbjEAMjE (ORCPT + 99 others); Mon, 1 May 2023 08:39:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232532AbjEAMjA (ORCPT ); Mon, 1 May 2023 08:39:00 -0400 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9724110F5 for ; Mon, 1 May 2023 05:38:58 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-94f7a7a3351so494584866b.2 for ; Mon, 01 May 2023 05:38:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1682944737; x=1685536737; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=3VFL3iZxIdOgDdsPxwYNOecpa455WH41HpG7wDJvLnU=; b=QqU2UNe+paNeYGSdfcTar7S50/+Okp8oJPdt3wPhFzSSiyCOPSSWedHlduNqvaunHM gaKS5N8nixqV0jQFEY4CYw/c39iK+z7F+TpS5E4AOTnMAN0leHAcNRngWaORXlZjRXoF eAXCgvP9a33GSDtlqXwTma3h7mmlo4iNwTji8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682944737; x=1685536737; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3VFL3iZxIdOgDdsPxwYNOecpa455WH41HpG7wDJvLnU=; b=kNEj4jsJlYYY/lJJvA5IGyuXAeRGsmgUVNC6LBzZwOHMk8E1mLs2plJtf83CEXr+DT yNwYk+PPi0QlrQJDTAtr/YaCRS3Aww7WdvFrMOMJmbebK27YSmzEgXHwcMlLLGQJ66pP 1OpF7ZMDAYSk6NQsIH6Ex9JkpOaUyhCWqWilmmpG45pTXL/Ncupg+KWYGxblo8KMXY15 rq5ymDGEtFBAqfaSynl36Rm7sCkbt/nFXEXEr/mTcLrWH3ApbCbGyK+DGI8upm/pQPXY oCGKlMYsSH2O6GWLrAQhKug+nz1nqtL+KY16YLdPwlmvgSTTXeZzwkeg4poDdGzwMD19 QyBw== X-Gm-Message-State: AC+VfDwB0FnE/I9tazni8Ij1AFUewTwMtj0oRYuPHjoPe5b2hRuOqwxy VDXWjgHzQ1S4P7/SgNlQP/tEgA== X-Received: by 2002:a17:907:930a:b0:94f:39b4:e32c with SMTP id bu10-20020a170907930a00b0094f39b4e32cmr13020825ejc.43.1682944737007; Mon, 01 May 2023 05:38:57 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:c573:159e:712e:688b]) by smtp.gmail.com with ESMTPSA id c8-20020aa7c988000000b0050bcbb5708asm146772edt.35.2023.05.01.05.38.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 May 2023 05:38:56 -0700 (PDT) From: Ricardo Ribalda Date: Mon, 01 May 2023 14:38:19 +0200 Subject: [PATCH v6 1/4] kexec: Support purgatories with .text.hot sections MIME-Version: 1.0 Message-Id: <20230321-kexec_clang16-v6-1-a2255e81ab45@chromium.org> References: <20230321-kexec_clang16-v6-0-a2255e81ab45@chromium.org> In-Reply-To: <20230321-kexec_clang16-v6-0-a2255e81ab45@chromium.org> To: Eric Biederman , Philipp Rudo , Dave Young , Andrew Morton , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Nathan Chancellor , Tom Rix , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Baoquan He , Philipp Rudo , kexec@lists.infradead.org, linux-kernel@vger.kernel.org, Ross Zwisler , Steven Rostedt , Simon Horman , Nick Desaulniers , llvm@lists.linux.dev, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, Ricardo Ribalda , stable@vger.kernel.org X-Mailer: b4 0.12.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2611; i=ribalda@chromium.org; h=from:subject:message-id; bh=QL4lqGDXv6U/lMmg6BrW/gmw+SWb8Lpn29mxv2CH5Ys=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkT7LW0lkg4WYXTXiMAsHN2EkcqdL4dtFZm9xSg 6i4AKy1VW+JAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZE+y1gAKCRDRN9E+zzrE iNN+D/wIXMf38vUASc8jnlonv4XZMh98w77gcVeWjzT85pk5hEROQNNJ+sITsbwoQMjqHhTSeQj CiQUS66k93/5RZMhtZ1RL6+gK7uT6m7ke1wNabDo/lQ7SSoDHAcxbG26NAPBuqqPU8b7FtdxYEX oIhYNhCKPrn5RVbk977URc/gDgpB7fpZmVZSAluSx0lXssskFwF0x7RCqcGnT1r0bKrPrKBbw8Z r9Dt2L7kT1GM3yno8Ce2UhEDyf+4EiIqTPuT2YI+3+/ibS2wAW3fhXbNnBvQCGDl8iJF9w4raTn hx6MGu+HCWhUu80lIZmGwc7XortdwMX8SgfnUHHw7p50Rbmj6rTYMJQUY6wx3fzG39SBY6P/kUc 0vb7u/MPeJW+UGs/9XEVz1vRFtj1ibYfEJHlpbrtxY9JyX5iW0Dt0z8whd9wvNV0UM5qmOyUbNt Yt6gVYsK5ZPlgSKN7slxzHGWZe1wrFHDBy7nb1/lm4t3mf0iSGBo8rEFtl13xdZhPzpBZQkpF2N SqyX79/AnDIhq8N69fkU9Ka/PE2VKQq8uejp1B8/la8Xmxbc9Jxx5vDivniFN91grNqRBDCZh9C dmEovSTRvu6E+Ul7b2G+CZQDPWRUmK6Z3uqC0vcpV+doS3cWaQyFwSFpIaR64JkNuBXhIpAkx5n U9gEEpJ/GSMnlvg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764695712176982839?= X-GMAIL-MSGID: =?utf-8?q?1764695712176982839?= Clang16 links the purgatory text in two sections when PGO is in use: [ 1] .text PROGBITS 0000000000000000 00000040 00000000000011a1 0000000000000000 AX 0 0 16 [ 2] .rela.text RELA 0000000000000000 00003498 0000000000000648 0000000000000018 I 24 1 8 ... [17] .text.hot. PROGBITS 0000000000000000 00003220 000000000000020b 0000000000000000 AX 0 0 1 [18] .rela.text.hot. RELA 0000000000000000 00004428 0000000000000078 0000000000000018 I 24 17 8 And both of them have their range [sh_addr ... sh_addr+sh_size] on the area pointed by `e_entry`. This causes that image->start is calculated twice, once for .text and another time for .text.hot. The second calculation leaves image->start in a random location. Because of this, the system crashes immediately after: kexec_core: Starting new kernel Cc: stable@vger.kernel.org Fixes: 930457057abe ("kernel/kexec_file.c: split up __kexec_load_puragory") Reviewed-by: Ross Zwisler Reviewed-by: Steven Rostedt (Google) Reviewed-by: Philipp Rudo Signed-off-by: Ricardo Ribalda --- kernel/kexec_file.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index f989f5f1933b..69ee4a29136f 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -901,10 +901,22 @@ static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi, } offset = ALIGN(offset, align); + + /* + * Check if the segment contains the entry point, if so, + * calculate the value of image->start based on it. + * If the compiler has produced more than one .text section + * (Eg: .text.hot), they are generally after the main .text + * section, and they shall not be used to calculate + * image->start. So do not re-calculate image->start if it + * is not set to the initial value, and warn the user so they + * have a chance to fix their purgatory's linker script. + */ if (sechdrs[i].sh_flags & SHF_EXECINSTR && pi->ehdr->e_entry >= sechdrs[i].sh_addr && pi->ehdr->e_entry < (sechdrs[i].sh_addr - + sechdrs[i].sh_size)) { + + sechdrs[i].sh_size) && + !WARN_ON(kbuf->image->start != pi->ehdr->e_entry)) { kbuf->image->start -= sechdrs[i].sh_addr; kbuf->image->start += kbuf->mem + offset; }