Skip to contentSkip to conversation

ceph: fix memory leak when reallocating pages array for writepages

PATCH In review Jeff Layton 2 participants 6 messages Jul 26, 2020 – Jul 27, 2020 Ceph client

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ceph: fix memory leak when reallocating pages array for writepages

Once we've replaced it, we don't want to keep the old one around anymore.

---
 fs/ceph/addr.c | 1 +
 1 file changed, 1 insertion(+)
fs/ceph/addr.c +1 -0
index 01ad09733ac7..01e167efa104 100644
@@ -1212,6 +1212,7 @@ static int ceph_writepages_start(struct address_space *mapping,
 			       locked_pages * sizeof(*pages));
 			memset(data_pages + i, 0,
 			       locked_pages * sizeof(*pages));
+			kfree(data_pages);
 		} else {
 			BUG_ON(num_ops != req->r_num_ops);
 			index = pages[i - 1]->index + 1;
Signature · 1 line
2.26.2
#1 lore raw
Quoted 13 lines · Jeff Layton

Once we've replaced it, we don't want to keep the old one around anymore.

---
  fs/ceph/addr.c | 1 +
  1 file changed, 1 insertion(+)
fs/ceph/addr.c +0 -0
index 01ad09733ac7..01e167efa104 100644

fs/ceph/addr.c static int ceph_writepages_start(struct address_space *mapping, · line 1214

Quoted 3 lines
fs/ceph/addr.c +0 -0
@@ -1212,6 +1212,7 @@ static int ceph_writepages_start(struct address_space *mapping,
  			       locked_pages * sizeof(*pages));
  			memset(data_pages + i, 0,
  			       locked_pages * sizeof(*pages));

BTW, do we still need to memset() the data_pages ?

Quoted 4 lines
fs/ceph/addr.c +1 -0
+			kfree(data_pages);
  		} else {
  			BUG_ON(num_ops != req->r_num_ops);
  			index = pages[i - 1]->index + 1;
#2 lore raw
Quoted 9 lines · Xiubo Li
Quoted 8 lines · Jeff Layton

Once we've replaced it, we don't want to keep the old one around anymore.

---
  fs/ceph/addr.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 11 lines

fs/ceph/addr.c static int ceph_writepages_start(struct address_space *mapping, · line 1214

Quoted 8 lines
fs/ceph/addr.c +0 -0
index 01ad09733ac7..01e167efa104 100644
@@ -1212,6 +1212,7 @@ static int ceph_writepages_start(struct address_space *mapping,
  			       locked_pages * sizeof(*pages));
  			memset(data_pages + i, 0,
  			       locked_pages * sizeof(*pages));

BTW, do we still need to memset() the data_pages ?

Self-NAK on this patch...

Zheng pointed out that this array is actually freed by the request handler after the submission. This loop is creating a new pages array for a second request.

As far as whether we need to memset the end of the original array...I don't think we do. It looks like the pointers at the end of the array are ignored once you go past the length of the request. That said, it's fairly cheap to do so, and I'm not inclined to change it, just in case there is code that does look at those pointers.

Quoted 7 lines
Quoted 5 lines
fs/ceph/addr.c +1 -0
+			kfree(data_pages);
  		} else {
  			BUG_ON(num_ops != req->r_num_ops);
  			index = pages[i - 1]->index + 1;
Signature · 1 line
Jeff Layton <[email protected]>
#3 lore raw
Quoted 20 lines · Jeff Layton
Quoted 9 lines · Xiubo Li
Quoted 8 lines · Jeff Layton

Once we've replaced it, we don't want to keep the old one around anymore.

---
   fs/ceph/addr.c | 1 +
   1 file changed, 1 insertion(+)
Quoted 10 lines

fs/ceph/addr.c static int ceph_writepages_start(struct address_space *mapping, · line 1214

Quoted 8 lines
fs/ceph/addr.c +0 -0
index 01ad09733ac7..01e167efa104 100644
@@ -1212,6 +1212,7 @@ static int ceph_writepages_start(struct address_space *mapping,
   			       locked_pages * sizeof(*pages));
   			memset(data_pages + i, 0,
   			       locked_pages * sizeof(*pages));

BTW, do we still need to memset() the data_pages ?

Quoted 5 lines

Self-NAK on this patch...

Zheng pointed out that this array is actually freed by the request handler after the submission. This loop is creating a new pages array for a second request.

Do you mean ceph_osd_data_release() ?

The request is only freeing the pages in that arrary, not the arrary itself, did I miss something ?

Quoted 11 lines

As far as whether we need to memset the end of the original array...I don't think we do. It looks like the pointers at the end of the array are ignored once you go past the length of the request. That said, it's fairly cheap to do so, and I'm not inclined to change it, just in case there is code that does look at those pointers.

Quoted 5 lines
Quoted 4 lines
fs/ceph/addr.c +1 -0
+			kfree(data_pages);
   		} else {
   			BUG_ON(num_ops != req->r_num_ops);
   			index = pages[i - 1]->index + 1;
#4 lore raw
Quoted 23 lines · Xiubo Li
Quoted 22 lines · Jeff Layton
Quoted 9 lines · Xiubo Li
Quoted 8 lines · Jeff Layton

Once we've replaced it, we don't want to keep the old one around anymore.

---
   fs/ceph/addr.c | 1 +
   1 file changed, 1 insertion(+)
Quoted 10 lines

fs/ceph/addr.c static int ceph_writepages_start(struct address_space *mapping, · line 1214

Quoted 8 lines
fs/ceph/addr.c +0 -0
index 01ad09733ac7..01e167efa104 100644
@@ -1212,6 +1212,7 @@ static int ceph_writepages_start(struct address_space *mapping,
   			       locked_pages * sizeof(*pages));
   			memset(data_pages + i, 0,
   			       locked_pages * sizeof(*pages));

BTW, do we still need to memset() the data_pages ?

Self-NAK on this patch...

Quoted 10 lines
Quoted 3 lines

Zheng pointed out that this array is actually freed by the request handler after the submission. This loop is creating a new pages array for a second request.

Do you mean ceph_osd_data_release() ?

The request is only freeing the pages in that arrary, not the arrary itself, did I miss something ?

No, I meant in writepages_finish(). It has this:

        if (osd_data->pages_from_pool)
                mempool_free(osd_data->pages,
                             ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
        else
                kfree(osd_data->pages);

The pages themselves are freed in the loop above that point however.

Quoted 10 lines
Quoted 10 lines

As far as whether we need to memset the end of the original array...I don't think we do. It looks like the pointers at the end of the array are ignored once you go past the length of the request. That said, it's fairly cheap to do so, and I'm not inclined to change it, just in case there is code that does look at those pointers.

Quoted 4 lines
Quoted 4 lines
fs/ceph/addr.c +1 -0
+			kfree(data_pages);
   		} else {
   			BUG_ON(num_ops != req->r_num_ops);
   			index = pages[i - 1]->index + 1;
Signature · 1 line
Jeff Layton <[email protected]>
#5 lore raw
Quoted 35 lines · Jeff Layton
Quoted 23 lines · Xiubo Li
Quoted 22 lines · Jeff Layton
Quoted 9 lines · Xiubo Li
Quoted 8 lines · Jeff Layton

Once we've replaced it, we don't want to keep the old one around anymore.

---
    fs/ceph/addr.c | 1 +
    1 file changed, 1 insertion(+)
Quoted 10 lines

fs/ceph/addr.c static int ceph_writepages_start(struct address_space *mapping, · line 1214

Quoted 8 lines
fs/ceph/addr.c +0 -0
index 01ad09733ac7..01e167efa104 100644
@@ -1212,6 +1212,7 @@ static int ceph_writepages_start(struct address_space *mapping,
    			       locked_pages * sizeof(*pages));
    			memset(data_pages + i, 0,
    			       locked_pages * sizeof(*pages));

BTW, do we still need to memset() the data_pages ?

Self-NAK on this patch...

Quoted 9 lines
Quoted 3 lines

Zheng pointed out that this array is actually freed by the request handler after the submission. This loop is creating a new pages array for a second request.

Do you mean ceph_osd_data_release() ?

The request is only freeing the pages in that arrary, not the arrary itself, did I miss something ?

No, I meant in writepages_finish(). It has this:

Quoted 7 lines
         if (osd_data->pages_from_pool)
                 mempool_free(osd_data->pages,
                              ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
         else
                 kfree(osd_data->pages);

The pages themselves are freed in the loop above that point however.

Okay, that's right.

Thanks.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

As far as whether we need to memset the end of the original array...I don't think we do. It looks like the pointers at the end of the array are ignored once you go past the length of the request. That said, it's fairly cheap to do so, and I'm not inclined to change it, just in case there is code that does look at those pointers.

Quoted 4 lines
Quoted 4 lines
fs/ceph/addr.c +1 -0
+			kfree(data_pages);
    		} else {
    			BUG_ON(num_ops != req->r_num_ops);
    			index = pages[i - 1]->index + 1;