Re: [PATCH v5 07/13] iommufd/selftest: Add container_of helpers

2024-10-29 Thread Jason Gunthorpe
On Fri, Oct 25, 2024 at 04:49:47PM -0700, Nicolin Chen wrote:
> Use these inline helpers to shorten those container_of lines.
> 
> Note that one of them goes back and forth between iommu_domain and
> mock_iommu_domain, which isn't necessary. So drop its container_of.
> 
> Signed-off-by: Nicolin Chen 
> ---
>  drivers/iommu/iommufd/selftest.c | 75 ++--
>  1 file changed, 42 insertions(+), 33 deletions(-)

Reviewed-by: Jason Gunthorpe 

Jason



RE: [PATCH v5 07/13] iommufd/selftest: Add container_of helpers

2024-10-27 Thread Tian, Kevin
> From: Nicolin Chen 
> Sent: Saturday, October 26, 2024 7:50 AM
> 
> Use these inline helpers to shorten those container_of lines.
> 
> Note that one of them goes back and forth between iommu_domain and
> mock_iommu_domain, which isn't necessary. So drop its container_of.
> 
> Signed-off-by: Nicolin Chen 

Reviewed-by: Kevin Tian 



[PATCH v5 07/13] iommufd/selftest: Add container_of helpers

2024-10-25 Thread Nicolin Chen
Use these inline helpers to shorten those container_of lines.

Note that one of them goes back and forth between iommu_domain and
mock_iommu_domain, which isn't necessary. So drop its container_of.

Signed-off-by: Nicolin Chen 
---
 drivers/iommu/iommufd/selftest.c | 75 ++--
 1 file changed, 42 insertions(+), 33 deletions(-)

diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index 540437be168a..322e57ff3605 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -126,12 +126,24 @@ struct mock_iommu_domain {
struct xarray pfns;
 };
 
+static inline struct mock_iommu_domain *
+to_mock_domain(struct iommu_domain *domain)
+{
+   return container_of(domain, struct mock_iommu_domain, domain);
+}
+
 struct mock_iommu_domain_nested {
struct iommu_domain domain;
struct mock_iommu_domain *parent;
u32 iotlb[MOCK_NESTED_DOMAIN_IOTLB_NUM];
 };
 
+static inline struct mock_iommu_domain_nested *
+to_mock_nested(struct iommu_domain *domain)
+{
+   return container_of(domain, struct mock_iommu_domain_nested, domain);
+}
+
 enum selftest_obj_type {
TYPE_IDEV,
 };
@@ -142,6 +154,11 @@ struct mock_dev {
int id;
 };
 
+static inline struct mock_dev *to_mock_dev(struct device *dev)
+{
+   return container_of(dev, struct mock_dev, dev);
+}
+
 struct selftest_obj {
struct iommufd_object obj;
enum selftest_obj_type type;
@@ -155,10 +172,15 @@ struct selftest_obj {
};
 };
 
+static inline struct selftest_obj *to_selftest_obj(struct iommufd_object *obj)
+{
+   return container_of(obj, struct selftest_obj, obj);
+}
+
 static int mock_domain_nop_attach(struct iommu_domain *domain,
  struct device *dev)
 {
-   struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
+   struct mock_dev *mdev = to_mock_dev(dev);
 
if (domain->dirty_ops && (mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY))
return -EINVAL;
@@ -193,8 +215,7 @@ static void *mock_domain_hw_info(struct device *dev, u32 
*length, u32 *type)
 static int mock_domain_set_dirty_tracking(struct iommu_domain *domain,
  bool enable)
 {
-   struct mock_iommu_domain *mock =
-   container_of(domain, struct mock_iommu_domain, domain);
+   struct mock_iommu_domain *mock = to_mock_domain(domain);
unsigned long flags = mock->flags;
 
if (enable && !domain->dirty_ops)
@@ -243,8 +264,7 @@ static int mock_domain_read_and_clear_dirty(struct 
iommu_domain *domain,
unsigned long flags,
struct iommu_dirty_bitmap *dirty)
 {
-   struct mock_iommu_domain *mock =
-   container_of(domain, struct mock_iommu_domain, domain);
+   struct mock_iommu_domain *mock = to_mock_domain(domain);
unsigned long end = iova + size;
void *ent;
 
@@ -281,7 +301,7 @@ static const struct iommu_dirty_ops dirty_ops = {
 
 static struct iommu_domain *mock_domain_alloc_paging(struct device *dev)
 {
-   struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
+   struct mock_dev *mdev = to_mock_dev(dev);
struct mock_iommu_domain *mock;
 
mock = kzalloc(sizeof(*mock), GFP_KERNEL);
@@ -327,7 +347,7 @@ mock_domain_alloc_user(struct device *dev, u32 flags,
 
/* must be mock_domain */
if (!parent) {
-   struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
+   struct mock_dev *mdev = to_mock_dev(dev);
bool has_dirty_flag = flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
bool no_dirty_ops = mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY;
struct iommu_domain *domain;
@@ -341,8 +361,7 @@ mock_domain_alloc_user(struct device *dev, u32 flags,
if (!domain)
return ERR_PTR(-ENOMEM);
if (has_dirty_flag)
-   container_of(domain, struct mock_iommu_domain, domain)
-   ->domain.dirty_ops = &dirty_ops;
+   domain->dirty_ops = &dirty_ops;
return domain;
}
 
@@ -352,7 +371,7 @@ mock_domain_alloc_user(struct device *dev, u32 flags,
if (!parent || parent->ops != mock_ops.default_domain_ops)
return ERR_PTR(-EINVAL);
 
-   mock_parent = container_of(parent, struct mock_iommu_domain, domain);
+   mock_parent = to_mock_domain(parent);
if (!mock_parent)
return ERR_PTR(-EINVAL);
 
@@ -366,8 +385,7 @@ mock_domain_alloc_user(struct device *dev, u32 flags,
 
 static void mock_domain_free(struct iommu_domain *domain)
 {
-   struct mock_iommu_domain *mock =
-   container_of(domain, struct mock_iommu_domain, domain);
+   struct mock_iommu_domain *mock = to_mock_domain(domain);
 
WARN