mm, slab: fix use of SLAB_SUPPORTS_SYSFS in kmem_cache_release()
Subsystems (3)
PATCH 1/1 mm, slab: fix use of SLAB_SUPPORTS_SYSFS in kmem_cache_release()
The fix implemented in commit 4ec10268ed98 ("mm, slab: unlink slabinfo, sysfs and debugfs immediately") caused a subtle side effect due to which while destroying the kmem cache, the code path would never get into sysfs_slab_release() function even though SLAB_SUPPORTS_SYSFS is defined and slab state is FULL. Due to this side effect, we would never release kobject defined for kmem cache and leak the associated memory.
The issue here's with the use of __is_defined() macro in kmem_cache_ release(). The __is_defined() macro expands to __take_second_arg( arg1_or_junk 1, 0). If "arg1_or_junk" is defined to 1 then it expands to __take_second_arg(0, 1, 0) and returns 1. If "arg1_or_junk" is NOT defined to any value then it expands to __take_second_arg(... 1, 0) and returns 0.
In this particular issue, SLAB_SUPPORTS_SYSFS is defined without any associated value and that causes __is_defined(SLAB_SUPPORTS_SYSFS) to always evaluate to 0 and hence it would never invoke sysfs_slab_release().
This patch helps fix this issue by defining SLAB_SUPPORTS_SYSFS to 1.
--- mm/slab.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slab.h +1 -1
@@ -310,7 +310,7 @@ struct kmem_cache { };#if defined(CONFIG_SYSFS ) && !defined(CONFIG_SLUB_TINY ) -#define SLAB_SUPPORTS_SYSFS +#define SLAB_SUPPORTS_SYSFS 1void sysfs_slab_unlink (struct kmem_cache *s);void sysfs_slab_release (struct kmem_cache *s);#else
Signature · 1 line
2.45.2
Quoted 14 lines · Nilay Shroff
The fix implemented in commit
4ec10268ed98("mm, slab: unlink slabinfo, sysfs and debugfs immediately") caused a subtle side effect due to which while destroying the kmem cache, the code path would never get intosysfs_slab_release()function even thoughSLAB_SUPPORTS_SYSFSis defined and slab state is FULL. Due to this side effect, we would never release kobject defined for kmem cache and leak the associated memory.The issue here's with the use of
__is_defined()macro inkmem_cache_release(). The__is_defined()macro expands to__take_second_arg( arg1_or_junk 1, 0). If "arg1_or_junk" is defined to 1 then it expands to__take_second_arg(0, 1, 0)and returns 1. If "arg1_or_junk" is NOT defined to any value then it expands to __take_second_arg(... 1, 0) and returns 0.
Quoted 5 lines
In this particular issue,
SLAB_SUPPORTS_SYSFSis defined without any associated value and that causes__is_defined(SLAB_SUPPORTS_SYSFS)to always evaluate to 0 and hence it would never invokesysfs_slab_release().This patch helps fix this issue by defining
SLAB_SUPPORTS_SYSFSto 1.
Hi Nilay,
Thanks for your effort in investigating the issue and fixing it! This makes sense to me, but is there any reason the code avoids using IS_ENABLED()?
I think technically either IS_ENABLED() or __is_defined() (with your fix) would work
in this case, but it made me think "What is the difference between IS_ENABLED() and __is_defined()?"
IS_ENABLED() is already frequently used in mm and only few code snippets use __is_defined() directly.
Best,
Hyeonggon
Quoted 24 lines
Fixes 4ec10268ed98 ("mm, slab: unlink slabinfo, sysfs and debugfs immediately")Reported-by Yi Zhang <[email protected]>Closes https://lore.kernel.org/all/CAHj4cs9YCCcfmdxN43-9H3HnTYQsRtTYw1Kzq-L468GfLKAENA@mail.gmail.com/ open hereSigned-off-by Nilay Shroff <[email protected]>--- mm/slab.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/slab.h +1
-2@@ -310,7 +310,7 @@ struct kmem_cache { };#if defined(CONFIG_SYSFS ) && !defined(CONFIG_SLUB_TINY ) -#define SLAB_SUPPORTS_SYSFS +#define SLAB_SUPPORTS_SYSFS 1void sysfs_slab_unlink (struct kmem_cache *s);void sysfs_slab_release (struct kmem_cache *s);#else --2.45.2
Quoted 15 lines · Hyeonggon Yoo
Quoted 14 lines · Nilay Shroff
The fix implemented in commit
4ec10268ed98("mm, slab: unlink slabinfo, sysfs and debugfs immediately") caused a subtle side effect due to which while destroying the kmem cache, the code path would never get intosysfs_slab_release()function even thoughSLAB_SUPPORTS_SYSFSis defined and slab state is FULL. Due to this side effect, we would never release kobject defined for kmem cache and leak the associated memory.The issue here's with the use of
__is_defined()macro inkmem_cache_release(). The__is_defined()macro expands to__take_second_arg( arg1_or_junk 1, 0). If "arg1_or_junk" is defined to 1 then it expands to__take_second_arg(0, 1, 0)and returns 1. If "arg1_or_junk" is NOT defined to any value then it expands to __take_second_arg(... 1, 0) and returns 0.
Quoted 5 lines
Quoted 5 lines
In this particular issue,
SLAB_SUPPORTS_SYSFSis defined without any associated value and that causes__is_defined(SLAB_SUPPORTS_SYSFS)to always evaluate to 0 and hence it would never invokesysfs_slab_release().This patch helps fix this issue by defining
SLAB_SUPPORTS_SYSFSto 1.
Oops, thanks a lot for debugging and fixing this!
Quoted 7 lines
Hi Nilay,
Thanks for your effort in investigating the issue and fixing it! This makes sense to me, but is there any reason the code avoids using
IS_ENABLED()?
Quoted 7 lines
I think technically either
IS_ENABLED()or__is_defined()(with your fix) would work
in this case, but it made me think "What is the difference betweenIS_ENABLED()and__is_defined()?"
IS_ENABLED()is already frequently used in mm and only few code snippets use__is_defined()directly.
I was wary of using IS_ENABLED() because that's intended for CONFIG_ macros and SLAB_SUPPORTS_SYSFS isn't one, so even if it worked now, it wouldn't be guaranteed to stay working.
Quoted 27 lines
Best,
HyeonggonQuoted 24 lines
Fixes 4ec10268ed98 ("mm, slab: unlink slabinfo, sysfs and debugfs immediately")Reported-by Yi Zhang <[email protected]>Closes https://lore.kernel.org/all/CAHj4cs9YCCcfmdxN43-9H3HnTYQsRtTYw1Kzq-L468GfLKAENA@mail.gmail.com/ open hereSigned-off-by Nilay Shroff <[email protected]>--- mm/slab.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/slab.h +1
-2@@ -310,7 +310,7 @@ struct kmem_cache { };#if defined(CONFIG_SYSFS ) && !defined(CONFIG_SLUB_TINY ) -#define SLAB_SUPPORTS_SYSFS +#define SLAB_SUPPORTS_SYSFS 1void sysfs_slab_unlink (struct kmem_cache *s);void sysfs_slab_release (struct kmem_cache *s);#else --2.45.2
Hyeonggon Yoo
Reviewed-byQuoted 37 lines · Vlastimil Babka
Quoted 15 lines · Hyeonggon Yoo
Quoted 14 lines · Nilay Shroff
The fix implemented in commit
4ec10268ed98("mm, slab: unlink slabinfo, sysfs and debugfs immediately") caused a subtle side effect due to which while destroying the kmem cache, the code path would never get intosysfs_slab_release()function even thoughSLAB_SUPPORTS_SYSFSis defined and slab state is FULL. Due to this side effect, we would never release kobject defined for kmem cache and leak the associated memory.The issue here's with the use of
__is_defined()macro inkmem_cache_release(). The__is_defined()macro expands to__take_second_arg( arg1_or_junk 1, 0). If "arg1_or_junk" is defined to 1 then it expands to__take_second_arg(0, 1, 0)and returns 1. If "arg1_or_junk" is NOT defined to any value then it expands to __take_second_arg(... 1, 0) and returns 0.Quoted 5 lines
Quoted 5 lines
In this particular issue,
SLAB_SUPPORTS_SYSFSis defined without any associated value and that causes__is_defined(SLAB_SUPPORTS_SYSFS)to always evaluate to 0 and hence it would never invokesysfs_slab_release().This patch helps fix this issue by defining
SLAB_SUPPORTS_SYSFSto 1.Oops, thanks a lot for debugging and fixing this!
Quoted 12 lines
Hi Nilay,
Thanks for your effort in investigating the issue and fixing it! This makes sense to me, but is there any reason the code avoids using
IS_ENABLED()?I think technically either
IS_ENABLED()or__is_defined()(with your fix) would work
in this case, but it made me think "What is the difference betweenIS_ENABLED()and__is_defined()?"
Quoted 6 lines
Quoted 2 lines
IS_ENABLED()is already frequently used in mm and only few code snippets use__is_defined()directly.I was wary of using
IS_ENABLED()because that's intended forCONFIG_macros andSLAB_SUPPORTS_SYSFSisn't one, so even if it worked now, it wouldn't be guaranteed to stay working.
Oh, you are right. After looking into the history, __is_defined() is actually intended for
non-config macros.
With that in mind, the fix looks good to me.
Quoted 28 lines
Quoted 27 lines
Best,
HyeonggonQuoted 24 lines
Fixes 4ec10268ed98 ("mm, slab: unlink slabinfo, sysfs and debugfs immediately")Reported-by Yi Zhang <[email protected]>Closes https://lore.kernel.org/all/CAHj4cs9YCCcfmdxN43-9H3HnTYQsRtTYw1Kzq-L468GfLKAENA@mail.gmail.com/ open hereSigned-off-by Nilay Shroff <[email protected]>--- mm/slab.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/slab.h +1
-2@@ -310,7 +310,7 @@ struct kmem_cache { };#if defined(CONFIG_SYSFS ) && !defined(CONFIG_SLUB_TINY ) -#define SLAB_SUPPORTS_SYSFS +#define SLAB_SUPPORTS_SYSFS 1void sysfs_slab_unlink (struct kmem_cache *s);void sysfs_slab_release (struct kmem_cache *s);#else --2.45.2
Quoted 18 lines · Nilay Shroff
The fix implemented in commit
4ec10268ed98("mm, slab: unlink slabinfo, sysfs and debugfs immediately") caused a subtle side effect due to which while destroying the kmem cache, the code path would never get intosysfs_slab_release()function even thoughSLAB_SUPPORTS_SYSFSis defined and slab state is FULL. Due to this side effect, we would never release kobject defined for kmem cache and leak the associated memory.The issue here's with the use of
__is_defined()macro inkmem_cache_release(). The__is_defined()macro expands to__take_second_arg( arg1_or_junk 1, 0). If "arg1_or_junk" is defined to 1 then it expands to__take_second_arg(0, 1, 0)and returns 1. If "arg1_or_junk" is NOT defined to any value then it expands to __take_second_arg(... 1, 0) and returns 0.In this particular issue,
SLAB_SUPPORTS_SYSFSis defined without any associated value and that causes__is_defined(SLAB_SUPPORTS_SYSFS)to always evaluate to 0 and hence it would never invokesysfs_slab_release().
Quoted 6 lines
This patch helps fix this issue by defining
SLAB_SUPPORTS_SYSFSto 1.Fixes 4ec10268ed98 ("mm, slab: unlink slabinfo, sysfs and debugfs immediately")Reported-by Yi Zhang <[email protected]>Closes https://lore.kernel.org/all/CAHj4cs9YCCcfmdxN43-9H3HnTYQsRtTYw1Kzq-L468GfLKAENA@mail.gmail.com/ open hereSigned-off-by Nilay Shroff <[email protected]>
Quoted 20 lines
--- mm/slab.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/slab.h +1
-2@@ -310,7 +310,7 @@ struct kmem_cache { };#if defined(CONFIG_SYSFS ) && !defined(CONFIG_SLUB_TINY ) -#define SLAB_SUPPORTS_SYSFS +#define SLAB_SUPPORTS_SYSFS 1void sysfs_slab_unlink (struct kmem_cache *s);void sysfs_slab_release (struct kmem_cache *s);#else --2.45.2
Signature · 2 lines
Best Regards, Yi Zhang
Quoted 17 lines · Nilay Shroff
The fix implemented in commit
4ec10268ed98("mm, slab: unlink slabinfo, sysfs and debugfs immediately") caused a subtle side effect due to which while destroying the kmem cache, the code path would never get intosysfs_slab_release()function even thoughSLAB_SUPPORTS_SYSFSis defined and slab state is FULL. Due to this side effect, we would never release kobject defined for kmem cache and leak the associated memory.The issue here's with the use of
__is_defined()macro inkmem_cache_release(). The__is_defined()macro expands to__take_second_arg( arg1_or_junk 1, 0). If "arg1_or_junk" is defined to 1 then it expands to__take_second_arg(0, 1, 0)and returns 1. If "arg1_or_junk" is NOT defined to any value then it expands to __take_second_arg(... 1, 0) and returns 0.In this particular issue,
SLAB_SUPPORTS_SYSFSis defined without any associated value and that causes__is_defined(SLAB_SUPPORTS_SYSFS)to always evaluate to 0 and hence it would never invokesysfs_slab_release().
Quoted 6 lines
This patch helps fix this issue by defining
SLAB_SUPPORTS_SYSFSto 1.Fixes 4ec10268ed98 ("mm, slab: unlink slabinfo, sysfs and debugfs immediately")Reported-by Yi Zhang <[email protected]>Closes https://lore.kernel.org/all/CAHj4cs9YCCcfmdxN43-9H3HnTYQsRtTYw1Kzq-L468GfLKAENA@mail.gmail.com/ open hereSigned-off-by Nilay Shroff <[email protected]>
Added to slab/for-next, thanks!
Quoted 17 lines
--- mm/slab.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/slab.h +1
-1@@ -310,7 +310,7 @@ struct kmem_cache { };#if defined(CONFIG_SYSFS ) && !defined(CONFIG_SLUB_TINY ) -#define SLAB_SUPPORTS_SYSFS +#define SLAB_SUPPORTS_SYSFS 1void sysfs_slab_unlink (struct kmem_cache *s);void sysfs_slab_release (struct kmem_cache *s);#else