From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2800C4332F for ; Tue, 20 Dec 2022 02:32:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232392AbiLTCcj (ORCPT ); Mon, 19 Dec 2022 21:32:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229492AbiLTCch (ORCPT ); Mon, 19 Dec 2022 21:32:37 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9458DE0BA; Mon, 19 Dec 2022 18:32:35 -0800 (PST) Received: from kwepemi500013.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NbgYx6xz5zJqV7; Tue, 20 Dec 2022 10:31:33 +0800 (CST) Received: from [10.67.111.192] (10.67.111.192) by kwepemi500013.china.huawei.com (7.221.188.120) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Tue, 20 Dec 2022 10:32:32 +0800 Message-ID: Date: Tue, 20 Dec 2022 10:32:31 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [RFC PATCH RESEND bpf-next 1/4] bpf: Rollback to text_poke when arch not supported ftrace direct call Content-Language: en-US To: Pu Lehui , , , CC: =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Luke Nelson , Xi Wang , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Paul Walmsley , Palmer Dabbelt , Albert Ou , Pu Lehui References: <20221220021319.1655871-1-pulehui@huaweicloud.com> <20221220021319.1655871-2-pulehui@huaweicloud.com> From: Xu Kuohai In-Reply-To: <20221220021319.1655871-2-pulehui@huaweicloud.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.111.192] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemi500013.china.huawei.com (7.221.188.120) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 12/20/2022 10:13 AM, Pu Lehui wrote: > From: Pu Lehui > > The current bpf trampoline attach to kernel functions via ftrace direct > call API, while text_poke is applied for bpf2bpf attach and tail call > optimization. For architectures that do not support ftrace direct call, > text_poke is still able to attach bpf trampoline to kernel functions. > Let's relax it by rollback to text_poke when architecture not supported. > > Signed-off-by: Pu Lehui > --- > kernel/bpf/trampoline.c | 8 ++------ > 1 file changed, 2 insertions(+), 6 deletions(-) > > diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c > index d6395215b849..386197a7952c 100644 > --- a/kernel/bpf/trampoline.c > +++ b/kernel/bpf/trampoline.c > @@ -228,15 +228,11 @@ static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_ad > static int register_fentry(struct bpf_trampoline *tr, void *new_addr) > { > void *ip = tr->func.addr; > - unsigned long faddr; > int ret; > > - faddr = ftrace_location((unsigned long)ip); > - if (faddr) { > - if (!tr->fops) > - return -ENOTSUPP; > + if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) && > + !!ftrace_location((unsigned long)ip)) > tr->func.ftrace_managed = true; > - } > After this patch, a kernel function with true trace_location will be patched by bpf when CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS is disabled, which means that a kernel function may be patched by both bpf and ftrace in a mutually unaware way. This will cause ftrace and bpf_arch_text_poke to fail in a somewhat random way if the function to be patched was already patched by the other. > if (bpf_trampoline_module_get(tr)) > return -ENOENT;