[PATCH] NFS: check the return value of mempool_alloc()

xkernel.wang@foxmail.com posted 1 patch 4 years, 3 months ago
fs/nfs/write.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
[PATCH] NFS: check the return value of mempool_alloc()
Posted by xkernel.wang@foxmail.com 4 years, 3 months ago
From: Xiaoke Wang <xkernel.wang@foxmail.com>

The check was first removed in 518662e ("NFS: fix usage of mempools.")
as the passed GFP flags is `GFP_NOIO`.
While now the flag is changed to `GFP_KERNEL` by 2b17d72 ("NFS: Clean
up writeback code"), so it is better to check it.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
---
 fs/nfs/write.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/fs/nfs/write.c b/fs/nfs/write.c
index eae9bf1..831fad9 100644
--- a/fs/nfs/write.c
+++ b/fs/nfs/write.c
@@ -106,8 +106,10 @@ static struct nfs_pgio_header *nfs_writehdr_alloc(void)
 {
 	struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_KERNEL);
 
-	memset(p, 0, sizeof(*p));
-	p->rw_mode = FMODE_WRITE;
+	if (p) {
+		memset(p, 0, sizeof(*p));
+		p->rw_mode = FMODE_WRITE;
+	}
 	return p;
 }
 
--
Re: [PATCH] NFS: check the return value of mempool_alloc()
Posted by NeilBrown 4 years, 3 months ago
On Wed, 23 Mar 2022, xkernel.wang@foxmail.com wrote:
> From: Xiaoke Wang <xkernel.wang@foxmail.com>
> 
> The check was first removed in 518662e ("NFS: fix usage of mempools.")
> as the passed GFP flags is `GFP_NOIO`.
> While now the flag is changed to `GFP_KERNEL` by 2b17d72 ("NFS: Clean
> up writeback code"), so it is better to check it.

no.  GFP_KERNEL is not that different from GFP_NOIO.

mempool_alloc() can only fail with __GFP_DIRECT_RECLAIM is not passed.

Please try to understand the code before you change it.

NeilBrown


> 
> Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
> ---
>  fs/nfs/write.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/nfs/write.c b/fs/nfs/write.c
> index eae9bf1..831fad9 100644
> --- a/fs/nfs/write.c
> +++ b/fs/nfs/write.c
> @@ -106,8 +106,10 @@ static struct nfs_pgio_header *nfs_writehdr_alloc(void)
>  {
>  	struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_KERNEL);
>  
> -	memset(p, 0, sizeof(*p));
> -	p->rw_mode = FMODE_WRITE;
> +	if (p) {
> +		memset(p, 0, sizeof(*p));
> +		p->rw_mode = FMODE_WRITE;
> +	}
>  	return p;
>  }
>  
> -- 
> 
>