Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1112815pxf; Thu, 18 Mar 2021 21:57:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbFjeP7BuKttwAlw2TVCcQG1PuT1fLt1LWubvGMv/3k04VsIB2AD1kODv/aenwwjnHrK0N X-Received: by 2002:a17:906:d797:: with SMTP id pj23mr2246016ejb.367.1616129871534; Thu, 18 Mar 2021 21:57:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616129871; cv=none; d=google.com; s=arc-20160816; b=mwnRiSNYCpBFSJf0976ff5cBotwHQQQla7lmE2vlOEUvROgOvFH+vh6t18HzNkNr08 3Hi4Gur9S/ImU/fRPfqLbOUYztWnEh20+cjgw16+VYxTnHH7YR6e0b9JReWjTUGjSRm2 3dlq1/Tvhl6lkDUcHRuw3Goc/ROB8aF2w+S2D7kktFUJU1WnlrwBbtc/Qu9g307OABxn wLAhROt0HInOKGwmgMBnS6zB3zpzE6I0qb+QCbFT1AW1mstVnjfl66cjIgAGVCdxihKT gD8biwXFP1W1hidYlx1JYLHH96zDmMdUv1OF6LEde1/v/Ndg1QAOjiC6pY7Wi7x+CBXF U3bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=tpt94xtYll9ZLq6Ys5+o3xkJXlN5PgDO6uZNupo2feQ=; b=NcoTf7DcQHmP26cc2PVTNk8ztT186gdNQ05dxk6IMXEA+qpXZ9PvqNENp2RrUFnV0l RiOar2GNRUYv3WFWdHaQgpHQW5Nux+qcxjqPBTQQRJacEdoiRZpw3wyAdpiZ4rRVRN9q pb1aIpJTHUvVQiVEJFPeuOCfA6WGJW7ZhA2m6Op15NyHHZbbCkQKU9dxM4k1hxudBwsZ c+GvmGVLz2/QvE0O69J5tdJBeaLtJae5qrlmrC2qzMMkOJ7kIa0TZzmZGT4o0nlJrSvp plTQmgSJ3QLaiiwfwdgdpILH17fFUAaD1wOtM7n0zTbhQ48Ks1V3jc1RNgv3bCfUqnpD CICw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jN1LtZ3r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j19si3009698edp.531.2021.03.18.21.57.29; Thu, 18 Mar 2021 21:57:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jN1LtZ3r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233633AbhCSEzk (ORCPT + 99 others); Fri, 19 Mar 2021 00:55:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:42912 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233680AbhCSEzN (ORCPT ); Fri, 19 Mar 2021 00:55:13 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 621BD64F53; Fri, 19 Mar 2021 04:55:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1616129713; bh=3KJhudPkw2fjFs3MGsB08Y0IMXhu7LIKfsovqH3NJoE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=jN1LtZ3rGSdpUlrwhppvQ9n/HVsLWGiyOYSSqNiq6N0rbCdqXc3Qo/Nx9nC9foox0 uur6fhd5j5uMm/muICxjbD28CqnD1BqRms2lP/l/6FyVI5pidVkBRhHGrJfp0/unLJ ydwTPCmUyF1W8K1jjByY8dV8+on5csiolTujAsKeoRL+j5KPIenqqJ8rISGSIZGTaz x7IWlGGgR1WL2d0L8iuKYMZxCfm/rpzpOy/c7rdqXA72+jOakcaLxtCs0UwTsoi3Q/ 6soqsAfcxGLZK0YRnMlL/p5g8QdeFhcQ/t6HxTaX/4/rSq+ZdBvopWyn2XHez5je7Q in+ipbJqz/C3A== Date: Fri, 19 Mar 2021 06:55:09 +0200 From: Leon Romanovsky To: Alex Elder Cc: davem@davemloft.net, kuba@kernel.org, bjorn.andersson@linaro.org, evgreen@chromium.org, cpratapa@codeaurora.org, elder@kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH net-next 3/4] net: ipa: introduce ipa_assert() Message-ID: References: <20210319042923.1584593-1-elder@linaro.org> <20210319042923.1584593-4-elder@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210319042923.1584593-4-elder@linaro.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 18, 2021 at 11:29:22PM -0500, Alex Elder wrote: > Create a new macro ipa_assert() to verify that a condition is true. > This produces a build-time error if the condition can be evaluated > at build time; otherwise __ipa_assert_runtime() is called (described > below). > > Another macro, ipa_assert_always() verifies that an expression > yields true at runtime, and if it does not, reports an error > message. > > When IPA validation is enabled, __ipa_assert_runtime() becomes a > call to ipa_assert_always(), resulting in runtime verification of > the asserted condition. Otherwise __ipa_assert_runtime() has no > effect, so such ipa_assert() calls are effectively ignored. > > IPA assertion errors will be reported using the IPA device if > possible. > > Signed-off-by: Alex Elder > --- > drivers/net/ipa/ipa_assert.h | 50 ++++++++++++++++++++++++++++++++++++ > 1 file changed, 50 insertions(+) > create mode 100644 drivers/net/ipa/ipa_assert.h > > diff --git a/drivers/net/ipa/ipa_assert.h b/drivers/net/ipa/ipa_assert.h > new file mode 100644 > index 0000000000000..7e5b9d487f69d > --- /dev/null > +++ b/drivers/net/ipa/ipa_assert.h > @@ -0,0 +1,50 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * Copyright (C) 2021 Linaro Ltd. > + */ > +#ifndef _IPA_ASSERT_H_ > +#define _IPA_ASSERT_H_ > + > +#include > +#include > +#include > + > +/* Verify the expression yields true, and fail at build time if possible */ > +#define ipa_assert(dev, expr) \ > + do { \ > + if (__builtin_constant_p(expr)) \ > + compiletime_assert(expr, __ipa_failure_msg(expr)); \ > + else \ > + __ipa_assert_runtime(dev, expr); \ > + } while (0) > + > +/* Report an error if the given expression evaluates to false at runtime */ > +#define ipa_assert_always(dev, expr) \ > + do { \ > + if (unlikely(!(expr))) { \ > + struct device *__dev = (dev); \ > + \ > + if (__dev) \ > + dev_err(__dev, __ipa_failure_msg(expr)); \ > + else \ > + pr_err(__ipa_failure_msg(expr)); \ > + } \ > + } while (0) It will be much better for everyone if you don't obfuscate existing kernel primitives and don't hide constant vs. dynamic expressions. So any random kernel developer will be able to change the code without investing too much time to understand this custom logic. And constant expressions are checked with BUILD_BUG_ON(). If you still feel need to provide assertion like this, it should be done in general code. Thanks > + > +/* Constant message used when an assertion fails */ > +#define __ipa_failure_msg(expr) "IPA assertion failed: " #expr "\n" > + > +#ifdef IPA_VALIDATION > + > +/* Only do runtime checks for "normal" assertions if validating the code */ > +#define __ipa_assert_runtime(dev, expr) ipa_assert_always(dev, expr) > + > +#else /* !IPA_VALIDATION */ > + > +/* "Normal" assertions aren't checked when validation is disabled */ > +#define __ipa_assert_runtime(dev, expr) \ > + do { (void)(dev); (void)(expr); } while (0) > + > +#endif /* !IPA_VALIDATION */ > + > +#endif /* _IPA_ASSERT_H_ */ > -- > 2.27.0 >