nfsd: fix memory leak in nfsd_create_serv error paths
Subsystems (2)
PATCH 1/1 nfsd: fix memory leak in nfsd_create_serv error paths
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.
--- fs/nfsd/nfssvc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fs/nfsd/nfssvc.c +4 -1
@@ -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);
Signature · 1 line
2.34.1
Quoted 38 lines · Shardul Bankar
When
nfsd_create_serv()callspercpu_ref_init()to initializenn->nfsd_net_ref, it allocates both a percpu reference counter and apercpu_ref_datastructure (64 bytes). However, if the function fails later due tosvc_create_pooled()returning NULL orsvc_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)
- Unreferencedpercpu_ref_datastructure (64 bytes)Fix this by adding
percpu_ref_exit()calls in both error paths to properly clean up thepercpu_ref_init()allocations.This patch fixes the
percpu_refleak innfsd_create_serv()seen as an auxiliary leak in syzbot report099461f8558eb0a1f4f3; theprepare_creds()and vsock-related leaks in the same report remain to be addressed separately.Reported-by [email protected]Fixes 47e988147f40 ("nfsd: add nfsd_serv_try_get and nfsd_serv_put")Signed-off-by Shardul Bankar <[email protected]>--- fs/nfsd/nfssvc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)fs/nfsd/nfssvc.c +2
-1@@ -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);
fs/nfsd/nfssvc.c int nfsd_create_serv(struct net *net) · line 647
Quoted 10 lines
fs/nfsd/nfssvc.c +2
-0return -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!
Chuck Lever
maintainerFrom: Chuck Lever <[email protected]>
Quoted 8 lines · Shardul Bankar
When
nfsd_create_serv()callspercpu_ref_init()to initializenn->nfsd_net_ref, it allocates both a percpu reference counter and apercpu_ref_datastructure (64 bytes). However, if the function fails later due tosvc_create_pooled()returning NULL orsvc_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