linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fanotify: add error handling for kmem_cache_create
@ 2018-06-12  4:19 Zhouyang Jia
  2018-06-12  5:37 ` Amir Goldstein
  0 siblings, 1 reply; 2+ messages in thread
From: Zhouyang Jia @ 2018-06-12  4:19 UTC (permalink / raw)
  Cc: Zhouyang Jia, Jan Kara, Amir Goldstein, linux-fsdevel, linux-kernel

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.

Signed-off-by: Zhouyang Jia <jiazhouyang09@gmail.com>
---
 fs/notify/fanotify/fanotify_user.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index ec4d8c5..e3fa861 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -959,9 +959,14 @@ static int __init fanotify_user_setup(void)
 {
 	fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
 	fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
+	if (!fanotify_mark_cache || !fanotify_event_cachep)
+		return -ENOMEM;
+
 	if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
 		fanotify_perm_event_cachep =
 			KMEM_CACHE(fanotify_perm_event_info, SLAB_PANIC);
+		if (!fanotify_perm_event_cachep)
+			return -ENOMEM;
 	}
 
 	return 0;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] fanotify: add error handling for kmem_cache_create
  2018-06-12  4:19 [PATCH] fanotify: add error handling for kmem_cache_create Zhouyang Jia
@ 2018-06-12  5:37 ` Amir Goldstein
  0 siblings, 0 replies; 2+ messages in thread
From: Amir Goldstein @ 2018-06-12  5:37 UTC (permalink / raw)
  To: Zhouyang Jia; +Cc: Jan Kara, linux-fsdevel, linux-kernel

On Tue, Jun 12, 2018 at 7:19 AM, Zhouyang Jia <jiazhouyang09@gmail.com> wrote:
> 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.
>
> Signed-off-by: Zhouyang Jia <jiazhouyang09@gmail.com>
> ---
>  fs/notify/fanotify/fanotify_user.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
> index ec4d8c5..e3fa861 100644
> --- a/fs/notify/fanotify/fanotify_user.c
> +++ b/fs/notify/fanotify/fanotify_user.c
> @@ -959,9 +959,14 @@ static int __init fanotify_user_setup(void)
>  {
>         fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
>         fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
> +       if (!fanotify_mark_cache || !fanotify_event_cachep)
> +               return -ENOMEM;

If only one failed need to free the other.

> +
>         if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
>                 fanotify_perm_event_cachep =
>                         KMEM_CACHE(fanotify_perm_event_info, SLAB_PANIC);
> +               if (!fanotify_perm_event_cachep)
> +                       return -ENOMEM;

here as well.
best implement as goto fail

>         }
>
>         return 0;

fail:
           if (fanotify_mark_cache)
                            kmem_cache_destroy(fanotify_mark_cache);
           ...
           return -ENOMEM;

Thanks,
Amir.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-06-12  5:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-12  4:19 [PATCH] fanotify: add error handling for kmem_cache_create Zhouyang Jia
2018-06-12  5:37 ` Amir Goldstein

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).