Skip to content

Commit 614def7

Browse files
committed
ext4 crypto: shrink size of the ext4_crypto_ctx structure
Some fields are only used when the crypto_ctx is being used on the read path, some are only used on the write path, and some are only used when the structure is on free list. Optimize memory use by using a union. Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 1aaa6e8 commit 614def7

4 files changed

Lines changed: 30 additions & 34 deletions

File tree

‎fs/ext4/crypto.c‎

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx)
7171
{
7272
unsigned long flags;
7373

74-
if (ctx->bounce_page) {
74+
if (ctx->flags & EXT4_WRITE_PATH_FL && ctx->w.bounce_page) {
7575
if (ctx->flags & EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL)
76-
__free_page(ctx->bounce_page);
76+
__free_page(ctx->w.bounce_page);
7777
else
78-
mempool_free(ctx->bounce_page, ext4_bounce_page_pool);
79-
ctx->bounce_page = NULL;
78+
mempool_free(ctx->w.bounce_page, ext4_bounce_page_pool);
8079
}
81-
ctx->control_page = NULL;
80+
ctx->w.bounce_page = NULL;
81+
ctx->w.control_page = NULL;
8282
if (ctx->flags & EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL) {
8383
if (ctx->tfm)
8484
crypto_free_tfm(ctx->tfm);
@@ -134,6 +134,7 @@ struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode)
134134
} else {
135135
ctx->flags &= ~EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
136136
}
137+
ctx->flags &= ~EXT4_WRITE_PATH_FL;
137138

138139
/* Allocate a new Crypto API context if we don't already have
139140
* one or if it isn't the right mode. */
@@ -165,10 +166,6 @@ struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode)
165166
}
166167
BUG_ON(ci->ci_size != ext4_encryption_key_size(ci->ci_data_mode));
167168

168-
/* There shouldn't be a bounce page attached to the crypto
169-
* context at this point. */
170-
BUG_ON(ctx->bounce_page);
171-
172169
out:
173170
if (res) {
174171
if (!IS_ERR_OR_NULL(ctx))
@@ -189,15 +186,6 @@ void ext4_exit_crypto(void)
189186
struct ext4_crypto_ctx *pos, *n;
190187

191188
list_for_each_entry_safe(pos, n, &ext4_free_crypto_ctxs, free_list) {
192-
if (pos->bounce_page) {
193-
if (pos->flags &
194-
EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL) {
195-
__free_page(pos->bounce_page);
196-
} else {
197-
mempool_free(pos->bounce_page,
198-
ext4_bounce_page_pool);
199-
}
200-
}
201189
if (pos->tfm)
202190
crypto_free_tfm(pos->tfm);
203191
kmem_cache_free(ext4_crypto_ctx_cachep, pos);
@@ -425,8 +413,9 @@ struct page *ext4_encrypt(struct inode *inode,
425413
} else {
426414
ctx->flags |= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
427415
}
428-
ctx->bounce_page = ciphertext_page;
429-
ctx->control_page = plaintext_page;
416+
ctx->flags |= EXT4_WRITE_PATH_FL;
417+
ctx->w.bounce_page = ciphertext_page;
418+
ctx->w.control_page = plaintext_page;
430419
err = ext4_page_crypto(ctx, inode, EXT4_ENCRYPT, plaintext_page->index,
431420
plaintext_page, ciphertext_page);
432421
if (err) {
@@ -505,7 +494,7 @@ int ext4_encrypted_zeroout(struct inode *inode, struct ext4_extent *ex)
505494
} else {
506495
ctx->flags |= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
507496
}
508-
ctx->bounce_page = ciphertext_page;
497+
ctx->w.bounce_page = ciphertext_page;
509498

510499
while (len--) {
511500
err = ext4_page_crypto(ctx, inode, EXT4_ENCRYPT, lblk,

‎fs/ext4/ext4_crypto.h‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,23 @@ struct ext4_crypt_info {
8686

8787
#define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
8888
#define EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL 0x00000002
89+
#define EXT4_WRITE_PATH_FL 0x00000004
8990

9091
struct ext4_crypto_ctx {
9192
struct crypto_tfm *tfm; /* Crypto API context */
92-
struct page *bounce_page; /* Ciphertext page on write path */
93-
struct page *control_page; /* Original page on write path */
94-
struct bio *bio; /* The bio for this context */
95-
struct work_struct work; /* Work queue for read complete path */
96-
struct list_head free_list; /* Free list */
97-
int flags; /* Flags */
98-
int mode; /* Encryption mode for tfm */
93+
union {
94+
struct {
95+
struct page *bounce_page; /* Ciphertext page */
96+
struct page *control_page; /* Original page */
97+
} w;
98+
struct {
99+
struct bio *bio;
100+
struct work_struct work;
101+
} r;
102+
struct list_head free_list; /* Free list */
103+
};
104+
char flags; /* Flags */
105+
char mode; /* Encryption mode for tfm */
99106
};
100107

101108
struct ext4_completion_result {

‎fs/ext4/page-io.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static void ext4_finish_bio(struct bio *bio)
8484
/* The bounce data pages are unmapped. */
8585
data_page = page;
8686
ctx = (struct ext4_crypto_ctx *)page_private(data_page);
87-
page = ctx->control_page;
87+
page = ctx->w.control_page;
8888
}
8989
#endif
9090

‎fs/ext4/readpage.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ static void completion_pages(struct work_struct *work)
5454
{
5555
#ifdef CONFIG_EXT4_FS_ENCRYPTION
5656
struct ext4_crypto_ctx *ctx =
57-
container_of(work, struct ext4_crypto_ctx, work);
58-
struct bio *bio = ctx->bio;
57+
container_of(work, struct ext4_crypto_ctx, r.work);
58+
struct bio *bio = ctx->r.bio;
5959
struct bio_vec *bv;
6060
int i;
6161

@@ -109,9 +109,9 @@ static void mpage_end_io(struct bio *bio, int err)
109109
if (err) {
110110
ext4_release_crypto_ctx(ctx);
111111
} else {
112-
INIT_WORK(&ctx->work, completion_pages);
113-
ctx->bio = bio;
114-
queue_work(ext4_read_workqueue, &ctx->work);
112+
INIT_WORK(&ctx->r.work, completion_pages);
113+
ctx->r.bio = bio;
114+
queue_work(ext4_read_workqueue, &ctx->r.work);
115115
return;
116116
}
117117
}

0 commit comments

Comments
 (0)