2022-03-25 17:36:27

by Xiaoke Wang

[permalink] [raw]
Subject: [PATCH] iommu/msm: add a check for the return of kzalloc()

From: Xiaoke Wang <[email protected]>

kzalloc() is a memory allocation function which can return NULL when
some internal memory errors happen. So it is better to check it to
prevent potential wrong memory access.

Signed-off-by: Xiaoke Wang <[email protected]>
---
drivers/iommu/msm_iommu.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/drivers/iommu/msm_iommu.c b/drivers/iommu/msm_iommu.c
index 3a38352..697ad63 100644
--- a/drivers/iommu/msm_iommu.c
+++ b/drivers/iommu/msm_iommu.c
@@ -597,6 +597,10 @@ static void insert_iommu_master(struct device *dev,

if (list_empty(&(*iommu)->ctx_list)) {
master = kzalloc(sizeof(*master), GFP_ATOMIC);
+ if (!master) {
+ dev_err(dev, "Failed to allocate iommu_master\n");
+ return;
+ }
master->of_node = dev->of_node;
list_add(&master->list, &(*iommu)->ctx_list);
dev_iommu_priv_set(dev, master);
--


2022-04-28 13:36:37

by Joerg Roedel

[permalink] [raw]
Subject: Re: [PATCH] iommu/msm: add a check for the return of kzalloc()

On Fri, Mar 25, 2022 at 10:08:01AM +0800, [email protected] wrote:
> From: Xiaoke Wang <[email protected]>
>
> kzalloc() is a memory allocation function which can return NULL when
> some internal memory errors happen. So it is better to check it to
> prevent potential wrong memory access.
>
> Signed-off-by: Xiaoke Wang <[email protected]>
> ---
> drivers/iommu/msm_iommu.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/iommu/msm_iommu.c b/drivers/iommu/msm_iommu.c
> index 3a38352..697ad63 100644
> --- a/drivers/iommu/msm_iommu.c
> +++ b/drivers/iommu/msm_iommu.c
> @@ -597,6 +597,10 @@ static void insert_iommu_master(struct device *dev,
>
> if (list_empty(&(*iommu)->ctx_list)) {
> master = kzalloc(sizeof(*master), GFP_ATOMIC);
> + if (!master) {
> + dev_err(dev, "Failed to allocate iommu_master\n");
> + return;
> + }

This is not enough, if the error happens it also need to be propagated
to the caller.

Regards,

Joerg

2022-04-29 18:48:34

by Jeffrey Hugo

[permalink] [raw]
Subject: Re: [PATCH] iommu/msm: add a check for the return of kzalloc()

On Fri, Mar 25, 2022 at 2:13 PM <[email protected]> wrote:
>
> From: Xiaoke Wang <[email protected]>
>
> kzalloc() is a memory allocation function which can return NULL when
> some internal memory errors happen. So it is better to check it to
> prevent potential wrong memory access.
>
> Signed-off-by: Xiaoke Wang <[email protected]>
> ---
> drivers/iommu/msm_iommu.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/iommu/msm_iommu.c b/drivers/iommu/msm_iommu.c
> index 3a38352..697ad63 100644
> --- a/drivers/iommu/msm_iommu.c
> +++ b/drivers/iommu/msm_iommu.c
> @@ -597,6 +597,10 @@ static void insert_iommu_master(struct device *dev,
>
> if (list_empty(&(*iommu)->ctx_list)) {
> master = kzalloc(sizeof(*master), GFP_ATOMIC);
> + if (!master) {
> + dev_err(dev, "Failed to allocate iommu_master\n");

How do you reconcile this with chapter 14 of the coding style document?

"These generic allocation functions all emit a stack dump on failure when used
without __GFP_NOWARN so there is no use in emitting an additional failure
message when NULL is returned."

> + return;
> + }
> master->of_node = dev->of_node;
> list_add(&master->list, &(*iommu)->ctx_list);
> dev_iommu_priv_set(dev, master);
> --