[PATCH] zram: don't free statically defined names

Andrey Skvortsov posted 1 patch 2 months ago
There is a newer version of this series
drivers/block/zram/zram_drv.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] zram: don't free statically defined names
Posted by Andrey Skvortsov 2 months ago
The change is similar to that is used in comp_algorithm_set.
This is detected by KASAN.

==================================================================
 Unable to handle kernel paging request at virtual address ffffffffc1edc3c8
 KASAN: maybe wild-memory-access in range
 [0x0003fffe0f6e1e40-0x0003fffe0f6e1e47]
 Mem abort info:
   ESR = 0x0000000096000006
   EC = 0x25: DABT (current EL), IL = 32 bits
   SET = 0, FnV = 0
   EA = 0, S1PTW = 0
   FSC = 0x06: level 2 translation fault
 Data abort info:
   ISV = 0, ISS = 0x00000006, ISS2 = 0x00000000
   CM = 0, WnR = 0, TnD = 0, TagAccess = 0
   GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
 swapper pgtable: 4k pages, 48-bit VAs, pgdp=00000000427dc000
 [ffffffffc1edc3c8] pgd=00000000430e7003, p4d=00000000430e7003,
 pud=00000000430e8003, pmd=0000000000000000
 Internal error: Oops: 0000000096000006 [#1] SMP

 Tainted: [W]=WARN, [C]=CRAP, [N]=TEST
 Hardware name: Pine64 PinePhone (1.2) (DT)
 pstate: a0000005 (NzCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
 pc : kfree+0x60/0x3a0
 lr : zram_destroy_comps+0x98/0x198 [zram]
 sp : ffff800089b57450
 x29: ffff800089b57460 x28: 0000000000000004 x27: ffff800082833010
 x26: 1fffe00000c8039c x25: 1fffe00000ba5004 x24: ffff000005d28000
 x23: ffff800082533178 x22: ffff80007b71eaa8 x21: ffff000006401ce8
 x20: ffff80007b70f7a0 x19: ffffffffc1edc3c0 x18: 1ffff00010506d6b
 x17: 0000000000000000 x16: 0000000000000000 x15: ffff8000808e85e4
 x14: ffff8000808e8478 x13: ffff80008003fa50 x12: ffff80008003f87c
 x11: ffff800080011550 x10: ffff800081ee63f0 x9 : ffff80007b71eaa8
 x8 : ffff80008003fa50 x7 : ffff80008003f87c x6 : 00000018a10e2f30
 x5 : 00ffffffffffffff x4 : ffff00000ec93200 x3 : ffff00000bbee6e0
 x2 : 0000000000000000 x1 : 0000000000000000 x0 : fffffdffc0000000

 Call trace:
  kfree+0x60/0x3a0
  zram_destroy_comps+0x98/0x198 [zram]
  zram_reset_device+0x22c/0x4a8 [zram]
  reset_store+0x1bc/0x2d8 [zram]
  dev_attr_store+0x44/0x80
  sysfs_kf_write+0xfc/0x188
  kernfs_fop_write_iter+0x28c/0x428
  vfs_write+0x4dc/0x9b8
  ksys_write+0x100/0x1f8
  __arm64_sys_write+0x74/0xb8
  invoke_syscall+0xd8/0x260
  el0_svc_common.constprop.0+0xb4/0x240
  do_el0_svc+0x48/0x68
  el0_svc+0x40/0xc8
  el0t_64_sync_handler+0x120/0x130
  el0t_64_sync+0x190/0x198
 Code: b26287e0 d34cfe73 f2dfbfe0 8b131813 (f9400660)
==================================================================

Signed-off-by: Andrey Skvortsov <andrej.skvortzov@gmail.com>
Fixes: 684826f8271a ("zram: free secondary algorithms names")
Cc: <stable@vger.kernel.org>
---
 drivers/block/zram/zram_drv.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index c3d245617083d..d9d2c36658f59 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -2116,7 +2116,9 @@ static void zram_destroy_comps(struct zram *zram)
 	}
 
 	for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
-		kfree(zram->comp_algs[prio]);
+		/* Do not free statically defined compression algorithms */
+		if (zram->comp_algs[prio] != default_compressor)
+			kfree(zram->comp_algs[prio]);
 		zram->comp_algs[prio] = NULL;
 	}
 
-- 
2.45.2
Re: [PATCH] zram: don't free statically defined names
Posted by Sergey Senozhatsky 2 months ago
On (24/09/23 11:02), Andrey Skvortsov wrote:
> The change is similar to that is used in comp_algorithm_set.
> This is detected by KASAN.
> 
> ==================================================================

---8<---

>  Unable to handle kernel paging request at virtual address ffffffffc1edc3c8
>  KASAN: maybe wild-memory-access in range
>  [0x0003fffe0f6e1e40-0x0003fffe0f6e1e47]
>  Mem abort info:
>    ESR = 0x0000000096000006
>    EC = 0x25: DABT (current EL), IL = 32 bits
>    SET = 0, FnV = 0
>    EA = 0, S1PTW = 0
>    FSC = 0x06: level 2 translation fault
>  Data abort info:
>    ISV = 0, ISS = 0x00000006, ISS2 = 0x00000000
>    CM = 0, WnR = 0, TnD = 0, TagAccess = 0
>    GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
>  swapper pgtable: 4k pages, 48-bit VAs, pgdp=00000000427dc000
>  [ffffffffc1edc3c8] pgd=00000000430e7003, p4d=00000000430e7003,
>  pud=00000000430e8003, pmd=0000000000000000
>  Internal error: Oops: 0000000096000006 [#1] SMP
> 
>  Tainted: [W]=WARN, [C]=CRAP, [N]=TEST
>  Hardware name: Pine64 PinePhone (1.2) (DT)
>  pstate: a0000005 (NzCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
>  pc : kfree+0x60/0x3a0
>  lr : zram_destroy_comps+0x98/0x198 [zram]
>  sp : ffff800089b57450
>  x29: ffff800089b57460 x28: 0000000000000004 x27: ffff800082833010
>  x26: 1fffe00000c8039c x25: 1fffe00000ba5004 x24: ffff000005d28000
>  x23: ffff800082533178 x22: ffff80007b71eaa8 x21: ffff000006401ce8
>  x20: ffff80007b70f7a0 x19: ffffffffc1edc3c0 x18: 1ffff00010506d6b
>  x17: 0000000000000000 x16: 0000000000000000 x15: ffff8000808e85e4
>  x14: ffff8000808e8478 x13: ffff80008003fa50 x12: ffff80008003f87c
>  x11: ffff800080011550 x10: ffff800081ee63f0 x9 : ffff80007b71eaa8
>  x8 : ffff80008003fa50 x7 : ffff80008003f87c x6 : 00000018a10e2f30
>  x5 : 00ffffffffffffff x4 : ffff00000ec93200 x3 : ffff00000bbee6e0
>  x2 : 0000000000000000 x1 : 0000000000000000 x0 : fffffdffc0000000

---8<---

The above is not needed in the commit message (not even sure if the
backtrace below is relevant).

>  Call trace:
>   kfree+0x60/0x3a0
>   zram_destroy_comps+0x98/0x198 [zram]
>   zram_reset_device+0x22c/0x4a8 [zram]
>   reset_store+0x1bc/0x2d8 [zram]
>   dev_attr_store+0x44/0x80
>   sysfs_kf_write+0xfc/0x188
>   kernfs_fop_write_iter+0x28c/0x428
>   vfs_write+0x4dc/0x9b8
>   ksys_write+0x100/0x1f8
>   __arm64_sys_write+0x74/0xb8
>   invoke_syscall+0xd8/0x260
>   el0_svc_common.constprop.0+0xb4/0x240
>   do_el0_svc+0x48/0x68
>   el0_svc+0x40/0xc8
>   el0t_64_sync_handler+0x120/0x130
>   el0t_64_sync+0x190/0x198

[..]

> diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> index c3d245617083d..d9d2c36658f59 100644
> --- a/drivers/block/zram/zram_drv.c
> +++ b/drivers/block/zram/zram_drv.c
> @@ -2116,7 +2116,9 @@ static void zram_destroy_comps(struct zram *zram)
>  	}
>  
>  	for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
> -		kfree(zram->comp_algs[prio]);
> +		/* Do not free statically defined compression algorithms */

We probably don't really need this comment.

> +		if (zram->comp_algs[prio] != default_compressor)
> +			kfree(zram->comp_algs[prio]);
>  		zram->comp_algs[prio] = NULL;
>  	}

OK, so... I wonder how do you get a `default_compressor` on a
non-ZRAM_PRIMARY_COMP prio.  May I ask what's your reproducer?

I didn't expect `default_compressor` on ZRAM_SECONDARY_COMP
and below.  As far as I can tell, we only do this:

	comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);

in zram_reset_device() and zram_add().  So, how does it end up in
ZRAM_SECONDARY_COMP...
Re: [PATCH] zram: don't free statically defined names
Posted by Sergey Senozhatsky 2 months ago
** Re-sending properly, somehow I managed to badly mess up
   the headers the first time, sorry ***


On (24/09/24 00:34), Sergey Senozhatsky wrote:
> On (24/09/23 11:02), Andrey Skvortsov wrote:
> >     for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
> > -           kfree(zram->comp_algs[prio]);
> > +           /* Do not free statically defined compression algorithms */
>
> We probably don't really need this comment.
>
> > +           if (zram->comp_algs[prio] != default_compressor)
> > +                   kfree(zram->comp_algs[prio]);
> >             zram->comp_algs[prio] = NULL;
> >     }
>
> OK, so... I wonder how do you get a `default_compressor` on a
> non-ZRAM_PRIMARY_COMP prio.  May I ask what's your reproducer?
>
> I didn't expect `default_compressor` on ZRAM_SECONDARY_COMP
> and below.  As far as I can tell, we only do this:
>
>       comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
>
> in zram_reset_device() and zram_add().  So, how does it end up in
> ZRAM_SECONDARY_COMP...

Ugh, I know what's happening.  You don't have CONFIG_ZRAM_MULTI_COMP
so that ZRAM_PRIMARY_COMP and ZRAM_SECONDARY_COMP are the same thing.
Yeah, that all makes sense now, I haven't thought about it.

Can you please send v2 (with the feedback resolved).
Re: [PATCH] zram: don't free statically defined names
Posted by Andrey Skvortsov 2 months ago
Hi Sergey,

On 24-09-24 00:47, Sergey Senozhatsky wrote:
> ** Re-sending properly, somehow I managed to badly mess up
>    the headers the first time, sorry ***
> 
> 
> On (24/09/24 00:34), Sergey Senozhatsky wrote:
> > On (24/09/23 11:02), Andrey Skvortsov wrote:
> > >     for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
> > > -           kfree(zram->comp_algs[prio]);
> > > +           /* Do not free statically defined compression algorithms */
> >
> > We probably don't really need this comment.
> >
> > > +           if (zram->comp_algs[prio] != default_compressor)
> > > +                   kfree(zram->comp_algs[prio]);
> > >             zram->comp_algs[prio] = NULL;
> > >     }
> >
> > OK, so... I wonder how do you get a `default_compressor` on a
> > non-ZRAM_PRIMARY_COMP prio.  May I ask what's your reproducer?
> >
> > I didn't expect `default_compressor` on ZRAM_SECONDARY_COMP
> > and below.  As far as I can tell, we only do this:
> >
> >       comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
> >
> > in zram_reset_device() and zram_add().  So, how does it end up in
> > ZRAM_SECONDARY_COMP...
> 
> Ugh, I know what's happening.  You don't have CONFIG_ZRAM_MULTI_COMP
> so that ZRAM_PRIMARY_COMP and ZRAM_SECONDARY_COMP are the same thing.
> Yeah, that all makes sense now, I haven't thought about it.

yes, I don't have CONFIG_ZRAM_MULTI_COMP set. I'll include your
comment into commit description for v2.

> Can you please send v2 (with the feedback resolved).

-- 
Best regards,
Andrey Skvortsov
Re: [PATCH] zram: don't free statically defined names
Posted by Sergey Senozhatsky 2 months ago
On (24/09/23 18:55), Andrey Skvortsov wrote:
[..]
> > Ugh, I know what's happening.  You don't have CONFIG_ZRAM_MULTI_COMP
> > so that ZRAM_PRIMARY_COMP and ZRAM_SECONDARY_COMP are the same thing.
> > Yeah, that all makes sense now, I haven't thought about it.
> 
> yes, I don't have CONFIG_ZRAM_MULTI_COMP set. I'll include your
> comment into commit description for v2.

Thanks.

Can you do it something like the diff below?  Let's iterate
->comp_algs from ZRAM_PRIMARY_COMP.  I don't really mind the
"Do not free statically defined" comment, up to you.

And the commit message probably can stay that: on !CONFIG_ZRAM_MULTI_COMP
systems ZRAM_SECONDARY_COMP can hold default_compressor, because it's
the same offset as ZRAM_PRIMARY_COMP, so we need to make sure that we
don't attempt to kfree() the statically defined comp name.

---

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index c3d245617083..ad9c9bc3ccfc 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -2115,8 +2115,10 @@ static void zram_destroy_comps(struct zram *zram)
                zram->num_active_comps--;
        }
 
-       for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
-               kfree(zram->comp_algs[prio]);
+       for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
+               /* Do not free statically defined compression algorithms */
+               if (zram->comp_algs[prio] != default_compressor)
+                       kfree(zram->comp_algs[prio]);
                zram->comp_algs[prio] = NULL;
        }
Re: [PATCH] zram: don't free statically defined names
Posted by Andrey Skvortsov 2 months ago
On 24-09-24 01:07, Sergey Senozhatsky wrote:
> On (24/09/23 18:55), Andrey Skvortsov wrote:
> [..]
> > > Ugh, I know what's happening.  You don't have CONFIG_ZRAM_MULTI_COMP
> > > so that ZRAM_PRIMARY_COMP and ZRAM_SECONDARY_COMP are the same thing.
> > > Yeah, that all makes sense now, I haven't thought about it.
> > 
> > yes, I don't have CONFIG_ZRAM_MULTI_COMP set. I'll include your
> > comment into commit description for v2.
> 
> Thanks.
> 
> Can you do it something like the diff below?  Let's iterate
> ->comp_algs from ZRAM_PRIMARY_COMP.  I don't really mind the
> "Do not free statically defined" comment, up to you.
> 
> And the commit message probably can stay that: on !CONFIG_ZRAM_MULTI_COMP
> systems ZRAM_SECONDARY_COMP can hold default_compressor, because it's
> the same offset as ZRAM_PRIMARY_COMP, so we need to make sure that we
> don't attempt to kfree() the statically defined comp name.
> 
> ---
> 
> diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> index c3d245617083..ad9c9bc3ccfc 100644
> --- a/drivers/block/zram/zram_drv.c
> +++ b/drivers/block/zram/zram_drv.c
> @@ -2115,8 +2115,10 @@ static void zram_destroy_comps(struct zram *zram)
>                 zram->num_active_comps--;
>         }
>  
> -       for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
> -               kfree(zram->comp_algs[prio]);
> +       for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
> +               /* Do not free statically defined compression algorithms */
> +               if (zram->comp_algs[prio] != default_compressor)
> +                       kfree(zram->comp_algs[prio]);
>                 zram->comp_algs[prio] = NULL;
>         }
Sorry, I've seen you comment after I've sent v2. I'll do this in v3. 

-- 
Best regards,
Andrey Skvortsov
Re: [PATCH] zram: don't free statically defined names
Posted by Christophe JAILLET 2 months ago
Le 23/09/2024 à 18:43, Andrey Skvortsov a écrit :
> On 24-09-24 01:07, Sergey Senozhatsky wrote:
>> On (24/09/23 18:55), Andrey Skvortsov wrote:
>> [..]
>>>> Ugh, I know what's happening.  You don't have CONFIG_ZRAM_MULTI_COMP
>>>> so that ZRAM_PRIMARY_COMP and ZRAM_SECONDARY_COMP are the same thing.
>>>> Yeah, that all makes sense now, I haven't thought about it.
>>>
>>> yes, I don't have CONFIG_ZRAM_MULTI_COMP set. I'll include your
>>> comment into commit description for v2.
>>
>> Thanks.
>>
>> Can you do it something like the diff below?  Let's iterate
>> ->comp_algs from ZRAM_PRIMARY_COMP.  I don't really mind the
>> "Do not free statically defined" comment, up to you.
>>
>> And the commit message probably can stay that: on !CONFIG_ZRAM_MULTI_COMP
>> systems ZRAM_SECONDARY_COMP can hold default_compressor, because it's
>> the same offset as ZRAM_PRIMARY_COMP, so we need to make sure that we
>> don't attempt to kfree() the statically defined comp name.
>>
>> ---
>>
>> diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
>> index c3d245617083..ad9c9bc3ccfc 100644
>> --- a/drivers/block/zram/zram_drv.c
>> +++ b/drivers/block/zram/zram_drv.c
>> @@ -2115,8 +2115,10 @@ static void zram_destroy_comps(struct zram *zram)
>>                  zram->num_active_comps--;
>>          }
>>   
>> -       for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
>> -               kfree(zram->comp_algs[prio]);
>> +       for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
>> +               /* Do not free statically defined compression algorithms */
>> +               if (zram->comp_algs[prio] != default_compressor)
>> +                       kfree(zram->comp_algs[prio]);
>>                  zram->comp_algs[prio] = NULL;
>>          }
> Sorry, I've seen you comment after I've sent v2. I'll do this in v3.
> 

Hi,

maybe kfree_const() to catch potential future cases and simplify code?

CJ