Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp501962pxb; Fri, 15 Oct 2021 09:49:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwvDi07UbEpgrTA2/TM/1Yob4zwFF4BU5gixDp+iAIp0xRocukiTe/voZQYcRfAswbs+sDD X-Received: by 2002:aa7:cb92:: with SMTP id r18mr19533268edt.282.1634316586990; Fri, 15 Oct 2021 09:49:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634316586; cv=none; d=google.com; s=arc-20160816; b=Awn2Xl4mtJEilw8IspI5CfcoJ/IWCJPawcF99AnGKovzeC5mlHdsFCrEIwLKp2ihHN oWAorT0nkDIss9iQwEW9pbXr8HjeH/VAWPEI20qwwHXfeSJYCBU/z32U8i2ITbxAO/hI IkBw9y8Zkrg65T0uO22ufpZzfR4uryVxu3mXnhhb66gCWoAgnsv9x9Jx8878+hBl33lf H6qXHYVfIB1FOobaerxzA6Y4HzjCwp36sdJQEXXgXGeiQnOuzeANwqmPxlXH5yBQq+2z wY6Ei/7hZRCJDIP/njRLpTywLQr643FYT57j6XhxdxRDyVSuUTm+iHCYFl3EgbrhgjLw ftqA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=rmzSrjTAZEQ7N3R2i2Z86dnNVR/pshIwOWi26WbD4R0=; b=m4qilZwdWYIOGVwP10yJDYuWfZTdaYN38kgE3mfhX4EKMGNCw8ipdATRDI35N6pQ/7 PREtbIBqXkxxpo7OoWvgPYE2kGtj1S3e3Pi13qqKTOFwqIborLCm4ejrJ5HX6XMQpTCD dSrYLxRr/hcR7/i2IqCcM2qTD+aEeMYdKEdZspLTyqO96TWvKPCagk0yzEWp/VXAZMkn ax0gkIUXxZzhtv/A57Ds7gJP3WIWPt2sCD1faOVQtgqEOGsQrXblYR37ZX1+9Vjkx2LG VF6+IPfohmSe/61EJkxL6nDtQRtyE6vvdW0R8UpUt4H1KOuSlmxvXbbKqU1EYXG82chH 4x2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=OXrBFUPY; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y5si10813991edc.106.2021.10.15.09.49.21; Fri, 15 Oct 2021 09:49:46 -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=@rasmusvillemoes.dk header.s=google header.b=OXrBFUPY; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236340AbhJOJHp (ORCPT + 99 others); Fri, 15 Oct 2021 05:07:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236176AbhJOJHp (ORCPT ); Fri, 15 Oct 2021 05:07:45 -0400 Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A023DC061570 for ; Fri, 15 Oct 2021 02:05:38 -0700 (PDT) Received: by mail-lf1-x12c.google.com with SMTP id j21so39320894lfe.0 for ; Fri, 15 Oct 2021 02:05:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=rmzSrjTAZEQ7N3R2i2Z86dnNVR/pshIwOWi26WbD4R0=; b=OXrBFUPYT5iXWiAtLLHhBy7KA/lg9vqaQ4jvCMSvyFgtnq5wdPw3ORmv8IYKQglzJQ KJSYvutHRRjpXPYPUua+Bd64k0nlsvuczjMXRCE6DEx0yjDLHxj0PUNHqZSllUwJawXE 2W1W/G8Wr3Zx1iLn9mToAIvsEbMNP4+nsBpGQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=rmzSrjTAZEQ7N3R2i2Z86dnNVR/pshIwOWi26WbD4R0=; b=GHCkdKNE91g4xInD2GCyGSJaEfId2pxVAmgTpzFvgc2kNYhljbS7A7AefeV+p0U4oF Qf2O4Cchn6NjPxNuXouL7Nbb7viuoMIw2k8ceAvhFDLP7wnkKghAfk199/1WmIsGU4sY L8yzASz3JUpIbNLuQb4lhw//7QCUTzYkQvbMVZVlUryS3GE1zrtKL5JwPGMsoB5i/sBY ixm2aXpiwxXDBvpGLSvH7UdoKNbfQPKz9HEr1pC1cybFKCD/sRpkT8BktGSaZWMDIhep 2iPrKSJ7lVDAKwyUyWWlgWdWmCX8EcnMjwFenM8N9wSSlu8aZzAWZ0bcCkF4bMV0keJT V/Ww== X-Gm-Message-State: AOAM532Rwk+WIHD56D1Gi8s5ESgDIweMbDltNyJidfM0+tadEbNFrpig AB5iQPktVyWiKEBlsjaBtei0zw== X-Received: by 2002:a05:6512:338b:: with SMTP id h11mr10530600lfg.310.1634288736951; Fri, 15 Oct 2021 02:05:36 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id v12sm491708ljp.124.2021.10.15.02.05.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Oct 2021 02:05:36 -0700 (PDT) From: Rasmus Villemoes To: akpm@linux-foundation.org Cc: Miguel Ojeda , Andy Shevchenko , Nick Desaulniers , Rasmus Villemoes , linux-kernel@vger.kernel.org Subject: [PATCH] linux/container_of.h: switch to static_assert Date: Fri, 15 Oct 2021 11:05:30 +0200 Message-Id: <20211015090530.2774079-1-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org _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 ‘foo’: ./include/linux/build_bug.h:78:41: error: static assertion failed: "pointer type mismatch in container_of()" 78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg) compared to lib/sort.c: In function ‘foo’: ././include/linux/compiler_types.h:322:38: error: call to ‘__compiletime_assert_2’ declared with attribute error: pointer type mismatch in container_of() 322 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) While at it, fix the copy-pasto in container_of_safe(). Signed-off-by: Rasmus Villemoes --- 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 = (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 = (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