Skip to content

Commit 1565bda

Browse files
committed
fscrypt: remove struct fscrypt_ctx
Now that ext4 and f2fs implement their own post-read workflow that supports both fscrypt and fsverity, the fscrypt-only workflow based around struct fscrypt_ctx is no longer used. So remove the unused code. This is based on a patch from Chandan Rajendra's "Consolidate FS read I/O callbacks code" patchset, but rebased onto the latest kernel, folded __fscrypt_decrypt_bio() into fscrypt_decrypt_bio(), cleaned up fscrypt_initialize(), and updated the commit message. Originally-from: Chandan Rajendra <chandan@linux.ibm.com> Signed-off-by: Eric Biggers <ebiggers@google.com>
1 parent 4006d79 commit 1565bda

4 files changed

Lines changed: 10 additions & 163 deletions

File tree

‎fs/crypto/bio.c‎

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <linux/namei.h>
2727
#include "fscrypt_private.h"
2828

29-
static void __fscrypt_decrypt_bio(struct bio *bio, bool done)
29+
void fscrypt_decrypt_bio(struct bio *bio)
3030
{
3131
struct bio_vec *bv;
3232
struct bvec_iter_all iter_all;
@@ -37,37 +37,10 @@ static void __fscrypt_decrypt_bio(struct bio *bio, bool done)
3737
bv->bv_offset);
3838
if (ret)
3939
SetPageError(page);
40-
else if (done)
41-
SetPageUptodate(page);
42-
if (done)
43-
unlock_page(page);
4440
}
4541
}
46-
47-
void fscrypt_decrypt_bio(struct bio *bio)
48-
{
49-
__fscrypt_decrypt_bio(bio, false);
50-
}
5142
EXPORT_SYMBOL(fscrypt_decrypt_bio);
5243

53-
static void completion_pages(struct work_struct *work)
54-
{
55-
struct fscrypt_ctx *ctx = container_of(work, struct fscrypt_ctx, work);
56-
struct bio *bio = ctx->bio;
57-
58-
__fscrypt_decrypt_bio(bio, true);
59-
fscrypt_release_ctx(ctx);
60-
bio_put(bio);
61-
}
62-
63-
void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx, struct bio *bio)
64-
{
65-
INIT_WORK(&ctx->work, completion_pages);
66-
ctx->bio = bio;
67-
fscrypt_enqueue_decrypt_work(&ctx->work);
68-
}
69-
EXPORT_SYMBOL(fscrypt_enqueue_decrypt_bio);
70-
7144
int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
7245
sector_t pblk, unsigned int len)
7346
{

‎fs/crypto/crypto.c‎

Lines changed: 9 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,16 @@
3131
#include "fscrypt_private.h"
3232

3333
static unsigned int num_prealloc_crypto_pages = 32;
34-
static unsigned int num_prealloc_crypto_ctxs = 128;
3534

3635
module_param(num_prealloc_crypto_pages, uint, 0444);
3736
MODULE_PARM_DESC(num_prealloc_crypto_pages,
3837
"Number of crypto pages to preallocate");
39-
module_param(num_prealloc_crypto_ctxs, uint, 0444);
40-
MODULE_PARM_DESC(num_prealloc_crypto_ctxs,
41-
"Number of crypto contexts to preallocate");
4238

4339
static mempool_t *fscrypt_bounce_page_pool = NULL;
4440

45-
static LIST_HEAD(fscrypt_free_ctxs);
46-
static DEFINE_SPINLOCK(fscrypt_ctx_lock);
47-
4841
static struct workqueue_struct *fscrypt_read_workqueue;
4942
static DEFINE_MUTEX(fscrypt_init_mutex);
5043

51-
static struct kmem_cache *fscrypt_ctx_cachep;
5244
struct kmem_cache *fscrypt_info_cachep;
5345

5446
void fscrypt_enqueue_decrypt_work(struct work_struct *work)
@@ -57,62 +49,6 @@ void fscrypt_enqueue_decrypt_work(struct work_struct *work)
5749
}
5850
EXPORT_SYMBOL(fscrypt_enqueue_decrypt_work);
5951

60-
/**
61-
* fscrypt_release_ctx() - Release a decryption context
62-
* @ctx: The decryption context to release.
63-
*
64-
* If the decryption context was allocated from the pre-allocated pool, return
65-
* it to that pool. Else, free it.
66-
*/
67-
void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
68-
{
69-
unsigned long flags;
70-
71-
if (ctx->flags & FS_CTX_REQUIRES_FREE_ENCRYPT_FL) {
72-
kmem_cache_free(fscrypt_ctx_cachep, ctx);
73-
} else {
74-
spin_lock_irqsave(&fscrypt_ctx_lock, flags);
75-
list_add(&ctx->free_list, &fscrypt_free_ctxs);
76-
spin_unlock_irqrestore(&fscrypt_ctx_lock, flags);
77-
}
78-
}
79-
EXPORT_SYMBOL(fscrypt_release_ctx);
80-
81-
/**
82-
* fscrypt_get_ctx() - Get a decryption context
83-
* @gfp_flags: The gfp flag for memory allocation
84-
*
85-
* Allocate and initialize a decryption context.
86-
*
87-
* Return: A new decryption context on success; an ERR_PTR() otherwise.
88-
*/
89-
struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
90-
{
91-
struct fscrypt_ctx *ctx;
92-
unsigned long flags;
93-
94-
/*
95-
* First try getting a ctx from the free list so that we don't have to
96-
* call into the slab allocator.
97-
*/
98-
spin_lock_irqsave(&fscrypt_ctx_lock, flags);
99-
ctx = list_first_entry_or_null(&fscrypt_free_ctxs,
100-
struct fscrypt_ctx, free_list);
101-
if (ctx)
102-
list_del(&ctx->free_list);
103-
spin_unlock_irqrestore(&fscrypt_ctx_lock, flags);
104-
if (!ctx) {
105-
ctx = kmem_cache_zalloc(fscrypt_ctx_cachep, gfp_flags);
106-
if (!ctx)
107-
return ERR_PTR(-ENOMEM);
108-
ctx->flags |= FS_CTX_REQUIRES_FREE_ENCRYPT_FL;
109-
} else {
110-
ctx->flags &= ~FS_CTX_REQUIRES_FREE_ENCRYPT_FL;
111-
}
112-
return ctx;
113-
}
114-
EXPORT_SYMBOL(fscrypt_get_ctx);
115-
11652
struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags)
11753
{
11854
return mempool_alloc(fscrypt_bounce_page_pool, gfp_flags);
@@ -392,59 +328,37 @@ const struct dentry_operations fscrypt_d_ops = {
392328
.d_revalidate = fscrypt_d_revalidate,
393329
};
394330

395-
static void fscrypt_destroy(void)
396-
{
397-
struct fscrypt_ctx *pos, *n;
398-
399-
list_for_each_entry_safe(pos, n, &fscrypt_free_ctxs, free_list)
400-
kmem_cache_free(fscrypt_ctx_cachep, pos);
401-
INIT_LIST_HEAD(&fscrypt_free_ctxs);
402-
mempool_destroy(fscrypt_bounce_page_pool);
403-
fscrypt_bounce_page_pool = NULL;
404-
}
405-
406331
/**
407332
* fscrypt_initialize() - allocate major buffers for fs encryption.
408333
* @cop_flags: fscrypt operations flags
409334
*
410335
* We only call this when we start accessing encrypted files, since it
411336
* results in memory getting allocated that wouldn't otherwise be used.
412337
*
413-
* Return: Zero on success, non-zero otherwise.
338+
* Return: 0 on success; -errno on failure
414339
*/
415340
int fscrypt_initialize(unsigned int cop_flags)
416341
{
417-
int i, res = -ENOMEM;
342+
int err = 0;
418343

419344
/* No need to allocate a bounce page pool if this FS won't use it. */
420345
if (cop_flags & FS_CFLG_OWN_PAGES)
421346
return 0;
422347

423348
mutex_lock(&fscrypt_init_mutex);
424349
if (fscrypt_bounce_page_pool)
425-
goto already_initialized;
426-
427-
for (i = 0; i < num_prealloc_crypto_ctxs; i++) {
428-
struct fscrypt_ctx *ctx;
429-
430-
ctx = kmem_cache_zalloc(fscrypt_ctx_cachep, GFP_NOFS);
431-
if (!ctx)
432-
goto fail;
433-
list_add(&ctx->free_list, &fscrypt_free_ctxs);
434-
}
350+
goto out_unlock;
435351

352+
err = -ENOMEM;
436353
fscrypt_bounce_page_pool =
437354
mempool_create_page_pool(num_prealloc_crypto_pages, 0);
438355
if (!fscrypt_bounce_page_pool)
439-
goto fail;
356+
goto out_unlock;
440357

441-
already_initialized:
442-
mutex_unlock(&fscrypt_init_mutex);
443-
return 0;
444-
fail:
445-
fscrypt_destroy();
358+
err = 0;
359+
out_unlock:
446360
mutex_unlock(&fscrypt_init_mutex);
447-
return res;
361+
return err;
448362
}
449363

450364
void fscrypt_msg(const struct inode *inode, const char *level,
@@ -490,13 +404,9 @@ static int __init fscrypt_init(void)
490404
if (!fscrypt_read_workqueue)
491405
goto fail;
492406

493-
fscrypt_ctx_cachep = KMEM_CACHE(fscrypt_ctx, SLAB_RECLAIM_ACCOUNT);
494-
if (!fscrypt_ctx_cachep)
495-
goto fail_free_queue;
496-
497407
fscrypt_info_cachep = KMEM_CACHE(fscrypt_info, SLAB_RECLAIM_ACCOUNT);
498408
if (!fscrypt_info_cachep)
499-
goto fail_free_ctx;
409+
goto fail_free_queue;
500410

501411
err = fscrypt_init_keyring();
502412
if (err)
@@ -506,8 +416,6 @@ static int __init fscrypt_init(void)
506416

507417
fail_free_info:
508418
kmem_cache_destroy(fscrypt_info_cachep);
509-
fail_free_ctx:
510-
kmem_cache_destroy(fscrypt_ctx_cachep);
511419
fail_free_queue:
512420
destroy_workqueue(fscrypt_read_workqueue);
513421
fail:

‎fs/crypto/fscrypt_private.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ typedef enum {
203203
FS_ENCRYPT,
204204
} fscrypt_direction_t;
205205

206-
#define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
207-
208206
static inline bool fscrypt_valid_enc_modes(u32 contents_mode,
209207
u32 filenames_mode)
210208
{

‎include/linux/fscrypt.h‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#define FS_CRYPTO_BLOCK_SIZE 16
2222

23-
struct fscrypt_ctx;
2423
struct fscrypt_info;
2524

2625
struct fscrypt_str {
@@ -64,18 +63,6 @@ struct fscrypt_operations {
6463
unsigned int max_namelen;
6564
};
6665

67-
/* Decryption work */
68-
struct fscrypt_ctx {
69-
union {
70-
struct {
71-
struct bio *bio;
72-
struct work_struct work;
73-
};
74-
struct list_head free_list; /* Free list */
75-
};
76-
u8 flags; /* Flags */
77-
};
78-
7966
static inline bool fscrypt_has_encryption_key(const struct inode *inode)
8067
{
8168
/* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
@@ -102,8 +89,6 @@ static inline void fscrypt_handle_d_move(struct dentry *dentry)
10289

10390
/* crypto.c */
10491
extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
105-
extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t);
106-
extern void fscrypt_release_ctx(struct fscrypt_ctx *);
10792

10893
extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
10994
unsigned int len,
@@ -244,8 +229,6 @@ static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
244229

245230
/* bio.c */
246231
extern void fscrypt_decrypt_bio(struct bio *);
247-
extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
248-
struct bio *bio);
249232
extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
250233
unsigned int);
251234

@@ -295,16 +278,6 @@ static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
295278
{
296279
}
297280

298-
static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
299-
{
300-
return ERR_PTR(-EOPNOTSUPP);
301-
}
302-
303-
static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
304-
{
305-
return;
306-
}
307-
308281
static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
309282
unsigned int len,
310283
unsigned int offs,
@@ -484,11 +457,6 @@ static inline void fscrypt_decrypt_bio(struct bio *bio)
484457
{
485458
}
486459

487-
static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
488-
struct bio *bio)
489-
{
490-
}
491-
492460
static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
493461
sector_t pblk, unsigned int len)
494462
{

0 commit comments

Comments
 (0)