2024-02-28 23:55:20

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v4 0/8] iio: core: New macros and making use of them

Added new macros to overflow.h and reuse it in IIO. For the sake of examples
a few more places were updated (requested by Kees). In case maintainers are okay,
tags will be appreciated.

v4:
- dropped applied patches
- refactored macros and code to make them simpler (Jonathan)
- moved (renamed) macros to overflow.h

v3: https://lore.kernel.org/r/[email protected]
- dropped applied patches
- use switch-case for the supported clocks (Jonathan) - redone opaque_struct_size() to be simpler (Uwe)
- dropped wrong hunk for krealloc_array() conversion (Jonathan) - dropped initcall move (Jonathan)

v2:
- sprintf() --> sysfs_emit() (Nuno)
- added tag (Nuno)

Andy Shevchenko (8):
overflow: Use POD in check_shl_overflow()
overflow: Add struct_size_with_data() and struct_data_pointer()
helpers
iio: core: NULLify private pointer when there is no private data
iio: core: Calculate alloc_size only once in iio_device_alloc()
iio: core: Use new helpers from overflow.h in iio_device_alloc()
spi: Use new helpers from overflow.h in __spi_alloc_controller()
net-device: Use new helpers from overflow.h in netdevice APIs
dmaengine: ste_dma40: Use new helpers from overflow.h

drivers/dma/ste_dma40.c | 12 ++++++------
drivers/iio/industrialio-core.c | 16 +++++++++-------
drivers/spi/spi.c | 6 +++---
include/linux/netdevice.h | 3 ++-
include/linux/overflow.h | 29 +++++++++++++++++++++++++++--
net/core/dev.c | 10 +++++-----
6 files changed, 52 insertions(+), 24 deletions(-)

--
2.43.0.rc1.1.gbec44491f096



2024-02-29 02:17:01

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v4 8/8] dmaengine: ste_dma40: Use new helpers from overflow.h

We have two new helpers struct_size_with_data() and struct_data_pointer()
that we can utilize in d40_hw_detect_init(). Do it so.

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/dma/ste_dma40.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/dma/ste_dma40.c b/drivers/dma/ste_dma40.c
index 2c489299148e..bead3b8836c7 100644
--- a/drivers/dma/ste_dma40.c
+++ b/drivers/dma/ste_dma40.c
@@ -15,6 +15,7 @@
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/log2.h>
+#include <linux/overflow.h>
#include <linux/pm.h>
#include <linux/pm_runtime.h>
#include <linux/err.h>
@@ -3141,6 +3142,7 @@ static int __init d40_hw_detect_init(struct platform_device *pdev,
int num_log_chans;
int num_phy_chans;
int num_memcpy_chans;
+ size_t sz;
int i;
u32 pid;
u32 cid;
@@ -3207,11 +3209,9 @@ static int __init d40_hw_detect_init(struct platform_device *pdev,
"hardware rev: %d with %d physical and %d logical channels\n",
rev, num_phy_chans, num_log_chans);

- base = devm_kzalloc(dev,
- ALIGN(sizeof(struct d40_base), 4) +
- (num_phy_chans + num_log_chans + num_memcpy_chans) *
- sizeof(struct d40_chan), GFP_KERNEL);
-
+ sz = array_size(num_phy_chans + num_log_chans + num_memcpy_chans,
+ sizeof(struct d40_chan));
+ base = devm_kzalloc(dev, struct_size_with_data(base, 4, sz), GFP_KERNEL);
if (!base)
return -ENOMEM;

@@ -3223,7 +3223,7 @@ static int __init d40_hw_detect_init(struct platform_device *pdev,
base->virtbase = virtbase;
base->plat_data = plat_data;
base->dev = dev;
- base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
+ base->phy_chans = struct_data_pointer(base, 4);
base->log_chans = &base->phy_chans[num_phy_chans];

if (base->plat_data->num_of_phy_chans == 14) {
--
2.43.0.rc1.1.gbec44491f096


2024-02-29 02:46:44

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v4 4/8] iio: core: Calculate alloc_size only once in iio_device_alloc()

No need to rewrite the value, instead use 'else' branch.
This will also help further refactoring the code later on.

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/iio/industrialio-core.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index bd305fa87093..1986b3386307 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1643,11 +1643,10 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
struct iio_dev *indio_dev;
size_t alloc_size;

- alloc_size = sizeof(struct iio_dev_opaque);
- if (sizeof_priv) {
- alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN);
- alloc_size += sizeof_priv;
- }
+ if (sizeof_priv)
+ alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN) + sizeof_priv;
+ else
+ alloc_size = sizeof(struct iio_dev_opaque);

iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
if (!iio_dev_opaque)
--
2.43.0.rc1.1.gbec44491f096


2024-02-29 14:14:45

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH v4 8/8] dmaengine: ste_dma40: Use new helpers from overflow.h

On Wed, Feb 28, 2024 at 9:49 PM Andy Shevchenko
<[email protected]> wrote:

> We have two new helpers struct_size_with_data() and struct_data_pointer()
> that we can utilize in d40_hw_detect_init(). Do it so.
>
> Signed-off-by: Andy Shevchenko <[email protected]>

Wow really neat! Much easier to read and understand the code like this.
Reviewed-by: Linus Walleij <[email protected]>

Yours,
Linus Walleij

2024-02-29 16:08:13

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v4 8/8] dmaengine: ste_dma40: Use new helpers from overflow.h

On Thu, Feb 29, 2024 at 03:14:03PM +0100, Linus Walleij wrote:
> On Wed, Feb 28, 2024 at 9:49 PM Andy Shevchenko
> <[email protected]> wrote:
>
> > We have two new helpers struct_size_with_data() and struct_data_pointer()
> > that we can utilize in d40_hw_detect_init(). Do it so.
> >
> > Signed-off-by: Andy Shevchenko <[email protected]>
>
> Wow really neat! Much easier to read and understand the code like this.
> Reviewed-by: Linus Walleij <[email protected]>

Thanks, but Kees has seems even better suggestion.

--
With Best Regards,
Andy Shevchenko