[PATCH v2 2/3] tpm: Provide a function tpm_chip_free() to free tpm chips

From: Lino Sanfilippo
Date: Tue Feb 02 2021 - 17:12:23 EST


From: Lino Sanfilippo <l.sanfilippo@xxxxxxxxxx>

Provide a function tpm_chip_free() as a counterpart to tpm_chip_alloc().
The function hides the internals of freeing a struct tpm_chip instance
by putting the device references which are part of this structure.

Use the new function at the appropriate places.

Signed-off-by: Lino Sanfilippo <l.sanfilippo@xxxxxxxxxx>
---
drivers/char/tpm/tpm-chip.c | 16 ++++++++++++++++
drivers/char/tpm/tpm.h | 1 +
drivers/char/tpm/tpm_ftpm_tee.c | 6 ++----
drivers/char/tpm/tpm_vtpm_proxy.c | 3 +--
4 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index 3ace199..777baae 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -402,6 +402,22 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
EXPORT_SYMBOL_GPL(tpm_chip_alloc);

/**
+ * tpm_chip_free() - free an instance of struct tpm_chip that has been
+ * allocated with tpm_chip_alloc() before.
+ * @chip: chip to free
+ *
+ * Frees an instance of struct tpm_chip by releasing internal device references.
+ * This function is used to hide the internals needed to free a struct tpm_chip
+ * instance thas has been allocated with tpm_chip_alloc() before.
+ */
+void tpm_chip_free(struct tpm_chip *chip)
+{
+ put_device(&chip->devs);
+ put_device(&chip->dev);
+}
+EXPORT_SYMBOL_GPL(tpm_chip_free);
+
+/**
* tpmm_chip_alloc() - allocate a new struct tpm_chip instance
* @pdev: parent device to which the chip is associated
* @ops: struct tpm_class_ops instance
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index 947d1db..e6bb6ae 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -199,6 +199,7 @@ void tpm_put_ops(struct tpm_chip *chip);

struct tpm_chip *tpm_chip_alloc(struct device *dev,
const struct tpm_class_ops *ops);
+void tpm_chip_free(struct tpm_chip *chip);
struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
const struct tpm_class_ops *ops);
int tpm_chip_register(struct tpm_chip *chip);
diff --git a/drivers/char/tpm/tpm_ftpm_tee.c b/drivers/char/tpm/tpm_ftpm_tee.c
index 82858c2..47ffaae 100644
--- a/drivers/char/tpm/tpm_ftpm_tee.c
+++ b/drivers/char/tpm/tpm_ftpm_tee.c
@@ -285,8 +285,7 @@ static int ftpm_tee_probe(struct device *dev)
return 0;

out_chip:
- put_device(&pvt_data->chip->dev);
- put_device(&pvt_data->chip->devs);
+ tpm_chip_free(chip);
out_chip_alloc:
tee_shm_free(pvt_data->shm);
out_shm_alloc:
@@ -319,8 +318,7 @@ static int ftpm_tee_remove(struct device *dev)
tpm_chip_unregister(pvt_data->chip);

/* frees chip */
- put_device(&pvt_data->chip->devs);
- put_device(&pvt_data->chip->dev);
+ tpm_chip_free(pvt_data->chip);

/* Free the shared memory pool */
tee_shm_free(pvt_data->shm);
diff --git a/drivers/char/tpm/tpm_vtpm_proxy.c b/drivers/char/tpm/tpm_vtpm_proxy.c
index 97b60f8..f887bb3 100644
--- a/drivers/char/tpm/tpm_vtpm_proxy.c
+++ b/drivers/char/tpm/tpm_vtpm_proxy.c
@@ -520,8 +520,7 @@ static struct proxy_dev *vtpm_proxy_create_proxy_dev(void)
*/
static inline void vtpm_proxy_delete_proxy_dev(struct proxy_dev *proxy_dev)
{
- put_device(&proxy_dev->chip->devs);
- put_device(&proxy_dev->chip->dev); /* frees chip */
+ tpm_chip_free(proxy_dev->chip);
kfree(proxy_dev);
}

--
2.7.4