Re: [PATCH] NFC: nxp-nci: fix deadlock during firmware update

From: Krzysztof Kozlowski
Date: Wed Jul 13 2022 - 02:57:58 EST


On 12/07/2022 17:25, Michael Walle wrote:
> During firmware update, both nxp_nci_i2c_irq_thread_fn() and
> nxp_nci_fw_work() will hold the info_lock mutex and one will wait
> for the other via a completion:
>
> nxp_nci_fw_work()
> mutex_lock(info_lock)
> nxp_nci_fw_send()
> wait_for_completion(cmd_completion)
> mutex_unlock(info_lock)
>
> nxp_nci_i2c_irq_thread_fn()
> mutex_lock(info_lock)
> nxp_nci_fw_recv_frame()
> complete(cmd_completion)
> mutex_unlock(info_lock)
>
> This will result in a -ETIMEDOUT error during firmware update (note
> that the wait_for_completion() above is a variant with a timeout).
>
> Drop the lock in nxp_nci_fw_work() and instead take it after the
> work is done in nxp_nci_fw_work_complete() when the NFC controller mode
> is switched and the info structure is updated.
>
> Fixes: dece45855a8b ("NFC: nxp-nci: Add support for NXP NCI chips")
> Signed-off-by: Michael Walle <michael@xxxxxxxx>
> ---
> drivers/nfc/nxp-nci/firmware.c | 5 ++---
> 1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/nfc/nxp-nci/firmware.c b/drivers/nfc/nxp-nci/firmware.c
> index 119bf305c642..6a4d4aa7239f 100644
> --- a/drivers/nfc/nxp-nci/firmware.c
> +++ b/drivers/nfc/nxp-nci/firmware.c
> @@ -54,6 +54,7 @@ void nxp_nci_fw_work_complete(struct nxp_nci_info *info, int result)
> struct nxp_nci_fw_info *fw_info = &info->fw_info;
> int r;
>
> + mutex_lock(&info->info_lock);
> if (info->phy_ops->set_mode) {
> r = info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_COLD);
> if (r < 0 && result == 0)
> @@ -66,6 +67,7 @@ void nxp_nci_fw_work_complete(struct nxp_nci_info *info, int result)
> release_firmware(fw_info->fw);
> fw_info->fw = NULL;
> }
> + mutex_unlock(&info->info_lock);
>
> nfc_fw_download_done(info->ndev->nfc_dev, fw_info->name, (u32) -result);
> }
> @@ -172,8 +174,6 @@ void nxp_nci_fw_work(struct work_struct *work)
> fw_info = container_of(work, struct nxp_nci_fw_info, work);
> info = container_of(fw_info, struct nxp_nci_info, fw_info);
>
> - mutex_lock(&info->info_lock);
> -

I am not sure this is correct. info_lock should protect members of info
thus also info->fw_info. By removing the mutex the protection is gone.

Unless you are sure that fw_info cannot be modified concurrently?

Best regards,
Krzysztof