public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Sakari Ailus <sakari.ailus@linux.intel.com>
Cc: linux-kernel@vger.kernel.org,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Jason Gunthorpe <jgg@nvidia.com>,
	Matthew Wilcox <willy@infradead.org>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	Hans Verkuil <hverkuil@xs4all.nl>
Subject: Re: [PATCH 1/1] container_of: Document container_of_const() is preferred
Date: Mon, 17 Jun 2024 12:44:55 +0200	[thread overview]
Message-ID: <2024061702-vexingly-hypocrisy-d93d@gregkh> (raw)
In-Reply-To: <20240617100825.2510728-1-sakari.ailus@linux.intel.com>

On Mon, Jun 17, 2024 at 01:08:25PM +0300, Sakari Ailus wrote:
> There is a warning in kerneldoc documentation of container_of() that
> constness of @ptr is lost. While this is a suggestion container_of_const()
> should be used instead, the vast majority of new code still uses
> container_of():
> 
> $ git diff v6.8 v6.9|grep container_of\(|wc -l
> 788
> $ git diff v6.8 v6.9|grep container_of_const|wc -l
> 11

That is because container_of_const is new, and you don't normally go
back and change things unless you have to.  Which is what I am starting
to do for some cases now in the driver core interactions, but generally
it's rare to need this.

Also note that container_of_const does not work in an inline function,
which is another reason people might not want to use it.

> Make an explicit recommendation to use container_of_const(), unless @ptr
> is const but its container isn't.
> 
> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> ---
>  include/linux/container_of.h | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/container_of.h b/include/linux/container_of.h
> index 713890c867be..7563015ff165 100644
> --- a/include/linux/container_of.h
> +++ b/include/linux/container_of.h
> @@ -13,7 +13,9 @@
>   * @type:	the type of the container struct this is embedded in.
>   * @member:	the name of the member within the struct.
>   *
> - * WARNING: any const qualifier of @ptr is lost.
> + * WARNING: any const qualifier of @ptr is lost. container_of() should only be
> + * used in cases where @ptr is const and its container is not and you know what
> + * you're doing. Otherwise always use container_of_const().

I know of no cases where a @ptr would be const yet the container would
not be, do you?  So why say that here?  That implies that it is a valid
thing to actually do.

I don't understand the goal here, do you want to just not have new
usages use container_of() at all?  Or are you trying to warn people of a
common problem that they make?  Having a const @ptr is not normal in the
kernel, so this should be ok.  If not, send patches to fix up those
users please.

thanks,

greg k-h

  reply	other threads:[~2024-06-17 10:44 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-17 10:08 [PATCH 1/1] container_of: Document container_of_const() is preferred Sakari Ailus
2024-06-17 10:44 ` Greg Kroah-Hartman [this message]
2024-06-18  9:09   ` Sakari Ailus
2024-06-18 10:01     ` Greg Kroah-Hartman
2024-06-18 11:52       ` Sakari Ailus
2024-06-18 12:54         ` Greg Kroah-Hartman
2024-06-18 13:56           ` Matthew Wilcox
2024-06-18 14:02             ` Greg Kroah-Hartman
2024-08-09 12:59 ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2024061702-vexingly-hypocrisy-d93d@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=hverkuil@xs4all.nl \
    --cc=jgg@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox