ext4: Further de-generalize the bio postprocessing code

Since the bio postprocessing code in fs/ext4/readpage.c is now used only
for fsverity, rename things accordingly.

Also:

- Don't create the caches at all when !CONFIG_FS_VERITY.
- Remove the unused inode argument from ext4_set_verity_work().

Link: https://patch.msgid.link/20260713023708.9245-10-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
This commit is contained in:
Eric Biggers
2026-07-12 22:37:00 -04:00
parent afcc0bcbe3
commit cc2d0cf518
3 changed files with 36 additions and 39 deletions

View File

@@ -3829,8 +3829,8 @@ static inline void ext4_set_de_type(struct super_block *sb,
/* readpages.c */
int ext4_read_folio(struct file *file, struct folio *folio);
void ext4_readahead(struct readahead_control *rac);
extern int __init ext4_init_post_read_processing(void);
extern void ext4_exit_post_read_processing(void);
int __init ext4_init_verity_caches(void);
void ext4_exit_verity_caches(void);
/* symlink.c */
extern const struct inode_operations ext4_encrypted_symlink_inode_operations;

View File

@@ -47,12 +47,12 @@
#include "ext4.h"
#include <trace/events/ext4.h>
#define NUM_PREALLOC_POST_READ_CTXS 128
#define NUM_VERITY_WORKS 128
static struct kmem_cache *bio_post_read_ctx_cache;
static mempool_t *bio_post_read_ctx_pool;
static struct kmem_cache *ext4_verity_work_cache;
static mempool_t *ext4_verity_work_pool;
struct bio_post_read_ctx {
struct ext4_verity_work {
struct bio *bio;
struct fsverity_info *vi;
struct work_struct work;
@@ -65,22 +65,22 @@ static void __read_end_io(struct bio *bio)
bio_for_each_folio_all(fi, bio)
folio_end_read(fi.folio, bio->bi_status == 0);
if (bio->bi_private)
mempool_free(bio->bi_private, bio_post_read_ctx_pool);
mempool_free(bio->bi_private, ext4_verity_work_pool);
bio_put(bio);
}
static void verity_work(struct work_struct *work)
{
struct bio_post_read_ctx *ctx =
container_of(work, struct bio_post_read_ctx, work);
struct ext4_verity_work *ctx =
container_of(work, struct ext4_verity_work, work);
struct bio *bio = ctx->bio;
struct fsverity_info *vi = ctx->vi;
/*
* Free the bio_post_read_ctx right away, since it's no longer needed.
* Free the ext4_verity_work right away, since it's no longer needed.
* This relieves the pressure on the mempool as much as possible.
*/
mempool_free(ctx, bio_post_read_ctx_pool);
mempool_free(ctx, ext4_verity_work_pool);
bio->bi_private = NULL;
fsverity_verify_bio(vi, bio);
@@ -88,12 +88,6 @@ static void verity_work(struct work_struct *work)
__read_end_io(bio);
}
static bool bio_post_read_required(struct bio *bio)
{
return IS_ENABLED(CONFIG_FS_VERITY) && bio->bi_private &&
!bio->bi_status;
}
/*
* I/O completion handler for multipage BIOs.
*
@@ -108,8 +102,9 @@ static bool bio_post_read_required(struct bio *bio)
*/
static void mpage_end_io(struct bio *bio)
{
if (bio_post_read_required(bio)) {
struct bio_post_read_ctx *ctx = bio->bi_private;
if (IS_ENABLED(CONFIG_FS_VERITY) && bio->bi_private &&
!bio->bi_status) {
struct ext4_verity_work *ctx = bio->bi_private;
INIT_WORK(&ctx->work, verity_work);
fsverity_enqueue_verify_work(&ctx->work);
@@ -118,14 +113,12 @@ static void mpage_end_io(struct bio *bio)
__read_end_io(bio);
}
static void ext4_set_bio_post_read_ctx(struct bio *bio,
const struct inode *inode,
struct fsverity_info *vi)
static void ext4_set_verity_work(struct bio *bio, struct fsverity_info *vi)
{
if (vi) {
/* Due to the mempool, this never fails. */
struct bio_post_read_ctx *ctx =
mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
struct ext4_verity_work *ctx =
mempool_alloc(ext4_verity_work_pool, GFP_NOFS);
ctx->bio = bio;
ctx->vi = vi;
@@ -289,7 +282,7 @@ static int ext4_mpage_readpages(struct inode *inode, struct fsverity_info *vi,
bio = bio_alloc(bdev, bio_max_segs(nr_pages),
REQ_OP_READ, GFP_KERNEL);
fscrypt_set_bio_crypt_ctx(bio, inode, pos, GFP_KERNEL);
ext4_set_bio_post_read_ctx(bio, inode, vi);
ext4_set_verity_work(bio, vi);
bio->bi_iter.bi_sector = first_block << (blkbits - 9);
bio->bi_end_io = mpage_end_io;
if (rac)
@@ -363,27 +356,31 @@ void ext4_readahead(struct readahead_control *rac)
ext4_mpage_readpages(inode, vi, rac, NULL);
}
int __init ext4_init_post_read_processing(void)
int __init ext4_init_verity_caches(void)
{
bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, SLAB_RECLAIM_ACCOUNT);
if (!IS_ENABLED(CONFIG_FS_VERITY))
return 0;
ext4_verity_work_cache =
KMEM_CACHE(ext4_verity_work, SLAB_RECLAIM_ACCOUNT);
if (!bio_post_read_ctx_cache)
if (!ext4_verity_work_cache)
goto fail;
bio_post_read_ctx_pool =
mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
bio_post_read_ctx_cache);
if (!bio_post_read_ctx_pool)
ext4_verity_work_pool = mempool_create_slab_pool(
NUM_VERITY_WORKS, ext4_verity_work_cache);
if (!ext4_verity_work_pool)
goto fail_free_cache;
return 0;
fail_free_cache:
kmem_cache_destroy(bio_post_read_ctx_cache);
kmem_cache_destroy(ext4_verity_work_cache);
fail:
return -ENOMEM;
}
void ext4_exit_post_read_processing(void)
void ext4_exit_verity_caches(void)
{
mempool_destroy(bio_post_read_ctx_pool);
kmem_cache_destroy(bio_post_read_ctx_cache);
if (!IS_ENABLED(CONFIG_FS_VERITY))
return;
mempool_destroy(ext4_verity_work_pool);
kmem_cache_destroy(ext4_verity_work_cache);
}

View File

@@ -7531,7 +7531,7 @@ static int __init ext4_init_fs(void)
if (err)
goto out7;
err = ext4_init_post_read_processing();
err = ext4_init_verity_caches();
if (err)
goto out6;
@@ -7580,7 +7580,7 @@ static int __init ext4_init_fs(void)
out4:
ext4_exit_pageio();
out5:
ext4_exit_post_read_processing();
ext4_exit_verity_caches();
out6:
ext4_exit_pending();
out7:
@@ -7601,7 +7601,7 @@ static void __exit ext4_exit_fs(void)
ext4_exit_sysfs();
ext4_exit_system_zone();
ext4_exit_pageio();
ext4_exit_post_read_processing();
ext4_exit_verity_caches();
ext4_exit_es();
ext4_exit_pending();
}