From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752802AbdESMHV (ORCPT ); Fri, 19 May 2017 08:07:21 -0400 Received: from mga02.intel.com ([134.134.136.20]:19147 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750828AbdESMHR (ORCPT ); Fri, 19 May 2017 08:07:17 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.38,363,1491289200"; d="scan'208";a="859027986" From: Sakari Ailus To: linux-kernel@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com Subject: [RFC 1/1] linux/kernel.h: const-safe container_of() macro Date: Fri, 19 May 2017 15:06:10 +0300 Message-Id: <1495195570-5249-1-git-send-email-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Make container_of() macro const-safe by using _Generic selections. It's easy to mistakenly pass a const ptr argument to container_of() and use a non-const type for the container struct, which results in a non-const reference to a struct that used to be const. This type of errors are currently not flagged during compilation. Another useful case for this is to provide a const-safe conversion between different structs such as OF specific struct device_node and the more generic struct fwnode_handle. const-ness of the argument is preserved in the conversion and const warnings will be flagged by the compiler. The "default" selection will match if the ptr argument is not const. The "const typeof(*(ptr)) *" selection will match otherwise: constifying a const type does not change it. Signed-off-by: Sakari Ailus --- Hi folks, This patch currently triggers a large number of compiler warnings and not necessarily all of the cases are at fault but nevertheless this suggests that there could be issues. I haven't checked most of the locations where the warnings are emitted and rather wanted to ask about the approach. Another option to support e.g. const-safe conversion could be to add a new macro and leave the existing one as-is. This has the drawback of not drawing attention to potential const-correctness issues. Regards, Sakari include/linux/kernel.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 13bc08a..ddd4987 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -850,10 +850,20 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } * @member: the name of the member within the struct. * */ -#define container_of(ptr, type, member) ({ \ +#define ____container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) +#if GCC_VERSION >= 40900 /* GCC 4.9 added support for _Generic */ +#define container_of(ptr, type, member) \ + _Generic((ptr), \ + default: ____container_of(ptr, type, member), \ + const typeof(*(ptr)) *: ____container_of(ptr, const type, \ + member)) +#else +#define container_of(ptr, type, member) ____container_of(ptr, type, member) +#endif + /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ #ifdef CONFIG_FTRACE_MCOUNT_RECORD # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD -- 2.7.4