2018-06-12 04:04:41

by Zhouyang Jia

[permalink] [raw]
Subject: [PATCH] gfs2: add error handling for kmem_cache_zalloc

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

This patch adds error-handling code after calling kmem_cache_zalloc.

Signed-off-by: Zhouyang Jia <[email protected]>
---
fs/gfs2/trans.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/fs/gfs2/trans.c b/fs/gfs2/trans.c
index 064c9a0..da81eb4 100644
--- a/fs/gfs2/trans.c
+++ b/fs/gfs2/trans.c
@@ -130,6 +130,9 @@ static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
struct gfs2_bufdata *bd;

bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
+ if (!bd)
+ return NULL;
+
bd->bd_bh = bh;
bd->bd_gl = gl;
bd->bd_ops = lops;
--
2.7.4



2018-06-12 07:25:32

by Chengguang Xu

[permalink] [raw]
Subject: Re: [PATCH] gfs2: add error handling for kmem_cache_zalloc


> ?? 2018??6??12?գ?????11:38??Zhouyang Jia <[email protected]> д????
>
> When kmem_cache_zalloc fails, the lack of error-handling code may
> cause unexpected results.
>
> This patch adds error-handling code after calling kmem_cache_zalloc.

Should not fail on this case.

Thanks,
Chengguang.


>
> Signed-off-by: Zhouyang Jia <[email protected]>
> ---
> fs/gfs2/trans.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/fs/gfs2/trans.c b/fs/gfs2/trans.c
> index 064c9a0..da81eb4 100644
> --- a/fs/gfs2/trans.c
> +++ b/fs/gfs2/trans.c
> @@ -130,6 +130,9 @@ static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
> struct gfs2_bufdata *bd;
>
> bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
> + if (!bd)
> + return NULL;
> +
> bd->bd_bh = bh;
> bd->bd_gl = gl;
> bd->bd_ops = lops;
> --
> 2.7.4
>


2018-06-12 08:24:20

by Andreas Gruenbacher

[permalink] [raw]
Subject: Re: [PATCH] gfs2: add error handling for kmem_cache_zalloc

On 12 June 2018 at 09:24, [email protected] <[email protected]> wrote:
>
>> 在 2018年6月12日,上午11:38,Zhouyang Jia <[email protected]> 写道:
>>
>> When kmem_cache_zalloc fails, the lack of error-handling code may
>> cause unexpected results.
>>
>> This patch adds error-handling code after calling kmem_cache_zalloc.
>
> Should not fail on this case.

Indeed, with the __GFP_NOFAIL flag, kmem_cache_zalloc will not fail.
This is documented in include/linux/gfp.h.

Thanks,
Andreas

2018-06-12 12:21:38

by Steven Whitehouse

[permalink] [raw]
Subject: Re: [Cluster-devel] [PATCH] gfs2: add error handling for kmem_cache_zalloc

Hi,


On 12/06/18 08:24, [email protected] wrote:
>> ?? 2018??6??12?գ?????11:38??Zhouyang Jia <[email protected]> д????
>>
>> When kmem_cache_zalloc fails, the lack of error-handling code may
>> cause unexpected results.
>>
>> This patch adds error-handling code after calling kmem_cache_zalloc.
> Should not fail on this case.
>
> Thanks,
> Chengguang.
Indeed. It is one of the few GFP_NOFAIL allocations left in GFS2. It
would be good to figure out how to remove the last few of these
allocations, but simply adding a check for a NULL return is not the
right solution here anyway. I would also add that if this problem was
easier then I'm sure we'd have solved it before - the right answer is
not very obvious in this case,

Steve.

>
>> Signed-off-by: Zhouyang Jia <[email protected]>
>> ---
>> fs/gfs2/trans.c | 3 +++
>> 1 file changed, 3 insertions(+)
>>
>> diff --git a/fs/gfs2/trans.c b/fs/gfs2/trans.c
>> index 064c9a0..da81eb4 100644
>> --- a/fs/gfs2/trans.c
>> +++ b/fs/gfs2/trans.c
>> @@ -130,6 +130,9 @@ static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
>> struct gfs2_bufdata *bd;
>>
>> bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
>> + if (!bd)
>> + return NULL;
>> +
>> bd->bd_bh = bh;
>> bd->bd_gl = gl;
>> bd->bd_ops = lops;
>> --
>> 2.7.4
>>
>