Skip to contentSkip to conversation

w1: simplify allocation of w1_master

PATCH In review Rosen Penev 2 participants 4 messages Mar 24 – Mar 27 W1 dallas's 1-wire bus

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 w1: simplify allocation of w1_master

Convert bus_master to a flexible array member as it's being used as one with the old C89 + 1 pointer trick.

---
 drivers/w1/w1_int.c | 4 +---
 include/linux/w1.h  | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)
drivers/w1/w1_int.c +1 -3
index 19a0ea28e9f3..a0bb97608564 100644
@@ -31,12 +31,10 @@ static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
 	/*
 	 * We are in process context(kernel thread), so can sleep.
 	 */
-	dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
+	dev = kzalloc_flex(*dev, bus_master, 1);
 	if (!dev)
 		return NULL;
 
-	dev->bus_master = (struct w1_bus_master *)(dev + 1);
-
 	dev->owner		= THIS_MODULE;
 	dev->max_slave_count	= slave_count;
 	dev->slave_count	= 0;
include/linux/w1.h +2 -2
index 064805bfae3f..7379a9ddefc0 100644
@@ -236,9 +236,9 @@ struct w1_master {
 	struct device_driver	*driver;
 	struct device		dev;
 
-	struct w1_bus_master	*bus_master;
-
 	u32			seq;
+
+	struct w1_bus_master	bus_master[];
 };
 
 int w1_add_master_device(struct w1_bus_master *master);

--
2.53.0

#1 lore raw
Quoted 32 lines · Rosen Penev

Convert bus_master to a flexible array member as it's being used as one with the old C89 + 1 pointer trick.

---
 drivers/w1/w1_int.c | 4 +---
 include/linux/w1.h  | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)
drivers/w1/w1_int.c +1 -3
index 19a0ea28e9f3..a0bb97608564 100644
@@ -31,12 +31,10 @@ static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
 	/*
 	 * We are in process context(kernel thread), so can sleep.
 	 */
-	dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
+	dev = kzalloc_flex(*dev, bus_master, 1);
 	if (!dev)
 		return NULL;
 
-	dev->bus_master = (struct w1_bus_master *)(dev + 1);
-
 	dev->owner		= THIS_MODULE;
 	dev->max_slave_count	= slave_count;
 	dev->slave_count	= 0;
include/linux/w1.h +0 -0
index 064805bfae3f..7379a9ddefc0 100644

include/linux/w1.h struct w1_master { · line 241

Quoted 8 lines
include/linux/w1.h +2 -2
@@ -236,9 +236,9 @@ struct w1_master {
 	struct device_driver	*driver;
 	struct device		dev;
 
-	struct w1_bus_master	*bus_master;
-
 	u32			seq;
+
+	struct w1_bus_master	bus_master[];

Code does not use it as flexible array member, does it? Why this cannot be just two kzalloc's - one for w1_master and then next for bus_master? Or embed it directly (composition) in w1_master?

Best regards,
Krzysztof

#2 lore raw
Quoted 37 lines · Krzysztof Kozlowski
Quoted 35 lines · Rosen Penev

Convert bus_master to a flexible array member as it's being used as one with the old C89 + 1 pointer trick.

---
 drivers/w1/w1_int.c | 4 +---
 include/linux/w1.h  | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)
drivers/w1/w1_int.c +1 -3
index 19a0ea28e9f3..a0bb97608564 100644
@@ -31,12 +31,10 @@ static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
      /*
       * We are in process context(kernel thread), so can sleep.
       */
-     dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
+     dev = kzalloc_flex(*dev, bus_master, 1);
      if (!dev)
              return NULL;
 
-     dev->bus_master = (struct w1_bus_master *)(dev + 1);
-
      dev->owner              = THIS_MODULE;
      dev->max_slave_count    = slave_count;
      dev->slave_count        = 0;
include/linux/w1.h +0 -0
index 064805bfae3f..7379a9ddefc0 100644
@@ -236,9 +236,9 @@ struct w1_master {
      struct device_driver    *driver;
      struct device           dev;
 
Quoted 9 lines

include/linux/w1.h struct w1_master { · line 241

Quoted 5 lines
include/linux/w1.h +2 -2
-     struct w1_bus_master    *bus_master;
-
      u32                     seq;
+
+     struct w1_bus_master    bus_master[];

Code does not use it as flexible array member, does it? Why this cannot be just two kzalloc's - one for w1_master and then next for bus_master? Or embed it directly (composition) in w1_master?

two kzalloc or kzalloc + kcalloc just does two allocations needlessly.

It can be embedded, yes.

Quoted 3 lines

Best regards,
Krzysztof

#3 lore raw
Quoted 47 lines · Rosen Penev
Quoted 37 lines · Krzysztof Kozlowski
Quoted 35 lines · Rosen Penev

Convert bus_master to a flexible array member as it's being used as one with the old C89 + 1 pointer trick.

---
 drivers/w1/w1_int.c | 4 +---
 include/linux/w1.h  | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)
drivers/w1/w1_int.c +1 -3
index 19a0ea28e9f3..a0bb97608564 100644
@@ -31,12 +31,10 @@ static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
      /*
       * We are in process context(kernel thread), so can sleep.
       */
-     dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
+     dev = kzalloc_flex(*dev, bus_master, 1);
      if (!dev)
              return NULL;
 
-     dev->bus_master = (struct w1_bus_master *)(dev + 1);
-
      dev->owner              = THIS_MODULE;
      dev->max_slave_count    = slave_count;
      dev->slave_count        = 0;
include/linux/w1.h +0 -0
index 064805bfae3f..7379a9ddefc0 100644
@@ -236,9 +236,9 @@ struct w1_master {
      struct device_driver    *driver;
      struct device           dev;
 
Quoted 9 lines

include/linux/w1.h struct w1_master { · line 241

Quoted 5 lines
include/linux/w1.h +2 -2
-     struct w1_bus_master    *bus_master;
-
      u32                     seq;
+
+     struct w1_bus_master    bus_master[];

Code does not use it as flexible array member, does it? Why this cannot be just two kzalloc's - one for w1_master and then next for bus_master? Or embed it directly (composition) in w1_master?

Quoted 1 line

two kzalloc or kzalloc + kcalloc just does two allocations needlessly.

Needless is marking something as flexible array while it is not such. You write readable and obvious code, not something to optimize number of allocs.

Quoted 5 lines

It can be embedded, yes.

Quoted 3 lines

Best regards,
Krzysztof

Best regards,
Krzysztof