regulator: Fix locking, OOB read, lifetime, and global state bugs (batch 2)
cover regulator: Fix locking, OOB read, lifetime, and global state bugs (batch 2)
These issues were reported by Sashiko.
This is batch 2 of Regulator bug fixes, based on regulator/for-next. It addresses supply teardown and locking bugs in regulator core, slab and global out-of-bounds reads in tps6594 and tps65910, devres lifetime mismatches in tps6594, a NULL device_node dereference in of_regulator, and shared global table mutations in max8907 and aat2870.
Lee Jones (8):
regulator: core: Defer supply cleanup and unregister event forwarding
on error
regulator: max8907: Duplicate of_regulator_match table per device
regulator: tps6594-regulator: Use dedicated handler for external
regulator IRQs
regulator: tps65910: Fix OOB read and bit shift in external sleep
config
regulator: tps6594-regulator: Bind IRQ resources to child platform
device
regulator: core: Respect is_locked when disabling supply on enable
failure
regulator: of: Guard against NULL device_node in
of_regulator_bulk_get_all()
regulator: aat2870: Duplicate regulator data per device
drivers/regulator/aat2870-regulator.c | 30 +++++++++----- drivers/regulator/core.c | 18 ++++---- drivers/regulator/max8907-regulator.c | 57 +++++++++++--------------- drivers/regulator/of_regulator.c | 3 ++ drivers/regulator/tps65910-regulator.c | 20 +++++++-- drivers/regulator/tps6594-regulator.c | 27 +++++++++--- 6 files changed, 95 insertions(+), 60 deletions(-)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 1/8 regulator: core: Defer supply cleanup and unregister event forwarding on error
When enabling rdev->supply fails in set_machine_constraints(), the error path calls _regulator_put(rdev->supply) without holding regulator_list_mutex and clears rdev->supply to NULL. Neither regulator_register() nor regulator_resolve_supply() holds regulator_list_mutex here, and when called from regulator_resolve_supply() under regulator_lock_dependent(), clearing rdev->supply prevents regulator_unlock_dependent() from unlocking the supply chain and skips unregister_regulator_event_forwarding().
In addition, the wash error label in regulator_register() releases rdev->supply and drops the device reference without calling unregister_regulator_event_forwarding(), leaving a dangling notifier on the supply chain after rdev is freed.
Leave supply teardown in set_machine_constraints() to its callers, which already handle cleanup with proper locking, and unregister event forwarding before putting rdev->supply on the regulator_register() error path.
--- drivers/regulator/core.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/regulator/core.c +5 -5
@@ -1680,11 +1680,8 @@ static int set_machine_constraints(struct regulator_dev *rdev, ret = (is_locked ?_regulator_enable (rdev->supply,NULL ) :regulator_enable (rdev->supply)); -if (ret <0 ) { -_regulator_put (rdev->supply); - rdev->supply =NULL ; +if (ret <0 )return ret; - } supply_enabled = true; }
@@ -6392,7 +6389,10 @@ regulator_register(struct device *dev,regulator_remove_coupling (rdev);mutex_unlock (®ulator_list_mutex); wash: -regulator_put (rdev->supply); +if (rdev->supply) { +unregister_regulator_event_forwarding (rdev); +regulator_put (rdev->supply); + }kfree (rdev->coupling_desc.coupled_rdevs);mutex_lock (®ulator_list_mutex);regulator_ena_gpio_free (rdev);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 2/8 regulator: max8907: Duplicate of_regulator_match table per device
In reply to Lee Jonesmax8907_regulator_parse_dt() passes the static global max8907_matches array directly to of_regulator_match(), which populates init_data and of_node in place and registers a devres callback holding a pointer to that array. With multiple MAX8907 instances, a second probe overwrites the first instance's entries and subsequent unbinds invoke of_node_put() twice on the same device nodes, causing reference count underflow and use-after-free.
Mark max8907_matches const and duplicate it per device with devm_kmemdup() before calling of_regulator_match().
--- drivers/regulator/max8907-regulator.c | 57 +++++++++++---------------- 1 file changed, 24 insertions(+), 33 deletions(-)
drivers/regulator/max8907-regulator.c +24 -33
@@ -186,7 +186,7 @@ static const struct regulator_desc max8907_regulators[] = { .driver_data = (void *)&max8907_regulators[MAX8907_##_id], \ } -static struct of_regulator_match max8907_matches[] = { +static const struct of_regulator_match max8907_matches[] = {MATCH (mbatt,MBATT ),MATCH (sd1,SD1 ),MATCH (sd2,SD2 ),
@@ -218,54 +218,44 @@ static struct of_regulator_match max8907_matches[] = { MATCH(vrtc, VRTC), }; -static int max8907_regulator_parse_dt (struct platform_device *pdev) +static struct of_regulator_match * +max8907_regulator_parse_dt (struct platform_device *pdev) { +struct of_regulator_match *matches;struct device_node *np, *regulators;int ret; np = pdev->dev.parent->of_node;if (!np) -return 0 ; +return NULL ; regulators =of_get_child_by_name (np,"regulators" );if (!regulators) {dev_err (&pdev->dev,"regulators node not found\n" ); -return -EINVAL ; +return ERR_PTR (-EINVAL ); } - ret =of_regulator_match (&pdev->dev, regulators, max8907_matches, + matches =devm_kmemdup (&pdev->dev, max8907_matches, +sizeof (max8907_matches),GFP_KERNEL ); +if (!matches) { +of_node_put (regulators); +return ERR_PTR (-ENOMEM ); + } + + ret =of_regulator_match (&pdev->dev, regulators, matches,ARRAY_SIZE (max8907_matches));of_node_put (regulators);if (ret <0 ) {dev_err (&pdev->dev,"Error parsing regulator init data: %d\n" , ret); -return ret; +return ERR_PTR (ret); } -return 0 ; -} - -static inline struct regulator_init_data *match_init_data (int index) -{ -return max8907_matches[index].init_data; -} - -static inline struct device_node *match_of_node (int index) -{ -return max8907_matches[index].of_node; +return matches; }#else -static int max8907_regulator_parse_dt (struct platform_device *pdev) -{ -return 0 ; -} - -static inline struct regulator_init_data *match_init_data (int index) -{ -return NULL ; -} - -static inline struct device_node *match_of_node (int index) +static struct of_regulator_match * +max8907_regulator_parse_dt (struct platform_device *pdev) {return NULL ; }
@@ -280,12 +270,13 @@ static int max8907_regulator_probe(struct platform_device *pdev)unsigned int val;int i;struct regulator_config config = {}; +struct of_regulator_match *matches;struct regulator_init_data *idata;const char *mbatt_rail_name =NULL ; - ret =max8907_regulator_parse_dt (pdev); -if (ret) -return ret; + matches =max8907_regulator_parse_dt (pdev); +if (IS_ERR (matches)) +return PTR_ERR (matches); pmic =devm_kzalloc_obj (&pdev->dev, *pmic);if (!pmic)
@@ -315,11 +306,11 @@ static int max8907_regulator_probe(struct platform_device *pdev)if (pdata) idata = pdata->init_data[i];else - idata =match_init_data (i); + idata = matches ? matches[i].init_data :NULL ; config.init_data = idata; config.driver_data = pmic; config.regmap = max8907->regmap_gen; - config.of_node =match_of_node (i); + config.of_node = matches ? matches[i].of_node :NULL ;switch (pmic->desc[i].id) {case MAX8907_MBATT :
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 3/8 regulator: tps6594-regulator: Use dedicated handler for external regulator IRQs
In reply to Lee JonesExternal regulator and VMON interrupts are registered with a pointer to struct tps6594_ext_regulator_irq_data, which has no rdev member, yet they reuse tps6594_regulator_irq_handler(). That handler casts the pointer to struct tps6594_regulator_irq_data and reads irq_data->rdev out of bounds before passing it to regulator_notifier_call_chain().
Add a dedicated tps6594_ext_regulator_irq_handler() that operates on struct tps6594_ext_regulator_irq_data and logs the trap without calling regulator_notifier_call_chain().
--- drivers/regulator/tps6594-regulator.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/regulator/tps6594-regulator.c +18 -1
@@ -509,6 +509,23 @@ static irqreturn_t tps6594_regulator_irq_handler(int irq, void *data)return IRQ_HANDLED ; } +static irqreturn_t tps6594_ext_regulator_irq_handler (int irq,void *data) +{ +struct tps6594_ext_regulator_irq_data *irq_data = data; + +if (irq_data->type->event_name[0 ] =='\0' ) { +/* This is the timeout interrupt no specific regulator */ +dev_err (irq_data->dev, +"System was put in shutdown due to timeout during an active or standby transition.\n" ); +return IRQ_HANDLED ; + } + +dev_err (irq_data->dev,"Error IRQ trap %s for %s\n" , + irq_data->type->event_name, irq_data->type->regulator_name); + +return IRQ_HANDLED ; +} + static int tps6594_request_reg_irqs(struct platform_device *pdev, struct regulator_dev *rdev, struct tps6594_regulator_irq_data *irq_data,
@@ -827,7 +844,7 @@ static int tps6594_regulator_probe(struct platform_device *pdev) irq_ext_reg_data[i].type = irq_type; error =devm_request_threaded_irq (tps->dev, irq,NULL , - tps6594_regulator_irq_handler, + tps6594_ext_regulator_irq_handler,IRQF_ONESHOT , irq_type->irq_name, &irq_ext_reg_data[i]);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 4/8 regulator: tps65910: Fix OOB read and bit shift in external sleep config
In reply to Lee Jonestps65910_regs contains 14 regulators including VBB at index 13, whereas tps65910_ext_sleep_control has only 13 elements. Both tps65910_probe() and tps65910_shutdown() iterate up to pmic->num_regulators and index pmic->ext_sleep_control[id] out of bounds for VBB.
In addition, unsupported rails such as VRTC use 0, which collides with VMMC and LDO6 (offset 0, bit 0), and computing
(1 << pmic->ext_sleep_control[id] & 0xFF) shifts by more than 31 bits whenever regs_offs is non-zero because << binds tighter than &.
Introduce EXT_CONTROL_REG_NONE as a distinct sentinel for rails without external sleep control (VRTC and VBB), add BUILD_BUG_ON() array-size checks, return early in tps65910_set_ext_sleep_config() for unsupported rails, and use BIT(pmic->ext_sleep_control[id] & 0xFF) for bit_pos.
--- drivers/regulator/tps65910-regulator.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/regulator/tps65910-regulator.c +16 -4
@@ -271,9 +271,10 @@ static struct tps_info tps65911_regs[] = { }, }; +#define EXT_CONTROL_REG_NONE (~0U)#define EXT_CONTROL_REG_BITS (id, regs_offs, bits) (((regs_offs) << 8) | (bits))static unsigned int tps65910_ext_sleep_control[] = { -0 , +EXT_CONTROL_REG_NONE ,EXT_CONTROL_REG_BITS (VIO ,1 ,0 ),EXT_CONTROL_REG_BITS (VDD1 ,1 ,1 ),EXT_CONTROL_REG_BITS (VDD2 ,1 ,2 ),
@@ -286,10 +287,11 @@ static unsigned int tps65910_ext_sleep_control[] = {EXT_CONTROL_REG_BITS (VAUX2 ,0 ,4 ),EXT_CONTROL_REG_BITS (VAUX33 ,0 ,5 ),EXT_CONTROL_REG_BITS (VMMC ,0 ,0 ), + EXT_CONTROL_REG_NONE, };static unsigned int tps65911_ext_sleep_control[] = { -0 , +EXT_CONTROL_REG_NONE ,EXT_CONTROL_REG_BITS (VIO ,1 ,0 ),EXT_CONTROL_REG_BITS (VDD1 ,1 ,1 ), EXT_CONTROL_REG_BITS(VDD2, 1, 2),
@@ -822,10 +824,16 @@ static int tps65910_set_ext_sleep_config(struct tps65910_reg *pmic, int id, int ext_sleep_config) {struct tps65910 *mfd = pmic->mfd; -u8 regoffs = (pmic->ext_sleep_control[id] >>8 ) &0xFF ; -u8 bit_pos = (1 << pmic->ext_sleep_control[id] &0xFF ); +u8 regoffs; +u8 bit_pos;int ret; +if (pmic->ext_sleep_control[id] ==EXT_CONTROL_REG_NONE ) +return 0 ; + + regoffs = (pmic->ext_sleep_control[id] >>8 ) &0xFF ; + bit_pos =BIT (pmic->ext_sleep_control[id] &0xFF ); +/* * Regulator can not be control from multiple external input EN1, EN2 * and EN3 together.
@@ -1104,6 +1112,8 @@ static int tps65910_probe(struct platform_device *pdev)switch (tps65910_chip_id (tps65910)) {case TPS65910 :BUILD_BUG_ON (TPS65910_NUM_REGS <ARRAY_SIZE (tps65910_regs)); +BUILD_BUG_ON (ARRAY_SIZE (tps65910_regs) != +ARRAY_SIZE (tps65910_ext_sleep_control)); pmic->get_ctrl_reg = &tps65910_get_ctrl_register; pmic->num_regulators =ARRAY_SIZE (tps65910_regs); pmic->ext_sleep_control = tps65910_ext_sleep_control;
@@ -1117,6 +1127,8 @@ static int tps65910_probe(struct platform_device *pdev)break ;case TPS65911 :BUILD_BUG_ON (TPS65910_NUM_REGS <ARRAY_SIZE (tps65911_regs)); +BUILD_BUG_ON (ARRAY_SIZE (tps65911_regs) != +ARRAY_SIZE (tps65911_ext_sleep_control)); pmic->get_ctrl_reg = &tps65911_get_ctrl_register; pmic->num_regulators =ARRAY_SIZE (tps65911_regs); pmic->ext_sleep_control = tps65911_ext_sleep_control;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 5/8 regulator: tps6594-regulator: Bind IRQ resources to child platform device
In reply to Lee Jonestps6594_regulator_probe() and tps6594_request_reg_irqs() allocate IRQ data and request threaded IRQs using devres attached to the parent MFD device tps->dev, while registering the regulators with &pdev->dev. If probe fails partway through or the regulator platform device is unbound, the regulators are unregistered and freed while the threaded IRQs remain active with dangling rdev pointers and block subsequent probes.
Attach the devm_kmalloc_objs() allocations and devm_request_threaded_irq() handlers to &pdev->dev so LIFO devres teardown frees the IRQs before unregistering the regulators and freeing the IRQ data.
--- drivers/regulator/tps6594-regulator.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/regulator/tps6594-regulator.c +4 -4
@@ -549,7 +549,7 @@ static int tps6594_request_reg_irqs(struct platform_device *pdev, irq_data[*irq_idx].type = irq_type; irq_data[*irq_idx].rdev = rdev; - error =devm_request_threaded_irq (tps->dev, irq,NULL , + error =devm_request_threaded_irq (&pdev->dev, irq,NULL , tps6594_regulator_irq_handler,IRQF_ONESHOT , irq_type->irq_name, &irq_data[*irq_idx]);if (error) {
@@ -738,7 +738,7 @@ static int tps6594_regulator_probe(struct platform_device *pdev) reg_irq_nb = desc->num_irq_types * (desc->num_buck_regs + desc->num_ldo_regs); - irq_data =devm_kmalloc_objs (tps->dev, + irq_data =devm_kmalloc_objs (&pdev->dev, struct tps6594_regulator_irq_data, reg_irq_nb);if (!irq_data)
@@ -828,7 +828,7 @@ static int tps6594_regulator_probe(struct platform_device *pdev)return error; } - irq_ext_reg_data =devm_kmalloc_objs (tps->dev, + irq_ext_reg_data =devm_kmalloc_objs (&pdev->dev, struct tps6594_ext_regulator_irq_data, desc->num_ext_irqs);if (!irq_ext_reg_data)
@@ -843,7 +843,7 @@ static int tps6594_regulator_probe(struct platform_device *pdev) irq_ext_reg_data[i].dev = tps->dev; irq_ext_reg_data[i].type = irq_type; - error = devm_request_threaded_irq(tps->dev, irq, NULL, + error = devm_request_threaded_irq(&pdev->dev, irq, NULL, tps6594_ext_regulator_irq_handler, IRQF_ONESHOT, irq_type->irq_name,
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 6/8 regulator: core: Respect is_locked when disabling supply on enable failure
In reply to Lee JonesWhen set_machine_constraints() is called with is_locked set from regulator_resolve_supply(), it already holds regulator_lock_dependent() and uses _regulator_enable() to enable rdev->supply. If _regulator_do_enable() then fails, however, the error path calls regulator_disable() unconditionally, re-entering regulator_lock_dependent() and inverting regulator_list_mutex against the held ww_mutex locks.
Use _regulator_disable() when is_locked is true, matching the enable path above.
--- drivers/regulator/core.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/regulator/core.c +6 -2
@@ -1688,8 +1688,12 @@ static int set_machine_constraints(struct regulator_dev *rdev, ret =_regulator_do_enable (rdev);if (ret <0 && ret != -EINVAL ) {rdev_err (rdev,"failed to enable: %pe\n" ,ERR_PTR (ret)); -if (supply_enabled) -regulator_disable (rdev->supply); +if (supply_enabled) { +if (is_locked) +_regulator_disable (rdev->supply); +else +regulator_disable (rdev->supply); + }return ret; }
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 7/8 regulator: of: Guard against NULL device_node in of_regulator_bulk_get_all()
In reply to Lee Jonesof_regulator_bulk_get_all() passes np straight into for_each_property_of_node(), which dereferences np->properties without checking for NULL. Callers passing dev_of_node(dev) on non-DT devices therefore crash with a NULL pointer dereference.
Return 0 immediately when np is NULL, matching the !CONFIG_OF stub and the empty-supply case.
--- drivers/regulator/of_regulator.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/regulator/of_regulator.c +3 -0
@@ -947,6 +947,9 @@ int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,char *names;int i, n =0 , ret; +if (!np) +return 0 ; +/* * first pass: get numbers of xxx-supply and the room their names take * second pass: fill consumers
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 8/8 regulator: aat2870: Duplicate regulator data per device
In reply to Lee Jonesaat2870_get_regulator() returns a pointer into the static global aat2870_regulators array and mutates its fields in place, after which aat2870_regulator_probe() stores the parent MFD pointer into that shared entry. Probing a second AAT2870 device overwrites the first device's state and leads to misrouted operations or use-after-free on removal.
Mark aat2870_regulators const and duplicate the matching entry per device with devm_kmemdup() before registering the regulator.
--- drivers/regulator/aat2870-regulator.c | 30 +++++++++++++++++---------- 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/regulator/aat2870-regulator.c +19 -11
@@ -114,26 +114,33 @@ static const unsigned int aat2870_ldo_voltages[] = { }, \ } -static struct aat2870_regulator aat2870_regulators[] = { +static const struct aat2870_regulator aat2870_regulators[] = {AAT2870_LDO (LDOA ),AAT2870_LDO (LDOB ),AAT2870_LDO (LDOC ),AAT2870_LDO (LDOD ), }; -static struct aat2870_regulator *aat2870_get_regulator (int id) +static struct aat2870_regulator *aat2870_get_regulator (struct device *dev, +int id) { -struct aat2870_regulator *ri =NULL ; +const struct aat2870_regulator *template =NULL ; +struct aat2870_regulator *ri;int i;for (i =0 ; i <ARRAY_SIZE (aat2870_regulators); i++) { - ri = &aat2870_regulators[i]; -if (ri->desc.id == id) +if (aat2870_regulators[i].desc.id == id) { + template = &aat2870_regulators[i];break ; + } } -if (i ==ARRAY_SIZE (aat2870_regulators)) -return NULL ; +if (!template) +return ERR_PTR (-EINVAL ); + + ri =devm_kmemdup (dev, template,sizeof (*ri),GFP_KERNEL ); +if (!ri) +return ERR_PTR (-ENOMEM ); ri->enable_addr =AAT2870_LDO_EN ; ri->enable_shift = id -AAT2870_ID_LDOA ;
@@ -153,10 +160,11 @@ static int aat2870_regulator_probe(struct platform_device *pdev)struct regulator_config config = { };struct regulator_dev *rdev; - ri =aat2870_get_regulator (pdev->id); -if (!ri) { -dev_err (&pdev->dev,"Invalid device ID, %d\n" , pdev->id); -return -EINVAL ; + ri =aat2870_get_regulator (&pdev->dev, pdev->id); +if (IS_ERR (ri)) { +if (PTR_ERR (ri) == -EINVAL ) +dev_err (&pdev->dev,"Invalid device ID, %d\n" , pdev->id); +return PTR_ERR (ri); } ri->aat2870 =dev_get_drvdata (pdev->dev.parent);
Signature · 1 line
2.56.1.839.g22a8290db9-goog