Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1279703rwb; Thu, 1 Dec 2022 15:10:04 -0800 (PST) X-Google-Smtp-Source: AA0mqf7wQwSjPLuDtXJ26nhCSz50vejjxmBzPoc0u7crJTjE09Gnj2qCjRh0ckOxyJkMv+JeB1QU X-Received: by 2002:a17:906:dfc1:b0:78d:894d:e123 with SMTP id jt1-20020a170906dfc100b0078d894de123mr7157388ejc.112.1669936204717; Thu, 01 Dec 2022 15:10:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669936204; cv=none; d=google.com; s=arc-20160816; b=HYi7rb1CBAOdVFjL+tfMnROk/T8tXPF25Rzz2dqMIkLoYjzA1hF0FsmMNmLN163QU6 /lX5m/LLUDU+53L5JN8BDC0AN5jS3m3UjMglcZEXGHQpe3tvs4ckXUI4e53DJcm5cFew k6jbqHR8OEyfQSzxdIkj1R3kiN8gZMJgf9sVeg4HCuIGOcMDNmqEWHqe5MLFmQKdVNZl k9dCmUCFJhZngUaxJm1pmNws3CP7LBGFjHggbhS5aI8PGLawuPCmpbYvqni5npc4UsUa fH6c/5nSMdhJSidCcOvEb6Ujy1wY01+yQHLkrY/1diIleG3y+N6shrRsFjyr0eWQA2K+ LpzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:organization:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=oNoQdqkmJqetnHdDRT/e5Ca/97a5Mnt+SWvqGBtkp1U=; b=vsjR1p8tvZi1I8NVRYIg5sapsYDSyG47GQMikdyZ8icPp5cIOFilzia3p4zZcvmix+ I9c2g6k31xJKqxV3qxjSiazvED51ksYV4XaQ6xZuyMHzoNIp8bx1uICjU2SevvOOF4c0 5rZ3Ncj0v6JEQal/S8suGDYamvx40olSCfhCKGzkKgL+YWcSjthp/XqThBrwB63idi3h 2e0XVhDCbz1Ruq7JuM1cink05EOQ9V8aKArn5zN2ppmNUVRo6ZebyjC4htub4tFzk7W0 l3tDI5Zs0W8Wt3TUvghfgvyeIRmGKL5kJxIbVjJadRExMdmajQe7e49V9a45zESyNcE7 yJiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YlguOzN7; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l15-20020a170906794f00b0078da3218b54si5303540ejo.171.2022.12.01.15.09.43; Thu, 01 Dec 2022 15:10:04 -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=@intel.com header.s=Intel header.b=YlguOzN7; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231707AbiLAWv2 (ORCPT + 82 others); Thu, 1 Dec 2022 17:51:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231207AbiLAWu4 (ORCPT ); Thu, 1 Dec 2022 17:50:56 -0500 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81AB8CA783 for ; Thu, 1 Dec 2022 14:50:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1669935009; x=1701471009; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=UeIy8pAdmrZ3lUzO5VL6SSZX6v4M9UmNVytJ9UmGaPM=; b=YlguOzN7yotgQRHkqfL2w3G704+5m9S2qAH5SfNh+P7K+L+TJI9ikvyX s0l8uiRtpyxV9UmmyGj0t/5m4snp36J819SMuWC5fQS7m8/vJhqyGLZPd Hb5RgKXnNJEUFio/aWjOHTUTkB6aksKRWDDwdxhivIC3WSyAj9hd3B6uP NIua1V6SBHcAoClGlcWRzHtbMmd4oHQCB/IlHtSXFPpnpFQHI87bOMm1U aHmFyH1D9eEpwi/Ni606Cn8DF8C+2cH0w7Sz55qldDIQKBzTPzgTnSvBC IGEv/TN1pWIL/y9HzT/0zN8hWy2GwE4xVJEZFEJ5/CU44dwVMzRb60m5G w==; X-IronPort-AV: E=McAfee;i="6500,9779,10548"; a="377974544" X-IronPort-AV: E=Sophos;i="5.96,210,1665471600"; d="scan'208";a="377974544" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2022 14:50:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10548"; a="819222654" X-IronPort-AV: E=Sophos;i="5.96,210,1665471600"; d="scan'208";a="819222654" Received: from smile.fi.intel.com ([10.237.72.54]) by orsmga005.jf.intel.com with ESMTP; 01 Dec 2022 14:50:07 -0800 Received: from andy by smile.fi.intel.com with local (Exim 4.96) (envelope-from ) id 1p0sNZ-0036pP-1N; Fri, 02 Dec 2022 00:50:05 +0200 Date: Fri, 2 Dec 2022 00:50:05 +0200 From: Andy Shevchenko To: Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, Jason Gunthorpe , Matthew Wilcox , Sakari Ailus , "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: <20221201193057.1195255-1-gregkh@linuxfoundation.org> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE 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 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 I believe this tag requires SoB of the co-develper. The code looks good to me, thanks! Reviewed-by: Andy Shevchenko > 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) \ > + _Generic(ptr, \ > + const ptr_type *: ((const member_type *)container_of(ptr, member_type, member)),\ > + ptr_type *: ((member_type *)container_of(ptr, member_type, member)) \ > + ) > + > #endif /* _LINUX_CONTAINER_OF_H */ > -- > 2.38.1 > -- With Best Regards, Andy Shevchenko