* [PATCH] container_of.h: make container_of const-aware
@ 2022-02-10 17:04 Dmitry Baryshkov
2022-02-11 22:05 ` Andrew Morton
0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Baryshkov @ 2022-02-10 17:04 UTC (permalink / raw)
To: linux-kernel, Andrew Morton; +Cc: Andy Shevchenko
container_of() macro has one major drawback. It does not check whether
the passed ptr has a const pointer, the result will always be a
non-const pointer. Use a _Generic() construct (supported since gcc 4.9
and Clang 3.0) to teach container_of that if converting a const pointer,
the returned pointer should also have the const modifier.
Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
---
include/linux/container_of.h | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/include/linux/container_of.h b/include/linux/container_of.h
index 2f4944b791b8..269f64e27b09 100644
--- a/include/linux/container_of.h
+++ b/include/linux/container_of.h
@@ -19,7 +19,11 @@
static_assert(__same_type(*(ptr), ((type *)0)->member) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
- ((type *)(__mptr - offsetof(type, member))); })
+ _Generic((ptr), \
+ const typeof(((type *)0)->member)*: \
+ ((const type *)(__mptr - offsetof(type, member))), \
+ default: ((type *)(__mptr - offsetof(type, member))) \
+ ); })
/**
* container_of_safe - cast a member of a structure out to the containing structure
@@ -35,6 +39,10 @@
__same_type(*(ptr), void), \
"pointer type mismatch in container_of_safe()"); \
IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \
- ((type *)(__mptr - offsetof(type, member))); })
+ _Generic((ptr), \
+ const typeof(((type *)0)->member)*: \
+ ((const type *)(__mptr - offsetof(type, member))), \
+ default: ((type *)(__mptr - offsetof(type, member))) \
+ ); })
#endif /* _LINUX_CONTAINER_OF_H */
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] container_of.h: make container_of const-aware
2022-02-10 17:04 [PATCH] container_of.h: make container_of const-aware Dmitry Baryshkov
@ 2022-02-11 22:05 ` Andrew Morton
2022-02-11 22:10 ` Dmitry Baryshkov
0 siblings, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2022-02-11 22:05 UTC (permalink / raw)
To: Dmitry Baryshkov; +Cc: linux-kernel, Andy Shevchenko
On Thu, 10 Feb 2022 20:04:34 +0300 Dmitry Baryshkov <dmitry.baryshkov@linaro.org> wrote:
> container_of() macro has one major drawback. It does not check whether
> the passed ptr has a const pointer, the result will always be a
> non-const pointer. Use a _Generic() construct (supported since gcc 4.9
> and Clang 3.0) to teach container_of that if converting a const pointer,
> the returned pointer should also have the const modifier.
>
Nice idea, but my x86_64 allnoconfig build explodes with zillions of
warnings.
In file included from ./include/linux/list.h:5,
from ./include/linux/module.h:12,
from init/do_mounts.c:2:
./include/net/sock.h: In function 'sk_entry':
./include/linux/container_of.h:17:42: warning: return discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] container_of.h: make container_of const-aware
2022-02-11 22:05 ` Andrew Morton
@ 2022-02-11 22:10 ` Dmitry Baryshkov
2022-02-11 22:18 ` Andrew Morton
0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Baryshkov @ 2022-02-11 22:10 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel, Andy Shevchenko
On Sat, 12 Feb 2022 at 01:05, Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Thu, 10 Feb 2022 20:04:34 +0300 Dmitry Baryshkov <dmitry.baryshkov@linaro.org> wrote:
>
> > container_of() macro has one major drawback. It does not check whether
> > the passed ptr has a const pointer, the result will always be a
> > non-const pointer. Use a _Generic() construct (supported since gcc 4.9
> > and Clang 3.0) to teach container_of that if converting a const pointer,
> > the returned pointer should also have the const modifier.
> >
>
> Nice idea, but my x86_64 allnoconfig build explodes with zillions of
> warnings.
>
> In file included from ./include/linux/list.h:5,
> from ./include/linux/module.h:12,
> from init/do_mounts.c:2:
> ./include/net/sock.h: In function 'sk_entry':
> ./include/linux/container_of.h:17:42: warning: return discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
Are these warnings legit? In other words, is the code converting a
const pointer into a modifiable one? I've stumbled upon this in drm
drivers.
How do we proceed with this patch?
--
With best wishes
Dmitry
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] container_of.h: make container_of const-aware
2022-02-11 22:10 ` Dmitry Baryshkov
@ 2022-02-11 22:18 ` Andrew Morton
0 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2022-02-11 22:18 UTC (permalink / raw)
To: Dmitry Baryshkov; +Cc: linux-kernel, Andy Shevchenko
On Sat, 12 Feb 2022 01:10:09 +0300 Dmitry Baryshkov <dmitry.baryshkov@linaro.org> wrote:
> On Sat, 12 Feb 2022 at 01:05, Andrew Morton <akpm@linux-foundation.org> wrote:
> >
> > On Thu, 10 Feb 2022 20:04:34 +0300 Dmitry Baryshkov <dmitry.baryshkov@linaro.org> wrote:
> >
> > > container_of() macro has one major drawback. It does not check whether
> > > the passed ptr has a const pointer, the result will always be a
> > > non-const pointer. Use a _Generic() construct (supported since gcc 4.9
> > > and Clang 3.0) to teach container_of that if converting a const pointer,
> > > the returned pointer should also have the const modifier.
> > >
> >
> > Nice idea, but my x86_64 allnoconfig build explodes with zillions of
> > warnings.
> >
> > In file included from ./include/linux/list.h:5,
> > from ./include/linux/module.h:12,
> > from init/do_mounts.c:2:
> > ./include/net/sock.h: In function 'sk_entry':
> > ./include/linux/container_of.h:17:42: warning: return discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
>
> Are these warnings legit? In other words, is the code converting a
> const pointer into a modifiable one?
It is indeed doing that.
static inline struct sock *sk_entry(const struct hlist_node *node)
{
return hlist_entry(node, struct sock, sk_node);
}
Which is a pretty rude thing to do. What was the point in that const arg?
> I've stumbled upon this in drm
> drivers.
> How do we proceed with this patch?
They all need to be fixed up :(
You could take a shot, if you have the time. If not, bug the relevant
code's owners about it. But we can't add a patch like this, blow up
everyone's builds and wait for various people to fix up the fallout!
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-02-11 22:18 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-10 17:04 [PATCH] container_of.h: make container_of const-aware Dmitry Baryshkov
2022-02-11 22:05 ` Andrew Morton
2022-02-11 22:10 ` Dmitry Baryshkov
2022-02-11 22:18 ` Andrew Morton
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.