Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1531840rdb; Thu, 7 Dec 2023 01:34:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IG4ibUCnI0+ZuTUrwsV6B6FKi5NJ/QDnqlmBO0gHXntIIklPnoTVrd+k8oEdntH8aA8O69n X-Received: by 2002:a05:6871:2b29:b0:1fa:d948:5992 with SMTP id dr41-20020a0568712b2900b001fad9485992mr2322446oac.54.1701941657805; Thu, 07 Dec 2023 01:34:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701941657; cv=none; d=google.com; s=arc-20160816; b=X5yxB0ERw3OmYJxoz1XbzpgoJ15MN//dHveD1p0+MURoSy7K5S/0Jb0co0cdmhIzyR 2bpMH4DcVNQNuO61McNh8MvT6bD/pMP8ZDZRjPDMbMwm4kHjif96auNSalllCIaLy607 Ri0nU/apNbeUTQey2HjGL+PU0dxy7MItPGbj2pjduYOjmd7vgMv7xJsVzgW/+KrW1EK+ Cf+s/50cDvZnF2l8xoPK7B0LlhOK1lnc7o7kihGuhShRXwx9YrKY60gxdsxYQtyhwGgB tL2ZcMUukKDSGdUFwue1LTclkP9TdtrMXzpdgtdsBJAjqLp/vZkM/ra18LAxdFFuWZ/J f6+g== 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=vFTMUOzkVz1yhy1eVbvlXLTlgFpP3ARXqzrNiOd/DUQ=; fh=31pHNESfIDSRXSirSss2cSA7Q2Y3tA2WaWyHSS2Dgno=; b=EUbSWXcWyvjzFcuvKeexREdGSnXjVOEwGaSFd2TW8558d9nhnWIBNmDjcb3tiqR1b7 n2UjW66hHLRVEPDVEghq6a9/H1yfEgkxPn710I5oZIK2OoqMSxcbzFYAPrKQAs696kKG ueX/Jd3rrsLJesEh/yO3PBcrix2ng8bZ6/4T6zJu5zpYzcAezGPk3QgKx+kA9WoxY1nR e8feUo0B1KQWHY3i3m7BcL4xXmDLmLkXnSkMgeNVzVekiK4xJRoSoltkv0bLyvgrwWwz LxLP1Z71qkSOGoyJ08TiORVrOLyWkPOIqRUmf0a6ttuiRoGlEcgsPsl0MAK7Dl2tuYFt C+ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=BjH07tO6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id 73-20020a63004c000000b005c67bb15866si919225pga.52.2023.12.07.01.34.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 01:34:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=BjH07tO6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 8E6228072A29; Thu, 7 Dec 2023 01:34:16 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378575AbjLGJeB (ORCPT + 99 others); Thu, 7 Dec 2023 04:34:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378491AbjLGJeA (ORCPT ); Thu, 7 Dec 2023 04:34:00 -0500 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C4A8D53; Thu, 7 Dec 2023 01:34:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=vFTMUOzkVz1yhy1eVbvlXLTlgFpP3ARXqzrNiOd/DUQ=; b=BjH07tO6hYuSjTfilfl4n85dJn jwCzc+ni2sBN9SitG9DfFVJjs61YLkfrb8GoQfH8XUGR24A3w3hE+1+4eiXvch0bjuircH2TzwNe3 2wib7xfIbfMu0ZZCat/L7nH7JGRv5WicJZ/YEBVBSbdkmRW3wvF7MqUwoqS1mwMuKRmY9XOrwRUQh gZqD610ihrIYCMrm8rtS55j06Xe9kiv0m2lp+eptll2P0QUCCn78OPoHJlJr2h6EdGWAwM+xhz9Qx BRv9z5wptGCweR1/JNdj+9h0GN55+WMsNwvvADN6sEL9iwgdTFdETyA1h0mlfWIcOHhLOs0Jjfqb4 dnXvWrjA==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1rBAio-005war-2y; Thu, 07 Dec 2023 09:33:07 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id B8E6C300338; Thu, 7 Dec 2023 10:31:05 +0100 (CET) Date: Thu, 7 Dec 2023 10:31:05 +0100 From: Peter Zijlstra To: Alexei Starovoitov Cc: Jiri Olsa , Song Liu , Song Liu , Paul Walmsley , Palmer Dabbelt , Albert Ou , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , X86 ML , "H. Peter Anvin" , "David S. Miller" , David Ahern , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Arnd Bergmann , Sami Tolvanen , Kees Cook , Nathan Chancellor , Nick Desaulniers , linux-riscv , LKML , Network Development , bpf , linux-arch , clang-built-linux , Josh Poimboeuf , Joao Moreira , Mark Rutland Subject: Re: [PATCH v2 2/2] x86/cfi,bpf: Fix BPF JIT call Message-ID: <20231207093105.GA28727@noisy.programming.kicks-ass.net> References: <20231204091334.GM3818@noisy.programming.kicks-ass.net> <20231204111128.GV8262@noisy.programming.kicks-ass.net> <20231204125239.GA1319@noisy.programming.kicks-ass.net> <20231204181614.GA7299@noisy.programming.kicks-ass.net> <20231204183354.GC7299@noisy.programming.kicks-ass.net> <20231206163814.GB36423@noisy.programming.kicks-ass.net> <20231206183713.GA35897@noisy.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 07 Dec 2023 01:34:16 -0800 (PST) On Wed, Dec 06, 2023 at 01:39:43PM -0800, Alexei Starovoitov wrote: > All is ok until kCFI comes into picture. > Here we probably need to teach arch_prepare_bpf_trampoline() to emit > different __kcfi_typeid depending on kernel function proto, > so that caller hash checking logic won't be tripped. > I suspect that requires to reverse engineer an algorithm of computing kcfi from clang. > other ideas? I was going to try and extend bpf_struct_ops with a pointer, this pointer will point to a struct of the right type with all ops filled out as stubs. Then I was going to have bpf_struct_ops_map_update_elem() pass a pointer to the stub op (using moff) into bpf_struct_ops_prepare_trampoline() and eventually arch_prepare_bpf_trampoline(). Additionally I was going to add BPF_TRAMP_F_INDIRECT. Then when F_INDIRECT is set, have it generate the CFI preamble based on the stub passed -- which will have the correct preamble for that method. At least, that's what I'm thinking now, I've yet to try and implement it. > > > The other case: > In the case of bpf_for_each_map_elem() the 'bloom_callback' is a subprog > of bpf_callback_t type. > So the kernel is doing: > ret = callback_fn((u64)(long)map, (u64)(long)&key, > (u64)(long)val, (u64)(long)callback_ctx, 0); > and that works on all archs including 32-bit. > The kernel is doing conversion from native calling convention to bpf calling convention > and for lucky archs like x86-64 the conversion is a true nop. > It's a plain indirect call to JITed bpf prog. > Note there is no interpreter support here. This works on archs with JITs only. > No ftrace and no trampoline. > > This case is easier to make work with kCFI. > The JIT will use: > cfi_bpf_hash: > .long __kcfi_typeid___bpf_prog_runX > like your patch already does. > And will use > extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64); > cfi_bpf_subprog_hash: > .long __kcfi_typeid___bpf_callback_fn > to JIT all subprogs. See bpf_is_subprog(). Aaah!, yes it should be trivial to use another hash value when is_subprog in emit_prologue(). > btw there are two patchsets in progress that will touch core bits of JITs. > This one: > https://patchwork.kernel.org/project/netdevbpf/cover/20231201190654.1233153-1-song@kernel.org/ > and this one: > https://patchwork.kernel.org/project/netdevbpf/cover/20231011152725.95895-1-hffilwlqm@gmail.com/ > > so do you mind resending your current set with get_cfi_offset() change and > I can land it into bpf-next, so we can fix one bug at a time, > build on top, and avoid conflicts? I can do. > The more we dig the more it looks like that the follow up you planned to do > on top of this set isn't going to happen soon. > So should be ok going through bpf-next and then you can follow up with x86 things > after merge window? Yes, we can do that. Plans have changed on my side too -- I'm taking a 6 week break soon, so I'll do whatever I can before I'm out, and then continue from whatever state I find when I get back. Thanks for the details!