The patch titled Subject: container_of.h: make container_of const-aware has been added to the -mm tree. Its filename is container_ofh-make-container_of-const-aware.patch This patch should soon appear at https://ozlabs.org/~akpm/mmots/broken-out/container_ofh-make-container_of-const-aware.patch and later at https://ozlabs.org/~akpm/mmotm/broken-out/container_ofh-make-container_of-const-aware.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Dmitry Baryshkov <dmitry.baryshkov@xxxxxxxxxx> Subject: container_of.h: make container_of const-aware 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. Link: https://lkml.kernel.org/r/20220210170434.3924169-1-dmitry.baryshkov@xxxxxxxxxx Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@xxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- --- a/include/linux/container_of.h~container_ofh-make-container_of-const-aware +++ a/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 */ _ Patches currently in -mm which might be from dmitry.baryshkov@xxxxxxxxxx are container_ofh-make-container_of-const-aware.patch