[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Y4+jrBBRQ6XfNMfY@casper.infradead.org>
Date: Tue, 6 Dec 2022 20:18:52 +0000
From: Matthew Wilcox <willy@...radead.org>
To: Greg Kroah-Hartman <gregkh@...uxfoundation.org>
Cc: linux-kernel@...r.kernel.org, Jason Gunthorpe <jgg@...pe.ca>,
Sakari Ailus <sakari.ailus@...ux.intel.com>,
Jason Gunthorpe <jgg@...dia.com>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
"Rafael J. Wysocki" <rafael@...nel.org>
Subject: Re: [PATCH v2 1/4] container_of: add container_of_const() that
preserves const-ness of the pointer
On Tue, Dec 06, 2022 at 07:46:47PM +0100, Greg Kroah-Hartman wrote:
> On Tue, Dec 06, 2022 at 05:18:22PM +0000, Matthew Wilcox wrote:
> > static inline struct external_name *external_name(struct dentry *dentry)
> > {
> > - return container_of(dentry->d_name.name, struct external_name, name[0]);
> > + return container_of_not_const(dentry->d_name.name,
> > + struct external_name, name[0]);
> > }
>
> Will just:
> return container_of((unsigned char *)dentry->d_name.name, struct external_name, name[0]);
> work by casting away the "const" of the name?
>
> Yeah it's ugly, I never considered the address of a const char * being
> used as a base to cast back from. The vfs is fun :)
Yes, that also works. This isn't particularly common in the VFS, it's
just the dcache. And I understand why it's done like this; you don't
want rando filesystems modifying dentry names without also updating
the hash.
I feel like all the options here are kind of ugly. Seeing casts in
the arguments to container_of should be a red flag!
Here's a bit of a weird option ...
+#define container_of_2(ptr, p_m, type, member) \
+ _Generic(ptr, \
+ const typeof(*(ptr)) *: (const type *)container_of(ptr->p_m, type, member), \
+ default: ((type *)container_of(ptr->p_m, type, member)))
+
static inline struct external_name *external_name(struct dentry *dentry)
{
- return container_of(dentry->d_name.name, struct external_name, name[0]);
+ return container_of_2(dentry, d_name.name, struct external_name,
+ name[0]);
}
so we actually split the first argument into two -- the pointer which
isn't const, then the pointer member which might be const, but we don't
use it for the return result of container_of_2.
Powered by blists - more mailing lists