nfs: fix pnfs direct write memory leak
Subsystems (2)
PATCH 1/1 nfs: fix pnfs direct write memory leak
For pNFS direct writes, layout driver may dynamically allocate ds_cinfo.buckets. So we need to take care to free them when freeing dreq.
Ideally this needs to be done inside layout driver where ds_cinfo.buckets are allocated. But buckets are attached to dreq and reused across LD IO iterations. So I feel it's OK to free them in the generic layer.
--- fs/nfs/direct.c | 2 ++ 1 file changed, 2 insertions(+)
fs/nfs/direct.c +2 -0
@@ -266,6 +266,8 @@ static void nfs_direct_req_free(struct kref *kref) { struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); +if (dreq->ds_cinfo.nbuckets) +kfree (dreq->ds_cinfo.buckets);if (dreq->l_ctx !=NULL )nfs_put_lock_context (dreq->l_ctx);if (dreq->ctx !=NULL )
Signature · 1 line
1.9.1
Quoted 19 lines · Peng Tao
For pNFS direct writes, layout driver may dynamically allocate ds_cinfo.buckets. So we need to take care to free them when freeing dreq.
Ideally this needs to be done inside layout driver where ds_cinfo.buckets are allocated. But buckets are attached to dreq and reused across LD IO iterations. So I feel it's OK to free them in the generic layer.
Cc [email protected] [v3.4+]Signed-off-by Peng Tao <[email protected]>--- fs/nfs/direct.c | 2 ++ 1 file changed, 2 insertions(+)fs/nfs/direct.c +0
-0
fs/nfs/direct.c static void nfs_direct_req_free(struct kref *kref)
Quoted 9 lines
fs/nfs/direct.c +2
-1@@ -266,6 +266,8 @@ static void nfs_direct_req_free(struct kref *kref) { struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); +if (dreq->ds_cinfo.nbuckets) +kfree (dreq->ds_cinfo.buckets);if (dreq->l_ctx !=NULL )nfs_put_lock_context (dreq->l_ctx);if (dreq->ctx !=NULL ) --
Well spotted!
However doesn't the above need to be limited with an #ifdef CONFIG_NFS_V4_1? Perhaps you can add a helper in include/linux/nfs_xdr.h that takes a struct pnfs_ds_commit_info and then frees the nbuckets.
Note also that kfree() is happy to take a NULL argument.
Thanks!
Trond
Quoted 22 lines · Trond Myklebust
Quoted 21 lines · Peng Tao
For pNFS direct writes, layout driver may dynamically allocate ds_cinfo.buckets. So we need to take care to free them when freeing dreq.
Ideally this needs to be done inside layout driver where ds_cinfo.buckets are allocated. But buckets are attached to dreq and reused across LD IO iterations. So I feel it's OK to free them in the generic layer.
Cc [email protected] [v3.4+]Signed-off-by Peng Tao <[email protected]>--- fs/nfs/direct.c | 2 ++ 1 file changed, 2 insertions(+)fs/nfs/direct.c +0
-0@@ -266,6 +266,8 @@ static void nfs_direct_req_free(struct kref *kref) { struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
Quoted 15 lines
fs/nfs/direct.c static void nfs_direct_req_free(struct kref *kref)
Quoted 7 lines
fs/nfs/direct.c +2
-1+if (dreq->ds_cinfo.nbuckets) +kfree (dreq->ds_cinfo.buckets);if (dreq->l_ctx !=NULL )nfs_put_lock_context (dreq->l_ctx);if (dreq->ctx !=NULL ) --Well spotted!
However doesn't the above need to be limited with an #ifdefCONFIG_NFS_V4_1? Perhaps you can add a helper ininclude/linux/nfs_xdr.hthat takes a structpnfs_ds_commit_infoand then frees the nbuckets.
Note also thatkfree()is happy to take a NULL argument.
You are right! I'll send v2.
Thanks,
Tao