pinctrl: sunxi: Increase size of regulator array
Subsystems (3)
PATCH 1/2 pinctrl: sunxi: Increase size of regulator array
On the A80, the pin banks go up to PN, which translates to the 14th entry in the regulator array. The array is only 12 entries long, which causes the sunxi_pmx_{request,free} functions to access beyond the array on the A80 and the A31 (which has pin bank PM). While the accessed data is still valid allocated data within the same driver data structure, it is likely not a pointer.
Increase the size of the regulator array from 12 to 14. This is a simple fix. While we could have the code take into account the fact that R_PIO pin banks start from PL, or maybe even dynamically allocate the array based on the last pin of the pin controller, the size reduction probably isn't worth the additional code complexity.
---
This fixes a crash due to an invalid pointer on the A80.
--- drivers/pinctrl/sunxi/pinctrl-sunxi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pinctrl/sunxi/pinctrl-sunxi.h +1 -1
@@ -136,7 +136,7 @@ struct sunxi_pinctrl {struct gpio_chip *chip;const struct sunxi_pinctrl_desc *desc;struct device *dev; -struct sunxi_pinctrl_regulator regulators[12 ]; +struct sunxi_pinctrl_regulator regulators[14 ];struct irq_domain *domain;struct sunxi_pinctrl_function *functions;unsigned nfunctions;
Signature · 1 line
2.20.1
PATCH 2/2 pinctrl: sunxi: Fix and simplify pin bank regulator handling
The new per-pin-bank regulator handling code in the sunxi pinctrl driver has mismatched conditions for enabling and disabling the regulator: it is enabled each time a pin is requested, but only disabled when the pin-bank's reference count reaches zero.
Since we are doing reference counting already, there's no need to enable the regulator each time a pin is requested. Instead we can just do it for the first requested pin of each pin-bank. Thus we can reverse the test and bail out early if it's not the first occurrence.
---
I'm getting the feeling that the current code is prone to race conditions when pinctrl sets are actively switched during runtime, or gpios are requested and freed from userspace.
Any ideas?
--- drivers/pinctrl/sunxi/pinctrl-sunxi.c | 36 ++++++++++++--------------- 1 file changed, 16 insertions(+), 20 deletions(-)
drivers/pinctrl/sunxi/pinctrl-sunxi.c +16 -20
@@ -699,25 +699,21 @@ static int sunxi_pmx_request(struct pinctrl_dev *pctldev, unsigned offset)struct sunxi_pinctrl *pctl =pinctrl_dev_get_drvdata (pctldev);unsigned short bank = offset /PINS_PER_BANK ;struct sunxi_pinctrl_regulator *s_reg = &pctl->regulators[bank]; -struct regulator *reg; +struct regulator *reg = s_reg->regulator; +char supply[16 ];int ret; - reg = s_reg->regulator; -if (!reg) { -char supply[16 ]; - -snprintf (supply,sizeof (supply),"vcc-p%c" ,'a' + bank); - reg =regulator_get (pctl->dev, supply); -if (IS_ERR (reg)) { -dev_err (pctl->dev,"Couldn't get bank P%c regulator\n" , -'A' + bank); -return PTR_ERR (reg); - } - - s_reg->regulator = reg; -refcount_set (&s_reg->refcount,1 ); - }else { +if (reg) {refcount_inc (&s_reg->refcount); +return 0 ; + } + +snprintf (supply,sizeof (supply),"vcc-p%c" ,'a' + bank); + reg =regulator_get (pctl->dev, supply); +if (IS_ERR (reg)) { +dev_err (pctl->dev,"Couldn't get bank P%c regulator\n" , +'A' + bank); +return PTR_ERR (reg); } ret =regulator_enable (reg);
@@ -727,13 +723,13 @@ static int sunxi_pmx_request(struct pinctrl_dev *pctldev, unsigned offset) goto out; } + s_reg->regulator = reg; +refcount_set (&s_reg->refcount,1 ); +return 0 ; out: -if (refcount_dec_and_test (&s_reg->refcount)) { -regulator_put (s_reg->regulator); - s_reg->regulator =NULL ; - } +regulator_put (s_reg->regulator);return ret; }
Signature · 1 line
2.20.1
Re: [PATCH 2/2] pinctrl: sunxi: Fix and simplify pin bank regulator handling
Quoted 5 lines · Chen-Yu Tsai
The new per-pin-bank regulator handling code in the sunxi pinctrl driver has mismatched conditions for enabling and disabling the regulator: it is enabled each time a pin is requested, but only disabled when the pin-bank's reference count reaches zero.
Quoted 7 lines
Since we are doing reference counting already, there's no need to enable the regulator each time a pin is requested. Instead we can just do it for the first requested pin of each pin-bank. Thus we can reverse the test and bail out early if it's not the first occurrence.
Fixes 9a2a566adb00 ("pinctrl: sunxi: Deal with per-bank regulators")Signed-off-by Chen-Yu Tsai <[email protected]>
Quoted 5 lines
I'm getting the feeling that the current code is prone to race conditions when pinctrl sets are actively switched during runtime, or gpios are requested and freed from userspace.
Any ideas?
IIRC, I've looked into it when writing that patch and there's an upper mutex that prevents request from being called multiple times at once. Linus might prove me wrong though.
Maxime
Signature · 3 lines
Maxime Ripard, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Hi,
Quoted 7 lines · Chen-Yu Tsai
On the A80, the pin banks go up to PN, which translates to the 14th entry in the regulator array. The array is only 12 entries long, which causes the sunxi_pmx_{request,free} functions to access beyond the array on the A80 and the A31 (which has pin bank PM). While the accessed data is still valid allocated data within the same driver data structure, it is likely not a pointer.
Quoted 8 lines
Increase the size of the regulator array from 12 to 14. This is a simple fix. While we could have the code take into account the fact that
R_PIOpin banks start from PL, or maybe even dynamically allocate the array based on the last pin of the pin controller, the size reduction probably isn't worth the additional code complexity.Fixes 9a2a566adb00 ("pinctrl: sunxi: Deal with per-bank regulators")Signed-off-by Chen-Yu Tsai <[email protected]>
I definitely overlooked the R_PIO case, sorry. I guess the proper fix would be the first alternative one you suggested, and we should take the pin_base into account. There's no need to store twice such a large array for this case.
Maxime
Signature · 3 lines
Maxime Ripard, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Quoted 17 lines · Maxime Ripard
Hi,
Quoted 13 lines · Chen-Yu Tsai
On the A80, the pin banks go up to PN, which translates to the 14th entry in the regulator array. The array is only 12 entries long, which causes the sunxi_pmx_{request,free} functions to access beyond the array on the A80 and the A31 (which has pin bank PM). While the accessed data is still valid allocated data within the same driver data structure, it is likely not a pointer.
Increase the size of the regulator array from 12 to 14. This is a simple fix. While we could have the code take into account the fact that
R_PIOpin banks start from PL, or maybe even dynamically allocate the array based on the last pin of the pin controller, the size reduction probably isn't worth the additional code complexity.
Quoted 7 lines
Quoted 2 lines
Fixes 9a2a566adb00 ("pinctrl: sunxi: Deal with per-bank regulators")Signed-off-by Chen-Yu Tsai <[email protected]>I definitely overlooked the
R_PIOcase, sorry. I guess the proper fix would be the first alternative one you suggested, and we should take thepin_baseinto account. There's no need to store twice such a large array for this case.
OK.
I wonder if we should try to get rid of pin_base though, at least as far as the pinctrl core is concerned. In other words, in our description we'd still have PL0 == 352, but when the pin is registered, PL0 == 0. This is OK since each pinctrl device has its own numbering space. And it would match up with what we use for GPIO.
It would also be better to nail this down one way or the other if we want to use the gpio-ranges property.
Either way this would be a separate patch.
ChenYu