Skip to contentSkip to conversation

nfs: fix pnfs direct write memory leak

PATCH Superseded Peng Tao 2 participants 3 messages Nov 5, 2014

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Peng Tao

author
#0 lore raw

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
index 20cffc8..61f1904 100644
@@ -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
#1 lore raw
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.

---
 fs/nfs/direct.c | 2 ++
 1 file changed, 2 insertions(+)
fs/nfs/direct.c +0 -0
index 20cffc8..61f1904 100644

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

author
#2 lore raw
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.

---
 fs/nfs/direct.c | 2 ++
 1 file changed, 2 insertions(+)
fs/nfs/direct.c +0 -0
index 20cffc8..61f1904 100644
@@ -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 #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.

You are right! I'll send v2.

Thanks,
Tao