Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp29145549rwd; Wed, 5 Jul 2023 07:50:09 -0700 (PDT) X-Google-Smtp-Source: APBJJlGa9Al/HVVdoxbkq4ZQD7gjPgHorM8mtbPMVs1bHzOv2o5H3zVaLFDMW1F6LvcIMVGbNARI X-Received: by 2002:a05:6a00:158d:b0:668:7209:1856 with SMTP id u13-20020a056a00158d00b0066872091856mr17519695pfk.14.1688568609692; Wed, 05 Jul 2023 07:50:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688568609; cv=none; d=google.com; s=arc-20160816; b=ZX24O+pFMXmTAomonOFt97edZz1amvjUIGbfxl6X0aLqu2WWxuE7vVnaMxSnNww9vt iadUepHhG1DR1YSWn+fe4I5T+m3XdeJpitIDQTsWS+fCYqpaxXHpZfDJJDXDv2q1jkfV iuQjQz6eC56DVyUBvpGERZitqfZZV8NvkMls9t00uDbvKuO++y29eRxKUNsOHpsYka7Y d/8iwvQXoYkUQTD67xrA8En06W+Yp1QxnCXFx5jb6EDyPKN9snTh1ahpZ0YWdQAybsTN xWEulFzIxMkcD0oWc/dpB/p+C8WwpXNWUovdzsW7WIuNktIsGagX9U5RLgyJB81blJJ4 1qvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=F1yGZoFLsAuYu37YZd+AWhdNFzTWX+6vDtIfgohqhNk=; fh=G18YXOwHH9BcJNpdOtlXI1g8nrAmHgb3mO8bjovQP4Q=; b=aamYHPGLmxOQRkSjfXL2QLtbtMwE1oUSXEAnC3ZOtzzweNLyLF4N2Nnhe+89rk0H06 ebP6gCipOkQ7+PcP7fpK7pmGVOJJLjKkBLVhSlFR6CktZlxcHKPozTqec4ub/N4KXs54 O9dizvIOzihRPXZ1aIOf+Mj7hbAU/fF280jWXUtn4dQLJ2eQK+JOQNIBlReeeK1z7q/9 NAp3pHaS3wWUaelczHf6bffhiNwjBzdv7vT4ynKJsYQNAZECbj22fP4xN2bS6Ib4GGif iHUq43c+rIIFNAm5W6AtX+dTcUk/yR2FtGDTfs+4hg0AS8odfVg1kwTGt/Y/FC3q971E cugg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FUvE4++p; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ea24-20020a056a004c1800b0063b7c4435c0si14631467pfb.54.2023.07.05.07.49.57; Wed, 05 Jul 2023 07:50:09 -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=@kernel.org header.s=k20201202 header.b=FUvE4++p; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232529AbjGEOkl (ORCPT + 99 others); Wed, 5 Jul 2023 10:40:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231565AbjGEOkk (ORCPT ); Wed, 5 Jul 2023 10:40:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30607CE; Wed, 5 Jul 2023 07:40:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B9A08615CB; Wed, 5 Jul 2023 14:40:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5379EC433C8; Wed, 5 Jul 2023 14:40:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1688568038; bh=8lOxVmG05F0ecJWHeVz8zcLl0eVMk/+f7NxbVDTmM5E=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=FUvE4++pU/R3wqyF2xYjVo21LAIkMLfnSeYF3fzPIqd9pQo1PnN6+mgRy9U39+dWh c0NLq3ey/HlAB/j6u1cqiBFE8i2LyGrg2agO8Wq0HA9e5Fxq+kcnw1r9xbBmHa3xWV TbBrmIncTK6ZtVYNoz4n10xWVrtnl/vJ8acDDhzTaNAdIoQ7ur1HIVKRy/V3mlNWDg k/x8W/mmF7P71l1ssy9Ke+Edzn8U/R3zCOZCY+RG7qyphxzPsERdiS8N3BjvuqGZwm IbWWypy7rLDS6mAi1w93mZw9LW4iuxNzXYNx4OYdWSay+nytXhq5ARXmYsIW4JyUeT iizD+X2Znk4/w== Date: Wed, 5 Jul 2023 23:40:32 +0900 From: Masami Hiramatsu (Google) To: Petr Pavlu Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, peterz@infradead.org, samitolvanen@google.com, x86@kernel.org, linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/2] x86/retpoline,kprobes: Avoid treating rethunk as an indirect jump Message-Id: <20230705234032.eb0758a3c3fa412169862fab@kernel.org> In-Reply-To: <20230705081547.25130-3-petr.pavlu@suse.com> References: <20230705081547.25130-1-petr.pavlu@suse.com> <20230705081547.25130-3-petr.pavlu@suse.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_MED,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 On Wed, 5 Jul 2023 10:15:47 +0200 Petr Pavlu wrote: > Functions can_optimize() and insn_is_indirect_jump() consider jumps to > the range [__indirect_thunk_start, __indirect_thunk_end] as indirect > jumps and prevent use of optprobes in functions containing them. > > Linker script arch/x86/kernel/vmlinux.lds.S places into this range also > the special section .text.__x86.return_thunk which contains the return > thunk. It causes that machines which use the return thunk as > a mitigation and don't have it patched by any alternative then end up > not being able to use optprobes in any regular function. Ah, I got it. So with retpoline, the 'ret' instruction is replaced by 'jmp __x86_return_thunk' and the "__x86_return_thunk" is also listed in the __indirect_thunk_start/end. Good catch! And I think Peter's suggestion is simpler and easier to understand. Can you update this? Thank you, > > The return thunk doesn't need to be treated as an indirect jump from the > perspective of insn_is_indirect_jump(). It returns to a caller and > cannot land into an optprobe jump operand which is the purpose of the > insn_is_indirect_jump() check. > > Fix the problem by defining the symbols __indirect_thunk_start and > __indirect_thunk_end directly in arch/x86/lib/retpoline.S. This is > possible because commit 9bc0bb50727c ("objtool/x86: Rewrite retpoline > thunk calls") made all indirect thunks present in a single section. > > Fixes: 0b53c374b9ef ("x86/retpoline: Use -mfunction-return") > Signed-off-by: Petr Pavlu > --- > arch/x86/kernel/vmlinux.lds.S | 2 -- > arch/x86/lib/retpoline.S | 4 ++++ > 2 files changed, 4 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S > index a4cd04c458df..dd5b0a68cf84 100644 > --- a/arch/x86/kernel/vmlinux.lds.S > +++ b/arch/x86/kernel/vmlinux.lds.S > @@ -133,9 +133,7 @@ SECTIONS > KPROBES_TEXT > SOFTIRQENTRY_TEXT > #ifdef CONFIG_RETPOLINE > - __indirect_thunk_start = .; > *(.text..__x86.*) > - __indirect_thunk_end = .; > #endif > STATIC_CALL_TEXT > > diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S > index 3bea96341d00..f45a3e7f776f 100644 > --- a/arch/x86/lib/retpoline.S > +++ b/arch/x86/lib/retpoline.S > @@ -14,6 +14,7 @@ > > .section .text..__x86.indirect_thunk > > +SYM_ENTRY(__indirect_thunk_start, SYM_L_GLOBAL, SYM_A_NONE) > > .macro POLINE reg > ANNOTATE_INTRA_FUNCTION_CALL > @@ -125,6 +126,9 @@ SYM_CODE_END(__x86_indirect_jump_thunk_array) > #include > #undef GEN > #endif > + > +SYM_ENTRY(__indirect_thunk_end, SYM_L_GLOBAL, SYM_A_NONE) > + > /* > * This function name is magical and is used by -mfunction-return=thunk-extern > * for the compiler to generate JMPs to it. > -- > 2.35.3 > -- Masami Hiramatsu (Google)