* [PATCH] zsmalloc: don't underflow size calculation in zs_obj_write()
@ 2025-05-04 11:00 Sergey Senozhatsky
2025-05-05 9:05 ` Igor Belousov
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2025-05-04 11:00 UTC (permalink / raw)
To: Andrew Morton
Cc: Minchan Kim, Yosry Ahmed, Vitaly Wool, linux-kernel, linux-mm,
Sergey Senozhatsky, Igor Belousov, stable
Do not mix class->size and object size during offsets/sizes
calculation in zs_obj_write(). Size classes can merge into
clusters, based on objects-per-zspage and pages-per-zspage
characteristics, so some size classes can store objects
smaller than class->size. This becomes problematic when
object size is much smaller than class->size - we can determine
that object spans two physical pages, because we use a larger
class->size for this, while the actual object is much smaller
and fits one physical page, so there is nothing to write to
the second page and memcpy() size calculation underflows.
We always know the exact size in bytes of the object
that we are about to write (store), so use it instead of
class->size.
Reported-by: Igor Belousov <igor.b@beldev.am>
Cc: <stable@vger.kernel.org>
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
mm/zsmalloc.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index 70406ac94bbd..999b513c7fdf 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -1233,19 +1233,19 @@ void zs_obj_write(struct zs_pool *pool, unsigned long handle,
class = zspage_class(pool, zspage);
off = offset_in_page(class->size * obj_idx);
- if (off + class->size <= PAGE_SIZE) {
+ if (!ZsHugePage(zspage))
+ off += ZS_HANDLE_SIZE;
+
+ if (off + mem_len <= PAGE_SIZE) {
/* this object is contained entirely within a page */
void *dst = kmap_local_zpdesc(zpdesc);
- if (!ZsHugePage(zspage))
- off += ZS_HANDLE_SIZE;
memcpy(dst + off, handle_mem, mem_len);
kunmap_local(dst);
} else {
/* this object spans two pages */
size_t sizes[2];
- off += ZS_HANDLE_SIZE;
sizes[0] = PAGE_SIZE - off;
sizes[1] = mem_len - sizes[0];
--
2.49.0.906.g1f30a19c02-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] zsmalloc: don't underflow size calculation in zs_obj_write()
2025-05-04 11:00 [PATCH] zsmalloc: don't underflow size calculation in zs_obj_write() Sergey Senozhatsky
@ 2025-05-05 9:05 ` Igor Belousov
2025-05-06 4:25 ` Sergey Senozhatsky
2025-05-06 13:56 ` Johannes Weiner
2 siblings, 0 replies; 5+ messages in thread
From: Igor Belousov @ 2025-05-05 9:05 UTC (permalink / raw)
To: Sergey Senozhatsky
Cc: Andrew Morton, Minchan Kim, Yosry Ahmed, Vitaly Wool,
linux-kernel, linux-mm, stable
On 2025-05-04 15:00, Sergey Senozhatsky wrote:
> Do not mix class->size and object size during offsets/sizes
> calculation in zs_obj_write(). Size classes can merge into
> clusters, based on objects-per-zspage and pages-per-zspage
> characteristics, so some size classes can store objects
> smaller than class->size. This becomes problematic when
> object size is much smaller than class->size - we can determine
> that object spans two physical pages, because we use a larger
> class->size for this, while the actual object is much smaller
> and fits one physical page, so there is nothing to write to
> the second page and memcpy() size calculation underflows.
>
> We always know the exact size in bytes of the object
> that we are about to write (store), so use it instead of
> class->size.
>
> Reported-by: Igor Belousov <igor.b@beldev.am>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Tested-by: Igor Belousov <igor.b@beldev.am>
> ---
> mm/zsmalloc.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
> index 70406ac94bbd..999b513c7fdf 100644
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c
> @@ -1233,19 +1233,19 @@ void zs_obj_write(struct zs_pool *pool,
> unsigned long handle,
> class = zspage_class(pool, zspage);
> off = offset_in_page(class->size * obj_idx);
>
> - if (off + class->size <= PAGE_SIZE) {
> + if (!ZsHugePage(zspage))
> + off += ZS_HANDLE_SIZE;
> +
> + if (off + mem_len <= PAGE_SIZE) {
> /* this object is contained entirely within a page */
> void *dst = kmap_local_zpdesc(zpdesc);
>
> - if (!ZsHugePage(zspage))
> - off += ZS_HANDLE_SIZE;
> memcpy(dst + off, handle_mem, mem_len);
> kunmap_local(dst);
> } else {
> /* this object spans two pages */
> size_t sizes[2];
>
> - off += ZS_HANDLE_SIZE;
> sizes[0] = PAGE_SIZE - off;
> sizes[1] = mem_len - sizes[0];
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] zsmalloc: don't underflow size calculation in zs_obj_write()
2025-05-04 11:00 [PATCH] zsmalloc: don't underflow size calculation in zs_obj_write() Sergey Senozhatsky
2025-05-05 9:05 ` Igor Belousov
@ 2025-05-06 4:25 ` Sergey Senozhatsky
2025-05-06 13:56 ` Johannes Weiner
2 siblings, 0 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2025-05-06 4:25 UTC (permalink / raw)
To: Andrew Morton
Cc: Minchan Kim, Yosry Ahmed, Vitaly Wool, linux-kernel, linux-mm,
Igor Belousov, stable, Sergey Senozhatsky
On (25/05/04 20:00), Sergey Senozhatsky wrote:
> Do not mix class->size and object size during offsets/sizes
> calculation in zs_obj_write(). Size classes can merge into
> clusters, based on objects-per-zspage and pages-per-zspage
> characteristics, so some size classes can store objects
> smaller than class->size. This becomes problematic when
> object size is much smaller than class->size - we can determine
> that object spans two physical pages, because we use a larger
> class->size for this, while the actual object is much smaller
> and fits one physical page, so there is nothing to write to
> the second page and memcpy() size calculation underflows.
>
> We always know the exact size in bytes of the object
> that we are about to write (store), so use it instead of
> class->size.
I think it's
Fixes: 44f76413496e ("zsmalloc: introduce new object mapping API")
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] zsmalloc: don't underflow size calculation in zs_obj_write()
2025-05-04 11:00 [PATCH] zsmalloc: don't underflow size calculation in zs_obj_write() Sergey Senozhatsky
2025-05-05 9:05 ` Igor Belousov
2025-05-06 4:25 ` Sergey Senozhatsky
@ 2025-05-06 13:56 ` Johannes Weiner
2025-05-07 5:40 ` Sergey Senozhatsky
2 siblings, 1 reply; 5+ messages in thread
From: Johannes Weiner @ 2025-05-06 13:56 UTC (permalink / raw)
To: Sergey Senozhatsky
Cc: Andrew Morton, Minchan Kim, Yosry Ahmed, Vitaly Wool,
linux-kernel, linux-mm, Igor Belousov, stable
On Sun, May 04, 2025 at 08:00:22PM +0900, Sergey Senozhatsky wrote:
> Do not mix class->size and object size during offsets/sizes
> calculation in zs_obj_write(). Size classes can merge into
> clusters, based on objects-per-zspage and pages-per-zspage
> characteristics, so some size classes can store objects
> smaller than class->size. This becomes problematic when
> object size is much smaller than class->size - we can determine
> that object spans two physical pages, because we use a larger
> class->size for this, while the actual object is much smaller
> and fits one physical page, so there is nothing to write to
> the second page and memcpy() size calculation underflows.
>
> We always know the exact size in bytes of the object
> that we are about to write (store), so use it instead of
> class->size.
>
> Reported-by: Igor Belousov <igor.b@beldev.am>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Could you please include user-visible effects and circumstances that
Igor reported? Crash, backtrace etc, 16k pages etc. in the changelog?
This type of information helps tremendously with backports, or finding
this patch when encountering the issue in the wild.
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] zsmalloc: don't underflow size calculation in zs_obj_write()
2025-05-06 13:56 ` Johannes Weiner
@ 2025-05-07 5:40 ` Sergey Senozhatsky
0 siblings, 0 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2025-05-07 5:40 UTC (permalink / raw)
To: Johannes Weiner
Cc: Sergey Senozhatsky, Andrew Morton, Minchan Kim, Yosry Ahmed,
Vitaly Wool, linux-kernel, linux-mm, Igor Belousov, stable
On (25/05/06 09:56), Johannes Weiner wrote:
> Could you please include user-visible effects and circumstances that
> Igor reported? Crash, backtrace etc, 16k pages etc. in the changelog?
>
> This type of information helps tremendously with backports, or finding
> this patch when encountering the issue in the wild.
Fair enough. I'll send a v2 with updated commit message.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-05-07 5:40 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-04 11:00 [PATCH] zsmalloc: don't underflow size calculation in zs_obj_write() Sergey Senozhatsky
2025-05-05 9:05 ` Igor Belousov
2025-05-06 4:25 ` Sergey Senozhatsky
2025-05-06 13:56 ` Johannes Weiner
2025-05-07 5:40 ` Sergey Senozhatsky
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox