2022-05-09 03:20:30

by Paran Lee

[permalink] [raw]
Subject: [PATCH] slub: kunit catch kmem_cache_alloc failed

Catch kmem_cache_alloc failed on slub kunit test.

Signed-off-by: Paran Lee <[email protected]>
---
lib/slub_kunit.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)

diff --git a/lib/slub_kunit.c b/lib/slub_kunit.c
index 8662dc6cb509..7b80241dd498 100644
--- a/lib/slub_kunit.c
+++ b/lib/slub_kunit.c
@@ -32,6 +32,11 @@ static void test_next_pointer(struct kunit *test)
struct kmem_cache *s = kmem_cache_create("TestSlub_next_ptr_free", 64, 0,
SLAB_POISON, NULL);
u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
+ if (!p) {
+ kunit_err(test, "Allocation failed: %s\n", __func__);
+ kmem_cache_destroy(s);
+ return;
+ }
unsigned long tmp;
unsigned long *ptr_addr;

@@ -77,6 +82,11 @@ static void test_first_word(struct kunit *test)
struct kmem_cache *s = kmem_cache_create("TestSlub_1th_word_free", 64, 0,
SLAB_POISON, NULL);
u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
+ if (!p) {
+ kunit_err(test, "Allocation failed: %s\n", __func__);
+ kmem_cache_destroy(s);
+ return;
+ }

kmem_cache_free(s, p);
*p = 0x78;
@@ -92,6 +102,11 @@ static void test_clobber_50th_byte(struct kunit *test)
struct kmem_cache *s = kmem_cache_create("TestSlub_50th_word_free", 64, 0,
SLAB_POISON, NULL);
u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
+ if (!p) {
+ kunit_err(test, "Allocation failed: %s\n", __func__);
+ kmem_cache_destroy(s);
+ return;
+ }

kmem_cache_free(s, p);
p[50] = 0x9a;
@@ -108,6 +123,11 @@ static void test_clobber_redzone_free(struct kunit *test)
struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_free", 64, 0,
SLAB_RED_ZONE, NULL);
u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
+ if (!p) {
+ kunit_err(test, "Allocation failed: %s\n", __func__);
+ kmem_cache_destroy(s);
+ return;
+ }

kasan_disable_current();
kmem_cache_free(s, p);
--
2.25.1



2022-05-09 05:59:02

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] slub: kunit catch kmem_cache_alloc failed

Hi Paran,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.18-rc5 next-20220506]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/intel-lab-lkp/linux/commits/Paran-Lee/slub-kunit-catch-kmem_cache_alloc-failed/20220506-125540
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git fe27d189e3f42e31d3c8223d5daed7285e334c5e
config: hexagon-randconfig-r014-20220505 (https://download.01.org/0day-ci/archive/20220506/[email protected]/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project 5e004fb787698440a387750db7f8028e7cb14cfc)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/4827e156a14ba66c648125c98967c96178ca024d
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Paran-Lee/slub-kunit-catch-kmem_cache_alloc-failed/20220506-125540
git checkout 4827e156a14ba66c648125c98967c96178ca024d
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash lib/

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All warnings (new ones prefixed by >>):

>> lib/slub_kunit.c:40:16: warning: mixing declarations and code is incompatible with standards before C99 [-Wdeclaration-after-statement]
unsigned long tmp;
^
1 warning generated.


vim +40 lib/slub_kunit.c

1f9f78b1b376f8 Oliver Glitta 2021-06-28 28
1f9f78b1b376f8 Oliver Glitta 2021-06-28 29 #ifndef CONFIG_KASAN
1f9f78b1b376f8 Oliver Glitta 2021-06-28 30 static void test_next_pointer(struct kunit *test)
1f9f78b1b376f8 Oliver Glitta 2021-06-28 31 {
1f9f78b1b376f8 Oliver Glitta 2021-06-28 32 struct kmem_cache *s = kmem_cache_create("TestSlub_next_ptr_free", 64, 0,
1f9f78b1b376f8 Oliver Glitta 2021-06-28 33 SLAB_POISON, NULL);
1f9f78b1b376f8 Oliver Glitta 2021-06-28 34 u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
4827e156a14ba6 Paran Lee 2022-05-06 35 if (!p) {
4827e156a14ba6 Paran Lee 2022-05-06 36 kunit_err(test, "Allocation failed: %s\n", __func__);
4827e156a14ba6 Paran Lee 2022-05-06 37 kmem_cache_destroy(s);
4827e156a14ba6 Paran Lee 2022-05-06 38 return;
4827e156a14ba6 Paran Lee 2022-05-06 39 }
1f9f78b1b376f8 Oliver Glitta 2021-06-28 @40 unsigned long tmp;
1f9f78b1b376f8 Oliver Glitta 2021-06-28 41 unsigned long *ptr_addr;
1f9f78b1b376f8 Oliver Glitta 2021-06-28 42
1f9f78b1b376f8 Oliver Glitta 2021-06-28 43 kmem_cache_free(s, p);
1f9f78b1b376f8 Oliver Glitta 2021-06-28 44
1f9f78b1b376f8 Oliver Glitta 2021-06-28 45 ptr_addr = (unsigned long *)(p + s->offset);
1f9f78b1b376f8 Oliver Glitta 2021-06-28 46 tmp = *ptr_addr;
1f9f78b1b376f8 Oliver Glitta 2021-06-28 47 p[s->offset] = 0x12;
1f9f78b1b376f8 Oliver Glitta 2021-06-28 48
1f9f78b1b376f8 Oliver Glitta 2021-06-28 49 /*
1f9f78b1b376f8 Oliver Glitta 2021-06-28 50 * Expecting three errors.
1f9f78b1b376f8 Oliver Glitta 2021-06-28 51 * One for the corrupted freechain and the other one for the wrong
1f9f78b1b376f8 Oliver Glitta 2021-06-28 52 * count of objects in use. The third error is fixing broken cache.
1f9f78b1b376f8 Oliver Glitta 2021-06-28 53 */
1f9f78b1b376f8 Oliver Glitta 2021-06-28 54 validate_slab_cache(s);
1f9f78b1b376f8 Oliver Glitta 2021-06-28 55 KUNIT_EXPECT_EQ(test, 3, slab_errors);
1f9f78b1b376f8 Oliver Glitta 2021-06-28 56
1f9f78b1b376f8 Oliver Glitta 2021-06-28 57 /*
1f9f78b1b376f8 Oliver Glitta 2021-06-28 58 * Try to repair corrupted freepointer.
1f9f78b1b376f8 Oliver Glitta 2021-06-28 59 * Still expecting two errors. The first for the wrong count
1f9f78b1b376f8 Oliver Glitta 2021-06-28 60 * of objects in use.
1f9f78b1b376f8 Oliver Glitta 2021-06-28 61 * The second error is for fixing broken cache.
1f9f78b1b376f8 Oliver Glitta 2021-06-28 62 */
1f9f78b1b376f8 Oliver Glitta 2021-06-28 63 *ptr_addr = tmp;
1f9f78b1b376f8 Oliver Glitta 2021-06-28 64 slab_errors = 0;
1f9f78b1b376f8 Oliver Glitta 2021-06-28 65
1f9f78b1b376f8 Oliver Glitta 2021-06-28 66 validate_slab_cache(s);
1f9f78b1b376f8 Oliver Glitta 2021-06-28 67 KUNIT_EXPECT_EQ(test, 2, slab_errors);
1f9f78b1b376f8 Oliver Glitta 2021-06-28 68
1f9f78b1b376f8 Oliver Glitta 2021-06-28 69 /*
1f9f78b1b376f8 Oliver Glitta 2021-06-28 70 * Previous validation repaired the count of objects in use.
1f9f78b1b376f8 Oliver Glitta 2021-06-28 71 * Now expecting no error.
1f9f78b1b376f8 Oliver Glitta 2021-06-28 72 */
1f9f78b1b376f8 Oliver Glitta 2021-06-28 73 slab_errors = 0;
1f9f78b1b376f8 Oliver Glitta 2021-06-28 74 validate_slab_cache(s);
1f9f78b1b376f8 Oliver Glitta 2021-06-28 75 KUNIT_EXPECT_EQ(test, 0, slab_errors);
1f9f78b1b376f8 Oliver Glitta 2021-06-28 76
1f9f78b1b376f8 Oliver Glitta 2021-06-28 77 kmem_cache_destroy(s);
1f9f78b1b376f8 Oliver Glitta 2021-06-28 78 }
1f9f78b1b376f8 Oliver Glitta 2021-06-28 79

--
0-DAY CI Kernel Test Service
https://01.org/lkp

2022-05-09 12:02:06

by Hyeonggon Yoo

[permalink] [raw]
Subject: Re: [PATCH] slub: kunit catch kmem_cache_alloc failed

On Fri, May 06, 2022 at 01:53:19PM +0900, Paran Lee wrote:
> Catch kmem_cache_alloc failed on slub kunit test.
>
> Signed-off-by: Paran Lee <[email protected]>
> ---
> lib/slub_kunit.c | 20 ++++++++++++++++++++
> 1 file changed, 20 insertions(+)
>
> diff --git a/lib/slub_kunit.c b/lib/slub_kunit.c
> index 8662dc6cb509..7b80241dd498 100644
> --- a/lib/slub_kunit.c
> +++ b/lib/slub_kunit.c
> @@ -32,6 +32,11 @@ static void test_next_pointer(struct kunit *test)
> struct kmem_cache *s = kmem_cache_create("TestSlub_next_ptr_free", 64, 0,
> SLAB_POISON, NULL);
> u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
> + if (!p) {
> + kunit_err(test, "Allocation failed: %s\n", __func__);
> + kmem_cache_destroy(s);
> + return;
> + }
> unsigned long tmp;
> unsigned long *ptr_addr;
>
> @@ -77,6 +82,11 @@ static void test_first_word(struct kunit *test)
> struct kmem_cache *s = kmem_cache_create("TestSlub_1th_word_free", 64, 0,
> SLAB_POISON, NULL);

Recently SLAB_NO_USER_FLAGS is added for test caches.
Looks like it's not based on Vlastimil's slab tree:
https://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab.git

> u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
> + if (!p) {
> + kunit_err(test, "Allocation failed: %s\n", __func__);
> + kmem_cache_destroy(s);
> + return;
> + }
>
> kmem_cache_free(s, p);
> *p = 0x78;
> @@ -92,6 +102,11 @@ static void test_clobber_50th_byte(struct kunit *test)
> struct kmem_cache *s = kmem_cache_create("TestSlub_50th_word_free", 64, 0,
> SLAB_POISON, NULL);
> u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
> + if (!p) {
> + kunit_err(test, "Allocation failed: %s\n", __func__);
> + kmem_cache_destroy(s);
> + return;
> + }
>
> kmem_cache_free(s, p);
> p[50] = 0x9a;
> @@ -108,6 +123,11 @@ static void test_clobber_redzone_free(struct kunit *test)
> struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_free", 64, 0,
> SLAB_RED_ZONE, NULL);
> u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
> + if (!p) {
> + kunit_err(test, "Allocation failed: %s\n", __func__);
> + kmem_cache_destroy(s);
> + return;
> + }
>
> kasan_disable_current();
> kmem_cache_free(s, p);
> --
> 2.25.1
>

To be more paranoid kmem_cache_create() can also fail - but I'm not sure
these checks are useful for tests.

--
Thanks,
Hyeonggon