Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp568218lqi; Thu, 7 Mar 2024 05:52:47 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX04ifPLVSra0FviFUjRxcLISyXpanh7tZ/Acfyg5cufOtMKgVIBIFbT3Aczv5LIgb+KO5bBuh8Fw5BVV8PwdXHCkBYPpJzPSEqW+5ROw== X-Google-Smtp-Source: AGHT+IGdfEVk/59DS2ivi4+AYZ1RZDX3OJrsYm0c5SowjitFux281nMHyRNSRjwM5QoQjgj1B471 X-Received: by 2002:a05:6214:1847:b0:68f:2d21:d62f with SMTP id d7-20020a056214184700b0068f2d21d62fmr8862297qvy.40.1709819567656; Thu, 07 Mar 2024 05:52:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709819567; cv=pass; d=google.com; s=arc-20160816; b=Zq9dvyQkq+wHz8fEHrZdmtOawaG0bJRBNO+HOVNw0hbDeh9ZXdMrjR7PQfOBxBKUxn JWov5GxiczqYf1II5MqOtusTSKCgC9nwkXiFF45H9VQS2FvEndvTpmgMDiwK+dgJ1Eqx dC97EDS0XsOLXPCr2ErUQLxFXzGQrbn+xjTVSQvh1qjBoDQhF9saM1i0/qCjyu7DSPaY 6M+NZpzTY03PkZUwILX6xpM6U4npTRf/VeavJ3EgMoPAJATk2dQqg9RFS3s+Kc2JM8Ds SOlS+kRJOVWJJWPrhFdIB6L5j8/h1zRgeQFcx7x4UUpSYSYY2wqivV4Hm4DChCvcR8kV /hvA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:cc:references:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=CBriqAM5O3FyFnjXqSk0qP4V/CyxWxE7cjkgEtifenU=; fh=RYkpxGgIczPTBzuTx7bkp0fFFXDfrdCNPQMAOrZsuAU=; b=iTyFDHZgqG+ROXuvzqqnIlZ0iW0bYz6iXF9XBIdbVPUjJkcbWm1VPzlrh3cy1/faHR ZRcmD7noxd0SBDy8w7qqJHDViUufvqtWF5A7v18OO98FidXOnErq9NQ3u+dQGZb1+LRy +mr2czMMdzEZR1WuhaNULfQjfY4gL6EB3TuduBgXY7eihSO49FifnHpODVZ6LdMmURwu +8/vgGLsPvMYb+lQ9lC+aGrkhSwIIplbQqBW3mqJpDBpoly1o0SBMr3B8zs4nsjQRwD5 vi+3GnemOT/5NPjMSPUlp3OKfKNWbmMsciJ3Q9Wa9jYRz/kq2IxnotUGOt/T/7bDgXM8 Hu4g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-95664-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95664-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=QUARANTINE) header.from=huawei.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 3-20020a056214202300b0068f7b29e52csi19386231qvf.364.2024.03.07.05.52.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 05:52:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-95664-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-95664-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95664-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=QUARANTINE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 57F061C237B2 for ; Thu, 7 Mar 2024 13:52:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 749A112D74E; Thu, 7 Mar 2024 13:52:15 +0000 (UTC) Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D581512C7E2; Thu, 7 Mar 2024 13:52:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709819535; cv=none; b=OIlmJGxaK0nruS/MwNVL9mozXtwSRuK0cOJxtQ/fxf6NIBPnry10wzI+HoQEdewkWf1N6VWqurjpywMgq1VJHmHWSwmpPD82ZAgv/CnnesexXmeuDgOo7BAiwwN8fv6pOw1uD2n7P5G8w3gv+U986rhuWOxFhOl1mxE90uura/w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709819535; c=relaxed/simple; bh=KACBD3FSuJOn9a5+HguwCmiT6CB3AHGw3w1coDABmhk=; h=Message-ID:Date:MIME-Version:Subject:To:References:CC:From: In-Reply-To:Content-Type; b=XcmuUsP77kEoqhnIq6z6PQ2EEU3yOxxG2D5+i1Cd4uGah+f72jXr9ze4cFDghk6n5UisCzPGrHL2BFNFoduclj5pibbEfvMYrIKokkVGp0CFausNw64rL11dfQ6VZO7eFR0rTq4TtUyAH7YHxq//ixT/TK2uGJwKD11cZlXZwyY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Tr9fl1hd7zHqfx; Thu, 7 Mar 2024 21:50:23 +0800 (CST) Received: from kwepemd100009.china.huawei.com (unknown [7.221.188.135]) by mail.maildlp.com (Postfix) with ESMTPS id 941CD140134; Thu, 7 Mar 2024 21:52:02 +0800 (CST) Received: from [10.67.109.184] (10.67.109.184) by kwepemd100009.china.huawei.com (7.221.188.135) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Thu, 7 Mar 2024 21:52:01 +0800 Message-ID: Date: Thu, 7 Mar 2024 21:52:01 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH bpf-next] arm64, bpf: Use bpf_prog_pack for arm64 bpf trampoline Content-Language: en-US To: Puranjay Mohan , References: <20240304202803.31400-1-puranjay12@gmail.com> CC: , , , , , , , , , , From: Pu Lehui In-Reply-To: <20240304202803.31400-1-puranjay12@gmail.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemd100009.china.huawei.com (7.221.188.135) On 2024/3/5 4:28, Puranjay Mohan wrote: > We used bpf_prog_pack to aggregate bpf programs into huge page to > relieve the iTLB pressure on the system. This was merged for ARM64[1] > We can apply it to bpf trampoline as well. This would increase the > preformance of fentry and struct_ops programs. > > [1] https://lore.kernel.org/bpf/20240228141824.119877-1-puranjay12@gmail.com/ > > Signed-off-by: Puranjay Mohan > --- > arch/arm64/net/bpf_jit_comp.c | 55 +++++++++++++++++++++++++++++------ > 1 file changed, 46 insertions(+), 9 deletions(-) > > diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c > index 5afc7a525eca..c5b461dda438 100644 > --- a/arch/arm64/net/bpf_jit_comp.c > +++ b/arch/arm64/net/bpf_jit_comp.c > @@ -2076,7 +2076,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im, > /* store return value */ > emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx); > /* reserve a nop for bpf_tramp_image_put */ > - im->ip_after_call = ctx->image + ctx->idx; > + im->ip_after_call = ctx->ro_image + ctx->idx; > emit(A64_NOP, ctx); > } > > @@ -2091,7 +2091,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im, > run_ctx_off, false); > > if (flags & BPF_TRAMP_F_CALL_ORIG) { > - im->ip_epilogue = ctx->image + ctx->idx; > + im->ip_epilogue = ctx->ro_image + ctx->idx; > emit_addr_mov_i64(A64_R(0), (const u64)im, ctx); > emit_call((const u64)__bpf_tramp_exit, ctx); > } > @@ -2124,9 +2124,6 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im, > emit(A64_RET(A64_R(10)), ctx); > } > > - if (ctx->image) > - bpf_flush_icache(ctx->image, ctx->image + ctx->idx); > - > kfree(branches); > > return ctx->idx; > @@ -2169,14 +2166,43 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, > return ret < 0 ? ret : ret * AARCH64_INSN_SIZE; > } > > -int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, > - void *image_end, const struct btf_func_model *m, > +void *arch_alloc_bpf_trampoline(unsigned int size) > +{ > + return bpf_prog_pack_alloc(size, jit_fill_hole); > +} > + > +void arch_free_bpf_trampoline(void *image, unsigned int size) > +{ > + bpf_prog_pack_free(image, size); > +} > + > +void arch_protect_bpf_trampoline(void *image, unsigned int size) > +{ > +} > + > +void arch_unprotect_bpf_trampoline(void *image, unsigned int size) > +{ > +} > + > +int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image, > + void *ro_image_end, const struct btf_func_model *m, > u32 flags, struct bpf_tramp_links *tlinks, > void *func_addr) > { > int ret, nregs; > + void *image, *tmp; > + u32 size = ro_image_end - ro_image; > + > + /* image doesn't need to be in module memory range, so we can > + * use kvmalloc. > + */ > + image = kvmalloc(size, GFP_KERNEL); > + if (!image) > + return -ENOMEM; > + > struct jit_ctx ctx = { > .image = image, > + .ro_image = ro_image, > .idx = 0, > }; > > @@ -2185,15 +2211,26 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, > if (nregs > 8) > return -ENOTSUPP; > > - jit_fill_hole(image, (unsigned int)(image_end - image)); > + jit_fill_hole(image, (unsigned int)(ro_image_end - ro_image)); > ret = prepare_trampoline(&ctx, im, tlinks, func_addr, nregs, flags); > > - if (ret > 0 && validate_code(&ctx) < 0) > + if (ret > 0 && validate_code(&ctx) < 0) { > ret = -EINVAL; > + goto out; > + } > > if (ret > 0) > ret *= AARCH64_INSN_SIZE; > > + tmp = bpf_arch_text_copy(ro_image, image, size); > + if (IS_ERR(tmp)) { > + ret = PTR_ERR(tmp); > + goto out; > + } > + > + bpf_flush_icache(ro_image, ro_image + size); > +out: > + kvfree(image); > return ret; > } > Reviewed-by: Pu Lehui