Skip to content

Commit

Permalink
NFC: nfcmrvl: do not use device-managed resources
Browse files Browse the repository at this point in the history
This specifically fixes resource leaks in the registration error paths.

Device-managed resources is a bad fit for this driver as devices can be
registered from the n_nci line discipline. Firstly, a tty may not even
have a corresponding device (should it be part of a Unix98 pty)
something which would lead to a NULL-pointer dereference when
registering resources.

Secondly, if the tty has a class device, its lifetime exceeds that of
the line discipline, which means that resources would leak every time
the line discipline is closed (or if registration fails).

Currently, the devres interface was only being used to request a reset
gpio despite the fact that it was already explicitly freed in
nfcmrvl_nci_unregister_dev() (along with the private data), something
which also prevented the resource leak at close.

Note that the driver treats gpio number 0 as invalid despite it being
perfectly valid. This will be addressed in a follow-up patch.

Fixes: b2fe288 ("NFC: nfcmrvl: free reset gpio")
Fixes: 4a2b947 ("NFC: nfcmrvl: add chip reset management")
Cc: stable <stable@vger.kernel.org>     # 4.2: b2fe288
Cc: Vincent Cuissard <cuissard@marvell.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
  • Loading branch information
Johan Hovold authored and Samuel Ortiz committed Jun 18, 2017
1 parent 15e0c59 commit 0cbe401
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions drivers/nfc/nfcmrvl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ struct nfcmrvl_private *nfcmrvl_nci_register_dev(enum nfcmrvl_phy phy,
memcpy(&priv->config, pdata, sizeof(*pdata));

if (priv->config.reset_n_io) {
rc = devm_gpio_request_one(dev,
priv->config.reset_n_io,
GPIOF_OUT_INIT_LOW,
"nfcmrvl_reset_n");
if (rc < 0)
rc = gpio_request_one(priv->config.reset_n_io,
GPIOF_OUT_INIT_LOW,
"nfcmrvl_reset_n");
if (rc < 0) {
priv->config.reset_n_io = 0;
nfc_err(dev, "failed to request reset_n io\n");
}
}

if (phy == NFCMRVL_PHY_SPI) {
Expand All @@ -154,7 +155,7 @@ struct nfcmrvl_private *nfcmrvl_nci_register_dev(enum nfcmrvl_phy phy,
if (!priv->ndev) {
nfc_err(dev, "nci_allocate_device failed\n");
rc = -ENOMEM;
goto error;
goto error_free_gpio;
}

nci_set_drvdata(priv->ndev, priv);
Expand All @@ -179,7 +180,9 @@ struct nfcmrvl_private *nfcmrvl_nci_register_dev(enum nfcmrvl_phy phy,

error_free_dev:
nci_free_device(priv->ndev);
error:
error_free_gpio:
if (priv->config.reset_n_io)
gpio_free(priv->config.reset_n_io);
kfree(priv);
return ERR_PTR(rc);
}
Expand All @@ -195,7 +198,7 @@ void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
nfcmrvl_fw_dnld_deinit(priv);

if (priv->config.reset_n_io)
devm_gpio_free(priv->dev, priv->config.reset_n_io);
gpio_free(priv->config.reset_n_io);

nci_unregister_device(ndev);
nci_free_device(ndev);
Expand Down

0 comments on commit 0cbe401

Please sign in to comment.