Re: [PATCH v4 12/23] iommufd/driver: Add iommufd_hw_queue_depend/undepend() helpers

Nicolin Chen
2025-05-16T04:52:08Z

On Thu, May 15, 2025 at 01:12:54PM -0300, Jason Gunthorpe wrote:
> On Thu, May 08, 2025 at 08:02:33PM -0700, Nicolin Chen wrote:
>
> > +/*
> > + * Helpers for IOMMU driver to build/destroy a dependency between two sibling
> > + * structures created by one of the allocators above
> > + */
> > +#define iommufd_hw_queue_depend(dependent, depended, member) \
> > + ({ \
> > + static_assert(__same_type(struct iommufd_hw_queue, \
> > + dependent->member)); \
> > + static_assert(offsetof(typeof(*dependent), member.obj) == 0); \
> > + static_assert(__same_type(struct iommufd_hw_queue, \
> > + depended->member)); \
> > + static_assert(offsetof(typeof(*depended), member.obj) == 0); \
> > + _iommufd_object_depend(&dependent->member.obj, \
> > + &depended->member.obj); \
> > + })
>
> This doesn't need the offsetof == 0 checks, it isn't an allocator. And
> you want to check that the two structs have the same type:
>
> static_assert(__same_type(struct iommufd_hw_queue, dependent->member));
> static_assert(__same_type(typeof(*dependent), *dependend));

Ack. I also added ictx comparison:

+#define iommufd_hw_queue_depend(dependent, depended, member) \
+ ({ \
+ int ret = -EINVAL; \
+ \
+ static_assert(__same_type(struct iommufd_hw_queue, \
+ dependent->member)); \
+ static_assert(__same_type(typeof(*dependent), *depended)); \
+ if (!WARN_ON_ONCE(dependent->member.ictx != \
+ depended->member.ictx)) \
+ ret = _iommufd_object_depend(&dependent->member.obj, \
+ &depended->member.obj); \
+ ret; \
+ })
+
+#define iommufd_hw_queue_undepend(dependent, depended, member) \
+ ({ \
+ static_assert(__same_type(struct iommufd_hw_queue, \
+ dependent->member)); \
+ static_assert(__same_type(typeof(*dependent), *depended)); \
+ WARN_ON_ONCE(dependent->member.ictx != depended->member.ictx); \
+ _iommufd_object_undepend(&dependent->member.obj, \
+ &depended->member.obj); \
+ })

Thanks
Nicolin