Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6528757rwb; Mon, 5 Dec 2022 13:46:32 -0800 (PST) X-Google-Smtp-Source: AA0mqf7bhy/x9o8jd1D/cWEwuOIrwFeTnbktPqMOZur8HGW1/OtACBHN8D7itcmTTRBAFkNlRnV0 X-Received: by 2002:a05:6402:230f:b0:46c:dc40:4635 with SMTP id l15-20020a056402230f00b0046cdc404635mr3800290eda.358.1670276792172; Mon, 05 Dec 2022 13:46:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670276792; cv=none; d=google.com; s=arc-20160816; b=MrSqMNaqGXwLPQrpRbbJntqu65Hi5brtmKwMPxJJP9OC/g7mxgfUoQVVnX5rs5sIfP VmLQvgCPcnH25JJ3efSUUgSjAK0sNKTa8/gXoBVc9+AUFfZsjpzX7a8BJ4ZjJfCosQVY NaZTO8xXz2uCVNGw4GLdXFxXB9mS2QbqNoWmbm2h556zTPYZm59/fyMV4J0WWE2JC/vd NWVOiK6+cTNocNVwz9Wt5VMzY6VFf3MpB2m3o6IXqs+edY8QKL8CN32R0M15SqND6wAG bkTJghNkKolgt22/08gfz4bwAWdzzWgubnajX9ZRNrRTf3vqL4Fbls9BTMDQzj4FHlUk z9jA== 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=ntmTz5s5kqaaNDmzhr5t2kTVSZ27ZYHTcXaBrqRxK4I=; b=qk2+1uJjosjxM/urXTG0pJo+/jaJROOVRtd21zwa7wh1RmNynlM7zd1SofyTko7sK2 NTRvTyqwPUickF5lsNUXPhm8sTbJ5zTbXJx2ZqlLdiI1Waoq2+8Av8Ruv0kstH6A9I70 VCeQdnaSCGpmg/X8NvsQU/K6d2kxZUpZT28ofGrZGJUpIf0kaD3W4mPPsxw/grYe5XcE 8Hfd7RphY2+Mb7pBm/txlFYTfyJKvP+qTZ3dsyWfXeUI2z1h0v8Qc063tSGvjjA/yGJp hohKm7l4zH1Jo6JLCssa/xlY1qKyzRnq5MynL4IpnVjNGK7qZty1xODidwW5ka3l5uu5 6+Xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="dodniG/3"; 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 hq2-20020a1709073f0200b007ad9f041c3bsi13670944ejc.27.2022.12.05.13.46.12; Mon, 05 Dec 2022 13:46:32 -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="dodniG/3"; 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 S233781AbiLEVYy (ORCPT + 80 others); Mon, 5 Dec 2022 16:24:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233320AbiLEVYc (ORCPT ); Mon, 5 Dec 2022 16:24:32 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10A412CC9F for ; Mon, 5 Dec 2022 13:24:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670275461; x=1701811461; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=JKW51AkPX6aovqq8UwYtair/CStxY2nC7HiEsg6lAUo=; b=dodniG/3sWa7pzG9TCcLxMHpy/BC+tnYIyRbo4zaj9nROFI2edWIVd+X AcSoFT/eDV6F0SC4R2Vo9R4P/0zvur0tAPDjtsfwiCPsPxNATxPVM8oHz LVpfyF6BdSu7JUmTajuCMUXIRifIpA//kTBoPye81iZ5lsWv+sniklsIK 7/QpR3xNgpqMIvakcNGLYdIzuNLh/LNU50FgUGhzYImk14Lu7iBRp3qT3 9WR8GK8T77650UUWpsb3EoSn/TrvWLm/zcaesITEKA7esofvmjs61UGIc YOzNt+ffMVo8aGJsXLl/MGuioyJabR0vcqin/OwGnSVu//CWMTCSYADgz Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10552"; a="318327682" X-IronPort-AV: E=Sophos;i="5.96,220,1665471600"; d="scan'208";a="318327682" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2022 13:24:18 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10552"; a="623657701" X-IronPort-AV: E=Sophos;i="5.96,220,1665471600"; d="scan'208";a="623657701" Received: from punajuuri.fi.intel.com (HELO paasikivi.fi.intel.com) ([10.237.72.43]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2022 13:24:16 -0800 Received: from paasikivi.fi.intel.com (localhost [127.0.0.1]) by paasikivi.fi.intel.com (Postfix) with SMTP id EB94A20363; Mon, 5 Dec 2022 23:24:13 +0200 (EET) Date: Mon, 5 Dec 2022 21:24:13 +0000 From: Sakari Ailus To: Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, Jason Gunthorpe , Jason Gunthorpe , Andy Shevchenko , Matthew Wilcox , "Rafael J. Wysocki" Subject: Re: [PATCH v2 1/4] container_of: add container_of_const() that preserves const-ness of the pointer Message-ID: References: <20221205121206.166576-1-gregkh@linuxfoundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221205121206.166576-1-gregkh@linuxfoundation.org> 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 Hi Greg, On Mon, Dec 05, 2022 at 01:12:03PM +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. > > Suggested-by: Jason Gunthorpe > Suggested-by: Sakari Ailus > Reviewed-by: Jason Gunthorpe > Reviewed-by: Andy Shevchenko > Cc: Matthew Wilcox > Cc: "Rafael J. Wysocki" > Signed-off-by: Greg Kroah-Hartman > --- > v2: - removed one parameter, now matches container_of(), thanks to > suggestion from Sakari > - changed Jason's tag to suggested-by and reviewed-by > - added Andy's tag Thanks for the update. For the set: Reviewed-by: Sakari Ailus > > include/linux/container_of.h | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/include/linux/container_of.h b/include/linux/container_of.h > index 2008e9f4058c..1d898f9158b4 100644 > --- a/include/linux/container_of.h > +++ b/include/linux/container_of.h > @@ -22,4 +22,17 @@ > "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: 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_const(ptr, type, member) \ > + _Generic(ptr, \ > + const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\ > + default: ((type *)container_of(ptr, type, member)) \ > + ) > + > #endif /* _LINUX_CONTAINER_OF_H */ -- Kind regards, Sakari Ailus