Received: by 2002:a05:6500:1b41:b0:1fb:d597:ff75 with SMTP id cz1csp340338lqb; Tue, 4 Jun 2024 13:03:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUARoGPtv9DzfvmluGkzP/b47kULhTOhbg45KrT4O32yn3mgunzY6lurwNLSVWUI/0pnD+awbrOYLwM1JZQXcIavupzcU4PgPFqADSTLQ== X-Google-Smtp-Source: AGHT+IFcdl6841ZVFhfLguKJH/t9qHjCzHzvqWGPAtGFjoz3l5aZk1Ud1oceO/JcEbbzEQhYKGRb X-Received: by 2002:a17:902:e5cf:b0:1f6:6cd5:d030 with SMTP id d9443c01a7336-1f6a5905e98mr7322755ad.1.1717531396882; Tue, 04 Jun 2024 13:03:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717531396; cv=pass; d=google.com; s=arc-20160816; b=D8juPJJeK8pcZypsnbyu032osul2iCrAy8aKn6RnQnWm4j62g0wDbWD1VDaq6qcIBd CEDD7gWqvmONQGZZHSsRgLFWpOqg2udZg651Z7bOTBRvlVItcTtmBV0BG5FyGs5HHyTP 91lYTTxwpMT7DkTeLm5EukteQH5jv95W1PvM94Ucv70rHsTTxo/5mMF2E6bhCl8As7DZ 2oNIm2FQ79T8hbgscBTGjBpbE0QVdN2aXDn3S6u1kGnRwMi9OThjlkXHIP+XgcSbyVC5 ptMmcCDcBVtodbVQ/YcQKGeGS3L2NI3DqagSIiwuCOVNYccxHeRMN1SEg1sSF+CRAUH0 zqIw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=oeQnQUYQ70sOSUNAtTc5oL+BskYVVZh34YXLFSpZjws=; fh=u3to46i6vVc15/oqkgIGXd5IRsv1JvYq+fe7qFLhiF8=; b=Baz2KCXJSgLMeCNgTadBFi1/71NKd8fGHCmhGJoY1zYdCG+kLkeTHK+2Hsfq67YVaS dx2EbCPjCYiRWkrCdXb//YDXq8GlJ4Mm906nx9x9gIGqyWRIRs8OElGJRpsFrmfSf/RL PJlU9vnbvHiiD0HguS6c0pOBBpocQdgSgBM5hSxhGQGcncykX08CfUhWr2BIGzFYA1D5 CNInWfYgBoKqAWX9bBlYZKydjjMgpePmM4wIE//YRH0AP1+kwJC3T+L8LOjkRNFQ/3HM sMzhuIZImqaqnpyJFmlkM0SGOonCSlZlybqhWpKtrK+te5GTZDuXpuWQhM5yaoD+aaEd ciSg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FeKh8kvg; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-201315-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201315-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f632403445si16175845ad.479.2024.06.04.13.03.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 13:03:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-201315-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FeKh8kvg; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-201315-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201315-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 80C01285549 for ; Tue, 4 Jun 2024 20:03:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E177514B950; Tue, 4 Jun 2024 20:03:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FeKh8kvg" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 D507114A0AD; Tue, 4 Jun 2024 20:03:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531382; cv=none; b=UgZyr8E28z839uLRXYn3GWSe+sMX4mrI5hwh3ROJH4O+1et/ILm6MV90BqCXWNdSoWQ1wTF1yUAqtsjAtnr8y++ti+9jo2wI0SQAOAvRN1ZHOYGMy/DGvp9SWxy6qfoB4PjEXWFT36UArWusyCRa3hJ01s/Fjl/J+ucFuoPqGOw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531382; c=relaxed/simple; bh=VWxqp+tYVmK0gZXdn9ueD+SAmOvJYQ6Mldxj9MWQ6BM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jvKOojpxzMCVf434ZADVsrUut6DyMxC5DQt5E3QtB7b0+1ZbTv4qutfQ6FeM8Pm7VsXbnTLvSuNY9PF3OI3Xf1INs7mN6fcyE4qO69rPBFgiPGcpUZQbs2YQvXzcpEVgB8D7oJaUu9ALCdST5OgYWwr9FCGCVuSGCZJnJxBZ/NA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FeKh8kvg; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A69A5C2BBFC; Tue, 4 Jun 2024 20:02:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717531381; bh=VWxqp+tYVmK0gZXdn9ueD+SAmOvJYQ6Mldxj9MWQ6BM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FeKh8kvgKafkCCYct4p963mpZBZJN/mWofov6bmRzqlEKsGPdRyDlT+gYY9yGaO94 fP/Hn0eRIGs+p5g0rUbx9U5UC54AF3BJNulRaWY3eQZyXhJ6jEGW4C6LESqos0oI+E 6vYNYnZtrfTGpKg1N5Y6p2Ci0uMTMlH11yDXhCh61hiEbnQv6dULRHAwTD6x/a9HkK WK2Xz9UqDweSA993WD7ug1HX8zuUmk7K9qtgAtgBVn88Vb2Gd9FmhaJss2wj2lT3nA tJz1bx5COay+eYqbCf605I56ZhA9NjaD68u/Rx/qNWxXT2Q3whQMtHVhwZD6UcaqUx gZq9deTDA8H6Q== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC bpf-next 02/10] bpf: Add support for uprobe multi session attach Date: Tue, 4 Jun 2024 22:02:13 +0200 Message-ID: <20240604200221.377848-3-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240604200221.377848-1-jolsa@kernel.org> References: <20240604200221.377848-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Adding support to attach bpf program for entry and return probe of the same function. This is common use case which at the moment requires to create two uprobe multi links. Adding new BPF_TRACE_UPROBE_SESSION attach type that instructs kernel to attach single link program to both entry and exit probe. It's possible to control execution of the bpf program on return probe simply by returning zero or non zero from the entry bpf program execution to execute or not the bpf program on return probe respectively. Signed-off-by: Jiri Olsa --- include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 9 ++++-- kernel/trace/bpf_trace.c | 50 +++++++++++++++++++++++++++++----- tools/include/uapi/linux/bpf.h | 1 + 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 25ea393cf084..b400f50e2c3c 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1116,6 +1116,7 @@ enum bpf_attach_type { BPF_NETKIT_PRIMARY, BPF_NETKIT_PEER, BPF_TRACE_KPROBE_SESSION, + BPF_TRACE_UPROBE_SESSION, __MAX_BPF_ATTACH_TYPE }; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 5070fa20d05c..71d279907a0c 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -4048,10 +4048,14 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI && attach_type != BPF_TRACE_UPROBE_MULTI) return -EINVAL; + if (prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION && + attach_type != BPF_TRACE_UPROBE_SESSION) + return -EINVAL; if (attach_type != BPF_PERF_EVENT && attach_type != BPF_TRACE_KPROBE_MULTI && attach_type != BPF_TRACE_KPROBE_SESSION && - attach_type != BPF_TRACE_UPROBE_MULTI) + attach_type != BPF_TRACE_UPROBE_MULTI && + attach_type != BPF_TRACE_UPROBE_SESSION) return -EINVAL; return 0; case BPF_PROG_TYPE_SCHED_CLS: @@ -5314,7 +5318,8 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr) else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI || attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION) ret = bpf_kprobe_multi_link_attach(attr, prog); - else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI) + else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI || + attr->link_create.attach_type == BPF_TRACE_UPROBE_SESSION) ret = bpf_uprobe_multi_link_attach(attr, prog); break; default: diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index f5154c051d2c..53b111c8e887 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1642,6 +1642,17 @@ static inline bool is_kprobe_session(const struct bpf_prog *prog) return prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION; } +static inline bool is_uprobe_multi(const struct bpf_prog *prog) +{ + return prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI || + prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION; +} + +static inline bool is_uprobe_session(const struct bpf_prog *prog) +{ + return prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION; +} + static const struct bpf_func_proto * kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -1659,13 +1670,13 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_get_func_ip: if (is_kprobe_multi(prog)) return &bpf_get_func_ip_proto_kprobe_multi; - if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI) + if (is_uprobe_multi(prog)) return &bpf_get_func_ip_proto_uprobe_multi; return &bpf_get_func_ip_proto_kprobe; case BPF_FUNC_get_attach_cookie: if (is_kprobe_multi(prog)) return &bpf_get_attach_cookie_proto_kmulti; - if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI) + if (is_uprobe_multi(prog)) return &bpf_get_attach_cookie_proto_umulti; return &bpf_get_attach_cookie_proto_trace; default: @@ -3346,6 +3357,26 @@ uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, s return uprobe_prog_run(uprobe, func, regs); } +static int +uprobe_multi_link_handler_session(struct uprobe_consumer *con, struct pt_regs *regs, + unsigned long *data) +{ + struct bpf_uprobe *uprobe; + + uprobe = container_of(con, struct bpf_uprobe, consumer); + return uprobe_prog_run(uprobe, instruction_pointer(regs), regs); +} + +static int +uprobe_multi_link_ret_handler_session(struct uprobe_consumer *con, unsigned long func, + struct pt_regs *regs, unsigned long *data) +{ + struct bpf_uprobe *uprobe; + + uprobe = container_of(con, struct bpf_uprobe, consumer); + return uprobe_prog_run(uprobe, func, regs); +} + static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx) { struct bpf_uprobe_multi_run_ctx *run_ctx; @@ -3382,7 +3413,7 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr if (sizeof(u64) != sizeof(void *)) return -EOPNOTSUPP; - if (prog->expected_attach_type != BPF_TRACE_UPROBE_MULTI) + if (!is_uprobe_multi(prog)) return -EINVAL; flags = attr->link_create.uprobe_multi.flags; @@ -3460,10 +3491,15 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr uprobes[i].link = link; - if (flags & BPF_F_UPROBE_MULTI_RETURN) - uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler; - else - uprobes[i].consumer.handler = uprobe_multi_link_handler; + if (is_uprobe_session(prog)) { + uprobes[i].consumer.handler_session = uprobe_multi_link_handler_session; + uprobes[i].consumer.ret_handler_session = uprobe_multi_link_ret_handler_session; + } else { + if (flags & BPF_F_UPROBE_MULTI_RETURN) + uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler; + else + uprobes[i].consumer.handler = uprobe_multi_link_handler; + } if (pid) uprobes[i].consumer.filter = uprobe_multi_link_filter; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 25ea393cf084..b400f50e2c3c 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1116,6 +1116,7 @@ enum bpf_attach_type { BPF_NETKIT_PRIMARY, BPF_NETKIT_PEER, BPF_TRACE_KPROBE_SESSION, + BPF_TRACE_UPROBE_SESSION, __MAX_BPF_ATTACH_TYPE }; -- 2.45.1