lkml.org 
[lkml]   [2022]   [Dec]   [20]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
SubjectRe: [PATCH v2] sched: Make const-safe
On Wed, Dec 14, 2022 at 06:03:04PM +0000, Matthew Wilcox wrote:
> On Wed, Dec 14, 2022 at 10:11:01AM +0100, Peter Zijlstra wrote:
> > On Mon, Dec 12, 2022 at 02:49:46PM +0000, Matthew Wilcox (Oracle) wrote:
> > > With a modified container_of() that preserves constness, the compiler
> > > finds some pointers which should have been marked as const. task_of()
> > > also needs to become const-preserving for the !FAIR_GROUP_SCHED case so
> > > that cfs_rq_of() can take a const argument. No change to generated code.
> >
> > More const more better I suppose.. Thanks!
> >
> > Happen to have a sha for the container_of() commit handy?
>
> There isn't one yet. Obviously we can't make container_of()
> const-preserving until we've fixed all the places which would warn.
> The diff I have in my tree looks like this:
>
> diff --git a/include/linux/container_of.h b/include/linux/container_of.h
> index 1d898f9158b4..9416e6cc8c88 100644
> --- a/include/linux/container_of.h
> +++ b/include/linux/container_of.h
> @@ -20,7 +20,10 @@
> static_assert(__same_type(*(ptr), ((type *)0)->member) || \
> __same_type(*(ptr), void), \
> "pointer type mismatch in container_of()"); \
> - ((type *)(__mptr - offsetof(type, member))); })
> + __mptr -= offsetof(type, member); \
> + _Generic(ptr, \
> + const typeof(*(ptr)) *: (const type *)__mptr, \
> + default: ((type *)__mptr)); })
>
> /**
> * container_of_const - cast a member of a structure out to the containing
>
> I have all of fs/ and net/ compiling cleanly now. There are a few
> places which really need the const-removing properties, and I've made
> those call a new macro called container_of_not_const(), but I don't
> like that name.

#define const_cast(T, exp) _Generic((exp), const T : (T)(exp), default: (exp))

perhaps? Then one can write something like:

struct task_struct *p = const_cast(struct task_struct *,
constainer_of(node, struct task_struct *, run_node));

The repetition is a bit naf, but at least the construct is more
generally useful.

(I really wish there was a qualifier stripping typeof() variant -- and
yes, I know about the _Atomic thing).

\
 
 \ /
  Last update: 2023-03-26 23:15    [W:0.043 / U:0.716 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site