Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2167787pxb; Sun, 17 Oct 2021 07:33:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1OBN/n59BZC5w6mVbagBpJRFU0uzvgbJytK22Fj5Py+SUhNm67ONQ12LcF2FcinWgSNsD X-Received: by 2002:a17:90b:4c86:: with SMTP id my6mr27523205pjb.203.1634481232526; Sun, 17 Oct 2021 07:33:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634481232; cv=none; d=google.com; s=arc-20160816; b=FETsVuDG3T5UQHy9FlJ3X9MqOQoZE7RpoFOQIUMEY1dZo4Qh7CAb9RlIMYWxrUsrAv kty0dQcBlgFjrSG5qSq3HycWquR/N4zWaxOucrCyDYLOF3z7WylKX/I9N/zqDGF2bDrB i0AR3frsSNBr4egbLccwzngb2Yc7xUuiSE4SaK3oI8QGDSaiPu1RsRgMO2VR7AFvBLS8 3h3IGxpbsRZcPAqky9hYEQHoX1AifpkDLw/h8TRio/RryWp3ArWgTQxFwB+9iJqDwLNn mlZtDInNbF4D6jNZaFO1MSwPWk065uJaqa10brTUyTsjth6+vKVBxi27QP6yb2PQrRX9 oGpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=2hjBOvKy6xDlDiNJhW19TOyI4tkya8JrKkq1DPtskBE=; b=On1UTsmrwFpo3DREeemwlthsD7SsPFjXOpTezUIp9sjhJZ+omk2AvBuRKi4b2mntcH g6ZTPKPAtWn5APSIWQz0o3rAHx3nMYpq9JNv0PwZPD3q/jb/stS0rLZVC5YG2eT8FVCm a/6sG/PyVmAjDQ54U4GTN5b8ypqJw1WRAzguiVvK09SOurJoaXE5GxcyrL4jWmoLGuth PeFQX4Uei1QUqJa51y/J/x/dvjv8370nMWpCfmz0tS36I7TObdaV5vEYGMsytS4U1vOk xXjk5z3vhtLSsjL0ViUQwAr2yWFca2XRS+M5z3cUCe0/dqJDU1tdBA6GjrJdsaeMzHv6 f9bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=PPJ8oC+N; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s11si20389957plg.9.2021.10.17.07.33.40; Sun, 17 Oct 2021 07:33:52 -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=@google.com header.s=20210112 header.b=PPJ8oC+N; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232884AbhJORgM (ORCPT + 99 others); Fri, 15 Oct 2021 13:36:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232596AbhJORgL (ORCPT ); Fri, 15 Oct 2021 13:36:11 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D42F0C061570 for ; Fri, 15 Oct 2021 10:34:04 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id g36so28005014lfv.3 for ; Fri, 15 Oct 2021 10:34:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=2hjBOvKy6xDlDiNJhW19TOyI4tkya8JrKkq1DPtskBE=; b=PPJ8oC+NOgUO7AJaZAU9oaPGEWQNCKG3btar0K7YPO0GLkb4Y/SjZRsJ7zDMW2BWGU B87xeGg/6pJ6rrBZ9M75M8g+pwchnTq/6F4kYdyFUUtuWucmxUcZ9AGfx/6CESgxxR6Y 5x8Js7R7ncgxcI+p7vpRgAf6kvR9uaqsZuandFHA5hbgRjK7L1+Pck5W+DmsrbeApVV5 mV38sv8gUESwYZLvIF96VLlUFTna8AhqHN14pHatVsYLgZGe7L4RXIfoQCpJuCI8pwSz YJE+rFYCx41SXhGCNu9DL8yI5BIO/mIw9bKeQ2noejlY9AKeoJzFPJ9xM0Z8JJu/krFv OdjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=2hjBOvKy6xDlDiNJhW19TOyI4tkya8JrKkq1DPtskBE=; b=XhnmEOw2eH1cUlyW7irHbQr3/32aPwdnI+ck6K8b400HA7PXwGvUt6HzR1x4iMK6/f 6/uDb9BWA8YTia3+dw8mBowbWqpb1rGib1s08NJZiZe3b6OmavN3s4Ih12pulvKcR5KJ 7zkRmgoyBvdinjkWLb7XMWsTBEJUxba39SAVVdeb9clxbxswYI8bE2nJoPoll+sxrwP1 nhO5jzrxxagmnB9HJdFotGGLqKBGCxP/Tx/o4FnuGQTbkcum+OLjHMMPf7c51cL6LwcZ 1SQ4Wk3QeYkRqmuchOAh62yo22S+BCFZR5VGDTz/gmDUWOaC8SSPyz35zut8HwBtedVX pcEA== X-Gm-Message-State: AOAM531VWuFbmLn4KRvD9iMOwY77zm6fyZzSPTNLwLsmTkv8HFeTZosL g5YIrNjo6E+G1HO6NM89YB2PlTHCUtXhSU1J39Pvuw== X-Received: by 2002:a05:6512:b0a:: with SMTP id w10mr4483529lfu.240.1634319242989; Fri, 15 Oct 2021 10:34:02 -0700 (PDT) MIME-Version: 1.0 References: <20211015090530.2774079-1-linux@rasmusvillemoes.dk> In-Reply-To: <20211015090530.2774079-1-linux@rasmusvillemoes.dk> From: Nick Desaulniers Date: Fri, 15 Oct 2021 10:33:50 -0700 Message-ID: Subject: Re: [PATCH] linux/container_of.h: switch to static_assert To: Rasmus Villemoes Cc: akpm@linux-foundation.org, Miguel Ojeda , Andy Shevchenko , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 15, 2021 at 2:05 AM Rasmus Villemoes wrote: > > _Static_assert() is evaluated already in the compiler's frontend, and > gives a somehat more to-the-point error, compared to the BUILD_BUG_ON > macro, which only fires after the optimizer has had a chance to > eliminate calls to functions marked with > __attribute__((error)). In theory, this might make builds a tiny bit > faster. > > There's also a little less gunk in the error message emitted: > > lib/sort.c: In function =E2=80=98foo=E2=80=99: > ./include/linux/build_bug.h:78:41: error: static assertion failed: "point= er type mismatch in container_of()" > 78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg) > > compared to > > lib/sort.c: In function =E2=80=98foo=E2=80=99: > ././include/linux/compiler_types.h:322:38: error: call to =E2=80=98__comp= iletime_assert_2=E2=80=99 declared with attribute error: pointer type misma= tch in container_of() > 322 | _compiletime_assert(condition, msg, __compiletime_assert_, __COU= NTER__) > > While at it, fix the copy-pasto in container_of_safe(). > > Signed-off-by: Rasmus Villemoes Thanks for the patch! Reviewed-by: Nick Desaulniers > --- > akpm: This is obviously on top of Andy's kernel.h splitup series, so > should go along with those if acked. > > include/linux/container_of.h | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/include/linux/container_of.h b/include/linux/container_of.h > index dd56019838c6..2f4944b791b8 100644 > --- a/include/linux/container_of.h > +++ b/include/linux/container_of.h > @@ -16,9 +16,9 @@ > */ > #define container_of(ptr, type, member) ({ \ > void *__mptr =3D (void *)(ptr); = \ > - BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ > - !__same_type(*(ptr), void), \ > - "pointer type mismatch in container_of()"); \ > + static_assert(__same_type(*(ptr), ((type *)0)->member) || \ > + __same_type(*(ptr), void), \ > + "pointer type mismatch in container_of()"); \ > ((type *)(__mptr - offsetof(type, member))); }) > > /** > @@ -31,9 +31,9 @@ > */ > #define container_of_safe(ptr, type, member) ({ = \ > void *__mptr =3D (void *)(ptr); = \ > - BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ > - !__same_type(*(ptr), void), \ > - "pointer type mismatch in container_of()"); \ > + static_assert(__same_type(*(ptr), ((type *)0)->member) || \ > + __same_type(*(ptr), void), \ > + "pointer type mismatch in container_of_safe()"); \ > IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \ > ((type *)(__mptr - offsetof(type, member))); }) > > -- > 2.31.1 > --=20 Thanks, ~Nick Desaulniers