Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp6576893rwi; Mon, 24 Oct 2022 03:25:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM77SrfAfIKm7fayw+DzBmNaLo7hNIX8a4tbPX17jx7VQky7C6vvVJG70HHin/DdU9DpGd8R X-Received: by 2002:a17:90b:4d8d:b0:20a:ad78:7826 with SMTP id oj13-20020a17090b4d8d00b0020aad787826mr37532000pjb.237.1666607102165; Mon, 24 Oct 2022 03:25:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666607102; cv=none; d=google.com; s=arc-20160816; b=D+fivNoQHerJe7PZePXOelVC6eOaIN+bKdw61ZjjblE/0scCrNMKywVj7PcV/D7iqu oZ0twUbGg9qi0OovBn7RFQxfUr9Oz1M6cxP5SgQyx8dDGxQK/2GcMLNJZWQ1tRJwMGAt 29FRjRSh/6iDtN21BeOmXdtKEUDoRsdzft/qT711Vv63qzcUmXNdqAFZnbdLPkmSEcob d3o7iqnZLZ9Akg5AsvxdJLWoJe+tEL5/Fn/Mx+QT+86lA+cdcorv9RRzP1f78xHwcKaW J2vsdQ0znBAp2xpl+n6q8NZO8mNjYKJyxg9exL20Nrb9vxcJiQ9UM6FQswtTxmd/J6eb Y+WA== 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=X5wPKtSRIc7nFtJKjhLmufwphBMvOri59mNIfH+Yh88=; b=sE6FL9PuhKgixqL0q/XPR8pALvCvbWTz/wq4YOFi+ofI8IhDPZBoMH/Drp8CdnC/qo T6icCvANBs0coA6n2d4gH+rOkWEj2NPe/kuGz6v2kYJiS1HhjqpAFbQDmhfWyG6axM9T 5iIhYoqnKCqhLCwo9kVdSuybBghSGoBYeQlKgaNRW03swhXpAow4n3W7U/nRulFwnPaB OuupVH7bdHGc9YMINZUmXEOtV3mgiMwTOtG4RI1j+tK9HVu9qblfcJNy7yTOXRO/HMI9 xftmlTEemDwBwhqnLHfQsTQ178886klvJURCCaxXPT/4cYjT2znKipKhdsqY9mIpNME9 B2kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XakPhmME; 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 ls9-20020a17090b350900b00213255221e3si1036149pjb.1.2022.10.24.03.24.50; Mon, 24 Oct 2022 03:25:02 -0700 (PDT) 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=XakPhmME; 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 S229661AbiJXJsj (ORCPT + 99 others); Mon, 24 Oct 2022 05:48:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230293AbiJXJsi (ORCPT ); Mon, 24 Oct 2022 05:48:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25AFD2E691 for ; Mon, 24 Oct 2022 02:48:36 -0700 (PDT) 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 dfw.source.kernel.org (Postfix) with ESMTPS id AC7AD6114F for ; Mon, 24 Oct 2022 09:48:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9491EC433D6; Mon, 24 Oct 2022 09:48:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666604915; bh=zsSgP6O6PMReNmIT7HnjsUjfYy70VyQ1SSf+r1MAStY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=XakPhmMEFKa1uaAqTpxO3pwM2lhYISY5KdvvbLy8RckypSErDZpgGwG6dtLqXuUpM cFCy2NUc6bhBAz4iVcr5Ud82GTz6G2J/wNNcFIFOPuf0hIgYFRaxES6+qQWdNumS4o X6B8fBL09OfDlSiAU4ZyjezmidAmEVpOIYwhZ1mw= Date: Mon, 24 Oct 2022 11:48:32 +0200 From: Greg Kroah-Hartman To: Sakari Ailus Cc: linux-kernel@vger.kernel.org, Andy Shevchenko , "Rafael J. Wysocki" , David Laight Subject: Re: [PATCH 1/1] linux/container_of.h: Warn about loss of constness Message-ID: References: <20221024082610.74990-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.6 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 Mon, Oct 24, 2022 at 09:11:53AM +0000, Sakari Ailus wrote: > Hi Greg, > > Thanks for the comments. > > On Mon, Oct 24, 2022 at 10:45:25AM +0200, Greg Kroah-Hartman wrote: > > On Mon, Oct 24, 2022 at 10:43:52AM +0200, Greg Kroah-Hartman wrote: > > > On Mon, Oct 24, 2022 at 11:26:10AM +0300, Sakari Ailus wrote: > > > > container_of() casts the original type to another which leads to the loss > > > > of the const qualifier if it is not specified in the caller-provided type. > > > > This easily leads to container_of() returning a non-const pointer to a > > > > const struct which the C compiler does not warn about. > > > > > > > > Signed-off-by: Sakari Ailus > > > > --- > > > > include/linux/container_of.h | 9 +++++++++ > > > > 1 file changed, 9 insertions(+) > > > > > > > > diff --git a/include/linux/container_of.h b/include/linux/container_of.h > > > > index 2f4944b791b81..c7c21d0f41a87 100644 > > > > --- a/include/linux/container_of.h > > > > +++ b/include/linux/container_of.h > > > > @@ -13,6 +13,10 @@ > > > > * @type: the type of the container struct this is embedded in. > > > > * @member: the name of the member within the struct. > > > > * > > > > + * WARNING: as container_of() casts the given struct to another, also the > > > > > > No need for "also" here (sorry for the grammar nit.) > > > > > > > + * possible const qualifier of @ptr is lost unless it is also specified in > > > > + * @type. This is not a problem if the containing object is not const. Use with > > > > + * care. > > > > > > I do not think these last two sentences you added here are needed > > > either. > > > > > > > > > > */ > > > > #define container_of(ptr, type, member) ({ \ > > > > void *__mptr = (void *)(ptr); \ > > > > @@ -27,6 +31,11 @@ > > > > * @type: the type of the container struct this is embedded in. > > > > * @member: the name of the member within the struct. > > > > * > > > > + * WARNING: as container_of() casts the given struct to another, also the > > > > Wrong function name here. > > I'll address this and the other two issues above in v2. > > > > > > > + * possible const qualifier of @ptr is lost unless it is also specified in > > > > + * @type. This is not a problem if the containing object is not const. Use with > > > > + * care. > > > > > > Same comments here. > > > > Wait, no one uses this macro, so why not just remove it entirely? > > Good question. It appears to be a (relatively) common pattern to look up > something and the return its containing object if the lookup was > successful. Doing a quick > > $ git grep 'container_of.*:' drivers include And odds are, they all are wrong. Any function that has a pointer sent to it that it wants to then cast out to the outer size of the structure has to implicitly know that this is a valid pointer. There's no way to check so you have to trust the fact that the caller sent you the right thing. Trying to check is almost always someone trying to be "over eager" in testing things that can never happen. Just like all of the checks for the result of a container_of() call, that's always wrong as well. thanks, > reveals more than 20 instances of the pattern. There are probably more > those that use if for testing for NULL. I guess people don't know about > this macro, apart from the developers of the staging driver it was added > for (commit 05e6557b8ed833546ee2b66ce6b58fecf09f439e). Ah, lustre is long-gone, so I'll just add a patch to my tree to remove this macro. thanks, greg k-h