From patchwork Fri Sep 20 15:30:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tao Chen X-Patchwork-Id: 13808571 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F166910E5; Fri, 20 Sep 2024 15:31:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726846272; cv=none; b=NMxuFssxSL0N9EU8erkW27u48qi99mjlvyTXDt5Sem2KqgRB2dPeM/RgrNEY48/HJ1y09tnkJ3zH0CIYWR5mJADJafovLqklldb+hj8lF0ndqpXx518KFDszVPWP81S1JkLNJRQDGPHFEBgEu5XSe2cH9ahFqc38R4QsgGueRlc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726846272; c=relaxed/simple; bh=IhqzmrCAVaLbNPfFQSlXvHcZL1XBwRYgh7yR40B9I6M=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=Kn2MRofwn5JWS7hONRJsof1r1mfMNYj1Kdofs/RXMDC1TQ7FxkeJxBIqZdyhVd7HymJ0Obtvr4rAwPiy8eoG5pptzaLQ4RonI96ep7/ehR+3H8UrOXJDQ+xVl9mVeKB90mVca+w/E+N13nQc25mJ3Y0yycgLztnt9rAHiAnLy+g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Gg7gzb07; arc=none smtp.client-ip=209.85.215.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Gg7gzb07" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-656d8b346d2so1446171a12.2; Fri, 20 Sep 2024 08:31:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726846270; x=1727451070; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=zc/acfT9kdgzhmkJk3mvwRHkKN76tDWkgF+Kd466Ixk=; b=Gg7gzb07hqogBJITJ/EqAlDAEl2lwRoghAcfA0cqvr+9CvU0O6NOQPVnUHKPNrscyq UV6+ij8jLcNn/TnZLq67GPzYMQ+irx86CVRYkd9qPeEBOnrUxCNBhzsjT050gZemPJm8 UeZhFtzZIZj+430QfwHUT+dxZCjlqgLw/92uLdKX2tx6c/JTZtLIwb1LWKG+BgPObZra d8jRoZBcXG3WjfW1cZF3AZY1vsT/dV2zL/rReDrhdptLagfdt0bbIQtpicV6FblAt4A8 U0Kd0eLOKAAMZjMRE+/HZ4weK2+lmY8Q5aLwJVULG8BFoNS++athrARAzvgcJ8kGqXSA 8ZaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726846270; x=1727451070; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zc/acfT9kdgzhmkJk3mvwRHkKN76tDWkgF+Kd466Ixk=; b=GGVQbswKHIdi7It7O1wBhTDJ/6nbKJM4JZyzLBrk9nWdQq9YDdq1owhPD+F/6mlcby dgaAgSsmfH0/I5/F2fggg1WNag9tnKQ8ZDUSwpuq6UT9gYn1efsnDl1iJMe7j/OlGe9G 6OZR3WrbczyphWzUnMByVd1B6i9/ZZn4wKwjxl1cFT04IElvxqnnV+iieS4u89UqJBvc /AwuXNmRF/XFJyzAX/dMcbIqho3tPzYriRoUPDOlTm64beayrxgNNxxRWXAS9zcG+JME 51gQNnkngXjRCCVnsFLPI69cwHfEN333zlvLUNvyJ3Nh3pODo/d3LC6CtnTAaLlcLczJ If4Q== X-Forwarded-Encrypted: i=1; AJvYcCWbkA57/7n6UPEq68VLCqG7Q++sRUBwCo7Do8+/uawJL8ZVcXHcxP2rmSoTlOi+y4ol9LT2aDVIfvTj6Js=@vger.kernel.org X-Gm-Message-State: AOJu0YyISltVExlyHMMtSf7CnbZcUH0tn1EeLMt/LXQqqjPF/1adOriK pJlfm/s/+XylkpLvT4YLi0XiMzviFmkuiKMKjrg/yG9uh2QCesJs X-Google-Smtp-Source: AGHT+IHjpe779ClnZOTU5SO9V278H2pLnOi7zxNeBp9yEXdP658YfTZE9CXq4DHWdyh8M+/yj1coNQ== X-Received: by 2002:a05:6a21:1690:b0:1cf:6c64:f924 with SMTP id adf61e73a8af0-1d30a9fb5aemr5328127637.38.1726846269968; Fri, 20 Sep 2024 08:31:09 -0700 (PDT) Received: from localhost ([116.198.225.81]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7db498f9eb2sm9543956a12.23.2024.09.20.08.31.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Sep 2024 08:31:09 -0700 (PDT) From: Tao Chen To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , Jiri Olsa Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Tao Chen Subject: [PATCH bpf-next 1/2] bpf: Simplify code with BPF_CALL_IMM Date: Fri, 20 Sep 2024 23:30:50 +0800 Message-Id: <20240920153050.918697-1-chen.dylane@gmail.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net No logic changed, simplify code with BPF_CALL_IMM. Signed-off-by: Tao Chen --- kernel/bpf/verifier.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 53d0556fbbf3..26fe57c79f93 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -21157,7 +21157,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env) func_id_name(insn->imm), insn->imm); return -EFAULT; } - insn->imm = fn->func - __bpf_call_base; + insn->imm = BPF_CALL_IMM(fn->func); next_insn: if (subprogs[cur_subprog + 1].start == i + delta + 1) { subprogs[cur_subprog].stack_depth += stack_depth_extra; From patchwork Fri Sep 20 15:37:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tao Chen X-Patchwork-Id: 13808573 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1D691F4ED; Fri, 20 Sep 2024 15:37:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726846663; cv=none; b=eGuAfpk2XxhGAvYRHR5cbxi8xXz9mRWw13ghADwmjDJgOwKl/dpt46qyqarIs27kODIWFhITZp4Fr5w7IzeqALSp+M6akpCoAC8OTuFojzNtWtixafs2wZzGZQYrhZJlu3x1ob73OobTP0YHuEO0ypmpCBlCR3e7GCY8BIEfDFo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726846663; c=relaxed/simple; bh=IBNi5+etZxBxaJtA4MvHJd8EcTOHUlXwvzlgxQMIVZo=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=nMEsYXyfH71aVKAKUYPQ/qKelCMfkMHiuDwpdSupRLabXmLoJC1j8gUhtI+8Z7Obbg2oMJ2HpLCCTGxoVnbSFZJGGpDflxenBt1a8iIoz/mmH0QtuzjxIVXrZyDvYjSNZWZ88AH3zHqvniOEPvMDFMvWvc2E80i7fNCMzZpiFQU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nmf7IF0k; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nmf7IF0k" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2053525bd90so22108685ad.0; Fri, 20 Sep 2024 08:37:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726846661; x=1727451461; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=vZPEWZoZN6gyioYEDJsWWtC09rzdzpFL6+7dviCo3C8=; b=nmf7IF0kWlXHbCbay3kjoBkYO18pg0nz0C2Ta62e6yDxF7lm3IwU7paR1gdBGC8tBd ckGk1sGiqNlSFpmNMH9hWUuepTtRvQcmHQ7bxFzhJ6rTNGfg0OewXID0QZXf5fW/ozsd R8TAemCwUb5NGeRE+UythzrBp3QudvSWxPW3bw8TzkphOs8e1lauJEIuKgn4QKmZv4ln CVfZljwpp9XDRgB/8E3TnZpvp/HvFWSsFCb23ok+6eXs+IdqUCqQpaT1lyZ0mm0Lhyff oRDX/K0uVuw6n1vMXiYjOKyoAvTPhSS9Jule1Q1pnL2Ly3kwdGtKovXs8L5MWqJUxKT3 PP3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726846661; x=1727451461; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vZPEWZoZN6gyioYEDJsWWtC09rzdzpFL6+7dviCo3C8=; b=gi1tFA7SzO3F/6m/BxqB5zNfq0Hb3x4MhHEEjSNAdBH1cxjOAT0XXWXdl1jDalLS0C AXhjyP2GouGe5+Uje0l2t44VGjg9RDY3ukhqTE8nk4wykDlLQGk+F1RiRK27F7zJ5pxL 0gGIw0qtRxUFKJ1WLoLhu8ze/lpGHE44CeTnEAASy+yt5p7Pjsn7c3bJXvIWL6UNpsMh WBWdM0QWyizPOlZlYBOfjzKkuNxvgOMBufssTWXyRww71krqQIHHT82qDyBrz48U5bky nQUKO3Dv7lJdFj8692M6hNXFxBYuCJMSIQAGkWp90hjzsIVH5f8p//ec0GPptccSCVmD g2+g== X-Forwarded-Encrypted: i=1; AJvYcCUad4OiGRFbBbYs2tFyOtC3L0KZ08TqyNkwUOiaGFODC4TRXJo+nOgK1vh5fFyHv9CyHraDlhTaPLyq+9E=@vger.kernel.org, AJvYcCV3/hz7Vyzj75xuQZoQbZo4ZP3pyO10fA7vPNvp1fLv2lWBva1z5+T7w/+VLHB05bOsu0PELmWmZitMxA==@vger.kernel.org X-Gm-Message-State: AOJu0Yy154+7n5Agnf5HblGja1bW8oUvpdYUcI7Ubo3ADq+w+7SpUAnb H2Bt1ioKnX/PeYZeZkgmd0M/Epxkwm1RYQy+zH8iR8V6WJl6x9WJ X-Google-Smtp-Source: AGHT+IGPosCOh7nMMAoZzPbV+Z8FF/maBKEqdN9fsZINhjrwcKMd3EXETF6KUBqarQWAqD8AXU9C9A== X-Received: by 2002:a17:903:2c8:b0:207:625:cf04 with SMTP id d9443c01a7336-208d8565119mr52721765ad.52.1726846661158; Fri, 20 Sep 2024 08:37:41 -0700 (PDT) Received: from localhost ([116.198.225.81]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-207945da8a3sm95990985ad.56.2024.09.20.08.37.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Sep 2024 08:37:40 -0700 (PDT) From: Tao Chen To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , Jiri Olsa , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, sparclinux@vger.kernel.org, Tao Chen Subject: [PATCH bpf-next 2/2] bpf: Add BPF_CALL_FUNC* to simplify code Date: Fri, 20 Sep 2024 23:37:06 +0800 Message-Id: <20240920153706.919154-1-chen.dylane@gmail.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net No logic changed, like macro BPF_CALL_IMM, add BPF_CALL_FUNC/_FUNC_ARGS to simplify code. Signed-off-by: Tao Chen --- arch/sparc/net/bpf_jit_comp_64.c | 2 +- arch/x86/net/bpf_jit_comp.c | 2 +- arch/x86/net/bpf_jit_comp32.c | 5 ++--- include/linux/filter.h | 4 ++++ kernel/bpf/core.c | 6 +++--- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/arch/sparc/net/bpf_jit_comp_64.c b/arch/sparc/net/bpf_jit_comp_64.c index 73bf0aea8baf..076b1f216360 100644 --- a/arch/sparc/net/bpf_jit_comp_64.c +++ b/arch/sparc/net/bpf_jit_comp_64.c @@ -1213,7 +1213,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx) /* function call */ case BPF_JMP | BPF_CALL: { - u8 *func = ((u8 *)__bpf_call_base) + imm; + u8 *func = BPF_CALL_FUNC(imm); ctx->saw_call = true; diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 06b080b61aa5..052e5cc65fc0 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -2126,7 +2126,7 @@ st: if (is_imm8(insn->off)) case BPF_JMP | BPF_CALL: { u8 *ip = image + addrs[i - 1]; - func = (u8 *) __bpf_call_base + imm32; + func = BPF_CALL_FUNC(imm32); if (tail_call_reachable) { LOAD_TAIL_CALL_CNT_PTR(bpf_prog->aux->stack_depth); ip += 7; diff --git a/arch/x86/net/bpf_jit_comp32.c b/arch/x86/net/bpf_jit_comp32.c index de0f9e5f9f73..f7277639bd2c 100644 --- a/arch/x86/net/bpf_jit_comp32.c +++ b/arch/x86/net/bpf_jit_comp32.c @@ -1627,8 +1627,7 @@ static int emit_kfunc_call(const struct bpf_prog *bpf_prog, u8 *end_addr, /* mov dword ptr [ebp+off],eax */ if (fm->ret_size) end_addr -= 3; - - jmp_offset = (u8 *)__bpf_call_base + insn->imm - end_addr; + jmp_offset = BPF_CALL_FUNC(insn->imm) - end_addr; if (!is_simm32(jmp_offset)) { pr_err("unsupported BPF kernel function jmp_offset:%lld\n", jmp_offset); @@ -2103,7 +2102,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, break; } - func = (u8 *) __bpf_call_base + imm32; + func = BPF_CALL_FUNC(imm32); jmp_offset = func - (image + addrs[i]); if (!imm32 || !is_simm32(jmp_offset)) { diff --git a/include/linux/filter.h b/include/linux/filter.h index 99b6fc83825b..d06526decc6d 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -461,6 +461,10 @@ static inline bool insn_is_cast_user(const struct bpf_insn *insn) #define BPF_CALL_IMM(x) ((void *)(x) - (void *)__bpf_call_base) +#define BPF_CALL_FUNC(x) ((x) + (u8 *)__bpf_call_base) + +#define BPF_CALL_FUNC_ARGS(x) ((x) + (u8 *)__bpf_call_base_args) + #define BPF_EMIT_CALL(FUNC) \ ((struct bpf_insn) { \ .code = BPF_JMP | BPF_CALL, \ diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 4e07cc057d6f..f965f0d586f3 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -1278,7 +1278,7 @@ int bpf_jit_get_func_addr(const struct bpf_prog *prog, * and the helper with imm relative to it are both in core * kernel. */ - addr = (u8 *)__bpf_call_base + imm; + addr = BPF_CALL_FUNC(imm); } *func_addr = (unsigned long)addr; @@ -2007,12 +2007,12 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn) * preserves BPF_R6-BPF_R9, and stores return value * into BPF_R0. */ - BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3, + BPF_R0 = BPF_CALL_FUNC(insn->imm)(BPF_R1, BPF_R2, BPF_R3, BPF_R4, BPF_R5); CONT; JMP_CALL_ARGS: - BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2, + BPF_R0 = BPF_CALL_FUNC_ARGS(insn->imm)(BPF_R1, BPF_R2, BPF_R3, BPF_R4, BPF_R5, insn + insn->off + 1);