Skip to content

Commit d10eba8

Browse files
committed
drivers: spi: ambiq_spid: fix runtime PM leaks and return status
In spi_ambiq_transceive(), three issues affected the runtime PM path and error reporting: 1. When spi_config() failed, the function returned directly without releasing the runtime PM reference acquired by pm_device_runtime_get(), causing the usage count to permanently leak. 2. The return status of spi_ambiq_xfer() was overwritten by the return value of pm_device_runtime_put_async(), masking transfer errors from callers. 3. When pm_device_runtime_get() failed, execution continued into the hardware routine instead of exiting early. Restructure spi_ambiq_transceive() to return immediately on get failures, route spi_config() errors through a shared cleanup label, and use a separate variable for the async put return value. Fixes #116324 Signed-off-by: harshit kudhial <harshitkudhial@gmail.com>
1 parent e64f6be commit d10eba8

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

drivers/spi/spi_ambiq_spid.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ static int spi_ambiq_transceive(const struct device *dev, const struct spi_confi
276276
const struct spi_buf_set *rx_bufs)
277277
{
278278
struct spi_ambiq_data *data = dev->data;
279+
int pm_ret;
279280
int ret;
280281

281282
if (!tx_bufs && !rx_bufs) {
@@ -285,29 +286,30 @@ static int spi_ambiq_transceive(const struct device *dev, const struct spi_confi
285286
ret = pm_device_runtime_get(dev);
286287
if (ret < 0) {
287288
LOG_ERR_PM_DEVICE_RUNTIME_GET(dev, ret);
289+
return ret;
288290
}
289291

290292
/* context setup */
291293
spi_context_lock(&data->ctx, false, NULL, NULL, config);
292294

293295
ret = spi_config(dev, config);
294296
if (ret) {
295-
spi_context_release(&data->ctx, ret);
296-
return ret;
297+
goto end;
297298
}
298299

299300
spi_context_buffers_setup(&data->ctx, tx_bufs, rx_bufs, 1);
300301

301302
ret = spi_ambiq_xfer(dev, config);
302303

304+
end:
303305
spi_context_release(&data->ctx, ret);
304306

305307
/* Use async put to avoid useless device suspension/resumption
306308
* when doing consecutive transmission.
307309
*/
308-
ret = pm_device_runtime_put_async(dev, K_MSEC(2));
309-
if (ret < 0) {
310-
LOG_ERR_PM_DEVICE_RUNTIME_PUT(dev, ret);
310+
pm_ret = pm_device_runtime_put_async(dev, K_MSEC(2));
311+
if (pm_ret < 0) {
312+
LOG_ERR_PM_DEVICE_RUNTIME_PUT(dev, pm_ret);
311313
}
312314

313315
return ret;

0 commit comments

Comments
 (0)