Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2386595rwb; Fri, 2 Dec 2022 09:06:05 -0800 (PST) X-Google-Smtp-Source: AA0mqf6yhhV1NxCoSp3IcMT8EDoEW1bX9VQKYxqP6A0JKuIKpsLfC6VLB9GIgjEDZBzvNHvXlVMH X-Received: by 2002:a05:6402:6c6:b0:46c:1326:e186 with SMTP id n6-20020a05640206c600b0046c1326e186mr4825926edy.228.1670000765063; Fri, 02 Dec 2022 09:06:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670000765; cv=none; d=google.com; s=arc-20160816; b=tVLE5565HAxsBqpTi4zE+hqCNsNVmztJ4GThAdf3/96U6aVsnL2g7mw4fS2zH4lQWj hfWD4keh1/mZjwAWbQ6mc1PSHUX6s3OhxAvEBOm+89O+bLmD/U0pgYKsZps1bGhRqZnN j0Uu1TuZmJAMC4bhETPL/c8DA5zOLING3HxWBZ6Db+3n7PAUhEmzfkEUbrGEIP1lBgSc tm8drmHne0I8Y94LkL+xa4b+egZRnxChYs/aeCuiyPFTdaVXZ3ALPXmlnyIU/xv4DB3a llVS+8+x4e9hAZSUB+Svdci60av0n1Gj5hbssPqt+T8MrH6HNvdrNAeFF0yulFSw2nq6 xwqg== 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=jXQ1NuSK6x8X1nTxBkEysjTeh/eGJ3XstiRsr6UCrqE=; b=FkXRneWzR+9vzofiRKOMuaFCMXEKq9phmbqqHIazOlnEK1pu78Wxei0MCRADObNgLq deN3d6kZWv46ixaj68ayB5IiGZj8csAplaRVlWP8qUYF5SWZ9vY9RUg4+G0gUCGIg9Jy OtiRbVyYNh1e4sx8CxdBhXveECYGSKnG5O56y3VQV1BLnHGLLTMlRiuUTI6VedENWUPN 5QxM9QNQIBZlSbsrq2bnOQf8s5y8gqtBOxna7JI5S4+nHbBV9i3+3NHnbuz4e9oiwbiW bmidWPA5lN9TC46ffE5yQWgaHRw3ONc9m1/Vd0D/fCXsMoThqanv00qr0RY30EPdTiQ8 145w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xWL54NQ1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s21-20020a056402521500b00457263f9ee0si2112212edd.93.2022.12.02.09.05.43; Fri, 02 Dec 2022 09:06:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xWL54NQ1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233709AbiLBQZs (ORCPT + 83 others); Fri, 2 Dec 2022 11:25:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233497AbiLBQZ3 (ORCPT ); Fri, 2 Dec 2022 11:25:29 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF1FD101CC for ; Fri, 2 Dec 2022 08:24:47 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 84FBEB821E6 for ; Fri, 2 Dec 2022 16:24:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C19B9C433D6; Fri, 2 Dec 2022 16:24:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1669998285; bh=uqX5oHSQxePTMTDs8aLN1oxheczLbhv31aeoAxjDn7k=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=xWL54NQ1XK/WH7crr7nzwjBZmAc4cSwOcqBWBGR4+xJlnc6jiBafNIFUa6gpRNGMR SLBFrXVXIsHcY549QNioHwVK8eEQ186GpWuEs2x5hTNxW2J3rm9s4RNlwyj8BENHIC k2NJO/IDo59awXns49NxumBySXb7cnJuIxqpkOY4= Date: Fri, 2 Dec 2022 17:24:42 +0100 From: Greg Kroah-Hartman To: Sakari Ailus Cc: linux-kernel@vger.kernel.org, Jason Gunthorpe , Matthew Wilcox , Andy Shevchenko , "Rafael J. Wysocki" Subject: Re: [PATCH 1/4] container_of: add container_of_const() that preserves const-ness of the pointer Message-ID: References: <20221201193057.1195255-1-gregkh@linuxfoundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Dec 02, 2022 at 12:48:57PM +0000, Sakari Ailus wrote: > Hi Greg, > > On Thu, Dec 01, 2022 at 08:30:54PM +0100, Greg Kroah-Hartman wrote: > > container_of does not preserve the const-ness of a pointer that is > > passed into it, which can cause C code that passes in a const pointer to > > get a pointer back that is not const and then scribble all over the data > > in it. To prevent this, container_of_const() will preserve the const > > status of the pointer passed into it using the newly available _Generic() > > method. > > > > Co-developed-by: Jason Gunthorpe > > Cc: Matthew Wilcox > > Cc: Sakari Ailus > > Cc: Andy Shevchenko > > Cc: "Rafael J. Wysocki" > > Signed-off-by: Greg Kroah-Hartman > > --- > > include/linux/container_of.h | 14 ++++++++++++++ > > 1 file changed, 14 insertions(+) > > > > diff --git a/include/linux/container_of.h b/include/linux/container_of.h > > index 2008e9f4058c..3c290e865151 100644 > > --- a/include/linux/container_of.h > > +++ b/include/linux/container_of.h > > @@ -22,4 +22,18 @@ > > "pointer type mismatch in container_of()"); \ > > ((type *)(__mptr - offsetof(type, member))); }) > > > > +/** > > + * container_of_const - cast a member of a structure out to the containing > > + * structure and preserve the const-ness of the pointer > > + * @ptr_type: the type of the pointer @ptr > > + * @ptr: the pointer to the member > > + * @member_type: the type of the container struct this is embedded in. > > + * @member: the name of the member within the struct. > > + */ > > +#define container_of_const(ptr_type, ptr, member_type, member) \ > > I missed earlier you had four arguments for the macro instead of three. > > With default: this can be done with just three: > > #define container_of_const(ptr, member_type, member) \ > _Generic(ptr, \ > const typeof(*(ptr)) *: ((const member_type *)container_of(ptr, member_type, member)),\ > default: ((member_type *)container_of(ptr, member_type, member)) \ > ) > > The const typeof(*(ptr)) * will match if ptr is const, otherwise default > matches. I had tried to use typeof before, your way is easier, thanks, I couldn't get it to work myself... > But you can't have typeof(*(ptr)) * instead of default as the two > types are still the same, hence default. Ah. Ok, I really didn't want to use default, and that's probably why it wasn't working for me. > I've tested this on GCC 10.2.1 and clang 11.0.1. > > This should also make it a bit easier for existing users to switch to the > new macro and hopefully eventual rename back to container_of() once all > users have been converted. True. Ok, I'll try this later tomorrow and send out a new version if all goes well, thanks! greg k-h