linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iommu: Fix offsetof() usage
@ 2019-04-08 12:41 Robin Murphy
  2019-04-09 10:00 ` Christoph Hellwig
  0 siblings, 1 reply; 2+ messages in thread
From: Robin Murphy @ 2019-04-08 12:41 UTC (permalink / raw)
  To: joro; +Cc: iommu, David Laight, linux-arm-kernel

David rightly points out that, although GCC lets us get away with it,
using offsetof() with a non-constant member designator is not compliant
with the C standard. I'm responsible for a couple of those misuses, but
both in circumstances for which we now have a specific helper, so we can
use that to put things straight.

Suggested-by: David Laight <David.Laight@ACULAB.COM>
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
---
 drivers/iommu/arm-smmu.c | 3 +--
 drivers/iommu/iommu.c    | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index 74e944bd4a8d..81d449451494 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -1484,8 +1484,7 @@ static int arm_smmu_add_device(struct device *dev)
 	}
 
 	ret = -ENOMEM;
-	cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
-		      GFP_KERNEL);
+	cfg = kzalloc(struct_size(cfg, smendx, i), GFP_KERNEL);
 	if (!cfg)
 		goto out_free;
 
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 33a982e33716..ad28919e1452 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -2021,7 +2021,7 @@ int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
 	if (!fwspec)
 		return -EINVAL;
 
-	size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
+	size = struct_size(fwspec, ids, fwspec->num_ids + num_ids);
 	if (size > sizeof(*fwspec)) {
 		fwspec = krealloc(fwspec, size, GFP_KERNEL);
 		if (!fwspec)
-- 
2.21.0.dirty


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] iommu: Fix offsetof() usage
  2019-04-08 12:41 [PATCH] iommu: Fix offsetof() usage Robin Murphy
@ 2019-04-09 10:00 ` Christoph Hellwig
  0 siblings, 0 replies; 2+ messages in thread
From: Christoph Hellwig @ 2019-04-09 10:00 UTC (permalink / raw)
  To: Robin Murphy; +Cc: joro, David Laight, iommu, linux-arm-kernel

> index 74e944bd4a8d..81d449451494 100644
> --- a/drivers/iommu/arm-smmu.c
> +++ b/drivers/iommu/arm-smmu.c
> @@ -1484,8 +1484,7 @@ static int arm_smmu_add_device(struct device *dev)
>  	}
>  
>  	ret = -ENOMEM;
> -	cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
> -		      GFP_KERNEL);
> +	cfg = kzalloc(struct_size(cfg, smendx, i), GFP_KERNEL);

This looks like a huge improvement, but I find the usage of i
here still very obsfucating.  Can we please use fwspec->num_ids
directly instead of make it much more obvious?

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-04-09 10:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-08 12:41 [PATCH] iommu: Fix offsetof() usage Robin Murphy
2019-04-09 10:00 ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).