From patchwork Wed Jul 7 21:47:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12364069 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45CE7C07E95 for ; Wed, 7 Jul 2021 21:48:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 27E1561C3C for ; Wed, 7 Jul 2021 21:48:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232859AbhGGVvA (ORCPT ); Wed, 7 Jul 2021 17:51:00 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:27879 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232774AbhGGVvA (ORCPT ); Wed, 7 Jul 2021 17:51:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625694499; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jm5cd0zOZ7Itm+FjlMPQhsBe4LoJlMQjbASHrTs/ggA=; b=JfPvXLah9ma06orCRN/m6//y9eFiarHSrL/UZbWevl3/+fBORGTRW50wR5rT5ZrZtYzqfk bFWXWAi0dgi+NgBzMbpexx8Tx4XWW61IqBxPhuhsSWFO75TQDgz/6kNSe8KlrBzVTa1j82 0fuCuH1uM32sZwSIK/x5TEemamYeOd0= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-225-q-2uwREzMLqgui41YUFBwg-1; Wed, 07 Jul 2021 17:48:18 -0400 X-MC-Unique: q-2uwREzMLqgui41YUFBwg-1 Received: by mail-wm1-f72.google.com with SMTP id a129-20020a1ce3870000b02901f050bc61d2so1533532wmh.8 for ; Wed, 07 Jul 2021 14:48:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Jm5cd0zOZ7Itm+FjlMPQhsBe4LoJlMQjbASHrTs/ggA=; b=JKx0zhrdX5blIT4frWEHDjPyRHxqIQcutFjEfkktKgwh8bTZLJJgWpPBHQJ0GVK2KW mNbqpGbFUyna5gaMM1gccEy0Xo7XwkZ7oWhZrW+Bil2IwkrmUGIAUV8KQEFsbn6Var0i UnmaHkEE7UHascZTcX/5PTfVI5S1TZUX9JKJVVVoetbPFvbJpBi5IVKs0CDZ8ZcTEr+U 0/hLPtmLZCpRzi2kBZq0hK2AydweaqMgV6vd8nMDqwE0lBXX/6Ma4VXPJ4MpQsri0FTa 34ItRejOdA6rxIyhB1yu0ycBEjcnU271nb9gbKSYdcgEi4C0KU7gbumwUK2wemq4rZ7P BSBw== X-Gm-Message-State: AOAM531CVYHjzxEEDr8lAaB8yPq8DpaNa+3cXLnJuW9p3LQ5gwrscfmX DCYAhK7tTIWkJZMcHl6d+6wcdw5sWFmBsHTFZwxBFitdd5s5hoNCdQlEey+sYl/rOTHAtxC4nLv WdNXNk28Bdmb/ X-Received: by 2002:a05:600c:4f15:: with SMTP id l21mr28700399wmq.72.1625694497181; Wed, 07 Jul 2021 14:48:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxM7lGZp/iD4L9G5xqaYUuQfm/4DcRAzM24ezG7RuatVD8l4V3ER8Jf4oCp6GSduKGjyxhVSA== X-Received: by 2002:a05:600c:4f15:: with SMTP id l21mr28700380wmq.72.1625694497005; Wed, 07 Jul 2021 14:48:17 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id u15sm7265281wmq.1.2021.07.07.14.48.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 14:48:16 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Masami Hiramatsu , Alan Maguire Subject: [PATCHv3 bpf-next 2/7] bpf: Enable BPF_TRAMP_F_IP_ARG for trampolines with call_get_func_ip Date: Wed, 7 Jul 2021 23:47:46 +0200 Message-Id: <20210707214751.159713-3-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707214751.159713-1-jolsa@kernel.org> References: <20210707214751.159713-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Enabling BPF_TRAMP_F_IP_ARG for trampolines that actually need it. The BPF_TRAMP_F_IP_ARG adds extra 3 instructions to trampoline code and is used only by programs with bpf_get_func_ip helper, which is added in following patch and sets call_get_func_ip bit. This patch ensures that BPF_TRAMP_F_IP_ARG flag is used only for trampolines that have programs with call_get_func_ip set. Signed-off-by: Jiri Olsa --- include/linux/filter.h | 3 ++- kernel/bpf/trampoline.c | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index 472f97074da0..ba36989f711a 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -559,7 +559,8 @@ struct bpf_prog { kprobe_override:1, /* Do we override a kprobe? */ has_callchain_buf:1, /* callchain buffer allocated? */ enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */ - call_get_stack:1; /* Do we call bpf_get_stack() or bpf_get_stackid() */ + call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */ + call_get_func_ip:1; /* Do we call get_func_ip() */ enum bpf_prog_type type; /* Type of BPF program */ enum bpf_attach_type expected_attach_type; /* For some prog types */ u32 len; /* Number of filter blocks */ diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 28a3630c48ee..b2535acfe9db 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -172,7 +172,7 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr) } static struct bpf_tramp_progs * -bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total) +bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg) { const struct bpf_prog_aux *aux; struct bpf_tramp_progs *tprogs; @@ -189,8 +189,10 @@ bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total) *total += tr->progs_cnt[kind]; progs = tprogs[kind].progs; - hlist_for_each_entry(aux, &tr->progs_hlist[kind], tramp_hlist) + hlist_for_each_entry(aux, &tr->progs_hlist[kind], tramp_hlist) { + *ip_arg |= aux->prog->call_get_func_ip; *progs++ = aux->prog; + } } return tprogs; } @@ -333,9 +335,10 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) struct bpf_tramp_image *im; struct bpf_tramp_progs *tprogs; u32 flags = BPF_TRAMP_F_RESTORE_REGS; + bool ip_arg = false; int err, total; - tprogs = bpf_trampoline_get_progs(tr, &total); + tprogs = bpf_trampoline_get_progs(tr, &total, &ip_arg); if (IS_ERR(tprogs)) return PTR_ERR(tprogs); @@ -357,6 +360,9 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) tprogs[BPF_TRAMP_MODIFY_RETURN].nr_progs) flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME; + if (ip_arg) + flags |= BPF_TRAMP_F_IP_ARG; + err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE, &tr->func.model, flags, tprogs, tr->func.addr);