[PATCH v3] mm/vmalloc: replace BUG_ON to a simple if statement

Hyunmin Lee posted 1 patch 2 years, 7 months ago
There is a newer version of this series
mm/vmalloc.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
[PATCH v3] mm/vmalloc: replace BUG_ON to a simple if statement
Posted by Hyunmin Lee 2 years, 7 months ago
As per the coding standards, in the event of an abnormal condition that
should not occur under normal circumstances, the kernel should attempt
recovery and proceed with execution, rather than halting the machine.

Specifically, in the alloc_vmap_area() function, use a simple if()
instead of using BUG_ON() halting the machine.

Co-Developed-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
Co-Developed-by: Jeungwoo Yoo <casionwoo@gmail.com>
Co-Developed-by: Sangyun Kim <sangyun.kim@snu.ac.kr>
Signed-off-by: Hyunmin Lee <hn.min.lee@gmail.com>
Signed-off-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
Signed-off-by: Jeungwoo Yoo <casionwoo@gmail.com>
Signed-off-by: Sangyun Kim <sangyun.kim@snu.ac.kr>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
---
v1->v2 : Add commit description
v2->v3 : Change WARN_ON() to if()
---
 mm/vmalloc.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 74afa2208558..52a346bc02a1 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -1587,9 +1587,14 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
 	int purged = 0;
 	int ret;
 
-	BUG_ON(!size);
-	BUG_ON(offset_in_page(size));
-	BUG_ON(!is_power_of_2(align));
+	if (unlikely(!size))
+		return ERR_PTR(-EINVAL);
+
+	if (unlikely(offset_in_page(size)))
+		return ERR_PTR(-EINVAL);
+
+	if (unlikely(!is_power_of_2(align)))
+		return ERR_PTR(-EINVAL);
 
 	if (unlikely(!vmap_initialized))
 		return ERR_PTR(-EBUSY);
-- 
2.25.1
Re: [PATCH v3] mm/vmalloc: replace BUG_ON to a simple if statement
Posted by Christophe Leroy 2 years, 7 months ago

Le 01/02/2023 à 11:11, Hyunmin Lee a écrit :
> [Vous ne recevez pas souvent de courriers de hn.min.lee@gmail.com. D?couvrez pourquoi ceci est important ? https://aka.ms/LearnAboutSenderIdentification ]
> 
> As per the coding standards, in the event of an abnormal condition that
> should not occur under normal circumstances, the kernel should attempt
> recovery and proceed with execution, rather than halting the machine.
> 
> Specifically, in the alloc_vmap_area() function, use a simple if()
> instead of using BUG_ON() halting the machine.
> 
> Co-Developed-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
> Co-Developed-by: Jeungwoo Yoo <casionwoo@gmail.com>
> Co-Developed-by: Sangyun Kim <sangyun.kim@snu.ac.kr>
> Signed-off-by: Hyunmin Lee <hn.min.lee@gmail.com>
> Signed-off-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
> Signed-off-by: Jeungwoo Yoo <casionwoo@gmail.com>
> Signed-off-by: Sangyun Kim <sangyun.kim@snu.ac.kr>
> Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
> ---
> v1->v2 : Add commit description
> v2->v3 : Change WARN_ON() to if()
> ---
>   mm/vmalloc.c | 11 ++++++++---
>   1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index 74afa2208558..52a346bc02a1 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -1587,9 +1587,14 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
>          int purged = 0;
>          int ret;
> 
> -       BUG_ON(!size);
> -       BUG_ON(offset_in_page(size));
> -       BUG_ON(!is_power_of_2(align));
> +       if (unlikely(!size))
> +               return ERR_PTR(-EINVAL);
> +
> +       if (unlikely(offset_in_page(size)))
> +               return ERR_PTR(-EINVAL);
> +
> +       if (unlikely(!is_power_of_2(align)))
> +               return ERR_PTR(-EINVAL);

I would have written it more compact. When using BUG_ON or WARN_ON it is 
interesting to have three separate lines because you get the line number 
in the Oops message, but here you are just returning the exact same 
error code, so it could be:

	if (unlikely(!size || offset_in_page(size) || !is_power_of_2(align)))
		return ERR_PTR(-EINVAL);

> 
>          if (unlikely(!vmap_initialized))
>                  return ERR_PTR(-EBUSY);
> --
> 2.25.1
> 
> 
Re: [PATCH v3] mm/vmalloc: replace BUG_ON to a simple if statement
Posted by Hyunmin Lee 2 years, 7 months ago
On Wed, Feb 01, 2023 at 10:37:19AM +0000, Christophe Leroy wrote:
> 
> 
> Le 01/02/2023 à 11:11, Hyunmin Lee a écrit :
> > [Vous ne recevez pas souvent de courriers de hn.min.lee@gmail.com. D?couvrez pourquoi ceci est important ? https://aka.ms/LearnAboutSenderIdentification ]
> > 
> > As per the coding standards, in the event of an abnormal condition that
> > should not occur under normal circumstances, the kernel should attempt
> > recovery and proceed with execution, rather than halting the machine.
> > 
> > Specifically, in the alloc_vmap_area() function, use a simple if()
> > instead of using BUG_ON() halting the machine.
> > 
> > Co-Developed-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
> > Co-Developed-by: Jeungwoo Yoo <casionwoo@gmail.com>
> > Co-Developed-by: Sangyun Kim <sangyun.kim@snu.ac.kr>
> > Signed-off-by: Hyunmin Lee <hn.min.lee@gmail.com>
> > Signed-off-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
> > Signed-off-by: Jeungwoo Yoo <casionwoo@gmail.com>
> > Signed-off-by: Sangyun Kim <sangyun.kim@snu.ac.kr>
> > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
> > ---
> > v1->v2 : Add commit description
> > v2->v3 : Change WARN_ON() to if()
> > ---
> >   mm/vmalloc.c | 11 ++++++++---
> >   1 file changed, 8 insertions(+), 3 deletions(-)
> > 
> > diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> > index 74afa2208558..52a346bc02a1 100644
> > --- a/mm/vmalloc.c
> > +++ b/mm/vmalloc.c
> > @@ -1587,9 +1587,14 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
> >          int purged = 0;
> >          int ret;
> > 
> > -       BUG_ON(!size);
> > -       BUG_ON(offset_in_page(size));
> > -       BUG_ON(!is_power_of_2(align));
> > +       if (unlikely(!size))
> > +               return ERR_PTR(-EINVAL);
> > +
> > +       if (unlikely(offset_in_page(size)))
> > +               return ERR_PTR(-EINVAL);
> > +
> > +       if (unlikely(!is_power_of_2(align)))
> > +               return ERR_PTR(-EINVAL);
> 
> I would have written it more compact. When using BUG_ON or WARN_ON it is 
> interesting to have three separate lines because you get the line number 
> in the Oops message, but here you are just returning the exact same 
> error code, so it could be:
> 
> 	if (unlikely(!size || offset_in_page(size) || !is_power_of_2(align)))
> 		return ERR_PTR(-EINVAL);
> 
> > 
> >          if (unlikely(!vmap_initialized))
> >                  return ERR_PTR(-EBUSY);
> > --
> > 2.25.1
> > 
> > 
Hi Christophe,

Thanks for your comment.
I will send new patch.

Best,
Min