On Wed, Jan 29, 2025 at 06:54:48AM +0100, Greg KH wrote: > On Wed, Jan 29, 2025 at 03:56:01PM +1030, Gustavo A. R. Silva wrote: > > This is like container_of_const() but it contains an assert to > > ensure that it's using the first member in the structure. > > But why? If you "know" it's the first member, just do a normal cast. > If you don't, then you probably shouldn't be caring about this anyway, > right? >
Heh. I had a long coversation with someone where I tried to explain four times that casting the first member was a thing. In the end, they were able to accept that it works but only "accidentally." We merged their patch as a cleanup. https://lore.kernel.org/all/[email protected]/ We have quite a bit of code like: drivers/iommu/iommufd/iommufd_private.h 243 static inline struct iommufd_ioas *iommufd_get_ioas(struct iommufd_ctx *ictx, 244 u32 id) 245 { 246 return container_of(iommufd_get_object(ictx, id, 247 IOMMUFD_OBJ_IOAS), 248 struct iommufd_ioas, obj); 249 } It's just a cast like you say, but it looks like pointer math. It would be more readable as container_of_first(). The weird thing is that when people check if (IS_ERR()) on a container_of() then normally the code is correct, but when they check for NULL then normally the NULL check is a harmless no-op. regards, dan carpenter
