Skip to contentSkip to conversation

pinctrl: Store mapping table as a list of chunks

PATCH In review Stephen Warren 2 participants 4 messages Feb 15, 2012 – Feb 16, 2012

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pinctrl: Store mapping table as a list of chunks

Instead of storing a single array of mapping table entries, which requires realloc()ing that array each time it's extended and copying the new data, simply store a list of pointers to the individual chunks. This also removes the need to copy the mapping table at all; a pointer is maintained to the original table, this saving memory.

A macro for_each_maps() is introduced to hide the additional complexity of iterating over the map entries.

This change will also simplify removing chunks of entries from the mapping table. This isn't important right now, but will be in the future, when mapping table entries are dynamically added when parsing them from the device tree, and removed when drivers no longer need to interact with pinctrl.

---
This depends on my previous patch "pinctrl: Make "hog" mapping table entries work", since both touch the same line in pinctrl_hog_map.

 arch/arm/mach-u300/core.c |    2 +-
 drivers/pinctrl/core.c    |  104 +++++++++++++++++++++++++++-----------------
 2 files changed, 65 insertions(+), 41 deletions(-)
arch/arm/mach-u300/core.c +1 -1
index d66bc97..5af05fb 100644
@@ -1552,7 +1552,7 @@ static struct platform_device pinmux_device = {
 };
 
 /* Pinmux settings */
-static struct pinctrl_map __initdata u300_pinmux_map[] = {
+static struct pinctrl_map u300_pinmux_map[] = {
 	/* anonymous maps for chip power and EMIFs */
 	PIN_MAP_SYS_HOG("POWER", "pinmux-u300", "power"),
 	PIN_MAP_SYS_HOG("EMIF0", "pinmux-u300", "emif0"),
drivers/pinctrl/core.c +64 -40

215 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#1 lore raw

Stephen Warren wrote at Wednesday, February 15, 2012 2:01 PM:

Quoted 5 lines

Instead of storing a single array of mapping table entries, which requires realloc()ing that array each time it's extended and copying the new data, simply store a list of pointers to the individual chunks. This also removes the need to copy the mapping table at all; a pointer is maintained to the original table, this saving memory.

...

Quoted 1 line
drivers/pinctrl/core.c +0 -0

...

Quoted 1 line
drivers/pinctrl/core.c +0 -0
@@ -689,31 +719,17 @@ int __init pinctrl_register_mappings(struct pinctrl_map const *maps,

...

drivers/pinctrl/core.c int __init pinctrl_register_mappings(struct pinctrl_map const *maps, · line 722

Quoted 1 line
drivers/pinctrl/core.c +1 -0
+	maps_node = kzalloc(sizeof(*maps), GFP_KERNEL);

That should be sizeof(*maps_node).

I'll post a respin of this with the rest of my pinctrl changes.

--
nvpublic

#2 lore raw
In reply to Stephen Warren

Overall this is exactly what we want, good work!

arch/arm/mach-u300/core.c static struct platform_device pinmux_device = { · line 1555

Quoted 8 lines · Stephen Warren
arch/arm/mach-u300/core.c +1 -1
@@ -1552,7 +1552,7 @@ static struct platform_device pinmux_device = {
  };
 
  /* Pinmux settings */
-static struct pinctrl_map __initdata u300_pinmux_map[] = {
+static struct pinctrl_map u300_pinmux_map[] = {

This was actually not tagged __initdata until last merge window.

The reason it was made discardable was for platform that needed to discard a large number of mappings after boot, since only a few of them got registered from the board files.

(Yes, I know this will not be a problem in device tree configs...)

Can we combine the best of two worlds by still making a shallow copy (like previously) and put that shallow copy into the list instead?

arch/arm/mach-u300/core.c static struct platform_device pinmux_device = {

Quoted 5 lines
arch/arm/mach-u300/core.c +5 -0
+#define for_each_maps(_maps_node_, _i_, _map_) \
+       list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
+               for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
+                       _i_ < _maps_node_->num_maps; \
+                       i++, _map_ = &_maps_node_->maps[_i_])

I really like this macro.

Quoted 29 lines

 /**

arch/arm/mach-u300/core.c +1 -15
@@ -654,8 +684,8 @@ EXPORT_SYMBOL_GPL(pinctrl_disable);
  int __init pinctrl_register_mappings(struct pinctrl_map const *maps,
                                     unsigned num_maps)
  {
-       void *tmp_maps;
        int i;
+       struct pinctrl_maps *maps_node;
 
        pr_debug("add %d pinmux maps\n", num_maps);
 
@@ -689,31 +719,17 @@ int __init pinctrl_register_mappings(struct pinctrl_map const *maps,
                                 maps[i].function);
        }
 
-       /*
-        * Make a copy of the map array - string pointers will end up in the
-        * kernel const section anyway so these do not need to be deep copied.
-        */
-       if (!pinctrl_maps_num) {
-               /* On first call, just copy them */
-               tmp_maps = kmemdup(maps,
-                                  sizeof(struct pinctrl_map) * num_maps,
-                                  GFP_KERNEL);
-               if (!tmp_maps)
-                       return -ENOMEM;
-       } else {
-               /* Subsequent calls, reallocate array to new size */
-               size_t oldsize = sizeof(struct pinctrl_map) * pinctrl_maps_num;

arch/arm/mach-u300/core.c int __init pinctrl_register_mappings(struct pinctrl_map const *maps, · line 712

Quoted 10 lines
arch/arm/mach-u300/core.c +3 -6
-               size_t newsize = sizeof(struct pinctrl_map) * num_maps;
+       maps_node = kzalloc(sizeof(*maps), GFP_KERNEL);
+       if (!maps_node)
+               return -ENOMEM;
 
-               tmp_maps = krealloc(pinctrl_maps,
-                                   oldsize + newsize, GFP_KERNEL);
-               if (!tmp_maps)
-                       return -ENOMEM;
-               memcpy((tmp_maps + oldsize), maps, newsize);

So can we keep this codepath (with apropriate changes) and have the copy added to the list entry?

arch/arm/mach-u300/core.c int __init pinctrl_register_mappings(struct pinctrl_map const *maps, · line 726

Quoted 2 lines
arch/arm/mach-u300/core.c +1 -1
-       }
+       maps_node->maps = maps;

i.e. here.

Thanks,
Linus Walleij

#3 lore raw

Linus Walleij wrote at Thursday, February 16, 2012 12:54 PM:

Quoted 10 lines · Stephen Warren

arch/arm/mach-u300/core.c static struct platform_device pinmux_device = { · line 1555

Quoted 3 lines

arch/arm/mach-u300/core.c:

arch/arm/mach-u300/core.c +1 -1
-static struct pinctrl_map __initdata u300_pinmux_map[] = {
+static struct pinctrl_map u300_pinmux_map[] = {

This was actually not tagged __initdata until last merge window.

The reason it was made discardable was for platform that needed to discard a large number of mappings after boot, since only a few of them got registered from the board files.

Ah right, yes I see that makes sense. I'll revert that and add back the copy.

Quoted 1 line

(Yes, I know this will not be a problem in device tree configs...)

I'll create a helper function that doesn't do the copy for the DT case.

Quoted 11 lines

Can we combine the best of two worlds by still making a shallow copy (like previously) and put that shallow copy into the list instead?

Quoted 5 lines

+#define for_each_maps(_maps_node_, _i_, _map_) \
+       list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
+               for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
+                       _i_ < _maps_node_->num_maps; \
+                       i++, _map_ = &_maps_node_->maps[_i_])

I really like this macro.

Checkpatch doesn't :-( but I think it's OK to ignore it in this case.

Signature · 1 line
nvpublic