From patchwork Thu Dec 7 16:34:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 175284 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp4907160vqy; Thu, 7 Dec 2023 08:36:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IGBt1Roz6mNMpcHeoq5NsCIYmtFHl90qzUeGO1g+h/vwA2YiE8rJqiBhlMO9FsmeMhkqhzG X-Received: by 2002:a05:6a20:7343:b0:18c:9705:47cd with SMTP id v3-20020a056a20734300b0018c970547cdmr3186522pzc.52.1701966996695; Thu, 07 Dec 2023 08:36:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701966996; cv=none; d=google.com; s=arc-20160816; b=MdiNguu7i77OnfzxVdHdctHrI+n+uaIrXmjX5JldrS4TSsULCHVutUuU+v4zwaI80H Vt+vrN03RqeExA/WXw+sLy3fREwMWsH0ZGWFkGK3K9FliGOSFWO1PT274vhPEXGCbmjQ lRxIiyNF+hIoLVVeCu7/2XGb53lPfRbLMOgGB84fND7CckG5xhWP1SThegvl6UA6q5cj 6d4EtPr3drhRx9wTHgHYI7NMCs6FblU5+OHoiEqCljaYKQMCzjWEG0J+yfrgt0WxPE4V zeIyxKMmTlP4I7y5hR2AOh5FPbCqzugjPEJ295QmvD29j+/XBPXdGYbvb0jSrByih3BD s9hA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IXhJZgRGOB5Sycf3QWqHrD17iXmXG1N3cpy0e+e8qfo=; fh=T2aUC8Bshg+HXrN4F056u0bzm3RMUPetV/w6+W6ei8w=; b=v35xPqzfkBA2rRHXK2kDn0pQB2bAD55ccV7zlntybybcKqu103rRQYLqjWpl9v7Nt/ vrsfK87zVQj/uSYuMhrtxQ8ryQWTE3xxG4b/zNjbA2qidT7gOq9JQAEQRFIdW/4GxtQq VM8xGaSkD8KkF6P//0sb9dEZdv2QgWMD39AzNznhqMWWIADDimCWnwFvx8Z/jCTMMcGg FFarJCqFIm6kRUIOuz6urzS+/A69qKtbEg7MDGT8w4Y/4sXADqXf4WPE9C29cqM/b1sC Bo3gtZJHNMa9UVwtBt3wLREmLVl9JZ3BVYwQu1fnc7YtDc2u3TuQf6JIdnXcriAC46Y4 hCfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b="DMpiC/Ho"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id r12-20020a6560cc000000b005c69365abb9si1460880pgv.697.2023.12.07.08.36.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 08:36:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b="DMpiC/Ho"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id C7630807F970; Thu, 7 Dec 2023 08:35:23 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232709AbjLGQfK (ORCPT + 99 others); Thu, 7 Dec 2023 11:35:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232894AbjLGQfG (ORCPT ); Thu, 7 Dec 2023 11:35:06 -0500 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CAE4610F3 for ; Thu, 7 Dec 2023 08:35:12 -0800 (PST) Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1d05e4a94c3so9686255ad.1 for ; Thu, 07 Dec 2023 08:35:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1701966912; x=1702571712; 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=IXhJZgRGOB5Sycf3QWqHrD17iXmXG1N3cpy0e+e8qfo=; b=DMpiC/Hon0R+3JwN8VGCqbuqzpnpWSu5pqBmcD9KUHVsbS11Mi8u5NBERxWWg/lGEq wwplDbbfBd7ZotHqL9Hg5bQRfd6wrwiak0rirXKR3XiFEcaoYP4EjjynBVYyxq0N1wLR A8EfFRf8m1mZG2VjVsuGib2P1Kx6T69q3Yzi8xvS/rO4eg95ANSkwLA1RVEhyKp8pIao gXbWsYjentZeJ0hY7C3HjXFFTEr4Mn548jUcNbQFpvxrDYNkNH/Za0pFvlYD8cG63SAt G7QwGBh5XBhmGHtgp7Vs38ha4HQHOxbF8QCBHHTfcyyGLN/MZrYEHTJWyQc7/vy9mxr/ GbKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701966912; x=1702571712; 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=IXhJZgRGOB5Sycf3QWqHrD17iXmXG1N3cpy0e+e8qfo=; b=YMU99r5wq0O+PkoFP34jy6mL91GTsIq8uUzSQRumHpbDwJNXTE+P0iHQ13q7UbZWEz Z03HZ9+E/jxH1OV9YjyOKA6YxAS1Q1BJe6Eu+5XebK54/4LKIYkyriZFwRsaZDA7EHiE vdZybYcLMjEJ4KZfxvM8/AoGm2TOmV+jqLVFxs7Jpy6jhHQN1EhPPbk7UFmVm2zB+qoJ eQNOHKhyiTpHntHx6heiyDOUFhKekFVAsyEsRXR1tgN1E/3YngQaBMz+CJpkDFiN/l8n 6G8RDAYm8xarN2Lfv1bBkvMnHdWumPuquyYtNzqkNNfhb+qk5SNFXoNuxmiZDPZ/6LO2 Crug== X-Gm-Message-State: AOJu0YwTu7zYOo1NGXqcMPAfSX6PoKsNul2ynHNpkwRS2kEh54mtDIwY JYfli10R2k3jsOJ++zQvihZTQQ== X-Received: by 2002:a17:902:dacf:b0:1cf:c42c:cfbd with SMTP id q15-20020a170902dacf00b001cfc42ccfbdmr3656529plx.0.1701966912115; Thu, 07 Dec 2023 08:35:12 -0800 (PST) Received: from zhadum.home.kylehuey.com (c-76-126-33-191.hsd1.ca.comcast.net. [76.126.33.191]) by smtp.gmail.com with ESMTPSA id iw15-20020a170903044f00b001d1cd7e4acfsm6143plb.201.2023.12.07.08.35.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 08:35:11 -0800 (PST) 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 Cc: Robert O'Callahan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org Subject: [PATCH v2 1/3] perf: Reorder overflow handler ahead of event_limit/sigtrap Date: Thu, 7 Dec 2023 08:34:56 -0800 Message-Id: <20231207163458.5554-2-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231207163458.5554-1-khuey@kylehuey.com> References: <20231207163458.5554-1-khuey@kylehuey.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 07 Dec 2023 08:35:24 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784641745719401684 X-GMAIL-MSGID: 1784641745719401684 The perf subsystem already allows an overflow handler to clear pending_kill to suppress a fasync signal (although nobody currently does this). Allow an overflow handler to suppress the other visible side effects of an overflow, namely event_limit accounting and SIGTRAP generation. Signed-off-by: Kyle Huey --- kernel/events/core.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index b704d83a28b2..19fddfc27a4a 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9541,6 +9541,12 @@ static int __perf_event_overflow(struct perf_event *event, */ event->pending_kill = POLL_IN; + + READ_ONCE(event->overflow_handler)(event, data, regs); + + if (!event->pending_kill) + return ret; + if (events && atomic_dec_and_test(&event->event_limit)) { ret = 1; event->pending_kill = POLL_HUP; @@ -9584,9 +9590,7 @@ static int __perf_event_overflow(struct perf_event *event, irq_work_queue(&event->pending_irq); } - READ_ONCE(event->overflow_handler)(event, data, regs); - - if (*perf_event_fasync(event) && event->pending_kill) { + if (*perf_event_fasync(event)) { event->pending_wakeup = 1; irq_work_queue(&event->pending_irq); } From patchwork Thu Dec 7 16:34:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 175282 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp4906774vqy; Thu, 7 Dec 2023 08:35:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IGFbYDQBHctvgYbTL+WX91qjNWscsEcwAe1fZyiTlo0jEA+pSu7nHbrh2kSHL0LqDQhQRJZ X-Received: by 2002:a17:902:d212:b0:1d0:6ffd:e2b4 with SMTP id t18-20020a170902d21200b001d06ffde2b4mr2480483ply.78.1701966958269; Thu, 07 Dec 2023 08:35:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701966958; cv=none; d=google.com; s=arc-20160816; b=VdIXrtvh50QsJlPQHl6s+aKwl3Z87af4ByWvK4dybYEpEHRPEY9ccv+G3QUOVe9lg3 hPT/unJZmN8GcRAcpGKmsikSpuBQ23xdNl2I3fW9Wo9YR4VsekQEWRCz1YAhN2kUiUrB T+vibxzjcNpB0nWDin3dI9Y59MSXCV22VQ7P3/BUX+QVf3L+wBQx+SRy4VjV3xOLBWWm qok8nvyCcufXEx25AJvnzjFlBawJ65ZgyXsvIrdgnQ6uv11TK5WWFA5Rh5BRvhPh+p1N bNw625SEd/dEFrQ+XT7Ms01eLVBFw+jJF30ZHEl0g1AJr+j2jflqPRq/iES3fur50mNc L/AQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=dWj1IWQKQUAn53hJd+8gzkbOyC7Uc3/+Xnz5vVEOJAM=; fh=TgNdCu0nkwWF6I0gv1FaAZU9nxNhZAbxhuG8wY4k0B8=; b=Hvm5AuoVcYaPMX4jI07DWwJqiw08CcncOM6rYRiTaiv3IInIjiUJj+NYI8Jyl2a0Pb oEjJ7S97cQ7ZcT/zK7cbWF05Sk4ey4xH48djQkC6ae5+1qUjUTt7StSBore2A1dtwclz lwbRjacrVcC50T6lBd/LZoUd17CtsxB61T5ykolQ+rSSDnqoNKYUpZEa9lhwPvOsakOm ExXyHj8KfztCtaYHfm/F6Pik4wZiNXGQpmR4ssXTKT6yegHQkASE4NmgKtLDofjUvTGn nDcPORtlLuFtwOmhMwMDge2T61k4izRdN+pNwhhmkL9xxpz/K8cQy0TG3ukable042V/ QCGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=VVwDBW55; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id c12-20020a63d50c000000b005c677a35324si1444906pgg.464.2023.12.07.08.35.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 08:35:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=VVwDBW55; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 9977E80A07F1; Thu, 7 Dec 2023 08:35:45 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233010AbjLGQfN (ORCPT + 99 others); Thu, 7 Dec 2023 11:35:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232933AbjLGQfI (ORCPT ); Thu, 7 Dec 2023 11:35:08 -0500 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D849312D for ; Thu, 7 Dec 2023 08:35:14 -0800 (PST) Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1d0538d9bbcso9409055ad.3 for ; Thu, 07 Dec 2023 08:35:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1701966914; x=1702571714; 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=dWj1IWQKQUAn53hJd+8gzkbOyC7Uc3/+Xnz5vVEOJAM=; b=VVwDBW55m0+CDPNtEo5vhp4IUC/kBBuTg9UYkGBv86g2h2uAjCqobJRyhnpOQVvSbG qATy2zfqJ6o4+Kkj5+W9rp2Rdv2C7Hsx86tKWmsJTlH3we8umq9P5y+/kqoQn8xTMFSS 3TSpknLzz1YrQRWyRl7xdHsCusaNWCrP+5IfhGReajs7G54gnc+u+TaBrmUz0vlTH08O iUec9Ff9BoKUu3RpIG1XVYA7Fns4e0RAKZB9lk2TWN1yOssf/JchcpWxGguZj1Vo+9Xl l1bYLVMtDf5CSkPUtmEsqBWmkLfP47Q6G8AEaXrhQJieSN0+JyOiNTYU5JmQs8r9nBwQ ffpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701966914; x=1702571714; 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=dWj1IWQKQUAn53hJd+8gzkbOyC7Uc3/+Xnz5vVEOJAM=; b=n+D8snVCZTvVDKLDAveraub5/vuSq23seTmb/6KMfpb1fCCRs+HOcgcT62jyPQocDV MDiVEkKa/m5UCpQwJEmxXFbehbKKoHDVquddrYsSTG+mC3QdzomyNpIAUnhmPe8+DzJH xA09QPH+FWpBsoeH05S0cTfOUlW3NZVEY3FDoCIF7ENYRp2O5byJcLUm/NSYnJ0a4cVw p1zBPbqUaDFjOcszW5gDHbpxdEl4XVM4Fufkq6lA/shOBe9Fz49ta2zwVURvJsu9GrUb mtwlk5rGJpf0JRK0IrIGQLwveksJURSuMLQTk+67pS/pgg51w6zUQZHKi0qrLHCZoT5P exeQ== X-Gm-Message-State: AOJu0Yxmhbsiyn+6DoZQimQAOC4nNk+yUpdGGtnNDt+IdiBYHLeelrMT ywny5G41D4n9i+0QwjM3ZSe6SQ== X-Received: by 2002:a17:902:b48d:b0:1d0:6ffd:e2ef with SMTP id y13-20020a170902b48d00b001d06ffde2efmr2666645plr.137.1701966914046; Thu, 07 Dec 2023 08:35:14 -0800 (PST) Received: from zhadum.home.kylehuey.com (c-76-126-33-191.hsd1.ca.comcast.net. [76.126.33.191]) by smtp.gmail.com with ESMTPSA id iw15-20020a170903044f00b001d1cd7e4acfsm6143plb.201.2023.12.07.08.35.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 08:35:13 -0800 (PST) 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 Cc: Robert O'Callahan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH v2 2/3] perf/bpf: Allow a bpf program to suppress all sample side effects. Date: Thu, 7 Dec 2023 08:34:57 -0800 Message-Id: <20231207163458.5554-3-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231207163458.5554-1-khuey@kylehuey.com> References: <20231207163458.5554-1-khuey@kylehuey.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 07 Dec 2023 08:35:45 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784641705289973812 X-GMAIL-MSGID: 1784641705289973812 Returning zero from a bpf program attached to a perf event already suppresses any data output. By clearing pending_kill, returning zero from a bpf program will effectively pretend the sample never happened for all userspace purposes. Signed-off-by: Kyle Huey Acked-by: Andrii Nakryiko --- kernel/events/core.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 19fddfc27a4a..6cda05a4969d 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10421,8 +10421,10 @@ static void bpf_overflow_handler(struct perf_event *event, rcu_read_unlock(); out: __this_cpu_dec(bpf_prog_active); - if (!ret) + if (!ret) { + event->pending_kill = 0; return; + } event->orig_overflow_handler(event, data, regs); } From patchwork Thu Dec 7 16:34:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 175283 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp4906785vqy; Thu, 7 Dec 2023 08:35:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IEA38v1A4dwrvxvm8bQrR/twxTDQfhHTHmnADVcAEvRSklrXXXC/olBR2oL04uvfqlk4x4Y X-Received: by 2002:a17:90a:e7c2:b0:286:e2bb:26a4 with SMTP id kb2-20020a17090ae7c200b00286e2bb26a4mr2194029pjb.40.1701966958883; Thu, 07 Dec 2023 08:35:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701966958; cv=none; d=google.com; s=arc-20160816; b=JY2EskB/ynGZLY1IoUslLKjcDUHmw4qz91s7ugFYJw+kUXivvLCwwQ0JTx+5Q6g1IX HxLRcqJdxQ/aekhzAKMgw9UNG9w/U9bvZSRVYBamMLd35DYZXHm2EKtA1dPBcCBoWU2r 8SRBXLuYTVS607ZWIANzpDZDtR7OlhCQ/yNRzVGIMu/2gsyHAJS0P/rkFEN2A+QbPCKa kOvJZE+ZDGIYx4pzuqQzBOE6PSAmHlgc6ws6xccQn9BJUWgpV+m4KWCOlv/wlTTs7Qwn P8N6R2WZ9ID6eHb5nYeOSVK6Js5Ng9l4+CdmiRvvtxcDO0X+yh6WF3GBtwnnorcEM76g 6hTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9osTTHotszQKXwqlQLd/oaMNF9NAZxsSosoDc6zgOuE=; fh=egt9JzErx/09xKZ01e8H7iMpAphoCk+NidsT8VHXf94=; b=WzUOm5tYtLUrZwbuKgKxvxwNrS4ZrRA0QLNMRcHynUv5F+9S4+vO065g3bDRpsTCUg p+WPEH1OGVlAMiNjbvGVB+pj/oFgagw9C06Ms8bJQOwc/Dm6D2H0nEjvPml5vBCLjmH3 mwtfOP+CyoBJ+4Ex0I3gcxdpPhDdVszlE5p1kN9sQjfmR+70MI1j1vZapdZqLNJNKgiA igot/n1E7JqsRs1ZO6SSVDkH6Z3rCdYUuUZGgBQmDrQn+fFq40tOEgS6GsklQEoTRAdn lueIjwdtiew/ddTbPAQIZmcuXGg8R/tuQBB3u2kGBxOV+bf0pf7gPV7Wd0SjlFqoBjDY T0Xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=guqiquCD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id a9-20020a17090a740900b00286acacd596si88207pjg.136.2023.12.07.08.35.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 08:35:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=guqiquCD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id B42CB80320EE; Thu, 7 Dec 2023 08:35:51 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233026AbjLGQfW (ORCPT + 99 others); Thu, 7 Dec 2023 11:35:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233041AbjLGQfT (ORCPT ); Thu, 7 Dec 2023 11:35:19 -0500 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10B2E132 for ; Thu, 7 Dec 2023 08:35:22 -0800 (PST) Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1d0c94397c0so8141955ad.2 for ; Thu, 07 Dec 2023 08:35:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1701966921; x=1702571721; 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=9osTTHotszQKXwqlQLd/oaMNF9NAZxsSosoDc6zgOuE=; b=guqiquCDDbMLYq3eH2n38T7sPBx426J4mjZcL0wNaAjrTabqZGy+S1ks8lcxs3Xfp1 C36Sj6oXju/pQDECYrk2GH5SpsWqyMLo+gNhg3ypC73rxWjOQiuGv7MENNOWMfXeZAoD eqXPCZuJFwP2l+cyIFu65Rf2pj1pg5YB+RJPDQ4X88rMro10dKJtNjzawZOwkMSsCMNb Pr8Le3ZtQGoNLImBwmSZM8tE8WSa8rPVR9SBOpRQJUdFdgqXd1Q3hwiIZFL1VbYQWetT B4ap62AEHiQ3sp0Ex3zQsgpZcQJJBQyOhwl6suuToU70J1R7hmw0UdJsumPgJXTaWmml ZEAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701966921; x=1702571721; 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=9osTTHotszQKXwqlQLd/oaMNF9NAZxsSosoDc6zgOuE=; b=dDli/Xw4Oy11hA9JdwHazv/Jp9/sHmLCVN9E0Rkne1IYolDoJNiEL9gnL5c4KRWdqJ MvS6C9aywQMDVguw4UCW5c3aIyBKE3ZCq/UO0Jd9GvRrToVaLlz052IvV7SJn6WsZwXy HqIB2F6ey5w76OhZAXZxdV9aNH98IrQ+ajrfsdCICC2odbyXDn5vxRmEh7geogxco2em Sv4gNcCtdwfI319irTwmdnp5K8YtzaIRCA3NEys+ok4cKRnxbY1QHCxdgxiLbihIt1rK tJiJASiBowPZ3Q6m1lcgME1GgpYRj/2celnnsuuMCkdYmcOFO+1cllhKjKqL8FcG3QV4 zMxA== X-Gm-Message-State: AOJu0YxnpQk23Zb4VSVhO7ZZJxBSqEo6qOmXFFl6HMQdsRbJr8hGKSp7 2Pfn4UD5GcWOTihKg3bWDMCHJg== X-Received: by 2002:a17:902:7c17:b0:1d0:b033:4a98 with SMTP id x23-20020a1709027c1700b001d0b0334a98mr2630388pll.17.1701966921325; Thu, 07 Dec 2023 08:35:21 -0800 (PST) Received: from zhadum.home.kylehuey.com (c-76-126-33-191.hsd1.ca.comcast.net. [76.126.33.191]) by smtp.gmail.com with ESMTPSA id iw15-20020a170903044f00b001d1cd7e4acfsm6143plb.201.2023.12.07.08.35.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 08:35:20 -0800 (PST) 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 Cc: Robert O'Callahan , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v2 3/3] selftest/bpf: Test a perf bpf program that suppresses side effects. Date: Thu, 7 Dec 2023 08:34:58 -0800 Message-Id: <20231207163458.5554-4-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231207163458.5554-1-khuey@kylehuey.com> References: <20231207163458.5554-1-khuey@kylehuey.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 07 Dec 2023 08:35:51 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784641706004809151 X-GMAIL-MSGID: 1784641706004809151 The test sets a hardware breakpoint and uses a bpf program to suppress the side effects of a perf event sample, including I/O availability signals, SIGTRAPs, and decrementing the event counter limit, if the ip matches the expected value. Then the function with the breakpoint is executed multiple times to test that all effects behave as expected. Signed-off-by: Kyle Huey --- .../selftests/bpf/prog_tests/perf_skip.c | 145 ++++++++++++++++++ .../selftests/bpf/progs/test_perf_skip.c | 15 ++ 2 files changed, 160 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/perf_skip.c create mode 100644 tools/testing/selftests/bpf/progs/test_perf_skip.c diff --git a/tools/testing/selftests/bpf/prog_tests/perf_skip.c b/tools/testing/selftests/bpf/prog_tests/perf_skip.c new file mode 100644 index 000000000000..f6fa9bfd9efa --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c @@ -0,0 +1,145 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +/* We need the latest siginfo from the kernel repo. */ +#include +#define __have_siginfo_t 1 +#define __have_sigval_t 1 +#define __have_sigevent_t 1 +#define __siginfo_t_defined +#define __sigval_t_defined +#define __sigevent_t_defined +#define _BITS_SIGINFO_CONSTS_H 1 +#define _BITS_SIGEVENT_CONSTS_H 1 + +#include +#include "test_perf_skip.skel.h" +#include +#include +#include + +int signals_unexpected = 1; +int sigio_count, sigtrap_count; + +static void handle_sigio(int sig __always_unused) +{ + ASSERT_OK(signals_unexpected, "perf event not skipped"); + ++sigio_count; +} + +static void handle_sigtrap(int signum __always_unused, + siginfo_t *info, + void *ucontext __always_unused) +{ + ASSERT_OK(signals_unexpected, "perf event not skipped"); + ASSERT_EQ(info->si_code, TRAP_PERF, "wrong si_code"); + ++sigtrap_count; +} + +static noinline int test_function(void) +{ + asm volatile (""); + return 0; +} + +void serial_test_perf_skip(void) +{ + struct sigaction action = {}; + struct sigaction previous_sigtrap; + sighandler_t previous_sigio; + struct test_perf_skip *skel = NULL; + struct perf_event_attr attr = {}; + int perf_fd = -1; + int err; + struct f_owner_ex owner; + struct bpf_link *prog_link = NULL; + + action.sa_flags = SA_SIGINFO | SA_NODEFER; + action.sa_sigaction = handle_sigtrap; + sigemptyset(&action.sa_mask); + if (!ASSERT_OK(sigaction(SIGTRAP, &action, &previous_sigtrap), "sigaction")) + return; + + previous_sigio = signal(SIGIO, handle_sigio); + + skel = test_perf_skip__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_load")) + goto cleanup; + + attr.type = PERF_TYPE_BREAKPOINT; + attr.size = sizeof(attr); + attr.bp_type = HW_BREAKPOINT_X; + attr.bp_addr = (uintptr_t)test_function; + attr.bp_len = sizeof(long); + attr.sample_period = 1; + attr.sample_type = PERF_SAMPLE_IP; + attr.pinned = 1; + attr.exclude_kernel = 1; + attr.exclude_hv = 1; + attr.precise_ip = 3; + attr.sigtrap = 1; + attr.remove_on_exec = 1; + + perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); + if (perf_fd < 0 && (errno == ENOENT || errno == EOPNOTSUPP)) { + printf("SKIP:no PERF_TYPE_BREAKPOINT/HW_BREAKPOINT_X\n"); + test__skip(); + goto cleanup; + } + if (!ASSERT_OK(perf_fd < 0, "perf_event_open")) + goto cleanup; + + // Configure the perf event to signal on sample. + err = fcntl(perf_fd, F_SETFL, O_ASYNC); + if (!ASSERT_OK(err, "fcntl(F_SETFL, O_ASYNC)")) + goto cleanup; + + owner.type = F_OWNER_TID; + owner.pid = syscall(__NR_gettid); + err = fcntl(perf_fd, F_SETOWN_EX, &owner); + if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)")) + goto cleanup; + + // Allow at most one sample. A sample rejected by bpf should + // not count against this. + err = ioctl(perf_fd, PERF_EVENT_IOC_REFRESH, 1); + if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_REFRESH)")) + goto cleanup; + + prog_link = bpf_program__attach_perf_event(skel->progs.handler, perf_fd); + if (!ASSERT_OK_PTR(prog_link, "bpf_program__attach_perf_event")) + goto cleanup; + + // Configure the bpf program to suppress the sample. + skel->bss->ip = (uintptr_t)test_function; + test_function(); + + ASSERT_EQ(sigio_count, 0, "sigio_count"); + ASSERT_EQ(sigtrap_count, 0, "sigtrap_count"); + + // Configure the bpf program to allow the sample. + skel->bss->ip = 0; + signals_unexpected = 0; + test_function(); + + ASSERT_EQ(sigio_count, 1, "sigio_count"); + ASSERT_EQ(sigtrap_count, 1, "sigtrap_count"); + + // Test that the sample above is the only one allowed (by perf, not + // by bpf) + test_function(); + + ASSERT_EQ(sigio_count, 1, "sigio_count"); + ASSERT_EQ(sigtrap_count, 1, "sigtrap_count"); + +cleanup: + if (prog_link) + bpf_link__destroy(prog_link); + if (perf_fd >= 0) + close(perf_fd); + if (skel) + test_perf_skip__destroy(skel); + + signal(SIGIO, previous_sigio); + sigaction(SIGTRAP, &previous_sigtrap, NULL); +} diff --git a/tools/testing/selftests/bpf/progs/test_perf_skip.c b/tools/testing/selftests/bpf/progs/test_perf_skip.c new file mode 100644 index 000000000000..417a9db3b770 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_perf_skip.c @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include +#include + +uintptr_t ip; + +SEC("perf_event") +int handler(struct bpf_perf_event_data *data) +{ + // Skip events that have the correct ip. + return ip != PT_REGS_IP(&data->regs); +} + +char _license[] SEC("license") = "GPL";