Skip to content

Commit da1aab3

Browse files
committed
md/raid1: r1buf_pool_alloc: free allocate pages when subsequent allocation fails.
When performing a user-request check/repair (MD_RECOVERY_REQUEST is set) on a raid1, we allocate multiple bios each with their own set of pages. If the page allocations for one bio fails, we currently do *not* free the pages allocated for the previous bios, nor do we free the bio itself. This patch frees all the already-allocate pages, and makes sure that all the bios are freed as well. This bug can cause a memory leak which can ultimately OOM a machine. It was introduced in 3.10-rc1. Fixes: a078760 Cc: Kent Overstreet <koverstreet@google.com> Cc: stable@vger.kernel.org (3.10+) Reported-by: Russell King - ARM Linux <linux@arm.linux.org.uk> Signed-off-by: NeilBrown <neilb@suse.de>
1 parent 035328c commit da1aab3

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

‎drivers/md/raid1.c‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
9797
struct pool_info *pi = data;
9898
struct r1bio *r1_bio;
9999
struct bio *bio;
100+
int need_pages;
100101
int i, j;
101102

102103
r1_bio = r1bio_pool_alloc(gfp_flags, pi);
@@ -119,15 +120,15 @@ static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
119120
* RESYNC_PAGES for each bio.
120121
*/
121122
if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
122-
j = pi->raid_disks;
123+
need_pages = pi->raid_disks;
123124
else
124-
j = 1;
125-
while(j--) {
125+
need_pages = 1;
126+
for (j = 0; j < need_pages; j++) {
126127
bio = r1_bio->bios[j];
127128
bio->bi_vcnt = RESYNC_PAGES;
128129

129130
if (bio_alloc_pages(bio, gfp_flags))
130-
goto out_free_bio;
131+
goto out_free_pages;
131132
}
132133
/* If not user-requests, copy the page pointers to all bios */
133134
if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
@@ -141,6 +142,14 @@ static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
141142

142143
return r1_bio;
143144

145+
out_free_pages:
146+
while (--j >= 0) {
147+
struct bio_vec *bv;
148+
149+
bio_for_each_segment_all(bv, r1_bio->bios[j], i)
150+
__free_page(bv->bv_page);
151+
}
152+
144153
out_free_bio:
145154
while (++j < pi->raid_disks)
146155
bio_put(r1_bio->bios[j]);

0 commit comments

Comments
 (0)