Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp6417002rdb; Thu, 14 Dec 2023 19:11:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IGdCKDNBvzeqAqsrizB3D5aeeEPCKWgK0OkuffndreLlQ10Dp0LAjMZx+vHTXqvxbTRgnAL X-Received: by 2002:a05:6512:e88:b0:50b:fc9f:3031 with SMTP id bi8-20020a0565120e8800b0050bfc9f3031mr6842130lfb.90.1702609887635; Thu, 14 Dec 2023 19:11:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702609887; cv=none; d=google.com; s=arc-20160816; b=UMVNdufFH+Y1kCOtE5J3OcMqm4zcTBvfqn4MXNadBVM/vf0yv9LkmjMQOPpnuyMcb5 dklKzREal/a2hBUuoq1aFBpqy/ZhuFH287l0TsPSfp3LSoo9JkteR2SFiduxdvZrVcPt CyM+eHiig91+Gd0mK4dSGnqhrKIO/H3NxdX2DhrrEW2tmWLxXcKpG3l43WJatbnFvzy6 PBZLzVcq7W2BbaCnFk1EDDOxmGY8nkNyt8Cc7gJVarSCL7emCTMBFqaKRnzY7n2DedaU DieWJ5uvj2kJ4qW/jsPcTjOAOaQy742cGO7ckuWiBDHOImVNgCl7AO7DQLJmqVaYOQB1 OajQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=HWWvM6V+4Sz/C3HWl2XXKBRKAxsD/qZWAz3+i6W9vIE=; fh=YZGP4lEWjtGDFirwY1avemseIuDub6k+TIVHVVfDOyo=; b=aZrlRU5ljzSE6jq4lF2JLtfCUoGgTf9R3Lla6hLRDJ9UOxIXdKt77J3lKOEhjoKMFh iU6IV3KbCrkRJL9AuEkUCRH3pgwUb94Pmjls8orfp3lokPT1nJb6joVwcNJbt0nPv7t1 vX1z4/j3oHOEpRZsqQJxd5JuNdB2kVXtRIsvvQ/pPszmKKtEk0vPcrV0QCqUYiqjWoJO SEcrc+5z/yclvjANwbbaSKG3ZL29FudqrxcRdp3Bh8ni0AZ/OggKlulcUvOL8WFRknq3 OvptlfBfwQ5Br6HEBEAdlEGj2Frq1AweI+Gvrb8l3of9Br9iKukMM1+O/EOsjKNM7dxR sY2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=AkoONAbH; spf=pass (google.com: domain of linux-kernel+bounces-394-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-394-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id n21-20020a509355000000b0054cd052378dsi6966328eda.320.2023.12.14.19.11.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 19:11:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-394-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=AkoONAbH; spf=pass (google.com: domain of linux-kernel+bounces-394-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-394-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 606DD1F2169E for ; Fri, 15 Dec 2023 03:11:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 863FE1FBC; Fri, 15 Dec 2023 03:11:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AkoONAbH" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 347D4D275; Fri, 15 Dec 2023 03:11:11 +0000 (UTC) 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 Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-40c48d7a7a7so2130845e9.3; Thu, 14 Dec 2023 19:11:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702609870; x=1703214670; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=HWWvM6V+4Sz/C3HWl2XXKBRKAxsD/qZWAz3+i6W9vIE=; b=AkoONAbHTLbcb+4vZxG8VUX3bdonca7dWy9n4/R2atJS3PFHA/zTvJ/Dw/T523SDeV jJ/f41TmBLdXbbrSzW/owEd0k7CLL/66REPWvdRNKbJcZVH6f/eCy/ssdgG48f8a7ZxR JlxyX3Tl9pzahbrqdm5T66aYzrMpn3p3Dp+A0NKfTroF3GeZ7m6QPiTNOQ2EdWG5TTLg ddd/L2sPgdt4D6c/f8S8q9z7ioEaA47rX+kwlyIn8S4I8Qh/CosbLSQsxjwz8bJJ/RaF 9dZ0/tqQn+f7yfjwtsIKp86i7faXhGJfTpqU9ge/SOMF3kfkRHkDwCUWmCAtnXWyjdrz ICHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702609870; x=1703214670; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HWWvM6V+4Sz/C3HWl2XXKBRKAxsD/qZWAz3+i6W9vIE=; b=tztv/Hf/tifLdduFJ30r8BjXrrK3jJW4ySHKxDwhB2Vdc/SBsAPur3ZG7T0S09+z4/ RujJ6V6FQLeab/d/CTTN7YcEfc3gzO7OfqbIo6fFzwgt9vxacBfkt/+wGqQZps73UxZl XBDwuwCByk3KByNTryqAdpt+tA5HbUjy7VNv7h3hY3gRA6OFVRM7zIwnuanRN8kc1uJh BEixsJ/QEV9hkXAD8t4RPmtYYAiW+s1KBxT6S9Ub11SM3dwvESQARuNjX1MS98T/o5z8 xl3s5vIW4sEtrA1FHEJ3Y9+U2keCoTiz3xrQKUYwY1gbZnWGkl1xDb+3jjoQ21mr10xT G3IA== X-Gm-Message-State: AOJu0YzmUFd2PWOf4W+Q0vBYL/g7JuTqml9snnkDWExN+8terbL5VUPI OP+7Q4dpphdyOBfVqlcfX4CXb272OAuXxMBPGyY= X-Received: by 2002:adf:e282:0:b0:332:ef1e:bb88 with SMTP id v2-20020adfe282000000b00332ef1ebb88mr5991991wri.33.1702609870183; Thu, 14 Dec 2023 19:11:10 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <97ada80f3aaaeb16bf97e31a8fc204513b4fb6a9.1702594357.git.dxu@dxuuu.xyz> In-Reply-To: From: Alexei Starovoitov Date: Thu, 14 Dec 2023 19:10:58 -0800 Message-ID: Subject: Re: [PATCH bpf-next 2/3] bpf: selftests: Add bpf_assert_if() and bpf_assert_with_if() macros To: Daniel Xu Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Shuah Khan , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , bpf , "open list:KERNEL SELFTEST FRAMEWORK" , LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Dec 14, 2023 at 6:46=E2=80=AFPM Alexei Starovoitov wrote: > > On Thu, Dec 14, 2023 at 2:56=E2=80=AFPM Daniel Xu wrote: > > > > These macros are a temporary stop-gap until bpf exceptions support > > unwinding acquired entities. Basically these macros act as if they take > > a callback which only get executed if the assertion fails. > > > > Signed-off-by: Daniel Xu > > --- > > .../testing/selftests/bpf/bpf_experimental.h | 22 +++++++++++++++++++ > > 1 file changed, 22 insertions(+) > > > > diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/tes= ting/selftests/bpf/bpf_experimental.h > > index 1386baf9ae4a..d63f415bef26 100644 > > --- a/tools/testing/selftests/bpf/bpf_experimental.h > > +++ b/tools/testing/selftests/bpf/bpf_experimental.h > > @@ -263,6 +263,17 @@ extern void bpf_throw(u64 cookie) __ksym; > > */ > > #define bpf_assert(cond) if (!(cond)) bpf_throw(0); > > > > +/* Description > > + * Assert that a conditional expression is true. If false, runs co= de in the > > + * body before throwing. > > + * Returns > > + * Void. > > + * Throws > > + * An exception with the value zero when the assertion fails. > > + */ > > +#define bpf_assert_if(cond) \ > > + for (int ___i =3D 0, ___j =3D !!(cond); !(___j) && !___i; bpf_t= hrow(0), ___i++) > > Kumar, > > Is this approach reliable? > I suspect the compiler can still optimize it. > I feel it will be annoying to clean up if folks start using it now, > since there won't be a drop in replacement. > Every such bpf_assert_if() would need to be manually patched. > > If 2nd part of exception is far, how about we add an equivalent > of __bpf_assert() macroses with conditional ops in asm, > but with extra 'asm volatile goto' that can be used to construct > release of resources. > > bpf_do_assert_eq(var1, 0) { bpf_spin_unlock(...); } > bpf_do_assert_lt(var2, 0) { bpf_spin_unlock(...); } Just realized that we can go the other way instead. We can get rid of bpf_assert_eq/ne/... and replace with: diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h index 1386baf9ae4a..1c500287766d 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -254,6 +254,15 @@ extern void bpf_throw(u64 cookie) __ksym; } \ }) +#define _EQ(LHS, RHS) \ + ({ int var =3D 1;\ + asm volatile goto("if %[lhs] =3D=3D %[rhs] goto %l[l_yes]" = \ + :: [lhs] "r"(LHS), [rhs] "i"(RHS) :: l_yes);\ + var =3D 0;\ +l_yes:\ + var;\ + }) + /* Description * Assert that a conditional expression is true. * Returns diff --git a/tools/testing/selftests/bpf/progs/exceptions.c b/tools/testing/selftests/bpf/progs/exceptions.c index 2811ee842b01..1111e852f154 100644 --- a/tools/testing/selftests/bpf/progs/exceptions.c +++ b/tools/testing/selftests/bpf/progs/exceptions.c @@ -203,6 +203,7 @@ __noinline int assert_nz_gfunc(u64 c) volatile u64 cookie =3D c; bpf_assert(cookie !=3D 0); + bpf_assert(_EQ(cookie, 2)); return 0; } we can probably remove bpf_assert_with() and all of the bpf_assert_le|ne|qt|eq|_with() Users can open code everything: if (!_EQ(foo, bar)) bpf_throw(123); bpf_assert_if() can work too, but let's call it bpf_do_assert() and use like: bpf_do_assert(EQ(time, 0)) { // cleanup }