2022-08-17 10:32:37

by Hyeonggon Yoo

[permalink] [raw]
Subject: [PATCH v4 17/17] mm/sl[au]b: check if large object is valid in __ksize()

If address of large object is not beginning of folio or size of
the folio is too small, it must be invalid. BUG() in such cases.

Cc: Marco Elver <[email protected]>
Suggested-by: Vlastimil Babka <[email protected]>
Signed-off-by: Hyeonggon Yoo <[email protected]>
Reviewed-by: Vlastimil Babka <[email protected]>
---
mm/slab_common.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/mm/slab_common.c b/mm/slab_common.c
index 9c273a5fb0d7..98d029212682 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -984,8 +984,11 @@ size_t __ksize(const void *object)

folio = virt_to_folio(object);

- if (unlikely(!folio_test_slab(folio)))
+ if (unlikely(!folio_test_slab(folio))) {
+ BUG_ON(folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE);
+ BUG_ON(object != folio_address(folio));
return folio_size(folio);
+ }

return slab_ksize(folio_slab(folio)->slab_cache);
}
--
2.32.0


2022-08-23 19:01:19

by Vlastimil Babka

[permalink] [raw]
Subject: Re: [PATCH v4 17/17] mm/sl[au]b: check if large object is valid in __ksize()

On 8/17/22 12:18, Hyeonggon Yoo wrote:
> If address of large object is not beginning of folio or size of
> the folio is too small, it must be invalid. BUG() in such cases.
>
> Cc: Marco Elver <[email protected]>
> Suggested-by: Vlastimil Babka <[email protected]>
> Signed-off-by: Hyeonggon Yoo <[email protected]>
> Reviewed-by: Vlastimil Babka <[email protected]>
> ---
> mm/slab_common.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/mm/slab_common.c b/mm/slab_common.c
> index 9c273a5fb0d7..98d029212682 100644
> --- a/mm/slab_common.c
> +++ b/mm/slab_common.c
> @@ -984,8 +984,11 @@ size_t __ksize(const void *object)
>
> folio = virt_to_folio(object);
>
> - if (unlikely(!folio_test_slab(folio)))
> + if (unlikely(!folio_test_slab(folio))) {
> + BUG_ON(folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE);
> + BUG_ON(object != folio_address(folio));
> return folio_size(folio);
> + }
>
> return slab_ksize(folio_slab(folio)->slab_cache);
> }

In light of latest Linus' rant on BUG_ON() [1] I'm changing it to WARN_ON
and return 0, as it was in v3.

[1] https://lore.kernel.org/all/CAHk-=wiEAH+ojSpAgx_Ep=NKPWHU8AdO3V56BXcCsU97oYJ1EA@mail.gmail.com/


diff --git a/mm/slab_common.c b/mm/slab_common.c
index 98d029212682..a80c3a5e194d 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -985,8 +985,10 @@ size_t __ksize(const void *object)
folio = virt_to_folio(object);

if (unlikely(!folio_test_slab(folio))) {
- BUG_ON(folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE);
- BUG_ON(object != folio_address(folio));
+ if (WARN_ON(folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE))
+ return 0;
+ if (WARN_ON(object != folio_address(folio)))
+ return 0;
return folio_size(folio);
}


2022-08-24 04:32:51

by Hyeonggon Yoo

[permalink] [raw]
Subject: Re: [PATCH v4 17/17] mm/sl[au]b: check if large object is valid in __ksize()

On Tue, Aug 23, 2022 at 05:12:01PM +0200, Vlastimil Babka wrote:
> On 8/17/22 12:18, Hyeonggon Yoo wrote:
> > If address of large object is not beginning of folio or size of
> > the folio is too small, it must be invalid. BUG() in such cases.
> >
> > Cc: Marco Elver <[email protected]>
> > Suggested-by: Vlastimil Babka <[email protected]>
> > Signed-off-by: Hyeonggon Yoo <[email protected]>
> > Reviewed-by: Vlastimil Babka <[email protected]>
> > ---
> > mm/slab_common.c | 5 ++++-
> > 1 file changed, 4 insertions(+), 1 deletion(-)
> >
> > diff --git a/mm/slab_common.c b/mm/slab_common.c
> > index 9c273a5fb0d7..98d029212682 100644
> > --- a/mm/slab_common.c
> > +++ b/mm/slab_common.c
> > @@ -984,8 +984,11 @@ size_t __ksize(const void *object)
> >
> > folio = virt_to_folio(object);
> >
> > - if (unlikely(!folio_test_slab(folio)))
> > + if (unlikely(!folio_test_slab(folio))) {
> > + BUG_ON(folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE);
> > + BUG_ON(object != folio_address(folio));
> > return folio_size(folio);
> > + }
> >
> > return slab_ksize(folio_slab(folio)->slab_cache);
> > }
>
> In light of latest Linus' rant on BUG_ON() [1] I'm changing it to WARN_ON
> and return 0, as it was in v3.
>
> [1] https://lore.kernel.org/all/CAHk-=wiEAH+ojSpAgx_Ep=NKPWHU8AdO3V56BXcCsU97oYJ1EA@mail.gmail.com/

Okay. I'm fine with that.

>
>
> diff --git a/mm/slab_common.c b/mm/slab_common.c
> index 98d029212682..a80c3a5e194d 100644
> --- a/mm/slab_common.c
> +++ b/mm/slab_common.c
> @@ -985,8 +985,10 @@ size_t __ksize(const void *object)
> folio = virt_to_folio(object);
>
> if (unlikely(!folio_test_slab(folio))) {
> - BUG_ON(folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE);
> - BUG_ON(object != folio_address(folio));
> + if (WARN_ON(folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE))
> + return 0;
> + if (WARN_ON(object != folio_address(folio)))
> + return 0;
> return folio_size(folio);
> }
>
>

--
Thanks,
Hyeonggon