Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BCA65EE4993 for ; Mon, 21 Aug 2023 16:26:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692635166; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=ePMNkGDJp3CTukJNWSHboor0nzdaFYmCYUO8dSH0x7E=; b=TX/ha8jBhVVVWjH0ESfdUHYC9epTYu4BA3rdw6qsiQrggtN6TEJkzOC41AiaylwJIeVksR VReaTjXJI5kx7DHPjHgRErh7iIW/YNwHziqEDsLiaJjcmj0X5zNGemU7bwRfa+xrM6vN8O orQFQ+YlU3zBCSsEVvMvKSjN2FODM6c= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-423-dsrSgsx2OG6x7zL4kXQCxQ-1; Mon, 21 Aug 2023 12:26:01 -0400 X-MC-Unique: dsrSgsx2OG6x7zL4kXQCxQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B06C8185A78F; Mon, 21 Aug 2023 16:26:00 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id A4B161410F02; Mon, 21 Aug 2023 16:26:00 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 645651946588; Mon, 21 Aug 2023 16:25:50 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id D48E21946586 for ; Mon, 21 Aug 2023 16:25:48 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id C107E492C1B; Mon, 21 Aug 2023 16:25:48 +0000 (UTC) Received: from fs-i40c-03.fs.lab.eng.bos.redhat.com (fs-i40c-03.fs.lab.eng.bos.redhat.com [10.16.224.23]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9C85A492C13; Mon, 21 Aug 2023 16:25:48 +0000 (UTC) From: Alexander Aring To: teigland@redhat.com Date: Mon, 21 Aug 2023 12:25:39 -0400 Message-Id: <20230821162540.594227-2-aahringo@redhat.com> In-Reply-To: <20230821162540.594227-1-aahringo@redhat.com> References: <20230821162540.594227-1-aahringo@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Subject: [Cluster-devel] [PATCH dlm-tool 2/3] dlm_controld: update container_of() implementation X-BeenThere: cluster-devel@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: "\[Cluster devel\]" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: cluster-devel@redhat.com Errors-To: cluster-devel-bounces@redhat.com Sender: "Cluster-devel" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch updates the container_of() implementation taken from the Linux kernel. We need to turn off -Wpointer-arith as the new container_of() implementation does void pointer artihmetic as shown by this example warning: linux_helpers.h:43:26: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith] --- dlm_controld/Makefile | 2 +- dlm_controld/linux_helpers.h | 36 ++++++++++++++++++++++++++++++++++++ dlm_controld/list.h | 12 +----------- dlm_controld/rbtree.h | 2 +- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/dlm_controld/Makefile b/dlm_controld/Makefile index 28b6bb0a..328a64ba 100644 --- a/dlm_controld/Makefile +++ b/dlm_controld/Makefile @@ -38,7 +38,7 @@ LIB_SOURCE = lib.c CFLAGS += -D_GNU_SOURCE -O2 -ggdb \ -Wall -Wformat -Wformat-security -Wmissing-prototypes -Wnested-externs \ - -Wpointer-arith -Wextra -Wshadow -Wcast-align -Wwrite-strings \ + -Wextra -Wshadow -Wcast-align -Wwrite-strings \ -Waggregate-return -Wstrict-prototypes -Winline -Wredundant-decls \ -Wno-sign-compare -Wno-unused-parameter -Wp,-D_FORTIFY_SOURCE=2 \ -fexceptions -fasynchronous-unwind-tables -fdiagnostics-show-option \ diff --git a/dlm_controld/linux_helpers.h b/dlm_controld/linux_helpers.h index 5ef13466..09705cff 100644 --- a/dlm_controld/linux_helpers.h +++ b/dlm_controld/linux_helpers.h @@ -3,6 +3,42 @@ #ifndef __DLM_LINUX_HELPERS__ #define __DLM_LINUX_HELPERS__ +/* + * static_assert - check integer constant expression at build time + * + * static_assert() is a wrapper for the C11 _Static_assert, with a + * little macro magic to make the message optional (defaulting to the + * stringification of the tested expression). + * + * Contrary to BUILD_BUG_ON(), static_assert() can be used at global + * scope, but requires the expression to be an integer constant + * expression (i.e., it is not enough that __builtin_constant_p() is + * true for expr). + * + * Also note that BUILD_BUG_ON() fails the build if the condition is + * true, while static_assert() fails the build if the expression is + * false. + */ +#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr) +#define __static_assert(expr, msg, ...) _Static_assert(expr, msg) + +#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) + +/** + * container_of - cast a member of a structure out to the containing structure + * @ptr: the pointer to the member. + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + * + * WARNING: any const qualifier of @ptr is lost. + */ +#define container_of(ptr, type, member) ({ \ + void *__mptr = (void *)(ptr); \ + static_assert(__same_type(*(ptr), ((type *)0)->member) || \ + __same_type(*(ptr), void), \ + "pointer type mismatch in container_of()"); \ + ((type *)(__mptr - offsetof(type, member))); }) + #define WRITE_ONCE(x, val) \ do { \ *(volatile typeof(x) *)&(x) = (val); \ diff --git a/dlm_controld/list.h b/dlm_controld/list.h index a2a5e5f3..e9df2ef2 100644 --- a/dlm_controld/list.h +++ b/dlm_controld/list.h @@ -3,17 +3,7 @@ #ifndef _LINUX_LIST_H #define _LINUX_LIST_H -/** - * container_of - cast a member of a structure out to the containing structure - * - * @ptr: the pointer to the member. - * @type: the type of the container struct this is embedded in. - * @member: the name of the member within the struct. - * - */ -#define container_of(ptr, type, member) ({ \ - const typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) );}) +#include "linux_helpers.h" /* * These are non-NULL pointers that will result in page faults diff --git a/dlm_controld/rbtree.h b/dlm_controld/rbtree.h index ddb86ff6..48b6e32f 100644 --- a/dlm_controld/rbtree.h +++ b/dlm_controld/rbtree.h @@ -20,7 +20,7 @@ #include #include -#include "list.h" +#include "linux_helpers.h" #include "rbtree_types.h" #define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3)) -- 2.31.1