public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iommu/msm: add a check for the return of kzalloc()
@ 2022-03-25  2:08 xkernel.wang
  2022-04-28  8:03 ` Joerg Roedel
  2022-04-28 14:51 ` Jeffrey Hugo
  0 siblings, 2 replies; 3+ messages in thread
From: xkernel.wang @ 2022-03-25  2:08 UTC (permalink / raw)
  To: agross, bjorn.andersson, joro, will
  Cc: linux-arm-msm, iommu, linux-kernel, Xiaoke Wang

From: Xiaoke Wang <xkernel.wang@foxmail.com>

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 <xkernel.wang@foxmail.com>
---
 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);
-- 

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] iommu/msm: add a check for the return of kzalloc()
  2022-03-25  2:08 [PATCH] iommu/msm: add a check for the return of kzalloc() xkernel.wang
@ 2022-04-28  8:03 ` Joerg Roedel
  2022-04-28 14:51 ` Jeffrey Hugo
  1 sibling, 0 replies; 3+ messages in thread
From: Joerg Roedel @ 2022-04-28  8:03 UTC (permalink / raw)
  To: xkernel.wang
  Cc: agross, bjorn.andersson, will, linux-arm-msm, iommu, linux-kernel

On Fri, Mar 25, 2022 at 10:08:01AM +0800, xkernel.wang@foxmail.com wrote:
> From: Xiaoke Wang <xkernel.wang@foxmail.com>
> 
> 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 <xkernel.wang@foxmail.com>
> ---
>  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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] iommu/msm: add a check for the return of kzalloc()
  2022-03-25  2:08 [PATCH] iommu/msm: add a check for the return of kzalloc() xkernel.wang
  2022-04-28  8:03 ` Joerg Roedel
@ 2022-04-28 14:51 ` Jeffrey Hugo
  1 sibling, 0 replies; 3+ messages in thread
From: Jeffrey Hugo @ 2022-04-28 14:51 UTC (permalink / raw)
  To: xkernel.wang
  Cc: Andy Gross, Bjorn Andersson, Joerg Roedel, Will Deacon, MSM,
	iommu, lkml

On Fri, Mar 25, 2022 at 2:13 PM <xkernel.wang@foxmail.com> wrote:
>
> From: Xiaoke Wang <xkernel.wang@foxmail.com>
>
> 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 <xkernel.wang@foxmail.com>
> ---
>  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);
> --

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-04-28 14:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-25  2:08 [PATCH] iommu/msm: add a check for the return of kzalloc() xkernel.wang
2022-04-28  8:03 ` Joerg Roedel
2022-04-28 14:51 ` Jeffrey Hugo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox