Skip to contentSkip to conversation

pinctrl fixes for generic functions and groups

PATCH In review Tony Lindgren 4 participants 10 messages Jul 5, 2018 – Jul 9, 2018

Subsystems (3)
lore
Share & tools
lore

Tony Lindgren

author maintainer
#0 lore raw

v3 cover pinctrl fixes for generic functions and groups

Here is a resend of fixes for a race issues for generic group and functions reported by H. Nikolaus Schaller <[email protected]>.

Regards,

Tony

Changes since v2:

- Added Nikolaus to Cc as Reported-by did not do it

Changes since v1:

- Check if a function or group already exists as suggested by Andy Shevchenko

- Make sure we always have a valid name for functions and groups as suggested by Christ van Willegen

- Prettify rza1 variables as suggested by Jacopo Mondi

Tony Lindgren (5):
pinctrl: core: Return selector to the pinctrl driver
pinctrl: pinmux: Return selector to the pinctrl driver
pinctrl: single: Fix group and function selector use
pinctrl: rza1: Fix selector use for groups and functions
pinctrl: core: Remove broken remove_last group and pinmux functions

 drivers/pinctrl/core.c           | 35 ++++++++++--
 drivers/pinctrl/core.h           |  6 ---
 drivers/pinctrl/pinctrl-rza1.c   | 24 +++++----
 drivers/pinctrl/pinctrl-single.c | 91 +++++++++++++++++++-------------
 drivers/pinctrl/pinmux.c         | 16 ++++--
 drivers/pinctrl/pinmux.h         |  7 ---
 6 files changed, 112 insertions(+), 67 deletions(-)
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH v3 1/5 pinctrl: core: Return selector to the pinctrl driver

We must return the selector from pinctrl_generic_add_group() so pin controller device drivers can remove the right group if needed for deferred probe for example. And we now must make sure that a proper name is passed so we can use it to check if the entry already exists.

Note that fixes are also needed for the pin controller drivers to use the selector value.

Fixes c7059c5ac70a ("pinctrl: core: Add generic pinctrl functions

for managing groups")

Cc Andy Shevchenko <[email protected]>
Cc Christ van Willegen <[email protected]>
Cc Haojian Zhuang <[email protected]>
Cc H. Nikolaus Schaller <[email protected]>
Cc Jacopo Mondi <[email protected]>
Cc Paul Cercueil <[email protected]>
Cc Sean Wang <[email protected]>
---
 drivers/pinctrl/core.c | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)
drivers/pinctrl/core.c +32 -3
@@ -616,6 +616,26 @@ struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
 }
 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
 
+static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev,
+						  const char *function)
+{
+	const struct pinctrl_ops *ops = pctldev->desc->pctlops;
+	int ngroups = ops->get_groups_count(pctldev);
+	int selector = 0;
+
+	/* See if this pctldev has this group */
+	while (selector < ngroups) {
+		const char *gname = ops->get_group_name(pctldev, selector);
+
+		if (!strcmp(function, gname))
+			return selector;
+
+		selector++;
+	}
+
+	return -EINVAL;
+}
+
 /**
  * pinctrl_generic_add_group() - adds a new pin group
  * @pctldev: pin controller device
@@ -630,6 +650,16 @@ int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
 			      int *pins, int num_pins, void *data)
 {
 	struct group_desc *group;
+	int selector;
+
+	if (!name)
+		return -EINVAL;
+
+	selector = pinctrl_generic_group_name_to_selector(pctldev, name);
+	if (selector >= 0)
+		return selector;
+
+	selector = pctldev->num_groups;
 
 	group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
 	if (!group)
@@ -640,12 +670,11 @@ int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
 	group->num_pins = num_pins;
 	group->data = data;
 
-	radix_tree_insert(&pctldev->pin_group_tree, pctldev->num_groups,
-			  group);
+	radix_tree_insert(&pctldev->pin_group_tree, selector, group);
 
 	pctldev->num_groups++;
 
-	return 0;
+	return selector;
 }
 EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
 
Signature · 1 line
2.17.1
author maintainer
#2 lore raw

PATCH v3 2/5 pinctrl: pinmux: Return selector to the pinctrl driver

In reply to Tony Lindgren

We must return the selector from pinmux_generic_add_function() so pin controller device drivers can remove the right group if needed for deferred probe for example. And we now must make sure that a proper name is passed so we can use it to check if the entry already exists.

Note that fixes are also needed for the pin controller drivers to use the selector value.

Fixes a76edc89b100 ("pinctrl: core: Add generic pinctrl functions for

managing groups")

Cc Andy Shevchenko <[email protected]>
Cc Christ van Willegen <[email protected]>
Cc Haojian Zhuang <[email protected]>
Cc H. Nikolaus Schaller <[email protected]>
Cc Jacopo Mondi <[email protected]>
Cc Paul Cercueil <[email protected]>
Cc Sean Wang <[email protected]>
---
 drivers/pinctrl/pinmux.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/pinctrl/pinmux.c +12 -4
@@ -307,7 +307,6 @@ static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
 		selector++;
 	}
 
-	dev_err(pctldev->dev, "function '%s' not supported\n", function);
 	return -EINVAL;
 }
 
@@ -774,6 +773,16 @@ int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
 				void *data)
 {
 	struct function_desc *function;
+	int selector;
+
+	if (!name)
+		return -EINVAL;
+
+	selector = pinmux_func_name_to_selector(pctldev, name);
+	if (selector >= 0)
+		return selector;
+
+	selector = pctldev->num_functions;
 
 	function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
 	if (!function)
@@ -784,12 +793,11 @@ int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
 	function->num_group_names = num_groups;
 	function->data = data;
 
-	radix_tree_insert(&pctldev->pin_function_tree, pctldev->num_functions,
-			  function);
+	radix_tree_insert(&pctldev->pin_function_tree, selector, function);
 
 	pctldev->num_functions++;
 
-	return 0;
+	return selector;
 }
 EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
 
Signature · 1 line
2.17.1
author maintainer
#3 lore raw

PATCH v3 3/5 pinctrl: single: Fix group and function selector use

In reply to Tony Lindgren

We must use a mutex around the generic_add functions and save the function and group selector in case we need to remove them. Otherwise the selector use will be racy for deferred probe at least.

Note that struct device_node *np is unused in pcs_add_function() we remove that too and fix a checkpatch warning for bare unsigned while at it.

Fixes 571aec4df5b7 ("pinctrl: single: Use generic pinmux helpers for

managing functions")

Cc Andy Shevchenko <[email protected]>
Cc Christ van Willegen <[email protected]>
Cc Haojian Zhuang <[email protected]>
Cc H. Nikolaus Schaller <[email protected]>
Cc Jacopo Mondi <[email protected]>
Cc Paul Cercueil <[email protected]>
Cc Sean Wang <[email protected]>
---
 drivers/pinctrl/pinctrl-single.c | 91 +++++++++++++++++++-------------
 1 file changed, 55 insertions(+), 36 deletions(-)
drivers/pinctrl/pinctrl-single.c +55 -36
@@ -747,38 +747,44 @@ static int pcs_allocate_pin_table(struct pcs_device *pcs)
 /**
  * pcs_add_function() - adds a new function to the function list
  * @pcs: pcs driver instance
- * @np: device node of the mux entry
+ * @fcn: new function allocated
  * @name: name of the function
  * @vals: array of mux register value pairs used by the function
  * @nvals: number of mux register value pairs
  * @pgnames: array of pingroup names for the function
  * @npgnames: number of pingroup names
+ *
+ * Caller must take care of locking.
  */
-static struct pcs_function *pcs_add_function(struct pcs_device *pcs,
-					struct device_node *np,
-					const char *name,
-					struct pcs_func_vals *vals,
-					unsigned nvals,
-					const char **pgnames,
-					unsigned npgnames)
+static int pcs_add_function(struct pcs_device *pcs,
+			    struct pcs_function **fcn,
+			    const char *name,
+			    struct pcs_func_vals *vals,
+			    unsigned int nvals,
+			    const char **pgnames,
+			    unsigned int npgnames)
 {
 	struct pcs_function *function;
-	int res;
+	int selector;
 
 	function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL);
 	if (!function)
-		return NULL;
+		return -ENOMEM;
 
 	function->vals = vals;
 	function->nvals = nvals;
 
-	res = pinmux_generic_add_function(pcs->pctl, name,
-					  pgnames, npgnames,
-					  function);
-	if (res)
-		return NULL;
+	selector = pinmux_generic_add_function(pcs->pctl, name,
+					       pgnames, npgnames,
+					       function);
+	if (selector < 0) {
+		devm_kfree(pcs->dev, function);
+		*fcn = NULL;
+	} else {
+		*fcn = function;
+	}
 
-	return function;
+	return selector;
 }
 
 /**
@@ -979,8 +985,8 @@ static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
 {
 	const char *name = "pinctrl-single,pins";
 	struct pcs_func_vals *vals;
-	int rows, *pins, found = 0, res = -ENOMEM, i;
-	struct pcs_function *function;
+	int rows, *pins, found = 0, res = -ENOMEM, i, fsel, gsel;
+	struct pcs_function *function = NULL;
 
 	rows = pinctrl_count_index_with_args(np, name);
 	if (rows <= 0) {
@@ -1030,21 +1036,25 @@ static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
 	}
 
 	pgnames[0] = np->name;
-	function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
-	if (!function) {
-		res = -ENOMEM;
+	mutex_lock(&pcs->mutex);
+	fsel = pcs_add_function(pcs, &function, np->name, vals, found,
+				pgnames, 1);
+	if (fsel < 0) {
+		res = fsel;
 		goto free_pins;
 	}
 
-	res = pinctrl_generic_add_group(pcs->pctl, np->name, pins, found, pcs);
-	if (res < 0)
+	gsel = pinctrl_generic_add_group(pcs->pctl, np->name, pins, found, pcs);
+	if (gsel < 0) {
+		res = gsel;
 		goto free_function;
+	}
 
 	(*map)->type = PIN_MAP_TYPE_MUX_GROUP;
 	(*map)->data.mux.group = np->name;
 	(*map)->data.mux.function = np->name;
 
-	if (PCS_HAS_PINCONF) {
+	if (PCS_HAS_PINCONF && function) {
 		res = pcs_parse_pinconf(pcs, np, function, map);
 		if (res)
 			goto free_pingroups;
@@ -1052,14 +1062,16 @@ static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
 	} else {
 		*num_maps = 1;
 	}
+	mutex_unlock(&pcs->mutex);
+
 	return 0;
 
 free_pingroups:
-	pinctrl_generic_remove_last_group(pcs->pctl);
+	pinctrl_generic_remove_group(pcs->pctl, gsel);
 	*num_maps = 1;
 free_function:
-	pinmux_generic_remove_last_function(pcs->pctl);
-
+	pinmux_generic_remove_function(pcs->pctl, fsel);
+	mutex_unlock(&pcs->mutex);
 free_pins:
 	devm_kfree(pcs->dev, pins);
 
@@ -1077,9 +1089,9 @@ static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
 {
 	const char *name = "pinctrl-single,bits";
 	struct pcs_func_vals *vals;
-	int rows, *pins, found = 0, res = -ENOMEM, i;
+	int rows, *pins, found = 0, res = -ENOMEM, i, fsel, gsel;
 	int npins_in_row;
-	struct pcs_function *function;
+	struct pcs_function *function = NULL;
 
 	rows = pinctrl_count_index_with_args(np, name);
 	if (rows <= 0) {
@@ -1166,15 +1178,19 @@ static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
 	}
 
 	pgnames[0] = np->name;
-	function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
-	if (!function) {
-		res = -ENOMEM;
+	mutex_lock(&pcs->mutex);
+	fsel = pcs_add_function(pcs, &function, np->name, vals, found,
+				pgnames, 1);
+	if (fsel < 0) {
+		res = fsel;
 		goto free_pins;
 	}
 
-	res = pinctrl_generic_add_group(pcs->pctl, np->name, pins, found, pcs);
-	if (res < 0)
+	gsel = pinctrl_generic_add_group(pcs->pctl, np->name, pins, found, pcs);
+	if (gsel < 0) {
+		res = gsel;
 		goto free_function;
+	}
 
 	(*map)->type = PIN_MAP_TYPE_MUX_GROUP;
 	(*map)->data.mux.group = np->name;
@@ -1186,13 +1202,16 @@ static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
 	}
 
 	*num_maps = 1;
+	mutex_unlock(&pcs->mutex);
+
 	return 0;
 
 free_pingroups:
-	pinctrl_generic_remove_last_group(pcs->pctl);
+	pinctrl_generic_remove_group(pcs->pctl, gsel);
 	*num_maps = 1;
 free_function:
-	pinmux_generic_remove_last_function(pcs->pctl);
+	pinmux_generic_remove_function(pcs->pctl, fsel);
+	mutex_unlock(&pcs->mutex);
 free_pins:
 	devm_kfree(pcs->dev, pins);
 
Signature · 1 line
2.17.1
author maintainer
#4 lore raw

PATCH v3 4/5 pinctrl: rza1: Fix selector use for groups and functions

In reply to Tony Lindgren

We must use a mutex around the generic_add functions and save the function and group selector in case we need to remove them. Otherwise the selector use will be racy for deferred probe at least.

Fixes 5a49b644b307 ("pinctrl: Renesas RZ/A1 pin and gpio controller")
Cc Andy Shevchenko <[email protected]>
Cc Christ van Willegen <[email protected]>
Cc Haojian Zhuang <[email protected]>
Cc H. Nikolaus Schaller <[email protected]>
Cc Paul Cercueil <[email protected]>
Cc Sean Wang <[email protected]>
---
 drivers/pinctrl/pinctrl-rza1.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
drivers/pinctrl/pinctrl-rza1.c +13 -11
@@ -1006,6 +1006,7 @@ static int rza1_dt_node_to_map(struct pinctrl_dev *pctldev,
 	const char *grpname;
 	const char **fngrps;
 	int ret, npins;
+	int gsel, fsel;
 
 	npins = rza1_dt_node_pin_count(np);
 	if (npins < 0) {
@@ -1055,18 +1056,19 @@ static int rza1_dt_node_to_map(struct pinctrl_dev *pctldev,
 	fngrps[0] = grpname;
 
 	mutex_lock(&rza1_pctl->mutex);
-	ret = pinctrl_generic_add_group(pctldev, grpname, grpins, npins,
-					NULL);
-	if (ret) {
+	gsel = pinctrl_generic_add_group(pctldev, grpname, grpins, npins,
+					 NULL);
+	if (gsel < 0) {
 		mutex_unlock(&rza1_pctl->mutex);
-		return ret;
+		return gsel;
 	}
 
-	ret = pinmux_generic_add_function(pctldev, grpname, fngrps, 1,
-					  mux_confs);
-	if (ret)
+	fsel = pinmux_generic_add_function(pctldev, grpname, fngrps, 1,
+					   mux_confs);
+	if (fsel < 0) {
+		ret = fsel;
 		goto remove_group;
-	mutex_unlock(&rza1_pctl->mutex);
+	}
 
 	dev_info(rza1_pctl->dev, "Parsed function and group %s with %d pins\n",
 				 grpname, npins);
@@ -1083,15 +1085,15 @@ static int rza1_dt_node_to_map(struct pinctrl_dev *pctldev,
 	(*map)->data.mux.group = np->name;
 	(*map)->data.mux.function = np->name;
 	*num_maps = 1;
+	mutex_unlock(&rza1_pctl->mutex);
 
 	return 0;
 
 remove_function:
-	mutex_lock(&rza1_pctl->mutex);
-	pinmux_generic_remove_last_function(pctldev);
+	pinmux_generic_remove_function(pctldev, fsel);
 
 remove_group:
-	pinctrl_generic_remove_last_group(pctldev);
+	pinctrl_generic_remove_group(pctldev, gsel);
 	mutex_unlock(&rza1_pctl->mutex);
 
 	dev_info(rza1_pctl->dev, "Unable to parse function and group %s\n",
Signature · 1 line
2.17.1
author maintainer
#5 lore raw

PATCH v3 5/5 pinctrl: core: Remove broken remove_last group and pinmux functions

In reply to Tony Lindgren

With no users left for these functions let's remove them.

Cc Andy Shevchenko <[email protected]>
Cc Christ van Willegen <[email protected]>
Cc Haojian Zhuang <[email protected]>
Cc H. Nikolaus Schaller <[email protected]>
Cc Jacopo Mondi <[email protected]>
Cc Paul Cercueil <[email protected]>
Cc Sean Wang <[email protected]>
---
 drivers/pinctrl/core.h   | 6 ------
 drivers/pinctrl/pinmux.h | 7 -------
 2 files changed, 13 deletions(-)
drivers/pinctrl/core.h +0 -6
@@ -218,12 +218,6 @@ int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
 int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
 				 unsigned int group_selector);
 
-static inline int
-pinctrl_generic_remove_last_group(struct pinctrl_dev *pctldev)
-{
-	return pinctrl_generic_remove_group(pctldev, pctldev->num_groups - 1);
-}
-
 #endif	/* CONFIG_GENERIC_PINCTRL_GROUPS */
 
 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *dev_name);
drivers/pinctrl/pinmux.h +0 -7
@@ -150,13 +150,6 @@ int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
 int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
 				   unsigned int selector);
 
-static inline int
-pinmux_generic_remove_last_function(struct pinctrl_dev *pctldev)
-{
-	return pinmux_generic_remove_function(pctldev,
-					      pctldev->num_functions - 1);
-}
-
 void pinmux_generic_free_functions(struct pinctrl_dev *pctldev);
 
 #else
Signature · 1 line
2.17.1
Tested-by on the v3 series
#6 lore raw
In reply to Tony Lindgren

Hi Tony and Linus,

Quoted 4 lines

Am 05.07.2018 um 11:10 schrieb Tony Lindgren <[email protected]>:

Here is a resend of fixes for a race issues for generic group and functions reported by H. Nikolaus Schaller <[email protected]>.

Thanks for adding me.

I have tested these patches on our OMAP3 based device where the problem was quite reproducible and testable by adding some printk. With these patches it seems now to be correct. Each group selector is assigned only once, even if driver probing fails and is redone later.

Code review of the call paths has convinced us that we now have proper mutex around the radix tree function calls and races are no longer a problem.

So you can add my

BR and thanks,
Nikolaus

Quoted 37 lines

Regards,

Tony

Changes since v2:

- Added Nikolaus to Cc as Reported-by did not do it

Changes since v1:

- Check if a function or group already exists as suggested by Andy Shevchenko

- Make sure we always have a valid name for functions and groups as suggested by Christ van Willegen

- Prettify rza1 variables as suggested by Jacopo Mondi

Tony Lindgren (5):
pinctrl: core: Return selector to the pinctrl driver
pinctrl: pinmux: Return selector to the pinctrl driver
pinctrl: single: Fix group and function selector use
pinctrl: rza1: Fix selector use for groups and functions
pinctrl: core: Remove broken remove_last group and pinmux functions

drivers/pinctrl/core.c           | 35 ++++++++++--
drivers/pinctrl/core.h           |  6 ---
drivers/pinctrl/pinctrl-rza1.c   | 24 +++++----
drivers/pinctrl/pinctrl-single.c | 91 +++++++++++++++++++-------------
drivers/pinctrl/pinmux.c         | 16 ++++--
drivers/pinctrl/pinmux.h         |  7 ---
6 files changed, 112 insertions(+), 67 deletions(-)
Signature · 1 line
2.17.1
Reviewed-by on the v3 series
#7 lore raw
In reply to Tony Lindgren
Quoted 3 lines · Tony Lindgren

Here is a resend of fixes for a race issues for generic group and functions reported by H. Nikolaus Schaller <[email protected]>.

I think for ACPI case we would use generic helpers, so this change is quite useful to not screw up with new stuff.

Quoted 36 lines

Regards,

Tony

Changes since v2:

- Added Nikolaus to Cc as Reported-by did not do it

Changes since v1:

- Check if a function or group already exists as suggested by Andy Shevchenko

- Make sure we always have a valid name for functions and groups as suggested by Christ van Willegen

- Prettify rza1 variables as suggested by Jacopo Mondi

Tony Lindgren (5):
pinctrl: core: Return selector to the pinctrl driver
pinctrl: pinmux: Return selector to the pinctrl driver
pinctrl: single: Fix group and function selector use
pinctrl: rza1: Fix selector use for groups and functions
pinctrl: core: Remove broken remove_last group and pinmux functions

 drivers/pinctrl/core.c           | 35 ++++++++++--
 drivers/pinctrl/core.h           |  6 ---
 drivers/pinctrl/pinctrl-rza1.c   | 24 +++++----
 drivers/pinctrl/pinctrl-single.c | 91 +++++++++++++++++++-------------
 drivers/pinctrl/pinmux.c         | 16 ++++--
 drivers/pinctrl/pinmux.h         |  7 ---
 6 files changed, 112 insertions(+), 67 deletions(-)

--
2.17.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
In reply to Tony Lindgren
Quoted 2 lines · Tony Lindgren

Here is a resend of fixes for a race issues for generic group and functions reported by H. Nikolaus Schaller <[email protected]>.

I applied these 5 for fixes (no choice I guess) will push them to the zeroday build for testing and then to next. Added the tags from H. Nikolaus and Andy on this.

Should this stuff even be tagged for stable? Else I target it for v4.18.

Yours,
Linus Walleij

author maintainer
#9 lore raw
Quoted 8 lines · Linus Walleij
Quoted 2 lines · Tony Lindgren

Here is a resend of fixes for a race issues for generic group and functions reported by H. Nikolaus Schaller <[email protected]>.

I applied these 5 for fixes (no choice I guess) will push them to the zeroday build for testing and then to next. Added the tags from H. Nikolaus and Andy on this.

OK thanks.

Quoted 2 lines

Should this stuff even be tagged for stable? Else I target it for v4.18.

Probably should. The fixes tags should do the trick already hopefully though.

Regards,

Tony