Skip to contentSkip to conversation

fsnotify: add error handling for kmem_cache_create

PATCH In review Zhouyang Jia 3 participants 3 messages Jun 12, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fsnotify: add error handling for kmem_cache_create

When kmem_cache_create fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling kmem_cache_create.

---
 fs/notify/dnotify/dnotify.c | 3 +++
 1 file changed, 3 insertions(+)
fs/notify/dnotify/dnotify.c +3 -0
index 63a1ca4..216b411 100644
@@ -387,6 +387,9 @@ static int __init dnotify_init(void)
 	dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC);
 	dnotify_mark_cache = KMEM_CACHE(dnotify_mark, SLAB_PANIC);
 
+	if (!dnotify_struct_cache || !dnotify_mark_cache)
+		return -ENOMEM;
+
 	dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops);
 	if (IS_ERR(dnotify_group))
 		panic("unable to allocate fsnotify group for dnotify\n");
Signature · 1 line
2.7.4
#1 lore raw
Quoted 15 lines · Zhouyang Jia

When kmem_cache_create fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling kmem_cache_create.

---
 fs/notify/dnotify/dnotify.c | 3 +++
 1 file changed, 3 insertions(+)
fs/notify/dnotify/dnotify.c +0 -0
index 63a1ca4..216b411 100644

fs/notify/dnotify/dnotify.c static int __init dnotify_init(void) · line 392

Quoted 6 lines
fs/notify/dnotify/dnotify.c +3 -0
@@ -387,6 +387,9 @@ static int __init dnotify_init(void)
        dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC);
        dnotify_mark_cache = KMEM_CACHE(dnotify_mark, SLAB_PANIC);
 
+       if (!dnotify_struct_cache || !dnotify_mark_cache)
+               return -ENOMEM;
+

If only one failed need to free the other.

Quoted 6 lines
fs/notify/dnotify/dnotify.c +0 -1
        dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops);
        if (IS_ERR(dnotify_group))
                panic("unable to allocate fsnotify group for dnotify\n");
--

2.7.4

maintainer
#2 lore raw
In reply to Zhouyang Jia
Quoted 15 lines · Zhouyang Jia

When kmem_cache_create fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling kmem_cache_create.

---
 fs/notify/dnotify/dnotify.c | 3 +++
 1 file changed, 3 insertions(+)
fs/notify/dnotify/dnotify.c +0 -0
index 63a1ca4..216b411 100644

fs/notify/dnotify/dnotify.c static int __init dnotify_init(void) · line 392

Quoted 6 lines
fs/notify/dnotify/dnotify.c +3 -0
@@ -387,6 +387,9 @@ static int __init dnotify_init(void)
 	dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC);
 	dnotify_mark_cache = KMEM_CACHE(dnotify_mark, SLAB_PANIC);
 
+	if (!dnotify_struct_cache || !dnotify_mark_cache)
+		return -ENOMEM;
+

Caches are allocated with SLAB_PANIC - i.e., we panic the system if caches cannot be created. And that's intended since something is really wrong with the system in that case. So this check is pointless.

Honza

Quoted 6 lines
fs/notify/dnotify/dnotify.c +0 -0
 	dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops);
 	if (IS_ERR(dnotify_group))
 		panic("unable to allocate fsnotify group for dnotify\n");
Signature · 1 line
2.7.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR