public inbox for linux-ext4@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/8] ext4 crypto: fix memory leaks in ext4_encrypted_zeroout
@ 2015-05-28 23:47 Theodore Ts'o
  2015-05-28 23:47 ` [PATCH 2/8] ext4 crypto: set up encryption info for new inodes in ext4_inherit_context() Theodore Ts'o
                   ` (7 more replies)
  0 siblings, 8 replies; 14+ messages in thread
From: Theodore Ts'o @ 2015-05-28 23:47 UTC (permalink / raw)
  To: Ext4 Developers List; +Cc: jaegeuk, mhalcrow, Theodore Ts'o

ext4_encrypted_zeroout() could end up leaking a bio and bounce page.
Fortunately it's not used much.  While we're fixing things up,
refactor out common code into the static function alloc_bounce_page().

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
 fs/ext4/crypto.c | 56 +++++++++++++++++++++++++-------------------------------
 1 file changed, 25 insertions(+), 31 deletions(-)

diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
index 68c7ab8..e43ed93 100644
--- a/fs/ext4/crypto.c
+++ b/fs/ext4/crypto.c
@@ -324,6 +324,28 @@ static int ext4_page_crypto(struct ext4_crypto_ctx *ctx,
 	return 0;
 }
 
+static struct page *alloc_bounce_page(struct ext4_crypto_ctx *ctx)
+{
+	struct page *ciphertext_page = alloc_page(GFP_NOFS);
+
+	if (!ciphertext_page) {
+		/* This is a potential bottleneck, but at least we'll have
+		 * forward progress. */
+		ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
+						 GFP_NOFS);
+		if (WARN_ON_ONCE(!ciphertext_page)) {
+			ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
+							 GFP_NOFS | __GFP_WAIT);
+		}
+		ctx->flags &= ~EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
+	} else {
+		ctx->flags |= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
+	}
+	ctx->flags |= EXT4_WRITE_PATH_FL;
+	ctx->w.bounce_page = ciphertext_page;
+	return ciphertext_page;
+}
+
 /**
  * ext4_encrypt() - Encrypts a page
  * @inode:          The inode for which the encryption should take place
@@ -353,22 +375,7 @@ struct page *ext4_encrypt(struct inode *inode,
 		return (struct page *) ctx;
 
 	/* The encryption operation will require a bounce page. */
-	ciphertext_page = alloc_page(GFP_NOFS);
-	if (!ciphertext_page) {
-		/* This is a potential bottleneck, but at least we'll have
-		 * forward progress. */
-		ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
-						 GFP_NOFS);
-		if (WARN_ON_ONCE(!ciphertext_page)) {
-			ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
-							 GFP_NOFS | __GFP_WAIT);
-		}
-		ctx->flags &= ~EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
-	} else {
-		ctx->flags |= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
-	}
-	ctx->flags |= EXT4_WRITE_PATH_FL;
-	ctx->w.bounce_page = ciphertext_page;
+	ciphertext_page = alloc_bounce_page(ctx);
 	ctx->w.control_page = plaintext_page;
 	err = ext4_page_crypto(ctx, inode, EXT4_ENCRYPT, plaintext_page->index,
 			       plaintext_page, ciphertext_page);
@@ -434,21 +441,7 @@ int ext4_encrypted_zeroout(struct inode *inode, struct ext4_extent *ex)
 	if (IS_ERR(ctx))
 		return PTR_ERR(ctx);
 
-	ciphertext_page = alloc_page(GFP_NOFS);
-	if (!ciphertext_page) {
-		/* This is a potential bottleneck, but at least we'll have
-		 * forward progress. */
-		ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
-						 GFP_NOFS);
-		if (WARN_ON_ONCE(!ciphertext_page)) {
-			ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
-							 GFP_NOFS | __GFP_WAIT);
-		}
-		ctx->flags &= ~EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
-	} else {
-		ctx->flags |= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
-	}
-	ctx->w.bounce_page = ciphertext_page;
+	ciphertext_page = alloc_bounce_page(ctx);
 
 	while (len--) {
 		err = ext4_page_crypto(ctx, inode, EXT4_ENCRYPT, lblk,
@@ -470,6 +463,7 @@ int ext4_encrypted_zeroout(struct inode *inode, struct ext4_extent *ex)
 			goto errout;
 		}
 		err = submit_bio_wait(WRITE, bio);
+		bio_put(bio);
 		if (err)
 			goto errout;
 	}
-- 
2.3.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2015-06-01 21:57 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-05-28 23:47 [PATCH 1/8] ext4 crypto: fix memory leaks in ext4_encrypted_zeroout Theodore Ts'o
2015-05-28 23:47 ` [PATCH 2/8] ext4 crypto: set up encryption info for new inodes in ext4_inherit_context() Theodore Ts'o
2015-05-28 23:47 ` [PATCH 3/8] ext4 crypto: make sure the encryption info is initialized on opendir(2) Theodore Ts'o
2015-05-28 23:47 ` [PATCH 4/8] ext4 crypto: encrypt tmpfile located in encryption protected directory Theodore Ts'o
2015-05-29 11:09   ` Albino Biasutti Neto
2015-05-29 16:33     ` Theodore Ts'o
2015-05-30 10:45       ` Albino Biasutti Neto
2015-05-28 23:47 ` [PATCH 5/8] ext4 crypto: enforce crypto policy restrictions on cross-renames Theodore Ts'o
2015-05-28 23:47 ` [PATCH 6/8] ext4 crypto: policies may only be set on directories Theodore Ts'o
2015-05-28 23:47 ` [PATCH 7/8] ext4 crypto: clean up error handling in ext4_fname_setup_filename Theodore Ts'o
2015-05-28 23:47 ` [PATCH 8/8] ext4 crypto: allocate the right amount of memory for the on-disk symlink Theodore Ts'o
2015-05-29 18:08 ` [PATCH 1/8] ext4 crypto: fix memory leaks in ext4_encrypted_zeroout Jaegeuk Kim
2015-05-31 14:26   ` Theodore Ts'o
2015-06-01 21:56     ` Jaegeuk Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox