iio: adc: fsl-imx25-gcq: check the return value of the
Subsystems (2)
PATCH 1/1 iio: adc: fsl-imx25-gcq: check the return value of the
The driver ignores the return value of all the regmap accesses. If one of them fails, e.g. because the memory mapped registers can't be accessed, the driver just continues with an undefined state.
The most severe case is the read of the FIFO in mx25_gcq_get_raw_value(): the destination variable is not initialized, so a failed read leaks uninitialized stack memory to userspace.
Check the return value of all the register accesses and propagate the errors to the caller. In the interrupt handler, where there is not much that can be done about a failed access, just don't use the content of the status register if it can't be read.
--- drivers/iio/adc/fsl-imx25-gcq.c | 82 ++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 22 deletions(-)
drivers/iio/adc/fsl-imx25-gcq.c +60 -22
@@ -83,7 +83,13 @@ static irqreturn_t mx25_gcq_irq(int irq, void *data)struct mx25_gcq_priv *priv = data;u32 stats; -regmap_read (priv->regs,MX25_ADCQ_SR , &stats); +/* +* If the status can't be read we don't know whether the conversion +* has finished. The interrupt is still acknowledged below, so just +* skip the completion and let the reader time out. +*/ +if (regmap_read (priv->regs,MX25_ADCQ_SR , &stats)) + stats =0 ;if (stats &MX25_ADCQ_SR_EOQ ) { regmap_set_bits(priv->regs, MX25_ADCQ_MR,
@@ -109,15 +115,29 @@ static int mx25_gcq_get_raw_value(struct device *dev, {long time_left;u32 data; +int ret;/* Setup the configuration we want to use */ -regmap_write (priv->regs,MX25_ADCQ_ITEM_7_0 , -MX25_ADCQ_ITEM (0 , chan->channel)); + ret =regmap_write (priv->regs,MX25_ADCQ_ITEM_7_0 , +MX25_ADCQ_ITEM (0 , chan->channel)); +if (ret) { +dev_err (dev,"Failed to setup the queue item: %d\n" , ret); +return ret; + } -regmap_clear_bits (priv->regs,MX25_ADCQ_MR ,MX25_ADCQ_MR_EOQ_IRQ ); + ret =regmap_clear_bits (priv->regs,MX25_ADCQ_MR , +MX25_ADCQ_MR_EOQ_IRQ ); +if (ret) { +dev_err (dev,"Failed to clear the EoQ interrupt: %d\n" , ret); +return ret; + }/* Trigger queue for one run */ -regmap_set_bits (priv->regs,MX25_ADCQ_CR ,MX25_ADCQ_CR_FQS ); + ret =regmap_set_bits (priv->regs,MX25_ADCQ_CR ,MX25_ADCQ_CR_FQS ); +if (ret) { +dev_err (dev,"Failed to trigger the queue: %d\n" , ret); +return ret; + } time_left =wait_for_completion_interruptible_timeout ( &priv->completed,MX25_GCQ_TIMEOUT );
@@ -129,7 +149,11 @@ static int mx25_gcq_get_raw_value(struct device *dev,return -ETIMEDOUT ; } -regmap_read (priv->regs,MX25_ADCQ_FIFO , &data); + ret =regmap_read (priv->regs,MX25_ADCQ_FIFO , &data); +if (ret) { +dev_err (dev,"Failed to read the FIFO: %d\n" , ret); +return ret; + } *val =MX25_ADCQ_FIFO_DATA (data);
@@ -204,14 +228,18 @@ static int mx25_gcq_setup_cfgs(struct platform_device *pdev,* Setup all configurations registers with a default conversion * configuration for each input */ -for (i =0 ; i <MX25_NUM_CFGS ; ++i) -regmap_write (priv->regs,MX25_ADCQ_CFG (i), -MX25_ADCQ_CFG_YPLL_OFF | -MX25_ADCQ_CFG_XNUR_OFF | -MX25_ADCQ_CFG_XPUL_OFF | -MX25_ADCQ_CFG_REFP_INT | -MX25_ADCQ_CFG_IN (i) | -MX25_ADCQ_CFG_REFN_NGND2 ); +for (i =0 ; i <MX25_NUM_CFGS ; ++i) { + ret =regmap_write (priv->regs,MX25_ADCQ_CFG (i), +MX25_ADCQ_CFG_YPLL_OFF | +MX25_ADCQ_CFG_XNUR_OFF | +MX25_ADCQ_CFG_XPUL_OFF | +MX25_ADCQ_CFG_REFP_INT | +MX25_ADCQ_CFG_IN (i) | +MX25_ADCQ_CFG_REFN_NGND2 ); +if (ret) +return dev_err_probe (dev, ret, +"Failed to write the configuration register\n" ); + }device_for_each_child_node_scoped (dev, child) {u32 reg;
@@ -265,16 +293,26 @@ static int mx25_gcq_setup_cfgs(struct platform_device *pdev,return dev_err_probe (dev, -EINVAL ,"Invalid fsl,adc-refn property value\n" ); -regmap_update_bits (priv->regs,MX25_ADCQ_CFG (reg), -MX25_ADCQ_CFG_REFP_MASK | -MX25_ADCQ_CFG_REFN_MASK , - refp | refn); + ret =regmap_update_bits (priv->regs,MX25_ADCQ_CFG (reg), +MX25_ADCQ_CFG_REFP_MASK | +MX25_ADCQ_CFG_REFN_MASK , + refp | refn); +if (ret) +return dev_err_probe (dev, ret, +"Failed to update the configuration register\n" ); } -regmap_set_bits (priv->regs,MX25_ADCQ_CR , -MX25_ADCQ_CR_FRST |MX25_ADCQ_CR_QRST ); -regmap_write (priv->regs,MX25_ADCQ_CR , -MX25_ADCQ_CR_PDMSK |MX25_ADCQ_CR_QSM_FQS ); + ret =regmap_set_bits (priv->regs,MX25_ADCQ_CR , +MX25_ADCQ_CR_FRST |MX25_ADCQ_CR_QRST ); +if (ret) +return dev_err_probe (dev, ret, +"Failed to reset the conversion queue\n" ); + + ret =regmap_write (priv->regs,MX25_ADCQ_CR , +MX25_ADCQ_CR_PDMSK |MX25_ADCQ_CR_QSM_FQS ); +if (ret) +return dev_err_probe (dev, ret, +"Failed to configure the conversion queue\n" );return 0 ; }
Quoted 5 lines · Arnav Kapoor
[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
The driver ignores the return value of all the regmap accesses. If one of them fails, e.g. because the memory mapped registers can't be accessed, the driver just continues with an undefined state.
MMIO never return failure. if write register fail at irq, it will cause irq storm.
Frank
Quoted 8 lines · Frank Li
Quoted 3 lines · Arnav Kapoor
The driver ignores the return value of all the regmap accesses. If one of them fails, e.g. because the memory mapped registers can't be accessed, the driver just continues with an undefined state.
MMIO never return failure. if write register fail at irq, it will cause irq storm.
Strictly speaking it may lead to some CPU exceptions in some cases, but that's different level of failures and not related to the error codes from regmap IO accessors.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 3 lines · Arnav Kapoor
The driver ignores the return value of all the regmap accesses. If one of them fails, e.g. because the memory mapped registers can't be accessed, the driver just continues with an undefined state.
Can MMIO actually fail? It can give invalid values (due to HW being off or wrong MMIO window). It can give a page fault, or some other error in HW, but the read itself most likely never fails.
Quoted 8 lines
The most severe case is the read of the FIFO in
mx25_gcq_get_raw_value(): the destination variable is not initialized, so a failed read leaks uninitialized stack memory to userspace.Check the return value of all the register accesses and propagate the errors to the caller. In the interrupt handler, where there is not much that can be done about a failed access, just don't use the content of the status register if it can't be read.
I consider this change an unneeded churn until you come up with real life example of how MMIO fails (at least on this platform, not exactly with this driver).
Yes, I aware that checking for error codes of regmap is a good manners and style in general, but would we do jsut that in a standalone change?
Signature · 2 lines
With Best Regards, Andy Shevchenko
(Resending as plain text, my earlier reply was rejected by one of the lists.)
Quoted 2 lines · Andy Shevchenko
I consider this change an unneeded churn until you come up with real life example of how MMIO fails
You and Frank are right. The regmap here is a plain devm_regmap_init_mmio() without a clock, so the accesses can't return an error, and the uninitialized-data case can't happen either. Please drop this patch.
Thanks to both of you for the review. And Andy, thank you for going through all of my patches today, the feedback has been very helpful.
Arnav