Received: by 2002:ab2:7407:0:b0:1f4:b336:87c4 with SMTP id e7csp170834lqn; Thu, 11 Apr 2024 18:51:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUCc13FU3aBS3/iZdElOsQAW+ZPoEU8k81GdmIzNwzNiDVOu6YVC/+cRPSybWQJUSYVkklkTkszG0ooC9d3JfsnCsY/vMGYvMRP9BfmFg== X-Google-Smtp-Source: AGHT+IEmaIcpqRRSl7m6EYBz9x1QgcNe/7DWhW+mCbV6WcdOQQLSv8s5SJluUayBBjUybfQop591 X-Received: by 2002:a17:902:e852:b0:1e2:58b7:2dd6 with SMTP id t18-20020a170902e85200b001e258b72dd6mr6028077plg.21.1712886662857; Thu, 11 Apr 2024 18:51:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712886662; cv=pass; d=google.com; s=arc-20160816; b=xcm/7q0lpgaOnTiBTSohJbPq7JYXAK2bKvJJI528BuCTyWqfGorNDVr+WWVH0hwswn WVEzPPrm4bnO8vznvsbqGIb9AgB0AGR/qKFzaQdAenzCCIJ4CM9TDW8uvqUlmgU47fy5 RacbUWj06HZR5lNtQ3rjPKVkxpcWeL55VhYO30zMe3rdlKZTKu7AL2bmBTTK2Ouchkfj G5gJJmaht2y/svgNWDbpFqs2Qov6EfVXlJb4M6Lljt+jDZwuhivYCUIGR2SMUQVhYTbx tcg1E01UjtKX3EyMQoyBDzr12vig+787rm3xJjZvnVDeKcGpbBdI6ISk9r5RNm4XSpwY wYyw== 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=2I8ioTIE3St0NCO304/67c5ZZcTg34djE+ltGa2DGgk=; fh=SX+B5yoOmP8ecXjV+l9Pg6FDsXeU0DB0M3jslMPVE2E=; b=m0eJKWT0rRhH9yPPX8TG4ax/yGQbF1QAL3WAlhPiRYcq+y/Lnpd/FOuGvDHJoNKQh3 Ky/ubBQ2Mckp8ecCXOlfa4cb+60/fUTpiAQPicenK5rRzy+kWK7+egrMYyonjOFfHVyy 3NxRIN2hoCk6huk4vcuE3qdlq79UTzMP+iyLG5j68uaW2YJobQLIG8CJU3h9KvL/f6D2 EC4x+rz9OGF/3FsnnntLNj93Fle8/bbKh1G2cSJWWNd74z55U0XjR75xmUyaqV1Q9XmT ZViXSUSjdPd+LjiHOIdbpFw7wvJKLm/4M3RJQ7WSZWlEhoRoJ8TyzwpGWNqgNBU/LFEk pyEA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=FQ3BuQZb; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-141781-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141781-linux.lists.archive=gmail.com@vger.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 q5-20020a170902f78500b001e445b8108csi2310753pln.391.2024.04.11.18.51.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 18:51:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-141781-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=@kylehuey.com header.s=google header.b=FQ3BuQZb; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-141781-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141781-linux.lists.archive=gmail.com@vger.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 57EEA2833D2 for ; Fri, 12 Apr 2024 01:51:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AA28DC152; Fri, 12 Apr 2024 01:50:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="FQ3BuQZb" Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) (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 342DBF9EC for ; Fri, 12 Apr 2024 01:50:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886648; cv=none; b=EZX8h5ohTrAaAmYxQ41hWvZMCpSQxDsQHjmQYEQr8wAn1O/bqM6YWIk1yGJx7Fjb1DgSOv/K19vx4x3TJX9Qul0OHxBZHxqYpC+jzjZ6Nm5Wcnzg0AeGBKTxXPVf5S4jH2Sb9ghHWCO95xfz0dfKCwqCMZhmRvtMIj5wqiQapSc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886648; c=relaxed/simple; bh=lLcJXMGYAt18ckXzjBJt3RZcIlon0AJDakZC8+N8M0M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=r5kY52Snju41gC8fmzL8a59Zwd3U0MGd3pawRavC3EOmRijSayx3xdhOLgLaj+SAbC0Op0KprhVq+2Nodj2vGLCzI3s/CWDbl1/WPBxDcDZxKKrJO90ifsqwfyereQBTDOB0nmUB8eg+8iXCU04Wd19MZcygSTFZqAZE/GdhaZU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylehuey.com; spf=pass smtp.mailfrom=kylehuey.com; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b=FQ3BuQZb; arc=none smtp.client-ip=209.85.222.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylehuey.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylehuey.com Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-78ebc7e1586so148637485a.1 for ; Thu, 11 Apr 2024 18:50:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1712886646; x=1713491446; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2I8ioTIE3St0NCO304/67c5ZZcTg34djE+ltGa2DGgk=; b=FQ3BuQZb8LtOhmt890Bmeq2fPTjqyrHlVC00FaR2K+bSiNS5ffhWMIBCbKqWbCoF9a yTKEojGuNSBLHgeauasteqnUs7ZXlVqVebsjOuOToLwj2QxBsBLCTIrm1YgnQ4ZdptSY Kn0JB14JBHBz64zM4H39qMyt7yft6iJNslfW7EP1u006R86EdrKnRdvwl1GqckKj6Ie2 bmgYnPNGoRRPQnX0i07qpCzGA/iBz/eNkYm2z65qVVDMY6G7ohPS6eRhxyOMeMtXrw90 /p4V0rsU/WxnRWomdyk0/CjfkbhOjQ+wcY/Gtshn6HRziwKWDkEIHi6FpxzvGm7vATrB pbfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712886646; x=1713491446; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2I8ioTIE3St0NCO304/67c5ZZcTg34djE+ltGa2DGgk=; b=cNeSKbuAblQVEeQU5lLx/wc6p8SkaDXHRR+Vdmi8FY921CJsOIuUaIEo3tLXS+lM51 mZoDGjqZhpGk5GOVP2FHE7lUfQjoBOjM7gS/pUgC0LIijCXJeB7dqeD/phELqyeFSV8y 3oShzO/kbmOZweUqnJBHF7dAQ2NOlg6pgYuCABMO/7BQAvdaJaZCr2vMg+lfDNrbu4c3 MXKujHKcKMUGWTXq6vGTksMKBlTBAINWVUorFlKxu3Fyls/4dzn72a5D9Qr/JgSXCSod b/OZA2YxVR8u0hO7KSaI1YwlhGVTjmwyCUuXyOakipweMHbLPfrg24VPSrMmwiDWrMVV Qh0g== X-Forwarded-Encrypted: i=1; AJvYcCV/wZgBxSWPqsOpEUdlAXzR20V6+8YP6hGtJRSgELPadvBQdSceuaZTAgRJ5qbrSspWA4ZoTYtwbAPPRRb7LBLRR/cI2wPs+aeffYO9 X-Gm-Message-State: AOJu0YwJd+9Rl11rjo57pR7gTysVV84TXQmCJYLPuihgeYPjycNREiuc uuCSMb1FQxl56dUMDW5tSR6yuReaAw1aUz/oKtecZXNJRwFPoThxGNs4vv5EAg== X-Received: by 2002:a05:620a:4589:b0:78e:b996:db93 with SMTP id bp9-20020a05620a458900b0078eb996db93mr3057028qkb.3.1712886646096; Thu, 11 Apr 2024 18:50:46 -0700 (PDT) Received: from ip-172-31-44-15.us-east-2.compute.internal (ec2-52-15-100-147.us-east-2.compute.amazonaws.com. [52.15.100.147]) by smtp.googlemail.com with ESMTPSA id f10-20020a05620a15aa00b0078d76c1178esm1756677qkk.119.2024.04.11.18.50.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 18:50:45 -0700 (PDT) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Kyle Huey , linux-kernel@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Namhyung Kim , Marco Elver , Yonghong Song , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: Robert O'Callahan , bpf@vger.kernel.org, Ingo Molnar , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org Subject: [PATCH v6 1/7] perf/bpf: Reorder bpf_overflow_handler() ahead of __perf_event_overflow() Date: Thu, 11 Apr 2024 18:50:13 -0700 Message-Id: <20240412015019.7060-2-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412015019.7060-1-khuey@kylehuey.com> References: <20240412015019.7060-1-khuey@kylehuey.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This will allow __perf_event_overflow() to call bpf_overflow_handler(). Signed-off-by: Kyle Huey --- kernel/events/core.c | 183 ++++++++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 91 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 724e6d7e128f..ee025125a681 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9544,6 +9544,98 @@ static inline bool sample_is_allowed(struct perf_event *event, struct pt_regs *r return true; } +#ifdef CONFIG_BPF_SYSCALL +static void bpf_overflow_handler(struct perf_event *event, + struct perf_sample_data *data, + struct pt_regs *regs) +{ + struct bpf_perf_event_data_kern ctx = { + .data = data, + .event = event, + }; + struct bpf_prog *prog; + int ret = 0; + + ctx.regs = perf_arch_bpf_user_pt_regs(regs); + if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) + goto out; + rcu_read_lock(); + prog = READ_ONCE(event->prog); + if (prog) { + perf_prepare_sample(data, event, regs); + ret = bpf_prog_run(prog, &ctx); + } + rcu_read_unlock(); +out: + __this_cpu_dec(bpf_prog_active); + if (!ret) + return; + + event->orig_overflow_handler(event, data, regs); +} + +static int perf_event_set_bpf_handler(struct perf_event *event, + struct bpf_prog *prog, + u64 bpf_cookie) +{ + if (event->overflow_handler_context) + /* hw breakpoint or kernel counter */ + return -EINVAL; + + if (event->prog) + return -EEXIST; + + if (prog->type != BPF_PROG_TYPE_PERF_EVENT) + return -EINVAL; + + if (event->attr.precise_ip && + prog->call_get_stack && + (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) || + event->attr.exclude_callchain_kernel || + event->attr.exclude_callchain_user)) { + /* + * On perf_event with precise_ip, calling bpf_get_stack() + * may trigger unwinder warnings and occasional crashes. + * bpf_get_[stack|stackid] works around this issue by using + * callchain attached to perf_sample_data. If the + * perf_event does not full (kernel and user) callchain + * attached to perf_sample_data, do not allow attaching BPF + * program that calls bpf_get_[stack|stackid]. + */ + return -EPROTO; + } + + event->prog = prog; + event->bpf_cookie = bpf_cookie; + event->orig_overflow_handler = READ_ONCE(event->overflow_handler); + WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); + return 0; +} + +static void perf_event_free_bpf_handler(struct perf_event *event) +{ + struct bpf_prog *prog = event->prog; + + if (!prog) + return; + + WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); + event->prog = NULL; + bpf_prog_put(prog); +} +#else +static int perf_event_set_bpf_handler(struct perf_event *event, + struct bpf_prog *prog, + u64 bpf_cookie) +{ + return -EOPNOTSUPP; +} + +static void perf_event_free_bpf_handler(struct perf_event *event) +{ +} +#endif + /* * Generic event overflow handling, sampling. */ @@ -10422,97 +10514,6 @@ static void perf_event_free_filter(struct perf_event *event) ftrace_profile_free_filter(event); } -#ifdef CONFIG_BPF_SYSCALL -static void bpf_overflow_handler(struct perf_event *event, - struct perf_sample_data *data, - struct pt_regs *regs) -{ - struct bpf_perf_event_data_kern ctx = { - .data = data, - .event = event, - }; - struct bpf_prog *prog; - int ret = 0; - - ctx.regs = perf_arch_bpf_user_pt_regs(regs); - if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) - goto out; - rcu_read_lock(); - prog = READ_ONCE(event->prog); - if (prog) { - perf_prepare_sample(data, event, regs); - ret = bpf_prog_run(prog, &ctx); - } - rcu_read_unlock(); -out: - __this_cpu_dec(bpf_prog_active); - if (!ret) - return; - - event->orig_overflow_handler(event, data, regs); -} - -static int perf_event_set_bpf_handler(struct perf_event *event, - struct bpf_prog *prog, - u64 bpf_cookie) -{ - if (event->overflow_handler_context) - /* hw breakpoint or kernel counter */ - return -EINVAL; - - if (event->prog) - return -EEXIST; - - if (prog->type != BPF_PROG_TYPE_PERF_EVENT) - return -EINVAL; - - if (event->attr.precise_ip && - prog->call_get_stack && - (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) || - event->attr.exclude_callchain_kernel || - event->attr.exclude_callchain_user)) { - /* - * On perf_event with precise_ip, calling bpf_get_stack() - * may trigger unwinder warnings and occasional crashes. - * bpf_get_[stack|stackid] works around this issue by using - * callchain attached to perf_sample_data. If the - * perf_event does not full (kernel and user) callchain - * attached to perf_sample_data, do not allow attaching BPF - * program that calls bpf_get_[stack|stackid]. - */ - return -EPROTO; - } - - event->prog = prog; - event->bpf_cookie = bpf_cookie; - event->orig_overflow_handler = READ_ONCE(event->overflow_handler); - WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); - return 0; -} - -static void perf_event_free_bpf_handler(struct perf_event *event) -{ - struct bpf_prog *prog = event->prog; - - if (!prog) - return; - - WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); - event->prog = NULL; - bpf_prog_put(prog); -} -#else -static int perf_event_set_bpf_handler(struct perf_event *event, - struct bpf_prog *prog, - u64 bpf_cookie) -{ - return -EOPNOTSUPP; -} -static void perf_event_free_bpf_handler(struct perf_event *event) -{ -} -#endif - /* * returns true if the event is a tracepoint, or a kprobe/upprobe created * with perf_event_open() -- 2.34.1