mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] nfsd: fix memory leak in nfsd_create_serv error paths
@ 2025-11-17 12:11 Shardul Bankar
  2025-11-17 15:36 ` Jeff Layton
  2025-11-17 16:15 ` Chuck Lever
  0 siblings, 2 replies; 3+ messages in thread
From: Shardul Bankar @ 2025-11-17 12:11 UTC (permalink / raw)
  To: shardulsb08
  Cc: linux-nfs, linux-kernel, syzbot+099461f8558eb0a1f4f3,
	chuck.lever, jlayton, neil, okorniev, Dai.Ngo, tom, janak,
	shardul.b

When nfsd_create_serv() calls percpu_ref_init() to initialize
nn->nfsd_net_ref, it allocates both a percpu reference counter
and a percpu_ref_data structure (64 bytes). However, if the
function fails later due to svc_create_pooled() returning NULL
or svc_bind() returning an error, these allocations are not
cleaned up, resulting in a memory leak.

The leak manifests as:
- Unreferenced percpu allocation (8 bytes per CPU)
- Unreferenced percpu_ref_data structure (64 bytes)

Fix this by adding percpu_ref_exit() calls in both error paths
to properly clean up the percpu_ref_init() allocations.

This patch fixes the percpu_ref leak in nfsd_create_serv() seen
as an auxiliary leak in syzbot report 099461f8558eb0a1f4f3; the
prepare_creds() and vsock-related leaks in the same report
remain to be addressed separately.

Reported-by: syzbot+099461f8558eb0a1f4f3@syzkaller.appspotmail.com
Link: https://syzkaller.appspot.com/bug?extid=099461f8558eb0a1f4f3
Fixes: 47e988147f40 ("nfsd: add nfsd_serv_try_get and nfsd_serv_put")
Signed-off-by: Shardul Bankar <shardul.b@mpiricsoftware.com>
---
 fs/nfsd/nfssvc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c
index 7057ddd7a0a8..32cc03a7e7be 100644
--- a/fs/nfsd/nfssvc.c
+++ b/fs/nfsd/nfssvc.c
@@ -633,12 +633,15 @@ int nfsd_create_serv(struct net *net)
 	serv = svc_create_pooled(nfsd_programs, ARRAY_SIZE(nfsd_programs),
 				 &nn->nfsd_svcstats,
 				 nfsd_max_blksize, nfsd);
-	if (serv == NULL)
+	if (serv == NULL) {
+		percpu_ref_exit(&nn->nfsd_net_ref);
 		return -ENOMEM;
+	}
 
 	error = svc_bind(serv, net);
 	if (error < 0) {
 		svc_destroy(&serv);
+		percpu_ref_exit(&nn->nfsd_net_ref);
 		return error;
 	}
 	spin_lock(&nfsd_notifier_lock);
-- 
2.34.1


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

* Re: [PATCH] nfsd: fix memory leak in nfsd_create_serv error paths
  2025-11-17 12:11 [PATCH] nfsd: fix memory leak in nfsd_create_serv error paths Shardul Bankar
@ 2025-11-17 15:36 ` Jeff Layton
  2025-11-17 16:15 ` Chuck Lever
  1 sibling, 0 replies; 3+ messages in thread
From: Jeff Layton @ 2025-11-17 15:36 UTC (permalink / raw)
  To: Shardul Bankar, shardulsb08
  Cc: linux-nfs, linux-kernel, syzbot+099461f8558eb0a1f4f3,
	chuck.lever, neil, okorniev, Dai.Ngo, tom, janak

On Mon, 2025-11-17 at 17:41 +0530, Shardul Bankar wrote:
> When nfsd_create_serv() calls percpu_ref_init() to initialize
> nn->nfsd_net_ref, it allocates both a percpu reference counter
> and a percpu_ref_data structure (64 bytes). However, if the
> function fails later due to svc_create_pooled() returning NULL
> or svc_bind() returning an error, these allocations are not
> cleaned up, resulting in a memory leak.
> 
> The leak manifests as:
> - Unreferenced percpu allocation (8 bytes per CPU)
> - Unreferenced percpu_ref_data structure (64 bytes)
> 
> Fix this by adding percpu_ref_exit() calls in both error paths
> to properly clean up the percpu_ref_init() allocations.
> 
> This patch fixes the percpu_ref leak in nfsd_create_serv() seen
> as an auxiliary leak in syzbot report 099461f8558eb0a1f4f3; the
> prepare_creds() and vsock-related leaks in the same report
> remain to be addressed separately.
> 
> Reported-by: syzbot+099461f8558eb0a1f4f3@syzkaller.appspotmail.com
> Link: https://syzkaller.appspot.com/bug?extid=099461f8558eb0a1f4f3
> Fixes: 47e988147f40 ("nfsd: add nfsd_serv_try_get and nfsd_serv_put")
> Signed-off-by: Shardul Bankar <shardul.b@mpiricsoftware.com>
> ---
>  fs/nfsd/nfssvc.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c
> index 7057ddd7a0a8..32cc03a7e7be 100644
> --- a/fs/nfsd/nfssvc.c
> +++ b/fs/nfsd/nfssvc.c
> @@ -633,12 +633,15 @@ int nfsd_create_serv(struct net *net)
>  	serv = svc_create_pooled(nfsd_programs, ARRAY_SIZE(nfsd_programs),
>  				 &nn->nfsd_svcstats,
>  				 nfsd_max_blksize, nfsd);
> -	if (serv == NULL)
> +	if (serv == NULL) {
> +		percpu_ref_exit(&nn->nfsd_net_ref);
>  		return -ENOMEM;
> +	}
>  
>  	error = svc_bind(serv, net);
>  	if (error < 0) {
>  		svc_destroy(&serv);
> +		percpu_ref_exit(&nn->nfsd_net_ref);
>  		return error;
>  	}
>  	spin_lock(&nfsd_notifier_lock);

Nice catch!

Reviewed-by: Jeff Layton <jlayton@kernel.org>

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

* Re: [PATCH] nfsd: fix memory leak in nfsd_create_serv error paths
  2025-11-17 12:11 [PATCH] nfsd: fix memory leak in nfsd_create_serv error paths Shardul Bankar
  2025-11-17 15:36 ` Jeff Layton
@ 2025-11-17 16:15 ` Chuck Lever
  1 sibling, 0 replies; 3+ messages in thread
From: Chuck Lever @ 2025-11-17 16:15 UTC (permalink / raw)
  To: shardulsb08, Shardul Bankar
  Cc: Chuck Lever, linux-nfs, linux-kernel,
	syzbot+099461f8558eb0a1f4f3, jlayton, neil, okorniev, Dai.Ngo,
	tom, janak

From: Chuck Lever <chuck.lever@oracle.com>

On Mon, 17 Nov 2025 17:41:21 +0530, Shardul Bankar wrote:
> When nfsd_create_serv() calls percpu_ref_init() to initialize
> nn->nfsd_net_ref, it allocates both a percpu reference counter
> and a percpu_ref_data structure (64 bytes). However, if the
> function fails later due to svc_create_pooled() returning NULL
> or svc_bind() returning an error, these allocations are not
> cleaned up, resulting in a memory leak.
> 
> [...]

Applied to nfsd-testing, thanks!

[1/1] nfsd: fix memory leak in nfsd_create_serv error paths
      commit: d0ce35d0a4fe945e72b57be84d6e4e59c28e87d3

--
Chuck Lever


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

end of thread, other threads:[~2025-11-17 16:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-17 12:11 [PATCH] nfsd: fix memory leak in nfsd_create_serv error paths Shardul Bankar
2025-11-17 15:36 ` Jeff Layton
2025-11-17 16:15 ` Chuck Lever

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®